test_protover.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #ifdef HAVE_RUST
  12. /** This test is disabled on rust builds, because it only exists to test
  13. * internal C functions. */
  14. tt_skip();
  15. done:
  16. ;
  17. #else
  18. char *re_encoded = NULL;
  19. const char *orig = "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900";
  20. smartlist_t *elts = parse_protocol_list(orig);
  21. tt_assert(elts);
  22. tt_int_op(smartlist_len(elts), OP_EQ, 4);
  23. const proto_entry_t *e;
  24. const proto_range_t *r;
  25. e = smartlist_get(elts, 0);
  26. tt_str_op(e->name, OP_EQ, "Foo");
  27. tt_int_op(smartlist_len(e->ranges), OP_EQ, 2);
  28. {
  29. r = smartlist_get(e->ranges, 0);
  30. tt_int_op(r->low, OP_EQ, 1);
  31. tt_int_op(r->high, OP_EQ, 1);
  32. r = smartlist_get(e->ranges, 1);
  33. tt_int_op(r->low, OP_EQ, 3);
  34. tt_int_op(r->high, OP_EQ, 3);
  35. }
  36. e = smartlist_get(elts, 1);
  37. tt_str_op(e->name, OP_EQ, "Bar");
  38. tt_int_op(smartlist_len(e->ranges), OP_EQ, 1);
  39. {
  40. r = smartlist_get(e->ranges, 0);
  41. tt_int_op(r->low, OP_EQ, 3);
  42. tt_int_op(r->high, OP_EQ, 3);
  43. }
  44. e = smartlist_get(elts, 2);
  45. tt_str_op(e->name, OP_EQ, "Baz");
  46. tt_int_op(smartlist_len(e->ranges), OP_EQ, 0);
  47. e = smartlist_get(elts, 3);
  48. tt_str_op(e->name, OP_EQ, "Quux");
  49. tt_int_op(smartlist_len(e->ranges), OP_EQ, 4);
  50. {
  51. r = smartlist_get(e->ranges, 0);
  52. tt_int_op(r->low, OP_EQ, 9);
  53. tt_int_op(r->high, OP_EQ, 12);
  54. r = smartlist_get(e->ranges, 1);
  55. tt_int_op(r->low, OP_EQ, 14);
  56. tt_int_op(r->high, OP_EQ, 14);
  57. r = smartlist_get(e->ranges, 2);
  58. tt_int_op(r->low, OP_EQ, 15);
  59. tt_int_op(r->high, OP_EQ, 16);
  60. r = smartlist_get(e->ranges, 3);
  61. tt_int_op(r->low, OP_EQ, 900);
  62. tt_int_op(r->high, OP_EQ, 900);
  63. }
  64. re_encoded = encode_protocol_list(elts);
  65. tt_assert(re_encoded);
  66. tt_str_op(re_encoded, OP_EQ, orig);
  67. done:
  68. if (elts)
  69. SMARTLIST_FOREACH(elts, proto_entry_t *, ent, proto_entry_free(ent));
  70. smartlist_free(elts);
  71. tor_free(re_encoded);
  72. #endif
  73. }
  74. static void
  75. test_protover_parse_fail(void *arg)
  76. {
  77. (void)arg;
  78. #ifdef HAVE_RUST
  79. /** This test is disabled on rust builds, because it only exists to test
  80. * internal C functions. */
  81. tt_skip();
  82. #else
  83. smartlist_t *elts;
  84. /* random junk */
  85. elts = parse_protocol_list("!!3@*");
  86. tt_ptr_op(elts, OP_EQ, NULL);
  87. /* Missing equals sign in an entry */
  88. elts = parse_protocol_list("Link=4 Haprauxymatyve Desc=9");
  89. tt_ptr_op(elts, OP_EQ, NULL);
  90. /* Missing word. */
  91. elts = parse_protocol_list("Link=4 =3 Desc=9");
  92. tt_ptr_op(elts, OP_EQ, NULL);
  93. /* Broken numbers */
  94. elts = parse_protocol_list("Link=fred");
  95. tt_ptr_op(elts, OP_EQ, NULL);
  96. elts = parse_protocol_list("Link=1,fred");
  97. tt_ptr_op(elts, OP_EQ, NULL);
  98. elts = parse_protocol_list("Link=1,fred,3");
  99. tt_ptr_op(elts, OP_EQ, NULL);
  100. /* Broken range */
  101. elts = parse_protocol_list("Link=1,9-8,3");
  102. tt_ptr_op(elts, OP_EQ, NULL);
  103. #endif
  104. done:
  105. ;
  106. }
  107. static void
  108. test_protover_vote(void *arg)
  109. {
  110. (void) arg;
  111. smartlist_t *lst = smartlist_new();
  112. char *result = protover_compute_vote(lst, 1);
  113. tt_str_op(result, OP_EQ, "");
  114. tor_free(result);
  115. smartlist_add(lst, (void*) "Foo=1-10,500 Bar=1,3-7,8");
  116. result = protover_compute_vote(lst, 1);
  117. tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,500");
  118. tor_free(result);
  119. smartlist_add(lst, (void*) "Quux=123-456,78 Bar=2-6,8 Foo=9");
  120. result = protover_compute_vote(lst, 1);
  121. tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,500 Quux=78,123-456");
  122. tor_free(result);
  123. result = protover_compute_vote(lst, 2);
  124. tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9");
  125. tor_free(result);
  126. done:
  127. tor_free(result);
  128. smartlist_free(lst);
  129. }
  130. static void
  131. test_protover_all_supported(void *arg)
  132. {
  133. (void)arg;
  134. char *msg = NULL;
  135. tt_assert(protover_all_supported(NULL, &msg));
  136. tt_ptr_op(msg, OP_EQ, NULL);
  137. tt_assert(protover_all_supported("", &msg));
  138. tt_ptr_op(msg, OP_EQ, NULL);
  139. // Some things that we do support
  140. tt_assert(protover_all_supported("Link=3-4", &msg));
  141. tt_ptr_op(msg, OP_EQ, NULL);
  142. tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg));
  143. tt_ptr_op(msg, OP_EQ, NULL);
  144. // Some things we don't support
  145. tt_assert(! protover_all_supported("Wombat=9", &msg));
  146. tt_str_op(msg, OP_EQ, "Wombat=9");
  147. tor_free(msg);
  148. tt_assert(! protover_all_supported("Link=999", &msg));
  149. tt_str_op(msg, OP_EQ, "Link=999");
  150. tor_free(msg);
  151. // Mix of things we support and things we don't
  152. tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg));
  153. tt_str_op(msg, OP_EQ, "Wombat=9");
  154. tor_free(msg);
  155. tt_assert(! protover_all_supported("Link=3-999", &msg));
  156. tt_str_op(msg, OP_EQ, "Link=3-999");
  157. tor_free(msg);
  158. done:
  159. tor_free(msg);
  160. }
  161. static void
  162. test_protover_list_supports_protocol_returns_true(void *arg)
  163. {
  164. (void)arg;
  165. const char *protocols = "Link=1";
  166. int is_supported = protocol_list_supports_protocol(protocols, PRT_LINK, 1);
  167. tt_int_op(is_supported, OP_EQ, 1);
  168. done:
  169. ;
  170. }
  171. static void
  172. test_protover_list_supports_protocol_for_unsupported_returns_false(void *arg)
  173. {
  174. (void)arg;
  175. const char *protocols = "Link=1";
  176. int is_supported = protocol_list_supports_protocol(protocols, PRT_LINK, 10);
  177. tt_int_op(is_supported, OP_EQ, 0);
  178. done:
  179. ;
  180. }
  181. static void
  182. test_protover_supports_version(void *arg)
  183. {
  184. (void)arg;
  185. tt_assert(protocol_list_supports_protocol("Link=3-6", PRT_LINK, 3));
  186. tt_assert(protocol_list_supports_protocol("Link=3-6", PRT_LINK, 6));
  187. tt_assert(!protocol_list_supports_protocol("Link=3-6", PRT_LINK, 7));
  188. tt_assert(!protocol_list_supports_protocol("Link=3-6", PRT_LINKAUTH, 3));
  189. tt_assert(!protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  190. PRT_LINKAUTH, 2));
  191. tt_assert(protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  192. PRT_LINKAUTH, 3));
  193. tt_assert(!protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  194. PRT_LINKAUTH, 4));
  195. tt_assert(!protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  196. PRT_LINKAUTH, 4));
  197. tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  198. PRT_LINKAUTH, 3));
  199. tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  200. PRT_LINKAUTH, 2));
  201. tt_assert(!protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  202. PRT_DESC, 2));
  203. done:
  204. ;
  205. }
  206. #define PV_TEST(name, flags) \
  207. { #name, test_protover_ ##name, (flags), NULL, NULL }
  208. struct testcase_t protover_tests[] = {
  209. PV_TEST(parse, 0),
  210. PV_TEST(parse_fail, 0),
  211. PV_TEST(vote, 0),
  212. PV_TEST(all_supported, 0),
  213. PV_TEST(list_supports_protocol_for_unsupported_returns_false, 0),
  214. PV_TEST(list_supports_protocol_returns_true, 0),
  215. PV_TEST(supports_version, 0),
  216. END_OF_TESTCASES
  217. };