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