1// 2// su-11-cfdata-der.c 3// utilities 4// 5// Created by Mitch Adler on 6/20/12. 6// Copyright (c) 2012 Apple Inc. All rights reserved. 7// 8 9#include "utilities/der_plist.h" 10#include "utilities/der_plist_internal.h" 11 12#include "utilities/SecCFRelease.h" 13#include "utilities/array_size.h" 14 15#include <CoreFoundation/CFData.h> 16 17#include "utilities_regressions.h" 18 19#define kMaxResultSize 512 20 21struct test_case { 22 size_t data_size; 23 uint8_t data[kMaxResultSize]; 24 size_t result_size; 25 uint8_t result[kMaxResultSize]; 26}; 27 28static struct test_case test_cases[] = { 29 { .data_size = 3, .data = { 0xFE, 0xFB, 0x10 }, 30 .result_size = 5, .result = { 0x04, 0x03, 0xFE, 0xFB, 0x10, }, }, 31 { .data_size = 260, .data = { 0xf2, 0x82, 0x8f, 0x71, 0x50, 0xdc, 0x2e, 0x7d, 0x1d, 0x9e, 0x1c, 0xea, 32 0xb4, 0x47, 0x2c, 0xc7, 0xc6, 0x11, 0x5d, 0x2d, 0xe6, 0xec, 0x3d, 0xea, 33 0x24, 0x06, 0x6a, 0x37, 0xbc, 0x80, 0xdd, 0x8e, 0x9b, 0xf0, 0x5d, 0x5c, 34 0x1a, 0xb4, 0x48, 0xec, 0x23, 0x20, 0x98, 0x63, 0x87, 0x0e, 0x21, 0xa6, 35 0x20, 0xef, 0xd8, 0xde, 0xd8, 0x89, 0xca, 0x8c, 0x42, 0x3c, 0xce, 0xdc, 36 0xc3, 0x7f, 0xa0, 0xb2, 0xce, 0xf6, 0xcc, 0x0b, 0x46, 0x5f, 0xc0, 0xd6, 37 0x2a, 0x6e, 0x0d, 0x4e, 0x78, 0xa1, 0x4c, 0x17, 0xb6, 0xa7, 0x48, 0xca, 38 0x05, 0x54, 0xf1, 0xa1, 0x03, 0xac, 0xb0, 0x2e, 0xa5, 0xdd, 0xf2, 0x6a, 39 0x51, 0xe5, 0xa0, 0xcf, 0xd1, 0x94, 0xbc, 0x8c, 0x45, 0xa8, 0xa6, 0x41, 40 0x71, 0xaf, 0x9c, 0x95, 0xb3, 0xb6, 0x71, 0x07, 0xdb, 0x95, 0xc9, 0x36, 41 0x18, 0xbc, 0x61, 0x24, 0x33, 0x4c, 0x39, 0x04, 0x82, 0xcb, 0x1d, 0x09, 42 0xf4, 0xb7, 0x2c, 0xe8, 0x23, 0x5e, 0x24, 0x99, 0x85, 0x0a, 0xc4, 0x2f, 43 0xe7, 0xdc, 0xb4, 0x3c, 0xa8, 0xf3, 0xfb, 0xc6, 0x6e, 0xd8, 0xfd, 0x76, 44 0xc2, 0x50, 0x15, 0xc9, 0x6d, 0x2f, 0xcf, 0x1b, 0x8f, 0x73, 0x24, 0x19, 45 0x12, 0x21, 0xa1, 0x50, 0x37, 0x8f, 0xa4, 0x27, 0x6c, 0x0b, 0x39, 0xef, 46 0x24, 0xd8, 0x3d, 0x97, 0xc8, 0x46, 0x5d, 0x86, 0x0f, 0x99, 0x24, 0x48, 47 0x94, 0xd0, 0x51, 0x49, 0x9a, 0xb2, 0x29, 0x51, 0x66, 0x41, 0xd0, 0x52, 48 0xa3, 0x15, 0x5a, 0x99, 0xfb, 0xf2, 0x2d, 0xfc, 0x73, 0xa7, 0x03, 0xd5, 49 0xb6, 0x45, 0x48, 0x11, 0x93, 0xa9, 0xb7, 0x2d, 0xd4, 0x22, 0xbb, 0x87, 50 0xa3, 0xd4, 0x5d, 0xff, 0xda, 0x84, 0x91, 0x6c, 0xea, 0x72, 0x52, 0x7a, 51 0xfc, 0x36, 0xe1, 0xc4, 0x40, 0xc6, 0x65, 0x8e, 0x55, 0x5b, 0x54, 0x21, 52 0x41, 0x1d, 0xe0, 0xe5, 0x0b, 0x4b, 0x62, 0xc5 }, 53 .result_size = 264, .result = { 0x04, 0x82, 0x01, 0x04, 54 0xF2, 0x82, 0x8F, 0x71, 0x50, 0xDC, 0x2E, 0x7D, 0x1D, 0x9E, 0x1C, 0xEA, 55 0xB4, 0x47, 0x2C, 0xC7, 0xC6, 0x11, 0x5D, 0x2D, 0xE6, 0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, 0x29, 0x51, 0x66, 0x41, 0xD0, 0x52, 71 0xA3, 0x15, 0x5A, 0x99, 0xFB, 0xF2, 0x2D, 0xFC, 0x73, 0xA7, 0x03, 0xD5, 72 0xB6, 0x45, 0x48, 0x11, 0x93, 0xA9, 0xB7, 0x2D, 0xD4, 0x22, 0xBB, 0x87, 73 0xA3, 0xD4, 0x5D, 0xFF, 0xDA, 0x84, 0x91, 0x6C, 0xEA, 0x72, 0x52, 0x7A, 74 0xFC, 0x36, 0xE1, 0xC4, 0x40, 0xC6, 0x65, 0x8E, 0x55, 0x5B, 0x54, 0x21, 75 0x41, 0x1D, 0xE0, 0xE5, 0x0B, 0x4B, 0x62, 0xC5, }, }, 76}; 77 78 79#define kTestsPerTestCase 8 80static void one_test(const struct test_case * thisCase) 81{ 82 CFDataRef start = CFDataCreateWithBytesNoCopy(NULL, thisCase->data, thisCase->data_size, kCFAllocatorNull); 83 84 uint8_t buffer[kMaxResultSize]; 85 uint8_t *buffer_end = buffer + sizeof(buffer); 86 87 uint8_t* encoded = der_encode_plist(start, NULL, buffer, buffer_end); 88 89 ok(encoded != NULL && 90 (thisCase->result_size == (buffer_end - encoded)) && 91 (memcmp(encoded, thisCase->result, thisCase->result_size) == 0)); 92 93 encoded = der_encode_data(start, NULL, buffer, buffer_end); 94 95 ok(encoded != NULL && 96 (thisCase->result_size == (buffer_end - encoded)) && 97 (memcmp(encoded, thisCase->result, thisCase->result_size) == 0)); 98 99#if 0 100 printf(".size = %d, .res = { ", (buffer_end - encoded)); 101 for(int c = 0; c < (buffer_end - encoded); ++c) 102 printf("0x%02X, ", encoded[c]); 103 printf("},\n"); 104#endif 105 106 CFDataRef decoded = NULL; 107 const uint8_t* decode_end = der_decode_data(NULL, kCFPropertyListMutableContainersAndLeaves, 108 &decoded, NULL, encoded, buffer_end); 109 110 ok(decode_end == buffer_end); 111 ok((decoded != NULL) && CFEqual(decoded, start)); 112 113 CFTypeRef decoded_type = NULL; 114 decode_end = der_decode_plist(NULL, kCFPropertyListMutableContainersAndLeaves, 115 &decoded_type, NULL, encoded, buffer_end); 116 117 ok(decode_end == buffer_end); 118 ok((decoded != NULL) && CFEqual(decoded_type, start)); 119 120 ok(der_sizeof_data(start, NULL) == thisCase->result_size); 121 ok(der_sizeof_plist(start, NULL) == thisCase->result_size); 122 123 CFReleaseNull(start); 124 CFReleaseNull(decoded); 125 CFReleaseNull(decoded_type); 126} 127 128#define kTestCount (array_size(test_cases) * kTestsPerTestCase) 129static void tests(void) 130{ 131 for (int testnumber = 0; testnumber < array_size(test_cases); ++testnumber) 132 one_test(test_cases + testnumber); 133} 134 135int su_11_cfdata_der(int argc, char *const *argv) 136{ 137 plan_tests(kTestCount); 138 tests(); 139 140 return 0; 141} 142