2 * Copyright (c) 2016 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 <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"
35 @interface CKKSAPSNotificationReceiver : NSObject <CKKSZoneUpdateReceiver>
36 @property XCTestExpectation* expectation;
37 @property void (^block)(CKRecordZoneNotification* notification);
39 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation;
40 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation block:(void (^)(CKRecordZoneNotification* notification))block;
43 @implementation CKKSAPSNotificationReceiver
44 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation {
45 return [self initWithExpectation:expectation block:nil];
48 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation block:(void (^)(CKRecordZoneNotification* notification))block
50 if((self = [super init])) {
51 _expectation = expectation;
57 - (void)notifyZoneChange: (CKRecordZoneNotification*) notification {
58 [self.expectation fulfill];
59 self.block(notification);
65 @interface CKKSAPSReceiverTests : XCTestCase
66 @property CKRecordZoneID* testZoneID;
69 @implementation CKKSAPSReceiverTests
71 - (APSIncomingMessage*)messageForZoneID:(CKRecordZoneID*)zoneID {
72 // reverse engineered from source code. Ugly.
74 NSMutableDictionary* zoneInfo = [[NSMutableDictionary alloc] init];
75 zoneInfo[@"zid"] = zoneID.zoneName; // kCKNotificationCKRecordZoneRecordZoneIDKey
77 NSMutableDictionary* ckinfo = [[NSMutableDictionary alloc] init];
78 ckinfo[@"fet"] = zoneInfo; // kCKNotificationCKRecordZoneKey
80 NSMutableDictionary* d = [[NSMutableDictionary alloc] init];
81 d[@"ck"] = ckinfo; // kCKNotificationCKKey
83 return [[APSIncomingMessage alloc] initWithTopic:@"i'm-not-sure" userInfo:d];
89 self.testZoneID = [[CKRecordZoneID alloc] initWithZoneName:@"testzone" ownerName:CKCurrentUserDefaultName];
91 // Make sure our helpers work properly
92 APSIncomingMessage* message = [self messageForZoneID:self.testZoneID];
93 XCTAssertNotNil(message, "Should have received a APSIncomingMessage");
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;
100 XCTAssertEqual(self.testZoneID, ckrzn.recordZoneID, "Should have received a notification for the test zone");
104 self.testZoneID = nil;
110 - (void)testRegisterAndReceive {
111 __weak __typeof(self)weakSelf = self;
113 CKKSAPSReceiver* apsr = [[CKKSAPSReceiver alloc] initWithEnvironmentName:@"testenvironment"
114 namedDelegatePort:SecCKKSAPSNamedPort
115 apsConnectionClass:[FakeAPSConnection class]];
117 XCTAssertNotNil(apsr, "Should have received a CKKSAPSReceiver");
119 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive notification"]
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");
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");
132 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:message];
134 [self waitForExpectationsWithTimeout:5.0 handler:nil];
137 - (void)testRegisterMultipleAndReceive {
138 __weak __typeof(self)weakSelf = self;
140 CKKSAPSReceiver* apsr = [[CKKSAPSReceiver alloc] initWithEnvironmentName:@"testenvironment"
141 namedDelegatePort:SecCKKSAPSNamedPort
142 apsConnectionClass:[FakeAPSConnection class]];
144 XCTAssertNotNil(apsr, "Should have received a CKKSAPSReceiver");
146 CKRecordZoneID* otherZoneID = [[CKRecordZoneID alloc] initWithZoneName:@"otherzone" ownerName:CKCurrentUserDefaultName];
148 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive testZoneID notification"]
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");
155 CKKSAPSNotificationReceiver* anr2 = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive otherzone notification"]
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");
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");
167 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:[self messageForZoneID:self.testZoneID]];
168 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:[self messageForZoneID:otherZoneID]];
170 [self waitForExpectationsWithTimeout:5.0 handler:nil];
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");
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];
184 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive notification"]
186 ^(CKRecordZoneNotification* notification) {
187 XCTAssertNil(notification, "Should not have received a notification, since we weren't alive to receive it");
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");
193 [self waitForExpectationsWithTimeout:5.0 handler:nil];