tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_trr_nat64.js (3505B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const override = Cc["@mozilla.org/network/native-dns-override;1"].getService(
      8  Ci.nsINativeDNSResolverOverride
      9 );
     10 
     11 Services.prefs.setBoolPref(
     12  "network.connectivity-service.wait_for_idle_startup",
     13  false
     14 );
     15 trr_test_setup();
     16 registerCleanupFunction(async () => {
     17  Services.prefs.clearUserPref("network.connectivity-service.nat64-prefix");
     18  override.clearOverrides();
     19  trr_clear_prefs();
     20 });
     21 
     22 /**
     23 * Waits for an observer notification to fire.
     24 *
     25 * @param {string} topic The notification topic.
     26 * @returns {Promise} A promise that fulfills when the notification is fired.
     27 */
     28 function promiseObserverNotification(topic, matchFunc) {
     29  return new Promise(resolve => {
     30    Services.obs.addObserver(function observe(subject, topic1, data) {
     31      let matches = typeof matchFunc != "function" || matchFunc(subject, data);
     32      if (!matches) {
     33        return;
     34      }
     35      Services.obs.removeObserver(observe, topic);
     36      resolve({ subject, data });
     37    }, topic);
     38  });
     39 }
     40 
     41 function makeChan(url) {
     42  let chan = NetUtil.newChannel({
     43    uri: url,
     44    loadUsingSystemPrincipal: true,
     45  }).QueryInterface(Ci.nsIHttpChannel);
     46  return chan;
     47 }
     48 
     49 function channelOpenPromise(chan) {
     50  return new Promise(resolve => {
     51    function finish(req, buffer) {
     52      resolve([req, buffer]);
     53    }
     54    chan.asyncOpen(new ChannelListener(finish));
     55  });
     56 }
     57 
     58 add_task(async function test_add_nat64_prefix_to_trr() {
     59  let trrServer = new TRRServer();
     60  registerCleanupFunction(async () => {
     61    await trrServer.stop();
     62  });
     63  await trrServer.start();
     64  dump(`port = ${trrServer.port()}\n`);
     65  let chan = makeChan(`https://localhost:${trrServer.port()}/test?bla=some`);
     66  let [, resp] = await channelOpenPromise(chan);
     67  equal(resp, "<h1> 404 Path not found: /test</h1>");
     68  Services.dns.clearCache(true);
     69  override.addIPOverride("ipv4only.arpa", "fe80::9b2b:c000:00aa");
     70  Services.prefs.setCharPref(
     71    "network.connectivity-service.nat64-prefix",
     72    "ae80::3b1b:c343:1133"
     73  );
     74 
     75  let topic = "network:connectivity-service:dns-checks-complete";
     76  if (mozinfo.socketprocess_networking) {
     77    topic += "-from-socket-process";
     78  }
     79  let notification = promiseObserverNotification(topic);
     80  Services.obs.notifyObservers(null, "network:captive-portal-connectivity");
     81  await notification;
     82 
     83  Services.prefs.setIntPref("network.trr.mode", 2);
     84  Services.prefs.setCharPref(
     85    "network.trr.uri",
     86    `https://foo.example.com:${trrServer.port()}/dns-query`
     87  );
     88 
     89  await trrServer.registerDoHAnswers("xyz.foo", "A", {
     90    answers: [
     91      {
     92        name: "xyz.foo",
     93        ttl: 55,
     94        type: "A",
     95        flush: false,
     96        data: "1.2.3.4",
     97      },
     98    ],
     99  });
    100  let { inRecord } = await new TRRDNSListener("xyz.foo", {
    101    expectedSuccess: false,
    102  });
    103 
    104  inRecord.QueryInterface(Ci.nsIDNSAddrRecord);
    105  Assert.equal(
    106    inRecord.getNextAddrAsString(),
    107    "1.2.3.4",
    108    `Checking that native IPv4 addresses have higher priority.`
    109  );
    110 
    111  Assert.equal(
    112    inRecord.getNextAddrAsString(),
    113    "ae80::3b1b:102:304",
    114    `Checking the manually entered NAT64-prefixed address is in the middle.`
    115  );
    116 
    117  Assert.equal(
    118    inRecord.getNextAddrAsString(),
    119    "fe80::9b2b:102:304",
    120    `Checking that the NAT64-prefixed address is appended at the back.`
    121  );
    122 
    123  await trrServer.stop();
    124 });