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