Back to index

tor  0.2.3.19-rc
test.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 /* Ordinarily defined in tor_main.c; this bit is just here to provide one
00007  * since we're not linking to tor_main.c */
00008 const char tor_git_revision[] = "";
00009 
00015 #include "orconfig.h"
00016 
00017 #include <stdio.h>
00018 #ifdef HAVE_FCNTL_H
00019 #include <fcntl.h>
00020 #endif
00021 
00022 #ifdef _WIN32
00023 /* For mkdir() */
00024 #include <direct.h>
00025 #else
00026 #include <dirent.h>
00027 #endif
00028 
00029 /* These macros pull in declarations for some functions and structures that
00030  * are typically file-private. */
00031 #define BUFFERS_PRIVATE
00032 #define CONFIG_PRIVATE
00033 #define GEOIP_PRIVATE
00034 #define ROUTER_PRIVATE
00035 #define CIRCUIT_PRIVATE
00036 
00037 /*
00038  * Linux doesn't provide lround in math.h by default, but mac os does...
00039  * It's best just to leave math.h out of the picture entirely.
00040  */
00041 //#include <math.h>
00042 long int lround(double x);
00043 double fabs(double x);
00044 
00045 #include "or.h"
00046 #include "buffers.h"
00047 #include "circuitbuild.h"
00048 #include "config.h"
00049 #include "connection_edge.h"
00050 #include "geoip.h"
00051 #include "rendcommon.h"
00052 #include "test.h"
00053 #include "torgzip.h"
00054 #include "mempool.h"
00055 #include "memarea.h"
00056 #include "onion.h"
00057 #include "policies.h"
00058 #include "rephist.h"
00059 #include "routerparse.h"
00060 
00061 #ifdef USE_DMALLOC
00062 #include <dmalloc.h>
00063 #include <openssl/crypto.h>
00064 #include "main.h"
00065 #endif
00066 
00069 int have_failed = 0;
00070 
00073 static char temp_dir[256];
00074 #ifdef _WIN32
00075 #define pid_t int
00076 #endif
00077 static pid_t temp_dir_setup_in_pid = 0;
00078 
00082 static void
00083 setup_directory(void)
00084 {
00085   static int is_setup = 0;
00086   int r;
00087   if (is_setup) return;
00088 
00089 #ifdef _WIN32
00090   {
00091     char buf[MAX_PATH];
00092     const char *tmp = buf;
00093     /* If this fails, we're probably screwed anyway */
00094     if (!GetTempPathA(sizeof(buf),buf))
00095       tmp = "c:\\windows\\temp";
00096     tor_snprintf(temp_dir, sizeof(temp_dir),
00097                  "%s\\tor_test_%d", tmp, (int)getpid());
00098     r = mkdir(temp_dir);
00099   }
00100 #else
00101   tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
00102   r = mkdir(temp_dir, 0700);
00103 #endif
00104   if (r) {
00105     fprintf(stderr, "Can't create directory %s:", temp_dir);
00106     perror("");
00107     exit(1);
00108   }
00109   is_setup = 1;
00110   temp_dir_setup_in_pid = getpid();
00111 }
00112 
00114 const char *
00115 get_fname(const char *name)
00116 {
00117   static char buf[1024];
00118   setup_directory();
00119   if (!name)
00120     return temp_dir;
00121   tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
00122   return buf;
00123 }
00124 
00125 /* Remove a directory and all of its subdirectories */
00126 static void
00127 rm_rf(const char *dir)
00128 {
00129   struct stat st;
00130   smartlist_t *elements;
00131 
00132   elements = tor_listdir(dir);
00133   if (elements) {
00134     SMARTLIST_FOREACH(elements, const char *, cp,
00135        {
00136          char *tmp = NULL;
00137          tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
00138          if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
00139            rm_rf(tmp);
00140          } else {
00141            if (unlink(tmp)) {
00142              fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
00143            }
00144          }
00145          tor_free(tmp);
00146        });
00147     SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
00148     smartlist_free(elements);
00149   }
00150   if (rmdir(dir))
00151     fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
00152 }
00153 
00156 static void
00157 remove_directory(void)
00158 {
00159   if (getpid() != temp_dir_setup_in_pid) {
00160     /* Only clean out the tempdir when the main process is exiting. */
00161     return;
00162   }
00163 
00164   rm_rf(temp_dir);
00165 }
00166 
00168 #undef CACHE_GENERATED_KEYS
00169 
00170 static crypto_pk_t *pregen_keys[5] = {NULL, NULL, NULL, NULL, NULL};
00171 #define N_PREGEN_KEYS ((int)(sizeof(pregen_keys)/sizeof(pregen_keys[0])))
00172 
00177 crypto_pk_t *
00178 pk_generate(int idx)
00179 {
00180 #ifdef CACHE_GENERATED_KEYS
00181   tor_assert(idx < N_PREGEN_KEYS);
00182   if (! pregen_keys[idx]) {
00183     pregen_keys[idx] = crypto_pk_new();
00184     tor_assert(!crypto_pk_generate_key(pregen_keys[idx]));
00185   }
00186   return crypto_pk_dup_key(pregen_keys[idx]);
00187 #else
00188   crypto_pk_t *result;
00189   (void) idx;
00190   result = crypto_pk_new();
00191   tor_assert(!crypto_pk_generate_key(result));
00192   return result;
00193 #endif
00194 }
00195 
00197 static void
00198 free_pregenerated_keys(void)
00199 {
00200   unsigned idx;
00201   for (idx = 0; idx < N_PREGEN_KEYS; ++idx) {
00202     if (pregen_keys[idx]) {
00203       crypto_pk_free(pregen_keys[idx]);
00204       pregen_keys[idx] = NULL;
00205     }
00206   }
00207 }
00208 
00209 typedef struct socks_test_data_t {
00210   socks_request_t *req;
00211   buf_t *buf;
00212 } socks_test_data_t;
00213 
00214 static void *
00215 socks_test_setup(const struct testcase_t *testcase)
00216 {
00217   socks_test_data_t *data = tor_malloc(sizeof(socks_test_data_t));
00218   (void)testcase;
00219   data->buf = buf_new_with_capacity(256);
00220   data->req = socks_request_new();
00221   config_register_addressmaps(get_options());
00222   return data;
00223 }
00224 static int
00225 socks_test_cleanup(const struct testcase_t *testcase, void *ptr)
00226 {
00227   socks_test_data_t *data = ptr;
00228   (void)testcase;
00229   buf_free(data->buf);
00230   socks_request_free(data->req);
00231   tor_free(data);
00232   return 1;
00233 }
00234 
00235 const struct testcase_setup_t socks_setup = {
00236   socks_test_setup, socks_test_cleanup
00237 };
00238 
00239 #define SOCKS_TEST_INIT()                       \
00240   socks_test_data_t *testdata = ptr;            \
00241   buf_t *buf = testdata->buf;                   \
00242   socks_request_t *socks = testdata->req;
00243 #define ADD_DATA(buf, s)                                        \
00244   write_to_buf(s, sizeof(s)-1, buf)
00245 
00246 static void
00247 socks_request_clear(socks_request_t *socks)
00248 {
00249   tor_free(socks->username);
00250   tor_free(socks->password);
00251   memset(socks, 0, sizeof(socks_request_t));
00252 }
00253 
00255 static void
00256 test_socks_4_unsupported_commands(void *ptr)
00257 {
00258   SOCKS_TEST_INIT();
00259 
00260   /* SOCKS 4 Send BIND [02] to IP address 2.2.2.2:4369 */
00261   ADD_DATA(buf, "\x04\x02\x11\x11\x02\x02\x02\x02\x00");
00262   test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00263                                    get_options()->SafeSocks) == -1);
00264   test_eq(4, socks->socks_version);
00265   test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
00266 
00267  done:
00268   ;
00269 }
00270 
00272 static void
00273 test_socks_4_supported_commands(void *ptr)
00274 {
00275   SOCKS_TEST_INIT();
00276 
00277   test_eq(0, buf_datalen(buf));
00278 
00279   /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4370 */
00280   ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x03\x00");
00281   test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00282                                    get_options()->SafeSocks) == 1);
00283   test_eq(4, socks->socks_version);
00284   test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
00285   test_eq(SOCKS_COMMAND_CONNECT, socks->command);
00286   test_streq("2.2.2.3", socks->address);
00287   test_eq(4370, socks->port);
00288   test_assert(socks->got_auth == 0);
00289   test_assert(! socks->username);
00290 
00291   test_eq(0, buf_datalen(buf));
00292   socks_request_clear(socks);
00293 
00294   /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4369 with userid*/
00295   ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x04me\x00");
00296   test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00297                                    get_options()->SafeSocks) == 1);
00298   test_eq(4, socks->socks_version);
00299   test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
00300   test_eq(SOCKS_COMMAND_CONNECT, socks->command);
00301   test_streq("2.2.2.4", socks->address);
00302   test_eq(4370, socks->port);
00303   test_assert(socks->got_auth == 1);
00304   test_assert(socks->username);
00305   test_eq(2, socks->usernamelen);
00306   test_memeq("me", socks->username, 2);
00307 
00308   test_eq(0, buf_datalen(buf));
00309   socks_request_clear(socks);
00310 
00311   /* SOCKS 4a Send RESOLVE [F0] request for torproject.org */
00312   ADD_DATA(buf, "\x04\xF0\x01\x01\x00\x00\x00\x02me\x00torproject.org\x00");
00313   test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00314                                    get_options()->SafeSocks) == 1);
00315   test_eq(4, socks->socks_version);
00316   test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
00317   test_streq("torproject.org", socks->address);
00318 
00319   test_eq(0, buf_datalen(buf));
00320 
00321  done:
00322   ;
00323 }
00324 
00326 static void
00327 test_socks_5_unsupported_commands(void *ptr)
00328 {
00329   SOCKS_TEST_INIT();
00330 
00331   /* SOCKS 5 Send unsupported BIND [02] command */
00332   ADD_DATA(buf, "\x05\x02\x00\x01");
00333 
00334   test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00335                                get_options()->SafeSocks), 0);
00336   test_eq(0, buf_datalen(buf));
00337   test_eq(5, socks->socks_version);
00338   test_eq(2, socks->replylen);
00339   test_eq(5, socks->reply[0]);
00340   test_eq(0, socks->reply[1]);
00341   ADD_DATA(buf, "\x05\x02\x00\x01\x02\x02\x02\x01\x01\x01");
00342   test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00343                                get_options()->SafeSocks), -1);
00344   /* XXX: shouldn't tor reply 'command not supported' [07]? */
00345 
00346   buf_clear(buf);
00347   socks_request_clear(socks);
00348 
00349   /* SOCKS 5 Send unsupported UDP_ASSOCIATE [03] command */
00350   ADD_DATA(buf, "\x05\x03\x00\x01\x02");
00351   test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00352                                get_options()->SafeSocks), 0);
00353   test_eq(5, socks->socks_version);
00354   test_eq(2, socks->replylen);
00355   test_eq(5, socks->reply[0]);
00356   test_eq(0, socks->reply[1]);
00357   ADD_DATA(buf, "\x05\x03\x00\x01\x02\x02\x02\x01\x01\x01");
00358   test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00359                                get_options()->SafeSocks), -1);
00360   /* XXX: shouldn't tor reply 'command not supported' [07]? */
00361 
00362  done:
00363   ;
00364 }
00365 
00367 static void
00368 test_socks_5_supported_commands(void *ptr)
00369 {
00370   SOCKS_TEST_INIT();
00371 
00372   /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
00373   ADD_DATA(buf, "\x05\x01\x00");
00374   test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00375                                    get_options()->SafeSocks), 0);
00376   test_eq(5, socks->socks_version);
00377   test_eq(2, socks->replylen);
00378   test_eq(5, socks->reply[0]);
00379   test_eq(0, socks->reply[1]);
00380 
00381   ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
00382   test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00383                                    get_options()->SafeSocks), 1);
00384   test_streq("2.2.2.2", socks->address);
00385   test_eq(4369, socks->port);
00386 
00387   test_eq(0, buf_datalen(buf));
00388   socks_request_clear(socks);
00389 
00390   /* SOCKS 5 Send CONNECT [01] to FQDN torproject.org:4369 */
00391   ADD_DATA(buf, "\x05\x01\x00");
00392   ADD_DATA(buf, "\x05\x01\x00\x03\x0Etorproject.org\x11\x11");
00393   test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00394                                    get_options()->SafeSocks), 1);
00395 
00396   test_eq(5, socks->socks_version);
00397   test_eq(2, socks->replylen);
00398   test_eq(5, socks->reply[0]);
00399   test_eq(0, socks->reply[1]);
00400   test_streq("torproject.org", socks->address);
00401   test_eq(4369, socks->port);
00402 
00403   test_eq(0, buf_datalen(buf));
00404   socks_request_clear(socks);
00405 
00406   /* SOCKS 5 Send RESOLVE [F0] request for torproject.org:4369 */
00407   ADD_DATA(buf, "\x05\x01\x00");
00408   ADD_DATA(buf, "\x05\xF0\x00\x03\x0Etorproject.org\x01\x02");
00409   test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00410                                    get_options()->SafeSocks) == 1);
00411   test_eq(5, socks->socks_version);
00412   test_eq(2, socks->replylen);
00413   test_eq(5, socks->reply[0]);
00414   test_eq(0, socks->reply[1]);
00415   test_streq("torproject.org", socks->address);
00416 
00417   test_eq(0, buf_datalen(buf));
00418   socks_request_clear(socks);
00419 
00420   /* SOCKS 5 Send RESOLVE_PTR [F1] for IP address 2.2.2.5 */
00421   ADD_DATA(buf, "\x05\x01\x00");
00422   ADD_DATA(buf, "\x05\xF1\x00\x01\x02\x02\x02\x05\x01\x03");
00423   test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
00424                                    get_options()->SafeSocks) == 1);
00425   test_eq(5, socks->socks_version);
00426   test_eq(2, socks->replylen);
00427   test_eq(5, socks->reply[0]);
00428   test_eq(0, socks->reply[1]);
00429   test_streq("2.2.2.5", socks->address);
00430 
00431   test_eq(0, buf_datalen(buf));
00432 
00433  done:
00434   ;
00435 }
00436 
00438 static void
00439 test_socks_5_no_authenticate(void *ptr)
00440 {
00441   SOCKS_TEST_INIT();
00442 
00443   /*SOCKS 5 No Authentication */
00444   ADD_DATA(buf,"\x05\x01\x00");
00445   test_assert(!fetch_from_buf_socks(buf, socks,
00446                                     get_options()->TestSocks,
00447                                     get_options()->SafeSocks));
00448   test_eq(2, socks->replylen);
00449   test_eq(5, socks->reply[0]);
00450   test_eq(SOCKS_NO_AUTH, socks->reply[1]);
00451 
00452   test_eq(0, buf_datalen(buf));
00453 
00454   /*SOCKS 5 Send username/password anyway - pretend to be broken */
00455   ADD_DATA(buf,"\x01\x02\x01\x01\x02\x01\x01");
00456   test_assert(!fetch_from_buf_socks(buf, socks,
00457                                     get_options()->TestSocks,
00458                                     get_options()->SafeSocks));
00459   test_eq(5, socks->socks_version);
00460   test_eq(2, socks->replylen);
00461   test_eq(5, socks->reply[0]);
00462   test_eq(0, socks->reply[1]);
00463 
00464   test_eq(2, socks->usernamelen);
00465   test_eq(2, socks->passwordlen);
00466 
00467   test_memeq("\x01\x01", socks->username, 2);
00468   test_memeq("\x01\x01", socks->password, 2);
00469 
00470  done:
00471   ;
00472 }
00473 
00475 static void
00476 test_socks_5_authenticate(void *ptr)
00477 {
00478   SOCKS_TEST_INIT();
00479 
00480   /* SOCKS 5 Negotiate username/password authentication */
00481   ADD_DATA(buf, "\x05\x01\x02");
00482 
00483   test_assert(!fetch_from_buf_socks(buf, socks,
00484                                    get_options()->TestSocks,
00485                                    get_options()->SafeSocks));
00486   test_eq(2, socks->replylen);
00487   test_eq(5, socks->reply[0]);
00488   test_eq(SOCKS_USER_PASS, socks->reply[1]);
00489   test_eq(5, socks->socks_version);
00490 
00491   test_eq(0, buf_datalen(buf));
00492 
00493   /* SOCKS 5 Send username/password */
00494   ADD_DATA(buf, "\x01\x02me\x08mypasswd");
00495   test_assert(!fetch_from_buf_socks(buf, socks,
00496                                    get_options()->TestSocks,
00497                                    get_options()->SafeSocks));
00498   test_eq(5, socks->socks_version);
00499   test_eq(2, socks->replylen);
00500   test_eq(5, socks->reply[0]);
00501   test_eq(0, socks->reply[1]);
00502 
00503   test_eq(2, socks->usernamelen);
00504   test_eq(8, socks->passwordlen);
00505 
00506   test_memeq("me", socks->username, 2);
00507   test_memeq("mypasswd", socks->password, 8);
00508 
00509  done:
00510   ;
00511 }
00512 
00514 static void
00515 test_socks_5_authenticate_with_data(void *ptr)
00516 {
00517   SOCKS_TEST_INIT();
00518 
00519   /* SOCKS 5 Negotiate username/password authentication */
00520   ADD_DATA(buf, "\x05\x01\x02");
00521 
00522   test_assert(!fetch_from_buf_socks(buf, socks,
00523                                    get_options()->TestSocks,
00524                                    get_options()->SafeSocks));
00525   test_eq(2, socks->replylen);
00526   test_eq(5, socks->reply[0]);
00527   test_eq(SOCKS_USER_PASS, socks->reply[1]);
00528   test_eq(5, socks->socks_version);
00529 
00530   test_eq(0, buf_datalen(buf));
00531 
00532   /* SOCKS 5 Send username/password */
00533   /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
00534   ADD_DATA(buf, "\x01\x02me\x03you\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
00535   test_assert(fetch_from_buf_socks(buf, socks,
00536                                    get_options()->TestSocks,
00537                                    get_options()->SafeSocks) == 1);
00538   test_eq(5, socks->socks_version);
00539   test_eq(2, socks->replylen);
00540   test_eq(5, socks->reply[0]);
00541   test_eq(0, socks->reply[1]);
00542 
00543   test_streq("2.2.2.2", socks->address);
00544   test_eq(4369, socks->port);
00545 
00546   test_eq(2, socks->usernamelen);
00547   test_eq(3, socks->passwordlen);
00548   test_memeq("me", socks->username, 2);
00549   test_memeq("you", socks->password, 3);
00550 
00551  done:
00552   ;
00553 }
00554 
00556 static void
00557 test_socks_5_auth_before_negotiation(void *ptr)
00558 {
00559   SOCKS_TEST_INIT();
00560 
00561   /* SOCKS 5 Send username/password */
00562   ADD_DATA(buf, "\x01\x02me\x02me");
00563   test_assert(fetch_from_buf_socks(buf, socks,
00564                                    get_options()->TestSocks,
00565                                    get_options()->SafeSocks) == -1);
00566   test_eq(0, socks->socks_version);
00567   test_eq(0, socks->replylen);
00568   test_eq(0, socks->reply[0]);
00569   test_eq(0, socks->reply[1]);
00570 
00571  done:
00572   ;
00573 }
00574 
00575 static void
00576 test_buffer_copy(void *arg)
00577 {
00578   generic_buffer_t *buf=NULL, *buf2=NULL;
00579   const char *s;
00580   size_t len;
00581   char b[256];
00582   int i;
00583   (void)arg;
00584 
00585   buf = generic_buffer_new();
00586   tt_assert(buf);
00587 
00588   /* Copy an empty buffer. */
00589   tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
00590   tt_assert(buf2);
00591   tt_int_op(0, ==, generic_buffer_len(buf2));
00592 
00593   /* Now try with a short buffer. */
00594   s = "And now comes an act of enormous enormance!";
00595   len = strlen(s);
00596   generic_buffer_add(buf, s, len);
00597   tt_int_op(len, ==, generic_buffer_len(buf));
00598   /* Add junk to buf2 so we can test replacing.*/
00599   generic_buffer_add(buf2, "BLARG", 5);
00600   tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
00601   tt_int_op(len, ==, generic_buffer_len(buf2));
00602   generic_buffer_get(buf2, b, len);
00603   test_mem_op(b, ==, s, len);
00604   /* Now free buf2 and retry so we can test allocating */
00605   generic_buffer_free(buf2);
00606   buf2 = NULL;
00607   tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
00608   tt_int_op(len, ==, generic_buffer_len(buf2));
00609   generic_buffer_get(buf2, b, len);
00610   test_mem_op(b, ==, s, len);
00611   /* Clear buf for next test */
00612   generic_buffer_get(buf, b, len);
00613   tt_int_op(generic_buffer_len(buf),==,0);
00614 
00615   /* Okay, now let's try a bigger buffer. */
00616   s = "Quis autem vel eum iure reprehenderit qui in ea voluptate velit "
00617     "esse quam nihil molestiae consequatur, vel illum qui dolorem eum "
00618     "fugiat quo voluptas nulla pariatur?";
00619   len = strlen(s);
00620   for (i = 0; i < 256; ++i) {
00621     b[0]=i;
00622     generic_buffer_add(buf, b, 1);
00623     generic_buffer_add(buf, s, len);
00624   }
00625   tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
00626   tt_int_op(generic_buffer_len(buf2), ==, generic_buffer_len(buf));
00627   for (i = 0; i < 256; ++i) {
00628     generic_buffer_get(buf2, b, len+1);
00629     tt_int_op((unsigned char)b[0],==,i);
00630     test_mem_op(b+1, ==, s, len);
00631   }
00632 
00633  done:
00634   if (buf)
00635     generic_buffer_free(buf);
00636   if (buf2)
00637     generic_buffer_free(buf2);
00638 }
00639 
00641 static void
00642 test_buffers(void)
00643 {
00644   char str[256];
00645   char str2[256];
00646 
00647   buf_t *buf = NULL, *buf2 = NULL;
00648   const char *cp;
00649 
00650   int j;
00651   size_t r;
00652 
00653   /****
00654    * buf_new
00655    ****/
00656   if (!(buf = buf_new()))
00657     test_fail();
00658 
00659   //test_eq(buf_capacity(buf), 4096);
00660   test_eq(buf_datalen(buf), 0);
00661 
00662   /****
00663    * General pointer frobbing
00664    */
00665   for (j=0;j<256;++j) {
00666     str[j] = (char)j;
00667   }
00668   write_to_buf(str, 256, buf);
00669   write_to_buf(str, 256, buf);
00670   test_eq(buf_datalen(buf), 512);
00671   fetch_from_buf(str2, 200, buf);
00672   test_memeq(str, str2, 200);
00673   test_eq(buf_datalen(buf), 312);
00674   memset(str2, 0, sizeof(str2));
00675 
00676   fetch_from_buf(str2, 256, buf);
00677   test_memeq(str+200, str2, 56);
00678   test_memeq(str, str2+56, 200);
00679   test_eq(buf_datalen(buf), 56);
00680   memset(str2, 0, sizeof(str2));
00681   /* Okay, now we should be 512 bytes into the 4096-byte buffer.  If we add
00682    * another 3584 bytes, we hit the end. */
00683   for (j=0;j<15;++j) {
00684     write_to_buf(str, 256, buf);
00685   }
00686   assert_buf_ok(buf);
00687   test_eq(buf_datalen(buf), 3896);
00688   fetch_from_buf(str2, 56, buf);
00689   test_eq(buf_datalen(buf), 3840);
00690   test_memeq(str+200, str2, 56);
00691   for (j=0;j<15;++j) {
00692     memset(str2, 0, sizeof(str2));
00693     fetch_from_buf(str2, 256, buf);
00694     test_memeq(str, str2, 256);
00695   }
00696   test_eq(buf_datalen(buf), 0);
00697   buf_free(buf);
00698   buf = NULL;
00699 
00700   /* Okay, now make sure growing can work. */
00701   buf = buf_new_with_capacity(16);
00702   //test_eq(buf_capacity(buf), 16);
00703   write_to_buf(str+1, 255, buf);
00704   //test_eq(buf_capacity(buf), 256);
00705   fetch_from_buf(str2, 254, buf);
00706   test_memeq(str+1, str2, 254);
00707   //test_eq(buf_capacity(buf), 256);
00708   assert_buf_ok(buf);
00709   write_to_buf(str, 32, buf);
00710   //test_eq(buf_capacity(buf), 256);
00711   assert_buf_ok(buf);
00712   write_to_buf(str, 256, buf);
00713   assert_buf_ok(buf);
00714   //test_eq(buf_capacity(buf), 512);
00715   test_eq(buf_datalen(buf), 33+256);
00716   fetch_from_buf(str2, 33, buf);
00717   test_eq(*str2, str[255]);
00718 
00719   test_memeq(str2+1, str, 32);
00720   //test_eq(buf_capacity(buf), 512);
00721   test_eq(buf_datalen(buf), 256);
00722   fetch_from_buf(str2, 256, buf);
00723   test_memeq(str, str2, 256);
00724 
00725   /* now try shrinking: case 1. */
00726   buf_free(buf);
00727   buf = buf_new_with_capacity(33668);
00728   for (j=0;j<67;++j) {
00729     write_to_buf(str,255, buf);
00730   }
00731   //test_eq(buf_capacity(buf), 33668);
00732   test_eq(buf_datalen(buf), 17085);
00733   for (j=0; j < 40; ++j) {
00734     fetch_from_buf(str2, 255,buf);
00735     test_memeq(str2, str, 255);
00736   }
00737 
00738   /* now try shrinking: case 2. */
00739   buf_free(buf);
00740   buf = buf_new_with_capacity(33668);
00741   for (j=0;j<67;++j) {
00742     write_to_buf(str,255, buf);
00743   }
00744   for (j=0; j < 20; ++j) {
00745     fetch_from_buf(str2, 255,buf);
00746     test_memeq(str2, str, 255);
00747   }
00748   for (j=0;j<80;++j) {
00749     write_to_buf(str,255, buf);
00750   }
00751   //test_eq(buf_capacity(buf),33668);
00752   for (j=0; j < 120; ++j) {
00753     fetch_from_buf(str2, 255,buf);
00754     test_memeq(str2, str, 255);
00755   }
00756 
00757   /* Move from buf to buf. */
00758   buf_free(buf);
00759   buf = buf_new_with_capacity(4096);
00760   buf2 = buf_new_with_capacity(4096);
00761   for (j=0;j<100;++j)
00762     write_to_buf(str, 255, buf);
00763   test_eq(buf_datalen(buf), 25500);
00764   for (j=0;j<100;++j) {
00765     r = 10;
00766     move_buf_to_buf(buf2, buf, &r);
00767     test_eq(r, 0);
00768   }
00769   test_eq(buf_datalen(buf), 24500);
00770   test_eq(buf_datalen(buf2), 1000);
00771   for (j=0;j<3;++j) {
00772     fetch_from_buf(str2, 255, buf2);
00773     test_memeq(str2, str, 255);
00774   }
00775   r = 8192; /*big move*/
00776   move_buf_to_buf(buf2, buf, &r);
00777   test_eq(r, 0);
00778   r = 30000; /* incomplete move */
00779   move_buf_to_buf(buf2, buf, &r);
00780   test_eq(r, 13692);
00781   for (j=0;j<97;++j) {
00782     fetch_from_buf(str2, 255, buf2);
00783     test_memeq(str2, str, 255);
00784   }
00785   buf_free(buf);
00786   buf_free(buf2);
00787   buf = buf2 = NULL;
00788 
00789   buf = buf_new_with_capacity(5);
00790   cp = "Testing. This is a moderately long Testing string.";
00791   for (j = 0; cp[j]; j++)
00792     write_to_buf(cp+j, 1, buf);
00793   test_eq(0, buf_find_string_offset(buf, "Testing", 7));
00794   test_eq(1, buf_find_string_offset(buf, "esting", 6));
00795   test_eq(1, buf_find_string_offset(buf, "est", 3));
00796   test_eq(39, buf_find_string_offset(buf, "ing str", 7));
00797   test_eq(35, buf_find_string_offset(buf, "Testing str", 11));
00798   test_eq(32, buf_find_string_offset(buf, "ng ", 3));
00799   test_eq(43, buf_find_string_offset(buf, "string.", 7));
00800   test_eq(-1, buf_find_string_offset(buf, "shrdlu", 6));
00801   test_eq(-1, buf_find_string_offset(buf, "Testing thing", 13));
00802   test_eq(-1, buf_find_string_offset(buf, "ngx", 3));
00803   buf_free(buf);
00804   buf = NULL;
00805 
00806  done:
00807   if (buf)
00808     buf_free(buf);
00809   if (buf2)
00810     buf_free(buf2);
00811 }
00812 
00814 static void
00815 test_onion_handshake(void)
00816 {
00817   /* client-side */
00818   crypto_dh_t *c_dh = NULL;
00819   char c_buf[ONIONSKIN_CHALLENGE_LEN];
00820   char c_keys[40];
00821 
00822   /* server-side */
00823   char s_buf[ONIONSKIN_REPLY_LEN];
00824   char s_keys[40];
00825 
00826   /* shared */
00827   crypto_pk_t *pk = NULL;
00828 
00829   pk = pk_generate(0);
00830 
00831   /* client handshake 1. */
00832   memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
00833   test_assert(! onion_skin_create(pk, &c_dh, c_buf));
00834 
00835   /* server handshake */
00836   memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
00837   memset(s_keys, 0, 40);
00838   test_assert(! onion_skin_server_handshake(c_buf, pk, NULL,
00839                                             s_buf, s_keys, 40));
00840 
00841   /* client handshake 2 */
00842   memset(c_keys, 0, 40);
00843   test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
00844 
00845   if (memcmp(c_keys, s_keys, 40)) {
00846     puts("Aiiiie");
00847     exit(1);
00848   }
00849   test_memeq(c_keys, s_keys, 40);
00850   memset(s_buf, 0, 40);
00851   test_memneq(c_keys, s_buf, 40);
00852 
00853  done:
00854   if (c_dh)
00855     crypto_dh_free(c_dh);
00856   if (pk)
00857     crypto_pk_free(pk);
00858 }
00859 
00860 static void
00861 test_circuit_timeout(void)
00862 {
00863   /* Plan:
00864    *  1. Generate 1000 samples
00865    *  2. Estimate parameters
00866    *  3. If difference, repeat
00867    *  4. Save state
00868    *  5. load state
00869    *  6. Estimate parameters
00870    *  7. compare differences
00871    */
00872   circuit_build_times_t initial;
00873   circuit_build_times_t estimate;
00874   circuit_build_times_t final;
00875   double timeout1, timeout2;
00876   or_state_t state;
00877   int i, runs;
00878   double close_ms;
00879   circuit_build_times_init(&initial);
00880   circuit_build_times_init(&estimate);
00881   circuit_build_times_init(&final);
00882 
00883   memset(&state, 0, sizeof(or_state_t));
00884 
00885   circuitbuild_running_unit_tests();
00886 #define timeout0 (build_time_t)(30*1000.0)
00887   initial.Xm = 3000;
00888   circuit_build_times_initial_alpha(&initial,
00889                                     CBT_DEFAULT_QUANTILE_CUTOFF/100.0,
00890                                     timeout0);
00891   close_ms = MAX(circuit_build_times_calculate_timeout(&initial,
00892                              CBT_DEFAULT_CLOSE_QUANTILE/100.0),
00893                  CBT_DEFAULT_TIMEOUT_INITIAL_VALUE);
00894   do {
00895     for (i=0; i < CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE; i++) {
00896       build_time_t sample = circuit_build_times_generate_sample(&initial,0,1);
00897 
00898       if (sample > close_ms) {
00899         circuit_build_times_add_time(&estimate, CBT_BUILD_ABANDONED);
00900       } else {
00901         circuit_build_times_add_time(&estimate, sample);
00902       }
00903     }
00904     circuit_build_times_update_alpha(&estimate);
00905     timeout1 = circuit_build_times_calculate_timeout(&estimate,
00906                                   CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
00907     circuit_build_times_set_timeout(&estimate);
00908     log_notice(LD_CIRC, "Timeout1 is %f, Xm is %d", timeout1, estimate.Xm);
00909            /* 2% error */
00910   } while (fabs(circuit_build_times_cdf(&initial, timeout0) -
00911                 circuit_build_times_cdf(&initial, timeout1)) > 0.02);
00912 
00913   test_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
00914 
00915   circuit_build_times_update_state(&estimate, &state);
00916   test_assert(circuit_build_times_parse_state(&final, &state) == 0);
00917 
00918   circuit_build_times_update_alpha(&final);
00919   timeout2 = circuit_build_times_calculate_timeout(&final,
00920                                  CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
00921 
00922   circuit_build_times_set_timeout(&final);
00923   log_notice(LD_CIRC, "Timeout2 is %f, Xm is %d", timeout2, final.Xm);
00924 
00925   /* 5% here because some accuracy is lost due to histogram conversion */
00926   test_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
00927                    circuit_build_times_cdf(&initial, timeout2)) < 0.05);
00928 
00929   for (runs = 0; runs < 50; runs++) {
00930     int build_times_idx = 0;
00931     int total_build_times = 0;
00932 
00933     final.close_ms = final.timeout_ms = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
00934     estimate.close_ms = estimate.timeout_ms
00935                       = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
00936 
00937     for (i = 0; i < CBT_DEFAULT_RECENT_CIRCUITS*2; i++) {
00938       circuit_build_times_network_circ_success(&estimate);
00939       circuit_build_times_add_time(&estimate,
00940             circuit_build_times_generate_sample(&estimate, 0,
00941                 CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
00942 
00943       circuit_build_times_network_circ_success(&estimate);
00944       circuit_build_times_add_time(&final,
00945             circuit_build_times_generate_sample(&final, 0,
00946                 CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
00947     }
00948 
00949     test_assert(!circuit_build_times_network_check_changed(&estimate));
00950     test_assert(!circuit_build_times_network_check_changed(&final));
00951 
00952     /* Reset liveness to be non-live */
00953     final.liveness.network_last_live = 0;
00954     estimate.liveness.network_last_live = 0;
00955 
00956     build_times_idx = estimate.build_times_idx;
00957     total_build_times = estimate.total_build_times;
00958 
00959     test_assert(circuit_build_times_network_check_live(&estimate));
00960     test_assert(circuit_build_times_network_check_live(&final));
00961 
00962     circuit_build_times_count_close(&estimate, 0,
00963             (time_t)(approx_time()-estimate.close_ms/1000.0-1));
00964     circuit_build_times_count_close(&final, 0,
00965             (time_t)(approx_time()-final.close_ms/1000.0-1));
00966 
00967     test_assert(!circuit_build_times_network_check_live(&estimate));
00968     test_assert(!circuit_build_times_network_check_live(&final));
00969 
00970     log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
00971              build_times_idx, estimate.build_times_idx,
00972              total_build_times, estimate.total_build_times);
00973 
00974     /* Check rollback index. Should match top of loop. */
00975     test_assert(build_times_idx == estimate.build_times_idx);
00976     // This can fail if estimate.total_build_times == 1000, because
00977     // in that case, rewind actually causes us to lose timeouts
00978     if (total_build_times != CBT_NCIRCUITS_TO_OBSERVE)
00979       test_assert(total_build_times == estimate.total_build_times);
00980 
00981     /* Now simulate that the network has become live and we need
00982      * a change */
00983     circuit_build_times_network_is_live(&estimate);
00984     circuit_build_times_network_is_live(&final);
00985 
00986     for (i = 0; i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT; i++) {
00987       circuit_build_times_count_timeout(&estimate, 1);
00988 
00989       if (i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1) {
00990         circuit_build_times_count_timeout(&final, 1);
00991       }
00992     }
00993 
00994     test_assert(estimate.liveness.after_firsthop_idx == 0);
00995     test_assert(final.liveness.after_firsthop_idx ==
00996                 CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1);
00997 
00998     test_assert(circuit_build_times_network_check_live(&estimate));
00999     test_assert(circuit_build_times_network_check_live(&final));
01000 
01001     circuit_build_times_count_timeout(&final, 1);
01002   }
01003 
01004  done:
01005   return;
01006 }
01007 
01011 static void
01012 test_policy_summary_helper(const char *policy_str,
01013                            const char *expected_summary)
01014 {
01015   config_line_t line;
01016   smartlist_t *policy = smartlist_new();
01017   char *summary = NULL;
01018   int r;
01019   short_policy_t *short_policy = NULL;
01020 
01021   line.key = (char*)"foo";
01022   line.value = (char *)policy_str;
01023   line.next = NULL;
01024 
01025   r = policies_parse_exit_policy(&line, &policy, 0, NULL, 1);
01026   test_eq(r, 0);
01027   summary = policy_summarize(policy);
01028 
01029   test_assert(summary != NULL);
01030   test_streq(summary, expected_summary);
01031 
01032   short_policy = parse_short_policy(summary);
01033   tt_assert(short_policy);
01034 
01035  done:
01036   tor_free(summary);
01037   if (policy)
01038     addr_policy_list_free(policy);
01039   short_policy_free(short_policy);
01040 }
01041 
01043 static void
01044 test_policies(void)
01045 {
01046   int i;
01047   smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
01048               *policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
01049               *policy7 = NULL;
01050   addr_policy_t *p;
01051   tor_addr_t tar;
01052   config_line_t line;
01053   smartlist_t *sm = NULL;
01054   char *policy_str = NULL;
01055 
01056   policy = smartlist_new();
01057 
01058   p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
01059   test_assert(p != NULL);
01060   test_eq(ADDR_POLICY_REJECT, p->policy_type);
01061   tor_addr_from_ipv4h(&tar, 0xc0a80000u);
01062   test_eq(0, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
01063   test_eq(16, p->maskbits);
01064   test_eq(1, p->prt_min);
01065   test_eq(65535, p->prt_max);
01066 
01067   smartlist_add(policy, p);
01068 
01069   tor_addr_from_ipv4h(&tar, 0x01020304u);
01070   test_assert(ADDR_POLICY_ACCEPTED ==
01071           compare_tor_addr_to_addr_policy(&tar, 2, policy));
01072   tor_addr_make_unspec(&tar);
01073   test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
01074           compare_tor_addr_to_addr_policy(&tar, 2, policy));
01075   tor_addr_from_ipv4h(&tar, 0xc0a80102);
01076   test_assert(ADDR_POLICY_REJECTED ==
01077           compare_tor_addr_to_addr_policy(&tar, 2, policy));
01078 
01079   test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, NULL, 1));
01080   test_assert(policy2);
01081 
01082   policy3 = smartlist_new();
01083   p = router_parse_addr_policy_item_from_string("reject *:*",-1);
01084   test_assert(p != NULL);
01085   smartlist_add(policy3, p);
01086   p = router_parse_addr_policy_item_from_string("accept *:*",-1);
01087   test_assert(p != NULL);
01088   smartlist_add(policy3, p);
01089 
01090   policy4 = smartlist_new();
01091   p = router_parse_addr_policy_item_from_string("accept *:443",-1);
01092   test_assert(p != NULL);
01093   smartlist_add(policy4, p);
01094   p = router_parse_addr_policy_item_from_string("accept *:443",-1);
01095   test_assert(p != NULL);
01096   smartlist_add(policy4, p);
01097 
01098   policy5 = smartlist_new();
01099   p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*",-1);
01100   test_assert(p != NULL);
01101   smartlist_add(policy5, p);
01102   p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*",-1);
01103   test_assert(p != NULL);
01104   smartlist_add(policy5, p);
01105   p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*",-1);
01106   test_assert(p != NULL);
01107   smartlist_add(policy5, p);
01108   p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
01109   test_assert(p != NULL);
01110   smartlist_add(policy5, p);
01111   p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*",-1);
01112   test_assert(p != NULL);
01113   smartlist_add(policy5, p);
01114   p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*",-1);
01115   test_assert(p != NULL);
01116   smartlist_add(policy5, p);
01117   p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*",-1);
01118   test_assert(p != NULL);
01119   smartlist_add(policy5, p);
01120   p = router_parse_addr_policy_item_from_string("reject *:1-65534",-1);
01121   test_assert(p != NULL);
01122   smartlist_add(policy5, p);
01123   p = router_parse_addr_policy_item_from_string("reject *:65535",-1);
01124   test_assert(p != NULL);
01125   smartlist_add(policy5, p);
01126   p = router_parse_addr_policy_item_from_string("accept *:1-65535",-1);
01127   test_assert(p != NULL);
01128   smartlist_add(policy5, p);
01129 
01130   policy6 = smartlist_new();
01131   p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*",-1);
01132   test_assert(p != NULL);
01133   smartlist_add(policy6, p);
01134 
01135   policy7 = smartlist_new();
01136   p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*",-1);
01137   test_assert(p != NULL);
01138   smartlist_add(policy7, p);
01139 
01140   test_assert(!exit_policy_is_general_exit(policy));
01141   test_assert(exit_policy_is_general_exit(policy2));
01142   test_assert(!exit_policy_is_general_exit(NULL));
01143   test_assert(!exit_policy_is_general_exit(policy3));
01144   test_assert(!exit_policy_is_general_exit(policy4));
01145   test_assert(!exit_policy_is_general_exit(policy5));
01146   test_assert(!exit_policy_is_general_exit(policy6));
01147   test_assert(!exit_policy_is_general_exit(policy7));
01148 
01149   test_assert(cmp_addr_policies(policy, policy2));
01150   test_assert(cmp_addr_policies(policy, NULL));
01151   test_assert(!cmp_addr_policies(policy2, policy2));
01152   test_assert(!cmp_addr_policies(NULL, NULL));
01153 
01154   test_assert(!policy_is_reject_star(policy2));
01155   test_assert(policy_is_reject_star(policy));
01156   test_assert(policy_is_reject_star(NULL));
01157 
01158   addr_policy_list_free(policy);
01159   policy = NULL;
01160 
01161   /* make sure compacting logic works. */
01162   policy = NULL;
01163   line.key = (char*)"foo";
01164   line.value = (char*)"accept *:80,reject private:*,reject *:*";
01165   line.next = NULL;
01166   test_assert(0 == policies_parse_exit_policy(&line, &policy, 0, NULL, 1));
01167   test_assert(policy);
01168   //test_streq(policy->string, "accept *:80");
01169   //test_streq(policy->next->string, "reject *:*");
01170   test_eq(smartlist_len(policy), 2);
01171 
01172   /* test policy summaries */
01173   /* check if we properly ignore private IP addresses */
01174   test_policy_summary_helper("reject 192.168.0.0/16:*,"
01175                              "reject 0.0.0.0/8:*,"
01176                              "reject 10.0.0.0/8:*,"
01177                              "accept *:10-30,"
01178                              "accept *:90,"
01179                              "reject *:*",
01180                              "accept 10-30,90");
01181   /* check all accept policies, and proper counting of rejects */
01182   test_policy_summary_helper("reject 11.0.0.0/9:80,"
01183                              "reject 12.0.0.0/9:80,"
01184                              "reject 13.0.0.0/9:80,"
01185                              "reject 14.0.0.0/9:80,"
01186                              "accept *:*", "accept 1-65535");
01187   test_policy_summary_helper("reject 11.0.0.0/9:80,"
01188                              "reject 12.0.0.0/9:80,"
01189                              "reject 13.0.0.0/9:80,"
01190                              "reject 14.0.0.0/9:80,"
01191                              "reject 15.0.0.0:81,"
01192                              "accept *:*", "accept 1-65535");
01193   test_policy_summary_helper("reject 11.0.0.0/9:80,"
01194                              "reject 12.0.0.0/9:80,"
01195                              "reject 13.0.0.0/9:80,"
01196                              "reject 14.0.0.0/9:80,"
01197                              "reject 15.0.0.0:80,"
01198                              "accept *:*",
01199                              "reject 80");
01200   /* no exits */
01201   test_policy_summary_helper("accept 11.0.0.0/9:80,"
01202                              "reject *:*",
01203                              "reject 1-65535");
01204   /* port merging */
01205   test_policy_summary_helper("accept *:80,"
01206                              "accept *:81,"
01207                              "accept *:100-110,"
01208                              "accept *:111,"
01209                              "reject *:*",
01210                              "accept 80-81,100-111");
01211   /* border ports */
01212   test_policy_summary_helper("accept *:1,"
01213                              "accept *:3,"
01214                              "accept *:65535,"
01215                              "reject *:*",
01216                              "accept 1,3,65535");
01217   /* holes */
01218   test_policy_summary_helper("accept *:1,"
01219                              "accept *:3,"
01220                              "accept *:5,"
01221                              "accept *:7,"
01222                              "reject *:*",
01223                              "accept 1,3,5,7");
01224   test_policy_summary_helper("reject *:1,"
01225                              "reject *:3,"
01226                              "reject *:5,"
01227                              "reject *:7,"
01228                              "accept *:*",
01229                              "reject 1,3,5,7");
01230 
01231   /* truncation ports */
01232   sm = smartlist_new();
01233   for (i=1; i<2000; i+=2) {
01234     char buf[POLICY_BUF_LEN];
01235     tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
01236     smartlist_add(sm, tor_strdup(buf));
01237   }
01238   smartlist_add(sm, tor_strdup("accept *:*"));
01239   policy_str = smartlist_join_strings(sm, ",", 0, NULL);
01240   test_policy_summary_helper( policy_str,
01241     "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
01242     "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
01243     "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
01244     "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
01245     "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
01246     "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
01247     "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
01248     "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
01249     "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
01250     "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
01251     "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
01252     "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
01253     "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
01254     "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
01255 
01256  done:
01257   addr_policy_list_free(policy);
01258   addr_policy_list_free(policy2);
01259   addr_policy_list_free(policy3);
01260   addr_policy_list_free(policy4);
01261   addr_policy_list_free(policy5);
01262   addr_policy_list_free(policy6);
01263   addr_policy_list_free(policy7);
01264   tor_free(policy_str);
01265   if (sm) {
01266     SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
01267     smartlist_free(sm);
01268   }
01269 }
01270 
01272 static void
01273 test_rend_fns(void)
01274 {
01275   rend_service_descriptor_t *generated = NULL, *parsed = NULL;
01276   char service_id[DIGEST_LEN];
01277   char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
01278   const char *next_desc;
01279   smartlist_t *descs = smartlist_new();
01280   char computed_desc_id[DIGEST_LEN];
01281   char parsed_desc_id[DIGEST_LEN];
01282   crypto_pk_t *pk1 = NULL, *pk2 = NULL;
01283   time_t now;
01284   char *intro_points_encrypted = NULL;
01285   size_t intro_points_size;
01286   size_t encoded_size;
01287   int i;
01288   char address1[] = "fooaddress.onion";
01289   char address2[] = "aaaaaaaaaaaaaaaa.onion";
01290   char address3[] = "fooaddress.exit";
01291   char address4[] = "www.torproject.org";
01292 
01293   test_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
01294   test_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
01295   test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
01296   test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
01297 
01298   pk1 = pk_generate(0);
01299   pk2 = pk_generate(1);
01300   generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
01301   generated->pk = crypto_pk_dup_key(pk1);
01302   crypto_pk_get_digest(generated->pk, service_id);
01303   base32_encode(service_id_base32, REND_SERVICE_ID_LEN_BASE32+1,
01304                 service_id, REND_SERVICE_ID_LEN);
01305   now = time(NULL);
01306   generated->timestamp = now;
01307   generated->version = 2;
01308   generated->protocols = 42;
01309   generated->intro_nodes = smartlist_new();
01310 
01311   for (i = 0; i < 3; i++) {
01312     rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
01313     crypto_pk_t *okey = pk_generate(2 + i);
01314     intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
01315     intro->extend_info->onion_key = okey;
01316     crypto_pk_get_digest(intro->extend_info->onion_key,
01317                          intro->extend_info->identity_digest);
01318     //crypto_rand(info->identity_digest, DIGEST_LEN); /* Would this work? */
01319     intro->extend_info->nickname[0] = '$';
01320     base16_encode(intro->extend_info->nickname + 1,
01321                   sizeof(intro->extend_info->nickname) - 1,
01322                   intro->extend_info->identity_digest, DIGEST_LEN);
01323     /* Does not cover all IP addresses. */
01324     tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
01325     intro->extend_info->port = 1 + crypto_rand_int(65535);
01326     intro->intro_key = crypto_pk_dup_key(pk2);
01327     smartlist_add(generated->intro_nodes, intro);
01328   }
01329   test_assert(rend_encode_v2_descriptors(descs, generated, now, 0,
01330                                          REND_NO_AUTH, NULL, NULL) > 0);
01331   test_assert(rend_compute_v2_desc_id(computed_desc_id, service_id_base32,
01332                                       NULL, now, 0) == 0);
01333   test_memeq(((rend_encoded_v2_service_descriptor_t *)
01334              smartlist_get(descs, 0))->desc_id, computed_desc_id, DIGEST_LEN);
01335   test_assert(rend_parse_v2_service_descriptor(&parsed, parsed_desc_id,
01336                                                &intro_points_encrypted,
01337                                                &intro_points_size,
01338                                                &encoded_size,
01339                                                &next_desc,
01340                                      ((rend_encoded_v2_service_descriptor_t *)
01341                                      smartlist_get(descs, 0))->desc_str) == 0);
01342   test_assert(parsed);
01343   test_memeq(((rend_encoded_v2_service_descriptor_t *)
01344              smartlist_get(descs, 0))->desc_id, parsed_desc_id, DIGEST_LEN);
01345   test_eq(rend_parse_introduction_points(parsed, intro_points_encrypted,
01346                                          intro_points_size), 3);
01347   test_assert(!crypto_pk_cmp_keys(generated->pk, parsed->pk));
01348   test_eq(parsed->timestamp, now);
01349   test_eq(parsed->version, 2);
01350   test_eq(parsed->protocols, 42);
01351   test_eq(smartlist_len(parsed->intro_nodes), 3);
01352   for (i = 0; i < smartlist_len(parsed->intro_nodes); i++) {
01353     rend_intro_point_t *par_intro = smartlist_get(parsed->intro_nodes, i),
01354       *gen_intro = smartlist_get(generated->intro_nodes, i);
01355     extend_info_t *par_info = par_intro->extend_info;
01356     extend_info_t *gen_info = gen_intro->extend_info;
01357     test_assert(!crypto_pk_cmp_keys(gen_info->onion_key, par_info->onion_key));
01358     test_memeq(gen_info->identity_digest, par_info->identity_digest,
01359                DIGEST_LEN);
01360     test_streq(gen_info->nickname, par_info->nickname);
01361     test_assert(tor_addr_eq(&gen_info->addr, &par_info->addr));
01362     test_eq(gen_info->port, par_info->port);
01363   }
01364 
01365   rend_service_descriptor_free(parsed);
01366   rend_service_descriptor_free(generated);
01367   parsed = generated = NULL;
01368 
01369  done:
01370   if (descs) {
01371     for (i = 0; i < smartlist_len(descs); i++)
01372       rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
01373     smartlist_free(descs);
01374   }
01375   if (parsed)
01376     rend_service_descriptor_free(parsed);
01377   if (generated)
01378     rend_service_descriptor_free(generated);
01379   if (pk1)
01380     crypto_pk_free(pk1);
01381   if (pk2)
01382     crypto_pk_free(pk2);
01383   tor_free(intro_points_encrypted);
01384 }
01385 
01387 static void
01388 test_geoip(void)
01389 {
01390   int i, j;
01391   time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
01392   char *s = NULL;
01393   const char *bridge_stats_1 =
01394       "bridge-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01395       "bridge-ips zz=24,xy=8\n",
01396   *dirreq_stats_1 =
01397       "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01398       "dirreq-v3-ips ab=8\n"
01399       "dirreq-v2-ips \n"
01400       "dirreq-v3-reqs ab=8\n"
01401       "dirreq-v2-reqs \n"
01402       "dirreq-v3-resp ok=0,not-enough-sigs=0,unavailable=0,not-found=0,"
01403           "not-modified=0,busy=0\n"
01404       "dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
01405           "busy=0\n"
01406       "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
01407       "dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
01408       "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n"
01409       "dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
01410   *dirreq_stats_2 =
01411       "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01412       "dirreq-v3-ips \n"
01413       "dirreq-v2-ips \n"
01414       "dirreq-v3-reqs \n"
01415       "dirreq-v2-reqs \n"
01416       "dirreq-v3-resp ok=0,not-enough-sigs=0,unavailable=0,not-found=0,"
01417           "not-modified=0,busy=0\n"
01418       "dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
01419           "busy=0\n"
01420       "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
01421       "dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
01422       "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n"
01423       "dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
01424   *dirreq_stats_3 =
01425       "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01426       "dirreq-v3-ips \n"
01427       "dirreq-v2-ips \n"
01428       "dirreq-v3-reqs \n"
01429       "dirreq-v2-reqs \n"
01430       "dirreq-v3-resp ok=8,not-enough-sigs=0,unavailable=0,not-found=0,"
01431           "not-modified=0,busy=0\n"
01432       "dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
01433           "busy=0\n"
01434       "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
01435       "dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
01436       "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n"
01437       "dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
01438   *dirreq_stats_4 =
01439       "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01440       "dirreq-v3-ips \n"
01441       "dirreq-v2-ips \n"
01442       "dirreq-v3-reqs \n"
01443       "dirreq-v2-reqs \n"
01444       "dirreq-v3-resp ok=8,not-enough-sigs=0,unavailable=0,not-found=0,"
01445           "not-modified=0,busy=0\n"
01446       "dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
01447           "busy=0\n"
01448       "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
01449       "dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
01450       "dirreq-v3-tunneled-dl complete=0,timeout=0,running=4\n"
01451       "dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
01452   *entry_stats_1 =
01453       "entry-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01454       "entry-ips ab=8\n",
01455   *entry_stats_2 =
01456       "entry-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01457       "entry-ips \n";
01458   tor_addr_t addr;
01459 
01460   /* Populate the DB a bit.  Add these in order, since we can't do the final
01461    * 'sort' step.  These aren't very good IP addresses, but they're perfectly
01462    * fine uint32_t values. */
01463   test_eq(0, geoip_parse_entry("10,50,AB"));
01464   test_eq(0, geoip_parse_entry("52,90,XY"));
01465   test_eq(0, geoip_parse_entry("95,100,AB"));
01466   test_eq(0, geoip_parse_entry("\"105\",\"140\",\"ZZ\""));
01467   test_eq(0, geoip_parse_entry("\"150\",\"190\",\"XY\""));
01468   test_eq(0, geoip_parse_entry("\"200\",\"250\",\"AB\""));
01469 
01470   /* We should have 4 countries: ??, ab, xy, zz. */
01471   test_eq(4, geoip_get_n_countries());
01472   /* Make sure that country ID actually works. */
01473 #define NAMEFOR(x) geoip_get_country_name(geoip_get_country_by_ip(x))
01474   test_streq("??", NAMEFOR(3));
01475   test_eq(0, geoip_get_country_by_ip(3));
01476   test_streq("ab", NAMEFOR(32));
01477   test_streq("??", NAMEFOR(5));
01478   test_streq("??", NAMEFOR(51));
01479   test_streq("xy", NAMEFOR(150));
01480   test_streq("xy", NAMEFOR(190));
01481   test_streq("??", NAMEFOR(2000));
01482 #undef NAMEFOR
01483 
01484   get_options_mutable()->BridgeRelay = 1;
01485   get_options_mutable()->BridgeRecordUsageByCountry = 1;
01486   /* Put 9 observations in AB... */
01487   for (i=32; i < 40; ++i) {
01488     tor_addr_from_ipv4h(&addr, (uint32_t) i);
01489     geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now-7200);
01490   }
01491   tor_addr_from_ipv4h(&addr, (uint32_t) 225);
01492   geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now-7200);
01493   /* and 3 observations in XY, several times. */
01494   for (j=0; j < 10; ++j)
01495     for (i=52; i < 55; ++i) {
01496       tor_addr_from_ipv4h(&addr, (uint32_t) i);
01497       geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now-3600);
01498     }
01499   /* and 17 observations in ZZ... */
01500   for (i=110; i < 127; ++i) {
01501     tor_addr_from_ipv4h(&addr, (uint32_t) i);
01502     geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
01503   }
01504   s = geoip_get_client_history(GEOIP_CLIENT_CONNECT);
01505   test_assert(s);
01506   test_streq("zz=24,ab=16,xy=8", s);
01507   tor_free(s);
01508 
01509   /* Now clear out all the AB observations. */
01510   geoip_remove_old_clients(now-6000);
01511   s = geoip_get_client_history(GEOIP_CLIENT_CONNECT);
01512   test_assert(s);
01513   test_streq("zz=24,xy=8", s);
01514 
01515   /* Start testing bridge statistics by making sure that we don't output
01516    * bridge stats without initializing them. */
01517   s = geoip_format_bridge_stats(now + 86400);
01518   test_assert(!s);
01519 
01520   /* Initialize stats and generate the bridge-stats history string out of
01521    * the connecting clients added above. */
01522   geoip_bridge_stats_init(now);
01523   s = geoip_format_bridge_stats(now + 86400);
01524   test_streq(bridge_stats_1, s);
01525   tor_free(s);
01526 
01527   /* Stop collecting bridge stats and make sure we don't write a history
01528    * string anymore. */
01529   geoip_bridge_stats_term();
01530   s = geoip_format_bridge_stats(now + 86400);
01531   test_assert(!s);
01532 
01533   /* Stop being a bridge and start being a directory mirror that gathers
01534    * directory request statistics. */
01535   geoip_bridge_stats_term();
01536   get_options_mutable()->BridgeRelay = 0;
01537   get_options_mutable()->BridgeRecordUsageByCountry = 0;
01538   get_options_mutable()->DirReqStatistics = 1;
01539 
01540   /* Start testing dirreq statistics by making sure that we don't collect
01541    * dirreq stats without initializing them. */
01542   tor_addr_from_ipv4h(&addr, (uint32_t) 100);
01543   geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
01544   s = geoip_format_dirreq_stats(now + 86400);
01545   test_assert(!s);
01546 
01547   /* Initialize stats, note one connecting client, and generate the
01548    * dirreq-stats history string. */
01549   geoip_dirreq_stats_init(now);
01550   tor_addr_from_ipv4h(&addr, (uint32_t) 100);
01551   geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
01552   s = geoip_format_dirreq_stats(now + 86400);
01553   test_streq(dirreq_stats_1, s);
01554   tor_free(s);
01555 
01556   /* Stop collecting stats, add another connecting client, and ensure we
01557    * don't generate a history string. */
01558   geoip_dirreq_stats_term();
01559   tor_addr_from_ipv4h(&addr, (uint32_t) 101);
01560   geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
01561   s = geoip_format_dirreq_stats(now + 86400);
01562   test_assert(!s);
01563 
01564   /* Re-start stats, add a connecting client, reset stats, and make sure
01565    * that we get an all empty history string. */
01566   geoip_dirreq_stats_init(now);
01567   tor_addr_from_ipv4h(&addr, (uint32_t) 100);
01568   geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
01569   geoip_reset_dirreq_stats(now);
01570   s = geoip_format_dirreq_stats(now + 86400);
01571   test_streq(dirreq_stats_2, s);
01572   tor_free(s);
01573 
01574   /* Note a successful network status response and make sure that it
01575    * appears in the history string. */
01576   geoip_note_ns_response(GEOIP_CLIENT_NETWORKSTATUS, GEOIP_SUCCESS);
01577   s = geoip_format_dirreq_stats(now + 86400);
01578   test_streq(dirreq_stats_3, s);
01579   tor_free(s);
01580 
01581   /* Start a tunneled directory request. */
01582   geoip_start_dirreq((uint64_t) 1, 1024, GEOIP_CLIENT_NETWORKSTATUS,
01583                      DIRREQ_TUNNELED);
01584   s = geoip_format_dirreq_stats(now + 86400);
01585   test_streq(dirreq_stats_4, s);
01586 
01587   /* Stop collecting directory request statistics and start gathering
01588    * entry stats. */
01589   geoip_dirreq_stats_term();
01590   get_options_mutable()->DirReqStatistics = 0;
01591   get_options_mutable()->EntryStatistics = 1;
01592 
01593   /* Start testing entry statistics by making sure that we don't collect
01594    * anything without initializing entry stats. */
01595   tor_addr_from_ipv4h(&addr, (uint32_t) 100);
01596   geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
01597   s = geoip_format_entry_stats(now + 86400);
01598   test_assert(!s);
01599 
01600   /* Initialize stats, note one connecting client, and generate the
01601    * entry-stats history string. */
01602   geoip_entry_stats_init(now);
01603   tor_addr_from_ipv4h(&addr, (uint32_t) 100);
01604   geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
01605   s = geoip_format_entry_stats(now + 86400);
01606   test_streq(entry_stats_1, s);
01607   tor_free(s);
01608 
01609   /* Stop collecting stats, add another connecting client, and ensure we
01610    * don't generate a history string. */
01611   geoip_entry_stats_term();
01612   tor_addr_from_ipv4h(&addr, (uint32_t) 101);
01613   geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
01614   s = geoip_format_entry_stats(now + 86400);
01615   test_assert(!s);
01616 
01617   /* Re-start stats, add a connecting client, reset stats, and make sure
01618    * that we get an all empty history string. */
01619   geoip_entry_stats_init(now);
01620   tor_addr_from_ipv4h(&addr, (uint32_t) 100);
01621   geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
01622   geoip_reset_entry_stats(now);
01623   s = geoip_format_entry_stats(now + 86400);
01624   test_streq(entry_stats_2, s);
01625   tor_free(s);
01626 
01627   /* Stop collecting entry statistics. */
01628   geoip_entry_stats_term();
01629   get_options_mutable()->EntryStatistics = 0;
01630 
01631  done:
01632   tor_free(s);
01633 }
01634 
01636 static void
01637 test_stats(void)
01638 {
01639   time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
01640   char *s = NULL;
01641   int i;
01642 
01643   /* Start with testing exit port statistics; we shouldn't collect exit
01644    * stats without initializing them. */
01645   rep_hist_note_exit_stream_opened(80);
01646   rep_hist_note_exit_bytes(80, 100, 10000);
01647   s = rep_hist_format_exit_stats(now + 86400);
01648   test_assert(!s);
01649 
01650   /* Initialize stats, note some streams and bytes, and generate history
01651    * string. */
01652   rep_hist_exit_stats_init(now);
01653   rep_hist_note_exit_stream_opened(80);
01654   rep_hist_note_exit_bytes(80, 100, 10000);
01655   rep_hist_note_exit_stream_opened(443);
01656   rep_hist_note_exit_bytes(443, 100, 10000);
01657   rep_hist_note_exit_bytes(443, 100, 10000);
01658   s = rep_hist_format_exit_stats(now + 86400);
01659   test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01660              "exit-kibibytes-written 80=1,443=1,other=0\n"
01661              "exit-kibibytes-read 80=10,443=20,other=0\n"
01662              "exit-streams-opened 80=4,443=4,other=0\n", s);
01663   tor_free(s);
01664 
01665   /* Add a few bytes on 10 more ports and ensure that only the top 10
01666    * ports are contained in the history string. */
01667   for (i = 50; i < 60; i++) {
01668     rep_hist_note_exit_bytes(i, i, i);
01669     rep_hist_note_exit_stream_opened(i);
01670   }
01671   s = rep_hist_format_exit_stats(now + 86400);
01672   test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01673              "exit-kibibytes-written 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
01674              "59=1,80=1,443=1,other=1\n"
01675              "exit-kibibytes-read 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
01676              "59=1,80=10,443=20,other=1\n"
01677              "exit-streams-opened 52=4,53=4,54=4,55=4,56=4,57=4,58=4,"
01678              "59=4,80=4,443=4,other=4\n", s);
01679   tor_free(s);
01680 
01681   /* Stop collecting stats, add some bytes, and ensure we don't generate
01682    * a history string. */
01683   rep_hist_exit_stats_term();
01684   rep_hist_note_exit_bytes(80, 100, 10000);
01685   s = rep_hist_format_exit_stats(now + 86400);
01686   test_assert(!s);
01687 
01688   /* Re-start stats, add some bytes, reset stats, and see what history we
01689    * get when observing no streams or bytes at all. */
01690   rep_hist_exit_stats_init(now);
01691   rep_hist_note_exit_stream_opened(80);
01692   rep_hist_note_exit_bytes(80, 100, 10000);
01693   rep_hist_reset_exit_stats(now);
01694   s = rep_hist_format_exit_stats(now + 86400);
01695   test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01696              "exit-kibibytes-written other=0\n"
01697              "exit-kibibytes-read other=0\n"
01698              "exit-streams-opened other=0\n", s);
01699   tor_free(s);
01700 
01701   /* Continue with testing connection statistics; we shouldn't collect
01702    * conn stats without initializing them. */
01703   rep_hist_note_or_conn_bytes(1, 20, 400, now);
01704   s = rep_hist_format_conn_stats(now + 86400);
01705   test_assert(!s);
01706 
01707   /* Initialize stats, note bytes, and generate history string. */
01708   rep_hist_conn_stats_init(now);
01709   rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
01710   rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
01711   rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
01712   rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
01713   s = rep_hist_format_conn_stats(now + 86400);
01714   test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n", s);
01715   tor_free(s);
01716 
01717   /* Stop collecting stats, add some bytes, and ensure we don't generate
01718    * a history string. */
01719   rep_hist_conn_stats_term();
01720   rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
01721   s = rep_hist_format_conn_stats(now + 86400);
01722   test_assert(!s);
01723 
01724   /* Re-start stats, add some bytes, reset stats, and see what history we
01725    * get when observing no bytes at all. */
01726   rep_hist_conn_stats_init(now);
01727   rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
01728   rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
01729   rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
01730   rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
01731   rep_hist_reset_conn_stats(now);
01732   s = rep_hist_format_conn_stats(now + 86400);
01733   test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n", s);
01734   tor_free(s);
01735 
01736   /* Continue with testing buffer statistics; we shouldn't collect buffer
01737    * stats without initializing them. */
01738   rep_hist_add_buffer_stats(2.0, 2.0, 20);
01739   s = rep_hist_format_buffer_stats(now + 86400);
01740   test_assert(!s);
01741 
01742   /* Initialize stats, add statistics for a single circuit, and generate
01743    * the history string. */
01744   rep_hist_buffer_stats_init(now);
01745   rep_hist_add_buffer_stats(2.0, 2.0, 20);
01746   s = rep_hist_format_buffer_stats(now + 86400);
01747   test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01748              "cell-processed-cells 20,0,0,0,0,0,0,0,0,0\n"
01749              "cell-queued-cells 2.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
01750                                "0.00,0.00\n"
01751              "cell-time-in-queue 2,0,0,0,0,0,0,0,0,0\n"
01752              "cell-circuits-per-decile 1\n", s);
01753   tor_free(s);
01754 
01755   /* Add nineteen more circuit statistics to the one that's already in the
01756    * history to see that the math works correctly. */
01757   for (i = 21; i < 30; i++)
01758     rep_hist_add_buffer_stats(2.0, 2.0, i);
01759   for (i = 20; i < 30; i++)
01760     rep_hist_add_buffer_stats(3.5, 3.5, i);
01761   s = rep_hist_format_buffer_stats(now + 86400);
01762   test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01763              "cell-processed-cells 29,28,27,26,25,24,23,22,21,20\n"
01764              "cell-queued-cells 2.75,2.75,2.75,2.75,2.75,2.75,2.75,2.75,"
01765                                "2.75,2.75\n"
01766              "cell-time-in-queue 3,3,3,3,3,3,3,3,3,3\n"
01767              "cell-circuits-per-decile 2\n", s);
01768   tor_free(s);
01769 
01770   /* Stop collecting stats, add statistics for one circuit, and ensure we
01771    * don't generate a history string. */
01772   rep_hist_buffer_stats_term();
01773   rep_hist_add_buffer_stats(2.0, 2.0, 20);
01774   s = rep_hist_format_buffer_stats(now + 86400);
01775   test_assert(!s);
01776 
01777   /* Re-start stats, add statistics for one circuit, reset stats, and make
01778    * sure that the history has all zeros. */
01779   rep_hist_buffer_stats_init(now);
01780   rep_hist_add_buffer_stats(2.0, 2.0, 20);
01781   rep_hist_reset_buffer_stats(now);
01782   s = rep_hist_format_buffer_stats(now + 86400);
01783   test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
01784              "cell-processed-cells 0,0,0,0,0,0,0,0,0,0\n"
01785              "cell-queued-cells 0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
01786                                "0.00,0.00\n"
01787              "cell-time-in-queue 0,0,0,0,0,0,0,0,0,0\n"
01788              "cell-circuits-per-decile 0\n", s);
01789 
01790  done:
01791   tor_free(s);
01792 }
01793 
01794 static void *
01795 legacy_test_setup(const struct testcase_t *testcase)
01796 {
01797   return testcase->setup_data;
01798 }
01799 
01800 void
01801 legacy_test_helper(void *data)
01802 {
01803   void (*fn)(void) = data;
01804   fn();
01805 }
01806 
01807 static int
01808 legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
01809 {
01810   (void)ptr;
01811   (void)testcase;
01812   return 1;
01813 }
01814 
01815 const struct testcase_setup_t legacy_setup = {
01816   legacy_test_setup, legacy_test_cleanup
01817 };
01818 
01819 #define ENT(name)                                                       \
01820   { #name, legacy_test_helper, 0, &legacy_setup, test_ ## name }
01821 #define SUBENT(group, name)                                             \
01822   { #group "_" #name, legacy_test_helper, 0, &legacy_setup,             \
01823       test_ ## group ## _ ## name }
01824 #define DISABLED(name)                                                  \
01825   { #name, legacy_test_helper, TT_SKIP, &legacy_setup, test_ ## name }
01826 #define FORK(name)                                                      \
01827   { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_ ## name }
01828 
01829 static struct testcase_t test_array[] = {
01830   ENT(buffers),
01831   { "buffer_copy", test_buffer_copy, 0, NULL, NULL },
01832   ENT(onion_handshake),
01833   ENT(circuit_timeout),
01834   ENT(policies),
01835   ENT(rend_fns),
01836   ENT(geoip),
01837   FORK(stats),
01838 
01839   END_OF_TESTCASES
01840 };
01841 
01842 #define SOCKSENT(name)                                  \
01843   { #name, test_socks_##name, TT_FORK, &socks_setup, NULL }
01844 
01845 static struct testcase_t socks_tests[] = {
01846   SOCKSENT(4_unsupported_commands),
01847   SOCKSENT(4_supported_commands),
01848 
01849   SOCKSENT(5_unsupported_commands),
01850   SOCKSENT(5_supported_commands),
01851   SOCKSENT(5_no_authenticate),
01852   SOCKSENT(5_auth_before_negotiation),
01853   SOCKSENT(5_authenticate),
01854   SOCKSENT(5_authenticate_with_data),
01855 
01856   END_OF_TESTCASES
01857 };
01858 
01859 extern struct testcase_t addr_tests[];
01860 extern struct testcase_t crypto_tests[];
01861 extern struct testcase_t container_tests[];
01862 extern struct testcase_t util_tests[];
01863 extern struct testcase_t dir_tests[];
01864 extern struct testcase_t microdesc_tests[];
01865 extern struct testcase_t pt_tests[];
01866 extern struct testcase_t config_tests[];
01867 
01868 static struct testgroup_t testgroups[] = {
01869   { "", test_array },
01870   { "socks/", socks_tests },
01871   { "addr/", addr_tests },
01872   { "crypto/", crypto_tests },
01873   { "container/", container_tests },
01874   { "util/", util_tests },
01875   { "dir/", dir_tests },
01876   { "dir/md/", microdesc_tests },
01877   { "pt/", pt_tests },
01878   { "config/", config_tests },
01879   END_OF_GROUPS
01880 };
01881 
01884 int
01885 main(int c, const char **v)
01886 {
01887   or_options_t *options;
01888   char *errmsg = NULL;
01889   int i, i_out;
01890   int loglevel = LOG_ERR;
01891 
01892 #ifdef USE_DMALLOC
01893   {
01894     int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
01895     tor_assert(r);
01896   }
01897 #endif
01898 
01899   update_approx_time(time(NULL));
01900   options = options_new();
01901   tor_threads_init();
01902   init_logging();
01903 
01904   for (i_out = i = 1; i < c; ++i) {
01905     if (!strcmp(v[i], "--warn")) {
01906       loglevel = LOG_WARN;
01907     } else if (!strcmp(v[i], "--notice")) {
01908       loglevel = LOG_NOTICE;
01909     } else if (!strcmp(v[i], "--info")) {
01910       loglevel = LOG_INFO;
01911     } else if (!strcmp(v[i], "--debug")) {
01912       loglevel = LOG_DEBUG;
01913     } else {
01914       v[i_out++] = v[i];
01915     }
01916   }
01917   c = i_out;
01918 
01919   {
01920     log_severity_list_t s;
01921     memset(&s, 0, sizeof(s));
01922     set_log_severity_config(loglevel, LOG_ERR, &s);
01923     add_stream_log(&s, "", fileno(stdout));
01924   }
01925 
01926   options->command = CMD_RUN_UNITTESTS;
01927   if (crypto_global_init(0, NULL, NULL)) {
01928     printf("Can't initialize crypto subsystem; exiting.\n");
01929     return 1;
01930   }
01931   crypto_set_tls_dh_prime(NULL);
01932   rep_hist_init();
01933   network_init();
01934   setup_directory();
01935   options_init(options);
01936   options->DataDirectory = tor_strdup(temp_dir);
01937   options->EntryStatistics = 1;
01938   if (set_options(options, &errmsg) < 0) {
01939     printf("Failed to set initial options: %s\n", errmsg);
01940     tor_free(errmsg);
01941     return 1;
01942   }
01943 
01944   crypto_seed_rng(1);
01945 
01946   atexit(remove_directory);
01947 
01948   have_failed = (tinytest_main(c, v, testgroups) != 0);
01949 
01950   free_pregenerated_keys();
01951 #ifdef USE_DMALLOC
01952   tor_free_all(0);
01953   dmalloc_log_unfreed();
01954 #endif
01955 
01956   if (have_failed)
01957     return 1;
01958   else
01959     return 0;
01960 }
01961