forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1744243: Fallback to nsIHandlerService if don't trust nsIExternal…
…ProtocolService. r=mak Differential Revision: https://phabricator.services.mozilla.com/D153836
- Loading branch information
Daisuke Akatsuka
committed
Aug 10, 2022
1 parent
c976443
commit 5b88a5d
Showing
3 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
docshell/test/unit/test_URIFixup_external_protocol_fallback.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
"use strict"; | ||
|
||
// Test whether fallback mechanism is working if don't trust nsIExternalProtocolService. | ||
|
||
const { MockRegistrar } = ChromeUtils.import( | ||
"resource://testing-common/MockRegistrar.jsm" | ||
); | ||
|
||
add_task(async function setup() { | ||
info( | ||
"Prepare mock nsIExternalProtocolService whose externalProtocolHandlerExists returns always true" | ||
); | ||
const externalProtocolService = Cc[ | ||
"@mozilla.org/uriloader/external-protocol-service;1" | ||
].getService(Ci.nsIExternalProtocolService); | ||
const mockId = MockRegistrar.register( | ||
"@mozilla.org/uriloader/external-protocol-service;1", | ||
{ | ||
getProtocolHandlerInfo: scheme => | ||
externalProtocolService.getProtocolHandlerInfo(scheme), | ||
externalProtocolHandlerExists: () => true, | ||
QueryInterface: ChromeUtils.generateQI(["nsIExternalProtocolService"]), | ||
} | ||
); | ||
const mockExternalProtocolService = Cc[ | ||
"@mozilla.org/uriloader/external-protocol-service;1" | ||
].getService(Ci.nsIExternalProtocolService); | ||
Assert.ok( | ||
mockExternalProtocolService.externalProtocolHandlerExists("__invalid__"), | ||
"Mock service is working" | ||
); | ||
|
||
info("Register new dummy protocol"); | ||
const dummyProtocolHandlerInfo = externalProtocolService.getProtocolHandlerInfo( | ||
"dummy" | ||
); | ||
const handlerService = Cc[ | ||
"@mozilla.org/uriloader/handler-service;1" | ||
].getService(Ci.nsIHandlerService); | ||
handlerService.store(dummyProtocolHandlerInfo); | ||
|
||
info("Prepare test search engine"); | ||
await setupSearchService(); | ||
await addTestEngines(); | ||
await Services.search.setDefault( | ||
Services.search.getEngineByName(kSearchEngineID) | ||
); | ||
|
||
registerCleanupFunction(() => { | ||
handlerService.remove(dummyProtocolHandlerInfo); | ||
MockRegistrar.unregister(mockId); | ||
}); | ||
}); | ||
|
||
add_task(function basic() { | ||
const testData = [ | ||
{ | ||
input: "mailto:[email protected]", | ||
expected: isSupportedInHandlerService("mailto") | ||
? "mailto:[email protected]" | ||
: "http://mailto:[email protected]/", | ||
}, | ||
{ | ||
input: "keyword:search", | ||
expected: "https://www.example.org/?search=keyword%3Asearch", | ||
}, | ||
{ | ||
input: "dummy:protocol", | ||
expected: "dummy:protocol", | ||
}, | ||
]; | ||
|
||
for (const { input, expected } of testData) { | ||
assertFixup(input, expected); | ||
} | ||
}); | ||
|
||
function assertFixup(input, expected) { | ||
const { preferredURI } = Services.uriFixup.getFixupURIInfo( | ||
input, | ||
Services.uriFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS | ||
); | ||
Assert.equal(preferredURI.spec, expected); | ||
} | ||
|
||
function isSupportedInHandlerService(scheme) { | ||
const externalProtocolService = Cc[ | ||
"@mozilla.org/uriloader/external-protocol-service;1" | ||
].getService(Ci.nsIExternalProtocolService); | ||
const handlerService = Cc[ | ||
"@mozilla.org/uriloader/handler-service;1" | ||
].getService(Ci.nsIHandlerService); | ||
return handlerService.exists( | ||
externalProtocolService.getProtocolHandlerInfo(scheme) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters