tor-browser

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

test_DownloadLastDir_basics.js (6181B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Basic test for setting and retrieving a download last dir.
      5 // More complex tests can be found in browser/components/privatebrowsing/.
      6 
      7 const SAVE_PER_SITE_PREF_BRANCH = "browser.download.lastDir";
      8 const SAVE_PER_SITE_PREF = SAVE_PER_SITE_PREF_BRANCH + ".savePerSite";
      9 
     10 let { FileUtils } = ChromeUtils.importESModule(
     11  "resource://gre/modules/FileUtils.sys.mjs"
     12 );
     13 let { DownloadLastDir } = ChromeUtils.importESModule(
     14  "resource://gre/modules/DownloadLastDir.sys.mjs"
     15 );
     16 
     17 add_task(
     18  {
     19    pref_set: [[SAVE_PER_SITE_PREF, true]],
     20  },
     21  async function test() {
     22    let downloadLastDir = new DownloadLastDir(null);
     23 
     24    let unknownUri = Services.io.newURI("https://unknown.org/");
     25    Assert.deepEqual(
     26      await downloadLastDir.getFileAsync(unknownUri),
     27      null,
     28      "Untracked URI, no pref set"
     29    );
     30 
     31    let dir1 = FileUtils.getDir("TmpD", ["dir1"]);
     32    dir1.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
     33    let uri1 = Services.io.newURI("https://test1.moz.org");
     34    downloadLastDir.setFile(uri1, dir1);
     35    let dir2 = FileUtils.getDir("TmpD", ["dir2"]);
     36    dir2.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
     37    let uri2 = Services.io.newURI("https://test2.moz.org");
     38    downloadLastDir.setFile(uri2, dir2);
     39    let dir3 = FileUtils.getDir("TmpD", ["dir3"]);
     40    dir3.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
     41    downloadLastDir.setFile(null, dir3);
     42    Assert.equal(
     43      (await downloadLastDir.getFileAsync(uri1)).path,
     44      dir1.path,
     45      "Check common URI"
     46    );
     47    Assert.equal(
     48      (await downloadLastDir.getFileAsync(uri2)).path,
     49      dir2.path,
     50      "Check common URI"
     51    );
     52    Assert.equal(downloadLastDir.file.path, dir3.path, "No URI");
     53    Assert.equal(
     54      (await downloadLastDir.getFileAsync(unknownUri)).path,
     55      dir3.path,
     56      "Untracked URI, pref set"
     57    );
     58 
     59    info("Check clearHistory removes all data");
     60    let subject = {};
     61    Services.obs.notifyObservers(subject, "browser:purge-session-history");
     62    await subject.promise;
     63    Assert.deepEqual(
     64      await downloadLastDir.getFileAsync(uri1),
     65      null,
     66      "Check common URI after clear history returns null"
     67    );
     68    Assert.deepEqual(
     69      await downloadLastDir.getFileAsync(uri2),
     70      null,
     71      "Check common URI after clear history returns null"
     72    );
     73    Assert.deepEqual(
     74      await downloadLastDir.getFileAsync(unknownUri),
     75      null,
     76      "Check untracked URI after clear history returns null"
     77    );
     78 
     79    // file: URIs should all point to the same folder.
     80    let fileUri1 = Services.io.newURI("file:///c:/test.txt");
     81    downloadLastDir.setFile(uri1, dir3);
     82    let dir4 = FileUtils.getDir("TmpD", ["dir4"]);
     83    dir4.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
     84    let fileUri2 = Services.io.newURI("file:///d:/test.png");
     85    downloadLastDir.setFile(uri1, dir4);
     86    Assert.equal(
     87      (await downloadLastDir.getFileAsync(fileUri1)).path,
     88      dir4.path,
     89      "Check file URI"
     90    );
     91    Assert.equal(
     92      (await downloadLastDir.getFileAsync(fileUri2)).path,
     93      dir4.path,
     94      "Check file URI"
     95    );
     96    let unknownFileUri = Services.io.newURI("file:///e:/test.mkv");
     97    Assert.equal(
     98      (await downloadLastDir.getFileAsync(unknownFileUri)).path,
     99      dir4.path,
    100      "Untracked File URI, pref set"
    101    );
    102 
    103    // data: URIs should point to a folder per mime-type.
    104    // Unspecified mime-type is handled as text/plain.
    105    let dataUri1 = Services.io.newURI("data:text/plain;charset=UTF-8,1234");
    106    downloadLastDir.setFile(dataUri1, dir1);
    107    let dataUri2 = Services.io.newURI("data:image/png;base64,1234");
    108    Assert.equal(
    109      (await downloadLastDir.getFileAsync(dataUri2)).path,
    110      dir1.path,
    111      "Check data URI"
    112    );
    113    let dataUri3 = Services.io.newURI("data:image/png,5678");
    114    downloadLastDir.setFile(dataUri3, dir2);
    115    Assert.equal(
    116      (await downloadLastDir.getFileAsync(dataUri2)).path,
    117      dir2.path,
    118      "Data URI was changed, same mime-type"
    119    );
    120    Assert.equal(
    121      (await downloadLastDir.getFileAsync(dataUri1)).path,
    122      dir1.path,
    123      "Data URI was not changed, different mime-type"
    124    );
    125    let dataUri4 = Services.io.newURI("data:,");
    126    Assert.equal(
    127      (await downloadLastDir.getFileAsync(dataUri4)).path,
    128      dir1.path,
    129      "Data URI defaults to text/plain"
    130    );
    131    downloadLastDir.setFile(null, dir4);
    132    let unknownDataUri = Services.io.newURI("data:application/zip,");
    133    Assert.deepEqual(
    134      (await downloadLastDir.getFileAsync(unknownDataUri)).path,
    135      dir4.path,
    136      "Untracked data URI"
    137    );
    138    Assert.equal(
    139      (await downloadLastDir.getFileAsync(dataUri4)).path,
    140      dir1.path,
    141      "Data URI didn't change"
    142    );
    143 
    144    info("blob: URIs should point to a folder based on their origin.");
    145    let blobUri1 = Services.io.newURI(
    146      "blob:https://chat.mozilla.org/35d6a992-6e18-4957-8216-070c53b9bc83"
    147    );
    148    let blobOriginUri1 = Services.io.newURI("https://chat.mozilla.org/");
    149    downloadLastDir.setFile(blobUri1, dir1);
    150    Assert.equal(
    151      (await downloadLastDir.getFileAsync(blobUri1)).path,
    152      (await downloadLastDir.getFileAsync(blobOriginUri1)).path,
    153      "Check blob URI"
    154    );
    155    // While we are no longer supposed to store pdf.js URLs like this, this
    156    // test remains to cover resource origins.
    157    info("Test blob: URIs to local resouce.");
    158    let blobUri2 = Services.io.newURI(
    159      "blob:resource://pdf.js/ed645567-3eea-4ff1-94fd-efb04812afe0"
    160    );
    161    let blobOriginUri2 = Services.io.newURI("resource://pdf.js/");
    162    downloadLastDir.setFile(blobUri2, dir2);
    163    Assert.equal(
    164      (await downloadLastDir.getFileAsync(blobUri2)).path,
    165      (await downloadLastDir.getFileAsync(blobOriginUri2)).path,
    166      "Check blob URI"
    167    );
    168    info("Test an empty blob:");
    169    let noOriginBlobUri = Services.io.newURI("blob:");
    170    downloadLastDir.setFile(blobUri2, dir3);
    171    Assert.equal(
    172      (await downloadLastDir.getFileAsync(noOriginBlobUri)).path,
    173      dir3.path,
    174      "Check blob URI"
    175    );
    176  }
    177 );