]> git.saurik.com Git - apple/security.git/blob - utilities/Regressions/su-10-cfstring-der.c
Security-55471.tar.gz
[apple/security.git] / utilities / Regressions / su-10-cfstring-der.c
1 //
2 // su-10-cfstring-der.c
3 // utilities
4 //
5 // Created by Mitch Adler on 6/18/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/CFString.h>
16
17 #include "utilities_regressions.h"
18
19
20 #define kMaxResultSize 256
21
22 struct test_case {
23 CFStringRef str;
24 size_t size;
25 uint8_t res[kMaxResultSize];
26 };
27
28 static struct test_case test_cases[] = {
29 { .str = CFSTR("FOO"), .size = 5, .res = { 0x0C, 0x03, 0x46, 0x4F, 0x4F, } },
30 { .str = CFSTR("!ß∂ƒ˙圈ø¥®xzfff"), .size = 29, .res = { 0x0C, 0x1B, 0x21, 0xC3, 0x9F, 0xE2, 0x88, 0x82, 0xC6, 0x92,
31 0xCB, 0x99, 0xC3, 0xA5, 0xC5, 0x93, 0xCB, 0x86, 0xC3, 0xB8,
32 0xC2, 0xA5, 0xC2, 0xAE, 0x78, 0x7A, 0x66, 0x66, 0x66, } },
33 };
34
35
36 #define kTestsPerTestCase 8
37 static void one_test(const struct test_case * thisCase)
38 {
39 uint8_t buffer[4 * kMaxResultSize + 1];
40 uint8_t* buffer_end = buffer + sizeof(buffer);
41
42 uint8_t* encoded = der_encode_string(thisCase->str, NULL, buffer, buffer_end);
43
44 ok(encoded != NULL &&
45 (thisCase->size == (buffer_end - encoded)) &&
46 (memcmp(encoded, thisCase->res, thisCase->size) == 0));
47
48 CFStringRef decoded = NULL;
49
50 const uint8_t* decode_end = der_decode_string(NULL, kCFPropertyListMutableContainersAndLeaves,
51 &decoded, NULL, encoded, buffer_end);
52
53 ok(decode_end == buffer_end);
54 ok((decoded != NULL) && CFEqual(decoded, thisCase->str));
55
56 encoded = der_encode_plist(thisCase->str, NULL, buffer, buffer_end);
57
58 ok(encoded != NULL &&
59 (thisCase->size == (buffer_end - encoded)) &&
60 (memcmp(encoded, thisCase->res, thisCase->size) == 0));
61
62 CFTypeRef decoded_type = NULL;
63
64 decode_end = der_decode_plist(NULL, kCFPropertyListMutableContainersAndLeaves,
65 &decoded_type, NULL, encoded, buffer_end);
66
67 ok(decode_end == buffer_end);
68 ok((decoded_type != NULL) && CFEqual(decoded_type, thisCase->str));
69
70 ok(der_sizeof_string(thisCase->str, NULL) == thisCase->size);
71 ok(der_sizeof_plist(thisCase->str, NULL) == thisCase->size);
72
73 CFReleaseNull(decoded);
74 CFReleaseNull(decoded_type);
75 }
76
77 #define kTestCount (array_size(test_cases) * kTestsPerTestCase)
78 static void tests(void)
79 {
80 for (int testnumber = 0; testnumber < array_size(test_cases); ++testnumber)
81 one_test(test_cases + testnumber);
82 }
83
84 int su_10_cfstring_der(int argc, char *const *argv)
85 {
86 plan_tests(kTestCount);
87 tests();
88
89 return 0;
90 }