]> git.saurik.com Git - apple/security.git/blob - keychain/trust/TrustedPeersTests/TPPeerTests.m
Security-58286.1.32.tar.gz
[apple/security.git] / keychain / trust / TrustedPeersTests / TPPeerTests.m
1 /*
2 * Copyright (c) 2017 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 <TrustedPeers/TrustedPeers.h>
26 #import "TPDummySigningKey.h"
27
28 @interface TPPeerTests : XCTestCase
29
30 @property (nonatomic, strong) TPPeer *peer;
31 @property (nonatomic, strong) TPDummySigningKey *goodKey;
32 @property (nonatomic, strong) TPDummySigningKey *badKey;
33
34 @end
35
36 @implementation TPPeerTests
37
38 - (void)setUp
39 {
40 NSData *goodKeyData = [@"goodKey" dataUsingEncoding:NSUTF8StringEncoding];
41 self.goodKey = [[TPDummySigningKey alloc] initWithPublicKeyData:goodKeyData];
42
43 NSData *badKeyData = [@"badKey" dataUsingEncoding:NSUTF8StringEncoding];
44 self.badKey = [[TPDummySigningKey alloc] initWithPublicKeyData:badKeyData];
45
46 TPPeerPermanentInfo *permanentInfo;
47 permanentInfo = [TPPeerPermanentInfo permanentInfoWithMachineID:@"A"
48 modelID:@"iPhone8,1"
49 epoch:1
50 trustSigningKey:self.goodKey
51 peerIDHashAlgo:kTPHashAlgoSHA256
52 error:NULL];
53 self.peer = [[TPPeer alloc] initWithPermanentInfo:permanentInfo];
54 }
55
56 - (void)testBadDynamicInfoKey
57 {
58 // Create a dynamicInfo with the wrong key
59 TPPeerDynamicInfo *dynamicInfo = [TPPeerDynamicInfo dynamicInfoWithCircleID:@"123"
60 clique:@"clique"
61 removals:0
62 clock:1
63 trustSigningKey:self.badKey
64 error:NULL];
65 XCTAssertEqual(TPResultSignatureMismatch, [self.peer updateDynamicInfo:dynamicInfo]);
66 }
67
68 - (void)testStableInfo
69 {
70 TPPeerStableInfo *info1 = [TPPeerStableInfo stableInfoWithDict:@{ @"hello": @"world1" }
71 clock:1
72 policyVersion:1
73 policyHash:@""
74 policySecrets:nil
75 trustSigningKey:self.goodKey
76 error:NULL];
77 XCTAssertEqual(TPResultOk, [self.peer updateStableInfo:info1]);
78
79 // Attempt update without advancing clock
80 TPPeerStableInfo *info2 = [TPPeerStableInfo stableInfoWithDict:@{ @"hello": @"world2" }
81 clock:1
82 policyVersion:1
83 policyHash:@""
84 policySecrets:nil
85 trustSigningKey:self.goodKey
86 error:NULL];
87 XCTAssertEqual(TPResultClockViolation, [self.peer updateStableInfo:info2]);
88 XCTAssertEqualObjects(self.peer.stableInfo, info1);
89
90 // Advance
91 TPPeerStableInfo *info3 = [TPPeerStableInfo stableInfoWithDict:@{ @"hello": @"world3" }
92 clock:3
93 policyVersion:1
94 policyHash:@""
95 policySecrets:nil
96 trustSigningKey:self.goodKey
97 error:NULL];
98 XCTAssertEqual(TPResultOk, [self.peer updateStableInfo:info3]);
99
100 // No change, should return OK
101 XCTAssertEqual(TPResultOk, [self.peer updateStableInfo:info3]);
102
103 // Attempt replay
104 XCTAssertEqual(TPResultClockViolation, [self.peer updateStableInfo:info1]);
105 XCTAssertEqualObjects(self.peer.stableInfo, info3);
106
107 // Attempt update with wrong key
108 TPPeerStableInfo *info4 = [TPPeerStableInfo stableInfoWithDict:@{ @"hello": @"world4" }
109 clock:4
110 policyVersion:1
111 policyHash:@""
112 policySecrets:nil
113 trustSigningKey:self.badKey
114 error:NULL];
115 XCTAssertEqual(TPResultSignatureMismatch, [self.peer updateStableInfo:info4]);
116 XCTAssertEqualObjects(self.peer.stableInfo, info3);
117 }
118
119 @end