Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_dns_offline.js
blobdb9c43629233bebc22b1828b2c861c056a7b51b1
1 "use strict";
3 var ioService = Services.io;
4 var prefs = Services.prefs;
5 var mainThread = Services.tm.currentThread;
7 var listener1 = {
8   onLookupComplete(inRequest, inRecord, inStatus) {
9     Assert.equal(inStatus, Cr.NS_ERROR_OFFLINE);
10     test2();
11     do_test_finished();
12   },
15 var listener2 = {
16   onLookupComplete(inRequest, inRecord, inStatus) {
17     Assert.equal(inStatus, Cr.NS_OK);
18     inRecord.QueryInterface(Ci.nsIDNSAddrRecord);
19     var answer = inRecord.getNextAddrAsString();
20     Assert.ok(answer == "127.0.0.1" || answer == "::1");
21     test3();
22     do_test_finished();
23   },
26 var listener3 = {
27   onLookupComplete(inRequest, inRecord, inStatus) {
28     Assert.equal(inStatus, Cr.NS_OK);
29     inRecord.QueryInterface(Ci.nsIDNSAddrRecord);
30     var answer = inRecord.getNextAddrAsString();
31     Assert.ok(answer == "127.0.0.1" || answer == "::1");
32     cleanup();
33     do_test_finished();
34   },
37 const defaultOriginAttributes = {};
39 function run_test() {
40   do_test_pending();
41   prefs.setBoolPref("network.dns.offline-localhost", false);
42   // We always resolve localhost as it's hardcoded without the following pref:
43   prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
44   ioService.offline = true;
45   try {
46     Services.dns.asyncResolve(
47       "localhost",
48       Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
49       0,
50       null, // resolverInfo
51       listener1,
52       mainThread,
53       defaultOriginAttributes
54     );
55   } catch (e) {
56     Assert.equal(e.result, Cr.NS_ERROR_OFFLINE);
57     test2();
58     do_test_finished();
59   }
62 function test2() {
63   do_test_pending();
64   prefs.setBoolPref("network.dns.offline-localhost", true);
65   ioService.offline = false;
66   ioService.offline = true;
67   // we need to let the main thread run and apply the changes
68   do_timeout(0, test2Continued);
71 function test2Continued() {
72   Services.dns.asyncResolve(
73     "localhost",
74     Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
75     0,
76     null, // resolverInfo
77     listener2,
78     mainThread,
79     defaultOriginAttributes
80   );
83 function test3() {
84   do_test_pending();
85   ioService.offline = false;
86   // we need to let the main thread run and apply the changes
87   do_timeout(0, test3Continued);
90 function test3Continued() {
91   Services.dns.asyncResolve(
92     "localhost",
93     Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
94     0,
95     null, // resolverInfo
96     listener3,
97     mainThread,
98     defaultOriginAttributes
99   );
102 function cleanup() {
103   prefs.clearUserPref("network.dns.offline-localhost");
104   prefs.clearUserPref("network.proxy.allow_hijacking_localhost");