]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/tests/CKKSAPSReceiverTests.m
Security-58286.60.28.tar.gz
[apple/security.git] / keychain / ckks / tests / CKKSAPSReceiverTests.m
1 /*
2 * Copyright (c) 2016 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 <XCTest/XCTest.h>
25 #import <OCMock/OCMock.h>
26 #import <CloudKit/CloudKit.h>
27 #import <CloudKit/CloudKit_Private.h>
28 #import "keychain/ckks/CKKS.h"
29 #import "keychain/ckks/CKKSAPSReceiver.h"
30 #import "keychain/ckks/tests/MockCloudKit.h"
31 #import "keychain/ckks/CKKSCondition.h"
32
33 #if OCTAGON
34
35 @interface CKKSAPSNotificationReceiver : NSObject <CKKSZoneUpdateReceiver>
36 @property XCTestExpectation* expectation;
37 @property void (^block)(CKRecordZoneNotification* notification);
38
39 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation;
40 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation block:(void (^)(CKRecordZoneNotification* notification))block;
41 @end
42
43 @implementation CKKSAPSNotificationReceiver
44 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation {
45 return [self initWithExpectation:expectation block:nil];
46 }
47
48 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation block:(void (^)(CKRecordZoneNotification* notification))block
49 {
50 if((self = [super init])) {
51 _expectation = expectation;
52 _block = block;
53 }
54 return self;
55 }
56
57 - (void)notifyZoneChange: (CKRecordZoneNotification*) notification {
58 [self.expectation fulfill];
59 self.block(notification);
60 }
61 @end
62
63
64
65 @interface CKKSAPSReceiverTests : XCTestCase
66 @property CKRecordZoneID* testZoneID;
67 @end
68
69 @implementation CKKSAPSReceiverTests
70
71 - (APSIncomingMessage*)messageForZoneID:(CKRecordZoneID*)zoneID {
72 // reverse engineered from source code. Ugly.
73
74 NSMutableDictionary* zoneInfo = [[NSMutableDictionary alloc] init];
75 zoneInfo[@"zid"] = zoneID.zoneName; // kCKNotificationCKRecordZoneRecordZoneIDKey
76
77 NSMutableDictionary* ckinfo = [[NSMutableDictionary alloc] init];
78 ckinfo[@"fet"] = zoneInfo; // kCKNotificationCKRecordZoneKey
79
80 NSMutableDictionary* d = [[NSMutableDictionary alloc] init];
81 d[@"ck"] = ckinfo; // kCKNotificationCKKey
82
83 return [[APSIncomingMessage alloc] initWithTopic:@"i'm-not-sure" userInfo:d];
84 }
85
86 - (void)setUp {
87 [super setUp];
88
89 self.testZoneID = [[CKRecordZoneID alloc] initWithZoneName:@"testzone" ownerName:CKCurrentUserDefaultName];
90
91 // Make sure our helpers work properly
92 APSIncomingMessage* message = [self messageForZoneID:self.testZoneID];
93 XCTAssertNotNil(message, "Should have received a APSIncomingMessage");
94
95 CKNotification* notification = [CKNotification notificationFromRemoteNotificationDictionary:message.userInfo];
96 XCTAssertNotNil(notification, "Should have received a CKNotification");
97 XCTAssert([notification isKindOfClass: [CKRecordZoneNotification class]], "Should have received a CKRecordZoneNotification");
98 CKRecordZoneNotification* ckrzn = (CKRecordZoneNotification*) notification;
99
100 XCTAssertEqual(self.testZoneID, ckrzn.recordZoneID, "Should have received a notification for the test zone");
101 }
102
103 - (void)tearDown {
104 self.testZoneID = nil;
105
106 [super tearDown];
107 }
108
109
110 - (void)testRegisterAndReceive {
111 __weak __typeof(self)weakSelf = self;
112
113 CKKSAPSReceiver* apsr = [[CKKSAPSReceiver alloc] initWithEnvironmentName:@"testenvironment"
114 namedDelegatePort:SecCKKSAPSNamedPort
115 apsConnectionClass:[FakeAPSConnection class]];
116
117 XCTAssertNotNil(apsr, "Should have received a CKKSAPSReceiver");
118
119 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive notification"]
120 block:
121 ^(CKRecordZoneNotification* notification) {
122 __strong __typeof(self) strongSelf = weakSelf;
123 XCTAssertNotNil(notification, "Should have received a notification");
124 XCTAssertEqual(strongSelf.testZoneID, notification.recordZoneID, "Should have received a notification for the test zone");
125 }];
126
127 CKKSCondition* registered = [apsr registerReceiver:anr forZoneID:self.testZoneID];
128 XCTAssertEqual(0, [registered wait:1*NSEC_PER_SEC], "Registration should have completed within a second");
129 APSIncomingMessage* message = [self messageForZoneID:self.testZoneID];
130 XCTAssertNotNil(message, "Should have received a APSIncomingMessage");
131
132 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:message];
133
134 [self waitForExpectationsWithTimeout:5.0 handler:nil];
135 }
136
137 - (void)testRegisterMultipleAndReceive {
138 __weak __typeof(self)weakSelf = self;
139
140 CKKSAPSReceiver* apsr = [[CKKSAPSReceiver alloc] initWithEnvironmentName:@"testenvironment"
141 namedDelegatePort:SecCKKSAPSNamedPort
142 apsConnectionClass:[FakeAPSConnection class]];
143
144 XCTAssertNotNil(apsr, "Should have received a CKKSAPSReceiver");
145
146 CKRecordZoneID* otherZoneID = [[CKRecordZoneID alloc] initWithZoneName:@"otherzone" ownerName:CKCurrentUserDefaultName];
147
148 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive testZoneID notification"]
149 block:
150 ^(CKRecordZoneNotification* notification) {
151 __strong __typeof(self) strongSelf = weakSelf;
152 XCTAssertNotNil(notification, "Should have received a notification");
153 XCTAssertEqual(strongSelf.testZoneID, notification.recordZoneID, "Should have received a notification for the test zone");
154 }];
155 CKKSAPSNotificationReceiver* anr2 = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive otherzone notification"]
156 block:
157 ^(CKRecordZoneNotification* notification) {
158 XCTAssertNotNil(notification, "Should have received a notification");
159 XCTAssertEqual(otherZoneID, notification.recordZoneID, "Should have received a notification for the test zone");
160 }];
161
162 CKKSCondition* registered = [apsr registerReceiver:anr forZoneID:self.testZoneID];
163 CKKSCondition* registered2 = [apsr registerReceiver:anr2 forZoneID:otherZoneID];
164 XCTAssertEqual(0, [registered wait:1*NSEC_PER_SEC], "Registration should have completed within a second");
165 XCTAssertEqual(0, [registered2 wait:1*NSEC_PER_SEC], "Registration should have completed within a second");
166
167 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:[self messageForZoneID:self.testZoneID]];
168 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:[self messageForZoneID:otherZoneID]];
169
170 [self waitForExpectationsWithTimeout:5.0 handler:nil];
171 }
172
173 - (void)testReceiveNullNotificationIfRegisteredAfterDelivery {
174 CKKSAPSReceiver* apsr = [[CKKSAPSReceiver alloc] initWithEnvironmentName:@"testenvironment"
175 namedDelegatePort:SecCKKSAPSNamedPort
176 apsConnectionClass:[FakeAPSConnection class]];
177 XCTAssertNotNil(apsr, "Should have received a CKKSAPSReceiver");
178
179 // Receives a notification for the test zone
180 APSIncomingMessage* message = [self messageForZoneID:self.testZoneID];
181 XCTAssertNotNil(message, "Should have received a APSIncomingMessage");
182 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:message];
183
184 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive notification"]
185 block:
186 ^(CKRecordZoneNotification* notification) {
187 XCTAssertNil(notification, "Should not have received a notification, since we weren't alive to receive it");
188 }];
189
190 CKKSCondition* registered = [apsr registerReceiver:anr forZoneID:self.testZoneID];
191 XCTAssertEqual(0, [registered wait:1*NSEC_PER_SEC], "Registration should have completed within a second");
192
193 [self waitForExpectationsWithTimeout:5.0 handler:nil];
194 }
195
196 @end
197
198 #endif