-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleKeychainTests.m
More file actions
154 lines (129 loc) · 5.73 KB
/
Copy pathSimpleKeychainTests.m
File metadata and controls
154 lines (129 loc) · 5.73 KB
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
//
// SimpleKeychainTests.m
// Copyright (c) 2013 Daniel Reese <dan@danandcheryl.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "SimpleKeychain.h"
#import <XCTest/XCTest.h>
@interface SimpleKeychainTests : XCTestCase
@end
@interface SimpleKeychain (UnitTests)
@property (nonatomic, strong) NSBundle *bundle;
+ (NSString *)stringFromErrorCode:(OSStatus)code;
@end
@implementation SimpleKeychainTests
- (void)setUp
{
[super setUp];
[SimpleKeychain sharedInstance].bundle = [NSBundle bundleForClass:[self class]];
[SimpleKeychain sharedInstance].serviceName = nil;
[[SimpleKeychain sharedInstance] removeAllStrings:nil];
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
#pragma mark - Tests
- (void)testSharedInstanceShouldNotBeNil
{
XCTAssertNotNil([SimpleKeychain sharedInstance]);
}
- (void)testSharedInstanceShouldAlwaysReturnSameObject
{
XCTAssertEqualObjects([SimpleKeychain sharedInstance], [SimpleKeychain sharedInstance]);
}
- (void)testDefaultServiceNameShouldBeBundleIdentifier
{
NSString *bid = [[NSBundle bundleForClass:[self class]] bundleIdentifier];
XCTAssertEqualObjects([[SimpleKeychain sharedInstance] serviceName], bid);
}
- (void)testChangingServiceNameShouldBeRememberd
{
[SimpleKeychain sharedInstance].serviceName = @"danwazhere";
XCTAssertEqualObjects([[SimpleKeychain sharedInstance] serviceName], @"danwazhere");
}
- (void)testStringFromErrorCodeShouldReturnNilForUnknownCodes
{
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:1], nil);
}
- (void)testStringFromErrorCodeShouldReturnCorrectStrings
{
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:errSecSuccess], @"No error.");
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:errSecUnimplemented], @"Function or operation not implemented.");
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:errSecParam], @"One or more parameters passed to the function were not valid.");
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:errSecAllocate], @"Failed to allocate memory.");
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:errSecNotAvailable], @"No trust results are available.");
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:errSecAuthFailed], @"Authorization/Authentication failed.");
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:errSecDuplicateItem], @"The item already exists.");
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:errSecItemNotFound], @"The item cannot be found.");
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:errSecInteractionNotAllowed], @"Interaction with the Security Server is not allowed.");
XCTAssertEqualObjects([SimpleKeychain stringFromErrorCode:errSecDecode], @"Unable to decode the provided data.");
}
- (void)testGettingValueWithNilKeyShouldThrowAnException
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
XCTAssertThrows([[SimpleKeychain sharedInstance] stringForKey:nil error:nil]);
#pragma clang diagnostic pop
}
- (void)testDefaultValueOfMissingItemShouldBeNil
{
XCTAssertNil([[SimpleKeychain sharedInstance] stringForKey:@"testkey" error:nil]);
}
- (void)testGettingValueOfMissingItemShouldReturnNotFoundError
{
NSError *error = nil;
[[SimpleKeychain sharedInstance] stringForKey:@"testkey" error:&error];
XCTAssertEqual([error code], errSecItemNotFound);
}
- (void)testSettingValueShouldNotReturnError
{
NSError *error = nil;
[[SimpleKeychain sharedInstance] setString:@"testvalue" forKey:@"testkey" error:&error];
XCTAssertNil(error, @"%@", [error localizedDescription]);
}
- (void)testSettingValueShouldSaveValue
{
[[SimpleKeychain sharedInstance] setString:@"testvalue" forKey:@"testkey" error:nil];
XCTAssertEqualObjects([[SimpleKeychain sharedInstance] stringForKey:@"testkey" error:nil], @"testvalue");
}
- (void)testGettingSavedValueShouldNotReturnError
{
[[SimpleKeychain sharedInstance] setString:@"testvalue" forKey:@"testkey" error:nil];
NSError *error = nil;
[[SimpleKeychain sharedInstance] stringForKey:@"testkey" error:&error];
XCTAssertNil(error, @"%@", [error localizedDescription]);
}
- (void)testUpdatingExistingValueShouldReturnNewValue
{
[[SimpleKeychain sharedInstance] setString:@"testvalue" forKey:@"testkey" error:nil];
[[SimpleKeychain sharedInstance] setString:@"testvalue1" forKey:@"testkey" error:nil];
XCTAssertEqualObjects([[SimpleKeychain sharedInstance] stringForKey:@"testkey" error:nil], @"testvalue1");
}
- (void)testResetShouldRemoveEverythingInKeychainItem
{
[[SimpleKeychain sharedInstance] setString:@"testvalue" forKey:@"testkey" error:nil];
NSError *error = nil;
[[SimpleKeychain sharedInstance] removeAllStrings:&error];
XCTAssertNil(error);
XCTAssertNil([[SimpleKeychain sharedInstance] stringForKey:@"testkey" error:nil]);
}
@end