test_nodelist.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /* Copyright (c) 2007-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_nodelist.c
  5. * \brief Unit tests for nodelist related functions.
  6. **/
  7. #define NODELIST_PRIVATE
  8. #include "core/or/or.h"
  9. #include "lib/crypt_ops/crypto_rand.h"
  10. #include "feature/nodelist/networkstatus.h"
  11. #include "feature/nodelist/nodefamily.h"
  12. #include "feature/nodelist/nodelist.h"
  13. #include "feature/nodelist/torcert.h"
  14. #include "feature/nodelist/microdesc_st.h"
  15. #include "feature/nodelist/networkstatus_st.h"
  16. #include "feature/nodelist/node_st.h"
  17. #include "feature/nodelist/nodefamily_st.h"
  18. #include "feature/nodelist/routerinfo_st.h"
  19. #include "feature/nodelist/routerstatus_st.h"
  20. #include "test/test.h"
  21. #include "test/log_test_helpers.h"
  22. /** Test the case when node_get_by_id() returns NULL,
  23. * node_get_verbose_nickname_by_id should return the base 16 encoding
  24. * of the id.
  25. */
  26. static void
  27. test_nodelist_node_get_verbose_nickname_by_id_null_node(void *arg)
  28. {
  29. char vname[MAX_VERBOSE_NICKNAME_LEN+1];
  30. const char ID[] = "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  31. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
  32. (void) arg;
  33. /* make sure node_get_by_id returns NULL */
  34. tt_assert(!node_get_by_id(ID));
  35. node_get_verbose_nickname_by_id(ID, vname);
  36. tt_str_op(vname,OP_EQ, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
  37. done:
  38. return;
  39. }
  40. /** For routers without named flag, get_verbose_nickname should return
  41. * "Fingerprint~Nickname"
  42. */
  43. static void
  44. test_nodelist_node_get_verbose_nickname_not_named(void *arg)
  45. {
  46. node_t mock_node;
  47. routerstatus_t mock_rs;
  48. char vname[MAX_VERBOSE_NICKNAME_LEN+1];
  49. (void) arg;
  50. memset(&mock_node, 0, sizeof(node_t));
  51. memset(&mock_rs, 0, sizeof(routerstatus_t));
  52. /* verbose nickname should use ~ instead of = for unnamed routers */
  53. strlcpy(mock_rs.nickname, "TestOR", sizeof(mock_rs.nickname));
  54. mock_node.rs = &mock_rs;
  55. memcpy(mock_node.identity,
  56. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  57. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
  58. DIGEST_LEN);
  59. node_get_verbose_nickname(&mock_node, vname);
  60. tt_str_op(vname,OP_EQ, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~TestOR");
  61. done:
  62. return;
  63. }
  64. /** A node should be considered a directory server if it has an open dirport
  65. * of it accepts tunnelled directory requests.
  66. */
  67. static void
  68. test_nodelist_node_is_dir(void *arg)
  69. {
  70. (void)arg;
  71. routerstatus_t rs;
  72. routerinfo_t ri;
  73. node_t node;
  74. memset(&node, 0, sizeof(node_t));
  75. memset(&rs, 0, sizeof(routerstatus_t));
  76. memset(&ri, 0, sizeof(routerinfo_t));
  77. tt_assert(!node_is_dir(&node));
  78. node.rs = &rs;
  79. tt_assert(!node_is_dir(&node));
  80. rs.is_v2_dir = 1;
  81. tt_assert(node_is_dir(&node));
  82. rs.is_v2_dir = 0;
  83. rs.dir_port = 1;
  84. tt_assert(! node_is_dir(&node));
  85. node.rs = NULL;
  86. tt_assert(!node_is_dir(&node));
  87. node.ri = &ri;
  88. ri.supports_tunnelled_dir_requests = 1;
  89. tt_assert(node_is_dir(&node));
  90. ri.supports_tunnelled_dir_requests = 0;
  91. ri.dir_port = 1;
  92. tt_assert(! node_is_dir(&node));
  93. done:
  94. return;
  95. }
  96. static networkstatus_t *dummy_ns = NULL;
  97. static networkstatus_t *
  98. mock_networkstatus_get_latest_consensus(void)
  99. {
  100. return dummy_ns;
  101. }
  102. static networkstatus_t *
  103. mock_networkstatus_get_latest_consensus_by_flavor(consensus_flavor_t f)
  104. {
  105. tor_assert(f == FLAV_MICRODESC);
  106. return dummy_ns;
  107. }
  108. static void
  109. test_nodelist_ed_id(void *arg)
  110. {
  111. #define N_NODES 5
  112. routerstatus_t *rs[N_NODES];
  113. microdesc_t *md[N_NODES];
  114. routerinfo_t *ri[N_NODES];
  115. networkstatus_t *ns;
  116. int i;
  117. (void)arg;
  118. ns = tor_malloc_zero(sizeof(networkstatus_t));
  119. ns->flavor = FLAV_MICRODESC;
  120. ns->routerstatus_list = smartlist_new();
  121. dummy_ns = ns;
  122. MOCK(networkstatus_get_latest_consensus,
  123. mock_networkstatus_get_latest_consensus);
  124. MOCK(networkstatus_get_latest_consensus_by_flavor,
  125. mock_networkstatus_get_latest_consensus_by_flavor);
  126. /* Make a bunch of dummy objects that we can play around with. Only set the
  127. necessary fields */
  128. for (i = 0; i < N_NODES; ++i) {
  129. rs[i] = tor_malloc_zero(sizeof(*rs[i]));
  130. md[i] = tor_malloc_zero(sizeof(*md[i]));
  131. ri[i] = tor_malloc_zero(sizeof(*ri[i]));
  132. crypto_rand(md[i]->digest, sizeof(md[i]->digest));
  133. md[i]->ed25519_identity_pkey = tor_malloc(sizeof(ed25519_public_key_t));
  134. crypto_rand((char*)md[i]->ed25519_identity_pkey,
  135. sizeof(ed25519_public_key_t));
  136. crypto_rand(rs[i]->identity_digest, sizeof(rs[i]->identity_digest));
  137. memcpy(ri[i]->cache_info.identity_digest, rs[i]->identity_digest,
  138. DIGEST_LEN);
  139. memcpy(rs[i]->descriptor_digest, md[i]->digest, DIGEST256_LEN);
  140. ri[i]->cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  141. memcpy(&ri[i]->cache_info.signing_key_cert->signing_key,
  142. md[i]->ed25519_identity_pkey, sizeof(ed25519_public_key_t));
  143. if (i < 3)
  144. smartlist_add(ns->routerstatus_list, rs[i]);
  145. }
  146. tt_int_op(0, OP_EQ, smartlist_len(nodelist_get_list()));
  147. nodelist_set_consensus(ns);
  148. tt_int_op(3, OP_EQ, smartlist_len(nodelist_get_list()));
  149. /* No Ed25519 info yet, so nothing has an ED id. */
  150. tt_ptr_op(NULL, OP_EQ, node_get_by_ed25519_id(md[0]->ed25519_identity_pkey));
  151. /* Register the first one by md, then look it up. */
  152. node_t *n = nodelist_add_microdesc(md[0]);
  153. tt_ptr_op(n, OP_EQ, node_get_by_ed25519_id(md[0]->ed25519_identity_pkey));
  154. /* Register the second by ri, then look it up. */
  155. routerinfo_t *ri_old = NULL;
  156. n = nodelist_set_routerinfo(ri[1], &ri_old);
  157. tt_ptr_op(n, OP_EQ, node_get_by_ed25519_id(md[1]->ed25519_identity_pkey));
  158. tt_ptr_op(ri_old, OP_EQ, NULL);
  159. /* Register it by md too. */
  160. node_t *n2 = nodelist_add_microdesc(md[1]);
  161. tt_ptr_op(n2, OP_EQ, n);
  162. tt_ptr_op(n, OP_EQ, node_get_by_ed25519_id(md[1]->ed25519_identity_pkey));
  163. /* Register the 4th by ri only -- we never put it into the networkstatus,
  164. * so it has to be independent */
  165. node_t *n3 = nodelist_set_routerinfo(ri[3], &ri_old);
  166. tt_ptr_op(n3, OP_EQ, node_get_by_ed25519_id(md[3]->ed25519_identity_pkey));
  167. tt_ptr_op(ri_old, OP_EQ, NULL);
  168. tt_int_op(4, OP_EQ, smartlist_len(nodelist_get_list()));
  169. /* Register the 5th by ri only, and rewrite its ed25519 pubkey to be
  170. * the same as the 4th, to test the duplicate ed25519 key logging in
  171. * nodelist.c */
  172. memcpy(md[4]->ed25519_identity_pkey, md[3]->ed25519_identity_pkey,
  173. sizeof(ed25519_public_key_t));
  174. memcpy(&ri[4]->cache_info.signing_key_cert->signing_key,
  175. md[3]->ed25519_identity_pkey, sizeof(ed25519_public_key_t));
  176. setup_capture_of_logs(LOG_NOTICE);
  177. node_t *n4 = nodelist_set_routerinfo(ri[4], &ri_old);
  178. tt_ptr_op(ri_old, OP_EQ, NULL);
  179. tt_int_op(5, OP_EQ, smartlist_len(nodelist_get_list()));
  180. tt_ptr_op(n4, OP_NE, node_get_by_ed25519_id(md[3]->ed25519_identity_pkey));
  181. tt_ptr_op(n3, OP_EQ, node_get_by_ed25519_id(md[3]->ed25519_identity_pkey));
  182. expect_log_msg_containing("Reused ed25519_id");
  183. done:
  184. teardown_capture_of_logs();
  185. for (i = 0; i < N_NODES; ++i) {
  186. tor_free(rs[i]);
  187. tor_free(md[i]->ed25519_identity_pkey);
  188. tor_free(md[i]);
  189. tor_free(ri[i]->cache_info.signing_key_cert);
  190. tor_free(ri[i]);
  191. }
  192. smartlist_clear(ns->routerstatus_list);
  193. networkstatus_vote_free(ns);
  194. UNMOCK(networkstatus_get_latest_consensus);
  195. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  196. #undef N_NODES
  197. }
  198. static void
  199. test_nodelist_nodefamily(void *arg)
  200. {
  201. (void)arg;
  202. /* hex ID digests */
  203. const char h1[] = "5B435D6869206861206C65207363617270652070";
  204. const char h2[] = "75C3B220616E6461726520696E206769726F2061";
  205. const char h3[] = "2074726F766172206461206D616E67696172652C";
  206. const char h4[] = "206D656E747265206E6F6E2076616C65206C2769";
  207. const char h5[] = "6E766572736F2E202D2D5072696D6F204C657669";
  208. /* binary ID digests */
  209. uint8_t d1[DIGEST_LEN], d2[DIGEST_LEN], d3[DIGEST_LEN], d4[DIGEST_LEN],
  210. d5[DIGEST_LEN];
  211. base16_decode((char*)d1, sizeof(d1), h1, strlen(h1));
  212. base16_decode((char*)d2, sizeof(d2), h2, strlen(h2));
  213. base16_decode((char*)d3, sizeof(d3), h3, strlen(h3));
  214. base16_decode((char*)d4, sizeof(d4), h4, strlen(h4));
  215. base16_decode((char*)d5, sizeof(d5), h5, strlen(h5));
  216. char *enc=NULL, *enc2=NULL;
  217. nodefamily_t *nf1 = NULL;
  218. nodefamily_t *nf2 = NULL;
  219. nodefamily_t *nf3 = NULL;
  220. enc = nodefamily_format(NULL);
  221. tt_str_op(enc, OP_EQ, "");
  222. tor_free(enc);
  223. /* Make sure that sorting and de-duplication work. */
  224. tor_asprintf(&enc, "$%s hello", h1);
  225. nf1 = nodefamily_parse(enc, NULL, 0);
  226. tt_assert(nf1);
  227. tor_free(enc);
  228. tor_asprintf(&enc, "hello hello $%s hello", h1);
  229. nf2 = nodefamily_parse(enc, NULL, 0);
  230. tt_assert(nf2);
  231. tt_ptr_op(nf1, OP_EQ, nf2);
  232. tor_free(enc);
  233. tor_asprintf(&enc, "%s $%s hello", h1, h1);
  234. nf3 = nodefamily_parse(enc, NULL, 0);
  235. tt_assert(nf3);
  236. tt_ptr_op(nf1, OP_EQ, nf3);
  237. tor_free(enc);
  238. tt_assert(nodefamily_contains_rsa_id(nf1, d1));
  239. tt_assert(! nodefamily_contains_rsa_id(nf1, d2));
  240. tt_assert(nodefamily_contains_nickname(nf1, "hello"));
  241. tt_assert(nodefamily_contains_nickname(nf1, "HELLO"));
  242. tt_assert(! nodefamily_contains_nickname(nf1, "goodbye"));
  243. tt_int_op(nf1->refcnt, OP_EQ, 3);
  244. nodefamily_free(nf3);
  245. tt_int_op(nf1->refcnt, OP_EQ, 2);
  246. /* Try parsing with a provided self RSA digest. */
  247. nf3 = nodefamily_parse("hello ", d1, 0);
  248. tt_assert(nf3);
  249. tt_ptr_op(nf1, OP_EQ, nf3);
  250. /* Do we get the expected result when we re-encode? */
  251. tor_asprintf(&enc, "$%s hello", h1);
  252. enc2 = nodefamily_format(nf1);
  253. tt_str_op(enc2, OP_EQ, enc);
  254. tor_free(enc2);
  255. tor_free(enc);
  256. /* Make sure that we get a different result if we give a different digest. */
  257. nodefamily_free(nf3);
  258. tor_asprintf(&enc, "hello $%s hello", h3);
  259. nf3 = nodefamily_parse(enc, NULL, 0);
  260. tt_assert(nf3);
  261. tt_ptr_op(nf1, OP_NE, nf3);
  262. tor_free(enc);
  263. tt_assert(nodefamily_contains_rsa_id(nf3, d3));
  264. tt_assert(! nodefamily_contains_rsa_id(nf3, d2));
  265. tt_assert(! nodefamily_contains_rsa_id(nf3, d1));
  266. tt_assert(nodefamily_contains_nickname(nf3, "hello"));
  267. tt_assert(! nodefamily_contains_nickname(nf3, "goodbye"));
  268. nodefamily_free(nf1);
  269. nodefamily_free(nf2);
  270. nodefamily_free(nf3);
  271. /* Try one with several digests, all with nicknames appended, in different
  272. formats. */
  273. tor_asprintf(&enc, "%s $%s $%s=res $%s~ist", h1, h2, h3, h4);
  274. nf1 = nodefamily_parse(enc, d5, 0);
  275. tt_assert(nf1);
  276. tt_assert(nodefamily_contains_rsa_id(nf1, d1));
  277. tt_assert(nodefamily_contains_rsa_id(nf1, d2));
  278. tt_assert(nodefamily_contains_rsa_id(nf1, d3));
  279. tt_assert(nodefamily_contains_rsa_id(nf1, d4));
  280. tt_assert(nodefamily_contains_rsa_id(nf1, d5));
  281. /* Nicknames aren't preserved when ids are present, since node naming is
  282. * deprecated */
  283. tt_assert(! nodefamily_contains_nickname(nf3, "res"));
  284. tor_free(enc);
  285. tor_asprintf(&enc, "$%s $%s $%s $%s $%s", h4, h3, h1, h5, h2);
  286. enc2 = nodefamily_format(nf1);
  287. tt_str_op(enc, OP_EQ, enc2);
  288. tor_free(enc);
  289. tor_free(enc2);
  290. /* Try ones where we parse the empty string. */
  291. nf2 = nodefamily_parse("", NULL, 0);
  292. nf3 = nodefamily_parse("", d4, 0);
  293. tt_assert(nf2);
  294. tt_assert(nf3);
  295. tt_ptr_op(nf2, OP_NE, nf3);
  296. tt_assert(! nodefamily_contains_rsa_id(nf2, d4));
  297. tt_assert(nodefamily_contains_rsa_id(nf3, d4));
  298. tt_assert(! nodefamily_contains_rsa_id(nf2, d5));
  299. tt_assert(! nodefamily_contains_rsa_id(nf3, d5));
  300. tt_assert(! nodefamily_contains_nickname(nf2, "fred"));
  301. tt_assert(! nodefamily_contains_nickname(nf3, "bosco"));
  302. /* The NULL family should contain nothing. */
  303. tt_assert(! nodefamily_contains_rsa_id(NULL, d4));
  304. tt_assert(! nodefamily_contains_rsa_id(NULL, d5));
  305. done:
  306. tor_free(enc);
  307. tor_free(enc2);
  308. nodefamily_free(nf1);
  309. nodefamily_free(nf2);
  310. nodefamily_free(nf3);
  311. nodefamily_free_all();
  312. }
  313. static void
  314. test_nodelist_nodefamily_parse_err(void *arg)
  315. {
  316. (void)arg;
  317. nodefamily_t *nf1 = NULL;
  318. char *enc = NULL;
  319. const char *semibogus =
  320. "sdakljfdslkfjdsaklfjdkl9sdf " // too long for nickname
  321. "$jkASDFLkjsadfjhkl " // not hex
  322. "$7468696e67732d696e2d7468656d73656c766573 " // ok
  323. "reticulatogranulate "// ok
  324. "$73656d69616e7468726f706f6c6f676963616c6c79 " // too long for hex
  325. "$616273656e746d696e6465646e6573736573" // too short for hex
  326. ;
  327. setup_capture_of_logs(LOG_WARN);
  328. // We only get two items when we parse this.
  329. for (int reject = 0; reject <= 1; ++reject) {
  330. for (int log_at_warn = 0; log_at_warn <= 1; ++log_at_warn) {
  331. unsigned flags = log_at_warn ? NF_WARN_MALFORMED : 0;
  332. flags |= reject ? NF_REJECT_MALFORMED : 0;
  333. nf1 = nodefamily_parse(semibogus, NULL, flags);
  334. if (reject) {
  335. tt_assert(nf1 == NULL);
  336. } else {
  337. tt_assert(nf1);
  338. enc = nodefamily_format(nf1);
  339. tt_str_op(enc, OP_EQ,
  340. "$7468696E67732D696E2D7468656D73656C766573 "
  341. "reticulatogranulate");
  342. tor_free(enc);
  343. }
  344. if (log_at_warn) {
  345. expect_log_msg_containing("$616273656e746d696e6465646e6573736573");
  346. expect_log_msg_containing("sdakljfdslkfjdsaklfjdkl9sdf");
  347. } else {
  348. tt_int_op(mock_saved_log_n_entries(), OP_EQ, 0);
  349. }
  350. mock_clean_saved_logs();
  351. }
  352. }
  353. done:
  354. tor_free(enc);
  355. nodefamily_free(nf1);
  356. teardown_capture_of_logs();
  357. }
  358. static const node_t *
  359. mock_node_get_by_id(const char *id)
  360. {
  361. if (fast_memeq(id, "!!!!!!!!!!!!!!!!!!!!", DIGEST_LEN))
  362. return NULL;
  363. // use tor_free, not node_free.
  364. node_t *fake_node = tor_malloc_zero(sizeof(node_t));
  365. memcpy(fake_node->identity, id, DIGEST_LEN);
  366. return fake_node;
  367. }
  368. static const node_t *
  369. mock_node_get_by_nickname(const char *nn, unsigned flags)
  370. {
  371. (void)flags;
  372. if (!strcmp(nn, "nonesuch"))
  373. return NULL;
  374. // use tor_free, not node_free.
  375. node_t *fake_node = tor_malloc_zero(sizeof(node_t));
  376. strlcpy(fake_node->identity, nn, DIGEST_LEN);
  377. return fake_node;
  378. }
  379. static void
  380. test_nodelist_nodefamily_lookup(void *arg)
  381. {
  382. (void)arg;
  383. MOCK(node_get_by_nickname, mock_node_get_by_nickname);
  384. MOCK(node_get_by_id, mock_node_get_by_id);
  385. smartlist_t *sl = smartlist_new();
  386. nodefamily_t *nf1 = NULL;
  387. char *mem_op_hex_tmp = NULL;
  388. // 'null' is allowed.
  389. nodefamily_add_nodes_to_smartlist(NULL, sl);
  390. tt_int_op(smartlist_len(sl), OP_EQ, 0);
  391. // Try a real family
  392. nf1 = nodefamily_parse("$EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE "
  393. "$2121212121212121212121212121212121212121 "
  394. "$3333333333333333333333333333333333333333 "
  395. "erewhon nonesuch", NULL, 0);
  396. tt_assert(nf1);
  397. nodefamily_add_nodes_to_smartlist(nf1, sl);
  398. // There were 5 elements; 2 were dropped because the mocked lookup failed.
  399. tt_int_op(smartlist_len(sl), OP_EQ, 3);
  400. const node_t *n = smartlist_get(sl, 0);
  401. test_memeq_hex(n->identity, "3333333333333333333333333333333333333333");
  402. n = smartlist_get(sl, 1);
  403. test_memeq_hex(n->identity, "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
  404. n = smartlist_get(sl, 2);
  405. tt_str_op(n->identity, OP_EQ, "erewhon");
  406. done:
  407. UNMOCK(node_get_by_nickname);
  408. UNMOCK(node_get_by_id);
  409. SMARTLIST_FOREACH(sl, node_t *, fake_node, tor_free(fake_node));
  410. smartlist_free(sl);
  411. nodefamily_free(nf1);
  412. tor_free(mem_op_hex_tmp);
  413. }
  414. static void
  415. test_nodelist_nickname_matches(void *arg)
  416. {
  417. (void)arg;
  418. node_t mock_node;
  419. routerstatus_t mock_rs;
  420. memset(&mock_node, 0, sizeof(mock_node));
  421. memset(&mock_rs, 0, sizeof(mock_rs));
  422. strlcpy(mock_rs.nickname, "evilgeniuses", sizeof(mock_rs.nickname));
  423. mock_node.rs = &mock_rs;
  424. memcpy(mock_node.identity, ".forabettertomorrow.", DIGEST_LEN);
  425. #define match(x) tt_assert(node_nickname_matches(&mock_node, (x)))
  426. #define no_match(x) tt_assert(! node_nickname_matches(&mock_node, (x)))
  427. match("evilgeniuses");
  428. match("EvilGeniuses");
  429. match("EvilGeniuses");
  430. match("2e666f7261626574746572746f6d6f72726f772e");
  431. match("2E666F7261626574746572746F6D6F72726F772E");
  432. match("$2e666f7261626574746572746f6d6f72726f772e");
  433. match("$2E666F7261626574746572746F6D6F72726F772E");
  434. match("$2E666F7261626574746572746F6D6F72726F772E~evilgeniuses");
  435. match("$2E666F7261626574746572746F6D6F72726F772E~EVILGENIUSES");
  436. no_match("evilgenius");
  437. no_match("evilgeniuseses");
  438. no_match("evil.genius");
  439. no_match("$2E666F7261626574746572746F6D6F72726FFFFF");
  440. no_match("2E666F7261626574746572746F6D6F72726FFFFF");
  441. no_match("$2E666F7261626574746572746F6D6F72726F772E~fred");
  442. no_match("$2E666F7261626574746572746F6D6F72726F772E=EVILGENIUSES");
  443. done:
  444. ;
  445. }
  446. static void
  447. test_nodelist_node_nodefamily(void *arg)
  448. {
  449. (void)arg;
  450. node_t mock_node1;
  451. routerstatus_t mock_rs;
  452. microdesc_t mock_md;
  453. node_t mock_node2;
  454. routerinfo_t mock_ri;
  455. smartlist_t *nodes=smartlist_new();
  456. memset(&mock_node1, 0, sizeof(mock_node1));
  457. memset(&mock_node2, 0, sizeof(mock_node2));
  458. memset(&mock_rs, 0, sizeof(mock_rs));
  459. memset(&mock_md, 0, sizeof(mock_md));
  460. memset(&mock_ri, 0, sizeof(mock_ri));
  461. mock_node1.rs = &mock_rs;
  462. mock_node1.md = &mock_md;
  463. mock_node2.ri = &mock_ri;
  464. strlcpy(mock_rs.nickname, "nodeone", sizeof(mock_rs.nickname));
  465. mock_ri.nickname = tor_strdup("nodetwo");
  466. memcpy(mock_node1.identity, "NodeOneNode1NodeOne1", DIGEST_LEN);
  467. memcpy(mock_node2.identity, "SecondNodeWe'reTestn", DIGEST_LEN);
  468. // empty families.
  469. tt_assert(! node_family_contains(&mock_node1, &mock_node2));
  470. tt_assert(! node_family_contains(&mock_node2, &mock_node1));
  471. // Families contain nodes, but not these nodes
  472. mock_ri.declared_family = smartlist_new();
  473. smartlist_add(mock_ri.declared_family, (char*)"NodeThree");
  474. mock_md.family = nodefamily_parse("NodeFour", NULL, 0);
  475. tt_assert(! node_family_contains(&mock_node1, &mock_node2));
  476. tt_assert(! node_family_contains(&mock_node2, &mock_node1));
  477. // Families contain one another.
  478. smartlist_add(mock_ri.declared_family, (char*)
  479. "4e6f64654f6e654e6f6465314e6f64654f6e6531");
  480. tt_assert(! node_family_contains(&mock_node1, &mock_node2));
  481. tt_assert(node_family_contains(&mock_node2, &mock_node1));
  482. nodefamily_free(mock_md.family);
  483. mock_md.family = nodefamily_parse(
  484. "NodeFour "
  485. "5365636f6e644e6f64655765277265546573746e", NULL, 0);
  486. tt_assert(node_family_contains(&mock_node1, &mock_node2));
  487. tt_assert(node_family_contains(&mock_node2, &mock_node1));
  488. // Try looking up families now.
  489. MOCK(node_get_by_nickname, mock_node_get_by_nickname);
  490. MOCK(node_get_by_id, mock_node_get_by_id);
  491. node_lookup_declared_family(nodes, &mock_node1);
  492. tt_int_op(smartlist_len(nodes), OP_EQ, 2);
  493. const node_t *n = smartlist_get(nodes, 0);
  494. tt_mem_op(n->identity, OP_EQ, "SecondNodeWe'reTestn", DIGEST_LEN);
  495. n = smartlist_get(nodes, 1);
  496. tt_str_op(n->identity, OP_EQ, "nodefour");
  497. // free, try the other one.
  498. SMARTLIST_FOREACH(nodes, node_t *, x, tor_free(x));
  499. smartlist_clear(nodes);
  500. node_lookup_declared_family(nodes, &mock_node2);
  501. tt_int_op(smartlist_len(nodes), OP_EQ, 2);
  502. n = smartlist_get(nodes, 0);
  503. // This gets a truncated hex hex ID since it was looked up by name
  504. tt_str_op(n->identity, OP_EQ, "NodeThree");
  505. n = smartlist_get(nodes, 1);
  506. tt_str_op(n->identity, OP_EQ, "4e6f64654f6e654e6f6");
  507. done:
  508. UNMOCK(node_get_by_nickname);
  509. UNMOCK(node_get_by_id);
  510. smartlist_free(mock_ri.declared_family);
  511. nodefamily_free(mock_md.family);
  512. tor_free(mock_ri.nickname);
  513. // use tor_free, these aren't real nodes
  514. SMARTLIST_FOREACH(nodes, node_t *, x, tor_free(x));
  515. smartlist_free(nodes);
  516. }
  517. static void
  518. test_nodelist_nodefamily_canonicalize(void *arg)
  519. {
  520. (void)arg;
  521. char *c = NULL;
  522. c = nodefamily_canonicalize("", NULL, 0);
  523. tt_str_op(c, OP_EQ, "");
  524. tor_free(c);
  525. uint8_t own_id[20];
  526. memset(own_id, 0, sizeof(own_id));
  527. c = nodefamily_canonicalize(
  528. "alice BOB caroL %potrzebie !!!@#@# "
  529. "$bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb=fred "
  530. "ffffffffffffffffffffffffffffffffffffffff "
  531. "$cccccccccccccccccccccccccccccccccccccccc ", own_id, 0);
  532. tt_str_op(c, OP_EQ,
  533. "!!!@#@# "
  534. "$0000000000000000000000000000000000000000 "
  535. "$BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB "
  536. "$CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
  537. "$FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF "
  538. "%potrzebie "
  539. "alice bob carol");
  540. done:
  541. tor_free(c);
  542. }
  543. #define NODE(name, flags) \
  544. { #name, test_nodelist_##name, (flags), NULL, NULL }
  545. struct testcase_t nodelist_tests[] = {
  546. NODE(node_get_verbose_nickname_by_id_null_node, TT_FORK),
  547. NODE(node_get_verbose_nickname_not_named, TT_FORK),
  548. NODE(node_is_dir, TT_FORK),
  549. NODE(ed_id, TT_FORK),
  550. NODE(nodefamily, TT_FORK),
  551. NODE(nodefamily_parse_err, TT_FORK),
  552. NODE(nodefamily_lookup, TT_FORK),
  553. NODE(nickname_matches, 0),
  554. NODE(node_nodefamily, TT_FORK),
  555. NODE(nodefamily_canonicalize, 0),
  556. END_OF_TESTCASES
  557. };