test.c 40 KB

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