diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /testing/web-platform/tests/WebCryptoAPI/randomUUID.https.any.js | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/WebCryptoAPI/randomUUID.https.any.js')
-rw-r--r-- | testing/web-platform/tests/WebCryptoAPI/randomUUID.https.any.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/testing/web-platform/tests/WebCryptoAPI/randomUUID.https.any.js b/testing/web-platform/tests/WebCryptoAPI/randomUUID.https.any.js new file mode 100644 index 0000000000..600a9728f0 --- /dev/null +++ b/testing/web-platform/tests/WebCryptoAPI/randomUUID.https.any.js @@ -0,0 +1,42 @@ +// Run for enough iterations that we're likely to catch edge-cases, like +// failing to set a reserved bit: +const iterations = 256; +// Track all the UUIDs generated during test run, bail if we ever collide: +const uuids = new Set() +function randomUUID() { + const uuid = self.crypto.randomUUID(); + if (uuids.has(uuid)) { + throw new Error(`uuid collision ${uuid}`) + } + uuids.add(uuid); + return uuid; +} + +// UUID is in namespace format (16 bytes separated by dashes): +test(function() { + const UUIDRegex = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/ + for (let i = 0; i < iterations; i++) { + assert_true(UUIDRegex.test(randomUUID())); + } +}, "namespace format"); + +// Set the 4 most significant bits of array[6], which represent the UUID +// version, to 0b0100: +test(function() { + for (let i = 0; i < iterations; i++) { + let value = parseInt(randomUUID().split('-')[2].slice(0, 2), 16); + value &= 0b11110000; + assert_true(value === 0b01000000); + } +}, "version set"); + +// Set the 2 most significant bits of array[8], which represent the UUID +// variant, to 0b10: +test(function() { + for (let i = 0; i < iterations; i++) { + // Grab the byte representing array[8]: + let value = parseInt(randomUUID().split('-')[3].slice(0, 2), 16); + value &= 0b11000000 + assert_true(value === 0b10000000); + } +}, "variant set"); |