tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

randomUUID.https.any.js (1416B)


      1 // Run for enough iterations that we're likely to catch edge-cases, like
      2 // failing to set a reserved bit:
      3 const iterations = 256;
      4 // Track all the UUIDs generated during test run, bail if we ever collide:
      5 const uuids = new Set()
      6 function randomUUID() {
      7    const uuid = self.crypto.randomUUID();
      8    if (uuids.has(uuid)) {
      9        throw new Error(`uuid collision ${uuid}`)
     10    }
     11    uuids.add(uuid);
     12    return uuid;
     13 }
     14 
     15 // UUID is in namespace format (16 bytes separated by dashes):
     16 test(function() {
     17    const UUIDRegex = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/
     18    for (let i = 0; i < iterations; i++) {
     19        assert_true(UUIDRegex.test(randomUUID()));
     20    }
     21 }, "namespace format");
     22 
     23 // Set the 4 most significant bits of array[6], which represent the UUID
     24 // version, to 0b0100:
     25 test(function() {
     26    for (let i = 0; i < iterations; i++) {
     27        let value = parseInt(randomUUID().split('-')[2].slice(0, 2), 16);
     28        value &= 0b11110000;
     29        assert_true(value === 0b01000000);
     30    }
     31 }, "version set");
     32 
     33 // Set the 2 most significant bits of array[8], which represent the UUID
     34 // variant, to 0b10:
     35 test(function() {
     36    for (let i = 0; i < iterations; i++) {
     37        // Grab the byte representing array[8]:
     38        let value = parseInt(randomUUID().split('-')[3].slice(0, 2), 16);
     39        value &= 0b11000000
     40        assert_true(value === 0b10000000);
     41    }
     42 }, "variant set");