Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 42c1b33

Browse files
committed
Added testcase example
1 parent cac0677 commit 42c1b33

File tree

4 files changed

+152
-66
lines changed

4 files changed

+152
-66
lines changed

CommonCrypto/Info.plist

-24
This file was deleted.

iOSCSRSwift.xcodeproj/project.pbxproj

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
9109209A1E787BE3008E5535 /* iOSCSRSwift.h in Headers */ = {isa = PBXBuildFile; fileRef = 910920971E787BE3008E5535 /* iOSCSRSwift.h */; settings = {ATTRIBUTES = (Public, ); }; };
1111
9109209C1E787BE3008E5535 /* CertificateSigningRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 910920991E787BE3008E5535 /* CertificateSigningRequest.swift */; };
1212
910920A01E787BFA008E5535 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9109209E1E787BFA008E5535 /* Info.plist */; };
13-
910920A31E788CB1008E5535 /* iOS_csr_swiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 910920A21E788CB1008E5535 /* iOS_csr_swiftTests.swift */; };
13+
910920A31E788CB1008E5535 /* iOSCSRSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 910920A21E788CB1008E5535 /* iOSCSRSwiftTests.swift */; };
1414
917B68901DD0F4FD00AE5629 /* iOSCSRSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 917B68861DD0F4FD00AE5629 /* iOSCSRSwift.framework */; };
1515
/* End PBXBuildFile section */
1616

@@ -29,8 +29,7 @@
2929
910920981E787BE3008E5535 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = iOSCSRSwift/Info.plist; sourceTree = SOURCE_ROOT; };
3030
910920991E787BE3008E5535 /* CertificateSigningRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CertificateSigningRequest.swift; path = iOSCSRSwift/CertificateSigningRequest.swift; sourceTree = SOURCE_ROOT; };
3131
9109209E1E787BFA008E5535 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = iOSCSRSwiftTests/Info.plist; sourceTree = SOURCE_ROOT; };
32-
910920A21E788CB1008E5535 /* iOS_csr_swiftTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = iOS_csr_swiftTests.swift; sourceTree = "<group>"; };
33-
910920D41E78AB5F008E5535 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
32+
910920A21E788CB1008E5535 /* iOSCSRSwiftTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = iOSCSRSwiftTests.swift; sourceTree = "<group>"; };
3433
910920EC1E78B87D008E5535 /* iphonesimulator.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = iphonesimulator.modulemap; sourceTree = "<group>"; };
3534
910920FE1E799B9B008E5535 /* iphoneos.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = iphoneos.modulemap; sourceTree = "<group>"; };
3635
9109216F1E79C101008E5535 /* macosx.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = macosx.modulemap; sourceTree = "<group>"; };
@@ -63,7 +62,6 @@
6362
910920D21E78AB5F008E5535 /* CommonCrypto */ = {
6463
isa = PBXGroup;
6564
children = (
66-
910920D41E78AB5F008E5535 /* Info.plist */,
6765
910920FE1E799B9B008E5535 /* iphoneos.modulemap */,
6866
910920EC1E78B87D008E5535 /* iphonesimulator.modulemap */,
6967
9109216F1E79C101008E5535 /* macosx.modulemap */,
@@ -106,7 +104,7 @@
106104
917B68931DD0F4FD00AE5629 /* iOSCSRSwiftTests */ = {
107105
isa = PBXGroup;
108106
children = (
109-
910920A21E788CB1008E5535 /* iOS_csr_swiftTests.swift */,
107+
910920A21E788CB1008E5535 /* iOSCSRSwiftTests.swift */,
110108
9109209E1E787BFA008E5535 /* Info.plist */,
111109
);
112110
path = iOSCSRSwiftTests;
@@ -234,7 +232,7 @@
234232
isa = PBXSourcesBuildPhase;
235233
buildActionMask = 2147483647;
236234
files = (
237-
910920A31E788CB1008E5535 /* iOS_csr_swiftTests.swift in Sources */,
235+
910920A31E788CB1008E5535 /* iOSCSRSwiftTests.swift in Sources */,
238236
);
239237
runOnlyForDeploymentPostprocessing = 0;
240238
};
+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}

iOSCSRSwiftTests/iOS_csr_swiftTests.swift

-36
This file was deleted.

0 commit comments

Comments
 (0)