]> git.saurik.com Git - apple/security.git/blob - tests/secdmockaks/testPlistDER.m
Security-58286.51.6.tar.gz
[apple/security.git] / tests / secdmockaks / testPlistDER.m
1 /*
2 * Copyright (c) 2018 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 #include "utilities/der_plist.h"
26 #include "SecCFWrappers.h"
27
28 @interface testPlistDER : XCTestCase
29 @end
30
31 static CFDataRef CreateDERFromDictionary(CFDictionaryRef di, CFErrorRef *error)
32 {
33 size_t size = der_sizeof_plist(di, error);
34 if (size == 0)
35 return NULL;
36 uint8_t *der = malloc(size);
37 if (der == NULL) {
38 return NULL;
39 }
40 der_encode_plist(di, error, der, der+size);
41 return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, der, size, kCFAllocatorMalloc);
42 }
43
44 @implementation testPlistDER
45
46
47 - (void)testSecPListLargeData {
48 NSMutableData *data = [NSMutableData dataWithLength:650000];
49 memset([data mutableBytes], 'A', [data length]);
50
51 NSDictionary *dictionary = @{
52 @"BackupKey" : [NSMutableData dataWithLength:32],
53 @"DeviceID" : data,
54 @"EscrowRecord" : @"<null>",
55 @"PreferIDFragmentation" : @(1),
56 @"PreferIDS" : @(0),
57 @"PreferIDSAckModel" : @(1),
58 @"SecurityProperties" : @{},
59 @"SerialNumber" : @"C02TD01QHXCW",
60 @"TransportType" : @"KVS",
61 @"Views" : @[
62 @"iCloudIdentity",
63 @"BackupBagV0",
64 @"PCS-Maildrop",
65 @"PCS-iMessage",
66 @"PCS-Notes",
67 @"PCS-FDE",
68 @"PCS-MasterKey",
69 @"NanoRegistry",
70 @"PCS-Feldspar",
71 @"PCS-iCloudDrive",
72 @"AccessoryPairing",
73 @"ContinuityUnlock",
74 @"WatchMigration",
75 @"PCS-Sharing",
76 @"PCS-Photos",
77 @"PCS-Escrow",
78 @"AppleTV",
79 @"HomeKit",
80 @"PCS-Backup",
81 @"PCS-CloudKit"
82 ],
83 };
84 CFErrorRef error = NULL;
85
86 size_t size = der_sizeof_plist((__bridge CFTypeRef)dictionary, &error);
87 XCTAssertNotEqual(size, (size_t)0, "no data?: %@", error);
88 CFReleaseNull(error);
89
90 uint8_t *der = malloc(size);
91 uint8_t *der_end = der + size;
92 uint8_t *der_fin = der_encode_plist((__bridge CFTypeRef)dictionary, &error, der, der_end);
93
94 XCTAssert(error == NULL, "error should be NULL: %@", error);
95 XCTAssertEqual(der, der_fin, "under/over-flow");
96
97 free(der);
98
99 CFReleaseNull(error);
100
101 NSData *outdata = (__bridge NSData *)CreateDERFromDictionary((__bridge CFTypeRef)dictionary, &error);
102 XCTAssertEqual(error, NULL, "error should be NULL: %@", error);
103 XCTAssertNotEqual(outdata, NULL, "should have data");
104
105 }
106
107 - (void)testSecPListLargeDataOtherThread
108 {
109 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
110 dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
111 [self testSecPListLargeData];
112 dispatch_semaphore_signal(sema);
113 });
114 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
115 }
116
117
118 @end