test.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2010, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* Ordinarily defined in tor_main.c; this bit is just here to provide one
  6. * since we're not linking to tor_main.c */
  7. const char tor_git_revision[] = "";
  8. /**
  9. * \file test.c
  10. * \brief Unit tests for many pieces of the lower level Tor modules.
  11. **/
  12. #include "orconfig.h"
  13. #include <stdio.h>
  14. #ifdef HAVE_FCNTL_H
  15. #include <fcntl.h>
  16. #endif
  17. #ifdef MS_WINDOWS
  18. /* For mkdir() */
  19. #include <direct.h>
  20. #else
  21. #include <dirent.h>
  22. #endif
  23. /* These macros pull in declarations for some functions and structures that
  24. * are typically file-private. */
  25. #define BUFFERS_PRIVATE
  26. #define CONFIG_PRIVATE
  27. #define GEOIP_PRIVATE
  28. #define ROUTER_PRIVATE
  29. #define CIRCUIT_PRIVATE
  30. /*
  31. * Linux doesn't provide lround in math.h by default, but mac os does...
  32. * It's best just to leave math.h out of the picture entirely.
  33. */
  34. //#include <math.h>
  35. long int lround(double x);
  36. double fabs(double x);
  37. #include "or.h"
  38. #include "buffers.h"
  39. #include "circuitbuild.h"
  40. #include "config.h"
  41. #include "connection_edge.h"
  42. #include "geoip.h"
  43. #include "rendcommon.h"
  44. #include "test.h"
  45. #include "torgzip.h"
  46. #include "mempool.h"
  47. #include "memarea.h"
  48. #include "onion.h"
  49. #include "policies.h"
  50. #include "rephist.h"
  51. #include "routerparse.h"
  52. #ifdef USE_DMALLOC
  53. #include <dmalloc.h>
  54. #include <openssl/crypto.h>
  55. #include "main.h"
  56. #endif
  57. /** Set to true if any unit test has failed. Mostly, this is set by the macros
  58. * in test.h */
  59. int have_failed = 0;
  60. /** Temporary directory (set up by setup_directory) under which we store all
  61. * our files during testing. */
  62. static char temp_dir[256];
  63. static pid_t temp_dir_setup_in_pid = 0;
  64. /** Select and create the temporary directory we'll use to run our unit tests.
  65. * Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
  66. * idempotent. */
  67. static void
  68. setup_directory(void)
  69. {
  70. static int is_setup = 0;
  71. int r;
  72. if (is_setup) return;
  73. #ifdef MS_WINDOWS
  74. // XXXX
  75. tor_snprintf(temp_dir, sizeof(temp_dir),
  76. "c:\\windows\\temp\\tor_test_%d", (int)getpid());
  77. r = mkdir(temp_dir);
  78. #else
  79. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
  80. r = mkdir(temp_dir, 0700);
  81. #endif
  82. if (r) {
  83. fprintf(stderr, "Can't create directory %s:", temp_dir);
  84. perror("");
  85. exit(1);
  86. }
  87. is_setup = 1;
  88. temp_dir_setup_in_pid = getpid();
  89. }
  90. /** Return a filename relative to our testing temporary directory */
  91. const char *
  92. get_fname(const char *name)
  93. {
  94. static char buf[1024];
  95. setup_directory();
  96. if (!name)
  97. return temp_dir;
  98. tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
  99. return buf;
  100. }
  101. /* Remove a directory and all of its subdirectories */
  102. static void
  103. rm_rf(const char *dir)
  104. {
  105. struct stat st;
  106. smartlist_t *elements;
  107. elements = tor_listdir(dir);
  108. if (elements) {
  109. SMARTLIST_FOREACH(elements, const char *, cp,
  110. {
  111. char *tmp = NULL;
  112. tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
  113. if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
  114. rm_rf(tmp);
  115. } else {
  116. if (unlink(tmp)) {
  117. fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
  118. }
  119. }
  120. tor_free(tmp);
  121. });
  122. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  123. smartlist_free(elements);
  124. }
  125. if (rmdir(dir))
  126. fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
  127. }
  128. /** Remove all files stored under the temporary directory, and the directory
  129. * itself. Called by atexit(). */
  130. static void
  131. remove_directory(void)
  132. {
  133. if (getpid() != temp_dir_setup_in_pid) {
  134. /* Only clean out the tempdir when the main process is exiting. */
  135. return;
  136. }
  137. rm_rf(temp_dir);
  138. }
  139. /** Define this if unit tests spend too much time generating public keys*/
  140. #undef CACHE_GENERATED_KEYS
  141. static crypto_pk_env_t *pregen_keys[5] = {NULL, NULL, NULL, NULL, NULL};
  142. #define N_PREGEN_KEYS ((int)(sizeof(pregen_keys)/sizeof(pregen_keys[0])))
  143. /** Generate and return a new keypair for use in unit tests. If we're using
  144. * the key cache optimization, we might reuse keys: we only guarantee that
  145. * keys made with distinct values for <b>idx</b> are different. The value of
  146. * <b>idx</b> must be at least 0, and less than N_PREGEN_KEYS. */
  147. crypto_pk_env_t *
  148. pk_generate(int idx)
  149. {
  150. #ifdef CACHE_GENERATED_KEYS
  151. tor_assert(idx < N_PREGEN_KEYS);
  152. if (! pregen_keys[idx]) {
  153. pregen_keys[idx] = crypto_new_pk_env();
  154. tor_assert(!crypto_pk_generate_key(pregen_keys[idx]));
  155. }
  156. return crypto_pk_dup_key(pregen_keys[idx]);
  157. #else
  158. crypto_pk_env_t *result;
  159. (void) idx;
  160. result = crypto_new_pk_env();
  161. tor_assert(!crypto_pk_generate_key(result));
  162. return result;
  163. #endif
  164. }
  165. /** Free all storage used for the cached key optimization. */
  166. static void
  167. free_pregenerated_keys(void)
  168. {
  169. unsigned idx;
  170. for (idx = 0; idx < N_PREGEN_KEYS; ++idx) {
  171. if (pregen_keys[idx]) {
  172. crypto_free_pk_env(pregen_keys[idx]);
  173. pregen_keys[idx] = NULL;
  174. }
  175. }
  176. }
  177. typedef struct socks_test_data_t {
  178. socks_request_t *req;
  179. buf_t *buf;
  180. } socks_test_data_t;
  181. static void *
  182. socks_test_setup(const struct testcase_t *testcase)
  183. {
  184. socks_test_data_t *data = tor_malloc(sizeof(socks_test_data_t));
  185. (void)testcase;
  186. data->buf = buf_new_with_capacity(256);
  187. data->req = socks_request_new();
  188. config_register_addressmaps(get_options());
  189. return data;
  190. }
  191. static int
  192. socks_test_cleanup(const struct testcase_t *testcase, void *ptr)
  193. {
  194. socks_test_data_t *data = ptr;
  195. (void)testcase;
  196. buf_free(data->buf);
  197. socks_request_free(data->req);
  198. tor_free(data);
  199. return 1;
  200. }
  201. const struct testcase_setup_t socks_setup = {
  202. socks_test_setup, socks_test_cleanup
  203. };
  204. #define SOCKS_TEST_INIT() \
  205. socks_test_data_t *testdata = ptr; \
  206. buf_t *buf = testdata->buf; \
  207. socks_request_t *socks = testdata->req;
  208. #define ADD_DATA(buf, s) \
  209. write_to_buf(s, sizeof(s)-1, buf)
  210. static void
  211. socks_request_clear(socks_request_t *socks)
  212. {
  213. tor_free(socks->username);
  214. tor_free(socks->password);
  215. memset(socks, 0, sizeof(socks_request_t));
  216. }
  217. /** Perform unsupported SOCKS 4 commands */
  218. static void
  219. test_socks_4_unsupported_commands(void *ptr)
  220. {
  221. SOCKS_TEST_INIT();
  222. /* SOCKS 4 Send BIND [02] to IP address 2.2.2.2:4369 */
  223. ADD_DATA(buf, "\x04\x02\x11\x11\x02\x02\x02\x02\x00");
  224. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  225. get_options()->SafeSocks) == -1);
  226. test_eq(4, socks->socks_version);
  227. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  228. done:
  229. ;
  230. }
  231. /** Perform supported SOCKS 4 commands */
  232. static void
  233. test_socks_4_supported_commands(void *ptr)
  234. {
  235. SOCKS_TEST_INIT();
  236. test_eq(0, buf_datalen(buf));
  237. /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4370 */
  238. ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x03\x00");
  239. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  240. get_options()->SafeSocks) == 1);
  241. test_eq(4, socks->socks_version);
  242. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  243. test_eq(SOCKS_COMMAND_CONNECT, socks->command);
  244. test_streq("2.2.2.3", socks->address);
  245. test_eq(4370, socks->port);
  246. test_assert(socks->got_auth == 0);
  247. test_assert(! socks->username);
  248. test_eq(0, buf_datalen(buf));
  249. socks_request_clear(socks);
  250. /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4369 with userid*/
  251. ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x04me\x00");
  252. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  253. get_options()->SafeSocks) == 1);
  254. test_eq(4, socks->socks_version);
  255. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  256. test_eq(SOCKS_COMMAND_CONNECT, socks->command);
  257. test_streq("2.2.2.4", socks->address);
  258. test_eq(4370, socks->port);
  259. test_assert(socks->got_auth == 1);
  260. test_assert(socks->username);
  261. test_eq(2, socks->usernamelen);
  262. test_memeq("me", socks->username, 2);
  263. test_eq(0, buf_datalen(buf));
  264. socks_request_clear(socks);
  265. /* SOCKS 4a Send RESOLVE [F0] request for torproject.org */
  266. ADD_DATA(buf, "\x04\xF0\x01\x01\x00\x00\x00\x02me\x00torproject.org\x00");
  267. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  268. get_options()->SafeSocks) == 1);
  269. test_eq(4, socks->socks_version);
  270. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  271. test_streq("torproject.org", socks->address);
  272. test_eq(0, buf_datalen(buf));
  273. done:
  274. ;
  275. }
  276. /** Perform unsupported SOCKS 5 commands */
  277. static void
  278. test_socks_5_unsupported_commands(void *ptr)
  279. {
  280. SOCKS_TEST_INIT();
  281. /* SOCKS 5 Send unsupported BIND [02] command */
  282. ADD_DATA(buf, "\x05\x02\x00\x01");
  283. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  284. get_options()->SafeSocks), 0);
  285. test_eq(0, buf_datalen(buf));
  286. test_eq(5, socks->socks_version);
  287. test_eq(2, socks->replylen);
  288. test_eq(5, socks->reply[0]);
  289. test_eq(0, socks->reply[1]);
  290. ADD_DATA(buf, "\x05\x02\x00\x01\x02\x02\x02\x01\x01\x01");
  291. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  292. get_options()->SafeSocks), -1);
  293. /* XXX: shouldn't tor reply 'command not supported' [07]? */
  294. buf_clear(buf);
  295. socks_request_clear(socks);
  296. /* SOCKS 5 Send unsupported UDP_ASSOCIATE [03] command */
  297. ADD_DATA(buf, "\x05\x03\x00\x01\x02");
  298. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  299. get_options()->SafeSocks), 0);
  300. test_eq(5, socks->socks_version);
  301. test_eq(2, socks->replylen);
  302. test_eq(5, socks->reply[0]);
  303. test_eq(0, socks->reply[1]);
  304. ADD_DATA(buf, "\x05\x03\x00\x01\x02\x02\x02\x01\x01\x01");
  305. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  306. get_options()->SafeSocks), -1);
  307. /* XXX: shouldn't tor reply 'command not supported' [07]? */
  308. done:
  309. ;
  310. }
  311. /** Perform supported SOCKS 5 commands */
  312. static void
  313. test_socks_5_supported_commands(void *ptr)
  314. {
  315. SOCKS_TEST_INIT();
  316. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  317. ADD_DATA(buf, "\x05\x01\x00");
  318. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  319. get_options()->SafeSocks), 0);
  320. test_eq(5, socks->socks_version);
  321. test_eq(2, socks->replylen);
  322. test_eq(5, socks->reply[0]);
  323. test_eq(0, socks->reply[1]);
  324. ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  325. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  326. get_options()->SafeSocks), 1);
  327. test_streq("2.2.2.2", socks->address);
  328. test_eq(4369, socks->port);
  329. test_eq(0, buf_datalen(buf));
  330. socks_request_clear(socks);
  331. /* SOCKS 5 Send CONNECT [01] to FQDN torproject.org:4369 */
  332. ADD_DATA(buf, "\x05\x01\x00");
  333. ADD_DATA(buf, "\x05\x01\x00\x03\x0Etorproject.org\x11\x11");
  334. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  335. get_options()->SafeSocks), 0);
  336. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  337. get_options()->SafeSocks), 1);
  338. test_eq(5, socks->socks_version);
  339. test_eq(2, socks->replylen);
  340. test_eq(5, socks->reply[0]);
  341. test_eq(0, socks->reply[1]);
  342. test_streq("torproject.org", socks->address);
  343. test_eq(4369, socks->port);
  344. test_eq(0, buf_datalen(buf));
  345. socks_request_clear(socks);
  346. /* SOCKS 5 Send RESOLVE [F0] request for torproject.org:4369 */
  347. ADD_DATA(buf, "\x05\x01\x00");
  348. ADD_DATA(buf, "\x05\xF0\x00\x03\x0Etorproject.org\x01\x02");
  349. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  350. get_options()->SafeSocks) == 0);
  351. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  352. get_options()->SafeSocks) == 1);
  353. test_eq(5, socks->socks_version);
  354. test_eq(2, socks->replylen);
  355. test_eq(5, socks->reply[0]);
  356. test_eq(0, socks->reply[1]);
  357. test_streq("torproject.org", socks->address);
  358. test_eq(0, buf_datalen(buf));
  359. socks_request_clear(socks);
  360. /* SOCKS 5 Send RESOLVE_PTR [F1] for IP address 2.2.2.5 */
  361. ADD_DATA(buf, "\x05\x01\x00");
  362. ADD_DATA(buf, "\x05\xF1\x00\x01\x02\x02\x02\x05\x01\x03");
  363. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  364. get_options()->SafeSocks) == 0);
  365. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  366. get_options()->SafeSocks) == 1);
  367. test_eq(5, socks->socks_version);
  368. test_eq(2, socks->replylen);
  369. test_eq(5, socks->reply[0]);
  370. test_eq(0, socks->reply[1]);
  371. test_streq("2.2.2.5", socks->address);
  372. test_eq(0, buf_datalen(buf));
  373. done:
  374. ;
  375. }
  376. /** Perform SOCKS 5 authentication */
  377. static void
  378. test_socks_5_no_authenticate(void *ptr)
  379. {
  380. SOCKS_TEST_INIT();
  381. /*SOCKS 5 No Authentication */
  382. ADD_DATA(buf,"\x05\x01\x00");
  383. test_assert(!fetch_from_buf_socks(buf, socks,
  384. get_options()->TestSocks,
  385. get_options()->SafeSocks));
  386. test_eq(2, socks->replylen);
  387. test_eq(5, socks->reply[0]);
  388. test_eq(SOCKS_NO_AUTH, socks->reply[1]);
  389. test_eq(0, buf_datalen(buf));
  390. /*SOCKS 5 Send username/password anyway - pretend to be broken */
  391. ADD_DATA(buf,"\x01\x02\x01\x01\x02\x01\x01");
  392. test_assert(!fetch_from_buf_socks(buf, socks,
  393. get_options()->TestSocks,
  394. get_options()->SafeSocks));
  395. test_eq(5, socks->socks_version);
  396. test_eq(2, socks->replylen);
  397. test_eq(5, socks->reply[0]);
  398. test_eq(0, socks->reply[1]);
  399. test_eq(2, socks->usernamelen);
  400. test_eq(2, socks->passwordlen);
  401. test_memeq("\x01\x01", socks->username, 2);
  402. test_memeq("\x01\x01", socks->password, 2);
  403. done:
  404. ;
  405. }
  406. /** Perform SOCKS 5 authentication */
  407. static void
  408. test_socks_5_authenticate(void *ptr)
  409. {
  410. SOCKS_TEST_INIT();
  411. /* SOCKS 5 Negotiate username/password authentication */
  412. ADD_DATA(buf, "\x05\x01\x02");
  413. test_assert(!fetch_from_buf_socks(buf, socks,
  414. get_options()->TestSocks,
  415. get_options()->SafeSocks));
  416. test_eq(2, socks->replylen);
  417. test_eq(5, socks->reply[0]);
  418. test_eq(SOCKS_USER_PASS, socks->reply[1]);
  419. test_eq(5, socks->socks_version);
  420. test_eq(0, buf_datalen(buf));
  421. /* SOCKS 5 Send username/password */
  422. ADD_DATA(buf, "\x01\x02me\x08mypasswd");
  423. test_assert(!fetch_from_buf_socks(buf, socks,
  424. get_options()->TestSocks,
  425. get_options()->SafeSocks));
  426. test_eq(5, socks->socks_version);
  427. test_eq(2, socks->replylen);
  428. test_eq(5, socks->reply[0]);
  429. test_eq(0, socks->reply[1]);
  430. test_eq(2, socks->usernamelen);
  431. test_eq(8, socks->passwordlen);
  432. test_memeq("me", socks->username, 2);
  433. test_memeq("mypasswd", socks->password, 8);
  434. done:
  435. ;
  436. }
  437. /** Perform SOCKS 5 authentication and send data all in one go */
  438. static void
  439. test_socks_5_authenticate_with_data(void *ptr)
  440. {
  441. SOCKS_TEST_INIT();
  442. /* SOCKS 5 Negotiate username/password authentication */
  443. ADD_DATA(buf, "\x05\x01\x02");
  444. test_assert(!fetch_from_buf_socks(buf, socks,
  445. get_options()->TestSocks,
  446. get_options()->SafeSocks));
  447. test_eq(2, socks->replylen);
  448. test_eq(5, socks->reply[0]);
  449. test_eq(SOCKS_USER_PASS, socks->reply[1]);
  450. test_eq(5, socks->socks_version);
  451. test_eq(0, buf_datalen(buf));
  452. /* SOCKS 5 Send username/password */
  453. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  454. ADD_DATA(buf, "\x01\x02me\x02me\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  455. test_assert(!fetch_from_buf_socks(buf, socks,
  456. get_options()->TestSocks,
  457. get_options()->SafeSocks));
  458. test_eq(5, socks->socks_version);
  459. test_eq(2, socks->replylen);
  460. test_eq(5, socks->reply[0]);
  461. test_eq(0, socks->reply[1]);
  462. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  463. get_options()->SafeSocks) == 1);
  464. test_eq(5, socks->socks_version);
  465. test_eq(2, socks->replylen);
  466. test_eq(5, socks->reply[0]);
  467. test_eq(0, socks->reply[1]);
  468. test_streq("2.2.2.2", socks->address);
  469. test_eq(4369, socks->port);
  470. done:
  471. ;
  472. }
  473. /** Perform SOCKS 5 authentication before method negotiated */
  474. static void
  475. test_socks_5_auth_before_negotiation(void *ptr)
  476. {
  477. SOCKS_TEST_INIT();
  478. /* SOCKS 5 Send username/password */
  479. ADD_DATA(buf, "\x01\x02me\x02me");
  480. test_assert(fetch_from_buf_socks(buf, socks,
  481. get_options()->TestSocks,
  482. get_options()->SafeSocks) == -1);
  483. test_eq(0, socks->socks_version);
  484. test_eq(0, socks->replylen);
  485. test_eq(0, socks->reply[0]);
  486. test_eq(0, socks->reply[1]);
  487. done:
  488. ;
  489. }
  490. /** Run unit tests for buffers.c */
  491. static void
  492. test_buffers(void)
  493. {
  494. char str[256];
  495. char str2[256];
  496. buf_t *buf = NULL, *buf2 = NULL;
  497. const char *cp;
  498. int j;
  499. size_t r;
  500. /****
  501. * buf_new
  502. ****/
  503. if (!(buf = buf_new()))
  504. test_fail();
  505. //test_eq(buf_capacity(buf), 4096);
  506. test_eq(buf_datalen(buf), 0);
  507. /****
  508. * General pointer frobbing
  509. */
  510. for (j=0;j<256;++j) {
  511. str[j] = (char)j;
  512. }
  513. write_to_buf(str, 256, buf);
  514. write_to_buf(str, 256, buf);
  515. test_eq(buf_datalen(buf), 512);
  516. fetch_from_buf(str2, 200, buf);
  517. test_memeq(str, str2, 200);
  518. test_eq(buf_datalen(buf), 312);
  519. memset(str2, 0, sizeof(str2));
  520. fetch_from_buf(str2, 256, buf);
  521. test_memeq(str+200, str2, 56);
  522. test_memeq(str, str2+56, 200);
  523. test_eq(buf_datalen(buf), 56);
  524. memset(str2, 0, sizeof(str2));
  525. /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
  526. * another 3584 bytes, we hit the end. */
  527. for (j=0;j<15;++j) {
  528. write_to_buf(str, 256, buf);
  529. }
  530. assert_buf_ok(buf);
  531. test_eq(buf_datalen(buf), 3896);
  532. fetch_from_buf(str2, 56, buf);
  533. test_eq(buf_datalen(buf), 3840);
  534. test_memeq(str+200, str2, 56);
  535. for (j=0;j<15;++j) {
  536. memset(str2, 0, sizeof(str2));
  537. fetch_from_buf(str2, 256, buf);
  538. test_memeq(str, str2, 256);
  539. }
  540. test_eq(buf_datalen(buf), 0);
  541. buf_free(buf);
  542. buf = NULL;
  543. /* Okay, now make sure growing can work. */
  544. buf = buf_new_with_capacity(16);
  545. //test_eq(buf_capacity(buf), 16);
  546. write_to_buf(str+1, 255, buf);
  547. //test_eq(buf_capacity(buf), 256);
  548. fetch_from_buf(str2, 254, buf);
  549. test_memeq(str+1, str2, 254);
  550. //test_eq(buf_capacity(buf), 256);
  551. assert_buf_ok(buf);
  552. write_to_buf(str, 32, buf);
  553. //test_eq(buf_capacity(buf), 256);
  554. assert_buf_ok(buf);
  555. write_to_buf(str, 256, buf);
  556. assert_buf_ok(buf);
  557. //test_eq(buf_capacity(buf), 512);
  558. test_eq(buf_datalen(buf), 33+256);
  559. fetch_from_buf(str2, 33, buf);
  560. test_eq(*str2, str[255]);
  561. test_memeq(str2+1, str, 32);
  562. //test_eq(buf_capacity(buf), 512);
  563. test_eq(buf_datalen(buf), 256);
  564. fetch_from_buf(str2, 256, buf);
  565. test_memeq(str, str2, 256);
  566. /* now try shrinking: case 1. */
  567. buf_free(buf);
  568. buf = buf_new_with_capacity(33668);
  569. for (j=0;j<67;++j) {
  570. write_to_buf(str,255, buf);
  571. }
  572. //test_eq(buf_capacity(buf), 33668);
  573. test_eq(buf_datalen(buf), 17085);
  574. for (j=0; j < 40; ++j) {
  575. fetch_from_buf(str2, 255,buf);
  576. test_memeq(str2, str, 255);
  577. }
  578. /* now try shrinking: case 2. */
  579. buf_free(buf);
  580. buf = buf_new_with_capacity(33668);
  581. for (j=0;j<67;++j) {
  582. write_to_buf(str,255, buf);
  583. }
  584. for (j=0; j < 20; ++j) {
  585. fetch_from_buf(str2, 255,buf);
  586. test_memeq(str2, str, 255);
  587. }
  588. for (j=0;j<80;++j) {
  589. write_to_buf(str,255, buf);
  590. }
  591. //test_eq(buf_capacity(buf),33668);
  592. for (j=0; j < 120; ++j) {
  593. fetch_from_buf(str2, 255,buf);
  594. test_memeq(str2, str, 255);
  595. }
  596. /* Move from buf to buf. */
  597. buf_free(buf);
  598. buf = buf_new_with_capacity(4096);
  599. buf2 = buf_new_with_capacity(4096);
  600. for (j=0;j<100;++j)
  601. write_to_buf(str, 255, buf);
  602. test_eq(buf_datalen(buf), 25500);
  603. for (j=0;j<100;++j) {
  604. r = 10;
  605. move_buf_to_buf(buf2, buf, &r);
  606. test_eq(r, 0);
  607. }
  608. test_eq(buf_datalen(buf), 24500);
  609. test_eq(buf_datalen(buf2), 1000);
  610. for (j=0;j<3;++j) {
  611. fetch_from_buf(str2, 255, buf2);
  612. test_memeq(str2, str, 255);
  613. }
  614. r = 8192; /*big move*/
  615. move_buf_to_buf(buf2, buf, &r);
  616. test_eq(r, 0);
  617. r = 30000; /* incomplete move */
  618. move_buf_to_buf(buf2, buf, &r);
  619. test_eq(r, 13692);
  620. for (j=0;j<97;++j) {
  621. fetch_from_buf(str2, 255, buf2);
  622. test_memeq(str2, str, 255);
  623. }
  624. buf_free(buf);
  625. buf_free(buf2);
  626. buf = buf2 = NULL;
  627. buf = buf_new_with_capacity(5);
  628. cp = "Testing. This is a moderately long Testing string.";
  629. for (j = 0; cp[j]; j++)
  630. write_to_buf(cp+j, 1, buf);
  631. test_eq(0, buf_find_string_offset(buf, "Testing", 7));
  632. test_eq(1, buf_find_string_offset(buf, "esting", 6));
  633. test_eq(1, buf_find_string_offset(buf, "est", 3));
  634. test_eq(39, buf_find_string_offset(buf, "ing str", 7));
  635. test_eq(35, buf_find_string_offset(buf, "Testing str", 11));
  636. test_eq(32, buf_find_string_offset(buf, "ng ", 3));
  637. test_eq(43, buf_find_string_offset(buf, "string.", 7));
  638. test_eq(-1, buf_find_string_offset(buf, "shrdlu", 6));
  639. test_eq(-1, buf_find_string_offset(buf, "Testing thing", 13));
  640. test_eq(-1, buf_find_string_offset(buf, "ngx", 3));
  641. buf_free(buf);
  642. buf = NULL;
  643. done:
  644. if (buf)
  645. buf_free(buf);
  646. if (buf2)
  647. buf_free(buf2);
  648. }
  649. /** Run unit tests for the onion handshake code. */
  650. static void
  651. test_onion_handshake(void)
  652. {
  653. /* client-side */
  654. crypto_dh_env_t *c_dh = NULL;
  655. char c_buf[ONIONSKIN_CHALLENGE_LEN];
  656. char c_keys[40];
  657. /* server-side */
  658. char s_buf[ONIONSKIN_REPLY_LEN];
  659. char s_keys[40];
  660. /* shared */
  661. crypto_pk_env_t *pk = NULL;
  662. pk = pk_generate(0);
  663. /* client handshake 1. */
  664. memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
  665. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  666. /* server handshake */
  667. memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
  668. memset(s_keys, 0, 40);
  669. test_assert(! onion_skin_server_handshake(c_buf, pk, NULL,
  670. s_buf, s_keys, 40));
  671. /* client handshake 2 */
  672. memset(c_keys, 0, 40);
  673. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  674. if (memcmp(c_keys, s_keys, 40)) {
  675. puts("Aiiiie");
  676. exit(1);
  677. }
  678. test_memeq(c_keys, s_keys, 40);
  679. memset(s_buf, 0, 40);
  680. test_memneq(c_keys, s_buf, 40);
  681. done:
  682. if (c_dh)
  683. crypto_dh_free(c_dh);
  684. if (pk)
  685. crypto_free_pk_env(pk);
  686. }
  687. static void
  688. test_circuit_timeout(void)
  689. {
  690. /* Plan:
  691. * 1. Generate 1000 samples
  692. * 2. Estimate parameters
  693. * 3. If difference, repeat
  694. * 4. Save state
  695. * 5. load state
  696. * 6. Estimate parameters
  697. * 7. compare differences
  698. */
  699. circuit_build_times_t initial;
  700. circuit_build_times_t estimate;
  701. circuit_build_times_t final;
  702. double timeout1, timeout2;
  703. or_state_t state;
  704. int i, runs;
  705. double close_ms;
  706. circuit_build_times_init(&initial);
  707. circuit_build_times_init(&estimate);
  708. circuit_build_times_init(&final);
  709. memset(&state, 0, sizeof(or_state_t));
  710. circuitbuild_running_unit_tests();
  711. #define timeout0 (build_time_t)(30*1000.0)
  712. initial.Xm = 3000;
  713. circuit_build_times_initial_alpha(&initial,
  714. CBT_DEFAULT_QUANTILE_CUTOFF/100.0,
  715. timeout0);
  716. close_ms = MAX(circuit_build_times_calculate_timeout(&initial,
  717. CBT_DEFAULT_CLOSE_QUANTILE/100.0),
  718. CBT_DEFAULT_TIMEOUT_INITIAL_VALUE);
  719. do {
  720. for (i=0; i < CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE; i++) {
  721. build_time_t sample = circuit_build_times_generate_sample(&initial,0,1);
  722. if (sample > close_ms) {
  723. circuit_build_times_add_time(&estimate, CBT_BUILD_ABANDONED);
  724. } else {
  725. circuit_build_times_add_time(&estimate, sample);
  726. }
  727. }
  728. circuit_build_times_update_alpha(&estimate);
  729. timeout1 = circuit_build_times_calculate_timeout(&estimate,
  730. CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
  731. circuit_build_times_set_timeout(&estimate);
  732. log_notice(LD_CIRC, "Timeout1 is %lf, Xm is %d", timeout1, estimate.Xm);
  733. /* 2% error */
  734. } while (fabs(circuit_build_times_cdf(&initial, timeout0) -
  735. circuit_build_times_cdf(&initial, timeout1)) > 0.02);
  736. test_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
  737. circuit_build_times_update_state(&estimate, &state);
  738. test_assert(circuit_build_times_parse_state(&final, &state) == 0);
  739. circuit_build_times_update_alpha(&final);
  740. timeout2 = circuit_build_times_calculate_timeout(&final,
  741. CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
  742. circuit_build_times_set_timeout(&final);
  743. log_notice(LD_CIRC, "Timeout2 is %lf, Xm is %d", timeout2, final.Xm);
  744. /* 5% here because some accuracy is lost due to histogram conversion */
  745. test_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
  746. circuit_build_times_cdf(&initial, timeout2)) < 0.05);
  747. for (runs = 0; runs < 50; runs++) {
  748. int build_times_idx = 0;
  749. int total_build_times = 0;
  750. final.close_ms = final.timeout_ms = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
  751. estimate.close_ms = estimate.timeout_ms
  752. = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
  753. for (i = 0; i < CBT_DEFAULT_RECENT_CIRCUITS*2; i++) {
  754. circuit_build_times_network_circ_success(&estimate);
  755. circuit_build_times_add_time(&estimate,
  756. circuit_build_times_generate_sample(&estimate, 0,
  757. CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
  758. circuit_build_times_network_circ_success(&estimate);
  759. circuit_build_times_add_time(&final,
  760. circuit_build_times_generate_sample(&final, 0,
  761. CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
  762. }
  763. test_assert(!circuit_build_times_network_check_changed(&estimate));
  764. test_assert(!circuit_build_times_network_check_changed(&final));
  765. /* Reset liveness to be non-live */
  766. final.liveness.network_last_live = 0;
  767. estimate.liveness.network_last_live = 0;
  768. build_times_idx = estimate.build_times_idx;
  769. total_build_times = estimate.total_build_times;
  770. test_assert(circuit_build_times_network_check_live(&estimate));
  771. test_assert(circuit_build_times_network_check_live(&final));
  772. circuit_build_times_count_close(&estimate, 0,
  773. (time_t)(approx_time()-estimate.close_ms/1000.0-1));
  774. circuit_build_times_count_close(&final, 0,
  775. (time_t)(approx_time()-final.close_ms/1000.0-1));
  776. test_assert(!circuit_build_times_network_check_live(&estimate));
  777. test_assert(!circuit_build_times_network_check_live(&final));
  778. log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
  779. build_times_idx, estimate.build_times_idx,
  780. total_build_times, estimate.total_build_times);
  781. /* Check rollback index. Should match top of loop. */
  782. test_assert(build_times_idx == estimate.build_times_idx);
  783. // This can fail if estimate.total_build_times == 1000, because
  784. // in that case, rewind actually causes us to lose timeouts
  785. if (total_build_times != CBT_NCIRCUITS_TO_OBSERVE)
  786. test_assert(total_build_times == estimate.total_build_times);
  787. /* Now simulate that the network has become live and we need
  788. * a change */
  789. circuit_build_times_network_is_live(&estimate);
  790. circuit_build_times_network_is_live(&final);
  791. for (i = 0; i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT; i++) {
  792. circuit_build_times_count_timeout(&estimate, 1);
  793. if (i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1) {
  794. circuit_build_times_count_timeout(&final, 1);
  795. }
  796. }
  797. test_assert(estimate.liveness.after_firsthop_idx == 0);
  798. test_assert(final.liveness.after_firsthop_idx ==
  799. CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1);
  800. test_assert(circuit_build_times_network_check_live(&estimate));
  801. test_assert(circuit_build_times_network_check_live(&final));
  802. circuit_build_times_count_timeout(&final, 1);
  803. }
  804. done:
  805. return;
  806. }
  807. /** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
  808. * that policies_summarize() produces the string <b>expected_summary</b> from
  809. * it. */
  810. static void
  811. test_policy_summary_helper(const char *policy_str,
  812. const char *expected_summary)
  813. {
  814. config_line_t line;
  815. smartlist_t *policy = smartlist_create();
  816. char *summary = NULL;
  817. int r;
  818. short_policy_t *short_policy = NULL;
  819. line.key = (char*)"foo";
  820. line.value = (char *)policy_str;
  821. line.next = NULL;
  822. r = policies_parse_exit_policy(&line, &policy, 0, NULL, 1);
  823. test_eq(r, 0);
  824. summary = policy_summarize(policy);
  825. test_assert(summary != NULL);
  826. test_streq(summary, expected_summary);
  827. short_policy = parse_short_policy(summary);
  828. tt_assert(short_policy);
  829. done:
  830. tor_free(summary);
  831. if (policy)
  832. addr_policy_list_free(policy);
  833. short_policy_free(short_policy);
  834. }
  835. /** Run unit tests for generating summary lines of exit policies */
  836. static void
  837. test_policies(void)
  838. {
  839. int i;
  840. smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
  841. *policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
  842. *policy7 = NULL;
  843. addr_policy_t *p;
  844. tor_addr_t tar;
  845. config_line_t line;
  846. smartlist_t *sm = NULL;
  847. char *policy_str = NULL;
  848. policy = smartlist_create();
  849. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
  850. test_assert(p != NULL);
  851. test_eq(ADDR_POLICY_REJECT, p->policy_type);
  852. tor_addr_from_ipv4h(&tar, 0xc0a80000u);
  853. test_eq(0, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
  854. test_eq(16, p->maskbits);
  855. test_eq(1, p->prt_min);
  856. test_eq(65535, p->prt_max);
  857. smartlist_add(policy, p);
  858. test_assert(ADDR_POLICY_ACCEPTED ==
  859. compare_addr_to_addr_policy(0x01020304u, 2, policy));
  860. test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  861. compare_addr_to_addr_policy(0, 2, policy));
  862. test_assert(ADDR_POLICY_REJECTED ==
  863. compare_addr_to_addr_policy(0xc0a80102, 2, policy));
  864. test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, NULL, 1));
  865. test_assert(policy2);
  866. policy3 = smartlist_create();
  867. p = router_parse_addr_policy_item_from_string("reject *:*",-1);
  868. test_assert(p != NULL);
  869. smartlist_add(policy3, p);
  870. p = router_parse_addr_policy_item_from_string("accept *:*",-1);
  871. test_assert(p != NULL);
  872. smartlist_add(policy3, p);
  873. policy4 = smartlist_create();
  874. p = router_parse_addr_policy_item_from_string("accept *:443",-1);
  875. test_assert(p != NULL);
  876. smartlist_add(policy4, p);
  877. p = router_parse_addr_policy_item_from_string("accept *:443",-1);
  878. test_assert(p != NULL);
  879. smartlist_add(policy4, p);
  880. policy5 = smartlist_create();
  881. p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*",-1);
  882. test_assert(p != NULL);
  883. smartlist_add(policy5, p);
  884. p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*",-1);
  885. test_assert(p != NULL);
  886. smartlist_add(policy5, p);
  887. p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*",-1);
  888. test_assert(p != NULL);
  889. smartlist_add(policy5, p);
  890. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
  891. test_assert(p != NULL);
  892. smartlist_add(policy5, p);
  893. p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*",-1);
  894. test_assert(p != NULL);
  895. smartlist_add(policy5, p);
  896. p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*",-1);
  897. test_assert(p != NULL);
  898. smartlist_add(policy5, p);
  899. p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*",-1);
  900. test_assert(p != NULL);
  901. smartlist_add(policy5, p);
  902. p = router_parse_addr_policy_item_from_string("reject *:1-65534",-1);
  903. test_assert(p != NULL);
  904. smartlist_add(policy5, p);
  905. p = router_parse_addr_policy_item_from_string("reject *:65535",-1);
  906. test_assert(p != NULL);
  907. smartlist_add(policy5, p);
  908. p = router_parse_addr_policy_item_from_string("accept *:1-65535",-1);
  909. test_assert(p != NULL);
  910. smartlist_add(policy5, p);
  911. policy6 = smartlist_create();
  912. p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*",-1);
  913. test_assert(p != NULL);
  914. smartlist_add(policy6, p);
  915. policy7 = smartlist_create();
  916. p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*",-1);
  917. test_assert(p != NULL);
  918. smartlist_add(policy7, p);
  919. test_assert(!exit_policy_is_general_exit(policy));
  920. test_assert(exit_policy_is_general_exit(policy2));
  921. test_assert(!exit_policy_is_general_exit(NULL));
  922. test_assert(!exit_policy_is_general_exit(policy3));
  923. test_assert(!exit_policy_is_general_exit(policy4));
  924. test_assert(!exit_policy_is_general_exit(policy5));
  925. test_assert(!exit_policy_is_general_exit(policy6));
  926. test_assert(!exit_policy_is_general_exit(policy7));
  927. test_assert(cmp_addr_policies(policy, policy2));
  928. test_assert(cmp_addr_policies(policy, NULL));
  929. test_assert(!cmp_addr_policies(policy2, policy2));
  930. test_assert(!cmp_addr_policies(NULL, NULL));
  931. test_assert(!policy_is_reject_star(policy2));
  932. test_assert(policy_is_reject_star(policy));
  933. test_assert(policy_is_reject_star(NULL));
  934. addr_policy_list_free(policy);
  935. policy = NULL;
  936. /* make sure compacting logic works. */
  937. policy = NULL;
  938. line.key = (char*)"foo";
  939. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  940. line.next = NULL;
  941. test_assert(0 == policies_parse_exit_policy(&line, &policy, 0, NULL, 1));
  942. test_assert(policy);
  943. //test_streq(policy->string, "accept *:80");
  944. //test_streq(policy->next->string, "reject *:*");
  945. test_eq(smartlist_len(policy), 2);
  946. /* test policy summaries */
  947. /* check if we properly ignore private IP addresses */
  948. test_policy_summary_helper("reject 192.168.0.0/16:*,"
  949. "reject 0.0.0.0/8:*,"
  950. "reject 10.0.0.0/8:*,"
  951. "accept *:10-30,"
  952. "accept *:90,"
  953. "reject *:*",
  954. "accept 10-30,90");
  955. /* check all accept policies, and proper counting of rejects */
  956. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  957. "reject 12.0.0.0/9:80,"
  958. "reject 13.0.0.0/9:80,"
  959. "reject 14.0.0.0/9:80,"
  960. "accept *:*", "accept 1-65535");
  961. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  962. "reject 12.0.0.0/9:80,"
  963. "reject 13.0.0.0/9:80,"
  964. "reject 14.0.0.0/9:80,"
  965. "reject 15.0.0.0:81,"
  966. "accept *:*", "accept 1-65535");
  967. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  968. "reject 12.0.0.0/9:80,"
  969. "reject 13.0.0.0/9:80,"
  970. "reject 14.0.0.0/9:80,"
  971. "reject 15.0.0.0:80,"
  972. "accept *:*",
  973. "reject 80");
  974. /* no exits */
  975. test_policy_summary_helper("accept 11.0.0.0/9:80,"
  976. "reject *:*",
  977. "reject 1-65535");
  978. /* port merging */
  979. test_policy_summary_helper("accept *:80,"
  980. "accept *:81,"
  981. "accept *:100-110,"
  982. "accept *:111,"
  983. "reject *:*",
  984. "accept 80-81,100-111");
  985. /* border ports */
  986. test_policy_summary_helper("accept *:1,"
  987. "accept *:3,"
  988. "accept *:65535,"
  989. "reject *:*",
  990. "accept 1,3,65535");
  991. /* holes */
  992. test_policy_summary_helper("accept *:1,"
  993. "accept *:3,"
  994. "accept *:5,"
  995. "accept *:7,"
  996. "reject *:*",
  997. "accept 1,3,5,7");
  998. test_policy_summary_helper("reject *:1,"
  999. "reject *:3,"
  1000. "reject *:5,"
  1001. "reject *:7,"
  1002. "accept *:*",
  1003. "reject 1,3,5,7");
  1004. /* truncation ports */
  1005. sm = smartlist_create();
  1006. for (i=1; i<2000; i+=2) {
  1007. char buf[POLICY_BUF_LEN];
  1008. tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
  1009. smartlist_add(sm, tor_strdup(buf));
  1010. }
  1011. smartlist_add(sm, tor_strdup("accept *:*"));
  1012. policy_str = smartlist_join_strings(sm, ",", 0, NULL);
  1013. test_policy_summary_helper( policy_str,
  1014. "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
  1015. "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
  1016. "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
  1017. "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
  1018. "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
  1019. "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
  1020. "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
  1021. "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
  1022. "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
  1023. "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
  1024. "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
  1025. "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
  1026. "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
  1027. "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
  1028. done:
  1029. addr_policy_list_free(policy);
  1030. addr_policy_list_free(policy2);
  1031. addr_policy_list_free(policy3);
  1032. addr_policy_list_free(policy4);
  1033. addr_policy_list_free(policy5);
  1034. addr_policy_list_free(policy6);
  1035. addr_policy_list_free(policy7);
  1036. tor_free(policy_str);
  1037. if (sm) {
  1038. SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
  1039. smartlist_free(sm);
  1040. }
  1041. }
  1042. /** Run AES performance benchmarks. */
  1043. static void
  1044. bench_aes(void)
  1045. {
  1046. int len, i;
  1047. char *b1, *b2;
  1048. crypto_cipher_env_t *c;
  1049. struct timeval start, end;
  1050. const int iters = 100000;
  1051. uint64_t nsec;
  1052. c = crypto_new_cipher_env();
  1053. crypto_cipher_generate_key(c);
  1054. crypto_cipher_encrypt_init_cipher(c);
  1055. for (len = 1; len <= 8192; len *= 2) {
  1056. b1 = tor_malloc_zero(len);
  1057. b2 = tor_malloc_zero(len);
  1058. tor_gettimeofday(&start);
  1059. for (i = 0; i < iters; ++i) {
  1060. crypto_cipher_encrypt(c, b1, b2, len);
  1061. }
  1062. tor_gettimeofday(&end);
  1063. tor_free(b1);
  1064. tor_free(b2);
  1065. nsec = (uint64_t) tv_udiff(&start,&end);
  1066. nsec *= 1000;
  1067. nsec /= (iters*len);
  1068. printf("%d bytes: "U64_FORMAT" nsec per byte\n", len,
  1069. U64_PRINTF_ARG(nsec));
  1070. }
  1071. crypto_free_cipher_env(c);
  1072. }
  1073. /** Run digestmap_t performance benchmarks. */
  1074. static void
  1075. bench_dmap(void)
  1076. {
  1077. smartlist_t *sl = smartlist_create();
  1078. smartlist_t *sl2 = smartlist_create();
  1079. struct timeval start, end, pt2, pt3, pt4;
  1080. const int iters = 10000;
  1081. const int elts = 4000;
  1082. const int fpostests = 1000000;
  1083. char d[20];
  1084. int i,n=0, fp = 0;
  1085. digestmap_t *dm = digestmap_new();
  1086. digestset_t *ds = digestset_new(elts);
  1087. for (i = 0; i < elts; ++i) {
  1088. crypto_rand(d, 20);
  1089. smartlist_add(sl, tor_memdup(d, 20));
  1090. }
  1091. for (i = 0; i < elts; ++i) {
  1092. crypto_rand(d, 20);
  1093. smartlist_add(sl2, tor_memdup(d, 20));
  1094. }
  1095. printf("nbits=%d\n", ds->mask+1);
  1096. tor_gettimeofday(&start);
  1097. for (i = 0; i < iters; ++i) {
  1098. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  1099. }
  1100. tor_gettimeofday(&pt2);
  1101. for (i = 0; i < iters; ++i) {
  1102. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
  1103. SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  1104. }
  1105. tor_gettimeofday(&pt3);
  1106. for (i = 0; i < iters; ++i) {
  1107. SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  1108. }
  1109. tor_gettimeofday(&pt4);
  1110. for (i = 0; i < iters; ++i) {
  1111. SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_isin(ds, cp));
  1112. SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_isin(ds, cp));
  1113. }
  1114. tor_gettimeofday(&end);
  1115. for (i = 0; i < fpostests; ++i) {
  1116. crypto_rand(d, 20);
  1117. if (digestset_isin(ds, d)) ++fp;
  1118. }
  1119. printf("%ld\n",(unsigned long)tv_udiff(&start, &pt2));
  1120. printf("%ld\n",(unsigned long)tv_udiff(&pt2, &pt3));
  1121. printf("%ld\n",(unsigned long)tv_udiff(&pt3, &pt4));
  1122. printf("%ld\n",(unsigned long)tv_udiff(&pt4, &end));
  1123. printf("-- %d\n", n);
  1124. printf("++ %f\n", fp/(double)fpostests);
  1125. digestmap_free(dm, NULL);
  1126. digestset_free(ds);
  1127. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1128. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  1129. smartlist_free(sl);
  1130. smartlist_free(sl2);
  1131. }
  1132. /** Test encoding and parsing of rendezvous service descriptors. */
  1133. static void
  1134. test_rend_fns(void)
  1135. {
  1136. rend_service_descriptor_t *generated = NULL, *parsed = NULL;
  1137. char service_id[DIGEST_LEN];
  1138. char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
  1139. const char *next_desc;
  1140. smartlist_t *descs = smartlist_create();
  1141. char computed_desc_id[DIGEST_LEN];
  1142. char parsed_desc_id[DIGEST_LEN];
  1143. crypto_pk_env_t *pk1 = NULL, *pk2 = NULL;
  1144. time_t now;
  1145. char *intro_points_encrypted = NULL;
  1146. size_t intro_points_size;
  1147. size_t encoded_size;
  1148. int i;
  1149. char address1[] = "fooaddress.onion";
  1150. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  1151. char address3[] = "fooaddress.exit";
  1152. char address4[] = "www.torproject.org";
  1153. test_assert(BAD_HOSTNAME == parse_extended_hostname(address1, 1));
  1154. test_assert(ONION_HOSTNAME == parse_extended_hostname(address2, 1));
  1155. test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3, 1));
  1156. test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4, 1));
  1157. pk1 = pk_generate(0);
  1158. pk2 = pk_generate(1);
  1159. generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  1160. generated->pk = crypto_pk_dup_key(pk1);
  1161. crypto_pk_get_digest(generated->pk, service_id);
  1162. base32_encode(service_id_base32, REND_SERVICE_ID_LEN_BASE32+1,
  1163. service_id, REND_SERVICE_ID_LEN);
  1164. now = time(NULL);
  1165. generated->timestamp = now;
  1166. generated->version = 2;
  1167. generated->protocols = 42;
  1168. generated->intro_nodes = smartlist_create();
  1169. for (i = 0; i < 3; i++) {
  1170. rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
  1171. crypto_pk_env_t *okey = pk_generate(2 + i);
  1172. intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
  1173. intro->extend_info->onion_key = okey;
  1174. crypto_pk_get_digest(intro->extend_info->onion_key,
  1175. intro->extend_info->identity_digest);
  1176. //crypto_rand(info->identity_digest, DIGEST_LEN); /* Would this work? */
  1177. intro->extend_info->nickname[0] = '$';
  1178. base16_encode(intro->extend_info->nickname + 1,
  1179. sizeof(intro->extend_info->nickname) - 1,
  1180. intro->extend_info->identity_digest, DIGEST_LEN);
  1181. /* Does not cover all IP addresses. */
  1182. tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
  1183. intro->extend_info->port = 1 + crypto_rand_int(65535);
  1184. intro->intro_key = crypto_pk_dup_key(pk2);
  1185. smartlist_add(generated->intro_nodes, intro);
  1186. }
  1187. test_assert(rend_encode_v2_descriptors(descs, generated, now, 0,
  1188. REND_NO_AUTH, NULL, NULL) > 0);
  1189. test_assert(rend_compute_v2_desc_id(computed_desc_id, service_id_base32,
  1190. NULL, now, 0) == 0);
  1191. test_memeq(((rend_encoded_v2_service_descriptor_t *)
  1192. smartlist_get(descs, 0))->desc_id, computed_desc_id, DIGEST_LEN);
  1193. test_assert(rend_parse_v2_service_descriptor(&parsed, parsed_desc_id,
  1194. &intro_points_encrypted,
  1195. &intro_points_size,
  1196. &encoded_size,
  1197. &next_desc,
  1198. ((rend_encoded_v2_service_descriptor_t *)
  1199. smartlist_get(descs, 0))->desc_str) == 0);
  1200. test_assert(parsed);
  1201. test_memeq(((rend_encoded_v2_service_descriptor_t *)
  1202. smartlist_get(descs, 0))->desc_id, parsed_desc_id, DIGEST_LEN);
  1203. test_eq(rend_parse_introduction_points(parsed, intro_points_encrypted,
  1204. intro_points_size), 3);
  1205. test_assert(!crypto_pk_cmp_keys(generated->pk, parsed->pk));
  1206. test_eq(parsed->timestamp, now);
  1207. test_eq(parsed->version, 2);
  1208. test_eq(parsed->protocols, 42);
  1209. test_eq(smartlist_len(parsed->intro_nodes), 3);
  1210. for (i = 0; i < smartlist_len(parsed->intro_nodes); i++) {
  1211. rend_intro_point_t *par_intro = smartlist_get(parsed->intro_nodes, i),
  1212. *gen_intro = smartlist_get(generated->intro_nodes, i);
  1213. extend_info_t *par_info = par_intro->extend_info;
  1214. extend_info_t *gen_info = gen_intro->extend_info;
  1215. test_assert(!crypto_pk_cmp_keys(gen_info->onion_key, par_info->onion_key));
  1216. test_memeq(gen_info->identity_digest, par_info->identity_digest,
  1217. DIGEST_LEN);
  1218. test_streq(gen_info->nickname, par_info->nickname);
  1219. test_assert(tor_addr_eq(&gen_info->addr, &par_info->addr));
  1220. test_eq(gen_info->port, par_info->port);
  1221. }
  1222. rend_service_descriptor_free(parsed);
  1223. rend_service_descriptor_free(generated);
  1224. parsed = generated = NULL;
  1225. done:
  1226. if (descs) {
  1227. for (i = 0; i < smartlist_len(descs); i++)
  1228. rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
  1229. smartlist_free(descs);
  1230. }
  1231. if (parsed)
  1232. rend_service_descriptor_free(parsed);
  1233. if (generated)
  1234. rend_service_descriptor_free(generated);
  1235. if (pk1)
  1236. crypto_free_pk_env(pk1);
  1237. if (pk2)
  1238. crypto_free_pk_env(pk2);
  1239. tor_free(intro_points_encrypted);
  1240. }
  1241. /** Run unit tests for GeoIP code. */
  1242. static void
  1243. test_geoip(void)
  1244. {
  1245. int i, j;
  1246. time_t now = time(NULL);
  1247. char *s = NULL;
  1248. /* Populate the DB a bit. Add these in order, since we can't do the final
  1249. * 'sort' step. These aren't very good IP addresses, but they're perfectly
  1250. * fine uint32_t values. */
  1251. test_eq(0, geoip_parse_entry("10,50,AB"));
  1252. test_eq(0, geoip_parse_entry("52,90,XY"));
  1253. test_eq(0, geoip_parse_entry("95,100,AB"));
  1254. test_eq(0, geoip_parse_entry("\"105\",\"140\",\"ZZ\""));
  1255. test_eq(0, geoip_parse_entry("\"150\",\"190\",\"XY\""));
  1256. test_eq(0, geoip_parse_entry("\"200\",\"250\",\"AB\""));
  1257. /* We should have 4 countries: ??, ab, xy, zz. */
  1258. test_eq(4, geoip_get_n_countries());
  1259. /* Make sure that country ID actually works. */
  1260. #define NAMEFOR(x) geoip_get_country_name(geoip_get_country_by_ip(x))
  1261. test_streq("??", NAMEFOR(3));
  1262. test_eq(0, geoip_get_country_by_ip(3));
  1263. test_streq("ab", NAMEFOR(32));
  1264. test_streq("??", NAMEFOR(5));
  1265. test_streq("??", NAMEFOR(51));
  1266. test_streq("xy", NAMEFOR(150));
  1267. test_streq("xy", NAMEFOR(190));
  1268. test_streq("??", NAMEFOR(2000));
  1269. #undef NAMEFOR
  1270. get_options()->BridgeRelay = 1;
  1271. get_options()->BridgeRecordUsageByCountry = 1;
  1272. /* Put 9 observations in AB... */
  1273. for (i=32; i < 40; ++i)
  1274. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, i, now-7200);
  1275. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, 225, now-7200);
  1276. /* and 3 observations in XY, several times. */
  1277. for (j=0; j < 10; ++j)
  1278. for (i=52; i < 55; ++i)
  1279. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, i, now-3600);
  1280. /* and 17 observations in ZZ... */
  1281. for (i=110; i < 127; ++i)
  1282. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, i, now);
  1283. s = geoip_get_client_history(GEOIP_CLIENT_CONNECT);
  1284. test_assert(s);
  1285. test_streq("zz=24,ab=16,xy=8", s);
  1286. tor_free(s);
  1287. /* Now clear out all the AB observations. */
  1288. geoip_remove_old_clients(now-6000);
  1289. s = geoip_get_client_history(GEOIP_CLIENT_CONNECT);
  1290. test_assert(s);
  1291. test_streq("zz=24,xy=8", s);
  1292. done:
  1293. tor_free(s);
  1294. }
  1295. /** Run unit tests for stats code. */
  1296. static void
  1297. test_stats(void)
  1298. {
  1299. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  1300. char *s = NULL;
  1301. int i;
  1302. /* Start with testing exit port statistics; we shouldn't collect exit
  1303. * stats without initializing them. */
  1304. rep_hist_note_exit_stream_opened(80);
  1305. rep_hist_note_exit_bytes(80, 100, 10000);
  1306. s = rep_hist_format_exit_stats(now + 86400);
  1307. test_assert(!s);
  1308. /* Initialize stats, note some streams and bytes, and generate history
  1309. * string. */
  1310. rep_hist_exit_stats_init(now);
  1311. rep_hist_note_exit_stream_opened(80);
  1312. rep_hist_note_exit_bytes(80, 100, 10000);
  1313. rep_hist_note_exit_stream_opened(443);
  1314. rep_hist_note_exit_bytes(443, 100, 10000);
  1315. rep_hist_note_exit_bytes(443, 100, 10000);
  1316. s = rep_hist_format_exit_stats(now + 86400);
  1317. test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1318. "exit-kibibytes-written 80=1,443=1,other=0\n"
  1319. "exit-kibibytes-read 80=10,443=20,other=0\n"
  1320. "exit-streams-opened 80=4,443=4,other=0\n", s);
  1321. tor_free(s);
  1322. /* Add a few bytes on 10 more ports and ensure that only the top 10
  1323. * ports are contained in the history string. */
  1324. for (i = 50; i < 60; i++) {
  1325. rep_hist_note_exit_bytes(i, i, i);
  1326. rep_hist_note_exit_stream_opened(i);
  1327. }
  1328. s = rep_hist_format_exit_stats(now + 86400);
  1329. test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1330. "exit-kibibytes-written 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
  1331. "59=1,80=1,443=1,other=1\n"
  1332. "exit-kibibytes-read 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
  1333. "59=1,80=10,443=20,other=1\n"
  1334. "exit-streams-opened 52=4,53=4,54=4,55=4,56=4,57=4,58=4,"
  1335. "59=4,80=4,443=4,other=4\n", s);
  1336. tor_free(s);
  1337. /* Stop collecting stats, add some bytes, and ensure we don't generate
  1338. * a history string. */
  1339. rep_hist_exit_stats_term();
  1340. rep_hist_note_exit_bytes(80, 100, 10000);
  1341. s = rep_hist_format_exit_stats(now + 86400);
  1342. test_assert(!s);
  1343. /* Re-start stats, add some bytes, reset stats, and see what history we
  1344. * get when observing no streams or bytes at all. */
  1345. rep_hist_exit_stats_init(now);
  1346. rep_hist_note_exit_stream_opened(80);
  1347. rep_hist_note_exit_bytes(80, 100, 10000);
  1348. rep_hist_reset_exit_stats(now);
  1349. s = rep_hist_format_exit_stats(now + 86400);
  1350. test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1351. "exit-kibibytes-written other=0\n"
  1352. "exit-kibibytes-read other=0\n"
  1353. "exit-streams-opened other=0\n", s);
  1354. tor_free(s);
  1355. /* Continue with testing connection statistics; we shouldn't collect
  1356. * conn stats without initializing them. */
  1357. rep_hist_note_or_conn_bytes(1, 20, 400, now);
  1358. s = rep_hist_format_conn_stats(now + 86400);
  1359. test_assert(!s);
  1360. /* Initialize stats, note bytes, and generate history string. */
  1361. rep_hist_conn_stats_init(now);
  1362. rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
  1363. rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
  1364. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
  1365. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  1366. s = rep_hist_format_conn_stats(now + 86400);
  1367. test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n", s);
  1368. tor_free(s);
  1369. /* Stop collecting stats, add some bytes, and ensure we don't generate
  1370. * a history string. */
  1371. rep_hist_conn_stats_term();
  1372. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  1373. s = rep_hist_format_conn_stats(now + 86400);
  1374. test_assert(!s);
  1375. /* Re-start stats, add some bytes, reset stats, and see what history we
  1376. * get when observing no bytes at all. */
  1377. rep_hist_conn_stats_init(now);
  1378. rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
  1379. rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
  1380. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
  1381. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  1382. rep_hist_reset_conn_stats(now);
  1383. s = rep_hist_format_conn_stats(now + 86400);
  1384. test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n", s);
  1385. done:
  1386. tor_free(s);
  1387. }
  1388. static void *
  1389. legacy_test_setup(const struct testcase_t *testcase)
  1390. {
  1391. return testcase->setup_data;
  1392. }
  1393. void
  1394. legacy_test_helper(void *data)
  1395. {
  1396. void (*fn)(void) = data;
  1397. fn();
  1398. }
  1399. static int
  1400. legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
  1401. {
  1402. (void)ptr;
  1403. (void)testcase;
  1404. return 1;
  1405. }
  1406. const struct testcase_setup_t legacy_setup = {
  1407. legacy_test_setup, legacy_test_cleanup
  1408. };
  1409. #define ENT(name) \
  1410. { #name, legacy_test_helper, 0, &legacy_setup, test_ ## name }
  1411. #define SUBENT(group, name) \
  1412. { #group "_" #name, legacy_test_helper, 0, &legacy_setup, \
  1413. test_ ## group ## _ ## name }
  1414. #define DISABLED(name) \
  1415. { #name, legacy_test_helper, TT_SKIP, &legacy_setup, name }
  1416. #define FORK(name) \
  1417. { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_ ## name }
  1418. static struct testcase_t test_array[] = {
  1419. ENT(buffers),
  1420. ENT(onion_handshake),
  1421. ENT(circuit_timeout),
  1422. ENT(policies),
  1423. ENT(rend_fns),
  1424. ENT(geoip),
  1425. FORK(stats),
  1426. DISABLED(bench_aes),
  1427. DISABLED(bench_dmap),
  1428. END_OF_TESTCASES
  1429. };
  1430. #define SOCKSENT(name) \
  1431. { #name, test_socks_##name, TT_FORK, &socks_setup, NULL }
  1432. static struct testcase_t socks_tests[] = {
  1433. SOCKSENT(4_unsupported_commands),
  1434. SOCKSENT(4_supported_commands),
  1435. SOCKSENT(5_unsupported_commands),
  1436. SOCKSENT(5_supported_commands),
  1437. SOCKSENT(5_no_authenticate),
  1438. SOCKSENT(5_auth_before_negotiation),
  1439. SOCKSENT(5_authenticate),
  1440. SOCKSENT(5_authenticate_with_data),
  1441. END_OF_TESTCASES
  1442. };
  1443. extern struct testcase_t addr_tests[];
  1444. extern struct testcase_t crypto_tests[];
  1445. extern struct testcase_t container_tests[];
  1446. extern struct testcase_t util_tests[];
  1447. extern struct testcase_t dir_tests[];
  1448. extern struct testcase_t microdesc_tests[];
  1449. static struct testgroup_t testgroups[] = {
  1450. { "", test_array },
  1451. { "socks/", socks_tests },
  1452. { "addr/", addr_tests },
  1453. { "crypto/", crypto_tests },
  1454. { "container/", container_tests },
  1455. { "util/", util_tests },
  1456. { "dir/", dir_tests },
  1457. { "dir/md/", microdesc_tests },
  1458. END_OF_GROUPS
  1459. };
  1460. /** Main entry point for unit test code: parse the command line, and run
  1461. * some unit tests. */
  1462. int
  1463. main(int c, const char **v)
  1464. {
  1465. or_options_t *options;
  1466. char *errmsg = NULL;
  1467. int i, i_out;
  1468. int loglevel = LOG_ERR;
  1469. #ifdef USE_DMALLOC
  1470. {
  1471. int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
  1472. tor_assert(r);
  1473. }
  1474. #endif
  1475. update_approx_time(time(NULL));
  1476. options = options_new();
  1477. tor_threads_init();
  1478. init_logging();
  1479. for (i_out = i = 1; i < c; ++i) {
  1480. if (!strcmp(v[i], "--warn")) {
  1481. loglevel = LOG_WARN;
  1482. } else if (!strcmp(v[i], "--notice")) {
  1483. loglevel = LOG_NOTICE;
  1484. } else if (!strcmp(v[i], "--info")) {
  1485. loglevel = LOG_INFO;
  1486. } else if (!strcmp(v[i], "--debug")) {
  1487. loglevel = LOG_DEBUG;
  1488. } else {
  1489. v[i_out++] = v[i];
  1490. }
  1491. }
  1492. c = i_out;
  1493. {
  1494. log_severity_list_t s;
  1495. memset(&s, 0, sizeof(s));
  1496. set_log_severity_config(loglevel, LOG_ERR, &s);
  1497. add_stream_log(&s, "", fileno(stdout));
  1498. }
  1499. options->command = CMD_RUN_UNITTESTS;
  1500. crypto_global_init(0, NULL, NULL);
  1501. rep_hist_init();
  1502. network_init();
  1503. setup_directory();
  1504. options_init(options);
  1505. options->DataDirectory = tor_strdup(temp_dir);
  1506. options->EntryStatistics = 1;
  1507. if (set_options(options, &errmsg) < 0) {
  1508. printf("Failed to set initial options: %s\n", errmsg);
  1509. tor_free(errmsg);
  1510. return 1;
  1511. }
  1512. crypto_seed_rng(1);
  1513. atexit(remove_directory);
  1514. have_failed = (tinytest_main(c, v, testgroups) != 0);
  1515. free_pregenerated_keys();
  1516. #ifdef USE_DMALLOC
  1517. tor_free_all(0);
  1518. dmalloc_log_unfreed();
  1519. #endif
  1520. if (have_failed)
  1521. return 1;
  1522. else
  1523. return 0;
  1524. }