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/OctagonAPSReceiver.h"
30 #import "keychain/ckks/tests/OctagonAPSReceiverTests.h"
31 #import "keychain/ckks/tests/MockCloudKit.h"
32 #import "keychain/ckks/CKKSCondition.h"
36 @interface CKKSAPSNotificationReceiver : NSObject <CKKSZoneUpdateReceiverProtocol>
37 @property XCTestExpectation* expectation;
38 @property void (^block)(CKRecordZoneNotification* notification);
40 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation;
41 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation block:(void (^)(CKRecordZoneNotification* notification))block;
44 @implementation CKKSAPSNotificationReceiver
45 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation {
46 return [self initWithExpectation:expectation block:nil];
49 - (instancetype)initWithExpectation:(XCTestExpectation*)expectation block:(void (^)(CKRecordZoneNotification* notification))block
51 if((self = [super init])) {
52 _expectation = expectation;
58 - (void)notifyZoneChange: (CKRecordZoneNotification*) notification {
59 [self.expectation fulfill];
60 self.block(notification);
65 @implementation OctagonAPSReceiverTests
67 + (APSIncomingMessage*)messageForZoneID:(CKRecordZoneID*)zoneID {
68 // reverse engineered from source code. Ugly.
70 NSMutableDictionary* zoneInfo = [[NSMutableDictionary alloc] init];
71 zoneInfo[@"zid"] = zoneID.zoneName; // kCKNotificationCKRecordZoneRecordZoneIDKey
72 zoneInfo[@"zoid"] = CKCurrentUserDefaultName; // kCKNotificationCKRecordZoneRecordZoneOwnerIDKey
74 NSMutableDictionary* ckinfo = [[NSMutableDictionary alloc] init];
75 ckinfo[@"fet"] = zoneInfo; // kCKNotificationCKRecordZoneKey
77 NSMutableDictionary* d = [[NSMutableDictionary alloc] init];
78 d[@"ck"] = ckinfo; // kCKNotificationCKKey
80 return [[APSIncomingMessage alloc] initWithTopic:@"i'm-not-sure" userInfo:d];
86 self.testZoneID = [[CKRecordZoneID alloc] initWithZoneName:@"testzone" ownerName:CKCurrentUserDefaultName];
88 // Make sure our helpers work properly
89 APSIncomingMessage* message = [OctagonAPSReceiverTests messageForZoneID:self.testZoneID];
90 XCTAssertNotNil(message, "Should have received a APSIncomingMessage");
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;
97 XCTAssertEqual(self.testZoneID, ckrzn.recordZoneID, "Should have received a notification for the test zone");
101 self.testZoneID = nil;
107 - (void)testRegisterAndReceive {
108 __weak __typeof(self)weakSelf = self;
110 OctagonAPSReceiver* apsr = [[OctagonAPSReceiver alloc] initWithNamedDelegatePort:SecCKKSAPSNamedPort
111 apsConnectionClass:[FakeAPSConnection class]];
113 XCTAssertNotNil(apsr, "Should have received a OctagonAPSReceiver");
115 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive notification"]
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");
123 CKKSCondition* registered = [apsr registerCKKSReceiver:anr];
124 XCTAssertEqual(0, [registered wait:1*NSEC_PER_SEC], "Registration should have completed within a second");
125 APSIncomingMessage* message = [OctagonAPSReceiverTests messageForZoneID:self.testZoneID];
126 XCTAssertNotNil(message, "Should have received a APSIncomingMessage");
128 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:message];
130 [self waitForExpectationsWithTimeout:5.0 handler:nil];
133 - (void)testReceiveNotificationIfRegisteredAfterDelivery {
134 OctagonAPSReceiver* apsr = [[OctagonAPSReceiver alloc] initWithNamedDelegatePort:SecCKKSAPSNamedPort
135 apsConnectionClass:[FakeAPSConnection class]];
136 XCTAssertNotNil(apsr, "Should have received a OctagonAPSReceiver");
138 // Receives a notification for the test zone
139 APSIncomingMessage* message = [OctagonAPSReceiverTests messageForZoneID:self.testZoneID];
140 XCTAssertNotNil(message, "Should have received a APSIncomingMessage");
141 [apsr connection:apsr.apsConnection didReceiveIncomingMessage:message];
143 CKKSAPSNotificationReceiver* anr = [[CKKSAPSNotificationReceiver alloc] initWithExpectation:[self expectationWithDescription:@"receive notification"]
145 ^(CKRecordZoneNotification* notification) {
146 XCTAssertNotNil(notification, "Should have received a (stored) notification");
149 CKKSCondition* registered = [apsr registerCKKSReceiver:anr];
150 XCTAssertEqual(0, [registered wait:1*NSEC_PER_SEC], "Registration should have completed within a second");
152 [self waitForExpectationsWithTimeout:5.0 handler:nil];