summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_dns.js
blob: 56bd3ae174700bfd0ffbade171e038f382dbe74d (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
167
168
169
170
171
172
173
174
175
176
"use strict";

// Some test machines and android are not returning ipv6, turn it
// off to get consistent test results.
Services.prefs.setBoolPref("network.dns.disableIPv6", true);

AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();

AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "1",
  "42"
);

function getExtension(background = undefined) {
  let manifest = {
    permissions: ["dns", "proxy"],
  };
  return ExtensionTestUtils.loadExtension({
    manifest,
    background() {
      browser.test.onMessage.addListener(async (msg, data) => {
        if (msg == "proxy") {
          await browser.proxy.settings.set({ value: data });
          browser.test.sendMessage("proxied");
          return;
        }
        browser.test.log(`=== dns resolve test ${JSON.stringify(data)}`);
        browser.dns
          .resolve(data.hostname, data.flags)
          .then(result => {
            browser.test.log(
              `=== dns resolve result ${JSON.stringify(result)}`
            );
            browser.test.sendMessage("resolved", result);
          })
          .catch(e => {
            browser.test.log(`=== dns resolve error ${e.message}`);
            browser.test.sendMessage("resolved", { message: e.message });
          });
      });
      browser.test.sendMessage("ready");
    },
    incognitoOverride: "spanning",
    useAddonManager: "temporary",
  });
}

const tests = [
  {
    request: {
      hostname: "localhost",
    },
    expect: {
      addresses: ["127.0.0.1"], // ipv6 disabled , "::1"
    },
  },
  {
    request: {
      hostname: "localhost",
      flags: ["offline"],
    },
    expect: {
      addresses: ["127.0.0.1"], // ipv6 disabled , "::1"
    },
  },
  {
    request: {
      hostname: "test.example",
    },
    expect: {
      // android will error with offline
      error: /NS_ERROR_UNKNOWN_HOST|NS_ERROR_OFFLINE/,
    },
  },
  {
    request: {
      hostname: "127.0.0.1",
      flags: ["canonical_name"],
    },
    expect: {
      canonicalName: "127.0.0.1",
      addresses: ["127.0.0.1"],
    },
  },
  {
    request: {
      hostname: "localhost",
      flags: ["disable_ipv6"],
    },
    expect: {
      addresses: ["127.0.0.1"],
    },
  },
];

add_setup(async function startup() {
  await AddonTestUtils.promiseStartupManager();
});

add_task(async function test_dns_resolve() {
  let extension = getExtension();
  await extension.startup();
  await extension.awaitMessage("ready");

  for (let test of tests) {
    extension.sendMessage("resolve", test.request);
    let result = await extension.awaitMessage("resolved");
    if (test.expect.error) {
      ok(
        test.expect.error.test(result.message),
        `expected error ${result.message}`
      );
    } else {
      equal(
        result.canonicalName,
        test.expect.canonicalName,
        "canonicalName match"
      );
      // It seems there are platform differences happening that make this
      // testing difficult. We're going to rely on other existing dns tests to validate
      // the dns service itself works and only validate that we're getting generally
      // expected results in the webext api.
      ok(
        result.addresses.length >= test.expect.addresses.length,
        "expected number of addresses returned"
      );
      if (test.expect.addresses.length && result.addresses.length) {
        ok(
          result.addresses.includes(test.expect.addresses[0]),
          "got expected ip address"
        );
      }
    }
  }

  await extension.unload();
});

add_task(async function test_dns_resolve_socks() {
  let extension = getExtension();
  await extension.startup();
  await extension.awaitMessage("ready");
  extension.sendMessage("proxy", {
    proxyType: "manual",
    socks: "127.0.0.1",
    socksVersion: 5,
    proxyDNS: true,
  });
  await extension.awaitMessage("proxied");
  equal(
    Services.prefs.getIntPref("network.proxy.type"),
    1 /* PROXYCONFIG_MANUAL */,
    "manual proxy"
  );
  equal(
    Services.prefs.getStringPref("network.proxy.socks"),
    "127.0.0.1",
    "socks proxy"
  );
  ok(
    Services.prefs.getBoolPref("network.proxy.socks_remote_dns"),
    "socks remote dns"
  );
  extension.sendMessage("resolve", {
    hostname: "mozilla.org",
  });
  let result = await extension.awaitMessage("resolved");
  ok(
    /NS_ERROR_UNKNOWN_PROXY_HOST/.test(result.message),
    `expected error ${result.message}`
  );
  await extension.unload();
});