tor-browser

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

test_DownloadsCommon_getMimeInfo.js (4937B)


      1 const DATA_PDF = atob(
      2  "JVBERi0xLjANCjEgMCBvYmo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFI+PmVuZG9iaiAyIDAgb2JqPDwvVHlwZS9QYWdlcy9LaWRzWzMgMCBSXS9Db3VudCAxPj5lbmRvYmogMyAwIG9iajw8L1R5cGUvUGFnZS9NZWRpYUJveFswIDAgMyAzXT4+ZW5kb2JqDQp4cmVmDQowIDQNCjAwMDAwMDAwMDAgNjU1MzUgZg0KMDAwMDAwMDAxMCAwMDAwMCBuDQowMDAwMDAwMDUzIDAwMDAwIG4NCjAwMDAwMDAxMDIgMDAwMDAgbg0KdHJhaWxlcjw8L1NpemUgNC9Sb290IDEgMCBSPj4NCnN0YXJ0eHJlZg0KMTQ5DQolRU9G"
      3 );
      4 
      5 const DOWNLOAD_TEMPLATE = {
      6  source: {
      7    url: "https://example.com/download",
      8  },
      9  target: {
     10    path: "",
     11  },
     12  contentType: "text/plain",
     13  succeeded: DownloadsCommon.DOWNLOAD_FINISHED,
     14  canceled: false,
     15  error: null,
     16  hasPartialData: false,
     17  hasBlockedData: false,
     18  startTime: new Date(Date.now() - 1000),
     19 };
     20 
     21 const TESTFILES = {
     22  "download-test.txt": "Text file contents\n",
     23  "download-test.pdf": DATA_PDF,
     24  "download-test.PDF": DATA_PDF,
     25  "download-test.xxunknown": "Unknown file contents\n",
     26  "download-test": "No extension file contents\n",
     27 };
     28 let gPublicList;
     29 
     30 add_task(async function test_setup() {
     31  let profileDir = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
     32  Assert.ok(profileDir, "profileDir: " + profileDir);
     33  for (let [filename, contents] of Object.entries(TESTFILES)) {
     34    TESTFILES[filename] = await createDownloadedFile(
     35      PathUtils.join(gDownloadDir, filename),
     36      contents
     37    );
     38  }
     39  gPublicList = await Downloads.getList(Downloads.PUBLIC);
     40 });
     41 
     42 const TESTCASES = [
     43  {
     44    name: "Check returned value is null when the download did not succeed",
     45    testFile: "download-test.txt",
     46    contentType: "text/plain",
     47    succeeded: false,
     48    expected: null,
     49  },
     50  {
     51    name: "Check correct mime-info is returned when download contentType is unambiguous",
     52    testFile: "download-test.txt",
     53    contentType: "text/plain",
     54    expected: {
     55      type: "text/plain",
     56    },
     57  },
     58  {
     59    name: "Returns correct mime-info from file extension when download contentType is missing",
     60    testFile: "download-test.pdf",
     61    contentType: undefined,
     62    expected: {
     63      type: "application/pdf",
     64    },
     65  },
     66  {
     67    name: "Returns correct mime-info from file extension case-insensitively",
     68    testFile: "download-test.PDF",
     69    contentType: undefined,
     70    expected: {
     71      type: "application/pdf",
     72    },
     73  },
     74  {
     75    name: "Returns null when contentType is missing and file extension is unknown",
     76    testFile: "download-test.xxunknown",
     77    contentType: undefined,
     78    expected: null,
     79  },
     80  {
     81    name: "Returns contentType when contentType is ambiguous and file extension is unknown",
     82    testFile: "download-test.xxunknown",
     83    contentType: "application/octet-stream",
     84    expected: {
     85      type: "application/octet-stream",
     86    },
     87  },
     88  {
     89    name: "Returns contentType when contentType is ambiguous and there is no file extension",
     90    testFile: "download-test",
     91    contentType: "application/octet-stream",
     92    expected: {
     93      type: "application/octet-stream",
     94    },
     95  },
     96  {
     97    name: "Returns null when there's no contentType and no file extension",
     98    testFile: "download-test",
     99    contentType: undefined,
    100    expected: null,
    101  },
    102 ];
    103 
    104 // add tests for each of the generic mime-types we recognize,
    105 // to ensure they prefer the associated mime-type of the target file extension
    106 for (let type of [
    107  "application/octet-stream",
    108  "binary/octet-stream",
    109  "application/unknown",
    110 ]) {
    111  TESTCASES.push({
    112    name: `Returns correct mime-info from file extension when contentType is generic (${type})`,
    113    testFile: "download-test.pdf",
    114    contentType: type,
    115    expected: {
    116      type: "application/pdf",
    117    },
    118  });
    119 }
    120 
    121 for (let testData of TESTCASES) {
    122  let tmp = {
    123    async [testData.name]() {
    124      info("testing with: " + JSON.stringify(testData));
    125      await test_getMimeInfo_basic_function(testData);
    126    },
    127  };
    128  add_task(tmp[testData.name]);
    129 }
    130 
    131 /**
    132 * Sanity test the DownloadsCommon.getMimeInfo method with test parameters
    133 */
    134 async function test_getMimeInfo_basic_function(testData) {
    135  let downloadData = {
    136    ...DOWNLOAD_TEMPLATE,
    137    source: "source" in testData ? testData.source : DOWNLOAD_TEMPLATE.source,
    138    succeeded:
    139      "succeeded" in testData
    140        ? testData.succeeded
    141        : DOWNLOAD_TEMPLATE.succeeded,
    142    target: TESTFILES[testData.testFile],
    143    contentType: testData.contentType,
    144  };
    145  Assert.ok(downloadData.target instanceof Ci.nsIFile, "target is a nsIFile");
    146  let download = await Downloads.createDownload(downloadData);
    147  await gPublicList.add(download);
    148  await download.refresh();
    149 
    150  Assert.ok(
    151    await IOUtils.exists(download.target.path),
    152    "The file should actually exist."
    153  );
    154  let result = await DownloadsCommon.getMimeInfo(download);
    155  if (testData.expected) {
    156    Assert.equal(
    157      result.type,
    158      testData.expected.type,
    159      "Got expected mimeInfo.type"
    160    );
    161  } else {
    162    Assert.equal(
    163      result,
    164      null,
    165      `Expected null, got object with type: ${result?.type}`
    166    );
    167  }
    168 }