5 // Created by Mitch Adler on 6/20/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/CoreFoundation.h>
17 #include "utilities_regressions.h"
19 #define kMaxResultSize 1024
22 CFTypeRef elements
[10];
27 static struct test_case test_cases
[] =
29 { .elements
= { CFSTR("First") },
30 .encoded_size
= 9, .encoded
= { 0x30, 0x07, 0x0C, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, }, },
31 { .elements
= { CFSTR("First"), CFSTR("SECOND") },
32 .encoded_size
= 17, .encoded
= { 0x30, 0x0F, 0x0C, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, 0x0C, 0x06, 0x53, 0x45, 0x43, 0x4F, 0x4E, 0x44, }, },
34 .encoded_size
= 2, .encoded
= { 0x30, 0x00, } },
37 #define kTestsPerTestCase 8
38 static void one_test(const struct test_case
* thisCase
)
40 CFIndex element_count
= 0;
41 while (element_count
< array_size(thisCase
->elements
) && thisCase
->elements
[element_count
] != NULL
)
44 CFArrayRef testValue
= CFArrayCreate(NULL
, (const void **)&thisCase
->elements
, element_count
, &kCFTypeArrayCallBacks
);
46 uint8_t buffer
[kMaxResultSize
];
47 uint8_t* buffer_end
= buffer
+ sizeof(buffer
);
49 uint8_t* encoded
= der_encode_plist(testValue
, NULL
, buffer
, buffer_end
);
52 (thisCase
->encoded_size
== (buffer_end
- encoded
)) &&
53 (memcmp(encoded
, thisCase
->encoded
, thisCase
->encoded_size
) == 0));
55 encoded
= der_encode_array(testValue
, NULL
, buffer
, buffer_end
);
58 (thisCase
->encoded_size
== (buffer_end
- encoded
)) &&
59 (memcmp(encoded
, thisCase
->encoded
, thisCase
->encoded_size
) == 0));
62 printf(".encoded_size = %d, .encoded = { ", (buffer_end
- encoded
));
63 for(int c
= 0; c
< (buffer_end
- encoded
); ++c
)
64 printf("0x%02X, ", encoded
[c
]);
68 CFArrayRef decoded
= NULL
;
70 const uint8_t* decode_end
= der_decode_array(NULL
, kCFPropertyListMutableContainersAndLeaves
,
71 &decoded
, NULL
, encoded
, buffer_end
);
73 ok(decode_end
== buffer_end
, "didn't decode whole buffer");
74 ok((decoded
!= NULL
) && CFEqual(decoded
, testValue
), "Didn't make equal value.");
76 CFTypeRef decoded_type
= NULL
;
78 decode_end
= der_decode_plist(NULL
, kCFPropertyListMutableContainersAndLeaves
,
79 &decoded_type
, NULL
, encoded
, buffer_end
);
81 ok(decode_end
== buffer_end
, "didn't decode whole buffer");
82 ok((decoded
!= NULL
) && CFEqual(decoded_type
, testValue
), "Didn't make equal value.");
84 ok(der_sizeof_array(testValue
, NULL
) == thisCase
->encoded_size
, "Size correct.");
85 ok(der_sizeof_plist(testValue
, NULL
) == thisCase
->encoded_size
, "Size correct.");
87 CFReleaseNull(decoded
);
88 CFReleaseNull(decoded_type
);
90 CFReleaseNull(testValue
);
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_14_cfarray_der(int argc
, char *const *argv
)
102 plan_tests(kTestCount
);