Debian Codegraph @

tests/test_tables.c

raw symbols SCIP
1 // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
2 // SPDX-License-Identifier: mit
3
4 #ifdef _MSC_VER
5 #define _USE_MATH_DEFINES
6 #define _CRT_SECURE_NO_WARNINGS
7 #endif
8
9 #include <errno.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include <inttypes.h>
16
17 #include <rabbitmq-c/amqp.h>
18
19 #include <math.h>
20
21 void die(const char *fmt, ...) {
22 va_list ap;
23 va_start(ap, fmt);
24 vfprintf(stderr, fmt, ap);
25 va_end(ap);
26 fprintf(stderr, "\n");
27 abort();
28 }
29
30 static void dump_indent(int indent, FILE *out) {
31 int i;
32
33 for (i = 0; i < indent; i++) {
34 fputc(' ', out);
35 }
36 }
37
38 static void dump_value(int indent, amqp_field_value_t v, FILE *out) {
39 int i;
40
41 dump_indent(indent, out);
42 fputc(v.kind, out);
43
44 switch (v.kind) {
45 case AMQP_FIELD_KIND_BOOLEAN:
46 fputs(v.value.boolean ? " true\n" : " false\n", out);
47 break;
48
49 case AMQP_FIELD_KIND_I8:
50 fprintf(out, " %" PRId8 "\n", v.value.i8);
51 break;
52
53 case AMQP_FIELD_KIND_U8:
54 fprintf(out, " %" PRIu8 "\n", v.value.u8);
55 break;
56
57 case AMQP_FIELD_KIND_I16:
58 fprintf(out, " %" PRId16 "\n", v.value.i16);
59 break;
60
61 case AMQP_FIELD_KIND_U16:
62 fprintf(out, " %" PRIu16 "\n", v.value.u16);
63 break;
64
65 case AMQP_FIELD_KIND_I32:
66 fprintf(out, " %" PRId32 "\n", v.value.i32);
67 break;
68
69 case AMQP_FIELD_KIND_U32:
70 fprintf(out, " %" PRIu32 "\n", v.value.u32);
71 break;
72
73 case AMQP_FIELD_KIND_I64:
74 fprintf(out, " %" PRId64 "\n", v.value.i64);
75 break;
76
77 case AMQP_FIELD_KIND_F32:
78 fprintf(out, " %g\n", (double)v.value.f32);
79 break;
80
81 case AMQP_FIELD_KIND_F64:
82 fprintf(out, " %g\n", v.value.f64);
83 break;
84
85 case AMQP_FIELD_KIND_DECIMAL:
86 fprintf(out, " %u:::%u\n", v.value.decimal.decimals,
87 v.value.decimal.value);
88 break;
89
90 case AMQP_FIELD_KIND_UTF8:
91 fprintf(out, " %.*s\n", (int)v.value.bytes.len,
92 (char *)v.value.bytes.bytes);
93 break;
94
95 case AMQP_FIELD_KIND_BYTES:
96 fputc(' ', out);
97 for (i = 0; i < (int)v.value.bytes.len; i++) {
98 fprintf(out, "%02x", ((char *)v.value.bytes.bytes)[i]);
99 }
100
101 fputc('\n', out);
102 break;
103
104 case AMQP_FIELD_KIND_ARRAY:
105 fputc('\n', out);
106 for (i = 0; i < v.value.array.num_entries; i++) {
107 dump_value(indent + 2, v.value.array.entries[i], out);
108 }
109
110 break;
111
112 case AMQP_FIELD_KIND_TIMESTAMP:
113 fprintf(out, " %" PRIu64 "\n", v.value.u64);
114 break;
115
116 case AMQP_FIELD_KIND_TABLE:
117 fputc('\n', out);
118 for (i = 0; i < v.value.table.num_entries; i++) {
119 dump_indent(indent + 2, out);
120 fprintf(out, "%.*s ->\n", (int)v.value.table.entries[i].key.len,
121 (char *)v.value.table.entries[i].key.bytes);
122 dump_value(indent + 4, v.value.table.entries[i].value, out);
123 }
124
125 break;
126
127 case AMQP_FIELD_KIND_VOID:
128 fputc('\n', out);
129 break;
130
131 default:
132 fprintf(out, "???\n");
133 break;
134 }
135 }
136
137 static void test_dump_value(FILE *out) {
138 amqp_table_entry_t entries[8];
139 amqp_table_t table;
140 amqp_field_value_t val;
141
142 entries[0].key = amqp_literal_bytes("zebra");
143 entries[0].value.kind = AMQP_FIELD_KIND_UTF8;
144 entries[0].value.value.bytes = amqp_literal_bytes("last");
145
146 entries[1].key = amqp_literal_bytes("aardvark");
147 entries[1].value.kind = AMQP_FIELD_KIND_UTF8;
148 entries[1].value.value.bytes = amqp_literal_bytes("first");
149
150 entries[2].key = amqp_literal_bytes("middle");
151 entries[2].value.kind = AMQP_FIELD_KIND_UTF8;
152 entries[2].value.value.bytes = amqp_literal_bytes("third");
153
154 entries[3].key = amqp_literal_bytes("number");
155 entries[3].value.kind = AMQP_FIELD_KIND_I32;
156 entries[3].value.value.i32 = 1234;
157
158 entries[4].key = amqp_literal_bytes("decimal");
159 entries[4].value.kind = AMQP_FIELD_KIND_DECIMAL;
160 entries[4].value.value.decimal.decimals = 2;
161 entries[4].value.value.decimal.value = 1234;
162
163 entries[5].key = amqp_literal_bytes("time");
164 entries[5].value.kind = AMQP_FIELD_KIND_TIMESTAMP;
165 entries[5].value.value.u64 = 1234123412341234;
166
167 entries[6].key = amqp_literal_bytes("beta");
168 entries[6].value.kind = AMQP_FIELD_KIND_UTF8;
169 entries[6].value.value.bytes = amqp_literal_bytes("second");
170
171 entries[7].key = amqp_literal_bytes("wombat");
172 entries[7].value.kind = AMQP_FIELD_KIND_UTF8;
173 entries[7].value.value.bytes = amqp_literal_bytes("fourth");
174
175 table.num_entries = 8;
176 table.entries = entries;
177
178 qsort(table.entries, table.num_entries, sizeof(amqp_table_entry_t),
179 &amqp_table_entry_cmp);
180
181 val.kind = AMQP_FIELD_KIND_TABLE;
182 val.value.table = table;
183
184 dump_value(0, val, out);
185 }
186
187 static uint8_t pre_encoded_table[] = {
188 0x00, 0x00, 0x00, 0xff, 0x07, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72,
189 0x53, 0x00, 0x00, 0x00, 0x15, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73,
190 0x20, 0x61, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x73, 0x74, 0x72, 0x69,
191 0x6e, 0x67, 0x09, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x69, 0x6e, 0x74,
192 0x49, 0x00, 0x00, 0x30, 0x39, 0x07, 0x64, 0x65, 0x63, 0x69, 0x6d, 0x61,
193 0x6c, 0x44, 0x03, 0x00, 0x01, 0xe2, 0x40, 0x09, 0x74, 0x69, 0x6d, 0x65,
194 0x73, 0x74, 0x61, 0x6d, 0x70, 0x54, 0x00, 0x00, 0x63, 0xee, 0xa0, 0x53,
195 0xc1, 0x94, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x46, 0x00, 0x00, 0x00,
196 0x1f, 0x03, 0x6f, 0x6e, 0x65, 0x49, 0x00, 0x00, 0xd4, 0x31, 0x03, 0x74,
197 0x77, 0x6f, 0x53, 0x00, 0x00, 0x00, 0x0d, 0x41, 0x20, 0x6c, 0x6f, 0x6e,
198 0x67, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x04, 0x62, 0x79, 0x74,
199 0x65, 0x62, 0xff, 0x04, 0x6c, 0x6f, 0x6e, 0x67, 0x6c, 0x00, 0x00, 0x00,
200 0x00, 0x49, 0x96, 0x02, 0xd2, 0x05, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x73,
201 0x02, 0x8f, 0x04, 0x62, 0x6f, 0x6f, 0x6c, 0x74, 0x01, 0x06, 0x62, 0x69,
202 0x6e, 0x61, 0x72, 0x79, 0x78, 0x00, 0x00, 0x00, 0x0f, 0x61, 0x20, 0x62,
203 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
204 0x04, 0x76, 0x6f, 0x69, 0x64, 0x56, 0x05, 0x61, 0x72, 0x72, 0x61, 0x79,
205 0x41, 0x00, 0x00, 0x00, 0x17, 0x49, 0x00, 0x00, 0xd4, 0x31, 0x53, 0x00,
206 0x00, 0x00, 0x0d, 0x41, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x73, 0x74,
207 0x72, 0x69, 0x6e, 0x67, 0x05, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x66, 0x40,
208 0x49, 0x0f, 0xdb, 0x06, 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x64, 0x40,
209 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18};
210
211 static void test_table_codec(FILE *out) {
212 amqp_pool_t pool;
213 int result;
214
215 amqp_table_entry_t inner_entries[2];
216 amqp_table_t inner_table;
217
218 amqp_field_value_t inner_values[2];
219 amqp_array_t inner_array;
220
221 amqp_table_entry_t entries[14];
222 amqp_table_t table;
223
224 inner_entries[0].key = amqp_literal_bytes("one");
225 inner_entries[0].value.kind = AMQP_FIELD_KIND_I32;
226 inner_entries[0].value.value.i32 = 54321;
227
228 inner_entries[1].key = amqp_literal_bytes("two");
229 inner_entries[1].value.kind = AMQP_FIELD_KIND_UTF8;
230 inner_entries[1].value.value.bytes = amqp_literal_bytes("A long string");
231
232 inner_table.num_entries = 2;
233 inner_table.entries = inner_entries;
234
235 inner_values[0].kind = AMQP_FIELD_KIND_I32;
236 inner_values[0].value.i32 = 54321;
237
238 inner_values[1].kind = AMQP_FIELD_KIND_UTF8;
239 inner_values[1].value.bytes = amqp_literal_bytes("A long string");
240
241 inner_array.num_entries = 2;
242 inner_array.entries = inner_values;
243
244 entries[0].key = amqp_literal_bytes("longstr");
245 entries[0].value.kind = AMQP_FIELD_KIND_UTF8;
246 entries[0].value.value.bytes = amqp_literal_bytes("Here is a long string");
247
248 entries[1].key = amqp_literal_bytes("signedint");
249 entries[1].value.kind = AMQP_FIELD_KIND_I32;
250 entries[1].value.value.i32 = 12345;
251
252 entries[2].key = amqp_literal_bytes("decimal");
253 entries[2].value.kind = AMQP_FIELD_KIND_DECIMAL;
254 entries[2].value.value.decimal.decimals = 3;
255 entries[2].value.value.decimal.value = 123456;
256
257 entries[3].key = amqp_literal_bytes("timestamp");
258 entries[3].value.kind = AMQP_FIELD_KIND_TIMESTAMP;
259 entries[3].value.value.u64 = 109876543209876;
260
261 entries[4].key = amqp_literal_bytes("table");
262 entries[4].value.kind = AMQP_FIELD_KIND_TABLE;
263 entries[4].value.value.table = inner_table;
264
265 entries[5].key = amqp_literal_bytes("byte");
266 entries[5].value.kind = AMQP_FIELD_KIND_I8;
267 entries[5].value.value.i8 = (int8_t)-1;
268
269 entries[6].key = amqp_literal_bytes("long");
270 entries[6].value.kind = AMQP_FIELD_KIND_I64;
271 entries[6].value.value.i64 = 1234567890;
272
273 entries[7].key = amqp_literal_bytes("short");
274 entries[7].value.kind = AMQP_FIELD_KIND_I16;
275 entries[7].value.value.i16 = 655;
276
277 entries[8].key = amqp_literal_bytes("bool");
278 entries[8].value.kind = AMQP_FIELD_KIND_BOOLEAN;
279 entries[8].value.value.boolean = 1;
280
281 entries[9].key = amqp_literal_bytes("binary");
282 entries[9].value.kind = AMQP_FIELD_KIND_BYTES;
283 entries[9].value.value.bytes = amqp_literal_bytes("a binary string");
284
285 entries[10].key = amqp_literal_bytes("void");
286 entries[10].value.kind = AMQP_FIELD_KIND_VOID;
287
288 entries[11].key = amqp_literal_bytes("array");
289 entries[11].value.kind = AMQP_FIELD_KIND_ARRAY;
290 entries[11].value.value.array = inner_array;
291
292 entries[12].key = amqp_literal_bytes("float");
293 entries[12].value.kind = AMQP_FIELD_KIND_F32;
294 entries[12].value.value.f32 = (float)M_PI;
295
296 entries[13].key = amqp_literal_bytes("double");
297 entries[13].value.kind = AMQP_FIELD_KIND_F64;
298 entries[13].value.value.f64 = M_PI;
299
300 table.num_entries = 14;
301 table.entries = entries;
302
303 fprintf(out, "AAAAAAAAAA\n");
304
305 {
306 amqp_field_value_t val;
307 val.kind = AMQP_FIELD_KIND_TABLE;
308 val.value.table = table;
309 dump_value(0, val, out);
310 }
311
312 init_amqp_pool(&pool, 4096);
313
314 {
315 amqp_table_t decoded;
316 size_t decoding_offset = 0;
317 amqp_bytes_t decoding_bytes;
318 decoding_bytes.len = sizeof(pre_encoded_table);
319 decoding_bytes.bytes = pre_encoded_table;
320
321 result =
322 amqp_decode_table(decoding_bytes, &pool, &decoded, &decoding_offset);
323 if (result < 0) {
324 die("Table decoding failed: %s", amqp_error_string2(result));
325 }
326
327 fprintf(out, "BBBBBBBBBB\n");
328
329 {
330 amqp_field_value_t val;
331 val.kind = AMQP_FIELD_KIND_TABLE;
332 val.value.table = decoded;
333
334 dump_value(0, val, out);
335 }
336 }
337
338 {
339 uint8_t encoding_buffer[4096];
340 amqp_bytes_t encoding_result;
341 size_t offset = 0;
342
343 memset(&encoding_buffer[0], 0, sizeof(encoding_buffer));
344 encoding_result.len = sizeof(encoding_buffer);
345 encoding_result.bytes = &encoding_buffer[0];
346
347 result = amqp_encode_table(encoding_result, &table, &offset);
348 if (result < 0) {
349 die("Table encoding failed: %s", amqp_error_string2(result));
350 }
351
352 if (offset != sizeof(pre_encoded_table))
353 die("Offset should be %ld, was %ld", (long)sizeof(pre_encoded_table),
354 (long)offset);
355
356 result = memcmp(pre_encoded_table, encoding_buffer, offset);
357 if (result != 0) {
358 die("Table encoding differed", result);
359 }
360 }
361
362 empty_amqp_pool(&pool);
363 }
364
365 #define CHUNK_SIZE 4096
366
367 static int compare_files(FILE *f1_in, FILE *f2_in) {
368 char f1_buf[CHUNK_SIZE];
369 char f2_buf[CHUNK_SIZE];
370 int res;
371
372 rewind(f1_in);
373 rewind(f2_in);
374
375 for (;;) {
376 size_t f1_got = fread(f1_buf, 1, CHUNK_SIZE, f1_in);
377 size_t f2_got = fread(f2_buf, 1, CHUNK_SIZE, f2_in);
378 res = memcmp(f1_buf, f2_buf, f1_got < f2_got ? f1_got : f2_got);
379
380 if (res) {
381 break;
382 }
383
384 if (f1_got < CHUNK_SIZE || f2_got < CHUNK_SIZE) {
385 if (f1_got != f2_got) {
386 res = (f1_got < f2_got ? -1 : 1);
387 }
388 break;
389 }
390 }
391
392 return res;
393 }
394
395 const char *expected_file_name = "tests/test_tables.expected";
396
397 int main(void) {
398 char *srcdir = getenv("srcdir");
399 FILE *out, *expected = NULL;
400 char *expected_path;
401
402 out = tmpfile();
403 if (out == NULL) {
404 die("failed to create temporary file: %s", strerror(errno));
405 }
406
407 test_table_codec(out);
408 fprintf(out, "----------\n");
409 test_dump_value(out);
410
411 if (srcdir == NULL) {
412 srcdir = ".";
413 }
414
415 expected_path = malloc(strlen(srcdir) + strlen(expected_file_name) + 2);
416 if (!expected_path) {
417 die("out of memory");
418 }
419 sprintf(expected_path, "%s/%s", srcdir, expected_file_name);
420 expected = fopen(expected_path, "r");
421 if (!expected) {
422 die("failed to open %s: %s", expected_path, strerror(errno));
423 }
424
425 if (compare_files(expected, out)) {
426 die("output file did not have expected contents");
427 }
428
429 fclose(expected);
430 free(expected_path);
431 fclose(out);
432
433 return 0;
434 }