test.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  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. extern struct testcase_t controller_event_tests[];
  1432. extern struct testcase_t logging_tests[];
  1433. extern struct testcase_t backtrace_tests[];
  1434. static struct testgroup_t testgroups[] = {
  1435. { "", test_array },
  1436. { "buffer/", buffer_tests },
  1437. { "socks/", socks_tests },
  1438. { "addr/", addr_tests },
  1439. { "crypto/", crypto_tests },
  1440. { "container/", container_tests },
  1441. { "util/", util_tests },
  1442. { "util/logging/", logging_tests },
  1443. { "cellfmt/", cell_format_tests },
  1444. { "cellqueue/", cell_queue_tests },
  1445. { "dir/", dir_tests },
  1446. { "dir/md/", microdesc_tests },
  1447. { "pt/", pt_tests },
  1448. { "config/", config_tests },
  1449. { "replaycache/", replaycache_tests },
  1450. { "introduce/", introduce_tests },
  1451. { "circuitlist/", circuitlist_tests },
  1452. { "circuitmux/", circuitmux_tests },
  1453. { "options/", options_tests },
  1454. { "extorport/", extorport_tests },
  1455. { "control/", controller_event_tests },
  1456. END_OF_GROUPS
  1457. };
  1458. /** Main entry point for unit test code: parse the command line, and run
  1459. * some unit tests. */
  1460. int
  1461. main(int c, const char **v)
  1462. {
  1463. or_options_t *options;
  1464. char *errmsg = NULL;
  1465. int i, i_out;
  1466. int loglevel = LOG_ERR;
  1467. #ifdef USE_DMALLOC
  1468. {
  1469. int r = CRYPTO_set_mem_ex_functions(tor_malloc_, tor_realloc_, tor_free_);
  1470. tor_assert(r);
  1471. }
  1472. #endif
  1473. update_approx_time(time(NULL));
  1474. options = options_new();
  1475. tor_threads_init();
  1476. init_logging();
  1477. for (i_out = i = 1; i < c; ++i) {
  1478. if (!strcmp(v[i], "--warn")) {
  1479. loglevel = LOG_WARN;
  1480. } else if (!strcmp(v[i], "--notice")) {
  1481. loglevel = LOG_NOTICE;
  1482. } else if (!strcmp(v[i], "--info")) {
  1483. loglevel = LOG_INFO;
  1484. } else if (!strcmp(v[i], "--debug")) {
  1485. loglevel = LOG_DEBUG;
  1486. } else {
  1487. v[i_out++] = v[i];
  1488. }
  1489. }
  1490. c = i_out;
  1491. {
  1492. log_severity_list_t s;
  1493. memset(&s, 0, sizeof(s));
  1494. set_log_severity_config(loglevel, LOG_ERR, &s);
  1495. add_stream_log(&s, "", fileno(stdout));
  1496. }
  1497. options->command = CMD_RUN_UNITTESTS;
  1498. if (crypto_global_init(0, NULL, NULL)) {
  1499. printf("Can't initialize crypto subsystem; exiting.\n");
  1500. return 1;
  1501. }
  1502. crypto_set_tls_dh_prime(NULL);
  1503. crypto_seed_rng(1);
  1504. rep_hist_init();
  1505. network_init();
  1506. setup_directory();
  1507. options_init(options);
  1508. options->DataDirectory = tor_strdup(temp_dir);
  1509. options->EntryStatistics = 1;
  1510. if (set_options(options, &errmsg) < 0) {
  1511. printf("Failed to set initial options: %s\n", errmsg);
  1512. tor_free(errmsg);
  1513. return 1;
  1514. }
  1515. atexit(remove_directory);
  1516. have_failed = (tinytest_main(c, v, testgroups) != 0);
  1517. free_pregenerated_keys();
  1518. #ifdef USE_DMALLOC
  1519. tor_free_all(0);
  1520. dmalloc_log_unfreed();
  1521. #endif
  1522. if (have_failed)
  1523. return 1;
  1524. else
  1525. return 0;
  1526. }