2 // su-13-cfnumber-der.c
5 // Created by Mitch Adler on 6/20/12.
6 // Copyright (c) 2012 Apple Inc. All rights reserved.
9 #include "utilities/der_plist.h"
10 #include "utilities/der_plist_internal.h"
12 #include "utilities/SecCFRelease.h"
13 #include "utilities/array_size.h"
15 #include <CoreFoundation/CoreFoundation.h>
17 #include "utilities_regressions.h"
19 #define kMaxResultSize 30
27 static struct test_case test_cases
[] =
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 }, },
40 #define kTestsPerTestCase 8
41 static void one_test(const struct test_case
* thisCase
)
43 uint8_t buffer
[kMaxResultSize
];
44 uint8_t* buffer_end
= buffer
+ sizeof(buffer
);
46 CFNumberRef initialValue
= CFNumberCreate(NULL
, kCFNumberLongLongType
, &thisCase
->value
);
48 uint8_t* encoded
= der_encode_plist(initialValue
, NULL
, buffer
, buffer_end
);
51 (thisCase
->encoded_size
== (buffer_end
- encoded
)) &&
52 (memcmp(encoded
, thisCase
->encoded
, thisCase
->encoded_size
) == 0));
54 encoded
= der_encode_number(initialValue
, NULL
, buffer
, buffer_end
);
57 (thisCase
->encoded_size
== (buffer_end
- encoded
)) &&
58 (memcmp(encoded
, thisCase
->encoded
, thisCase
->encoded_size
) == 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
]);
67 CFNumberRef decoded
= NULL
;
69 const uint8_t* decode_end
= der_decode_number(NULL
, kCFPropertyListMutableContainersAndLeaves
,
70 &decoded
, NULL
, encoded
, buffer_end
);
72 ok(decode_end
== buffer_end
, "didn't decode whole buffer");
73 ok((decoded
!= NULL
) && CFEqual(decoded
, initialValue
), "Didn't make equal value.");
75 CFPropertyListRef decoded_type
= NULL
;
77 decode_end
= der_decode_plist(NULL
, kCFPropertyListMutableContainersAndLeaves
,
78 &decoded_type
, NULL
, encoded
, buffer_end
);
80 ok(decode_end
== buffer_end
, "didn't decode whole buffer");
81 ok((decoded
!= NULL
) && CFEqual(decoded_type
, initialValue
), "Didn't make equal value.");
83 ok(der_sizeof_number(initialValue
, NULL
) == thisCase
->encoded_size
, "Size correct.");
84 ok(der_sizeof_plist(initialValue
, NULL
) == thisCase
->encoded_size
, "Size correct.");
86 CFReleaseNull(decoded
);
87 CFReleaseNull(decoded_type
);
90 #define kTestCount (array_size(test_cases) * kTestsPerTestCase)
91 static void tests(void)
93 for (int testnumber
= 0; testnumber
< array_size(test_cases
); ++testnumber
)
94 one_test(test_cases
+ testnumber
);
97 int su_13_cfnumber_der(int argc
, char *const *argv
)
99 plan_tests(kTestCount
);