xref: /freebsd/contrib/libcbor/test/string_encoders_test.c (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
1*5d3e7166SEd Maste /*
2*5d3e7166SEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3*5d3e7166SEd Maste  *
4*5d3e7166SEd Maste  * libcbor is free software; you can redistribute it and/or modify
5*5d3e7166SEd Maste  * it under the terms of the MIT license. See LICENSE for details.
6*5d3e7166SEd Maste  */
7*5d3e7166SEd Maste 
8*5d3e7166SEd Maste #include "assertions.h"
9*5d3e7166SEd Maste #include "cbor.h"
10*5d3e7166SEd Maste 
11*5d3e7166SEd Maste unsigned char buffer[512];
12*5d3e7166SEd Maste 
test_embedded_string_start(void ** _CBOR_UNUSED (_state))13*5d3e7166SEd Maste static void test_embedded_string_start(void **_CBOR_UNUSED(_state)) {
14*5d3e7166SEd Maste   assert_size_equal(1, cbor_encode_string_start(1, buffer, 512));
15*5d3e7166SEd Maste   assert_memory_equal(buffer, ((unsigned char[]){0x61}), 1);
16*5d3e7166SEd Maste }
17*5d3e7166SEd Maste 
test_string_start(void ** _CBOR_UNUSED (_state))18*5d3e7166SEd Maste static void test_string_start(void **_CBOR_UNUSED(_state)) {
19*5d3e7166SEd Maste   assert_size_equal(5, cbor_encode_string_start(1000000, buffer, 512));
20*5d3e7166SEd Maste   assert_memory_equal(buffer, ((unsigned char[]){0x7A, 0x00, 0x0F, 0x42, 0x40}),
21*5d3e7166SEd Maste                       5);
22*5d3e7166SEd Maste }
23*5d3e7166SEd Maste 
test_indef_string_start(void ** _CBOR_UNUSED (_state))24*5d3e7166SEd Maste static void test_indef_string_start(void **_CBOR_UNUSED(_state)) {
25*5d3e7166SEd Maste   assert_size_equal(1, cbor_encode_indef_string_start(buffer, 512));
26*5d3e7166SEd Maste   assert_size_equal(0, cbor_encode_indef_string_start(buffer, 0));
27*5d3e7166SEd Maste   assert_memory_equal(buffer, ((unsigned char[]){0x7F}), 1);
28*5d3e7166SEd Maste }
29*5d3e7166SEd Maste 
main(void)30*5d3e7166SEd Maste int main(void) {
31*5d3e7166SEd Maste   const struct CMUnitTest tests[] = {
32*5d3e7166SEd Maste       cmocka_unit_test(test_embedded_string_start),
33*5d3e7166SEd Maste       cmocka_unit_test(test_string_start),
34*5d3e7166SEd Maste       cmocka_unit_test(test_indef_string_start)};
35*5d3e7166SEd Maste   return cmocka_run_group_tests(tests, NULL, NULL);
36*5d3e7166SEd Maste }
37