11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciif (!common.hasCrypto) 61cb0ef41Sopenharmony_ci common.skip('missing crypto'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst assert = require('assert'); 91cb0ef41Sopenharmony_ciconst { 101cb0ef41Sopenharmony_ci randomUUID, 111cb0ef41Sopenharmony_ci} = require('crypto'); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst last = new Set([ 141cb0ef41Sopenharmony_ci '00000000-0000-0000-0000-000000000000', 151cb0ef41Sopenharmony_ci]); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cifunction testMatch(uuid) { 181cb0ef41Sopenharmony_ci assert.match( 191cb0ef41Sopenharmony_ci uuid, 201cb0ef41Sopenharmony_ci /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); 211cb0ef41Sopenharmony_ci} 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci// Generate a number of UUID's to make sure we're 241cb0ef41Sopenharmony_ci// not just generating the same value over and over 251cb0ef41Sopenharmony_ci// and to make sure the batching changes the random 261cb0ef41Sopenharmony_ci// bytes. 271cb0ef41Sopenharmony_cifor (let n = 0; n < 130; n++) { 281cb0ef41Sopenharmony_ci const uuid = randomUUID(); 291cb0ef41Sopenharmony_ci assert(!last.has(uuid)); 301cb0ef41Sopenharmony_ci last.add(uuid); 311cb0ef41Sopenharmony_ci assert.strictEqual(typeof uuid, 'string'); 321cb0ef41Sopenharmony_ci assert.strictEqual(uuid.length, 36); 331cb0ef41Sopenharmony_ci testMatch(uuid); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci // Check that version 4 identifier was populated. 361cb0ef41Sopenharmony_ci assert.strictEqual( 371cb0ef41Sopenharmony_ci Buffer.from(uuid.substr(14, 2), 'hex')[0] & 0x40, 0x40); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci // Check that clock_seq_hi_and_reserved was populated with reserved bits. 401cb0ef41Sopenharmony_ci assert.strictEqual( 411cb0ef41Sopenharmony_ci Buffer.from(uuid.substr(19, 2), 'hex')[0] & 0b1100_0000, 0b1000_0000); 421cb0ef41Sopenharmony_ci} 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci// Test non-buffered UUID's 451cb0ef41Sopenharmony_ci{ 461cb0ef41Sopenharmony_ci testMatch(randomUUID({ disableEntropyCache: true })); 471cb0ef41Sopenharmony_ci testMatch(randomUUID({ disableEntropyCache: true })); 481cb0ef41Sopenharmony_ci testMatch(randomUUID({ disableEntropyCache: true })); 491cb0ef41Sopenharmony_ci testMatch(randomUUID({ disableEntropyCache: true })); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci assert.throws(() => randomUUID(1), { 521cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE' 531cb0ef41Sopenharmony_ci }); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci assert.throws(() => randomUUID({ disableEntropyCache: '' }), { 561cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE' 571cb0ef41Sopenharmony_ci }); 581cb0ef41Sopenharmony_ci} 59