test_protover.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Copyright (c) 2016-2017, 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_parse_fail(void *arg)
  68. {
  69. (void)arg;
  70. smartlist_t *elts;
  71. /* random junk */
  72. elts = parse_protocol_list("!!3@*");
  73. tt_assert(elts == NULL);
  74. /* Missing equals sign in an entry */
  75. elts = parse_protocol_list("Link=4 Haprauxymatyve Desc=9");
  76. tt_assert(elts == NULL);
  77. /* Missing word. */
  78. elts = parse_protocol_list("Link=4 =3 Desc=9");
  79. tt_assert(elts == NULL);
  80. /* Broken numbers */
  81. elts = parse_protocol_list("Link=fred");
  82. tt_assert(elts == NULL);
  83. elts = parse_protocol_list("Link=1,fred");
  84. tt_assert(elts == NULL);
  85. elts = parse_protocol_list("Link=1,fred,3");
  86. tt_assert(elts == NULL);
  87. /* Broken range */
  88. elts = parse_protocol_list("Link=1,9-8,3");
  89. tt_assert(elts == NULL);
  90. done:
  91. ;
  92. }
  93. static void
  94. test_protover_vote(void *arg)
  95. {
  96. (void) arg;
  97. smartlist_t *lst = smartlist_new();
  98. char *result = protover_compute_vote(lst, 1);
  99. tt_str_op(result, OP_EQ, "");
  100. tor_free(result);
  101. smartlist_add(lst, (void*) "Foo=1-10,500 Bar=1,3-7,8");
  102. result = protover_compute_vote(lst, 1);
  103. tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,500");
  104. tor_free(result);
  105. smartlist_add(lst, (void*) "Quux=123-456,78 Bar=2-6,8 Foo=9");
  106. result = protover_compute_vote(lst, 1);
  107. tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,500 Quux=78,123-456");
  108. tor_free(result);
  109. result = protover_compute_vote(lst, 2);
  110. tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9");
  111. tor_free(result);
  112. done:
  113. tor_free(result);
  114. smartlist_free(lst);
  115. }
  116. static void
  117. test_protover_all_supported(void *arg)
  118. {
  119. (void)arg;
  120. char *msg = NULL;
  121. tt_assert(protover_all_supported(NULL, &msg));
  122. tt_assert(msg == NULL);
  123. tt_assert(protover_all_supported("", &msg));
  124. tt_assert(msg == NULL);
  125. // Some things that we do support
  126. tt_assert(protover_all_supported("Link=3-4", &msg));
  127. tt_assert(msg == NULL);
  128. tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg));
  129. tt_assert(msg == NULL);
  130. // Some things we don't support
  131. tt_assert(! protover_all_supported("Wombat=9", &msg));
  132. tt_str_op(msg, OP_EQ, "Wombat=9");
  133. tor_free(msg);
  134. tt_assert(! protover_all_supported("Link=999", &msg));
  135. tt_str_op(msg, OP_EQ, "Link=999");
  136. tor_free(msg);
  137. // Mix of things we support and things we don't
  138. tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg));
  139. tt_str_op(msg, OP_EQ, "Wombat=9");
  140. tor_free(msg);
  141. tt_assert(! protover_all_supported("Link=3-999", &msg));
  142. tt_str_op(msg, OP_EQ, "Link=3-999");
  143. tor_free(msg);
  144. done:
  145. tor_free(msg);
  146. }
  147. #define PV_TEST(name, flags) \
  148. { #name, test_protover_ ##name, (flags), NULL, NULL }
  149. struct testcase_t protover_tests[] = {
  150. PV_TEST(parse, 0),
  151. PV_TEST(parse_fail, 0),
  152. PV_TEST(vote, 0),
  153. PV_TEST(all_supported, 0),
  154. END_OF_TESTCASES
  155. };