2 * Copyright (c) 2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #include "utilities/der_plist.h"
26 #include "utilities/der_plist_internal.h"
28 #include "utilities/SecCFRelease.h"
29 #include "utilities/array_size.h"
31 #include <CoreFoundation/CoreFoundation.h>
33 #include "utilities_regressions.h"
35 #define kMaxResultSize 30
43 static struct test_case test_cases
[] =
45 { .value
= 0, .encoded_size
= 3, .encoded
= { 0x02, 0x01, 0x00 }, },
46 { .value
= 1, .encoded_size
= 3, .encoded
= { 0x02, 0x01, 0x01 }, },
47 { .value
= 128, .encoded_size
= 4, .encoded
= { 0x02, 0x02, 0x00, 0x80 }, },
48 { .value
= -1, .encoded_size
= 3, .encoded
= { 0x02, 0x01, 0xFF }, },
49 { .value
= -129, .encoded_size
= 4, .encoded
= { 0x02, 0x02, 0xFF, 0x7F }, },
50 { .value
= 1000, .encoded_size
= 4, .encoded
= { 0x02, 0x02, 0x03, 0xE8, }, },
51 { .value
= 65280, .encoded_size
= 5, .encoded
= { 0x02, 0x03, 0x00, 0xFF, 0x00 }, },
52 { .value
= 41234576, .encoded_size
= 6, .encoded
= { 0x02, 0x04, 0x02, 0x75, 0x30, 0x90 }, },
53 { .value
= -412343576, .encoded_size
= 6, .encoded
= { 0x02, 0x04, 0xE7, 0x6C, 0x22, 0xE8 }, },
56 #define kTestsPerTestCase 8
57 static void one_test(const struct test_case
* thisCase
)
59 uint8_t buffer
[kMaxResultSize
];
60 uint8_t* buffer_end
= buffer
+ sizeof(buffer
);
62 CFNumberRef initialValue
= CFNumberCreate(NULL
, kCFNumberLongLongType
, &thisCase
->value
);
64 uint8_t* encoded
= der_encode_plist(initialValue
, NULL
, buffer
, buffer_end
);
67 (thisCase
->encoded_size
== (buffer_end
- encoded
)) &&
68 (memcmp(encoded
, thisCase
->encoded
, thisCase
->encoded_size
) == 0));
70 encoded
= der_encode_number(initialValue
, NULL
, buffer
, buffer_end
);
73 (thisCase
->encoded_size
== (buffer_end
- encoded
)) &&
74 (memcmp(encoded
, thisCase
->encoded
, thisCase
->encoded_size
) == 0));
77 printf(".size = %d, .res = { ", (buffer_end
- encoded
));
78 for(int c
= 0; c
< (buffer_end
- encoded
); ++c
)
79 printf("0x%02X, ", encoded
[c
]);
83 CFNumberRef decoded
= NULL
;
85 const uint8_t* decode_end
= der_decode_number(NULL
,
86 &decoded
, NULL
, encoded
, buffer_end
);
88 ok(decode_end
== buffer_end
, "didn't decode whole buffer");
89 ok((decoded
!= NULL
) && CFEqual(decoded
, initialValue
), "Didn't make equal value.");
91 CFPropertyListRef decoded_type
= NULL
;
93 decode_end
= der_decode_plist(NULL
,
94 &decoded_type
, NULL
, encoded
, buffer_end
);
96 ok(decode_end
== buffer_end
, "didn't decode whole buffer");
97 ok((decoded
!= NULL
) && CFEqual(decoded_type
, initialValue
), "Didn't make equal value.");
99 ok(der_sizeof_number(initialValue
, NULL
) == thisCase
->encoded_size
, "Size correct.");
100 ok(der_sizeof_plist(initialValue
, NULL
) == thisCase
->encoded_size
, "Size correct.");
102 CFReleaseNull(decoded
);
103 CFReleaseNull(decoded_type
);
106 #define kTestCount (array_size(test_cases) * kTestsPerTestCase)
107 static void tests(void)
109 for (int testnumber
= 0; testnumber
< array_size(test_cases
); ++testnumber
)
110 one_test(test_cases
+ testnumber
);
113 int su_13_cfnumber_der(int argc
, char *const *argv
)
115 plan_tests(kTestCount
);