tor-browser

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

test_syncAccessHandle.js (7917B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const allowCreate = { create: true };
      5 
      6 exported_symbols.test0 = async function () {
      7  let root = await navigator.storage.getDirectory();
      8  Assert.ok(root, "Can we access the root directory?");
      9 
     10  try {
     11    await root.getFileHandle("test.txt");
     12    Assert.ok(false, "Opened file that shouldn't exist");
     13  } catch (e) {
     14    dump("caught exception when we tried to open a non-existant file\n");
     15  }
     16 };
     17 
     18 exported_symbols.test1 = async function () {
     19  let root = await navigator.storage.getDirectory();
     20  Assert.ok(root, "Can we access the root directory?");
     21 
     22  const testFile = await root.getFileHandle("test.txt", allowCreate);
     23  Assert.ok(!!testFile, "Can't create file");
     24  let handle = await testFile.createSyncAccessHandle();
     25  Assert.ok(!!handle, "Can't create SyncAccessHandle");
     26  await handle.close();
     27  handle = await testFile.createSyncAccessHandle();
     28  Assert.ok(!!handle, "Can't create second SyncAccessHandle to same file");
     29  await handle.close();
     30 };
     31 
     32 exported_symbols.test2 = async function () {
     33  let root = await navigator.storage.getDirectory();
     34  Assert.ok(root, "Can we access the root directory?");
     35 
     36  const testFile = await root.getFileHandle("test.txt", allowCreate);
     37  Assert.ok(!!testFile, "Can't open file");
     38  let handle = await testFile.createSyncAccessHandle();
     39  Assert.ok(!!handle, "Can't create SyncAccessHandle");
     40  await handle.close();
     41 
     42  await root.removeEntry("test.txt");
     43  try {
     44    handle = await testFile.createSyncAccessHandle();
     45    Assert.ok(!!handle, "Didn't remove file!");
     46    if (handle) {
     47      await handle.close();
     48    }
     49  } catch (e) {
     50    dump("Caught exception trying to create accesshandle to deleted file\n");
     51  }
     52 };
     53 
     54 exported_symbols.test3 = async function () {
     55  let root = await navigator.storage.getDirectory();
     56  Assert.ok(!!root, "Can we access the root directory?");
     57 
     58  let dir = await root.getDirectoryHandle("dir", allowCreate);
     59  Assert.ok(!!dir, "Can we create a directory?");
     60 
     61  // XXX not implemented yet
     62  //const path = await root.resolve(dir);
     63  //Assert.ok(path == ["dir"], "Wrong path: " + path);
     64 
     65  let dir2 = await dir.getDirectoryHandle("dir", allowCreate);
     66  Assert.ok(!!dir, "Can we create dir/dir?");
     67 
     68  // XXX not implemented yet
     69  //const path = await root.resolve(dir2);
     70  //Assert.ok(path == ["dir", "dir"], "Wrong path: " + path);
     71 
     72  let dir3 = await dir.getDirectoryHandle("bar", allowCreate);
     73  Assert.ok(!!dir3, "Can we create dir/bar?");
     74 
     75  // This should fail
     76  try {
     77    await root.getDirectoryHandle("bar");
     78    Assert.ok(!dir, "we shouldn't be able to get bar unless we create it");
     79  } catch (e) {
     80    dump("caught exception when we tried to get a non-existant dir\n");
     81  }
     82 
     83  const testFile = await dir2.getFileHandle("test.txt", allowCreate);
     84  Assert.ok(!!testFile, "Can't create file in dir2");
     85  let handle = await testFile.createSyncAccessHandle();
     86  Assert.ok(!!handle, "Can't create SyncAccessHandle in dir2");
     87  await handle.close();
     88 };
     89 
     90 exported_symbols.test4 = async function () {
     91  let root = await navigator.storage.getDirectory();
     92  Assert.ok(!!root, "Can we access the root directory?");
     93 
     94  const testFile = await root.getFileHandle("test.txt", allowCreate);
     95  Assert.ok(!!testFile, "Can't access existing file");
     96  let handle = await testFile.createSyncAccessHandle();
     97  Assert.ok(!!handle, "Can't create SyncAccessHandle to existing file");
     98 
     99  // Write a sentence to the end of the file.
    100  const encoder = new TextEncoder();
    101  const writeBuffer = encoder.encode("Thank you for reading this.");
    102  const writeSize = handle.write(writeBuffer);
    103  Assert.ok(!!writeSize);
    104 
    105  // Read it back
    106  // Get size of the file.
    107  let fileSize = await handle.getSize();
    108  Assert.ok(fileSize == writeBuffer.byteLength);
    109  // Read file content to a buffer.
    110  const readBuffer = new ArrayBuffer(fileSize);
    111  const readSize = handle.read(readBuffer, { at: 0 });
    112  Assert.ok(!!readSize);
    113  //Assert.ok(readBuffer == writeBuffer);
    114 
    115  await handle.truncate(5);
    116  fileSize = await handle.getSize();
    117  Assert.ok(fileSize == 5);
    118 
    119  await handle.flush();
    120  await handle.close();
    121 };
    122 
    123 exported_symbols.test5 = async function () {
    124  let root = await navigator.storage.getDirectory();
    125  Assert.ok(!!root, "Can we access the root directory?");
    126 
    127  const testFile = await root.getFileHandle("test.txt", allowCreate);
    128  Assert.ok(!!testFile, "Can't create file");
    129  let handle = await testFile.createSyncAccessHandle();
    130  Assert.ok(!!handle, "Can't create SyncAccessHandle");
    131 
    132  try {
    133    const testFile2 = await root.getFileHandle("test2.txt", allowCreate);
    134    let handle2 = await testFile2.createSyncAccessHandle();
    135    Assert.ok(!!handle2, "can't create SyncAccessHandle to second file!");
    136    if (handle2) {
    137      await handle2.close();
    138    }
    139  } catch (e) {
    140    Assert.ok(false, "Failed to create second file");
    141  }
    142 
    143  await handle.close();
    144 };
    145 
    146 exported_symbols.test6 = async function () {
    147  let root = await navigator.storage.getDirectory();
    148  Assert.ok(root, "Can we access the root directory?");
    149 
    150  const testFile = await root.getFileHandle("test.txt", allowCreate);
    151  Assert.ok(!!testFile, "Can't get file");
    152  let handle = await testFile.createSyncAccessHandle();
    153  Assert.ok(!!handle, "Can't create SyncAccessHandle");
    154 
    155  try {
    156    let handle2 = await testFile.createSyncAccessHandle();
    157    Assert.ok(!handle2, "Shouldn't create SyncAccessHandle!");
    158    if (handle2) {
    159      await handle2.close();
    160    }
    161  } catch (e) {
    162    // should always happen
    163    dump("caught exception when we tried to get 2 SyncAccessHandles\n");
    164  }
    165 
    166  // test that locks work across multiple connections for an origin
    167  try {
    168    let root2 = await navigator.storage.getDirectory();
    169    Assert.ok(root2, "Can we access the root2 directory?");
    170 
    171    const testFile2 = await root2.getFileHandle("test.txt");
    172    Assert.ok(!!testFile2, "Can't get file");
    173    let handle2 = await testFile2.createSyncAccessHandle();
    174    Assert.ok(!handle2, "Shouldn't create SyncAccessHandle (2)!");
    175    if (handle2) {
    176      await handle2.close();
    177    }
    178  } catch (e) {
    179    // should always happen
    180    dump("caught exception when we tried to get 2 SyncAccessHandles\n");
    181  }
    182 
    183  if (handle) {
    184    await handle.close();
    185  }
    186 };
    187 
    188 exported_symbols.quotaTest = async function () {
    189  const shrinkedStorageSizeKB = 5 * 1024;
    190  const defaultDatabaseSize = 491520;
    191 
    192  // Shrink storage size to 5MB.
    193  await Utils.shrinkStorageSize(shrinkedStorageSizeKB);
    194 
    195  let root = await navigator.storage.getDirectory();
    196  Assert.ok(root, "Can we access the root directory?");
    197 
    198  // Fill entire storage.
    199  const fileHandle = await root.getFileHandle("test.txt", allowCreate);
    200  Assert.ok(!!fileHandle, "Can we get file handle?");
    201 
    202  const accessHandle = await fileHandle.createSyncAccessHandle();
    203  Assert.ok(!!accessHandle, "Can we create sync access handle?");
    204 
    205  const buffer = new ArrayBuffer(
    206    shrinkedStorageSizeKB * 1024 - defaultDatabaseSize
    207  );
    208  Assert.ok(!!buffer, "Can we create array buffer?");
    209 
    210  const written = accessHandle.write(buffer);
    211  Assert.equal(written, buffer.byteLength, "Can we write entire buffer?");
    212 
    213  // Try to write one more byte.
    214  const fileHandle2 = await root.getFileHandle("test2.txt", allowCreate);
    215  Assert.ok(!!fileHandle2, "Can we get file handle?");
    216 
    217  const accessHandle2 = await fileHandle2.createSyncAccessHandle();
    218  Assert.ok(!!accessHandle2, "Can we create sync access handle?");
    219 
    220  const buffer2 = new ArrayBuffer(1);
    221  Assert.ok(!!buffer2, "Can we create array buffer?");
    222 
    223  const written2 = accessHandle2.write(buffer2);
    224  Assert.equal(written2, 0, "Can we write beyond the limit?");
    225 
    226  await accessHandle.close();
    227  await accessHandle2.close();
    228 
    229  await Utils.restoreStorageSize();
    230 };
    231 
    232 for (const [key, value] of Object.entries(exported_symbols)) {
    233  Object.defineProperty(value, "name", {
    234    value: key,
    235    writable: false,
    236  });
    237 }