]>
git.saurik.com Git - apple/security.git/blob - OSX/utilities/Regressions/su-14-cfarray-der.c
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/CoreFoundation.h>
33 #include "utilities_regressions.h"
35 #define kMaxResultSize 1024
38 CFTypeRef elements
[10];
43 static struct test_case test_cases
[] =
45 { .elements
= { CFSTR("First") },
46 .encoded_size
= 9, .encoded
= { 0x30, 0x07, 0x0C, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, }, },
47 { .elements
= { CFSTR("First"), CFSTR("SECOND") },
48 .encoded_size
= 17, .encoded
= { 0x30, 0x0F, 0x0C, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, 0x0C, 0x06, 0x53, 0x45, 0x43, 0x4F, 0x4E, 0x44, }, },
50 .encoded_size
= 2, .encoded
= { 0x30, 0x00, } },
53 #define kTestsPerTestCase 8
54 static void one_test(const struct test_case
* thisCase
)
56 CFIndex element_count
= 0;
57 while (element_count
< array_size(thisCase
->elements
) && thisCase
->elements
[element_count
] != NULL
)
60 CFArrayRef testValue
= CFArrayCreate(NULL
, (const void **)&thisCase
->elements
, element_count
, &kCFTypeArrayCallBacks
);
62 uint8_t buffer
[kMaxResultSize
];
63 uint8_t* buffer_end
= buffer
+ sizeof(buffer
);
65 uint8_t* encoded
= der_encode_plist(testValue
, NULL
, buffer
, buffer_end
);
68 (thisCase
->encoded_size
== (buffer_end
- encoded
)) &&
69 (memcmp(encoded
, thisCase
->encoded
, thisCase
->encoded_size
) == 0));
71 encoded
= der_encode_array(testValue
, NULL
, buffer
, buffer_end
);
74 (thisCase
->encoded_size
== (buffer_end
- encoded
)) &&
75 (memcmp(encoded
, thisCase
->encoded
, thisCase
->encoded_size
) == 0));
78 printf(".encoded_size = %d, .encoded = { ", (buffer_end
- encoded
));
79 for(int c
= 0; c
< (buffer_end
- encoded
); ++c
)
80 printf("0x%02X, ", encoded
[c
]);
84 CFArrayRef decoded
= NULL
;
86 const uint8_t* decode_end
= der_decode_array(NULL
,
87 &decoded
, NULL
, encoded
, buffer_end
);
89 ok(decode_end
== buffer_end
, "didn't decode whole buffer");
90 ok((decoded
!= NULL
) && CFEqual(decoded
, testValue
), "Didn't make equal value.");
92 CFTypeRef decoded_type
= NULL
;
94 decode_end
= der_decode_plist(NULL
,
95 &decoded_type
, NULL
, encoded
, buffer_end
);
97 ok(decode_end
== buffer_end
, "didn't decode whole buffer");
98 ok((decoded
!= NULL
) && CFEqual(decoded_type
, testValue
), "Didn't make equal value.");
100 ok(der_sizeof_array(testValue
, NULL
) == thisCase
->encoded_size
, "Size correct.");
101 ok(der_sizeof_plist(testValue
, NULL
) == thisCase
->encoded_size
, "Size correct.");
103 CFReleaseNull(decoded
);
104 CFReleaseNull(decoded_type
);
106 CFReleaseNull(testValue
);
109 #define kTestCount (array_size(test_cases) * kTestsPerTestCase)
110 static void tests(void)
112 for (int testnumber
= 0; testnumber
< array_size(test_cases
); ++testnumber
)
113 one_test(test_cases
+ testnumber
);
116 int su_14_cfarray_der(int argc
, char *const *argv
)
118 plan_tests(kTestCount
);