]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/Regressions/su-17-cfset-der.c
Security-59754.41.1.tar.gz
[apple/security.git] / OSX / utilities / Regressions / su-17-cfset-der.c
1 //
2 // su-17-cfset-der.c
3 // utilities
4 //
5 // Created by Richard Murphy on 1/26/15.
6 // Copyright © 2015 Apple Inc. All rights reserved.
7 //
8 /*
9 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
10 *
11 * @APPLE_LICENSE_HEADER_START@
12 *
13 * This file contains Original Code and/or Modifications of Original Code
14 * as defined in and that are subject to the Apple Public Source License
15 * Version 2.0 (the 'License'). You may not use this file except in
16 * compliance with the License. Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_HEADER_END@
29 */
30
31
32 #include "utilities_regressions.h"
33
34 #include "utilities/der_plist.h"
35 #include "utilities/der_plist_internal.h"
36
37 #include "utilities/SecCFRelease.h"
38 #include "utilities/array_size.h"
39
40 #include <CoreFoundation/CoreFoundation.h>
41
42
43 #define kMaxResultSize 1024
44
45 struct test_case {
46 CFTypeRef values[10];
47 size_t encoded_size;
48 uint8_t encoded[256];
49 };
50
51 static struct test_case test_cases[] =
52 {
53 {
54 .values = { CFSTR("AValue"), },
55 .encoded_size = 10, .encoded = { 0xD1, 0x08, 0x0C, 0x06, 0x41, 0x56, 0x61, 0x6C, 0x75, 0x65, },
56 },{
57 .values = { CFSTR("AValue"), CFSTR("BValue"), CFSTR("CValue"), CFSTR("DValue"), CFSTR("EValue"), CFSTR("FValue"), CFSTR("GValue"), CFSTR("HValue"), },
58 .encoded_size = 66, .encoded = { 0xD1, 0x40, 0x0C, 0x06, 0x41, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x42, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x43, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x44, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x45, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x46, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x47, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x48, 0x56, 0x61, 0x6C, 0x75, 0x65, },
59 },
60 };
61
62 #define kTestsPerSetTest 8
63 static void test_set(CFSetRef testValue, size_t expected_size, const uint8_t* expected_data)
64 {
65 uint8_t buffer[kMaxResultSize];
66 uint8_t* buffer_end = buffer + sizeof(buffer);
67
68 uint8_t* encoded = der_encode_plist(testValue, NULL, buffer, buffer_end);
69
70 ok(encoded != NULL &&
71 (expected_size == (buffer_end - encoded)) &&
72 (memcmp(encoded, expected_data, expected_size) == 0));
73
74 encoded = der_encode_set(testValue, NULL, buffer, buffer_end);
75
76 ok(encoded != NULL &&
77 (expected_size == (buffer_end - encoded)) &&
78 (memcmp(encoded, expected_data, expected_size) == 0));
79
80 #if 0
81 printf(".encoded_size = %d, .encoded = { ", (int) (buffer_end - encoded));
82 for(int c = 0; c < (buffer_end - encoded); ++c)
83 printf("0x%02X, ", encoded[c]);
84 printf("},\n");
85 #endif
86
87 CFSetRef decoded = NULL;
88
89 const uint8_t* decode_end = der_decode_set(NULL,
90 &decoded, NULL, encoded, buffer_end);
91
92 ok(decode_end == buffer_end, "didn't decode whole buffer");
93 ok((decoded != NULL) && CFEqual(decoded, testValue), "Didn't make equal value.");
94
95 CFPropertyListRef decoded_type = NULL;
96
97 decode_end = der_decode_plist(NULL,
98 &decoded_type, NULL, encoded, buffer_end);
99
100 ok(decode_end == buffer_end, "didn't decode whole buffer");
101 ok((decoded != NULL) && CFEqual(decoded_type, testValue), "Didn't make equal value.");
102
103 ok(der_sizeof_set(testValue, NULL) == expected_size, "Size correct.");
104 ok(der_sizeof_plist(testValue, NULL) == expected_size, "Size correct.");
105
106 CFReleaseNull(decoded);
107 CFReleaseNull(decoded_type);
108 }
109
110
111 #define kTestsPerTestCase (kTestsPerSetTest)
112 static void one_test(const struct test_case * thisCase)
113 {
114 CFIndex count = 0;
115 while (count < array_size(thisCase->values) && thisCase->values[count] != NULL)
116 ++count;
117
118 CFSetRef testValue = CFSetCreate(NULL, (const void**)thisCase->values, count, &kCFTypeSetCallBacks);
119
120 test_set(testValue, thisCase->encoded_size, thisCase->encoded);
121
122 CFReleaseNull(testValue);
123 }
124
125 #define kTestCount (array_size(test_cases) * kTestsPerTestCase)
126 static void tests(void)
127 {
128 for (int testnumber = 0; testnumber < array_size(test_cases); ++testnumber)
129 one_test(&test_cases[testnumber]);
130 }
131
132 int su_17_cfset_der(int argc, char *const *argv)
133 {
134 plan_tests(kTestCount);
135 tests();
136
137 return 0;
138 }