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 TPPeerPermanentInfoTests : XCTestCase
29 @property (nonatomic, strong) TPPeerPermanentInfo* info;
32 @implementation TPPeerPermanentInfoTests
36 NSData *keyData = [@"key123" dataUsingEncoding:NSUTF8StringEncoding];
37 TPDummySigningKey *key = [[TPDummySigningKey alloc] initWithPublicKeyData:keyData];
40 = [TPPeerPermanentInfo permanentInfoWithMachineID:@"machine123"
44 peerIDHashAlgo:kTPHashAlgoSHA256
46 XCTAssertNotNil(self.info);
52 NSString *machineID = @"machine123";
53 NSString *modelID = @"iPhone1,1";
55 NSData *keyData = [@"key123" dataUsingEncoding:NSUTF8StringEncoding];
57 TPPeerPermanentInfo *info2
58 = [TPPeerPermanentInfo permanentInfoWithPeerID:self.info.peerID
59 permanentInfoPList:self.info.permanentInfoPList
60 permanentInfoSig:self.info.permanentInfoSig
61 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
63 XCTAssertEqual(info2.epoch, epoch);
64 XCTAssert([info2.machineID isEqualToString:machineID]);
65 XCTAssert([info2.modelID isEqualToString:modelID]);
66 XCTAssert([info2.trustSigningKey.publicKey isEqualToData:keyData]);
68 XCTAssert([info2.peerID isEqualToString:self.info.peerID]);
69 XCTAssert([info2.permanentInfoPList isEqualToData:self.info.permanentInfoPList]);
70 XCTAssert([info2.permanentInfoSig isEqualToData:self.info.permanentInfoSig]);
73 - (void)testNonDictionary
75 NSData *data = [NSPropertyListSerialization dataWithPropertyList:@[ @"foo", @"bar"]
76 format:NSPropertyListXMLFormat_v1_0
79 TPPeerPermanentInfo *info
80 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
81 permanentInfoPList:data
83 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
87 - (void)testBadMachineID
89 NSData *data = [TPUtils serializedPListWithDictionary:@{
92 TPPeerPermanentInfo *info
93 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
94 permanentInfoPList:data
96 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
100 - (void)testBadModelID
102 NSData *data = [TPUtils serializedPListWithDictionary:@{
103 @"machineID": @"aaa",
106 TPPeerPermanentInfo *info
107 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
108 permanentInfoPList:data
109 permanentInfoSig:data
110 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
116 NSData *data = [TPUtils serializedPListWithDictionary:@{
117 @"machineID": @"aaa",
118 @"modelID": @"iPhone7,1",
121 TPPeerPermanentInfo *info
122 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
123 permanentInfoPList:data
124 permanentInfoSig:data
125 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
129 - (void)testBadTrustSigningKey
131 NSData *data = [TPUtils serializedPListWithDictionary:@{
132 @"machineID": @"aaa",
133 @"modelID": @"iPhone7,1",
135 @"trustSigningKey": @"foo",
137 TPPeerPermanentInfo *info
138 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
139 permanentInfoPList:data
140 permanentInfoSig:data
141 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
145 - (void)testBadTrustSigningKey2
147 NSData *data = [TPUtils serializedPListWithDictionary:@{
148 @"machineID": @"aaa",
149 @"modelID": @"iPhone7,1",
151 @"trustSigningKey": [NSData data],
153 TPPeerPermanentInfo *info
154 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"x"
155 permanentInfoPList:data
156 permanentInfoSig:data
157 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
161 - (void)testBadSignature
163 TPPeerPermanentInfo *info2
164 = [TPPeerPermanentInfo permanentInfoWithPeerID:self.info.peerID
165 permanentInfoPList:self.info.permanentInfoPList
166 permanentInfoSig:[NSData data]
167 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
171 - (void)testBadHashAlgo
173 TPPeerPermanentInfo *info2
174 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"foo"
175 permanentInfoPList:self.info.permanentInfoPList
176 permanentInfoSig:self.info.permanentInfoSig
177 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
181 - (void)testBadPeerID
183 TPPeerPermanentInfo *info2
184 = [TPPeerPermanentInfo permanentInfoWithPeerID:@"SHA256:foo"
185 permanentInfoPList:self.info.permanentInfoPList
186 permanentInfoSig:self.info.permanentInfoSig
187 keyFactory:[TPDummySigningKeyFactory dummySigningKeyFactory]];
191 - (void)testSigningKeyIsUnavailable
193 NSData *keyData = [@"key123" dataUsingEncoding:NSUTF8StringEncoding];
194 TPDummySigningKey *key = [[TPDummySigningKey alloc] initWithPublicKeyData:keyData];
195 key.privateKeyIsAvailable = NO;
197 NSError *error = nil;
198 TPPeerPermanentInfo *info
199 = [TPPeerPermanentInfo permanentInfoWithMachineID:@"machine123"
203 peerIDHashAlgo:kTPHashAlgoSHA256
206 XCTAssertNotNil(error);