tor-browser

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

test_nsEffectiveTLDService_getSite.js (3232B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 /**
      5 * Tests getSite and getSchemelessSite with example arguments
      6 */
      7 
      8 "use strict";
      9 
     10 add_task(() => {
     11  for (let [originString, result] of [
     12    ["http://.", null],
     13    ["http://com", "http://com"],
     14    ["http://test", "http://test"],
     15    ["http://test.", "http://test."],
     16    ["http://[::1]", "http://[::1]"],
     17    ["http://[::1]:8888", "http://[::1]"],
     18    ["http://localhost", "http://localhost"],
     19    ["http://127.0.0.1", "http://127.0.0.1"],
     20    ["http://user:pass@[::1]", "http://[::1]"],
     21    ["http://example.com", "http://example.com"],
     22    ["https://example.com", "https://example.com"],
     23    ["https://test.example.com", "https://example.com"],
     24    ["https://test1.test2.example.com", "https://example.com"],
     25    ["https://test1.test2.example.co.uk", "https://example.co.uk"],
     26    ["https://test.example.com:8888/index.html", "https://example.com"],
     27    [
     28      "https://test1.test2.example.公司.香港",
     29      "https://example.xn--55qx5d.xn--j6w193g",
     30    ],
     31  ]) {
     32    let origin = Services.io.newURI(originString);
     33    if (result === null) {
     34      Assert.throws(
     35        () => {
     36          Services.eTLD.getSite(origin);
     37        },
     38        /NS_ERROR_ILLEGAL_VALUE/,
     39        "Invalid origin for getSite throws"
     40      );
     41    } else {
     42      let answer = Services.eTLD.getSite(origin);
     43      Assert.equal(
     44        answer,
     45        result,
     46        `"${originString}" should have site ${result}, got ${answer}.`
     47      );
     48    }
     49  }
     50 });
     51 
     52 add_task(() => {
     53  for (let [originString, result] of [
     54    ["http://com", "com"],
     55    ["http://test", "test"],
     56    ["http://test.", "test."],
     57    ["http://[::1]", "[::1]"],
     58    ["http://[::1]:8888", "[::1]"],
     59    ["http://localhost", "localhost"],
     60    ["http://127.0.0.1", "127.0.0.1"],
     61    ["http://user:pass@[::1]", "[::1]"],
     62    ["http://example.com", "example.com"],
     63    ["https://example.com", "example.com"],
     64    ["https://test.example.com", "example.com"],
     65    ["https://test1.test2.example.com", "example.com"],
     66    ["https://test1.test2.example.co.uk", "example.co.uk"],
     67    [
     68      "https://test1.test2.example.\u516c\u53f8.\u9999\u6e2f",
     69      "example.xn--55qx5d.xn--j6w193g",
     70    ],
     71  ]) {
     72    let origin = Services.io.newURI(originString);
     73    let answer = Services.eTLD.getSchemelessSite(origin);
     74    Assert.equal(
     75      answer,
     76      result,
     77      `"${originString}" should have schemeless site ${result}, got ${answer}.`
     78    );
     79  }
     80 });
     81 
     82 add_task(function test_getSchemelessSiteFromHost() {
     83  for (let [host, result] of [
     84    ["com", "com"],
     85    ["test", "test"],
     86    ["test.", "test."],
     87    ["::1", "[::1]"],
     88    ["localhost", "localhost"],
     89    ["127.0.0.1", "127.0.0.1"],
     90    ["example.com", "example.com"],
     91    ["test.example.com", "example.com"],
     92    ["test1.test2.example.com", "example.com"],
     93    ["test1.test2.example.co.uk", "example.co.uk"],
     94    [
     95      "test1.test2.example.\u516c\u53f8.\u9999\u6e2f",
     96      "example.xn--55qx5d.xn--j6w193g",
     97    ],
     98  ]) {
     99    let answer = Services.eTLD.getSchemelessSiteFromHost(host);
    100    Assert.equal(
    101      answer,
    102      result,
    103      `"${host}" should have schemeless site ${result}, got ${answer}.`
    104    );
    105  }
    106 });