tor-browser

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

test_URIFixup_info.js (31067B)


      1 const { AppConstants } = ChromeUtils.importESModule(
      2  "resource://gre/modules/AppConstants.sys.mjs"
      3 );
      4 
      5 const kForceDNSLookup = "browser.fixup.dns_first_for_single_words";
      6 
      7 // TODO(bug 1522134), this test should also use
      8 // combinations of the following flags.
      9 var flagInputs = [
     10  Services.uriFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP,
     11  Services.uriFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP |
     12    Services.uriFixup.FIXUP_FLAG_PRIVATE_CONTEXT,
     13  Services.uriFixup.FIXUP_FLAGS_MAKE_ALTERNATE_URI,
     14  Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS,
     15  // This should not really generate a search, but it does, see Bug 1588118.
     16  Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS |
     17    Services.uriFixup.FIXUP_FLAG_PRIVATE_CONTEXT,
     18 ];
     19 
     20 function schemeTypoOnly(flags) {
     21  return flags & Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS;
     22 }
     23 
     24 /*
     25  The following properties are supported for these test cases:
     26  {
     27    input: "", // Input string, required
     28    fixedURI: "", // Expected fixedURI
     29    alternateURI: "", // Expected alternateURI
     30    keywordLookup: false, // Whether a keyword lookup is expected
     31    protocolChange: false, // Whether a protocol change is expected
     32    inWhitelist: false, // Whether the input host is in the whitelist
     33    affectedByDNSForSingleWordHosts: false, // Whether the input host could be a host, but is normally assumed to be a keyword query
     34  }
     35 */
     36 var testcases = [
     37  {
     38    input: "about:home",
     39    fixedURI: "about:home",
     40  },
     41  {
     42    input: "http://www.mozilla.org",
     43    fixedURI: "http://www.mozilla.org/",
     44  },
     45  {
     46    input: "http://127.0.0.1/",
     47    fixedURI: "http://127.0.0.1/",
     48  },
     49  {
     50    input: "file:///foo/bar",
     51    fixedURI: "file:///foo/bar",
     52  },
     53  {
     54    input: "://www.mozilla.org",
     55    fixedURI: "http://www.mozilla.org/",
     56    protocolChange: true,
     57  },
     58  {
     59    input: "www.mozilla.org",
     60    fixedURI: "http://www.mozilla.org/",
     61    protocolChange: true,
     62  },
     63  {
     64    input: "http://mozilla/",
     65    fixedURI: "http://mozilla/",
     66    alternateURI: "https://www.mozilla.com/",
     67  },
     68  {
     69    input: "http://test./",
     70    fixedURI: "http://test./",
     71    alternateURI: "https://www.test./",
     72  },
     73  {
     74    input: "127.0.0.1",
     75    fixedURI: "http://127.0.0.1/",
     76    protocolChange: true,
     77  },
     78  {
     79    input: "1.2.3.4/",
     80    fixedURI: "http://1.2.3.4/",
     81    protocolChange: true,
     82  },
     83  {
     84    input: "1.2.3.4/foo",
     85    fixedURI: "http://1.2.3.4/foo",
     86    protocolChange: true,
     87  },
     88  {
     89    input: "1.2.3.4:8000",
     90    fixedURI: "http://1.2.3.4:8000/",
     91    protocolChange: true,
     92  },
     93  {
     94    input: "1.2.3.4:8000/",
     95    fixedURI: "http://1.2.3.4:8000/",
     96    protocolChange: true,
     97  },
     98  {
     99    input: "1.2.3.4:8000/foo",
    100    fixedURI: "http://1.2.3.4:8000/foo",
    101    protocolChange: true,
    102  },
    103  {
    104    input: "192.168.10.110",
    105    fixedURI: "http://192.168.10.110/",
    106    protocolChange: true,
    107  },
    108  {
    109    input: "192.168.10.110/123",
    110    fixedURI: "http://192.168.10.110/123",
    111    protocolChange: true,
    112  },
    113  {
    114    input: "192.168.10.110/123foo",
    115    fixedURI: "http://192.168.10.110/123foo",
    116    protocolChange: true,
    117  },
    118  {
    119    input: "192.168.10.110:1234/123",
    120    fixedURI: "http://192.168.10.110:1234/123",
    121    protocolChange: true,
    122  },
    123  {
    124    input: "192.168.10.110:1234/123foo",
    125    fixedURI: "http://192.168.10.110:1234/123foo",
    126    protocolChange: true,
    127  },
    128  {
    129    input: "1.2.3",
    130    fixedURI: "http://1.2.0.3/",
    131    protocolChange: true,
    132  },
    133  {
    134    input: "1.2.3/",
    135    fixedURI: "http://1.2.0.3/",
    136    protocolChange: true,
    137  },
    138  {
    139    input: "1.2.3/foo",
    140    fixedURI: "http://1.2.0.3/foo",
    141    protocolChange: true,
    142  },
    143  {
    144    input: "1.2.3/123",
    145    fixedURI: "http://1.2.0.3/123",
    146    protocolChange: true,
    147  },
    148  {
    149    input: "1.2.3:8000",
    150    fixedURI: "http://1.2.0.3:8000/",
    151    protocolChange: true,
    152  },
    153  {
    154    input: "1.2.3:8000/",
    155    fixedURI: "http://1.2.0.3:8000/",
    156    protocolChange: true,
    157  },
    158  {
    159    input: "1.2.3:8000/foo",
    160    fixedURI: "http://1.2.0.3:8000/foo",
    161    protocolChange: true,
    162  },
    163  {
    164    input: "1.2.3:8000/123",
    165    fixedURI: "http://1.2.0.3:8000/123",
    166    protocolChange: true,
    167  },
    168  {
    169    input: "http://1.2.3",
    170    fixedURI: "http://1.2.0.3/",
    171  },
    172  {
    173    input: "http://1.2.3/",
    174    fixedURI: "http://1.2.0.3/",
    175  },
    176  {
    177    input: "http://1.2.3/foo",
    178    fixedURI: "http://1.2.0.3/foo",
    179  },
    180  {
    181    input: "[::1]",
    182    fixedURI: "http://[::1]/",
    183    protocolChange: true,
    184  },
    185  {
    186    input: "[::1]/",
    187    fixedURI: "http://[::1]/",
    188    protocolChange: true,
    189  },
    190  {
    191    input: "[::1]:8000",
    192    fixedURI: "http://[::1]:8000/",
    193    protocolChange: true,
    194  },
    195  {
    196    input: "[::1]:8000/",
    197    fixedURI: "http://[::1]:8000/",
    198    protocolChange: true,
    199  },
    200  {
    201    input: "[[::1]]/",
    202    keywordLookup: true,
    203  },
    204  {
    205    input: "[fe80:cd00:0:cde:1257:0:211e:729c]",
    206    fixedURI: "http://[fe80:cd00:0:cde:1257:0:211e:729c]/",
    207    protocolChange: true,
    208  },
    209  {
    210    input: "[64:ff9b::8.8.8.8]",
    211    fixedURI: "http://[64:ff9b::808:808]/",
    212    protocolChange: true,
    213  },
    214  {
    215    input: "[64:ff9b::8.8.8.8]/~moz",
    216    fixedURI: "http://[64:ff9b::808:808]/~moz",
    217    protocolChange: true,
    218  },
    219  {
    220    input: "[::1][::1]",
    221    keywordLookup: true,
    222  },
    223  {
    224    input: "[::1][100",
    225    keywordLookup: true,
    226  },
    227  {
    228    input: "[::1]]",
    229    keywordLookup: true,
    230  },
    231  {
    232    input: "1234",
    233    fixedURI: "http://0.0.4.210/",
    234    keywordLookup: true,
    235    protocolChange: true,
    236    affectedByDNSForSingleWordHosts: true,
    237  },
    238  {
    239    input: "whitelisted/foo.txt",
    240    fixedURI: "http://whitelisted/foo.txt",
    241    alternateURI: "https://www.whitelisted.com/foo.txt",
    242    protocolChange: true,
    243  },
    244  {
    245    input: "mozilla",
    246    fixedURI: "http://mozilla/",
    247    alternateURI: "https://www.mozilla.com/",
    248    keywordLookup: true,
    249    protocolChange: true,
    250    affectedByDNSForSingleWordHosts: true,
    251  },
    252  {
    253    input: "test.",
    254    fixedURI: "http://test./",
    255    alternateURI: "https://www.test./",
    256    keywordLookup: true,
    257    protocolChange: true,
    258    affectedByDNSForSingleWordHosts: true,
    259  },
    260  {
    261    input: ".test",
    262    fixedURI: "http://.test/",
    263    alternateURI: "https://www.test/",
    264    keywordLookup: true,
    265    protocolChange: true,
    266    affectedByDNSForSingleWordHosts: true,
    267  },
    268  {
    269    input: "mozilla is amazing",
    270    keywordLookup: true,
    271  },
    272  {
    273    input: "search ?mozilla",
    274    keywordLookup: true,
    275  },
    276  {
    277    input: "mozilla .com",
    278    keywordLookup: true,
    279  },
    280  {
    281    input: "what if firefox?",
    282    keywordLookup: true,
    283  },
    284  {
    285    input: "london's map",
    286    keywordLookup: true,
    287  },
    288  {
    289    input: "mozilla ",
    290    fixedURI: "http://mozilla/",
    291    alternateURI: "https://www.mozilla.com/",
    292    keywordLookup: true,
    293    protocolChange: true,
    294    affectedByDNSForSingleWordHosts: true,
    295  },
    296  {
    297    input: "   mozilla  ",
    298    fixedURI: "http://mozilla/",
    299    alternateURI: "https://www.mozilla.com/",
    300    keywordLookup: true,
    301    protocolChange: true,
    302    affectedByDNSForSingleWordHosts: true,
    303  },
    304  {
    305    input: "mozilla \\",
    306    keywordLookup: true,
    307  },
    308  {
    309    input: "mozilla \\ foo.txt",
    310    keywordLookup: true,
    311  },
    312  {
    313    input: "mozilla \\\r foo.txt",
    314    keywordLookup: true,
    315  },
    316  {
    317    input: "mozilla\n",
    318    fixedURI: "http://mozilla/",
    319    alternateURI: "https://www.mozilla.com/",
    320    keywordLookup: true,
    321    protocolChange: true,
    322    affectedByDNSForSingleWordHosts: true,
    323  },
    324  {
    325    input: "mozilla \r\n",
    326    fixedURI: "http://mozilla/",
    327    alternateURI: "https://www.mozilla.com/",
    328    keywordLookup: true,
    329    protocolChange: true,
    330    affectedByDNSForSingleWordHosts: true,
    331  },
    332  {
    333    input: "moz\r\nfirefox\nos\r",
    334    fixedURI: "http://mozfirefoxos/",
    335    alternateURI: "https://www.mozfirefoxos.com/",
    336    keywordLookup: true,
    337    protocolChange: true,
    338    affectedByDNSForSingleWordHosts: true,
    339  },
    340  {
    341    input: "moz\r\n firefox\n",
    342    keywordLookup: true,
    343  },
    344  {
    345    input: "",
    346    keywordLookup: true,
    347  },
    348  {
    349    input: "[]",
    350    keywordLookup: true,
    351  },
    352  {
    353    input: "http://whitelisted/",
    354    fixedURI: "http://whitelisted/",
    355    alternateURI: "https://www.whitelisted.com/",
    356    inWhitelist: true,
    357  },
    358  {
    359    input: "whitelisted",
    360    fixedURI: "http://whitelisted/",
    361    alternateURI: "https://www.whitelisted.com/",
    362    protocolChange: true,
    363    inWhitelist: true,
    364  },
    365  {
    366    input: "whitelisted.",
    367    fixedURI: "http://whitelisted./",
    368    alternateURI: "https://www.whitelisted./",
    369    protocolChange: true,
    370    inWhitelist: true,
    371  },
    372  {
    373    input: "mochi.test",
    374    fixedURI: "http://mochi.test/",
    375    alternateURI: "https://www.mochi.test/",
    376    protocolChange: true,
    377    inWhitelist: true,
    378  },
    379  // local.domain is a whitelisted suffix...
    380  {
    381    input: "some.local.domain",
    382    fixedURI: "http://some.local.domain/",
    383    protocolChange: true,
    384    inWhitelist: true,
    385  },
    386  // ...but .domain is not.
    387  {
    388    input: "some.domain",
    389    fixedURI: "http://some.domain/",
    390    alternateURI: "https://www.some.domain/",
    391    keywordLookup: true,
    392    protocolChange: true,
    393    affectedByDNSForSingleWordHosts: true,
    394  },
    395  {
    396    input: "café.com",
    397    fixedURI: "http://xn--caf-dma.com/",
    398    alternateURI: "https://www.xn--caf-dma.com/",
    399    protocolChange: true,
    400  },
    401  {
    402    input: "mozilla.nonexistent",
    403    fixedURI: "http://mozilla.nonexistent/",
    404    alternateURI: "https://www.mozilla.nonexistent/",
    405    keywordLookup: true,
    406    protocolChange: true,
    407    affectedByDNSForSingleWordHosts: true,
    408  },
    409  {
    410    input: "mochi.ocm",
    411    fixedURI: "http://mochi.com/",
    412    alternateURI: "https://www.mochi.com/",
    413    protocolChange: true,
    414  },
    415  {
    416    input: "47.6182,-122.830",
    417    keywordLookup: true,
    418  },
    419  {
    420    input: "-47.6182,-23.51",
    421    keywordLookup: true,
    422  },
    423  {
    424    input: "-22.14,23.51-",
    425    fixedURI: "http://-22.14,23.51-/",
    426    keywordLookup: true,
    427    protocolChange: true,
    428    affectedByDNSForSingleWordHosts: true,
    429  },
    430  {
    431    input: "32.7",
    432    fixedURI: "http://32.0.0.7/",
    433    keywordLookup: true,
    434    protocolChange: true,
    435    affectedByDNSForSingleWordHosts: true,
    436  },
    437  {
    438    input: "5+2",
    439    fixedURI: "http://5+2/",
    440    alternateURI: "https://www.5+2.com/",
    441    keywordLookup: true,
    442    protocolChange: true,
    443    affectedByDNSForSingleWordHosts: true,
    444  },
    445  {
    446    input: "5/2",
    447    fixedURI: "http://0.0.0.5/2",
    448    keywordLookup: true,
    449    protocolChange: true,
    450    affectedByDNSForSingleWordHosts: true,
    451  },
    452  {
    453    input: "moz ?.::%27",
    454    keywordLookup: true,
    455  },
    456  {
    457    input: "mozilla.com/?q=search",
    458    fixedURI: "http://mozilla.com/?q=search",
    459    alternateURI: "https://www.mozilla.com/?q=search",
    460    protocolChange: true,
    461  },
    462  {
    463    input: "mozilla.com?q=search",
    464    fixedURI: "http://mozilla.com/?q=search",
    465    alternateURI: "https://www.mozilla.com/?q=search",
    466    protocolChange: true,
    467  },
    468  {
    469    input: "mozilla.com ?q=search",
    470    keywordLookup: true,
    471  },
    472  {
    473    input: "mozilla.com.?q=search",
    474    fixedURI: "http://mozilla.com./?q=search",
    475    protocolChange: true,
    476  },
    477  {
    478    input: "mozilla.com'?q=search",
    479    fixedURI: "http://mozilla.com/?q=search",
    480    alternateURI: "https://www.mozilla.com/?q=search",
    481    protocolChange: true,
    482  },
    483  {
    484    input: "mozilla.com':search",
    485    keywordLookup: true,
    486  },
    487  {
    488    input: "[mozilla]",
    489    keywordLookup: true,
    490  },
    491  {
    492    input: "':?",
    493    fixedURI: "http://'/?",
    494    alternateURI: "https://www.'.com/?",
    495    keywordLookup: true,
    496    protocolChange: true,
    497    affectedByDNSForSingleWordHosts: true,
    498  },
    499  {
    500    input: "whitelisted?.com",
    501    fixedURI: "http://whitelisted/?.com",
    502    alternateURI: "https://www.whitelisted.com/?.com",
    503    protocolChange: true,
    504  },
    505  {
    506    input: "?'.com",
    507    keywordLookup: true,
    508  },
    509  {
    510    input: ".com",
    511    keywordLookup: true,
    512    affectedByDNSForSingleWordHosts: true,
    513    fixedURI: "http://.com/",
    514    alternateURI: "https://www.com/",
    515    protocolChange: true,
    516  },
    517  {
    518    input: "' ?.com",
    519    keywordLookup: true,
    520  },
    521  {
    522    input: "?mozilla",
    523    keywordLookup: true,
    524  },
    525  {
    526    input: "??mozilla",
    527    keywordLookup: true,
    528  },
    529  {
    530    input: "mozilla/",
    531    fixedURI: "http://mozilla/",
    532    alternateURI: "https://www.mozilla.com/",
    533    protocolChange: true,
    534  },
    535  {
    536    input: "mozilla",
    537    fixedURI: "http://mozilla/",
    538    alternateURI: "https://www.mozilla.com/",
    539    protocolChange: true,
    540    keywordLookup: true,
    541    affectedByDNSForSingleWordHosts: true,
    542  },
    543  {
    544    input: "mozilla5/2",
    545    fixedURI: "http://mozilla5/2",
    546    alternateURI: "https://www.mozilla5.com/2",
    547    protocolChange: true,
    548    keywordLookup: true,
    549    affectedByDNSForSingleWordHosts: true,
    550  },
    551  {
    552    input: "mozilla/foo",
    553    fixedURI: "http://mozilla/foo",
    554    alternateURI: "https://www.mozilla.com/foo",
    555    protocolChange: true,
    556    keywordLookup: true,
    557    affectedByDNSForSingleWordHosts: true,
    558  },
    559  {
    560    input: "mozilla\\",
    561    fixedURI: "http://mozilla/",
    562    alternateURI: "https://www.mozilla.com/",
    563    keywordLookup: true,
    564    protocolChange: true,
    565    affectedByDNSForSingleWordHosts: true,
    566  },
    567  {
    568    input: "localhost",
    569    fixedURI: "http://localhost/",
    570    keywordLookup: true,
    571    protocolChange: true,
    572    affectedByDNSForSingleWordHosts: true,
    573  },
    574  {
    575    input: "http",
    576    fixedURI: "http://http/",
    577    keywordLookup: true,
    578    protocolChange: true,
    579    affectedByDNSForSingleWordHosts: true,
    580  },
    581  {
    582    input: "https",
    583    fixedURI: "http://https/",
    584    keywordLookup: true,
    585    protocolChange: true,
    586    affectedByDNSForSingleWordHosts: true,
    587  },
    588  {
    589    input: "localhost:8080",
    590    fixedURI: "http://localhost:8080/",
    591    protocolChange: true,
    592  },
    593  {
    594    input: "plonk:8080",
    595    fixedURI: "http://plonk:8080/",
    596    protocolChange: true,
    597  },
    598  {
    599    input: "plonk:8080?test",
    600    fixedURI: "http://plonk:8080/?test",
    601    protocolChange: true,
    602  },
    603  {
    604    input: "plonk:8080#test",
    605    fixedURI: "http://plonk:8080/#test",
    606    protocolChange: true,
    607  },
    608  {
    609    input: "plonk/ #",
    610    fixedURI: "http://plonk/%20#",
    611    alternateURI: "https://www.plonk.com/%20#",
    612    protocolChange: true,
    613    keywordLookup: false,
    614  },
    615  {
    616    input: "blah.com.",
    617    fixedURI: "http://blah.com./",
    618    protocolChange: true,
    619  },
    620  {
    621    input:
    622      "\u10E0\u10D4\u10D2\u10D8\u10E1\u10E2\u10E0\u10D0\u10EA\u10D8\u10D0.\u10D2\u10D4",
    623    fixedURI: "http://xn--lodaehvb5cdik4g.xn--node/",
    624    alternateURI: "https://www.xn--lodaehvb5cdik4g.xn--node/",
    625    protocolChange: true,
    626  },
    627  {
    628    input: " \t mozilla.org/\t \t ",
    629    fixedURI: "http://mozilla.org/",
    630    alternateURI: "https://www.mozilla.org/",
    631    protocolChange: true,
    632  },
    633  {
    634    input: " moz\ti\tlla.org ",
    635    keywordLookup: true,
    636  },
    637  {
    638    input: "mozilla/",
    639    fixedURI: "http://mozilla/",
    640    alternateURI: "https://www.mozilla.com/",
    641    protocolChange: true,
    642  },
    643  {
    644    input: "mozilla/ test /",
    645    fixedURI: "http://mozilla/%20test%20/",
    646    alternateURI: "https://www.mozilla.com/%20test%20/",
    647    protocolChange: true,
    648  },
    649  {
    650    input: "mozilla /test/",
    651    keywordLookup: true,
    652  },
    653  {
    654    input: "pserver:8080",
    655    fixedURI: "http://pserver:8080/",
    656    protocolChange: true,
    657  },
    658  {
    659    input: "http;mozilla",
    660    fixedURI: "http://http;mozilla/",
    661    alternateURI: "https://www.http;mozilla.com/",
    662    keywordLookup: true,
    663    protocolChange: true,
    664    affectedByDNSForSingleWordHosts: true,
    665  },
    666  {
    667    input: "http//mozilla.org",
    668    fixedURI: "http://mozilla.org/",
    669    shouldRunTest: flags =>
    670      flags & Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS,
    671  },
    672  {
    673    input: "http//mozilla.org",
    674    fixedURI: "http://http//mozilla.org",
    675    keywordLookup: true,
    676    protocolChange: true,
    677    affectedByDNSForSingleWordHosts: true,
    678    shouldRunTest: flags =>
    679      !(flags & Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS),
    680  },
    681  {
    682    input: "www.mozilla",
    683    fixedURI: "http://www.mozilla/",
    684    protocolChange: true,
    685  },
    686  {
    687    input: "https://sub.www..mozilla...org/",
    688    fixedURI: "https://sub.www.mozilla.org/",
    689  },
    690  {
    691    input: "sub.www..mozilla...org/",
    692    fixedURI: "http://sub.www.mozilla.org/",
    693    protocolChange: true,
    694  },
    695  {
    696    input: "sub.www..mozilla...org",
    697    fixedURI: "http://sub.www.mozilla.org/",
    698    protocolChange: true,
    699  },
    700  {
    701    input: "www...mozilla",
    702    fixedURI: "http://www.mozilla/",
    703    keywordLookup: true,
    704    protocolChange: true,
    705    shouldRunTest: flags =>
    706      !gSingleWordDNSLookup &&
    707      flags & Services.uriFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP,
    708  },
    709  {
    710    input: "www...mozilla",
    711    fixedURI: "http://www.mozilla/",
    712    protocolChange: true,
    713    shouldRunTest: flags =>
    714      gSingleWordDNSLookup ||
    715      !(flags & Services.uriFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP),
    716  },
    717  {
    718    input: "mozilla...org",
    719    fixedURI: "http://mozilla.org/",
    720    protocolChange: true,
    721  },
    722  {
    723    input: "モジラ...org",
    724    fixedURI: "http://xn--yck6dwa.org/",
    725    protocolChange: true,
    726  },
    727  {
    728    input: "user@localhost",
    729    fixedURI: "http://user@localhost/",
    730    protocolChange: true,
    731    shouldRunTest: () => gSingleWordDNSLookup,
    732  },
    733  {
    734    input: "user@localhost",
    735    fixedURI: "http://user@localhost/",
    736    keywordLookup: true,
    737    protocolChange: true,
    738    shouldRunTest: () => !gSingleWordDNSLookup,
    739  },
    740  {
    741    input: "user@192.168.0.1",
    742    fixedURI: "http://user@192.168.0.1/",
    743    protocolChange: true,
    744  },
    745  {
    746    input: "user@dummy-host",
    747    fixedURI: "http://user@dummy-host/",
    748    protocolChange: true,
    749    shouldRunTest: () => gSingleWordDNSLookup,
    750  },
    751  {
    752    input: "user@dummy-host",
    753    fixedURI: "http://user@dummy-host/",
    754    keywordLookup: true,
    755    protocolChange: true,
    756    shouldRunTest: () => !gSingleWordDNSLookup,
    757  },
    758  {
    759    input: "user:pass@dummy-host",
    760    fixedURI: "http://user:pass@dummy-host/",
    761    protocolChange: true,
    762  },
    763  {
    764    input: ":pass@dummy-host",
    765    fixedURI: "http://:pass@dummy-host/",
    766    protocolChange: true,
    767  },
    768  {
    769    input: "user@dummy-host/path",
    770    fixedURI: "http://user@dummy-host/path",
    771    protocolChange: true,
    772    shouldRunTest: () => gSingleWordDNSLookup,
    773  },
    774  {
    775    input: "jar:file:///omni.ja!/",
    776    fixedURI: "jar:file:///omni.ja!/",
    777  },
    778  {
    779    input: "example.comm.",
    780    fixedURI: "http://example.comm./",
    781    protocolChange: true,
    782  },
    783  {
    784    input: "example。com。",
    785    fixedURI: "http://example.com./",
    786    protocolChange: true,
    787  },
    788  {
    789    input: "example。comm。",
    790    fixedURI: "http://example.comm./",
    791    protocolChange: true,
    792    keywordLookup: true,
    793    affectedByDNSForSingleWordHosts: true,
    794  },
    795 
    796  {
    797    input: "0.0.0.0",
    798    fixedURI: "http://127.0.0.1/",
    799    protocolChange: true,
    800    shouldRunTest: schemeTypoOnly,
    801  },
    802  {
    803    input: "0.0.0.0/",
    804    fixedURI: "http://127.0.0.1/",
    805    protocolChange: true,
    806    shouldRunTest: schemeTypoOnly,
    807  },
    808  {
    809    input: "0.0.0.0:8080",
    810    fixedURI: "http://127.0.0.1:8080/",
    811    protocolChange: true,
    812    shouldRunTest: schemeTypoOnly,
    813  },
    814  {
    815    input: "0.0.0.0:8080/blah?blah",
    816    fixedURI: "http://127.0.0.1:8080/blah?blah",
    817    protocolChange: true,
    818    shouldRunTest: schemeTypoOnly,
    819  },
    820  {
    821    input: "http://0.0.0.0",
    822    fixedURI: "http://127.0.0.1/",
    823    shouldRunTest: schemeTypoOnly,
    824  },
    825  {
    826    input: "http://0.0.0.0/",
    827    fixedURI: "http://127.0.0.1/",
    828    shouldRunTest: schemeTypoOnly,
    829  },
    830  {
    831    input: "http://0.0.0.0:8080",
    832    fixedURI: "http://127.0.0.1:8080/",
    833    shouldRunTest: schemeTypoOnly,
    834  },
    835  {
    836    input: "http://0.0.0.0:8080/blah?blah",
    837    fixedURI: "http://127.0.0.1:8080/blah?blah",
    838    shouldRunTest: schemeTypoOnly,
    839  },
    840  {
    841    input: "https://0.0.0.0",
    842    fixedURI: "https://127.0.0.1/",
    843    shouldRunTest: schemeTypoOnly,
    844  },
    845  {
    846    input: "https://0.0.0.0/",
    847    fixedURI: "https://127.0.0.1/",
    848    shouldRunTest: schemeTypoOnly,
    849  },
    850  {
    851    input: "https://0.0.0.0:8080",
    852    fixedURI: "https://127.0.0.1:8080/",
    853    shouldRunTest: schemeTypoOnly,
    854  },
    855  {
    856    input: "https://0.0.0.0:8080/blah?blah",
    857    fixedURI: "https://127.0.0.1:8080/blah?blah",
    858    shouldRunTest: schemeTypoOnly,
    859  },
    860 
    861  {
    862    input: "[::]",
    863    fixedURI: "http://[::1]/",
    864    protocolChange: true,
    865    shouldRunTest: schemeTypoOnly,
    866  },
    867  {
    868    input: "[::]/",
    869    fixedURI: "http://[::1]/",
    870    protocolChange: true,
    871    shouldRunTest: schemeTypoOnly,
    872  },
    873  {
    874    input: "[::]:8080",
    875    fixedURI: "http://[::1]:8080/",
    876    protocolChange: true,
    877    shouldRunTest: schemeTypoOnly,
    878  },
    879  {
    880    input: "[::]:8080/blah?blah",
    881    fixedURI: "http://[::1]:8080/blah?blah",
    882    protocolChange: true,
    883    shouldRunTest: schemeTypoOnly,
    884  },
    885  {
    886    input: "http://[::]",
    887    fixedURI: "http://[::1]/",
    888    shouldRunTest: schemeTypoOnly,
    889  },
    890  {
    891    input: "http://[::]/",
    892    fixedURI: "http://[::1]/",
    893    shouldRunTest: schemeTypoOnly,
    894  },
    895  {
    896    input: "http://[::]:8080",
    897    fixedURI: "http://[::1]:8080/",
    898    shouldRunTest: schemeTypoOnly,
    899  },
    900  {
    901    input: "http://[::]:8080/blah?blah",
    902    fixedURI: "http://[::1]:8080/blah?blah",
    903    shouldRunTest: schemeTypoOnly,
    904  },
    905  {
    906    input: "https://[::]",
    907    fixedURI: "https://[::1]/",
    908    shouldRunTest: schemeTypoOnly,
    909  },
    910  {
    911    input: "https://[::]/",
    912    fixedURI: "https://[::1]/",
    913    shouldRunTest: schemeTypoOnly,
    914  },
    915  {
    916    input: "https://[::]:8080",
    917    fixedURI: "https://[::1]:8080/",
    918    shouldRunTest: schemeTypoOnly,
    919  },
    920  {
    921    input: "https://[::]:8080/blah?blah",
    922    fixedURI: "https://[::1]:8080/blah?blah",
    923    shouldRunTest: schemeTypoOnly,
    924  },
    925 ];
    926 
    927 if (AppConstants.platform == "win") {
    928  testcases.push({
    929    input: "C:\\some\\file.txt",
    930    fixedURI: "file:///C:/some/file.txt",
    931    protocolChange: true,
    932  });
    933  testcases.push({
    934    input: "//mozilla",
    935    fixedURI: "http://mozilla/",
    936    alternateURI: "https://www.mozilla.com/",
    937    protocolChange: true,
    938  });
    939  testcases.push({
    940    input: "/a",
    941    fixedURI: "http://a/",
    942    alternateURI: "https://www.a.com/",
    943    keywordLookup: true,
    944    protocolChange: true,
    945    affectedByDNSForSingleWordHosts: true,
    946  });
    947 } else {
    948  const homeDir = Services.dirsvc.get("Home", Ci.nsIFile).path;
    949  const homeBase = AppConstants.platform == "macosx" ? "/Users" : "/home";
    950 
    951  testcases.push({
    952    input: "~",
    953    fixedURI: `file://${homeDir}`,
    954    protocolChange: true,
    955  });
    956  testcases.push({
    957    input: "~/foo",
    958    fixedURI: `file://${homeDir}/foo`,
    959    protocolChange: true,
    960  });
    961  testcases.push({
    962    input: "~foo",
    963    fixedURI: `file://${homeBase}/foo`,
    964    protocolChange: true,
    965  });
    966  testcases.push({
    967    input: "~foo/bar",
    968    fixedURI: `file://${homeBase}/foo/bar`,
    969    protocolChange: true,
    970  });
    971  testcases.push({
    972    input: "/some/file.txt",
    973    fixedURI: "file:///some/file.txt",
    974    protocolChange: true,
    975  });
    976  testcases.push({
    977    input: "//mozilla",
    978    fixedURI: "file:////mozilla",
    979    protocolChange: true,
    980  });
    981  testcases.push({
    982    input: "/a",
    983    fixedURI: "file:///a",
    984    protocolChange: true,
    985  });
    986 }
    987 
    988 function sanitize(input) {
    989  return input.replace(/\r|\n/g, "").trim();
    990 }
    991 
    992 add_task(async function setup() {
    993  // FIXME: the test fails without setting this to false. Bug 1995919.
    994  Services.prefs.setBoolPref("browser.fixup.domainwhitelist.localhost", false);
    995  var prefList = [
    996    "browser.fixup.typo.scheme",
    997    "keyword.enabled",
    998    "browser.fixup.domainwhitelist.whitelisted",
    999    "browser.fixup.domainsuffixwhitelist.test",
   1000    "browser.fixup.domainsuffixwhitelist.local.domain",
   1001    "browser.search.separatePrivateDefault",
   1002    "browser.search.separatePrivateDefault.ui.enabled",
   1003  ];
   1004  for (let pref of prefList) {
   1005    Services.prefs.setBoolPref(pref, true);
   1006  }
   1007 
   1008  await setupSearchService();
   1009  await addTestEngines();
   1010 
   1011  await Services.search.setDefault(
   1012    Services.search.getEngineByName(kSearchEngineID),
   1013    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
   1014  );
   1015  await Services.search.setDefaultPrivate(
   1016    Services.search.getEngineByName(kPrivateSearchEngineID),
   1017    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
   1018  );
   1019 });
   1020 
   1021 var gSingleWordDNSLookup = false;
   1022 add_task(async function run_test() {
   1023  // Only keywordlookup things should be affected by requiring a DNS lookup for single-word hosts:
   1024  info(
   1025    "Check only keyword lookup testcases should be affected by requiring DNS for single hosts"
   1026  );
   1027  let affectedTests = testcases.filter(
   1028    t => !t.keywordLookup && t.affectedByDNSForSingleWordHosts
   1029  );
   1030  if (affectedTests.length) {
   1031    for (let testcase of affectedTests) {
   1032      info("Affected: " + testcase.input);
   1033    }
   1034  }
   1035  Assert.equal(affectedTests.length, 0);
   1036  await do_single_test_run();
   1037  gSingleWordDNSLookup = true;
   1038  await do_single_test_run();
   1039  gSingleWordDNSLookup = false;
   1040  await Services.search.setDefault(
   1041    Services.search.getEngineByName(kPostSearchEngineID),
   1042    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
   1043  );
   1044  await do_single_test_run();
   1045 });
   1046 
   1047 async function do_single_test_run() {
   1048  Services.prefs.setBoolPref(kForceDNSLookup, gSingleWordDNSLookup);
   1049  let relevantTests = gSingleWordDNSLookup
   1050    ? testcases.filter(t => t.keywordLookup)
   1051    : testcases;
   1052 
   1053  let engine = await Services.search.getDefault();
   1054  let engineUrl =
   1055    engine.name == kPostSearchEngineID
   1056      ? kPostSearchEngineURL
   1057      : kSearchEngineURL;
   1058  let privateEngine = await Services.search.getDefaultPrivate();
   1059  let privateEngineUrl = kPrivateSearchEngineURL;
   1060 
   1061  for (let {
   1062    input: testInput,
   1063    fixedURI: expectedFixedURI,
   1064    alternateURI: alternativeURI,
   1065    keywordLookup: expectKeywordLookup,
   1066    protocolChange: expectProtocolChange,
   1067    inWhitelist: inWhitelist,
   1068    affectedByDNSForSingleWordHosts: affectedByDNSForSingleWordHosts,
   1069    shouldRunTest,
   1070  } of relevantTests) {
   1071    // Explicitly force these into a boolean
   1072    expectKeywordLookup = !!expectKeywordLookup;
   1073    expectProtocolChange = !!expectProtocolChange;
   1074    inWhitelist = !!inWhitelist;
   1075    affectedByDNSForSingleWordHosts = !!affectedByDNSForSingleWordHosts;
   1076 
   1077    expectKeywordLookup =
   1078      expectKeywordLookup &&
   1079      (!affectedByDNSForSingleWordHosts || !gSingleWordDNSLookup);
   1080 
   1081    for (let flags of flagInputs) {
   1082      info(
   1083        'Checking "' +
   1084          testInput +
   1085          '" with flags ' +
   1086          flags +
   1087          " (DNS lookup for single words: " +
   1088          (gSingleWordDNSLookup ? "yes" : "no") +
   1089          ")"
   1090      );
   1091 
   1092      if (shouldRunTest && !shouldRunTest(flags)) {
   1093        continue;
   1094      }
   1095 
   1096      let URIInfo;
   1097      try {
   1098        URIInfo = Services.uriFixup.getFixupURIInfo(testInput, flags);
   1099      } catch (ex) {
   1100        // Both APIs should return an error in the same cases.
   1101        info("Caught exception: " + ex);
   1102        Assert.equal(expectedFixedURI, null);
   1103        continue;
   1104      }
   1105 
   1106      // Check the fixedURI:
   1107      let makeAlternativeURI =
   1108        flags & Services.uriFixup.FIXUP_FLAGS_MAKE_ALTERNATE_URI;
   1109 
   1110      if (makeAlternativeURI && alternativeURI != null) {
   1111        Assert.equal(
   1112          URIInfo.fixedURI.spec,
   1113          alternativeURI,
   1114          "should have gotten alternate URI"
   1115        );
   1116      } else {
   1117        Assert.equal(
   1118          URIInfo.fixedURI && URIInfo.fixedURI.spec,
   1119          expectedFixedURI,
   1120          "should get correct fixed URI"
   1121        );
   1122      }
   1123 
   1124      // Check booleans on input:
   1125      let couldDoKeywordLookup =
   1126        flags & Services.uriFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
   1127      Assert.equal(
   1128        !!URIInfo.keywordProviderName,
   1129        couldDoKeywordLookup && expectKeywordLookup,
   1130        "keyword lookup as expected"
   1131      );
   1132 
   1133      let expectProtocolChangeAfterAlternate = false;
   1134      // If alternativeURI was created, the protocol of the URI
   1135      // might have been changed to browser.fixup.alternate.protocol
   1136      // If the protocol is not the same as what was in expectedFixedURI,
   1137      // the protocol must've changed in the fixup process.
   1138      if (
   1139        makeAlternativeURI &&
   1140        alternativeURI != null &&
   1141        !expectedFixedURI.startsWith(URIInfo.fixedURI.scheme)
   1142      ) {
   1143        expectProtocolChangeAfterAlternate = true;
   1144      }
   1145 
   1146      Assert.equal(
   1147        URIInfo.fixupChangedProtocol,
   1148        expectProtocolChange || expectProtocolChangeAfterAlternate,
   1149        "protocol change as expected"
   1150      );
   1151      Assert.equal(
   1152        URIInfo.fixupCreatedAlternateURI,
   1153        makeAlternativeURI && alternativeURI != null,
   1154        "alternative URI as expected"
   1155      );
   1156 
   1157      // Check the preferred URI
   1158      if (couldDoKeywordLookup) {
   1159        if (expectKeywordLookup) {
   1160          if (!inWhitelist) {
   1161            let urlparamInput = encodeURIComponent(sanitize(testInput)).replace(
   1162              /%20/g,
   1163              "+"
   1164            );
   1165            // If the input starts with `?`, then URIInfo.preferredURI.spec will omit it
   1166            // In order to test this behaviour, remove `?` only if it is the first character
   1167            if (urlparamInput.startsWith("%3F")) {
   1168              urlparamInput = urlparamInput.replace("%3F", "");
   1169            }
   1170            let isPrivate =
   1171              flags & Services.uriFixup.FIXUP_FLAG_PRIVATE_CONTEXT;
   1172            let searchEngineUrl = isPrivate ? privateEngineUrl : engineUrl;
   1173            let searchURL = searchEngineUrl.replace(
   1174              "{searchTerms}",
   1175              urlparamInput
   1176            );
   1177            let spec = URIInfo.preferredURI.spec.replace(/%27/g, "'");
   1178            Assert.equal(spec, searchURL, "should get correct search URI");
   1179            let providerName = isPrivate ? privateEngine.name : engine.name;
   1180            Assert.equal(
   1181              URIInfo.keywordProviderName,
   1182              providerName,
   1183              "should get correct provider name"
   1184            );
   1185            // Also check keywordToURI() uses the right engine.
   1186            let kwInfo = Services.uriFixup.keywordToURI(
   1187              urlparamInput,
   1188              isPrivate
   1189            );
   1190            Assert.equal(kwInfo.providerName, URIInfo.providerName);
   1191            if (providerName == kPostSearchEngineID) {
   1192              Assert.ok(kwInfo.postData);
   1193              let submission = engine.getSubmission(urlparamInput);
   1194              let enginePostData = NetUtil.readInputStreamToString(
   1195                submission.postData,
   1196                submission.postData.available()
   1197              );
   1198              let postData = NetUtil.readInputStreamToString(
   1199                kwInfo.postData,
   1200                kwInfo.postData.available()
   1201              );
   1202              Assert.equal(postData, enginePostData);
   1203            }
   1204          } else {
   1205            Assert.equal(
   1206              URIInfo.preferredURI,
   1207              null,
   1208              "not expecting a preferred URI"
   1209            );
   1210          }
   1211        } else {
   1212          Assert.equal(
   1213            URIInfo.preferredURI.spec,
   1214            URIInfo.fixedURI.spec,
   1215            "fixed URI should match"
   1216          );
   1217        }
   1218      } else {
   1219        // In these cases, we should never be doing a keyword lookup and
   1220        // the fixed URI should be preferred:
   1221        let prefURI = URIInfo.preferredURI && URIInfo.preferredURI.spec;
   1222        let fixedURI = URIInfo.fixedURI && URIInfo.fixedURI.spec;
   1223        Assert.equal(prefURI, fixedURI, "fixed URI should be same as expected");
   1224      }
   1225      Assert.equal(
   1226        sanitize(testInput),
   1227        URIInfo.originalInput,
   1228        "should mirror original input"
   1229      );
   1230    }
   1231  }
   1232 }