tor-browser

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

web-locks-header-batch-update.tentative.https.sub.html (3439B)


      1 <!doctype html>
      2 <script src="/resources/testharness.js"></script>
      3 <script src="/resources/testharnessreport.js"></script>
      4 <script src="/common/utils.js"></script>
      5 <script src="/shared-storage/resources/util.js"></script>
      6 <script src="/fenced-frame/resources/utils.js"></script>
      7 
      8 <body>
      9 <script>
     10 'use strict';
     11 
     12 promise_test(async t => {
     13  const worklet = await sharedStorage.createWorklet('resources/simple-module.js');
     14 
     15  const ancestor_key = token();
     16  const url0 = generateURL("/shared-storage/resources/frame0.html",
     17                           [ancestor_key]);
     18  const url1 = generateURL("/shared-storage/resources/frame1.html",
     19                           [ancestor_key]);
     20 
     21  // Invoke `selectURL()` to perform the following steps:
     22  // 1. Acquires the lock.
     23  // 2. Reads the current value at the given key.
     24  // 3. Waits for 500ms.
     25  // 4. Sets the shared storage value to the read value appended with the given letter.
     26  // 5. Releases the lock.
     27  //
     28  // After 100ms, send a fetch() request. The response sends the
     29  // `Shared-Storage-Write` header that:
     30  // - Acquires the same named lock via the batch `options`
     31  // - Executes two `append` methods, each appending the same letter.
     32  //
     33  // Expected behavior: After both of them finish, the value at the given key
     34  // should contain the letter repeated three times.
     35  //
     36  // This demonstrates that:
     37  // 1. The `withLock` option is effective, preventing the header batch update
     38  //    interfering with the "get and set" operation. If the lock were not used,
     39  //    the final value would likely be a single letter.
     40  // 2. The header batch update correctly executes all `append` methods within
     41  //    the batch.
     42  //
     43  // Note: This test remains valid even if the header batch update happens
     44  // outside the critical section protected by the lock within the worklet. The
     45  // test effectively demonstrates mutual exclusion as long as there's a
     46  // reasonable chance for the header batch update to occur while the worklet is
     47  // still running.
     48  const select_url_result = await worklet.selectURL(
     49      "get-wait-set-within-lock",
     50      [{url: url0}, {url: url1}],
     51      {data: {'key': 'key',
     52              'lock_name': 'lock1',
     53              'append_letter': 'a'},
     54      resolveToConfig: true});
     55 
     56  // Busy wait for 100ms.
     57  const startWaitTime = Date.now();
     58  while (Date.now() - startWaitTime < 100) {}
     59 
     60  // After 100ms, send a fetch() request. The response sends the
     61  // `Shared-Storage-Write` header that:
     62  // - Acquires the same named lock via the batch `options`
     63  // - Executes two `append` methods, each appending the same letter.
     64  const rawUpdatesHeader = 'append;key=key;value=a, append;key=key;value=a, options;with_lock=lock1';
     65  const updatesHeader = encodeURIComponent(rawUpdatesHeader);
     66  const updatesUrl =
     67    `/shared-storage/resources/shared-storage-write.py?write=${updatesHeader}`;
     68 
     69  const response = await fetch(updatesUrl, {sharedStorageWritable: true});
     70  const sharedStorageWritableHeader = await response.text();
     71  assert_equals(sharedStorageWritableHeader, "?1");
     72 
     73  attachFencedFrame(select_url_result, 'opaque-ads');
     74  const result = await nextValueFromServer(ancestor_key);
     75  assert_equals(result, "frame1_loaded");
     76 
     77  await verifyKeyValueForOrigin('key', 'aaa', location.origin);
     78 
     79  await deleteKeyForOrigin('key', location.origin);
     80 }, 'Test for batch withLock option in the Shared-Storage-Write response header');
     81 
     82 </script>
     83 </body>