tor-browser

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

test_fetch-file.js (3228B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 // Tests for DevToolsUtils.fetch on file:// URI's.
      6 
      7 const { FileUtils } = ChromeUtils.importESModule(
      8  "resource://gre/modules/FileUtils.sys.mjs"
      9 );
     10 
     11 const TEST_CONTENT = "aéd";
     12 
     13 // The TEST_CONTENT encoded as UTF-8.
     14 const UTF8_TEST_BUFFER = new Uint8Array([0x61, 0xc3, 0xa9, 0x64]);
     15 
     16 // The TEST_CONTENT encoded as ISO 8859-1.
     17 const ISO_8859_1_BUFFER = new Uint8Array([0x61, 0xe9, 0x64]);
     18 
     19 /**
     20 * Tests that URLs with arrows pointing to an actual source are handled properly
     21 * (bug 808960). For example 'resource://gre/modules/XPIProvider.sys.mjs ->
     22 * file://l10n.js' should load 'file://l10n.js'.
     23 */
     24 add_task(async function test_arrow_urls() {
     25  const { path } = createTemporaryFile(".js");
     26  const url = "resource://gre/modules/XPIProvider.sys.mjs -> file://" + path;
     27 
     28  await IOUtils.writeUTF8(path, TEST_CONTENT);
     29  const { content } = await DevToolsUtils.fetch(url);
     30 
     31  deepEqual(content, TEST_CONTENT, "The file contents were correctly read.");
     32 });
     33 
     34 /**
     35 * Tests that empty files are read correctly.
     36 */
     37 add_task(async function test_empty() {
     38  const { path } = createTemporaryFile();
     39  const { content } = await DevToolsUtils.fetch("file://" + path);
     40  deepEqual(content, "", "The empty file was read correctly.");
     41 });
     42 
     43 /**
     44 * Tests that UTF-8 encoded files are correctly read.
     45 */
     46 add_task(async function test_encoding_utf8() {
     47  const { path } = createTemporaryFile();
     48  await IOUtils.write(path, UTF8_TEST_BUFFER);
     49 
     50  const { content } = await DevToolsUtils.fetch(path);
     51  deepEqual(
     52    content,
     53    TEST_CONTENT,
     54    "The UTF-8 encoded file was correctly read."
     55  );
     56 });
     57 
     58 /**
     59 * Tests that ISO 8859-1 (Latin-1) encoded files are correctly read.
     60 */
     61 add_task(async function test_encoding_iso_8859_1() {
     62  const { path } = createTemporaryFile();
     63  await IOUtils.write(path, ISO_8859_1_BUFFER);
     64 
     65  const { content } = await DevToolsUtils.fetch(path);
     66  deepEqual(
     67    content,
     68    TEST_CONTENT,
     69    "The ISO 8859-1 encoded file was correctly read."
     70  );
     71 });
     72 
     73 /**
     74 * Test that non-existent files are handled correctly.
     75 */
     76 add_task(async function test_missing() {
     77  await DevToolsUtils.fetch("file:///file/not/found.right").then(
     78    result => {
     79      info(result);
     80      ok(false, "Fetch resolved unexpectedly when the file was not found.");
     81    },
     82    () => {
     83      ok(true, "Fetch rejected as expected because the file was not found.");
     84    }
     85  );
     86 });
     87 
     88 /**
     89 * Test that URLs without file:// scheme work.
     90 */
     91 add_task(async function test_schemeless_files() {
     92  const { path } = createTemporaryFile();
     93 
     94  await IOUtils.writeUTF8(path, TEST_CONTENT);
     95 
     96  const { content } = await DevToolsUtils.fetch(path);
     97  deepEqual(content, TEST_CONTENT, "The content was correct.");
     98 });
     99 
    100 /**
    101 * Creates a temporary file that is removed after the test completes.
    102 */
    103 function createTemporaryFile(extension) {
    104  const name = "test_fetch-file-" + Math.random() + (extension || "");
    105  const file = new FileUtils.File(PathUtils.join(PathUtils.tempDir, name));
    106  file.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0755", 8));
    107 
    108  registerCleanupFunction(() => {
    109    file.remove(false);
    110  });
    111 
    112  return file;
    113 }