tor-browser

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

test_defaultURI.js (5749B)


      1 "use strict";
      2 
      3 function stringToDefaultURI(str) {
      4  return Cc["@mozilla.org/network/default-uri-mutator;1"]
      5    .createInstance(Ci.nsIURIMutator)
      6    .setSpec(str)
      7    .finalize();
      8 }
      9 
     10 add_task(function test_getters() {
     11  let uri = stringToDefaultURI(
     12    "proto://user:password@hostname:123/path/to/file?query#hash"
     13  );
     14  equal(uri.spec, "proto://user:password@hostname:123/path/to/file?query#hash");
     15  equal(uri.prePath, "proto://user:password@hostname:123");
     16  equal(uri.scheme, "proto");
     17  equal(uri.userPass, "user:password");
     18  equal(uri.username, "user");
     19  equal(uri.password, "password");
     20  equal(uri.hasUserPass, true);
     21  equal(uri.hostPort, "hostname:123");
     22  equal(uri.host, "hostname");
     23  equal(uri.port, 123);
     24  equal(uri.pathQueryRef, "/path/to/file?query#hash");
     25  equal(uri.asciiSpec, uri.spec);
     26  equal(uri.asciiHostPort, uri.hostPort);
     27  equal(uri.asciiHost, uri.host);
     28  equal(uri.ref, "hash");
     29  equal(
     30    uri.specIgnoringRef,
     31    "proto://user:password@hostname:123/path/to/file?query"
     32  );
     33  equal(uri.hasRef, true);
     34  equal(uri.filePath, "/path/to/file");
     35  equal(uri.query, "query");
     36  equal(uri.displayHost, uri.host);
     37  equal(uri.displayHostPort, uri.hostPort);
     38  equal(uri.displaySpec, uri.spec);
     39  equal(uri.displayPrePath, uri.prePath);
     40 });
     41 
     42 add_task(function test_methods() {
     43  let uri = stringToDefaultURI(
     44    "proto://user:password@hostname:123/path/to/file?query#hash"
     45  );
     46  let uri_same = stringToDefaultURI(
     47    "proto://user:password@hostname:123/path/to/file?query#hash"
     48  );
     49  let uri_different = stringToDefaultURI(
     50    "proto://user:password@hostname:123/path/to/file?query"
     51  );
     52  let uri_very_different = stringToDefaultURI(
     53    "proto://user:password@hostname:123/path/to/file?query1#hash"
     54  );
     55  ok(uri.equals(uri_same));
     56  ok(!uri.equals(uri_different));
     57  ok(uri.schemeIs("proto"));
     58  ok(!uri.schemeIs("proto2"));
     59  ok(!uri.schemeIs("proto "));
     60  ok(!uri.schemeIs("proto\n"));
     61  equal(uri.resolve("/hello"), "proto://user:password@hostname:123/hello");
     62  equal(
     63    uri.resolve("hello"),
     64    "proto://user:password@hostname:123/path/to/hello"
     65  );
     66  equal(uri.resolve("proto2:otherhost"), "proto2:otherhost");
     67  ok(uri.equalsExceptRef(uri_same));
     68  ok(uri.equalsExceptRef(uri_different));
     69  ok(!uri.equalsExceptRef(uri_very_different));
     70 });
     71 
     72 add_task(function test_mutator() {
     73  let uri = stringToDefaultURI(
     74    "proto://user:pass@host:123/path/to/file?query#hash"
     75  );
     76 
     77  let check = (callSetters, verify) => {
     78    let m = uri.mutate();
     79    callSetters(m);
     80    verify(m.finalize());
     81  };
     82 
     83  check(
     84    m => m.setSpec("test:bla"),
     85    u => equal(u.spec, "test:bla")
     86  );
     87  check(
     88    m => m.setSpec("test:bla"),
     89    u => equal(u.spec, "test:bla")
     90  );
     91  check(
     92    m => m.setScheme("some"),
     93    u => equal(u.spec, "some://user:pass@host:123/path/to/file?query#hash")
     94  );
     95  check(
     96    m => m.setUserPass("u"),
     97    u => equal(u.spec, "proto://u@host:123/path/to/file?query#hash")
     98  );
     99  check(
    100    m => m.setUserPass("u:p"),
    101    u => equal(u.spec, "proto://u:p@host:123/path/to/file?query#hash")
    102  );
    103  check(
    104    m => m.setUserPass(":p"),
    105    u => equal(u.spec, "proto://:p@host:123/path/to/file?query#hash")
    106  );
    107  check(
    108    m => m.setUserPass(""),
    109    u => equal(u.spec, "proto://host:123/path/to/file?query#hash")
    110  );
    111  check(
    112    m => m.setUsername("u"),
    113    u => equal(u.spec, "proto://u:pass@host:123/path/to/file?query#hash")
    114  );
    115  check(
    116    m => m.setPassword("p"),
    117    u => equal(u.spec, "proto://user:p@host:123/path/to/file?query#hash")
    118  );
    119  check(
    120    m => m.setHostPort("h"),
    121    u => equal(u.spec, "proto://user:pass@h:123/path/to/file?query#hash")
    122  );
    123  check(
    124    m => m.setHostPort("h:456"),
    125    u => equal(u.spec, "proto://user:pass@h:456/path/to/file?query#hash")
    126  );
    127  check(
    128    m => m.setHost("bla"),
    129    u => equal(u.spec, "proto://user:pass@bla:123/path/to/file?query#hash")
    130  );
    131  check(
    132    m => m.setPort(987),
    133    u => equal(u.spec, "proto://user:pass@host:987/path/to/file?query#hash")
    134  );
    135  check(
    136    m => m.setPathQueryRef("/p?q#r"),
    137    u => equal(u.spec, "proto://user:pass@host:123/p?q#r")
    138  );
    139  check(
    140    m => m.setRef("r"),
    141    u => equal(u.spec, "proto://user:pass@host:123/path/to/file?query#r")
    142  );
    143  check(
    144    m => m.setFilePath("/my/path"),
    145    u => equal(u.spec, "proto://user:pass@host:123/my/path?query#hash")
    146  );
    147  check(
    148    m => m.setQuery("q"),
    149    u => equal(u.spec, "proto://user:pass@host:123/path/to/file?q#hash")
    150  );
    151 });
    152 
    153 add_task(function test_ipv6() {
    154  let uri = stringToDefaultURI("non-special://[2001::1]/");
    155  equal(uri.hostPort, "[2001::1]");
    156  // Hopefully this will change after bug 1603199.
    157  equal(uri.host, "2001::1");
    158 });
    159 
    160 add_task(function test_serialization() {
    161  let uri = stringToDefaultURI("http://example.org/path");
    162  let str = serialize_to_escaped_string(uri);
    163  let other = deserialize_from_escaped_string(str).QueryInterface(Ci.nsIURI);
    164  equal(other.spec, uri.spec);
    165 });
    166 
    167 // This test assumes the serialization never changes, which might not be true.
    168 // It's OK to change the test if we ever make changes to the serialization
    169 // code and this starts failing.
    170 add_task(function test_deserialize_from_string() {
    171  let payload =
    172    "%04DZ%A0%FD%27L%99%BDAk%E61%8A%E9%2C%00%00%00%00%00%00%00" +
    173    "%00%C0%00%00%00%00%00%00F%00%00%00%13scheme%3Astuff/to/say";
    174  equal(
    175    deserialize_from_escaped_string(payload).QueryInterface(Ci.nsIURI).spec,
    176    stringToDefaultURI("scheme:stuff/to/say").spec
    177  );
    178 
    179  let payload2 =
    180    "%04DZ%A0%FD%27L%99%BDAk%E61%8A%E9%2C%00%00%00%00%00%00%00" +
    181    "%00%C0%00%00%00%00%00%00F%00%00%00%17http%3A//example.org/path";
    182  equal(
    183    deserialize_from_escaped_string(payload2).QueryInterface(Ci.nsIURI).spec,
    184    stringToDefaultURI("http://example.org/path").spec
    185  );
    186 });