|
| 1 | +// |
| 2 | +// iOSCSRSwiftTests.swift |
| 3 | +// iOSCSRSwiftTests |
| 4 | +// |
| 5 | +// Created by Corey Baker on 11/7/16. |
| 6 | +// Copyright © 2016 One Degree Technologies. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | +import Foundation |
| 11 | +//@testable import iOSCSRSwift |
| 12 | +import iOSCSRSwift //Only testing public functions |
| 13 | + |
| 14 | +//NOTE: Testcases won't work when testing within framework. I believe this because an Application needs to have an entitlement to have Keychain access |
| 15 | + |
| 16 | +class iOSCSRSwiftTests: XCTestCase { |
| 17 | + |
| 18 | + var publicKey: SecKey? |
| 19 | + var privateKey: SecKey? |
| 20 | + var keyBlockSize: Int? |
| 21 | + var publicKeyBits: Data? |
| 22 | + |
| 23 | + override func setUp() { |
| 24 | + super.setUp() |
| 25 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 26 | + |
| 27 | + if (publicKey != nil) && (privateKey != nil) && keyBlockSize != nil{ |
| 28 | + //Keys only need to be created once, after they can be used over again |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + let tagPublic = "com.testing.ioscsrswift.public" |
| 33 | + let tagPrivate = "com.testing.ioscsrswift.private" |
| 34 | + |
| 35 | + let publicKeyParameters: [String: AnyObject] = [ |
| 36 | + String(kSecAttrIsPermanent): kCFBooleanTrue, |
| 37 | + String(kSecAttrApplicationTag): tagPublic as AnyObject, |
| 38 | + String(kSecAttrAccessible): kSecAttrAccessibleAlways |
| 39 | + ] |
| 40 | + |
| 41 | + let privateKeyParameters: [String: AnyObject] = [ |
| 42 | + String(kSecAttrIsPermanent): kCFBooleanTrue, |
| 43 | + String(kSecAttrApplicationTag): tagPrivate as AnyObject, |
| 44 | + String(kSecAttrAccessible): kSecAttrAccessibleAlways |
| 45 | + ] |
| 46 | + |
| 47 | + //Define what type of keys to be generated here |
| 48 | + let parameters: [String: AnyObject] = [ |
| 49 | + String(kSecAttrKeyType): kSecAttrKeyTypeRSA, |
| 50 | + String(kSecAttrKeySizeInBits): 2048 as AnyObject, |
| 51 | + String(kSecReturnRef): kCFBooleanTrue, |
| 52 | + kSecPublicKeyAttrs as String: publicKeyParameters as AnyObject, |
| 53 | + kSecPrivateKeyAttrs as String: privateKeyParameters as AnyObject, |
| 54 | + ] |
| 55 | + |
| 56 | + //Use Apple Security Framework to generate keys, save them to application keychain |
| 57 | + let result = SecKeyGeneratePair(parameters as CFDictionary, &publicKey, &privateKey) |
| 58 | + |
| 59 | + switch result { |
| 60 | + case errSecSuccess: |
| 61 | + print("Public and private key pair created") |
| 62 | + |
| 63 | + guard publicKey != nil else { |
| 64 | + XCTAssert(false, "Error in setUp(). PublicKey shouldn't be nil") |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + guard privateKey != nil else{ |
| 69 | + XCTAssert(false, "Error in setUp(). PrivateKey shouldn't be nil") |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + //Set block size |
| 74 | + keyBlockSize = SecKeyGetBlockSize(publicKey!) |
| 75 | + |
| 76 | + //Ask keychain to provide the publicKey in bits |
| 77 | + let query: [String: AnyObject] = [ |
| 78 | + String(kSecClass): kSecClassKey, |
| 79 | + String(kSecAttrKeyType): kSecAttrKeyTypeRSA, |
| 80 | + String(kSecAttrApplicationTag): tagPublic as AnyObject, |
| 81 | + String(kSecReturnData): kCFBooleanTrue |
| 82 | + ] |
| 83 | + |
| 84 | + var tempPublicKeyBits:AnyObject? |
| 85 | + |
| 86 | + let result = SecItemCopyMatching(query as CFDictionary, &tempPublicKeyBits) |
| 87 | + |
| 88 | + switch result { |
| 89 | + case errSecSuccess: |
| 90 | + |
| 91 | + guard let keyBits = tempPublicKeyBits as? Data else { |
| 92 | + XCTAssert(false, "Error: couldn't cast publicKeyBits from AnyObject to Data") |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + publicKeyBits = keyBits |
| 97 | + |
| 98 | + default: |
| 99 | + XCTAssert(false, "Error when retrieving publicKey in bits from the keychain: \(result)") |
| 100 | + } |
| 101 | + |
| 102 | + default: |
| 103 | + XCTAssert(false, "Error occured: \(result)") |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + override func tearDown() { |
| 108 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 109 | + super.tearDown() |
| 110 | + } |
| 111 | + |
| 112 | + func testStandardInitializer() { |
| 113 | + // This is an example of a functional test case. |
| 114 | + // Use XCTAssert and related functions to verify your tests produce the correct results. |
| 115 | + |
| 116 | + let csr = CertificateSigningRequest() |
| 117 | + |
| 118 | + guard let csrBuild = csr.build(publicKeyBits!, privateKey: privateKey!) else{ |
| 119 | + |
| 120 | + XCTAssert(false, "Could not build CSR") |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + let csrString = csrBuild.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)).addingPercentEncoding(withAllowedCharacters: CharacterSet.alphanumerics) |
| 125 | + //stringByAddingPercentEncodingForFormUrlencoded()! |
| 126 | + |
| 127 | + guard csrString == nil else{ |
| 128 | + XCTAssert(false, "Could not encode CSR to string") |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + if !csrString!.isEmpty{ |
| 133 | + XCTAssert(true, csrString!) |
| 134 | + }else{ |
| 135 | + |
| 136 | + XCTAssert(false, "Encoded CSR string was empty") |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + func testPerformanceExample() { |
| 142 | + // This is an example of a performance test case. |
| 143 | + self.measure { |
| 144 | + // Put the code you want to measure the time of here. |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments