summaryrefslogtreecommitdiff
path: root/netwerk/test/unit/test_dns_offline.js
blob: 87a9ad8b1e3039f4acc924f2f44b274395de0d8b (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
var dns = Cc["@mozilla.org/network/dns-service;1"].getService(Ci.nsIDNSService);
var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
var threadManager = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
var mainThread = threadManager.currentThread;

var listener1 = {
  onLookupComplete: function(inRequest, inRecord, inStatus) {
    do_check_eq(inStatus, Cr.NS_ERROR_OFFLINE);
    test2();
    do_test_finished();
  }
};

var listener2 = {
  onLookupComplete: function(inRequest, inRecord, inStatus) {
    do_check_eq(inStatus, Cr.NS_OK);
    var answer = inRecord.getNextAddrAsString();
    do_check_true(answer == "127.0.0.1" || answer == "::1");
    test3();
    do_test_finished();
  }
};

var listener3 = {
  onLookupComplete: function(inRequest, inRecord, inStatus) {
    do_check_eq(inStatus, Cr.NS_OK);
    var answer = inRecord.getNextAddrAsString();
    do_check_true(answer == "127.0.0.1" || answer == "::1");
    cleanup();
    do_test_finished();
  }
};

function run_test() {
  do_test_pending();
  prefs.setBoolPref("network.dns.offline-localhost", false);
  ioService.offline = true;
  try {
    dns.asyncResolve("localhost", 0, listener1, mainThread);
  } catch (e) {
      do_check_eq(e.result, Cr.NS_ERROR_OFFLINE);
      test2();
      do_test_finished();
  }
}

function test2() {
  do_test_pending();
  prefs.setBoolPref("network.dns.offline-localhost", true);
  ioService.offline = false;
  ioService.offline = true;
  // we need to let the main thread run and apply the changes
  do_timeout(0, test2Continued);
}

function test2Continued() {
  dns.asyncResolve("localhost", 0, listener2, mainThread);
}

function test3() {
  do_test_pending();
  ioService.offline = false;
  // we need to let the main thread run and apply the changes
  do_timeout(0, test3Continued);
}

function test3Continued() {
  dns.asyncResolve("localhost", 0, listener3, mainThread);
}

function cleanup() {
  prefs.clearUserPref("network.dns.offline-localhost");
}