-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #36729 Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com>
- Loading branch information
1 parent
a956fb3
commit 4e4deca
Showing
6 changed files
with
237 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const { randomUUID } = require('crypto'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e7], | ||
disableEntropyCache: [0, 1], | ||
}); | ||
|
||
function main({ n, disableEntropyCache }) { | ||
disableEntropyCache = !!disableEntropyCache; | ||
bench.start(); | ||
for (let i = 0; i < n; ++i) | ||
randomUUID({ disableEntropyCache }); | ||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const { | ||
randomUUID, | ||
} = require('crypto'); | ||
|
||
const last = new Set([ | ||
'00000000-0000-0000-0000-000000000000' | ||
]); | ||
|
||
function testMatch(uuid) { | ||
assert.match( | ||
uuid, | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); | ||
} | ||
|
||
// Generate a number of UUID's to make sure we're | ||
// not just generating the same value over and over | ||
// and to make sure the batching changes the random | ||
// bytes. | ||
for (let n = 0; n < 130; n++) { | ||
const uuid = randomUUID(); | ||
assert(!last.has(uuid)); | ||
last.add(uuid); | ||
assert.strictEqual(typeof uuid, 'string'); | ||
assert.strictEqual(uuid.length, 36); | ||
testMatch(uuid); | ||
|
||
// Check that version 4 identifier was populated. | ||
assert.strictEqual( | ||
Buffer.from(uuid.substr(14, 2), 'hex')[0] & 0x40, 0x40); | ||
|
||
// Check that clock_seq_hi_and_reserved was populated with reserved bits. | ||
assert.strictEqual( | ||
Buffer.from(uuid.substr(19, 2), 'hex')[0] & 0b1100_0000, 0b1000_0000); | ||
} | ||
|
||
// Test non-buffered UUID's | ||
{ | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
testMatch(randomUUID({ disableEntropyCache: true })); | ||
|
||
assert.throws(() => randomUUID(1), { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
|
||
assert.throws(() => randomUUID({ disableEntropyCache: '' }), { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
} |