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/CFString.h>
33 #include "utilities_regressions.h"
36 #define kMaxResultSize 256
41 uint8_t res
[kMaxResultSize
];
44 static struct test_case test_cases
[] = {
45 { .str
= CFSTR("FOO"), .size
= 5, .res
= { 0x0C, 0x03, 0x46, 0x4F, 0x4F, } },
46 { .str
= CFSTR("!ß∂ƒ˙圈ø¥®xzfff"), .size
= 29, .res
= { 0x0C, 0x1B, 0x21, 0xC3, 0x9F, 0xE2, 0x88, 0x82, 0xC6, 0x92,
47 0xCB, 0x99, 0xC3, 0xA5, 0xC5, 0x93, 0xCB, 0x86, 0xC3, 0xB8,
48 0xC2, 0xA5, 0xC2, 0xAE, 0x78, 0x7A, 0x66, 0x66, 0x66, } },
52 #define kTestsPerTestCase 8
53 static void one_test(const struct test_case
* thisCase
)
55 uint8_t buffer
[4 * kMaxResultSize
+ 1];
56 uint8_t* buffer_end
= buffer
+ sizeof(buffer
);
58 uint8_t* encoded
= der_encode_string(thisCase
->str
, NULL
, buffer
, buffer_end
);
61 (thisCase
->size
== (buffer_end
- encoded
)) &&
62 (memcmp(encoded
, thisCase
->res
, thisCase
->size
) == 0));
64 CFStringRef decoded
= NULL
;
66 const uint8_t* decode_end
= der_decode_string(NULL
, kCFPropertyListMutableContainersAndLeaves
,
67 &decoded
, NULL
, encoded
, buffer_end
);
69 ok(decode_end
== buffer_end
);
70 ok((decoded
!= NULL
) && CFEqual(decoded
, thisCase
->str
));
72 encoded
= der_encode_plist(thisCase
->str
, NULL
, buffer
, buffer_end
);
75 (thisCase
->size
== (buffer_end
- encoded
)) &&
76 (memcmp(encoded
, thisCase
->res
, thisCase
->size
) == 0));
78 CFTypeRef decoded_type
= NULL
;
80 decode_end
= der_decode_plist(NULL
, kCFPropertyListMutableContainersAndLeaves
,
81 &decoded_type
, NULL
, encoded
, buffer_end
);
83 ok(decode_end
== buffer_end
);
84 ok((decoded_type
!= NULL
) && CFEqual(decoded_type
, thisCase
->str
));
86 ok(der_sizeof_string(thisCase
->str
, NULL
) == thisCase
->size
);
87 ok(der_sizeof_plist(thisCase
->str
, NULL
) == thisCase
->size
);
89 CFReleaseNull(decoded
);
90 CFReleaseNull(decoded_type
);
93 #define kTestCount (array_size(test_cases) * kTestsPerTestCase)
94 static void tests(void)
96 for (int testnumber
= 0; testnumber
< array_size(test_cases
); ++testnumber
)
97 one_test(test_cases
+ testnumber
);
100 int su_10_cfstring_der(int argc
, char *const *argv
)
102 plan_tests(kTestCount
);