test_URIFixup_search.js (4269B)
1 const { AppConstants } = ChromeUtils.importESModule( 2 "resource://gre/modules/AppConstants.sys.mjs" 3 ); 4 5 var isWin = AppConstants.platform == "win"; 6 7 var data = [ 8 { 9 // Valid should not be changed. 10 wrong: "https://example.com/this/is/a/test.html", 11 fixed: "https://example.com/this/is/a/test.html", 12 }, 13 { 14 // Unrecognized protocols should be changed. 15 wrong: "whatever://this/is/a/test.html", 16 fixed: kSearchEngineURL.replace( 17 "{searchTerms}", 18 encodeURIComponent("whatever://this/is/a/test.html") 19 ), 20 }, 21 22 { 23 // Unrecognized protocols should be changed. 24 wrong: "whatever://this/is/a/test.html", 25 fixed: kPrivateSearchEngineURL.replace( 26 "{searchTerms}", 27 encodeURIComponent("whatever://this/is/a/test.html") 28 ), 29 inPrivateBrowsing: true, 30 }, 31 32 // The following tests check that when a user:password is present in the URL 33 // `user:` isn't treated as an unknown protocol thus leaking the user and 34 // password to the search engine. 35 { 36 wrong: "user:pass@example.com/this/is/a/test.html", 37 fixed: "http://user:pass@example.com/this/is/a/test.html", 38 }, 39 { 40 wrong: "user@example.com:8080/this/is/a/test.html", 41 fixed: "http://user@example.com:8080/this/is/a/test.html", 42 }, 43 { 44 wrong: "https:pass@example.com/this/is/a/test.html", 45 fixed: "https://pass@example.com/this/is/a/test.html", 46 }, 47 { 48 wrong: "user:pass@example.com:8080/this/is/a/test.html", 49 fixed: "http://user:pass@example.com:8080/this/is/a/test.html", 50 }, 51 { 52 wrong: "http:user:pass@example.com:8080/this/is/a/test.html", 53 fixed: "http://user:pass@example.com:8080/this/is/a/test.html", 54 }, 55 { 56 wrong: "ttp:user:pass@example.com:8080/this/is/a/test.html", 57 fixed: "http://user:pass@example.com:8080/this/is/a/test.html", 58 }, 59 { 60 wrong: "nonsense:user:pass@example.com:8080/this/is/a/test.html", 61 fixed: "http://nonsense:user%3Apass@example.com:8080/this/is/a/test.html", 62 }, 63 { 64 wrong: "user:@example.com:8080/this/is/a/test.html", 65 fixed: "http://user@example.com:8080/this/is/a/test.html", 66 }, 67 { 68 wrong: "//user:pass@example.com:8080/this/is/a/test.html", 69 fixed: 70 (isWin ? "http:" : "file://") + 71 "//user:pass@example.com:8080/this/is/a/test.html", 72 }, 73 { 74 wrong: "://user:pass@example.com:8080/this/is/a/test.html", 75 fixed: "http://user:pass@example.com:8080/this/is/a/test.html", 76 }, 77 { 78 wrong: "localhost:8080/?param=1", 79 fixed: "http://localhost:8080/?param=1", 80 }, 81 { 82 wrong: "localhost:8080?param=1", 83 fixed: "http://localhost:8080/?param=1", 84 }, 85 { 86 wrong: "localhost:8080#somewhere", 87 fixed: "http://localhost:8080/#somewhere", 88 }, 89 { 90 wrong: "whatever://this/is/a@b/test.html", 91 fixed: kSearchEngineURL.replace( 92 "{searchTerms}", 93 encodeURIComponent("whatever://this/is/a@b/test.html") 94 ), 95 }, 96 ]; 97 98 var extProtocolSvc = Cc[ 99 "@mozilla.org/uriloader/external-protocol-service;1" 100 ].getService(Ci.nsIExternalProtocolService); 101 102 if (extProtocolSvc && extProtocolSvc.externalProtocolHandlerExists("mailto")) { 103 data.push({ 104 wrong: "mailto:foo@bar.com", 105 fixed: "mailto:foo@bar.com", 106 }); 107 } 108 109 var len = data.length; 110 111 add_task(async function setup() { 112 await setupSearchService(); 113 await addTestEngines(); 114 115 Services.prefs.setBoolPref("keyword.enabled", true); 116 Services.prefs.setBoolPref("browser.search.separatePrivateDefault", true); 117 Services.prefs.setBoolPref( 118 "browser.search.separatePrivateDefault.ui.enabled", 119 true 120 ); 121 122 await Services.search.setDefault( 123 Services.search.getEngineByName(kSearchEngineID), 124 Ci.nsISearchService.CHANGE_REASON_UNKNOWN 125 ); 126 await Services.search.setDefaultPrivate( 127 Services.search.getEngineByName(kPrivateSearchEngineID), 128 Ci.nsISearchService.CHANGE_REASON_UNKNOWN 129 ); 130 }); 131 132 // Make sure we fix what needs fixing 133 add_task(function test_fix_unknown_schemes() { 134 for (let i = 0; i < len; ++i) { 135 let item = data[i]; 136 let flags = Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS; 137 if (item.inPrivateBrowsing) { 138 flags |= Services.uriFixup.FIXUP_FLAG_PRIVATE_CONTEXT; 139 } 140 let { preferredURI } = Services.uriFixup.getFixupURIInfo(item.wrong, flags); 141 Assert.equal(preferredURI.spec, item.fixed); 142 } 143 });