Skip to content

Commit

Permalink
Merge pull request #194 from square/dfed--improve-tests
Browse files Browse the repository at this point in the history
Expand test matrix
  • Loading branch information
dfed authored Jan 6, 2020
2 parents 7374cd3 + 08f0564 commit 50e134e
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 209 deletions.
19 changes: 18 additions & 1 deletion Sources/Valet/MigrationResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import Foundation

@objc(VALMigrationResult)
public enum MigrationResult: Int, Equatable {
public enum MigrationResult: Int, Equatable, CustomStringConvertible {
/// Migration succeeded.
case success = 1
/// Migration failed because the keychain query was not valid.
Expand All @@ -42,4 +42,21 @@ public enum MigrationResult: Int, Equatable {
case couldNotWriteToKeychain
/// Migration failed because removing the migrated data from the keychain failed.
case removalFailed

// MARK: CustomStringConvertible

public var description: String {
switch self {
case .success: return "success"
case .invalidQuery: return "invalidQuery"
case .noItemsToMigrateFound: return "noItemsToMigrateFound"
case .couldNotReadKeychain: return "couldNotReadKeychain"
case .keyInQueryResultInvalid: return "keyInQueryResultInvalid"
case .dataInQueryResultInvalid: return "dataInQueryResultInvalid"
case .duplicateKeyInQueryResult: return "duplicateKeyInQueryResult"
case .keyInQueryResultAlreadyExistsInValet: return "keyInQueryResultAlreadyExistsInValet"
case .couldNotWriteToKeychain: return "couldNotWriteToKeychain"
case .removalFailed: return "removalFailed"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ extension CloudIntegrationTests {
return
}

Valet.iCloudCurrentAndLegacyPermutations(with: valet.identifier).forEach { permutation, legacyValet in
let identifier = Identifier(nonEmpty: "BackwardsCompatibilityTest")!
Valet.iCloudCurrentAndLegacyPermutations(with: identifier).forEach { permutation, legacyValet in
legacyValet.setString(passcode, forKey: key)

XCTAssertNotNil(legacyValet.string(forKey: key))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ValetBackwardsCompatibilityIntegrationTests: ValetIntegrationTests {
// MARK: Tests

func test_backwardsCompatibility_withLegacyValet() {
Valet.currentAndLegacyPermutations(with: valet.identifier).forEach { permutation, legacyValet in
Valet.currentAndLegacyPermutations(with: vanillaValet.identifier).forEach { permutation, legacyValet in
legacyValet.setString(passcode, forKey: key)

XCTAssertNotNil(legacyValet.string(forKey: key))
Expand Down
74 changes: 29 additions & 45 deletions Tests/ValetIntegrationTests/CloudIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ import XCTest

class CloudIntegrationTests: XCTestCase
{
static let identifier = Identifier(nonEmpty: "valet_testing")!
static let identifier = Valet.sharedAccessGroupIdentifier
static let accessibility = CloudAccessibility.whenUnlocked
let valet = Valet.iCloudValet(with: identifier, accessibility: accessibility)
var allPermutations: [Valet] {
return (testEnvironmentIsSigned()
? Valet.iCloudPermutations(with: CloudIntegrationTests.identifier) + Valet.iCloudPermutations(with: ValetIntegrationTests.identifier, shared: true)
: [])
}
let key = "key"
let passcode = "topsecret"

Expand All @@ -39,10 +43,7 @@ class CloudIntegrationTests: XCTestCase
ErrorHandler.customAssertBody = { _, _, _, _ in
// Nothing to do here.
}

valet.removeAllObjects()
let identifier = CloudTests.identifier
let allPermutations = Valet.iCloudPermutations(with: identifier) + Valet.iCloudPermutations(with: identifier, shared: true)

allPermutations.forEach { testValet in testValet.removeAllObjects() }
}

Expand All @@ -51,65 +52,48 @@ class CloudIntegrationTests: XCTestCase
guard testEnvironmentIsSigned() else {
return
}

let localValet = Valet.valet(with: valet.identifier, accessibility: valet.accessibility)

let identifier = Identifier(nonEmpty: "DistinctTest")!
let vanillaValet = Valet.valet(with: identifier, accessibility: .afterFirstUnlock)
let iCloudValet = Valet.iCloudValet(with: identifier, accessibility: .afterFirstUnlock)

// Setting
XCTAssertTrue(valet.set(string: "butts", forKey: "cloud"))
XCTAssertEqual("butts", valet.string(forKey: "cloud"))
XCTAssertNil(localValet.string(forKey: "cloud"))
XCTAssertTrue(iCloudValet.set(string: "butts", forKey: "cloud"))
XCTAssertEqual("butts", iCloudValet.string(forKey: "cloud"))
XCTAssertNil(vanillaValet.string(forKey: "cloud"))

// Removal
XCTAssertTrue(localValet.set(string: "snake people", forKey: "millennials"))
XCTAssertTrue(valet.removeObject(forKey: "millennials"))
XCTAssertEqual("snake people", localValet.string(forKey: "millennials"))
XCTAssertTrue(vanillaValet.set(string: "snake people", forKey: "millennials"))
XCTAssertTrue(iCloudValet.removeObject(forKey: "millennials"))
XCTAssertEqual("snake people", vanillaValet.string(forKey: "millennials"))
}

func test_setStringForKey()
{
guard testEnvironmentIsSigned() else {
return
allPermutations.forEach { valet in
XCTAssertNil(valet.string(forKey: key), "\(valet) read item from keychain that should not exist")
XCTAssertTrue(valet.set(string: passcode, forKey: key), "\(valet) could not set item in keychain")
XCTAssertEqual(passcode, valet.string(forKey: key))
}

XCTAssertNil(valet.string(forKey: key))
XCTAssertTrue(valet.set(string: passcode, forKey: key))
XCTAssertEqual(passcode, valet.string(forKey: key))
}

func test_removeObjectForKey()
{
guard testEnvironmentIsSigned() else {
return
allPermutations.forEach { valet in
XCTAssertTrue(valet.set(string: passcode, forKey: key), "\(valet) could not set item in keychain")
XCTAssertEqual(passcode, valet.string(forKey: key), "\(valet) read incorrect value from keychain.")

XCTAssertTrue(valet.removeObject(forKey: key), "\(valet) did not remove item from keychain.")
XCTAssertNil(valet.string(forKey: key), "\(valet) found removed item in keychain.")
}

XCTAssertTrue(valet.set(string: passcode, forKey: key))
XCTAssertEqual(passcode, valet.string(forKey: key))

XCTAssertTrue(valet.removeObject(forKey: key))
XCTAssertNil(valet.string(forKey: key))
}

// MARK: canAccessKeychain

func test_canAccessKeychain()
{
guard testEnvironmentIsSigned() else {
return
}

Valet.iCloudPermutations(with: valet.identifier).forEach { permutation in
XCTAssertTrue(permutation.canAccessKeychain(), "\(permutation) could not access keychain.")
allPermutations.forEach { valet in
XCTAssertTrue(valet.canAccessKeychain(), "\(valet) could not access keychain.")
}
}

func test_canAccessKeychain_sharedAccessGroup()
{
guard testEnvironmentIsSigned() else {
return
}

Valet.iCloudPermutations(with: Valet.sharedAccessGroupIdentifier, shared: true).forEach { permutation in
XCTAssertTrue(permutation.canAccessKeychain(), "\(permutation) could not access keychain.")
}
}
}
Loading

0 comments on commit 50e134e

Please sign in to comment.