test_protover.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #include "or.h"
  8. #include "connection_or.h"
  9. static void
  10. test_protover_parse(void *arg)
  11. {
  12. (void) arg;
  13. #ifdef HAVE_RUST
  14. /** This test is disabled on rust builds, because it only exists to test
  15. * internal C functions. */
  16. tt_skip();
  17. done:
  18. ;
  19. #else
  20. char *re_encoded = NULL;
  21. const char *orig = "Foo=1,3 Bar=3 Baz= Quux=9-12,14,15-16,900";
  22. smartlist_t *elts = parse_protocol_list(orig);
  23. tt_assert(elts);
  24. tt_int_op(smartlist_len(elts), OP_EQ, 4);
  25. const proto_entry_t *e;
  26. const proto_range_t *r;
  27. e = smartlist_get(elts, 0);
  28. tt_str_op(e->name, OP_EQ, "Foo");
  29. tt_int_op(smartlist_len(e->ranges), OP_EQ, 2);
  30. {
  31. r = smartlist_get(e->ranges, 0);
  32. tt_int_op(r->low, OP_EQ, 1);
  33. tt_int_op(r->high, OP_EQ, 1);
  34. r = smartlist_get(e->ranges, 1);
  35. tt_int_op(r->low, OP_EQ, 3);
  36. tt_int_op(r->high, OP_EQ, 3);
  37. }
  38. e = smartlist_get(elts, 1);
  39. tt_str_op(e->name, OP_EQ, "Bar");
  40. tt_int_op(smartlist_len(e->ranges), OP_EQ, 1);
  41. {
  42. r = smartlist_get(e->ranges, 0);
  43. tt_int_op(r->low, OP_EQ, 3);
  44. tt_int_op(r->high, OP_EQ, 3);
  45. }
  46. e = smartlist_get(elts, 2);
  47. tt_str_op(e->name, OP_EQ, "Baz");
  48. tt_int_op(smartlist_len(e->ranges), OP_EQ, 0);
  49. e = smartlist_get(elts, 3);
  50. tt_str_op(e->name, OP_EQ, "Quux");
  51. tt_int_op(smartlist_len(e->ranges), OP_EQ, 4);
  52. {
  53. r = smartlist_get(e->ranges, 0);
  54. tt_int_op(r->low, OP_EQ, 9);
  55. tt_int_op(r->high, OP_EQ, 12);
  56. r = smartlist_get(e->ranges, 1);
  57. tt_int_op(r->low, OP_EQ, 14);
  58. tt_int_op(r->high, OP_EQ, 14);
  59. r = smartlist_get(e->ranges, 2);
  60. tt_int_op(r->low, OP_EQ, 15);
  61. tt_int_op(r->high, OP_EQ, 16);
  62. r = smartlist_get(e->ranges, 3);
  63. tt_int_op(r->low, OP_EQ, 900);
  64. tt_int_op(r->high, OP_EQ, 900);
  65. }
  66. re_encoded = encode_protocol_list(elts);
  67. tt_assert(re_encoded);
  68. tt_str_op(re_encoded, OP_EQ, orig);
  69. done:
  70. if (elts)
  71. SMARTLIST_FOREACH(elts, proto_entry_t *, ent, proto_entry_free(ent));
  72. smartlist_free(elts);
  73. tor_free(re_encoded);
  74. #endif
  75. }
  76. static void
  77. test_protover_parse_fail(void *arg)
  78. {
  79. (void)arg;
  80. #ifdef HAVE_RUST
  81. /** This test is disabled on rust builds, because it only exists to test
  82. * internal C functions. */
  83. tt_skip();
  84. #else
  85. smartlist_t *elts;
  86. /* random junk */
  87. elts = parse_protocol_list("!!3@*");
  88. tt_ptr_op(elts, OP_EQ, NULL);
  89. /* Missing equals sign in an entry */
  90. elts = parse_protocol_list("Link=4 Haprauxymatyve Desc=9");
  91. tt_ptr_op(elts, OP_EQ, NULL);
  92. /* Missing word. */
  93. elts = parse_protocol_list("Link=4 =3 Desc=9");
  94. tt_ptr_op(elts, OP_EQ, NULL);
  95. /* Broken numbers */
  96. elts = parse_protocol_list("Link=fred");
  97. tt_ptr_op(elts, OP_EQ, NULL);
  98. elts = parse_protocol_list("Link=1,fred");
  99. tt_ptr_op(elts, OP_EQ, NULL);
  100. elts = parse_protocol_list("Link=1,fred,3");
  101. tt_ptr_op(elts, OP_EQ, NULL);
  102. /* Broken range */
  103. elts = parse_protocol_list("Link=1,9-8,3");
  104. tt_ptr_op(elts, OP_EQ, NULL);
  105. #endif
  106. done:
  107. ;
  108. }
  109. static void
  110. test_protover_vote(void *arg)
  111. {
  112. (void) arg;
  113. smartlist_t *lst = smartlist_new();
  114. char *result = protover_compute_vote(lst, 1);
  115. tt_str_op(result, OP_EQ, "");
  116. tor_free(result);
  117. smartlist_add(lst, (void*) "Foo=1-10,500 Bar=1,3-7,8");
  118. result = protover_compute_vote(lst, 1);
  119. tt_str_op(result, OP_EQ, "Bar=1,3-8 Foo=1-10,500");
  120. tor_free(result);
  121. smartlist_add(lst, (void*) "Quux=123-456,78 Bar=2-6,8 Foo=9");
  122. result = protover_compute_vote(lst, 1);
  123. tt_str_op(result, OP_EQ, "Bar=1-8 Foo=1-10,500 Quux=78,123-456");
  124. tor_free(result);
  125. result = protover_compute_vote(lst, 2);
  126. tt_str_op(result, OP_EQ, "Bar=3-6,8 Foo=9");
  127. tor_free(result);
  128. done:
  129. tor_free(result);
  130. smartlist_free(lst);
  131. }
  132. static void
  133. test_protover_all_supported(void *arg)
  134. {
  135. (void)arg;
  136. char *msg = NULL;
  137. tt_assert(protover_all_supported(NULL, &msg));
  138. tt_ptr_op(msg, OP_EQ, NULL);
  139. tt_assert(protover_all_supported("", &msg));
  140. tt_ptr_op(msg, OP_EQ, NULL);
  141. // Some things that we do support
  142. tt_assert(protover_all_supported("Link=3-4", &msg));
  143. tt_ptr_op(msg, OP_EQ, NULL);
  144. tt_assert(protover_all_supported("Link=3-4 Desc=2", &msg));
  145. tt_ptr_op(msg, OP_EQ, NULL);
  146. // Some things we don't support
  147. tt_assert(! protover_all_supported("Wombat=9", &msg));
  148. tt_str_op(msg, OP_EQ, "Wombat=9");
  149. tor_free(msg);
  150. tt_assert(! protover_all_supported("Link=999", &msg));
  151. tt_str_op(msg, OP_EQ, "Link=999");
  152. tor_free(msg);
  153. // Mix of things we support and things we don't
  154. tt_assert(! protover_all_supported("Link=3-4 Wombat=9", &msg));
  155. tt_str_op(msg, OP_EQ, "Wombat=9");
  156. tor_free(msg);
  157. tt_assert(! protover_all_supported("Link=3-999", &msg));
  158. tt_str_op(msg, OP_EQ, "Link=3-999");
  159. tor_free(msg);
  160. done:
  161. tor_free(msg);
  162. }
  163. static void
  164. test_protover_list_supports_protocol_returns_true(void *arg)
  165. {
  166. (void)arg;
  167. const char *protocols = "Link=1";
  168. int is_supported = protocol_list_supports_protocol(protocols, PRT_LINK, 1);
  169. tt_int_op(is_supported, OP_EQ, 1);
  170. done:
  171. ;
  172. }
  173. static void
  174. test_protover_list_supports_protocol_for_unsupported_returns_false(void *arg)
  175. {
  176. (void)arg;
  177. const char *protocols = "Link=1";
  178. int is_supported = protocol_list_supports_protocol(protocols, PRT_LINK, 10);
  179. tt_int_op(is_supported, OP_EQ, 0);
  180. done:
  181. ;
  182. }
  183. static void
  184. test_protover_supports_version(void *arg)
  185. {
  186. (void)arg;
  187. tt_assert(protocol_list_supports_protocol("Link=3-6", PRT_LINK, 3));
  188. tt_assert(protocol_list_supports_protocol("Link=3-6", PRT_LINK, 6));
  189. tt_assert(!protocol_list_supports_protocol("Link=3-6", PRT_LINK, 7));
  190. tt_assert(!protocol_list_supports_protocol("Link=3-6", PRT_LINKAUTH, 3));
  191. tt_assert(!protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  192. PRT_LINKAUTH, 2));
  193. tt_assert(protocol_list_supports_protocol("Link=4-6 LinkAuth=3",
  194. PRT_LINKAUTH, 3));
  195. tt_assert(!protocol_list_supports_protocol("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, 4));
  199. tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  200. PRT_LINKAUTH, 3));
  201. tt_assert(protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  202. PRT_LINKAUTH, 2));
  203. tt_assert(!protocol_list_supports_protocol_or_later("Link=4-6 LinkAuth=3",
  204. PRT_DESC, 2));
  205. done:
  206. ;
  207. }
  208. /* This could be MAX_PROTOCOLS_TO_EXPAND, but that's not exposed by protover */
  209. #define MAX_PROTOCOLS_TO_TEST 1024
  210. /* LinkAuth and Relay protocol versions.
  211. * Hard-coded here, because they are not in the code, or not exposed in the
  212. * headers. */
  213. #define PROTOVER_LINKAUTH_V1 1
  214. #define PROTOVER_LINKAUTH_V3 3
  215. #define PROTOVER_RELAY_V1 1
  216. #define PROTOVER_RELAY_V2 2
  217. /* Highest supported HSv2 introduce protocol version.
  218. * Hard-coded here, because it does not appear anywhere in the code.
  219. * It's not clear if we actually support version 2, see #25068. */
  220. #define PROTOVER_HSINTRO_V2 3
  221. /* HSv2 Rend and HSDir protocol versions.
  222. * Hard-coded here, because they do not appear anywhere in the code. */
  223. #define PROTOVER_HS_RENDEZVOUS_POINT_V2 1
  224. #define PROTOVER_HSDIR_V2 1
  225. /* DirCache, Desc, Microdesc, and Cons protocol versions.
  226. * Hard-coded here, because they do not appear anywhere in the code. */
  227. #define PROTOVER_DIRCACHE_V1 1
  228. #define PROTOVER_DIRCACHE_V2 2
  229. #define PROTOVER_DESC_V1 1
  230. #define PROTOVER_DESC_V2 2
  231. #define PROTOVER_MICRODESC_V1 1
  232. #define PROTOVER_MICRODESC_V2 2
  233. #define PROTOVER_CONS_V1 1
  234. #define PROTOVER_CONS_V2 2
  235. /* Make sure we haven't forgotten any supported protocols */
  236. static void
  237. test_protover_supported_protocols(void *arg)
  238. {
  239. (void)arg;
  240. const char *supported_protocols = protover_get_supported_protocols();
  241. /* Test for new Link in the code, that hasn't been added to supported
  242. * protocols */
  243. tt_assert(protocol_list_supports_protocol(supported_protocols,
  244. PRT_LINK,
  245. MAX_LINK_PROTO));
  246. for (uint16_t i = 0; i < MAX_PROTOCOLS_TO_TEST; i++) {
  247. if (is_or_protocol_version_known(i)) {
  248. tt_assert(protocol_list_supports_protocol(supported_protocols,
  249. PRT_LINK,
  250. i));
  251. }
  252. }
  253. /* Legacy LinkAuth does not appear anywhere in the code. */
  254. tt_assert(protocol_list_supports_protocol(supported_protocols,
  255. PRT_LINKAUTH,
  256. PROTOVER_LINKAUTH_V1));
  257. /* Latest LinkAuth is not exposed in the headers. */
  258. tt_assert(protocol_list_supports_protocol(supported_protocols,
  259. PRT_LINKAUTH,
  260. PROTOVER_LINKAUTH_V3));
  261. /* Is there any way to test for new LinkAuth? */
  262. /* Relay protovers do not appear anywhere in the code. */
  263. tt_assert(protocol_list_supports_protocol(supported_protocols,
  264. PRT_RELAY,
  265. PROTOVER_RELAY_V1));
  266. tt_assert(protocol_list_supports_protocol(supported_protocols,
  267. PRT_RELAY,
  268. PROTOVER_RELAY_V2));
  269. /* Is there any way to test for new Relay? */
  270. /* We could test legacy HSIntro by calling rend_service_update_descriptor(),
  271. * and checking the protocols field. But that's unlikely to change, so
  272. * we just use a hard-coded value. */
  273. tt_assert(protocol_list_supports_protocol(supported_protocols,
  274. PRT_HSINTRO,
  275. PROTOVER_HSINTRO_V2));
  276. /* Test for HSv3 HSIntro */
  277. tt_assert(protocol_list_supports_protocol(supported_protocols,
  278. PRT_HSINTRO,
  279. PROTOVER_HS_INTRO_V3));
  280. /* Is there any way to test for new HSIntro? */
  281. /* Legacy HSRend does not appear anywhere in the code. */
  282. tt_assert(protocol_list_supports_protocol(supported_protocols,
  283. PRT_HSREND,
  284. PROTOVER_HS_RENDEZVOUS_POINT_V2));
  285. /* Test for HSv3 HSRend */
  286. tt_assert(protocol_list_supports_protocol(supported_protocols,
  287. PRT_HSREND,
  288. PROTOVER_HS_RENDEZVOUS_POINT_V3));
  289. /* Is there any way to test for new HSRend? */
  290. /* Legacy HSDir does not appear anywhere in the code. */
  291. tt_assert(protocol_list_supports_protocol(supported_protocols,
  292. PRT_HSDIR,
  293. PROTOVER_HSDIR_V2));
  294. /* Test for HSv3 HSDir */
  295. tt_assert(protocol_list_supports_protocol(supported_protocols,
  296. PRT_HSDIR,
  297. PROTOVER_HSDIR_V3));
  298. /* Is there any way to test for new HSDir? */
  299. /* No DirCache versions appear anywhere in the code. */
  300. tt_assert(protocol_list_supports_protocol(supported_protocols,
  301. PRT_DIRCACHE,
  302. PROTOVER_DIRCACHE_V1));
  303. tt_assert(protocol_list_supports_protocol(supported_protocols,
  304. PRT_DIRCACHE,
  305. PROTOVER_DIRCACHE_V2));
  306. /* Is there any way to test for new DirCache? */
  307. /* No Desc versions appear anywhere in the code. */
  308. tt_assert(protocol_list_supports_protocol(supported_protocols,
  309. PRT_DESC,
  310. PROTOVER_DESC_V1));
  311. tt_assert(protocol_list_supports_protocol(supported_protocols,
  312. PRT_DESC,
  313. PROTOVER_DESC_V2));
  314. /* Is there any way to test for new Desc? */
  315. /* No Microdesc versions appear anywhere in the code. */
  316. tt_assert(protocol_list_supports_protocol(supported_protocols,
  317. PRT_MICRODESC,
  318. PROTOVER_MICRODESC_V1));
  319. tt_assert(protocol_list_supports_protocol(supported_protocols,
  320. PRT_MICRODESC,
  321. PROTOVER_MICRODESC_V2));
  322. /* Is there any way to test for new Microdesc? */
  323. /* No Cons versions appear anywhere in the code. */
  324. tt_assert(protocol_list_supports_protocol(supported_protocols,
  325. PRT_CONS,
  326. PROTOVER_CONS_V1));
  327. tt_assert(protocol_list_supports_protocol(supported_protocols,
  328. PRT_CONS,
  329. PROTOVER_CONS_V2));
  330. /* Is there any way to test for new Cons? */
  331. done:
  332. ;
  333. }
  334. #define PV_TEST(name, flags) \
  335. { #name, test_protover_ ##name, (flags), NULL, NULL }
  336. struct testcase_t protover_tests[] = {
  337. PV_TEST(parse, 0),
  338. PV_TEST(parse_fail, 0),
  339. PV_TEST(vote, 0),
  340. PV_TEST(all_supported, 0),
  341. PV_TEST(list_supports_protocol_for_unsupported_returns_false, 0),
  342. PV_TEST(list_supports_protocol_returns_true, 0),
  343. PV_TEST(supports_version, 0),
  344. PV_TEST(supported_protocols, 0),
  345. END_OF_TESTCASES
  346. };