test_tables.c 12 KB

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