Evgenii Neumerzhitckii 11 years ago
parent
commit
3b0fe7c539
3 changed files with 60 additions and 35 deletions
  1. 12 15
      keychain/TegKeychain.swift
  2. 0 2
      keychain/ViewController.swift
  3. 48 18
      keychainTests/keychainTests.swift

+ 12 - 15
keychain/TegKeychain.swift

@@ -9,17 +9,17 @@
 import UIKit
 import Security
 
-class TegKeychain {
+public class TegKeychain {
   
-  class func set(key: String, value: String) -> Bool {
+  public class func set(key: String, value: String) -> Bool {
     if let currentData = value.dataUsingEncoding(NSUTF8StringEncoding) {
       return set(key, value: currentData)
     }
     
     return false
   }
-  
-  class func set(key: String, value: NSData) -> Bool {
+
+  public class func set(key: String, value: NSData) -> Bool {
     let query = [
       kSecClass as String       : kSecClassGenericPassword as String,
       kSecAttrAccount as String : key,
@@ -31,17 +31,16 @@ class TegKeychain {
     
     return status == noErr
   }
-  
-  class func getString(key: String) -> String? {
+
+  public class func getString(key: String) -> String? {
     if let currentData = getData(key) {
       return NSString(data: currentData, encoding: NSUTF8StringEncoding)
     }
     
     return nil
   }
-  
-  
-  class func getData(key: String) -> NSData? {
+
+  public class func getData(key: String) -> NSData? {
     let query = [
       kSecClass as String       : kSecClassGenericPassword,
       kSecAttrAccount as String : key,
@@ -60,8 +59,8 @@ class TegKeychain {
     
     return nil
   }
-  
-  class func delete(key: String) -> Bool {
+
+  public class func delete(key: String) -> Bool {
     let query = [
       kSecClass as String       : kSecClassGenericPassword,
       kSecAttrAccount as String : key ]
@@ -70,14 +69,12 @@ class TegKeychain {
     
     return status == noErr
   }
-  
-  
-  class func clear() -> Bool {
+
+  public class func clear() -> Bool {
     let query = [ kSecClass as String : kSecClassGenericPassword ]
     
     let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
     
     return status == noErr
   }
-  
 }

+ 0 - 2
keychain/ViewController.swift

@@ -20,8 +20,6 @@ class ViewController: UIViewController {
     super.viewDidLoad()
     
     updateValueLabel()
-    
-    
   }
 
   override func didReceiveMemoryWarning() {

+ 48 - 18
keychainTests/keychainTests.swift

@@ -8,29 +8,59 @@
 
 import UIKit
 import XCTest
+import keychain
 
 class keychainTests: XCTestCase {
+  override func setUp() {
+    super.setUp()
     
-    override func setUp() {
-        super.setUp()
-        // Put setup code here. This method is called before the invocation of each test method in the class.
-    }
+    TegKeychain.clear()
+  }
+
+  // Set
+  // -----------------------
+
+  func testSet() {
+    XCTAssertTrue(TegKeychain.set("key 1", value: "hello :)"))
+    XCTAssertEqual("hello :)", TegKeychain.getString("key 1")!)
+  }
+
+  // Get
+  // -----------------------
+
+  func testGet_returnNilWhenValueNotSet() {
+    XCTAssert(TegKeychain.getString("key 1") == nil)
+  }
+
+  // Delete
+  // -----------------------
+
+  func testDelete() {
+    TegKeychain.set("key 1", value: "hello :)")
+    TegKeychain.delete("key 1")
     
-    override func tearDown() {
-        // Put teardown code here. This method is called after the invocation of each test method in the class.
-        super.tearDown()
-    }
+    XCTAssert(TegKeychain.getString("key 1") == nil)
+  }
+
+  func testDelete_deleteOnSingleKey() {
+    TegKeychain.set("key 1", value: "hello :)")
+    TegKeychain.set("key 2", value: "hello two")
+
+    TegKeychain.delete("key 1")
     
-    func testExample() {
-        // This is an example of a functional test case.
-        XCTAssert(true, "Pass")
-    }
+    XCTAssertEqual("hello two", TegKeychain.getString("key 2")!)
+  }
+
+  // Clear
+  // -----------------------
+
+  func testClear() {
+    TegKeychain.set("key 1", value: "hello :)")
+    TegKeychain.set("key 2", value: "hello two")
     
-    func testPerformanceExample() {
-        // This is an example of a performance test case.
-        self.measureBlock() {
-            // Put the code you want to measure the time of here.
-        }
-    }
+    TegKeychain.clear()
     
+    XCTAssert(TegKeychain.getString("key 1") == nil)
+    XCTAssert(TegKeychain.getString("key 2") == nil)
+  }
 }