xref: /freebsd/contrib/libcbor/test/uint_encoders_test.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
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 "assertions.h"
9 #include "cbor.h"
10 
11 unsigned char buffer[512];
12 
13 static void test_embedded_uint8(void **_CBOR_UNUSED(_state)) {
14   assert_size_equal(1, cbor_encode_uint8(14, buffer, 512));
15   assert_memory_equal(buffer, (unsigned char[]){0x0E}, 1);
16 }
17 
18 static void test_uint8(void **_CBOR_UNUSED(_state)) {
19   assert_size_equal(0, cbor_encode_uint8(180, buffer, 1));
20   assert_size_equal(2, cbor_encode_uint8(255, buffer, 512));
21   assert_memory_equal(buffer, ((unsigned char[]){0x18, 0xFF}), 2);
22 }
23 
24 static void test_uint16(void **_CBOR_UNUSED(_state)) {
25   assert_size_equal(0, cbor_encode_uint16(1000, buffer, 2));
26   assert_size_equal(3, cbor_encode_uint16(1000, buffer, 512));
27   assert_memory_equal(buffer, ((unsigned char[]){0x19, 0x03, 0xE8}), 3);
28 }
29 
30 static void test_uint32(void **_CBOR_UNUSED(_state)) {
31   assert_size_equal(0, cbor_encode_uint32(1000000, buffer, 4));
32   assert_size_equal(5, cbor_encode_uint32(1000000, buffer, 512));
33   assert_memory_equal(buffer, ((unsigned char[]){0x1A, 0x00, 0x0F, 0x42, 0x40}),
34                       5);
35 }
36 
37 static void test_uint64(void **_CBOR_UNUSED(_state)) {
38   assert_size_equal(0, cbor_encode_uint64(18446744073709551615ULL, buffer, 8));
39   assert_size_equal(9,
40                     cbor_encode_uint64(18446744073709551615ULL, buffer, 512));
41   assert_memory_equal(
42       buffer,
43       ((unsigned char[]){0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
44       9);
45 }
46 
47 static void test_unspecified(void **_CBOR_UNUSED(_state)) {
48   assert_size_equal(9, cbor_encode_uint(18446744073709551615ULL, buffer, 512));
49   assert_memory_equal(
50       buffer,
51       ((unsigned char[]){0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
52       9);
53   assert_size_equal(5, cbor_encode_uint(1000000, buffer, 512));
54   assert_memory_equal(buffer, ((unsigned char[]){0x1A, 0x00, 0x0F, 0x42, 0x40}),
55                       5);
56   assert_size_equal(3, cbor_encode_uint(1000, buffer, 512));
57   assert_memory_equal(buffer, ((unsigned char[]){0x19, 0x03, 0xE8}), 3);
58   assert_size_equal(2, cbor_encode_uint(255, buffer, 512));
59   assert_memory_equal(buffer, ((unsigned char[]){0x18, 0xFF}), 2);
60 }
61 
62 int main(void) {
63   const struct CMUnitTest tests[] = {cmocka_unit_test(test_embedded_uint8),
64                                      cmocka_unit_test(test_uint8),
65                                      cmocka_unit_test(test_uint16),
66                                      cmocka_unit_test(test_uint32),
67                                      cmocka_unit_test(test_uint64),
68                                      cmocka_unit_test(test_unspecified)};
69   return cmocka_run_group_tests(tests, NULL, NULL);
70 }
71