tor-browser

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

FileSystemFileHandle-sync-access-handle-lock-modes.https.tentative.worker.js (4287B)


      1 importScripts('/resources/testharness.js');
      2 importScripts('resources/sandboxed-fs-test-helpers.js');
      3 importScripts('resources/test-helpers.js');
      4 
      5 'use strict';
      6 
      7 const LOCK_WRITE_PERMISSION = {
      8  NOT_WRITABLE: 'not writable',
      9  WRITABLE: 'writable',
     10 };
     11 
     12 async function testLockWritePermission(t, fileHandle, createSAHLock) {
     13  const syncHandle = await createSAHLock(t, fileHandle);
     14 
     15  let permission;
     16  const writeBuffer = new TextEncoder().encode('Hello Storage Foundation');
     17  try {
     18    syncHandle.write(writeBuffer, {at: 0});
     19    permission = LOCK_WRITE_PERMISSION.WRITABLE;
     20  } catch (e) {
     21    permission = LOCK_WRITE_PERMISSION.NOT_WRITABLE;
     22    assert_throws_dom('NoModificationAllowedError', () => {
     23      throw e;
     24    });
     25  }
     26  // truncate and flush should throw a NoModificationAllowedError if an only if
     27  // write threw a NoModificationAllowedError.
     28  if (permission == LOCK_WRITE_PERMISSION.WRITABLE) {
     29    syncHandle.truncate(0);
     30    syncHandle.flush();
     31  } else {
     32    assert_throws_dom(
     33        'NoModificationAllowedError', () => syncHandle.truncate(0));
     34    assert_throws_dom('NoModificationAllowedError', () => syncHandle.flush());
     35  }
     36 
     37  return permission;
     38 }
     39 
     40 // Adds tests for expected behaviors of an access handle created in `sahMode`
     41 // mode.
     42 function lockPropertyTests(
     43    sahMode, expectedLockAccess, expectedLockWritePermission) {
     44  const createSAHLock = createSAHWithCleanupFactory({mode: sahMode});
     45 
     46  directory_test(async (t, rootDir) => {
     47    const [fileHandle] = await createFileHandles(rootDir, 'BFS.test');
     48 
     49    const {mode} = await createSAHLock(t, fileHandle);
     50    assert_equals(mode, sahMode);
     51  }, `An access handle in ${sahMode} mode has a mode property equal to` +
     52    ` ${sahMode}`);
     53 
     54  directory_test(async (t, rootDir) => {
     55    const [fileHandle] = await createFileHandles(rootDir, 'BFS.test');
     56    assert_equals(
     57        await testLockAccess(t, fileHandle, createSAHLock), expectedLockAccess);
     58  }, `An access handle in ${sahMode} mode takes a lock that is` +
     59    ` ${expectedLockAccess}`);
     60 
     61  directory_test(async (t, rootDir) => {
     62    const [fileHandle] = await createFileHandles(rootDir, 'BFS.test');
     63    assert_equals(
     64        await testLockWritePermission(t, fileHandle, createSAHLock),
     65        expectedLockWritePermission);
     66  }, `An access handle in ${sahMode} mode is ${expectedLockWritePermission}`);
     67 
     68  // Test interaction with other access handle modes.
     69  for (const mode of SAH_MODES) {
     70    // Add tests depending on which access handle modes are being tested against
     71    // each other.
     72    const testingAgainstSelf = mode === sahMode;
     73    const testingExclusiveLock = expectedLockAccess === 'exclusive';
     74    const tests = {
     75      diffFile: `When there's an open access handle in ${sahMode} mode on a` +
     76          ` file, can open another access handle in ${mode} on a different` +
     77          ` file`,
     78    };
     79    if (!testingAgainstSelf || testingExclusiveLock) {
     80      tests.sameFile = `When there's an open access handle in ${sahMode} mode` +
     81          ` on a file, cannot open another access handle in ${mode} on that` +
     82          ` same file`;
     83    }
     84    if (testingExclusiveLock) {
     85      tests.acquireAfterRelease = `After an access handle in ${sahMode} mode` +
     86          ` on a file has been closed, can open another access handle in` +
     87          ` ${mode} on the same file`;
     88    }
     89    if (!testingExclusiveLock && !testingAgainstSelf) {
     90      tests.multiAcquireAfterRelease = `After all access handles in` +
     91          ` ${sahMode} mode on a file has been closed, can open another` +
     92          ` access handle in ${mode} on the same file`;
     93    }
     94 
     95    generateCrossLockTests(
     96        createSAHLock, createSAHWithCleanupFactory({mode: mode}), tests);
     97  }
     98 }
     99 
    100 directory_test(async (t, rootDir) => {
    101  const [fileHandle] = await createFileHandles(rootDir, 'BFS.test');
    102 
    103  const syncHandle = await createSAHWithCleanup(t, fileHandle);
    104  assert_equals(syncHandle.mode, 'readwrite');
    105 }, 'A sync access handle opens in readwrite mode by default');
    106 
    107 lockPropertyTests(
    108    'readwrite', LOCK_ACCESS.EXCLUSIVE, LOCK_WRITE_PERMISSION.WRITABLE);
    109 lockPropertyTests(
    110    'read-only', LOCK_ACCESS.SHARED, LOCK_WRITE_PERMISSION.NOT_WRITABLE);
    111 lockPropertyTests(
    112    'readwrite-unsafe', LOCK_ACCESS.SHARED, LOCK_WRITE_PERMISSION.WRITABLE);
    113 
    114 done();