summaryrefslogtreecommitdiffstats
path: root/dom/security/test/unit/test_https_only_https_first_default_port.js
blob: bd4d6717eb0856f71d4dc621feb6e8d6d2a7632e (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);
const { NetUtil } = ChromeUtils.importESModule(
  "resource://gre/modules/NetUtil.sys.mjs"
);

const TEST_PATH = "/https_only_https_first_port";
var httpserver = null;
var channel = null;
var curTest = null;

const TESTS = [
  {
    description: "Test 1 - Default Port (scheme: http, port: default)",
    url: "http://test1.example.com",
    expectedScheme: "https",
    expectedPort: -1, // -1 == default
  },
  {
    description: "Test 2 - Explicit Default Port (scheme: http, port: 80)",
    url: "http://test1.example.com:80",
    expectedScheme: "https",
    expectedPort: -1, // -1 == default
  },
  {
    description: "Test 3 - Explicit Custom Port (scheme: http, port: 8888)",
    url: "http://test1.example.com:8888",
    expectedScheme: "http",
    expectedPort: 8888,
  },
  {
    description:
      "Test 4 - Explicit Default Port for https (scheme: https, port: 443)",
    url: "https://test1.example.com:443",
    expectedScheme: "https",
    expectedPort: -1, // -1 == default
  },
];

function ChannelListener() {}

ChannelListener.prototype = {
  onStartRequest(request) {
    // dummy implementation
  },
  onDataAvailable(request, stream, offset, count) {
    do_throw("Should not get any data!");
  },
  onStopRequest(request, status) {
    var chan = request.QueryInterface(Ci.nsIChannel);
    let requestURL = chan.URI;
    Assert.equal(
      requestURL.scheme,
      curTest.expectedScheme,
      curTest.description
    );
    Assert.equal(requestURL.port, curTest.expectedPort, curTest.description);
    Assert.equal(requestURL.host, "test1.example.com", curTest.description);
    run_next_test();
  },
};

function setUpPrefs() {
  // set up the required prefs
  Services.prefs.setBoolPref("dom.security.https_first", true);
  Services.prefs.setBoolPref("dom.security.https_only_mode", false);
}

function setUpChannel() {
  var chan = NetUtil.newChannel({
    uri: curTest.url + TEST_PATH,
    loadingPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
    contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
    securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
  });
  chan.QueryInterface(Ci.nsIHttpChannel);
  chan.requestMethod = "GET";
  return chan;
}

function serverHandler(metadata, response) {
  // dummy implementation
}

function run_next_test() {
  curTest = TESTS.shift();
  if (!curTest) {
    httpserver.stop(do_test_finished);
    return;
  }

  channel = setUpChannel();
  channel.asyncOpen(new ChannelListener());
}

function run_test() {
  do_get_profile();
  do_test_pending();

  // set up the test environment
  httpserver = new HttpServer();
  httpserver.registerPathHandler(TEST_PATH, serverHandler);
  httpserver.start(-1);

  // set up prefs
  setUpPrefs();

  // run the tests
  run_next_test();
}