]> git.saurik.com Git - apple/security.git/blob - keychain/ot/tests/OTCliqueTests.m
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / ot / tests / OTCliqueTests.m
1 /*
2 * Copyright (c) 2018 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import <Foundation/Foundation.h>
25 #import "OTClique.h"
26 #import <XCTest/XCTest.h>
27 #import <utilities/SecCFWrappers.h>
28
29 @interface CliqueUnitTests : XCTestCase
30 @property (nonatomic, strong) OTClique* testClique;
31 @property (nonatomic, strong) OTConfigurationContext* testData;
32 @property (nonatomic, copy) NSString* testContextName;
33 @property (nonatomic, copy) NSString* testDSID;
34 @property (nonatomic, copy) NSString* testAltDSID;
35 @property (nonatomic, strong) SFSignInAnalytics *analytics;
36 @property (nonatomic, strong) XCTestExpectation *spiBlockExpectation;
37
38 @end
39
40 @implementation CliqueUnitTests
41
42 - (void)setUp
43 {
44 NSError *error = NULL;
45
46 [super setUp];
47 self.continueAfterFailure = NO;
48 _testDSID = @"123456789";
49 _testContextName = @"contextName";
50 _testAltDSID = @"testAltDSID";
51 _testData = [[OTConfigurationContext alloc]init];
52 _testData.context = _testContextName;
53 _testData.dsid = _testDSID;
54 _testData.altDSID = _testAltDSID;
55 _testData.analytics = _analytics;
56
57 _analytics = [[SFSignInAnalytics alloc]initWithSignInUUID:[NSUUID UUID].UUIDString category:@"com.apple.cdp" eventName:@"signed in"];
58 XCTAssertNotNil(_analytics, "sign in analytics object should not be nil");
59
60 _testClique = [[OTClique alloc]initWithContextData:_testData error:&error];
61 XCTAssertNotNil(_testClique, "clique should not be nil: %@", error);
62 }
63
64 - (void)tearDown
65 {
66 _analytics = nil;
67 _testClique = nil;
68
69 [super tearDown];
70 }
71
72
73 -(void) testCliqueStatus
74 {
75 NSError *error = NULL;
76 CliqueStatus clique = [_testClique fetchCliqueStatus:&error];
77 XCTAssertTrue(clique == CliqueStatusIn || clique == CliqueStatusError, "circle status should be in circle");
78 XCTAssertTrue(error == nil || [error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be nil");
79 }
80
81 -(void) testCachedCliqueStatus
82 {
83 NSError *error = NULL;
84 #pragma clang push
85 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
86 CliqueStatus clique = [_testClique cachedCliqueStatus:YES error:&error];
87 #pragma clang pop
88 XCTAssertTrue(clique == CliqueStatusIn || clique == CliqueStatusError, "circle status should be in circle");
89 XCTAssertTrue(error == nil || [error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be nil");
90 }
91
92 - (void) testMakeNewFriends
93 {
94 NSError *error = NULL;
95 OTConfigurationContext* newData = [[OTConfigurationContext alloc]init];
96 newData.context = _testContextName;
97 newData.dsid = _testDSID;
98 newData.altDSID = _testAltDSID;
99 newData.analytics = _analytics;
100
101 OTClique* clique = [OTClique newFriendsWithContextData:newData error:&error];
102 if(error){
103 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain] || [error.domain isEqualToString:NSMachErrorDomain], "error should be kern return error");
104 XCTAssertNil(clique, "clique should be nil");
105 }
106 else{
107 XCTAssertNotNil(clique, "new clique should be nil");
108 XCTAssertNil(error, "error should be nil");
109 }
110 }
111
112 - (void) testRemoveFriendFromClique
113 {
114 NSError *error = NULL;
115 CFErrorRef validPeerError = NULL;
116 CFArrayRef peerList = SOSCCCopyValidPeerPeerInfo(&validPeerError);
117 if(validPeerError){
118 BOOL result = [_testClique removeFriendsInClique:@[@""] error:&error];
119 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain] || [error.domain isEqualToString:NSMachErrorDomain], "error should be kern return error");
120 XCTAssertFalse(result, "should have returned NO, attempting to remove friends when not valid in the circle");
121 }else{
122 BOOL result = [_testClique removeFriendsInClique:@[@""] error:&error];
123 XCTAssertFalse(result, "should have returned NO, we passed an empty list");
124 }
125 CFReleaseNull(peerList);
126 }
127
128 - (void) testLeaveClique
129 {
130 NSError *error = NULL;
131 BOOL result = [_testClique leaveClique:&error];
132 if(error){
133 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain] || [error.domain isEqualToString:NSMachErrorDomain], "error should be kern return error");
134 XCTAssertFalse(result, "result should be NO");
135 }else{
136 XCTAssertNil(error, "error should be nil");
137 }
138 }
139
140 - (void) testListFriendIDs
141 {
142 NSError *error = NULL;
143 NSDictionary<NSString *,NSString *> *friends = [_testClique peerDeviceNamesByPeerID:&error];
144 if(error){
145 XCTAssertEqual([friends count], 0, "friends should be nil");
146 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
147 }else{
148 XCTAssertNotNil(friends, "friends should not be nil");
149 }
150 }
151
152 - (void) testWaitForInitialSync
153 {
154 NSError *error = NULL;
155 BOOL result = [_testClique waitForInitialSync:&error];
156 if(error){
157 XCTAssertFalse(result, "result should be NO");
158 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
159 }else{
160 XCTAssertTrue(result, "wait for initial sync should succeed");
161 }
162 }
163
164 - (void) testCopyViewUnawarePeerInfo
165 {
166 NSError *error = NULL;
167 NSArray* result = [_testClique copyViewUnawarePeerInfo:&error];
168 if(error){
169 XCTAssertNil(result, "result should be nil");
170 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
171 }else{
172 XCTAssertNotNil(result, "copy view unaware peer info should return an array of peer infos");
173 }
174 }
175
176 - (void) testSetUserCredentialsAndDSID
177 {
178 NSError *error = NULL;
179
180 BOOL result = [_testClique setUserCredentialsAndDSID:[NSString string] password:[NSData data] error:&error];
181 if(error) {
182 XCTAssertFalse(result, "result should be NO");
183 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain] || [error.domain isEqualToString:@"SyncedDefaults"] , "error should be from sos or KVS");
184 }else{
185 XCTAssertTrue(result, "result should be YES");
186 }
187 }
188
189 - (void) testTryUserCredentialsAndDSID
190 {
191 NSError *error = NULL;
192
193 BOOL result = [_testClique tryUserCredentialsAndDSID:[NSString string] password:[NSData data] error:&error];
194 if(error){
195 XCTAssertFalse(result, "result should be NO");
196 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
197 }else{
198 XCTAssertTrue(result, "result should be YES");
199 }
200 }
201
202 - (void) testCopyPeerPeerInfo
203 {
204 NSError *error = NULL;
205
206 NSArray* result = [_testClique copyPeerPeerInfo:&error];
207 if(error){
208 XCTAssertNil(result, "result should be nil");
209 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
210 }else{
211 XCTAssertNotNil(result, "result should not be nil");
212 }
213 }
214
215 - (void) testPeersHaveViewsEnabled
216 {
217 NSError *error = NULL;
218
219 BOOL result = [_testClique peersHaveViewsEnabled:[NSArray array] error:&error];
220 if(error){
221 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
222 }
223 XCTAssertFalse(result, "result should be NO");
224 }
225 - (void) testRequestToJoinCircle
226 {
227 NSError *error = NULL;
228
229 BOOL result = [_testClique requestToJoinCircle:&error];
230 if(error){
231 XCTAssertFalse(result, "result should be NO");
232 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
233 }else{
234 XCTAssertTrue(result, "result should be YES");
235 }
236 }
237
238 @end