resource-names.https.any.js (1721B)
1 // META: title=Web Locks API: Resources DOMString edge cases 2 // META: global=window,dedicatedworker,sharedworker,serviceworker 3 4 'use strict'; 5 6 function code_points(s) { 7 return [...s] 8 .map(c => '0x' + c.charCodeAt(0).toString(16).toUpperCase()) 9 .join(' '); 10 } 11 12 [ 13 '', // Empty strings 14 'abc\x00def', // Embedded NUL 15 '\uD800', // Unpaired low surrogage 16 '\uDC00', // Unpaired high surrogage 17 '\uDC00\uD800', // Swapped surrogate pair 18 '\uFFFF' // Non-character 19 ].forEach(string => { 20 promise_test(async t => { 21 await navigator.locks.request(string, lock => { 22 assert_equals(lock.name, string, 23 'Requested name matches granted name'); 24 }); 25 }, 'DOMString: ' + code_points(string)); 26 }); 27 28 promise_test(async t => { 29 // '\uD800' treated as a USVString would become '\uFFFD'. 30 await navigator.locks.request('\uD800', async lock => { 31 assert_equals(lock.name, '\uD800'); 32 33 // |lock| is held for the duration of this name. It 34 // Should not block acquiring |lock2| with a distinct 35 // DOMString. 36 await navigator.locks.request('\uFFFD', lock2 => { 37 assert_equals(lock2.name, '\uFFFD'); 38 }); 39 40 // If we did not time out, this passed. 41 }); 42 }, 'Resource names that are not valid UTF-16 are not mangled'); 43 44 promise_test(async t => { 45 for (const name of ['-', '-foo']) { 46 await promise_rejects_dom( 47 t, 'NotSupportedError', 48 navigator.locks.request(name, lock => {}), 49 'Names starting with "-" should be rejected'); 50 } 51 let got_lock = false; 52 await navigator.locks.request('x-anything', lock => { 53 got_lock = true; 54 }); 55 assert_true(got_lock, 'Names with embedded "-" should be accepted'); 56 }, 'Names cannot start with "-"');