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 16
36
37#define kTestsPerTestCase 8
38static void one_test(CFBooleanRef value, size_t der_size, const uint8_t *expected_der)
39{
40
41    uint8_t buffer[kMaxResultSize];
42    uint8_t *buffer_end = buffer + sizeof(buffer);
43
44    uint8_t* encoded = der_encode_plist(value, NULL, buffer, buffer_end);
45
46    ok(encoded != NULL &&
47       (der_size == (buffer_end - encoded)) &&
48       (memcmp(encoded, expected_der, der_size) == 0));
49
50    encoded = der_encode_boolean(value, NULL, buffer, buffer_end);
51
52    ok(encoded != NULL &&
53       (der_size == (buffer_end - encoded)) &&
54       (memcmp(encoded, expected_der, der_size) == 0));
55
56#if 0
57    printf(".size = %d, .res = { ", (buffer_end - encoded));
58    for(int c = 0; c < (buffer_end - encoded); ++c)
59        printf("0x%02X, ", encoded[c]);
60    printf("},\n");
61#endif
62
63    CFBooleanRef decoded = NULL;
64    const uint8_t* decode_end = der_decode_boolean(NULL, kCFPropertyListMutableContainersAndLeaves,
65                                                   &decoded, NULL, encoded, buffer_end);
66
67    ok(decode_end == buffer_end);
68    ok((decoded != NULL) && CFEqual(decoded, value));
69
70    CFPropertyListRef decoded_type = NULL;
71    decode_end = der_decode_plist(NULL, kCFPropertyListMutableContainersAndLeaves,
72                                  &decoded_type, NULL, encoded, buffer_end);
73
74    ok(decode_end == buffer_end);
75    ok((decoded != NULL) && CFEqual(decoded_type, value));
76
77    ok(der_sizeof_boolean(value, NULL) == der_size);
78    ok(der_sizeof_plist(value, NULL) == der_size);
79
80    CFReleaseNull(decoded);
81    CFReleaseNull(decoded_type);
82}
83
84#define kTestCount (2 * kTestsPerTestCase)
85static void tests(void)
86{
87    const uint8_t der_true[] = { 0x01, 0x01, 0x01 };
88
89    one_test(kCFBooleanTrue, sizeof(der_true), der_true);
90
91    const uint8_t der_false[] = { 0x01, 0x01, 0x00 };
92
93    one_test(kCFBooleanFalse, sizeof(der_false), der_false);
94}
95
96int su_12_cfboolean_der(int argc, char *const *argv)
97{
98    plan_tests(kTestCount);
99    tests();
100
101    return 0;
102}
103