2 * Copyright (c) 2017 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 <TrustedPeers/TrustedPeers.h>
26 #import "TPDummySigningKey.h"
28 @interface TPPeerTests : XCTestCase
30 @property (nonatomic, strong) TPPeer *peer;
31 @property (nonatomic, strong) TPDummySigningKey *goodKey;
32 @property (nonatomic, strong) TPDummySigningKey *badKey;
36 @implementation TPPeerTests
40 NSData *goodKeyData = [@"goodKey" dataUsingEncoding:NSUTF8StringEncoding];
41 self.goodKey = [[TPDummySigningKey alloc] initWithPublicKeyData:goodKeyData];
43 NSData *badKeyData = [@"badKey" dataUsingEncoding:NSUTF8StringEncoding];
44 self.badKey = [[TPDummySigningKey alloc] initWithPublicKeyData:badKeyData];
46 TPPeerPermanentInfo *permanentInfo;
47 permanentInfo = [TPPeerPermanentInfo permanentInfoWithMachineID:@"A"
50 trustSigningKey:self.goodKey
51 peerIDHashAlgo:kTPHashAlgoSHA256
53 self.peer = [[TPPeer alloc] initWithPermanentInfo:permanentInfo];
56 - (void)testBadDynamicInfoKey
58 // Create a dynamicInfo with the wrong key
59 TPPeerDynamicInfo *dynamicInfo = [TPPeerDynamicInfo dynamicInfoWithCircleID:@"123"
63 trustSigningKey:self.badKey
65 XCTAssertEqual(TPResultSignatureMismatch, [self.peer updateDynamicInfo:dynamicInfo]);
68 - (void)testStableInfo
70 TPPeerStableInfo *info1 = [TPPeerStableInfo stableInfoWithDict:@{ @"hello": @"world1" }
75 trustSigningKey:self.goodKey
77 XCTAssertEqual(TPResultOk, [self.peer updateStableInfo:info1]);
79 // Attempt update without advancing clock
80 TPPeerStableInfo *info2 = [TPPeerStableInfo stableInfoWithDict:@{ @"hello": @"world2" }
85 trustSigningKey:self.goodKey
87 XCTAssertEqual(TPResultClockViolation, [self.peer updateStableInfo:info2]);
88 XCTAssertEqualObjects(self.peer.stableInfo, info1);
91 TPPeerStableInfo *info3 = [TPPeerStableInfo stableInfoWithDict:@{ @"hello": @"world3" }
96 trustSigningKey:self.goodKey
98 XCTAssertEqual(TPResultOk, [self.peer updateStableInfo:info3]);
100 // No change, should return OK
101 XCTAssertEqual(TPResultOk, [self.peer updateStableInfo:info3]);
104 XCTAssertEqual(TPResultClockViolation, [self.peer updateStableInfo:info1]);
105 XCTAssertEqualObjects(self.peer.stableInfo, info3);
107 // Attempt update with wrong key
108 TPPeerStableInfo *info4 = [TPPeerStableInfo stableInfoWithDict:@{ @"hello": @"world4" }
113 trustSigningKey:self.badKey
115 XCTAssertEqual(TPResultSignatureMismatch, [self.peer updateStableInfo:info4]);
116 XCTAssertEqualObjects(self.peer.stableInfo, info3);