Merge branch 'maint-0.4.8'
[tor.git] / src / test / test_routerset.c
blobaff6bcb9c2f03f85a1bc302717eca463cbbc567d
1 /* Copyright (c) 2014-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define ROUTERSET_PRIVATE
6 #include "core/or/or.h"
7 #include "core/or/policies.h"
8 #include "feature/dirparse/policy_parse.h"
9 #include "feature/nodelist/nodelist.h"
10 #include "feature/nodelist/routerset.h"
11 #include "lib/geoip/geoip.h"
13 #include "core/or/addr_policy_st.h"
14 #include "core/or/extend_info_st.h"
15 #include "feature/nodelist/node_st.h"
16 #include "feature/nodelist/routerinfo_st.h"
17 #include "feature/nodelist/routerstatus_st.h"
19 #include "test/test.h"
22 * Functional (blackbox) test to determine that each member of the routerset
23 * is non-NULL
26 static void
27 test_rset_new(void *arg)
29 routerset_t *rs;
30 (void)arg;
32 rs = routerset_new();
34 tt_ptr_op(rs, OP_NE, NULL);
35 tt_ptr_op(rs->list, OP_NE, NULL);
36 tt_ptr_op(rs->names, OP_NE, NULL);
37 tt_ptr_op(rs->digests, OP_NE, NULL);
38 tt_ptr_op(rs->policies, OP_NE, NULL);
39 tt_ptr_op(rs->country_names, OP_NE, NULL);
41 done:
42 routerset_free(rs);
46 * Functional test to strip the braces from a "{xx}" country code string.
49 static void
50 test_rset_get_countryname(void *arg)
52 const char *input;
53 char *name;
54 (void)arg;
56 /* strlen(c) < 4 */
57 input = "xxx";
58 name = routerset_get_countryname(input);
59 tt_ptr_op(name, OP_EQ, NULL);
60 tor_free(name);
62 /* c[0] != '{' */
63 input = "xxx}";
64 name = routerset_get_countryname(input);
65 tt_ptr_op(name, OP_EQ, NULL);
66 tor_free(name);
68 /* c[3] != '}' */
69 input = "{xxx";
70 name = routerset_get_countryname(input);
71 tt_ptr_op(name, OP_EQ, NULL);
72 tor_free(name);
74 /* tor_strlower */
75 input = "{XX}";
76 name = routerset_get_countryname(input);
77 tt_str_op(name, OP_EQ, "xx");
78 tor_free(name);
80 input = "{xx}";
81 name = routerset_get_countryname(input);
82 tt_str_op(name, OP_EQ, "xx");
83 done:
84 tor_free(name);
88 * Structural (whitebox) test for routerset_refresh_counties, when the GeoIP DB
89 * is not loaded.
92 static int rset_refresh_geoip_not_loaded_geoip_is_loaded(sa_family_t family);
93 static int rset_refresh_geoip_not_loaded_geoip_is_loaded_called = 0;
94 static int rset_refresh_geoip_not_loaded_geoip_get_n_countries(void);
95 static int rset_refresh_geoip_not_loaded_geoip_get_n_countries_called = 0;
97 static void
98 test_rset_refresh_geoip_not_loaded(void *arg)
100 routerset_t *set = routerset_new();
101 (void)arg;
103 MOCK(geoip_is_loaded,
104 rset_refresh_geoip_not_loaded_geoip_is_loaded);
105 MOCK(geoip_get_n_countries,
106 rset_refresh_geoip_not_loaded_geoip_get_n_countries);
108 routerset_refresh_countries(set);
110 tt_ptr_op(set->countries, OP_EQ, NULL);
111 tt_int_op(set->n_countries, OP_EQ, 0);
112 tt_int_op(rset_refresh_geoip_not_loaded_geoip_is_loaded_called, OP_EQ, 1);
113 tt_int_op(rset_refresh_geoip_not_loaded_geoip_get_n_countries_called,
114 OP_EQ, 0);
116 done:
117 UNMOCK(geoip_is_loaded);
118 UNMOCK(geoip_get_n_countries);
119 routerset_free(set);
122 static int
123 rset_refresh_geoip_not_loaded_geoip_is_loaded(sa_family_t family)
125 (void)family;
126 rset_refresh_geoip_not_loaded_geoip_is_loaded_called++;
128 return 0;
131 static int
132 rset_refresh_geoip_not_loaded_geoip_get_n_countries(void)
134 rset_refresh_geoip_not_loaded_geoip_get_n_countries_called++;
136 return 0;
140 * Structural test for routerset_refresh_counties, when there are no countries.
143 static int rset_refresh_no_countries_geoip_is_loaded(sa_family_t family);
144 static int rset_refresh_no_countries_geoip_is_loaded_called = 0;
145 static int rset_refresh_no_countries_geoip_get_n_countries(void);
146 static int rset_refresh_no_countries_geoip_get_n_countries_called = 0;
147 static country_t rset_refresh_no_countries_geoip_get_country(
148 const char *country);
149 static int rset_refresh_no_countries_geoip_get_country_called = 0;
151 static void
152 test_rset_refresh_no_countries(void *arg)
154 routerset_t *set = routerset_new();
155 (void)arg;
157 MOCK(geoip_is_loaded,
158 rset_refresh_no_countries_geoip_is_loaded);
159 MOCK(geoip_get_n_countries,
160 rset_refresh_no_countries_geoip_get_n_countries);
161 MOCK(geoip_get_country,
162 rset_refresh_no_countries_geoip_get_country);
164 routerset_refresh_countries(set);
166 tt_ptr_op(set->countries, OP_NE, NULL);
167 tt_int_op(set->n_countries, OP_EQ, 1);
168 tt_int_op((unsigned int)(*set->countries), OP_EQ, 0);
169 tt_int_op(rset_refresh_no_countries_geoip_is_loaded_called, OP_EQ, 1);
170 tt_int_op(rset_refresh_no_countries_geoip_get_n_countries_called, OP_EQ, 1);
171 tt_int_op(rset_refresh_no_countries_geoip_get_country_called, OP_EQ, 0);
173 done:
174 UNMOCK(geoip_is_loaded);
175 UNMOCK(geoip_get_n_countries);
176 UNMOCK(geoip_get_country);
177 routerset_free(set);
180 static int
181 rset_refresh_no_countries_geoip_is_loaded(sa_family_t family)
183 (void)family;
184 rset_refresh_no_countries_geoip_is_loaded_called++;
186 return 1;
189 static int
190 rset_refresh_no_countries_geoip_get_n_countries(void)
192 rset_refresh_no_countries_geoip_get_n_countries_called++;
194 return 1;
197 static country_t
198 rset_refresh_no_countries_geoip_get_country(const char *countrycode)
200 (void)countrycode;
201 rset_refresh_no_countries_geoip_get_country_called++;
203 return 1;
207 * Structural test for routerset_refresh_counties, with one valid country.
210 static int rset_refresh_one_valid_country_geoip_is_loaded(sa_family_t family);
211 static int rset_refresh_one_valid_country_geoip_is_loaded_called = 0;
212 static int rset_refresh_one_valid_country_geoip_get_n_countries(void);
213 static int rset_refresh_one_valid_country_geoip_get_n_countries_called = 0;
214 static country_t rset_refresh_one_valid_country_geoip_get_country(
215 const char *country);
216 static int rset_refresh_one_valid_country_geoip_get_country_called = 0;
218 static void
219 test_rset_refresh_one_valid_country(void *arg)
221 routerset_t *set = routerset_new();
222 (void)arg;
224 MOCK(geoip_is_loaded,
225 rset_refresh_one_valid_country_geoip_is_loaded);
226 MOCK(geoip_get_n_countries,
227 rset_refresh_one_valid_country_geoip_get_n_countries);
228 MOCK(geoip_get_country,
229 rset_refresh_one_valid_country_geoip_get_country);
230 smartlist_add(set->country_names, tor_strndup("foo", 3));
232 routerset_refresh_countries(set);
234 tt_ptr_op(set->countries, OP_NE, NULL);
235 tt_int_op(set->n_countries, OP_EQ, 2);
236 tt_int_op(rset_refresh_one_valid_country_geoip_is_loaded_called, OP_EQ, 1);
237 tt_int_op(rset_refresh_one_valid_country_geoip_get_n_countries_called,
238 OP_EQ, 1);
239 tt_int_op(rset_refresh_one_valid_country_geoip_get_country_called, OP_EQ, 1);
240 tt_int_op((unsigned int)(*set->countries), OP_NE, 0);
242 done:
243 UNMOCK(geoip_is_loaded);
244 UNMOCK(geoip_get_n_countries);
245 UNMOCK(geoip_get_country);
246 routerset_free(set);
249 static int
250 rset_refresh_one_valid_country_geoip_is_loaded(sa_family_t family)
252 (void)family;
253 rset_refresh_one_valid_country_geoip_is_loaded_called++;
255 return 1;
258 static int
259 rset_refresh_one_valid_country_geoip_get_n_countries(void)
261 rset_refresh_one_valid_country_geoip_get_n_countries_called++;
263 return 2;
266 static country_t
267 rset_refresh_one_valid_country_geoip_get_country(const char *countrycode)
269 (void)countrycode;
270 rset_refresh_one_valid_country_geoip_get_country_called++;
272 return 1;
276 * Structural test for routerset_refresh_counties, with one invalid
277 * country code..
280 static int rset_refresh_one_invalid_country_geoip_is_loaded(
281 sa_family_t family);
282 static int rset_refresh_one_invalid_country_geoip_is_loaded_called = 0;
283 static int rset_refresh_one_invalid_country_geoip_get_n_countries(void);
284 static int rset_refresh_one_invalid_country_geoip_get_n_countries_called = 0;
285 static country_t rset_refresh_one_invalid_country_geoip_get_country(
286 const char *country);
287 static int rset_refresh_one_invalid_country_geoip_get_country_called = 0;
289 static void
290 test_rset_refresh_one_invalid_country(void *arg)
292 routerset_t *set = routerset_new();
293 (void)arg;
295 MOCK(geoip_is_loaded,
296 rset_refresh_one_invalid_country_geoip_is_loaded);
297 MOCK(geoip_get_n_countries,
298 rset_refresh_one_invalid_country_geoip_get_n_countries);
299 MOCK(geoip_get_country,
300 rset_refresh_one_invalid_country_geoip_get_country);
301 smartlist_add(set->country_names, tor_strndup("foo", 3));
303 routerset_refresh_countries(set);
305 tt_ptr_op(set->countries, OP_NE, NULL);
306 tt_int_op(set->n_countries, OP_EQ, 2);
307 tt_int_op(rset_refresh_one_invalid_country_geoip_is_loaded_called, OP_EQ, 1);
308 tt_int_op(rset_refresh_one_invalid_country_geoip_get_n_countries_called,
309 OP_EQ, 1);
310 tt_int_op(rset_refresh_one_invalid_country_geoip_get_country_called,
311 OP_EQ, 1);
312 tt_int_op((unsigned int)(*set->countries), OP_EQ, 0);
314 done:
315 UNMOCK(geoip_is_loaded);
316 UNMOCK(geoip_get_n_countries);
317 UNMOCK(geoip_get_country);
318 routerset_free(set);
321 static int
322 rset_refresh_one_invalid_country_geoip_is_loaded(sa_family_t family)
324 (void)family;
325 rset_refresh_one_invalid_country_geoip_is_loaded_called++;
327 return 1;
330 static int
331 rset_refresh_one_invalid_country_geoip_get_n_countries(void)
333 rset_refresh_one_invalid_country_geoip_get_n_countries_called++;
335 return 2;
338 static country_t
339 rset_refresh_one_invalid_country_geoip_get_country(const char *countrycode)
341 (void)countrycode;
342 rset_refresh_one_invalid_country_geoip_get_country_called++;
344 return -1;
348 * Functional test, with a malformed string to parse.
351 static void
352 test_rset_parse_malformed(void *arg)
354 routerset_t *set = routerset_new();
355 const char *s = "_";
356 int r;
357 (void)arg;
359 r = routerset_parse(set, s, "");
361 tt_int_op(r, OP_EQ, -1);
363 done:
364 routerset_free(set);
368 * Functional test for routerset_parse, that routerset_parse returns 0
369 * on a valid hexdigest entry.
372 static void
373 test_rset_parse_valid_hexdigest(void *arg)
375 routerset_t *set;
376 const char *s;
377 int r;
378 (void)arg;
380 set = routerset_new();
381 s = "$0000000000000000000000000000000000000000";
382 r = routerset_parse(set, s, "");
383 tt_int_op(r, OP_EQ, 0);
384 tt_int_op(digestmap_isempty(set->digests), OP_NE, 1);
386 done:
387 routerset_free(set);
391 * Functional test for routerset_parse, when given a valid nickname as input.
394 static void
395 test_rset_parse_valid_nickname(void *arg)
397 routerset_t *set;
398 const char *s;
399 int r;
400 (void)arg;
402 set = routerset_new();
403 s = "fred";
404 r = routerset_parse(set, s, "");
405 tt_int_op(r, OP_EQ, 0);
406 tt_int_op(strmap_isempty(set->names), OP_NE, 1);
408 done:
409 routerset_free(set);
413 * Functional test for routerset_parse, when given a valid countryname.
416 static void
417 test_rset_parse_get_countryname(void *arg)
419 routerset_t *set;
420 const char *s;
421 int r;
422 (void)arg;
424 set = routerset_new();
425 s = "{cc}";
426 r = routerset_parse(set, s, "");
427 tt_int_op(r, OP_EQ, 0);
428 tt_int_op(smartlist_len(set->country_names), OP_NE, 0);
430 done:
431 routerset_free(set);
435 * Structural test for routerset_parse, when given a valid wildcard policy.
438 static addr_policy_t * rset_parse_policy_wildcard_parse_item_from_string(
439 const char *s, int assume_action, int *malformed_list);
440 static int rset_parse_policy_wildcard_parse_item_from_string_called = 0;
442 static addr_policy_t *rset_parse_policy_wildcard_mock_addr_policy;
444 static void
445 test_rset_parse_policy_wildcard(void *arg)
447 routerset_t *set;
448 const char *s;
449 int r;
450 (void)arg;
452 MOCK(router_parse_addr_policy_item_from_string,
453 rset_parse_policy_wildcard_parse_item_from_string);
454 rset_parse_policy_wildcard_mock_addr_policy =
455 tor_malloc_zero(sizeof(addr_policy_t));
457 set = routerset_new();
458 s = "*";
459 r = routerset_parse(set, s, "");
460 tt_int_op(r, OP_EQ, 0);
461 tt_int_op(smartlist_len(set->policies), OP_NE, 0);
462 tt_int_op(rset_parse_policy_wildcard_parse_item_from_string_called,
463 OP_EQ, 1);
465 done:
466 routerset_free(set);
469 addr_policy_t *
470 rset_parse_policy_wildcard_parse_item_from_string(const char *s,
471 int assume_action,
472 int *malformed_list)
474 (void)s;
475 (void)assume_action;
476 (void)malformed_list;
477 rset_parse_policy_wildcard_parse_item_from_string_called++;
479 return rset_parse_policy_wildcard_mock_addr_policy;
483 * Structural test for routerset_parse, when given a valid IPv4 address
484 * literal policy.
487 static addr_policy_t * rset_parse_policy_ipv4_parse_item_from_string(
488 const char *s, int assume_action, int *bogus);
489 static int rset_parse_policy_ipv4_parse_item_from_string_called = 0;
491 static addr_policy_t *rset_parse_policy_ipv4_mock_addr_policy;
493 static void
494 test_rset_parse_policy_ipv4(void *arg)
496 routerset_t *set;
497 const char *s;
498 int r;
499 (void)arg;
501 MOCK(router_parse_addr_policy_item_from_string,
502 rset_parse_policy_ipv4_parse_item_from_string);
503 rset_parse_policy_ipv4_mock_addr_policy =
504 tor_malloc_zero(sizeof(addr_policy_t));
506 set = routerset_new();
507 s = "127.0.0.1";
508 r = routerset_parse(set, s, "");
509 tt_int_op(r, OP_EQ, 0);
510 tt_int_op(smartlist_len(set->policies), OP_NE, 0);
511 tt_int_op(rset_parse_policy_ipv4_parse_item_from_string_called, OP_EQ, 1);
513 done:
514 routerset_free(set);
517 addr_policy_t *
518 rset_parse_policy_ipv4_parse_item_from_string(
519 const char *s, int assume_action,
520 int *bogus)
522 (void)s;
523 (void)assume_action;
524 rset_parse_policy_ipv4_parse_item_from_string_called++;
525 *bogus = 0;
527 return rset_parse_policy_ipv4_mock_addr_policy;
531 * Structural test for routerset_parse, when given a valid IPv6 address
532 * literal policy.
535 static addr_policy_t * rset_parse_policy_ipv6_parse_item_from_string(
536 const char *s, int assume_action, int *bad);
537 static int rset_parse_policy_ipv6_parse_item_from_string_called = 0;
539 static addr_policy_t *rset_parse_policy_ipv6_mock_addr_policy;
541 static void
542 test_rset_parse_policy_ipv6(void *arg)
544 routerset_t *set;
545 const char *s;
546 int r;
547 (void)arg;
549 MOCK(router_parse_addr_policy_item_from_string,
550 rset_parse_policy_ipv6_parse_item_from_string);
551 rset_parse_policy_ipv6_mock_addr_policy =
552 tor_malloc_zero(sizeof(addr_policy_t));
554 set = routerset_new();
555 s = "::1";
556 r = routerset_parse(set, s, "");
557 tt_int_op(r, OP_EQ, 0);
558 tt_int_op(smartlist_len(set->policies), OP_NE, 0);
559 tt_int_op(rset_parse_policy_ipv6_parse_item_from_string_called, OP_EQ, 1);
561 done:
562 routerset_free(set);
565 addr_policy_t *
566 rset_parse_policy_ipv6_parse_item_from_string(const char *s,
567 int assume_action, int *bad)
569 (void)s;
570 (void)assume_action;
571 rset_parse_policy_ipv6_parse_item_from_string_called++;
572 *bad = 0;
574 return rset_parse_policy_ipv6_mock_addr_policy;
578 * Structural test for routerset_union, when given a bad source argument.
581 static smartlist_t * rset_union_source_bad_smartlist_new(void);
582 static int rset_union_source_bad_smartlist_new_called = 0;
584 static void
585 test_rset_union_source_bad(void *arg)
587 routerset_t *set, *bad_set;
588 (void)arg;
590 set = routerset_new();
591 bad_set = routerset_new();
592 smartlist_free(bad_set->list);
593 bad_set->list = NULL;
595 MOCK(smartlist_new,
596 rset_union_source_bad_smartlist_new);
598 routerset_union(set, NULL);
599 tt_int_op(rset_union_source_bad_smartlist_new_called, OP_EQ, 0);
601 routerset_union(set, bad_set);
602 tt_int_op(rset_union_source_bad_smartlist_new_called, OP_EQ, 0);
604 done:
605 UNMOCK(smartlist_new);
606 routerset_free(set);
608 /* Just recreate list, so we can simply use routerset_free. */
609 bad_set->list = smartlist_new();
610 routerset_free(bad_set);
613 static smartlist_t *
614 rset_union_source_bad_smartlist_new(void)
616 rset_union_source_bad_smartlist_new_called++;
618 return NULL;
622 * Functional test for routerset_union.
625 static void
626 test_rset_union_one(void *arg)
628 routerset_t *src = routerset_new();
629 routerset_t *tgt;
630 (void)arg;
632 tgt = routerset_new();
633 smartlist_add_strdup(src->list, "{xx}");
634 routerset_union(tgt, src);
636 tt_int_op(smartlist_len(tgt->list), OP_NE, 0);
638 done:
639 routerset_free(src);
640 routerset_free(tgt);
644 * Functional tests for routerset_is_list.
647 static void
648 test_rset_is_list(void *arg)
650 routerset_t *set;
651 addr_policy_t *policy;
652 int is_list;
653 (void)arg;
655 /* len(set->country_names) == 0, len(set->policies) == 0 */
656 set = routerset_new();
657 is_list = routerset_is_list(set);
658 routerset_free(set);
659 set = NULL;
660 tt_int_op(is_list, OP_NE, 0);
662 /* len(set->country_names) != 0, len(set->policies) == 0 */
663 set = routerset_new();
664 smartlist_add(set->country_names, tor_strndup("foo", 3));
665 is_list = routerset_is_list(set);
666 routerset_free(set);
667 set = NULL;
668 tt_int_op(is_list, OP_EQ, 0);
670 /* len(set->country_names) == 0, len(set->policies) != 0 */
671 set = routerset_new();
672 policy = tor_malloc_zero(sizeof(addr_policy_t));
673 smartlist_add(set->policies, (void *)policy);
674 is_list = routerset_is_list(set);
675 routerset_free(set);
676 set = NULL;
677 tt_int_op(is_list, OP_EQ, 0);
679 /* len(set->country_names) != 0, len(set->policies) != 0 */
680 set = routerset_new();
681 smartlist_add(set->country_names, tor_strndup("foo", 3));
682 policy = tor_malloc_zero(sizeof(addr_policy_t));
683 smartlist_add(set->policies, (void *)policy);
684 is_list = routerset_is_list(set);
685 routerset_free(set);
686 set = NULL;
687 tt_int_op(is_list, OP_EQ, 0);
689 done:
694 * Functional tests for routerset_needs_geoip.
697 static void
698 test_rset_needs_geoip(void *arg)
700 routerset_t *set;
701 int needs_geoip;
702 (void)arg;
704 set = NULL;
705 needs_geoip = routerset_needs_geoip(set);
706 tt_int_op(needs_geoip, OP_EQ, 0);
708 set = routerset_new();
709 needs_geoip = routerset_needs_geoip(set);
710 routerset_free(set);
711 tt_int_op(needs_geoip, OP_EQ, 0);
712 set = NULL;
714 set = routerset_new();
715 smartlist_add(set->country_names, tor_strndup("xx", 2));
716 needs_geoip = routerset_needs_geoip(set);
717 routerset_free(set);
718 set = NULL;
719 tt_int_op(needs_geoip, OP_NE, 0);
721 done:
726 * Functional tests for routerset_is_empty.
729 static void
730 test_rset_is_empty(void *arg)
732 routerset_t *set = NULL;
733 int is_empty;
734 (void)arg;
736 is_empty = routerset_is_empty(set);
737 tt_int_op(is_empty, OP_NE, 0);
739 set = routerset_new();
740 is_empty = routerset_is_empty(set);
741 routerset_free(set);
742 set = NULL;
743 tt_int_op(is_empty, OP_NE, 0);
745 set = routerset_new();
746 smartlist_add_strdup(set->list, "{xx}");
747 is_empty = routerset_is_empty(set);
748 routerset_free(set);
749 set = NULL;
750 tt_int_op(is_empty, OP_EQ, 0);
752 done:
757 * Functional test for routerset_contains, when given a NULL set or the
758 * set has a NULL list.
761 static void
762 test_rset_contains_null_set_or_list(void *arg)
764 routerset_t *set = NULL;
765 int contains;
766 (void)arg;
768 contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
770 tt_int_op(contains, OP_EQ, 0);
772 set = tor_malloc_zero(sizeof(routerset_t));
773 set->list = NULL;
774 contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
775 tor_free(set);
776 tt_int_op(contains, OP_EQ, 0);
778 done:
783 * Functional test for routerset_contains, when given a valid routerset but a
784 * NULL nickname.
787 static void
788 test_rset_contains_null_nickname(void *arg)
790 routerset_t *set = routerset_new();
791 char *nickname = NULL;
792 int contains;
793 (void)arg;
795 contains = routerset_contains(set, NULL, 0, nickname, NULL, 0);
796 routerset_free(set);
798 tt_int_op(contains, OP_EQ, 0);
800 done:
805 * Functional test for routerset_contains, when given a valid routerset
806 * and the nickname is in the routerset.
809 static void
810 test_rset_contains_nickname(void *arg)
812 routerset_t *set = routerset_new();
813 const char *nickname;
814 int contains;
815 (void)arg;
817 nickname = "Foo"; /* This tests the lowercase comparison as well. */
818 strmap_set_lc(set->names, nickname, (void *)1);
819 contains = routerset_contains(set, NULL, 0, nickname, NULL, 0);
820 routerset_free(set);
822 tt_int_op(contains, OP_EQ, 4);
823 done:
828 * Functional test for routerset_contains, when given a valid routerset
829 * and the nickname is not in the routerset.
832 static void
833 test_rset_contains_no_nickname(void *arg)
835 routerset_t *set = routerset_new();
836 int contains;
837 (void)arg;
839 strmap_set_lc(set->names, "bar", (void *)1);
840 contains = routerset_contains(set, NULL, 0, "foo", NULL, 0);
841 routerset_free(set);
843 tt_int_op(contains, OP_EQ, 0);
844 done:
849 * Functional test for routerset_contains, when given a valid routerset
850 * and the digest is contained in the routerset.
853 static void
854 test_rset_contains_digest(void *arg)
856 routerset_t *set = routerset_new();
857 int contains;
858 uint8_t foo[20] = { 2, 3, 4 };
859 (void)arg;
861 digestmap_set(set->digests, (const char*)foo, (void *)1);
862 contains = routerset_contains(set, NULL, 0, NULL, (const char*)foo, 0);
863 routerset_free(set);
865 tt_int_op(contains, OP_EQ, 4);
866 done:
871 * Functional test for routerset_contains, when given a valid routerset
872 * and the digest is not contained in the routerset.
875 static void
876 test_rset_contains_no_digest(void *arg)
878 routerset_t *set = routerset_new();
879 int contains;
880 uint8_t bar[20] = { 9, 10, 11, 55 };
881 uint8_t foo[20] = { 1, 2, 3, 4};
882 (void)arg;
884 digestmap_set(set->digests, (const char*)bar, (void *)1);
885 contains = routerset_contains(set, NULL, 0, NULL, (const char*)foo, 0);
886 routerset_free(set);
888 tt_int_op(contains, OP_EQ, 0);
889 done:
894 * Functional test for routerset_contains, when given a valid routerset
895 * and the digest is NULL.
898 static void
899 test_rset_contains_null_digest(void *arg)
901 routerset_t *set = routerset_new();
902 int contains;
903 uint8_t bar[20] = { 9, 10, 11, 55 };
904 (void)arg;
906 digestmap_set(set->digests, (const char*)bar, (void *)1);
907 contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
908 routerset_free(set);
910 tt_int_op(contains, OP_EQ, 0);
911 done:
916 * Structural test for routerset_contains, when given a valid routerset
917 * and the address is rejected by policy.
920 static addr_policy_result_t rset_contains_addr_cmp_addr_to_policy(
921 const tor_addr_t *addr, uint16_t port,
922 const smartlist_t *policy);
923 static int rset_contains_addr_cmp_addr_to_policy_called = 0;
925 static tor_addr_t MOCK_TOR_ADDR;
926 #define MOCK_TOR_ADDR_PTR (&MOCK_TOR_ADDR)
928 static void
929 test_rset_contains_addr(void *arg)
931 routerset_t *set = routerset_new();
932 tor_addr_t *addr = MOCK_TOR_ADDR_PTR;
933 int contains;
934 (void)arg;
936 MOCK(compare_tor_addr_to_addr_policy,
937 rset_contains_addr_cmp_addr_to_policy);
939 contains = routerset_contains(set, addr, 0, NULL, NULL, 0);
940 routerset_free(set);
942 tt_int_op(rset_contains_addr_cmp_addr_to_policy_called, OP_EQ, 1);
943 tt_int_op(contains, OP_EQ, 3);
945 done:
949 addr_policy_result_t
950 rset_contains_addr_cmp_addr_to_policy(const tor_addr_t *addr, uint16_t port,
951 const smartlist_t *policy)
953 (void)port;
954 (void)policy;
955 rset_contains_addr_cmp_addr_to_policy_called++;
956 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
957 return ADDR_POLICY_REJECTED;
959 done:
960 return 0;
964 * Structural test for routerset_contains, when given a valid routerset
965 * and the address is not rejected by policy.
968 static addr_policy_result_t rset_contains_no_addr_cmp_addr_to_policy(
969 const tor_addr_t *addr, uint16_t port,
970 const smartlist_t *policy);
971 static int rset_contains_no_addr_cmp_addr_to_policy_called = 0;
973 static void
974 test_rset_contains_no_addr(void *arg)
976 routerset_t *set = routerset_new();
977 tor_addr_t *addr = MOCK_TOR_ADDR_PTR;
978 int contains;
979 (void)arg;
981 MOCK(compare_tor_addr_to_addr_policy,
982 rset_contains_no_addr_cmp_addr_to_policy);
984 contains = routerset_contains(set, addr, 0, NULL, NULL, 0);
985 routerset_free(set);
987 tt_int_op(rset_contains_no_addr_cmp_addr_to_policy_called, OP_EQ, 1);
988 tt_int_op(contains, OP_EQ, 0);
990 done:
994 addr_policy_result_t
995 rset_contains_no_addr_cmp_addr_to_policy(const tor_addr_t *addr, uint16_t port,
996 const smartlist_t *policy)
998 (void)port;
999 (void)policy;
1000 rset_contains_no_addr_cmp_addr_to_policy_called++;
1001 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
1003 return ADDR_POLICY_ACCEPTED;
1005 done:
1006 return 0;
1010 * Structural test for routerset_contains, when given a valid routerset
1011 * and the address is NULL.
1014 static addr_policy_result_t rset_contains_null_addr_cmp_addr_to_policy(
1015 const tor_addr_t *addr, uint16_t port,
1016 const smartlist_t *policy);
1017 static int rset_contains_null_addr_cmp_addr_to_policy_called = 0;
1019 static void
1020 test_rset_contains_null_addr(void *arg)
1022 routerset_t *set = routerset_new();
1023 int contains;
1024 (void)arg;
1026 MOCK(compare_tor_addr_to_addr_policy,
1027 rset_contains_null_addr_cmp_addr_to_policy);
1029 contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
1030 routerset_free(set);
1032 tt_int_op(contains, OP_EQ, 0);
1034 done:
1038 addr_policy_result_t
1039 rset_contains_null_addr_cmp_addr_to_policy(
1040 const tor_addr_t *addr, uint16_t port,
1041 const smartlist_t *policy)
1043 (void)port;
1044 (void)policy;
1045 rset_contains_null_addr_cmp_addr_to_policy_called++;
1046 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
1048 return ADDR_POLICY_ACCEPTED;
1050 done:
1051 return 0;
1055 * Structural test for routerset_contains, when there is no matching country
1056 * for the address.
1059 static addr_policy_result_t rset_countries_no_geoip_cmp_addr_to_policy(
1060 const tor_addr_t *addr, uint16_t port,
1061 const smartlist_t *policy);
1062 static int rset_countries_no_geoip_cmp_addr_to_policy_called = 0;
1063 static int rset_countries_no_geoip_geoip_get_country_by_addr(
1064 const tor_addr_t *addr);
1065 static int rset_countries_no_geoip_geoip_get_country_by_addr_called = 0;
1067 static void
1068 test_rset_countries_no_geoip(void *arg)
1070 routerset_t *set = routerset_new();
1071 int contains = 1;
1072 (void)arg;
1074 MOCK(compare_tor_addr_to_addr_policy,
1075 rset_countries_no_geoip_cmp_addr_to_policy);
1076 MOCK(geoip_get_country_by_addr,
1077 rset_countries_no_geoip_geoip_get_country_by_addr);
1079 set->countries = bitarray_init_zero(1);
1080 bitarray_set(set->countries, 1);
1081 contains = routerset_contains(set, MOCK_TOR_ADDR_PTR, 0, NULL, NULL, -1);
1082 routerset_free(set);
1084 tt_int_op(contains, OP_EQ, 0);
1085 tt_int_op(rset_countries_no_geoip_cmp_addr_to_policy_called,
1086 OP_EQ, 1);
1087 tt_int_op(rset_countries_no_geoip_geoip_get_country_by_addr_called,
1088 OP_EQ, 1);
1090 done:
1094 addr_policy_result_t
1095 rset_countries_no_geoip_cmp_addr_to_policy(
1096 const tor_addr_t *addr, uint16_t port,
1097 const smartlist_t *policy)
1099 (void)port;
1100 (void)policy;
1101 rset_countries_no_geoip_cmp_addr_to_policy_called++;
1102 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
1104 done:
1105 return ADDR_POLICY_ACCEPTED;
1109 rset_countries_no_geoip_geoip_get_country_by_addr(const tor_addr_t *addr)
1111 rset_countries_no_geoip_geoip_get_country_by_addr_called++;
1112 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
1114 done:
1115 return -1;
1119 * Structural test for routerset_contains, when there a matching country
1120 * for the address.
1123 static addr_policy_result_t rset_countries_geoip_cmp_addr_to_policy(
1124 const tor_addr_t *addr, uint16_t port,
1125 const smartlist_t *policy);
1126 static int rset_countries_geoip_cmp_addr_to_policy_called = 0;
1127 static int rset_countries_geoip_geoip_get_country_by_addr(
1128 const tor_addr_t *addr);
1129 static int rset_countries_geoip_geoip_get_country_by_addr_called = 0;
1131 static void
1132 test_rset_countries_geoip(void *arg)
1134 routerset_t *set = routerset_new();
1135 int contains = 1;
1136 (void)arg;
1138 MOCK(compare_tor_addr_to_addr_policy,
1139 rset_countries_geoip_cmp_addr_to_policy);
1140 MOCK(geoip_get_country_by_addr,
1141 rset_countries_geoip_geoip_get_country_by_addr);
1143 set->n_countries = 2;
1144 set->countries = bitarray_init_zero(1);
1145 bitarray_set(set->countries, 1);
1146 contains = routerset_contains(set, MOCK_TOR_ADDR_PTR, 0, NULL, NULL, -1);
1147 routerset_free(set);
1149 tt_int_op(contains, OP_EQ, 2);
1150 tt_int_op(
1151 rset_countries_geoip_cmp_addr_to_policy_called,
1152 OP_EQ, 1);
1153 tt_int_op(rset_countries_geoip_geoip_get_country_by_addr_called,
1154 OP_EQ, 1);
1156 done:
1160 addr_policy_result_t
1161 rset_countries_geoip_cmp_addr_to_policy(
1162 const tor_addr_t *addr, uint16_t port,
1163 const smartlist_t *policy)
1165 (void)port;
1166 (void)policy;
1167 rset_countries_geoip_cmp_addr_to_policy_called++;
1168 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
1170 done:
1171 return ADDR_POLICY_ACCEPTED;
1175 rset_countries_geoip_geoip_get_country_by_addr(const tor_addr_t *addr)
1177 rset_countries_geoip_geoip_get_country_by_addr_called++;
1178 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
1180 done:
1181 return 1;
1185 * Functional test for routerset_add_unknown_ccs, where only_if_some_cc_set
1186 * is set and there are no country names.
1189 static void
1190 test_rset_add_unknown_ccs_only_flag(void *arg)
1192 routerset_t *set = routerset_new();
1193 routerset_t **setp = &set;
1194 int r;
1195 (void)arg;
1197 r = routerset_add_unknown_ccs(setp, 1);
1199 tt_int_op(r, OP_EQ, 0);
1201 done:
1202 routerset_free(set);
1206 * Functional test for routerset_add_unknown_ccs, where the set argument
1207 * is created if passed in as NULL.
1210 /* The mock is only used to stop the test from asserting erroneously. */
1211 static country_t rset_add_unknown_ccs_creates_set_geoip_get_country(
1212 const char *country);
1213 static int rset_add_unknown_ccs_creates_set_geoip_get_country_called = 0;
1215 static void
1216 test_rset_add_unknown_ccs_creates_set(void *arg)
1218 routerset_t *set = NULL;
1219 routerset_t **setp = &set;
1220 int r;
1221 (void)arg;
1223 MOCK(geoip_get_country,
1224 rset_add_unknown_ccs_creates_set_geoip_get_country);
1226 r = routerset_add_unknown_ccs(setp, 0);
1228 tt_ptr_op(*setp, OP_NE, NULL);
1229 tt_int_op(r, OP_EQ, 0);
1231 done:
1232 if (set != NULL)
1233 routerset_free(set);
1236 country_t
1237 rset_add_unknown_ccs_creates_set_geoip_get_country(const char *country)
1239 (void)country;
1240 rset_add_unknown_ccs_creates_set_geoip_get_country_called++;
1242 return -1;
1246 * Structural test for routerset_add_unknown_ccs, that the "{??}"
1247 * country code is added to the list.
1250 static country_t rset_add_unknown_ccs_add_unknown_geoip_get_country(
1251 const char *country);
1252 static int rset_add_unknown_ccs_add_unknown_geoip_get_country_called = 0;
1253 static int rset_add_unknown_ccs_add_unknown_geoip_is_loaded(
1254 sa_family_t family);
1255 static int rset_add_unknown_ccs_add_unknown_geoip_is_loaded_called = 0;
1257 static void
1258 test_rset_add_unknown_ccs_add_unknown(void *arg)
1260 routerset_t *set = routerset_new();
1261 routerset_t **setp = &set;
1262 int r;
1263 (void)arg;
1265 MOCK(geoip_get_country,
1266 rset_add_unknown_ccs_add_unknown_geoip_get_country);
1267 MOCK(geoip_is_loaded,
1268 rset_add_unknown_ccs_add_unknown_geoip_is_loaded);
1270 r = routerset_add_unknown_ccs(setp, 0);
1272 tt_int_op(r, OP_EQ, 1);
1273 tt_int_op(smartlist_contains_string(set->country_names, "??"), OP_EQ, 1);
1274 tt_int_op(smartlist_contains_string(set->list, "{??}"), OP_EQ, 1);
1276 done:
1277 if (set != NULL)
1278 routerset_free(set);
1281 country_t
1282 rset_add_unknown_ccs_add_unknown_geoip_get_country(const char *country)
1284 int arg_is_qq, arg_is_a1;
1286 rset_add_unknown_ccs_add_unknown_geoip_get_country_called++;
1288 arg_is_qq = !strcmp(country, "??");
1289 arg_is_a1 = !strcmp(country, "A1");
1291 tt_int_op(arg_is_qq || arg_is_a1, OP_EQ, 1);
1293 if (arg_is_qq)
1294 return 1;
1296 done:
1297 return -1;
1301 rset_add_unknown_ccs_add_unknown_geoip_is_loaded(sa_family_t family)
1303 rset_add_unknown_ccs_add_unknown_geoip_is_loaded_called++;
1305 tt_int_op(family, OP_EQ, AF_INET);
1307 done:
1308 return 0;
1312 * Structural test for routerset_add_unknown_ccs, that the "{a1}"
1313 * country code is added to the list.
1316 static country_t rset_add_unknown_ccs_add_a1_geoip_get_country(
1317 const char *country);
1318 static int rset_add_unknown_ccs_add_a1_geoip_get_country_called = 0;
1319 static int rset_add_unknown_ccs_add_a1_geoip_is_loaded(sa_family_t family);
1320 static int rset_add_unknown_ccs_add_a1_geoip_is_loaded_called = 0;
1322 static void
1323 test_rset_add_unknown_ccs_add_a1(void *arg)
1325 routerset_t *set = routerset_new();
1326 routerset_t **setp = &set;
1327 int r;
1328 (void)arg;
1330 MOCK(geoip_get_country,
1331 rset_add_unknown_ccs_add_a1_geoip_get_country);
1332 MOCK(geoip_is_loaded,
1333 rset_add_unknown_ccs_add_a1_geoip_is_loaded);
1335 r = routerset_add_unknown_ccs(setp, 0);
1337 tt_int_op(r, OP_EQ, 1);
1338 tt_int_op(smartlist_contains_string(set->country_names, "a1"), OP_EQ, 1);
1339 tt_int_op(smartlist_contains_string(set->list, "{a1}"), OP_EQ, 1);
1341 done:
1342 if (set != NULL)
1343 routerset_free(set);
1346 country_t
1347 rset_add_unknown_ccs_add_a1_geoip_get_country(const char *country)
1349 int arg_is_qq, arg_is_a1;
1351 rset_add_unknown_ccs_add_a1_geoip_get_country_called++;
1353 arg_is_qq = !strcmp(country, "??");
1354 arg_is_a1 = !strcmp(country, "A1");
1356 tt_int_op(arg_is_qq || arg_is_a1, OP_EQ, 1);
1358 if (arg_is_a1)
1359 return 1;
1361 done:
1362 return -1;
1366 rset_add_unknown_ccs_add_a1_geoip_is_loaded(sa_family_t family)
1368 rset_add_unknown_ccs_add_a1_geoip_is_loaded_called++;
1370 tt_int_op(family, OP_EQ, AF_INET);
1372 done:
1373 return 0;
1377 * Functional test for routerset_contains_extendinfo.
1380 static void
1381 test_rset_contains_extendinfo(void *arg)
1383 routerset_t *set = routerset_new();
1384 extend_info_t ei;
1385 int r;
1386 const char *nickname = "foo";
1387 (void)arg;
1389 memset(&ei, 0, sizeof(ei));
1390 strmap_set_lc(set->names, nickname, (void *)1);
1391 strncpy(ei.nickname, nickname, sizeof(ei.nickname) - 1);
1392 ei.nickname[sizeof(ei.nickname) - 1] = '\0';
1394 r = routerset_contains_extendinfo(set, &ei);
1396 tt_int_op(r, OP_EQ, 4);
1397 done:
1398 routerset_free(set);
1402 * Functional test for routerset_contains_router.
1405 static void
1406 test_rset_contains_router(void *arg)
1408 routerset_t *set = routerset_new();
1409 routerinfo_t ri;
1410 country_t country = 1;
1411 int r;
1412 const char *nickname = "foo";
1413 (void)arg;
1415 memset(&ri, 0, sizeof(ri));
1416 strmap_set_lc(set->names, nickname, (void *)1);
1417 ri.nickname = (char *)nickname;
1419 r = routerset_contains_router(set, &ri, country);
1420 tt_int_op(r, OP_EQ, 4);
1422 done:
1423 routerset_free(set);
1426 static void
1427 test_rset_contains_router_ipv4(void *arg)
1429 routerset_t *set;
1430 routerinfo_t ri;
1431 country_t country = 1;
1432 int r;
1433 const char *s;
1434 (void) arg;
1436 /* IPv4 address test. */
1437 memset(&ri, 0, sizeof(ri));
1438 set = routerset_new();
1439 s = "10.0.0.1";
1440 r = routerset_parse(set, s, "");
1441 tor_addr_from_ipv4h(&ri.ipv4_addr, 0x0a000001);
1442 ri.ipv4_orport = 1234;
1444 r = routerset_contains_router(set, &ri, country);
1445 tt_int_op(r, OP_EQ, 3);
1447 done:
1448 routerset_free(set);
1451 static void
1452 test_rset_contains_router_ipv6(void *arg)
1454 routerset_t *set;
1455 routerinfo_t ri;
1456 country_t country = 1;
1457 int r;
1458 const char *s;
1459 (void) arg;
1461 /* IPv6 address test. */
1462 memset(&ri, 0, sizeof(ri));
1463 set = routerset_new();
1464 s = "2600::1";
1465 r = routerset_parse(set, s, "");
1466 tor_addr_parse(&ri.ipv6_addr, "2600::1");
1467 ri.ipv6_orport = 12345;
1469 r = routerset_contains_router(set, &ri, country);
1470 tt_int_op(r, OP_EQ, 3);
1472 done:
1473 routerset_free(set);
1477 * Functional test for routerset_contains_routerstatus.
1480 // XXX: This is a bit brief. It only populates and tests the nickname fields
1481 // ie., enough to make the containment check succeed. Perhaps it should do
1482 // a bit more or test a bit more.
1484 static void
1485 test_rset_contains_routerstatus(void *arg)
1487 routerset_t *set = routerset_new();
1488 routerstatus_t rs;
1489 country_t country = 1;
1490 int r;
1491 const char *nickname = "foo";
1492 (void)arg;
1494 memset(&rs, 0, sizeof(rs));
1495 strmap_set_lc(set->names, nickname, (void *)1);
1496 strncpy(rs.nickname, nickname, sizeof(rs.nickname) - 1);
1497 rs.nickname[sizeof(rs.nickname) - 1] = '\0';
1499 r = routerset_contains_routerstatus(set, &rs, country);
1501 tt_int_op(r, OP_EQ, 4);
1502 done:
1503 routerset_free(set);
1507 * Functional test for routerset_contains_node, when the node has no
1508 * routerset or routerinfo.
1511 static node_t rset_contains_none_mock_node;
1513 static void
1514 test_rset_contains_none(void *arg)
1516 routerset_t *set = routerset_new();
1517 int r;
1518 (void)arg;
1520 memset(&rset_contains_none_mock_node, 0,
1521 sizeof(rset_contains_none_mock_node));
1522 rset_contains_none_mock_node.ri = NULL;
1523 rset_contains_none_mock_node.rs = NULL;
1525 r = routerset_contains_node(set, &rset_contains_none_mock_node);
1526 tt_int_op(r, OP_EQ, 0);
1528 done:
1529 routerset_free(set);
1533 * Functional test for routerset_contains_node, when the node has a
1534 * routerset and no routerinfo.
1537 static node_t rset_contains_rs_mock_node;
1539 static void
1540 test_rset_contains_rs(void *arg)
1542 routerset_t *set = routerset_new();
1543 int r;
1544 const char *nickname = "foo";
1545 routerstatus_t rs;
1546 (void)arg;
1548 strmap_set_lc(set->names, nickname, (void *)1);
1550 strncpy(rs.nickname, nickname, sizeof(rs.nickname) - 1);
1551 rs.nickname[sizeof(rs.nickname) - 1] = '\0';
1552 memset(&rset_contains_rs_mock_node, 0, sizeof(rset_contains_rs_mock_node));
1553 rset_contains_rs_mock_node.ri = NULL;
1554 rset_contains_rs_mock_node.rs = &rs;
1556 r = routerset_contains_node(set, &rset_contains_rs_mock_node);
1558 tt_int_op(r, OP_EQ, 4);
1559 done:
1560 routerset_free(set);
1564 * Functional test for routerset_contains_node, when the node has no
1565 * routerset and a routerinfo.
1568 static void
1569 test_rset_contains_routerinfo(void *arg)
1571 routerset_t *set = routerset_new();
1572 int r;
1573 const char *nickname = "foo";
1574 routerinfo_t ri;
1575 node_t mock_node;
1576 (void)arg;
1578 strmap_set_lc(set->names, nickname, (void *)1);
1580 ri.nickname = (char *)nickname;
1581 memset(&mock_node, 0, sizeof(mock_node));
1582 mock_node.ri = &ri;
1583 mock_node.rs = NULL;
1585 r = routerset_contains_node(set, &mock_node);
1587 tt_int_op(r, OP_EQ, 4);
1588 done:
1589 routerset_free(set);
1593 * Functional test for routerset_get_all_nodes, when routerset is NULL or
1594 * the routerset list is NULL.
1597 static void
1598 test_rset_get_all_no_routerset(void *arg)
1600 smartlist_t *out = smartlist_new();
1601 routerset_t *set = NULL;
1602 (void)arg;
1604 tt_int_op(smartlist_len(out), OP_EQ, 0);
1605 routerset_get_all_nodes(out, NULL, NULL, 0);
1607 tt_int_op(smartlist_len(out), OP_EQ, 0);
1609 set = routerset_new();
1610 smartlist_free(set->list);
1611 routerset_get_all_nodes(out, NULL, NULL, 0);
1612 tt_int_op(smartlist_len(out), OP_EQ, 0);
1614 /* Just recreate list, so we can simply use routerset_free. */
1615 set->list = smartlist_new();
1617 done:
1618 routerset_free(set);
1619 smartlist_free(out);
1623 * Structural test for routerset_get_all_nodes, when the routerset list
1624 * is empty.
1627 static const node_t * rset_get_all_l_no_nodes_node_get_by_nickname(
1628 const char *nickname, unsigned flags);
1629 static int rset_get_all_l_no_nodes_node_get_by_nickname_called = 0;
1630 static const char *rset_get_all_l_no_nodes_mock_nickname;
1632 static void
1633 test_rset_get_all_l_no_nodes(void *arg)
1635 smartlist_t *out = smartlist_new();
1636 routerset_t *set = routerset_new();
1637 int out_len;
1638 (void)arg;
1640 MOCK(node_get_by_nickname,
1641 rset_get_all_l_no_nodes_node_get_by_nickname);
1643 rset_get_all_l_no_nodes_mock_nickname = "foo";
1644 smartlist_add_strdup(set->list, rset_get_all_l_no_nodes_mock_nickname);
1646 routerset_get_all_nodes(out, set, NULL, 0);
1647 out_len = smartlist_len(out);
1649 smartlist_free(out);
1650 routerset_free(set);
1652 tt_int_op(out_len, OP_EQ, 0);
1653 tt_int_op(rset_get_all_l_no_nodes_node_get_by_nickname_called, OP_EQ, 1);
1655 done:
1659 const node_t *
1660 rset_get_all_l_no_nodes_node_get_by_nickname(const char *nickname,
1661 unsigned flags)
1663 rset_get_all_l_no_nodes_node_get_by_nickname_called++;
1664 tt_str_op(nickname, OP_EQ, rset_get_all_l_no_nodes_mock_nickname);
1665 tt_uint_op(flags, OP_EQ, 0);
1667 done:
1668 return NULL;
1672 * Structural test for routerset_get_all_nodes, with the running_only flag
1673 * is set but the nodes are not running.
1676 static const node_t * rset_get_all_l_not_running_node_get_by_nickname(
1677 const char *nickname, unsigned flags);
1678 static int rset_get_all_l_not_running_node_get_by_nickname_called = 0;
1679 static const char *rset_get_all_l_not_running_mock_nickname;
1680 static node_t rset_get_all_l_not_running_mock_node;
1682 static void
1683 test_rset_get_all_l_not_running(void *arg)
1685 smartlist_t *out = smartlist_new();
1686 routerset_t *set = routerset_new();
1687 int out_len;
1688 (void)arg;
1690 MOCK(node_get_by_nickname,
1691 rset_get_all_l_not_running_node_get_by_nickname);
1693 rset_get_all_l_not_running_mock_node.is_running = 0;
1694 rset_get_all_l_not_running_mock_nickname = "foo";
1695 smartlist_add_strdup(set->list, rset_get_all_l_not_running_mock_nickname);
1697 routerset_get_all_nodes(out, set, NULL, 1);
1698 out_len = smartlist_len(out);
1700 smartlist_free(out);
1701 routerset_free(set);
1703 tt_int_op(out_len, OP_EQ, 0);
1704 tt_int_op(rset_get_all_l_not_running_node_get_by_nickname_called, OP_EQ, 1);
1706 done:
1710 const node_t *
1711 rset_get_all_l_not_running_node_get_by_nickname(const char *nickname,
1712 unsigned flags)
1714 rset_get_all_l_not_running_node_get_by_nickname_called++;
1715 tt_str_op(nickname, OP_EQ, rset_get_all_l_not_running_mock_nickname);
1716 tt_int_op(flags, OP_EQ, 0);
1718 done:
1719 return &rset_get_all_l_not_running_mock_node;
1723 * Structural test for routerset_get_all_nodes.
1726 static const node_t * rset_get_all_list_node_get_by_nickname(
1727 const char *nickname, unsigned flags);
1728 static int rset_get_all_list_node_get_by_nickname_called = 0;
1729 static char *rset_get_all_list_mock_nickname;
1730 static node_t rset_get_all_list_mock_node;
1732 static void
1733 test_rset_get_all_list(void *arg)
1735 smartlist_t *out = smartlist_new();
1736 routerset_t *set = routerset_new();
1737 int out_len;
1738 node_t *ent;
1739 (void)arg;
1741 MOCK(node_get_by_nickname,
1742 rset_get_all_list_node_get_by_nickname);
1744 rset_get_all_list_mock_nickname = tor_strdup("foo");
1745 smartlist_add(set->list, rset_get_all_list_mock_nickname);
1747 routerset_get_all_nodes(out, set, NULL, 0);
1748 out_len = smartlist_len(out);
1749 ent = (node_t *)smartlist_get(out, 0);
1751 smartlist_free(out);
1752 routerset_free(set);
1754 tt_int_op(out_len, OP_EQ, 1);
1755 tt_ptr_op(ent, OP_EQ, &rset_get_all_list_mock_node);
1756 tt_int_op(rset_get_all_list_node_get_by_nickname_called, OP_EQ, 1);
1758 done:
1762 const node_t *
1763 rset_get_all_list_node_get_by_nickname(const char *nickname, unsigned flags)
1765 rset_get_all_list_node_get_by_nickname_called++;
1766 tt_str_op(nickname, OP_EQ, rset_get_all_list_mock_nickname);
1767 tt_int_op(flags, OP_EQ, 0);
1769 done:
1770 return &rset_get_all_list_mock_node;
1774 * Structural test for routerset_get_all_nodes, when the nodelist has no nodes.
1777 static const smartlist_t * rset_get_all_n_no_nodes_nodelist_get_list(void);
1778 static int rset_get_all_n_no_nodes_nodelist_get_list_called = 0;
1780 static smartlist_t *rset_get_all_n_no_nodes_mock_smartlist;
1781 static void
1782 test_rset_get_all_n_no_nodes(void *arg)
1784 routerset_t *set = routerset_new();
1785 smartlist_t *out = smartlist_new();
1786 int r;
1787 (void)arg;
1789 MOCK(nodelist_get_list,
1790 rset_get_all_n_no_nodes_nodelist_get_list);
1792 smartlist_add_strdup(set->country_names, "{xx}");
1793 rset_get_all_n_no_nodes_mock_smartlist = smartlist_new();
1795 routerset_get_all_nodes(out, set, NULL, 1);
1796 r = smartlist_len(out);
1797 routerset_free(set);
1798 smartlist_free(out);
1799 smartlist_free(rset_get_all_n_no_nodes_mock_smartlist);
1801 tt_int_op(r, OP_EQ, 0);
1802 tt_int_op(rset_get_all_n_no_nodes_nodelist_get_list_called, OP_EQ, 1);
1804 done:
1808 const smartlist_t *
1809 rset_get_all_n_no_nodes_nodelist_get_list(void)
1811 rset_get_all_n_no_nodes_nodelist_get_list_called++;
1813 return rset_get_all_n_no_nodes_mock_smartlist;
1817 * Structural test for routerset_get_all_nodes, with a non-list routerset
1818 * the running_only flag is set, but the nodes are not running.
1821 static const smartlist_t * rset_get_all_n_not_running_nodelist_get_list(void);
1822 static int rset_get_all_n_not_running_nodelist_get_list_called = 0;
1824 static smartlist_t *rset_get_all_n_not_running_mock_smartlist;
1825 static node_t rset_get_all_n_not_running_mock_node;
1827 static void
1828 test_rset_get_all_n_not_running(void *arg)
1830 routerset_t *set = routerset_new();
1831 smartlist_t *out = smartlist_new();
1832 int r;
1833 (void)arg;
1835 MOCK(nodelist_get_list,
1836 rset_get_all_n_not_running_nodelist_get_list);
1838 smartlist_add_strdup(set->country_names, "{xx}");
1839 rset_get_all_n_not_running_mock_smartlist = smartlist_new();
1840 rset_get_all_n_not_running_mock_node.is_running = 0;
1841 smartlist_add(rset_get_all_n_not_running_mock_smartlist,
1842 (void *)&rset_get_all_n_not_running_mock_node);
1844 routerset_get_all_nodes(out, set, NULL, 1);
1845 r = smartlist_len(out);
1846 routerset_free(set);
1847 smartlist_free(out);
1848 smartlist_free(rset_get_all_n_not_running_mock_smartlist);
1850 tt_int_op(r, OP_EQ, 0);
1851 tt_int_op(rset_get_all_n_not_running_nodelist_get_list_called, OP_EQ, 1);
1853 done:
1857 const smartlist_t *
1858 rset_get_all_n_not_running_nodelist_get_list(void)
1860 rset_get_all_n_not_running_nodelist_get_list_called++;
1862 return rset_get_all_n_not_running_mock_smartlist;
1866 * Functional test for routerset_subtract_nodes.
1869 static void
1870 test_rset_subtract_nodes(void *arg)
1872 routerset_t *set = routerset_new();
1873 smartlist_t *list = smartlist_new();
1874 const char *nickname = "foo";
1875 routerinfo_t ri;
1876 node_t mock_node;
1877 (void)arg;
1879 strmap_set_lc(set->names, nickname, (void *)1);
1881 ri.nickname = (char *)nickname;
1882 mock_node.rs = NULL;
1883 mock_node.ri = &ri;
1884 smartlist_add(list, (void *)&mock_node);
1886 tt_int_op(smartlist_len(list), OP_NE, 0);
1887 routerset_subtract_nodes(list, set);
1889 tt_int_op(smartlist_len(list), OP_EQ, 0);
1890 done:
1891 routerset_free(set);
1892 smartlist_free(list);
1896 * Functional test for routerset_subtract_nodes, with a NULL routerset.
1899 static void
1900 test_rset_subtract_nodes_null_routerset(void *arg)
1902 routerset_t *set = NULL;
1903 smartlist_t *list = smartlist_new();
1904 const char *nickname = "foo";
1905 routerinfo_t ri;
1906 node_t mock_node;
1907 (void)arg;
1909 ri.nickname = (char *)nickname;
1910 mock_node.ri = &ri;
1911 smartlist_add(list, (void *)&mock_node);
1913 tt_int_op(smartlist_len(list), OP_NE, 0);
1914 routerset_subtract_nodes(list, set);
1916 tt_int_op(smartlist_len(list), OP_NE, 0);
1917 done:
1918 routerset_free(set);
1919 smartlist_free(list);
1923 * Functional test for routerset_to_string.
1926 static void
1927 test_rset_to_string(void *arg)
1929 routerset_t *set = NULL;
1930 char *s = NULL;
1931 (void)arg;
1933 set = NULL;
1934 s = routerset_to_string(set);
1935 tt_str_op(s, OP_EQ, "");
1936 tor_free(s);
1938 set = routerset_new();
1939 s = routerset_to_string(set);
1940 tt_str_op(s, OP_EQ, "");
1941 tor_free(s);
1942 routerset_free(set); set = NULL;
1944 set = routerset_new();
1945 smartlist_add(set->list, tor_strndup("a", 1));
1946 s = routerset_to_string(set);
1947 tt_str_op(s, OP_EQ, "a");
1948 tor_free(s);
1949 routerset_free(set); set = NULL;
1951 set = routerset_new();
1952 smartlist_add(set->list, tor_strndup("a", 1));
1953 smartlist_add(set->list, tor_strndup("b", 1));
1954 s = routerset_to_string(set);
1955 tt_str_op(s, OP_EQ, "a,b");
1956 tor_free(s);
1957 routerset_free(set); set = NULL;
1959 done:
1960 tor_free(s);
1961 routerset_free(set);
1965 * Functional test for routerset_equal, with both routersets empty.
1968 static void
1969 test_rset_equal_empty_empty(void *arg)
1971 routerset_t *a = routerset_new(), *b = routerset_new();
1972 int r;
1973 (void)arg;
1975 r = routerset_equal(a, b);
1976 routerset_free(a);
1977 routerset_free(b);
1979 tt_int_op(r, OP_EQ, 1);
1981 done:
1986 * Functional test for routerset_equal, with one routersets empty.
1989 static void
1990 test_rset_equal_empty_not_empty(void *arg)
1992 routerset_t *a = routerset_new(), *b = routerset_new();
1993 int r;
1994 (void)arg;
1996 smartlist_add_strdup(b->list, "{xx}");
1997 r = routerset_equal(a, b);
1998 routerset_free(a);
1999 routerset_free(b);
2001 tt_int_op(r, OP_EQ, 0);
2002 done:
2007 * Functional test for routerset_equal, with the routersets having
2008 * differing lengths.
2011 static void
2012 test_rset_equal_differing_lengths(void *arg)
2014 routerset_t *a = routerset_new(), *b = routerset_new();
2015 int r;
2016 (void)arg;
2018 smartlist_add_strdup(a->list, "{aa}");
2019 smartlist_add_strdup(b->list, "{b1}");
2020 smartlist_add_strdup(b->list, "{b2}");
2021 r = routerset_equal(a, b);
2022 routerset_free(a);
2023 routerset_free(b);
2025 tt_int_op(r, OP_EQ, 0);
2026 done:
2031 * Functional test for routerset_equal, with the routersets being
2032 * different.
2035 static void
2036 test_rset_equal_unequal(void *arg)
2038 routerset_t *a = routerset_new(), *b = routerset_new();
2039 int r;
2040 (void)arg;
2042 smartlist_add_strdup(a->list, "foo");
2043 smartlist_add_strdup(b->list, "bar");
2044 r = routerset_equal(a, b);
2045 routerset_free(a);
2046 routerset_free(b);
2048 tt_int_op(r, OP_EQ, 0);
2049 done:
2054 * Functional test for routerset_equal, with the routersets being
2055 * equal.
2058 static void
2059 test_rset_equal_equal(void *arg)
2061 routerset_t *a = routerset_new(), *b = routerset_new();
2062 int r;
2063 (void)arg;
2065 smartlist_add_strdup(a->list, "foo");
2066 smartlist_add_strdup(b->list, "foo");
2067 r = routerset_equal(a, b);
2068 routerset_free(a);
2069 routerset_free(b);
2071 tt_int_op(r, OP_EQ, 1);
2072 done:
2077 * Structural test for routerset_free, where the routerset is NULL.
2080 static void rset_free_null_routerset_smartlist_free_(smartlist_t *sl);
2081 static int rset_free_null_routerset_smartlist_free__called = 0;
2083 static void
2084 test_rset_free_null_routerset(void *arg)
2086 (void)arg;
2088 MOCK(smartlist_free_,
2089 rset_free_null_routerset_smartlist_free_);
2091 routerset_free_(NULL);
2093 tt_int_op(rset_free_null_routerset_smartlist_free__called, OP_EQ, 0);
2095 done:
2099 void
2100 rset_free_null_routerset_smartlist_free_(smartlist_t *s)
2102 (void)s;
2103 rset_free_null_routerset_smartlist_free__called++;
2107 * Structural test for routerset_free.
2110 static void rset_free_smartlist_free_(smartlist_t *sl);
2111 static int rset_free_smartlist_free__called = 0;
2112 static void rset_free_strmap_free_(strmap_t *map, void (*free_val)(void*));
2113 static int rset_free_strmap_free__called = 0;
2114 static void rset_free_digestmap_free_(digestmap_t *map,
2115 void (*free_val)(void*));
2116 static int rset_free_digestmap_free__called = 0;
2118 static void
2119 test_rset_free(void *arg)
2121 routerset_t *routerset = routerset_new();
2122 (void)arg;
2124 MOCK(smartlist_free_,
2125 rset_free_smartlist_free_);
2126 MOCK(strmap_free_,
2127 rset_free_strmap_free_);
2128 MOCK(digestmap_free_,
2129 rset_free_digestmap_free_);
2131 routerset_free(routerset);
2133 tt_int_op(rset_free_smartlist_free__called, OP_NE, 0);
2134 tt_int_op(rset_free_strmap_free__called, OP_NE, 0);
2135 tt_int_op(rset_free_digestmap_free__called, OP_NE, 0);
2137 done:
2141 void
2142 rset_free_smartlist_free_(smartlist_t *s)
2144 rset_free_smartlist_free__called++;
2145 smartlist_free___real(s);
2148 void
2149 rset_free_strmap_free_(strmap_t *map, void (*free_val)(void*))
2151 rset_free_strmap_free__called++;
2152 strmap_free___real(map, free_val);
2155 void
2156 rset_free_digestmap_free_(digestmap_t *map, void (*free_val)(void*))
2158 rset_free_digestmap_free__called++;
2159 digestmap_free___real(map, free_val);
2162 struct testcase_t routerset_tests[] = {
2163 { "new", test_rset_new, TT_FORK, NULL, NULL },
2164 { "get_countryname", test_rset_get_countryname, TT_FORK, NULL, NULL },
2165 { "is_list", test_rset_is_list, TT_FORK, NULL, NULL },
2166 { "needs_geoip", test_rset_needs_geoip, TT_FORK, NULL, NULL },
2167 { "is_empty", test_rset_is_empty, TT_FORK, NULL, NULL },
2168 { "contains_null_set_or_list", test_rset_contains_null_set_or_list,
2169 TT_FORK, NULL, NULL },
2170 { "contains_nickname", test_rset_contains_nickname, TT_FORK, NULL, NULL },
2171 { "contains_null_nickname", test_rset_contains_null_nickname,
2172 TT_FORK, NULL, NULL },
2173 { "contains_no_nickname", test_rset_contains_no_nickname,
2174 TT_FORK, NULL, NULL },
2175 { "contains_digest", test_rset_contains_digest, TT_FORK, NULL, NULL },
2176 { "contains_no_digest", test_rset_contains_no_digest, TT_FORK, NULL, NULL },
2177 { "contains_null_digest", test_rset_contains_null_digest,
2178 TT_FORK, NULL, NULL },
2179 { "contains_addr", test_rset_contains_addr, TT_FORK, NULL, NULL },
2180 { "contains_no_addr", test_rset_contains_no_addr, TT_FORK, NULL, NULL },
2181 { "contains_null_addr", test_rset_contains_null_addr, TT_FORK, NULL, NULL },
2182 { "contains_countries_no_geoip", test_rset_countries_no_geoip,
2183 TT_FORK, NULL, NULL },
2184 { "contains_countries_geoip", test_rset_countries_geoip,
2185 TT_FORK, NULL, NULL },
2186 { "add_unknown_ccs_only_flag", test_rset_add_unknown_ccs_only_flag,
2187 TT_FORK, NULL, NULL },
2188 { "add_unknown_ccs_creates_set", test_rset_add_unknown_ccs_creates_set,
2189 TT_FORK, NULL, NULL },
2190 { "add_unknown_ccs_add_unknown", test_rset_add_unknown_ccs_add_unknown,
2191 TT_FORK, NULL, NULL },
2192 { "add_unknown_ccs_add_a1", test_rset_add_unknown_ccs_add_a1,
2193 TT_FORK, NULL, NULL },
2194 { "contains_extendinfo", test_rset_contains_extendinfo,
2195 TT_FORK, NULL, NULL },
2196 { "contains_router", test_rset_contains_router, TT_FORK, NULL, NULL },
2197 { "contains_router_ipv4", test_rset_contains_router_ipv4,
2198 TT_FORK, NULL, NULL },
2199 { "contains_router_ipv6", test_rset_contains_router_ipv6,
2200 TT_FORK, NULL, NULL },
2201 { "contains_routerstatus", test_rset_contains_routerstatus,
2202 TT_FORK, NULL, NULL },
2203 { "contains_none", test_rset_contains_none, TT_FORK, NULL, NULL },
2204 { "contains_routerinfo", test_rset_contains_routerinfo,
2205 TT_FORK, NULL, NULL },
2206 { "contains_rs", test_rset_contains_rs, TT_FORK, NULL, NULL },
2207 { "get_all_no_routerset", test_rset_get_all_no_routerset,
2208 TT_FORK, NULL, NULL },
2209 { "get_all_l_no_nodes", test_rset_get_all_l_no_nodes, TT_FORK, NULL, NULL },
2210 { "get_all_l_not_running", test_rset_get_all_l_not_running,
2211 TT_FORK, NULL, NULL },
2212 { "get_all_list", test_rset_get_all_list, TT_FORK, NULL, NULL },
2213 { "get_all_n_no_nodes", test_rset_get_all_n_no_nodes, TT_FORK, NULL, NULL },
2214 { "get_all_n_not_running", test_rset_get_all_n_not_running,
2215 TT_FORK, NULL, NULL },
2216 { "refresh_geoip_not_loaded", test_rset_refresh_geoip_not_loaded,
2217 TT_FORK, NULL, NULL },
2218 { "refresh_no_countries", test_rset_refresh_no_countries,
2219 TT_FORK, NULL, NULL },
2220 { "refresh_one_valid_country", test_rset_refresh_one_valid_country,
2221 TT_FORK, NULL, NULL },
2222 { "refresh_one_invalid_country", test_rset_refresh_one_invalid_country,
2223 TT_FORK, NULL, NULL },
2224 { "union_source_bad", test_rset_union_source_bad, TT_FORK, NULL, NULL },
2225 { "union_one", test_rset_union_one, TT_FORK, NULL, NULL },
2226 { "parse_malformed", test_rset_parse_malformed, TT_FORK, NULL, NULL },
2227 { "parse_valid_hexdigest", test_rset_parse_valid_hexdigest,
2228 TT_FORK, NULL, NULL },
2229 { "parse_valid_nickname", test_rset_parse_valid_nickname,
2230 TT_FORK, NULL, NULL },
2231 { "parse_get_countryname", test_rset_parse_get_countryname,
2232 TT_FORK, NULL, NULL },
2233 { "parse_policy_wildcard", test_rset_parse_policy_wildcard,
2234 TT_FORK, NULL, NULL },
2235 { "parse_policy_ipv4", test_rset_parse_policy_ipv4, TT_FORK, NULL, NULL },
2236 { "parse_policy_ipv6", test_rset_parse_policy_ipv6, TT_FORK, NULL, NULL },
2237 { "subtract_nodes", test_rset_subtract_nodes, TT_FORK, NULL, NULL },
2238 { "subtract_nodes_null_routerset", test_rset_subtract_nodes_null_routerset,
2239 TT_FORK, NULL, NULL },
2240 { "to_string", test_rset_to_string, TT_FORK, NULL, NULL },
2241 { "equal_empty_empty", test_rset_equal_empty_empty, TT_FORK, NULL, NULL },
2242 { "equal_empty_not_empty", test_rset_equal_empty_not_empty,
2243 TT_FORK, NULL, NULL },
2244 { "equal_differing_lengths", test_rset_equal_differing_lengths,
2245 TT_FORK, NULL, NULL },
2246 { "equal_unequal", test_rset_equal_unequal, TT_FORK, NULL, NULL },
2247 { "equal_equal", test_rset_equal_equal, TT_FORK, NULL, NULL },
2248 { "free_null_routerset", test_rset_free_null_routerset,
2249 TT_FORK, NULL, NULL },
2250 { "free", test_rset_free, TT_FORK, NULL, NULL },
2251 END_OF_TESTCASES