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