]> git.saurik.com Git - apple/security.git/blob - utilities/Regressions/su-13-cfnumber-der.c
Security-55471.tar.gz
[apple/security.git] / utilities / Regressions / su-13-cfnumber-der.c
1 //
2 // su-13-cfnumber-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 30
20
21 struct test_case {
22 long long value;
23 size_t encoded_size;
24 uint8_t encoded[20];
25 };
26
27 static struct test_case test_cases[] =
28 {
29 { .value = 0, .encoded_size = 3, .encoded = { 0x02, 0x01, 0x00 }, },
30 { .value = 1, .encoded_size = 3, .encoded = { 0x02, 0x01, 0x01 }, },
31 { .value = 128, .encoded_size = 4, .encoded = { 0x02, 0x02, 0x00, 0x80 }, },
32 { .value = -1, .encoded_size = 3, .encoded = { 0x02, 0x01, 0xFF }, },
33 { .value = -129, .encoded_size = 4, .encoded = { 0x02, 0x02, 0xFF, 0x7F }, },
34 { .value = 1000, .encoded_size = 4, .encoded = { 0x02, 0x02, 0x03, 0xE8, }, },
35 { .value = 65280, .encoded_size = 5, .encoded = { 0x02, 0x03, 0x00, 0xFF, 0x00 }, },
36 { .value = 41234576, .encoded_size = 6, .encoded = { 0x02, 0x04, 0x02, 0x75, 0x30, 0x90 }, },
37 { .value = -412343576, .encoded_size = 6, .encoded = { 0x02, 0x04, 0xE7, 0x6C, 0x22, 0xE8 }, },
38 };
39
40 #define kTestsPerTestCase 8
41 static void one_test(const struct test_case * thisCase)
42 {
43 uint8_t buffer[kMaxResultSize];
44 uint8_t* buffer_end = buffer + sizeof(buffer);
45
46 CFNumberRef initialValue = CFNumberCreate(NULL, kCFNumberLongLongType, &thisCase->value);
47
48 uint8_t* encoded = der_encode_plist(initialValue, NULL, buffer, buffer_end);
49
50 ok(encoded != NULL &&
51 (thisCase->encoded_size == (buffer_end - encoded)) &&
52 (memcmp(encoded, thisCase->encoded, thisCase->encoded_size) == 0));
53
54 encoded = der_encode_number(initialValue, NULL, buffer, buffer_end);
55
56 ok(encoded != NULL &&
57 (thisCase->encoded_size == (buffer_end - encoded)) &&
58 (memcmp(encoded, thisCase->encoded, thisCase->encoded_size) == 0));
59
60 #if 0
61 printf(".size = %d, .res = { ", (buffer_end - encoded));
62 for(int c = 0; c < (buffer_end - encoded); ++c)
63 printf("0x%02X, ", encoded[c]);
64 printf("},\n");
65 #endif
66
67 CFNumberRef decoded = NULL;
68
69 const uint8_t* decode_end = der_decode_number(NULL, kCFPropertyListMutableContainersAndLeaves,
70 &decoded, NULL, encoded, buffer_end);
71
72 ok(decode_end == buffer_end, "didn't decode whole buffer");
73 ok((decoded != NULL) && CFEqual(decoded, initialValue), "Didn't make equal value.");
74
75 CFPropertyListRef decoded_type = NULL;
76
77 decode_end = der_decode_plist(NULL, kCFPropertyListMutableContainersAndLeaves,
78 &decoded_type, NULL, encoded, buffer_end);
79
80 ok(decode_end == buffer_end, "didn't decode whole buffer");
81 ok((decoded != NULL) && CFEqual(decoded_type, initialValue), "Didn't make equal value.");
82
83 ok(der_sizeof_number(initialValue, NULL) == thisCase->encoded_size, "Size correct.");
84 ok(der_sizeof_plist(initialValue, NULL) == thisCase->encoded_size, "Size correct.");
85
86 CFReleaseNull(decoded);
87 CFReleaseNull(decoded_type);
88 }
89
90 #define kTestCount (array_size(test_cases) * kTestsPerTestCase)
91 static void tests(void)
92 {
93 for (int testnumber = 0; testnumber < array_size(test_cases); ++testnumber)
94 one_test(test_cases + testnumber);
95 }
96
97 int su_13_cfnumber_der(int argc, char *const *argv)
98 {
99 plan_tests(kTestCount);
100 tests();
101
102 return 0;
103 }