0.2.6.5-rc will come out today, because why not.
[tor.git] / src / test / test_routerset.c
blob9bd0c125c38078dc7d2dce11aa3266975118f106
1 #define ROUTERSET_PRIVATE
3 #include "or.h"
4 #include "geoip.h"
5 #include "routerset.h"
6 #include "routerparse.h"
7 #include "policies.h"
8 #include "nodelist.h"
9 #include "test.h"
11 #define NS_MODULE routerset
13 #define NS_SUBMODULE routerset_new
16 * Functional (blackbox) test to determine that each member of the routerset
17 * is non-NULL
20 static void
21 NS(test_main)(void *arg)
23 routerset_t *rs;
24 (void)arg;
26 rs = routerset_new();
28 tt_ptr_op(rs, OP_NE, NULL);
29 tt_ptr_op(rs->list, OP_NE, NULL);
30 tt_ptr_op(rs->names, OP_NE, NULL);
31 tt_ptr_op(rs->digests, OP_NE, NULL);
32 tt_ptr_op(rs->policies, OP_NE, NULL);
33 tt_ptr_op(rs->country_names, OP_NE, NULL);
35 done:
36 routerset_free(rs);
39 #undef NS_SUBMODULE
40 #define NS_SUBMODULE routerset_get_countryname
43 * Functional test to strip the braces from a "{xx}" country code string.
46 static void
47 NS(test_main)(void *arg)
49 const char *input;
50 char *name;
51 (void)arg;
53 /* strlen(c) < 4 */
54 input = "xxx";
55 name = routerset_get_countryname(input);
56 tt_ptr_op(name, OP_EQ, NULL);
57 tor_free(name);
59 /* c[0] != '{' */
60 input = "xxx}";
61 name = routerset_get_countryname(input);
62 tt_ptr_op(name, OP_EQ, NULL);
63 tor_free(name);
65 /* c[3] != '}' */
66 input = "{xxx";
67 name = routerset_get_countryname(input);
68 tt_ptr_op(name, OP_EQ, NULL);
69 tor_free(name);
71 /* tor_strlower */
72 input = "{XX}";
73 name = routerset_get_countryname(input);
74 tt_str_op(name, OP_EQ, "xx");
75 tor_free(name);
77 input = "{xx}";
78 name = routerset_get_countryname(input);
79 tt_str_op(name, OP_EQ, "xx");
80 done:
81 tor_free(name);
84 #undef NS_SUBMODULE
85 #define NS_SUBMODULE ASPECT(routerset_refresh_counties, geoip_not_loaded)
88 * Structural (whitebox) test for routerset_refresh_counties, when the GeoIP DB
89 * is not loaded.
92 NS_DECL(int, geoip_is_loaded, (sa_family_t family));
93 NS_DECL(int, geoip_get_n_countries, (void));
95 static void
96 NS(test_main)(void *arg)
98 routerset_t *set = routerset_new();
99 (void)arg;
101 NS_MOCK(geoip_is_loaded);
102 NS_MOCK(geoip_get_n_countries);
104 routerset_refresh_countries(set);
106 tt_ptr_op(set->countries, OP_EQ, NULL);
107 tt_int_op(set->n_countries, OP_EQ, 0);
108 tt_int_op(CALLED(geoip_is_loaded), OP_EQ, 1);
109 tt_int_op(CALLED(geoip_get_n_countries), OP_EQ, 0);
111 done:
112 NS_UNMOCK(geoip_is_loaded);
113 NS_UNMOCK(geoip_get_n_countries);
114 routerset_free(set);
117 static int
118 NS(geoip_is_loaded)(sa_family_t family)
120 (void)family;
121 CALLED(geoip_is_loaded)++;
123 return 0;
126 static int
127 NS(geoip_get_n_countries)(void)
129 CALLED(geoip_get_n_countries)++;
131 return 0;
134 #undef NS_SUBMODULE
135 #define NS_SUBMODULE ASPECT(routerset_refresh_counties, no_countries)
138 * Structural test for routerset_refresh_counties, when there are no countries.
141 NS_DECL(int, geoip_is_loaded, (sa_family_t family));
142 NS_DECL(int, geoip_get_n_countries, (void));
143 NS_DECL(country_t, geoip_get_country, (const char *country));
145 static void
146 NS(test_main)(void *arg)
148 routerset_t *set = routerset_new();
149 (void)arg;
151 NS_MOCK(geoip_is_loaded);
152 NS_MOCK(geoip_get_n_countries);
153 NS_MOCK(geoip_get_country);
155 routerset_refresh_countries(set);
157 tt_ptr_op(set->countries, OP_NE, NULL);
158 tt_int_op(set->n_countries, OP_EQ, 1);
159 tt_int_op((unsigned int)(*set->countries), OP_EQ, 0);
160 tt_int_op(CALLED(geoip_is_loaded), OP_EQ, 1);
161 tt_int_op(CALLED(geoip_get_n_countries), OP_EQ, 1);
162 tt_int_op(CALLED(geoip_get_country), OP_EQ, 0);
164 done:
165 NS_UNMOCK(geoip_is_loaded);
166 NS_UNMOCK(geoip_get_n_countries);
167 NS_UNMOCK(geoip_get_country);
168 routerset_free(set);
171 static int
172 NS(geoip_is_loaded)(sa_family_t family)
174 (void)family;
175 CALLED(geoip_is_loaded)++;
177 return 1;
180 static int
181 NS(geoip_get_n_countries)(void)
183 CALLED(geoip_get_n_countries)++;
185 return 1;
188 static country_t
189 NS(geoip_get_country)(const char *countrycode)
191 (void)countrycode;
192 CALLED(geoip_get_country)++;
194 return 1;
197 #undef NS_SUBMODULE
198 #define NS_SUBMODULE ASPECT(routerset_refresh_counties, one_valid_country)
201 * Structural test for routerset_refresh_counties, with one valid country.
204 NS_DECL(int, geoip_is_loaded, (sa_family_t family));
205 NS_DECL(int, geoip_get_n_countries, (void));
206 NS_DECL(country_t, geoip_get_country, (const char *country));
208 static void
209 NS(test_main)(void *arg)
211 routerset_t *set = routerset_new();
212 (void)arg;
214 NS_MOCK(geoip_is_loaded);
215 NS_MOCK(geoip_get_n_countries);
216 NS_MOCK(geoip_get_country);
217 smartlist_add(set->country_names, tor_strndup("foo", 3));
219 routerset_refresh_countries(set);
221 tt_ptr_op(set->countries, OP_NE, NULL);
222 tt_int_op(set->n_countries, OP_EQ, 2);
223 tt_int_op(CALLED(geoip_is_loaded), OP_EQ, 1);
224 tt_int_op(CALLED(geoip_get_n_countries), OP_EQ, 1);
225 tt_int_op(CALLED(geoip_get_country), OP_EQ, 1);
226 tt_int_op((unsigned int)(*set->countries), OP_NE, 0);
228 done:
229 NS_UNMOCK(geoip_is_loaded);
230 NS_UNMOCK(geoip_get_n_countries);
231 NS_UNMOCK(geoip_get_country);
232 routerset_free(set);
235 static int
236 NS(geoip_is_loaded)(sa_family_t family)
238 (void)family;
239 CALLED(geoip_is_loaded)++;
241 return 1;
244 static int
245 NS(geoip_get_n_countries)(void)
247 CALLED(geoip_get_n_countries)++;
249 return 2;
252 static country_t
253 NS(geoip_get_country)(const char *countrycode)
255 (void)countrycode;
256 CALLED(geoip_get_country)++;
258 return 1;
261 #undef NS_SUBMODULE
262 #define NS_SUBMODULE ASPECT(routerset_refresh_counties, one_invalid_country)
265 * Structural test for routerset_refresh_counties, with one invalid
266 * country code..
269 NS_DECL(int, geoip_is_loaded, (sa_family_t family));
270 NS_DECL(int, geoip_get_n_countries, (void));
271 NS_DECL(country_t, geoip_get_country, (const char *country));
273 static void
274 NS(test_main)(void *arg)
276 routerset_t *set = routerset_new();
277 (void)arg;
279 NS_MOCK(geoip_is_loaded);
280 NS_MOCK(geoip_get_n_countries);
281 NS_MOCK(geoip_get_country);
282 smartlist_add(set->country_names, tor_strndup("foo", 3));
284 routerset_refresh_countries(set);
286 tt_ptr_op(set->countries, OP_NE, NULL);
287 tt_int_op(set->n_countries, OP_EQ, 2);
288 tt_int_op(CALLED(geoip_is_loaded), OP_EQ, 1);
289 tt_int_op(CALLED(geoip_get_n_countries), OP_EQ, 1);
290 tt_int_op(CALLED(geoip_get_country), OP_EQ, 1);
291 tt_int_op((unsigned int)(*set->countries), OP_EQ, 0);
293 done:
294 NS_UNMOCK(geoip_is_loaded);
295 NS_UNMOCK(geoip_get_n_countries);
296 NS_UNMOCK(geoip_get_country);
297 routerset_free(set);
300 static int
301 NS(geoip_is_loaded)(sa_family_t family)
303 (void)family;
304 CALLED(geoip_is_loaded)++;
306 return 1;
309 static int
310 NS(geoip_get_n_countries)(void)
312 CALLED(geoip_get_n_countries)++;
314 return 2;
317 static country_t
318 NS(geoip_get_country)(const char *countrycode)
320 (void)countrycode;
321 CALLED(geoip_get_country)++;
323 return -1;
326 #undef NS_SUBMODULE
327 #define NS_SUBMODULE ASPECT(routerset_parse, malformed)
330 * Functional test, with a malformed string to parse.
333 static void
334 NS(test_main)(void *arg)
336 routerset_t *set = routerset_new();
337 const char *s = "_";
338 int r;
339 (void)arg;
341 r = routerset_parse(set, s, "");
343 tt_int_op(r, OP_EQ, -1);
345 done:
346 routerset_free(set);
349 #undef NS_SUBMODULE
350 #define NS_SUBMODULE ASPECT(routerset_parse, valid_hexdigest)
353 * Functional test for routerset_parse, that routerset_parse returns 0
354 * on a valid hexdigest entry.
357 static void
358 NS(test_main)(void *arg)
360 routerset_t *set;
361 const char *s;
362 int r;
363 (void)arg;
365 set = routerset_new();
366 s = "$0000000000000000000000000000000000000000";
367 r = routerset_parse(set, s, "");
368 tt_int_op(r, OP_EQ, 0);
369 tt_int_op(digestmap_isempty(set->digests), OP_NE, 1);
371 done:
372 routerset_free(set);
375 #undef NS_SUBMODULE
376 #define NS_SUBMODULE ASPECT(routerset_parse, valid_nickname)
379 * Functional test for routerset_parse, when given a valid nickname as input.
382 static void
383 NS(test_main)(void *arg)
385 routerset_t *set;
386 const char *s;
387 int r;
388 (void)arg;
390 set = routerset_new();
391 s = "fred";
392 r = routerset_parse(set, s, "");
393 tt_int_op(r, OP_EQ, 0);
394 tt_int_op(strmap_isempty(set->names), OP_NE, 1);
396 done:
397 routerset_free(set);
400 #undef NS_SUBMODULE
401 #define NS_SUBMODULE ASPECT(routerset_parse, get_countryname)
404 * Functional test for routerset_parse, when given a valid countryname.
407 static void
408 NS(test_main)(void *arg)
410 routerset_t *set;
411 const char *s;
412 int r;
413 (void)arg;
415 set = routerset_new();
416 s = "{cc}";
417 r = routerset_parse(set, s, "");
418 tt_int_op(r, OP_EQ, 0);
419 tt_int_op(smartlist_len(set->country_names), OP_NE, 0);
421 done:
422 routerset_free(set);
425 #undef NS_SUBMODULE
426 #define NS_SUBMODULE ASPECT(routerset_parse, policy)
429 * Structural test for routerset_parse, when given a valid policy.
432 NS_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
433 (const char *s, int assume_action));
435 addr_policy_t *NS(mock_addr_policy);
437 static void
438 NS(test_main)(void *arg)
440 routerset_t *set;
441 const char *s;
442 int r;
443 (void)arg;
445 NS_MOCK(router_parse_addr_policy_item_from_string);
446 NS(mock_addr_policy) = tor_malloc_zero(sizeof(addr_policy_t));
448 set = routerset_new();
449 s = "*";
450 r = routerset_parse(set, s, "");
451 tt_int_op(r, OP_EQ, 0);
452 tt_int_op(smartlist_len(set->policies), OP_NE, 0);
453 tt_int_op(CALLED(router_parse_addr_policy_item_from_string), OP_EQ, 1);
455 done:
456 routerset_free(set);
459 addr_policy_t *
460 NS(router_parse_addr_policy_item_from_string)(const char *s, int assume_action)
462 (void)s;
463 (void)assume_action;
464 CALLED(router_parse_addr_policy_item_from_string)++;
466 return NS(mock_addr_policy);
469 #undef NS_SUBMODULE
470 #define NS_SUBMODULE ASPECT(routerset_union, source_bad)
473 * Structural test for routerset_union, when given a bad source argument.
476 NS_DECL(smartlist_t *, smartlist_new, (void));
478 static void
479 NS(test_main)(void *arg)
481 routerset_t *set, *bad_set;
482 (void)arg;
484 set = routerset_new();
485 bad_set = routerset_new();
486 smartlist_free(bad_set->list);
487 bad_set->list = NULL;
489 NS_MOCK(smartlist_new);
491 routerset_union(set, NULL);
492 tt_int_op(CALLED(smartlist_new), OP_EQ, 0);
494 routerset_union(set, bad_set);
495 tt_int_op(CALLED(smartlist_new), OP_EQ, 0);
497 done:
498 NS_UNMOCK(smartlist_new);
499 routerset_free(set);
501 /* Just recreate list, so we can simply use routerset_free. */
502 bad_set->list = smartlist_new();
503 routerset_free(bad_set);
506 static smartlist_t *
507 NS(smartlist_new)(void)
509 CALLED(smartlist_new)++;
511 return NULL;
514 #undef NS_SUBMODULE
515 #define NS_SUBMODULE ASPECT(routerset_union, one)
518 * Functional test for routerset_union.
521 static void
522 NS(test_main)(void *arg)
524 routerset_t *src = routerset_new();
525 routerset_t *tgt;
526 (void)arg;
528 tgt = routerset_new();
529 smartlist_add(src->list, tor_strdup("{xx}"));
530 routerset_union(tgt, src);
532 tt_int_op(smartlist_len(tgt->list), OP_NE, 0);
534 done:
535 routerset_free(src);
536 routerset_free(tgt);
539 #undef NS_SUBMODULE
540 #define NS_SUBMODULE routerset_is_list
543 * Functional tests for routerset_is_list.
546 static void
547 NS(test_main)(void *arg)
549 routerset_t *set;
550 addr_policy_t *policy;
551 int is_list;
552 (void)arg;
554 /* len(set->country_names) == 0, len(set->policies) == 0 */
555 set = routerset_new();
556 is_list = routerset_is_list(set);
557 routerset_free(set);
558 set = NULL;
559 tt_int_op(is_list, OP_NE, 0);
561 /* len(set->country_names) != 0, len(set->policies) == 0 */
562 set = routerset_new();
563 smartlist_add(set->country_names, tor_strndup("foo", 3));
564 is_list = routerset_is_list(set);
565 routerset_free(set);
566 set = NULL;
567 tt_int_op(is_list, OP_EQ, 0);
569 /* len(set->country_names) == 0, len(set->policies) != 0 */
570 set = routerset_new();
571 policy = tor_malloc_zero(sizeof(addr_policy_t));
572 smartlist_add(set->policies, (void *)policy);
573 is_list = routerset_is_list(set);
574 routerset_free(set);
575 set = NULL;
576 tt_int_op(is_list, OP_EQ, 0);
578 /* len(set->country_names) != 0, len(set->policies) != 0 */
579 set = routerset_new();
580 smartlist_add(set->country_names, tor_strndup("foo", 3));
581 policy = tor_malloc_zero(sizeof(addr_policy_t));
582 smartlist_add(set->policies, (void *)policy);
583 is_list = routerset_is_list(set);
584 routerset_free(set);
585 set = NULL;
586 tt_int_op(is_list, OP_EQ, 0);
588 done:
592 #undef NS_SUBMODULE
593 #define NS_SUBMODULE routerset_needs_geoip
596 * Functional tests for routerset_needs_geoip.
599 static void
600 NS(test_main)(void *arg)
602 const routerset_t *set;
603 int needs_geoip;
604 (void)arg;
606 set = NULL;
607 needs_geoip = routerset_needs_geoip(set);
608 tt_int_op(needs_geoip, OP_EQ, 0);
610 set = routerset_new();
611 needs_geoip = routerset_needs_geoip(set);
612 routerset_free((routerset_t *)set);
613 tt_int_op(needs_geoip, OP_EQ, 0);
614 set = NULL;
616 set = routerset_new();
617 smartlist_add(set->country_names, tor_strndup("xx", 2));
618 needs_geoip = routerset_needs_geoip(set);
619 routerset_free((routerset_t *)set);
620 set = NULL;
621 tt_int_op(needs_geoip, OP_NE, 0);
623 done:
627 #undef NS_SUBMODULE
628 #define NS_SUBMODULE routerset_is_empty
631 * Functional tests for routerset_is_empty.
634 static void
635 NS(test_main)(void *arg)
637 routerset_t *set = NULL;
638 int is_empty;
639 (void)arg;
641 is_empty = routerset_is_empty(set);
642 tt_int_op(is_empty, OP_NE, 0);
644 set = routerset_new();
645 is_empty = routerset_is_empty(set);
646 routerset_free(set);
647 set = NULL;
648 tt_int_op(is_empty, OP_NE, 0);
650 set = routerset_new();
651 smartlist_add(set->list, tor_strdup("{xx}"));
652 is_empty = routerset_is_empty(set);
653 routerset_free(set);
654 set = NULL;
655 tt_int_op(is_empty, OP_EQ, 0);
657 done:
661 #undef NS_SUBMODULE
662 #define NS_SUBMODULE ASPECT(routerset_contains, null_set_or_null_set_list)
665 * Functional test for routerset_contains, when given a NULL set or the
666 * set has a NULL list.
669 static void
670 NS(test_main)(void *arg)
672 routerset_t *set = NULL;
673 int contains;
674 (void)arg;
676 contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
678 tt_int_op(contains, OP_EQ, 0);
680 set = tor_malloc_zero(sizeof(routerset_t));
681 set->list = NULL;
682 contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
683 tor_free(set);
684 tt_int_op(contains, OP_EQ, 0);
686 done:
690 #undef NS_SUBMODULE
691 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_nickname)
694 * Functional test for routerset_contains, when given a valid routerset but a
695 * NULL nickname.
698 static void
699 NS(test_main)(void *arg)
701 routerset_t *set = routerset_new();
702 char *nickname = NULL;
703 int contains;
704 (void)arg;
706 contains = routerset_contains(set, NULL, 0, nickname, NULL, 0);
707 routerset_free(set);
709 tt_int_op(contains, OP_EQ, 0);
711 done:
715 #undef NS_SUBMODULE
716 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_nickname)
719 * Functional test for routerset_contains, when given a valid routerset
720 * and the nickname is in the routerset.
723 static void
724 NS(test_main)(void *arg)
726 routerset_t *set = routerset_new();
727 const char *nickname;
728 int contains;
729 (void)arg;
731 nickname = "Foo"; /* This tests the lowercase comparison as well. */
732 strmap_set_lc(set->names, nickname, (void *)1);
733 contains = routerset_contains(set, NULL, 0, nickname, NULL, 0);
734 routerset_free(set);
736 tt_int_op(contains, OP_EQ, 4);
737 done:
741 #undef NS_SUBMODULE
742 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_nickname)
745 * Functional test for routerset_contains, when given a valid routerset
746 * and the nickname is not in the routerset.
749 static void
750 NS(test_main)(void *arg)
752 routerset_t *set = routerset_new();
753 int contains;
754 (void)arg;
756 strmap_set_lc(set->names, "bar", (void *)1);
757 contains = routerset_contains(set, NULL, 0, "foo", NULL, 0);
758 routerset_free(set);
760 tt_int_op(contains, OP_EQ, 0);
761 done:
765 #undef NS_SUBMODULE
766 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_digest)
769 * Functional test for routerset_contains, when given a valid routerset
770 * and the digest is contained in the routerset.
773 static void
774 NS(test_main)(void *arg)
776 routerset_t *set = routerset_new();
777 int contains;
778 uint8_t foo[20] = { 2, 3, 4 };
779 (void)arg;
781 digestmap_set(set->digests, (const char*)foo, (void *)1);
782 contains = routerset_contains(set, NULL, 0, NULL, (const char*)foo, 0);
783 routerset_free(set);
785 tt_int_op(contains, OP_EQ, 4);
786 done:
790 #undef NS_SUBMODULE
791 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_digest)
794 * Functional test for routerset_contains, when given a valid routerset
795 * and the digest is not contained in the routerset.
798 static void
799 NS(test_main)(void *arg)
801 routerset_t *set = routerset_new();
802 int contains;
803 uint8_t bar[20] = { 9, 10, 11, 55 };
804 uint8_t foo[20] = { 1, 2, 3, 4};
805 (void)arg;
807 digestmap_set(set->digests, (const char*)bar, (void *)1);
808 contains = routerset_contains(set, NULL, 0, NULL, (const char*)foo, 0);
809 routerset_free(set);
811 tt_int_op(contains, OP_EQ, 0);
812 done:
816 #undef NS_SUBMODULE
817 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_digest)
820 * Functional test for routerset_contains, when given a valid routerset
821 * and the digest is NULL.
824 static void
825 NS(test_main)(void *arg)
827 routerset_t *set = routerset_new();
828 int contains;
829 uint8_t bar[20] = { 9, 10, 11, 55 };
830 (void)arg;
832 digestmap_set(set->digests, (const char*)bar, (void *)1);
833 contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
834 routerset_free(set);
836 tt_int_op(contains, OP_EQ, 0);
837 done:
841 #undef NS_SUBMODULE
842 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_addr)
845 * Structural test for routerset_contains, when given a valid routerset
846 * and the address is rejected by policy.
849 NS_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
850 (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
852 static tor_addr_t MOCK_TOR_ADDR;
853 #define MOCK_TOR_ADDR_PTR (&MOCK_TOR_ADDR)
855 static void
856 NS(test_main)(void *arg)
858 routerset_t *set = routerset_new();
859 tor_addr_t *addr = MOCK_TOR_ADDR_PTR;
860 int contains;
861 (void)arg;
863 NS_MOCK(compare_tor_addr_to_addr_policy);
865 contains = routerset_contains(set, addr, 0, NULL, NULL, 0);
866 routerset_free(set);
868 tt_int_op(CALLED(compare_tor_addr_to_addr_policy), OP_EQ, 1);
869 tt_int_op(contains, OP_EQ, 3);
871 done:
875 addr_policy_result_t
876 NS(compare_tor_addr_to_addr_policy)(const tor_addr_t *addr, uint16_t port,
877 const smartlist_t *policy)
879 (void)port;
880 (void)policy;
881 CALLED(compare_tor_addr_to_addr_policy)++;
882 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
883 return ADDR_POLICY_REJECTED;
885 done:
886 return 0;
889 #undef NS_SUBMODULE
890 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_addr)
893 * Structural test for routerset_contains, when given a valid routerset
894 * and the address is not rejected by policy.
897 NS_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
898 (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
900 static void
901 NS(test_main)(void *arg)
903 routerset_t *set = routerset_new();
904 tor_addr_t *addr = MOCK_TOR_ADDR_PTR;
905 int contains;
906 (void)arg;
908 NS_MOCK(compare_tor_addr_to_addr_policy);
910 contains = routerset_contains(set, addr, 0, NULL, NULL, 0);
911 routerset_free(set);
913 tt_int_op(CALLED(compare_tor_addr_to_addr_policy), OP_EQ, 1);
914 tt_int_op(contains, OP_EQ, 0);
916 done:
920 addr_policy_result_t
921 NS(compare_tor_addr_to_addr_policy)(const tor_addr_t *addr, uint16_t port,
922 const smartlist_t *policy)
924 (void)port;
925 (void)policy;
926 CALLED(compare_tor_addr_to_addr_policy)++;
927 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
929 return ADDR_POLICY_ACCEPTED;
931 done:
932 return 0;
935 #undef NS_SUBMODULE
936 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_addr)
939 * Structural test for routerset_contains, when given a valid routerset
940 * and the address is NULL.
943 NS_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
944 (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
946 static void
947 NS(test_main)(void *arg)
949 routerset_t *set = routerset_new();
950 int contains;
951 (void)arg;
953 NS_MOCK(compare_tor_addr_to_addr_policy);
955 contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
956 routerset_free(set);
958 tt_int_op(contains, OP_EQ, 0);
960 done:
964 addr_policy_result_t
965 NS(compare_tor_addr_to_addr_policy)(const tor_addr_t *addr, uint16_t port,
966 const smartlist_t *policy)
968 (void)port;
969 (void)policy;
970 CALLED(compare_tor_addr_to_addr_policy)++;
971 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
973 return ADDR_POLICY_ACCEPTED;
975 done:
976 return 0;
979 #undef NS_SUBMODULE
980 #define NS_SUBMODULE ASPECT(routerset_contains, countries_no_geoip)
983 * Structural test for routerset_contains, when there is no matching country
984 * for the address.
987 NS_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
988 (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
989 NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
991 static void
992 NS(test_main)(void *arg)
994 routerset_t *set = routerset_new();
995 int contains = 1;
996 (void)arg;
998 NS_MOCK(compare_tor_addr_to_addr_policy);
999 NS_MOCK(geoip_get_country_by_addr);
1001 set->countries = bitarray_init_zero(1);
1002 bitarray_set(set->countries, 1);
1003 contains = routerset_contains(set, MOCK_TOR_ADDR_PTR, 0, NULL, NULL, -1);
1004 routerset_free(set);
1006 tt_int_op(contains, OP_EQ, 0);
1007 tt_int_op(CALLED(compare_tor_addr_to_addr_policy), OP_EQ, 1);
1008 tt_int_op(CALLED(geoip_get_country_by_addr), OP_EQ, 1);
1010 done:
1014 addr_policy_result_t
1015 NS(compare_tor_addr_to_addr_policy)(const tor_addr_t *addr, uint16_t port,
1016 const smartlist_t *policy)
1018 (void)port;
1019 (void)policy;
1020 CALLED(compare_tor_addr_to_addr_policy)++;
1021 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
1023 done:
1024 return ADDR_POLICY_ACCEPTED;
1028 NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
1030 CALLED(geoip_get_country_by_addr)++;
1031 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
1033 done:
1034 return -1;
1037 #undef NS_SUBMODULE
1038 #define NS_SUBMODULE ASPECT(routerset_contains, countries_geoip)
1041 * Structural test for routerset_contains, when there a matching country
1042 * for the address.
1045 NS_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
1046 (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
1047 NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
1049 static void
1050 NS(test_main)(void *arg)
1052 routerset_t *set = routerset_new();
1053 int contains = 1;
1054 (void)arg;
1056 NS_MOCK(compare_tor_addr_to_addr_policy);
1057 NS_MOCK(geoip_get_country_by_addr);
1059 set->n_countries = 2;
1060 set->countries = bitarray_init_zero(1);
1061 bitarray_set(set->countries, 1);
1062 contains = routerset_contains(set, MOCK_TOR_ADDR_PTR, 0, NULL, NULL, -1);
1063 routerset_free(set);
1065 tt_int_op(contains, OP_EQ, 2);
1066 tt_int_op(CALLED(compare_tor_addr_to_addr_policy), OP_EQ, 1);
1067 tt_int_op(CALLED(geoip_get_country_by_addr), OP_EQ, 1);
1069 done:
1073 addr_policy_result_t
1074 NS(compare_tor_addr_to_addr_policy)(const tor_addr_t *addr, uint16_t port,
1075 const smartlist_t *policy)
1077 (void)port;
1078 (void)policy;
1079 CALLED(compare_tor_addr_to_addr_policy)++;
1080 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
1082 done:
1083 return ADDR_POLICY_ACCEPTED;
1087 NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
1089 CALLED(geoip_get_country_by_addr)++;
1090 tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
1092 done:
1093 return 1;
1096 #undef NS_SUBMODULE
1097 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, only_flag_and_no_ccs)
1100 * Functional test for routerset_add_unknown_ccs, where only_if_some_cc_set
1101 * is set and there are no country names.
1104 static void
1105 NS(test_main)(void *arg)
1107 routerset_t *set = routerset_new();
1108 routerset_t **setp = &set;
1109 int r;
1110 (void)arg;
1112 r = routerset_add_unknown_ccs(setp, 1);
1114 tt_int_op(r, OP_EQ, 0);
1116 done:
1117 routerset_free(set);
1120 #undef NS_SUBMODULE
1121 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, creates_set)
1124 * Functional test for routerset_add_unknown_ccs, where the set argument
1125 * is created if passed in as NULL.
1128 /* The mock is only used to stop the test from asserting erroneously. */
1129 NS_DECL(country_t, geoip_get_country, (const char *country));
1131 static void
1132 NS(test_main)(void *arg)
1134 routerset_t *set = NULL;
1135 routerset_t **setp = &set;
1136 int r;
1137 (void)arg;
1139 NS_MOCK(geoip_get_country);
1141 r = routerset_add_unknown_ccs(setp, 0);
1143 tt_ptr_op(*setp, OP_NE, NULL);
1144 tt_int_op(r, OP_EQ, 0);
1146 done:
1147 if (set != NULL)
1148 routerset_free(set);
1151 country_t
1152 NS(geoip_get_country)(const char *country)
1154 (void)country;
1155 CALLED(geoip_get_country)++;
1157 return -1;
1160 #undef NS_SUBMODULE
1161 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, add_unknown)
1164 * Structural test for routerset_add_unknown_ccs, that the "{??}"
1165 * country code is added to the list.
1168 NS_DECL(country_t, geoip_get_country, (const char *country));
1169 NS_DECL(int, geoip_is_loaded, (sa_family_t family));
1171 static void
1172 NS(test_main)(void *arg)
1174 routerset_t *set = routerset_new();
1175 routerset_t **setp = &set;
1176 int r;
1177 (void)arg;
1179 NS_MOCK(geoip_get_country);
1180 NS_MOCK(geoip_is_loaded);
1182 r = routerset_add_unknown_ccs(setp, 0);
1184 tt_int_op(r, OP_EQ, 1);
1185 tt_int_op(smartlist_contains_string(set->country_names, "??"), OP_EQ, 1);
1186 tt_int_op(smartlist_contains_string(set->list, "{??}"), OP_EQ, 1);
1188 done:
1189 if (set != NULL)
1190 routerset_free(set);
1193 country_t
1194 NS(geoip_get_country)(const char *country)
1196 int arg_is_qq, arg_is_a1;
1198 CALLED(geoip_get_country)++;
1200 arg_is_qq = !strcmp(country, "??");
1201 arg_is_a1 = !strcmp(country, "A1");
1203 tt_int_op(arg_is_qq || arg_is_a1, OP_EQ, 1);
1205 if (arg_is_qq)
1206 return 1;
1208 done:
1209 return -1;
1213 NS(geoip_is_loaded)(sa_family_t family)
1215 CALLED(geoip_is_loaded)++;
1217 tt_int_op(family, OP_EQ, AF_INET);
1219 done:
1220 return 0;
1223 #undef NS_SUBMODULE
1224 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, add_a1)
1227 * Structural test for routerset_add_unknown_ccs, that the "{a1}"
1228 * country code is added to the list.
1231 NS_DECL(country_t, geoip_get_country, (const char *country));
1232 NS_DECL(int, geoip_is_loaded, (sa_family_t family));
1234 static void
1235 NS(test_main)(void *arg)
1237 routerset_t *set = routerset_new();
1238 routerset_t **setp = &set;
1239 int r;
1240 (void)arg;
1242 NS_MOCK(geoip_get_country);
1243 NS_MOCK(geoip_is_loaded);
1245 r = routerset_add_unknown_ccs(setp, 0);
1247 tt_int_op(r, OP_EQ, 1);
1248 tt_int_op(smartlist_contains_string(set->country_names, "a1"), OP_EQ, 1);
1249 tt_int_op(smartlist_contains_string(set->list, "{a1}"), OP_EQ, 1);
1251 done:
1252 if (set != NULL)
1253 routerset_free(set);
1256 country_t
1257 NS(geoip_get_country)(const char *country)
1259 int arg_is_qq, arg_is_a1;
1261 CALLED(geoip_get_country)++;
1263 arg_is_qq = !strcmp(country, "??");
1264 arg_is_a1 = !strcmp(country, "A1");
1266 tt_int_op(arg_is_qq || arg_is_a1, OP_EQ, 1);
1268 if (arg_is_a1)
1269 return 1;
1271 done:
1272 return -1;
1276 NS(geoip_is_loaded)(sa_family_t family)
1278 CALLED(geoip_is_loaded)++;
1280 tt_int_op(family, OP_EQ, AF_INET);
1282 done:
1283 return 0;
1286 #undef NS_SUBMODULE
1287 #define NS_SUBMODULE routerset_contains_extendinfo
1290 * Functional test for routerset_contains_extendinfo.
1293 static void
1294 NS(test_main)(void *arg)
1296 routerset_t *set = routerset_new();
1297 extend_info_t ei;
1298 int r;
1299 const char *nickname = "foo";
1300 (void)arg;
1302 memset(&ei, 0, sizeof(ei));
1303 strmap_set_lc(set->names, nickname, (void *)1);
1304 strncpy(ei.nickname, nickname, sizeof(ei.nickname) - 1);
1305 ei.nickname[sizeof(ei.nickname) - 1] = '\0';
1307 r = routerset_contains_extendinfo(set, &ei);
1309 tt_int_op(r, OP_EQ, 4);
1310 done:
1311 routerset_free(set);
1314 #undef NS_SUBMODULE
1315 #define NS_SUBMODULE routerset_contains_router
1318 * Functional test for routerset_contains_router.
1321 static void
1322 NS(test_main)(void *arg)
1324 routerset_t *set = routerset_new();
1325 routerinfo_t ri;
1326 country_t country = 1;
1327 int r;
1328 const char *nickname = "foo";
1329 (void)arg;
1331 memset(&ri, 0, sizeof(ri));
1332 strmap_set_lc(set->names, nickname, (void *)1);
1333 ri.nickname = (char *)nickname;
1335 r = routerset_contains_router(set, &ri, country);
1337 tt_int_op(r, OP_EQ, 4);
1338 done:
1339 routerset_free(set);
1342 #undef NS_SUBMODULE
1343 #define NS_SUBMODULE routerset_contains_routerstatus
1346 * Functional test for routerset_contains_routerstatus.
1349 // XXX: This is a bit brief. It only populates and tests the nickname fields
1350 // ie., enough to make the containment check succeed. Perhaps it should do
1351 // a bit more or test a bit more.
1353 static void
1354 NS(test_main)(void *arg)
1356 routerset_t *set = routerset_new();
1357 routerstatus_t rs;
1358 country_t country = 1;
1359 int r;
1360 const char *nickname = "foo";
1361 (void)arg;
1363 memset(&rs, 0, sizeof(rs));
1364 strmap_set_lc(set->names, nickname, (void *)1);
1365 strncpy(rs.nickname, nickname, sizeof(rs.nickname) - 1);
1366 rs.nickname[sizeof(rs.nickname) - 1] = '\0';
1368 r = routerset_contains_routerstatus(set, &rs, country);
1370 tt_int_op(r, OP_EQ, 4);
1371 done:
1372 routerset_free(set);
1375 #undef NS_SUBMODULE
1376 #define NS_SUBMODULE ASPECT(routerset_contains_node, none)
1379 * Functional test for routerset_contains_node, when the node has no
1380 * routerset or routerinfo.
1383 node_t NS(mock_node);
1385 static void
1386 NS(test_main)(void *arg)
1388 routerset_t *set = routerset_new();
1389 int r;
1390 (void)arg;
1392 NS(mock_node).ri = NULL;
1393 NS(mock_node).rs = NULL;
1395 r = routerset_contains_node(set, &NS(mock_node));
1396 tt_int_op(r, OP_EQ, 0);
1398 done:
1399 routerset_free(set);
1402 #undef NS_SUBMODULE
1403 #define NS_SUBMODULE ASPECT(routerset_contains_node, routerstatus)
1406 * Functional test for routerset_contains_node, when the node has a
1407 * routerset and no routerinfo.
1410 node_t NS(mock_node);
1412 static void
1413 NS(test_main)(void *arg)
1415 routerset_t *set = routerset_new();
1416 int r;
1417 const char *nickname = "foo";
1418 routerstatus_t rs;
1419 (void)arg;
1421 strmap_set_lc(set->names, nickname, (void *)1);
1423 strncpy(rs.nickname, nickname, sizeof(rs.nickname) - 1);
1424 rs.nickname[sizeof(rs.nickname) - 1] = '\0';
1425 NS(mock_node).ri = NULL;
1426 NS(mock_node).rs = &rs;
1428 r = routerset_contains_node(set, &NS(mock_node));
1430 tt_int_op(r, OP_EQ, 4);
1431 done:
1432 routerset_free(set);
1435 #undef NS_SUBMODULE
1436 #define NS_SUBMODULE ASPECT(routerset_contains_node, routerinfo)
1439 * Functional test for routerset_contains_node, when the node has no
1440 * routerset and a routerinfo.
1443 static void
1444 NS(test_main)(void *arg)
1446 routerset_t *set = routerset_new();
1447 int r;
1448 const char *nickname = "foo";
1449 routerinfo_t ri;
1450 node_t mock_node;
1451 (void)arg;
1453 strmap_set_lc(set->names, nickname, (void *)1);
1455 ri.nickname = (char *)nickname;
1456 mock_node.ri = &ri;
1457 mock_node.rs = NULL;
1459 r = routerset_contains_node(set, &mock_node);
1461 tt_int_op(r, OP_EQ, 4);
1462 done:
1463 routerset_free(set);
1466 #undef NS_SUBMODULE
1467 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, no_routerset)
1470 * Functional test for routerset_get_all_nodes, when routerset is NULL or
1471 * the routerset list is NULL.
1474 static void
1475 NS(test_main)(void *arg)
1477 smartlist_t *out = smartlist_new();
1478 routerset_t *set = NULL;
1479 (void)arg;
1481 tt_int_op(smartlist_len(out), OP_EQ, 0);
1482 routerset_get_all_nodes(out, NULL, NULL, 0);
1484 tt_int_op(smartlist_len(out), OP_EQ, 0);
1486 set = routerset_new();
1487 smartlist_free(set->list);
1488 routerset_get_all_nodes(out, NULL, NULL, 0);
1489 tt_int_op(smartlist_len(out), OP_EQ, 0);
1491 /* Just recreate list, so we can simply use routerset_free. */
1492 set->list = smartlist_new();
1494 done:
1495 routerset_free(set);
1496 smartlist_free(out);
1499 #undef NS_SUBMODULE
1500 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list_with_no_nodes)
1503 * Structural test for routerset_get_all_nodes, when the routerset list
1504 * is empty.
1507 NS_DECL(const node_t *, node_get_by_nickname,
1508 (const char *nickname, int warn_if_unused));
1509 const char *NS(mock_nickname);
1511 static void
1512 NS(test_main)(void *arg)
1514 smartlist_t *out = smartlist_new();
1515 routerset_t *set = routerset_new();
1516 int out_len;
1517 (void)arg;
1519 NS_MOCK(node_get_by_nickname);
1521 NS(mock_nickname) = "foo";
1522 smartlist_add(set->list, tor_strdup(NS(mock_nickname)));
1524 routerset_get_all_nodes(out, set, NULL, 0);
1525 out_len = smartlist_len(out);
1527 smartlist_free(out);
1528 routerset_free(set);
1530 tt_int_op(out_len, OP_EQ, 0);
1531 tt_int_op(CALLED(node_get_by_nickname), OP_EQ, 1);
1533 done:
1537 const node_t *
1538 NS(node_get_by_nickname)(const char *nickname, int warn_if_unused)
1540 CALLED(node_get_by_nickname)++;
1541 tt_str_op(nickname, OP_EQ, NS(mock_nickname));
1542 tt_int_op(warn_if_unused, OP_EQ, 1);
1544 done:
1545 return NULL;
1548 #undef NS_SUBMODULE
1549 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list_flag_not_running)
1552 * Structural test for routerset_get_all_nodes, with the running_only flag
1553 * is set but the nodes are not running.
1556 NS_DECL(const node_t *, node_get_by_nickname,
1557 (const char *nickname, int warn_if_unused));
1558 const char *NS(mock_nickname);
1559 node_t NS(mock_node);
1561 static void
1562 NS(test_main)(void *arg)
1564 smartlist_t *out = smartlist_new();
1565 routerset_t *set = routerset_new();
1566 int out_len;
1567 (void)arg;
1569 NS_MOCK(node_get_by_nickname);
1571 NS(mock_node).is_running = 0;
1572 NS(mock_nickname) = "foo";
1573 smartlist_add(set->list, tor_strdup(NS(mock_nickname)));
1575 routerset_get_all_nodes(out, set, NULL, 1);
1576 out_len = smartlist_len(out);
1578 smartlist_free(out);
1579 routerset_free(set);
1581 tt_int_op(out_len, OP_EQ, 0);
1582 tt_int_op(CALLED(node_get_by_nickname), OP_EQ, 1);
1584 done:
1588 const node_t *
1589 NS(node_get_by_nickname)(const char *nickname, int warn_if_unused)
1591 CALLED(node_get_by_nickname)++;
1592 tt_str_op(nickname, OP_EQ, NS(mock_nickname));
1593 tt_int_op(warn_if_unused, OP_EQ, 1);
1595 done:
1596 return &NS(mock_node);
1599 #undef NS_SUBMODULE
1600 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list)
1603 * Structural test for routerset_get_all_nodes.
1606 NS_DECL(const node_t *, node_get_by_nickname,
1607 (const char *nickname, int warn_if_unused));
1608 char *NS(mock_nickname);
1609 node_t NS(mock_node);
1611 static void
1612 NS(test_main)(void *arg)
1614 smartlist_t *out = smartlist_new();
1615 routerset_t *set = routerset_new();
1616 int out_len;
1617 node_t *ent;
1618 (void)arg;
1620 NS_MOCK(node_get_by_nickname);
1622 NS(mock_nickname) = tor_strdup("foo");
1623 smartlist_add(set->list, NS(mock_nickname));
1625 routerset_get_all_nodes(out, set, NULL, 0);
1626 out_len = smartlist_len(out);
1627 ent = (node_t *)smartlist_get(out, 0);
1629 smartlist_free(out);
1630 routerset_free(set);
1632 tt_int_op(out_len, OP_EQ, 1);
1633 tt_ptr_op(ent, OP_EQ, &NS(mock_node));
1634 tt_int_op(CALLED(node_get_by_nickname), OP_EQ, 1);
1636 done:
1640 const node_t *
1641 NS(node_get_by_nickname)(const char *nickname, int warn_if_unused)
1643 CALLED(node_get_by_nickname)++;
1644 tt_str_op(nickname, OP_EQ, NS(mock_nickname));
1645 tt_int_op(warn_if_unused, OP_EQ, 1);
1647 done:
1648 return &NS(mock_node);
1651 #undef NS_SUBMODULE
1652 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, nodelist_with_no_nodes)
1655 * Structural test for routerset_get_all_nodes, when the nodelist has no nodes.
1658 NS_DECL(smartlist_t *, nodelist_get_list, (void));
1660 smartlist_t *NS(mock_smartlist);
1662 static void
1663 NS(test_main)(void *arg)
1665 routerset_t *set = routerset_new();
1666 smartlist_t *out = smartlist_new();
1667 int r;
1668 (void)arg;
1670 NS_MOCK(nodelist_get_list);
1672 smartlist_add(set->country_names, tor_strdup("{xx}"));
1673 NS(mock_smartlist) = smartlist_new();
1675 routerset_get_all_nodes(out, set, NULL, 1);
1676 r = smartlist_len(out);
1677 routerset_free(set);
1678 smartlist_free(out);
1679 smartlist_free(NS(mock_smartlist));
1681 tt_int_op(r, OP_EQ, 0);
1682 tt_int_op(CALLED(nodelist_get_list), OP_EQ, 1);
1684 done:
1688 smartlist_t *
1689 NS(nodelist_get_list)(void)
1691 CALLED(nodelist_get_list)++;
1693 return NS(mock_smartlist);
1696 #undef NS_SUBMODULE
1697 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, nodelist_flag_not_running)
1700 * Structural test for routerset_get_all_nodes, with a non-list routerset
1701 * the running_only flag is set, but the nodes are not running.
1704 NS_DECL(smartlist_t *, nodelist_get_list, (void));
1706 smartlist_t *NS(mock_smartlist);
1707 node_t NS(mock_node);
1709 static void
1710 NS(test_main)(void *arg)
1712 routerset_t *set = routerset_new();
1713 smartlist_t *out = smartlist_new();
1714 int r;
1715 (void)arg;
1717 NS_MOCK(nodelist_get_list);
1719 smartlist_add(set->country_names, tor_strdup("{xx}"));
1720 NS(mock_smartlist) = smartlist_new();
1721 NS(mock_node).is_running = 0;
1722 smartlist_add(NS(mock_smartlist), (void *)&NS(mock_node));
1724 routerset_get_all_nodes(out, set, NULL, 1);
1725 r = smartlist_len(out);
1726 routerset_free(set);
1727 smartlist_free(out);
1728 smartlist_free(NS(mock_smartlist));
1730 tt_int_op(r, OP_EQ, 0);
1731 tt_int_op(CALLED(nodelist_get_list), OP_EQ, 1);
1733 done:
1737 smartlist_t *
1738 NS(nodelist_get_list)(void)
1740 CALLED(nodelist_get_list)++;
1742 return NS(mock_smartlist);
1745 #undef NS_SUBMODULE
1746 #define NS_SUBMODULE routerset_subtract_nodes
1749 * Functional test for routerset_subtract_nodes.
1752 static void
1753 NS(test_main)(void *arg)
1755 routerset_t *set = routerset_new();
1756 smartlist_t *list = smartlist_new();
1757 const char *nickname = "foo";
1758 routerinfo_t ri;
1759 node_t mock_node;
1760 (void)arg;
1762 strmap_set_lc(set->names, nickname, (void *)1);
1764 ri.nickname = (char *)nickname;
1765 mock_node.rs = NULL;
1766 mock_node.ri = &ri;
1767 smartlist_add(list, (void *)&mock_node);
1769 tt_int_op(smartlist_len(list), OP_NE, 0);
1770 routerset_subtract_nodes(list, set);
1772 tt_int_op(smartlist_len(list), OP_EQ, 0);
1773 done:
1774 routerset_free(set);
1775 smartlist_free(list);
1778 #undef NS_SUBMODULE
1779 #define NS_SUBMODULE ASPECT(routerset_subtract_nodes, null_routerset)
1782 * Functional test for routerset_subtract_nodes, with a NULL routerset.
1785 static void
1786 NS(test_main)(void *arg)
1788 routerset_t *set = NULL;
1789 smartlist_t *list = smartlist_new();
1790 const char *nickname = "foo";
1791 routerinfo_t ri;
1792 node_t mock_node;
1793 (void)arg;
1795 ri.nickname = (char *)nickname;
1796 mock_node.ri = &ri;
1797 smartlist_add(list, (void *)&mock_node);
1799 tt_int_op(smartlist_len(list), OP_NE, 0);
1800 routerset_subtract_nodes(list, set);
1802 tt_int_op(smartlist_len(list), OP_NE, 0);
1803 done:
1804 routerset_free(set);
1805 smartlist_free(list);
1808 #undef NS_SUBMODULE
1809 #define NS_SUBMODULE routerset_to_string
1812 * Functional test for routerset_to_string.
1815 static void
1816 NS(test_main)(void *arg)
1818 routerset_t *set = NULL;
1819 char *s = NULL;
1820 (void)arg;
1822 set = NULL;
1823 s = routerset_to_string(set);
1824 tt_str_op(s, OP_EQ, "");
1825 tor_free(s);
1827 set = routerset_new();
1828 s = routerset_to_string(set);
1829 tt_str_op(s, OP_EQ, "");
1830 tor_free(s);
1831 routerset_free(set); set = NULL;
1833 set = routerset_new();
1834 smartlist_add(set->list, tor_strndup("a", 1));
1835 s = routerset_to_string(set);
1836 tt_str_op(s, OP_EQ, "a");
1837 tor_free(s);
1838 routerset_free(set); set = NULL;
1840 set = routerset_new();
1841 smartlist_add(set->list, tor_strndup("a", 1));
1842 smartlist_add(set->list, tor_strndup("b", 1));
1843 s = routerset_to_string(set);
1844 tt_str_op(s, OP_EQ, "a,b");
1845 tor_free(s);
1846 routerset_free(set); set = NULL;
1848 done:
1849 tor_free(s);
1850 routerset_free((routerset_t *)set);
1853 #undef NS_SUBMODULE
1854 #define NS_SUBMODULE ASPECT(routerset_equal, empty_empty)
1857 * Functional test for routerset_equal, with both routersets empty.
1860 static void
1861 NS(test_main)(void *arg)
1863 routerset_t *a = routerset_new(), *b = routerset_new();
1864 int r;
1865 (void)arg;
1867 r = routerset_equal(a, b);
1868 routerset_free(a);
1869 routerset_free(b);
1871 tt_int_op(r, OP_EQ, 1);
1873 done:
1877 #undef NS_SUBMODULE
1878 #define NS_SUBMODULE ASPECT(routerset_equal, empty_not_empty)
1881 * Functional test for routerset_equal, with one routersets empty.
1884 static void
1885 NS(test_main)(void *arg)
1887 routerset_t *a = routerset_new(), *b = routerset_new();
1888 int r;
1889 (void)arg;
1891 smartlist_add(b->list, tor_strdup("{xx}"));
1892 r = routerset_equal(a, b);
1893 routerset_free(a);
1894 routerset_free(b);
1896 tt_int_op(r, OP_EQ, 0);
1897 done:
1901 #undef NS_SUBMODULE
1902 #define NS_SUBMODULE ASPECT(routerset_equal, differing_lengths)
1905 * Functional test for routerset_equal, with the routersets having
1906 * differing lengths.
1909 static void
1910 NS(test_main)(void *arg)
1912 routerset_t *a = routerset_new(), *b = routerset_new();
1913 int r;
1914 (void)arg;
1916 smartlist_add(a->list, tor_strdup("{aa}"));
1917 smartlist_add(b->list, tor_strdup("{b1}"));
1918 smartlist_add(b->list, tor_strdup("{b2}"));
1919 r = routerset_equal(a, b);
1920 routerset_free(a);
1921 routerset_free(b);
1923 tt_int_op(r, OP_EQ, 0);
1924 done:
1928 #undef NS_SUBMODULE
1929 #define NS_SUBMODULE ASPECT(routerset_equal, unequal)
1932 * Functional test for routerset_equal, with the routersets being
1933 * different.
1936 static void
1937 NS(test_main)(void *arg)
1939 routerset_t *a = routerset_new(), *b = routerset_new();
1940 int r;
1941 (void)arg;
1943 smartlist_add(a->list, tor_strdup("foo"));
1944 smartlist_add(b->list, tor_strdup("bar"));
1945 r = routerset_equal(a, b);
1946 routerset_free(a);
1947 routerset_free(b);
1949 tt_int_op(r, OP_EQ, 0);
1950 done:
1954 #undef NS_SUBMODULE
1955 #define NS_SUBMODULE ASPECT(routerset_equal, equal)
1958 * Functional test for routerset_equal, with the routersets being
1959 * equal.
1962 static void
1963 NS(test_main)(void *arg)
1965 routerset_t *a = routerset_new(), *b = routerset_new();
1966 int r;
1967 (void)arg;
1969 smartlist_add(a->list, tor_strdup("foo"));
1970 smartlist_add(b->list, tor_strdup("foo"));
1971 r = routerset_equal(a, b);
1972 routerset_free(a);
1973 routerset_free(b);
1975 tt_int_op(r, OP_EQ, 1);
1976 done:
1980 #undef NS_SUBMODULE
1981 #define NS_SUBMODULE ASPECT(routerset_free, null_routerset)
1984 * Structural test for routerset_free, where the routerset is NULL.
1987 NS_DECL(void, smartlist_free, (smartlist_t *sl));
1989 static void
1990 NS(test_main)(void *arg)
1992 (void)arg;
1994 NS_MOCK(smartlist_free);
1996 routerset_free(NULL);
1998 tt_int_op(CALLED(smartlist_free), OP_EQ, 0);
2000 done:
2004 void
2005 NS(smartlist_free)(smartlist_t *s)
2007 (void)s;
2008 CALLED(smartlist_free)++;
2011 #undef NS_SUBMODULE
2012 #define NS_SUBMODULE routerset_free
2015 * Structural test for routerset_free.
2018 NS_DECL(void, smartlist_free, (smartlist_t *sl));
2019 NS_DECL(void, strmap_free,(strmap_t *map, void (*free_val)(void*)));
2020 NS_DECL(void, digestmap_free, (digestmap_t *map, void (*free_val)(void*)));
2022 static void
2023 NS(test_main)(void *arg)
2025 routerset_t *routerset = routerset_new();
2026 (void)arg;
2028 NS_MOCK(smartlist_free);
2029 NS_MOCK(strmap_free);
2030 NS_MOCK(digestmap_free);
2032 routerset_free(routerset);
2034 tt_int_op(CALLED(smartlist_free), OP_NE, 0);
2035 tt_int_op(CALLED(strmap_free), OP_NE, 0);
2036 tt_int_op(CALLED(digestmap_free), OP_NE, 0);
2038 done:
2042 void
2043 NS(smartlist_free)(smartlist_t *s)
2045 CALLED(smartlist_free)++;
2046 smartlist_free__real(s);
2049 void
2050 NS(strmap_free)(strmap_t *map, void (*free_val)(void*))
2052 CALLED(strmap_free)++;
2053 strmap_free__real(map, free_val);
2056 void
2057 NS(digestmap_free)(digestmap_t *map, void (*free_val)(void*))
2059 CALLED(digestmap_free)++;
2060 digestmap_free__real(map, free_val);
2063 #undef NS_SUBMODULE
2065 struct testcase_t routerset_tests[] = {
2066 TEST_CASE(routerset_new),
2067 TEST_CASE(routerset_get_countryname),
2068 TEST_CASE(routerset_is_list),
2069 TEST_CASE(routerset_needs_geoip),
2070 TEST_CASE(routerset_is_empty),
2071 TEST_CASE_ASPECT(routerset_contains, null_set_or_null_set_list),
2072 TEST_CASE_ASPECT(routerset_contains, set_and_nickname),
2073 TEST_CASE_ASPECT(routerset_contains, set_and_null_nickname),
2074 TEST_CASE_ASPECT(routerset_contains, set_and_no_nickname),
2075 TEST_CASE_ASPECT(routerset_contains, set_and_digest),
2076 TEST_CASE_ASPECT(routerset_contains, set_and_no_digest),
2077 TEST_CASE_ASPECT(routerset_contains, set_and_null_digest),
2078 TEST_CASE_ASPECT(routerset_contains, set_and_addr),
2079 TEST_CASE_ASPECT(routerset_contains, set_and_no_addr),
2080 TEST_CASE_ASPECT(routerset_contains, set_and_null_addr),
2081 TEST_CASE_ASPECT(routerset_contains, countries_no_geoip),
2082 TEST_CASE_ASPECT(routerset_contains, countries_geoip),
2083 TEST_CASE_ASPECT(routerset_add_unknown_ccs, only_flag_and_no_ccs),
2084 TEST_CASE_ASPECT(routerset_add_unknown_ccs, creates_set),
2085 TEST_CASE_ASPECT(routerset_add_unknown_ccs, add_unknown),
2086 TEST_CASE_ASPECT(routerset_add_unknown_ccs, add_a1),
2087 TEST_CASE(routerset_contains_extendinfo),
2088 TEST_CASE(routerset_contains_router),
2089 TEST_CASE(routerset_contains_routerstatus),
2090 TEST_CASE_ASPECT(routerset_contains_node, none),
2091 TEST_CASE_ASPECT(routerset_contains_node, routerinfo),
2092 TEST_CASE_ASPECT(routerset_contains_node, routerstatus),
2093 TEST_CASE_ASPECT(routerset_get_all_nodes, no_routerset),
2094 TEST_CASE_ASPECT(routerset_get_all_nodes, list_with_no_nodes),
2095 TEST_CASE_ASPECT(routerset_get_all_nodes, list_flag_not_running),
2096 TEST_CASE_ASPECT(routerset_get_all_nodes, list),
2097 TEST_CASE_ASPECT(routerset_get_all_nodes, nodelist_with_no_nodes),
2098 TEST_CASE_ASPECT(routerset_get_all_nodes, nodelist_flag_not_running),
2099 TEST_CASE_ASPECT(routerset_refresh_counties, geoip_not_loaded),
2100 TEST_CASE_ASPECT(routerset_refresh_counties, no_countries),
2101 TEST_CASE_ASPECT(routerset_refresh_counties, one_valid_country),
2102 TEST_CASE_ASPECT(routerset_refresh_counties, one_invalid_country),
2103 TEST_CASE_ASPECT(routerset_union, source_bad),
2104 TEST_CASE_ASPECT(routerset_union, one),
2105 TEST_CASE_ASPECT(routerset_parse, malformed),
2106 TEST_CASE_ASPECT(routerset_parse, valid_hexdigest),
2107 TEST_CASE_ASPECT(routerset_parse, valid_nickname),
2108 TEST_CASE_ASPECT(routerset_parse, get_countryname),
2109 TEST_CASE_ASPECT(routerset_parse, policy),
2110 TEST_CASE(routerset_subtract_nodes),
2111 TEST_CASE_ASPECT(routerset_subtract_nodes, null_routerset),
2112 TEST_CASE(routerset_to_string),
2113 TEST_CASE_ASPECT(routerset_equal, empty_empty),
2114 TEST_CASE_ASPECT(routerset_equal, empty_not_empty),
2115 TEST_CASE_ASPECT(routerset_equal, differing_lengths),
2116 TEST_CASE_ASPECT(routerset_equal, unequal),
2117 TEST_CASE_ASPECT(routerset_equal, equal),
2118 TEST_CASE_ASPECT(routerset_free, null_routerset),
2119 TEST_CASE(routerset_free),
2120 END_OF_TESTCASES