tor-browser

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

test_bug368702.js (4239B)


      1 "use strict";
      2 
      3 function run_test() {
      4  var tld = Services.eTLD;
      5  Assert.equal(tld.getPublicSuffixFromHost("localhost"), "localhost");
      6  Assert.equal(tld.getPublicSuffixFromHost("localhost."), "localhost.");
      7  Assert.equal(tld.getPublicSuffixFromHost("domain.com"), "com");
      8  Assert.equal(tld.getPublicSuffixFromHost("domain.com."), "com.");
      9  Assert.equal(tld.getPublicSuffixFromHost("domain.co.uk"), "co.uk");
     10  Assert.equal(tld.getPublicSuffixFromHost("domain.co.uk."), "co.uk.");
     11  Assert.equal(tld.getPublicSuffixFromHost("co.uk"), "co.uk");
     12  Assert.equal(tld.getBaseDomainFromHost("domain.co.uk"), "domain.co.uk");
     13  Assert.equal(tld.getBaseDomainFromHost("domain.co.uk."), "domain.co.uk.");
     14 
     15  try {
     16    tld.getPublicSuffixFromHost("");
     17    do_throw("this should fail");
     18  } catch (e) {
     19    Assert.equal(e.result, Cr.NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS);
     20  }
     21 
     22  try {
     23    tld.getBaseDomainFromHost("domain.co.uk", 1);
     24    do_throw("this should fail");
     25  } catch (e) {
     26    Assert.equal(e.result, Cr.NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS);
     27  }
     28 
     29  try {
     30    tld.getBaseDomainFromHost("co.uk");
     31    do_throw("this should fail");
     32  } catch (e) {
     33    Assert.equal(e.result, Cr.NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS);
     34  }
     35 
     36  try {
     37    tld.getBaseDomainFromHost("");
     38    do_throw("this should fail");
     39  } catch (e) {
     40    Assert.equal(e.result, Cr.NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS);
     41  }
     42 
     43  try {
     44    tld.getPublicSuffixFromHost("1.2.3.4");
     45    do_throw("this should fail");
     46  } catch (e) {
     47    Assert.equal(e.result, Cr.NS_ERROR_HOST_IS_IP_ADDRESS);
     48  }
     49 
     50  try {
     51    tld.getPublicSuffixFromHost("2010:836B:4179::836B:4179");
     52    do_throw("this should fail");
     53  } catch (e) {
     54    Assert.equal(e.result, Cr.NS_ERROR_HOST_IS_IP_ADDRESS);
     55  }
     56 
     57  try {
     58    tld.getPublicSuffixFromHost("3232235878");
     59    do_throw("this should fail");
     60  } catch (e) {
     61    Assert.equal(e.result, Cr.NS_ERROR_HOST_IS_IP_ADDRESS);
     62  }
     63 
     64  try {
     65    tld.getPublicSuffixFromHost("::ffff:192.9.5.5");
     66    do_throw("this should fail");
     67  } catch (e) {
     68    Assert.equal(e.result, Cr.NS_ERROR_HOST_IS_IP_ADDRESS);
     69  }
     70 
     71  try {
     72    tld.getPublicSuffixFromHost("::1");
     73    do_throw("this should fail");
     74  } catch (e) {
     75    Assert.equal(e.result, Cr.NS_ERROR_HOST_IS_IP_ADDRESS);
     76  }
     77 
     78  // Check IP addresses with trailing dot as well, Necko sometimes accepts
     79  // those (depending on operating system, see bug 380543)
     80  try {
     81    tld.getPublicSuffixFromHost("127.0.0.1.");
     82    do_throw("this should fail");
     83  } catch (e) {
     84    Assert.equal(e.result, Cr.NS_ERROR_HOST_IS_IP_ADDRESS);
     85  }
     86 
     87  try {
     88    tld.getPublicSuffixFromHost("::ffff:127.0.0.1.");
     89    do_throw("this should fail");
     90  } catch (e) {
     91    Assert.equal(e.result, Cr.NS_ERROR_HOST_IS_IP_ADDRESS);
     92  }
     93 
     94  // check normalization: output should be consistent with
     95  // nsIURI::GetAsciiHost(), i.e. lowercased and ASCII/ACE encoded
     96  var uri = Services.io.newURI("http://b\u00FCcher.co.uk");
     97  Assert.equal(tld.getBaseDomain(uri), "xn--bcher-kva.co.uk");
     98  Assert.equal(
     99    tld.getBaseDomainFromHost("b\u00FCcher.co.uk"),
    100    "xn--bcher-kva.co.uk"
    101  );
    102  Assert.equal(tld.getPublicSuffix(uri), "co.uk");
    103  Assert.equal(tld.getPublicSuffixFromHost("b\u00FCcher.co.uk"), "co.uk");
    104 
    105  // check that malformed hosts are rejected as invalid args
    106  try {
    107    tld.getBaseDomainFromHost("domain.co.uk..");
    108    do_throw("this should fail");
    109  } catch (e) {
    110    Assert.equal(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
    111  }
    112 
    113  try {
    114    tld.getBaseDomainFromHost("domain.co..uk");
    115    do_throw("this should fail");
    116  } catch (e) {
    117    Assert.equal(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
    118  }
    119 
    120  try {
    121    tld.getBaseDomainFromHost(".domain.co.uk");
    122    do_throw("this should fail");
    123  } catch (e) {
    124    Assert.equal(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
    125  }
    126 
    127  try {
    128    tld.getBaseDomainFromHost(".domain.co.uk");
    129    do_throw("this should fail");
    130  } catch (e) {
    131    Assert.equal(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
    132  }
    133 
    134  try {
    135    tld.getBaseDomainFromHost(".");
    136    do_throw("this should fail");
    137  } catch (e) {
    138    Assert.equal(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
    139  }
    140 
    141  try {
    142    tld.getBaseDomainFromHost("..");
    143    do_throw("this should fail");
    144  } catch (e) {
    145    Assert.equal(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
    146  }
    147 }