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/CoreFoundation.h> 32 33#include "utilities_regressions.h" 34 35#define kMaxResultSize 1024 36 37struct test_case { 38 CFTypeRef elements[10]; 39 size_t encoded_size; 40 uint8_t encoded[20]; 41}; 42 43static struct test_case test_cases[] = 44{ 45 { .elements = { CFSTR("First") }, 46 .encoded_size = 9, .encoded = { 0x30, 0x07, 0x0C, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, }, }, 47 { .elements = { CFSTR("First"), CFSTR("SECOND") }, 48 .encoded_size = 17, .encoded = { 0x30, 0x0F, 0x0C, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, 0x0C, 0x06, 0x53, 0x45, 0x43, 0x4F, 0x4E, 0x44, }, }, 49 { .elements = { }, 50 .encoded_size = 2, .encoded = { 0x30, 0x00, } }, 51}; 52 53#define kTestsPerTestCase 8 54static void one_test(const struct test_case * thisCase) 55{ 56 CFIndex element_count = 0; 57 while (element_count < array_size(thisCase->elements) && thisCase->elements[element_count] != NULL) 58 ++element_count; 59 60 CFArrayRef testValue = CFArrayCreate(NULL, (const void **)&thisCase->elements, element_count, &kCFTypeArrayCallBacks); 61 62 uint8_t buffer[kMaxResultSize]; 63 uint8_t* buffer_end = buffer + sizeof(buffer); 64 65 uint8_t* encoded = der_encode_plist(testValue, NULL, buffer, buffer_end); 66 67 ok(encoded != NULL && 68 (thisCase->encoded_size == (buffer_end - encoded)) && 69 (memcmp(encoded, thisCase->encoded, thisCase->encoded_size) == 0)); 70 71 encoded = der_encode_array(testValue, NULL, buffer, buffer_end); 72 73 ok(encoded != NULL && 74 (thisCase->encoded_size == (buffer_end - encoded)) && 75 (memcmp(encoded, thisCase->encoded, thisCase->encoded_size) == 0)); 76 77#if 0 78 printf(".encoded_size = %d, .encoded = { ", (buffer_end - encoded)); 79 for(int c = 0; c < (buffer_end - encoded); ++c) 80 printf("0x%02X, ", encoded[c]); 81 printf("},\n"); 82#endif 83 84 CFArrayRef decoded = NULL; 85 86 const uint8_t* decode_end = der_decode_array(NULL, kCFPropertyListMutableContainersAndLeaves, 87 &decoded, NULL, encoded, buffer_end); 88 89 ok(decode_end == buffer_end, "didn't decode whole buffer"); 90 ok((decoded != NULL) && CFEqual(decoded, testValue), "Didn't make equal value."); 91 92 CFTypeRef decoded_type = NULL; 93 94 decode_end = der_decode_plist(NULL, kCFPropertyListMutableContainersAndLeaves, 95 &decoded_type, NULL, encoded, buffer_end); 96 97 ok(decode_end == buffer_end, "didn't decode whole buffer"); 98 ok((decoded != NULL) && CFEqual(decoded_type, testValue), "Didn't make equal value."); 99 100 ok(der_sizeof_array(testValue, NULL) == thisCase->encoded_size, "Size correct."); 101 ok(der_sizeof_plist(testValue, NULL) == thisCase->encoded_size, "Size correct."); 102 103 CFReleaseNull(decoded); 104 CFReleaseNull(decoded_type); 105 106 CFReleaseNull(testValue); 107} 108 109#define kTestCount (array_size(test_cases) * kTestsPerTestCase) 110static void tests(void) 111{ 112 for (int testnumber = 0; testnumber < array_size(test_cases); ++testnumber) 113 one_test(test_cases + testnumber); 114} 115 116int su_14_cfarray_der(int argc, char *const *argv) 117{ 118 plan_tests(kTestCount); 119 tests(); 120 121 return 0; 122} 123