summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_default_uri_bypass.js
blob: d3ceac5d8c5f16f7ef2abea0e80e267f474f4a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 *  Test that default uri is bypassable by an unknown protocol that is
 *  present in the bypass list (and the pref is enabled)
 */
"use strict";

function inChildProcess() {
  return Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
}

function run_test() {
  // In-Parent-only process pref setup
  if (!inChildProcess()) {
    Services.prefs.setBoolPref("network.url.useDefaultURI", true);
    Services.prefs.setBoolPref(
      "network.url.some_schemes_bypass_defaultURI_fallback",
      true
    );

    Services.prefs.setCharPref(
      "network.url.simple_uri_schemes",
      "simpleprotocol,otherproto"
    );
  }

  // check valid url is fine
  let uri = NetUtil.newURI("https://example.com/");
  Assert.equal(uri.spec, "https://example.com/"); // same

  // nsStandardURL removes second colon when nesting protocols
  let uri1 = NetUtil.newURI("https://https://example.com/");
  Assert.equal(uri1.spec, "https://https//example.com/");

  // defaultUri removes second colon
  // no-bypass protocol uses defaultURI
  let uri2 = NetUtil.newURI("nonsimpleprotocol://https://example.com");
  Assert.equal(uri2.spec, "nonsimpleprotocol://https//example.com");

  // simpleURI keeps the second colon
  // an unknown protocol in the bypass list will use simpleURI
  // despite network.url.useDefaultURI being enabled
  let same = "simpleprotocol://https://example.com";
  let uri3 = NetUtil.newURI(same);
  Assert.equal(uri3.spec, same); // simple uri keeps second colon

  // setCharPref not accessible from child process
  if (!inChildProcess()) {
    // check that pref update removes simpleprotocol from bypass list
    Services.prefs.setCharPref("network.url.simple_uri_schemes", "otherproto");
    let uri4 = NetUtil.newURI("simpleprotocol://https://example.com");
    Assert.equal(uri4.spec, "simpleprotocol://https//example.com"); // default uri removes second colon

    // check that spaces are parsed out
    Services.prefs.setCharPref(
      "network.url.simple_uri_schemes",
      " simpleprotocol , otherproto "
    );
    let uri5 = NetUtil.newURI("simpleprotocol://https://example.com");
    Assert.equal(uri5.spec, "simpleprotocol://https://example.com"); // simple uri keeps second colon
    let uri6 = NetUtil.newURI("otherproto://https://example.com");
    Assert.equal(uri6.spec, "otherproto://https://example.com"); // simple uri keeps second colon
  }
}