Skip to content

Commit

Permalink
update reference test to use new cookie storage approach
Browse files Browse the repository at this point in the history
  • Loading branch information
brindy committed Jan 24, 2024
1 parent 4a9120d commit 2df585d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 67 deletions.
2 changes: 2 additions & 0 deletions Core/CookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public class CookieStorage {
}

persistedCookiesByDomain.keys.forEach {
guard $0 != "duckduckgo.com" else { return } // DDG cookies are for SERP settings only

if !preservedLogins.isAllowed(cookieDomain: $0) {
persistedCookiesByDomain.removeValue(forKey: $0)
}
Expand Down
15 changes: 15 additions & 0 deletions DuckDuckGoTests/CookieStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public class CookieStorageTests: XCTestCase {
storage = CookieStorage(userDefaults: defaults)
logins.clearAll()
}

func testWhenUpdatedThenDuckDuckGoCookiesAreNotRemoved() {
storage.updateCookies([
make("duckduckgo.com", name: "x", value: "1"),
], keepingPreservedLogins: logins)

XCTAssertEqual(1, storage.cookies.count)

storage.updateCookies([
make("test.com", name: "x", value: "1"),
], keepingPreservedLogins: logins)

XCTAssertEqual(2, storage.cookies.count)

}

func testWhenUpdatedThenCookiesWithFutureExpirationAreNotRemoved() {
storage.updateCookies([
Expand Down
93 changes: 26 additions & 67 deletions DuckDuckGoTests/FireButtonReferenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ import WebKit
@testable import Core

final class FireButtonReferenceTests: XCTestCase {
private var referenceTests = [Test]()
private let preservedLogins = PreserveLogins.shared
private let dataStore = WKWebsiteDataStore.default()

private enum Resource {
static let tests = "privacy-reference-tests/storage-clearing/tests.json"
Expand All @@ -36,19 +33,6 @@ final class FireButtonReferenceTests: XCTestCase {
// swiftlint:disable:next force_try
return try! JSONDecoder().decode(TestData.self, from: testJSON)
}()

override func tearDownWithError() throws {
try super.tearDownWithError()

// Remove fireproofed sites
for site in testData.fireButtonFireproofing.fireproofedSites {
let sanitizedSite = sanitizedSite(site)
os_log("Removing %s from fireproofed sites", sanitizedSite)
PreserveLogins.shared.remove(domain: sanitizedSite)
}

referenceTests.removeAll()
}

private func sanitizedSite(_ site: String) -> String {
let url: URL
Expand All @@ -60,67 +44,42 @@ final class FireButtonReferenceTests: XCTestCase {
return url.host!
}

func testFireproofing() throws {
// Setup fireproofed sites
func testCookieStorage() {
let preservedLogins = PreserveLogins.shared
preservedLogins.clearAll()

for site in testData.fireButtonFireproofing.fireproofedSites {
let sanitizedSite = sanitizedSite(site)
os_log("Adding %s to fireproofed sites", sanitizedSite)
preservedLogins.addToAllowed(domain: sanitizedSite)

}

referenceTests = testData.fireButtonFireproofing.tests.filter {
$0.exceptPlatforms.contains("ios-browser") == false
}

let testsExecuted = expectation(description: "tests executed")
testsExecuted.expectedFulfillmentCount = referenceTests.count

runReferenceTests(onTestExecuted: testsExecuted)
waitForExpectations(timeout: 30, handler: nil)
}

private func runReferenceTests(onTestExecuted: XCTestExpectation) {
guard let test = referenceTests.popLast() else {
return
}

guard let cookie = cookie(for: test) else {
XCTFail("Cookie should exist for test \(test.name)")
return
let referenceTests = testData.fireButtonFireproofing.tests.filter {
$0.exceptPlatforms.contains("ios-browser") == false
}

dataStore.cookieStore?.setCookie(cookie, completionHandler: {

let dataStoreIdManager = DataStoreIdManager()
WebCacheManager.shared.clear(logins: self.preservedLogins, dataStoreIdManager: dataStoreIdManager) {
let cookieStorage = CookieStorage()
for test in referenceTests {
guard let cookie = cookie(for: test) else {
XCTFail("Cookie should exist for test \(test.name)")
return
}

cookieStorage.updateCookies([
cookie
], keepingPreservedLogins: preservedLogins)

let testCookie = cookieStorage.cookies.filter { $0.name == test.cookieName }.first

self.dataStore.cookieStore?.getAllCookies { hotCookies in
let testCookie = hotCookies.filter { $0.name == test.cookieName }.first

if test.expectCookieRemoved {
XCTAssertNil(testCookie, "Cookie should not exist for test: \(test.name)")
} else {
XCTAssertNotNil(testCookie, "Cookie should exist for test: \(test.name)")
}


// Remove all cookies from this test
let group = DispatchGroup()
for cookie in hotCookies {
group.enter()
self.dataStore.cookieStore?.delete(cookie, completionHandler: {
group.leave()
})
}

group.notify(queue: .main) {
onTestExecuted.fulfill()
self.runReferenceTests(onTestExecuted: onTestExecuted)
}
}
if test.expectCookieRemoved {
XCTAssertNil(testCookie, "Cookie should not exist for test: \(test.name)")
} else {
XCTAssertNotNil(testCookie, "Cookie should exist for test: \(test.name)")
}
})

// Reset cache
cookieStorage.cookies = []
}
}

private func cookie(for test: Test) -> HTTPCookie? {
Expand Down

0 comments on commit 2df585d

Please sign in to comment.