5 // Created by Richard Murphy on 1/26/15.
6 // Copyright © 2015 Apple Inc. All rights reserved.
9 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
11 * @APPLE_LICENSE_HEADER_START@
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
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.
28 * @APPLE_LICENSE_HEADER_END@
32 #include "utilities_regressions.h"
34 #include "utilities/der_plist.h"
35 #include "utilities/der_plist_internal.h"
37 #include "utilities/SecCFRelease.h"
38 #include "utilities/array_size.h"
40 #include <CoreFoundation/CoreFoundation.h>
43 #define kMaxResultSize 1024
51 static struct test_case test_cases
[] =
54 .values
= { CFSTR("AValue"), },
55 .encoded_size
= 10, .encoded
= { 0xD1, 0x08, 0x0C, 0x06, 0x41, 0x56, 0x61, 0x6C, 0x75, 0x65, },
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, },
62 #define kTestsPerSetTest 8
63 static void test_set(CFSetRef testValue
, size_t expected_size
, const uint8_t* expected_data
)
65 uint8_t buffer
[kMaxResultSize
];
66 uint8_t* buffer_end
= buffer
+ sizeof(buffer
);
68 uint8_t* encoded
= der_encode_plist(testValue
, NULL
, buffer
, buffer_end
);
71 (expected_size
== (buffer_end
- encoded
)) &&
72 (memcmp(encoded
, expected_data
, expected_size
) == 0));
74 encoded
= der_encode_set(testValue
, NULL
, buffer
, buffer_end
);
77 (expected_size
== (buffer_end
- encoded
)) &&
78 (memcmp(encoded
, expected_data
, expected_size
) == 0));
81 printf(".encoded_size = %d, .encoded = { ", (int) (buffer_end
- encoded
));
82 for(int c
= 0; c
< (buffer_end
- encoded
); ++c
)
83 printf("0x%02X, ", encoded
[c
]);
87 CFSetRef decoded
= NULL
;
89 const uint8_t* decode_end
= der_decode_set(NULL
,
90 &decoded
, NULL
, encoded
, buffer_end
);
92 ok(decode_end
== buffer_end
, "didn't decode whole buffer");
93 ok((decoded
!= NULL
) && CFEqual(decoded
, testValue
), "Didn't make equal value.");
95 CFPropertyListRef decoded_type
= NULL
;
97 decode_end
= der_decode_plist(NULL
,
98 &decoded_type
, NULL
, encoded
, buffer_end
);
100 ok(decode_end
== buffer_end
, "didn't decode whole buffer");
101 ok((decoded
!= NULL
) && CFEqual(decoded_type
, testValue
), "Didn't make equal value.");
103 ok(der_sizeof_set(testValue
, NULL
) == expected_size
, "Size correct.");
104 ok(der_sizeof_plist(testValue
, NULL
) == expected_size
, "Size correct.");
106 CFReleaseNull(decoded
);
107 CFReleaseNull(decoded_type
);
111 #define kTestsPerTestCase (kTestsPerSetTest)
112 static void one_test(const struct test_case
* thisCase
)
115 while (count
< array_size(thisCase
->values
) && thisCase
->values
[count
] != NULL
)
118 CFSetRef testValue
= CFSetCreate(NULL
, (const void**)thisCase
->values
, count
, &kCFTypeSetCallBacks
);
120 test_set(testValue
, thisCase
->encoded_size
, thisCase
->encoded
);
122 CFReleaseNull(testValue
);
125 #define kTestCount (array_size(test_cases) * kTestsPerTestCase)
126 static void tests(void)
128 for (int testnumber
= 0; testnumber
< array_size(test_cases
); ++testnumber
)
129 one_test(&test_cases
[testnumber
]);
132 int su_17_cfset_der(int argc
, char *const *argv
)
134 plan_tests(kTestCount
);