test.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2009, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* Ordinarily defined in tor_main.c; this bit is just here to provide one
  6. * since we're not linking to tor_main.c */
  7. const char tor_git_revision[] = "";
  8. /**
  9. * \file test.c
  10. * \brief Unit tests for many pieces of the lower level Tor modules.
  11. **/
  12. #include "orconfig.h"
  13. #include <stdio.h>
  14. #ifdef HAVE_FCNTL_H
  15. #include <fcntl.h>
  16. #endif
  17. #ifdef MS_WINDOWS
  18. /* For mkdir() */
  19. #include <direct.h>
  20. #else
  21. #include <dirent.h>
  22. #endif
  23. /* These macros pull in declarations for some functions and structures that
  24. * are typically file-private. */
  25. #define BUFFERS_PRIVATE
  26. #define CONFIG_PRIVATE
  27. #define GEOIP_PRIVATE
  28. #define ROUTER_PRIVATE
  29. #define CIRCUIT_PRIVATE
  30. /*
  31. * Linux doesn't provide lround in math.h by default, but mac os does...
  32. * It's best just to leave math.h out of the picture entirely.
  33. */
  34. //#include <math.h>
  35. long int lround(double x);
  36. double fabs(double x);
  37. #include "or.h"
  38. #include "test.h"
  39. #include "torgzip.h"
  40. #include "mempool.h"
  41. #include "memarea.h"
  42. #ifdef USE_DMALLOC
  43. #include <dmalloc.h>
  44. #include <openssl/crypto.h>
  45. #endif
  46. /** Set to true if any unit test has failed. Mostly, this is set by the macros
  47. * in test.h */
  48. int have_failed = 0;
  49. /** Temporary directory (set up by setup_directory) under which we store all
  50. * our files during testing. */
  51. static char temp_dir[256];
  52. /** Select and create the temporary directory we'll use to run our unit tests.
  53. * Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
  54. * idempotent. */
  55. static void
  56. setup_directory(void)
  57. {
  58. static int is_setup = 0;
  59. int r;
  60. if (is_setup) return;
  61. #ifdef MS_WINDOWS
  62. // XXXX
  63. tor_snprintf(temp_dir, sizeof(temp_dir),
  64. "c:\\windows\\temp\\tor_test_%d", (int)getpid());
  65. r = mkdir(temp_dir);
  66. #else
  67. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
  68. r = mkdir(temp_dir, 0700);
  69. #endif
  70. if (r) {
  71. fprintf(stderr, "Can't create directory %s:", temp_dir);
  72. perror("");
  73. exit(1);
  74. }
  75. is_setup = 1;
  76. }
  77. /** Return a filename relative to our testing temporary directory */
  78. const char *
  79. get_fname(const char *name)
  80. {
  81. static char buf[1024];
  82. setup_directory();
  83. tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
  84. return buf;
  85. }
  86. /** Remove all files stored under the temporary directory, and the directory
  87. * itself. */
  88. static void
  89. remove_directory(void)
  90. {
  91. smartlist_t *elements = tor_listdir(temp_dir);
  92. if (elements) {
  93. SMARTLIST_FOREACH(elements, const char *, cp,
  94. {
  95. size_t len = strlen(cp)+strlen(temp_dir)+16;
  96. char *tmp = tor_malloc(len);
  97. tor_snprintf(tmp, len, "%s"PATH_SEPARATOR"%s", temp_dir, cp);
  98. unlink(tmp);
  99. tor_free(tmp);
  100. });
  101. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  102. smartlist_free(elements);
  103. }
  104. rmdir(temp_dir);
  105. }
  106. /** Define this if unit tests spend too much time generating public keys*/
  107. #undef CACHE_GENERATED_KEYS
  108. static crypto_pk_env_t *pregen_keys[5] = {NULL, NULL, NULL, NULL, NULL};
  109. #define N_PREGEN_KEYS ((int)(sizeof(pregen_keys)/sizeof(pregen_keys[0])))
  110. /** Generate and return a new keypair for use in unit tests. If we're using
  111. * the key cache optimization, we might reuse keys: we only guarantee that
  112. * keys made with distinct values for <b>idx</b> are different. The value of
  113. * <b>idx</b> must be at least 0, and less than N_PREGEN_KEYS. */
  114. crypto_pk_env_t *
  115. pk_generate(int idx)
  116. {
  117. #ifdef CACHE_GENERATED_KEYS
  118. tor_assert(idx < N_PREGEN_KEYS);
  119. if (! pregen_keys[idx]) {
  120. pregen_keys[idx] = crypto_new_pk_env();
  121. tor_assert(!crypto_pk_generate_key(pregen_keys[idx]));
  122. }
  123. return crypto_pk_dup_key(pregen_keys[idx]);
  124. #else
  125. crypto_pk_env_t *result;
  126. (void) idx;
  127. result = crypto_new_pk_env();
  128. tor_assert(!crypto_pk_generate_key(result));
  129. return result;
  130. #endif
  131. }
  132. /** Free all storage used for the cached key optimization. */
  133. static void
  134. free_pregenerated_keys(void)
  135. {
  136. unsigned idx;
  137. for (idx = 0; idx < N_PREGEN_KEYS; ++idx) {
  138. if (pregen_keys[idx]) {
  139. crypto_free_pk_env(pregen_keys[idx]);
  140. pregen_keys[idx] = NULL;
  141. }
  142. }
  143. }
  144. /** Run unit tests for buffers.c */
  145. static void
  146. test_buffers(void)
  147. {
  148. char str[256];
  149. char str2[256];
  150. buf_t *buf = NULL, *buf2 = NULL;
  151. const char *cp;
  152. int j;
  153. size_t r;
  154. /****
  155. * buf_new
  156. ****/
  157. if (!(buf = buf_new()))
  158. test_fail();
  159. //test_eq(buf_capacity(buf), 4096);
  160. test_eq(buf_datalen(buf), 0);
  161. /****
  162. * General pointer frobbing
  163. */
  164. for (j=0;j<256;++j) {
  165. str[j] = (char)j;
  166. }
  167. write_to_buf(str, 256, buf);
  168. write_to_buf(str, 256, buf);
  169. test_eq(buf_datalen(buf), 512);
  170. fetch_from_buf(str2, 200, buf);
  171. test_memeq(str, str2, 200);
  172. test_eq(buf_datalen(buf), 312);
  173. memset(str2, 0, sizeof(str2));
  174. fetch_from_buf(str2, 256, buf);
  175. test_memeq(str+200, str2, 56);
  176. test_memeq(str, str2+56, 200);
  177. test_eq(buf_datalen(buf), 56);
  178. memset(str2, 0, sizeof(str2));
  179. /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
  180. * another 3584 bytes, we hit the end. */
  181. for (j=0;j<15;++j) {
  182. write_to_buf(str, 256, buf);
  183. }
  184. assert_buf_ok(buf);
  185. test_eq(buf_datalen(buf), 3896);
  186. fetch_from_buf(str2, 56, buf);
  187. test_eq(buf_datalen(buf), 3840);
  188. test_memeq(str+200, str2, 56);
  189. for (j=0;j<15;++j) {
  190. memset(str2, 0, sizeof(str2));
  191. fetch_from_buf(str2, 256, buf);
  192. test_memeq(str, str2, 256);
  193. }
  194. test_eq(buf_datalen(buf), 0);
  195. buf_free(buf);
  196. buf = NULL;
  197. /* Okay, now make sure growing can work. */
  198. buf = buf_new_with_capacity(16);
  199. //test_eq(buf_capacity(buf), 16);
  200. write_to_buf(str+1, 255, buf);
  201. //test_eq(buf_capacity(buf), 256);
  202. fetch_from_buf(str2, 254, buf);
  203. test_memeq(str+1, str2, 254);
  204. //test_eq(buf_capacity(buf), 256);
  205. assert_buf_ok(buf);
  206. write_to_buf(str, 32, buf);
  207. //test_eq(buf_capacity(buf), 256);
  208. assert_buf_ok(buf);
  209. write_to_buf(str, 256, buf);
  210. assert_buf_ok(buf);
  211. //test_eq(buf_capacity(buf), 512);
  212. test_eq(buf_datalen(buf), 33+256);
  213. fetch_from_buf(str2, 33, buf);
  214. test_eq(*str2, str[255]);
  215. test_memeq(str2+1, str, 32);
  216. //test_eq(buf_capacity(buf), 512);
  217. test_eq(buf_datalen(buf), 256);
  218. fetch_from_buf(str2, 256, buf);
  219. test_memeq(str, str2, 256);
  220. /* now try shrinking: case 1. */
  221. buf_free(buf);
  222. buf = buf_new_with_capacity(33668);
  223. for (j=0;j<67;++j) {
  224. write_to_buf(str,255, buf);
  225. }
  226. //test_eq(buf_capacity(buf), 33668);
  227. test_eq(buf_datalen(buf), 17085);
  228. for (j=0; j < 40; ++j) {
  229. fetch_from_buf(str2, 255,buf);
  230. test_memeq(str2, str, 255);
  231. }
  232. /* now try shrinking: case 2. */
  233. buf_free(buf);
  234. buf = buf_new_with_capacity(33668);
  235. for (j=0;j<67;++j) {
  236. write_to_buf(str,255, buf);
  237. }
  238. for (j=0; j < 20; ++j) {
  239. fetch_from_buf(str2, 255,buf);
  240. test_memeq(str2, str, 255);
  241. }
  242. for (j=0;j<80;++j) {
  243. write_to_buf(str,255, buf);
  244. }
  245. //test_eq(buf_capacity(buf),33668);
  246. for (j=0; j < 120; ++j) {
  247. fetch_from_buf(str2, 255,buf);
  248. test_memeq(str2, str, 255);
  249. }
  250. /* Move from buf to buf. */
  251. buf_free(buf);
  252. buf = buf_new_with_capacity(4096);
  253. buf2 = buf_new_with_capacity(4096);
  254. for (j=0;j<100;++j)
  255. write_to_buf(str, 255, buf);
  256. test_eq(buf_datalen(buf), 25500);
  257. for (j=0;j<100;++j) {
  258. r = 10;
  259. move_buf_to_buf(buf2, buf, &r);
  260. test_eq(r, 0);
  261. }
  262. test_eq(buf_datalen(buf), 24500);
  263. test_eq(buf_datalen(buf2), 1000);
  264. for (j=0;j<3;++j) {
  265. fetch_from_buf(str2, 255, buf2);
  266. test_memeq(str2, str, 255);
  267. }
  268. r = 8192; /*big move*/
  269. move_buf_to_buf(buf2, buf, &r);
  270. test_eq(r, 0);
  271. r = 30000; /* incomplete move */
  272. move_buf_to_buf(buf2, buf, &r);
  273. test_eq(r, 13692);
  274. for (j=0;j<97;++j) {
  275. fetch_from_buf(str2, 255, buf2);
  276. test_memeq(str2, str, 255);
  277. }
  278. buf_free(buf);
  279. buf_free(buf2);
  280. buf = buf2 = NULL;
  281. buf = buf_new_with_capacity(5);
  282. cp = "Testing. This is a moderately long Testing string.";
  283. for (j = 0; cp[j]; j++)
  284. write_to_buf(cp+j, 1, buf);
  285. test_eq(0, buf_find_string_offset(buf, "Testing", 7));
  286. test_eq(1, buf_find_string_offset(buf, "esting", 6));
  287. test_eq(1, buf_find_string_offset(buf, "est", 3));
  288. test_eq(39, buf_find_string_offset(buf, "ing str", 7));
  289. test_eq(35, buf_find_string_offset(buf, "Testing str", 11));
  290. test_eq(32, buf_find_string_offset(buf, "ng ", 3));
  291. test_eq(43, buf_find_string_offset(buf, "string.", 7));
  292. test_eq(-1, buf_find_string_offset(buf, "shrdlu", 6));
  293. test_eq(-1, buf_find_string_offset(buf, "Testing thing", 13));
  294. test_eq(-1, buf_find_string_offset(buf, "ngx", 3));
  295. buf_free(buf);
  296. buf = NULL;
  297. #if 0
  298. {
  299. int s;
  300. int eof;
  301. int i;
  302. buf_t *buf2;
  303. /****
  304. * read_to_buf
  305. ****/
  306. s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
  307. write(s, str, 256);
  308. close(s);
  309. s = open(get_fname("data"), O_RDONLY, 0);
  310. eof = 0;
  311. errno = 0; /* XXXX */
  312. i = read_to_buf(s, 10, buf, &eof);
  313. printf("%s\n", strerror(errno));
  314. test_eq(i, 10);
  315. test_eq(eof, 0);
  316. //test_eq(buf_capacity(buf), 4096);
  317. test_eq(buf_datalen(buf), 10);
  318. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
  319. /* Test reading 0 bytes. */
  320. i = read_to_buf(s, 0, buf, &eof);
  321. //test_eq(buf_capacity(buf), 512*1024);
  322. test_eq(buf_datalen(buf), 10);
  323. test_eq(eof, 0);
  324. test_eq(i, 0);
  325. /* Now test when buffer is filled exactly. */
  326. buf2 = buf_new_with_capacity(6);
  327. i = read_to_buf(s, 6, buf2, &eof);
  328. //test_eq(buf_capacity(buf2), 6);
  329. test_eq(buf_datalen(buf2), 6);
  330. test_eq(eof, 0);
  331. test_eq(i, 6);
  332. test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
  333. buf_free(buf2);
  334. buf2 = NULL;
  335. /* Now test when buffer is filled with more data to read. */
  336. buf2 = buf_new_with_capacity(32);
  337. i = read_to_buf(s, 128, buf2, &eof);
  338. //test_eq(buf_capacity(buf2), 128);
  339. test_eq(buf_datalen(buf2), 32);
  340. test_eq(eof, 0);
  341. test_eq(i, 32);
  342. buf_free(buf2);
  343. buf2 = NULL;
  344. /* Now read to eof. */
  345. test_assert(buf_capacity(buf) > 256);
  346. i = read_to_buf(s, 1024, buf, &eof);
  347. test_eq(i, (256-32-10-6));
  348. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  349. test_eq(buf_datalen(buf), 256-6-32);
  350. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
  351. test_eq(eof, 0);
  352. i = read_to_buf(s, 1024, buf, &eof);
  353. test_eq(i, 0);
  354. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  355. test_eq(buf_datalen(buf), 256-6-32);
  356. test_eq(eof, 1);
  357. }
  358. #endif
  359. done:
  360. if (buf)
  361. buf_free(buf);
  362. if (buf2)
  363. buf_free(buf2);
  364. }
  365. /** Run unit tests for the onion handshake code. */
  366. static void
  367. test_onion_handshake(void)
  368. {
  369. /* client-side */
  370. crypto_dh_env_t *c_dh = NULL;
  371. char c_buf[ONIONSKIN_CHALLENGE_LEN];
  372. char c_keys[40];
  373. /* server-side */
  374. char s_buf[ONIONSKIN_REPLY_LEN];
  375. char s_keys[40];
  376. /* shared */
  377. crypto_pk_env_t *pk = NULL;
  378. pk = pk_generate(0);
  379. /* client handshake 1. */
  380. memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
  381. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  382. /* server handshake */
  383. memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
  384. memset(s_keys, 0, 40);
  385. test_assert(! onion_skin_server_handshake(c_buf, pk, NULL,
  386. s_buf, s_keys, 40));
  387. /* client handshake 2 */
  388. memset(c_keys, 0, 40);
  389. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  390. if (memcmp(c_keys, s_keys, 40)) {
  391. puts("Aiiiie");
  392. exit(1);
  393. }
  394. test_memeq(c_keys, s_keys, 40);
  395. memset(s_buf, 0, 40);
  396. test_memneq(c_keys, s_buf, 40);
  397. done:
  398. if (c_dh)
  399. crypto_dh_free(c_dh);
  400. if (pk)
  401. crypto_free_pk_env(pk);
  402. }
  403. static void
  404. test_circuit_timeout(void)
  405. {
  406. /* Plan:
  407. * 1. Generate 1000 samples
  408. * 2. Estimate parameters
  409. * 3. If difference, repeat
  410. * 4. Save state
  411. * 5. load state
  412. * 6. Estimate parameters
  413. * 7. compare differences
  414. */
  415. circuit_build_times_t initial;
  416. circuit_build_times_t estimate;
  417. circuit_build_times_t final;
  418. double timeout1, timeout2;
  419. or_state_t state;
  420. char *msg;
  421. int i, runs;
  422. circuit_build_times_init(&initial);
  423. circuit_build_times_init(&estimate);
  424. circuit_build_times_init(&final);
  425. memset(&state, 0, sizeof(or_state_t));
  426. circuitbuild_running_unit_tests();
  427. #define timeout0 (build_time_t)(30*1000.0)
  428. initial.Xm = 750;
  429. circuit_build_times_initial_alpha(&initial, BUILDTIMEOUT_QUANTILE_CUTOFF,
  430. timeout0);
  431. do {
  432. int n = 0;
  433. for (i=0; i < MIN_CIRCUITS_TO_OBSERVE; i++) {
  434. if (circuit_build_times_add_time(&estimate,
  435. circuit_build_times_generate_sample(&initial, 0, 1)) == 0) {
  436. n++;
  437. }
  438. }
  439. circuit_build_times_update_alpha(&estimate);
  440. timeout1 = circuit_build_times_calculate_timeout(&estimate,
  441. BUILDTIMEOUT_QUANTILE_CUTOFF);
  442. circuit_build_times_set_timeout(&estimate);
  443. log_warn(LD_CIRC, "Timeout is %lf, Xm is %d", timeout1, estimate.Xm);
  444. /* XXX: 5% distribution error may not be the right metric */
  445. } while (fabs(circuit_build_times_cdf(&initial, timeout0) -
  446. circuit_build_times_cdf(&initial, timeout1)) > 0.05
  447. /* 5% error */
  448. && estimate.total_build_times < NCIRCUITS_TO_OBSERVE);
  449. test_assert(estimate.total_build_times < NCIRCUITS_TO_OBSERVE);
  450. circuit_build_times_update_state(&estimate, &state);
  451. test_assert(circuit_build_times_parse_state(&final, &state, &msg) == 0);
  452. circuit_build_times_update_alpha(&final);
  453. timeout2 = circuit_build_times_calculate_timeout(&final,
  454. BUILDTIMEOUT_QUANTILE_CUTOFF);
  455. circuit_build_times_set_timeout(&final);
  456. log_warn(LD_CIRC, "Timeout is %lf, Xm is %d", timeout2, final.Xm);
  457. test_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
  458. circuit_build_times_cdf(&initial, timeout2)) < 0.05);
  459. for (runs = 0; runs < 50; runs++) {
  460. int build_times_idx = 0;
  461. int total_build_times = 0;
  462. final.timeout_ms = BUILD_TIMEOUT_INITIAL_VALUE;
  463. estimate.timeout_ms = BUILD_TIMEOUT_INITIAL_VALUE;
  464. for (i = 0; i < RECENT_CIRCUITS*2; i++) {
  465. circuit_build_times_network_circ_success(&estimate);
  466. circuit_build_times_add_time(&estimate,
  467. circuit_build_times_generate_sample(&estimate, 0,
  468. BUILDTIMEOUT_QUANTILE_CUTOFF));
  469. estimate.have_computed_timeout = 1;
  470. circuit_build_times_network_circ_success(&estimate);
  471. circuit_build_times_add_time(&final,
  472. circuit_build_times_generate_sample(&final, 0,
  473. BUILDTIMEOUT_QUANTILE_CUTOFF));
  474. final.have_computed_timeout = 1;
  475. }
  476. test_assert(!circuit_build_times_network_check_changed(&estimate));
  477. test_assert(!circuit_build_times_network_check_changed(&final));
  478. /* Reset liveness to be non-live */
  479. final.liveness.network_last_live = 0;
  480. estimate.liveness.network_last_live = 0;
  481. build_times_idx = estimate.build_times_idx;
  482. total_build_times = estimate.total_build_times;
  483. for (i = 0; i < NETWORK_NONLIVE_TIMEOUT_COUNT; i++) {
  484. test_assert(circuit_build_times_network_check_live(&estimate));
  485. test_assert(circuit_build_times_network_check_live(&final));
  486. if (circuit_build_times_add_timeout(&estimate, 0,
  487. (time_t)(approx_time()-estimate.timeout_ms/1000.0-1)))
  488. estimate.have_computed_timeout = 1;
  489. if (circuit_build_times_add_timeout(&final, 0,
  490. (time_t)(approx_time()-final.timeout_ms/1000.0-1)))
  491. final.have_computed_timeout = 1;
  492. }
  493. test_assert(!circuit_build_times_network_check_live(&estimate));
  494. test_assert(!circuit_build_times_network_check_live(&final));
  495. for ( ; i < NETWORK_NONLIVE_DISCARD_COUNT; i++) {
  496. if (circuit_build_times_add_timeout(&estimate, 0,
  497. (time_t)(approx_time()-estimate.timeout_ms/1000.0-1)))
  498. estimate.have_computed_timeout = 1;
  499. if (i < NETWORK_NONLIVE_DISCARD_COUNT-1) {
  500. if (circuit_build_times_add_timeout(&final, 0,
  501. (time_t)(approx_time()-final.timeout_ms/1000.0-1)))
  502. final.have_computed_timeout = 1;
  503. }
  504. }
  505. test_assert(!circuit_build_times_network_check_live(&estimate));
  506. test_assert(!circuit_build_times_network_check_live(&final));
  507. log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
  508. build_times_idx, estimate.build_times_idx,
  509. total_build_times, estimate.total_build_times);
  510. /* Check rollback index. Should match top of loop. */
  511. test_assert(build_times_idx == estimate.build_times_idx);
  512. test_assert(total_build_times == estimate.total_build_times);
  513. /* Now simulate that the network has become live and we need
  514. * a change */
  515. circuit_build_times_network_is_live(&estimate);
  516. circuit_build_times_network_is_live(&final);
  517. for (i = 0; i < MAX_RECENT_TIMEOUT_COUNT; i++) {
  518. if (circuit_build_times_add_timeout(&estimate, 1, approx_time()-1))
  519. estimate.have_computed_timeout = 1;
  520. if (i < MAX_RECENT_TIMEOUT_COUNT-1) {
  521. if (circuit_build_times_add_timeout(&final, 1, approx_time()-1))
  522. final.have_computed_timeout = 1;
  523. }
  524. }
  525. test_assert(estimate.liveness.after_firsthop_idx == 0);
  526. test_assert(final.liveness.after_firsthop_idx ==
  527. MAX_RECENT_TIMEOUT_COUNT-1);
  528. test_assert(circuit_build_times_network_check_live(&estimate));
  529. test_assert(circuit_build_times_network_check_live(&final));
  530. if (circuit_build_times_add_timeout(&final, 1, approx_time()-1))
  531. final.have_computed_timeout = 1;
  532. }
  533. done:
  534. return;
  535. }
  536. /** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
  537. * that policies_summarize() produces the string <b>expected_summary</b> from
  538. * it. */
  539. static void
  540. test_policy_summary_helper(const char *policy_str,
  541. const char *expected_summary)
  542. {
  543. config_line_t line;
  544. smartlist_t *policy = smartlist_create();
  545. char *summary = NULL;
  546. int r;
  547. line.key = (char*)"foo";
  548. line.value = (char *)policy_str;
  549. line.next = NULL;
  550. r = policies_parse_exit_policy(&line, &policy, 0, NULL, 1);
  551. test_eq(r, 0);
  552. summary = policy_summarize(policy);
  553. test_assert(summary != NULL);
  554. test_streq(summary, expected_summary);
  555. done:
  556. tor_free(summary);
  557. if (policy)
  558. addr_policy_list_free(policy);
  559. }
  560. /** Run unit tests for generating summary lines of exit policies */
  561. static void
  562. test_policies(void)
  563. {
  564. int i;
  565. smartlist_t *policy = NULL, *policy2 = NULL;
  566. addr_policy_t *p;
  567. tor_addr_t tar;
  568. config_line_t line;
  569. smartlist_t *sm = NULL;
  570. char *policy_str = NULL;
  571. policy = smartlist_create();
  572. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
  573. test_assert(p != NULL);
  574. test_eq(ADDR_POLICY_REJECT, p->policy_type);
  575. tor_addr_from_ipv4h(&tar, 0xc0a80000u);
  576. test_eq(0, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
  577. test_eq(16, p->maskbits);
  578. test_eq(1, p->prt_min);
  579. test_eq(65535, p->prt_max);
  580. smartlist_add(policy, p);
  581. test_assert(ADDR_POLICY_ACCEPTED ==
  582. compare_addr_to_addr_policy(0x01020304u, 2, policy));
  583. test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  584. compare_addr_to_addr_policy(0, 2, policy));
  585. test_assert(ADDR_POLICY_REJECTED ==
  586. compare_addr_to_addr_policy(0xc0a80102, 2, policy));
  587. policy2 = NULL;
  588. test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, NULL, 1));
  589. test_assert(policy2);
  590. test_assert(!exit_policy_is_general_exit(policy));
  591. test_assert(exit_policy_is_general_exit(policy2));
  592. test_assert(!exit_policy_is_general_exit(NULL));
  593. test_assert(cmp_addr_policies(policy, policy2));
  594. test_assert(cmp_addr_policies(policy, NULL));
  595. test_assert(!cmp_addr_policies(policy2, policy2));
  596. test_assert(!cmp_addr_policies(NULL, NULL));
  597. test_assert(!policy_is_reject_star(policy2));
  598. test_assert(policy_is_reject_star(policy));
  599. test_assert(policy_is_reject_star(NULL));
  600. addr_policy_list_free(policy);
  601. policy = NULL;
  602. /* make sure compacting logic works. */
  603. policy = NULL;
  604. line.key = (char*)"foo";
  605. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  606. line.next = NULL;
  607. test_assert(0 == policies_parse_exit_policy(&line, &policy, 0, NULL, 1));
  608. test_assert(policy);
  609. //test_streq(policy->string, "accept *:80");
  610. //test_streq(policy->next->string, "reject *:*");
  611. test_eq(smartlist_len(policy), 2);
  612. /* test policy summaries */
  613. /* check if we properly ignore private IP addresses */
  614. test_policy_summary_helper("reject 192.168.0.0/16:*,"
  615. "reject 0.0.0.0/8:*,"
  616. "reject 10.0.0.0/8:*,"
  617. "accept *:10-30,"
  618. "accept *:90,"
  619. "reject *:*",
  620. "accept 10-30,90");
  621. /* check all accept policies, and proper counting of rejects */
  622. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  623. "reject 12.0.0.0/9:80,"
  624. "reject 13.0.0.0/9:80,"
  625. "reject 14.0.0.0/9:80,"
  626. "accept *:*", "accept 1-65535");
  627. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  628. "reject 12.0.0.0/9:80,"
  629. "reject 13.0.0.0/9:80,"
  630. "reject 14.0.0.0/9:80,"
  631. "reject 15.0.0.0:81,"
  632. "accept *:*", "accept 1-65535");
  633. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  634. "reject 12.0.0.0/9:80,"
  635. "reject 13.0.0.0/9:80,"
  636. "reject 14.0.0.0/9:80,"
  637. "reject 15.0.0.0:80,"
  638. "accept *:*",
  639. "reject 80");
  640. /* no exits */
  641. test_policy_summary_helper("accept 11.0.0.0/9:80,"
  642. "reject *:*",
  643. "reject 1-65535");
  644. /* port merging */
  645. test_policy_summary_helper("accept *:80,"
  646. "accept *:81,"
  647. "accept *:100-110,"
  648. "accept *:111,"
  649. "reject *:*",
  650. "accept 80-81,100-111");
  651. /* border ports */
  652. test_policy_summary_helper("accept *:1,"
  653. "accept *:3,"
  654. "accept *:65535,"
  655. "reject *:*",
  656. "accept 1,3,65535");
  657. /* holes */
  658. test_policy_summary_helper("accept *:1,"
  659. "accept *:3,"
  660. "accept *:5,"
  661. "accept *:7,"
  662. "reject *:*",
  663. "accept 1,3,5,7");
  664. test_policy_summary_helper("reject *:1,"
  665. "reject *:3,"
  666. "reject *:5,"
  667. "reject *:7,"
  668. "accept *:*",
  669. "reject 1,3,5,7");
  670. /* truncation ports */
  671. sm = smartlist_create();
  672. for (i=1; i<2000; i+=2) {
  673. char buf[POLICY_BUF_LEN];
  674. tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
  675. smartlist_add(sm, tor_strdup(buf));
  676. }
  677. smartlist_add(sm, tor_strdup("accept *:*"));
  678. policy_str = smartlist_join_strings(sm, ",", 0, NULL);
  679. test_policy_summary_helper( policy_str,
  680. "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
  681. "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
  682. "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
  683. "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
  684. "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
  685. "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
  686. "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
  687. "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
  688. "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
  689. "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
  690. "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
  691. "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
  692. "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
  693. "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
  694. done:
  695. if (policy)
  696. addr_policy_list_free(policy);
  697. if (policy2)
  698. addr_policy_list_free(policy2);
  699. tor_free(policy_str);
  700. if (sm) {
  701. SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
  702. smartlist_free(sm);
  703. }
  704. }
  705. /** Run AES performance benchmarks. */
  706. static void
  707. bench_aes(void)
  708. {
  709. int len, i;
  710. char *b1, *b2;
  711. crypto_cipher_env_t *c;
  712. struct timeval start, end;
  713. const int iters = 100000;
  714. uint64_t nsec;
  715. c = crypto_new_cipher_env();
  716. crypto_cipher_generate_key(c);
  717. crypto_cipher_encrypt_init_cipher(c);
  718. for (len = 1; len <= 8192; len *= 2) {
  719. b1 = tor_malloc_zero(len);
  720. b2 = tor_malloc_zero(len);
  721. tor_gettimeofday(&start);
  722. for (i = 0; i < iters; ++i) {
  723. crypto_cipher_encrypt(c, b1, b2, len);
  724. }
  725. tor_gettimeofday(&end);
  726. tor_free(b1);
  727. tor_free(b2);
  728. nsec = (uint64_t) tv_udiff(&start,&end);
  729. nsec *= 1000;
  730. nsec /= (iters*len);
  731. printf("%d bytes: "U64_FORMAT" nsec per byte\n", len,
  732. U64_PRINTF_ARG(nsec));
  733. }
  734. crypto_free_cipher_env(c);
  735. }
  736. /** Run digestmap_t performance benchmarks. */
  737. static void
  738. bench_dmap(void)
  739. {
  740. smartlist_t *sl = smartlist_create();
  741. smartlist_t *sl2 = smartlist_create();
  742. struct timeval start, end, pt2, pt3, pt4;
  743. const int iters = 10000;
  744. const int elts = 4000;
  745. const int fpostests = 1000000;
  746. char d[20];
  747. int i,n=0, fp = 0;
  748. digestmap_t *dm = digestmap_new();
  749. digestset_t *ds = digestset_new(elts);
  750. for (i = 0; i < elts; ++i) {
  751. crypto_rand(d, 20);
  752. smartlist_add(sl, tor_memdup(d, 20));
  753. }
  754. for (i = 0; i < elts; ++i) {
  755. crypto_rand(d, 20);
  756. smartlist_add(sl2, tor_memdup(d, 20));
  757. }
  758. printf("nbits=%d\n", ds->mask+1);
  759. tor_gettimeofday(&start);
  760. for (i = 0; i < iters; ++i) {
  761. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  762. }
  763. tor_gettimeofday(&pt2);
  764. for (i = 0; i < iters; ++i) {
  765. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
  766. SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  767. }
  768. tor_gettimeofday(&pt3);
  769. for (i = 0; i < iters; ++i) {
  770. SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  771. }
  772. tor_gettimeofday(&pt4);
  773. for (i = 0; i < iters; ++i) {
  774. SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_isin(ds, cp));
  775. SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_isin(ds, cp));
  776. }
  777. tor_gettimeofday(&end);
  778. for (i = 0; i < fpostests; ++i) {
  779. crypto_rand(d, 20);
  780. if (digestset_isin(ds, d)) ++fp;
  781. }
  782. printf("%ld\n",(unsigned long)tv_udiff(&start, &pt2));
  783. printf("%ld\n",(unsigned long)tv_udiff(&pt2, &pt3));
  784. printf("%ld\n",(unsigned long)tv_udiff(&pt3, &pt4));
  785. printf("%ld\n",(unsigned long)tv_udiff(&pt4, &end));
  786. printf("-- %d\n", n);
  787. printf("++ %f\n", fp/(double)fpostests);
  788. digestmap_free(dm, NULL);
  789. digestset_free(ds);
  790. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  791. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  792. smartlist_free(sl);
  793. smartlist_free(sl2);
  794. }
  795. /** Test encoding and parsing of rendezvous service descriptors. */
  796. static void
  797. test_rend_fns(void)
  798. {
  799. rend_service_descriptor_t *generated = NULL, *parsed = NULL;
  800. char service_id[DIGEST_LEN];
  801. char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
  802. const char *next_desc;
  803. smartlist_t *descs = smartlist_create();
  804. char computed_desc_id[DIGEST_LEN];
  805. char parsed_desc_id[DIGEST_LEN];
  806. crypto_pk_env_t *pk1 = NULL, *pk2 = NULL;
  807. time_t now;
  808. char *intro_points_encrypted = NULL;
  809. size_t intro_points_size;
  810. size_t encoded_size;
  811. int i;
  812. char address1[] = "fooaddress.onion";
  813. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  814. char address3[] = "fooaddress.exit";
  815. char address4[] = "www.torproject.org";
  816. test_assert(BAD_HOSTNAME == parse_extended_hostname(address1, 1));
  817. test_assert(ONION_HOSTNAME == parse_extended_hostname(address2, 1));
  818. test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3, 1));
  819. test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4, 1));
  820. pk1 = pk_generate(0);
  821. pk2 = pk_generate(1);
  822. generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  823. generated->pk = crypto_pk_dup_key(pk1);
  824. crypto_pk_get_digest(generated->pk, service_id);
  825. base32_encode(service_id_base32, REND_SERVICE_ID_LEN_BASE32+1,
  826. service_id, REND_SERVICE_ID_LEN);
  827. now = time(NULL);
  828. generated->timestamp = now;
  829. generated->version = 2;
  830. generated->protocols = 42;
  831. generated->intro_nodes = smartlist_create();
  832. for (i = 0; i < 3; i++) {
  833. rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
  834. crypto_pk_env_t *okey = pk_generate(2 + i);
  835. intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
  836. intro->extend_info->onion_key = okey;
  837. crypto_pk_get_digest(intro->extend_info->onion_key,
  838. intro->extend_info->identity_digest);
  839. //crypto_rand(info->identity_digest, DIGEST_LEN); /* Would this work? */
  840. intro->extend_info->nickname[0] = '$';
  841. base16_encode(intro->extend_info->nickname + 1,
  842. sizeof(intro->extend_info->nickname) - 1,
  843. intro->extend_info->identity_digest, DIGEST_LEN);
  844. /* Does not cover all IP addresses. */
  845. tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
  846. intro->extend_info->port = crypto_rand_int(65536);
  847. intro->intro_key = crypto_pk_dup_key(pk2);
  848. smartlist_add(generated->intro_nodes, intro);
  849. }
  850. test_assert(rend_encode_v2_descriptors(descs, generated, now, 0,
  851. REND_NO_AUTH, NULL, NULL) > 0);
  852. test_assert(rend_compute_v2_desc_id(computed_desc_id, service_id_base32,
  853. NULL, now, 0) == 0);
  854. test_memeq(((rend_encoded_v2_service_descriptor_t *)
  855. smartlist_get(descs, 0))->desc_id, computed_desc_id, DIGEST_LEN);
  856. test_assert(rend_parse_v2_service_descriptor(&parsed, parsed_desc_id,
  857. &intro_points_encrypted,
  858. &intro_points_size,
  859. &encoded_size,
  860. &next_desc,
  861. ((rend_encoded_v2_service_descriptor_t *)
  862. smartlist_get(descs, 0))->desc_str) == 0);
  863. test_assert(parsed);
  864. test_memeq(((rend_encoded_v2_service_descriptor_t *)
  865. smartlist_get(descs, 0))->desc_id, parsed_desc_id, DIGEST_LEN);
  866. test_eq(rend_parse_introduction_points(parsed, intro_points_encrypted,
  867. intro_points_size), 3);
  868. test_assert(!crypto_pk_cmp_keys(generated->pk, parsed->pk));
  869. test_eq(parsed->timestamp, now);
  870. test_eq(parsed->version, 2);
  871. test_eq(parsed->protocols, 42);
  872. test_eq(smartlist_len(parsed->intro_nodes), 3);
  873. for (i = 0; i < smartlist_len(parsed->intro_nodes); i++) {
  874. rend_intro_point_t *par_intro = smartlist_get(parsed->intro_nodes, i),
  875. *gen_intro = smartlist_get(generated->intro_nodes, i);
  876. extend_info_t *par_info = par_intro->extend_info;
  877. extend_info_t *gen_info = gen_intro->extend_info;
  878. test_assert(!crypto_pk_cmp_keys(gen_info->onion_key, par_info->onion_key));
  879. test_memeq(gen_info->identity_digest, par_info->identity_digest,
  880. DIGEST_LEN);
  881. test_streq(gen_info->nickname, par_info->nickname);
  882. test_assert(tor_addr_eq(&gen_info->addr, &par_info->addr));
  883. test_eq(gen_info->port, par_info->port);
  884. }
  885. rend_service_descriptor_free(parsed);
  886. rend_service_descriptor_free(generated);
  887. parsed = generated = NULL;
  888. done:
  889. if (descs) {
  890. for (i = 0; i < smartlist_len(descs); i++)
  891. rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
  892. smartlist_free(descs);
  893. }
  894. if (parsed)
  895. rend_service_descriptor_free(parsed);
  896. if (generated)
  897. rend_service_descriptor_free(generated);
  898. if (pk1)
  899. crypto_free_pk_env(pk1);
  900. if (pk2)
  901. crypto_free_pk_env(pk2);
  902. tor_free(intro_points_encrypted);
  903. }
  904. /** Run unit tests for GeoIP code. */
  905. static void
  906. test_geoip(void)
  907. {
  908. int i, j;
  909. time_t now = time(NULL);
  910. char *s = NULL;
  911. /* Populate the DB a bit. Add these in order, since we can't do the final
  912. * 'sort' step. These aren't very good IP addresses, but they're perfectly
  913. * fine uint32_t values. */
  914. test_eq(0, geoip_parse_entry("10,50,AB"));
  915. test_eq(0, geoip_parse_entry("52,90,XY"));
  916. test_eq(0, geoip_parse_entry("95,100,AB"));
  917. test_eq(0, geoip_parse_entry("\"105\",\"140\",\"ZZ\""));
  918. test_eq(0, geoip_parse_entry("\"150\",\"190\",\"XY\""));
  919. test_eq(0, geoip_parse_entry("\"200\",\"250\",\"AB\""));
  920. /* We should have 3 countries: ab, xy, zz. */
  921. test_eq(3, geoip_get_n_countries());
  922. /* Make sure that country ID actually works. */
  923. #define NAMEFOR(x) geoip_get_country_name(geoip_get_country_by_ip(x))
  924. test_streq("ab", NAMEFOR(32));
  925. test_streq("??", NAMEFOR(5));
  926. test_streq("??", NAMEFOR(51));
  927. test_streq("xy", NAMEFOR(150));
  928. test_streq("xy", NAMEFOR(190));
  929. test_streq("??", NAMEFOR(2000));
  930. #undef NAMEFOR
  931. get_options()->BridgeRelay = 1;
  932. get_options()->BridgeRecordUsageByCountry = 1;
  933. /* Put 9 observations in AB... */
  934. for (i=32; i < 40; ++i)
  935. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, i, now-7200);
  936. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, 225, now-7200);
  937. /* and 3 observations in XY, several times. */
  938. for (j=0; j < 10; ++j)
  939. for (i=52; i < 55; ++i)
  940. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, i, now-3600);
  941. /* and 17 observations in ZZ... */
  942. for (i=110; i < 127; ++i)
  943. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, i, now);
  944. s = geoip_get_client_history_bridge(now+5*24*60*60,
  945. GEOIP_CLIENT_CONNECT);
  946. test_assert(s);
  947. test_streq("zz=24,ab=16,xy=8", s);
  948. tor_free(s);
  949. /* Now clear out all the AB observations. */
  950. geoip_remove_old_clients(now-6000);
  951. s = geoip_get_client_history_bridge(now+5*24*60*60,
  952. GEOIP_CLIENT_CONNECT);
  953. test_assert(s);
  954. test_streq("zz=24,xy=8", s);
  955. done:
  956. tor_free(s);
  957. }
  958. static void *
  959. legacy_test_setup(const struct testcase_t *testcase)
  960. {
  961. return testcase->setup_data;
  962. }
  963. void
  964. legacy_test_helper(void *data)
  965. {
  966. void (*fn)(void) = data;
  967. fn();
  968. }
  969. static int
  970. legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
  971. {
  972. (void)ptr;
  973. (void)testcase;
  974. return 1;
  975. }
  976. const struct testcase_setup_t legacy_setup = {
  977. legacy_test_setup, legacy_test_cleanup
  978. };
  979. #define ENT(name) \
  980. { #name, legacy_test_helper, 0, &legacy_setup, test_ ## name }
  981. #define SUBENT(group, name) \
  982. { #group "_" #name, legacy_test_helper, 0, &legacy_setup, \
  983. test_ ## group ## _ ## name }
  984. #define DISABLED(name) \
  985. { #name, legacy_test_helper, TT_SKIP, &legacy_setup, name }
  986. static struct testcase_t test_array[] = {
  987. ENT(buffers),
  988. ENT(onion_handshake),
  989. ENT(circuit_timeout),
  990. ENT(policies),
  991. ENT(rend_fns),
  992. ENT(geoip),
  993. DISABLED(bench_aes),
  994. DISABLED(bench_dmap),
  995. END_OF_TESTCASES
  996. };
  997. extern struct testcase_t addr_tests[];
  998. extern struct testcase_t crypto_tests[];
  999. extern struct testcase_t container_tests[];
  1000. extern struct testcase_t util_tests[];
  1001. extern struct testcase_t dir_tests[];
  1002. static struct testgroup_t testgroups[] = {
  1003. { "", test_array },
  1004. { "addr/", addr_tests },
  1005. { "crypto/", crypto_tests },
  1006. { "container/", container_tests },
  1007. { "util/", util_tests },
  1008. { "dir/", dir_tests },
  1009. END_OF_GROUPS
  1010. };
  1011. /** Main entry point for unit test code: parse the command line, and run
  1012. * some unit tests. */
  1013. int
  1014. main(int c, const char **v)
  1015. {
  1016. or_options_t *options;
  1017. char *errmsg = NULL;
  1018. int i, i_out;
  1019. int loglevel = LOG_ERR;
  1020. #ifdef USE_DMALLOC
  1021. {
  1022. int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
  1023. tor_assert(r);
  1024. }
  1025. #endif
  1026. update_approx_time(time(NULL));
  1027. options = options_new();
  1028. tor_threads_init();
  1029. init_logging();
  1030. for (i_out = i = 1; i < c; ++i) {
  1031. if (!strcmp(v[i], "--warn")) {
  1032. loglevel = LOG_WARN;
  1033. } else if (!strcmp(v[i], "--notice")) {
  1034. loglevel = LOG_NOTICE;
  1035. } else if (!strcmp(v[i], "--info")) {
  1036. loglevel = LOG_INFO;
  1037. } else if (!strcmp(v[i], "--debug")) {
  1038. loglevel = LOG_DEBUG;
  1039. } else {
  1040. v[i_out++] = v[i];
  1041. }
  1042. }
  1043. c = i_out;
  1044. {
  1045. log_severity_list_t s;
  1046. memset(&s, 0, sizeof(s));
  1047. set_log_severity_config(loglevel, LOG_ERR, &s);
  1048. add_stream_log(&s, "", fileno(stdout));
  1049. }
  1050. options->command = CMD_RUN_UNITTESTS;
  1051. crypto_global_init(0, NULL, NULL);
  1052. rep_hist_init();
  1053. network_init();
  1054. setup_directory();
  1055. options_init(options);
  1056. options->DataDirectory = tor_strdup(temp_dir);
  1057. options->EntryStatistics = 1;
  1058. if (set_options(options, &errmsg) < 0) {
  1059. printf("Failed to set initial options: %s\n", errmsg);
  1060. tor_free(errmsg);
  1061. return 1;
  1062. }
  1063. crypto_seed_rng(1);
  1064. atexit(remove_directory);
  1065. have_failed = (tinytest_main(c, v, testgroups) != 0);
  1066. free_pregenerated_keys();
  1067. #ifdef USE_DMALLOC
  1068. tor_free_all(0);
  1069. dmalloc_log_unfreed();
  1070. #endif
  1071. if (have_failed)
  1072. return 1;
  1073. else
  1074. return 0;
  1075. }