test_URIFixup.js (5130B)
1 const { PromiseTestUtils } = ChromeUtils.importESModule( 2 "resource://testing-common/PromiseTestUtils.sys.mjs" 3 ); 4 5 var pref = "browser.fixup.typo.scheme"; 6 7 var data = [ 8 { 9 // ttp -> http. 10 wrong: "ttp://www.example.com/", 11 fixed: "http://www.example.com/", 12 }, 13 { 14 // htp -> http. 15 wrong: "htp://www.example.com/", 16 fixed: "http://www.example.com/", 17 }, 18 { 19 // ttps -> https. 20 wrong: "ttps://www.example.com/", 21 fixed: "https://www.example.com/", 22 }, 23 { 24 // tps -> https. 25 wrong: "tps://www.example.com/", 26 fixed: "https://www.example.com/", 27 }, 28 { 29 // ps -> https. 30 wrong: "ps://www.example.com/", 31 fixed: "https://www.example.com/", 32 }, 33 { 34 // htps -> https. 35 wrong: "htps://www.example.com/", 36 fixed: "https://www.example.com/", 37 }, 38 { 39 // ile -> file. 40 wrong: "ile:///this/is/a/test.html", 41 fixed: "file:///this/is/a/test.html", 42 }, 43 { 44 // le -> file. 45 wrong: "le:///this/is/a/test.html", 46 fixed: "file:///this/is/a/test.html", 47 }, 48 { 49 // Replace ';' with ':'. 50 wrong: "http;//www.example.com/", 51 fixed: "http://www.example.com/", 52 noPrefValue: "http://http;//www.example.com/", 53 }, 54 { 55 // Missing ':'. 56 wrong: "https//www.example.com/", 57 fixed: "https://www.example.com/", 58 noPrefValue: "http://https//www.example.com/", 59 }, 60 { 61 // Missing ':' for file scheme. 62 wrong: "file///this/is/a/test.html", 63 fixed: "file:///this/is/a/test.html", 64 noPrefValue: "http://file///this/is/a/test.html", 65 }, 66 { 67 // Valid should not be changed. 68 wrong: "https://example.com/this/is/a/test.html", 69 fixed: "https://example.com/this/is/a/test.html", 70 }, 71 { 72 // Unmatched should not be changed. 73 wrong: "whatever://this/is/a/test.html", 74 fixed: "whatever://this/is/a/test.html", 75 }, 76 { 77 // Valid should not be changed. 78 wrong: "whatever://user:pass@example.com/test.html", 79 fixed: "whatever://user:pass@example.com/test.html", 80 }, 81 { 82 // Spaces before @ are valid if it appears after the domain. 83 wrong: "example.com/ @test.com", 84 fixed: "http://example.com/%20@test.com", 85 noPrefValue: "http://example.com/%20@test.com", 86 }, 87 ]; 88 89 var dontFixURIs = [ 90 { 91 input: " leadingSpaceUsername@example.com/", 92 testInfo: "dont fix usernames with leading space", 93 }, 94 { 95 input: "trailingSpacerUsername @example.com/", 96 testInfo: "dont fix usernames with trailing space", 97 }, 98 { 99 input: "multiple words username@example.com/", 100 testInfo: "dont fix usernames with multiple spaces", 101 }, 102 { 103 input: "one spaceTwo SpacesThree Spaces@example.com/", 104 testInfo: "dont match multiple consecutive spaces", 105 }, 106 { 107 input: " dontMatchCredentialsWithSpaces: secret password @example.com/", 108 testInfo: "dont fix credentials with spaces", 109 }, 110 ]; 111 112 var len = data.length; 113 114 add_task(async function setup() { 115 // Force search service to fail, so we do not have any engines that can 116 // interfere with this test. 117 // Search engine integration is tested in test_URIFixup_search.js. 118 Services.search.wrappedJSObject.errorToThrowInTest.type = "Settings"; 119 120 // When search service fails, we want the promise rejection to be uncaught 121 // so we can continue running the test. 122 PromiseTestUtils.expectUncaughtRejection( 123 /Fake Settings error during search service initialization./ 124 ); 125 126 try { 127 await setupSearchService(); 128 } catch {} 129 }); 130 131 // Make sure we fix what needs fixing when there is no pref set. 132 add_task(function test_unset_pref_fixes_typos() { 133 Services.prefs.clearUserPref(pref); 134 for (let i = 0; i < len; ++i) { 135 let item = data[i]; 136 let { preferredURI } = Services.uriFixup.getFixupURIInfo( 137 item.wrong, 138 Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS 139 ); 140 Assert.equal(preferredURI.spec, item.fixed); 141 } 142 }); 143 144 // Make sure we don't do anything when the pref is explicitly 145 // set to false. 146 add_task(function test_false_pref_keeps_typos() { 147 Services.prefs.setBoolPref(pref, false); 148 for (let i = 0; i < len; ++i) { 149 let item = data[i]; 150 let { preferredURI } = Services.uriFixup.getFixupURIInfo( 151 item.wrong, 152 Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS 153 ); 154 Assert.equal(preferredURI.spec, item.noPrefValue || item.wrong); 155 } 156 }); 157 158 // Finally, make sure we still fix what needs fixing if the pref is 159 // explicitly set to true. 160 add_task(function test_true_pref_fixes_typos() { 161 Services.prefs.setBoolPref(pref, true); 162 for (let i = 0; i < len; ++i) { 163 let item = data[i]; 164 let { preferredURI } = Services.uriFixup.getFixupURIInfo( 165 item.wrong, 166 Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS 167 ); 168 Assert.equal(preferredURI.spec, item.fixed); 169 } 170 }); 171 172 add_task(function test_dont_fix_uris() { 173 let dontFixLength = dontFixURIs.length; 174 for (let i = 0; i < dontFixLength; i++) { 175 let testCase = dontFixURIs[i]; 176 Assert.throws( 177 () => { 178 Services.uriFixup.getFixupURIInfo( 179 testCase.input, 180 Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS 181 ); 182 }, 183 /NS_ERROR_MALFORMED_URI/, 184 testCase.testInfo 185 ); 186 } 187 });