1/*
2 * Copyright (c) 2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25#include "utilities/der_plist.h"
26#include "utilities/der_plist_internal.h"
27
28#include "utilities/SecCFRelease.h"
29#include "utilities/array_size.h"
30
31#include <CoreFoundation/CoreFoundation.h>
32
33#include "utilities_regressions.h"
34
35#define kMaxResultSize 30
36
37struct test_case {
38    long long value;
39    size_t    encoded_size;
40    uint8_t   encoded[20];
41};
42
43static struct test_case test_cases[] =
44{
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 }, },
54};
55
56#define kTestsPerTestCase 8
57static void one_test(const struct test_case * thisCase)
58{
59    uint8_t buffer[kMaxResultSize];
60    uint8_t* buffer_end = buffer + sizeof(buffer);
61
62    CFNumberRef initialValue = CFNumberCreate(NULL, kCFNumberLongLongType, &thisCase->value);
63
64    uint8_t* encoded = der_encode_plist(initialValue, NULL, buffer, buffer_end);
65
66    ok(encoded != NULL &&
67       (thisCase->encoded_size == (buffer_end - encoded)) &&
68       (memcmp(encoded, thisCase->encoded, thisCase->encoded_size) == 0));
69
70    encoded = der_encode_number(initialValue, NULL, buffer, buffer_end);
71
72    ok(encoded != NULL &&
73       (thisCase->encoded_size == (buffer_end - encoded)) &&
74       (memcmp(encoded, thisCase->encoded, thisCase->encoded_size) == 0));
75
76#if 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]);
80    printf("},\n");
81#endif
82
83    CFNumberRef decoded = NULL;
84
85    const uint8_t* decode_end = der_decode_number(NULL, kCFPropertyListMutableContainersAndLeaves,
86                                                  &decoded, NULL, encoded, buffer_end);
87
88    ok(decode_end == buffer_end, "didn't decode whole buffer");
89    ok((decoded != NULL) && CFEqual(decoded, initialValue), "Didn't make equal value.");
90
91    CFPropertyListRef decoded_type = NULL;
92
93    decode_end = der_decode_plist(NULL, kCFPropertyListMutableContainersAndLeaves,
94                                  &decoded_type, NULL, encoded, buffer_end);
95
96    ok(decode_end == buffer_end, "didn't decode whole buffer");
97    ok((decoded != NULL) && CFEqual(decoded_type, initialValue), "Didn't make equal value.");
98
99    ok(der_sizeof_number(initialValue, NULL) == thisCase->encoded_size, "Size correct.");
100    ok(der_sizeof_plist(initialValue, NULL) == thisCase->encoded_size, "Size correct.");
101
102    CFReleaseNull(decoded);
103    CFReleaseNull(decoded_type);
104}
105
106#define kTestCount (array_size(test_cases) * kTestsPerTestCase)
107static void tests(void)
108{
109    for (int testnumber = 0; testnumber < array_size(test_cases); ++testnumber)
110        one_test(test_cases + testnumber);
111}
112
113int su_13_cfnumber_der(int argc, char *const *argv)
114{
115    plan_tests(kTestCount);
116    tests();
117
118    return 0;
119}
120