test.c 57 KB

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