Skip to content

Commit

Permalink
Ensure lazy-loading is not returning stale data
Browse files Browse the repository at this point in the history
  • Loading branch information
not-a-rootkit committed Oct 28, 2024
1 parent cda34ac commit 9b9d038
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Sources/PhishingDetection/PhishingDetectionDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,18 @@ public class PhishingDetectionDataStore: PhishingDetectionDataSaving {

extension PhishingDetectionDataStore {
public func saveFilterSet(set: Set<Filter>) {
writeFilterSet()
self.filterSet = set
writeFilterSet()
}

public func saveHashPrefixes(set: Set<String>) {
writeHashPrefixes()
self.hashPrefixes = set
writeHashPrefixes()
}

public func saveRevision(_ revision: Int) {
writeRevision()
self.currentRevision = revision
writeRevision()
}
}

Expand Down
47 changes: 47 additions & 0 deletions Tests/PhishingDetectionTests/PhishingDetectionDataStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,51 @@ class PhishingDetectionDataStoreTests: XCTestCase {
XCTFail("Failed to decode stored PhishingDetection data")
}
}

func testLazyLoadingDoesNotReturnStaleData() async {
clearDatasets()

// Set up initial data
let initialFilterSet = Set([Filter(hashValue: "initial", regex: "initial")])
let initialHashPrefixes = Set(["initialPrefix"])
mockDataProvider.shouldReturnFilterSet(set: initialFilterSet)
mockDataProvider.shouldReturnHashPrefixes(set: initialHashPrefixes)

// Access the lazy-loaded properties to trigger loading
let loadedFilterSet = dataStore.filterSet
let loadedHashPrefixes = dataStore.hashPrefixes

// Validate loaded data matches initial data
XCTAssertEqual(loadedFilterSet, initialFilterSet)
XCTAssertEqual(loadedHashPrefixes, initialHashPrefixes)

// Update in-memory data
let updatedFilterSet = Set([Filter(hashValue: "updated", regex: "updated")])
let updatedHashPrefixes = Set(["updatedPrefix"])
dataStore.saveFilterSet(set: updatedFilterSet)
dataStore.saveHashPrefixes(set: updatedHashPrefixes)

// Access lazy-loaded properties again
let reloadedFilterSet = dataStore.filterSet
let reloadedHashPrefixes = dataStore.hashPrefixes

// Validate reloaded data matches updated data
XCTAssertEqual(reloadedFilterSet, updatedFilterSet)
XCTAssertEqual(reloadedHashPrefixes, updatedHashPrefixes)

// Validate on-disk data is also updated
let storedFilterSetData = fileStorageManager.read(from: "filterSet.json")
let storedHashPrefixesData = fileStorageManager.read(from: "hashPrefixes.json")

let decoder = JSONDecoder()
if let storedFilterSet = try? decoder.decode(Set<Filter>.self, from: storedFilterSetData!),
let storedHashPrefixes = try? decoder.decode(Set<String>.self, from: storedHashPrefixesData!) {

Check failure on line 189 in Tests/PhishingDetectionTests/PhishingDetectionDataStoreTests.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
XCTAssertEqual(storedFilterSet, updatedFilterSet)
XCTAssertEqual(storedHashPrefixes, updatedHashPrefixes)
} else {
XCTFail("Failed to decode stored PhishingDetection data after update")
}
}

}

0 comments on commit 9b9d038

Please sign in to comment.