]> git.saurik.com Git - apple/security.git/blob - utilities/Regressions/su-12-cfboolean-der.c
Security-55471.tar.gz
[apple/security.git] / utilities / Regressions / su-12-cfboolean-der.c
1 //
2 // su-11-cfdata-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 16
20
21 #define kTestsPerTestCase 8
22 static void one_test(CFBooleanRef value, size_t der_size, const uint8_t *expected_der)
23 {
24
25 uint8_t buffer[kMaxResultSize];
26 uint8_t *buffer_end = buffer + sizeof(buffer);
27
28 uint8_t* encoded = der_encode_plist(value, NULL, buffer, buffer_end);
29
30 ok(encoded != NULL &&
31 (der_size == (buffer_end - encoded)) &&
32 (memcmp(encoded, expected_der, der_size) == 0));
33
34 encoded = der_encode_boolean(value, NULL, buffer, buffer_end);
35
36 ok(encoded != NULL &&
37 (der_size == (buffer_end - encoded)) &&
38 (memcmp(encoded, expected_der, der_size) == 0));
39
40 #if 0
41 printf(".size = %d, .res = { ", (buffer_end - encoded));
42 for(int c = 0; c < (buffer_end - encoded); ++c)
43 printf("0x%02X, ", encoded[c]);
44 printf("},\n");
45 #endif
46
47 CFBooleanRef decoded = NULL;
48 const uint8_t* decode_end = der_decode_boolean(NULL, kCFPropertyListMutableContainersAndLeaves,
49 &decoded, NULL, encoded, buffer_end);
50
51 ok(decode_end == buffer_end);
52 ok((decoded != NULL) && CFEqual(decoded, value));
53
54 CFPropertyListRef decoded_type = NULL;
55 decode_end = der_decode_plist(NULL, kCFPropertyListMutableContainersAndLeaves,
56 &decoded_type, NULL, encoded, buffer_end);
57
58 ok(decode_end == buffer_end);
59 ok((decoded != NULL) && CFEqual(decoded_type, value));
60
61 ok(der_sizeof_boolean(value, NULL) == der_size);
62 ok(der_sizeof_plist(value, NULL) == der_size);
63
64 CFReleaseNull(decoded);
65 CFReleaseNull(decoded_type);
66 }
67
68 #define kTestCount (2 * kTestsPerTestCase)
69 static void tests(void)
70 {
71 const uint8_t der_true[] = { 0x01, 0x01, 0x01 };
72
73 one_test(kCFBooleanTrue, sizeof(der_true), der_true);
74
75 const uint8_t der_false[] = { 0x01, 0x01, 0x00 };
76
77 one_test(kCFBooleanFalse, sizeof(der_false), der_false);
78 }
79
80 int su_12_cfboolean_der(int argc, char *const *argv)
81 {
82 plan_tests(kTestCount);
83 tests();
84
85 return 0;
86 }