Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_dns_proxy_bypass.js
blob8750e21819b5a09684f09c4ff3e106867f4bc3de
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 var prefs = Services.prefs;
9 function setup() {
10 prefs.setBoolPref("network.dns.notifyResolution", true);
11 prefs.setCharPref("network.proxy.socks", "127.0.0.1");
12 prefs.setIntPref("network.proxy.socks_port", 9000);
13 prefs.setIntPref("network.proxy.type", 1);
16 setup();
17 registerCleanupFunction(async () => {
18 prefs.clearUserPref("network.proxy.socks");
19 prefs.clearUserPref("network.proxy.socks_port");
20 prefs.clearUserPref("network.proxy.type");
21 prefs.clearUserPref("network.dns.notifyResolution");
22 });
24 var url = "ws://dnsleak.example.com";
26 var dnsRequestObserver = {
27 register() {
28 this.obs = Services.obs;
29 this.obs.addObserver(this, "dns-resolution-request");
32 unregister() {
33 if (this.obs) {
34 this.obs.removeObserver(this, "dns-resolution-request");
38 observe(subject, topic, data) {
39 if (topic == "dns-resolution-request") {
40 Assert.ok(!data.includes("dnsleak.example.com"), `no dnsleak: ${data}`);
45 function WSListener(closure) {
46 this._closure = closure;
48 WSListener.prototype = {
49 onAcknowledge() {},
50 onBinaryMessageAvailable() {},
51 onMessageAvailable() {},
52 onServerClose() {},
53 onStart() {},
54 onStop() {
55 dnsRequestObserver.unregister();
56 this._closure();
60 add_task(async function test_dns_websocket_channel() {
61 dnsRequestObserver.register();
63 var chan = Cc["@mozilla.org/network/protocol;1?name=ws"].createInstance(
64 Ci.nsIWebSocketChannel
67 var uri = Services.io.newURI(url);
68 chan.initLoadInfo(
69 null, // aLoadingNode
70 Services.scriptSecurityManager.createContentPrincipal(uri, {}),
71 null, // aTriggeringPrincipal
72 Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
73 Ci.nsIContentPolicy.TYPE_WEBSOCKET
76 await new Promise(resolve =>
77 chan.asyncOpen(uri, url, {}, 0, new WSListener(resolve), null)
79 });
81 add_task(async function test_dns_resolve_proxy() {
82 dnsRequestObserver.register();
84 let { error } = await new TRRDNSListener("dnsleak.example.com", {
85 expectEarlyFail: true,
86 });
87 Assert.equal(
88 error.result,
89 Cr.NS_ERROR_UNKNOWN_PROXY_HOST,
90 "error is NS_ERROR_UNKNOWN_PROXY_HOST"
92 dnsRequestObserver.unregister();
93 });