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