summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_dns_cancel.js
blob: 60cff1cc528666d620611617a53c2c866ed14d30 (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
"use strict";

var dns = Cc["@mozilla.org/network/dns-service;1"].getService(Ci.nsIDNSService);

var hostname1 = "";
var hostname2 = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for (var i = 0; i < 20; i++) {
  hostname1 += possible.charAt(Math.floor(Math.random() * possible.length));
  hostname2 += possible.charAt(Math.floor(Math.random() * possible.length));
}

var requestList1Canceled1;
var requestList1Canceled2;
var requestList1NotCanceled;

var requestList2Canceled;
var requestList2NotCanceled;

var listener1 = {
  onLookupComplete(inRequest, inRecord, inStatus) {
    // One request should be resolved and two request should be canceled.
    if (inRequest == requestList1NotCanceled) {
      // This request should not be canceled.
      Assert.notEqual(inStatus, Cr.NS_ERROR_ABORT);

      do_test_finished();
    }
  },
  QueryInterface: ChromeUtils.generateQI(["nsIDNSListener"]),
};

var listener2 = {
  onLookupComplete(inRequest, inRecord, inStatus) {
    // One request should be resolved and the other canceled.
    if (inRequest == requestList2NotCanceled) {
      // The request should not be canceled.
      Assert.notEqual(inStatus, Cr.NS_ERROR_ABORT);

      do_test_finished();
    }
  },
  QueryInterface: ChromeUtils.generateQI(["nsIDNSListener"]),
};

const defaultOriginAttributes = {};

function run_test() {
  var threadManager = Cc["@mozilla.org/thread-manager;1"].getService(
    Ci.nsIThreadManager
  );
  var mainThread = threadManager.currentThread;

  var flags = Ci.nsIDNSService.RESOLVE_BYPASS_CACHE;

  // This one will be canceled with cancelAsyncResolve.
  requestList1Canceled1 = dns.asyncResolve(
    hostname2,
    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
    flags,
    null, // resolverInfo
    listener1,
    mainThread,
    defaultOriginAttributes
  );
  dns.cancelAsyncResolve(
    hostname2,
    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
    flags,
    null, // resolverInfo
    listener1,
    Cr.NS_ERROR_ABORT,
    defaultOriginAttributes
  );

  // This one will not be canceled.
  requestList1NotCanceled = dns.asyncResolve(
    hostname1,
    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
    flags,
    null, // resolverInfo
    listener1,
    mainThread,
    defaultOriginAttributes
  );

  // This one will be canceled with cancel(Cr.NS_ERROR_ABORT).
  requestList1Canceled2 = dns.asyncResolve(
    hostname1,
    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
    flags,
    null, // resolverInfo
    listener1,
    mainThread,
    defaultOriginAttributes
  );
  requestList1Canceled2.cancel(Cr.NS_ERROR_ABORT);

  // This one will not be canceled.
  requestList2NotCanceled = dns.asyncResolve(
    hostname1,
    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
    flags,
    null, // resolverInfo
    listener2,
    mainThread,
    defaultOriginAttributes
  );

  // This one will be canceled with cancel(Cr.NS_ERROR_ABORT).
  requestList2Canceled = dns.asyncResolve(
    hostname2,
    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
    flags,
    null, // resolverInfo
    listener2,
    mainThread,
    defaultOriginAttributes
  );
  requestList2Canceled.cancel(Cr.NS_ERROR_ABORT);

  do_test_pending();
  do_test_pending();
}