test.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, 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 _WIN32
  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 GEOIP_PRIVATE
  26. #define ROUTER_PRIVATE
  27. #define CIRCUITSTATS_PRIVATE
  28. /*
  29. * Linux doesn't provide lround in math.h by default, but mac os does...
  30. * It's best just to leave math.h out of the picture entirely.
  31. */
  32. //#include <math.h>
  33. long int lround(double x);
  34. double fabs(double x);
  35. #include "or.h"
  36. #include "buffers.h"
  37. #include "circuitlist.h"
  38. #include "circuitstats.h"
  39. #include "config.h"
  40. #include "connection_edge.h"
  41. #include "geoip.h"
  42. #include "rendcommon.h"
  43. #include "test.h"
  44. #include "torgzip.h"
  45. #include "mempool.h"
  46. #include "memarea.h"
  47. #include "onion.h"
  48. #include "onion_ntor.h"
  49. #include "onion_tap.h"
  50. #include "policies.h"
  51. #include "rephist.h"
  52. #include "routerparse.h"
  53. #ifdef CURVE25519_ENABLED
  54. #include "crypto_curve25519.h"
  55. #include "onion_ntor.h"
  56. #endif
  57. #ifdef USE_DMALLOC
  58. #include <dmalloc.h>
  59. #include <openssl/crypto.h>
  60. #include "main.h"
  61. #endif
  62. /** Set to true if any unit test has failed. Mostly, this is set by the macros
  63. * in test.h */
  64. int have_failed = 0;
  65. /** Temporary directory (set up by setup_directory) under which we store all
  66. * our files during testing. */
  67. static char temp_dir[256];
  68. #ifdef _WIN32
  69. #define pid_t int
  70. #endif
  71. static pid_t temp_dir_setup_in_pid = 0;
  72. /** Select and create the temporary directory we'll use to run our unit tests.
  73. * Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
  74. * idempotent. */
  75. static void
  76. setup_directory(void)
  77. {
  78. static int is_setup = 0;
  79. int r;
  80. char rnd[256], rnd32[256];
  81. if (is_setup) return;
  82. /* Due to base32 limitation needs to be a multiple of 5. */
  83. #define RAND_PATH_BYTES 5
  84. crypto_rand(rnd, RAND_PATH_BYTES);
  85. base32_encode(rnd32, sizeof(rnd32), rnd, RAND_PATH_BYTES);
  86. #ifdef _WIN32
  87. {
  88. char buf[MAX_PATH];
  89. const char *tmp = buf;
  90. /* If this fails, we're probably screwed anyway */
  91. if (!GetTempPathA(sizeof(buf),buf))
  92. tmp = "c:\\windows\\temp";
  93. tor_snprintf(temp_dir, sizeof(temp_dir),
  94. "%s\\tor_test_%d_%s", tmp, (int)getpid(), rnd32);
  95. r = mkdir(temp_dir);
  96. }
  97. #else
  98. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d_%s",
  99. (int) getpid(), rnd32);
  100. r = mkdir(temp_dir, 0700);
  101. #endif
  102. if (r) {
  103. fprintf(stderr, "Can't create directory %s:", temp_dir);
  104. perror("");
  105. exit(1);
  106. }
  107. is_setup = 1;
  108. temp_dir_setup_in_pid = getpid();
  109. }
  110. /** Return a filename relative to our testing temporary directory */
  111. const char *
  112. get_fname(const char *name)
  113. {
  114. static char buf[1024];
  115. setup_directory();
  116. if (!name)
  117. return temp_dir;
  118. tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
  119. return buf;
  120. }
  121. /* Remove a directory and all of its subdirectories */
  122. static void
  123. rm_rf(const char *dir)
  124. {
  125. struct stat st;
  126. smartlist_t *elements;
  127. elements = tor_listdir(dir);
  128. if (elements) {
  129. SMARTLIST_FOREACH_BEGIN(elements, const char *, cp) {
  130. char *tmp = NULL;
  131. tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
  132. if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
  133. rm_rf(tmp);
  134. } else {
  135. if (unlink(tmp)) {
  136. fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
  137. }
  138. }
  139. tor_free(tmp);
  140. } SMARTLIST_FOREACH_END(cp);
  141. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  142. smartlist_free(elements);
  143. }
  144. if (rmdir(dir))
  145. fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
  146. }
  147. /** Remove all files stored under the temporary directory, and the directory
  148. * itself. Called by atexit(). */
  149. static void
  150. remove_directory(void)
  151. {
  152. if (getpid() != temp_dir_setup_in_pid) {
  153. /* Only clean out the tempdir when the main process is exiting. */
  154. return;
  155. }
  156. rm_rf(temp_dir);
  157. }
  158. /** Define this if unit tests spend too much time generating public keys*/
  159. #undef CACHE_GENERATED_KEYS
  160. static crypto_pk_t *pregen_keys[5] = {NULL, NULL, NULL, NULL, NULL};
  161. #define N_PREGEN_KEYS ((int)(sizeof(pregen_keys)/sizeof(pregen_keys[0])))
  162. /** Generate and return a new keypair for use in unit tests. If we're using
  163. * the key cache optimization, we might reuse keys: we only guarantee that
  164. * keys made with distinct values for <b>idx</b> are different. The value of
  165. * <b>idx</b> must be at least 0, and less than N_PREGEN_KEYS. */
  166. crypto_pk_t *
  167. pk_generate(int idx)
  168. {
  169. #ifdef CACHE_GENERATED_KEYS
  170. tor_assert(idx < N_PREGEN_KEYS);
  171. if (! pregen_keys[idx]) {
  172. pregen_keys[idx] = crypto_pk_new();
  173. tor_assert(!crypto_pk_generate_key(pregen_keys[idx]));
  174. }
  175. return crypto_pk_dup_key(pregen_keys[idx]);
  176. #else
  177. crypto_pk_t *result;
  178. (void) idx;
  179. result = crypto_pk_new();
  180. tor_assert(!crypto_pk_generate_key(result));
  181. return result;
  182. #endif
  183. }
  184. /** Free all storage used for the cached key optimization. */
  185. static void
  186. free_pregenerated_keys(void)
  187. {
  188. unsigned idx;
  189. for (idx = 0; idx < N_PREGEN_KEYS; ++idx) {
  190. if (pregen_keys[idx]) {
  191. crypto_pk_free(pregen_keys[idx]);
  192. pregen_keys[idx] = NULL;
  193. }
  194. }
  195. }
  196. /** Run unit tests for the onion handshake code. */
  197. static void
  198. test_onion_handshake(void)
  199. {
  200. /* client-side */
  201. crypto_dh_t *c_dh = NULL;
  202. char c_buf[TAP_ONIONSKIN_CHALLENGE_LEN];
  203. char c_keys[40];
  204. /* server-side */
  205. char s_buf[TAP_ONIONSKIN_REPLY_LEN];
  206. char s_keys[40];
  207. int i;
  208. /* shared */
  209. crypto_pk_t *pk = NULL, *pk2 = NULL;
  210. pk = pk_generate(0);
  211. pk2 = pk_generate(1);
  212. /* client handshake 1. */
  213. memset(c_buf, 0, TAP_ONIONSKIN_CHALLENGE_LEN);
  214. test_assert(! onion_skin_TAP_create(pk, &c_dh, c_buf));
  215. for (i = 1; i <= 3; ++i) {
  216. crypto_pk_t *k1, *k2;
  217. if (i==1) {
  218. /* server handshake: only one key known. */
  219. k1 = pk; k2 = NULL;
  220. } else if (i==2) {
  221. /* server handshake: try the right key first. */
  222. k1 = pk; k2 = pk2;
  223. } else {
  224. /* server handshake: try the right key second. */
  225. k1 = pk2; k2 = pk;
  226. }
  227. memset(s_buf, 0, TAP_ONIONSKIN_REPLY_LEN);
  228. memset(s_keys, 0, 40);
  229. test_assert(! onion_skin_TAP_server_handshake(c_buf, k1, k2,
  230. s_buf, s_keys, 40));
  231. /* client handshake 2 */
  232. memset(c_keys, 0, 40);
  233. test_assert(! onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40));
  234. test_memeq(c_keys, s_keys, 40);
  235. memset(s_buf, 0, 40);
  236. test_memneq(c_keys, s_buf, 40);
  237. }
  238. done:
  239. crypto_dh_free(c_dh);
  240. crypto_pk_free(pk);
  241. crypto_pk_free(pk2);
  242. }
  243. static void
  244. test_bad_onion_handshake(void *arg)
  245. {
  246. char junk_buf[TAP_ONIONSKIN_CHALLENGE_LEN];
  247. char junk_buf2[TAP_ONIONSKIN_CHALLENGE_LEN];
  248. /* client-side */
  249. crypto_dh_t *c_dh = NULL;
  250. char c_buf[TAP_ONIONSKIN_CHALLENGE_LEN];
  251. char c_keys[40];
  252. /* server-side */
  253. char s_buf[TAP_ONIONSKIN_REPLY_LEN];
  254. char s_keys[40];
  255. /* shared */
  256. crypto_pk_t *pk = NULL, *pk2 = NULL;
  257. (void)arg;
  258. pk = pk_generate(0);
  259. pk2 = pk_generate(1);
  260. /* Server: Case 1: the encrypted data is degenerate. */
  261. memset(junk_buf, 0, sizeof(junk_buf));
  262. crypto_pk_public_hybrid_encrypt(pk, junk_buf2, TAP_ONIONSKIN_CHALLENGE_LEN,
  263. junk_buf, DH_KEY_LEN, PK_PKCS1_OAEP_PADDING, 1);
  264. tt_int_op(-1, ==,
  265. onion_skin_TAP_server_handshake(junk_buf2, pk, NULL,
  266. s_buf, s_keys, 40));
  267. /* Server: Case 2: the encrypted data is not long enough. */
  268. memset(junk_buf, 0, sizeof(junk_buf));
  269. memset(junk_buf2, 0, sizeof(junk_buf2));
  270. crypto_pk_public_encrypt(pk, junk_buf2, sizeof(junk_buf2),
  271. junk_buf, 48, PK_PKCS1_OAEP_PADDING);
  272. tt_int_op(-1, ==,
  273. onion_skin_TAP_server_handshake(junk_buf2, pk, NULL,
  274. s_buf, s_keys, 40));
  275. /* client handshake 1: do it straight. */
  276. memset(c_buf, 0, TAP_ONIONSKIN_CHALLENGE_LEN);
  277. test_assert(! onion_skin_TAP_create(pk, &c_dh, c_buf));
  278. /* Server: Case 3: we just don't have the right key. */
  279. tt_int_op(-1, ==,
  280. onion_skin_TAP_server_handshake(c_buf, pk2, NULL,
  281. s_buf, s_keys, 40));
  282. /* Server: Case 4: The RSA-encrypted portion is corrupt. */
  283. c_buf[64] ^= 33;
  284. tt_int_op(-1, ==,
  285. onion_skin_TAP_server_handshake(c_buf, pk, NULL,
  286. s_buf, s_keys, 40));
  287. c_buf[64] ^= 33;
  288. /* (Let the server procede) */
  289. tt_int_op(0, ==,
  290. onion_skin_TAP_server_handshake(c_buf, pk, NULL,
  291. s_buf, s_keys, 40));
  292. /* Client: Case 1: The server sent back junk. */
  293. s_buf[64] ^= 33;
  294. tt_int_op(-1, ==,
  295. onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40));
  296. s_buf[64] ^= 33;
  297. /* Let the client finish; make sure it can. */
  298. tt_int_op(0, ==,
  299. onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40));
  300. test_memeq(s_keys, c_keys, 40);
  301. /* Client: Case 2: The server sent back a degenerate DH. */
  302. memset(s_buf, 0, sizeof(s_buf));
  303. tt_int_op(-1, ==,
  304. onion_skin_TAP_client_handshake(c_dh, s_buf, c_keys, 40));
  305. done:
  306. crypto_dh_free(c_dh);
  307. crypto_pk_free(pk);
  308. crypto_pk_free(pk2);
  309. }
  310. #ifdef CURVE25519_ENABLED
  311. static void
  312. test_ntor_handshake(void *arg)
  313. {
  314. /* client-side */
  315. ntor_handshake_state_t *c_state = NULL;
  316. uint8_t c_buf[NTOR_ONIONSKIN_LEN];
  317. uint8_t c_keys[400];
  318. /* server-side */
  319. di_digest256_map_t *s_keymap=NULL;
  320. curve25519_keypair_t s_keypair;
  321. uint8_t s_buf[NTOR_REPLY_LEN];
  322. uint8_t s_keys[400];
  323. /* shared */
  324. const curve25519_public_key_t *server_pubkey;
  325. uint8_t node_id[20] = "abcdefghijklmnopqrst";
  326. (void) arg;
  327. /* Make the server some keys */
  328. curve25519_secret_key_generate(&s_keypair.seckey, 0);
  329. curve25519_public_key_generate(&s_keypair.pubkey, &s_keypair.seckey);
  330. dimap_add_entry(&s_keymap, s_keypair.pubkey.public_key, &s_keypair);
  331. server_pubkey = &s_keypair.pubkey;
  332. /* client handshake 1. */
  333. memset(c_buf, 0, NTOR_ONIONSKIN_LEN);
  334. tt_int_op(0, ==, onion_skin_ntor_create(node_id, server_pubkey,
  335. &c_state, c_buf));
  336. /* server handshake */
  337. memset(s_buf, 0, NTOR_REPLY_LEN);
  338. memset(s_keys, 0, 40);
  339. tt_int_op(0, ==, onion_skin_ntor_server_handshake(c_buf, s_keymap, NULL,
  340. node_id,
  341. s_buf, s_keys, 400));
  342. /* client handshake 2 */
  343. memset(c_keys, 0, 40);
  344. tt_int_op(0, ==, onion_skin_ntor_client_handshake(c_state, s_buf,
  345. c_keys, 400));
  346. test_memeq(c_keys, s_keys, 400);
  347. memset(s_buf, 0, 40);
  348. test_memneq(c_keys, s_buf, 40);
  349. done:
  350. ntor_handshake_state_free(c_state);
  351. dimap_free(s_keymap, NULL);
  352. }
  353. #endif
  354. /** Run unit tests for the onion queues. */
  355. static void
  356. test_onion_queues(void)
  357. {
  358. uint8_t buf1[TAP_ONIONSKIN_CHALLENGE_LEN] = {0};
  359. uint8_t buf2[NTOR_ONIONSKIN_LEN] = {0};
  360. or_circuit_t *circ1 = or_circuit_new(0, NULL);
  361. or_circuit_t *circ2 = or_circuit_new(0, NULL);
  362. create_cell_t *onionskin = NULL;
  363. create_cell_t *create1 = tor_malloc_zero(sizeof(create_cell_t));
  364. create_cell_t *create2 = tor_malloc_zero(sizeof(create_cell_t));
  365. create_cell_init(create1, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
  366. TAP_ONIONSKIN_CHALLENGE_LEN, buf1);
  367. create_cell_init(create2, CELL_CREATE, ONION_HANDSHAKE_TYPE_NTOR,
  368. NTOR_ONIONSKIN_LEN, buf2);
  369. test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
  370. test_eq(0, onion_pending_add(circ1, create1));
  371. test_eq(1, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
  372. test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
  373. test_eq(0, onion_pending_add(circ2, create2));
  374. test_eq(1, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
  375. test_eq_ptr(circ2, onion_next_task(&onionskin));
  376. test_eq(1, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
  377. test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
  378. clear_pending_onions();
  379. test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
  380. test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
  381. done:
  382. ;
  383. // circuit_free(circ1);
  384. // circuit_free(circ2);
  385. /* and free create1 and create2 */
  386. /* XXX leaks everything here */
  387. }
  388. static void
  389. test_circuit_timeout(void)
  390. {
  391. /* Plan:
  392. * 1. Generate 1000 samples
  393. * 2. Estimate parameters
  394. * 3. If difference, repeat
  395. * 4. Save state
  396. * 5. load state
  397. * 6. Estimate parameters
  398. * 7. compare differences
  399. */
  400. circuit_build_times_t initial;
  401. circuit_build_times_t estimate;
  402. circuit_build_times_t final;
  403. double timeout1, timeout2;
  404. or_state_t state;
  405. int i, runs;
  406. double close_ms;
  407. circuit_build_times_init(&initial);
  408. circuit_build_times_init(&estimate);
  409. circuit_build_times_init(&final);
  410. memset(&state, 0, sizeof(or_state_t));
  411. circuitbuild_running_unit_tests();
  412. #define timeout0 (build_time_t)(30*1000.0)
  413. initial.Xm = 3000;
  414. circuit_build_times_initial_alpha(&initial,
  415. CBT_DEFAULT_QUANTILE_CUTOFF/100.0,
  416. timeout0);
  417. close_ms = MAX(circuit_build_times_calculate_timeout(&initial,
  418. CBT_DEFAULT_CLOSE_QUANTILE/100.0),
  419. CBT_DEFAULT_TIMEOUT_INITIAL_VALUE);
  420. do {
  421. for (i=0; i < CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE; i++) {
  422. build_time_t sample = circuit_build_times_generate_sample(&initial,0,1);
  423. if (sample > close_ms) {
  424. circuit_build_times_add_time(&estimate, CBT_BUILD_ABANDONED);
  425. } else {
  426. circuit_build_times_add_time(&estimate, sample);
  427. }
  428. }
  429. circuit_build_times_update_alpha(&estimate);
  430. timeout1 = circuit_build_times_calculate_timeout(&estimate,
  431. CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
  432. circuit_build_times_set_timeout(&estimate);
  433. log_notice(LD_CIRC, "Timeout1 is %f, Xm is %d", timeout1, estimate.Xm);
  434. /* 2% error */
  435. } while (fabs(circuit_build_times_cdf(&initial, timeout0) -
  436. circuit_build_times_cdf(&initial, timeout1)) > 0.02);
  437. test_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
  438. circuit_build_times_update_state(&estimate, &state);
  439. test_assert(circuit_build_times_parse_state(&final, &state) == 0);
  440. circuit_build_times_update_alpha(&final);
  441. timeout2 = circuit_build_times_calculate_timeout(&final,
  442. CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
  443. circuit_build_times_set_timeout(&final);
  444. log_notice(LD_CIRC, "Timeout2 is %f, Xm is %d", timeout2, final.Xm);
  445. /* 5% here because some accuracy is lost due to histogram conversion */
  446. test_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
  447. circuit_build_times_cdf(&initial, timeout2)) < 0.05);
  448. for (runs = 0; runs < 50; runs++) {
  449. int build_times_idx = 0;
  450. int total_build_times = 0;
  451. final.close_ms = final.timeout_ms = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
  452. estimate.close_ms = estimate.timeout_ms
  453. = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
  454. for (i = 0; i < CBT_DEFAULT_RECENT_CIRCUITS*2; i++) {
  455. circuit_build_times_network_circ_success(&estimate);
  456. circuit_build_times_add_time(&estimate,
  457. circuit_build_times_generate_sample(&estimate, 0,
  458. CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
  459. circuit_build_times_network_circ_success(&estimate);
  460. circuit_build_times_add_time(&final,
  461. circuit_build_times_generate_sample(&final, 0,
  462. CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
  463. }
  464. test_assert(!circuit_build_times_network_check_changed(&estimate));
  465. test_assert(!circuit_build_times_network_check_changed(&final));
  466. /* Reset liveness to be non-live */
  467. final.liveness.network_last_live = 0;
  468. estimate.liveness.network_last_live = 0;
  469. build_times_idx = estimate.build_times_idx;
  470. total_build_times = estimate.total_build_times;
  471. test_assert(circuit_build_times_network_check_live(&estimate));
  472. test_assert(circuit_build_times_network_check_live(&final));
  473. circuit_build_times_count_close(&estimate, 0,
  474. (time_t)(approx_time()-estimate.close_ms/1000.0-1));
  475. circuit_build_times_count_close(&final, 0,
  476. (time_t)(approx_time()-final.close_ms/1000.0-1));
  477. test_assert(!circuit_build_times_network_check_live(&estimate));
  478. test_assert(!circuit_build_times_network_check_live(&final));
  479. log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
  480. build_times_idx, estimate.build_times_idx,
  481. total_build_times, estimate.total_build_times);
  482. /* Check rollback index. Should match top of loop. */
  483. test_assert(build_times_idx == estimate.build_times_idx);
  484. // This can fail if estimate.total_build_times == 1000, because
  485. // in that case, rewind actually causes us to lose timeouts
  486. if (total_build_times != CBT_NCIRCUITS_TO_OBSERVE)
  487. test_assert(total_build_times == estimate.total_build_times);
  488. /* Now simulate that the network has become live and we need
  489. * a change */
  490. circuit_build_times_network_is_live(&estimate);
  491. circuit_build_times_network_is_live(&final);
  492. for (i = 0; i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT; i++) {
  493. circuit_build_times_count_timeout(&estimate, 1);
  494. if (i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1) {
  495. circuit_build_times_count_timeout(&final, 1);
  496. }
  497. }
  498. test_assert(estimate.liveness.after_firsthop_idx == 0);
  499. test_assert(final.liveness.after_firsthop_idx ==
  500. CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1);
  501. test_assert(circuit_build_times_network_check_live(&estimate));
  502. test_assert(circuit_build_times_network_check_live(&final));
  503. circuit_build_times_count_timeout(&final, 1);
  504. }
  505. done:
  506. return;
  507. }
  508. /* Helper: assert that short_policy parses and writes back out as itself,
  509. or as <b>expected</b> if that's provided. */
  510. static void
  511. test_short_policy_parse(const char *input,
  512. const char *expected)
  513. {
  514. short_policy_t *short_policy = NULL;
  515. char *out = NULL;
  516. if (expected == NULL)
  517. expected = input;
  518. short_policy = parse_short_policy(input);
  519. tt_assert(short_policy);
  520. out = write_short_policy(short_policy);
  521. tt_str_op(out, ==, expected);
  522. done:
  523. tor_free(out);
  524. short_policy_free(short_policy);
  525. }
  526. /** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
  527. * that policies_summarize() produces the string <b>expected_summary</b> from
  528. * it. */
  529. static void
  530. test_policy_summary_helper(const char *policy_str,
  531. const char *expected_summary)
  532. {
  533. config_line_t line;
  534. smartlist_t *policy = smartlist_new();
  535. char *summary = NULL;
  536. char *summary_after = NULL;
  537. int r;
  538. short_policy_t *short_policy = NULL;
  539. line.key = (char*)"foo";
  540. line.value = (char *)policy_str;
  541. line.next = NULL;
  542. r = policies_parse_exit_policy(&line, &policy, 1, 0, NULL, 1);
  543. test_eq(r, 0);
  544. summary = policy_summarize(policy, AF_INET);
  545. test_assert(summary != NULL);
  546. test_streq(summary, expected_summary);
  547. short_policy = parse_short_policy(summary);
  548. tt_assert(short_policy);
  549. summary_after = write_short_policy(short_policy);
  550. test_streq(summary, summary_after);
  551. done:
  552. tor_free(summary_after);
  553. tor_free(summary);
  554. if (policy)
  555. addr_policy_list_free(policy);
  556. short_policy_free(short_policy);
  557. }
  558. /** Run unit tests for generating summary lines of exit policies */
  559. static void
  560. test_policies(void)
  561. {
  562. int i;
  563. smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
  564. *policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
  565. *policy7 = 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_new();
  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. tor_addr_from_ipv4h(&tar, 0x01020304u);
  582. test_assert(ADDR_POLICY_ACCEPTED ==
  583. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  584. tor_addr_make_unspec(&tar);
  585. test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  586. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  587. tor_addr_from_ipv4h(&tar, 0xc0a80102);
  588. test_assert(ADDR_POLICY_REJECTED ==
  589. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  590. test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, 1, NULL, 1));
  591. test_assert(policy2);
  592. policy3 = smartlist_new();
  593. p = router_parse_addr_policy_item_from_string("reject *:*",-1);
  594. test_assert(p != NULL);
  595. smartlist_add(policy3, p);
  596. p = router_parse_addr_policy_item_from_string("accept *:*",-1);
  597. test_assert(p != NULL);
  598. smartlist_add(policy3, p);
  599. policy4 = smartlist_new();
  600. p = router_parse_addr_policy_item_from_string("accept *:443",-1);
  601. test_assert(p != NULL);
  602. smartlist_add(policy4, p);
  603. p = router_parse_addr_policy_item_from_string("accept *:443",-1);
  604. test_assert(p != NULL);
  605. smartlist_add(policy4, p);
  606. policy5 = smartlist_new();
  607. p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*",-1);
  608. test_assert(p != NULL);
  609. smartlist_add(policy5, p);
  610. p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*",-1);
  611. test_assert(p != NULL);
  612. smartlist_add(policy5, p);
  613. p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*",-1);
  614. test_assert(p != NULL);
  615. smartlist_add(policy5, p);
  616. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
  617. test_assert(p != NULL);
  618. smartlist_add(policy5, p);
  619. p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*",-1);
  620. test_assert(p != NULL);
  621. smartlist_add(policy5, p);
  622. p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*",-1);
  623. test_assert(p != NULL);
  624. smartlist_add(policy5, p);
  625. p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*",-1);
  626. test_assert(p != NULL);
  627. smartlist_add(policy5, p);
  628. p = router_parse_addr_policy_item_from_string("reject *:1-65534",-1);
  629. test_assert(p != NULL);
  630. smartlist_add(policy5, p);
  631. p = router_parse_addr_policy_item_from_string("reject *:65535",-1);
  632. test_assert(p != NULL);
  633. smartlist_add(policy5, p);
  634. p = router_parse_addr_policy_item_from_string("accept *:1-65535",-1);
  635. test_assert(p != NULL);
  636. smartlist_add(policy5, p);
  637. policy6 = smartlist_new();
  638. p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*",-1);
  639. test_assert(p != NULL);
  640. smartlist_add(policy6, p);
  641. policy7 = smartlist_new();
  642. p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*",-1);
  643. test_assert(p != NULL);
  644. smartlist_add(policy7, p);
  645. test_assert(!exit_policy_is_general_exit(policy));
  646. test_assert(exit_policy_is_general_exit(policy2));
  647. test_assert(!exit_policy_is_general_exit(NULL));
  648. test_assert(!exit_policy_is_general_exit(policy3));
  649. test_assert(!exit_policy_is_general_exit(policy4));
  650. test_assert(!exit_policy_is_general_exit(policy5));
  651. test_assert(!exit_policy_is_general_exit(policy6));
  652. test_assert(!exit_policy_is_general_exit(policy7));
  653. test_assert(cmp_addr_policies(policy, policy2));
  654. test_assert(cmp_addr_policies(policy, NULL));
  655. test_assert(!cmp_addr_policies(policy2, policy2));
  656. test_assert(!cmp_addr_policies(NULL, NULL));
  657. test_assert(!policy_is_reject_star(policy2, AF_INET));
  658. test_assert(policy_is_reject_star(policy, AF_INET));
  659. test_assert(policy_is_reject_star(NULL, AF_INET));
  660. addr_policy_list_free(policy);
  661. policy = NULL;
  662. /* make sure compacting logic works. */
  663. policy = NULL;
  664. line.key = (char*)"foo";
  665. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  666. line.next = NULL;
  667. test_assert(0 == policies_parse_exit_policy(&line, &policy, 1, 0, NULL, 1));
  668. test_assert(policy);
  669. //test_streq(policy->string, "accept *:80");
  670. //test_streq(policy->next->string, "reject *:*");
  671. test_eq(smartlist_len(policy), 4);
  672. /* test policy summaries */
  673. /* check if we properly ignore private IP addresses */
  674. test_policy_summary_helper("reject 192.168.0.0/16:*,"
  675. "reject 0.0.0.0/8:*,"
  676. "reject 10.0.0.0/8:*,"
  677. "accept *:10-30,"
  678. "accept *:90,"
  679. "reject *:*",
  680. "accept 10-30,90");
  681. /* check all accept policies, and proper counting of rejects */
  682. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  683. "reject 12.0.0.0/9:80,"
  684. "reject 13.0.0.0/9:80,"
  685. "reject 14.0.0.0/9:80,"
  686. "accept *:*", "accept 1-65535");
  687. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  688. "reject 12.0.0.0/9:80,"
  689. "reject 13.0.0.0/9:80,"
  690. "reject 14.0.0.0/9:80,"
  691. "reject 15.0.0.0:81,"
  692. "accept *:*", "accept 1-65535");
  693. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  694. "reject 12.0.0.0/9:80,"
  695. "reject 13.0.0.0/9:80,"
  696. "reject 14.0.0.0/9:80,"
  697. "reject 15.0.0.0:80,"
  698. "accept *:*",
  699. "reject 80");
  700. /* no exits */
  701. test_policy_summary_helper("accept 11.0.0.0/9:80,"
  702. "reject *:*",
  703. "reject 1-65535");
  704. /* port merging */
  705. test_policy_summary_helper("accept *:80,"
  706. "accept *:81,"
  707. "accept *:100-110,"
  708. "accept *:111,"
  709. "reject *:*",
  710. "accept 80-81,100-111");
  711. /* border ports */
  712. test_policy_summary_helper("accept *:1,"
  713. "accept *:3,"
  714. "accept *:65535,"
  715. "reject *:*",
  716. "accept 1,3,65535");
  717. /* holes */
  718. test_policy_summary_helper("accept *:1,"
  719. "accept *:3,"
  720. "accept *:5,"
  721. "accept *:7,"
  722. "reject *:*",
  723. "accept 1,3,5,7");
  724. test_policy_summary_helper("reject *:1,"
  725. "reject *:3,"
  726. "reject *:5,"
  727. "reject *:7,"
  728. "accept *:*",
  729. "reject 1,3,5,7");
  730. /* Short policies with unrecognized formats should get accepted. */
  731. test_short_policy_parse("accept fred,2,3-5", "accept 2,3-5");
  732. test_short_policy_parse("accept 2,fred,3", "accept 2,3");
  733. test_short_policy_parse("accept 2,fred,3,bob", "accept 2,3");
  734. test_short_policy_parse("accept 2,-3,500-600", "accept 2,500-600");
  735. /* Short policies with nil entries are accepted too. */
  736. test_short_policy_parse("accept 1,,3", "accept 1,3");
  737. test_short_policy_parse("accept 100-200,,", "accept 100-200");
  738. test_short_policy_parse("reject ,1-10,,,,30-40", "reject 1-10,30-40");
  739. /* Try parsing various broken short policies */
  740. tt_ptr_op(NULL, ==, parse_short_policy("accept 200-199"));
  741. tt_ptr_op(NULL, ==, parse_short_policy(""));
  742. tt_ptr_op(NULL, ==, parse_short_policy("rejekt 1,2,3"));
  743. tt_ptr_op(NULL, ==, parse_short_policy("reject "));
  744. tt_ptr_op(NULL, ==, parse_short_policy("reject"));
  745. tt_ptr_op(NULL, ==, parse_short_policy("rej"));
  746. tt_ptr_op(NULL, ==, parse_short_policy("accept 2,3,100000"));
  747. tt_ptr_op(NULL, ==, parse_short_policy("accept 2,3x,4"));
  748. tt_ptr_op(NULL, ==, parse_short_policy("accept 2,3x,4"));
  749. tt_ptr_op(NULL, ==, parse_short_policy("accept 2-"));
  750. tt_ptr_op(NULL, ==, parse_short_policy("accept 2-x"));
  751. tt_ptr_op(NULL, ==, parse_short_policy("accept 1-,3"));
  752. tt_ptr_op(NULL, ==, parse_short_policy("accept 1-,3"));
  753. /* Test a too-long policy. */
  754. {
  755. int i;
  756. char *policy = NULL;
  757. smartlist_t *chunks = smartlist_new();
  758. smartlist_add(chunks, tor_strdup("accept "));
  759. for (i=1; i<10000; ++i)
  760. smartlist_add_asprintf(chunks, "%d,", i);
  761. smartlist_add(chunks, tor_strdup("20000"));
  762. policy = smartlist_join_strings(chunks, "", 0, NULL);
  763. SMARTLIST_FOREACH(chunks, char *, ch, tor_free(ch));
  764. smartlist_free(chunks);
  765. tt_ptr_op(NULL, ==, parse_short_policy(policy));/* shouldn't be accepted */
  766. tor_free(policy); /* could leak. */
  767. }
  768. /* truncation ports */
  769. sm = smartlist_new();
  770. for (i=1; i<2000; i+=2) {
  771. char buf[POLICY_BUF_LEN];
  772. tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
  773. smartlist_add(sm, tor_strdup(buf));
  774. }
  775. smartlist_add(sm, tor_strdup("accept *:*"));
  776. policy_str = smartlist_join_strings(sm, ",", 0, NULL);
  777. test_policy_summary_helper( policy_str,
  778. "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
  779. "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
  780. "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
  781. "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
  782. "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
  783. "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
  784. "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
  785. "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
  786. "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
  787. "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
  788. "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
  789. "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
  790. "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
  791. "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
  792. done:
  793. addr_policy_list_free(policy);
  794. addr_policy_list_free(policy2);
  795. addr_policy_list_free(policy3);
  796. addr_policy_list_free(policy4);
  797. addr_policy_list_free(policy5);
  798. addr_policy_list_free(policy6);
  799. addr_policy_list_free(policy7);
  800. tor_free(policy_str);
  801. if (sm) {
  802. SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
  803. smartlist_free(sm);
  804. }
  805. }
  806. /** Test encoding and parsing of rendezvous service descriptors. */
  807. static void
  808. test_rend_fns(void)
  809. {
  810. rend_service_descriptor_t *generated = NULL, *parsed = NULL;
  811. char service_id[DIGEST_LEN];
  812. char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
  813. const char *next_desc;
  814. smartlist_t *descs = smartlist_new();
  815. char computed_desc_id[DIGEST_LEN];
  816. char parsed_desc_id[DIGEST_LEN];
  817. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  818. time_t now;
  819. char *intro_points_encrypted = NULL;
  820. size_t intro_points_size;
  821. size_t encoded_size;
  822. int i;
  823. char address1[] = "fooaddress.onion";
  824. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  825. char address3[] = "fooaddress.exit";
  826. char address4[] = "www.torproject.org";
  827. char address5[] = "foo.abcdefghijklmnop.onion";
  828. char address6[] = "foo.bar.abcdefghijklmnop.onion";
  829. char address7[] = ".abcdefghijklmnop.onion";
  830. test_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
  831. test_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
  832. test_streq(address2, "aaaaaaaaaaaaaaaa");
  833. test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
  834. test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
  835. test_assert(ONION_HOSTNAME == parse_extended_hostname(address5));
  836. test_streq(address5, "abcdefghijklmnop");
  837. test_assert(ONION_HOSTNAME == parse_extended_hostname(address6));
  838. test_streq(address6, "abcdefghijklmnop");
  839. test_assert(BAD_HOSTNAME == parse_extended_hostname(address7));
  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_new();
  852. for (i = 0; i < 3; i++) {
  853. rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
  854. crypto_pk_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_pk_free(pk1);
  920. if (pk2)
  921. crypto_pk_free(pk2);
  922. tor_free(intro_points_encrypted);
  923. }
  924. /* Record odd numbered fake-IPs using ipv6, even numbered fake-IPs
  925. * using ipv4. Since our fake geoip database is the same between
  926. * ipv4 and ipv6, we should get the same result no matter which
  927. * address family we pick for each IP. */
  928. #define SET_TEST_ADDRESS(i) do { \
  929. if ((i) & 1) { \
  930. SET_TEST_IPV6(i); \
  931. tor_addr_from_in6(&addr, &in6); \
  932. } else { \
  933. tor_addr_from_ipv4h(&addr, (uint32_t) i); \
  934. } \
  935. } while (0)
  936. /* Make sure that country ID actually works. */
  937. #define SET_TEST_IPV6(i) \
  938. do { \
  939. set_uint32(in6.s6_addr + 12, htonl((uint32_t) (i))); \
  940. } while (0)
  941. #define CHECK_COUNTRY(country, val) do { \
  942. /* test ipv4 country lookup */ \
  943. test_streq(country, \
  944. geoip_get_country_name(geoip_get_country_by_ipv4(val))); \
  945. /* test ipv6 country lookup */ \
  946. SET_TEST_IPV6(val); \
  947. test_streq(country, \
  948. geoip_get_country_name(geoip_get_country_by_ipv6(&in6))); \
  949. } while (0)
  950. /** Run unit tests for GeoIP code. */
  951. static void
  952. test_geoip(void)
  953. {
  954. int i, j;
  955. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  956. char *s = NULL, *v = NULL;
  957. const char *bridge_stats_1 =
  958. "bridge-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  959. "bridge-ips zz=24,xy=8\n"
  960. "bridge-ip-versions v4=16,v6=16\n"
  961. "bridge-ip-transports <OR>=24\n",
  962. *dirreq_stats_1 =
  963. "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  964. "dirreq-v3-ips ab=8\n"
  965. "dirreq-v3-reqs ab=8\n"
  966. "dirreq-v3-resp ok=0,not-enough-sigs=0,unavailable=0,not-found=0,"
  967. "not-modified=0,busy=0\n"
  968. "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
  969. "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n",
  970. *dirreq_stats_2 =
  971. "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  972. "dirreq-v3-ips \n"
  973. "dirreq-v3-reqs \n"
  974. "dirreq-v3-resp ok=0,not-enough-sigs=0,unavailable=0,not-found=0,"
  975. "not-modified=0,busy=0\n"
  976. "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
  977. "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n",
  978. *dirreq_stats_3 =
  979. "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  980. "dirreq-v3-ips \n"
  981. "dirreq-v3-reqs \n"
  982. "dirreq-v3-resp ok=8,not-enough-sigs=0,unavailable=0,not-found=0,"
  983. "not-modified=0,busy=0\n"
  984. "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
  985. "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n",
  986. *dirreq_stats_4 =
  987. "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  988. "dirreq-v3-ips \n"
  989. "dirreq-v3-reqs \n"
  990. "dirreq-v3-resp ok=8,not-enough-sigs=0,unavailable=0,not-found=0,"
  991. "not-modified=0,busy=0\n"
  992. "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
  993. "dirreq-v3-tunneled-dl complete=0,timeout=0,running=4\n",
  994. *entry_stats_1 =
  995. "entry-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  996. "entry-ips ab=8\n",
  997. *entry_stats_2 =
  998. "entry-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  999. "entry-ips \n";
  1000. tor_addr_t addr;
  1001. struct in6_addr in6;
  1002. /* Populate the DB a bit. Add these in order, since we can't do the final
  1003. * 'sort' step. These aren't very good IP addresses, but they're perfectly
  1004. * fine uint32_t values. */
  1005. test_eq(0, geoip_parse_entry("10,50,AB", AF_INET));
  1006. test_eq(0, geoip_parse_entry("52,90,XY", AF_INET));
  1007. test_eq(0, geoip_parse_entry("95,100,AB", AF_INET));
  1008. test_eq(0, geoip_parse_entry("\"105\",\"140\",\"ZZ\"", AF_INET));
  1009. test_eq(0, geoip_parse_entry("\"150\",\"190\",\"XY\"", AF_INET));
  1010. test_eq(0, geoip_parse_entry("\"200\",\"250\",\"AB\"", AF_INET));
  1011. /* Populate the IPv6 DB equivalently with fake IPs in the same range */
  1012. test_eq(0, geoip_parse_entry("::a,::32,AB", AF_INET6));
  1013. test_eq(0, geoip_parse_entry("::34,::5a,XY", AF_INET6));
  1014. test_eq(0, geoip_parse_entry("::5f,::64,AB", AF_INET6));
  1015. test_eq(0, geoip_parse_entry("::69,::8c,ZZ", AF_INET6));
  1016. test_eq(0, geoip_parse_entry("::96,::be,XY", AF_INET6));
  1017. test_eq(0, geoip_parse_entry("::c8,::fa,AB", AF_INET6));
  1018. /* We should have 4 countries: ??, ab, xy, zz. */
  1019. test_eq(4, geoip_get_n_countries());
  1020. memset(&in6, 0, sizeof(in6));
  1021. CHECK_COUNTRY("??", 3);
  1022. CHECK_COUNTRY("ab", 32);
  1023. CHECK_COUNTRY("??", 5);
  1024. CHECK_COUNTRY("??", 51);
  1025. CHECK_COUNTRY("xy", 150);
  1026. CHECK_COUNTRY("xy", 190);
  1027. CHECK_COUNTRY("??", 2000);
  1028. test_eq(0, geoip_get_country_by_ipv4(3));
  1029. SET_TEST_IPV6(3);
  1030. test_eq(0, geoip_get_country_by_ipv6(&in6));
  1031. get_options_mutable()->BridgeRelay = 1;
  1032. get_options_mutable()->BridgeRecordUsageByCountry = 1;
  1033. /* Put 9 observations in AB... */
  1034. for (i=32; i < 40; ++i) {
  1035. SET_TEST_ADDRESS(i);
  1036. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now-7200);
  1037. }
  1038. SET_TEST_ADDRESS(225);
  1039. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now-7200);
  1040. /* and 3 observations in XY, several times. */
  1041. for (j=0; j < 10; ++j)
  1042. for (i=52; i < 55; ++i) {
  1043. SET_TEST_ADDRESS(i);
  1044. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now-3600);
  1045. }
  1046. /* and 17 observations in ZZ... */
  1047. for (i=110; i < 127; ++i) {
  1048. SET_TEST_ADDRESS(i);
  1049. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
  1050. }
  1051. geoip_get_client_history(GEOIP_CLIENT_CONNECT, &s, &v);
  1052. test_assert(s);
  1053. test_assert(v);
  1054. test_streq("zz=24,ab=16,xy=8", s);
  1055. test_streq("v4=16,v6=16", v);
  1056. tor_free(s);
  1057. tor_free(v);
  1058. /* Now clear out all the AB observations. */
  1059. geoip_remove_old_clients(now-6000);
  1060. geoip_get_client_history(GEOIP_CLIENT_CONNECT, &s, &v);
  1061. test_assert(s);
  1062. test_assert(v);
  1063. test_streq("zz=24,xy=8", s);
  1064. test_streq("v4=16,v6=16", v);
  1065. tor_free(s);
  1066. tor_free(v);
  1067. /* Start testing bridge statistics by making sure that we don't output
  1068. * bridge stats without initializing them. */
  1069. s = geoip_format_bridge_stats(now + 86400);
  1070. test_assert(!s);
  1071. /* Initialize stats and generate the bridge-stats history string out of
  1072. * the connecting clients added above. */
  1073. geoip_bridge_stats_init(now);
  1074. s = geoip_format_bridge_stats(now + 86400);
  1075. test_assert(s);
  1076. test_streq(bridge_stats_1, s);
  1077. tor_free(s);
  1078. /* Stop collecting bridge stats and make sure we don't write a history
  1079. * string anymore. */
  1080. geoip_bridge_stats_term();
  1081. s = geoip_format_bridge_stats(now + 86400);
  1082. test_assert(!s);
  1083. /* Stop being a bridge and start being a directory mirror that gathers
  1084. * directory request statistics. */
  1085. geoip_bridge_stats_term();
  1086. get_options_mutable()->BridgeRelay = 0;
  1087. get_options_mutable()->BridgeRecordUsageByCountry = 0;
  1088. get_options_mutable()->DirReqStatistics = 1;
  1089. /* Start testing dirreq statistics by making sure that we don't collect
  1090. * dirreq stats without initializing them. */
  1091. SET_TEST_ADDRESS(100);
  1092. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
  1093. s = geoip_format_dirreq_stats(now + 86400);
  1094. test_assert(!s);
  1095. /* Initialize stats, note one connecting client, and generate the
  1096. * dirreq-stats history string. */
  1097. geoip_dirreq_stats_init(now);
  1098. SET_TEST_ADDRESS(100);
  1099. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
  1100. s = geoip_format_dirreq_stats(now + 86400);
  1101. test_streq(dirreq_stats_1, s);
  1102. tor_free(s);
  1103. /* Stop collecting stats, add another connecting client, and ensure we
  1104. * don't generate a history string. */
  1105. geoip_dirreq_stats_term();
  1106. SET_TEST_ADDRESS(101);
  1107. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
  1108. s = geoip_format_dirreq_stats(now + 86400);
  1109. test_assert(!s);
  1110. /* Re-start stats, add a connecting client, reset stats, and make sure
  1111. * that we get an all empty history string. */
  1112. geoip_dirreq_stats_init(now);
  1113. SET_TEST_ADDRESS(100);
  1114. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, NULL, now);
  1115. geoip_reset_dirreq_stats(now);
  1116. s = geoip_format_dirreq_stats(now + 86400);
  1117. test_streq(dirreq_stats_2, s);
  1118. tor_free(s);
  1119. /* Note a successful network status response and make sure that it
  1120. * appears in the history string. */
  1121. geoip_note_ns_response(GEOIP_SUCCESS);
  1122. s = geoip_format_dirreq_stats(now + 86400);
  1123. test_streq(dirreq_stats_3, s);
  1124. tor_free(s);
  1125. /* Start a tunneled directory request. */
  1126. geoip_start_dirreq((uint64_t) 1, 1024, DIRREQ_TUNNELED);
  1127. s = geoip_format_dirreq_stats(now + 86400);
  1128. test_streq(dirreq_stats_4, s);
  1129. /* Stop collecting directory request statistics and start gathering
  1130. * entry stats. */
  1131. geoip_dirreq_stats_term();
  1132. get_options_mutable()->DirReqStatistics = 0;
  1133. get_options_mutable()->EntryStatistics = 1;
  1134. /* Start testing entry statistics by making sure that we don't collect
  1135. * anything without initializing entry stats. */
  1136. SET_TEST_ADDRESS(100);
  1137. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
  1138. s = geoip_format_entry_stats(now + 86400);
  1139. test_assert(!s);
  1140. /* Initialize stats, note one connecting client, and generate the
  1141. * entry-stats history string. */
  1142. geoip_entry_stats_init(now);
  1143. SET_TEST_ADDRESS(100);
  1144. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
  1145. s = geoip_format_entry_stats(now + 86400);
  1146. test_streq(entry_stats_1, s);
  1147. tor_free(s);
  1148. /* Stop collecting stats, add another connecting client, and ensure we
  1149. * don't generate a history string. */
  1150. geoip_entry_stats_term();
  1151. SET_TEST_ADDRESS(101);
  1152. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
  1153. s = geoip_format_entry_stats(now + 86400);
  1154. test_assert(!s);
  1155. /* Re-start stats, add a connecting client, reset stats, and make sure
  1156. * that we get an all empty history string. */
  1157. geoip_entry_stats_init(now);
  1158. SET_TEST_ADDRESS(100);
  1159. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now);
  1160. geoip_reset_entry_stats(now);
  1161. s = geoip_format_entry_stats(now + 86400);
  1162. test_streq(entry_stats_2, s);
  1163. tor_free(s);
  1164. /* Stop collecting entry statistics. */
  1165. geoip_entry_stats_term();
  1166. get_options_mutable()->EntryStatistics = 0;
  1167. done:
  1168. tor_free(s);
  1169. tor_free(v);
  1170. }
  1171. static void
  1172. test_geoip_with_pt(void)
  1173. {
  1174. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  1175. char *s = NULL;
  1176. int i;
  1177. tor_addr_t addr;
  1178. struct in6_addr in6;
  1179. get_options_mutable()->BridgeRelay = 1;
  1180. get_options_mutable()->BridgeRecordUsageByCountry = 1;
  1181. /* No clients seen yet. */
  1182. s = geoip_get_transport_history();
  1183. tor_assert(!s);
  1184. /* 4 connections without a pluggable transport */
  1185. for (i=0; i < 4; ++i) {
  1186. SET_TEST_ADDRESS(i);
  1187. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, NULL, now-7200);
  1188. }
  1189. /* 9 connections with "alpha" */
  1190. for (i=4; i < 13; ++i) {
  1191. SET_TEST_ADDRESS(i);
  1192. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "alpha", now-7200);
  1193. }
  1194. /* one connection with "beta" */
  1195. SET_TEST_ADDRESS(13);
  1196. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "beta", now-7200);
  1197. /* 14 connections with "charlie" */
  1198. for (i=14; i < 28; ++i) {
  1199. SET_TEST_ADDRESS(i);
  1200. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "charlie", now-7200);
  1201. }
  1202. /* 131 connections with "ddr" */
  1203. for (i=28; i < 159; ++i) {
  1204. SET_TEST_ADDRESS(i);
  1205. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "ddr", now-7200);
  1206. }
  1207. /* 8 connections with "entropy" */
  1208. for (i=159; i < 167; ++i) {
  1209. SET_TEST_ADDRESS(i);
  1210. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "entropy", now-7200);
  1211. }
  1212. /* 2 connections from the same IP with two different transports. */
  1213. SET_TEST_ADDRESS(++i);
  1214. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "fire", now-7200);
  1215. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, "google", now-7200);
  1216. /* Test the transport history string. */
  1217. s = geoip_get_transport_history();
  1218. tor_assert(s);
  1219. test_streq(s, "<OR>=8,alpha=16,beta=8,charlie=16,ddr=136,"
  1220. "entropy=8,fire=8,google=8");
  1221. /* Stop collecting entry statistics. */
  1222. geoip_entry_stats_term();
  1223. get_options_mutable()->EntryStatistics = 0;
  1224. done:
  1225. tor_free(s);
  1226. }
  1227. #undef SET_TEST_ADDRESS
  1228. #undef SET_TEST_IPV6
  1229. #undef CHECK_COUNTRY
  1230. /** Run unit tests for stats code. */
  1231. static void
  1232. test_stats(void)
  1233. {
  1234. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  1235. char *s = NULL;
  1236. int i;
  1237. /* Start with testing exit port statistics; we shouldn't collect exit
  1238. * stats without initializing them. */
  1239. rep_hist_note_exit_stream_opened(80);
  1240. rep_hist_note_exit_bytes(80, 100, 10000);
  1241. s = rep_hist_format_exit_stats(now + 86400);
  1242. test_assert(!s);
  1243. /* Initialize stats, note some streams and bytes, and generate history
  1244. * string. */
  1245. rep_hist_exit_stats_init(now);
  1246. rep_hist_note_exit_stream_opened(80);
  1247. rep_hist_note_exit_bytes(80, 100, 10000);
  1248. rep_hist_note_exit_stream_opened(443);
  1249. rep_hist_note_exit_bytes(443, 100, 10000);
  1250. rep_hist_note_exit_bytes(443, 100, 10000);
  1251. s = rep_hist_format_exit_stats(now + 86400);
  1252. test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1253. "exit-kibibytes-written 80=1,443=1,other=0\n"
  1254. "exit-kibibytes-read 80=10,443=20,other=0\n"
  1255. "exit-streams-opened 80=4,443=4,other=0\n", s);
  1256. tor_free(s);
  1257. /* Add a few bytes on 10 more ports and ensure that only the top 10
  1258. * ports are contained in the history string. */
  1259. for (i = 50; i < 60; i++) {
  1260. rep_hist_note_exit_bytes(i, i, i);
  1261. rep_hist_note_exit_stream_opened(i);
  1262. }
  1263. s = rep_hist_format_exit_stats(now + 86400);
  1264. test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1265. "exit-kibibytes-written 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
  1266. "59=1,80=1,443=1,other=1\n"
  1267. "exit-kibibytes-read 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
  1268. "59=1,80=10,443=20,other=1\n"
  1269. "exit-streams-opened 52=4,53=4,54=4,55=4,56=4,57=4,58=4,"
  1270. "59=4,80=4,443=4,other=4\n", s);
  1271. tor_free(s);
  1272. /* Stop collecting stats, add some bytes, and ensure we don't generate
  1273. * a history string. */
  1274. rep_hist_exit_stats_term();
  1275. rep_hist_note_exit_bytes(80, 100, 10000);
  1276. s = rep_hist_format_exit_stats(now + 86400);
  1277. test_assert(!s);
  1278. /* Re-start stats, add some bytes, reset stats, and see what history we
  1279. * get when observing no streams or bytes at all. */
  1280. rep_hist_exit_stats_init(now);
  1281. rep_hist_note_exit_stream_opened(80);
  1282. rep_hist_note_exit_bytes(80, 100, 10000);
  1283. rep_hist_reset_exit_stats(now);
  1284. s = rep_hist_format_exit_stats(now + 86400);
  1285. test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1286. "exit-kibibytes-written other=0\n"
  1287. "exit-kibibytes-read other=0\n"
  1288. "exit-streams-opened other=0\n", s);
  1289. tor_free(s);
  1290. /* Continue with testing connection statistics; we shouldn't collect
  1291. * conn stats without initializing them. */
  1292. rep_hist_note_or_conn_bytes(1, 20, 400, now);
  1293. s = rep_hist_format_conn_stats(now + 86400);
  1294. test_assert(!s);
  1295. /* Initialize stats, note bytes, and generate history string. */
  1296. rep_hist_conn_stats_init(now);
  1297. rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
  1298. rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
  1299. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
  1300. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  1301. s = rep_hist_format_conn_stats(now + 86400);
  1302. test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n", s);
  1303. tor_free(s);
  1304. /* Stop collecting stats, add some bytes, and ensure we don't generate
  1305. * a history string. */
  1306. rep_hist_conn_stats_term();
  1307. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  1308. s = rep_hist_format_conn_stats(now + 86400);
  1309. test_assert(!s);
  1310. /* Re-start stats, add some bytes, reset stats, and see what history we
  1311. * get when observing no bytes at all. */
  1312. rep_hist_conn_stats_init(now);
  1313. rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
  1314. rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
  1315. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
  1316. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  1317. rep_hist_reset_conn_stats(now);
  1318. s = rep_hist_format_conn_stats(now + 86400);
  1319. test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n", s);
  1320. tor_free(s);
  1321. /* Continue with testing buffer statistics; we shouldn't collect buffer
  1322. * stats without initializing them. */
  1323. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  1324. s = rep_hist_format_buffer_stats(now + 86400);
  1325. test_assert(!s);
  1326. /* Initialize stats, add statistics for a single circuit, and generate
  1327. * the history string. */
  1328. rep_hist_buffer_stats_init(now);
  1329. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  1330. s = rep_hist_format_buffer_stats(now + 86400);
  1331. test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1332. "cell-processed-cells 20,0,0,0,0,0,0,0,0,0\n"
  1333. "cell-queued-cells 2.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
  1334. "0.00,0.00\n"
  1335. "cell-time-in-queue 2,0,0,0,0,0,0,0,0,0\n"
  1336. "cell-circuits-per-decile 1\n", s);
  1337. tor_free(s);
  1338. /* Add nineteen more circuit statistics to the one that's already in the
  1339. * history to see that the math works correctly. */
  1340. for (i = 21; i < 30; i++)
  1341. rep_hist_add_buffer_stats(2.0, 2.0, i);
  1342. for (i = 20; i < 30; i++)
  1343. rep_hist_add_buffer_stats(3.5, 3.5, i);
  1344. s = rep_hist_format_buffer_stats(now + 86400);
  1345. test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1346. "cell-processed-cells 29,28,27,26,25,24,23,22,21,20\n"
  1347. "cell-queued-cells 2.75,2.75,2.75,2.75,2.75,2.75,2.75,2.75,"
  1348. "2.75,2.75\n"
  1349. "cell-time-in-queue 3,3,3,3,3,3,3,3,3,3\n"
  1350. "cell-circuits-per-decile 2\n", s);
  1351. tor_free(s);
  1352. /* Stop collecting stats, add statistics for one circuit, and ensure we
  1353. * don't generate a history string. */
  1354. rep_hist_buffer_stats_term();
  1355. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  1356. s = rep_hist_format_buffer_stats(now + 86400);
  1357. test_assert(!s);
  1358. /* Re-start stats, add statistics for one circuit, reset stats, and make
  1359. * sure that the history has all zeros. */
  1360. rep_hist_buffer_stats_init(now);
  1361. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  1362. rep_hist_reset_buffer_stats(now);
  1363. s = rep_hist_format_buffer_stats(now + 86400);
  1364. test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1365. "cell-processed-cells 0,0,0,0,0,0,0,0,0,0\n"
  1366. "cell-queued-cells 0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
  1367. "0.00,0.00\n"
  1368. "cell-time-in-queue 0,0,0,0,0,0,0,0,0,0\n"
  1369. "cell-circuits-per-decile 0\n", s);
  1370. done:
  1371. tor_free(s);
  1372. }
  1373. static void *
  1374. legacy_test_setup(const struct testcase_t *testcase)
  1375. {
  1376. return testcase->setup_data;
  1377. }
  1378. void
  1379. legacy_test_helper(void *data)
  1380. {
  1381. void (*fn)(void) = data;
  1382. fn();
  1383. }
  1384. static int
  1385. legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
  1386. {
  1387. (void)ptr;
  1388. (void)testcase;
  1389. return 1;
  1390. }
  1391. const struct testcase_setup_t legacy_setup = {
  1392. legacy_test_setup, legacy_test_cleanup
  1393. };
  1394. #define ENT(name) \
  1395. { #name, legacy_test_helper, 0, &legacy_setup, test_ ## name }
  1396. #define FORK(name) \
  1397. { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_ ## name }
  1398. static struct testcase_t test_array[] = {
  1399. ENT(onion_handshake),
  1400. { "bad_onion_handshake", test_bad_onion_handshake, 0, NULL, NULL },
  1401. ENT(onion_queues),
  1402. #ifdef CURVE25519_ENABLED
  1403. { "ntor_handshake", test_ntor_handshake, 0, NULL, NULL },
  1404. #endif
  1405. ENT(circuit_timeout),
  1406. ENT(policies),
  1407. ENT(rend_fns),
  1408. ENT(geoip),
  1409. FORK(geoip_with_pt),
  1410. FORK(stats),
  1411. END_OF_TESTCASES
  1412. };
  1413. extern struct testcase_t addr_tests[];
  1414. extern struct testcase_t buffer_tests[];
  1415. extern struct testcase_t crypto_tests[];
  1416. extern struct testcase_t container_tests[];
  1417. extern struct testcase_t util_tests[];
  1418. extern struct testcase_t dir_tests[];
  1419. extern struct testcase_t microdesc_tests[];
  1420. extern struct testcase_t pt_tests[];
  1421. extern struct testcase_t config_tests[];
  1422. extern struct testcase_t introduce_tests[];
  1423. extern struct testcase_t replaycache_tests[];
  1424. extern struct testcase_t cell_format_tests[];
  1425. extern struct testcase_t circuitlist_tests[];
  1426. extern struct testcase_t circuitmux_tests[];
  1427. extern struct testcase_t cell_queue_tests[];
  1428. extern struct testcase_t options_tests[];
  1429. extern struct testcase_t socks_tests[];
  1430. extern struct testcase_t extorport_tests[];
  1431. static struct testgroup_t testgroups[] = {
  1432. { "", test_array },
  1433. { "buffer/", buffer_tests },
  1434. { "socks/", socks_tests },
  1435. { "addr/", addr_tests },
  1436. { "crypto/", crypto_tests },
  1437. { "container/", container_tests },
  1438. { "util/", util_tests },
  1439. { "cellfmt/", cell_format_tests },
  1440. { "cellqueue/", cell_queue_tests },
  1441. { "dir/", dir_tests },
  1442. { "dir/md/", microdesc_tests },
  1443. { "pt/", pt_tests },
  1444. { "config/", config_tests },
  1445. { "replaycache/", replaycache_tests },
  1446. { "introduce/", introduce_tests },
  1447. { "circuitlist/", circuitlist_tests },
  1448. { "circuitmux/", circuitmux_tests },
  1449. { "options/", options_tests },
  1450. { "extorport/", extorport_tests },
  1451. END_OF_GROUPS
  1452. };
  1453. /** Main entry point for unit test code: parse the command line, and run
  1454. * some unit tests. */
  1455. int
  1456. main(int c, const char **v)
  1457. {
  1458. or_options_t *options;
  1459. char *errmsg = NULL;
  1460. int i, i_out;
  1461. int loglevel = LOG_ERR;
  1462. #ifdef USE_DMALLOC
  1463. {
  1464. int r = CRYPTO_set_mem_ex_functions(tor_malloc_, tor_realloc_, tor_free_);
  1465. tor_assert(r);
  1466. }
  1467. #endif
  1468. update_approx_time(time(NULL));
  1469. options = options_new();
  1470. tor_threads_init();
  1471. init_logging();
  1472. for (i_out = i = 1; i < c; ++i) {
  1473. if (!strcmp(v[i], "--warn")) {
  1474. loglevel = LOG_WARN;
  1475. } else if (!strcmp(v[i], "--notice")) {
  1476. loglevel = LOG_NOTICE;
  1477. } else if (!strcmp(v[i], "--info")) {
  1478. loglevel = LOG_INFO;
  1479. } else if (!strcmp(v[i], "--debug")) {
  1480. loglevel = LOG_DEBUG;
  1481. } else {
  1482. v[i_out++] = v[i];
  1483. }
  1484. }
  1485. c = i_out;
  1486. {
  1487. log_severity_list_t s;
  1488. memset(&s, 0, sizeof(s));
  1489. set_log_severity_config(loglevel, LOG_ERR, &s);
  1490. add_stream_log(&s, "", fileno(stdout));
  1491. }
  1492. options->command = CMD_RUN_UNITTESTS;
  1493. if (crypto_global_init(0, NULL, NULL)) {
  1494. printf("Can't initialize crypto subsystem; exiting.\n");
  1495. return 1;
  1496. }
  1497. crypto_set_tls_dh_prime(NULL);
  1498. crypto_seed_rng(1);
  1499. rep_hist_init();
  1500. network_init();
  1501. setup_directory();
  1502. options_init(options);
  1503. options->DataDirectory = tor_strdup(temp_dir);
  1504. options->EntryStatistics = 1;
  1505. if (set_options(options, &errmsg) < 0) {
  1506. printf("Failed to set initial options: %s\n", errmsg);
  1507. tor_free(errmsg);
  1508. return 1;
  1509. }
  1510. atexit(remove_directory);
  1511. have_failed = (tinytest_main(c, v, testgroups) != 0);
  1512. free_pregenerated_keys();
  1513. #ifdef USE_DMALLOC
  1514. tor_free_all(0);
  1515. dmalloc_log_unfreed();
  1516. #endif
  1517. if (have_failed)
  1518. return 1;
  1519. else
  1520. return 0;
  1521. }