tor-browser

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

test_basics.js (10354B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 // This test must be first, since we need the actor not to be created already.
      7 exported_symbols.testGetDirectoryTwice = async function () {
      8  const promise1 = navigator.storage.getDirectory();
      9  const promise2 = navigator.storage.getDirectory();
     10 
     11  await Promise.all([promise1, promise2]);
     12 
     13  Assert.ok(true, "Should not have thrown");
     14 };
     15 
     16 exported_symbols.testGetDirectoryDoesNotThrow = async function () {
     17  await navigator.storage.getDirectory();
     18 
     19  Assert.ok(true, "Should not have thrown");
     20 };
     21 
     22 exported_symbols.testGetDirectoryKindIsDirectory = async function () {
     23  const root = await navigator.storage.getDirectory();
     24 
     25  Assert.equal(root.kind, "directory");
     26 };
     27 
     28 exported_symbols.testDirectoryHandleStringConversion = async function () {
     29  const root = await navigator.storage.getDirectory();
     30 
     31  Assert.equal(
     32    "" + root,
     33    "[object FileSystemDirectoryHandle]",
     34    "Is directoryHandle convertible to string?"
     35  );
     36 };
     37 
     38 exported_symbols.testNewDirectoryHandleFromPrototype = async function () {
     39  const root = await navigator.storage.getDirectory();
     40 
     41  try {
     42    Object.create(root.prototype);
     43    Assert.ok(false, "Should have thrown");
     44  } catch (ex) {
     45    Assert.ok(true, "Should have thrown");
     46    Assert.ok(ex instanceof TypeError, "Threw the right error type");
     47  }
     48 };
     49 
     50 exported_symbols.testIsSameEntryRoot = async function () {
     51  const root = await navigator.storage.getDirectory();
     52  try {
     53    await root.move(root);
     54    Assert.ok(false, "root should not be movable");
     55  } catch (ex) {
     56    Assert.ok(true, "root isn't movable");
     57  }
     58 };
     59 
     60 exported_symbols.testDirectoryHandleSupportsKeysIterator = async function () {
     61  const root = await navigator.storage.getDirectory();
     62 
     63  const it = await root.keys();
     64  Assert.ok(!!it, "Does root support keys iterator?");
     65 };
     66 
     67 exported_symbols.testKeysIteratorNextIsCallable = async function () {
     68  const root = await navigator.storage.getDirectory();
     69 
     70  const it = await root.keys();
     71  Assert.ok(!!it, "Does root support keys iterator?");
     72 
     73  const item = await it.next();
     74  Assert.ok(!!item, "Should return an item");
     75 };
     76 
     77 exported_symbols.testDirectoryHandleSupportsValuesIterator = async function () {
     78  const root = await navigator.storage.getDirectory();
     79 
     80  const it = await root.values();
     81  Assert.ok(!!it, "Does root support values iterator?");
     82 };
     83 
     84 exported_symbols.testValuesIteratorNextIsCallable = async function () {
     85  const root = await navigator.storage.getDirectory();
     86 
     87  const it = await root.values();
     88  Assert.ok(!!it, "Does root support values iterator?");
     89 
     90  const item = await it.next();
     91  Assert.ok(!!item, "Should return an item");
     92 };
     93 
     94 exported_symbols.testDirectoryHandleSupportsEntriesIterator =
     95  async function () {
     96    const root = await navigator.storage.getDirectory();
     97 
     98    const it = await root.entries();
     99    Assert.ok(!!it, "Does root support entries iterator?");
    100  };
    101 
    102 exported_symbols.testEntriesIteratorNextIsCallable = async function () {
    103  const root = await navigator.storage.getDirectory();
    104 
    105  const it = await root.entries();
    106  Assert.ok(!!it, "Does root support entries iterator?");
    107 
    108  const item = await it.next();
    109  Assert.ok(!!item, "Should return an item");
    110 };
    111 
    112 exported_symbols.testGetFileHandleIsCallable = async function () {
    113  const root = await navigator.storage.getDirectory();
    114  const allowCreate = { create: true };
    115 
    116  const item = await root.getFileHandle("fileName", allowCreate);
    117  Assert.ok(!!item, "Should return an item");
    118 
    119  await root.removeEntry("fileName");
    120 };
    121 
    122 exported_symbols.testGetDirectoryHandleIsCallable = async function () {
    123  const root = await navigator.storage.getDirectory();
    124  const allowCreate = { create: true };
    125 
    126  const item = await root.getDirectoryHandle("dirName", allowCreate);
    127  Assert.ok(!!item, "Should return an item");
    128 
    129  await root.removeEntry("dirName");
    130 };
    131 
    132 exported_symbols.testRemoveEntryIsCallable = async function () {
    133  const root = await navigator.storage.getDirectory();
    134  const removeOptions = { recursive: true };
    135  const allowCreate = { create: true };
    136 
    137  // Ensure file and directory items exists
    138  await root.getFileHandle("fileName", allowCreate);
    139  await root.getDirectoryHandle("dirName", allowCreate);
    140  await root.removeEntry("fileName", removeOptions);
    141  await root.removeEntry("dirName", removeOptions);
    142  try {
    143    await root.removeEntry("doesNotExist", removeOptions);
    144    Assert.ok(false, "Should have thrown");
    145  } catch (ex) {
    146    Assert.ok(true, "Should have thrown");
    147    Assert.equal(
    148      ex.message,
    149      "Entry not found",
    150      "Threw the right error message"
    151    );
    152  }
    153 };
    154 
    155 exported_symbols.testResolveIsCallable = async function () {
    156  const root = await navigator.storage.getDirectory();
    157  const allowCreate = { create: true };
    158  const item = await root.getFileHandle("fileName", allowCreate);
    159 
    160  let path = await root.resolve(item);
    161  Assert.equal(path.length, 1);
    162  Assert.equal(path[0], "fileName", "Resolve got the right path");
    163 
    164  await root.removeEntry("fileName");
    165 };
    166 
    167 exported_symbols.testFileType = async function () {
    168  const root = await navigator.storage.getDirectory();
    169  const allowCreate = { create: true };
    170  const nameStem = "testFileType";
    171  const empty = "";
    172 
    173  const extensions = [
    174    "txt",
    175    "jS",
    176    "JSON",
    177    "css",
    178    "html",
    179    "htm",
    180    "xhtml",
    181    "xml",
    182    "xhtml+xml",
    183    "png",
    184    "apng",
    185    "jPg",
    186    "Jpeg",
    187    "pdF",
    188    "out",
    189    "sh",
    190    "ExE",
    191    "psid",
    192    "EXE ",
    193    " EXE",
    194    "EX\uff65",
    195    "\udbff\udbff\udbff",
    196    // XXX: Invalid surrogate combos like "\udc00\udc00\udc00" may map to the same names impacting cleanup.
    197    "js\udbff",
    198    "\udc00js",
    199    "???",
    200    "\root",
    201    empty,
    202    "AXS",
    203    "dll",
    204    "ocx",
    205    "1",
    206    "ps1",
    207    "cmd",
    208    "xpi",
    209    "swf",
    210  ];
    211 
    212  const expectedTypes = [
    213    "text/plain",
    214    "application/javascript",
    215    "application/json",
    216    "text/css",
    217    "text/html",
    218    "text/html",
    219    "application/xhtml+xml",
    220    "text/xml",
    221    empty,
    222    "image/png",
    223    "image/apng",
    224    "image/jpeg",
    225    "image/jpeg",
    226    "application/pdf",
    227    empty,
    228    "application/x-sh",
    229    "application/octet-stream",
    230    empty,
    231    empty,
    232    empty,
    233    empty,
    234    empty,
    235    empty,
    236    empty,
    237    empty,
    238    empty,
    239    empty,
    240    "application/olescript",
    241    "application/x-msdownload",
    242    "application/octet-stream",
    243    empty,
    244    empty,
    245    "text/plain",
    246    "application/x-xpinstall",
    247    "application/x-shockwave-flash",
    248  ];
    249 
    250  Assert.equal(extensions.length, expectedTypes.length);
    251 
    252  await Promise.all(
    253    extensions.map(async (ext, i) => {
    254      const fileName = nameStem + "." + ext;
    255      const fileHandle = await root.getFileHandle(fileName, allowCreate);
    256      const fileObject = await fileHandle.getFile();
    257      Assert.equal(fileObject.name, fileHandle.name);
    258      Assert.equal(fileObject.type, expectedTypes[i]);
    259      await root.removeEntry(fileName);
    260    })
    261  );
    262 };
    263 
    264 exported_symbols.testContentTypeChangesOnFileMove = async function () {
    265  const allowCreate = { create: true };
    266  const root = await navigator.storage.getDirectory();
    267  const oldName = "testFile.txt";
    268  const oldType = "text/plain";
    269  const subdir = await root.getDirectoryHandle("subdir", allowCreate);
    270 
    271  const fileHandle = await root.getFileHandle(oldName, allowCreate);
    272 
    273  async function checkMove(newName, newType) {
    274    Assert.equal(fileHandle.name, newName, "Has filename changed?");
    275    {
    276      const fileObject = await fileHandle.getFile();
    277      Assert.equal(fileObject.name, newName, "Is the fileobject renamed?");
    278      Assert.equal(fileObject.type, newType, "Is the fileobject type updated?");
    279    }
    280  }
    281 
    282  async function restoreTest() {
    283    await fileHandle.move(root, oldName);
    284    await checkMove(oldName, oldType);
    285  }
    286 
    287  // No name change
    288  await checkMove(oldName, oldType);
    289  await fileHandle.move(subdir);
    290  await checkMove(oldName, oldType);
    291  await restoreTest();
    292 
    293  // With name change
    294 
    295  async function testMoveWithParams(testName, testType) {
    296    async function testFileMoveCall(...combo) {
    297      await fileHandle.move(...combo);
    298      await checkMove(testName, testType);
    299      await restoreTest();
    300    }
    301 
    302    await testFileMoveCall(subdir, testName);
    303    await testFileMoveCall(root, testName);
    304    await testFileMoveCall(testName);
    305  }
    306 
    307  const testParams = {
    308    "testFile.json": "application/json",
    309    testFile: oldType,
    310    "testFile.äüö": "",
    311  };
    312 
    313  for (const [aName, aType] of Object.entries(testParams)) {
    314    await testMoveWithParams(aName, aType);
    315  }
    316 };
    317 
    318 exported_symbols.testContentTypeChangesOnDirMove = async function () {
    319  const allowCreate = { create: true };
    320  const root = await navigator.storage.getDirectory();
    321  const oldName = "testFile.txt";
    322  const oldType = "text/plain";
    323  const subDirOrig = await root.getDirectoryHandle("subDirOrig", allowCreate);
    324  const subDirOther = await root.getDirectoryHandle("subDirOther", allowCreate);
    325  const subSubDir = await subDirOrig.getDirectoryHandle(
    326    "subSubDir",
    327    allowCreate
    328  );
    329 
    330  const testName = "testFile.json";
    331  const testType = "application/json";
    332 
    333  async function checkMove(newName, newType) {
    334    const fileHandle = await subSubDir.getFileHandle(newName, allowCreate);
    335 
    336    Assert.equal(fileHandle.name, newName, "Has filename changed?");
    337    {
    338      const fileObject = await fileHandle.getFile();
    339      Assert.equal(fileObject.name, newName, "Is the fileobject renamed?");
    340      Assert.equal(fileObject.type, newType, "Is the fileobject type updated?");
    341    }
    342  }
    343 
    344  async function restoreTest() {
    345    await subSubDir.move(subDirOrig, "subSubDir");
    346    await checkMove(oldName, oldType);
    347  }
    348 
    349  await checkMove(oldName, oldType);
    350 
    351  // No name change
    352  await subSubDir.move(subDirOther, "other");
    353  await checkMove(oldName, oldType);
    354  await restoreTest();
    355 
    356  // With name change
    357 
    358  async function testDirMoveCall(...combo) {
    359    await subSubDir.move(...combo);
    360    await checkMove(testName, testType);
    361    await restoreTest();
    362  }
    363 
    364  await testDirMoveCall(subDirOther);
    365  await testDirMoveCall(subDirOther, testName);
    366  await testDirMoveCall(subDirOrig, testName);
    367  await testDirMoveCall(subDirOrig);
    368 };
    369 
    370 for (const [key, value] of Object.entries(exported_symbols)) {
    371  Object.defineProperty(value, "name", {
    372    value: key,
    373    writable: false,
    374  });
    375 }