|
|
@@ -2,31 +2,35 @@ import UIKit
|
|
|
import XCTest
|
|
|
|
|
|
class keychainTests: XCTestCase {
|
|
|
+
|
|
|
+ var obj: KeychainSwift!
|
|
|
+
|
|
|
override func setUp() {
|
|
|
super.setUp()
|
|
|
|
|
|
- KeychainSwift.clear()
|
|
|
- KeychainSwift.lastQueryParameters = nil
|
|
|
+ obj = KeychainSwift()
|
|
|
+ obj.clear()
|
|
|
+ obj.lastQueryParameters = nil
|
|
|
}
|
|
|
|
|
|
// MARK: - Set text
|
|
|
// -----------------------
|
|
|
|
|
|
func testSet() {
|
|
|
- XCTAssertTrue(KeychainSwift.set("hello :)", forKey: "key 1"))
|
|
|
- XCTAssertEqual("hello :)", KeychainSwift.get("key 1")!)
|
|
|
+ XCTAssertTrue(obj.set("hello :)", forKey: "key 1"))
|
|
|
+ XCTAssertEqual("hello :)", obj.get("key 1")!)
|
|
|
}
|
|
|
|
|
|
func testSet_usesAccessibleWhenUnlockedByDefault() {
|
|
|
- XCTAssertTrue(KeychainSwift.set("hello :)", forKey: "key 1"))
|
|
|
+ XCTAssertTrue(obj.set("hello :)", forKey: "key 1"))
|
|
|
|
|
|
- let accessValue = KeychainSwift.lastQueryParameters?[KeychainSwiftConstants.accessible] as? String
|
|
|
+ let accessValue = obj.lastQueryParameters?[KeychainSwiftConstants.accessible] as? String
|
|
|
XCTAssertEqual(KeychainSwiftAccessOptions.AccessibleWhenUnlocked.value, accessValue!)
|
|
|
}
|
|
|
|
|
|
func testSetWithAccessOption() {
|
|
|
- KeychainSwift.set("hello :)", forKey: "key 1", withAccess: .AccessibleAfterFirstUnlock)
|
|
|
- let accessValue = KeychainSwift.lastQueryParameters?[KeychainSwiftConstants.accessible] as? String
|
|
|
+ obj.set("hello :)", forKey: "key 1", withAccess: .AccessibleAfterFirstUnlock)
|
|
|
+ let accessValue = obj.lastQueryParameters?[KeychainSwiftConstants.accessible] as? String
|
|
|
XCTAssertEqual(KeychainSwiftAccessOptions.AccessibleAfterFirstUnlock.value, accessValue!)
|
|
|
}
|
|
|
|
|
|
@@ -36,9 +40,9 @@ class keychainTests: XCTestCase {
|
|
|
func testSetData() {
|
|
|
let data = "hello world".dataUsingEncoding(NSUTF8StringEncoding)!
|
|
|
|
|
|
- XCTAssertTrue(KeychainSwift.set(data, forKey: "key 123"))
|
|
|
+ XCTAssertTrue(obj.set(data, forKey: "key 123"))
|
|
|
|
|
|
- let dataFromKeychain = KeychainSwift.getData("key 123")!
|
|
|
+ let dataFromKeychain = obj.getData("key 123")!
|
|
|
let textFromKeychain = NSString(data: dataFromKeychain, encoding:NSUTF8StringEncoding) as! String
|
|
|
XCTAssertEqual("hello world", textFromKeychain)
|
|
|
}
|
|
|
@@ -46,9 +50,9 @@ class keychainTests: XCTestCase {
|
|
|
func testSetData_usesAccessibleWhenUnlockedByDefault() {
|
|
|
let data = "hello world".dataUsingEncoding(NSUTF8StringEncoding)!
|
|
|
|
|
|
- KeychainSwift.set(data, forKey: "key 123")
|
|
|
+ obj.set(data, forKey: "key 123")
|
|
|
|
|
|
- let accessValue = KeychainSwift.lastQueryParameters?[KeychainSwiftConstants.accessible] as? String
|
|
|
+ let accessValue = obj.lastQueryParameters?[KeychainSwiftConstants.accessible] as? String
|
|
|
XCTAssertEqual(KeychainSwiftAccessOptions.AccessibleWhenUnlocked.value, accessValue!)
|
|
|
}
|
|
|
|
|
|
@@ -56,38 +60,38 @@ class keychainTests: XCTestCase {
|
|
|
// -----------------------
|
|
|
|
|
|
func testGet_returnNilWhenValueNotSet() {
|
|
|
- XCTAssert(KeychainSwift.get("key 1") == nil)
|
|
|
+ XCTAssert(obj.get("key 1") == nil)
|
|
|
}
|
|
|
|
|
|
// MARK: - Delete
|
|
|
// -----------------------
|
|
|
|
|
|
func testDelete() {
|
|
|
- KeychainSwift.set("hello :)", forKey: "key 1")
|
|
|
- KeychainSwift.delete("key 1")
|
|
|
+ obj.set("hello :)", forKey: "key 1")
|
|
|
+ obj.delete("key 1")
|
|
|
|
|
|
- XCTAssert(KeychainSwift.get("key 1") == nil)
|
|
|
+ XCTAssert(obj.get("key 1") == nil)
|
|
|
}
|
|
|
|
|
|
func testDelete_deleteOnSingleKey() {
|
|
|
- KeychainSwift.set("hello :)", forKey: "key 1")
|
|
|
- KeychainSwift.set("hello two", forKey: "key 2")
|
|
|
+ obj.set("hello :)", forKey: "key 1")
|
|
|
+ obj.set("hello two", forKey: "key 2")
|
|
|
|
|
|
- KeychainSwift.delete("key 1")
|
|
|
+ obj.delete("key 1")
|
|
|
|
|
|
- XCTAssertEqual("hello two", KeychainSwift.get("key 2")!)
|
|
|
+ XCTAssertEqual("hello two", obj.get("key 2")!)
|
|
|
}
|
|
|
|
|
|
// MARK: - Clear
|
|
|
// -----------------------
|
|
|
|
|
|
func testClear() {
|
|
|
- KeychainSwift.set("hello :)", forKey: "key 1")
|
|
|
- KeychainSwift.set("hello two", forKey: "key 2")
|
|
|
+ obj.set("hello :)", forKey: "key 1")
|
|
|
+ obj.set("hello two", forKey: "key 2")
|
|
|
|
|
|
- KeychainSwift.clear()
|
|
|
+ obj.clear()
|
|
|
|
|
|
- XCTAssert(KeychainSwift.get("key 1") == nil)
|
|
|
- XCTAssert(KeychainSwift.get("key 2") == nil)
|
|
|
+ XCTAssert(obj.get("key 1") == nil)
|
|
|
+ XCTAssert(obj.get("key 2") == nil)
|
|
|
}
|
|
|
}
|