tor-browser

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

FileSystemFileHandle-writable-file-stream-lock-modes.https.tentative.worker.js (2755B)


      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 // Adds tests for expected behaviors of a writable stream created in `wfsMode`
      8 // mode.
      9 function lockPropertyTests(wfsMode, expectedLockAccess) {
     10  const createWFSLock = createWFSWithCleanupFactory({mode: wfsMode});
     11 
     12  directory_test(async (t, rootDir) => {
     13    const [fileHandle] = await createFileHandles(rootDir, 'BFS.test');
     14 
     15    const {mode} = await createWFSLock(t, fileHandle);
     16    assert_equals(mode, wfsMode);
     17  }, `A writable stream in ${wfsMode} mode has a mode property equal to` +
     18    ` ${wfsMode}`);
     19 
     20  directory_test(async (t, rootDir) => {
     21    const [fileHandle] = await createFileHandles(rootDir, 'BFS.test');
     22    assert_equals(
     23        await testLockAccess(t, fileHandle, createWFSLock), expectedLockAccess);
     24  }, `A writable stream in ${wfsMode} mode takes a lock that is` +
     25    ` ${expectedLockAccess}`);
     26 
     27  // Test interaction with other writable stream modes.
     28  for (const mode of WFS_MODES) {
     29    // Add tests depending on which writable stream modes are being tested
     30    // against each other.
     31    const testingAgainstSelf = mode === wfsMode;
     32    const testingExclusiveLock = expectedLockAccess === 'exclusive';
     33    const tests = {
     34      diffFile: `When there's an open writable stream in ${wfsMode} mode on a` +
     35          ` file, can open another writable stream in ${mode} on a different` +
     36          ` file`,
     37    };
     38    if (!testingAgainstSelf || testingExclusiveLock) {
     39      tests.sameFile = `When there's an open writable stream in ${wfsMode}` +
     40          ` mode on a file, cannot open another writable stream in ${mode} on` +
     41          ` that same file`;
     42    }
     43    if (testingExclusiveLock) {
     44      tests.acquireAfterRelease = `After a writable stream in ${wfsMode} mode` +
     45          ` on a file has been closed, can open another writable stream in` +
     46          ` ${mode} on the same file`;
     47    }
     48    if (!testingExclusiveLock && !testingAgainstSelf) {
     49      tests.multiAcquireAfterRelease = `After all writable streams in` +
     50          ` ${wfsMode} mode on a file has been closed, can open another` +
     51          ` writable stream in ${mode} on the same file`;
     52    }
     53 
     54    generateCrossLockTests(
     55        createWFSLock, createWFSWithCleanupFactory({mode: mode}), tests);
     56  }
     57 }
     58 
     59 directory_test(async (t, rootDir) => {
     60  const [fileHandle] = await createFileHandles(rootDir, 'BFS.test');
     61 
     62  const syncHandle = await createWFSWithCleanup(t, fileHandle);
     63  assert_equals(syncHandle.mode, 'siloed');
     64 }, 'A writable stream opens in siloed mode by default');
     65 
     66 lockPropertyTests('siloed', LOCK_ACCESS.SHARED);
     67 lockPropertyTests('exclusive', LOCK_ACCESS.EXCLUSIVE);
     68 
     69 done();