1/* 2 * Copyright (c) 2012,2014 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 25#include "utilities/der_plist.h" 26#include "utilities/der_plist_internal.h" 27 28#include "utilities/SecCFRelease.h" 29#include "utilities/array_size.h" 30 31#include <CoreFoundation/CFData.h> 32 33#include "utilities_regressions.h" 34 35#define kMaxResultSize 512 36 37struct test_case { 38 size_t data_size; 39 uint8_t data[kMaxResultSize]; 40 size_t result_size; 41 uint8_t result[kMaxResultSize]; 42}; 43 44static struct test_case test_cases[] = { 45 { .data_size = 3, .data = { 0xFE, 0xFB, 0x10 }, 46 .result_size = 5, .result = { 0x04, 0x03, 0xFE, 0xFB, 0x10, }, }, 47 { .data_size = 260, .data = { 0xf2, 0x82, 0x8f, 0x71, 0x50, 0xdc, 0x2e, 0x7d, 0x1d, 0x9e, 0x1c, 0xea, 48 0xb4, 0x47, 0x2c, 0xc7, 0xc6, 0x11, 0x5d, 0x2d, 0xe6, 0xec, 0x3d, 0xea, 49 0x24, 0x06, 0x6a, 0x37, 0xbc, 0x80, 0xdd, 0x8e, 0x9b, 0xf0, 0x5d, 0x5c, 50 0x1a, 0xb4, 0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x04, 0x82, 0xCB, 0x1D, 0x09, 81 0xF4, 0xB7, 0x2C, 0xE8, 0x23, 0x5E, 0x24, 0x99, 0x85, 0x0A, 0xC4, 0x2F, 82 0xE7, 0xDC, 0xB4, 0x3C, 0xA8, 0xF3, 0xFB, 0xC6, 0x6E, 0xD8, 0xFD, 0x76, 83 0xC2, 0x50, 0x15, 0xC9, 0x6D, 0x2F, 0xCF, 0x1B, 0x8F, 0x73, 0x24, 0x19, 84 0x12, 0x21, 0xA1, 0x50, 0x37, 0x8F, 0xA4, 0x27, 0x6C, 0x0B, 0x39, 0xEF, 85 0x24, 0xD8, 0x3D, 0x97, 0xC8, 0x46, 0x5D, 0x86, 0x0F, 0x99, 0x24, 0x48, 86 0x94, 0xD0, 0x51, 0x49, 0x9A, 0xB2, 0x29, 0x51, 0x66, 0x41, 0xD0, 0x52, 87 0xA3, 0x15, 0x5A, 0x99, 0xFB, 0xF2, 0x2D, 0xFC, 0x73, 0xA7, 0x03, 0xD5, 88 0xB6, 0x45, 0x48, 0x11, 0x93, 0xA9, 0xB7, 0x2D, 0xD4, 0x22, 0xBB, 0x87, 89 0xA3, 0xD4, 0x5D, 0xFF, 0xDA, 0x84, 0x91, 0x6C, 0xEA, 0x72, 0x52, 0x7A, 90 0xFC, 0x36, 0xE1, 0xC4, 0x40, 0xC6, 0x65, 0x8E, 0x55, 0x5B, 0x54, 0x21, 91 0x41, 0x1D, 0xE0, 0xE5, 0x0B, 0x4B, 0x62, 0xC5, }, }, 92}; 93 94 95#define kTestsPerTestCase 8 96static void one_test(const struct test_case * thisCase) 97{ 98 CFDataRef start = CFDataCreateWithBytesNoCopy(NULL, thisCase->data, thisCase->data_size, kCFAllocatorNull); 99 100 uint8_t buffer[kMaxResultSize]; 101 uint8_t *buffer_end = buffer + sizeof(buffer); 102 103 uint8_t* encoded = der_encode_plist(start, NULL, buffer, buffer_end); 104 105 ok(encoded != NULL && 106 (thisCase->result_size == (buffer_end - encoded)) && 107 (memcmp(encoded, thisCase->result, thisCase->result_size) == 0)); 108 109 encoded = der_encode_data(start, NULL, buffer, buffer_end); 110 111 ok(encoded != NULL && 112 (thisCase->result_size == (buffer_end - encoded)) && 113 (memcmp(encoded, thisCase->result, thisCase->result_size) == 0)); 114 115#if 0 116 printf(".size = %d, .res = { ", (buffer_end - encoded)); 117 for(int c = 0; c < (buffer_end - encoded); ++c) 118 printf("0x%02X, ", encoded[c]); 119 printf("},\n"); 120#endif 121 122 CFDataRef decoded = NULL; 123 const uint8_t* decode_end = der_decode_data(NULL, kCFPropertyListMutableContainersAndLeaves, 124 &decoded, NULL, encoded, buffer_end); 125 126 ok(decode_end == buffer_end); 127 ok((decoded != NULL) && CFEqual(decoded, start)); 128 129 CFTypeRef decoded_type = NULL; 130 decode_end = der_decode_plist(NULL, kCFPropertyListMutableContainersAndLeaves, 131 &decoded_type, NULL, encoded, buffer_end); 132 133 ok(decode_end == buffer_end); 134 ok((decoded != NULL) && CFEqual(decoded_type, start)); 135 136 ok(der_sizeof_data(start, NULL) == thisCase->result_size); 137 ok(der_sizeof_plist(start, NULL) == thisCase->result_size); 138 139 CFReleaseNull(start); 140 CFReleaseNull(decoded); 141 CFReleaseNull(decoded_type); 142} 143 144#define kTestCount (array_size(test_cases) * kTestsPerTestCase) 145static void tests(void) 146{ 147 for (int testnumber = 0; testnumber < array_size(test_cases); ++testnumber) 148 one_test(test_cases + testnumber); 149} 150 151int su_11_cfdata_der(int argc, char *const *argv) 152{ 153 plan_tests(kTestCount); 154 tests(); 155 156 return 0; 157} 158