test_protover.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define PROTOVER_PRIVATE
  4. #include "orconfig.h"
  5. #include "test.h"
  6. #include "protover.h"
  7. static void
  8. test_protover_parse(void *arg)
  9. {
  10. (void) arg;
  11. char *re_encoded = NULL;
  12. const char *orig = "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900";
  13. smartlist_t *elts = parse_protocol_list(orig);
  14. tt_assert(elts);
  15. tt_int_op(smartlist_len(elts), OP_EQ, 4);
  16. const proto_entry_t *e;
  17. const proto_range_t *r;
  18. e = smartlist_get(elts, 0);
  19. tt_str_op(e->name, OP_EQ, "Foo");
  20. tt_int_op(smartlist_len(e->ranges), OP_EQ, 2);
  21. {
  22. r = smartlist_get(e->ranges, 0);
  23. tt_int_op(r->low, OP_EQ, 1);
  24. tt_int_op(r->high, OP_EQ, 1);
  25. r = smartlist_get(e->ranges, 1);
  26. tt_int_op(r->low, OP_EQ, 3);
  27. tt_int_op(r->high, OP_EQ, 3);
  28. }
  29. e = smartlist_get(elts, 1);
  30. tt_str_op(e->name, OP_EQ, "Bar");
  31. tt_int_op(smartlist_len(e->ranges), OP_EQ, 1);
  32. {
  33. r = smartlist_get(e->ranges, 0);
  34. tt_int_op(r->low, OP_EQ, 3);
  35. tt_int_op(r->high, OP_EQ, 3);
  36. }
  37. e = smartlist_get(elts, 2);
  38. tt_str_op(e->name, OP_EQ, "Baz");
  39. tt_int_op(smartlist_len(e->ranges), OP_EQ, 0);
  40. e = smartlist_get(elts, 3);
  41. tt_str_op(e->name, OP_EQ, "Quux");
  42. tt_int_op(smartlist_len(e->ranges), OP_EQ, 4);
  43. {
  44. r = smartlist_get(e->ranges, 0);
  45. tt_int_op(r->low, OP_EQ, 9);
  46. tt_int_op(r->high, OP_EQ, 12);
  47. r = smartlist_get(e->ranges, 1);
  48. tt_int_op(r->low, OP_EQ, 14);
  49. tt_int_op(r->high, OP_EQ, 14);
  50. r = smartlist_get(e->ranges, 2);
  51. tt_int_op(r->low, OP_EQ, 15);
  52. tt_int_op(r->high, OP_EQ, 16);
  53. r = smartlist_get(e->ranges, 3);
  54. tt_int_op(r->low, OP_EQ, 900);
  55. tt_int_op(r->high, OP_EQ, 900);
  56. }
  57. re_encoded = encode_protocol_list(elts);
  58. tt_assert(re_encoded);
  59. tt_str_op(re_encoded, OP_EQ, orig);
  60. done:
  61. if (elts)
  62. SMARTLIST_FOREACH(elts, proto_entry_t *, ent, proto_entry_free(ent));
  63. smartlist_free(elts);
  64. tor_free(re_encoded);
  65. }
  66. static void
  67. test_protover_vote(void *arg)
  68. {
  69. (void) arg;
  70. smartlist_t *lst = smartlist_new();
  71. char *result = compute_protover_vote(lst, 1);
  72. tt_str_op(result, OP_EQ, "");
  73. tor_free(result);
  74. smartlist_add(lst, (void*) "Foo=1-10,500 Bar=1,3-7,8");
  75. result = compute_protover_vote(lst, 1);
  76. tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,500");
  77. tor_free(result);
  78. smartlist_add(lst, (void*) "Quux=123-456,78 Bar=2-6,8 Foo=9");
  79. result = compute_protover_vote(lst, 1);
  80. tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,500 Quux=78,123-456");
  81. tor_free(result);
  82. result = compute_protover_vote(lst, 2);
  83. tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9");
  84. tor_free(result);
  85. done:
  86. tor_free(result);
  87. smartlist_free(lst);
  88. }
  89. static void
  90. test_protover_all_supported(void *arg)
  91. {
  92. (void)arg;
  93. char *msg = NULL;
  94. tt_assert(protover_all_supported(NULL, &msg));
  95. tt_assert(msg == NULL);
  96. tt_assert(protover_all_supported("", &msg));
  97. tt_assert(msg == NULL);
  98. // Some things that we do support
  99. tt_assert(protover_all_supported("Link=3-4", &msg));
  100. tt_assert(msg == NULL);
  101. tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg));
  102. tt_assert(msg == NULL);
  103. // Some things we don't support
  104. tt_assert(! protover_all_supported("Wombat=9", &msg));
  105. tt_str_op(msg, OP_EQ, "Wombat=9");
  106. tor_free(msg);
  107. tt_assert(! protover_all_supported("Link=999", &msg));
  108. tt_str_op(msg, OP_EQ, "Link=999");
  109. tor_free(msg);
  110. // Mix of things we support and things we don't
  111. tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg));
  112. tt_str_op(msg, OP_EQ, "Wombat=9");
  113. tor_free(msg);
  114. tt_assert(! protover_all_supported("Link=3-999", &msg));
  115. tt_str_op(msg, OP_EQ, "Link=3-999");
  116. tor_free(msg);
  117. done:
  118. tor_free(msg);
  119. }
  120. #define PV_TEST(name, flags) \
  121. { #name, test_protover_ ##name, (flags), NULL, NULL }
  122. struct testcase_t protover_tests[] = {
  123. PV_TEST(parse, 0),
  124. PV_TEST(vote, 0),
  125. PV_TEST(all_supported, 0),
  126. END_OF_TESTCASES
  127. };