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