]> git.saurik.com Git - apple/security.git/blob - keychain/trust/TrustedPeersTests/TPVoucherTests.m
Security-58286.41.2.tar.gz
[apple/security.git] / keychain / trust / TrustedPeersTests / TPVoucherTests.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 TPVoucherTests : XCTestCase
29
30 @end
31
32 @implementation TPVoucherTests
33
34 - (void)testRoundTrip
35 {
36 NSData *keyData = [@"key" dataUsingEncoding:NSUTF8StringEncoding];
37 id<TPSigningKey> key = [[TPDummySigningKey alloc] initWithPublicKeyData:keyData];
38
39 TPVoucher *voucher1 = [TPVoucher voucherWithBeneficiaryID:@"B"
40 sponsorID:@"A"
41 clock:1
42 trustSigningKey:key
43 error:NULL];
44 TPVoucher *voucher1b = [TPVoucher voucherWithPList:voucher1.voucherInfoPList
45 sig:voucher1.voucherInfoSig];
46
47 XCTAssertEqualObjects(voucher1, voucher1b);
48 XCTAssertEqual([voucher1 hash], [voucher1b hash]);
49 XCTAssert([voucher1 isEqual:voucher1]);
50 XCTAssert([voucher1 isEqualToVoucher:voucher1]);
51 XCTAssert(![voucher1 isEqual:@"foo"]);
52
53 TPVoucher *voucher2 = [TPVoucher voucherWithBeneficiaryID:@"C"
54 sponsorID:@"A"
55 clock:1
56 trustSigningKey:key
57 error:NULL];
58 XCTAssertNotEqualObjects(voucher1, voucher2);
59 }
60
61 - (void)testMalformed
62 {
63 NSData *data = [@"foo" dataUsingEncoding:NSUTF8StringEncoding];
64 XCTAssertNil([TPVoucher voucherWithPList:data sig:data]);
65
66 data = [TPUtils serializedPListWithDictionary:@{
67 @"beneficiaryID": @[],
68 @"sponsorID": @"A",
69 @"clock": @1
70 }];
71 XCTAssertNil([TPVoucher voucherWithPList:data sig:data]);
72
73 data = [TPUtils serializedPListWithDictionary:@{
74 @"beneficiaryID": @"B",
75 @"sponsorID": @7,
76 @"clock": @1
77 }];
78 XCTAssertNil([TPVoucher voucherWithPList:data sig:data]);
79
80 data = [TPUtils serializedPListWithDictionary:@{
81 @"beneficiaryID": @"B",
82 @"sponsorID": @"A",
83 @"clock": @"foo"
84 }];
85 XCTAssertNil([TPVoucher voucherWithPList:data sig:data]);
86 }
87
88 - (void)testCannotSign
89 {
90 NSData *keyData = [@"key" dataUsingEncoding:NSUTF8StringEncoding];
91 TPDummySigningKey *key = [[TPDummySigningKey alloc] initWithPublicKeyData:keyData];
92 key.privateKeyIsAvailable = NO;
93
94 NSError *error = nil;
95 TPVoucher *voucher = [TPVoucher voucherWithBeneficiaryID:@"B"
96 sponsorID:@"A"
97 clock:1
98 trustSigningKey:key
99 error:&error];
100 XCTAssertNil(voucher);
101 }
102
103 @end