test.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2012, 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 BUFFERS_PRIVATE
  26. #define CONFIG_PRIVATE
  27. #define GEOIP_PRIVATE
  28. #define ROUTER_PRIVATE
  29. #define CIRCUITSTATS_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 "circuitstats.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. #ifdef _WIN32
  64. #define pid_t int
  65. #endif
  66. static pid_t temp_dir_setup_in_pid = 0;
  67. /** Select and create the temporary directory we'll use to run our unit tests.
  68. * Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
  69. * idempotent. */
  70. static void
  71. setup_directory(void)
  72. {
  73. static int is_setup = 0;
  74. int r;
  75. if (is_setup) return;
  76. #ifdef _WIN32
  77. {
  78. char buf[MAX_PATH];
  79. const char *tmp = buf;
  80. /* If this fails, we're probably screwed anyway */
  81. if (!GetTempPathA(sizeof(buf),buf))
  82. tmp = "c:\\windows\\temp";
  83. tor_snprintf(temp_dir, sizeof(temp_dir),
  84. "%s\\tor_test_%d", tmp, (int)getpid());
  85. r = mkdir(temp_dir);
  86. }
  87. #else
  88. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
  89. r = mkdir(temp_dir, 0700);
  90. #endif
  91. if (r) {
  92. fprintf(stderr, "Can't create directory %s:", temp_dir);
  93. perror("");
  94. exit(1);
  95. }
  96. is_setup = 1;
  97. temp_dir_setup_in_pid = getpid();
  98. }
  99. /** Return a filename relative to our testing temporary directory */
  100. const char *
  101. get_fname(const char *name)
  102. {
  103. static char buf[1024];
  104. setup_directory();
  105. if (!name)
  106. return temp_dir;
  107. tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
  108. return buf;
  109. }
  110. /* Remove a directory and all of its subdirectories */
  111. static void
  112. rm_rf(const char *dir)
  113. {
  114. struct stat st;
  115. smartlist_t *elements;
  116. elements = tor_listdir(dir);
  117. if (elements) {
  118. SMARTLIST_FOREACH_BEGIN(elements, const char *, cp) {
  119. char *tmp = NULL;
  120. tor_asprintf(&tmp, "%s"PATH_SEPARATOR"%s", dir, cp);
  121. if (0 == stat(tmp,&st) && (st.st_mode & S_IFDIR)) {
  122. rm_rf(tmp);
  123. } else {
  124. if (unlink(tmp)) {
  125. fprintf(stderr, "Error removing %s: %s\n", tmp, strerror(errno));
  126. }
  127. }
  128. tor_free(tmp);
  129. } SMARTLIST_FOREACH_END(cp);
  130. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  131. smartlist_free(elements);
  132. }
  133. if (rmdir(dir))
  134. fprintf(stderr, "Error removing directory %s: %s\n", dir, strerror(errno));
  135. }
  136. /** Remove all files stored under the temporary directory, and the directory
  137. * itself. Called by atexit(). */
  138. static void
  139. remove_directory(void)
  140. {
  141. if (getpid() != temp_dir_setup_in_pid) {
  142. /* Only clean out the tempdir when the main process is exiting. */
  143. return;
  144. }
  145. rm_rf(temp_dir);
  146. }
  147. /** Define this if unit tests spend too much time generating public keys*/
  148. #undef CACHE_GENERATED_KEYS
  149. static crypto_pk_t *pregen_keys[5] = {NULL, NULL, NULL, NULL, NULL};
  150. #define N_PREGEN_KEYS ((int)(sizeof(pregen_keys)/sizeof(pregen_keys[0])))
  151. /** Generate and return a new keypair for use in unit tests. If we're using
  152. * the key cache optimization, we might reuse keys: we only guarantee that
  153. * keys made with distinct values for <b>idx</b> are different. The value of
  154. * <b>idx</b> must be at least 0, and less than N_PREGEN_KEYS. */
  155. crypto_pk_t *
  156. pk_generate(int idx)
  157. {
  158. #ifdef CACHE_GENERATED_KEYS
  159. tor_assert(idx < N_PREGEN_KEYS);
  160. if (! pregen_keys[idx]) {
  161. pregen_keys[idx] = crypto_pk_new();
  162. tor_assert(!crypto_pk_generate_key(pregen_keys[idx]));
  163. }
  164. return crypto_pk_dup_key(pregen_keys[idx]);
  165. #else
  166. crypto_pk_t *result;
  167. (void) idx;
  168. result = crypto_pk_new();
  169. tor_assert(!crypto_pk_generate_key(result));
  170. return result;
  171. #endif
  172. }
  173. /** Free all storage used for the cached key optimization. */
  174. static void
  175. free_pregenerated_keys(void)
  176. {
  177. unsigned idx;
  178. for (idx = 0; idx < N_PREGEN_KEYS; ++idx) {
  179. if (pregen_keys[idx]) {
  180. crypto_pk_free(pregen_keys[idx]);
  181. pregen_keys[idx] = NULL;
  182. }
  183. }
  184. }
  185. typedef struct socks_test_data_t {
  186. socks_request_t *req;
  187. buf_t *buf;
  188. } socks_test_data_t;
  189. static void *
  190. socks_test_setup(const struct testcase_t *testcase)
  191. {
  192. socks_test_data_t *data = tor_malloc(sizeof(socks_test_data_t));
  193. (void)testcase;
  194. data->buf = buf_new_with_capacity(256);
  195. data->req = socks_request_new();
  196. config_register_addressmaps(get_options());
  197. return data;
  198. }
  199. static int
  200. socks_test_cleanup(const struct testcase_t *testcase, void *ptr)
  201. {
  202. socks_test_data_t *data = ptr;
  203. (void)testcase;
  204. buf_free(data->buf);
  205. socks_request_free(data->req);
  206. tor_free(data);
  207. return 1;
  208. }
  209. const struct testcase_setup_t socks_setup = {
  210. socks_test_setup, socks_test_cleanup
  211. };
  212. #define SOCKS_TEST_INIT() \
  213. socks_test_data_t *testdata = ptr; \
  214. buf_t *buf = testdata->buf; \
  215. socks_request_t *socks = testdata->req;
  216. #define ADD_DATA(buf, s) \
  217. write_to_buf(s, sizeof(s)-1, buf)
  218. static void
  219. socks_request_clear(socks_request_t *socks)
  220. {
  221. tor_free(socks->username);
  222. tor_free(socks->password);
  223. memset(socks, 0, sizeof(socks_request_t));
  224. }
  225. /** Perform unsupported SOCKS 4 commands */
  226. static void
  227. test_socks_4_unsupported_commands(void *ptr)
  228. {
  229. SOCKS_TEST_INIT();
  230. /* SOCKS 4 Send BIND [02] to IP address 2.2.2.2:4369 */
  231. ADD_DATA(buf, "\x04\x02\x11\x11\x02\x02\x02\x02\x00");
  232. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  233. get_options()->SafeSocks) == -1);
  234. test_eq(4, socks->socks_version);
  235. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  236. done:
  237. ;
  238. }
  239. /** Perform supported SOCKS 4 commands */
  240. static void
  241. test_socks_4_supported_commands(void *ptr)
  242. {
  243. SOCKS_TEST_INIT();
  244. test_eq(0, buf_datalen(buf));
  245. /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4370 */
  246. ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x03\x00");
  247. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  248. get_options()->SafeSocks) == 1);
  249. test_eq(4, socks->socks_version);
  250. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  251. test_eq(SOCKS_COMMAND_CONNECT, socks->command);
  252. test_streq("2.2.2.3", socks->address);
  253. test_eq(4370, socks->port);
  254. test_assert(socks->got_auth == 0);
  255. test_assert(! socks->username);
  256. test_eq(0, buf_datalen(buf));
  257. socks_request_clear(socks);
  258. /* SOCKS 4 Send CONNECT [01] to IP address 2.2.2.2:4369 with userid*/
  259. ADD_DATA(buf, "\x04\x01\x11\x12\x02\x02\x02\x04me\x00");
  260. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  261. get_options()->SafeSocks) == 1);
  262. test_eq(4, socks->socks_version);
  263. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  264. test_eq(SOCKS_COMMAND_CONNECT, socks->command);
  265. test_streq("2.2.2.4", socks->address);
  266. test_eq(4370, socks->port);
  267. test_assert(socks->got_auth == 1);
  268. test_assert(socks->username);
  269. test_eq(2, socks->usernamelen);
  270. test_memeq("me", socks->username, 2);
  271. test_eq(0, buf_datalen(buf));
  272. socks_request_clear(socks);
  273. /* SOCKS 4a Send RESOLVE [F0] request for torproject.org */
  274. ADD_DATA(buf, "\x04\xF0\x01\x01\x00\x00\x00\x02me\x00torproject.org\x00");
  275. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  276. get_options()->SafeSocks) == 1);
  277. test_eq(4, socks->socks_version);
  278. test_eq(0, socks->replylen); /* XXX: shouldn't tor reply? */
  279. test_streq("torproject.org", socks->address);
  280. test_eq(0, buf_datalen(buf));
  281. done:
  282. ;
  283. }
  284. /** Perform unsupported SOCKS 5 commands */
  285. static void
  286. test_socks_5_unsupported_commands(void *ptr)
  287. {
  288. SOCKS_TEST_INIT();
  289. /* SOCKS 5 Send unsupported BIND [02] command */
  290. ADD_DATA(buf, "\x05\x02\x00\x01");
  291. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  292. get_options()->SafeSocks), 0);
  293. test_eq(0, buf_datalen(buf));
  294. test_eq(5, socks->socks_version);
  295. test_eq(2, socks->replylen);
  296. test_eq(5, socks->reply[0]);
  297. test_eq(0, socks->reply[1]);
  298. ADD_DATA(buf, "\x05\x02\x00\x01\x02\x02\x02\x01\x01\x01");
  299. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  300. get_options()->SafeSocks), -1);
  301. /* XXX: shouldn't tor reply 'command not supported' [07]? */
  302. buf_clear(buf);
  303. socks_request_clear(socks);
  304. /* SOCKS 5 Send unsupported UDP_ASSOCIATE [03] command */
  305. ADD_DATA(buf, "\x05\x03\x00\x01\x02");
  306. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  307. get_options()->SafeSocks), 0);
  308. test_eq(5, socks->socks_version);
  309. test_eq(2, socks->replylen);
  310. test_eq(5, socks->reply[0]);
  311. test_eq(0, socks->reply[1]);
  312. ADD_DATA(buf, "\x05\x03\x00\x01\x02\x02\x02\x01\x01\x01");
  313. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  314. get_options()->SafeSocks), -1);
  315. /* XXX: shouldn't tor reply 'command not supported' [07]? */
  316. done:
  317. ;
  318. }
  319. /** Perform supported SOCKS 5 commands */
  320. static void
  321. test_socks_5_supported_commands(void *ptr)
  322. {
  323. SOCKS_TEST_INIT();
  324. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  325. ADD_DATA(buf, "\x05\x01\x00");
  326. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  327. get_options()->SafeSocks), 0);
  328. test_eq(5, socks->socks_version);
  329. test_eq(2, socks->replylen);
  330. test_eq(5, socks->reply[0]);
  331. test_eq(0, socks->reply[1]);
  332. ADD_DATA(buf, "\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  333. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  334. get_options()->SafeSocks), 1);
  335. test_streq("2.2.2.2", socks->address);
  336. test_eq(4369, socks->port);
  337. test_eq(0, buf_datalen(buf));
  338. socks_request_clear(socks);
  339. /* SOCKS 5 Send CONNECT [01] to FQDN torproject.org:4369 */
  340. ADD_DATA(buf, "\x05\x01\x00");
  341. ADD_DATA(buf, "\x05\x01\x00\x03\x0Etorproject.org\x11\x11");
  342. test_eq(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  343. get_options()->SafeSocks), 1);
  344. test_eq(5, socks->socks_version);
  345. test_eq(2, socks->replylen);
  346. test_eq(5, socks->reply[0]);
  347. test_eq(0, socks->reply[1]);
  348. test_streq("torproject.org", socks->address);
  349. test_eq(4369, socks->port);
  350. test_eq(0, buf_datalen(buf));
  351. socks_request_clear(socks);
  352. /* SOCKS 5 Send RESOLVE [F0] request for torproject.org:4369 */
  353. ADD_DATA(buf, "\x05\x01\x00");
  354. ADD_DATA(buf, "\x05\xF0\x00\x03\x0Etorproject.org\x01\x02");
  355. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  356. get_options()->SafeSocks) == 1);
  357. test_eq(5, socks->socks_version);
  358. test_eq(2, socks->replylen);
  359. test_eq(5, socks->reply[0]);
  360. test_eq(0, socks->reply[1]);
  361. test_streq("torproject.org", socks->address);
  362. test_eq(0, buf_datalen(buf));
  363. socks_request_clear(socks);
  364. /* SOCKS 5 Send RESOLVE_PTR [F1] for IP address 2.2.2.5 */
  365. ADD_DATA(buf, "\x05\x01\x00");
  366. ADD_DATA(buf, "\x05\xF1\x00\x01\x02\x02\x02\x05\x01\x03");
  367. test_assert(fetch_from_buf_socks(buf, socks, get_options()->TestSocks,
  368. get_options()->SafeSocks) == 1);
  369. test_eq(5, socks->socks_version);
  370. test_eq(2, socks->replylen);
  371. test_eq(5, socks->reply[0]);
  372. test_eq(0, socks->reply[1]);
  373. test_streq("2.2.2.5", socks->address);
  374. test_eq(0, buf_datalen(buf));
  375. done:
  376. ;
  377. }
  378. /** Perform SOCKS 5 authentication */
  379. static void
  380. test_socks_5_no_authenticate(void *ptr)
  381. {
  382. SOCKS_TEST_INIT();
  383. /*SOCKS 5 No Authentication */
  384. ADD_DATA(buf,"\x05\x01\x00");
  385. test_assert(!fetch_from_buf_socks(buf, socks,
  386. get_options()->TestSocks,
  387. get_options()->SafeSocks));
  388. test_eq(2, socks->replylen);
  389. test_eq(5, socks->reply[0]);
  390. test_eq(SOCKS_NO_AUTH, socks->reply[1]);
  391. test_eq(0, buf_datalen(buf));
  392. /*SOCKS 5 Send username/password anyway - pretend to be broken */
  393. ADD_DATA(buf,"\x01\x02\x01\x01\x02\x01\x01");
  394. test_assert(!fetch_from_buf_socks(buf, socks,
  395. get_options()->TestSocks,
  396. get_options()->SafeSocks));
  397. test_eq(5, socks->socks_version);
  398. test_eq(2, socks->replylen);
  399. test_eq(5, socks->reply[0]);
  400. test_eq(0, socks->reply[1]);
  401. test_eq(2, socks->usernamelen);
  402. test_eq(2, socks->passwordlen);
  403. test_memeq("\x01\x01", socks->username, 2);
  404. test_memeq("\x01\x01", socks->password, 2);
  405. done:
  406. ;
  407. }
  408. /** Perform SOCKS 5 authentication */
  409. static void
  410. test_socks_5_authenticate(void *ptr)
  411. {
  412. SOCKS_TEST_INIT();
  413. /* SOCKS 5 Negotiate username/password authentication */
  414. ADD_DATA(buf, "\x05\x01\x02");
  415. test_assert(!fetch_from_buf_socks(buf, socks,
  416. get_options()->TestSocks,
  417. get_options()->SafeSocks));
  418. test_eq(2, socks->replylen);
  419. test_eq(5, socks->reply[0]);
  420. test_eq(SOCKS_USER_PASS, socks->reply[1]);
  421. test_eq(5, socks->socks_version);
  422. test_eq(0, buf_datalen(buf));
  423. /* SOCKS 5 Send username/password */
  424. ADD_DATA(buf, "\x01\x02me\x08mypasswd");
  425. test_assert(!fetch_from_buf_socks(buf, socks,
  426. get_options()->TestSocks,
  427. get_options()->SafeSocks));
  428. test_eq(5, socks->socks_version);
  429. test_eq(2, socks->replylen);
  430. test_eq(5, socks->reply[0]);
  431. test_eq(0, socks->reply[1]);
  432. test_eq(2, socks->usernamelen);
  433. test_eq(8, socks->passwordlen);
  434. test_memeq("me", socks->username, 2);
  435. test_memeq("mypasswd", socks->password, 8);
  436. done:
  437. ;
  438. }
  439. /** Perform SOCKS 5 authentication and send data all in one go */
  440. static void
  441. test_socks_5_authenticate_with_data(void *ptr)
  442. {
  443. SOCKS_TEST_INIT();
  444. /* SOCKS 5 Negotiate username/password authentication */
  445. ADD_DATA(buf, "\x05\x01\x02");
  446. test_assert(!fetch_from_buf_socks(buf, socks,
  447. get_options()->TestSocks,
  448. get_options()->SafeSocks));
  449. test_eq(2, socks->replylen);
  450. test_eq(5, socks->reply[0]);
  451. test_eq(SOCKS_USER_PASS, socks->reply[1]);
  452. test_eq(5, socks->socks_version);
  453. test_eq(0, buf_datalen(buf));
  454. /* SOCKS 5 Send username/password */
  455. /* SOCKS 5 Send CONNECT [01] to IP address 2.2.2.2:4369 */
  456. ADD_DATA(buf, "\x01\x02me\x03you\x05\x01\x00\x01\x02\x02\x02\x02\x11\x11");
  457. test_assert(fetch_from_buf_socks(buf, socks,
  458. get_options()->TestSocks,
  459. get_options()->SafeSocks) == 1);
  460. test_eq(5, socks->socks_version);
  461. test_eq(2, socks->replylen);
  462. test_eq(5, socks->reply[0]);
  463. test_eq(0, socks->reply[1]);
  464. test_streq("2.2.2.2", socks->address);
  465. test_eq(4369, socks->port);
  466. test_eq(2, socks->usernamelen);
  467. test_eq(3, socks->passwordlen);
  468. test_memeq("me", socks->username, 2);
  469. test_memeq("you", socks->password, 3);
  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. static void
  491. test_buffer_copy(void *arg)
  492. {
  493. generic_buffer_t *buf=NULL, *buf2=NULL;
  494. const char *s;
  495. size_t len;
  496. char b[256];
  497. int i;
  498. (void)arg;
  499. buf = generic_buffer_new();
  500. tt_assert(buf);
  501. /* Copy an empty buffer. */
  502. tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
  503. tt_assert(buf2);
  504. tt_int_op(0, ==, generic_buffer_len(buf2));
  505. /* Now try with a short buffer. */
  506. s = "And now comes an act of enormous enormance!";
  507. len = strlen(s);
  508. generic_buffer_add(buf, s, len);
  509. tt_int_op(len, ==, generic_buffer_len(buf));
  510. /* Add junk to buf2 so we can test replacing.*/
  511. generic_buffer_add(buf2, "BLARG", 5);
  512. tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
  513. tt_int_op(len, ==, generic_buffer_len(buf2));
  514. generic_buffer_get(buf2, b, len);
  515. test_mem_op(b, ==, s, len);
  516. /* Now free buf2 and retry so we can test allocating */
  517. generic_buffer_free(buf2);
  518. buf2 = NULL;
  519. tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
  520. tt_int_op(len, ==, generic_buffer_len(buf2));
  521. generic_buffer_get(buf2, b, len);
  522. test_mem_op(b, ==, s, len);
  523. /* Clear buf for next test */
  524. generic_buffer_get(buf, b, len);
  525. tt_int_op(generic_buffer_len(buf),==,0);
  526. /* Okay, now let's try a bigger buffer. */
  527. s = "Quis autem vel eum iure reprehenderit qui in ea voluptate velit "
  528. "esse quam nihil molestiae consequatur, vel illum qui dolorem eum "
  529. "fugiat quo voluptas nulla pariatur?";
  530. len = strlen(s);
  531. for (i = 0; i < 256; ++i) {
  532. b[0]=i;
  533. generic_buffer_add(buf, b, 1);
  534. generic_buffer_add(buf, s, len);
  535. }
  536. tt_int_op(0, ==, generic_buffer_set_to_copy(&buf2, buf));
  537. tt_int_op(generic_buffer_len(buf2), ==, generic_buffer_len(buf));
  538. for (i = 0; i < 256; ++i) {
  539. generic_buffer_get(buf2, b, len+1);
  540. tt_int_op((unsigned char)b[0],==,i);
  541. test_mem_op(b+1, ==, s, len);
  542. }
  543. done:
  544. if (buf)
  545. generic_buffer_free(buf);
  546. if (buf2)
  547. generic_buffer_free(buf2);
  548. }
  549. /** Run unit tests for buffers.c */
  550. static void
  551. test_buffers(void)
  552. {
  553. char str[256];
  554. char str2[256];
  555. buf_t *buf = NULL, *buf2 = NULL;
  556. const char *cp;
  557. int j;
  558. size_t r;
  559. /****
  560. * buf_new
  561. ****/
  562. if (!(buf = buf_new()))
  563. test_fail();
  564. //test_eq(buf_capacity(buf), 4096);
  565. test_eq(buf_datalen(buf), 0);
  566. /****
  567. * General pointer frobbing
  568. */
  569. for (j=0;j<256;++j) {
  570. str[j] = (char)j;
  571. }
  572. write_to_buf(str, 256, buf);
  573. write_to_buf(str, 256, buf);
  574. test_eq(buf_datalen(buf), 512);
  575. fetch_from_buf(str2, 200, buf);
  576. test_memeq(str, str2, 200);
  577. test_eq(buf_datalen(buf), 312);
  578. memset(str2, 0, sizeof(str2));
  579. fetch_from_buf(str2, 256, buf);
  580. test_memeq(str+200, str2, 56);
  581. test_memeq(str, str2+56, 200);
  582. test_eq(buf_datalen(buf), 56);
  583. memset(str2, 0, sizeof(str2));
  584. /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
  585. * another 3584 bytes, we hit the end. */
  586. for (j=0;j<15;++j) {
  587. write_to_buf(str, 256, buf);
  588. }
  589. assert_buf_ok(buf);
  590. test_eq(buf_datalen(buf), 3896);
  591. fetch_from_buf(str2, 56, buf);
  592. test_eq(buf_datalen(buf), 3840);
  593. test_memeq(str+200, str2, 56);
  594. for (j=0;j<15;++j) {
  595. memset(str2, 0, sizeof(str2));
  596. fetch_from_buf(str2, 256, buf);
  597. test_memeq(str, str2, 256);
  598. }
  599. test_eq(buf_datalen(buf), 0);
  600. buf_free(buf);
  601. buf = NULL;
  602. /* Okay, now make sure growing can work. */
  603. buf = buf_new_with_capacity(16);
  604. //test_eq(buf_capacity(buf), 16);
  605. write_to_buf(str+1, 255, buf);
  606. //test_eq(buf_capacity(buf), 256);
  607. fetch_from_buf(str2, 254, buf);
  608. test_memeq(str+1, str2, 254);
  609. //test_eq(buf_capacity(buf), 256);
  610. assert_buf_ok(buf);
  611. write_to_buf(str, 32, buf);
  612. //test_eq(buf_capacity(buf), 256);
  613. assert_buf_ok(buf);
  614. write_to_buf(str, 256, buf);
  615. assert_buf_ok(buf);
  616. //test_eq(buf_capacity(buf), 512);
  617. test_eq(buf_datalen(buf), 33+256);
  618. fetch_from_buf(str2, 33, buf);
  619. test_eq(*str2, str[255]);
  620. test_memeq(str2+1, str, 32);
  621. //test_eq(buf_capacity(buf), 512);
  622. test_eq(buf_datalen(buf), 256);
  623. fetch_from_buf(str2, 256, buf);
  624. test_memeq(str, str2, 256);
  625. /* now try shrinking: case 1. */
  626. buf_free(buf);
  627. buf = buf_new_with_capacity(33668);
  628. for (j=0;j<67;++j) {
  629. write_to_buf(str,255, buf);
  630. }
  631. //test_eq(buf_capacity(buf), 33668);
  632. test_eq(buf_datalen(buf), 17085);
  633. for (j=0; j < 40; ++j) {
  634. fetch_from_buf(str2, 255,buf);
  635. test_memeq(str2, str, 255);
  636. }
  637. /* now try shrinking: case 2. */
  638. buf_free(buf);
  639. buf = buf_new_with_capacity(33668);
  640. for (j=0;j<67;++j) {
  641. write_to_buf(str,255, buf);
  642. }
  643. for (j=0; j < 20; ++j) {
  644. fetch_from_buf(str2, 255,buf);
  645. test_memeq(str2, str, 255);
  646. }
  647. for (j=0;j<80;++j) {
  648. write_to_buf(str,255, buf);
  649. }
  650. //test_eq(buf_capacity(buf),33668);
  651. for (j=0; j < 120; ++j) {
  652. fetch_from_buf(str2, 255,buf);
  653. test_memeq(str2, str, 255);
  654. }
  655. /* Move from buf to buf. */
  656. buf_free(buf);
  657. buf = buf_new_with_capacity(4096);
  658. buf2 = buf_new_with_capacity(4096);
  659. for (j=0;j<100;++j)
  660. write_to_buf(str, 255, buf);
  661. test_eq(buf_datalen(buf), 25500);
  662. for (j=0;j<100;++j) {
  663. r = 10;
  664. move_buf_to_buf(buf2, buf, &r);
  665. test_eq(r, 0);
  666. }
  667. test_eq(buf_datalen(buf), 24500);
  668. test_eq(buf_datalen(buf2), 1000);
  669. for (j=0;j<3;++j) {
  670. fetch_from_buf(str2, 255, buf2);
  671. test_memeq(str2, str, 255);
  672. }
  673. r = 8192; /*big move*/
  674. move_buf_to_buf(buf2, buf, &r);
  675. test_eq(r, 0);
  676. r = 30000; /* incomplete move */
  677. move_buf_to_buf(buf2, buf, &r);
  678. test_eq(r, 13692);
  679. for (j=0;j<97;++j) {
  680. fetch_from_buf(str2, 255, buf2);
  681. test_memeq(str2, str, 255);
  682. }
  683. buf_free(buf);
  684. buf_free(buf2);
  685. buf = buf2 = NULL;
  686. buf = buf_new_with_capacity(5);
  687. cp = "Testing. This is a moderately long Testing string.";
  688. for (j = 0; cp[j]; j++)
  689. write_to_buf(cp+j, 1, buf);
  690. test_eq(0, buf_find_string_offset(buf, "Testing", 7));
  691. test_eq(1, buf_find_string_offset(buf, "esting", 6));
  692. test_eq(1, buf_find_string_offset(buf, "est", 3));
  693. test_eq(39, buf_find_string_offset(buf, "ing str", 7));
  694. test_eq(35, buf_find_string_offset(buf, "Testing str", 11));
  695. test_eq(32, buf_find_string_offset(buf, "ng ", 3));
  696. test_eq(43, buf_find_string_offset(buf, "string.", 7));
  697. test_eq(-1, buf_find_string_offset(buf, "shrdlu", 6));
  698. test_eq(-1, buf_find_string_offset(buf, "Testing thing", 13));
  699. test_eq(-1, buf_find_string_offset(buf, "ngx", 3));
  700. buf_free(buf);
  701. buf = NULL;
  702. done:
  703. if (buf)
  704. buf_free(buf);
  705. if (buf2)
  706. buf_free(buf2);
  707. }
  708. /** Run unit tests for the onion handshake code. */
  709. static void
  710. test_onion_handshake(void)
  711. {
  712. /* client-side */
  713. crypto_dh_t *c_dh = NULL;
  714. char c_buf[ONIONSKIN_CHALLENGE_LEN];
  715. char c_keys[40];
  716. /* server-side */
  717. char s_buf[ONIONSKIN_REPLY_LEN];
  718. char s_keys[40];
  719. /* shared */
  720. crypto_pk_t *pk = NULL;
  721. pk = pk_generate(0);
  722. /* client handshake 1. */
  723. memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
  724. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  725. /* server handshake */
  726. memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
  727. memset(s_keys, 0, 40);
  728. test_assert(! onion_skin_server_handshake(c_buf, pk, NULL,
  729. s_buf, s_keys, 40));
  730. /* client handshake 2 */
  731. memset(c_keys, 0, 40);
  732. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  733. if (memcmp(c_keys, s_keys, 40)) {
  734. puts("Aiiiie");
  735. exit(1);
  736. }
  737. test_memeq(c_keys, s_keys, 40);
  738. memset(s_buf, 0, 40);
  739. test_memneq(c_keys, s_buf, 40);
  740. done:
  741. if (c_dh)
  742. crypto_dh_free(c_dh);
  743. if (pk)
  744. crypto_pk_free(pk);
  745. }
  746. static void
  747. test_circuit_timeout(void)
  748. {
  749. /* Plan:
  750. * 1. Generate 1000 samples
  751. * 2. Estimate parameters
  752. * 3. If difference, repeat
  753. * 4. Save state
  754. * 5. load state
  755. * 6. Estimate parameters
  756. * 7. compare differences
  757. */
  758. circuit_build_times_t initial;
  759. circuit_build_times_t estimate;
  760. circuit_build_times_t final;
  761. double timeout1, timeout2;
  762. or_state_t state;
  763. int i, runs;
  764. double close_ms;
  765. circuit_build_times_init(&initial);
  766. circuit_build_times_init(&estimate);
  767. circuit_build_times_init(&final);
  768. memset(&state, 0, sizeof(or_state_t));
  769. circuitbuild_running_unit_tests();
  770. #define timeout0 (build_time_t)(30*1000.0)
  771. initial.Xm = 3000;
  772. circuit_build_times_initial_alpha(&initial,
  773. CBT_DEFAULT_QUANTILE_CUTOFF/100.0,
  774. timeout0);
  775. close_ms = MAX(circuit_build_times_calculate_timeout(&initial,
  776. CBT_DEFAULT_CLOSE_QUANTILE/100.0),
  777. CBT_DEFAULT_TIMEOUT_INITIAL_VALUE);
  778. do {
  779. for (i=0; i < CBT_DEFAULT_MIN_CIRCUITS_TO_OBSERVE; i++) {
  780. build_time_t sample = circuit_build_times_generate_sample(&initial,0,1);
  781. if (sample > close_ms) {
  782. circuit_build_times_add_time(&estimate, CBT_BUILD_ABANDONED);
  783. } else {
  784. circuit_build_times_add_time(&estimate, sample);
  785. }
  786. }
  787. circuit_build_times_update_alpha(&estimate);
  788. timeout1 = circuit_build_times_calculate_timeout(&estimate,
  789. CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
  790. circuit_build_times_set_timeout(&estimate);
  791. log_notice(LD_CIRC, "Timeout1 is %f, Xm is %d", timeout1, estimate.Xm);
  792. /* 2% error */
  793. } while (fabs(circuit_build_times_cdf(&initial, timeout0) -
  794. circuit_build_times_cdf(&initial, timeout1)) > 0.02);
  795. test_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
  796. circuit_build_times_update_state(&estimate, &state);
  797. test_assert(circuit_build_times_parse_state(&final, &state) == 0);
  798. circuit_build_times_update_alpha(&final);
  799. timeout2 = circuit_build_times_calculate_timeout(&final,
  800. CBT_DEFAULT_QUANTILE_CUTOFF/100.0);
  801. circuit_build_times_set_timeout(&final);
  802. log_notice(LD_CIRC, "Timeout2 is %f, Xm is %d", timeout2, final.Xm);
  803. /* 5% here because some accuracy is lost due to histogram conversion */
  804. test_assert(fabs(circuit_build_times_cdf(&initial, timeout0) -
  805. circuit_build_times_cdf(&initial, timeout2)) < 0.05);
  806. for (runs = 0; runs < 50; runs++) {
  807. int build_times_idx = 0;
  808. int total_build_times = 0;
  809. final.close_ms = final.timeout_ms = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
  810. estimate.close_ms = estimate.timeout_ms
  811. = CBT_DEFAULT_TIMEOUT_INITIAL_VALUE;
  812. for (i = 0; i < CBT_DEFAULT_RECENT_CIRCUITS*2; i++) {
  813. circuit_build_times_network_circ_success(&estimate);
  814. circuit_build_times_add_time(&estimate,
  815. circuit_build_times_generate_sample(&estimate, 0,
  816. CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
  817. circuit_build_times_network_circ_success(&estimate);
  818. circuit_build_times_add_time(&final,
  819. circuit_build_times_generate_sample(&final, 0,
  820. CBT_DEFAULT_QUANTILE_CUTOFF/100.0));
  821. }
  822. test_assert(!circuit_build_times_network_check_changed(&estimate));
  823. test_assert(!circuit_build_times_network_check_changed(&final));
  824. /* Reset liveness to be non-live */
  825. final.liveness.network_last_live = 0;
  826. estimate.liveness.network_last_live = 0;
  827. build_times_idx = estimate.build_times_idx;
  828. total_build_times = estimate.total_build_times;
  829. test_assert(circuit_build_times_network_check_live(&estimate));
  830. test_assert(circuit_build_times_network_check_live(&final));
  831. circuit_build_times_count_close(&estimate, 0,
  832. (time_t)(approx_time()-estimate.close_ms/1000.0-1));
  833. circuit_build_times_count_close(&final, 0,
  834. (time_t)(approx_time()-final.close_ms/1000.0-1));
  835. test_assert(!circuit_build_times_network_check_live(&estimate));
  836. test_assert(!circuit_build_times_network_check_live(&final));
  837. log_info(LD_CIRC, "idx: %d %d, tot: %d %d",
  838. build_times_idx, estimate.build_times_idx,
  839. total_build_times, estimate.total_build_times);
  840. /* Check rollback index. Should match top of loop. */
  841. test_assert(build_times_idx == estimate.build_times_idx);
  842. // This can fail if estimate.total_build_times == 1000, because
  843. // in that case, rewind actually causes us to lose timeouts
  844. if (total_build_times != CBT_NCIRCUITS_TO_OBSERVE)
  845. test_assert(total_build_times == estimate.total_build_times);
  846. /* Now simulate that the network has become live and we need
  847. * a change */
  848. circuit_build_times_network_is_live(&estimate);
  849. circuit_build_times_network_is_live(&final);
  850. for (i = 0; i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT; i++) {
  851. circuit_build_times_count_timeout(&estimate, 1);
  852. if (i < CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1) {
  853. circuit_build_times_count_timeout(&final, 1);
  854. }
  855. }
  856. test_assert(estimate.liveness.after_firsthop_idx == 0);
  857. test_assert(final.liveness.after_firsthop_idx ==
  858. CBT_DEFAULT_MAX_RECENT_TIMEOUT_COUNT-1);
  859. test_assert(circuit_build_times_network_check_live(&estimate));
  860. test_assert(circuit_build_times_network_check_live(&final));
  861. circuit_build_times_count_timeout(&final, 1);
  862. }
  863. done:
  864. return;
  865. }
  866. /* Helper: assert that short_policy parses and writes back out as itself,
  867. or as <b>expected</b> if that's provided. */
  868. static void
  869. test_short_policy_parse(const char *input,
  870. const char *expected)
  871. {
  872. short_policy_t *short_policy = NULL;
  873. char *out = NULL;
  874. if (expected == NULL)
  875. expected = input;
  876. short_policy = parse_short_policy(input);
  877. tt_assert(short_policy);
  878. out = write_short_policy(short_policy);
  879. tt_str_op(out, ==, expected);
  880. done:
  881. tor_free(out);
  882. short_policy_free(short_policy);
  883. }
  884. /** Helper: Parse the exit policy string in <b>policy_str</b>, and make sure
  885. * that policies_summarize() produces the string <b>expected_summary</b> from
  886. * it. */
  887. static void
  888. test_policy_summary_helper(const char *policy_str,
  889. const char *expected_summary)
  890. {
  891. config_line_t line;
  892. smartlist_t *policy = smartlist_new();
  893. char *summary = NULL;
  894. char *summary_after = NULL;
  895. int r;
  896. short_policy_t *short_policy = NULL;
  897. line.key = (char*)"foo";
  898. line.value = (char *)policy_str;
  899. line.next = NULL;
  900. r = policies_parse_exit_policy(&line, &policy, 1, 0, NULL, 1);
  901. test_eq(r, 0);
  902. summary = policy_summarize(policy, AF_INET);
  903. test_assert(summary != NULL);
  904. test_streq(summary, expected_summary);
  905. short_policy = parse_short_policy(summary);
  906. tt_assert(short_policy);
  907. summary_after = write_short_policy(short_policy);
  908. test_streq(summary, summary_after);
  909. done:
  910. tor_free(summary_after);
  911. tor_free(summary);
  912. if (policy)
  913. addr_policy_list_free(policy);
  914. short_policy_free(short_policy);
  915. }
  916. /** Run unit tests for generating summary lines of exit policies */
  917. static void
  918. test_policies(void)
  919. {
  920. int i;
  921. smartlist_t *policy = NULL, *policy2 = NULL, *policy3 = NULL,
  922. *policy4 = NULL, *policy5 = NULL, *policy6 = NULL,
  923. *policy7 = NULL;
  924. addr_policy_t *p;
  925. tor_addr_t tar;
  926. config_line_t line;
  927. smartlist_t *sm = NULL;
  928. char *policy_str = NULL;
  929. policy = smartlist_new();
  930. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
  931. test_assert(p != NULL);
  932. test_eq(ADDR_POLICY_REJECT, p->policy_type);
  933. tor_addr_from_ipv4h(&tar, 0xc0a80000u);
  934. test_eq(0, tor_addr_compare(&p->addr, &tar, CMP_EXACT));
  935. test_eq(16, p->maskbits);
  936. test_eq(1, p->prt_min);
  937. test_eq(65535, p->prt_max);
  938. smartlist_add(policy, p);
  939. tor_addr_from_ipv4h(&tar, 0x01020304u);
  940. test_assert(ADDR_POLICY_ACCEPTED ==
  941. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  942. tor_addr_make_unspec(&tar);
  943. test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  944. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  945. tor_addr_from_ipv4h(&tar, 0xc0a80102);
  946. test_assert(ADDR_POLICY_REJECTED ==
  947. compare_tor_addr_to_addr_policy(&tar, 2, policy));
  948. test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1, 1, NULL, 1));
  949. test_assert(policy2);
  950. policy3 = smartlist_new();
  951. p = router_parse_addr_policy_item_from_string("reject *:*",-1);
  952. test_assert(p != NULL);
  953. smartlist_add(policy3, p);
  954. p = router_parse_addr_policy_item_from_string("accept *:*",-1);
  955. test_assert(p != NULL);
  956. smartlist_add(policy3, p);
  957. policy4 = smartlist_new();
  958. p = router_parse_addr_policy_item_from_string("accept *:443",-1);
  959. test_assert(p != NULL);
  960. smartlist_add(policy4, p);
  961. p = router_parse_addr_policy_item_from_string("accept *:443",-1);
  962. test_assert(p != NULL);
  963. smartlist_add(policy4, p);
  964. policy5 = smartlist_new();
  965. p = router_parse_addr_policy_item_from_string("reject 0.0.0.0/8:*",-1);
  966. test_assert(p != NULL);
  967. smartlist_add(policy5, p);
  968. p = router_parse_addr_policy_item_from_string("reject 169.254.0.0/16:*",-1);
  969. test_assert(p != NULL);
  970. smartlist_add(policy5, p);
  971. p = router_parse_addr_policy_item_from_string("reject 127.0.0.0/8:*",-1);
  972. test_assert(p != NULL);
  973. smartlist_add(policy5, p);
  974. p = router_parse_addr_policy_item_from_string("reject 192.168.0.0/16:*",-1);
  975. test_assert(p != NULL);
  976. smartlist_add(policy5, p);
  977. p = router_parse_addr_policy_item_from_string("reject 10.0.0.0/8:*",-1);
  978. test_assert(p != NULL);
  979. smartlist_add(policy5, p);
  980. p = router_parse_addr_policy_item_from_string("reject 172.16.0.0/12:*",-1);
  981. test_assert(p != NULL);
  982. smartlist_add(policy5, p);
  983. p = router_parse_addr_policy_item_from_string("reject 80.190.250.90:*",-1);
  984. test_assert(p != NULL);
  985. smartlist_add(policy5, p);
  986. p = router_parse_addr_policy_item_from_string("reject *:1-65534",-1);
  987. test_assert(p != NULL);
  988. smartlist_add(policy5, p);
  989. p = router_parse_addr_policy_item_from_string("reject *:65535",-1);
  990. test_assert(p != NULL);
  991. smartlist_add(policy5, p);
  992. p = router_parse_addr_policy_item_from_string("accept *:1-65535",-1);
  993. test_assert(p != NULL);
  994. smartlist_add(policy5, p);
  995. policy6 = smartlist_new();
  996. p = router_parse_addr_policy_item_from_string("accept 43.3.0.0/9:*",-1);
  997. test_assert(p != NULL);
  998. smartlist_add(policy6, p);
  999. policy7 = smartlist_new();
  1000. p = router_parse_addr_policy_item_from_string("accept 0.0.0.0/8:*",-1);
  1001. test_assert(p != NULL);
  1002. smartlist_add(policy7, p);
  1003. test_assert(!exit_policy_is_general_exit(policy));
  1004. test_assert(exit_policy_is_general_exit(policy2));
  1005. test_assert(!exit_policy_is_general_exit(NULL));
  1006. test_assert(!exit_policy_is_general_exit(policy3));
  1007. test_assert(!exit_policy_is_general_exit(policy4));
  1008. test_assert(!exit_policy_is_general_exit(policy5));
  1009. test_assert(!exit_policy_is_general_exit(policy6));
  1010. test_assert(!exit_policy_is_general_exit(policy7));
  1011. test_assert(cmp_addr_policies(policy, policy2));
  1012. test_assert(cmp_addr_policies(policy, NULL));
  1013. test_assert(!cmp_addr_policies(policy2, policy2));
  1014. test_assert(!cmp_addr_policies(NULL, NULL));
  1015. test_assert(!policy_is_reject_star(policy2, AF_INET));
  1016. test_assert(policy_is_reject_star(policy, AF_INET));
  1017. test_assert(policy_is_reject_star(NULL, AF_INET));
  1018. addr_policy_list_free(policy);
  1019. policy = NULL;
  1020. /* make sure compacting logic works. */
  1021. policy = NULL;
  1022. line.key = (char*)"foo";
  1023. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  1024. line.next = NULL;
  1025. test_assert(0 == policies_parse_exit_policy(&line, &policy, 1, 0, NULL, 1));
  1026. test_assert(policy);
  1027. //test_streq(policy->string, "accept *:80");
  1028. //test_streq(policy->next->string, "reject *:*");
  1029. test_eq(smartlist_len(policy), 4);
  1030. /* test policy summaries */
  1031. /* check if we properly ignore private IP addresses */
  1032. test_policy_summary_helper("reject 192.168.0.0/16:*,"
  1033. "reject 0.0.0.0/8:*,"
  1034. "reject 10.0.0.0/8:*,"
  1035. "accept *:10-30,"
  1036. "accept *:90,"
  1037. "reject *:*",
  1038. "accept 10-30,90");
  1039. /* check all accept policies, and proper counting of rejects */
  1040. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  1041. "reject 12.0.0.0/9:80,"
  1042. "reject 13.0.0.0/9:80,"
  1043. "reject 14.0.0.0/9:80,"
  1044. "accept *:*", "accept 1-65535");
  1045. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  1046. "reject 12.0.0.0/9:80,"
  1047. "reject 13.0.0.0/9:80,"
  1048. "reject 14.0.0.0/9:80,"
  1049. "reject 15.0.0.0:81,"
  1050. "accept *:*", "accept 1-65535");
  1051. test_policy_summary_helper("reject 11.0.0.0/9:80,"
  1052. "reject 12.0.0.0/9:80,"
  1053. "reject 13.0.0.0/9:80,"
  1054. "reject 14.0.0.0/9:80,"
  1055. "reject 15.0.0.0:80,"
  1056. "accept *:*",
  1057. "reject 80");
  1058. /* no exits */
  1059. test_policy_summary_helper("accept 11.0.0.0/9:80,"
  1060. "reject *:*",
  1061. "reject 1-65535");
  1062. /* port merging */
  1063. test_policy_summary_helper("accept *:80,"
  1064. "accept *:81,"
  1065. "accept *:100-110,"
  1066. "accept *:111,"
  1067. "reject *:*",
  1068. "accept 80-81,100-111");
  1069. /* border ports */
  1070. test_policy_summary_helper("accept *:1,"
  1071. "accept *:3,"
  1072. "accept *:65535,"
  1073. "reject *:*",
  1074. "accept 1,3,65535");
  1075. /* holes */
  1076. test_policy_summary_helper("accept *:1,"
  1077. "accept *:3,"
  1078. "accept *:5,"
  1079. "accept *:7,"
  1080. "reject *:*",
  1081. "accept 1,3,5,7");
  1082. test_policy_summary_helper("reject *:1,"
  1083. "reject *:3,"
  1084. "reject *:5,"
  1085. "reject *:7,"
  1086. "accept *:*",
  1087. "reject 1,3,5,7");
  1088. /* Short policies with unrecognized formats should get accepted. */
  1089. test_short_policy_parse("accept fred,2,3-5", "accept 2,3-5");
  1090. test_short_policy_parse("accept 2,fred,3", "accept 2,3");
  1091. test_short_policy_parse("accept 2,fred,3,bob", "accept 2,3");
  1092. test_short_policy_parse("accept 2,-3,500-600", "accept 2,500-600");
  1093. /* Short policies with nil entries are accepted too. */
  1094. test_short_policy_parse("accept 1,,3", "accept 1,3");
  1095. test_short_policy_parse("accept 100-200,,", "accept 100-200");
  1096. test_short_policy_parse("reject ,1-10,,,,30-40", "reject 1-10,30-40");
  1097. /* Try parsing various broken short policies */
  1098. tt_ptr_op(NULL, ==, parse_short_policy("accept 200-199"));
  1099. tt_ptr_op(NULL, ==, parse_short_policy(""));
  1100. tt_ptr_op(NULL, ==, parse_short_policy("rejekt 1,2,3"));
  1101. tt_ptr_op(NULL, ==, parse_short_policy("reject "));
  1102. tt_ptr_op(NULL, ==, parse_short_policy("reject"));
  1103. tt_ptr_op(NULL, ==, parse_short_policy("rej"));
  1104. tt_ptr_op(NULL, ==, parse_short_policy("accept 2,3,100000"));
  1105. tt_ptr_op(NULL, ==, parse_short_policy("accept 2,3x,4"));
  1106. tt_ptr_op(NULL, ==, parse_short_policy("accept 2,3x,4"));
  1107. tt_ptr_op(NULL, ==, parse_short_policy("accept 2-"));
  1108. tt_ptr_op(NULL, ==, parse_short_policy("accept 2-x"));
  1109. tt_ptr_op(NULL, ==, parse_short_policy("accept 1-,3"));
  1110. tt_ptr_op(NULL, ==, parse_short_policy("accept 1-,3"));
  1111. /* Test a too-long policy. */
  1112. {
  1113. int i;
  1114. char *policy = NULL;
  1115. smartlist_t *chunks = smartlist_new();
  1116. smartlist_add(chunks, tor_strdup("accept "));
  1117. for (i=1; i<10000; ++i)
  1118. smartlist_add_asprintf(chunks, "%d,", i);
  1119. smartlist_add(chunks, tor_strdup("20000"));
  1120. policy = smartlist_join_strings(chunks, "", 0, NULL);
  1121. SMARTLIST_FOREACH(chunks, char *, ch, tor_free(ch));
  1122. smartlist_free(chunks);
  1123. tt_ptr_op(NULL, ==, parse_short_policy(policy));/* shouldn't be accepted */
  1124. tor_free(policy); /* could leak. */
  1125. }
  1126. /* truncation ports */
  1127. sm = smartlist_new();
  1128. for (i=1; i<2000; i+=2) {
  1129. char buf[POLICY_BUF_LEN];
  1130. tor_snprintf(buf, sizeof(buf), "reject *:%d", i);
  1131. smartlist_add(sm, tor_strdup(buf));
  1132. }
  1133. smartlist_add(sm, tor_strdup("accept *:*"));
  1134. policy_str = smartlist_join_strings(sm, ",", 0, NULL);
  1135. test_policy_summary_helper( policy_str,
  1136. "accept 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,"
  1137. "46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,"
  1138. "92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,"
  1139. "130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,"
  1140. "166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,"
  1141. "202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,"
  1142. "238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,"
  1143. "274,276,278,280,282,284,286,288,290,292,294,296,298,300,302,304,306,308,"
  1144. "310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,"
  1145. "346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,"
  1146. "382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,"
  1147. "418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,"
  1148. "454,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,"
  1149. "490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522");
  1150. done:
  1151. addr_policy_list_free(policy);
  1152. addr_policy_list_free(policy2);
  1153. addr_policy_list_free(policy3);
  1154. addr_policy_list_free(policy4);
  1155. addr_policy_list_free(policy5);
  1156. addr_policy_list_free(policy6);
  1157. addr_policy_list_free(policy7);
  1158. tor_free(policy_str);
  1159. if (sm) {
  1160. SMARTLIST_FOREACH(sm, char *, s, tor_free(s));
  1161. smartlist_free(sm);
  1162. }
  1163. }
  1164. /** Test encoding and parsing of rendezvous service descriptors. */
  1165. static void
  1166. test_rend_fns(void)
  1167. {
  1168. rend_service_descriptor_t *generated = NULL, *parsed = NULL;
  1169. char service_id[DIGEST_LEN];
  1170. char service_id_base32[REND_SERVICE_ID_LEN_BASE32+1];
  1171. const char *next_desc;
  1172. smartlist_t *descs = smartlist_new();
  1173. char computed_desc_id[DIGEST_LEN];
  1174. char parsed_desc_id[DIGEST_LEN];
  1175. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  1176. time_t now;
  1177. char *intro_points_encrypted = NULL;
  1178. size_t intro_points_size;
  1179. size_t encoded_size;
  1180. int i;
  1181. char address1[] = "fooaddress.onion";
  1182. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  1183. char address3[] = "fooaddress.exit";
  1184. char address4[] = "www.torproject.org";
  1185. test_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
  1186. test_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
  1187. test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
  1188. test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
  1189. pk1 = pk_generate(0);
  1190. pk2 = pk_generate(1);
  1191. generated = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  1192. generated->pk = crypto_pk_dup_key(pk1);
  1193. crypto_pk_get_digest(generated->pk, service_id);
  1194. base32_encode(service_id_base32, REND_SERVICE_ID_LEN_BASE32+1,
  1195. service_id, REND_SERVICE_ID_LEN);
  1196. now = time(NULL);
  1197. generated->timestamp = now;
  1198. generated->version = 2;
  1199. generated->protocols = 42;
  1200. generated->intro_nodes = smartlist_new();
  1201. for (i = 0; i < 3; i++) {
  1202. rend_intro_point_t *intro = tor_malloc_zero(sizeof(rend_intro_point_t));
  1203. crypto_pk_t *okey = pk_generate(2 + i);
  1204. intro->extend_info = tor_malloc_zero(sizeof(extend_info_t));
  1205. intro->extend_info->onion_key = okey;
  1206. crypto_pk_get_digest(intro->extend_info->onion_key,
  1207. intro->extend_info->identity_digest);
  1208. //crypto_rand(info->identity_digest, DIGEST_LEN); /* Would this work? */
  1209. intro->extend_info->nickname[0] = '$';
  1210. base16_encode(intro->extend_info->nickname + 1,
  1211. sizeof(intro->extend_info->nickname) - 1,
  1212. intro->extend_info->identity_digest, DIGEST_LEN);
  1213. /* Does not cover all IP addresses. */
  1214. tor_addr_from_ipv4h(&intro->extend_info->addr, crypto_rand_int(65536));
  1215. intro->extend_info->port = 1 + crypto_rand_int(65535);
  1216. intro->intro_key = crypto_pk_dup_key(pk2);
  1217. smartlist_add(generated->intro_nodes, intro);
  1218. }
  1219. test_assert(rend_encode_v2_descriptors(descs, generated, now, 0,
  1220. REND_NO_AUTH, NULL, NULL) > 0);
  1221. test_assert(rend_compute_v2_desc_id(computed_desc_id, service_id_base32,
  1222. NULL, now, 0) == 0);
  1223. test_memeq(((rend_encoded_v2_service_descriptor_t *)
  1224. smartlist_get(descs, 0))->desc_id, computed_desc_id, DIGEST_LEN);
  1225. test_assert(rend_parse_v2_service_descriptor(&parsed, parsed_desc_id,
  1226. &intro_points_encrypted,
  1227. &intro_points_size,
  1228. &encoded_size,
  1229. &next_desc,
  1230. ((rend_encoded_v2_service_descriptor_t *)
  1231. smartlist_get(descs, 0))->desc_str) == 0);
  1232. test_assert(parsed);
  1233. test_memeq(((rend_encoded_v2_service_descriptor_t *)
  1234. smartlist_get(descs, 0))->desc_id, parsed_desc_id, DIGEST_LEN);
  1235. test_eq(rend_parse_introduction_points(parsed, intro_points_encrypted,
  1236. intro_points_size), 3);
  1237. test_assert(!crypto_pk_cmp_keys(generated->pk, parsed->pk));
  1238. test_eq(parsed->timestamp, now);
  1239. test_eq(parsed->version, 2);
  1240. test_eq(parsed->protocols, 42);
  1241. test_eq(smartlist_len(parsed->intro_nodes), 3);
  1242. for (i = 0; i < smartlist_len(parsed->intro_nodes); i++) {
  1243. rend_intro_point_t *par_intro = smartlist_get(parsed->intro_nodes, i),
  1244. *gen_intro = smartlist_get(generated->intro_nodes, i);
  1245. extend_info_t *par_info = par_intro->extend_info;
  1246. extend_info_t *gen_info = gen_intro->extend_info;
  1247. test_assert(!crypto_pk_cmp_keys(gen_info->onion_key, par_info->onion_key));
  1248. test_memeq(gen_info->identity_digest, par_info->identity_digest,
  1249. DIGEST_LEN);
  1250. test_streq(gen_info->nickname, par_info->nickname);
  1251. test_assert(tor_addr_eq(&gen_info->addr, &par_info->addr));
  1252. test_eq(gen_info->port, par_info->port);
  1253. }
  1254. rend_service_descriptor_free(parsed);
  1255. rend_service_descriptor_free(generated);
  1256. parsed = generated = NULL;
  1257. done:
  1258. if (descs) {
  1259. for (i = 0; i < smartlist_len(descs); i++)
  1260. rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i));
  1261. smartlist_free(descs);
  1262. }
  1263. if (parsed)
  1264. rend_service_descriptor_free(parsed);
  1265. if (generated)
  1266. rend_service_descriptor_free(generated);
  1267. if (pk1)
  1268. crypto_pk_free(pk1);
  1269. if (pk2)
  1270. crypto_pk_free(pk2);
  1271. tor_free(intro_points_encrypted);
  1272. }
  1273. /** Run unit tests for GeoIP code. */
  1274. static void
  1275. test_geoip(void)
  1276. {
  1277. int i, j;
  1278. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  1279. char *s = NULL, *v = NULL;
  1280. const char *bridge_stats_1 =
  1281. "bridge-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1282. "bridge-ips zz=24,xy=8\n"
  1283. "bridge-ip-versions v4=16,v6=16\n",
  1284. *dirreq_stats_1 =
  1285. "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1286. "dirreq-v3-ips ab=8\n"
  1287. "dirreq-v2-ips \n"
  1288. "dirreq-v3-reqs ab=8\n"
  1289. "dirreq-v2-reqs \n"
  1290. "dirreq-v3-resp ok=0,not-enough-sigs=0,unavailable=0,not-found=0,"
  1291. "not-modified=0,busy=0\n"
  1292. "dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
  1293. "busy=0\n"
  1294. "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
  1295. "dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
  1296. "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n"
  1297. "dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
  1298. *dirreq_stats_2 =
  1299. "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1300. "dirreq-v3-ips \n"
  1301. "dirreq-v2-ips \n"
  1302. "dirreq-v3-reqs \n"
  1303. "dirreq-v2-reqs \n"
  1304. "dirreq-v3-resp ok=0,not-enough-sigs=0,unavailable=0,not-found=0,"
  1305. "not-modified=0,busy=0\n"
  1306. "dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
  1307. "busy=0\n"
  1308. "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
  1309. "dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
  1310. "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n"
  1311. "dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
  1312. *dirreq_stats_3 =
  1313. "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1314. "dirreq-v3-ips \n"
  1315. "dirreq-v2-ips \n"
  1316. "dirreq-v3-reqs \n"
  1317. "dirreq-v2-reqs \n"
  1318. "dirreq-v3-resp ok=8,not-enough-sigs=0,unavailable=0,not-found=0,"
  1319. "not-modified=0,busy=0\n"
  1320. "dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
  1321. "busy=0\n"
  1322. "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
  1323. "dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
  1324. "dirreq-v3-tunneled-dl complete=0,timeout=0,running=0\n"
  1325. "dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
  1326. *dirreq_stats_4 =
  1327. "dirreq-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1328. "dirreq-v3-ips \n"
  1329. "dirreq-v2-ips \n"
  1330. "dirreq-v3-reqs \n"
  1331. "dirreq-v2-reqs \n"
  1332. "dirreq-v3-resp ok=8,not-enough-sigs=0,unavailable=0,not-found=0,"
  1333. "not-modified=0,busy=0\n"
  1334. "dirreq-v2-resp ok=0,unavailable=0,not-found=0,not-modified=0,"
  1335. "busy=0\n"
  1336. "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
  1337. "dirreq-v2-direct-dl complete=0,timeout=0,running=0\n"
  1338. "dirreq-v3-tunneled-dl complete=0,timeout=0,running=4\n"
  1339. "dirreq-v2-tunneled-dl complete=0,timeout=0,running=0\n",
  1340. *entry_stats_1 =
  1341. "entry-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1342. "entry-ips ab=8\n",
  1343. *entry_stats_2 =
  1344. "entry-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1345. "entry-ips \n";
  1346. tor_addr_t addr;
  1347. struct in6_addr in6;
  1348. /* Populate the DB a bit. Add these in order, since we can't do the final
  1349. * 'sort' step. These aren't very good IP addresses, but they're perfectly
  1350. * fine uint32_t values. */
  1351. test_eq(0, geoip_parse_entry("10,50,AB", AF_INET));
  1352. test_eq(0, geoip_parse_entry("52,90,XY", AF_INET));
  1353. test_eq(0, geoip_parse_entry("95,100,AB", AF_INET));
  1354. test_eq(0, geoip_parse_entry("\"105\",\"140\",\"ZZ\"", AF_INET));
  1355. test_eq(0, geoip_parse_entry("\"150\",\"190\",\"XY\"", AF_INET));
  1356. test_eq(0, geoip_parse_entry("\"200\",\"250\",\"AB\"", AF_INET));
  1357. /* Populate the IPv6 DB equivalently with fake IPs in the same range */
  1358. test_eq(0, geoip_parse_entry("::a,::32,AB", AF_INET6));
  1359. test_eq(0, geoip_parse_entry("::34,::5a,XY", AF_INET6));
  1360. test_eq(0, geoip_parse_entry("::5f,::64,AB", AF_INET6));
  1361. test_eq(0, geoip_parse_entry("::69,::8c,ZZ", AF_INET6));
  1362. test_eq(0, geoip_parse_entry("::96,::be,XY", AF_INET6));
  1363. test_eq(0, geoip_parse_entry("::c8,::fa,AB", AF_INET6));
  1364. /* We should have 4 countries: ??, ab, xy, zz. */
  1365. test_eq(4, geoip_get_n_countries());
  1366. memset(&in6, 0, sizeof(in6));
  1367. /* Make sure that country ID actually works. */
  1368. #define SET_TEST_IPV6(i) \
  1369. do { \
  1370. set_uint32(in6.s6_addr + 12, htonl((uint32_t) (i))); \
  1371. } while (0)
  1372. #define CHECK_COUNTRY(country, val) do { \
  1373. /* test ipv4 country lookup */ \
  1374. test_streq(country, \
  1375. geoip_get_country_name(geoip_get_country_by_ipv4(val))); \
  1376. /* test ipv6 country lookup */ \
  1377. SET_TEST_IPV6(val); \
  1378. test_streq(country, \
  1379. geoip_get_country_name(geoip_get_country_by_ipv6(&in6))); \
  1380. } while (0)
  1381. CHECK_COUNTRY("??", 3);
  1382. CHECK_COUNTRY("ab", 32);
  1383. CHECK_COUNTRY("??", 5);
  1384. CHECK_COUNTRY("??", 51);
  1385. CHECK_COUNTRY("xy", 150);
  1386. CHECK_COUNTRY("xy", 190);
  1387. CHECK_COUNTRY("??", 2000);
  1388. test_eq(0, geoip_get_country_by_ipv4(3));
  1389. SET_TEST_IPV6(3);
  1390. test_eq(0, geoip_get_country_by_ipv6(&in6));
  1391. #undef CHECK_COUNTRY
  1392. /* Record odd numbered fake-IPs using ipv6, even numbered fake-IPs
  1393. * using ipv4. Since our fake geoip database is the same between
  1394. * ipv4 and ipv6, we should get the same result no matter which
  1395. * address family we pick for each IP. */
  1396. #define SET_TEST_ADDRESS(i) do { \
  1397. if ((i) & 1) { \
  1398. SET_TEST_IPV6(i); \
  1399. tor_addr_from_in6(&addr, &in6); \
  1400. } else { \
  1401. tor_addr_from_ipv4h(&addr, (uint32_t) i); \
  1402. } \
  1403. } while (0)
  1404. get_options_mutable()->BridgeRelay = 1;
  1405. get_options_mutable()->BridgeRecordUsageByCountry = 1;
  1406. /* Put 9 observations in AB... */
  1407. for (i=32; i < 40; ++i) {
  1408. SET_TEST_ADDRESS(i);
  1409. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now-7200);
  1410. }
  1411. SET_TEST_ADDRESS(225);
  1412. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now-7200);
  1413. /* and 3 observations in XY, several times. */
  1414. for (j=0; j < 10; ++j)
  1415. for (i=52; i < 55; ++i) {
  1416. SET_TEST_ADDRESS(i);
  1417. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now-3600);
  1418. }
  1419. /* and 17 observations in ZZ... */
  1420. for (i=110; i < 127; ++i) {
  1421. SET_TEST_ADDRESS(i);
  1422. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
  1423. }
  1424. geoip_get_client_history(GEOIP_CLIENT_CONNECT, &s, &v);
  1425. test_assert(s);
  1426. test_assert(v);
  1427. test_streq("zz=24,ab=16,xy=8", s);
  1428. test_streq("v4=16,v6=16", v);
  1429. tor_free(s);
  1430. tor_free(v);
  1431. /* Now clear out all the AB observations. */
  1432. geoip_remove_old_clients(now-6000);
  1433. geoip_get_client_history(GEOIP_CLIENT_CONNECT, &s, &v);
  1434. test_assert(s);
  1435. test_assert(v);
  1436. test_streq("zz=24,xy=8", s);
  1437. test_streq("v4=16,v6=16", v);
  1438. tor_free(s);
  1439. tor_free(v);
  1440. /* Start testing bridge statistics by making sure that we don't output
  1441. * bridge stats without initializing them. */
  1442. s = geoip_format_bridge_stats(now + 86400);
  1443. test_assert(!s);
  1444. /* Initialize stats and generate the bridge-stats history string out of
  1445. * the connecting clients added above. */
  1446. geoip_bridge_stats_init(now);
  1447. s = geoip_format_bridge_stats(now + 86400);
  1448. test_streq(bridge_stats_1, s);
  1449. tor_free(s);
  1450. /* Stop collecting bridge stats and make sure we don't write a history
  1451. * string anymore. */
  1452. geoip_bridge_stats_term();
  1453. s = geoip_format_bridge_stats(now + 86400);
  1454. test_assert(!s);
  1455. /* Stop being a bridge and start being a directory mirror that gathers
  1456. * directory request statistics. */
  1457. geoip_bridge_stats_term();
  1458. get_options_mutable()->BridgeRelay = 0;
  1459. get_options_mutable()->BridgeRecordUsageByCountry = 0;
  1460. get_options_mutable()->DirReqStatistics = 1;
  1461. /* Start testing dirreq statistics by making sure that we don't collect
  1462. * dirreq stats without initializing them. */
  1463. SET_TEST_ADDRESS(100);
  1464. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
  1465. s = geoip_format_dirreq_stats(now + 86400);
  1466. test_assert(!s);
  1467. /* Initialize stats, note one connecting client, and generate the
  1468. * dirreq-stats history string. */
  1469. geoip_dirreq_stats_init(now);
  1470. SET_TEST_ADDRESS(100);
  1471. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
  1472. s = geoip_format_dirreq_stats(now + 86400);
  1473. test_streq(dirreq_stats_1, s);
  1474. tor_free(s);
  1475. /* Stop collecting stats, add another connecting client, and ensure we
  1476. * don't generate a history string. */
  1477. geoip_dirreq_stats_term();
  1478. SET_TEST_ADDRESS(101);
  1479. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
  1480. s = geoip_format_dirreq_stats(now + 86400);
  1481. test_assert(!s);
  1482. /* Re-start stats, add a connecting client, reset stats, and make sure
  1483. * that we get an all empty history string. */
  1484. geoip_dirreq_stats_init(now);
  1485. SET_TEST_ADDRESS(100);
  1486. geoip_note_client_seen(GEOIP_CLIENT_NETWORKSTATUS, &addr, now);
  1487. geoip_reset_dirreq_stats(now);
  1488. s = geoip_format_dirreq_stats(now + 86400);
  1489. test_streq(dirreq_stats_2, s);
  1490. tor_free(s);
  1491. /* Note a successful network status response and make sure that it
  1492. * appears in the history string. */
  1493. geoip_note_ns_response(GEOIP_CLIENT_NETWORKSTATUS, GEOIP_SUCCESS);
  1494. s = geoip_format_dirreq_stats(now + 86400);
  1495. test_streq(dirreq_stats_3, s);
  1496. tor_free(s);
  1497. /* Start a tunneled directory request. */
  1498. geoip_start_dirreq((uint64_t) 1, 1024, GEOIP_CLIENT_NETWORKSTATUS,
  1499. DIRREQ_TUNNELED);
  1500. s = geoip_format_dirreq_stats(now + 86400);
  1501. test_streq(dirreq_stats_4, s);
  1502. /* Stop collecting directory request statistics and start gathering
  1503. * entry stats. */
  1504. geoip_dirreq_stats_term();
  1505. get_options_mutable()->DirReqStatistics = 0;
  1506. get_options_mutable()->EntryStatistics = 1;
  1507. /* Start testing entry statistics by making sure that we don't collect
  1508. * anything without initializing entry stats. */
  1509. SET_TEST_ADDRESS(100);
  1510. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
  1511. s = geoip_format_entry_stats(now + 86400);
  1512. test_assert(!s);
  1513. /* Initialize stats, note one connecting client, and generate the
  1514. * entry-stats history string. */
  1515. geoip_entry_stats_init(now);
  1516. SET_TEST_ADDRESS(100);
  1517. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
  1518. s = geoip_format_entry_stats(now + 86400);
  1519. test_streq(entry_stats_1, s);
  1520. tor_free(s);
  1521. /* Stop collecting stats, add another connecting client, and ensure we
  1522. * don't generate a history string. */
  1523. geoip_entry_stats_term();
  1524. SET_TEST_ADDRESS(101);
  1525. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
  1526. s = geoip_format_entry_stats(now + 86400);
  1527. test_assert(!s);
  1528. /* Re-start stats, add a connecting client, reset stats, and make sure
  1529. * that we get an all empty history string. */
  1530. geoip_entry_stats_init(now);
  1531. SET_TEST_ADDRESS(100);
  1532. geoip_note_client_seen(GEOIP_CLIENT_CONNECT, &addr, now);
  1533. geoip_reset_entry_stats(now);
  1534. s = geoip_format_entry_stats(now + 86400);
  1535. test_streq(entry_stats_2, s);
  1536. tor_free(s);
  1537. #undef SET_TEST_ADDRESS
  1538. #undef SET_TEST_IPV6
  1539. /* Stop collecting entry statistics. */
  1540. geoip_entry_stats_term();
  1541. get_options_mutable()->EntryStatistics = 0;
  1542. done:
  1543. tor_free(s);
  1544. }
  1545. /** Run unit tests for stats code. */
  1546. static void
  1547. test_stats(void)
  1548. {
  1549. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  1550. char *s = NULL;
  1551. int i;
  1552. /* Start with testing exit port statistics; we shouldn't collect exit
  1553. * stats without initializing them. */
  1554. rep_hist_note_exit_stream_opened(80);
  1555. rep_hist_note_exit_bytes(80, 100, 10000);
  1556. s = rep_hist_format_exit_stats(now + 86400);
  1557. test_assert(!s);
  1558. /* Initialize stats, note some streams and bytes, and generate history
  1559. * string. */
  1560. rep_hist_exit_stats_init(now);
  1561. rep_hist_note_exit_stream_opened(80);
  1562. rep_hist_note_exit_bytes(80, 100, 10000);
  1563. rep_hist_note_exit_stream_opened(443);
  1564. rep_hist_note_exit_bytes(443, 100, 10000);
  1565. rep_hist_note_exit_bytes(443, 100, 10000);
  1566. s = rep_hist_format_exit_stats(now + 86400);
  1567. test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1568. "exit-kibibytes-written 80=1,443=1,other=0\n"
  1569. "exit-kibibytes-read 80=10,443=20,other=0\n"
  1570. "exit-streams-opened 80=4,443=4,other=0\n", s);
  1571. tor_free(s);
  1572. /* Add a few bytes on 10 more ports and ensure that only the top 10
  1573. * ports are contained in the history string. */
  1574. for (i = 50; i < 60; i++) {
  1575. rep_hist_note_exit_bytes(i, i, i);
  1576. rep_hist_note_exit_stream_opened(i);
  1577. }
  1578. s = rep_hist_format_exit_stats(now + 86400);
  1579. test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1580. "exit-kibibytes-written 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
  1581. "59=1,80=1,443=1,other=1\n"
  1582. "exit-kibibytes-read 52=1,53=1,54=1,55=1,56=1,57=1,58=1,"
  1583. "59=1,80=10,443=20,other=1\n"
  1584. "exit-streams-opened 52=4,53=4,54=4,55=4,56=4,57=4,58=4,"
  1585. "59=4,80=4,443=4,other=4\n", s);
  1586. tor_free(s);
  1587. /* Stop collecting stats, add some bytes, and ensure we don't generate
  1588. * a history string. */
  1589. rep_hist_exit_stats_term();
  1590. rep_hist_note_exit_bytes(80, 100, 10000);
  1591. s = rep_hist_format_exit_stats(now + 86400);
  1592. test_assert(!s);
  1593. /* Re-start stats, add some bytes, reset stats, and see what history we
  1594. * get when observing no streams or bytes at all. */
  1595. rep_hist_exit_stats_init(now);
  1596. rep_hist_note_exit_stream_opened(80);
  1597. rep_hist_note_exit_bytes(80, 100, 10000);
  1598. rep_hist_reset_exit_stats(now);
  1599. s = rep_hist_format_exit_stats(now + 86400);
  1600. test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1601. "exit-kibibytes-written other=0\n"
  1602. "exit-kibibytes-read other=0\n"
  1603. "exit-streams-opened other=0\n", s);
  1604. tor_free(s);
  1605. /* Continue with testing connection statistics; we shouldn't collect
  1606. * conn stats without initializing them. */
  1607. rep_hist_note_or_conn_bytes(1, 20, 400, now);
  1608. s = rep_hist_format_conn_stats(now + 86400);
  1609. test_assert(!s);
  1610. /* Initialize stats, note bytes, and generate history string. */
  1611. rep_hist_conn_stats_init(now);
  1612. rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
  1613. rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
  1614. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
  1615. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  1616. s = rep_hist_format_conn_stats(now + 86400);
  1617. test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,1,0\n", s);
  1618. tor_free(s);
  1619. /* Stop collecting stats, add some bytes, and ensure we don't generate
  1620. * a history string. */
  1621. rep_hist_conn_stats_term();
  1622. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  1623. s = rep_hist_format_conn_stats(now + 86400);
  1624. test_assert(!s);
  1625. /* Re-start stats, add some bytes, reset stats, and see what history we
  1626. * get when observing no bytes at all. */
  1627. rep_hist_conn_stats_init(now);
  1628. rep_hist_note_or_conn_bytes(1, 30000, 400000, now);
  1629. rep_hist_note_or_conn_bytes(1, 30000, 400000, now + 5);
  1630. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 10);
  1631. rep_hist_note_or_conn_bytes(2, 400000, 30000, now + 15);
  1632. rep_hist_reset_conn_stats(now);
  1633. s = rep_hist_format_conn_stats(now + 86400);
  1634. test_streq("conn-bi-direct 2010-08-12 13:27:30 (86400 s) 0,0,0,0\n", s);
  1635. tor_free(s);
  1636. /* Continue with testing buffer statistics; we shouldn't collect buffer
  1637. * stats without initializing them. */
  1638. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  1639. s = rep_hist_format_buffer_stats(now + 86400);
  1640. test_assert(!s);
  1641. /* Initialize stats, add statistics for a single circuit, and generate
  1642. * the history string. */
  1643. rep_hist_buffer_stats_init(now);
  1644. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  1645. s = rep_hist_format_buffer_stats(now + 86400);
  1646. test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1647. "cell-processed-cells 20,0,0,0,0,0,0,0,0,0\n"
  1648. "cell-queued-cells 2.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
  1649. "0.00,0.00\n"
  1650. "cell-time-in-queue 2,0,0,0,0,0,0,0,0,0\n"
  1651. "cell-circuits-per-decile 1\n", s);
  1652. tor_free(s);
  1653. /* Add nineteen more circuit statistics to the one that's already in the
  1654. * history to see that the math works correctly. */
  1655. for (i = 21; i < 30; i++)
  1656. rep_hist_add_buffer_stats(2.0, 2.0, i);
  1657. for (i = 20; i < 30; i++)
  1658. rep_hist_add_buffer_stats(3.5, 3.5, i);
  1659. s = rep_hist_format_buffer_stats(now + 86400);
  1660. test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1661. "cell-processed-cells 29,28,27,26,25,24,23,22,21,20\n"
  1662. "cell-queued-cells 2.75,2.75,2.75,2.75,2.75,2.75,2.75,2.75,"
  1663. "2.75,2.75\n"
  1664. "cell-time-in-queue 3,3,3,3,3,3,3,3,3,3\n"
  1665. "cell-circuits-per-decile 2\n", s);
  1666. tor_free(s);
  1667. /* Stop collecting stats, add statistics for one circuit, and ensure we
  1668. * don't generate a history string. */
  1669. rep_hist_buffer_stats_term();
  1670. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  1671. s = rep_hist_format_buffer_stats(now + 86400);
  1672. test_assert(!s);
  1673. /* Re-start stats, add statistics for one circuit, reset stats, and make
  1674. * sure that the history has all zeros. */
  1675. rep_hist_buffer_stats_init(now);
  1676. rep_hist_add_buffer_stats(2.0, 2.0, 20);
  1677. rep_hist_reset_buffer_stats(now);
  1678. s = rep_hist_format_buffer_stats(now + 86400);
  1679. test_streq("cell-stats-end 2010-08-12 13:27:30 (86400 s)\n"
  1680. "cell-processed-cells 0,0,0,0,0,0,0,0,0,0\n"
  1681. "cell-queued-cells 0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,"
  1682. "0.00,0.00\n"
  1683. "cell-time-in-queue 0,0,0,0,0,0,0,0,0,0\n"
  1684. "cell-circuits-per-decile 0\n", s);
  1685. done:
  1686. tor_free(s);
  1687. }
  1688. static void *
  1689. legacy_test_setup(const struct testcase_t *testcase)
  1690. {
  1691. return testcase->setup_data;
  1692. }
  1693. void
  1694. legacy_test_helper(void *data)
  1695. {
  1696. void (*fn)(void) = data;
  1697. fn();
  1698. }
  1699. static int
  1700. legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
  1701. {
  1702. (void)ptr;
  1703. (void)testcase;
  1704. return 1;
  1705. }
  1706. const struct testcase_setup_t legacy_setup = {
  1707. legacy_test_setup, legacy_test_cleanup
  1708. };
  1709. #define ENT(name) \
  1710. { #name, legacy_test_helper, 0, &legacy_setup, test_ ## name }
  1711. #define SUBENT(group, name) \
  1712. { #group "_" #name, legacy_test_helper, 0, &legacy_setup, \
  1713. test_ ## group ## _ ## name }
  1714. #define DISABLED(name) \
  1715. { #name, legacy_test_helper, TT_SKIP, &legacy_setup, test_ ## name }
  1716. #define FORK(name) \
  1717. { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_ ## name }
  1718. static struct testcase_t test_array[] = {
  1719. ENT(buffers),
  1720. { "buffer_copy", test_buffer_copy, 0, NULL, NULL },
  1721. ENT(onion_handshake),
  1722. ENT(circuit_timeout),
  1723. ENT(policies),
  1724. ENT(rend_fns),
  1725. ENT(geoip),
  1726. FORK(stats),
  1727. END_OF_TESTCASES
  1728. };
  1729. #define SOCKSENT(name) \
  1730. { #name, test_socks_##name, TT_FORK, &socks_setup, NULL }
  1731. static struct testcase_t socks_tests[] = {
  1732. SOCKSENT(4_unsupported_commands),
  1733. SOCKSENT(4_supported_commands),
  1734. SOCKSENT(5_unsupported_commands),
  1735. SOCKSENT(5_supported_commands),
  1736. SOCKSENT(5_no_authenticate),
  1737. SOCKSENT(5_auth_before_negotiation),
  1738. SOCKSENT(5_authenticate),
  1739. SOCKSENT(5_authenticate_with_data),
  1740. END_OF_TESTCASES
  1741. };
  1742. extern struct testcase_t addr_tests[];
  1743. extern struct testcase_t crypto_tests[];
  1744. extern struct testcase_t container_tests[];
  1745. extern struct testcase_t util_tests[];
  1746. extern struct testcase_t dir_tests[];
  1747. extern struct testcase_t microdesc_tests[];
  1748. extern struct testcase_t pt_tests[];
  1749. extern struct testcase_t config_tests[];
  1750. extern struct testcase_t introduce_tests[];
  1751. extern struct testcase_t replaycache_tests[];
  1752. extern struct testcase_t cell_format_tests[];
  1753. static struct testgroup_t testgroups[] = {
  1754. { "", test_array },
  1755. { "socks/", socks_tests },
  1756. { "addr/", addr_tests },
  1757. { "crypto/", crypto_tests },
  1758. { "container/", container_tests },
  1759. { "util/", util_tests },
  1760. { "cellfmt/", cell_format_tests },
  1761. { "dir/", dir_tests },
  1762. { "dir/md/", microdesc_tests },
  1763. { "pt/", pt_tests },
  1764. { "config/", config_tests },
  1765. { "replaycache/", replaycache_tests },
  1766. { "introduce/", introduce_tests },
  1767. END_OF_GROUPS
  1768. };
  1769. /** Main entry point for unit test code: parse the command line, and run
  1770. * some unit tests. */
  1771. int
  1772. main(int c, const char **v)
  1773. {
  1774. or_options_t *options;
  1775. char *errmsg = NULL;
  1776. int i, i_out;
  1777. int loglevel = LOG_ERR;
  1778. #ifdef USE_DMALLOC
  1779. {
  1780. int r = CRYPTO_set_mem_ex_functions(tor_malloc_, tor_realloc_, tor_free_);
  1781. tor_assert(r);
  1782. }
  1783. #endif
  1784. update_approx_time(time(NULL));
  1785. options = options_new();
  1786. tor_threads_init();
  1787. init_logging();
  1788. for (i_out = i = 1; i < c; ++i) {
  1789. if (!strcmp(v[i], "--warn")) {
  1790. loglevel = LOG_WARN;
  1791. } else if (!strcmp(v[i], "--notice")) {
  1792. loglevel = LOG_NOTICE;
  1793. } else if (!strcmp(v[i], "--info")) {
  1794. loglevel = LOG_INFO;
  1795. } else if (!strcmp(v[i], "--debug")) {
  1796. loglevel = LOG_DEBUG;
  1797. } else {
  1798. v[i_out++] = v[i];
  1799. }
  1800. }
  1801. c = i_out;
  1802. {
  1803. log_severity_list_t s;
  1804. memset(&s, 0, sizeof(s));
  1805. set_log_severity_config(loglevel, LOG_ERR, &s);
  1806. add_stream_log(&s, "", fileno(stdout));
  1807. }
  1808. options->command = CMD_RUN_UNITTESTS;
  1809. if (crypto_global_init(0, NULL, NULL)) {
  1810. printf("Can't initialize crypto subsystem; exiting.\n");
  1811. return 1;
  1812. }
  1813. crypto_set_tls_dh_prime(NULL);
  1814. rep_hist_init();
  1815. network_init();
  1816. setup_directory();
  1817. options_init(options);
  1818. options->DataDirectory = tor_strdup(temp_dir);
  1819. options->EntryStatistics = 1;
  1820. if (set_options(options, &errmsg) < 0) {
  1821. printf("Failed to set initial options: %s\n", errmsg);
  1822. tor_free(errmsg);
  1823. return 1;
  1824. }
  1825. crypto_seed_rng(1);
  1826. atexit(remove_directory);
  1827. have_failed = (tinytest_main(c, v, testgroups) != 0);
  1828. free_pregenerated_keys();
  1829. #ifdef USE_DMALLOC
  1830. tor_free_all(0);
  1831. dmalloc_log_unfreed();
  1832. #endif
  1833. if (have_failed)
  1834. return 1;
  1835. else
  1836. return 0;
  1837. }