2 // su-10-cfstring-der.c
5 // Created by Mitch Adler on 6/18/12.
6 // Copyright (c) 2012 Apple Inc. All rights reserved.
9 #include "utilities/der_plist.h"
10 #include "utilities/der_plist_internal.h"
12 #include "utilities/SecCFRelease.h"
13 #include "utilities/array_size.h"
15 #include <CoreFoundation/CFString.h>
17 #include "utilities_regressions.h"
20 #define kMaxResultSize 256
25 uint8_t res
[kMaxResultSize
];
28 static struct test_case test_cases
[] = {
29 { .str
= CFSTR("FOO"), .size
= 5, .res
= { 0x0C, 0x03, 0x46, 0x4F, 0x4F, } },
30 { .str
= CFSTR("!ß∂ƒ˙圈ø¥®xzfff"), .size
= 29, .res
= { 0x0C, 0x1B, 0x21, 0xC3, 0x9F, 0xE2, 0x88, 0x82, 0xC6, 0x92,
31 0xCB, 0x99, 0xC3, 0xA5, 0xC5, 0x93, 0xCB, 0x86, 0xC3, 0xB8,
32 0xC2, 0xA5, 0xC2, 0xAE, 0x78, 0x7A, 0x66, 0x66, 0x66, } },
36 #define kTestsPerTestCase 8
37 static void one_test(const struct test_case
* thisCase
)
39 uint8_t buffer
[4 * kMaxResultSize
+ 1];
40 uint8_t* buffer_end
= buffer
+ sizeof(buffer
);
42 uint8_t* encoded
= der_encode_string(thisCase
->str
, NULL
, buffer
, buffer_end
);
45 (thisCase
->size
== (buffer_end
- encoded
)) &&
46 (memcmp(encoded
, thisCase
->res
, thisCase
->size
) == 0));
48 CFStringRef decoded
= NULL
;
50 const uint8_t* decode_end
= der_decode_string(NULL
, kCFPropertyListMutableContainersAndLeaves
,
51 &decoded
, NULL
, encoded
, buffer_end
);
53 ok(decode_end
== buffer_end
);
54 ok((decoded
!= NULL
) && CFEqual(decoded
, thisCase
->str
));
56 encoded
= der_encode_plist(thisCase
->str
, NULL
, buffer
, buffer_end
);
59 (thisCase
->size
== (buffer_end
- encoded
)) &&
60 (memcmp(encoded
, thisCase
->res
, thisCase
->size
) == 0));
62 CFTypeRef decoded_type
= NULL
;
64 decode_end
= der_decode_plist(NULL
, kCFPropertyListMutableContainersAndLeaves
,
65 &decoded_type
, NULL
, encoded
, buffer_end
);
67 ok(decode_end
== buffer_end
);
68 ok((decoded_type
!= NULL
) && CFEqual(decoded_type
, thisCase
->str
));
70 ok(der_sizeof_string(thisCase
->str
, NULL
) == thisCase
->size
);
71 ok(der_sizeof_plist(thisCase
->str
, NULL
) == thisCase
->size
);
73 CFReleaseNull(decoded
);
74 CFReleaseNull(decoded_type
);
77 #define kTestCount (array_size(test_cases) * kTestsPerTestCase)
78 static void tests(void)
80 for (int testnumber
= 0; testnumber
< array_size(test_cases
); ++testnumber
)
81 one_test(test_cases
+ testnumber
);
84 int su_10_cfstring_der(int argc
, char *const *argv
)
86 plan_tests(kTestCount
);