/
EKTestInternetItems.m
209 lines (174 loc) · 9.78 KB
/
EKTestInternetItems.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// EKTestInternetItems.m
// EMKeychainUnitTests
//
// Created by Nathaniel Irons on 9/1/09.
// Copyright 2009 Bumpposoft. All rights reserved.
//
#import "EKTestInternetItems.h"
#import "EMKeychain.h"
#define kServer @"testserver.emkeychain.com"
#define kUsername @"TestUsername"
#define kPassword @"TestPassword"
#define kPath @"/foo/bar/baz/luhrmann"
#define kPort 999
#define kProtocol (SecProtocolType)kSecProtocolTypeHTTPSProxy
@implementation EKTestInternetItems
-(void)setUp {
EMKeychainItem *kcItem = [EMInternetKeychainItem addInternetKeychainItemForServer:kServer
withUsername:kUsername
password:kPassword
path:kPath
port:kPort
protocol:kProtocol];
kcItem = nil;
kcItem = [EMInternetKeychainItem internetKeychainItemForServer:kServer
withUsername:kUsername
path:kPath
port:kPort
protocol:kProtocol];
STAssertNotNil(kcItem, @"Did not expect EMKeychainItem to be nil after creation in setup");
}
-(void)tearDown {
// Find the keychain item we created in setUp
EMKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:kServer
withUsername:kUsername
path:kPath
port:kPort
protocol:kProtocol];
STAssertNotNil(kcItem, @"Did not expect EMKeychainItem to be nil after creation in teardown");
// Now delete it
NSError *err = nil;
BOOL result = [EMKeychainItem deleteKeychainItem: kcItem error: &err];
// Make sure we think it's gone
STAssertTrue(result, @"Error deleting test keychain item: %d, %@", [err code], [[err userInfo] objectForKey: NSLocalizedDescriptionKey]);
// Make sure we can't find it anymore
kcItem = [EMInternetKeychainItem internetKeychainItemForServer:kServer
withUsername:kUsername
path:kPath
port:kPort
protocol:kProtocol];
STAssertNil(kcItem, @"Did not expect EMKeychainItem to be recoverable after we just deleted it");
}
-(void)testPassword {
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:kServer
withUsername:kUsername
path:kPath
port:kPort
protocol:kProtocol];
STAssertEqualObjects([kcItem password], kPassword, @"Password from keychain did not match expected");
}
-(void)testServer {
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:kServer
withUsername:kUsername
path:kPath
port:kPort
protocol:kProtocol];
STAssertEqualObjects([kcItem server], kServer, @"Server address from keychain did not match expected");
}
-(void)testUsername {
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:kServer
withUsername:kUsername
path:kPath
port:kPort
protocol:kProtocol];
STAssertEqualObjects([kcItem username], kUsername, @"Username from keychain did not match expected");
}
-(void)testPath {
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:kServer
withUsername:kUsername
path:kPath
port:kPort
protocol:kProtocol];
STAssertEqualObjects([kcItem path], kPath, @"Path from keychain did not match expected");
}
-(void)testPort {
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:kServer
withUsername:kUsername
path:kPath
port:kPort
protocol:kProtocol];
STAssertEquals([kcItem port], kPort, @"Port from keychain ('%d') did not match expected ('%d')", [kcItem port], kPort);
}
-(void)testProtocol {
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:kServer
withUsername:kUsername
path:kPath
port:kPort
protocol:kProtocol];
STAssertEquals([kcItem protocol], kProtocol, @"Protocol from keychain ('%@') did not match expected ('%@')", NSFileTypeForHFSTypeCode([kcItem protocol]), NSFileTypeForHFSTypeCode(kProtocol));
}
-(void)testFindByServerNameAlone {
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:kServer
withUsername:nil
path:nil
port:kAnyPort
protocol:kSecProtocolTypeAny];
STAssertNotNil(kcItem, @"Did not expect EMKeychainItem to be nil after creation");
STAssertEqualObjects([kcItem server], kServer, @"Server address from keychain did not match expected");
STAssertEqualObjects([kcItem password], kPassword, @"Password from keychain did not match expected");
STAssertEqualObjects([kcItem username], kUsername, @"Username from keychain did not match expected");
STAssertEqualObjects([kcItem path], kPath, @"Path from keychain did not match expected");
STAssertEquals([kcItem port], kPort, @"Port from keychain did not match expected");
STAssertEquals([kcItem protocol], kProtocol, @"Protocol from keychain ('%@') did not match expected ('%@')", NSFileTypeForHFSTypeCode([kcItem protocol]), NSFileTypeForHFSTypeCode(kProtocol));
}
-(void)testFindByUsernameAlone {
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:nil
withUsername:kUsername
path:nil
port:kAnyPort
protocol:kSecProtocolTypeAny];
STAssertNotNil(kcItem, @"Did not expect EMKeychainItem to be nil after creation");
STAssertEqualObjects([kcItem server], kServer, @"Server address from keychain did not match expected");
STAssertEqualObjects([kcItem password], kPassword, @"Password from keychain did not match expected");
STAssertEqualObjects([kcItem username], kUsername, @"Username from keychain did not match expected");
STAssertEqualObjects([kcItem path], kPath, @"Path from keychain did not match expected");
STAssertEquals([kcItem port], kPort, @"Port from keychain did not match expected");
STAssertEquals([kcItem protocol], kProtocol, @"Protocol from keychain ('%@') did not match expected ('%@')", NSFileTypeForHFSTypeCode([kcItem protocol]), NSFileTypeForHFSTypeCode(kProtocol));
}
-(void)testFindByPathAlone {
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:nil
withUsername:nil
path:kPath
port:kAnyPort
protocol:kSecProtocolTypeAny];
STAssertNotNil(kcItem, @"Did not expect EMKeychainItem to be nil after creation");
STAssertEqualObjects([kcItem server], kServer, @"Server address from keychain did not match expected");
STAssertEqualObjects([kcItem password], kPassword, @"Password from keychain did not match expected");
STAssertEqualObjects([kcItem username], kUsername, @"Username from keychain did not match expected");
STAssertEqualObjects([kcItem path], kPath, @"Path from keychain did not match expected");
STAssertEquals([kcItem port], kPort, @"Port from keychain did not match expected");
STAssertEquals([kcItem protocol], kProtocol, @"Protocol from keychain ('%@') did not match expected ('%@')", NSFileTypeForHFSTypeCode([kcItem protocol]), NSFileTypeForHFSTypeCode(kProtocol));
}
-(void)testFindByPortAlone {
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:nil
withUsername:nil
path:nil
port:kPort
protocol:kSecProtocolTypeAny];
STAssertNotNil(kcItem, @"Did not expect EMKeychainItem to be nil after creation");
STAssertEqualObjects([kcItem server], kServer, @"Server address from keychain did not match expected");
STAssertEqualObjects([kcItem password], kPassword, @"Password from keychain did not match expected");
STAssertEqualObjects([kcItem username], kUsername, @"Username from keychain did not match expected");
STAssertEqualObjects([kcItem path], kPath, @"Path from keychain did not match expected");
STAssertEquals([kcItem port], kPort, @"Port from keychain did not match expected");
STAssertEquals([kcItem protocol], kProtocol, @"Protocol from keychain ('%@') did not match expected ('%@')", NSFileTypeForHFSTypeCode([kcItem protocol]), NSFileTypeForHFSTypeCode(kProtocol));
}
-(void)testFindByProtocolAlone {
// This test may fail if you have a keychain item for an HTTPS proxy service.
// This just means it's grabbing the wrong item. In reality, you probably
// shouldn't fetch keychain items by protocol alone.
EMInternetKeychainItem *kcItem = [EMInternetKeychainItem internetKeychainItemForServer:nil
withUsername:nil
path:nil
port:kAnyPort
protocol:kProtocol];
STAssertNotNil(kcItem, @"Did not expect EMKeychainItem to be nil after creation");
STAssertEqualObjects([kcItem server], kServer, @"Server address from keychain did not match expected");
STAssertEqualObjects([kcItem password], kPassword, @"Password from keychain did not match expected");
STAssertEqualObjects([kcItem username], kUsername, @"Username from keychain did not match expected");
STAssertEqualObjects([kcItem path], kPath, @"Path from keychain did not match expected");
STAssertEquals([kcItem port], kPort, @"Port from keychain did not match expected");
STAssertEquals([kcItem protocol], kProtocol, @"Protocol from keychain ('%@') did not match expected ('%@')", NSFileTypeForHFSTypeCode([kcItem protocol]), NSFileTypeForHFSTypeCode(kProtocol));
}
@end