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/CFString.h> 32 33#include "utilities_regressions.h" 34 35 36#define kMaxResultSize 256 37 38struct test_case { 39 CFStringRef str; 40 size_t size; 41 uint8_t res[kMaxResultSize]; 42}; 43 44static struct test_case test_cases[] = { 45 { .str = CFSTR("FOO"), .size = 5, .res = { 0x0C, 0x03, 0x46, 0x4F, 0x4F, } }, 46 { .str = CFSTR("!ß∂ƒ˙圈ø¥®xzfff"), .size = 29, .res = { 0x0C, 0x1B, 0x21, 0xC3, 0x9F, 0xE2, 0x88, 0x82, 0xC6, 0x92, 47 0xCB, 0x99, 0xC3, 0xA5, 0xC5, 0x93, 0xCB, 0x86, 0xC3, 0xB8, 48 0xC2, 0xA5, 0xC2, 0xAE, 0x78, 0x7A, 0x66, 0x66, 0x66, } }, 49}; 50 51 52#define kTestsPerTestCase 8 53static void one_test(const struct test_case * thisCase) 54{ 55 uint8_t buffer[4 * kMaxResultSize + 1]; 56 uint8_t* buffer_end = buffer + sizeof(buffer); 57 58 uint8_t* encoded = der_encode_string(thisCase->str, NULL, buffer, buffer_end); 59 60 ok(encoded != NULL && 61 (thisCase->size == (buffer_end - encoded)) && 62 (memcmp(encoded, thisCase->res, thisCase->size) == 0)); 63 64 CFStringRef decoded = NULL; 65 66 const uint8_t* decode_end = der_decode_string(NULL, kCFPropertyListMutableContainersAndLeaves, 67 &decoded, NULL, encoded, buffer_end); 68 69 ok(decode_end == buffer_end); 70 ok((decoded != NULL) && CFEqual(decoded, thisCase->str)); 71 72 encoded = der_encode_plist(thisCase->str, NULL, buffer, buffer_end); 73 74 ok(encoded != NULL && 75 (thisCase->size == (buffer_end - encoded)) && 76 (memcmp(encoded, thisCase->res, thisCase->size) == 0)); 77 78 CFTypeRef decoded_type = NULL; 79 80 decode_end = der_decode_plist(NULL, kCFPropertyListMutableContainersAndLeaves, 81 &decoded_type, NULL, encoded, buffer_end); 82 83 ok(decode_end == buffer_end); 84 ok((decoded_type != NULL) && CFEqual(decoded_type, thisCase->str)); 85 86 ok(der_sizeof_string(thisCase->str, NULL) == thisCase->size); 87 ok(der_sizeof_plist(thisCase->str, NULL) == thisCase->size); 88 89 CFReleaseNull(decoded); 90 CFReleaseNull(decoded_type); 91} 92 93#define kTestCount (array_size(test_cases) * kTestsPerTestCase) 94static void tests(void) 95{ 96 for (int testnumber = 0; testnumber < array_size(test_cases); ++testnumber) 97 one_test(test_cases + testnumber); 98} 99 100int su_10_cfstring_der(int argc, char *const *argv) 101{ 102 plan_tests(kTestCount); 103 tests(); 104 105 return 0; 106} 107