tor-browser

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

test_data_protocol.js (2837B)


      1 /* run some tests on the data: protocol handler */
      2 
      3 // The behaviour wrt spaces is:
      4 // - Textual content keeps all spaces
      5 // - Other content strips unescaped spaces
      6 // - Base64 content strips escaped and unescaped spaces
      7 
      8 "use strict";
      9 
     10 var urls = [
     11  ["data:,", "text/plain", ""],
     12  ["data:,foo", "text/plain", "foo"],
     13  [
     14    "data:application/octet-stream,foo bar",
     15    "application/octet-stream",
     16    "foo bar",
     17  ],
     18  [
     19    "data:application/octet-stream,foo%20bar",
     20    "application/octet-stream",
     21    "foo bar",
     22  ],
     23  ["data:application/xhtml+xml,foo bar", "application/xhtml+xml", "foo bar"],
     24  ["data:application/xhtml+xml,foo%20bar", "application/xhtml+xml", "foo bar"],
     25  ["data:text/plain,foo%00 bar", "text/plain", "foo\x00 bar"],
     26  ["data:text/plain;x=y,foo%00 bar", "text/plain", "foo\x00 bar"],
     27  ["data:;x=y,foo%00 bar", "text/plain", "foo\x00 bar"],
     28  ["data:text/plain;base64,Zm9 vI%20GJ%0Dhc%0Ag==", "text/plain", "foo bar"],
     29  ["DATA:TEXT/PLAIN;BASE64,Zm9 vI%20GJ%0Dhc%0Ag==", "text/plain", "foo bar"],
     30  ["DaTa:;BaSe64,Zm9 vI%20GJ%0Dhc%0Ag==", "text/plain", "foo bar"],
     31  ["data:;x=y;base64,Zm9 vI%20GJ%0Dhc%0Ag==", "text/plain", "foo bar"],
     32  // Bug 774240
     33  [
     34    "data:application/octet-stream;base64=y,foobar",
     35    "application/octet-stream",
     36    "foobar",
     37  ],
     38  ["data:text/plain;base64;x=y,dGVzdA==", "text/plain", "dGVzdA=="],
     39  ["data:text/plain;x=y;base64,dGVzdA==", "text/plain", "test"],
     40  ["data:text/plain;x=y;base64,", "text/plain", ""],
     41  ["data:  ;charset=x   ;  base64,WA", "text/plain", "X", "x"],
     42  ["data:base64,WA", "text/plain", "WA", "US-ASCII"],
     43 ];
     44 
     45 function run_test() {
     46  dump("*** run_test\n");
     47 
     48  function on_read_complete(request, data, idx) {
     49    dump("*** run_test.on_read_complete\n");
     50 
     51    if (request.nsIChannel.contentType != urls[idx][1]) {
     52      do_throw(
     53        "Type mismatch! Is <" +
     54          chan.contentType +
     55          ">, should be <" +
     56          urls[idx][1] +
     57          ">"
     58      );
     59    }
     60 
     61    if (urls[idx][3] && request.nsIChannel.contentCharset !== urls[idx][3]) {
     62      do_throw(
     63        `Charset mismatch! Test <${urls[idx][0]}> - Is <${request.nsIChannel.contentCharset}>, should be <${urls[idx][3]}>`
     64      );
     65    }
     66 
     67    /* read completed successfully.  now compare the data. */
     68    if (data != urls[idx][2]) {
     69      do_throw(
     70        "Stream contents do not match with direct read! Is <" +
     71          data +
     72          ">, should be <" +
     73          urls[idx][2] +
     74          ">"
     75      );
     76    }
     77    do_test_finished();
     78  }
     79 
     80  for (var i = 0; i < urls.length; ++i) {
     81    dump("*** opening channel " + i + "\n");
     82    do_test_pending();
     83    var chan = NetUtil.newChannel({
     84      uri: urls[i][0],
     85      loadUsingSystemPrincipal: true,
     86    });
     87    chan.contentType = "foo/bar"; // should be ignored
     88    chan.asyncOpen(new ChannelListener(on_read_complete, i));
     89  }
     90 }