xref: /freebsd/contrib/libcbor/test/uint_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 "test_allocator.h"
10*5d3e7166SEd Maste 
11*5d3e7166SEd Maste #include "cbor.h"
12*5d3e7166SEd Maste 
13*5d3e7166SEd Maste cbor_item_t *number;
14*5d3e7166SEd Maste struct cbor_load_result res;
15*5d3e7166SEd Maste 
16*5d3e7166SEd Maste unsigned char data1[] = {0x02, 0xFF};
17*5d3e7166SEd Maste unsigned char data2[] = {0x18, 0xFF, 0xFF};
18*5d3e7166SEd Maste unsigned char data3[] = {0x19, 0x01, 0xf4, 0xFF};
19*5d3e7166SEd Maste unsigned char data4[] = {0x1a, 0xa5, 0xf7, 0x02, 0xb3, 0xFF};
20*5d3e7166SEd Maste unsigned char data5[] = {0x1b, 0xa5, 0xf7, 0x02, 0xb3,
21*5d3e7166SEd Maste                          0xa5, 0xf7, 0x02, 0xb3, 0xFF};
22*5d3e7166SEd Maste 
test_very_short_int(void ** _CBOR_UNUSED (_state))23*5d3e7166SEd Maste static void test_very_short_int(void **_CBOR_UNUSED(_state)) {
24*5d3e7166SEd Maste   number = cbor_load(data1, 2, &res);
25*5d3e7166SEd Maste   assert_true(cbor_typeof(number) == CBOR_TYPE_UINT);
26*5d3e7166SEd Maste   assert_true(cbor_int_get_width(number) == CBOR_INT_8);
27*5d3e7166SEd Maste   assert_true(cbor_isa_uint(number));
28*5d3e7166SEd Maste   assert_false(cbor_isa_negint(number));
29*5d3e7166SEd Maste   assert_true(cbor_get_uint8(number) == 2);
30*5d3e7166SEd Maste   assert_true(res.error.code == 0);
31*5d3e7166SEd Maste   assert_true(res.read == 1);
32*5d3e7166SEd Maste   assert_true(cbor_is_int(number));
33*5d3e7166SEd Maste   cbor_decref(&number);
34*5d3e7166SEd Maste   assert_null(number);
35*5d3e7166SEd Maste }
36*5d3e7166SEd Maste 
test_incomplete_data(void ** _CBOR_UNUSED (_state))37*5d3e7166SEd Maste static void test_incomplete_data(void **_CBOR_UNUSED(_state)) {
38*5d3e7166SEd Maste   number = cbor_load(data2, 1, &res);
39*5d3e7166SEd Maste   assert_null(number);
40*5d3e7166SEd Maste   assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
41*5d3e7166SEd Maste }
42*5d3e7166SEd Maste 
test_short_int(void ** _CBOR_UNUSED (_state))43*5d3e7166SEd Maste static void test_short_int(void **_CBOR_UNUSED(_state)) {
44*5d3e7166SEd Maste   number = cbor_load(data2, 3, &res);
45*5d3e7166SEd Maste   assert_true(cbor_typeof(number) == CBOR_TYPE_UINT);
46*5d3e7166SEd Maste   assert_true(cbor_int_get_width(number) == CBOR_INT_8);
47*5d3e7166SEd Maste   assert_true(cbor_isa_uint(number));
48*5d3e7166SEd Maste   assert_false(cbor_isa_negint(number));
49*5d3e7166SEd Maste   assert_true(cbor_get_uint8(number) == 255);
50*5d3e7166SEd Maste   assert_true(res.error.code == 0);
51*5d3e7166SEd Maste   assert_true(res.read == 2);
52*5d3e7166SEd Maste   assert_true(cbor_is_int(number));
53*5d3e7166SEd Maste   cbor_decref(&number);
54*5d3e7166SEd Maste   assert_null(number);
55*5d3e7166SEd Maste }
56*5d3e7166SEd Maste 
test_half_int(void ** _CBOR_UNUSED (_state))57*5d3e7166SEd Maste static void test_half_int(void **_CBOR_UNUSED(_state)) {
58*5d3e7166SEd Maste   number = cbor_load(data3, 5, &res);
59*5d3e7166SEd Maste   assert_true(cbor_typeof(number) == CBOR_TYPE_UINT);
60*5d3e7166SEd Maste   assert_true(cbor_int_get_width(number) == CBOR_INT_16);
61*5d3e7166SEd Maste   assert_true(cbor_isa_uint(number));
62*5d3e7166SEd Maste   assert_false(cbor_isa_negint(number));
63*5d3e7166SEd Maste   assert_true(cbor_get_uint16(number) == 500);
64*5d3e7166SEd Maste   assert_true(res.error.code == 0);
65*5d3e7166SEd Maste   assert_true(res.read == 3);
66*5d3e7166SEd Maste   assert_true(cbor_is_int(number));
67*5d3e7166SEd Maste   cbor_decref(&number);
68*5d3e7166SEd Maste   assert_null(number);
69*5d3e7166SEd Maste }
70*5d3e7166SEd Maste 
test_int(void ** _CBOR_UNUSED (_state))71*5d3e7166SEd Maste static void test_int(void **_CBOR_UNUSED(_state)) {
72*5d3e7166SEd Maste   number = cbor_load(data4, 6, &res);
73*5d3e7166SEd Maste   assert_true(cbor_typeof(number) == CBOR_TYPE_UINT);
74*5d3e7166SEd Maste   assert_true(cbor_int_get_width(number) == CBOR_INT_32);
75*5d3e7166SEd Maste   assert_true(cbor_isa_uint(number));
76*5d3e7166SEd Maste   assert_false(cbor_isa_negint(number));
77*5d3e7166SEd Maste   assert_true(cbor_get_uint32(number) == 2784428723);
78*5d3e7166SEd Maste   assert_true(res.error.code == 0);
79*5d3e7166SEd Maste   assert_true(res.read == 5);
80*5d3e7166SEd Maste   assert_true(cbor_is_int(number));
81*5d3e7166SEd Maste   cbor_decref(&number);
82*5d3e7166SEd Maste   assert_null(number);
83*5d3e7166SEd Maste }
84*5d3e7166SEd Maste 
test_long_int(void ** _CBOR_UNUSED (_state))85*5d3e7166SEd Maste static void test_long_int(void **_CBOR_UNUSED(_state)) {
86*5d3e7166SEd Maste   number = cbor_load(data5, 10, &res);
87*5d3e7166SEd Maste   assert_true(cbor_typeof(number) == CBOR_TYPE_UINT);
88*5d3e7166SEd Maste   assert_true(cbor_int_get_width(number) == CBOR_INT_64);
89*5d3e7166SEd Maste   assert_true(cbor_isa_uint(number));
90*5d3e7166SEd Maste   assert_false(cbor_isa_negint(number));
91*5d3e7166SEd Maste   assert_true(cbor_get_uint64(number) == 11959030306112471731ULL);
92*5d3e7166SEd Maste   assert_true(res.error.code == 0);
93*5d3e7166SEd Maste   assert_true(res.read == 9);
94*5d3e7166SEd Maste   assert_true(cbor_is_int(number));
95*5d3e7166SEd Maste   cbor_decref(&number);
96*5d3e7166SEd Maste   assert_null(number);
97*5d3e7166SEd Maste }
98*5d3e7166SEd Maste 
test_refcounting(void ** _CBOR_UNUSED (_state))99*5d3e7166SEd Maste static void test_refcounting(void **_CBOR_UNUSED(_state)) {
100*5d3e7166SEd Maste   number = cbor_load(data5, 10, &res);
101*5d3e7166SEd Maste   cbor_incref(number);
102*5d3e7166SEd Maste   assert_true(number->refcount == 2);
103*5d3e7166SEd Maste   cbor_decref(&number);
104*5d3e7166SEd Maste   assert_non_null(number);
105*5d3e7166SEd Maste   cbor_decref(&number);
106*5d3e7166SEd Maste   assert_null(number);
107*5d3e7166SEd Maste }
108*5d3e7166SEd Maste 
test_empty_input(void ** _CBOR_UNUSED (_state))109*5d3e7166SEd Maste static void test_empty_input(void **_CBOR_UNUSED(_state)) {
110*5d3e7166SEd Maste   number = cbor_load(data5, 0, &res);
111*5d3e7166SEd Maste   assert_null(number);
112*5d3e7166SEd Maste   assert_true(res.error.code == CBOR_ERR_NODATA);
113*5d3e7166SEd Maste }
114*5d3e7166SEd Maste 
test_inline_creation(void ** _CBOR_UNUSED (_state))115*5d3e7166SEd Maste static void test_inline_creation(void **_CBOR_UNUSED(_state)) {
116*5d3e7166SEd Maste   number = cbor_build_uint8(10);
117*5d3e7166SEd Maste   assert_true(cbor_get_int(number) == 10);
118*5d3e7166SEd Maste   cbor_decref(&number);
119*5d3e7166SEd Maste 
120*5d3e7166SEd Maste   number = cbor_build_uint16(10);
121*5d3e7166SEd Maste   assert_true(cbor_get_int(number) == 10);
122*5d3e7166SEd Maste   cbor_decref(&number);
123*5d3e7166SEd Maste 
124*5d3e7166SEd Maste   number = cbor_build_uint32(10);
125*5d3e7166SEd Maste   assert_true(cbor_get_int(number) == 10);
126*5d3e7166SEd Maste   cbor_decref(&number);
127*5d3e7166SEd Maste 
128*5d3e7166SEd Maste   number = cbor_build_uint64(10);
129*5d3e7166SEd Maste   assert_true(cbor_get_int(number) == 10);
130*5d3e7166SEd Maste   cbor_decref(&number);
131*5d3e7166SEd Maste }
132*5d3e7166SEd Maste 
test_int_creation(void ** _CBOR_UNUSED (_state))133*5d3e7166SEd Maste static void test_int_creation(void **_CBOR_UNUSED(_state)) {
134*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_int8()); });
135*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_int16()); });
136*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_int32()); });
137*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_int64()); });
138*5d3e7166SEd Maste 
139*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_uint8(0xFF)); });
140*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_uint16(0xFF)); });
141*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_uint32(0xFF)); });
142*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_uint64(0xFF)); });
143*5d3e7166SEd Maste }
144*5d3e7166SEd Maste 
main(void)145*5d3e7166SEd Maste int main(void) {
146*5d3e7166SEd Maste   const struct CMUnitTest tests[] = {
147*5d3e7166SEd Maste       cmocka_unit_test(test_very_short_int),
148*5d3e7166SEd Maste       cmocka_unit_test(test_short_int),
149*5d3e7166SEd Maste       cmocka_unit_test(test_half_int),
150*5d3e7166SEd Maste       cmocka_unit_test(test_int),
151*5d3e7166SEd Maste       cmocka_unit_test(test_long_int),
152*5d3e7166SEd Maste       cmocka_unit_test(test_incomplete_data),
153*5d3e7166SEd Maste       cmocka_unit_test(test_refcounting),
154*5d3e7166SEd Maste       cmocka_unit_test(test_empty_input),
155*5d3e7166SEd Maste       cmocka_unit_test(test_inline_creation),
156*5d3e7166SEd Maste       cmocka_unit_test(test_int_creation),
157*5d3e7166SEd Maste   };
158*5d3e7166SEd Maste   return cmocka_run_group_tests(tests, NULL, NULL);
159*5d3e7166SEd Maste }
160