xref: /freebsd/contrib/libcbor/test/negint_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 #include "test_allocator.h"
11*5d3e7166SEd Maste 
12*5d3e7166SEd Maste cbor_item_t *number;
13*5d3e7166SEd Maste struct cbor_load_result res;
14*5d3e7166SEd Maste 
15*5d3e7166SEd Maste unsigned char data1[] = {0x22, 0xFF};
16*5d3e7166SEd Maste unsigned char data2[] = {0x38, 0xFF, 0xFF};
17*5d3e7166SEd Maste unsigned char data3[] = {0x39, 0x01, 0xf4, 0xFF};
18*5d3e7166SEd Maste unsigned char data4[] = {0x3a, 0xa5, 0xf7, 0x02, 0xb3, 0xFF};
19*5d3e7166SEd Maste unsigned char data5[] = {0x3b, 0xa5, 0xf7, 0x02, 0xb3,
20*5d3e7166SEd Maste                          0xa5, 0xf7, 0x02, 0xb3, 0xFF};
21*5d3e7166SEd Maste 
test_very_short_int(void ** _CBOR_UNUSED (_state))22*5d3e7166SEd Maste static void test_very_short_int(void **_CBOR_UNUSED(_state)) {
23*5d3e7166SEd Maste   number = cbor_load(data1, 2, &res);
24*5d3e7166SEd Maste   assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
25*5d3e7166SEd Maste   assert_true(cbor_int_get_width(number) == CBOR_INT_8);
26*5d3e7166SEd Maste   assert_false(cbor_isa_uint(number));
27*5d3e7166SEd Maste   assert_true(cbor_isa_negint(number));
28*5d3e7166SEd Maste   assert_true(cbor_get_uint8(number) == 2);
29*5d3e7166SEd Maste   assert_true(res.error.code == 0);
30*5d3e7166SEd Maste   assert_true(res.read == 1);
31*5d3e7166SEd Maste   assert_true(cbor_is_int(number));
32*5d3e7166SEd Maste   cbor_decref(&number);
33*5d3e7166SEd Maste   assert_null(number);
34*5d3e7166SEd Maste }
35*5d3e7166SEd Maste 
test_short_int(void ** _CBOR_UNUSED (_state))36*5d3e7166SEd Maste static void test_short_int(void **_CBOR_UNUSED(_state)) {
37*5d3e7166SEd Maste   number = cbor_load(data2, 3, &res);
38*5d3e7166SEd Maste   assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
39*5d3e7166SEd Maste   assert_true(cbor_int_get_width(number) == CBOR_INT_8);
40*5d3e7166SEd Maste   assert_false(cbor_isa_uint(number));
41*5d3e7166SEd Maste   assert_true(cbor_isa_negint(number));
42*5d3e7166SEd Maste   assert_true(cbor_get_uint8(number) == 255);
43*5d3e7166SEd Maste   assert_true(res.error.code == 0);
44*5d3e7166SEd Maste   assert_true(res.read == 2);
45*5d3e7166SEd Maste   assert_true(cbor_is_int(number));
46*5d3e7166SEd Maste   cbor_decref(&number);
47*5d3e7166SEd Maste   assert_null(number);
48*5d3e7166SEd Maste }
49*5d3e7166SEd Maste 
test_half_int(void ** _CBOR_UNUSED (_state))50*5d3e7166SEd Maste static void test_half_int(void **_CBOR_UNUSED(_state)) {
51*5d3e7166SEd Maste   number = cbor_load(data3, 5, &res);
52*5d3e7166SEd Maste   assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
53*5d3e7166SEd Maste   assert_true(cbor_int_get_width(number) == CBOR_INT_16);
54*5d3e7166SEd Maste   assert_false(cbor_isa_uint(number));
55*5d3e7166SEd Maste   assert_true(cbor_isa_negint(number));
56*5d3e7166SEd Maste   assert_true(cbor_get_uint16(number) == 500);
57*5d3e7166SEd Maste   assert_true(res.error.code == 0);
58*5d3e7166SEd Maste   assert_true(res.read == 3);
59*5d3e7166SEd Maste   assert_true(cbor_is_int(number));
60*5d3e7166SEd Maste   cbor_decref(&number);
61*5d3e7166SEd Maste   assert_null(number);
62*5d3e7166SEd Maste }
63*5d3e7166SEd Maste 
test_int(void ** _CBOR_UNUSED (_state))64*5d3e7166SEd Maste static void test_int(void **_CBOR_UNUSED(_state)) {
65*5d3e7166SEd Maste   number = cbor_load(data4, 6, &res);
66*5d3e7166SEd Maste   assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
67*5d3e7166SEd Maste   assert_true(cbor_int_get_width(number) == CBOR_INT_32);
68*5d3e7166SEd Maste   assert_false(cbor_isa_uint(number));
69*5d3e7166SEd Maste   assert_true(cbor_isa_negint(number));
70*5d3e7166SEd Maste   assert_true(cbor_get_uint32(number) == 2784428723);
71*5d3e7166SEd Maste   assert_true(res.error.code == 0);
72*5d3e7166SEd Maste   assert_true(res.read == 5);
73*5d3e7166SEd Maste   assert_true(cbor_is_int(number));
74*5d3e7166SEd Maste   cbor_decref(&number);
75*5d3e7166SEd Maste   assert_null(number);
76*5d3e7166SEd Maste }
77*5d3e7166SEd Maste 
test_long_int(void ** _CBOR_UNUSED (_state))78*5d3e7166SEd Maste static void test_long_int(void **_CBOR_UNUSED(_state)) {
79*5d3e7166SEd Maste   number = cbor_load(data5, 10, &res);
80*5d3e7166SEd Maste   assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
81*5d3e7166SEd Maste   assert_true(cbor_int_get_width(number) == CBOR_INT_64);
82*5d3e7166SEd Maste   assert_false(cbor_isa_uint(number));
83*5d3e7166SEd Maste   assert_true(cbor_isa_negint(number));
84*5d3e7166SEd Maste   assert_true(cbor_get_uint64(number) == 11959030306112471731ULL);
85*5d3e7166SEd Maste   assert_true(res.error.code == 0);
86*5d3e7166SEd Maste   assert_true(res.read == 9);
87*5d3e7166SEd Maste   assert_true(cbor_is_int(number));
88*5d3e7166SEd Maste   cbor_decref(&number);
89*5d3e7166SEd Maste   assert_null(number);
90*5d3e7166SEd Maste }
91*5d3e7166SEd Maste 
test_int_creation(void ** _CBOR_UNUSED (_state))92*5d3e7166SEd Maste static void test_int_creation(void **_CBOR_UNUSED(_state)) {
93*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_int8()); });
94*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_int16()); });
95*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_int32()); });
96*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_new_int64()); });
97*5d3e7166SEd Maste 
98*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_negint8(0xFF)); });
99*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_negint16(0xFF)); });
100*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_negint32(0xFF)); });
101*5d3e7166SEd Maste   WITH_FAILING_MALLOC({ assert_null(cbor_build_negint64(0xFF)); });
102*5d3e7166SEd Maste }
103*5d3e7166SEd Maste 
main(void)104*5d3e7166SEd Maste int main(void) {
105*5d3e7166SEd Maste   const struct CMUnitTest tests[] = {
106*5d3e7166SEd Maste       cmocka_unit_test(test_very_short_int),
107*5d3e7166SEd Maste       cmocka_unit_test(test_short_int),
108*5d3e7166SEd Maste       cmocka_unit_test(test_half_int),
109*5d3e7166SEd Maste       cmocka_unit_test(test_int),
110*5d3e7166SEd Maste       cmocka_unit_test(test_long_int),
111*5d3e7166SEd Maste       cmocka_unit_test(test_int_creation),
112*5d3e7166SEd Maste   };
113*5d3e7166SEd Maste   return cmocka_run_group_tests(tests, NULL, NULL);
114*5d3e7166SEd Maste }
115