]> git.saurik.com Git - apple/security.git/blob - keychain/trust/TrustedPeersTests/TPPeerPermanentInfoTests.m
Security-58286.20.16.tar.gz
[apple/security.git] / keychain / trust / TrustedPeersTests / TPPeerPermanentInfoTests.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 TPPeerPermanentInfoTests : XCTestCase
29 @property (nonatomic, strong) TPPeerPermanentInfo* info;
30 @end
31
32 @implementation TPPeerPermanentInfoTests
33
34 - (void)setUp
35 {
36 NSData *keyData = [@"key123" dataUsingEncoding:NSUTF8StringEncoding];
37 TPDummySigningKey *key = [[TPDummySigningKey alloc] initWithPublicKeyData:keyData];
38
39 self.info
40 = [TPPeerPermanentInfo permanentInfoWithMachineID:@"machine123"
41 modelID:@"iPhone1,1"
42 epoch:7
43 trustSigningKey:key
44 peerIDHashAlgo:kTPHashAlgoSHA256
45 error:NULL];
46 XCTAssertNotNil(self.info);
47 }
48
49 - (void)testRoundTrip
50 {
51 TPCounter epoch = 7;
52 NSString *machineID = @"machine123";
53 NSString *modelID = @"iPhone1,1";
54
55 NSData *keyData = [@"key123" dataUsingEncoding:NSUTF8StringEncoding];
56
57 TPPeerPermanentInfo *info2
58 = [TPPeerPermanentInfo permanentInfoWithPeerID:self.info.peerID
59 permanentInfoPList:self.info.permanentInfoPList
60 permanentInfoSig:self.info.permanentInfoSig
61 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
62
63 XCTAssertEqual(info2.epoch, epoch);
64 XCTAssert([info2.machineID isEqualToString:machineID]);
65 XCTAssert([info2.modelID isEqualToString:modelID]);
66 XCTAssert([info2.trustSigningKey.publicKey isEqualToData:keyData]);
67
68 XCTAssert([info2.peerID isEqualToString:self.info.peerID]);
69 XCTAssert([info2.permanentInfoPList isEqualToData:self.info.permanentInfoPList]);
70 XCTAssert([info2.permanentInfoSig isEqualToData:self.info.permanentInfoSig]);
71 }
72
73 - (void)testNonDictionary
74 {
75 NSData *data = [NSPropertyListSerialization dataWithPropertyList:@[ @"foo", @"bar"]
76 format:NSPropertyListXMLFormat_v1_0
77 options:0
78 error:NULL];
79 TPPeerPermanentInfo *info
80 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
81 permanentInfoPList:data
82 permanentInfoSig:data
83 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
84 XCTAssertNil(info);
85 }
86
87 - (void)testBadMachineID
88 {
89 NSData *data = [TPUtils serializedPListWithDictionary:@{
90 @"machineID": @5
91 }];
92 TPPeerPermanentInfo *info
93 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
94 permanentInfoPList:data
95 permanentInfoSig:data
96 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
97 XCTAssertNil(info);
98 }
99
100 - (void)testBadModelID
101 {
102 NSData *data = [TPUtils serializedPListWithDictionary:@{
103 @"machineID": @"aaa",
104 @"modelID": @5,
105 }];
106 TPPeerPermanentInfo *info
107 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
108 permanentInfoPList:data
109 permanentInfoSig:data
110 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
111 XCTAssertNil(info);
112 }
113
114 - (void)testBadEpoch
115 {
116 NSData *data = [TPUtils serializedPListWithDictionary:@{
117 @"machineID": @"aaa",
118 @"modelID": @"iPhone7,1",
119 @"epoch": @"five",
120 }];
121 TPPeerPermanentInfo *info
122 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
123 permanentInfoPList:data
124 permanentInfoSig:data
125 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
126 XCTAssertNil(info);
127 }
128
129 - (void)testBadTrustSigningKey
130 {
131 NSData *data = [TPUtils serializedPListWithDictionary:@{
132 @"machineID": @"aaa",
133 @"modelID": @"iPhone7,1",
134 @"epoch": @5,
135 @"trustSigningKey": @"foo",
136 }];
137 TPPeerPermanentInfo *info
138 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
139 permanentInfoPList:data
140 permanentInfoSig:data
141 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
142 XCTAssertNil(info);
143 }
144
145 - (void)testBadTrustSigningKey2
146 {
147 NSData *data = [TPUtils serializedPListWithDictionary:@{
148 @"machineID": @"aaa",
149 @"modelID": @"iPhone7,1",
150 @"epoch": @5,
151 @"trustSigningKey": [NSData data],
152 }];
153 TPPeerPermanentInfo *info
154 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
155 permanentInfoPList:data
156 permanentInfoSig:data
157 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
158 XCTAssertNil(info);
159 }
160
161 - (void)testBadSignature
162 {
163 TPPeerPermanentInfo *info2
164 = [TPPeerPermanentInfo permanentInfoWithPeerID:self.info.peerID
165 permanentInfoPList:self.info.permanentInfoPList
166 permanentInfoSig:[NSData data]
167 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
168 XCTAssertNil(info2);
169 }
170
171 - (void)testBadHashAlgo
172 {
173 TPPeerPermanentInfo *info2
174 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"foo"
175 permanentInfoPList:self.info.permanentInfoPList
176 permanentInfoSig:self.info.permanentInfoSig
177 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
178 XCTAssertNil(info2);
179 }
180
181 - (void)testBadPeerID
182 {
183 TPPeerPermanentInfo *info2
184 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"SHA256:foo"
185 permanentInfoPList:self.info.permanentInfoPList
186 permanentInfoSig:self.info.permanentInfoSig
187 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
188 XCTAssertNil(info2);
189 }
190
191 - (void)testSigningKeyIsUnavailable
192 {
193 NSData *keyData = [@"key123" dataUsingEncoding:NSUTF8StringEncoding];
194 TPDummySigningKey *key = [[TPDummySigningKey alloc] initWithPublicKeyData:keyData];
195 key.privateKeyIsAvailable = NO;
196
197 NSError *error = nil;
198 TPPeerPermanentInfo *info
199 = [TPPeerPermanentInfo permanentInfoWithMachineID:@"machine123"
200 modelID:@"iPhone1,1"
201 epoch:7
202 trustSigningKey:key
203 peerIDHashAlgo:kTPHashAlgoSHA256
204 error:&error];
205 XCTAssertNil(info);
206 XCTAssertNotNil(error);
207 }
208
209 @end