1 /* Copyright (c) 2014-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define ROUTERSET_PRIVATE
6 #include "core/or/or.h"
7 #include "lib/geoip/geoip.h"
8 #include "feature/nodelist/routerset.h"
9 #include "feature/nodelist/routerparse.h"
10 #include "core/or/policies.h"
11 #include "feature/nodelist/nodelist.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"
21 #define NS_MODULE routerset
23 #define NS_SUBMODULE routerset_new
26 * Functional (blackbox) test to determine that each member of the routerset
31 NS(test_main
)(void *arg
)
38 tt_ptr_op(rs
, OP_NE
, NULL
);
39 tt_ptr_op(rs
->list
, OP_NE
, NULL
);
40 tt_ptr_op(rs
->names
, OP_NE
, NULL
);
41 tt_ptr_op(rs
->digests
, OP_NE
, NULL
);
42 tt_ptr_op(rs
->policies
, OP_NE
, NULL
);
43 tt_ptr_op(rs
->country_names
, OP_NE
, NULL
);
50 #define NS_SUBMODULE routerset_get_countryname
53 * Functional test to strip the braces from a "{xx}" country code string.
57 NS(test_main
)(void *arg
)
65 name
= routerset_get_countryname(input
);
66 tt_ptr_op(name
, OP_EQ
, NULL
);
71 name
= routerset_get_countryname(input
);
72 tt_ptr_op(name
, OP_EQ
, NULL
);
77 name
= routerset_get_countryname(input
);
78 tt_ptr_op(name
, OP_EQ
, NULL
);
83 name
= routerset_get_countryname(input
);
84 tt_str_op(name
, OP_EQ
, "xx");
88 name
= routerset_get_countryname(input
);
89 tt_str_op(name
, OP_EQ
, "xx");
95 #define NS_SUBMODULE ASPECT(routerset_refresh_counties, geoip_not_loaded)
98 * Structural (whitebox) test for routerset_refresh_counties, when the GeoIP DB
102 NS_DECL(int, geoip_is_loaded
, (sa_family_t family
));
103 NS_DECL(int, geoip_get_n_countries
, (void));
106 NS(test_main
)(void *arg
)
108 routerset_t
*set
= routerset_new();
111 NS_MOCK(geoip_is_loaded
);
112 NS_MOCK(geoip_get_n_countries
);
114 routerset_refresh_countries(set
);
116 tt_ptr_op(set
->countries
, OP_EQ
, NULL
);
117 tt_int_op(set
->n_countries
, OP_EQ
, 0);
118 tt_int_op(CALLED(geoip_is_loaded
), OP_EQ
, 1);
119 tt_int_op(CALLED(geoip_get_n_countries
), OP_EQ
, 0);
122 NS_UNMOCK(geoip_is_loaded
);
123 NS_UNMOCK(geoip_get_n_countries
);
128 NS(geoip_is_loaded
)(sa_family_t family
)
131 CALLED(geoip_is_loaded
)++;
137 NS(geoip_get_n_countries
)(void)
139 CALLED(geoip_get_n_countries
)++;
145 #define NS_SUBMODULE ASPECT(routerset_refresh_counties, no_countries)
148 * Structural test for routerset_refresh_counties, when there are no countries.
151 NS_DECL(int, geoip_is_loaded
, (sa_family_t family
));
152 NS_DECL(int, geoip_get_n_countries
, (void));
153 NS_DECL(country_t
, geoip_get_country
, (const char *country
));
156 NS(test_main
)(void *arg
)
158 routerset_t
*set
= routerset_new();
161 NS_MOCK(geoip_is_loaded
);
162 NS_MOCK(geoip_get_n_countries
);
163 NS_MOCK(geoip_get_country
);
165 routerset_refresh_countries(set
);
167 tt_ptr_op(set
->countries
, OP_NE
, NULL
);
168 tt_int_op(set
->n_countries
, OP_EQ
, 1);
169 tt_int_op((unsigned int)(*set
->countries
), OP_EQ
, 0);
170 tt_int_op(CALLED(geoip_is_loaded
), OP_EQ
, 1);
171 tt_int_op(CALLED(geoip_get_n_countries
), OP_EQ
, 1);
172 tt_int_op(CALLED(geoip_get_country
), OP_EQ
, 0);
175 NS_UNMOCK(geoip_is_loaded
);
176 NS_UNMOCK(geoip_get_n_countries
);
177 NS_UNMOCK(geoip_get_country
);
182 NS(geoip_is_loaded
)(sa_family_t family
)
185 CALLED(geoip_is_loaded
)++;
191 NS(geoip_get_n_countries
)(void)
193 CALLED(geoip_get_n_countries
)++;
199 NS(geoip_get_country
)(const char *countrycode
)
202 CALLED(geoip_get_country
)++;
208 #define NS_SUBMODULE ASPECT(routerset_refresh_counties, one_valid_country)
211 * Structural test for routerset_refresh_counties, with one valid country.
214 NS_DECL(int, geoip_is_loaded
, (sa_family_t family
));
215 NS_DECL(int, geoip_get_n_countries
, (void));
216 NS_DECL(country_t
, geoip_get_country
, (const char *country
));
219 NS(test_main
)(void *arg
)
221 routerset_t
*set
= routerset_new();
224 NS_MOCK(geoip_is_loaded
);
225 NS_MOCK(geoip_get_n_countries
);
226 NS_MOCK(geoip_get_country
);
227 smartlist_add(set
->country_names
, tor_strndup("foo", 3));
229 routerset_refresh_countries(set
);
231 tt_ptr_op(set
->countries
, OP_NE
, NULL
);
232 tt_int_op(set
->n_countries
, OP_EQ
, 2);
233 tt_int_op(CALLED(geoip_is_loaded
), OP_EQ
, 1);
234 tt_int_op(CALLED(geoip_get_n_countries
), OP_EQ
, 1);
235 tt_int_op(CALLED(geoip_get_country
), OP_EQ
, 1);
236 tt_int_op((unsigned int)(*set
->countries
), OP_NE
, 0);
239 NS_UNMOCK(geoip_is_loaded
);
240 NS_UNMOCK(geoip_get_n_countries
);
241 NS_UNMOCK(geoip_get_country
);
246 NS(geoip_is_loaded
)(sa_family_t family
)
249 CALLED(geoip_is_loaded
)++;
255 NS(geoip_get_n_countries
)(void)
257 CALLED(geoip_get_n_countries
)++;
263 NS(geoip_get_country
)(const char *countrycode
)
266 CALLED(geoip_get_country
)++;
272 #define NS_SUBMODULE ASPECT(routerset_refresh_counties, one_invalid_country)
275 * Structural test for routerset_refresh_counties, with one invalid
279 NS_DECL(int, geoip_is_loaded
, (sa_family_t family
));
280 NS_DECL(int, geoip_get_n_countries
, (void));
281 NS_DECL(country_t
, geoip_get_country
, (const char *country
));
284 NS(test_main
)(void *arg
)
286 routerset_t
*set
= routerset_new();
289 NS_MOCK(geoip_is_loaded
);
290 NS_MOCK(geoip_get_n_countries
);
291 NS_MOCK(geoip_get_country
);
292 smartlist_add(set
->country_names
, tor_strndup("foo", 3));
294 routerset_refresh_countries(set
);
296 tt_ptr_op(set
->countries
, OP_NE
, NULL
);
297 tt_int_op(set
->n_countries
, OP_EQ
, 2);
298 tt_int_op(CALLED(geoip_is_loaded
), OP_EQ
, 1);
299 tt_int_op(CALLED(geoip_get_n_countries
), OP_EQ
, 1);
300 tt_int_op(CALLED(geoip_get_country
), OP_EQ
, 1);
301 tt_int_op((unsigned int)(*set
->countries
), OP_EQ
, 0);
304 NS_UNMOCK(geoip_is_loaded
);
305 NS_UNMOCK(geoip_get_n_countries
);
306 NS_UNMOCK(geoip_get_country
);
311 NS(geoip_is_loaded
)(sa_family_t family
)
314 CALLED(geoip_is_loaded
)++;
320 NS(geoip_get_n_countries
)(void)
322 CALLED(geoip_get_n_countries
)++;
328 NS(geoip_get_country
)(const char *countrycode
)
331 CALLED(geoip_get_country
)++;
337 #define NS_SUBMODULE ASPECT(routerset_parse, malformed)
340 * Functional test, with a malformed string to parse.
344 NS(test_main
)(void *arg
)
346 routerset_t
*set
= routerset_new();
351 r
= routerset_parse(set
, s
, "");
353 tt_int_op(r
, OP_EQ
, -1);
360 #define NS_SUBMODULE ASPECT(routerset_parse, valid_hexdigest)
363 * Functional test for routerset_parse, that routerset_parse returns 0
364 * on a valid hexdigest entry.
368 NS(test_main
)(void *arg
)
375 set
= routerset_new();
376 s
= "$0000000000000000000000000000000000000000";
377 r
= routerset_parse(set
, s
, "");
378 tt_int_op(r
, OP_EQ
, 0);
379 tt_int_op(digestmap_isempty(set
->digests
), OP_NE
, 1);
386 #define NS_SUBMODULE ASPECT(routerset_parse, valid_nickname)
389 * Functional test for routerset_parse, when given a valid nickname as input.
393 NS(test_main
)(void *arg
)
400 set
= routerset_new();
402 r
= routerset_parse(set
, s
, "");
403 tt_int_op(r
, OP_EQ
, 0);
404 tt_int_op(strmap_isempty(set
->names
), OP_NE
, 1);
411 #define NS_SUBMODULE ASPECT(routerset_parse, get_countryname)
414 * Functional test for routerset_parse, when given a valid countryname.
418 NS(test_main
)(void *arg
)
425 set
= routerset_new();
427 r
= routerset_parse(set
, s
, "");
428 tt_int_op(r
, OP_EQ
, 0);
429 tt_int_op(smartlist_len(set
->country_names
), OP_NE
, 0);
436 #define NS_SUBMODULE ASPECT(routerset_parse, policy_wildcard)
439 * Structural test for routerset_parse, when given a valid wildcard policy.
442 NS_DECL(addr_policy_t
*, router_parse_addr_policy_item_from_string
,
443 (const char *s
, int assume_action
, int *malformed_list
));
445 static addr_policy_t
*NS(mock_addr_policy
);
448 NS(test_main
)(void *arg
)
455 NS_MOCK(router_parse_addr_policy_item_from_string
);
456 NS(mock_addr_policy
) = tor_malloc_zero(sizeof(addr_policy_t
));
458 set
= routerset_new();
460 r
= routerset_parse(set
, s
, "");
461 tt_int_op(r
, OP_EQ
, 0);
462 tt_int_op(smartlist_len(set
->policies
), OP_NE
, 0);
463 tt_int_op(CALLED(router_parse_addr_policy_item_from_string
), OP_EQ
, 1);
470 NS(router_parse_addr_policy_item_from_string
)(const char *s
,
476 (void)malformed_list
;
477 CALLED(router_parse_addr_policy_item_from_string
)++;
479 return NS(mock_addr_policy
);
483 #define NS_SUBMODULE ASPECT(routerset_parse, policy_ipv4)
486 * Structural test for routerset_parse, when given a valid IPv4 address
490 NS_DECL(addr_policy_t
*, router_parse_addr_policy_item_from_string
,
491 (const char *s
, int assume_action
, int *bogus
));
493 static addr_policy_t
*NS(mock_addr_policy
);
496 NS(test_main
)(void *arg
)
503 NS_MOCK(router_parse_addr_policy_item_from_string
);
504 NS(mock_addr_policy
) = tor_malloc_zero(sizeof(addr_policy_t
));
506 set
= routerset_new();
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(CALLED(router_parse_addr_policy_item_from_string
), OP_EQ
, 1);
518 NS(router_parse_addr_policy_item_from_string
)(const char *s
, int assume_action
,
523 CALLED(router_parse_addr_policy_item_from_string
)++;
526 return NS(mock_addr_policy
);
530 #define NS_SUBMODULE ASPECT(routerset_parse, policy_ipv6)
533 * Structural test for routerset_parse, when given a valid IPv6 address
537 NS_DECL(addr_policy_t
*, router_parse_addr_policy_item_from_string
,
538 (const char *s
, int assume_action
, int *bad
));
540 static addr_policy_t
*NS(mock_addr_policy
);
543 NS(test_main
)(void *arg
)
550 NS_MOCK(router_parse_addr_policy_item_from_string
);
551 NS(mock_addr_policy
) = tor_malloc_zero(sizeof(addr_policy_t
));
553 set
= routerset_new();
555 r
= routerset_parse(set
, s
, "");
556 tt_int_op(r
, OP_EQ
, 0);
557 tt_int_op(smartlist_len(set
->policies
), OP_NE
, 0);
558 tt_int_op(CALLED(router_parse_addr_policy_item_from_string
), OP_EQ
, 1);
565 NS(router_parse_addr_policy_item_from_string
)(const char *s
,
566 int assume_action
, int *bad
)
570 CALLED(router_parse_addr_policy_item_from_string
)++;
573 return NS(mock_addr_policy
);
577 #define NS_SUBMODULE ASPECT(routerset_union, source_bad)
580 * Structural test for routerset_union, when given a bad source argument.
583 NS_DECL(smartlist_t
*, smartlist_new
, (void));
586 NS(test_main
)(void *arg
)
588 routerset_t
*set
, *bad_set
;
591 set
= routerset_new();
592 bad_set
= routerset_new();
593 smartlist_free(bad_set
->list
);
594 bad_set
->list
= NULL
;
596 NS_MOCK(smartlist_new
);
598 routerset_union(set
, NULL
);
599 tt_int_op(CALLED(smartlist_new
), OP_EQ
, 0);
601 routerset_union(set
, bad_set
);
602 tt_int_op(CALLED(smartlist_new
), OP_EQ
, 0);
605 NS_UNMOCK(smartlist_new
);
608 /* Just recreate list, so we can simply use routerset_free. */
609 bad_set
->list
= smartlist_new();
610 routerset_free(bad_set
);
614 NS(smartlist_new
)(void)
616 CALLED(smartlist_new
)++;
622 #define NS_SUBMODULE ASPECT(routerset_union, one)
625 * Functional test for routerset_union.
629 NS(test_main
)(void *arg
)
631 routerset_t
*src
= routerset_new();
635 tgt
= routerset_new();
636 smartlist_add_strdup(src
->list
, "{xx}");
637 routerset_union(tgt
, src
);
639 tt_int_op(smartlist_len(tgt
->list
), OP_NE
, 0);
647 #define NS_SUBMODULE routerset_is_list
650 * Functional tests for routerset_is_list.
654 NS(test_main
)(void *arg
)
657 addr_policy_t
*policy
;
661 /* len(set->country_names) == 0, len(set->policies) == 0 */
662 set
= routerset_new();
663 is_list
= routerset_is_list(set
);
666 tt_int_op(is_list
, OP_NE
, 0);
668 /* len(set->country_names) != 0, len(set->policies) == 0 */
669 set
= routerset_new();
670 smartlist_add(set
->country_names
, tor_strndup("foo", 3));
671 is_list
= routerset_is_list(set
);
674 tt_int_op(is_list
, OP_EQ
, 0);
676 /* len(set->country_names) == 0, len(set->policies) != 0 */
677 set
= routerset_new();
678 policy
= tor_malloc_zero(sizeof(addr_policy_t
));
679 smartlist_add(set
->policies
, (void *)policy
);
680 is_list
= routerset_is_list(set
);
683 tt_int_op(is_list
, OP_EQ
, 0);
685 /* len(set->country_names) != 0, len(set->policies) != 0 */
686 set
= routerset_new();
687 smartlist_add(set
->country_names
, tor_strndup("foo", 3));
688 policy
= tor_malloc_zero(sizeof(addr_policy_t
));
689 smartlist_add(set
->policies
, (void *)policy
);
690 is_list
= routerset_is_list(set
);
693 tt_int_op(is_list
, OP_EQ
, 0);
700 #define NS_SUBMODULE routerset_needs_geoip
703 * Functional tests for routerset_needs_geoip.
707 NS(test_main
)(void *arg
)
714 needs_geoip
= routerset_needs_geoip(set
);
715 tt_int_op(needs_geoip
, OP_EQ
, 0);
717 set
= routerset_new();
718 needs_geoip
= routerset_needs_geoip(set
);
720 tt_int_op(needs_geoip
, OP_EQ
, 0);
723 set
= routerset_new();
724 smartlist_add(set
->country_names
, tor_strndup("xx", 2));
725 needs_geoip
= routerset_needs_geoip(set
);
728 tt_int_op(needs_geoip
, OP_NE
, 0);
735 #define NS_SUBMODULE routerset_is_empty
738 * Functional tests for routerset_is_empty.
742 NS(test_main
)(void *arg
)
744 routerset_t
*set
= NULL
;
748 is_empty
= routerset_is_empty(set
);
749 tt_int_op(is_empty
, OP_NE
, 0);
751 set
= routerset_new();
752 is_empty
= routerset_is_empty(set
);
755 tt_int_op(is_empty
, OP_NE
, 0);
757 set
= routerset_new();
758 smartlist_add_strdup(set
->list
, "{xx}");
759 is_empty
= routerset_is_empty(set
);
762 tt_int_op(is_empty
, OP_EQ
, 0);
769 #define NS_SUBMODULE ASPECT(routerset_contains, null_set_or_null_set_list)
772 * Functional test for routerset_contains, when given a NULL set or the
773 * set has a NULL list.
777 NS(test_main
)(void *arg
)
779 routerset_t
*set
= NULL
;
783 contains
= routerset_contains(set
, NULL
, 0, NULL
, NULL
, 0);
785 tt_int_op(contains
, OP_EQ
, 0);
787 set
= tor_malloc_zero(sizeof(routerset_t
));
789 contains
= routerset_contains(set
, NULL
, 0, NULL
, NULL
, 0);
791 tt_int_op(contains
, OP_EQ
, 0);
798 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_nickname)
801 * Functional test for routerset_contains, when given a valid routerset but a
806 NS(test_main
)(void *arg
)
808 routerset_t
*set
= routerset_new();
809 char *nickname
= NULL
;
813 contains
= routerset_contains(set
, NULL
, 0, nickname
, NULL
, 0);
816 tt_int_op(contains
, OP_EQ
, 0);
823 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_nickname)
826 * Functional test for routerset_contains, when given a valid routerset
827 * and the nickname is in the routerset.
831 NS(test_main
)(void *arg
)
833 routerset_t
*set
= routerset_new();
834 const char *nickname
;
838 nickname
= "Foo"; /* This tests the lowercase comparison as well. */
839 strmap_set_lc(set
->names
, nickname
, (void *)1);
840 contains
= routerset_contains(set
, NULL
, 0, nickname
, NULL
, 0);
843 tt_int_op(contains
, OP_EQ
, 4);
849 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_nickname)
852 * Functional test for routerset_contains, when given a valid routerset
853 * and the nickname is not in the routerset.
857 NS(test_main
)(void *arg
)
859 routerset_t
*set
= routerset_new();
863 strmap_set_lc(set
->names
, "bar", (void *)1);
864 contains
= routerset_contains(set
, NULL
, 0, "foo", NULL
, 0);
867 tt_int_op(contains
, OP_EQ
, 0);
873 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_digest)
876 * Functional test for routerset_contains, when given a valid routerset
877 * and the digest is contained in the routerset.
881 NS(test_main
)(void *arg
)
883 routerset_t
*set
= routerset_new();
885 uint8_t foo
[20] = { 2, 3, 4 };
888 digestmap_set(set
->digests
, (const char*)foo
, (void *)1);
889 contains
= routerset_contains(set
, NULL
, 0, NULL
, (const char*)foo
, 0);
892 tt_int_op(contains
, OP_EQ
, 4);
898 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_digest)
901 * Functional test for routerset_contains, when given a valid routerset
902 * and the digest is not contained in the routerset.
906 NS(test_main
)(void *arg
)
908 routerset_t
*set
= routerset_new();
910 uint8_t bar
[20] = { 9, 10, 11, 55 };
911 uint8_t foo
[20] = { 1, 2, 3, 4};
914 digestmap_set(set
->digests
, (const char*)bar
, (void *)1);
915 contains
= routerset_contains(set
, NULL
, 0, NULL
, (const char*)foo
, 0);
918 tt_int_op(contains
, OP_EQ
, 0);
924 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_digest)
927 * Functional test for routerset_contains, when given a valid routerset
928 * and the digest is NULL.
932 NS(test_main
)(void *arg
)
934 routerset_t
*set
= routerset_new();
936 uint8_t bar
[20] = { 9, 10, 11, 55 };
939 digestmap_set(set
->digests
, (const char*)bar
, (void *)1);
940 contains
= routerset_contains(set
, NULL
, 0, NULL
, NULL
, 0);
943 tt_int_op(contains
, OP_EQ
, 0);
949 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_addr)
952 * Structural test for routerset_contains, when given a valid routerset
953 * and the address is rejected by policy.
956 NS_DECL(addr_policy_result_t
, compare_tor_addr_to_addr_policy
,
957 (const tor_addr_t
*addr
, uint16_t port
, const smartlist_t
*policy
));
959 static tor_addr_t MOCK_TOR_ADDR
;
960 #define MOCK_TOR_ADDR_PTR (&MOCK_TOR_ADDR)
963 NS(test_main
)(void *arg
)
965 routerset_t
*set
= routerset_new();
966 tor_addr_t
*addr
= MOCK_TOR_ADDR_PTR
;
970 NS_MOCK(compare_tor_addr_to_addr_policy
);
972 contains
= routerset_contains(set
, addr
, 0, NULL
, NULL
, 0);
975 tt_int_op(CALLED(compare_tor_addr_to_addr_policy
), OP_EQ
, 1);
976 tt_int_op(contains
, OP_EQ
, 3);
983 NS(compare_tor_addr_to_addr_policy
)(const tor_addr_t
*addr
, uint16_t port
,
984 const smartlist_t
*policy
)
988 CALLED(compare_tor_addr_to_addr_policy
)++;
989 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
990 return ADDR_POLICY_REJECTED
;
997 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_addr)
1000 * Structural test for routerset_contains, when given a valid routerset
1001 * and the address is not rejected by policy.
1004 NS_DECL(addr_policy_result_t
, compare_tor_addr_to_addr_policy
,
1005 (const tor_addr_t
*addr
, uint16_t port
, const smartlist_t
*policy
));
1008 NS(test_main
)(void *arg
)
1010 routerset_t
*set
= routerset_new();
1011 tor_addr_t
*addr
= MOCK_TOR_ADDR_PTR
;
1015 NS_MOCK(compare_tor_addr_to_addr_policy
);
1017 contains
= routerset_contains(set
, addr
, 0, NULL
, NULL
, 0);
1018 routerset_free(set
);
1020 tt_int_op(CALLED(compare_tor_addr_to_addr_policy
), OP_EQ
, 1);
1021 tt_int_op(contains
, OP_EQ
, 0);
1027 addr_policy_result_t
1028 NS(compare_tor_addr_to_addr_policy
)(const tor_addr_t
*addr
, uint16_t port
,
1029 const smartlist_t
*policy
)
1033 CALLED(compare_tor_addr_to_addr_policy
)++;
1034 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1036 return ADDR_POLICY_ACCEPTED
;
1043 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_addr)
1046 * Structural test for routerset_contains, when given a valid routerset
1047 * and the address is NULL.
1050 NS_DECL(addr_policy_result_t
, compare_tor_addr_to_addr_policy
,
1051 (const tor_addr_t
*addr
, uint16_t port
, const smartlist_t
*policy
));
1054 NS(test_main
)(void *arg
)
1056 routerset_t
*set
= routerset_new();
1060 NS_MOCK(compare_tor_addr_to_addr_policy
);
1062 contains
= routerset_contains(set
, NULL
, 0, NULL
, NULL
, 0);
1063 routerset_free(set
);
1065 tt_int_op(contains
, OP_EQ
, 0);
1071 addr_policy_result_t
1072 NS(compare_tor_addr_to_addr_policy
)(const tor_addr_t
*addr
, uint16_t port
,
1073 const smartlist_t
*policy
)
1077 CALLED(compare_tor_addr_to_addr_policy
)++;
1078 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1080 return ADDR_POLICY_ACCEPTED
;
1087 #define NS_SUBMODULE ASPECT(routerset_contains, countries_no_geoip)
1090 * Structural test for routerset_contains, when there is no matching country
1094 NS_DECL(addr_policy_result_t
, compare_tor_addr_to_addr_policy
,
1095 (const tor_addr_t
*addr
, uint16_t port
, const smartlist_t
*policy
));
1096 NS_DECL(int, geoip_get_country_by_addr
, (const tor_addr_t
*addr
));
1099 NS(test_main
)(void *arg
)
1101 routerset_t
*set
= routerset_new();
1105 NS_MOCK(compare_tor_addr_to_addr_policy
);
1106 NS_MOCK(geoip_get_country_by_addr
);
1108 set
->countries
= bitarray_init_zero(1);
1109 bitarray_set(set
->countries
, 1);
1110 contains
= routerset_contains(set
, MOCK_TOR_ADDR_PTR
, 0, NULL
, NULL
, -1);
1111 routerset_free(set
);
1113 tt_int_op(contains
, OP_EQ
, 0);
1114 tt_int_op(CALLED(compare_tor_addr_to_addr_policy
), OP_EQ
, 1);
1115 tt_int_op(CALLED(geoip_get_country_by_addr
), OP_EQ
, 1);
1121 addr_policy_result_t
1122 NS(compare_tor_addr_to_addr_policy
)(const tor_addr_t
*addr
, uint16_t port
,
1123 const smartlist_t
*policy
)
1127 CALLED(compare_tor_addr_to_addr_policy
)++;
1128 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1131 return ADDR_POLICY_ACCEPTED
;
1135 NS(geoip_get_country_by_addr
)(const tor_addr_t
*addr
)
1137 CALLED(geoip_get_country_by_addr
)++;
1138 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1145 #define NS_SUBMODULE ASPECT(routerset_contains, countries_geoip)
1148 * Structural test for routerset_contains, when there a matching country
1152 NS_DECL(addr_policy_result_t
, compare_tor_addr_to_addr_policy
,
1153 (const tor_addr_t
*addr
, uint16_t port
, const smartlist_t
*policy
));
1154 NS_DECL(int, geoip_get_country_by_addr
, (const tor_addr_t
*addr
));
1157 NS(test_main
)(void *arg
)
1159 routerset_t
*set
= routerset_new();
1163 NS_MOCK(compare_tor_addr_to_addr_policy
);
1164 NS_MOCK(geoip_get_country_by_addr
);
1166 set
->n_countries
= 2;
1167 set
->countries
= bitarray_init_zero(1);
1168 bitarray_set(set
->countries
, 1);
1169 contains
= routerset_contains(set
, MOCK_TOR_ADDR_PTR
, 0, NULL
, NULL
, -1);
1170 routerset_free(set
);
1172 tt_int_op(contains
, OP_EQ
, 2);
1173 tt_int_op(CALLED(compare_tor_addr_to_addr_policy
), OP_EQ
, 1);
1174 tt_int_op(CALLED(geoip_get_country_by_addr
), OP_EQ
, 1);
1180 addr_policy_result_t
1181 NS(compare_tor_addr_to_addr_policy
)(const tor_addr_t
*addr
, uint16_t port
,
1182 const smartlist_t
*policy
)
1186 CALLED(compare_tor_addr_to_addr_policy
)++;
1187 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1190 return ADDR_POLICY_ACCEPTED
;
1194 NS(geoip_get_country_by_addr
)(const tor_addr_t
*addr
)
1196 CALLED(geoip_get_country_by_addr
)++;
1197 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1204 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, only_flag_and_no_ccs)
1207 * Functional test for routerset_add_unknown_ccs, where only_if_some_cc_set
1208 * is set and there are no country names.
1212 NS(test_main
)(void *arg
)
1214 routerset_t
*set
= routerset_new();
1215 routerset_t
**setp
= &set
;
1219 r
= routerset_add_unknown_ccs(setp
, 1);
1221 tt_int_op(r
, OP_EQ
, 0);
1224 routerset_free(set
);
1228 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, creates_set)
1231 * Functional test for routerset_add_unknown_ccs, where the set argument
1232 * is created if passed in as NULL.
1235 /* The mock is only used to stop the test from asserting erroneously. */
1236 NS_DECL(country_t
, geoip_get_country
, (const char *country
));
1239 NS(test_main
)(void *arg
)
1241 routerset_t
*set
= NULL
;
1242 routerset_t
**setp
= &set
;
1246 NS_MOCK(geoip_get_country
);
1248 r
= routerset_add_unknown_ccs(setp
, 0);
1250 tt_ptr_op(*setp
, OP_NE
, NULL
);
1251 tt_int_op(r
, OP_EQ
, 0);
1255 routerset_free(set
);
1259 NS(geoip_get_country
)(const char *country
)
1262 CALLED(geoip_get_country
)++;
1268 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, add_unknown)
1271 * Structural test for routerset_add_unknown_ccs, that the "{??}"
1272 * country code is added to the list.
1275 NS_DECL(country_t
, geoip_get_country
, (const char *country
));
1276 NS_DECL(int, geoip_is_loaded
, (sa_family_t family
));
1279 NS(test_main
)(void *arg
)
1281 routerset_t
*set
= routerset_new();
1282 routerset_t
**setp
= &set
;
1286 NS_MOCK(geoip_get_country
);
1287 NS_MOCK(geoip_is_loaded
);
1289 r
= routerset_add_unknown_ccs(setp
, 0);
1291 tt_int_op(r
, OP_EQ
, 1);
1292 tt_int_op(smartlist_contains_string(set
->country_names
, "??"), OP_EQ
, 1);
1293 tt_int_op(smartlist_contains_string(set
->list
, "{??}"), OP_EQ
, 1);
1297 routerset_free(set
);
1301 NS(geoip_get_country
)(const char *country
)
1303 int arg_is_qq
, arg_is_a1
;
1305 CALLED(geoip_get_country
)++;
1307 arg_is_qq
= !strcmp(country
, "??");
1308 arg_is_a1
= !strcmp(country
, "A1");
1310 tt_int_op(arg_is_qq
|| arg_is_a1
, OP_EQ
, 1);
1320 NS(geoip_is_loaded
)(sa_family_t family
)
1322 CALLED(geoip_is_loaded
)++;
1324 tt_int_op(family
, OP_EQ
, AF_INET
);
1331 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, add_a1)
1334 * Structural test for routerset_add_unknown_ccs, that the "{a1}"
1335 * country code is added to the list.
1338 NS_DECL(country_t
, geoip_get_country
, (const char *country
));
1339 NS_DECL(int, geoip_is_loaded
, (sa_family_t family
));
1342 NS(test_main
)(void *arg
)
1344 routerset_t
*set
= routerset_new();
1345 routerset_t
**setp
= &set
;
1349 NS_MOCK(geoip_get_country
);
1350 NS_MOCK(geoip_is_loaded
);
1352 r
= routerset_add_unknown_ccs(setp
, 0);
1354 tt_int_op(r
, OP_EQ
, 1);
1355 tt_int_op(smartlist_contains_string(set
->country_names
, "a1"), OP_EQ
, 1);
1356 tt_int_op(smartlist_contains_string(set
->list
, "{a1}"), OP_EQ
, 1);
1360 routerset_free(set
);
1364 NS(geoip_get_country
)(const char *country
)
1366 int arg_is_qq
, arg_is_a1
;
1368 CALLED(geoip_get_country
)++;
1370 arg_is_qq
= !strcmp(country
, "??");
1371 arg_is_a1
= !strcmp(country
, "A1");
1373 tt_int_op(arg_is_qq
|| arg_is_a1
, OP_EQ
, 1);
1383 NS(geoip_is_loaded
)(sa_family_t family
)
1385 CALLED(geoip_is_loaded
)++;
1387 tt_int_op(family
, OP_EQ
, AF_INET
);
1394 #define NS_SUBMODULE routerset_contains_extendinfo
1397 * Functional test for routerset_contains_extendinfo.
1401 NS(test_main
)(void *arg
)
1403 routerset_t
*set
= routerset_new();
1406 const char *nickname
= "foo";
1409 memset(&ei
, 0, sizeof(ei
));
1410 strmap_set_lc(set
->names
, nickname
, (void *)1);
1411 strncpy(ei
.nickname
, nickname
, sizeof(ei
.nickname
) - 1);
1412 ei
.nickname
[sizeof(ei
.nickname
) - 1] = '\0';
1414 r
= routerset_contains_extendinfo(set
, &ei
);
1416 tt_int_op(r
, OP_EQ
, 4);
1418 routerset_free(set
);
1422 #define NS_SUBMODULE routerset_contains_router
1425 * Functional test for routerset_contains_router.
1429 NS(test_main
)(void *arg
)
1431 routerset_t
*set
= routerset_new();
1433 country_t country
= 1;
1435 const char *nickname
= "foo";
1438 memset(&ri
, 0, sizeof(ri
));
1439 strmap_set_lc(set
->names
, nickname
, (void *)1);
1440 ri
.nickname
= (char *)nickname
;
1442 r
= routerset_contains_router(set
, &ri
, country
);
1444 tt_int_op(r
, OP_EQ
, 4);
1446 routerset_free(set
);
1450 #define NS_SUBMODULE routerset_contains_routerstatus
1453 * Functional test for routerset_contains_routerstatus.
1456 // XXX: This is a bit brief. It only populates and tests the nickname fields
1457 // ie., enough to make the containment check succeed. Perhaps it should do
1458 // a bit more or test a bit more.
1461 NS(test_main
)(void *arg
)
1463 routerset_t
*set
= routerset_new();
1465 country_t country
= 1;
1467 const char *nickname
= "foo";
1470 memset(&rs
, 0, sizeof(rs
));
1471 strmap_set_lc(set
->names
, nickname
, (void *)1);
1472 strncpy(rs
.nickname
, nickname
, sizeof(rs
.nickname
) - 1);
1473 rs
.nickname
[sizeof(rs
.nickname
) - 1] = '\0';
1475 r
= routerset_contains_routerstatus(set
, &rs
, country
);
1477 tt_int_op(r
, OP_EQ
, 4);
1479 routerset_free(set
);
1483 #define NS_SUBMODULE ASPECT(routerset_contains_node, none)
1486 * Functional test for routerset_contains_node, when the node has no
1487 * routerset or routerinfo.
1490 static node_t
NS(mock_node
);
1493 NS(test_main
)(void *arg
)
1495 routerset_t
*set
= routerset_new();
1499 NS(mock_node
).ri
= NULL
;
1500 NS(mock_node
).rs
= NULL
;
1502 r
= routerset_contains_node(set
, &NS(mock_node
));
1503 tt_int_op(r
, OP_EQ
, 0);
1506 routerset_free(set
);
1510 #define NS_SUBMODULE ASPECT(routerset_contains_node, routerstatus)
1513 * Functional test for routerset_contains_node, when the node has a
1514 * routerset and no routerinfo.
1517 static node_t
NS(mock_node
);
1520 NS(test_main
)(void *arg
)
1522 routerset_t
*set
= routerset_new();
1524 const char *nickname
= "foo";
1528 strmap_set_lc(set
->names
, nickname
, (void *)1);
1530 strncpy(rs
.nickname
, nickname
, sizeof(rs
.nickname
) - 1);
1531 rs
.nickname
[sizeof(rs
.nickname
) - 1] = '\0';
1532 NS(mock_node
).ri
= NULL
;
1533 NS(mock_node
).rs
= &rs
;
1535 r
= routerset_contains_node(set
, &NS(mock_node
));
1537 tt_int_op(r
, OP_EQ
, 4);
1539 routerset_free(set
);
1543 #define NS_SUBMODULE ASPECT(routerset_contains_node, routerinfo)
1546 * Functional test for routerset_contains_node, when the node has no
1547 * routerset and a routerinfo.
1551 NS(test_main
)(void *arg
)
1553 routerset_t
*set
= routerset_new();
1555 const char *nickname
= "foo";
1560 strmap_set_lc(set
->names
, nickname
, (void *)1);
1562 ri
.nickname
= (char *)nickname
;
1564 mock_node
.rs
= NULL
;
1566 r
= routerset_contains_node(set
, &mock_node
);
1568 tt_int_op(r
, OP_EQ
, 4);
1570 routerset_free(set
);
1574 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, no_routerset)
1577 * Functional test for routerset_get_all_nodes, when routerset is NULL or
1578 * the routerset list is NULL.
1582 NS(test_main
)(void *arg
)
1584 smartlist_t
*out
= smartlist_new();
1585 routerset_t
*set
= NULL
;
1588 tt_int_op(smartlist_len(out
), OP_EQ
, 0);
1589 routerset_get_all_nodes(out
, NULL
, NULL
, 0);
1591 tt_int_op(smartlist_len(out
), OP_EQ
, 0);
1593 set
= routerset_new();
1594 smartlist_free(set
->list
);
1595 routerset_get_all_nodes(out
, NULL
, NULL
, 0);
1596 tt_int_op(smartlist_len(out
), OP_EQ
, 0);
1598 /* Just recreate list, so we can simply use routerset_free. */
1599 set
->list
= smartlist_new();
1602 routerset_free(set
);
1603 smartlist_free(out
);
1607 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list_with_no_nodes)
1610 * Structural test for routerset_get_all_nodes, when the routerset list
1614 NS_DECL(const node_t
*, node_get_by_nickname
,
1615 (const char *nickname
, unsigned flags
));
1616 static const char *NS(mock_nickname
);
1619 NS(test_main
)(void *arg
)
1621 smartlist_t
*out
= smartlist_new();
1622 routerset_t
*set
= routerset_new();
1626 NS_MOCK(node_get_by_nickname
);
1628 NS(mock_nickname
) = "foo";
1629 smartlist_add_strdup(set
->list
, NS(mock_nickname
));
1631 routerset_get_all_nodes(out
, set
, NULL
, 0);
1632 out_len
= smartlist_len(out
);
1634 smartlist_free(out
);
1635 routerset_free(set
);
1637 tt_int_op(out_len
, OP_EQ
, 0);
1638 tt_int_op(CALLED(node_get_by_nickname
), OP_EQ
, 1);
1645 NS(node_get_by_nickname
)(const char *nickname
, unsigned flags
)
1647 CALLED(node_get_by_nickname
)++;
1648 tt_str_op(nickname
, OP_EQ
, NS(mock_nickname
));
1649 tt_uint_op(flags
, OP_EQ
, 0);
1656 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list_flag_not_running)
1659 * Structural test for routerset_get_all_nodes, with the running_only flag
1660 * is set but the nodes are not running.
1663 NS_DECL(const node_t
*, node_get_by_nickname
,
1664 (const char *nickname
, unsigned flags
));
1665 static const char *NS(mock_nickname
);
1666 static node_t
NS(mock_node
);
1669 NS(test_main
)(void *arg
)
1671 smartlist_t
*out
= smartlist_new();
1672 routerset_t
*set
= routerset_new();
1676 NS_MOCK(node_get_by_nickname
);
1678 NS(mock_node
).is_running
= 0;
1679 NS(mock_nickname
) = "foo";
1680 smartlist_add_strdup(set
->list
, NS(mock_nickname
));
1682 routerset_get_all_nodes(out
, set
, NULL
, 1);
1683 out_len
= smartlist_len(out
);
1685 smartlist_free(out
);
1686 routerset_free(set
);
1688 tt_int_op(out_len
, OP_EQ
, 0);
1689 tt_int_op(CALLED(node_get_by_nickname
), OP_EQ
, 1);
1696 NS(node_get_by_nickname
)(const char *nickname
, unsigned flags
)
1698 CALLED(node_get_by_nickname
)++;
1699 tt_str_op(nickname
, OP_EQ
, NS(mock_nickname
));
1700 tt_int_op(flags
, OP_EQ
, 0);
1703 return &NS(mock_node
);
1707 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list)
1710 * Structural test for routerset_get_all_nodes.
1713 NS_DECL(const node_t
*, node_get_by_nickname
,
1714 (const char *nickname
, unsigned flags
));
1715 static char *NS(mock_nickname
);
1716 static node_t
NS(mock_node
);
1719 NS(test_main
)(void *arg
)
1721 smartlist_t
*out
= smartlist_new();
1722 routerset_t
*set
= routerset_new();
1727 NS_MOCK(node_get_by_nickname
);
1729 NS(mock_nickname
) = tor_strdup("foo");
1730 smartlist_add(set
->list
, NS(mock_nickname
));
1732 routerset_get_all_nodes(out
, set
, NULL
, 0);
1733 out_len
= smartlist_len(out
);
1734 ent
= (node_t
*)smartlist_get(out
, 0);
1736 smartlist_free(out
);
1737 routerset_free(set
);
1739 tt_int_op(out_len
, OP_EQ
, 1);
1740 tt_ptr_op(ent
, OP_EQ
, &NS(mock_node
));
1741 tt_int_op(CALLED(node_get_by_nickname
), OP_EQ
, 1);
1748 NS(node_get_by_nickname
)(const char *nickname
, unsigned flags
)
1750 CALLED(node_get_by_nickname
)++;
1751 tt_str_op(nickname
, OP_EQ
, NS(mock_nickname
));
1752 tt_int_op(flags
, OP_EQ
, 0);
1755 return &NS(mock_node
);
1759 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, nodelist_with_no_nodes)
1762 * Structural test for routerset_get_all_nodes, when the nodelist has no nodes.
1765 NS_DECL(smartlist_t
*, nodelist_get_list
, (void));
1767 static smartlist_t
*NS(mock_smartlist
);
1770 NS(test_main
)(void *arg
)
1772 routerset_t
*set
= routerset_new();
1773 smartlist_t
*out
= smartlist_new();
1777 NS_MOCK(nodelist_get_list
);
1779 smartlist_add_strdup(set
->country_names
, "{xx}");
1780 NS(mock_smartlist
) = smartlist_new();
1782 routerset_get_all_nodes(out
, set
, NULL
, 1);
1783 r
= smartlist_len(out
);
1784 routerset_free(set
);
1785 smartlist_free(out
);
1786 smartlist_free(NS(mock_smartlist
));
1788 tt_int_op(r
, OP_EQ
, 0);
1789 tt_int_op(CALLED(nodelist_get_list
), OP_EQ
, 1);
1796 NS(nodelist_get_list
)(void)
1798 CALLED(nodelist_get_list
)++;
1800 return NS(mock_smartlist
);
1804 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, nodelist_flag_not_running)
1807 * Structural test for routerset_get_all_nodes, with a non-list routerset
1808 * the running_only flag is set, but the nodes are not running.
1811 NS_DECL(smartlist_t
*, nodelist_get_list
, (void));
1813 static smartlist_t
*NS(mock_smartlist
);
1814 static node_t
NS(mock_node
);
1817 NS(test_main
)(void *arg
)
1819 routerset_t
*set
= routerset_new();
1820 smartlist_t
*out
= smartlist_new();
1824 NS_MOCK(nodelist_get_list
);
1826 smartlist_add_strdup(set
->country_names
, "{xx}");
1827 NS(mock_smartlist
) = smartlist_new();
1828 NS(mock_node
).is_running
= 0;
1829 smartlist_add(NS(mock_smartlist
), (void *)&NS(mock_node
));
1831 routerset_get_all_nodes(out
, set
, NULL
, 1);
1832 r
= smartlist_len(out
);
1833 routerset_free(set
);
1834 smartlist_free(out
);
1835 smartlist_free(NS(mock_smartlist
));
1837 tt_int_op(r
, OP_EQ
, 0);
1838 tt_int_op(CALLED(nodelist_get_list
), OP_EQ
, 1);
1845 NS(nodelist_get_list
)(void)
1847 CALLED(nodelist_get_list
)++;
1849 return NS(mock_smartlist
);
1853 #define NS_SUBMODULE routerset_subtract_nodes
1856 * Functional test for routerset_subtract_nodes.
1860 NS(test_main
)(void *arg
)
1862 routerset_t
*set
= routerset_new();
1863 smartlist_t
*list
= smartlist_new();
1864 const char *nickname
= "foo";
1869 strmap_set_lc(set
->names
, nickname
, (void *)1);
1871 ri
.nickname
= (char *)nickname
;
1872 mock_node
.rs
= NULL
;
1874 smartlist_add(list
, (void *)&mock_node
);
1876 tt_int_op(smartlist_len(list
), OP_NE
, 0);
1877 routerset_subtract_nodes(list
, set
);
1879 tt_int_op(smartlist_len(list
), OP_EQ
, 0);
1881 routerset_free(set
);
1882 smartlist_free(list
);
1886 #define NS_SUBMODULE ASPECT(routerset_subtract_nodes, null_routerset)
1889 * Functional test for routerset_subtract_nodes, with a NULL routerset.
1893 NS(test_main
)(void *arg
)
1895 routerset_t
*set
= NULL
;
1896 smartlist_t
*list
= smartlist_new();
1897 const char *nickname
= "foo";
1902 ri
.nickname
= (char *)nickname
;
1904 smartlist_add(list
, (void *)&mock_node
);
1906 tt_int_op(smartlist_len(list
), OP_NE
, 0);
1907 routerset_subtract_nodes(list
, set
);
1909 tt_int_op(smartlist_len(list
), OP_NE
, 0);
1911 routerset_free(set
);
1912 smartlist_free(list
);
1916 #define NS_SUBMODULE routerset_to_string
1919 * Functional test for routerset_to_string.
1923 NS(test_main
)(void *arg
)
1925 routerset_t
*set
= NULL
;
1930 s
= routerset_to_string(set
);
1931 tt_str_op(s
, OP_EQ
, "");
1934 set
= routerset_new();
1935 s
= routerset_to_string(set
);
1936 tt_str_op(s
, OP_EQ
, "");
1938 routerset_free(set
); set
= NULL
;
1940 set
= routerset_new();
1941 smartlist_add(set
->list
, tor_strndup("a", 1));
1942 s
= routerset_to_string(set
);
1943 tt_str_op(s
, OP_EQ
, "a");
1945 routerset_free(set
); set
= NULL
;
1947 set
= routerset_new();
1948 smartlist_add(set
->list
, tor_strndup("a", 1));
1949 smartlist_add(set
->list
, tor_strndup("b", 1));
1950 s
= routerset_to_string(set
);
1951 tt_str_op(s
, OP_EQ
, "a,b");
1953 routerset_free(set
); set
= NULL
;
1957 routerset_free(set
);
1961 #define NS_SUBMODULE ASPECT(routerset_equal, empty_empty)
1964 * Functional test for routerset_equal, with both routersets empty.
1968 NS(test_main
)(void *arg
)
1970 routerset_t
*a
= routerset_new(), *b
= routerset_new();
1974 r
= routerset_equal(a
, b
);
1978 tt_int_op(r
, OP_EQ
, 1);
1985 #define NS_SUBMODULE ASPECT(routerset_equal, empty_not_empty)
1988 * Functional test for routerset_equal, with one routersets empty.
1992 NS(test_main
)(void *arg
)
1994 routerset_t
*a
= routerset_new(), *b
= routerset_new();
1998 smartlist_add_strdup(b
->list
, "{xx}");
1999 r
= routerset_equal(a
, b
);
2003 tt_int_op(r
, OP_EQ
, 0);
2009 #define NS_SUBMODULE ASPECT(routerset_equal, differing_lengths)
2012 * Functional test for routerset_equal, with the routersets having
2013 * differing lengths.
2017 NS(test_main
)(void *arg
)
2019 routerset_t
*a
= routerset_new(), *b
= routerset_new();
2023 smartlist_add_strdup(a
->list
, "{aa}");
2024 smartlist_add_strdup(b
->list
, "{b1}");
2025 smartlist_add_strdup(b
->list
, "{b2}");
2026 r
= routerset_equal(a
, b
);
2030 tt_int_op(r
, OP_EQ
, 0);
2036 #define NS_SUBMODULE ASPECT(routerset_equal, unequal)
2039 * Functional test for routerset_equal, with the routersets being
2044 NS(test_main
)(void *arg
)
2046 routerset_t
*a
= routerset_new(), *b
= routerset_new();
2050 smartlist_add_strdup(a
->list
, "foo");
2051 smartlist_add_strdup(b
->list
, "bar");
2052 r
= routerset_equal(a
, b
);
2056 tt_int_op(r
, OP_EQ
, 0);
2062 #define NS_SUBMODULE ASPECT(routerset_equal, equal)
2065 * Functional test for routerset_equal, with the routersets being
2070 NS(test_main
)(void *arg
)
2072 routerset_t
*a
= routerset_new(), *b
= routerset_new();
2076 smartlist_add_strdup(a
->list
, "foo");
2077 smartlist_add_strdup(b
->list
, "foo");
2078 r
= routerset_equal(a
, b
);
2082 tt_int_op(r
, OP_EQ
, 1);
2088 #define NS_SUBMODULE ASPECT(routerset_free, null_routerset)
2091 * Structural test for routerset_free, where the routerset is NULL.
2094 NS_DECL(void, smartlist_free_
, (smartlist_t
*sl
));
2097 NS(test_main
)(void *arg
)
2101 NS_MOCK(smartlist_free_
);
2103 routerset_free_(NULL
);
2105 tt_int_op(CALLED(smartlist_free_
), OP_EQ
, 0);
2112 NS(smartlist_free_
)(smartlist_t
*s
)
2115 CALLED(smartlist_free_
)++;
2119 #define NS_SUBMODULE routerset_free
2122 * Structural test for routerset_free.
2125 NS_DECL(void, smartlist_free_
, (smartlist_t
*sl
));
2126 NS_DECL(void, strmap_free_
,(strmap_t
*map
, void (*free_val
)(void*)));
2127 NS_DECL(void, digestmap_free_
, (digestmap_t
*map
, void (*free_val
)(void*)));
2130 NS(test_main
)(void *arg
)
2132 routerset_t
*routerset
= routerset_new();
2135 NS_MOCK(smartlist_free_
);
2136 NS_MOCK(strmap_free_
);
2137 NS_MOCK(digestmap_free_
);
2139 routerset_free(routerset
);
2141 tt_int_op(CALLED(smartlist_free_
), OP_NE
, 0);
2142 tt_int_op(CALLED(strmap_free_
), OP_NE
, 0);
2143 tt_int_op(CALLED(digestmap_free_
), OP_NE
, 0);
2150 NS(smartlist_free_
)(smartlist_t
*s
)
2152 CALLED(smartlist_free_
)++;
2153 smartlist_free___real(s
);
2157 NS(strmap_free_
)(strmap_t
*map
, void (*free_val
)(void*))
2159 CALLED(strmap_free_
)++;
2160 strmap_free___real(map
, free_val
);
2164 NS(digestmap_free_
)(digestmap_t
*map
, void (*free_val
)(void*))
2166 CALLED(digestmap_free_
)++;
2167 digestmap_free___real(map
, free_val
);
2172 struct testcase_t routerset_tests
[] = {
2173 TEST_CASE(routerset_new
),
2174 TEST_CASE(routerset_get_countryname
),
2175 TEST_CASE(routerset_is_list
),
2176 TEST_CASE(routerset_needs_geoip
),
2177 TEST_CASE(routerset_is_empty
),
2178 TEST_CASE_ASPECT(routerset_contains
, null_set_or_null_set_list
),
2179 TEST_CASE_ASPECT(routerset_contains
, set_and_nickname
),
2180 TEST_CASE_ASPECT(routerset_contains
, set_and_null_nickname
),
2181 TEST_CASE_ASPECT(routerset_contains
, set_and_no_nickname
),
2182 TEST_CASE_ASPECT(routerset_contains
, set_and_digest
),
2183 TEST_CASE_ASPECT(routerset_contains
, set_and_no_digest
),
2184 TEST_CASE_ASPECT(routerset_contains
, set_and_null_digest
),
2185 TEST_CASE_ASPECT(routerset_contains
, set_and_addr
),
2186 TEST_CASE_ASPECT(routerset_contains
, set_and_no_addr
),
2187 TEST_CASE_ASPECT(routerset_contains
, set_and_null_addr
),
2188 TEST_CASE_ASPECT(routerset_contains
, countries_no_geoip
),
2189 TEST_CASE_ASPECT(routerset_contains
, countries_geoip
),
2190 TEST_CASE_ASPECT(routerset_add_unknown_ccs
, only_flag_and_no_ccs
),
2191 TEST_CASE_ASPECT(routerset_add_unknown_ccs
, creates_set
),
2192 TEST_CASE_ASPECT(routerset_add_unknown_ccs
, add_unknown
),
2193 TEST_CASE_ASPECT(routerset_add_unknown_ccs
, add_a1
),
2194 TEST_CASE(routerset_contains_extendinfo
),
2195 TEST_CASE(routerset_contains_router
),
2196 TEST_CASE(routerset_contains_routerstatus
),
2197 TEST_CASE_ASPECT(routerset_contains_node
, none
),
2198 TEST_CASE_ASPECT(routerset_contains_node
, routerinfo
),
2199 TEST_CASE_ASPECT(routerset_contains_node
, routerstatus
),
2200 TEST_CASE_ASPECT(routerset_get_all_nodes
, no_routerset
),
2201 TEST_CASE_ASPECT(routerset_get_all_nodes
, list_with_no_nodes
),
2202 TEST_CASE_ASPECT(routerset_get_all_nodes
, list_flag_not_running
),
2203 TEST_CASE_ASPECT(routerset_get_all_nodes
, list
),
2204 TEST_CASE_ASPECT(routerset_get_all_nodes
, nodelist_with_no_nodes
),
2205 TEST_CASE_ASPECT(routerset_get_all_nodes
, nodelist_flag_not_running
),
2206 TEST_CASE_ASPECT(routerset_refresh_counties
, geoip_not_loaded
),
2207 TEST_CASE_ASPECT(routerset_refresh_counties
, no_countries
),
2208 TEST_CASE_ASPECT(routerset_refresh_counties
, one_valid_country
),
2209 TEST_CASE_ASPECT(routerset_refresh_counties
, one_invalid_country
),
2210 TEST_CASE_ASPECT(routerset_union
, source_bad
),
2211 TEST_CASE_ASPECT(routerset_union
, one
),
2212 TEST_CASE_ASPECT(routerset_parse
, malformed
),
2213 TEST_CASE_ASPECT(routerset_parse
, valid_hexdigest
),
2214 TEST_CASE_ASPECT(routerset_parse
, valid_nickname
),
2215 TEST_CASE_ASPECT(routerset_parse
, get_countryname
),
2216 TEST_CASE_ASPECT(routerset_parse
, policy_wildcard
),
2217 TEST_CASE_ASPECT(routerset_parse
, policy_ipv4
),
2218 TEST_CASE_ASPECT(routerset_parse
, policy_ipv6
),
2219 TEST_CASE(routerset_subtract_nodes
),
2220 TEST_CASE_ASPECT(routerset_subtract_nodes
, null_routerset
),
2221 TEST_CASE(routerset_to_string
),
2222 TEST_CASE_ASPECT(routerset_equal
, empty_empty
),
2223 TEST_CASE_ASPECT(routerset_equal
, empty_not_empty
),
2224 TEST_CASE_ASPECT(routerset_equal
, differing_lengths
),
2225 TEST_CASE_ASPECT(routerset_equal
, unequal
),
2226 TEST_CASE_ASPECT(routerset_equal
, equal
),
2227 TEST_CASE_ASPECT(routerset_free
, null_routerset
),
2228 TEST_CASE(routerset_free
),