tor-browser

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

test_dns_cancel.js (3017B)


      1 "use strict";
      2 
      3 var hostname1 = "";
      4 var hostname2 = "";
      5 var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
      6 
      7 for (var i = 0; i < 20; i++) {
      8  hostname1 += possible.charAt(Math.floor(Math.random() * possible.length));
      9  hostname2 += possible.charAt(Math.floor(Math.random() * possible.length));
     10 }
     11 
     12 var requestList1Canceled2;
     13 var requestList1NotCanceled;
     14 
     15 var requestList2Canceled;
     16 var requestList2NotCanceled;
     17 
     18 var listener1 = {
     19  onLookupComplete(inRequest, inRecord, inStatus) {
     20    // One request should be resolved and two request should be canceled.
     21    if (inRequest == requestList1NotCanceled) {
     22      // This request should not be canceled.
     23      Assert.notEqual(inStatus, Cr.NS_ERROR_ABORT);
     24 
     25      do_test_finished();
     26    }
     27  },
     28  QueryInterface: ChromeUtils.generateQI(["nsIDNSListener"]),
     29 };
     30 
     31 var listener2 = {
     32  onLookupComplete(inRequest, inRecord, inStatus) {
     33    // One request should be resolved and the other canceled.
     34    if (inRequest == requestList2NotCanceled) {
     35      // The request should not be canceled.
     36      Assert.notEqual(inStatus, Cr.NS_ERROR_ABORT);
     37 
     38      do_test_finished();
     39    }
     40  },
     41  QueryInterface: ChromeUtils.generateQI(["nsIDNSListener"]),
     42 };
     43 
     44 const defaultOriginAttributes = {};
     45 
     46 function run_test() {
     47  var mainThread = Services.tm.currentThread;
     48 
     49  var flags = Ci.nsIDNSService.RESOLVE_BYPASS_CACHE;
     50 
     51  // This one will be canceled with cancelAsyncResolve.
     52  Services.dns.asyncResolve(
     53    hostname2,
     54    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
     55    flags,
     56    null, // resolverInfo
     57    listener1,
     58    mainThread,
     59    defaultOriginAttributes
     60  );
     61  Services.dns.cancelAsyncResolve(
     62    hostname2,
     63    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
     64    flags,
     65    null, // resolverInfo
     66    listener1,
     67    Cr.NS_ERROR_ABORT,
     68    defaultOriginAttributes
     69  );
     70 
     71  // This one will not be canceled.
     72  requestList1NotCanceled = Services.dns.asyncResolve(
     73    hostname1,
     74    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
     75    flags,
     76    null, // resolverInfo
     77    listener1,
     78    mainThread,
     79    defaultOriginAttributes
     80  );
     81 
     82  // This one will be canceled with cancel(Cr.NS_ERROR_ABORT).
     83  requestList1Canceled2 = Services.dns.asyncResolve(
     84    hostname1,
     85    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
     86    flags,
     87    null, // resolverInfo
     88    listener1,
     89    mainThread,
     90    defaultOriginAttributes
     91  );
     92  requestList1Canceled2.cancel(Cr.NS_ERROR_ABORT);
     93 
     94  // This one will not be canceled.
     95  requestList2NotCanceled = Services.dns.asyncResolve(
     96    hostname1,
     97    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
     98    flags,
     99    null, // resolverInfo
    100    listener2,
    101    mainThread,
    102    defaultOriginAttributes
    103  );
    104 
    105  // This one will be canceled with cancel(Cr.NS_ERROR_ABORT).
    106  requestList2Canceled = Services.dns.asyncResolve(
    107    hostname2,
    108    Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT,
    109    flags,
    110    null, // resolverInfo
    111    listener2,
    112    mainThread,
    113    defaultOriginAttributes
    114  );
    115  requestList2Canceled.cancel(Cr.NS_ERROR_ABORT);
    116 
    117  do_test_pending();
    118  do_test_pending();
    119 }