tor-browser

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

test_file_filelist.html (4386B)


      1 <!--
      2  Any copyright is dedicated to the Public Domain.
      3  http://creativecommons.org/publicdomain/zero/1.0/
      4 -->
      5 <html>
      6 <head>
      7 <title>Indexed Database Filelist Serialization Blob Sharing Test</title>
      8 
      9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     10 
     11 <script src="/tests/SimpleTest/SimpleTest.js"></script>
     12 
     13 <script type="text/javascript">
     14  // Arbitrary values chosen to ensure
     15  // we have at least a first/middle/last file and a moderate payload size
     16  // that we would never choose to store inline
     17  // in the structured serialization payload
     18  // if we started doing that for small blobs/files.
     19  const fileListSize = 3;
     20  const elementSize = 100000;
     21 
     22  async function testSteps() {
     23    const makeFileList = aFile => {
     24      const dataTransfer = new DataTransfer();
     25      for (let i = 0; i < fileListSize; ++i) {
     26        // Currently it's legal to add the same File
     27        // to a FileList multiple times
     28        // but this may change in the future;
     29        // there was some brief discussion about this at TPAC 2024.
     30        dataTransfer.items.add(aFile);
     31      }
     32 
     33      return dataTransfer.files;
     34    };
     35 
     36    const dbName = window.location.pathname;
     37 
     38    // This is a test of IndexedDB's Blob/File-interning logic
     39    // and we expect randomFile to be persisted to disk
     40    // by our IndexedDB impl exactly once and so all Files
     41    // retrieved from the database should have the same underlying file id.
     42    const randomFile = getRandomFile("random.bin", elementSize);
     43    const fileId = 1;
     44 
     45    const objectStoreInfo = [
     46      {
     47        name: "FileLists",
     48        options: {},
     49        data: { key: "A", fileList: makeFileList(randomFile) },
     50      },
     51      {
     52        name: "Other FileLists",
     53        options: {},
     54        data: { key: "B", fileList: makeFileList(randomFile) },
     55      },
     56    ];
     57 
     58    let request = indexedDB.open(dbName, /*version*/ 1);
     59    let event = await expectingUpgrade(request);
     60    let db = event.target.result;
     61    db.onerror = errorHandler;
     62 
     63    // Add filelists in version change transaction
     64    for (let info of objectStoreInfo) {
     65      let objectStore = db.createObjectStore(info.name, info.options);
     66      objectStore.add(info.data.fileList, info.data.key);
     67    }
     68 
     69    event = await expectingSuccess(request);
     70    db = event.target.result;
     71    db.onerror = errorHandler;
     72 
     73    let refResult;
     74    let refList;
     75    for (let info of objectStoreInfo) {
     76      let objectStore = db.transaction([info.name]).objectStore(info.name);
     77 
     78      event = await expectingSuccess(objectStore.get(info.data.key));
     79      let result = event.target.result;
     80 
     81      if (!refList) {
     82        refList = result;
     83      }
     84 
     85      const expectedLength = info.data.fileList.length;
     86      is(result.length, expectedLength, "Do filelist lengths match?");
     87      for (let i = 0; i < result.length; ++i) {
     88        await verifyBlobAsync(result.item(i), randomFile, fileId);
     89 
     90        if (!refResult) {
     91          refResult = result.item(i);
     92          continue;
     93        }
     94 
     95        is(
     96          getFilePath(result.item(i)),
     97          getFilePath(refResult),
     98          "The same os file"
     99        );
    100      }
    101    }
    102 
    103    // Add filelist in a regular read-write transaction
    104    for (let i = 0; i < objectStoreInfo.length; i++) {
    105      let info = objectStoreInfo[i];
    106 
    107      let objectStore = db
    108        .transaction([info.name], "readwrite")
    109        .objectStore(info.name);
    110 
    111      request = objectStore.add(refList, "C");
    112      event = await expectingSuccess(request);
    113 
    114      is(event.target.result, "C", "Got correct key");
    115 
    116      request = objectStore.get("C");
    117      event = await expectingSuccess(request);
    118 
    119      let result = event.target.result;
    120      const expectedLength = info.data.fileList.length;
    121      is(result.length, expectedLength, "Do filelist lengths match?");
    122      for (let i = 0; i < result.length; ++i) {
    123        await verifyBlobAsync(result.item(i), randomFile, fileId);
    124 
    125        is(
    126          getFilePath(result.item(i)),
    127          getFilePath(refResult),
    128          "The same os file"
    129        );
    130      }
    131    }
    132 
    133    // Two object store infos * two file lists * three items plus
    134    // original file called randomFile
    135    is(bufferCache.length, 13, "Correct length");
    136  }
    137 </script>
    138 <script type="text/javascript" src="file.js"></script>
    139 <script type="text/javascript" src="helpers.js"></script>
    140 
    141 </head>
    142 
    143 <body onload="runTest()">
    144 </body>
    145 
    146 </html>