Skip to content

Commit

Permalink
Fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandroboron committed Nov 27, 2024
1 parent 4d4f428 commit c3815f5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ import BrowserServicesKit
import SpecialErrorPages
import Core

struct SSLSpecialError {
let url: URL
let type: SSLErrorType
let errorData: SpecialErrorData
}

protocol SSLSpecialErrorPageNavigationHandling {
func handleServerTrustChallenge(_ challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

func makeNewRequestURLAndSpecialErrorDataIfEnabled(error: NSError) -> (url: URL, sslErrorType: SSLErrorType, specialErrorData: SpecialErrorData)?
func makeNewRequestURLAndSpecialErrorDataIfEnabled(error: NSError) -> SSLSpecialError?

func errorPageVisited(errorType: SSLErrorType)
}
Expand Down Expand Up @@ -63,7 +69,7 @@ extension SSLErrorPageNavigationHandler: SSLSpecialErrorPageNavigationHandling {
completionHandler(.useCredential, credential)
}

func makeNewRequestURLAndSpecialErrorDataIfEnabled(error: NSError) -> (url: URL, sslErrorType: SSLErrorType, specialErrorData: SpecialErrorData)? {
func makeNewRequestURLAndSpecialErrorDataIfEnabled(error: NSError) -> SSLSpecialError? {
guard featureFlagger.isFeatureOn(.sslCertificatesBypass),
error.code == NSURLErrorServerCertificateUntrusted,
let errorCode = error.userInfo["_kCFStreamErrorCodeKey"] as? Int32,
Expand All @@ -77,7 +83,7 @@ extension SSLErrorPageNavigationHandler: SSLSpecialErrorPageNavigationHandling {
domain: failedURL.host,
eTldPlus1: storageCache.tld.eTLDplus1(failedURL.host))

return (failedURL, errorType, errorData)
return SSLSpecialError(url: failedURL, type: errorType, errorData: errorData)
}

func errorPageVisited(errorType: SSLErrorType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ extension SpecialErrorPageNavigationHandler: WebViewNavigationHandling {
}

func handleWebView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WebViewNavigation, withError error: NSError) {
guard let (url, sslErrorType, specialErrorData) = sslErrorPageNavigationHandler.makeNewRequestURLAndSpecialErrorDataIfEnabled(error: error) else { return }
failedURL = url
sslErrorPageNavigationHandler.errorPageVisited(errorType: sslErrorType)
errorData = specialErrorData
guard let sslSpecialError = sslErrorPageNavigationHandler.makeNewRequestURLAndSpecialErrorDataIfEnabled(error: error) else { return }
failedURL = sslSpecialError.url
sslErrorPageNavigationHandler.errorPageVisited(errorType: sslSpecialError.type)
errorData = sslSpecialError.errorData
errorPageType = .ssl
loadSpecialErrorPage(url: url)
loadSpecialErrorPage(url: sslSpecialError.url)
}

func handleWebView(_ webView: WKWebView, didFinish navigation: WebViewNavigation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ final class SSLSpecialErrorPageTests: XCTestCase {
NSURLErrorFailingURLErrorKey: URL(string: "https://expired.badssl.com")!])

// WHEN
let (failedURL, sslError, errorData) = try XCTUnwrap(sut.makeNewRequestURLAndSpecialErrorDataIfEnabled(error: error))
let sslError = try XCTUnwrap(sut.makeNewRequestURLAndSpecialErrorDataIfEnabled(error: error))

// THEN
XCTAssertEqual(failedURL, URL(string: "https://expired.badssl.com")!)
XCTAssertEqual(sslError, .expired)
XCTAssertEqual(errorData, SpecialErrorData(kind: .ssl,
XCTAssertEqual(sslError.url, URL(string: "https://expired.badssl.com")!)
XCTAssertEqual(sslError.type, .expired)
XCTAssertEqual(sslError.errorData, SpecialErrorData(kind: .ssl,
errorType: "expired",
domain: "expired.badssl.com",
eTldPlus1: "badssl.com"))
Expand All @@ -67,12 +67,12 @@ final class SSLSpecialErrorPageTests: XCTestCase {
NSURLErrorFailingURLErrorKey: URL(string: "https://wrong.host.badssl.com")!])

// WHEN
let (failedURL, sslError, errorData) = try XCTUnwrap(sut.makeNewRequestURLAndSpecialErrorDataIfEnabled(error: error))
let sslError = try XCTUnwrap(sut.makeNewRequestURLAndSpecialErrorDataIfEnabled(error: error))

// THEN
XCTAssertEqual(failedURL, URL(string: "https://wrong.host.badssl.com")!)
XCTAssertEqual(sslError, .wrongHost)
XCTAssertEqual(errorData, SpecialErrorData(kind: .ssl,
XCTAssertEqual(sslError.url, URL(string: "https://wrong.host.badssl.com")!)
XCTAssertEqual(sslError.type, .wrongHost)
XCTAssertEqual(sslError.errorData, SpecialErrorData(kind: .ssl,
errorType: "wrongHost",
domain: "wrong.host.badssl.com",
eTldPlus1: "badssl.com"))
Expand All @@ -86,12 +86,12 @@ final class SSLSpecialErrorPageTests: XCTestCase {
NSURLErrorFailingURLErrorKey: URL(string: "https://self-signed.badssl.com")!])

// WHEN
let (failedURL, sslError, errorData) = try XCTUnwrap(sut.makeNewRequestURLAndSpecialErrorDataIfEnabled(error: error))
let sslError = try XCTUnwrap(sut.makeNewRequestURLAndSpecialErrorDataIfEnabled(error: error))

// THEN
XCTAssertEqual(failedURL, URL(string: "https://self-signed.badssl.com")!)
XCTAssertEqual(sslError, .selfSigned)
XCTAssertEqual(errorData, SpecialErrorData(kind: .ssl,
XCTAssertEqual(sslError.url, URL(string: "https://self-signed.badssl.com")!)
XCTAssertEqual(sslError.type, .selfSigned)
XCTAssertEqual(sslError.errorData, SpecialErrorData(kind: .ssl,
errorType: "selfSigned",
domain: "self-signed.badssl.com",
eTldPlus1: "badssl.com"))
Expand All @@ -105,12 +105,12 @@ final class SSLSpecialErrorPageTests: XCTestCase {
NSURLErrorFailingURLErrorKey: URL(string: "https://untrusted-root.badssl.com")!])

// WHEN
let (failedURL, sslError, errorData) = try XCTUnwrap(sut.makeNewRequestURLAndSpecialErrorDataIfEnabled(error: error))
let sslError = try XCTUnwrap(sut.makeNewRequestURLAndSpecialErrorDataIfEnabled(error: error))

// THEN
XCTAssertEqual(failedURL, URL(string: "https://untrusted-root.badssl.com")!)
XCTAssertEqual(sslError, .invalid)
XCTAssertEqual(errorData, SpecialErrorData(kind: .ssl,
XCTAssertEqual(sslError.url, URL(string: "https://untrusted-root.badssl.com")!)
XCTAssertEqual(sslError.type, .invalid)
XCTAssertEqual(sslError.errorData, SpecialErrorData(kind: .ssl,
errorType: "invalid",
domain: "untrusted-root.badssl.com",
eTldPlus1: "badssl.com"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MockWKNavigation.swift
// DummyWKNavigation.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ final class MockSSLErrorPageNavigationHandler: SSLSpecialErrorPageNavigationHand
handleServerTrustChallengeHandler(.performDefaultHandling, nil)
}

func makeNewRequestURLAndSpecialErrorDataIfEnabled(error: NSError) -> (url: URL, sslErrorType: SSLErrorType, specialErrorData: SpecialErrorData)? {
func makeNewRequestURLAndSpecialErrorDataIfEnabled(error: NSError) -> SSLSpecialError? {
didCallMakeNewRequestURLAndSpecialErrorDataIfEnabled = true
return (URL(string: "www.example.com")!, .expired, SpecialErrorData(kind: .ssl))
return SSLSpecialError(url: URL(string: "www.example.com")!, type: .expired, errorData: SpecialErrorData(kind: .ssl))
}

func errorPageVisited(errorType: SSLErrorType) {
Expand Down

0 comments on commit c3815f5

Please sign in to comment.