]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/tests/OctagonAPSReceiverTests.m
Security-59306.61.1.tar.gz
[apple/security.git] / keychain / ckks / tests / OctagonAPSReceiverTests.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/OctagonAPSReceiver.h"
30 #import "keychain/ckks/tests/OctagonAPSReceiverTests.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 OctagonAPSReceiverTests
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 zoneInfo[@"zoid"] = CKCurrentUserDefaultName; // kCKNotificationCKRecordZoneRecordZoneOwnerIDKey
73
74 NSMutableDictionary* ckinfo = [[NSMutableDictionary alloc] init];
75 ckinfo[@"fet"] = zoneInfo; // kCKNotificationCKRecordZoneKey
76
77 NSMutableDictionary* d = [[NSMutableDictionary alloc] init];
78 d[@"ck"] = ckinfo; // kCKNotificationCKKey
79
80 return [[APSIncomingMessage alloc] initWithTopic:@"i'm-not-sure" userInfo:d];
81 }
82
83 - (void)setUp {
84 [super setUp];
85
86 self.testZoneID = [[CKRecordZoneID alloc] initWithZoneName:@"testzone" ownerName:CKCurrentUserDefaultName];
87
88 // Make sure our helpers work properly
89 APSIncomingMessage* message = [OctagonAPSReceiverTests messageForZoneID:self.testZoneID];
90 XCTAssertNotNil(message, "Should have received a APSIncomingMessage");
91
92 CKNotification* notification = [CKNotification notificationFromRemoteNotificationDictionary:message.userInfo];
93 XCTAssertNotNil(notification, "Should have received a CKNotification");
94 XCTAssert([notification isKindOfClass: [CKRecordZoneNotification class]], "Should have received a CKRecordZoneNotification");
95 CKRecordZoneNotification* ckrzn = (CKRecordZoneNotification*) notification;
96
97 XCTAssertEqual(self.testZoneID, ckrzn.recordZoneID, "Should have received a notification for the test zone");
98 }
99
100 - (void)tearDown {
101 self.testZoneID = nil;
102
103 [super tearDown];
104 }
105
106
107 - (void)testRegisterAndReceive {
108 __weak __typeof(self)weakSelf = self;
109
110 OctagonAPSReceiver* apsr = [[OctagonAPSReceiver alloc] initWithEnvironmentName:@"testenvironment"
111 namedDelegatePort:SecCKKSAPSNamedPort
112 apsConnectionClass:[FakeAPSConnection class]];
113
114 XCTAssertNotNil(apsr, "Should have received a OctagonAPSReceiver");
115
116 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive notification"]
117 block:
118 ^(CKRecordZoneNotification* notification) {
119 __strong __typeof(self) strongSelf = weakSelf;
120 XCTAssertNotNil(notification, "Should have received a notification");
121 XCTAssertEqual(strongSelf.testZoneID, notification.recordZoneID, "Should have received a notification for the test zone");
122 }];
123
124 CKKSCondition* registered = [apsr registerReceiver:anr forZoneID:self.testZoneID];
125 XCTAssertEqual(0, [registered wait:1*NSEC_PER_SEC], "Registration should have completed within a second");
126 APSIncomingMessage* message = [OctagonAPSReceiverTests messageForZoneID:self.testZoneID];
127 XCTAssertNotNil(message, "Should have received a APSIncomingMessage");
128
129 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:message];
130
131 [self waitForExpectationsWithTimeout:5.0 handler:nil];
132 }
133
134 - (void)testRegisterMultipleAndReceive {
135 __weak __typeof(self)weakSelf = self;
136
137 OctagonAPSReceiver* apsr = [[OctagonAPSReceiver alloc] initWithEnvironmentName:@"testenvironment"
138 namedDelegatePort:SecCKKSAPSNamedPort
139 apsConnectionClass:[FakeAPSConnection class]];
140
141 XCTAssertNotNil(apsr, "Should have received a OctagonAPSReceiver");
142
143 CKRecordZoneID* otherZoneID = [[CKRecordZoneID alloc] initWithZoneName:@"otherzone" ownerName:CKCurrentUserDefaultName];
144
145 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive testZoneID notification"]
146 block:
147 ^(CKRecordZoneNotification* notification) {
148 __strong __typeof(self) strongSelf = weakSelf;
149 XCTAssertNotNil(notification, "Should have received a notification");
150 XCTAssertEqual(strongSelf.testZoneID, notification.recordZoneID, "Should have received a notification for the test zone");
151 }];
152 CKKSAPSNotificationReceiver* anr2 = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive otherzone notification"]
153 block:
154 ^(CKRecordZoneNotification* notification) {
155 XCTAssertNotNil(notification, "Should have received a notification");
156 XCTAssertEqual(otherZoneID, notification.recordZoneID, "Should have received a notification for the test zone");
157 }];
158
159 CKKSCondition* registered = [apsr registerReceiver:anr forZoneID:self.testZoneID];
160 CKKSCondition* registered2 = [apsr registerReceiver:anr2 forZoneID:otherZoneID];
161 XCTAssertEqual(0, [registered wait:1*NSEC_PER_SEC], "Registration should have completed within a second");
162 XCTAssertEqual(0, [registered2 wait:1*NSEC_PER_SEC], "Registration should have completed within a second");
163
164 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:[OctagonAPSReceiverTests messageForZoneID:self.testZoneID]];
165 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:[OctagonAPSReceiverTests messageForZoneID:otherZoneID]];
166
167 [self waitForExpectationsWithTimeout:5.0 handler:nil];
168 }
169
170 - (void)testReceiveNotificationIfRegisteredAfterDelivery {
171 OctagonAPSReceiver* apsr = [[OctagonAPSReceiver alloc] initWithEnvironmentName:@"testenvironment"
172 namedDelegatePort:SecCKKSAPSNamedPort
173 apsConnectionClass:[FakeAPSConnection class]];
174 XCTAssertNotNil(apsr, "Should have received a OctagonAPSReceiver");
175
176 // Receives a notification for the test zone
177 APSIncomingMessage* message = [OctagonAPSReceiverTests messageForZoneID:self.testZoneID];
178 XCTAssertNotNil(message, "Should have received a APSIncomingMessage");
179 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:message];
180
181 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive notification"]
182 block:
183 ^(CKRecordZoneNotification* notification) {
184 XCTAssertNotNil(notification, "Should have received a (stored) notification");
185 }];
186
187 CKKSCondition* registered = [apsr registerReceiver:anr forZoneID:self.testZoneID];
188 XCTAssertEqual(0, [registered wait:1*NSEC_PER_SEC], "Registration should have completed within a second");
189
190 [self waitForExpectationsWithTimeout:5.0 handler:nil];
191 }
192
193 @end
194
195 #endif