forked from PJK/libcbor
/
type_3_test.c
165 lines (152 loc) · 9.87 KB
/
type_3_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdio.h>
#include "cbor.h"
#include <inttypes.h>
cbor_item_t * string;
struct cbor_load_result res;
unsigned char empty_string_data[] = { 0x60 };
static void test_empty_string(void **state) {
string = cbor_load(empty_string_data, 1, CBOR_FLAGS_NONE, &res);
assert_non_null(string);
assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
assert_true(cbor_isa_string(string));
assert_int_equal(cbor_string_length(string), 0);
assert_int_equal(cbor_string_codepoint_count(string), 0);
assert_true(res.read == 1);
cbor_decref(&string);
assert_null(string);
}
unsigned char short_string_data[] = { 0x6C, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21 };
/* 0x60 + 12 | Hello world! */
static void test_short_string(void **state) {
string = cbor_load(short_string_data, 13, CBOR_FLAGS_NONE, &res);
assert_non_null(string);
assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
assert_true(cbor_isa_string(string));
assert_int_equal(cbor_string_length(string), 12);
assert_int_equal(cbor_string_codepoint_count(string), 12);
assert_memory_equal(&"Hello world!", cbor_string_handle(string), 12);
assert_true(res.read == 13);
cbor_decref(&string);
assert_null(string);
}
unsigned char short_multibyte_string_data[] = { 0x6F, 0xC4, 0x8C, 0x61, 0x75, 0x65, 0x73, 0x20, 0xC3, 0x9F, 0x76, 0xC4, 0x9B, 0x74, 0x65, 0x21 };
/* 0x60 + 15 | Čaues ßvěte! */
static void test_short_multibyte_string(void **state) {
string = cbor_load(short_multibyte_string_data, 16, CBOR_FLAGS_NONE, &res);
assert_non_null(string);
assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
assert_true(cbor_isa_string(string));
assert_int_equal(cbor_string_length(string), 15);
assert_int_equal(cbor_string_codepoint_count(string), 12);
assert_memory_equal(&"Čaues ßvěte!", cbor_string_handle(string), 15);
assert_true(res.read == 16);
cbor_decref(&string);
assert_null(string);
}
unsigned char int8_string_data[] = { 0x78, 0x96, 0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69, 0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20, 0x44, 0x6F, 0x6E, 0x65, 0x63, 0x20, 0x6D, 0x69, 0x20, 0x74, 0x65, 0x6C, 0x6C, 0x75, 0x73, 0x2C, 0x20, 0x69, 0x61, 0x63, 0x75, 0x6C, 0x69, 0x73, 0x20, 0x6E, 0x65, 0x63, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6C, 0x75, 0x6D, 0x20, 0x71, 0x75, 0x69, 0x73, 0x2C, 0x20, 0x66, 0x65, 0x72, 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x6E, 0x6F, 0x6E, 0x20, 0x66, 0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65, 0x63, 0x65, 0x6E, 0x61, 0x73, 0x20, 0x75, 0x74, 0x20, 0x6A, 0x75, 0x73, 0x74, 0x6F, 0x20, 0x70, 0x6F, 0x73, 0x75, 0x65, 0x72, 0x65, 0x2E };
/* 150 | Lorem ....*/
static void test_int8_string(void **state) {
string = cbor_load(int8_string_data, 152, CBOR_FLAGS_NONE, &res);
assert_non_null(string);
assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
assert_true(cbor_isa_string(string));
assert_int_equal(cbor_string_length(string), 150);
assert_int_equal(cbor_string_codepoint_count(string), 150);
assert_memory_equal(
&"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.",
cbor_string_handle(string),
150
);
assert_true(res.read == 152);
cbor_decref(&string);
assert_null(string);
}
unsigned char int16_string_data[] = { 0x79, 0x00, 0x96, 0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69, 0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20, 0x44, 0x6F, 0x6E, 0x65, 0x63, 0x20, 0x6D, 0x69, 0x20, 0x74, 0x65, 0x6C, 0x6C, 0x75, 0x73, 0x2C, 0x20, 0x69, 0x61, 0x63, 0x75, 0x6C, 0x69, 0x73, 0x20, 0x6E, 0x65, 0x63, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6C, 0x75, 0x6D, 0x20, 0x71, 0x75, 0x69, 0x73, 0x2C, 0x20, 0x66, 0x65, 0x72, 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x6E, 0x6F, 0x6E, 0x20, 0x66, 0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65, 0x63, 0x65, 0x6E, 0x61, 0x73, 0x20, 0x75, 0x74, 0x20, 0x6A, 0x75, 0x73, 0x74, 0x6F, 0x20, 0x70, 0x6F, 0x73, 0x75, 0x65, 0x72, 0x65, 0x2E };
/* 150 | Lorem ....*/
/* This valid but not realistic - length 150 could be encoded in a single uint8_t (but we need to keep the test files reasonably compact) */
static void test_int16_string(void **state) {
string = cbor_load(int16_string_data, 153, CBOR_FLAGS_NONE, &res);
assert_non_null(string);
assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
assert_true(cbor_isa_string(string));
assert_int_equal(cbor_string_length(string), 150);
assert_int_equal(cbor_string_codepoint_count(string), 150);
assert_memory_equal(
&"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.",
cbor_string_handle(string),
150
);
assert_true(res.read == 153);
cbor_decref(&string);
assert_null(string);
}
unsigned char int32_string_data[] = { 0x7A, 0x00, 0x00, 0x00, 0x96, 0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69, 0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20, 0x44, 0x6F, 0x6E, 0x65, 0x63, 0x20, 0x6D, 0x69, 0x20, 0x74, 0x65, 0x6C, 0x6C, 0x75, 0x73, 0x2C, 0x20, 0x69, 0x61, 0x63, 0x75, 0x6C, 0x69, 0x73, 0x20, 0x6E, 0x65, 0x63, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6C, 0x75, 0x6D, 0x20, 0x71, 0x75, 0x69, 0x73, 0x2C, 0x20, 0x66, 0x65, 0x72, 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x6E, 0x6F, 0x6E, 0x20, 0x66, 0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65, 0x63, 0x65, 0x6E, 0x61, 0x73, 0x20, 0x75, 0x74, 0x20, 0x6A, 0x75, 0x73, 0x74, 0x6F, 0x20, 0x70, 0x6F, 0x73, 0x75, 0x65, 0x72, 0x65, 0x2E };
/* 150 | Lorem ....*/
static void test_int32_string(void **state) {
string = cbor_load(int32_string_data, 155, CBOR_FLAGS_NONE, &res);
assert_non_null(string);
assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
assert_true(cbor_isa_string(string));
assert_int_equal(cbor_string_length(string), 150);
assert_int_equal(cbor_string_codepoint_count(string), 150);
assert_memory_equal(
&"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.",
cbor_string_handle(string),
150
);
assert_true(res.read == 155);
cbor_decref(&string);
assert_null(string);
}
unsigned char int64_string_data[] = { 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x96, 0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69, 0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20, 0x44, 0x6F, 0x6E, 0x65, 0x63, 0x20, 0x6D, 0x69, 0x20, 0x74, 0x65, 0x6C, 0x6C, 0x75, 0x73, 0x2C, 0x20, 0x69, 0x61, 0x63, 0x75, 0x6C, 0x69, 0x73, 0x20, 0x6E, 0x65, 0x63, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6C, 0x75, 0x6D, 0x20, 0x71, 0x75, 0x69, 0x73, 0x2C, 0x20, 0x66, 0x65, 0x72, 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x6E, 0x6F, 0x6E, 0x20, 0x66, 0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65, 0x63, 0x65, 0x6E, 0x61, 0x73, 0x20, 0x75, 0x74, 0x20, 0x6A, 0x75, 0x73, 0x74, 0x6F, 0x20, 0x70, 0x6F, 0x73, 0x75, 0x65, 0x72, 0x65, 0x2E };
/* 150 | Lorem ....*/
static void test_int64_string(void **state) {
string = cbor_load(int64_string_data, 159, CBOR_FLAGS_NONE, &res);
assert_non_null(string);
assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
assert_true(cbor_isa_string(string));
assert_int_equal(cbor_string_length(string), 150);
assert_int_equal(cbor_string_codepoint_count(string), 150);
assert_memory_equal(
&"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.",
cbor_string_handle(string),
150
);
assert_true(res.read == 159);
cbor_decref(&string);
assert_null(string);
}
unsigned char short_indef_string_data[] = { 0x7F, 0x78, 0x01, 0x65, 0xFF, 0xFF };
/* start | string | break| extra */
static void test_short_indef_string(void **state) {
string = cbor_load(short_indef_string_data, 6, CBOR_FLAGS_NONE, &res);
assert_non_null(string);
assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
assert_true(cbor_isa_string(string));
assert_true(cbor_string_length(string) == 0);
assert_true(cbor_string_is_indefinite(string));
assert_true(cbor_string_chunk_count(string) == 1);
assert_true(res.read == 5);
assert_true(cbor_isa_string(cbor_string_chunks_handle(string)[0]));
assert_true(cbor_string_length(cbor_string_chunks_handle(string)[0]) == 1);
assert_true(*cbor_string_handle(cbor_string_chunks_handle(string)[0]) == 'e');
cbor_decref(&string);
assert_null(string);
}
int main(void) {
const UnitTest tests[] = {
unit_test(test_empty_string),
unit_test(test_short_string),
unit_test(test_short_multibyte_string),
unit_test(test_int8_string),
unit_test(test_int16_string),
unit_test(test_int32_string),
unit_test(test_int64_string),
unit_test(test_short_indef_string)
};
return run_tests(tests);
}