2 * Copyright (c) 2018 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #import <Foundation/Foundation.h>
26 #import <XCTest/XCTest.h>
27 #import <utilities/SecCFWrappers.h>
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;
40 @implementation CliqueUnitTests
44 NSError *error = NULL;
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;
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");
60 _testClique = [[OTClique alloc]initWithContextData:_testData error:&error];
61 XCTAssertNotNil(_testClique, "clique should not be nil: %@", error);
73 -(void) testCliqueStatus
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");
81 -(void) testCachedCliqueStatus
83 NSError *error = NULL;
85 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
86 CliqueStatus clique = [_testClique cachedCliqueStatus:YES error:&error];
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");
92 - (void) testMakeNewFriends
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;
101 OTClique* clique = [OTClique newFriendsWithContextData:newData error:&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");
107 XCTAssertNotNil(clique, "new clique should be nil");
108 XCTAssertNil(error, "error should be nil");
112 - (void) testRemoveFriendFromClique
114 NSError *error = NULL;
115 CFErrorRef validPeerError = NULL;
116 CFArrayRef peerList = SOSCCCopyValidPeerPeerInfo(&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");
122 BOOL result = [_testClique removeFriendsInClique:@[@""] error:&error];
123 XCTAssertFalse(result, "should have returned NO, we passed an empty list");
125 CFReleaseNull(peerList);
128 - (void) testLeaveClique
130 NSError *error = NULL;
131 BOOL result = [_testClique leaveClique:&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");
136 XCTAssertNil(error, "error should be nil");
140 - (void) testListFriendIDs
142 NSError *error = NULL;
143 NSDictionary<NSString *,NSString *> *friends = [_testClique peerDeviceNamesByPeerID:&error];
145 XCTAssertEqual([friends count], 0, "friends should be nil");
146 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
148 XCTAssertNotNil(friends, "friends should not be nil");
152 - (void) testWaitForInitialSync
154 NSError *error = NULL;
155 BOOL result = [_testClique waitForInitialSync:&error];
157 XCTAssertFalse(result, "result should be NO");
158 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
160 XCTAssertTrue(result, "wait for initial sync should succeed");
164 - (void) testCopyViewUnawarePeerInfo
166 NSError *error = NULL;
167 NSArray* result = [_testClique copyViewUnawarePeerInfo:&error];
169 XCTAssertNil(result, "result should be nil");
170 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
172 XCTAssertNotNil(result, "copy view unaware peer info should return an array of peer infos");
176 - (void) testSetUserCredentialsAndDSID
178 NSError *error = NULL;
180 BOOL result = [_testClique setUserCredentialsAndDSID:[NSString string] password:[NSData data] error:&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");
185 XCTAssertTrue(result, "result should be YES");
189 - (void) testTryUserCredentialsAndDSID
191 NSError *error = NULL;
193 BOOL result = [_testClique tryUserCredentialsAndDSID:[NSString string] password:[NSData data] error:&error];
195 XCTAssertFalse(result, "result should be NO");
196 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
198 XCTAssertTrue(result, "result should be YES");
202 - (void) testCopyPeerPeerInfo
204 NSError *error = NULL;
206 NSArray* result = [_testClique copyPeerPeerInfo:&error];
208 XCTAssertNil(result, "result should be nil");
209 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
211 XCTAssertNotNil(result, "result should not be nil");
215 - (void) testPeersHaveViewsEnabled
217 NSError *error = NULL;
219 BOOL result = [_testClique peersHaveViewsEnabled:[NSArray array] error:&error];
221 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
223 XCTAssertFalse(result, "result should be NO");
225 - (void) testRequestToJoinCircle
227 NSError *error = NULL;
229 BOOL result = [_testClique requestToJoinCircle:&error];
231 XCTAssertFalse(result, "result should be NO");
232 XCTAssertTrue([error.domain isEqualToString:(__bridge NSString*)kSOSErrorDomain], "error should be from sos");
234 XCTAssertTrue(result, "result should be YES");