From a06b4bdd76547c7860da2a78c61308663bb7b8c6 Mon Sep 17 00:00:00 2001 From: Bartek Waresiak Date: Fri, 29 Nov 2024 13:42:20 +0100 Subject: [PATCH] Remove Special Pages tests --- DuckDuckGo.xcodeproj/project.pbxproj | 4 - DuckDuckGoTests/MockTabDelegate.swift | 8 + DuckDuckGoTests/SpecialErrorPageTests.swift | 304 -------------------- 3 files changed, 8 insertions(+), 308 deletions(-) delete mode 100644 DuckDuckGoTests/SpecialErrorPageTests.swift diff --git a/DuckDuckGo.xcodeproj/project.pbxproj b/DuckDuckGo.xcodeproj/project.pbxproj index 3273c57da7..6b1fa683d5 100644 --- a/DuckDuckGo.xcodeproj/project.pbxproj +++ b/DuckDuckGo.xcodeproj/project.pbxproj @@ -982,7 +982,6 @@ CBAA195A27BFE15600A4BD49 /* NSManagedObjectContextExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBAA195927BFE15600A4BD49 /* NSManagedObjectContextExtension.swift */; }; CBC83E3429B631780008E19C /* Configuration in Frameworks */ = {isa = PBXBuildFile; productRef = CBC83E3329B631780008E19C /* Configuration */; }; CBC88EE12C7F834300F0F8C5 /* SpecialErrorPageUserScriptTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBC88EE02C7F834300F0F8C5 /* SpecialErrorPageUserScriptTests.swift */; }; - CBC88EE32C7F8B1700F0F8C5 /* SpecialErrorPageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBC88EE22C7F8B1700F0F8C5 /* SpecialErrorPageTests.swift */; }; CBC88EE52C8097B500F0F8C5 /* URLCredentialCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBC88EE42C8097B500F0F8C5 /* URLCredentialCreator.swift */; }; CBCCF96828885DEE006F4A71 /* AppPrivacyConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02C4BC3127C3F9B600C40026 /* AppPrivacyConfigurationTests.swift */; }; CBD4F13C279EBF4A00B20FD7 /* HomeMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBD4F13B279EBF4A00B20FD7 /* HomeMessage.swift */; }; @@ -2817,7 +2816,6 @@ CBB6B2542AF6D543006B777C /* lt */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = lt; path = lt.lproj/InfoPlist.strings; sourceTree = ""; }; CBC7AB542AF6D583008CB798 /* ro */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ro; path = ro.lproj/InfoPlist.strings; sourceTree = ""; }; CBC88EE02C7F834300F0F8C5 /* SpecialErrorPageUserScriptTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpecialErrorPageUserScriptTests.swift; sourceTree = ""; }; - CBC88EE22C7F8B1700F0F8C5 /* SpecialErrorPageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpecialErrorPageTests.swift; sourceTree = ""; }; CBC88EE42C8097B500F0F8C5 /* URLCredentialCreator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLCredentialCreator.swift; sourceTree = ""; }; CBC8DC252AF6D4CD00BA681A /* da */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = da; path = da.lproj/InfoPlist.strings; sourceTree = ""; }; CBD4F13B279EBF4A00B20FD7 /* HomeMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeMessage.swift; sourceTree = ""; }; @@ -6001,7 +5999,6 @@ F189AED61F18F6DE001EBAE1 /* TabTests.swift */, D625AAEA2BBEEFC900BC189A /* TabURLInterceptorTests.swift */, CBC88EE02C7F834300F0F8C5 /* SpecialErrorPageUserScriptTests.swift */, - CBC88EE22C7F8B1700F0F8C5 /* SpecialErrorPageTests.swift */, ); name = Tabs; sourceTree = ""; @@ -8157,7 +8154,6 @@ CBDD5DE129A6741300832877 /* MockBundle.swift in Sources */, C158AC7B297AB5DC0008723A /* MockSecureVault.swift in Sources */, 569437342BE4E41500C0881B /* SyncErrorHandlerSyncErrorsAlertsTests.swift in Sources */, - CBC88EE32C7F8B1700F0F8C5 /* SpecialErrorPageTests.swift in Sources */, 85C11E4120904BBE00BFFEB4 /* VariantManagerTests.swift in Sources */, F1134ECE1F40EA9C00B73467 /* AtbParserTests.swift in Sources */, F189AEE41F18FDAF001EBAE1 /* LinkTests.swift in Sources */, diff --git a/DuckDuckGoTests/MockTabDelegate.swift b/DuckDuckGoTests/MockTabDelegate.swift index fefb80faa7..acb7db0e53 100644 --- a/DuckDuckGoTests/MockTabDelegate.swift +++ b/DuckDuckGoTests/MockTabDelegate.swift @@ -115,6 +115,14 @@ final class MockTabDelegate: TabDelegate { } +final class MockCredentialCreator: URLCredentialCreating { + + func urlCredentialFrom(trust: SecTrust?) -> URLCredential? { + return URLCredential(user: "", password: "", persistence: .forSession) + } + +} + extension TabViewController { static func fake( diff --git a/DuckDuckGoTests/SpecialErrorPageTests.swift b/DuckDuckGoTests/SpecialErrorPageTests.swift deleted file mode 100644 index f3ff737d3b..0000000000 --- a/DuckDuckGoTests/SpecialErrorPageTests.swift +++ /dev/null @@ -1,304 +0,0 @@ -// -// SpecialErrorPageTests.swift -// DuckDuckGo -// -// Copyright © 2024 DuckDuckGo. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import Foundation -import XCTest -import WebKit - -@testable import SpecialErrorPages -@testable import DuckDuckGo - -class MockSpecialErrorWebView: WKWebView { - - var loadRequestHandler: ((URLRequest, String) -> Void)? - var currentURL: URL? - - override func loadSimulatedRequest(_ request: URLRequest, responseHTML string: String) -> WKNavigation { - loadRequestHandler?(request, string) - return super.loadSimulatedRequest(request, responseHTML: string) - } - - override var url: URL? { - return currentURL - } - - func setCurrentURL(_ url: URL) { - self.currentURL = url - } - -} - -final class SpecialErrorPageTests: XCTestCase { - - var webView: MockSpecialErrorWebView! - var sut: TabViewController! - - override func setUpWithError() throws { - throw XCTSkip("Potentially Flaky") - - try super.setUpWithError() - let featureFlagger = MockFeatureFlagger() - featureFlagger.enabledFeatureFlags = [.sslCertificatesBypass] - sut = .fake(customWebView: { [weak self] configuration in - guard let self else { fatalError("It has to exist") } - self.webView = MockSpecialErrorWebView(frame: CGRect(), configuration: configuration) - return self.webView - }, featureFlagger: featureFlagger) - WKNavigation.swizzleDealloc() - } - - override func tearDown() async throws { - try await super.tearDown() - WKNavigation.restoreDealloc() - } - - func testWhenCertificateExpiredThenExpectedErrorPageIsShown() { - // GIVEN - let error = NSError(domain: "test", - code: NSURLErrorServerCertificateUntrusted, - userInfo: ["_kCFStreamErrorCodeKey": errSSLCertExpired, - NSURLErrorFailingURLErrorKey: URL(string: "https://expired.badssl.com")!]) - let expectation = self.expectation(description: "Special error page should be loaded") - var didFulfill = false - webView.loadRequestHandler = { request, html in - if !didFulfill { - XCTAssertTrue(html.contains("Warning: This site may be insecure")) - XCTAssertTrue(html.contains("is expired")) - XCTAssertEqual(request.url!.host, URL(string: "https://expired.badssl.com")!.host) - expectation.fulfill() - didFulfill = true - } - } - - // WHEN - sut.webView(webView, didFailProvisionalNavigation: WKNavigation(), withError: error) - - // THEN - XCTAssertEqual(sut.failedURL, URL(string: "https://expired.badssl.com")!) - XCTAssertEqual(sut.errorData, SpecialErrorData(kind: .ssl, - errorType: "expired", - domain: "expired.badssl.com", - eTldPlus1: "badssl.com")) - waitForExpectations(timeout: 1) { error in - XCTAssertNil(error, "Expectation was not fulfilled in time") - } - } - - func testWhenCertificateWrongHostThenExpectedErrorPageIsShown() { - // GIVEN - let error = NSError(domain: "test", - code: NSURLErrorServerCertificateUntrusted, - userInfo: ["_kCFStreamErrorCodeKey": errSSLHostNameMismatch, - NSURLErrorFailingURLErrorKey: URL(string: "https://wrong.host.badssl.com")!]) - let expectation = self.expectation(description: "Special error page should be loaded") - var didFulfill = false - webView.loadRequestHandler = { request, html in - if !didFulfill { - XCTAssertTrue(html.contains("Warning: This site may be insecure")) - XCTAssertTrue(html.contains("does not match")) - XCTAssertEqual(request.url!.host, URL(string: "https://wrong.host.badssl.com")!.host) - expectation.fulfill() - didFulfill = true - } - } - - // WHEN - sut.webView(webView, didFailProvisionalNavigation: WKNavigation(), withError: error) - - // THEN - XCTAssertEqual(sut.failedURL, URL(string: "https://wrong.host.badssl.com")!) - XCTAssertEqual(sut.errorData, SpecialErrorData(kind: .ssl, - errorType: "wrongHost", - domain: "wrong.host.badssl.com", - eTldPlus1: "badssl.com")) - waitForExpectations(timeout: 1) { error in - XCTAssertNil(error, "Expectation was not fulfilled in time") - } - } - - func testWhenCertificateSelfSignedThenExpectedErrorPageIsShown() { - // GIVEN - let error = NSError(domain: "test", - code: NSURLErrorServerCertificateUntrusted, - userInfo: ["_kCFStreamErrorCodeKey": errSSLXCertChainInvalid, - NSURLErrorFailingURLErrorKey: URL(string: "https://self-signed.badssl.com")!]) - let expectation = self.expectation(description: "Special error page should be loaded") - var didFulfill = false - webView.loadRequestHandler = { request, html in - if !didFulfill { - XCTAssertTrue(html.contains("Warning: This site may be insecure")) - XCTAssertTrue(html.contains("is not trusted")) - XCTAssertEqual(request.url!.host, URL(string: "https://self-signed.badssl.com")!.host) - expectation.fulfill() - didFulfill = true - } - } - - // WHEN - sut.webView(webView, didFailProvisionalNavigation: WKNavigation(), withError: error) - - // THEN - XCTAssertEqual(sut.failedURL, URL(string: "https://self-signed.badssl.com")!) - XCTAssertEqual(sut.errorData, SpecialErrorData(kind: .ssl, - errorType: "selfSigned", - domain: "self-signed.badssl.com", - eTldPlus1: "badssl.com")) - waitForExpectations(timeout: 1) { error in - XCTAssertNil(error, "Expectation was not fulfilled in time") - } - } - - func testWhenOtherCertificateIssueThenExpectedErrorPageIsShown() { - // GIVEN - let error = NSError(domain: "test", - code: NSURLErrorServerCertificateUntrusted, - userInfo: ["_kCFStreamErrorCodeKey": errSSLUnknownRootCert, - NSURLErrorFailingURLErrorKey: URL(string: "https://untrusted-root.badssl.com")!]) - let expectation = self.expectation(description: "Special error page should be loaded") - var didFulfill = false - webView.loadRequestHandler = { request, html in - if !didFulfill { - XCTAssertTrue(html.contains("Warning: This site may be insecure")) - XCTAssertTrue(html.contains("is not trusted")) - XCTAssertEqual(request.url!.host, URL(string: "https://untrusted-root.badssl.com")!.host) - expectation.fulfill() - didFulfill = true - } - } - - // WHEN - sut.webView(webView, didFailProvisionalNavigation: WKNavigation(), withError: error) - - // THEN - XCTAssertEqual(sut.failedURL, URL(string: "https://untrusted-root.badssl.com")!) - XCTAssertEqual(sut.errorData, SpecialErrorData(kind: .ssl, - errorType: "invalid", - domain: "untrusted-root.badssl.com", - eTldPlus1: "badssl.com")) - waitForExpectations(timeout: 1) { error in - XCTAssertNil(error, "Expectation was not fulfilled in time") - } - } - - @MainActor - func testWhenNavigationEndedIfNoSSLFailureSSLUserScriptIsNotEnabled() { - // GIVEN - webView.setCurrentURL(URL(string: "https://self-signed.badssl.com")!) - sut.storedSpecialErrorPageUserScript = SpecialErrorPageUserScript(localeStrings: "", languageCode: "") - - // WHEN - sut.webView(webView, didFinish: WKNavigation()) - - // THEN - XCTAssertFalse(sut.specialErrorPageUserScript?.isEnabled ?? true) - } - - @MainActor - func testWhenNavigationEndedIfSSLFailureButURLIsDifferentFromNavigationURLThenSSLUserScriptIsNotEnabled() { - // GIVEN - webView.setCurrentURL(URL(string: "https://self-signed.badssl.com")!) - sut.failedURL = URL(string: "https://different.url.com")! - sut.storedSpecialErrorPageUserScript = SpecialErrorPageUserScript(localeStrings: "", languageCode: "") - - // WHEN - sut.webView(webView, didFinish: WKNavigation()) - - // THEN - XCTAssertFalse(sut.specialErrorPageUserScript?.isEnabled ?? true) - } - - @MainActor - func testWhenNavigationEndedIfSSLFailureAndNavigationURLIsTheSameAsFailingURLThenSSLUserScriptIsEnabled() { - // GIVEN - webView.setCurrentURL(URL(string: "https://self-signed.badssl.com")!) - sut.failedURL = URL(string: "https://self-signed.badssl.com")! - sut.storedSpecialErrorPageUserScript = SpecialErrorPageUserScript(localeStrings: "", languageCode: "") - - // WHEN - sut.webView(webView, didFinish: WKNavigation()) - - // THEN - XCTAssertTrue(sut.specialErrorPageUserScript?.isEnabled ?? false) - } - - func testWhenDidReceiveChallengeIfChallengeForCertificateValidationAndNoBypassThenShouldNotReturnCredentials() async { - let protectionSpace = URLProtectionSpace(host: "", port: 4, protocol: nil, realm: nil, authenticationMethod: NSURLAuthenticationMethodServerTrust) - let challenge = URLAuthenticationChallenge(protectionSpace: protectionSpace, proposedCredential: nil, previousFailureCount: 0, failureResponse: nil, error: nil, sender: ChallengeSender()) - await sut.webView(webView, didReceive: challenge) { _, credential in - XCTAssertNil(credential) - } - } - - func testWhenDidReceiveChallengeIfChallengeForCertificateValidationAndUserRequestBypassThenReturnsCredentials() async { - let protectionSpace = URLProtectionSpace(host: "", port: 4, protocol: nil, realm: nil, authenticationMethod: NSURLAuthenticationMethodServerTrust) - let challenge = URLAuthenticationChallenge(protectionSpace: protectionSpace, proposedCredential: nil, previousFailureCount: 0, failureResponse: nil, error: nil, sender: ChallengeSender()) - await sut.visitSite() - await sut.webView(webView, didReceive: challenge) { _, credential in - XCTAssertNotNil(credential) - } - } - -} - -final class ChallengeSender: URLAuthenticationChallengeSender { - func use(_ credential: URLCredential, for challenge: URLAuthenticationChallenge) {} - func continueWithoutCredential(for challenge: URLAuthenticationChallenge) {} - func cancel(_ challenge: URLAuthenticationChallenge) {} - func isEqual(_ object: Any?) -> Bool { - return false - } - var hash: Int = 0 - var superclass: AnyClass? - func `self`() -> Self { - self - } - func perform(_ aSelector: Selector!) -> Unmanaged! { - return nil - } - func perform(_ aSelector: Selector!, with object: Any!) -> Unmanaged! { - return nil - } - func perform(_ aSelector: Selector!, with object1: Any!, with object2: Any!) -> Unmanaged! { - return nil - } - func isProxy() -> Bool { - return false - } - func isKind(of aClass: AnyClass) -> Bool { - return false - } - func isMember(of aClass: AnyClass) -> Bool { - return false - } - func conforms(to aProtocol: Protocol) -> Bool { - return false - } - func responds(to aSelector: Selector!) -> Bool { - return false - } - var description: String = "" -} - -final class MockCredentialCreator: URLCredentialCreating { - - func urlCredentialFrom(trust: SecTrust?) -> URLCredential? { - return URLCredential(user: "", password: "", persistence: .forSession) - } - -}