summaryrefslogtreecommitdiffstats
path: root/services/settings/test/unit/test_remote_settings_utils.js
blob: de372b3e4468e4fe1f75ecc2857f5c1546868cd0 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* import-globals-from ../../../common/tests/unit/head_helpers.js */

const { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);
const { Utils } = ChromeUtils.importESModule(
  "resource://services-settings/Utils.sys.mjs"
);

const BinaryOutputStream = Components.Constructor(
  "@mozilla.org/binaryoutputstream;1",
  "nsIBinaryOutputStream",
  "setOutputStream"
);

const server = new HttpServer();
server.start(-1);
registerCleanupFunction(() => server.stop(() => {}));
const SERVER_BASE_URL = `http://localhost:${server.identity.primaryPort}`;

const proxyServer = new HttpServer();
proxyServer.identity.add("http", "localhost", server.identity.primaryPort);
proxyServer.start(-1);
registerCleanupFunction(() => proxyServer.stop(() => {}));
const PROXY_PORT = proxyServer.identity.primaryPort;

// A sequence of bytes that would become garbage if it were to be read as UTF-8:
// - 0xEF 0xBB 0xBF is a byte order mark.
// - 0xC0 on its own is invalid (it's the first byte of a 2-byte encoding).
const INVALID_UTF_8_BYTES = [0xef, 0xbb, 0xbf, 0xc0];

server.registerPathHandler("/binary.dat", (request, response) => {
  response.setStatusLine(null, 201, "StatusLineHere");
  response.setHeader("headerName", "HeaderValue: HeaderValueEnd");
  let binaryOut = new BinaryOutputStream(response.bodyOutputStream);
  binaryOut.writeByteArray([0xef, 0xbb, 0xbf, 0xc0]);
});

// HTTPS requests are proxied with CONNECT, but our test server is HTTP,
// which means that the proxy will receive GET http://localhost:port.
var proxiedCount = 0;
proxyServer.registerPrefixHandler("/", (request, response) => {
  ++proxiedCount;
  Assert.equal(request.path, "/binary.dat", `Proxy request ${proxiedCount}`);
  // Close connection without sending any response.
  response.seizePower();
  response.finish();
});

add_task(async function test_utils_fetch_binary() {
  let res = await Utils.fetch(`${SERVER_BASE_URL}/binary.dat`);

  Assert.equal(res.status, 201, "res.status");
  Assert.equal(res.statusText, "StatusLineHere", "res.statusText");
  Assert.equal(
    res.headers.get("headerName"),
    "HeaderValue: HeaderValueEnd",
    "Utils.fetch should return the header"
  );

  Assert.deepEqual(
    Array.from(new Uint8Array(await res.arrayBuffer())),
    INVALID_UTF_8_BYTES,
    "Binary response body should be returned as is"
  );
});

add_task(async function test_utils_fetch_binary_as_text() {
  let res = await Utils.fetch(`${SERVER_BASE_URL}/binary.dat`);
  Assert.deepEqual(
    Array.from(await res.text(), c => c.charCodeAt(0)),
    [65533],
    "Interpreted as UTF-8, the response becomes garbage"
  );
});

add_task(async function test_utils_fetch_binary_as_json() {
  let res = await Utils.fetch(`${SERVER_BASE_URL}/binary.dat`);
  await Assert.rejects(
    res.json(),
    /SyntaxError: JSON.parse: unexpected character/,
    "Binary data is invalid JSON"
  );
});

add_task(async function test_utils_fetch_has_conservative() {
  let channelPromise = TestUtils.topicObserved("http-on-modify-request");
  await Utils.fetch(`${SERVER_BASE_URL}/binary.dat`);

  let channel = (await channelPromise)[0].QueryInterface(Ci.nsIHttpChannel);

  Assert.equal(channel.URI.spec, `${SERVER_BASE_URL}/binary.dat`, "URL OK");

  let internalChannel = channel.QueryInterface(Ci.nsIHttpChannelInternal);
  Assert.ok(internalChannel.beConservative, "beConservative flag is set");
});

add_task(async function test_utils_fetch_has_conservative() {
  let channelPromise = TestUtils.topicObserved("http-on-modify-request");
  await Utils.fetch(`${SERVER_BASE_URL}/binary.dat`);

  let channel = (await channelPromise)[0].QueryInterface(Ci.nsIHttpChannel);

  Assert.equal(channel.URI.spec, `${SERVER_BASE_URL}/binary.dat`, "URL OK");

  let internalChannel = channel.QueryInterface(Ci.nsIHttpChannelInternal);
  Assert.ok(internalChannel.beConservative, "beConservative flag is set");
});

add_task(async function test_utils_fetch_with_bad_proxy() {
  Services.prefs.setIntPref("network.proxy.type", 1);
  Services.prefs.setStringPref("network.proxy.http", "127.0.0.1");
  Services.prefs.setIntPref("network.proxy.http_port", PROXY_PORT);
  Services.prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);

  // The URL that we're going to request.
  const DESTINATION_URL = `${SERVER_BASE_URL}/binary.dat`;

  Assert.equal(proxiedCount, 0, "Proxy not used yet");
  {
    info("Bad proxy, default prefs");
    let res = await Utils.fetch(DESTINATION_URL);
    Assert.equal(res.status, 201, "Bypassed bad proxy");
    // 10 instead of 1 because of reconnect attempts after a dropped request.
    Assert.equal(proxiedCount, 10, "Proxy was used by HttpChannel");
  }

  // Disables the failover logic from HttpChannel.
  Services.prefs.setBoolPref("network.proxy.failover_direct", false);
  proxiedCount = 0;
  {
    info("Bad proxy, disabled network.proxy.failover_direct");
    let res = await Utils.fetch(DESTINATION_URL);
    Assert.equal(res.status, 201, "Bypassed bad proxy");
    // 10 instead of 1 because of reconnect attempts after a dropped request.
    Assert.equal(proxiedCount, 10, "Proxy was used by ServiceRequest");
  }

  proxiedCount = 0;
  {
    info("Using internal option of Utils.fetch: bypassProxy=true");
    let res = await Utils.fetch(DESTINATION_URL, { bypassProxy: true });
    Assert.equal(res.status, 201, "Bypassed bad proxy");
    Assert.equal(proxiedCount, 0, "Not using proxy when bypassProxy=true");
  }

  // Disables the failover logic from ServiceRequest/Utils.fetch
  Services.prefs.setBoolPref("network.proxy.allow_bypass", false);
  proxiedCount = 0;

  info("Bad proxy, disabled network.proxy.allow_bypass");
  await Assert.rejects(
    Utils.fetch(DESTINATION_URL),
    /NetworkError/,
    "Bad proxy request should fail without failover"
  );
  // 10 instead of 1 because of reconnect attempts after a dropped request.
  Assert.equal(proxiedCount, 10, "Attempted to use proxy again");

  Services.prefs.clearUserPref("network.proxy.type");
  Services.prefs.clearUserPref("network.proxy.http");
  Services.prefs.clearUserPref("network.proxy.http_port");
  Services.prefs.clearUserPref("network.proxy.allow_hijacking_localhost");
  Services.prefs.clearUserPref("network.proxy.failover_direct");
  Services.prefs.clearUserPref("network.proxy.allow_bypass");
});