]> git.saurik.com Git - apple/security.git/blame - OSX/utilities/Regressions/su-17-cfset-der.c
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / utilities / Regressions / su-17-cfset-der.c
CommitLineData
5c19dc3a
A
1//
2// su-17-cfset-der.c
3// utilities
4//
5// Created by Richard Murphy on 1/26/15.
6// Copyright © 2015 Apple Inc. All rights reserved.
7//
d8f41ccd 8/*
5c19dc3a 9 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
d8f41ccd
A
10 *
11 * @APPLE_LICENSE_HEADER_START@
5c19dc3a 12 *
d8f41ccd
A
13 * This file contains Original Code and/or Modifications of Original Code
14 * as defined in and that are subject to the Apple Public Source License
15 * Version 2.0 (the 'License'). You may not use this file except in
16 * compliance with the License. Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
5c19dc3a 19 *
d8f41ccd
A
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
5c19dc3a 27 *
d8f41ccd
A
28 * @APPLE_LICENSE_HEADER_END@
29 */
30
427c49bc 31
5c19dc3a
A
32#include "utilities_regressions.h"
33
427c49bc
A
34#include "utilities/der_plist.h"
35#include "utilities/der_plist_internal.h"
36
37#include "utilities/SecCFRelease.h"
38#include "utilities/array_size.h"
39
40#include <CoreFoundation/CoreFoundation.h>
41
427c49bc
A
42
43#define kMaxResultSize 1024
44
45struct test_case {
5c19dc3a 46 CFTypeRef values[10];
427c49bc 47 size_t encoded_size;
5c19dc3a 48 uint8_t encoded[256];
427c49bc
A
49};
50
51static struct test_case test_cases[] =
52{
5c19dc3a
A
53 {
54 .values = { CFSTR("AValue"), },
55 .encoded_size = 10, .encoded = { 0xD1, 0x08, 0x0C, 0x06, 0x41, 0x56, 0x61, 0x6C, 0x75, 0x65, },
56 },{
57 .values = { CFSTR("AValue"), CFSTR("BValue"), CFSTR("CValue"), CFSTR("DValue"), CFSTR("EValue"), CFSTR("FValue"), CFSTR("GValue"), CFSTR("HValue"), },
58 .encoded_size = 66, .encoded = { 0xD1, 0x40, 0x0C, 0x06, 0x41, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x42, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x43, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x44, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x45, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x46, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x47, 0x56, 0x61, 0x6C, 0x75, 0x65, 0x0C, 0x06, 0x48, 0x56, 0x61, 0x6C, 0x75, 0x65, },
59 },
427c49bc
A
60};
61
5c19dc3a
A
62#define kTestsPerSetTest 8
63static void test_set(CFSetRef testValue, size_t expected_size, const uint8_t* expected_data)
427c49bc 64{
427c49bc
A
65 uint8_t buffer[kMaxResultSize];
66 uint8_t* buffer_end = buffer + sizeof(buffer);
67
68 uint8_t* encoded = der_encode_plist(testValue, NULL, buffer, buffer_end);
5c19dc3a 69
427c49bc 70 ok(encoded != NULL &&
5c19dc3a
A
71 (expected_size == (buffer_end - encoded)) &&
72 (memcmp(encoded, expected_data, expected_size) == 0));
73
74 encoded = der_encode_set(testValue, NULL, buffer, buffer_end);
75
427c49bc 76 ok(encoded != NULL &&
5c19dc3a
A
77 (expected_size == (buffer_end - encoded)) &&
78 (memcmp(encoded, expected_data, expected_size) == 0));
79
427c49bc 80#if 0
5c19dc3a 81 printf(".encoded_size = %d, .encoded = { ", (int) (buffer_end - encoded));
427c49bc
A
82 for(int c = 0; c < (buffer_end - encoded); ++c)
83 printf("0x%02X, ", encoded[c]);
84 printf("},\n");
85#endif
427c49bc 86
5c19dc3a
A
87 CFSetRef decoded = NULL;
88
89 const uint8_t* decode_end = der_decode_set(NULL, kCFPropertyListMutableContainersAndLeaves,
90 &decoded, NULL, encoded, buffer_end);
427c49bc
A
91
92 ok(decode_end == buffer_end, "didn't decode whole buffer");
93 ok((decoded != NULL) && CFEqual(decoded, testValue), "Didn't make equal value.");
5c19dc3a
A
94
95 CFPropertyListRef decoded_type = NULL;
96
427c49bc
A
97 decode_end = der_decode_plist(NULL, kCFPropertyListMutableContainersAndLeaves,
98 &decoded_type, NULL, encoded, buffer_end);
5c19dc3a 99
427c49bc
A
100 ok(decode_end == buffer_end, "didn't decode whole buffer");
101 ok((decoded != NULL) && CFEqual(decoded_type, testValue), "Didn't make equal value.");
5c19dc3a
A
102
103 ok(der_sizeof_set(testValue, NULL) == expected_size, "Size correct.");
104 ok(der_sizeof_plist(testValue, NULL) == expected_size, "Size correct.");
105
427c49bc
A
106 CFReleaseNull(decoded);
107 CFReleaseNull(decoded_type);
5c19dc3a
A
108}
109
110
111#define kTestsPerTestCase (kTestsPerSetTest)
112static void one_test(const struct test_case * thisCase)
113{
114 CFIndex count = 0;
115 while (count < array_size(thisCase->values) && thisCase->values[count] != NULL)
116 ++count;
117
118 CFSetRef testValue = CFSetCreate(NULL, (const void**)thisCase->values, count, &kCFTypeSetCallBacks);
119
120 test_set(testValue, thisCase->encoded_size, thisCase->encoded);
427c49bc
A
121
122 CFReleaseNull(testValue);
123}
124
125#define kTestCount (array_size(test_cases) * kTestsPerTestCase)
126static void tests(void)
127{
128 for (int testnumber = 0; testnumber < array_size(test_cases); ++testnumber)
5c19dc3a 129 one_test(&test_cases[testnumber]);
427c49bc
A
130}
131
5c19dc3a 132int su_17_cfset_der(int argc, char *const *argv)
427c49bc
A
133{
134 plan_tests(kTestCount);
135 tests();
5c19dc3a 136
427c49bc
A
137 return 0;
138}