2 * Copyright (c) 2018 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 #include "utilities/der_plist.h"
26 #include "SecCFWrappers.h"
28 @interface testPlistDER : XCTestCase
31 static CFDataRef CreateDERFromDictionary(CFDictionaryRef di, CFErrorRef *error)
33 size_t size = der_sizeof_plist(di, error);
36 uint8_t *der = malloc(size);
40 der_encode_plist(di, error, der, der+size);
41 return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, der, size, kCFAllocatorMalloc);
44 @implementation testPlistDER
47 - (void)testSecPListLargeData {
48 NSMutableData *data = [NSMutableData dataWithLength:650000];
49 memset([data mutableBytes], 'A', [data length]);
51 NSDictionary *dictionary = @{
52 @"BackupKey" : [NSMutableData dataWithLength:32],
54 @"EscrowRecord" : @"<null>",
55 @"PreferIDFragmentation" : @(1),
57 @"PreferIDSAckModel" : @(1),
58 @"SecurityProperties" : @{},
59 @"SerialNumber" : @"C02TD01QHXCW",
60 @"TransportType" : @"KVS",
84 CFErrorRef error = NULL;
86 size_t size = der_sizeof_plist((__bridge CFTypeRef)dictionary, &error);
87 XCTAssertNotEqual(size, (size_t)0, "no data?: %@", error);
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);
94 XCTAssert(error == NULL, "error should be NULL: %@", error);
95 XCTAssertEqual(der, der_fin, "under/over-flow");
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");
107 - (void)testSecPListLargeDataOtherThread
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);
114 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);