summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_trr_cancel.js
blob: ce2ae527219d84ac28d6939c76ad9a914f9382e2 (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
177
178
179
180
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

trr_test_setup();
registerCleanupFunction(async () => {
  trr_clear_prefs();
});

let trrServer = null;
add_task(async function start_trr_server() {
  trrServer = new TRRServer();
  registerCleanupFunction(async () => {
    await trrServer.stop();
  });
  await trrServer.start();
  dump(`port = ${trrServer.port}\n`);

  Services.prefs.setCharPref(
    "network.trr.uri",
    `https://foo.example.com:${trrServer.port}/dns-query`
  );
  Services.prefs.setIntPref("network.trr.mode", Ci.nsIDNSService.MODE_TRRFIRST);
});

add_task(async function sanity_check() {
  await trrServer.registerDoHAnswers("example.com", "A", {
    answers: [
      {
        name: "example.com",
        ttl: 55,
        type: "A",
        flush: false,
        data: "1.2.3.4",
      },
    ],
  });
  // Simple check to see that TRR works.
  await new TRRDNSListener("example.com", { expectedAnswer: "1.2.3.4" });
});

// Cancelling the request is not sync when using the socket process, so
// we skip this test when it's enabled.
add_task(
  { skip_if: () => mozinfo.socketprocess_networking },
  async function cancel_immediately() {
    await trrServer.registerDoHAnswers("example.org", "A", {
      answers: [
        {
          name: "example.org",
          ttl: 55,
          type: "A",
          flush: false,
          data: "2.3.4.5",
        },
      ],
    });
    let r1 = new TRRDNSListener("example.org", { expectedSuccess: false });
    let r2 = new TRRDNSListener("example.org", { expectedAnswer: "2.3.4.5" });
    r1.cancel();
    let { inStatus } = await r1;
    equal(inStatus, Cr.NS_ERROR_ABORT);
    await r2;
    equal(await trrServer.requestCount("example.org", "A"), 1);

    // Now we cancel both of them
    Services.dns.clearCache(true);
    r1 = new TRRDNSListener("example.org", { expectedSuccess: false });
    r2 = new TRRDNSListener("example.org", { expectedSuccess: false });
    r1.cancel();
    r2.cancel();
    ({ inStatus } = await r1);
    equal(inStatus, Cr.NS_ERROR_ABORT);
    ({ inStatus } = await r2);
    equal(inStatus, Cr.NS_ERROR_ABORT);
    await new Promise(resolve => do_timeout(50, resolve));
    equal(await trrServer.requestCount("example.org", "A"), 2);
  }
);

add_task(async function cancel_delayed() {
  Services.dns.clearCache(true);
  await trrServer.registerDoHAnswers("example.com", "A", {
    answers: [
      {
        name: "example.com",
        ttl: 55,
        type: "A",
        flush: false,
        data: "1.1.1.1",
      },
    ],
    delay: 500,
  });
  let r1 = new TRRDNSListener("example.com", { expectedSuccess: false });
  let r2 = new TRRDNSListener("example.com", { expectedAnswer: "1.1.1.1" });
  await new Promise(resolve => do_timeout(50, resolve));
  r1.cancel();
  let { inStatus } = await r1;
  equal(inStatus, Cr.NS_ERROR_ABORT);
  await r2;
});

add_task(async function cancel_after_completed() {
  Services.dns.clearCache(true);
  await trrServer.registerDoHAnswers("example.com", "A", {
    answers: [
      {
        name: "example.com",
        ttl: 55,
        type: "A",
        flush: false,
        data: "2.2.2.2",
      },
    ],
  });
  let r1 = new TRRDNSListener("example.com", { expectedAnswer: "2.2.2.2" });
  await r1;
  let r2 = new TRRDNSListener("example.com", { expectedAnswer: "2.2.2.2" });
  // Check that cancelling r1 after it's complete does not affect r2 in any way.
  r1.cancel();
  await r2;
});

add_task(async function clearCacheWhileResolving() {
  Services.dns.clearCache(true);
  await trrServer.registerDoHAnswers("example.com", "A", {
    answers: [
      {
        name: "example.com",
        ttl: 55,
        type: "A",
        flush: false,
        data: "3.3.3.3",
      },
    ],
    delay: 500,
  });
  // Check that calling clearCache does not leave the request hanging.
  let r1 = new TRRDNSListener("example.com", { expectedAnswer: "3.3.3.3" });
  let r2 = new TRRDNSListener("example.com", { expectedAnswer: "3.3.3.3" });
  Services.dns.clearCache(true);
  await r1;
  await r2;

  // Also check the same for HTTPS records
  await trrServer.registerDoHAnswers("httpsvc.com", "HTTPS", {
    answers: [
      {
        name: "httpsvc.com",
        ttl: 55,
        type: "HTTPS",
        flush: false,
        data: {
          priority: 1,
          name: "test.p1.com",
          values: [{ key: "alpn", value: ["h2", "h3"] }],
        },
      },
    ],
    delay: 500,
  });
  let r3 = new TRRDNSListener("httpsvc.com", {
    type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
  });
  let r4 = new TRRDNSListener("httpsvc.com", {
    type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
  });
  Services.dns.clearCache(true);
  await r3;
  await r4;
  equal(await trrServer.requestCount("httpsvc.com", "HTTPS"), 1);
  Services.dns.clearCache(true);
  await new TRRDNSListener("httpsvc.com", {
    type: Ci.nsIDNSService.RESOLVE_TYPE_HTTPSSVC,
  });
  equal(await trrServer.requestCount("httpsvc.com", "HTTPS"), 2);
});