1 #define ROUTERSET_PRIVATE
6 #include "routerparse.h"
11 #define NS_MODULE routerset
13 #define NS_SUBMODULE routerset_new
16 * Functional (blackbox) test to determine that each member of the routerset
21 NS(test_main
)(void *arg
)
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
);
40 #define NS_SUBMODULE routerset_get_countryname
43 * Functional test to strip the braces from a "{xx}" country code string.
47 NS(test_main
)(void *arg
)
55 name
= routerset_get_countryname(input
);
56 tt_ptr_op(name
, OP_EQ
, NULL
);
61 name
= routerset_get_countryname(input
);
62 tt_ptr_op(name
, OP_EQ
, NULL
);
67 name
= routerset_get_countryname(input
);
68 tt_ptr_op(name
, OP_EQ
, NULL
);
73 name
= routerset_get_countryname(input
);
74 tt_str_op(name
, OP_EQ
, "xx");
78 name
= routerset_get_countryname(input
);
79 tt_str_op(name
, OP_EQ
, "xx");
85 #define NS_SUBMODULE ASPECT(routerset_refresh_counties, geoip_not_loaded)
88 * Structural (whitebox) test for routerset_refresh_counties, when the GeoIP DB
92 NS_DECL(int, geoip_is_loaded
, (sa_family_t family
));
93 NS_DECL(int, geoip_get_n_countries
, (void));
96 NS(test_main
)(void *arg
)
98 routerset_t
*set
= routerset_new();
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);
112 NS_UNMOCK(geoip_is_loaded
);
113 NS_UNMOCK(geoip_get_n_countries
);
118 NS(geoip_is_loaded
)(sa_family_t family
)
121 CALLED(geoip_is_loaded
)++;
127 NS(geoip_get_n_countries
)(void)
129 CALLED(geoip_get_n_countries
)++;
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
));
146 NS(test_main
)(void *arg
)
148 routerset_t
*set
= routerset_new();
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);
165 NS_UNMOCK(geoip_is_loaded
);
166 NS_UNMOCK(geoip_get_n_countries
);
167 NS_UNMOCK(geoip_get_country
);
172 NS(geoip_is_loaded
)(sa_family_t family
)
175 CALLED(geoip_is_loaded
)++;
181 NS(geoip_get_n_countries
)(void)
183 CALLED(geoip_get_n_countries
)++;
189 NS(geoip_get_country
)(const char *countrycode
)
192 CALLED(geoip_get_country
)++;
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
));
209 NS(test_main
)(void *arg
)
211 routerset_t
*set
= routerset_new();
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);
229 NS_UNMOCK(geoip_is_loaded
);
230 NS_UNMOCK(geoip_get_n_countries
);
231 NS_UNMOCK(geoip_get_country
);
236 NS(geoip_is_loaded
)(sa_family_t family
)
239 CALLED(geoip_is_loaded
)++;
245 NS(geoip_get_n_countries
)(void)
247 CALLED(geoip_get_n_countries
)++;
253 NS(geoip_get_country
)(const char *countrycode
)
256 CALLED(geoip_get_country
)++;
262 #define NS_SUBMODULE ASPECT(routerset_refresh_counties, one_invalid_country)
265 * Structural test for routerset_refresh_counties, with one invalid
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
));
274 NS(test_main
)(void *arg
)
276 routerset_t
*set
= routerset_new();
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);
294 NS_UNMOCK(geoip_is_loaded
);
295 NS_UNMOCK(geoip_get_n_countries
);
296 NS_UNMOCK(geoip_get_country
);
301 NS(geoip_is_loaded
)(sa_family_t family
)
304 CALLED(geoip_is_loaded
)++;
310 NS(geoip_get_n_countries
)(void)
312 CALLED(geoip_get_n_countries
)++;
318 NS(geoip_get_country
)(const char *countrycode
)
321 CALLED(geoip_get_country
)++;
327 #define NS_SUBMODULE ASPECT(routerset_parse, malformed)
330 * Functional test, with a malformed string to parse.
334 NS(test_main
)(void *arg
)
336 routerset_t
*set
= routerset_new();
341 r
= routerset_parse(set
, s
, "");
343 tt_int_op(r
, OP_EQ
, -1);
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.
358 NS(test_main
)(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);
376 #define NS_SUBMODULE ASPECT(routerset_parse, valid_nickname)
379 * Functional test for routerset_parse, when given a valid nickname as input.
383 NS(test_main
)(void *arg
)
390 set
= routerset_new();
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);
401 #define NS_SUBMODULE ASPECT(routerset_parse, get_countryname)
404 * Functional test for routerset_parse, when given a valid countryname.
408 NS(test_main
)(void *arg
)
415 set
= routerset_new();
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);
426 #define NS_SUBMODULE ASPECT(routerset_parse, policy_wildcard)
429 * Structural test for routerset_parse, when given a valid wildcard policy.
432 NS_DECL(addr_policy_t
*, router_parse_addr_policy_item_from_string
,
433 (const char *s
, int assume_action
, int *malformed_list
));
435 static addr_policy_t
*NS(mock_addr_policy
);
438 NS(test_main
)(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();
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);
460 NS(router_parse_addr_policy_item_from_string
)(const char *s
,
466 (void)malformed_list
;
467 CALLED(router_parse_addr_policy_item_from_string
)++;
469 return NS(mock_addr_policy
);
473 #define NS_SUBMODULE ASPECT(routerset_parse, policy_ipv4)
476 * Structural test for routerset_parse, when given a valid IPv4 address
480 NS_DECL(addr_policy_t
*, router_parse_addr_policy_item_from_string
,
481 (const char *s
, int assume_action
, int *bogus
));
483 static addr_policy_t
*NS(mock_addr_policy
);
486 NS(test_main
)(void *arg
)
493 NS_MOCK(router_parse_addr_policy_item_from_string
);
494 NS(mock_addr_policy
) = tor_malloc_zero(sizeof(addr_policy_t
));
496 set
= routerset_new();
498 r
= routerset_parse(set
, s
, "");
499 tt_int_op(r
, OP_EQ
, 0);
500 tt_int_op(smartlist_len(set
->policies
), OP_NE
, 0);
501 tt_int_op(CALLED(router_parse_addr_policy_item_from_string
), OP_EQ
, 1);
508 NS(router_parse_addr_policy_item_from_string
)(const char *s
, int assume_action
,
513 CALLED(router_parse_addr_policy_item_from_string
)++;
516 return NS(mock_addr_policy
);
520 #define NS_SUBMODULE ASPECT(routerset_parse, policy_ipv6)
523 * Structural test for routerset_parse, when given a valid IPv6 address
527 NS_DECL(addr_policy_t
*, router_parse_addr_policy_item_from_string
,
528 (const char *s
, int assume_action
, int *bad
));
530 static addr_policy_t
*NS(mock_addr_policy
);
533 NS(test_main
)(void *arg
)
540 NS_MOCK(router_parse_addr_policy_item_from_string
);
541 NS(mock_addr_policy
) = tor_malloc_zero(sizeof(addr_policy_t
));
543 set
= routerset_new();
545 r
= routerset_parse(set
, s
, "");
546 tt_int_op(r
, OP_EQ
, 0);
547 tt_int_op(smartlist_len(set
->policies
), OP_NE
, 0);
548 tt_int_op(CALLED(router_parse_addr_policy_item_from_string
), OP_EQ
, 1);
555 NS(router_parse_addr_policy_item_from_string
)(const char *s
,
556 int assume_action
, int *bad
)
560 CALLED(router_parse_addr_policy_item_from_string
)++;
563 return NS(mock_addr_policy
);
567 #define NS_SUBMODULE ASPECT(routerset_union, source_bad)
570 * Structural test for routerset_union, when given a bad source argument.
573 NS_DECL(smartlist_t
*, smartlist_new
, (void));
576 NS(test_main
)(void *arg
)
578 routerset_t
*set
, *bad_set
;
581 set
= routerset_new();
582 bad_set
= routerset_new();
583 smartlist_free(bad_set
->list
);
584 bad_set
->list
= NULL
;
586 NS_MOCK(smartlist_new
);
588 routerset_union(set
, NULL
);
589 tt_int_op(CALLED(smartlist_new
), OP_EQ
, 0);
591 routerset_union(set
, bad_set
);
592 tt_int_op(CALLED(smartlist_new
), OP_EQ
, 0);
595 NS_UNMOCK(smartlist_new
);
598 /* Just recreate list, so we can simply use routerset_free. */
599 bad_set
->list
= smartlist_new();
600 routerset_free(bad_set
);
604 NS(smartlist_new
)(void)
606 CALLED(smartlist_new
)++;
612 #define NS_SUBMODULE ASPECT(routerset_union, one)
615 * Functional test for routerset_union.
619 NS(test_main
)(void *arg
)
621 routerset_t
*src
= routerset_new();
625 tgt
= routerset_new();
626 smartlist_add(src
->list
, tor_strdup("{xx}"));
627 routerset_union(tgt
, src
);
629 tt_int_op(smartlist_len(tgt
->list
), OP_NE
, 0);
637 #define NS_SUBMODULE routerset_is_list
640 * Functional tests for routerset_is_list.
644 NS(test_main
)(void *arg
)
647 addr_policy_t
*policy
;
651 /* len(set->country_names) == 0, len(set->policies) == 0 */
652 set
= routerset_new();
653 is_list
= routerset_is_list(set
);
656 tt_int_op(is_list
, OP_NE
, 0);
658 /* len(set->country_names) != 0, len(set->policies) == 0 */
659 set
= routerset_new();
660 smartlist_add(set
->country_names
, tor_strndup("foo", 3));
661 is_list
= routerset_is_list(set
);
664 tt_int_op(is_list
, OP_EQ
, 0);
666 /* len(set->country_names) == 0, len(set->policies) != 0 */
667 set
= routerset_new();
668 policy
= tor_malloc_zero(sizeof(addr_policy_t
));
669 smartlist_add(set
->policies
, (void *)policy
);
670 is_list
= routerset_is_list(set
);
673 tt_int_op(is_list
, OP_EQ
, 0);
675 /* len(set->country_names) != 0, len(set->policies) != 0 */
676 set
= routerset_new();
677 smartlist_add(set
->country_names
, tor_strndup("foo", 3));
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);
690 #define NS_SUBMODULE routerset_needs_geoip
693 * Functional tests for routerset_needs_geoip.
697 NS(test_main
)(void *arg
)
699 const routerset_t
*set
;
704 needs_geoip
= routerset_needs_geoip(set
);
705 tt_int_op(needs_geoip
, OP_EQ
, 0);
707 set
= routerset_new();
708 needs_geoip
= routerset_needs_geoip(set
);
709 routerset_free((routerset_t
*)set
);
710 tt_int_op(needs_geoip
, OP_EQ
, 0);
713 set
= routerset_new();
714 smartlist_add(set
->country_names
, tor_strndup("xx", 2));
715 needs_geoip
= routerset_needs_geoip(set
);
716 routerset_free((routerset_t
*)set
);
718 tt_int_op(needs_geoip
, OP_NE
, 0);
725 #define NS_SUBMODULE routerset_is_empty
728 * Functional tests for routerset_is_empty.
732 NS(test_main
)(void *arg
)
734 routerset_t
*set
= NULL
;
738 is_empty
= routerset_is_empty(set
);
739 tt_int_op(is_empty
, OP_NE
, 0);
741 set
= routerset_new();
742 is_empty
= routerset_is_empty(set
);
745 tt_int_op(is_empty
, OP_NE
, 0);
747 set
= routerset_new();
748 smartlist_add(set
->list
, tor_strdup("{xx}"));
749 is_empty
= routerset_is_empty(set
);
752 tt_int_op(is_empty
, OP_EQ
, 0);
759 #define NS_SUBMODULE ASPECT(routerset_contains, null_set_or_null_set_list)
762 * Functional test for routerset_contains, when given a NULL set or the
763 * set has a NULL list.
767 NS(test_main
)(void *arg
)
769 routerset_t
*set
= NULL
;
773 contains
= routerset_contains(set
, NULL
, 0, NULL
, NULL
, 0);
775 tt_int_op(contains
, OP_EQ
, 0);
777 set
= tor_malloc_zero(sizeof(routerset_t
));
779 contains
= routerset_contains(set
, NULL
, 0, NULL
, NULL
, 0);
781 tt_int_op(contains
, OP_EQ
, 0);
788 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_nickname)
791 * Functional test for routerset_contains, when given a valid routerset but a
796 NS(test_main
)(void *arg
)
798 routerset_t
*set
= routerset_new();
799 char *nickname
= NULL
;
803 contains
= routerset_contains(set
, NULL
, 0, nickname
, NULL
, 0);
806 tt_int_op(contains
, OP_EQ
, 0);
813 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_nickname)
816 * Functional test for routerset_contains, when given a valid routerset
817 * and the nickname is in the routerset.
821 NS(test_main
)(void *arg
)
823 routerset_t
*set
= routerset_new();
824 const char *nickname
;
828 nickname
= "Foo"; /* This tests the lowercase comparison as well. */
829 strmap_set_lc(set
->names
, nickname
, (void *)1);
830 contains
= routerset_contains(set
, NULL
, 0, nickname
, NULL
, 0);
833 tt_int_op(contains
, OP_EQ
, 4);
839 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_nickname)
842 * Functional test for routerset_contains, when given a valid routerset
843 * and the nickname is not in the routerset.
847 NS(test_main
)(void *arg
)
849 routerset_t
*set
= routerset_new();
853 strmap_set_lc(set
->names
, "bar", (void *)1);
854 contains
= routerset_contains(set
, NULL
, 0, "foo", NULL
, 0);
857 tt_int_op(contains
, OP_EQ
, 0);
863 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_digest)
866 * Functional test for routerset_contains, when given a valid routerset
867 * and the digest is contained in the routerset.
871 NS(test_main
)(void *arg
)
873 routerset_t
*set
= routerset_new();
875 uint8_t foo
[20] = { 2, 3, 4 };
878 digestmap_set(set
->digests
, (const char*)foo
, (void *)1);
879 contains
= routerset_contains(set
, NULL
, 0, NULL
, (const char*)foo
, 0);
882 tt_int_op(contains
, OP_EQ
, 4);
888 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_digest)
891 * Functional test for routerset_contains, when given a valid routerset
892 * and the digest is not contained in the routerset.
896 NS(test_main
)(void *arg
)
898 routerset_t
*set
= routerset_new();
900 uint8_t bar
[20] = { 9, 10, 11, 55 };
901 uint8_t foo
[20] = { 1, 2, 3, 4};
904 digestmap_set(set
->digests
, (const char*)bar
, (void *)1);
905 contains
= routerset_contains(set
, NULL
, 0, NULL
, (const char*)foo
, 0);
908 tt_int_op(contains
, OP_EQ
, 0);
914 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_digest)
917 * Functional test for routerset_contains, when given a valid routerset
918 * and the digest is NULL.
922 NS(test_main
)(void *arg
)
924 routerset_t
*set
= routerset_new();
926 uint8_t bar
[20] = { 9, 10, 11, 55 };
929 digestmap_set(set
->digests
, (const char*)bar
, (void *)1);
930 contains
= routerset_contains(set
, NULL
, 0, NULL
, NULL
, 0);
933 tt_int_op(contains
, OP_EQ
, 0);
939 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_addr)
942 * Structural test for routerset_contains, when given a valid routerset
943 * and the address is rejected by policy.
946 NS_DECL(addr_policy_result_t
, compare_tor_addr_to_addr_policy
,
947 (const tor_addr_t
*addr
, uint16_t port
, const smartlist_t
*policy
));
949 static tor_addr_t MOCK_TOR_ADDR
;
950 #define MOCK_TOR_ADDR_PTR (&MOCK_TOR_ADDR)
953 NS(test_main
)(void *arg
)
955 routerset_t
*set
= routerset_new();
956 tor_addr_t
*addr
= MOCK_TOR_ADDR_PTR
;
960 NS_MOCK(compare_tor_addr_to_addr_policy
);
962 contains
= routerset_contains(set
, addr
, 0, NULL
, NULL
, 0);
965 tt_int_op(CALLED(compare_tor_addr_to_addr_policy
), OP_EQ
, 1);
966 tt_int_op(contains
, OP_EQ
, 3);
973 NS(compare_tor_addr_to_addr_policy
)(const tor_addr_t
*addr
, uint16_t port
,
974 const smartlist_t
*policy
)
978 CALLED(compare_tor_addr_to_addr_policy
)++;
979 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
980 return ADDR_POLICY_REJECTED
;
987 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_addr)
990 * Structural test for routerset_contains, when given a valid routerset
991 * and the address is not rejected by policy.
994 NS_DECL(addr_policy_result_t
, compare_tor_addr_to_addr_policy
,
995 (const tor_addr_t
*addr
, uint16_t port
, const smartlist_t
*policy
));
998 NS(test_main
)(void *arg
)
1000 routerset_t
*set
= routerset_new();
1001 tor_addr_t
*addr
= MOCK_TOR_ADDR_PTR
;
1005 NS_MOCK(compare_tor_addr_to_addr_policy
);
1007 contains
= routerset_contains(set
, addr
, 0, NULL
, NULL
, 0);
1008 routerset_free(set
);
1010 tt_int_op(CALLED(compare_tor_addr_to_addr_policy
), OP_EQ
, 1);
1011 tt_int_op(contains
, OP_EQ
, 0);
1017 addr_policy_result_t
1018 NS(compare_tor_addr_to_addr_policy
)(const tor_addr_t
*addr
, uint16_t port
,
1019 const smartlist_t
*policy
)
1023 CALLED(compare_tor_addr_to_addr_policy
)++;
1024 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1026 return ADDR_POLICY_ACCEPTED
;
1033 #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_addr)
1036 * Structural test for routerset_contains, when given a valid routerset
1037 * and the address is NULL.
1040 NS_DECL(addr_policy_result_t
, compare_tor_addr_to_addr_policy
,
1041 (const tor_addr_t
*addr
, uint16_t port
, const smartlist_t
*policy
));
1044 NS(test_main
)(void *arg
)
1046 routerset_t
*set
= routerset_new();
1050 NS_MOCK(compare_tor_addr_to_addr_policy
);
1052 contains
= routerset_contains(set
, NULL
, 0, NULL
, NULL
, 0);
1053 routerset_free(set
);
1055 tt_int_op(contains
, OP_EQ
, 0);
1061 addr_policy_result_t
1062 NS(compare_tor_addr_to_addr_policy
)(const tor_addr_t
*addr
, uint16_t port
,
1063 const smartlist_t
*policy
)
1067 CALLED(compare_tor_addr_to_addr_policy
)++;
1068 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1070 return ADDR_POLICY_ACCEPTED
;
1077 #define NS_SUBMODULE ASPECT(routerset_contains, countries_no_geoip)
1080 * Structural test for routerset_contains, when there is no matching country
1084 NS_DECL(addr_policy_result_t
, compare_tor_addr_to_addr_policy
,
1085 (const tor_addr_t
*addr
, uint16_t port
, const smartlist_t
*policy
));
1086 NS_DECL(int, geoip_get_country_by_addr
, (const tor_addr_t
*addr
));
1089 NS(test_main
)(void *arg
)
1091 routerset_t
*set
= routerset_new();
1095 NS_MOCK(compare_tor_addr_to_addr_policy
);
1096 NS_MOCK(geoip_get_country_by_addr
);
1098 set
->countries
= bitarray_init_zero(1);
1099 bitarray_set(set
->countries
, 1);
1100 contains
= routerset_contains(set
, MOCK_TOR_ADDR_PTR
, 0, NULL
, NULL
, -1);
1101 routerset_free(set
);
1103 tt_int_op(contains
, OP_EQ
, 0);
1104 tt_int_op(CALLED(compare_tor_addr_to_addr_policy
), OP_EQ
, 1);
1105 tt_int_op(CALLED(geoip_get_country_by_addr
), OP_EQ
, 1);
1111 addr_policy_result_t
1112 NS(compare_tor_addr_to_addr_policy
)(const tor_addr_t
*addr
, uint16_t port
,
1113 const smartlist_t
*policy
)
1117 CALLED(compare_tor_addr_to_addr_policy
)++;
1118 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1121 return ADDR_POLICY_ACCEPTED
;
1125 NS(geoip_get_country_by_addr
)(const tor_addr_t
*addr
)
1127 CALLED(geoip_get_country_by_addr
)++;
1128 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1135 #define NS_SUBMODULE ASPECT(routerset_contains, countries_geoip)
1138 * Structural test for routerset_contains, when there a matching country
1142 NS_DECL(addr_policy_result_t
, compare_tor_addr_to_addr_policy
,
1143 (const tor_addr_t
*addr
, uint16_t port
, const smartlist_t
*policy
));
1144 NS_DECL(int, geoip_get_country_by_addr
, (const tor_addr_t
*addr
));
1147 NS(test_main
)(void *arg
)
1149 routerset_t
*set
= routerset_new();
1153 NS_MOCK(compare_tor_addr_to_addr_policy
);
1154 NS_MOCK(geoip_get_country_by_addr
);
1156 set
->n_countries
= 2;
1157 set
->countries
= bitarray_init_zero(1);
1158 bitarray_set(set
->countries
, 1);
1159 contains
= routerset_contains(set
, MOCK_TOR_ADDR_PTR
, 0, NULL
, NULL
, -1);
1160 routerset_free(set
);
1162 tt_int_op(contains
, OP_EQ
, 2);
1163 tt_int_op(CALLED(compare_tor_addr_to_addr_policy
), OP_EQ
, 1);
1164 tt_int_op(CALLED(geoip_get_country_by_addr
), OP_EQ
, 1);
1170 addr_policy_result_t
1171 NS(compare_tor_addr_to_addr_policy
)(const tor_addr_t
*addr
, uint16_t port
,
1172 const smartlist_t
*policy
)
1176 CALLED(compare_tor_addr_to_addr_policy
)++;
1177 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1180 return ADDR_POLICY_ACCEPTED
;
1184 NS(geoip_get_country_by_addr
)(const tor_addr_t
*addr
)
1186 CALLED(geoip_get_country_by_addr
)++;
1187 tt_ptr_op(addr
, OP_EQ
, MOCK_TOR_ADDR_PTR
);
1194 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, only_flag_and_no_ccs)
1197 * Functional test for routerset_add_unknown_ccs, where only_if_some_cc_set
1198 * is set and there are no country names.
1202 NS(test_main
)(void *arg
)
1204 routerset_t
*set
= routerset_new();
1205 routerset_t
**setp
= &set
;
1209 r
= routerset_add_unknown_ccs(setp
, 1);
1211 tt_int_op(r
, OP_EQ
, 0);
1214 routerset_free(set
);
1218 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, creates_set)
1221 * Functional test for routerset_add_unknown_ccs, where the set argument
1222 * is created if passed in as NULL.
1225 /* The mock is only used to stop the test from asserting erroneously. */
1226 NS_DECL(country_t
, geoip_get_country
, (const char *country
));
1229 NS(test_main
)(void *arg
)
1231 routerset_t
*set
= NULL
;
1232 routerset_t
**setp
= &set
;
1236 NS_MOCK(geoip_get_country
);
1238 r
= routerset_add_unknown_ccs(setp
, 0);
1240 tt_ptr_op(*setp
, OP_NE
, NULL
);
1241 tt_int_op(r
, OP_EQ
, 0);
1245 routerset_free(set
);
1249 NS(geoip_get_country
)(const char *country
)
1252 CALLED(geoip_get_country
)++;
1258 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, add_unknown)
1261 * Structural test for routerset_add_unknown_ccs, that the "{??}"
1262 * country code is added to the list.
1265 NS_DECL(country_t
, geoip_get_country
, (const char *country
));
1266 NS_DECL(int, geoip_is_loaded
, (sa_family_t family
));
1269 NS(test_main
)(void *arg
)
1271 routerset_t
*set
= routerset_new();
1272 routerset_t
**setp
= &set
;
1276 NS_MOCK(geoip_get_country
);
1277 NS_MOCK(geoip_is_loaded
);
1279 r
= routerset_add_unknown_ccs(setp
, 0);
1281 tt_int_op(r
, OP_EQ
, 1);
1282 tt_int_op(smartlist_contains_string(set
->country_names
, "??"), OP_EQ
, 1);
1283 tt_int_op(smartlist_contains_string(set
->list
, "{??}"), OP_EQ
, 1);
1287 routerset_free(set
);
1291 NS(geoip_get_country
)(const char *country
)
1293 int arg_is_qq
, arg_is_a1
;
1295 CALLED(geoip_get_country
)++;
1297 arg_is_qq
= !strcmp(country
, "??");
1298 arg_is_a1
= !strcmp(country
, "A1");
1300 tt_int_op(arg_is_qq
|| arg_is_a1
, OP_EQ
, 1);
1310 NS(geoip_is_loaded
)(sa_family_t family
)
1312 CALLED(geoip_is_loaded
)++;
1314 tt_int_op(family
, OP_EQ
, AF_INET
);
1321 #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, add_a1)
1324 * Structural test for routerset_add_unknown_ccs, that the "{a1}"
1325 * country code is added to the list.
1328 NS_DECL(country_t
, geoip_get_country
, (const char *country
));
1329 NS_DECL(int, geoip_is_loaded
, (sa_family_t family
));
1332 NS(test_main
)(void *arg
)
1334 routerset_t
*set
= routerset_new();
1335 routerset_t
**setp
= &set
;
1339 NS_MOCK(geoip_get_country
);
1340 NS_MOCK(geoip_is_loaded
);
1342 r
= routerset_add_unknown_ccs(setp
, 0);
1344 tt_int_op(r
, OP_EQ
, 1);
1345 tt_int_op(smartlist_contains_string(set
->country_names
, "a1"), OP_EQ
, 1);
1346 tt_int_op(smartlist_contains_string(set
->list
, "{a1}"), OP_EQ
, 1);
1350 routerset_free(set
);
1354 NS(geoip_get_country
)(const char *country
)
1356 int arg_is_qq
, arg_is_a1
;
1358 CALLED(geoip_get_country
)++;
1360 arg_is_qq
= !strcmp(country
, "??");
1361 arg_is_a1
= !strcmp(country
, "A1");
1363 tt_int_op(arg_is_qq
|| arg_is_a1
, OP_EQ
, 1);
1373 NS(geoip_is_loaded
)(sa_family_t family
)
1375 CALLED(geoip_is_loaded
)++;
1377 tt_int_op(family
, OP_EQ
, AF_INET
);
1384 #define NS_SUBMODULE routerset_contains_extendinfo
1387 * Functional test for routerset_contains_extendinfo.
1391 NS(test_main
)(void *arg
)
1393 routerset_t
*set
= routerset_new();
1396 const char *nickname
= "foo";
1399 memset(&ei
, 0, sizeof(ei
));
1400 strmap_set_lc(set
->names
, nickname
, (void *)1);
1401 strncpy(ei
.nickname
, nickname
, sizeof(ei
.nickname
) - 1);
1402 ei
.nickname
[sizeof(ei
.nickname
) - 1] = '\0';
1404 r
= routerset_contains_extendinfo(set
, &ei
);
1406 tt_int_op(r
, OP_EQ
, 4);
1408 routerset_free(set
);
1412 #define NS_SUBMODULE routerset_contains_router
1415 * Functional test for routerset_contains_router.
1419 NS(test_main
)(void *arg
)
1421 routerset_t
*set
= routerset_new();
1423 country_t country
= 1;
1425 const char *nickname
= "foo";
1428 memset(&ri
, 0, sizeof(ri
));
1429 strmap_set_lc(set
->names
, nickname
, (void *)1);
1430 ri
.nickname
= (char *)nickname
;
1432 r
= routerset_contains_router(set
, &ri
, country
);
1434 tt_int_op(r
, OP_EQ
, 4);
1436 routerset_free(set
);
1440 #define NS_SUBMODULE routerset_contains_routerstatus
1443 * Functional test for routerset_contains_routerstatus.
1446 // XXX: This is a bit brief. It only populates and tests the nickname fields
1447 // ie., enough to make the containment check succeed. Perhaps it should do
1448 // a bit more or test a bit more.
1451 NS(test_main
)(void *arg
)
1453 routerset_t
*set
= routerset_new();
1455 country_t country
= 1;
1457 const char *nickname
= "foo";
1460 memset(&rs
, 0, sizeof(rs
));
1461 strmap_set_lc(set
->names
, nickname
, (void *)1);
1462 strncpy(rs
.nickname
, nickname
, sizeof(rs
.nickname
) - 1);
1463 rs
.nickname
[sizeof(rs
.nickname
) - 1] = '\0';
1465 r
= routerset_contains_routerstatus(set
, &rs
, country
);
1467 tt_int_op(r
, OP_EQ
, 4);
1469 routerset_free(set
);
1473 #define NS_SUBMODULE ASPECT(routerset_contains_node, none)
1476 * Functional test for routerset_contains_node, when the node has no
1477 * routerset or routerinfo.
1480 static node_t
NS(mock_node
);
1483 NS(test_main
)(void *arg
)
1485 routerset_t
*set
= routerset_new();
1489 NS(mock_node
).ri
= NULL
;
1490 NS(mock_node
).rs
= NULL
;
1492 r
= routerset_contains_node(set
, &NS(mock_node
));
1493 tt_int_op(r
, OP_EQ
, 0);
1496 routerset_free(set
);
1500 #define NS_SUBMODULE ASPECT(routerset_contains_node, routerstatus)
1503 * Functional test for routerset_contains_node, when the node has a
1504 * routerset and no routerinfo.
1507 static node_t
NS(mock_node
);
1510 NS(test_main
)(void *arg
)
1512 routerset_t
*set
= routerset_new();
1514 const char *nickname
= "foo";
1518 strmap_set_lc(set
->names
, nickname
, (void *)1);
1520 strncpy(rs
.nickname
, nickname
, sizeof(rs
.nickname
) - 1);
1521 rs
.nickname
[sizeof(rs
.nickname
) - 1] = '\0';
1522 NS(mock_node
).ri
= NULL
;
1523 NS(mock_node
).rs
= &rs
;
1525 r
= routerset_contains_node(set
, &NS(mock_node
));
1527 tt_int_op(r
, OP_EQ
, 4);
1529 routerset_free(set
);
1533 #define NS_SUBMODULE ASPECT(routerset_contains_node, routerinfo)
1536 * Functional test for routerset_contains_node, when the node has no
1537 * routerset and a routerinfo.
1541 NS(test_main
)(void *arg
)
1543 routerset_t
*set
= routerset_new();
1545 const char *nickname
= "foo";
1550 strmap_set_lc(set
->names
, nickname
, (void *)1);
1552 ri
.nickname
= (char *)nickname
;
1554 mock_node
.rs
= NULL
;
1556 r
= routerset_contains_node(set
, &mock_node
);
1558 tt_int_op(r
, OP_EQ
, 4);
1560 routerset_free(set
);
1564 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, no_routerset)
1567 * Functional test for routerset_get_all_nodes, when routerset is NULL or
1568 * the routerset list is NULL.
1572 NS(test_main
)(void *arg
)
1574 smartlist_t
*out
= smartlist_new();
1575 routerset_t
*set
= NULL
;
1578 tt_int_op(smartlist_len(out
), OP_EQ
, 0);
1579 routerset_get_all_nodes(out
, NULL
, NULL
, 0);
1581 tt_int_op(smartlist_len(out
), OP_EQ
, 0);
1583 set
= routerset_new();
1584 smartlist_free(set
->list
);
1585 routerset_get_all_nodes(out
, NULL
, NULL
, 0);
1586 tt_int_op(smartlist_len(out
), OP_EQ
, 0);
1588 /* Just recreate list, so we can simply use routerset_free. */
1589 set
->list
= smartlist_new();
1592 routerset_free(set
);
1593 smartlist_free(out
);
1597 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list_with_no_nodes)
1600 * Structural test for routerset_get_all_nodes, when the routerset list
1604 NS_DECL(const node_t
*, node_get_by_nickname
,
1605 (const char *nickname
, int warn_if_unused
));
1606 static const char *NS(mock_nickname
);
1609 NS(test_main
)(void *arg
)
1611 smartlist_t
*out
= smartlist_new();
1612 routerset_t
*set
= routerset_new();
1616 NS_MOCK(node_get_by_nickname
);
1618 NS(mock_nickname
) = "foo";
1619 smartlist_add(set
->list
, tor_strdup(NS(mock_nickname
)));
1621 routerset_get_all_nodes(out
, set
, NULL
, 0);
1622 out_len
= smartlist_len(out
);
1624 smartlist_free(out
);
1625 routerset_free(set
);
1627 tt_int_op(out_len
, OP_EQ
, 0);
1628 tt_int_op(CALLED(node_get_by_nickname
), OP_EQ
, 1);
1635 NS(node_get_by_nickname
)(const char *nickname
, int warn_if_unused
)
1637 CALLED(node_get_by_nickname
)++;
1638 tt_str_op(nickname
, OP_EQ
, NS(mock_nickname
));
1639 tt_int_op(warn_if_unused
, OP_EQ
, 1);
1646 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list_flag_not_running)
1649 * Structural test for routerset_get_all_nodes, with the running_only flag
1650 * is set but the nodes are not running.
1653 NS_DECL(const node_t
*, node_get_by_nickname
,
1654 (const char *nickname
, int warn_if_unused
));
1655 static const char *NS(mock_nickname
);
1656 static node_t
NS(mock_node
);
1659 NS(test_main
)(void *arg
)
1661 smartlist_t
*out
= smartlist_new();
1662 routerset_t
*set
= routerset_new();
1666 NS_MOCK(node_get_by_nickname
);
1668 NS(mock_node
).is_running
= 0;
1669 NS(mock_nickname
) = "foo";
1670 smartlist_add(set
->list
, tor_strdup(NS(mock_nickname
)));
1672 routerset_get_all_nodes(out
, set
, NULL
, 1);
1673 out_len
= smartlist_len(out
);
1675 smartlist_free(out
);
1676 routerset_free(set
);
1678 tt_int_op(out_len
, OP_EQ
, 0);
1679 tt_int_op(CALLED(node_get_by_nickname
), OP_EQ
, 1);
1686 NS(node_get_by_nickname
)(const char *nickname
, int warn_if_unused
)
1688 CALLED(node_get_by_nickname
)++;
1689 tt_str_op(nickname
, OP_EQ
, NS(mock_nickname
));
1690 tt_int_op(warn_if_unused
, OP_EQ
, 1);
1693 return &NS(mock_node
);
1697 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list)
1700 * Structural test for routerset_get_all_nodes.
1703 NS_DECL(const node_t
*, node_get_by_nickname
,
1704 (const char *nickname
, int warn_if_unused
));
1705 static char *NS(mock_nickname
);
1706 static node_t
NS(mock_node
);
1709 NS(test_main
)(void *arg
)
1711 smartlist_t
*out
= smartlist_new();
1712 routerset_t
*set
= routerset_new();
1717 NS_MOCK(node_get_by_nickname
);
1719 NS(mock_nickname
) = tor_strdup("foo");
1720 smartlist_add(set
->list
, NS(mock_nickname
));
1722 routerset_get_all_nodes(out
, set
, NULL
, 0);
1723 out_len
= smartlist_len(out
);
1724 ent
= (node_t
*)smartlist_get(out
, 0);
1726 smartlist_free(out
);
1727 routerset_free(set
);
1729 tt_int_op(out_len
, OP_EQ
, 1);
1730 tt_ptr_op(ent
, OP_EQ
, &NS(mock_node
));
1731 tt_int_op(CALLED(node_get_by_nickname
), OP_EQ
, 1);
1738 NS(node_get_by_nickname
)(const char *nickname
, int warn_if_unused
)
1740 CALLED(node_get_by_nickname
)++;
1741 tt_str_op(nickname
, OP_EQ
, NS(mock_nickname
));
1742 tt_int_op(warn_if_unused
, OP_EQ
, 1);
1745 return &NS(mock_node
);
1749 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, nodelist_with_no_nodes)
1752 * Structural test for routerset_get_all_nodes, when the nodelist has no nodes.
1755 NS_DECL(smartlist_t
*, nodelist_get_list
, (void));
1757 static smartlist_t
*NS(mock_smartlist
);
1760 NS(test_main
)(void *arg
)
1762 routerset_t
*set
= routerset_new();
1763 smartlist_t
*out
= smartlist_new();
1767 NS_MOCK(nodelist_get_list
);
1769 smartlist_add(set
->country_names
, tor_strdup("{xx}"));
1770 NS(mock_smartlist
) = smartlist_new();
1772 routerset_get_all_nodes(out
, set
, NULL
, 1);
1773 r
= smartlist_len(out
);
1774 routerset_free(set
);
1775 smartlist_free(out
);
1776 smartlist_free(NS(mock_smartlist
));
1778 tt_int_op(r
, OP_EQ
, 0);
1779 tt_int_op(CALLED(nodelist_get_list
), OP_EQ
, 1);
1786 NS(nodelist_get_list
)(void)
1788 CALLED(nodelist_get_list
)++;
1790 return NS(mock_smartlist
);
1794 #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, nodelist_flag_not_running)
1797 * Structural test for routerset_get_all_nodes, with a non-list routerset
1798 * the running_only flag is set, but the nodes are not running.
1801 NS_DECL(smartlist_t
*, nodelist_get_list
, (void));
1803 static smartlist_t
*NS(mock_smartlist
);
1804 static node_t
NS(mock_node
);
1807 NS(test_main
)(void *arg
)
1809 routerset_t
*set
= routerset_new();
1810 smartlist_t
*out
= smartlist_new();
1814 NS_MOCK(nodelist_get_list
);
1816 smartlist_add(set
->country_names
, tor_strdup("{xx}"));
1817 NS(mock_smartlist
) = smartlist_new();
1818 NS(mock_node
).is_running
= 0;
1819 smartlist_add(NS(mock_smartlist
), (void *)&NS(mock_node
));
1821 routerset_get_all_nodes(out
, set
, NULL
, 1);
1822 r
= smartlist_len(out
);
1823 routerset_free(set
);
1824 smartlist_free(out
);
1825 smartlist_free(NS(mock_smartlist
));
1827 tt_int_op(r
, OP_EQ
, 0);
1828 tt_int_op(CALLED(nodelist_get_list
), OP_EQ
, 1);
1835 NS(nodelist_get_list
)(void)
1837 CALLED(nodelist_get_list
)++;
1839 return NS(mock_smartlist
);
1843 #define NS_SUBMODULE routerset_subtract_nodes
1846 * Functional test for routerset_subtract_nodes.
1850 NS(test_main
)(void *arg
)
1852 routerset_t
*set
= routerset_new();
1853 smartlist_t
*list
= smartlist_new();
1854 const char *nickname
= "foo";
1859 strmap_set_lc(set
->names
, nickname
, (void *)1);
1861 ri
.nickname
= (char *)nickname
;
1862 mock_node
.rs
= NULL
;
1864 smartlist_add(list
, (void *)&mock_node
);
1866 tt_int_op(smartlist_len(list
), OP_NE
, 0);
1867 routerset_subtract_nodes(list
, set
);
1869 tt_int_op(smartlist_len(list
), OP_EQ
, 0);
1871 routerset_free(set
);
1872 smartlist_free(list
);
1876 #define NS_SUBMODULE ASPECT(routerset_subtract_nodes, null_routerset)
1879 * Functional test for routerset_subtract_nodes, with a NULL routerset.
1883 NS(test_main
)(void *arg
)
1885 routerset_t
*set
= NULL
;
1886 smartlist_t
*list
= smartlist_new();
1887 const char *nickname
= "foo";
1892 ri
.nickname
= (char *)nickname
;
1894 smartlist_add(list
, (void *)&mock_node
);
1896 tt_int_op(smartlist_len(list
), OP_NE
, 0);
1897 routerset_subtract_nodes(list
, set
);
1899 tt_int_op(smartlist_len(list
), OP_NE
, 0);
1901 routerset_free(set
);
1902 smartlist_free(list
);
1906 #define NS_SUBMODULE routerset_to_string
1909 * Functional test for routerset_to_string.
1913 NS(test_main
)(void *arg
)
1915 routerset_t
*set
= NULL
;
1920 s
= routerset_to_string(set
);
1921 tt_str_op(s
, OP_EQ
, "");
1924 set
= routerset_new();
1925 s
= routerset_to_string(set
);
1926 tt_str_op(s
, OP_EQ
, "");
1928 routerset_free(set
); set
= NULL
;
1930 set
= routerset_new();
1931 smartlist_add(set
->list
, tor_strndup("a", 1));
1932 s
= routerset_to_string(set
);
1933 tt_str_op(s
, OP_EQ
, "a");
1935 routerset_free(set
); set
= NULL
;
1937 set
= routerset_new();
1938 smartlist_add(set
->list
, tor_strndup("a", 1));
1939 smartlist_add(set
->list
, tor_strndup("b", 1));
1940 s
= routerset_to_string(set
);
1941 tt_str_op(s
, OP_EQ
, "a,b");
1943 routerset_free(set
); set
= NULL
;
1947 routerset_free((routerset_t
*)set
);
1951 #define NS_SUBMODULE ASPECT(routerset_equal, empty_empty)
1954 * Functional test for routerset_equal, with both routersets empty.
1958 NS(test_main
)(void *arg
)
1960 routerset_t
*a
= routerset_new(), *b
= routerset_new();
1964 r
= routerset_equal(a
, b
);
1968 tt_int_op(r
, OP_EQ
, 1);
1975 #define NS_SUBMODULE ASPECT(routerset_equal, empty_not_empty)
1978 * Functional test for routerset_equal, with one routersets empty.
1982 NS(test_main
)(void *arg
)
1984 routerset_t
*a
= routerset_new(), *b
= routerset_new();
1988 smartlist_add(b
->list
, tor_strdup("{xx}"));
1989 r
= routerset_equal(a
, b
);
1993 tt_int_op(r
, OP_EQ
, 0);
1999 #define NS_SUBMODULE ASPECT(routerset_equal, differing_lengths)
2002 * Functional test for routerset_equal, with the routersets having
2003 * differing lengths.
2007 NS(test_main
)(void *arg
)
2009 routerset_t
*a
= routerset_new(), *b
= routerset_new();
2013 smartlist_add(a
->list
, tor_strdup("{aa}"));
2014 smartlist_add(b
->list
, tor_strdup("{b1}"));
2015 smartlist_add(b
->list
, tor_strdup("{b2}"));
2016 r
= routerset_equal(a
, b
);
2020 tt_int_op(r
, OP_EQ
, 0);
2026 #define NS_SUBMODULE ASPECT(routerset_equal, unequal)
2029 * Functional test for routerset_equal, with the routersets being
2034 NS(test_main
)(void *arg
)
2036 routerset_t
*a
= routerset_new(), *b
= routerset_new();
2040 smartlist_add(a
->list
, tor_strdup("foo"));
2041 smartlist_add(b
->list
, tor_strdup("bar"));
2042 r
= routerset_equal(a
, b
);
2046 tt_int_op(r
, OP_EQ
, 0);
2052 #define NS_SUBMODULE ASPECT(routerset_equal, equal)
2055 * Functional test for routerset_equal, with the routersets being
2060 NS(test_main
)(void *arg
)
2062 routerset_t
*a
= routerset_new(), *b
= routerset_new();
2066 smartlist_add(a
->list
, tor_strdup("foo"));
2067 smartlist_add(b
->list
, tor_strdup("foo"));
2068 r
= routerset_equal(a
, b
);
2072 tt_int_op(r
, OP_EQ
, 1);
2078 #define NS_SUBMODULE ASPECT(routerset_free, null_routerset)
2081 * Structural test for routerset_free, where the routerset is NULL.
2084 NS_DECL(void, smartlist_free
, (smartlist_t
*sl
));
2087 NS(test_main
)(void *arg
)
2091 NS_MOCK(smartlist_free
);
2093 routerset_free(NULL
);
2095 tt_int_op(CALLED(smartlist_free
), OP_EQ
, 0);
2102 NS(smartlist_free
)(smartlist_t
*s
)
2105 CALLED(smartlist_free
)++;
2109 #define NS_SUBMODULE routerset_free
2112 * Structural test for routerset_free.
2115 NS_DECL(void, smartlist_free
, (smartlist_t
*sl
));
2116 NS_DECL(void, strmap_free
,(strmap_t
*map
, void (*free_val
)(void*)));
2117 NS_DECL(void, digestmap_free
, (digestmap_t
*map
, void (*free_val
)(void*)));
2120 NS(test_main
)(void *arg
)
2122 routerset_t
*routerset
= routerset_new();
2125 NS_MOCK(smartlist_free
);
2126 NS_MOCK(strmap_free
);
2127 NS_MOCK(digestmap_free
);
2129 routerset_free(routerset
);
2131 tt_int_op(CALLED(smartlist_free
), OP_NE
, 0);
2132 tt_int_op(CALLED(strmap_free
), OP_NE
, 0);
2133 tt_int_op(CALLED(digestmap_free
), OP_NE
, 0);
2140 NS(smartlist_free
)(smartlist_t
*s
)
2142 CALLED(smartlist_free
)++;
2143 smartlist_free__real(s
);
2147 NS(strmap_free
)(strmap_t
*map
, void (*free_val
)(void*))
2149 CALLED(strmap_free
)++;
2150 strmap_free__real(map
, free_val
);
2154 NS(digestmap_free
)(digestmap_t
*map
, void (*free_val
)(void*))
2156 CALLED(digestmap_free
)++;
2157 digestmap_free__real(map
, free_val
);
2162 struct testcase_t routerset_tests
[] = {
2163 TEST_CASE(routerset_new
),
2164 TEST_CASE(routerset_get_countryname
),
2165 TEST_CASE(routerset_is_list
),
2166 TEST_CASE(routerset_needs_geoip
),
2167 TEST_CASE(routerset_is_empty
),
2168 TEST_CASE_ASPECT(routerset_contains
, null_set_or_null_set_list
),
2169 TEST_CASE_ASPECT(routerset_contains
, set_and_nickname
),
2170 TEST_CASE_ASPECT(routerset_contains
, set_and_null_nickname
),
2171 TEST_CASE_ASPECT(routerset_contains
, set_and_no_nickname
),
2172 TEST_CASE_ASPECT(routerset_contains
, set_and_digest
),
2173 TEST_CASE_ASPECT(routerset_contains
, set_and_no_digest
),
2174 TEST_CASE_ASPECT(routerset_contains
, set_and_null_digest
),
2175 TEST_CASE_ASPECT(routerset_contains
, set_and_addr
),
2176 TEST_CASE_ASPECT(routerset_contains
, set_and_no_addr
),
2177 TEST_CASE_ASPECT(routerset_contains
, set_and_null_addr
),
2178 TEST_CASE_ASPECT(routerset_contains
, countries_no_geoip
),
2179 TEST_CASE_ASPECT(routerset_contains
, countries_geoip
),
2180 TEST_CASE_ASPECT(routerset_add_unknown_ccs
, only_flag_and_no_ccs
),
2181 TEST_CASE_ASPECT(routerset_add_unknown_ccs
, creates_set
),
2182 TEST_CASE_ASPECT(routerset_add_unknown_ccs
, add_unknown
),
2183 TEST_CASE_ASPECT(routerset_add_unknown_ccs
, add_a1
),
2184 TEST_CASE(routerset_contains_extendinfo
),
2185 TEST_CASE(routerset_contains_router
),
2186 TEST_CASE(routerset_contains_routerstatus
),
2187 TEST_CASE_ASPECT(routerset_contains_node
, none
),
2188 TEST_CASE_ASPECT(routerset_contains_node
, routerinfo
),
2189 TEST_CASE_ASPECT(routerset_contains_node
, routerstatus
),
2190 TEST_CASE_ASPECT(routerset_get_all_nodes
, no_routerset
),
2191 TEST_CASE_ASPECT(routerset_get_all_nodes
, list_with_no_nodes
),
2192 TEST_CASE_ASPECT(routerset_get_all_nodes
, list_flag_not_running
),
2193 TEST_CASE_ASPECT(routerset_get_all_nodes
, list
),
2194 TEST_CASE_ASPECT(routerset_get_all_nodes
, nodelist_with_no_nodes
),
2195 TEST_CASE_ASPECT(routerset_get_all_nodes
, nodelist_flag_not_running
),
2196 TEST_CASE_ASPECT(routerset_refresh_counties
, geoip_not_loaded
),
2197 TEST_CASE_ASPECT(routerset_refresh_counties
, no_countries
),
2198 TEST_CASE_ASPECT(routerset_refresh_counties
, one_valid_country
),
2199 TEST_CASE_ASPECT(routerset_refresh_counties
, one_invalid_country
),
2200 TEST_CASE_ASPECT(routerset_union
, source_bad
),
2201 TEST_CASE_ASPECT(routerset_union
, one
),
2202 TEST_CASE_ASPECT(routerset_parse
, malformed
),
2203 TEST_CASE_ASPECT(routerset_parse
, valid_hexdigest
),
2204 TEST_CASE_ASPECT(routerset_parse
, valid_nickname
),
2205 TEST_CASE_ASPECT(routerset_parse
, get_countryname
),
2206 TEST_CASE_ASPECT(routerset_parse
, policy_wildcard
),
2207 TEST_CASE_ASPECT(routerset_parse
, policy_ipv4
),
2208 TEST_CASE_ASPECT(routerset_parse
, policy_ipv6
),
2209 TEST_CASE(routerset_subtract_nodes
),
2210 TEST_CASE_ASPECT(routerset_subtract_nodes
, null_routerset
),
2211 TEST_CASE(routerset_to_string
),
2212 TEST_CASE_ASPECT(routerset_equal
, empty_empty
),
2213 TEST_CASE_ASPECT(routerset_equal
, empty_not_empty
),
2214 TEST_CASE_ASPECT(routerset_equal
, differing_lengths
),
2215 TEST_CASE_ASPECT(routerset_equal
, unequal
),
2216 TEST_CASE_ASPECT(routerset_equal
, equal
),
2217 TEST_CASE_ASPECT(routerset_free
, null_routerset
),
2218 TEST_CASE(routerset_free
),