Revision control
Copy as Markdown
// 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
import XCTest
@testable import WebEngine
final class URLExtensionTests: XCTestCase {
private let webserverPort = 6571
// MARK: isReaderModeURL tests
func testIsReaderModeURLGivenGoogleURLThenFalse() {
XCTAssertFalse(url.isReaderModeURL)
}
func testIsReaderModeURLGivenHTTPSchemeURLThenFalse() {
XCTAssertFalse(url.isReaderModeURL)
}
func testIsReaderModeURLGivenLocalHostURLThenFalse() {
XCTAssertFalse(url.isReaderModeURL)
}
func testIsReaderModeURLGivenReaderModeURLThenTrue() {
XCTAssertTrue(url.isReaderModeURL)
}
// MARK: isSyncedReaderModeURL tests
func testIsSyncedReaderModeURLWhenEmptyURLThenIsTrue() {
let url = "about:reader?url="
XCTAssertTrue(URL(string: url)!.isSyncedReaderModeURL)
}
func testIsSyncedReaderModeURLWhenSimpleURLThenIsTrue() {
XCTAssertTrue(URL(string: url)!.isSyncedReaderModeURL)
}
func testIsSyncedReaderModeURLWhenComplicatedURLThenIsTrue() {
let url = "about:reader?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage"
XCTAssertTrue(URL(string: url)!.isSyncedReaderModeURL)
}
func testIsSyncedReaderModeURLWhenGoogleURLThenIsFalse() {
XCTAssertFalse(URL(string: url)!.isSyncedReaderModeURL)
}
func testIsSyncedReaderModeURLWhenLocalHostURLThenIsFalse() {
XCTAssertFalse(URL(string: url)!.isSyncedReaderModeURL)
}
func testIsSyncedReaderModeURLWhenAboutURLThenIsFalse() {
let url = "about:reader"
XCTAssertFalse(URL(string: url)!.isSyncedReaderModeURL)
}
// MARK: decodeReaderModeURL tests
func testDecodeReaderModeURLWhenLocalReaderModeThenGivesWikiPage() {
let readerModeURL = "http://localhost:\(webserverPort)/reader-mode/page?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage&uuidkey=AAAAA"
XCTAssertEqual(URL(string: readerModeURL)!.decodeReaderModeURL, url)
}
func testDecodeReaderModeURLWhenReaderModeThenGivesWikiPage() {
let readerModeURL = "about:reader?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage&uuidkey=AAAAA"
XCTAssertEqual(URL(string: readerModeURL)!.decodeReaderModeURL, url)
}
func testDecodeReaderModeURLWhenParameterURLThenGivesCorrectParameterURL() {
let readerModeURL = "about:reader?url=http%3A%2F%2Fexample%2Ecom%3Furl%3Dparam%26key%3Dvalue&uuidkey=AAAAA"
XCTAssertEqual(URL(string: readerModeURL)!.decodeReaderModeURL, url)
}
func testDecodeReaderModeURLGivenNotAReaderModeURLThenNil() {
XCTAssertNil(URL(string: url)!.decodeReaderModeURL)
}
func testDecodeReaderModeURLWhenLocalHostSessionRestoreURLThenNil() {
XCTAssertNil(URL(string: url)!.decodeReaderModeURL)
}
func testDecodeReaderModeURLWhenLocalAboutHomeURLThenNil() {
XCTAssertNil(URL(string: url)!.decodeReaderModeURL)
}
func testDecodeReaderModeURLWhenLocalHostReaderModePageThenNil() {
XCTAssertNil(URL(string: url)!.decodeReaderModeURL)
}
func testDecodeReaderModeURLWhenAboutReaderURLThenNil() {
let url = "about:reader?url="
XCTAssertNil(URL(string: url)!.decodeReaderModeURL)
}
// MARK: encodeReaderModeURL tests
func testEncodeReaderModeURLGivenReaderAndURLThenEncodeReaderURL() {
let expectedReaderModeURL = URL(string: "http://localhost:\(webserverPort)/reader-mode/page?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage")
XCTAssertEqual(URL(string: stringURL)!.encodeReaderModeURL(readerURL), expectedReaderModeURL)
}
// MARK: safeEncodedUrl tests
func testSafeEncodedUrlGivenJavaScriptSanitization() {
// reader mode generic url sanitized JS to prevent XSS alert
let genericUrl = url.safeEncodedUrl
XCTAssertNotNil(genericUrl)
XCTAssertTrue(
genericUrl!.absoluteString.contains("javascript:alert(%26%2339;ALERT%26%2339;)")
)
}
func testSafeEncodedUrlGivenScriptInnerHtmlTextSanitization() {
// reader mode generic url script tags are sanitized to prevent body change
let url = URL(string: "http://localhost:1234/reader-mode/page?url=javascript:document.body.innerText='Hello';")!
let genericUrl = url.safeEncodedUrl
XCTAssertNotNil(genericUrl)
XCTAssertTrue(
genericUrl!.absoluteString.contains("javascript:document.body.innerText%3D%26%2339;Hello%26%2339;;")
)
}
func testSafeEncodedUrlGivenHTMLFontSanitization() {
// reader mode generic url with HTML is sanitized
let url = URL(string: "http://localhost:1234/reader-mode/page?url=javascript:document.body.style.fontSize='50px';")!
let genericUrl = url.safeEncodedUrl
XCTAssertNotNil(genericUrl)
XCTAssertTrue(
genericUrl!.absoluteString.contains("javascript:document.body.style.fontSize%3D%26%2339;50px%26%2339;;")
)
}
func testSafeEncodedUrlGivenJavaScriptSanitizationNonLocalhost() {
// Check if JavaScript code in a non-localhost URL is sanitized
let genericUrl = url.safeEncodedUrl
XCTAssertNotNil(genericUrl)
XCTAssertTrue(genericUrl!.absoluteString.contains("javascript:alert(%26%2339;XSS%26%2339;)"))
}
func testSafeEncodedUrlGivenEmptyPath() {
let safeUrl = url.safeEncodedUrl
XCTAssertNotNil(safeUrl)
}
func testSafeEncodedUrlGivenEmptyScheme() {
let url = URL(string: "//localhost:1234/page")!
let safeUrl = url.safeEncodedUrl
XCTAssertNil(safeUrl)
}
func testSafeEncodedUrlGivenMissingComponentsJS() {
let url = URL(string: "javascript:blob")!
let safeUrl = url.safeEncodedUrl
XCTAssertNil(safeUrl)
}
func testSafeEncodedUrlGivenMissingComponents() {
let url = URL(string: "/page?url=javascript")!
let safeUrl = url.safeEncodedUrl
XCTAssertNil(safeUrl)
}
func testFaviconRootDirectoryURL() {
let favicon1 = url1?.faviconUrl()
let favicon2 = url2?.faviconUrl()
let favicon3 = url3?.faviconUrl()
}
}