Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

Commit

Permalink
Swift tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Mar 11, 2016
1 parent d2869fc commit a731e3d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 152 deletions.
8 changes: 0 additions & 8 deletions SSKeychain.xcodeproj/project.pbxproj
Expand Up @@ -22,7 +22,6 @@
21632DB21C925A6000C40D7D /* SSKeychain.m in Sources */ = {isa = PBXBuildFile; fileRef = 21632D7D1C92599100C40D7D /* SSKeychain.m */; };
21632DB31C925A6000C40D7D /* SSKeychainQuery.h in Headers */ = {isa = PBXBuildFile; fileRef = 21632D7E1C92599100C40D7D /* SSKeychainQuery.h */; settings = {ATTRIBUTES = (Public, ); }; };
21632DB41C925A6000C40D7D /* SSKeychainQuery.m in Sources */ = {isa = PBXBuildFile; fileRef = 21632D7F1C92599100C40D7D /* SSKeychainQuery.m */; };
21632DB51C925A6800C40D7D /* SSKeychainTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 21CC42DB17DB877900201DDC /* SSKeychainTests.m */; };
21632DB61C925A6D00C40D7D /* SSKeychain.strings in Resources */ = {isa = PBXBuildFile; fileRef = 21632D871C9259B400C40D7D /* SSKeychain.strings */; };
21632DC51C925B3700C40D7D /* SSKeychain.h in Headers */ = {isa = PBXBuildFile; fileRef = 21632D7C1C92599100C40D7D /* SSKeychain.h */; settings = {ATTRIBUTES = (Public, ); }; };
21632DC61C925B3700C40D7D /* SSKeychain.m in Sources */ = {isa = PBXBuildFile; fileRef = 21632D7D1C92599100C40D7D /* SSKeychain.m */; };
Expand All @@ -33,9 +32,7 @@
21632DD41C9282BD00C40D7D /* KeychainTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21632DD21C9282BD00C40D7D /* KeychainTests.swift */; };
21632DD51C9282BD00C40D7D /* KeychainTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21632DD21C9282BD00C40D7D /* KeychainTests.swift */; };
E8A6665B1A844D3A00287CA3 /* SSKeychain.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E8A666341A844CC400287CA3 /* SSKeychain.framework */; };
E8A666691A844E0500287CA3 /* SSKeychainTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 21CC42DB17DB877900201DDC /* SSKeychainTests.m */; };
E8A6667A1A844E4100287CA3 /* SSKeychain.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E8A6666F1A844E4100287CA3 /* SSKeychain.framework */; };
E8A6668F1A844E8300287CA3 /* SSKeychainTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 21CC42DB17DB877900201DDC /* SSKeychainTests.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -84,7 +81,6 @@
21CC42AC17DB874300201DDC /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
21CC42AE17DB874300201DDC /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
21CC42C317DB874300201DDC /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
21CC42DB17DB877900201DDC /* SSKeychainTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SSKeychainTests.m; sourceTree = "<group>"; };
21CC42F917DB87C300201DDC /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
E8A666341A844CC400287CA3 /* SSKeychain.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SSKeychain.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E8A666551A844D3A00287CA3 /* SSKeychainTests-iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SSKeychainTests-iOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -224,7 +220,6 @@
21CC42D917DB877900201DDC /* Tests */ = {
isa = PBXGroup;
children = (
21CC42DB17DB877900201DDC /* SSKeychainTests.m */,
21632DD21C9282BD00C40D7D /* KeychainTests.swift */,
);
path = Tests;
Expand Down Expand Up @@ -518,7 +513,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
21632DB51C925A6800C40D7D /* SSKeychainTests.m in Sources */,
21632DD51C9282BD00C40D7D /* KeychainTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -545,7 +539,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E8A666691A844E0500287CA3 /* SSKeychainTests.m in Sources */,
21632DD31C9282BD00C40D7D /* KeychainTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -563,7 +556,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E8A6668F1A844E8300287CA3 /* SSKeychainTests.m in Sources */,
21632DD41C9282BD00C40D7D /* KeychainTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
131 changes: 105 additions & 26 deletions Tests/KeychainTests.swift
Expand Up @@ -11,49 +11,62 @@ import SSKeychain

class KeychainTests: XCTestCase {

let testServiceName = "SSToolkitTestService"
let testAccountName = "SSToolkitTestAccount"
// MARK: - Properties

let testService = "SSToolkitTestService"
let testAccount = "SSToolkitTestAccount"
let testPassword = "SSToolkitTestPassword"
let testLabel = "SSToolkitLabel"


// MARK: - XCTestCase

override func tearDown() {
SSKeychain.deletePasswordForService(testService, account: testAccount)
super.tearDown()
}


// MARK: - Tests

func testNewItem() {
// New item
let newQuery = SSKeychainQuery()
newQuery.password = testPassword
newQuery.service = testServiceName
newQuery.account = testAccountName
newQuery.service = testService
newQuery.account = testAccount
newQuery.label = testLabel
try! newQuery.save()

// Look up
let lookupQuery = SSKeychainQuery()
lookupQuery.service = testServiceName
lookupQuery.account = testAccountName
lookupQuery.service = testService
lookupQuery.account = testAccount
try! lookupQuery.fetch()

XCTAssertEqual(newQuery.password, lookupQuery.password)

// Search for all accounts
let allQuery = SSKeychainQuery()
var accounts = try! allQuery.fetchAll()
XCTAssertTrue(self.accounts(accounts, containsAccountWithName: testAccountName), "Matching account was not returned")
XCTAssertTrue(self.accounts(accounts, containsAccountWithName: testAccount), "Matching account was not returned")

// Check accounts for service
allQuery.service = testServiceName
allQuery.service = testService
accounts = try! allQuery.fetchAll()
XCTAssertTrue(self.accounts(accounts, containsAccountWithName: testAccountName), "Matching account was not returned")
XCTAssertTrue(self.accounts(accounts, containsAccountWithName: testAccount), "Matching account was not returned")

// Delete
let deleteQuery = SSKeychainQuery()
deleteQuery.service = testServiceName
deleteQuery.account = testAccountName
deleteQuery.service = testService
deleteQuery.account = testAccount
try! deleteQuery.deleteItem()
}

func testPasswordObject() {
let newQuery = SSKeychainQuery()
newQuery.service = testServiceName
newQuery.account = testAccountName
newQuery.service = testService
newQuery.account = testAccount

let dictionary: [String: NSObject] = [
"number": 42,
Expand All @@ -64,51 +77,117 @@ class KeychainTests: XCTestCase {
try! newQuery.save()

let lookupQuery = SSKeychainQuery()
lookupQuery.service = testServiceName
lookupQuery.account = testAccountName
lookupQuery.service = testService
lookupQuery.account = testAccount
try! lookupQuery.fetch()

let readDictionary = lookupQuery.passwordObject as! [String: NSObject]
XCTAssertEqual(dictionary, readDictionary)
}

func testMissingInformation() {
func testCreateWithMissingInformation() {
var query = SSKeychainQuery()
query.service = testServiceName
query.account = testAccountName
query.service = testService
query.account = testAccount
XCTAssertThrowsError(try query.save())

query = SSKeychainQuery()
query.account = testAccountName
query.account = testAccount
query.password = testPassword
XCTAssertThrowsError(try query.save())

query = SSKeychainQuery()
query.service = testServiceName
query.service = testService
query.password = testPassword
XCTAssertThrowsError(try query.save())
}

func testDeleteWithMissingInformation() {
var query = SSKeychainQuery()
query.account = testAccount
XCTAssertThrowsError(try query.deleteItem())

query = SSKeychainQuery()
query.service = testService
XCTAssertThrowsError(try query.deleteItem())

query = SSKeychainQuery()
query.account = testAccount
XCTAssertThrowsError(try query.deleteItem())
}

func testFetchWithMissingInformation() {
var query = SSKeychainQuery()
query.account = testAccount
XCTAssertThrowsError(try query.fetch())

query = SSKeychainQuery()
query.service = testService
XCTAssertThrowsError(try query.fetch())
}

func testSynchronizable() {
let createQuery = SSKeychainQuery()
createQuery.service = testService
createQuery.account = testAccount
createQuery.password = testPassword
createQuery.synchronizationMode = .Yes
try! createQuery.save()

let noFetchQuery = SSKeychainQuery()
noFetchQuery.service = testService
noFetchQuery.account = testAccount
noFetchQuery.synchronizationMode = .No
XCTAssertThrowsError(try noFetchQuery.fetch())
XCTAssertNotEqual(createQuery.password, noFetchQuery.password)

let anyFetchQuery = SSKeychainQuery()
anyFetchQuery.service = testService
anyFetchQuery.account = testAccount
anyFetchQuery.synchronizationMode = .Any
try! anyFetchQuery.fetch()
XCTAssertEqual(createQuery.password, anyFetchQuery.password)
}

func testConvenienceMethods() {
// Create a new item
SSKeychain.setPassword(testPassword, forService: testService, account: testAccount)

// Check password
XCTAssertEqual(testPassword, SSKeychain.passwordForService(testService, account: testAccount))

// Check all accounts
XCTAssertTrue(accounts(SSKeychain.allAccounts(), containsAccountWithName: testAccount))

// Check account
XCTAssertTrue(accounts(SSKeychain.accountsForService(testService), containsAccountWithName: testAccount))

#if !os(OSX)
SSKeychain.setAccessibilityType(kSecAttrAccessibleWhenUnlockedThisDeviceOnly)
XCTAssertEqual(String(kSecAttrAccessibleWhenUnlockedThisDeviceOnly), String(SSKeychain.accessibilityType().takeRetainedValue()))
#endif
}

func testUpdateAccessibilityType() {
SSKeychain.setAccessibilityType(kSecAttrAccessibleWhenUnlockedThisDeviceOnly)

// Create a new item
SSKeychain.setPassword(testPassword, forService: testServiceName, account: testAccountName)
SSKeychain.setPassword(testPassword, forService: testService, account: testAccount)

// Check all accounts
XCTAssertTrue(accounts(SSKeychain.allAccounts(), containsAccountWithName: testAccountName))
XCTAssertTrue(accounts(SSKeychain.allAccounts(), containsAccountWithName: testAccount))

// Check account
XCTAssertTrue(accounts(SSKeychain.accountsForService(testServiceName), containsAccountWithName: testAccountName))
XCTAssertTrue(accounts(SSKeychain.accountsForService(testService), containsAccountWithName: testAccount))

SSKeychain.setAccessibilityType(kSecAttrAccessibleAlwaysThisDeviceOnly)
SSKeychain.setPassword(testPassword, forService: testServiceName, account: testAccountName)
SSKeychain.setPassword(testPassword, forService: testService, account: testAccount)

// Check all accounts
XCTAssertTrue(accounts(SSKeychain.allAccounts(), containsAccountWithName: testAccountName))
XCTAssertTrue(accounts(SSKeychain.allAccounts(), containsAccountWithName: testAccount))

// Check account
XCTAssertTrue(accounts(SSKeychain.accountsForService(testServiceName), containsAccountWithName: testAccountName))
XCTAssertTrue(accounts(SSKeychain.accountsForService(testService), containsAccountWithName: testAccount))
}


Expand Down
118 changes: 0 additions & 118 deletions Tests/SSKeychainTests.m

This file was deleted.

0 comments on commit a731e3d

Please sign in to comment.