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