test_merge_capabilities.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  2. // SPDX-License-Identifier: mit
  3. #include "amqp_socket.h"
  4. #include "amqp_table.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. static int compare_bytes(amqp_bytes_t l, amqp_bytes_t r);
  8. static int compare_amqp_table_entry(amqp_table_entry_t result,
  9. amqp_table_entry_t expect);
  10. static int compare_field_value(amqp_field_value_t result,
  11. amqp_field_value_t expect);
  12. static int compare_amqp_table(amqp_table_t* result, amqp_table_t* expect);
  13. static int compare_bytes(amqp_bytes_t l, amqp_bytes_t r) {
  14. if (l.len == r.len &&
  15. (l.bytes == r.bytes || 0 == memcmp(l.bytes, r.bytes, l.len))) {
  16. return 1;
  17. }
  18. return 0;
  19. }
  20. static int compare_amqp_table_entry(amqp_table_entry_t result,
  21. amqp_table_entry_t expect) {
  22. if (!compare_bytes(result.key, expect.key)) {
  23. return 0;
  24. }
  25. return compare_field_value(result.value, expect.value);
  26. }
  27. static int compare_field_value(amqp_field_value_t result,
  28. amqp_field_value_t expect) {
  29. if (result.kind != expect.kind) {
  30. return 0;
  31. }
  32. switch (result.kind) {
  33. case AMQP_FIELD_KIND_BOOLEAN:
  34. return result.value.boolean == expect.value.boolean;
  35. case AMQP_FIELD_KIND_I8:
  36. return result.value.i8 == expect.value.i8;
  37. case AMQP_FIELD_KIND_U8:
  38. return result.value.u8 == expect.value.u8;
  39. case AMQP_FIELD_KIND_I16:
  40. return result.value.i16 == expect.value.i16;
  41. case AMQP_FIELD_KIND_U16:
  42. return result.value.u16 == expect.value.u16;
  43. case AMQP_FIELD_KIND_I32:
  44. return result.value.i32 == expect.value.i32;
  45. case AMQP_FIELD_KIND_U32:
  46. return result.value.u32 == expect.value.u32;
  47. case AMQP_FIELD_KIND_I64:
  48. return result.value.i64 == expect.value.i64;
  49. case AMQP_FIELD_KIND_U64:
  50. case AMQP_FIELD_KIND_TIMESTAMP:
  51. return result.value.u64 == expect.value.u64;
  52. case AMQP_FIELD_KIND_F32:
  53. return result.value.f32 == expect.value.f32;
  54. case AMQP_FIELD_KIND_F64:
  55. return result.value.f64 == expect.value.f64;
  56. case AMQP_FIELD_KIND_DECIMAL:
  57. return !memcmp(&result.value.decimal, &expect.value.decimal,
  58. sizeof(expect.value.decimal));
  59. case AMQP_FIELD_KIND_UTF8:
  60. case AMQP_FIELD_KIND_BYTES:
  61. return compare_bytes(result.value.bytes, expect.value.bytes);
  62. case AMQP_FIELD_KIND_ARRAY: {
  63. int i;
  64. if (result.value.array.num_entries != expect.value.array.num_entries) {
  65. return 0;
  66. }
  67. for (i = 0; i < result.value.array.num_entries; ++i) {
  68. if (!compare_field_value(result.value.array.entries[i],
  69. expect.value.array.entries[i])) {
  70. return 0;
  71. }
  72. }
  73. return 1;
  74. }
  75. case AMQP_FIELD_KIND_TABLE:
  76. return compare_amqp_table(&result.value.table, &expect.value.table);
  77. case AMQP_FIELD_KIND_VOID:
  78. return 1;
  79. }
  80. return 1;
  81. }
  82. static int compare_amqp_table(amqp_table_t* result, amqp_table_t* expect) {
  83. int i;
  84. if (result->num_entries != expect->num_entries) {
  85. return 0;
  86. }
  87. for (i = 0; i < expect->num_entries; ++i) {
  88. if (!compare_amqp_table_entry(expect->entries[i], result->entries[i])) {
  89. return 0;
  90. }
  91. }
  92. return 1;
  93. }
  94. static void test_merge_capabilities(amqp_table_t* base, amqp_table_t* add,
  95. amqp_table_t* expect) {
  96. amqp_pool_t pool;
  97. amqp_table_t result;
  98. int res;
  99. init_amqp_pool(&pool, 4096);
  100. res = amqp_merge_capabilities(base, add, &result, &pool);
  101. if (AMQP_STATUS_OK != res) {
  102. fprintf(stderr, "amqp_merge_capabilities returned !ok: %d\n", res);
  103. abort();
  104. }
  105. if (!compare_amqp_table(&result, expect)) {
  106. fprintf(stderr, "amqp_merge_capabilities incorrect result.\n");
  107. abort();
  108. }
  109. empty_amqp_pool(&pool);
  110. return;
  111. }
  112. int main(void) {
  113. {
  114. amqp_table_t sub_base;
  115. amqp_table_t sub_add;
  116. amqp_table_t sub_expect;
  117. amqp_table_t base;
  118. amqp_table_t add;
  119. amqp_table_t expect;
  120. amqp_table_entry_t sub_base_entries[1];
  121. amqp_table_entry_t sub_add_entries[2];
  122. amqp_table_entry_t sub_expect_entries[2];
  123. amqp_table_entry_t base_entries[3];
  124. amqp_table_entry_t add_entries[3];
  125. amqp_table_entry_t expect_entries[4];
  126. sub_base_entries[0] = amqp_table_construct_utf8_entry("foo", "bar");
  127. sub_base.num_entries =
  128. sizeof(sub_base_entries) / sizeof(amqp_table_entry_t);
  129. sub_base.entries = sub_base_entries;
  130. sub_add_entries[0] = amqp_table_construct_utf8_entry("something", "else");
  131. sub_add_entries[1] = amqp_table_construct_utf8_entry("foo", "baz");
  132. sub_add.num_entries = sizeof(sub_add_entries) / sizeof(amqp_table_entry_t);
  133. sub_add.entries = sub_add_entries;
  134. sub_expect_entries[0] = amqp_table_construct_utf8_entry("foo", "baz");
  135. sub_expect_entries[1] =
  136. amqp_table_construct_utf8_entry("something", "else");
  137. sub_expect.num_entries =
  138. sizeof(sub_expect_entries) / sizeof(amqp_table_entry_t);
  139. sub_expect.entries = sub_expect_entries;
  140. base_entries[0] = amqp_table_construct_utf8_entry("product", "1.0");
  141. base_entries[1] = amqp_table_construct_utf8_entry("nooverride", "yeah");
  142. base_entries[2] = amqp_table_construct_table_entry("props", &sub_base);
  143. base.num_entries = sizeof(base_entries) / sizeof(amqp_table_entry_t);
  144. base.entries = base_entries;
  145. add_entries[0] = amqp_table_construct_bool_entry("bool_entry", 1);
  146. add_entries[1] = amqp_table_construct_utf8_entry("product", "2.0");
  147. add_entries[2] = amqp_table_construct_table_entry("props", &sub_add);
  148. add.num_entries = sizeof(add_entries) / sizeof(amqp_table_entry_t);
  149. add.entries = add_entries;
  150. expect_entries[0] = amqp_table_construct_utf8_entry("product", "2.0"),
  151. expect_entries[1] = amqp_table_construct_utf8_entry("nooverride", "yeah"),
  152. expect_entries[2] = amqp_table_construct_table_entry("props", &sub_expect);
  153. expect_entries[3] = amqp_table_construct_bool_entry("bool_entry", 1);
  154. expect.num_entries = sizeof(expect_entries) / sizeof(amqp_table_entry_t);
  155. expect.entries = expect_entries;
  156. test_merge_capabilities(&base, &add, &expect);
  157. }
  158. fprintf(stderr, "ok\n");
  159. return 0;
  160. }