tor-browser

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

test_file_uri.html (2779B)


      1 <!DOCTYPE HTML>
      2 <html lang="en">
      3 <head>
      4  <meta charset="utf8">
      5  <title>Test for file activity tracking</title>
      6  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
      7  <script type="text/javascript" src="common.js"></script>
      8  <!-- Any copyright is dedicated to the Public Domain.
      9     - http://creativecommons.org/publicdomain/zero/1.0/ -->
     10 </head>
     11 <body>
     12 <p>Test for file activity tracking</p>
     13 
     14 <script class="testbody" type="text/javascript">
     15 "use strict";
     16 
     17 SimpleTest.waitForExplicitFinish();
     18 
     19 const {NetUtil} = ChromeUtils.importESModule(
     20  "resource://gre/modules/NetUtil.sys.mjs"
     21 );
     22 const {FileUtils} = ChromeUtils.importESModule(
     23  "resource://gre/modules/FileUtils.sys.mjs"
     24 );
     25 
     26 let gState;
     27 let gTmpFile;
     28 
     29 function doFileActivity()
     30 {
     31  info("doFileActivity");
     32  const fileContent = "<p>hello world from bug 798764";
     33 
     34  gTmpFile = new FileUtils.File(PathUtils.join(PathUtils.tempDir, "bug798764.html"));
     35  gTmpFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
     36 
     37  const fout = FileUtils.openSafeFileOutputStream(gTmpFile,
     38    FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE);
     39 
     40  const stream = Cc[
     41    "@mozilla.org/io/arraybuffer-input-stream;1"
     42  ].createInstance(Ci.nsIArrayBufferInputStream);
     43  const buffer = new TextEncoder().encode(fileContent).buffer;
     44  stream.setData(buffer, 0, buffer.byteLength);
     45  NetUtil.asyncCopy(stream, fout, addIframe);
     46 }
     47 
     48 function addIframe(status)
     49 {
     50  ok(Components.isSuccessCode(status),
     51     "the temporary file was saved successfully");
     52 
     53  const iframe = document.createElement("iframe");
     54  iframe.src = NetUtil.newURI(gTmpFile).spec;
     55  document.body.appendChild(iframe);
     56 }
     57 
     58 async function startTest()
     59 {
     60  removeEventListener("load", startTest);
     61 
     62  const {state} = await attachConsole(["FileActivity"]);
     63  onAttach(state);
     64 }
     65 
     66 function onAttach(state)
     67 {
     68  gState = state;
     69  gState.webConsoleFront.on("fileActivity", onFileActivity);
     70  doFileActivity();
     71 }
     72 
     73 function onFileActivity(packet)
     74 {
     75  gState.webConsoleFront.off("fileActivity", onFileActivity);
     76 
     77  info("packet.uri: " + packet.uri);
     78  ok(/\bbug798764\b.*\.html$/.test(packet.uri), "file URI match");
     79 
     80  testEnd();
     81 }
     82 
     83 function testEnd()
     84 {
     85  if (gTmpFile) {
     86    SimpleTest.executeSoon(function() {
     87      try {
     88        gTmpFile.remove(false);
     89      }
     90      catch (ex) {
     91        if (ex.name != "NS_ERROR_FILE_IS_LOCKED") {
     92          throw ex;
     93        }
     94        // Sometimes remove() throws because the file is not unlocked soon
     95        // enough.
     96      }
     97      gTmpFile = null;
     98    });
     99  }
    100 
    101  if (gState) {
    102    closeDebugger(gState, function() {
    103      gState = null;
    104      SimpleTest.finish();
    105    });
    106  } else {
    107    SimpleTest.finish();
    108  }
    109 }
    110 
    111 addEventListener("load", startTest);
    112 </script>
    113 </body>
    114 </html>