Back to index

tor  0.2.3.19-rc
test_dir.c
Go to the documentation of this file.
00001 /* Copyright (c) 2001-2004, Roger Dingledine.
00002  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00003  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00004 /* See LICENSE for licensing information */
00005 
00006 #include "orconfig.h"
00007 #define DIRSERV_PRIVATE
00008 #define DIRVOTE_PRIVATE
00009 #define ROUTER_PRIVATE
00010 #define HIBERNATE_PRIVATE
00011 #include "or.h"
00012 #include "directory.h"
00013 #include "dirserv.h"
00014 #include "dirvote.h"
00015 #include "hibernate.h"
00016 #include "networkstatus.h"
00017 #include "router.h"
00018 #include "routerlist.h"
00019 #include "routerparse.h"
00020 #include "test.h"
00021 
00022 static void
00023 test_dir_nicknames(void)
00024 {
00025   test_assert( is_legal_nickname("a"));
00026   test_assert(!is_legal_nickname(""));
00027   test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
00028   test_assert(!is_legal_nickname("hyphen-")); /* bad char */
00029   test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
00030   test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
00031   /* valid */
00032   test_assert( is_legal_nickname_or_hexdigest(
00033                                  "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
00034   test_assert( is_legal_nickname_or_hexdigest(
00035                          "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
00036   test_assert( is_legal_nickname_or_hexdigest(
00037                          "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
00038   /* too short */
00039   test_assert(!is_legal_nickname_or_hexdigest(
00040                                  "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
00041   /* illegal char */
00042   test_assert(!is_legal_nickname_or_hexdigest(
00043                                  "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
00044   /* hex part too long */
00045   test_assert(!is_legal_nickname_or_hexdigest(
00046                          "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
00047   test_assert(!is_legal_nickname_or_hexdigest(
00048                          "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
00049   /* Bad nickname */
00050   test_assert(!is_legal_nickname_or_hexdigest(
00051                          "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
00052   test_assert(!is_legal_nickname_or_hexdigest(
00053                          "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
00054   test_assert(!is_legal_nickname_or_hexdigest(
00055                        "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
00056   test_assert(!is_legal_nickname_or_hexdigest(
00057                        "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
00058                        "abcdefghijklmnoppqrst"));
00059   /* Bad extra char. */
00060   test_assert(!is_legal_nickname_or_hexdigest(
00061                          "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
00062   test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
00063   test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
00064   test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
00065  done:
00066   ;
00067 }
00068 
00070 static void
00071 test_dir_formats(void)
00072 {
00073   char buf[8192], buf2[8192];
00074   char platform[256];
00075   char fingerprint[FINGERPRINT_LEN+1];
00076   char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
00077   size_t pk1_str_len, pk2_str_len, pk3_str_len;
00078   routerinfo_t *r1=NULL, *r2=NULL;
00079   crypto_pk_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
00080   routerinfo_t *rp1 = NULL;
00081   addr_policy_t *ex1, *ex2;
00082   routerlist_t *dir1 = NULL, *dir2 = NULL;
00083 
00084   pk1 = pk_generate(0);
00085   pk2 = pk_generate(1);
00086   pk3 = pk_generate(2);
00087 
00088   test_assert(pk1 && pk2 && pk3);
00089 
00090   hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
00091 
00092   get_platform_str(platform, sizeof(platform));
00093   r1 = tor_malloc_zero(sizeof(routerinfo_t));
00094   r1->address = tor_strdup("18.244.0.1");
00095   r1->addr = 0xc0a80001u; /* 192.168.0.1 */
00096   r1->cache_info.published_on = 0;
00097   r1->or_port = 9000;
00098   r1->dir_port = 9003;
00099   tor_addr_parse(&r1->ipv6_addr, "1:2:3:4::");
00100   r1->ipv6_orport = 9999;
00101   r1->onion_pkey = crypto_pk_dup_key(pk1);
00102   r1->identity_pkey = crypto_pk_dup_key(pk2);
00103   r1->bandwidthrate = 1000;
00104   r1->bandwidthburst = 5000;
00105   r1->bandwidthcapacity = 10000;
00106   r1->exit_policy = NULL;
00107   r1->nickname = tor_strdup("Magri");
00108   r1->platform = tor_strdup(platform);
00109 
00110   ex1 = tor_malloc_zero(sizeof(addr_policy_t));
00111   ex2 = tor_malloc_zero(sizeof(addr_policy_t));
00112   ex1->policy_type = ADDR_POLICY_ACCEPT;
00113   tor_addr_from_ipv4h(&ex1->addr, 0);
00114   ex1->maskbits = 0;
00115   ex1->prt_min = ex1->prt_max = 80;
00116   ex2->policy_type = ADDR_POLICY_REJECT;
00117   tor_addr_from_ipv4h(&ex2->addr, 18<<24);
00118   ex2->maskbits = 8;
00119   ex2->prt_min = ex2->prt_max = 24;
00120   r2 = tor_malloc_zero(sizeof(routerinfo_t));
00121   r2->address = tor_strdup("1.1.1.1");
00122   r2->addr = 0x0a030201u; /* 10.3.2.1 */
00123   r2->platform = tor_strdup(platform);
00124   r2->cache_info.published_on = 5;
00125   r2->or_port = 9005;
00126   r2->dir_port = 0;
00127   r2->onion_pkey = crypto_pk_dup_key(pk2);
00128   r2->identity_pkey = crypto_pk_dup_key(pk1);
00129   r2->bandwidthrate = r2->bandwidthburst = r2->bandwidthcapacity = 3000;
00130   r2->exit_policy = smartlist_new();
00131   smartlist_add(r2->exit_policy, ex2);
00132   smartlist_add(r2->exit_policy, ex1);
00133   r2->nickname = tor_strdup("Fred");
00134 
00135   test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
00136                                                     &pk1_str_len));
00137   test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
00138                                                     &pk2_str_len));
00139   test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
00140                                                     &pk3_str_len));
00141 
00142   memset(buf, 0, 2048);
00143   test_assert(router_dump_router_to_string(buf, 2048, r1, pk2)>0);
00144 
00145   strlcpy(buf2, "router Magri 18.244.0.1 9000 0 9003\n"
00146           "or-address [1:2:3:4::]:9999\n"
00147           "platform Tor "VERSION" on ", sizeof(buf2));
00148   strlcat(buf2, get_uname(), sizeof(buf2));
00149   strlcat(buf2, "\n"
00150           "opt protocols Link 1 2 Circuit 1\n"
00151           "published 1970-01-01 00:00:00\n"
00152           "opt fingerprint ", sizeof(buf2));
00153   test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
00154   strlcat(buf2, fingerprint, sizeof(buf2));
00155   strlcat(buf2, "\nuptime 0\n"
00156   /* XXX the "0" above is hard-coded, but even if we made it reflect
00157    * uptime, that still wouldn't make it right, because the two
00158    * descriptors might be made on different seconds... hm. */
00159          "bandwidth 1000 5000 10000\n"
00160           "onion-key\n", sizeof(buf2));
00161   strlcat(buf2, pk1_str, sizeof(buf2));
00162   strlcat(buf2, "signing-key\n", sizeof(buf2));
00163   strlcat(buf2, pk2_str, sizeof(buf2));
00164   strlcat(buf2, "opt hidden-service-dir\n", sizeof(buf2));
00165   strlcat(buf2, "reject *:*\nrouter-signature\n", sizeof(buf2));
00166   buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
00167                              * twice */
00168 
00169   test_streq(buf, buf2);
00170 
00171   test_assert(router_dump_router_to_string(buf, 2048, r1, pk2)>0);
00172   cp = buf;
00173   rp1 = router_parse_entry_from_string((const char*)cp,NULL,1,0,NULL);
00174   test_assert(rp1);
00175   test_streq(rp1->address, r1->address);
00176   test_eq(rp1->or_port, r1->or_port);
00177   //test_eq(rp1->dir_port, r1->dir_port);
00178   test_eq(rp1->bandwidthrate, r1->bandwidthrate);
00179   test_eq(rp1->bandwidthburst, r1->bandwidthburst);
00180   test_eq(rp1->bandwidthcapacity, r1->bandwidthcapacity);
00181   test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
00182   test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
00183   //test_assert(rp1->exit_policy == NULL);
00184 
00185 #if 0
00186   /* XXX Once we have exit policies, test this again. XXX */
00187   strlcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n", sizeof(buf2));
00188   strlcat(buf2, pk2_str, sizeof(buf2));
00189   strlcat(buf2, "signing-key\n", sizeof(buf2));
00190   strlcat(buf2, pk1_str, sizeof(buf2));
00191   strlcat(buf2, "accept *:80\nreject 18.*:24\n\n", sizeof(buf2));
00192   test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
00193   test_streq(buf, buf2);
00194 
00195   cp = buf;
00196   rp2 = router_parse_entry_from_string(&cp,1);
00197   test_assert(rp2);
00198   test_streq(rp2->address, r2.address);
00199   test_eq(rp2->or_port, r2.or_port);
00200   test_eq(rp2->dir_port, r2.dir_port);
00201   test_eq(rp2->bandwidth, r2.bandwidth);
00202   test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
00203   test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
00204   test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
00205   test_streq(rp2->exit_policy->string, "accept *:80");
00206   test_streq(rp2->exit_policy->address, "*");
00207   test_streq(rp2->exit_policy->port, "80");
00208   test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
00209   test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
00210   test_streq(rp2->exit_policy->next->address, "18.*");
00211   test_streq(rp2->exit_policy->next->port, "24");
00212   test_assert(rp2->exit_policy->next->next == NULL);
00213 
00214   /* Okay, now for the directories. */
00215   {
00216     fingerprint_list = smartlist_new();
00217     crypto_pk_get_fingerprint(pk2, buf, 1);
00218     add_fingerprint_to_dir("Magri", buf, fingerprint_list);
00219     crypto_pk_get_fingerprint(pk1, buf, 1);
00220     add_fingerprint_to_dir("Fred", buf, fingerprint_list);
00221   }
00222 
00223   {
00224   char d[DIGEST_LEN];
00225   const char *m;
00226   /* XXXX NM re-enable. */
00227   /* Make sure routers aren't too far in the past any more. */
00228   r1->cache_info.published_on = time(NULL);
00229   r2->cache_info.published_on = time(NULL)-3*60*60;
00230   test_assert(router_dump_router_to_string(buf, 2048, r1, pk2)>0);
00231   test_eq(dirserv_add_descriptor(buf,&m,""), ROUTER_ADDED_NOTIFY_GENERATOR);
00232   test_assert(router_dump_router_to_string(buf, 2048, r2, pk1)>0);
00233   test_eq(dirserv_add_descriptor(buf,&m,""), ROUTER_ADDED_NOTIFY_GENERATOR);
00234   get_options()->Nickname = tor_strdup("DirServer");
00235   test_assert(!dirserv_dump_directory_to_string(&cp,pk3, 0));
00236   crypto_pk_get_digest(pk3, d);
00237   test_assert(!router_parse_directory(cp));
00238   test_eq(2, smartlist_len(dir1->routers));
00239   tor_free(cp);
00240   }
00241 #endif
00242   dirserv_free_fingerprint_list();
00243 
00244  done:
00245   if (r1)
00246     routerinfo_free(r1);
00247   if (r2)
00248     routerinfo_free(r2);
00249 
00250   tor_free(pk1_str);
00251   tor_free(pk2_str);
00252   tor_free(pk3_str);
00253   if (pk1) crypto_pk_free(pk1);
00254   if (pk2) crypto_pk_free(pk2);
00255   if (pk3) crypto_pk_free(pk3);
00256   if (rp1) routerinfo_free(rp1);
00257   tor_free(dir1); /* XXXX And more !*/
00258   tor_free(dir2); /* And more !*/
00259 }
00260 
00261 static void
00262 test_dir_versions(void)
00263 {
00264   tor_version_t ver1;
00265 
00266   /* Try out version parsing functionality */
00267   test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
00268   test_eq(0, ver1.major);
00269   test_eq(3, ver1.minor);
00270   test_eq(4, ver1.micro);
00271   test_eq(VER_PRE, ver1.status);
00272   test_eq(2, ver1.patchlevel);
00273   test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
00274   test_eq(0, ver1.major);
00275   test_eq(3, ver1.minor);
00276   test_eq(4, ver1.micro);
00277   test_eq(VER_RC, ver1.status);
00278   test_eq(1, ver1.patchlevel);
00279   test_eq(0, tor_version_parse("1.3.4", &ver1));
00280   test_eq(1, ver1.major);
00281   test_eq(3, ver1.minor);
00282   test_eq(4, ver1.micro);
00283   test_eq(VER_RELEASE, ver1.status);
00284   test_eq(0, ver1.patchlevel);
00285   test_eq(0, tor_version_parse("1.3.4.999", &ver1));
00286   test_eq(1, ver1.major);
00287   test_eq(3, ver1.minor);
00288   test_eq(4, ver1.micro);
00289   test_eq(VER_RELEASE, ver1.status);
00290   test_eq(999, ver1.patchlevel);
00291   test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
00292   test_eq(0, ver1.major);
00293   test_eq(1, ver1.minor);
00294   test_eq(2, ver1.micro);
00295   test_eq(4, ver1.patchlevel);
00296   test_eq(VER_RELEASE, ver1.status);
00297   test_streq("alpha", ver1.status_tag);
00298   test_eq(0, tor_version_parse("0.1.2.4", &ver1));
00299   test_eq(0, ver1.major);
00300   test_eq(1, ver1.minor);
00301   test_eq(2, ver1.micro);
00302   test_eq(4, ver1.patchlevel);
00303   test_eq(VER_RELEASE, ver1.status);
00304   test_streq("", ver1.status_tag);
00305 
00306 #define tt_versionstatus_op(vs1, op, vs2)                               \
00307   tt_assert_test_type(vs1,vs2,#vs1" "#op" "#vs2,version_status_t,       \
00308                       (val1_ op val2_),"%d",TT_EXIT_TEST_FUNCTION)
00309 #define test_v_i_o(val, ver, lst)                                       \
00310   tt_versionstatus_op(val, ==, tor_version_is_obsolete(ver, lst))
00311 
00312   /* make sure tor_version_is_obsolete() works */
00313   test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
00314   test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
00315   test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
00316   test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
00317   test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
00318   test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
00319   test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
00320   test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
00321   test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
00322   test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
00323   test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
00324   /* Not on list, but newer than any in same series. */
00325   test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
00326              "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
00327   /* Series newer than any on list. */
00328   test_v_i_o(VS_NEW, "0.1.2.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
00329   /* Series older than any on list. */
00330   test_v_i_o(VS_OLD, "0.0.1.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
00331   /* Not on list, not newer than any on same series. */
00332   test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
00333              "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
00334   /* On list, not newer than any on same series. */
00335   test_v_i_o(VS_UNRECOMMENDED,
00336              "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
00337   test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
00338   test_eq(1, tor_version_as_new_as(
00339           "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
00340           "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
00341           "0.0.8rc2"));
00342   test_eq(0, tor_version_as_new_as(
00343           "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
00344           "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
00345 
00346   /* Now try svn revisions. */
00347   test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
00348                                    "Tor 0.2.1.0-dev (r99)"));
00349   test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100) on Banana Jr",
00350                                    "Tor 0.2.1.0-dev (r99) on Hal 9000"));
00351   test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
00352                                    "Tor 0.2.1.0-dev on Colossus"));
00353   test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
00354                                    "Tor 0.2.1.0-dev (r100)"));
00355   test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
00356                                    "Tor 0.2.1.0-dev (r100) on AM"));
00357   test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev",
00358                                    "Tor 0.2.1.0-dev (r99)"));
00359   test_eq(1, tor_version_as_new_as("Tor 0.2.1.1",
00360                                    "Tor 0.2.1.0-dev (r99)"));
00361 
00362   /* Now try git revisions */
00363   test_eq(0, tor_version_parse("0.5.6.7 (git-ff00ff)", &ver1));
00364   test_eq(0, ver1.major);
00365   test_eq(5, ver1.minor);
00366   test_eq(6, ver1.micro);
00367   test_eq(7, ver1.patchlevel);
00368   test_eq(3, ver1.git_tag_len);
00369   test_memeq(ver1.git_tag, "\xff\x00\xff", 3);
00370   test_eq(-1, tor_version_parse("0.5.6.7 (git-ff00xx)", &ver1));
00371   test_eq(-1, tor_version_parse("0.5.6.7 (git-ff00fff)", &ver1));
00372   test_eq(0, tor_version_parse("0.5.6.7 (git ff00fff)", &ver1));
00373  done:
00374   ;
00375 }
00376 
00378 static void
00379 test_dir_fp_pairs(void)
00380 {
00381   smartlist_t *sl = smartlist_new();
00382   fp_pair_t *pair;
00383 
00384   dir_split_resource_into_fingerprint_pairs(
00385        /* Two pairs, out of order, with one duplicate. */
00386        "73656372657420646174612E0000000000FFFFFF-"
00387        "557365204145532d32353620696e73746561642e+"
00388        "73656372657420646174612E0000000000FFFFFF-"
00389        "557365204145532d32353620696e73746561642e+"
00390        "48657861646563696d616c2069736e277420736f-"
00391        "676f6f6420666f7220686964696e6720796f7572.z", sl);
00392 
00393   test_eq(smartlist_len(sl), 2);
00394   pair = smartlist_get(sl, 0);
00395   test_memeq(pair->first,  "Hexadecimal isn't so", DIGEST_LEN);
00396   test_memeq(pair->second, "good for hiding your", DIGEST_LEN);
00397   pair = smartlist_get(sl, 1);
00398   test_memeq(pair->first,  "secret data.\0\0\0\0\0\xff\xff\xff", DIGEST_LEN);
00399   test_memeq(pair->second, "Use AES-256 instead.", DIGEST_LEN);
00400 
00401  done:
00402   SMARTLIST_FOREACH(sl, fp_pair_t *, pair, tor_free(pair));
00403   smartlist_free(sl);
00404 }
00405 
00406 static void
00407 test_dir_split_fps(void *testdata)
00408 {
00409   smartlist_t *sl = smartlist_new();
00410   char *mem_op_hex_tmp = NULL;
00411   (void)testdata;
00412 
00413   /* Some example hex fingerprints and their base64 equivalents */
00414 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
00415 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
00416 #define HEX3 "b33ff00db33ff00db33ff00db33ff00db33ff00d"
00417 #define HEX256_1 \
00418     "f3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf3f3f3f3fbbbf3f3f3f3fbbbbf"
00419 #define HEX256_2 \
00420     "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccCCc"
00421 #define HEX256_3 \
00422     "0123456789ABCdef0123456789ABCdef0123456789ABCdef0123456789ABCdef"
00423 #define B64_1 "/g2v+JEnOJvGdVhpEjEjRVEZPu4"
00424 #define B64_2 "3q2+75mZmZERERmZmRERERHwC6Q"
00425 #define B64_3 "sz/wDbM/8A2zP/ANsz/wDbM/8A0"
00426 #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
00427 #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
00428 #define B64_256_3 "ASNFZ4mrze8BI0VniavN7wEjRWeJq83vASNFZ4mrze8"
00429 
00430   /* no flags set */
00431   dir_split_resource_into_fingerprints("A+C+B", sl, NULL, 0);
00432   tt_int_op(smartlist_len(sl), ==, 3);
00433   tt_str_op(smartlist_get(sl, 0), ==, "A");
00434   tt_str_op(smartlist_get(sl, 1), ==, "C");
00435   tt_str_op(smartlist_get(sl, 2), ==, "B");
00436   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00437   smartlist_clear(sl);
00438 
00439   /* uniq strings. */
00440   dir_split_resource_into_fingerprints("A+C+B+A+B+B", sl, NULL, DSR_SORT_UNIQ);
00441   tt_int_op(smartlist_len(sl), ==, 3);
00442   tt_str_op(smartlist_get(sl, 0), ==, "A");
00443   tt_str_op(smartlist_get(sl, 1), ==, "B");
00444   tt_str_op(smartlist_get(sl, 2), ==, "C");
00445   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00446   smartlist_clear(sl);
00447 
00448   /* Decode hex. */
00449   dir_split_resource_into_fingerprints(HEX1"+"HEX2, sl, NULL, DSR_HEX);
00450   tt_int_op(smartlist_len(sl), ==, 2);
00451   test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
00452   test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
00453   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00454   smartlist_clear(sl);
00455 
00456   /* decode hex and drop weirdness. */
00457   dir_split_resource_into_fingerprints(HEX1"+bogus+"HEX2"+"HEX256_1,
00458                                        sl, NULL, DSR_HEX);
00459   tt_int_op(smartlist_len(sl), ==, 2);
00460   test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
00461   test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
00462   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00463   smartlist_clear(sl);
00464 
00465   /* Decode long hex */
00466   dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX2"+"HEX256_3,
00467                                        sl, NULL, DSR_HEX|DSR_DIGEST256);
00468   tt_int_op(smartlist_len(sl), ==, 3);
00469   test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
00470   test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
00471   test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_3);
00472   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00473   smartlist_clear(sl);
00474 
00475   /* Decode hex and sort. */
00476   dir_split_resource_into_fingerprints(HEX1"+"HEX2"+"HEX3"+"HEX2,
00477                                        sl, NULL, DSR_HEX|DSR_SORT_UNIQ);
00478   tt_int_op(smartlist_len(sl), ==, 3);
00479   test_mem_op_hex(smartlist_get(sl, 0), ==, HEX3);
00480   test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
00481   test_mem_op_hex(smartlist_get(sl, 2), ==, HEX1);
00482   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00483   smartlist_clear(sl);
00484 
00485   /* Decode long hex and sort */
00486   dir_split_resource_into_fingerprints(HEX256_1"+"HEX256_2"+"HEX256_3
00487                                        "+"HEX256_1,
00488                                        sl, NULL,
00489                                        DSR_HEX|DSR_DIGEST256|DSR_SORT_UNIQ);
00490   tt_int_op(smartlist_len(sl), ==, 3);
00491   test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_3);
00492   test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
00493   test_mem_op_hex(smartlist_get(sl, 2), ==, HEX256_1);
00494   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00495   smartlist_clear(sl);
00496 
00497   /* Decode base64 */
00498   dir_split_resource_into_fingerprints(B64_1"-"B64_2, sl, NULL, DSR_BASE64);
00499   tt_int_op(smartlist_len(sl), ==, 2);
00500   test_mem_op_hex(smartlist_get(sl, 0), ==, HEX1);
00501   test_mem_op_hex(smartlist_get(sl, 1), ==, HEX2);
00502   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00503   smartlist_clear(sl);
00504 
00505   /* Decode long base64 */
00506   dir_split_resource_into_fingerprints(B64_256_1"-"B64_256_2,
00507                                        sl, NULL, DSR_BASE64|DSR_DIGEST256);
00508   tt_int_op(smartlist_len(sl), ==, 2);
00509   test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
00510   test_mem_op_hex(smartlist_get(sl, 1), ==, HEX256_2);
00511   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00512   smartlist_clear(sl);
00513 
00514   dir_split_resource_into_fingerprints(B64_256_1,
00515                                        sl, NULL, DSR_BASE64|DSR_DIGEST256);
00516   tt_int_op(smartlist_len(sl), ==, 1);
00517   test_mem_op_hex(smartlist_get(sl, 0), ==, HEX256_1);
00518   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00519   smartlist_clear(sl);
00520 
00521  done:
00522   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
00523   smartlist_free(sl);
00524   tor_free(mem_op_hex_tmp);
00525 }
00526 
00527 static void
00528 test_dir_measured_bw(void)
00529 {
00530   measured_bw_line_t mbwl;
00531   int i;
00532   const char *lines_pass[] = {
00533     "node_id=$557365204145532d32353620696e73746561642e bw=1024\n",
00534     "node_id=$557365204145532d32353620696e73746561642e\t  bw=1024 \n",
00535     " node_id=$557365204145532d32353620696e73746561642e  bw=1024\n",
00536     "\tnoise\tnode_id=$557365204145532d32353620696e73746561642e  "
00537                 "bw=1024 junk=007\n",
00538     "misc=junk node_id=$557365204145532d32353620696e73746561642e  "
00539                 "bw=1024 junk=007\n",
00540     "end"
00541   };
00542   const char *lines_fail[] = {
00543     /* Test possible python stupidity on input */
00544     "node_id=None bw=1024\n",
00545     "node_id=$None bw=1024\n",
00546     "node_id=$557365204145532d32353620696e73746561642e bw=None\n",
00547     "node_id=$557365204145532d32353620696e73746561642e bw=1024.0\n",
00548     "node_id=$557365204145532d32353620696e73746561642e bw=.1024\n",
00549     "node_id=$557365204145532d32353620696e73746561642e bw=1.024\n",
00550     "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=0\n",
00551     "node_id=$557365204145532d32353620696e73746561642e bw=1024 bw=None\n",
00552     "node_id=$557365204145532d32353620696e73746561642e bw=-1024\n",
00553     /* Test incomplete writes due to race conditions, partial copies, etc */
00554     "node_i",
00555     "node_i\n",
00556     "node_id=",
00557     "node_id=\n",
00558     "node_id=$557365204145532d32353620696e73746561642e bw=",
00559     "node_id=$557365204145532d32353620696e73746561642e bw=1024",
00560     "node_id=$557365204145532d32353620696e73746561642e bw=\n",
00561     "node_id=$557365204145532d32353620696e7374",
00562     "node_id=$557365204145532d32353620696e7374\n",
00563     "",
00564     "\n",
00565     " \n ",
00566     " \n\n",
00567     /* Test assorted noise */
00568     " node_id= ",
00569     "node_id==$557365204145532d32353620696e73746561642e bw==1024\n",
00570     "node_id=$55736520414552d32353620696e73746561642e bw=1024\n",
00571     "node_id=557365204145532d32353620696e73746561642e bw=1024\n",
00572     "node_id= $557365204145532d32353620696e73746561642e bw=0.23\n",
00573     "end"
00574   };
00575 
00576   for (i = 0; strcmp(lines_fail[i], "end"); i++) {
00577     //fprintf(stderr, "Testing: %s\n", lines_fail[i]);
00578     test_assert(measured_bw_line_parse(&mbwl, lines_fail[i]) == -1);
00579   }
00580 
00581   for (i = 0; strcmp(lines_pass[i], "end"); i++) {
00582     //fprintf(stderr, "Testing: %s %d\n", lines_pass[i], TOR_ISSPACE('\n'));
00583     test_assert(measured_bw_line_parse(&mbwl, lines_pass[i]) == 0);
00584     test_assert(mbwl.bw == 1024);
00585     test_assert(strcmp(mbwl.node_hex,
00586                 "557365204145532d32353620696e73746561642e") == 0);
00587   }
00588 
00589  done:
00590   return;
00591 }
00592 
00593 static void
00594 test_dir_param_voting(void)
00595 {
00596   networkstatus_t vote1, vote2, vote3, vote4;
00597   smartlist_t *votes = smartlist_new();
00598   char *res = NULL;
00599 
00600   /* dirvote_compute_params only looks at the net_params field of the votes,
00601      so that's all we need to set.
00602    */
00603   memset(&vote1, 0, sizeof(vote1));
00604   memset(&vote2, 0, sizeof(vote2));
00605   memset(&vote3, 0, sizeof(vote3));
00606   memset(&vote4, 0, sizeof(vote4));
00607   vote1.net_params = smartlist_new();
00608   vote2.net_params = smartlist_new();
00609   vote3.net_params = smartlist_new();
00610   vote4.net_params = smartlist_new();
00611   smartlist_split_string(vote1.net_params,
00612                          "ab=90 abcd=20 cw=50 x-yz=-99", NULL, 0, 0);
00613   smartlist_split_string(vote2.net_params,
00614                          "ab=27 cw=5 x-yz=88", NULL, 0, 0);
00615   smartlist_split_string(vote3.net_params,
00616                          "abcd=20 c=60 cw=500 x-yz=-9 zzzzz=101", NULL, 0, 0);
00617   smartlist_split_string(vote4.net_params,
00618                          "ab=900 abcd=200 c=1 cw=51 x-yz=100", NULL, 0, 0);
00619   test_eq(100, networkstatus_get_param(&vote4, "x-yz", 50, 0, 300));
00620   test_eq(222, networkstatus_get_param(&vote4, "foobar", 222, 0, 300));
00621   test_eq(80, networkstatus_get_param(&vote4, "ab", 12, 0, 80));
00622   test_eq(-8, networkstatus_get_param(&vote4, "ab", -12, -100, -8));
00623   test_eq(0, networkstatus_get_param(&vote4, "foobar", 0, -100, 8));
00624 
00625   smartlist_add(votes, &vote1);
00626 
00627   /* Do the first tests without adding all the other votes, for
00628    * networks without many dirauths. */
00629 
00630   res = dirvote_compute_params(votes, 11, 6);
00631   test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-99");
00632   tor_free(res);
00633 
00634   res = dirvote_compute_params(votes, 12, 2);
00635   test_streq(res, "");
00636   tor_free(res);
00637 
00638   res = dirvote_compute_params(votes, 12, 1);
00639   test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-99");
00640   tor_free(res);
00641 
00642   smartlist_add(votes, &vote2);
00643 
00644   res = dirvote_compute_params(votes, 11, 2);
00645   test_streq(res, "ab=27 abcd=20 cw=5 x-yz=-99");
00646   tor_free(res);
00647 
00648   res = dirvote_compute_params(votes, 12, 2);
00649   test_streq(res, "ab=27 cw=5 x-yz=-99");
00650   tor_free(res);
00651 
00652   res = dirvote_compute_params(votes, 12, 3);
00653   test_streq(res, "ab=27 cw=5 x-yz=-99");
00654   tor_free(res);
00655 
00656   res = dirvote_compute_params(votes, 12, 6);
00657   test_streq(res, "");
00658   tor_free(res);
00659 
00660   smartlist_add(votes, &vote3);
00661 
00662   res = dirvote_compute_params(votes, 11, 3);
00663   test_streq(res, "ab=27 abcd=20 c=60 cw=50 x-yz=-9 zzzzz=101");
00664   tor_free(res);
00665 
00666   res = dirvote_compute_params(votes, 12, 3);
00667   test_streq(res, "ab=27 abcd=20 cw=50 x-yz=-9");
00668   tor_free(res);
00669 
00670   res = dirvote_compute_params(votes, 12, 5);
00671   test_streq(res, "cw=50 x-yz=-9");
00672   tor_free(res);
00673 
00674   res = dirvote_compute_params(votes, 12, 9);
00675   test_streq(res, "cw=50 x-yz=-9");
00676   tor_free(res);
00677 
00678   smartlist_add(votes, &vote4);
00679 
00680   res = dirvote_compute_params(votes, 11, 4);
00681   test_streq(res, "ab=90 abcd=20 c=1 cw=50 x-yz=-9 zzzzz=101");
00682   tor_free(res);
00683 
00684   res = dirvote_compute_params(votes, 12, 4);
00685   test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
00686   tor_free(res);
00687 
00688   res = dirvote_compute_params(votes, 12, 5);
00689   test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
00690   tor_free(res);
00691 
00692   /* Test that the special-cased "at least three dirauths voted for
00693    * this param" logic works as expected. */
00694   res = dirvote_compute_params(votes, 12, 6);
00695   test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
00696   tor_free(res);
00697 
00698   res = dirvote_compute_params(votes, 12, 10);
00699   test_streq(res, "ab=90 abcd=20 cw=50 x-yz=-9");
00700   tor_free(res);
00701 
00702  done:
00703   tor_free(res);
00704   SMARTLIST_FOREACH(vote1.net_params, char *, cp, tor_free(cp));
00705   SMARTLIST_FOREACH(vote2.net_params, char *, cp, tor_free(cp));
00706   SMARTLIST_FOREACH(vote3.net_params, char *, cp, tor_free(cp));
00707   SMARTLIST_FOREACH(vote4.net_params, char *, cp, tor_free(cp));
00708   smartlist_free(vote1.net_params);
00709   smartlist_free(vote2.net_params);
00710   smartlist_free(vote3.net_params);
00711   smartlist_free(vote4.net_params);
00712   smartlist_free(votes);
00713 
00714   return;
00715 }
00716 
00717 extern const char AUTHORITY_CERT_1[];
00718 extern const char AUTHORITY_SIGNKEY_1[];
00719 extern const char AUTHORITY_CERT_2[];
00720 extern const char AUTHORITY_SIGNKEY_2[];
00721 extern const char AUTHORITY_CERT_3[];
00722 extern const char AUTHORITY_SIGNKEY_3[];
00723 
00727 static void
00728 test_same_voter(networkstatus_voter_info_t *v1,
00729                 networkstatus_voter_info_t *v2)
00730 {
00731   test_streq(v1->nickname, v2->nickname);
00732   test_memeq(v1->identity_digest, v2->identity_digest, DIGEST_LEN);
00733   test_streq(v1->address, v2->address);
00734   test_eq(v1->addr, v2->addr);
00735   test_eq(v1->dir_port, v2->dir_port);
00736   test_eq(v1->or_port, v2->or_port);
00737   test_streq(v1->contact, v2->contact);
00738   test_memeq(v1->vote_digest, v2->vote_digest, DIGEST_LEN);
00739  done:
00740   ;
00741 }
00742 
00745 static routerinfo_t *
00746 generate_ri_from_rs(const vote_routerstatus_t *vrs)
00747 {
00748   routerinfo_t *r;
00749   const routerstatus_t *rs = &vrs->status;
00750   static time_t published = 0;
00751 
00752   r = tor_malloc_zero(sizeof(routerinfo_t));
00753   memcpy(r->cache_info.identity_digest, rs->identity_digest, DIGEST_LEN);
00754   memcpy(r->cache_info.signed_descriptor_digest, rs->descriptor_digest,
00755          DIGEST_LEN);
00756   r->cache_info.do_not_cache = 1;
00757   r->cache_info.routerlist_index = -1;
00758   r->cache_info.signed_descriptor_body =
00759     tor_strdup("123456789012345678901234567890123");
00760   r->cache_info.signed_descriptor_len =
00761     strlen(r->cache_info.signed_descriptor_body);
00762   r->exit_policy = smartlist_new();
00763   r->cache_info.published_on = ++published + time(NULL);
00764   return r;
00765 }
00766 
00769 static char *
00770 get_detached_sigs(networkstatus_t *ns, networkstatus_t *ns2)
00771 {
00772   char *r;
00773   smartlist_t *sl;
00774   tor_assert(ns && ns->flavor == FLAV_NS);
00775   sl = smartlist_new();
00776   smartlist_add(sl,ns);
00777   if (ns2)
00778     smartlist_add(sl,ns2);
00779   r = networkstatus_get_detached_signatures(sl);
00780   smartlist_free(sl);
00781   return r;
00782 }
00783 
00786 static void
00787 test_dir_v3_networkstatus(void)
00788 {
00789   authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
00790   crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
00791   crypto_pk_t *sign_skey_leg1=NULL;
00792   const char *msg=NULL;
00793 
00794   time_t now = time(NULL);
00795   networkstatus_voter_info_t *voter;
00796   document_signature_t *sig;
00797   networkstatus_t *vote=NULL, *v1=NULL, *v2=NULL, *v3=NULL, *con=NULL,
00798     *con_md=NULL;
00799   vote_routerstatus_t *vrs;
00800   routerstatus_t *rs;
00801   char *v1_text=NULL, *v2_text=NULL, *v3_text=NULL, *consensus_text=NULL, *cp;
00802   smartlist_t *votes = smartlist_new();
00803 
00804   /* For generating the two other consensuses. */
00805   char *detached_text1=NULL, *detached_text2=NULL;
00806   char *consensus_text2=NULL, *consensus_text3=NULL;
00807   char *consensus_text_md2=NULL, *consensus_text_md3=NULL;
00808   char *consensus_text_md=NULL;
00809   networkstatus_t *con2=NULL, *con_md2=NULL, *con3=NULL, *con_md3=NULL;
00810   ns_detached_signatures_t *dsig1=NULL, *dsig2=NULL;
00811 
00812   /* Parse certificates and keys. */
00813   cert1 = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
00814   test_assert(cert1);
00815   test_assert(cert1->is_cross_certified);
00816   cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
00817   test_assert(cert2);
00818   cert3 = authority_cert_parse_from_string(AUTHORITY_CERT_3, NULL);
00819   test_assert(cert3);
00820   sign_skey_1 = crypto_pk_new();
00821   sign_skey_2 = crypto_pk_new();
00822   sign_skey_3 = crypto_pk_new();
00823   sign_skey_leg1 = pk_generate(4);
00824 
00825   test_assert(!crypto_pk_read_private_key_from_string(sign_skey_1,
00826                                                    AUTHORITY_SIGNKEY_1, -1));
00827   test_assert(!crypto_pk_read_private_key_from_string(sign_skey_2,
00828                                                    AUTHORITY_SIGNKEY_2, -1));
00829   test_assert(!crypto_pk_read_private_key_from_string(sign_skey_3,
00830                                                    AUTHORITY_SIGNKEY_3, -1));
00831 
00832   test_assert(!crypto_pk_cmp_keys(sign_skey_1, cert1->signing_key));
00833   test_assert(!crypto_pk_cmp_keys(sign_skey_2, cert2->signing_key));
00834 
00835   /*
00836    * Set up a vote; generate it; try to parse it.
00837    */
00838   vote = tor_malloc_zero(sizeof(networkstatus_t));
00839   vote->type = NS_TYPE_VOTE;
00840   vote->published = now;
00841   vote->valid_after = now+1000;
00842   vote->fresh_until = now+2000;
00843   vote->valid_until = now+3000;
00844   vote->vote_seconds = 100;
00845   vote->dist_seconds = 200;
00846   vote->supported_methods = smartlist_new();
00847   smartlist_split_string(vote->supported_methods, "1 2 3", NULL, 0, -1);
00848   vote->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
00849   vote->server_versions = tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
00850   vote->known_flags = smartlist_new();
00851   smartlist_split_string(vote->known_flags,
00852                      "Authority Exit Fast Guard Running Stable V2Dir Valid",
00853                      0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
00854   vote->voters = smartlist_new();
00855   voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
00856   voter->nickname = tor_strdup("Voter1");
00857   voter->address = tor_strdup("1.2.3.4");
00858   voter->addr = 0x01020304;
00859   voter->dir_port = 80;
00860   voter->or_port = 9000;
00861   voter->contact = tor_strdup("voter@example.com");
00862   crypto_pk_get_digest(cert1->identity_key, voter->identity_digest);
00863   smartlist_add(vote->voters, voter);
00864   vote->cert = authority_cert_dup(cert1);
00865   vote->net_params = smartlist_new();
00866   smartlist_split_string(vote->net_params, "circuitwindow=101 foo=990",
00867                          NULL, 0, 0);
00868   vote->routerstatus_list = smartlist_new();
00869   /* add the first routerstatus. */
00870   vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
00871   rs = &vrs->status;
00872   vrs->version = tor_strdup("0.1.2.14");
00873   rs->published_on = now-1500;
00874   strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
00875   memset(rs->identity_digest, 3, DIGEST_LEN);
00876   memset(rs->descriptor_digest, 78, DIGEST_LEN);
00877   rs->addr = 0x99008801;
00878   rs->or_port = 443;
00879   rs->dir_port = 8000;
00880   /* all flags but running cleared */
00881   rs->is_flagged_running = 1;
00882   smartlist_add(vote->routerstatus_list, vrs);
00883   test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs), &msg,0,0)>=0);
00884 
00885   /* add the second routerstatus. */
00886   vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
00887   rs = &vrs->status;
00888   vrs->version = tor_strdup("0.2.0.5");
00889   rs->published_on = now-1000;
00890   strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
00891   memset(rs->identity_digest, 5, DIGEST_LEN);
00892   memset(rs->descriptor_digest, 77, DIGEST_LEN);
00893   rs->addr = 0x99009901;
00894   rs->or_port = 443;
00895   rs->dir_port = 0;
00896   rs->is_exit = rs->is_stable = rs->is_fast = rs->is_flagged_running =
00897     rs->is_valid = rs->is_v2_dir = rs->is_possible_guard = 1;
00898   smartlist_add(vote->routerstatus_list, vrs);
00899   test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs), &msg,0,0)>=0);
00900 
00901   /* add the third routerstatus. */
00902   vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
00903   rs = &vrs->status;
00904   vrs->version = tor_strdup("0.1.0.3");
00905   rs->published_on = now-1000;
00906   strlcpy(rs->nickname, "router3", sizeof(rs->nickname));
00907   memset(rs->identity_digest, 33, DIGEST_LEN);
00908   memset(rs->descriptor_digest, 79, DIGEST_LEN);
00909   rs->addr = 0xAA009901;
00910   rs->or_port = 400;
00911   rs->dir_port = 9999;
00912   rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
00913     rs->is_flagged_running = rs->is_valid = rs->is_v2_dir =
00914     rs->is_possible_guard = 1;
00915   smartlist_add(vote->routerstatus_list, vrs);
00916   test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs), &msg,0,0)>=0);
00917 
00918   /* add a fourth routerstatus that is not running. */
00919   vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
00920   rs = &vrs->status;
00921   vrs->version = tor_strdup("0.1.6.3");
00922   rs->published_on = now-1000;
00923   strlcpy(rs->nickname, "router4", sizeof(rs->nickname));
00924   memset(rs->identity_digest, 34, DIGEST_LEN);
00925   memset(rs->descriptor_digest, 47, DIGEST_LEN);
00926   rs->addr = 0xC0000203;
00927   rs->or_port = 500;
00928   rs->dir_port = 1999;
00929   /* Running flag (and others) cleared */
00930   smartlist_add(vote->routerstatus_list, vrs);
00931   test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs), &msg,0,0)>=0);
00932 
00933   /* dump the vote and try to parse it. */
00934   v1_text = format_networkstatus_vote(sign_skey_1, vote);
00935   test_assert(v1_text);
00936   v1 = networkstatus_parse_vote_from_string(v1_text, NULL, NS_TYPE_VOTE);
00937   test_assert(v1);
00938 
00939   /* Make sure the parsed thing was right. */
00940   test_eq(v1->type, NS_TYPE_VOTE);
00941   test_eq(v1->published, vote->published);
00942   test_eq(v1->valid_after, vote->valid_after);
00943   test_eq(v1->fresh_until, vote->fresh_until);
00944   test_eq(v1->valid_until, vote->valid_until);
00945   test_eq(v1->vote_seconds, vote->vote_seconds);
00946   test_eq(v1->dist_seconds, vote->dist_seconds);
00947   test_streq(v1->client_versions, vote->client_versions);
00948   test_streq(v1->server_versions, vote->server_versions);
00949   test_assert(v1->voters && smartlist_len(v1->voters));
00950   voter = smartlist_get(v1->voters, 0);
00951   test_streq(voter->nickname, "Voter1");
00952   test_streq(voter->address, "1.2.3.4");
00953   test_eq(voter->addr, 0x01020304);
00954   test_eq(voter->dir_port, 80);
00955   test_eq(voter->or_port, 9000);
00956   test_streq(voter->contact, "voter@example.com");
00957   test_assert(v1->cert);
00958   test_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
00959   cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
00960   test_streq(cp, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
00961   tor_free(cp);
00962   test_eq(smartlist_len(v1->routerstatus_list), 4);
00963   /* Check the first routerstatus. */
00964   vrs = smartlist_get(v1->routerstatus_list, 0);
00965   rs = &vrs->status;
00966   test_streq(vrs->version, "0.1.2.14");
00967   test_eq(rs->published_on, now-1500);
00968   test_streq(rs->nickname, "router2");
00969   test_memeq(rs->identity_digest,
00970              "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
00971              DIGEST_LEN);
00972   test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
00973   test_eq(rs->addr, 0x99008801);
00974   test_eq(rs->or_port, 443);
00975   test_eq(rs->dir_port, 8000);
00976   test_eq(vrs->flags, U64_LITERAL(16)); // no flags except "running"
00977   /* Check the second routerstatus. */
00978   vrs = smartlist_get(v1->routerstatus_list, 1);
00979   rs = &vrs->status;
00980   test_streq(vrs->version, "0.2.0.5");
00981   test_eq(rs->published_on, now-1000);
00982   test_streq(rs->nickname, "router1");
00983   test_memeq(rs->identity_digest,
00984              "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
00985              DIGEST_LEN);
00986   test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
00987   test_eq(rs->addr, 0x99009901);
00988   test_eq(rs->or_port, 443);
00989   test_eq(rs->dir_port, 0);
00990   test_eq(vrs->flags, U64_LITERAL(254)); // all flags except "authority."
00991 
00992   {
00993     measured_bw_line_t mbw;
00994     memset(mbw.node_id, 33, sizeof(mbw.node_id));
00995     mbw.bw = 1024;
00996     test_assert(measured_bw_line_apply(&mbw,
00997                 v1->routerstatus_list) == 1);
00998     vrs = smartlist_get(v1->routerstatus_list, 2);
00999     test_assert(vrs->status.has_measured_bw &&
01000                 vrs->status.measured_bw == 1024);
01001   }
01002 
01003   /* Generate second vote. It disagrees on some of the times,
01004    * and doesn't list versions, and knows some crazy flags */
01005   vote->published = now+1;
01006   vote->fresh_until = now+3005;
01007   vote->dist_seconds = 300;
01008   authority_cert_free(vote->cert);
01009   vote->cert = authority_cert_dup(cert2);
01010   vote->net_params = smartlist_new();
01011   smartlist_split_string(vote->net_params, "bar=2000000000 circuitwindow=20",
01012                          NULL, 0, 0);
01013   tor_free(vote->client_versions);
01014   tor_free(vote->server_versions);
01015   voter = smartlist_get(vote->voters, 0);
01016   tor_free(voter->nickname);
01017   tor_free(voter->address);
01018   voter->nickname = tor_strdup("Voter2");
01019   voter->address = tor_strdup("2.3.4.5");
01020   voter->addr = 0x02030405;
01021   crypto_pk_get_digest(cert2->identity_key, voter->identity_digest);
01022   smartlist_add(vote->known_flags, tor_strdup("MadeOfCheese"));
01023   smartlist_add(vote->known_flags, tor_strdup("MadeOfTin"));
01024   smartlist_sort_strings(vote->known_flags);
01025   vrs = smartlist_get(vote->routerstatus_list, 2);
01026   smartlist_del_keeporder(vote->routerstatus_list, 2);
01027   tor_free(vrs->version);
01028   tor_free(vrs);
01029   vrs = smartlist_get(vote->routerstatus_list, 0);
01030   vrs->status.is_fast = 1;
01031   /* generate and parse. */
01032   v2_text = format_networkstatus_vote(sign_skey_2, vote);
01033   test_assert(v2_text);
01034   v2 = networkstatus_parse_vote_from_string(v2_text, NULL, NS_TYPE_VOTE);
01035   test_assert(v2);
01036   /* Check that flags come out right.*/
01037   cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
01038   test_streq(cp, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
01039              "Running:Stable:V2Dir:Valid");
01040   tor_free(cp);
01041   vrs = smartlist_get(v2->routerstatus_list, 1);
01042   /* 1023 - authority(1) - madeofcheese(16) - madeoftin(32) */
01043   test_eq(vrs->flags, U64_LITERAL(974));
01044 
01045   /* Generate the third vote. */
01046   vote->published = now;
01047   vote->fresh_until = now+2003;
01048   vote->dist_seconds = 250;
01049   authority_cert_free(vote->cert);
01050   vote->cert = authority_cert_dup(cert3);
01051   vote->net_params = smartlist_new();
01052   smartlist_split_string(vote->net_params, "circuitwindow=80 foo=660",
01053                          NULL, 0, 0);
01054   smartlist_add(vote->supported_methods, tor_strdup("4"));
01055   vote->client_versions = tor_strdup("0.1.2.14,0.1.2.17");
01056   vote->server_versions = tor_strdup("0.1.2.10,0.1.2.15,0.1.2.16");
01057   voter = smartlist_get(vote->voters, 0);
01058   tor_free(voter->nickname);
01059   tor_free(voter->address);
01060   voter->nickname = tor_strdup("Voter3");
01061   voter->address = tor_strdup("3.4.5.6");
01062   voter->addr = 0x03040506;
01063   crypto_pk_get_digest(cert3->identity_key, voter->identity_digest);
01064   /* This one has a legacy id. */
01065   memset(voter->legacy_id_digest, (int)'A', DIGEST_LEN);
01066   vrs = smartlist_get(vote->routerstatus_list, 0);
01067   smartlist_del_keeporder(vote->routerstatus_list, 0);
01068   tor_free(vrs->version);
01069   tor_free(vrs);
01070   vrs = smartlist_get(vote->routerstatus_list, 0);
01071   memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
01072   test_assert(router_add_to_routerlist(generate_ri_from_rs(vrs), &msg,0,0)>=0);
01073 
01074   v3_text = format_networkstatus_vote(sign_skey_3, vote);
01075   test_assert(v3_text);
01076 
01077   v3 = networkstatus_parse_vote_from_string(v3_text, NULL, NS_TYPE_VOTE);
01078   test_assert(v3);
01079 
01080   /* Compute a consensus as voter 3. */
01081   smartlist_add(votes, v3);
01082   smartlist_add(votes, v1);
01083   smartlist_add(votes, v2);
01084   consensus_text = networkstatus_compute_consensus(votes, 3,
01085                                                    cert3->identity_key,
01086                                                    sign_skey_3,
01087                                                    "AAAAAAAAAAAAAAAAAAAA",
01088                                                    sign_skey_leg1,
01089                                                    FLAV_NS);
01090   test_assert(consensus_text);
01091   con = networkstatus_parse_vote_from_string(consensus_text, NULL,
01092                                              NS_TYPE_CONSENSUS);
01093   test_assert(con);
01094   //log_notice(LD_GENERAL, "<<%s>>\n<<%s>>\n<<%s>>\n",
01095   //           v1_text, v2_text, v3_text);
01096   consensus_text_md = networkstatus_compute_consensus(votes, 3,
01097                                                    cert3->identity_key,
01098                                                    sign_skey_3,
01099                                                    "AAAAAAAAAAAAAAAAAAAA",
01100                                                    sign_skey_leg1,
01101                                                    FLAV_MICRODESC);
01102   test_assert(consensus_text_md);
01103   con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
01104                                                 NS_TYPE_CONSENSUS);
01105   test_assert(con_md);
01106   test_eq(con_md->flavor, FLAV_MICRODESC);
01107 
01108   /* Check consensus contents. */
01109   test_assert(con->type == NS_TYPE_CONSENSUS);
01110   test_eq(con->published, 0); /* this field only appears in votes. */
01111   test_eq(con->valid_after, now+1000);
01112   test_eq(con->fresh_until, now+2003); /* median */
01113   test_eq(con->valid_until, now+3000);
01114   test_eq(con->vote_seconds, 100);
01115   test_eq(con->dist_seconds, 250); /* median */
01116   test_streq(con->client_versions, "0.1.2.14");
01117   test_streq(con->server_versions, "0.1.2.15,0.1.2.16");
01118   cp = smartlist_join_strings(v2->known_flags, ":", 0, NULL);
01119   test_streq(cp, "Authority:Exit:Fast:Guard:MadeOfCheese:MadeOfTin:"
01120              "Running:Stable:V2Dir:Valid");
01121   tor_free(cp);
01122   cp = smartlist_join_strings(con->net_params, ":", 0, NULL);
01123   test_streq(cp, "circuitwindow=80:foo=660");
01124   tor_free(cp);
01125 
01126   test_eq(4, smartlist_len(con->voters)); /*3 voters, 1 legacy key.*/
01127   /* The voter id digests should be in this order. */
01128   test_assert(memcmp(cert2->cache_info.identity_digest,
01129                      cert1->cache_info.identity_digest,DIGEST_LEN)<0);
01130   test_assert(memcmp(cert1->cache_info.identity_digest,
01131                      cert3->cache_info.identity_digest,DIGEST_LEN)<0);
01132   test_same_voter(smartlist_get(con->voters, 1),
01133                   smartlist_get(v2->voters, 0));
01134   test_same_voter(smartlist_get(con->voters, 2),
01135                   smartlist_get(v1->voters, 0));
01136   test_same_voter(smartlist_get(con->voters, 3),
01137                   smartlist_get(v3->voters, 0));
01138 
01139   test_assert(!con->cert);
01140   test_eq(2, smartlist_len(con->routerstatus_list));
01141   /* There should be two listed routers: one with identity 3, one with
01142    * identity 5. */
01143   /* This one showed up in 2 digests. */
01144   rs = smartlist_get(con->routerstatus_list, 0);
01145   test_memeq(rs->identity_digest,
01146              "\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3\x3",
01147              DIGEST_LEN);
01148   test_memeq(rs->descriptor_digest, "NNNNNNNNNNNNNNNNNNNN", DIGEST_LEN);
01149   test_assert(!rs->is_authority);
01150   test_assert(!rs->is_exit);
01151   test_assert(!rs->is_fast);
01152   test_assert(!rs->is_possible_guard);
01153   test_assert(!rs->is_stable);
01154   /* (If it wasn't running it wouldn't be here) */
01155   test_assert(rs->is_flagged_running);
01156   test_assert(!rs->is_v2_dir);
01157   test_assert(!rs->is_valid);
01158   test_assert(!rs->is_named);
01159   /* XXXX check version */
01160 
01161   rs = smartlist_get(con->routerstatus_list, 1);
01162   /* This one showed up in 3 digests. Twice with ID 'M', once with 'Z'.  */
01163   test_memeq(rs->identity_digest,
01164              "\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5\x5",
01165              DIGEST_LEN);
01166   test_streq(rs->nickname, "router1");
01167   test_memeq(rs->descriptor_digest, "MMMMMMMMMMMMMMMMMMMM", DIGEST_LEN);
01168   test_eq(rs->published_on, now-1000);
01169   test_eq(rs->addr, 0x99009901);
01170   test_eq(rs->or_port, 443);
01171   test_eq(rs->dir_port, 0);
01172   test_assert(!rs->is_authority);
01173   test_assert(rs->is_exit);
01174   test_assert(rs->is_fast);
01175   test_assert(rs->is_possible_guard);
01176   test_assert(rs->is_stable);
01177   test_assert(rs->is_flagged_running);
01178   test_assert(rs->is_v2_dir);
01179   test_assert(rs->is_valid);
01180   test_assert(!rs->is_named);
01181   /* XXXX check version */
01182 
01183   /* Check signatures.  the first voter is a pseudo-entry with a legacy key.
01184    * The second one hasn't signed.  The fourth one has signed: validate it. */
01185   voter = smartlist_get(con->voters, 1);
01186   test_eq(smartlist_len(voter->sigs), 0);
01187 
01188   voter = smartlist_get(con->voters, 3);
01189   test_eq(smartlist_len(voter->sigs), 1);
01190   sig = smartlist_get(voter->sigs, 0);
01191   test_assert(sig->signature);
01192   test_assert(!sig->good_signature);
01193   test_assert(!sig->bad_signature);
01194 
01195   test_assert(!networkstatus_check_document_signature(con, sig, cert3));
01196   test_assert(sig->signature);
01197   test_assert(sig->good_signature);
01198   test_assert(!sig->bad_signature);
01199 
01200   {
01201     const char *msg=NULL;
01202     /* Compute the other two signed consensuses. */
01203     smartlist_shuffle(votes);
01204     consensus_text2 = networkstatus_compute_consensus(votes, 3,
01205                                                       cert2->identity_key,
01206                                                       sign_skey_2, NULL,NULL,
01207                                                       FLAV_NS);
01208     consensus_text_md2 = networkstatus_compute_consensus(votes, 3,
01209                                                       cert2->identity_key,
01210                                                       sign_skey_2, NULL,NULL,
01211                                                       FLAV_MICRODESC);
01212     smartlist_shuffle(votes);
01213     consensus_text3 = networkstatus_compute_consensus(votes, 3,
01214                                                       cert1->identity_key,
01215                                                       sign_skey_1, NULL,NULL,
01216                                                       FLAV_NS);
01217     consensus_text_md3 = networkstatus_compute_consensus(votes, 3,
01218                                                       cert1->identity_key,
01219                                                       sign_skey_1, NULL,NULL,
01220                                                       FLAV_MICRODESC);
01221     test_assert(consensus_text2);
01222     test_assert(consensus_text3);
01223     test_assert(consensus_text_md2);
01224     test_assert(consensus_text_md3);
01225     con2 = networkstatus_parse_vote_from_string(consensus_text2, NULL,
01226                                                 NS_TYPE_CONSENSUS);
01227     con3 = networkstatus_parse_vote_from_string(consensus_text3, NULL,
01228                                                 NS_TYPE_CONSENSUS);
01229     con_md2 = networkstatus_parse_vote_from_string(consensus_text_md2, NULL,
01230                                                 NS_TYPE_CONSENSUS);
01231     con_md3 = networkstatus_parse_vote_from_string(consensus_text_md3, NULL,
01232                                                 NS_TYPE_CONSENSUS);
01233     test_assert(con2);
01234     test_assert(con3);
01235     test_assert(con_md2);
01236     test_assert(con_md3);
01237 
01238     /* All three should have the same digest. */
01239     test_memeq(&con->digests, &con2->digests, sizeof(digests_t));
01240     test_memeq(&con->digests, &con3->digests, sizeof(digests_t));
01241 
01242     test_memeq(&con_md->digests, &con_md2->digests, sizeof(digests_t));
01243     test_memeq(&con_md->digests, &con_md3->digests, sizeof(digests_t));
01244 
01245     /* Extract a detached signature from con3. */
01246     detached_text1 = get_detached_sigs(con3, con_md3);
01247     tt_assert(detached_text1);
01248     /* Try to parse it. */
01249     dsig1 = networkstatus_parse_detached_signatures(detached_text1, NULL);
01250     tt_assert(dsig1);
01251 
01252     /* Are parsed values as expected? */
01253     test_eq(dsig1->valid_after, con3->valid_after);
01254     test_eq(dsig1->fresh_until, con3->fresh_until);
01255     test_eq(dsig1->valid_until, con3->valid_until);
01256     {
01257       digests_t *dsig_digests = strmap_get(dsig1->digests, "ns");
01258       test_assert(dsig_digests);
01259       test_memeq(dsig_digests->d[DIGEST_SHA1], con3->digests.d[DIGEST_SHA1],
01260                  DIGEST_LEN);
01261       dsig_digests = strmap_get(dsig1->digests, "microdesc");
01262       test_assert(dsig_digests);
01263       test_memeq(dsig_digests->d[DIGEST_SHA256],
01264                  con_md3->digests.d[DIGEST_SHA256],
01265                  DIGEST256_LEN);
01266     }
01267     {
01268       smartlist_t *dsig_signatures = strmap_get(dsig1->signatures, "ns");
01269       test_assert(dsig_signatures);
01270       test_eq(1, smartlist_len(dsig_signatures));
01271       sig = smartlist_get(dsig_signatures, 0);
01272       test_memeq(sig->identity_digest, cert1->cache_info.identity_digest,
01273                  DIGEST_LEN);
01274       test_eq(sig->alg, DIGEST_SHA1);
01275 
01276       dsig_signatures = strmap_get(dsig1->signatures, "microdesc");
01277       test_assert(dsig_signatures);
01278       test_eq(1, smartlist_len(dsig_signatures));
01279       sig = smartlist_get(dsig_signatures, 0);
01280       test_memeq(sig->identity_digest, cert1->cache_info.identity_digest,
01281                  DIGEST_LEN);
01282       test_eq(sig->alg, DIGEST_SHA256);
01283     }
01284 
01285     /* Try adding it to con2. */
01286     detached_text2 = get_detached_sigs(con2,con_md2);
01287     test_eq(1, networkstatus_add_detached_signatures(con2, dsig1, "test",
01288                                                      LOG_INFO, &msg));
01289     tor_free(detached_text2);
01290     test_eq(1, networkstatus_add_detached_signatures(con_md2, dsig1, "test",
01291                                                      LOG_INFO, &msg));
01292     tor_free(detached_text2);
01293     detached_text2 = get_detached_sigs(con2,con_md2);
01294     //printf("\n<%s>\n", detached_text2);
01295     dsig2 = networkstatus_parse_detached_signatures(detached_text2, NULL);
01296     test_assert(dsig2);
01297     /*
01298     printf("\n");
01299     SMARTLIST_FOREACH(dsig2->signatures, networkstatus_voter_info_t *, vi, {
01300         char hd[64];
01301         base16_encode(hd, sizeof(hd), vi->identity_digest, DIGEST_LEN);
01302         printf("%s\n", hd);
01303       });
01304     */
01305     test_eq(2,
01306             smartlist_len((smartlist_t*)strmap_get(dsig2->signatures, "ns")));
01307     test_eq(2,
01308             smartlist_len((smartlist_t*)strmap_get(dsig2->signatures,
01309                                                    "microdesc")));
01310 
01311     /* Try adding to con2 twice; verify that nothing changes. */
01312     test_eq(0, networkstatus_add_detached_signatures(con2, dsig1, "test",
01313                                                      LOG_INFO, &msg));
01314 
01315     /* Add to con. */
01316     test_eq(2, networkstatus_add_detached_signatures(con, dsig2, "test",
01317                                                      LOG_INFO, &msg));
01318     /* Check signatures */
01319     voter = smartlist_get(con->voters, 1);
01320     sig = smartlist_get(voter->sigs, 0);
01321     test_assert(sig);
01322     test_assert(!networkstatus_check_document_signature(con, sig, cert2));
01323     voter = smartlist_get(con->voters, 2);
01324     sig = smartlist_get(voter->sigs, 0);
01325     test_assert(sig);
01326     test_assert(!networkstatus_check_document_signature(con, sig, cert1));
01327   }
01328 
01329  done:
01330   smartlist_free(votes);
01331   tor_free(v1_text);
01332   tor_free(v2_text);
01333   tor_free(v3_text);
01334   tor_free(consensus_text);
01335   tor_free(consensus_text_md);
01336 
01337   if (vote)
01338     networkstatus_vote_free(vote);
01339   if (v1)
01340     networkstatus_vote_free(v1);
01341   if (v2)
01342     networkstatus_vote_free(v2);
01343   if (v3)
01344     networkstatus_vote_free(v3);
01345   if (con)
01346     networkstatus_vote_free(con);
01347   if (con_md)
01348     networkstatus_vote_free(con_md);
01349   if (sign_skey_1)
01350     crypto_pk_free(sign_skey_1);
01351   if (sign_skey_2)
01352     crypto_pk_free(sign_skey_2);
01353   if (sign_skey_3)
01354     crypto_pk_free(sign_skey_3);
01355   if (sign_skey_leg1)
01356     crypto_pk_free(sign_skey_leg1);
01357   if (cert1)
01358     authority_cert_free(cert1);
01359   if (cert2)
01360     authority_cert_free(cert2);
01361   if (cert3)
01362     authority_cert_free(cert3);
01363 
01364   tor_free(consensus_text2);
01365   tor_free(consensus_text3);
01366   tor_free(consensus_text_md2);
01367   tor_free(consensus_text_md3);
01368   tor_free(detached_text1);
01369   tor_free(detached_text2);
01370   if (con2)
01371     networkstatus_vote_free(con2);
01372   if (con3)
01373     networkstatus_vote_free(con3);
01374   if (con_md2)
01375     networkstatus_vote_free(con_md2);
01376   if (con_md3)
01377     networkstatus_vote_free(con_md3);
01378   if (dsig1)
01379     ns_detached_signatures_free(dsig1);
01380   if (dsig2)
01381     ns_detached_signatures_free(dsig2);
01382 }
01383 
01384 #define DIR_LEGACY(name)                                                   \
01385   { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_dir_ ## name }
01386 
01387 #define DIR(name)                               \
01388   { #name, test_dir_##name, 0, NULL, NULL }
01389 
01390 struct testcase_t dir_tests[] = {
01391   DIR_LEGACY(nicknames),
01392   DIR_LEGACY(formats),
01393   DIR_LEGACY(versions),
01394   DIR_LEGACY(fp_pairs),
01395   DIR(split_fps),
01396   DIR_LEGACY(measured_bw),
01397   DIR_LEGACY(param_voting),
01398   DIR_LEGACY(v3_networkstatus),
01399   END_OF_TESTCASES
01400 };
01401