xref: /freebsd/contrib/libcbor/test/unicode_test.c (revision 3078531de10dcae44b253a35125c949ff4235284)
1 /*
2  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #include <setjmp.h>
9 #include <stdarg.h>
10 #include <stddef.h>
11 
12 #include <cmocka.h>
13 
14 #include "../src/cbor/internal/unicode.h"
15 
16 struct _cbor_unicode_status status;
17 
18 unsigned char missing_bytes_data[] = {0xC4, 0x8C};
19 
20 /* Capital accented C */
21 static void test_missing_bytes(void **state) {
22   _cbor_unicode_codepoint_count(missing_bytes_data, 1, &status);
23   assert_true(status.status == _CBOR_UNICODE_BADCP);
24   _cbor_unicode_codepoint_count(missing_bytes_data, 2, &status);
25   assert_true(status.status == _CBOR_UNICODE_OK);
26 }
27 
28 unsigned char invalid_sequence_data[] = {0x65, 0xC4, 0x00};
29 
30 /* e, invalid seq */
31 static void test_invalid_sequence(void **state) {
32   _cbor_unicode_codepoint_count(invalid_sequence_data, 3, &status);
33   assert_true(status.status == _CBOR_UNICODE_BADCP);
34   assert_true(status.location == 2);
35 }
36 
37 int main(void) {
38   const struct CMUnitTest tests[] = {cmocka_unit_test(test_missing_bytes),
39                                      cmocka_unit_test(test_invalid_sequence)};
40   return cmocka_run_group_tests(tests, NULL, NULL);
41 }
42