test.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char test_c_id[] =
  6. "$Id$";
  7. const char tor_svn_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 CONFIG_PRIVATE
  26. #define CONTROL_PRIVATE
  27. #define MEMPOOL_PRIVATE
  28. #define ROUTER_PRIVATE
  29. #include "or.h"
  30. #include "../common/test.h"
  31. #include "../common/torgzip.h"
  32. #include "../common/mempool.h"
  33. int have_failed = 0;
  34. static char temp_dir[256];
  35. static void
  36. setup_directory(void)
  37. {
  38. static int is_setup = 0;
  39. int r;
  40. if (is_setup) return;
  41. #ifdef MS_WINDOWS
  42. // XXXX
  43. tor_snprintf(temp_dir, sizeof(temp_dir),
  44. "c:\\windows\\temp\\tor_test_%d", (int)getpid());
  45. r = mkdir(temp_dir);
  46. #else
  47. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
  48. r = mkdir(temp_dir, 0700);
  49. #endif
  50. if (r) {
  51. fprintf(stderr, "Can't create directory %s:", temp_dir);
  52. perror("");
  53. exit(1);
  54. }
  55. is_setup = 1;
  56. }
  57. static const char *
  58. get_fname(const char *name)
  59. {
  60. static char buf[1024];
  61. setup_directory();
  62. tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
  63. return buf;
  64. }
  65. static void
  66. remove_directory(void)
  67. {
  68. smartlist_t *elements = tor_listdir(temp_dir);
  69. if (elements) {
  70. SMARTLIST_FOREACH(elements, const char *, cp,
  71. {
  72. size_t len = strlen(cp)+strlen(temp_dir)+16;
  73. char *tmp = tor_malloc(len);
  74. tor_snprintf(tmp, len, "%s"PATH_SEPARATOR"%s", temp_dir, cp);
  75. unlink(tmp);
  76. tor_free(tmp);
  77. });
  78. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  79. smartlist_free(elements);
  80. }
  81. rmdir(temp_dir);
  82. }
  83. static crypto_pk_env_t *
  84. pk_generate(int idx)
  85. {
  86. static crypto_pk_env_t *pregen[3] = {NULL, NULL, NULL};
  87. tor_assert(idx < (int)(sizeof(pregen)/sizeof(pregen[0])));
  88. if (! pregen[idx]) {
  89. pregen[idx] = crypto_new_pk_env();
  90. tor_assert(!crypto_pk_generate_key(pregen[idx]));
  91. }
  92. return crypto_pk_dup_key(pregen[idx]);
  93. }
  94. static void
  95. test_buffers(void)
  96. {
  97. char str[256];
  98. char str2[256];
  99. buf_t *buf;
  100. int j;
  101. /****
  102. * buf_new
  103. ****/
  104. if (!(buf = buf_new()))
  105. test_fail();
  106. test_eq(buf_capacity(buf), 4096);
  107. test_eq(buf_datalen(buf), 0);
  108. /****
  109. * General pointer frobbing
  110. */
  111. for (j=0;j<256;++j) {
  112. str[j] = (char)j;
  113. }
  114. write_to_buf(str, 256, buf);
  115. write_to_buf(str, 256, buf);
  116. test_eq(buf_datalen(buf), 512);
  117. fetch_from_buf(str2, 200, buf);
  118. test_memeq(str, str2, 200);
  119. test_eq(buf_datalen(buf), 312);
  120. memset(str2, 0, sizeof(str2));
  121. fetch_from_buf(str2, 256, buf);
  122. test_memeq(str+200, str2, 56);
  123. test_memeq(str, str2+56, 200);
  124. test_eq(buf_datalen(buf), 56);
  125. memset(str2, 0, sizeof(str2));
  126. /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
  127. * another 3584 bytes, we hit the end. */
  128. for (j=0;j<15;++j) {
  129. write_to_buf(str, 256, buf);
  130. }
  131. assert_buf_ok(buf);
  132. test_eq(buf_datalen(buf), 3896);
  133. fetch_from_buf(str2, 56, buf);
  134. test_eq(buf_datalen(buf), 3840);
  135. test_memeq(str+200, str2, 56);
  136. for (j=0;j<15;++j) {
  137. memset(str2, 0, sizeof(str2));
  138. fetch_from_buf(str2, 256, buf);
  139. test_memeq(str, str2, 256);
  140. }
  141. test_eq(buf_datalen(buf), 0);
  142. buf_free(buf);
  143. /* Okay, now make sure growing can work. */
  144. buf = buf_new_with_capacity(16);
  145. test_eq(buf_capacity(buf), 16);
  146. write_to_buf(str+1, 255, buf);
  147. test_eq(buf_capacity(buf), 256);
  148. fetch_from_buf(str2, 254, buf);
  149. test_memeq(str+1, str2, 254);
  150. test_eq(buf_capacity(buf), 256);
  151. assert_buf_ok(buf);
  152. write_to_buf(str, 32, buf);
  153. test_eq(buf_capacity(buf), 256);
  154. assert_buf_ok(buf);
  155. write_to_buf(str, 256, buf);
  156. assert_buf_ok(buf);
  157. test_eq(buf_capacity(buf), 512);
  158. test_eq(buf_datalen(buf), 33+256);
  159. fetch_from_buf(str2, 33, buf);
  160. test_eq(*str2, str[255]);
  161. test_memeq(str2+1, str, 32);
  162. test_eq(buf_capacity(buf), 512);
  163. test_eq(buf_datalen(buf), 256);
  164. fetch_from_buf(str2, 256, buf);
  165. test_memeq(str, str2, 256);
  166. /* now try shrinking: case 1. */
  167. buf_free(buf);
  168. buf = buf_new_with_capacity(33668);
  169. for (j=0;j<67;++j) {
  170. write_to_buf(str,255, buf);
  171. }
  172. test_eq(buf_capacity(buf), 33668);
  173. test_eq(buf_datalen(buf), 17085);
  174. for (j=0; j < 40; ++j) {
  175. fetch_from_buf(str2, 255,buf);
  176. test_memeq(str2, str, 255);
  177. }
  178. /* now try shrinking: case 2. */
  179. buf_free(buf);
  180. buf = buf_new_with_capacity(33668);
  181. for (j=0;j<67;++j) {
  182. write_to_buf(str,255, buf);
  183. }
  184. for (j=0; j < 20; ++j) {
  185. fetch_from_buf(str2, 255,buf);
  186. test_memeq(str2, str, 255);
  187. }
  188. for (j=0;j<80;++j) {
  189. write_to_buf(str,255, buf);
  190. }
  191. test_eq(buf_capacity(buf),33668);
  192. for (j=0; j < 120; ++j) {
  193. fetch_from_buf(str2, 255,buf);
  194. test_memeq(str2, str, 255);
  195. }
  196. #if 0
  197. {
  198. int s;
  199. int eof;
  200. int i;
  201. buf_t *buf2;
  202. /****
  203. * read_to_buf
  204. ****/
  205. s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
  206. write(s, str, 256);
  207. close(s);
  208. s = open(get_fname("data"), O_RDONLY, 0);
  209. eof = 0;
  210. errno = 0; /* XXXX */
  211. i = read_to_buf(s, 10, buf, &eof);
  212. printf("%s\n", strerror(errno));
  213. test_eq(i, 10);
  214. test_eq(eof, 0);
  215. test_eq(buf_capacity(buf), 4096);
  216. test_eq(buf_datalen(buf), 10);
  217. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
  218. /* Test reading 0 bytes. */
  219. i = read_to_buf(s, 0, buf, &eof);
  220. test_eq(buf_capacity(buf), 512*1024);
  221. test_eq(buf_datalen(buf), 10);
  222. test_eq(eof, 0);
  223. test_eq(i, 0);
  224. /* Now test when buffer is filled exactly. */
  225. buf2 = buf_new_with_capacity(6);
  226. i = read_to_buf(s, 6, buf2, &eof);
  227. test_eq(buf_capacity(buf2), 6);
  228. test_eq(buf_datalen(buf2), 6);
  229. test_eq(eof, 0);
  230. test_eq(i, 6);
  231. test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
  232. buf_free(buf2);
  233. /* Now test when buffer is filled with more data to read. */
  234. buf2 = buf_new_with_capacity(32);
  235. i = read_to_buf(s, 128, buf2, &eof);
  236. test_eq(buf_capacity(buf2), 128);
  237. test_eq(buf_datalen(buf2), 32);
  238. test_eq(eof, 0);
  239. test_eq(i, 32);
  240. buf_free(buf2);
  241. /* Now read to eof. */
  242. test_assert(buf_capacity(buf) > 256);
  243. i = read_to_buf(s, 1024, buf, &eof);
  244. test_eq(i, (256-32-10-6));
  245. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  246. test_eq(buf_datalen(buf), 256-6-32);
  247. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
  248. test_eq(eof, 0);
  249. i = read_to_buf(s, 1024, buf, &eof);
  250. test_eq(i, 0);
  251. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  252. test_eq(buf_datalen(buf), 256-6-32);
  253. test_eq(eof, 1);
  254. }
  255. #endif
  256. buf_free(buf);
  257. }
  258. static void
  259. test_crypto_dh(void)
  260. {
  261. crypto_dh_env_t *dh1, *dh2;
  262. char p1[DH_BYTES];
  263. char p2[DH_BYTES];
  264. char s1[DH_BYTES];
  265. char s2[DH_BYTES];
  266. int s1len, s2len;
  267. dh1 = crypto_dh_new();
  268. dh2 = crypto_dh_new();
  269. test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
  270. test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
  271. memset(p1, 0, DH_BYTES);
  272. memset(p2, 0, DH_BYTES);
  273. test_memeq(p1, p2, DH_BYTES);
  274. test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  275. test_memneq(p1, p2, DH_BYTES);
  276. test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  277. test_memneq(p1, p2, DH_BYTES);
  278. memset(s1, 0, DH_BYTES);
  279. memset(s2, 0xFF, DH_BYTES);
  280. s1len = crypto_dh_compute_secret(dh1, p2, DH_BYTES, s1, 50);
  281. s2len = crypto_dh_compute_secret(dh2, p1, DH_BYTES, s2, 50);
  282. test_assert(s1len > 0);
  283. test_eq(s1len, s2len);
  284. test_memeq(s1, s2, s1len);
  285. crypto_dh_free(dh1);
  286. crypto_dh_free(dh2);
  287. }
  288. static void
  289. test_crypto(void)
  290. {
  291. crypto_cipher_env_t *env1, *env2;
  292. crypto_pk_env_t *pk1, *pk2;
  293. char *data1, *data2, *data3, *cp;
  294. int i, j, p, len;
  295. size_t size;
  296. data1 = tor_malloc(1024);
  297. data2 = tor_malloc(1024);
  298. data3 = tor_malloc(1024);
  299. test_assert(data1 && data2 && data3);
  300. /* Try out RNG. */
  301. test_assert(! crypto_seed_rng());
  302. crypto_rand(data1, 100);
  303. crypto_rand(data2, 100);
  304. test_memneq(data1,data2,100);
  305. /* Now, test encryption and decryption with stream cipher. */
  306. data1[0]='\0';
  307. for (i = 1023; i>0; i -= 35)
  308. strncat(data1, "Now is the time for all good onions", i);
  309. memset(data2, 0, 1024);
  310. memset(data3, 0, 1024);
  311. env1 = crypto_new_cipher_env();
  312. test_neq(env1, 0);
  313. env2 = crypto_new_cipher_env();
  314. test_neq(env2, 0);
  315. j = crypto_cipher_generate_key(env1);
  316. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  317. crypto_cipher_encrypt_init_cipher(env1);
  318. crypto_cipher_decrypt_init_cipher(env2);
  319. /* Try encrypting 512 chars. */
  320. crypto_cipher_encrypt(env1, data2, data1, 512);
  321. crypto_cipher_decrypt(env2, data3, data2, 512);
  322. test_memeq(data1, data3, 512);
  323. test_memneq(data1, data2, 512);
  324. /* Now encrypt 1 at a time, and get 1 at a time. */
  325. for (j = 512; j < 560; ++j) {
  326. crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
  327. }
  328. for (j = 512; j < 560; ++j) {
  329. crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
  330. }
  331. test_memeq(data1, data3, 560);
  332. /* Now encrypt 3 at a time, and get 5 at a time. */
  333. for (j = 560; j < 1024-5; j += 3) {
  334. crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
  335. }
  336. for (j = 560; j < 1024-5; j += 5) {
  337. crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
  338. }
  339. test_memeq(data1, data3, 1024-5);
  340. /* Now make sure that when we encrypt with different chunk sizes, we get
  341. the same results. */
  342. crypto_free_cipher_env(env2);
  343. memset(data3, 0, 1024);
  344. env2 = crypto_new_cipher_env();
  345. test_neq(env2, 0);
  346. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  347. crypto_cipher_encrypt_init_cipher(env2);
  348. for (j = 0; j < 1024-16; j += 17) {
  349. crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
  350. }
  351. for (j= 0; j < 1024-16; ++j) {
  352. if (data2[j] != data3[j]) {
  353. printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
  354. }
  355. }
  356. test_memeq(data2, data3, 1024-16);
  357. crypto_free_cipher_env(env1);
  358. crypto_free_cipher_env(env2);
  359. /* Test vectors for stream ciphers. */
  360. /* XXXX Look up some test vectors for the ciphers and make sure we match. */
  361. /* Test SHA-1 with a test vector from the specification. */
  362. i = crypto_digest(data1, "abc", 3);
  363. test_memeq(data1,
  364. "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
  365. "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
  366. /* Public-key ciphers */
  367. pk1 = pk_generate(0);
  368. pk2 = crypto_new_pk_env();
  369. test_assert(pk1 && pk2);
  370. test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &size));
  371. test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, size));
  372. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  373. tor_free(cp);
  374. test_eq(128, crypto_pk_keysize(pk1));
  375. test_eq(128, crypto_pk_keysize(pk2));
  376. test_eq(128, crypto_pk_public_encrypt(pk2, data1, "Hello whirled.", 15,
  377. PK_PKCS1_OAEP_PADDING));
  378. test_eq(128, crypto_pk_public_encrypt(pk1, data2, "Hello whirled.", 15,
  379. PK_PKCS1_OAEP_PADDING));
  380. /* oaep padding should make encryption not match */
  381. test_memneq(data1, data2, 128);
  382. test_eq(15, crypto_pk_private_decrypt(pk1, data3, data1, 128,
  383. PK_PKCS1_OAEP_PADDING,1));
  384. test_streq(data3, "Hello whirled.");
  385. memset(data3, 0, 1024);
  386. test_eq(15, crypto_pk_private_decrypt(pk1, data3, data2, 128,
  387. PK_PKCS1_OAEP_PADDING,1));
  388. test_streq(data3, "Hello whirled.");
  389. /* Can't decrypt with public key. */
  390. test_eq(-1, crypto_pk_private_decrypt(pk2, data3, data2, 128,
  391. PK_PKCS1_OAEP_PADDING,1));
  392. /* Try again with bad padding */
  393. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  394. test_eq(-1, crypto_pk_private_decrypt(pk1, data3, data2, 128,
  395. PK_PKCS1_OAEP_PADDING,1));
  396. /* File operations: save and load private key */
  397. test_assert(! crypto_pk_write_private_key_to_filename(pk1,
  398. get_fname("pkey1")));
  399. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  400. get_fname("pkey1")));
  401. test_eq(15, crypto_pk_private_decrypt(pk2, data3, data1, 128,
  402. PK_PKCS1_OAEP_PADDING,1));
  403. /* Now try signing. */
  404. strlcpy(data1, "Ossifrage", 1024);
  405. test_eq(128, crypto_pk_private_sign(pk1, data2, data1, 10));
  406. test_eq(10, crypto_pk_public_checksig(pk1, data3, data2, 128));
  407. test_streq(data3, "Ossifrage");
  408. /* Try signing digests. */
  409. test_eq(128, crypto_pk_private_sign_digest(pk1, data2, data1, 10));
  410. test_eq(20, crypto_pk_public_checksig(pk1, data3, data2, 128));
  411. test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  412. test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  413. /*XXXX test failed signing*/
  414. /* Try encoding */
  415. crypto_free_pk_env(pk2);
  416. pk2 = NULL;
  417. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  418. test_assert(i>0);
  419. pk2 = crypto_pk_asn1_decode(data1, i);
  420. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  421. /* Try with hybrid encryption wrappers. */
  422. crypto_rand(data1, 1024);
  423. for (i = 0; i < 3; ++i) {
  424. for (j = 85; j < 140; ++j) {
  425. memset(data2,0,1024);
  426. memset(data3,0,1024);
  427. if (i == 0 && j < 129)
  428. continue;
  429. p = (i==0)?PK_NO_PADDING:
  430. (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
  431. len = crypto_pk_public_hybrid_encrypt(pk1,data2,data1,j,p,0);
  432. test_assert(len>=0);
  433. len = crypto_pk_private_hybrid_decrypt(pk1,data3,data2,len,p,1);
  434. test_eq(len,j);
  435. test_memeq(data1,data3,j);
  436. }
  437. }
  438. crypto_free_pk_env(pk1);
  439. crypto_free_pk_env(pk2);
  440. /* Base64 tests */
  441. strlcpy(data1, "Test string that contains 35 chars.", 1024);
  442. strlcat(data1, " 2nd string that contains 35 chars.", 1024);
  443. i = base64_encode(data2, 1024, data1, 71);
  444. j = base64_decode(data3, 1024, data2, i);
  445. test_streq(data3, data1);
  446. test_eq(j, 71);
  447. test_assert(data2[i] == '\0');
  448. crypto_rand(data1, DIGEST_LEN);
  449. memset(data2, 100, 1024);
  450. digest_to_base64(data2, data1);
  451. test_eq(BASE64_DIGEST_LEN, strlen(data2));
  452. test_eq(100, data2[BASE64_DIGEST_LEN+2]);
  453. memset(data3, 99, 1024);
  454. digest_from_base64(data3, data2);
  455. test_memeq(data1, data3, DIGEST_LEN);
  456. test_eq(99, data3[DIGEST_LEN+1]);
  457. /* Base32 tests */
  458. strlcpy(data1, "5chrs", 1024);
  459. /* bit pattern is: [35 63 68 72 73] ->
  460. * [00110101 01100011 01101000 01110010 01110011]
  461. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  462. */
  463. base32_encode(data2, 9, data1, 5);
  464. test_streq(data2, "gvrwq4tt");
  465. strlcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
  466. base32_encode(data2, 30, data1, 10);
  467. test_streq(data2, "772w2rfobvomsywe");
  468. /* Base16 tests */
  469. strlcpy(data1, "6chrs\xff", 1024);
  470. base16_encode(data2, 13, data1, 6);
  471. test_streq(data2, "3663687273FF");
  472. strlcpy(data1, "f0d678affc000100", 1024);
  473. i = base16_decode(data2, 8, data1, 16);
  474. test_eq(i,0);
  475. test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  476. /* now try some failing base16 decodes */
  477. test_eq(-1, base16_decode(data2, 8, data1, 15)); /* odd input len */
  478. test_eq(-1, base16_decode(data2, 7, data1, 16)); /* dest too short */
  479. strlcpy(data1, "f0dz!8affc000100", 1024);
  480. test_eq(-1, base16_decode(data2, 8, data1, 16));
  481. tor_free(data1);
  482. tor_free(data2);
  483. tor_free(data3);
  484. }
  485. static void
  486. test_crypto_s2k(void)
  487. {
  488. char buf[29];
  489. char buf2[29];
  490. char *buf3;
  491. int i;
  492. memset(buf, 0, sizeof(buf));
  493. memset(buf2, 0, sizeof(buf2));
  494. buf3 = tor_malloc(65536);
  495. memset(buf3, 0, 65536);
  496. secret_to_key(buf+9, 20, "", 0, buf);
  497. crypto_digest(buf2+9, buf3, 1024);
  498. test_memeq(buf, buf2, 29);
  499. memcpy(buf,"vrbacrda",8);
  500. memcpy(buf2,"vrbacrda",8);
  501. buf[8] = 96;
  502. buf2[8] = 96;
  503. secret_to_key(buf+9, 20, "12345678", 8, buf);
  504. for (i = 0; i < 65536; i += 16) {
  505. memcpy(buf3+i, "vrbacrda12345678", 16);
  506. }
  507. crypto_digest(buf2+9, buf3, 65536);
  508. test_memeq(buf, buf2, 29);
  509. }
  510. static int
  511. _compare_strs(const void **a, const void **b)
  512. {
  513. const char *s1 = *a, *s2 = *b;
  514. return strcmp(s1, s2);
  515. }
  516. static int
  517. _compare_without_first_ch(const void *a, const void **b)
  518. {
  519. const char *s1 = a, *s2 = *b;
  520. return strcasecmp(s1+1, s2);
  521. }
  522. static void
  523. test_util(void)
  524. {
  525. struct timeval start, end;
  526. struct tm a_time;
  527. char timestr[RFC1123_TIME_LEN+1];
  528. char buf[1024];
  529. time_t t_res;
  530. int i;
  531. uint32_t u32;
  532. uint16_t u16;
  533. char *cp, *k, *v;
  534. start.tv_sec = 5;
  535. start.tv_usec = 5000;
  536. end.tv_sec = 5;
  537. end.tv_usec = 5000;
  538. test_eq(0L, tv_udiff(&start, &end));
  539. end.tv_usec = 7000;
  540. test_assert(tv_cmp(&start, &end)<0);
  541. test_assert(tv_cmp(&end, &start)>0);
  542. test_assert(tv_cmp(&end, &end)==0);
  543. test_eq(2000L, tv_udiff(&start, &end));
  544. end.tv_sec = 6;
  545. test_eq(1002000L, tv_udiff(&start, &end));
  546. end.tv_usec = 0;
  547. test_eq(995000L, tv_udiff(&start, &end));
  548. end.tv_sec = 4;
  549. test_eq(-1005000L, tv_udiff(&start, &end));
  550. tv_addms(&end, 5090);
  551. test_eq(end.tv_sec, 9);
  552. test_eq(end.tv_usec, 90000);
  553. end.tv_usec = 999990;
  554. start.tv_sec = 1;
  555. start.tv_usec = 500;
  556. tv_add(&start, &end);
  557. test_eq(start.tv_sec, 11);
  558. test_eq(start.tv_usec, 490);
  559. /* The test values here are confirmed to be correct on a platform
  560. * with a working timegm. */
  561. a_time.tm_year = 2003-1900;
  562. a_time.tm_mon = 7;
  563. a_time.tm_mday = 30;
  564. a_time.tm_hour = 6;
  565. a_time.tm_min = 14;
  566. a_time.tm_sec = 55;
  567. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  568. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  569. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  570. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  571. a_time.tm_mday = 10;
  572. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  573. format_rfc1123_time(timestr, 0);
  574. test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
  575. format_rfc1123_time(timestr, (time_t)1091580502UL);
  576. test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
  577. t_res = 0;
  578. i = parse_rfc1123_time(timestr, &t_res);
  579. test_eq(i,0);
  580. test_eq(t_res, (time_t)1091580502UL);
  581. test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
  582. tor_gettimeofday(&start);
  583. /* Test tor_strstrip() */
  584. strlcpy(buf, "Testing 1 2 3", sizeof(buf));
  585. test_eq(0, tor_strstrip(buf, ",!"));
  586. test_streq(buf, "Testing 1 2 3");
  587. strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
  588. test_eq(5, tor_strstrip(buf, "!? "));
  589. test_streq(buf, "Testing123");
  590. /* Test tor_strpartition() */
  591. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3));
  592. test_streq(buf, "abc##def##ghi");
  593. /* Test parse_addr_port */
  594. cp = NULL; u32 = 3; u16 = 3;
  595. test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
  596. test_streq(cp, "1.2.3.4");
  597. test_eq(u32, 0x01020304u);
  598. test_eq(u16, 0);
  599. tor_free(cp);
  600. test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
  601. test_streq(cp, "4.3.2.1");
  602. test_eq(u32, 0x04030201u);
  603. test_eq(u16, 99);
  604. tor_free(cp);
  605. test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
  606. &cp, NULL, &u16));
  607. test_streq(cp, "nonexistent.address");
  608. test_eq(u16, 4040);
  609. tor_free(cp);
  610. test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
  611. test_streq(cp, "localhost");
  612. test_eq(u32, 0x7f000001u);
  613. test_eq(u16, 9999);
  614. tor_free(cp);
  615. u32 = 3;
  616. test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
  617. test_eq(cp, NULL);
  618. test_eq(u32, 0x7f000001u);
  619. test_eq(u16, 0);
  620. tor_free(cp);
  621. test_eq(0, addr_mask_get_bits(0x0u));
  622. test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
  623. test_eq(16, addr_mask_get_bits(0xFFFF0000u));
  624. test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
  625. test_eq(1, addr_mask_get_bits(0x80000000u));
  626. /* Test tor_parse_long. */
  627. test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
  628. test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
  629. test_eq(-50L, tor_parse_long("-50",10,-100,100,NULL,NULL));
  630. /* Test tor_parse_ulong */
  631. test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL,NULL));
  632. test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL,NULL));
  633. /* Test tor_parse_uint64. */
  634. test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
  635. test_assert(i == 1);
  636. test_streq(cp, " x");
  637. test_assert(U64_LITERAL(12345678901) ==
  638. tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
  639. test_assert(i == 1);
  640. test_streq(cp, "");
  641. test_assert(U64_LITERAL(0) ==
  642. tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
  643. test_assert(i == 0);
  644. /* Test printf with uint64 */
  645. tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
  646. U64_PRINTF_ARG(U64_LITERAL(12345678901)));
  647. test_streq(buf, "x!12345678901!x");
  648. /* Test parse_line_from_str */
  649. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  650. "k2\n"
  651. "k3 \n" "\n" " \n" "#comment\n"
  652. "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf));
  653. cp = buf;
  654. cp = parse_line_from_str(cp, &k, &v);
  655. test_streq(k, "k");
  656. test_streq(v, "v");
  657. test_assert(!strcmpstart(cp, " key value with"));
  658. cp = parse_line_from_str(cp, &k, &v);
  659. test_streq(k, "key");
  660. test_streq(v, "value with spaces");
  661. test_assert(!strcmpstart(cp, "keykey"));
  662. cp = parse_line_from_str(cp, &k, &v);
  663. test_streq(k, "keykey");
  664. test_streq(v, "val");
  665. test_assert(!strcmpstart(cp, "k2\n"));
  666. cp = parse_line_from_str(cp, &k, &v);
  667. test_streq(k, "k2");
  668. test_streq(v, "");
  669. test_assert(!strcmpstart(cp, "k3 \n"));
  670. cp = parse_line_from_str(cp, &k, &v);
  671. test_streq(k, "k3");
  672. test_streq(v, "");
  673. test_assert(!strcmpstart(cp, "\n \n"));
  674. cp = parse_line_from_str(cp, &k, &v);
  675. test_streq(k, "k4");
  676. test_streq(v, "");
  677. test_assert(!strcmpstart(cp, "k5#abc"));
  678. cp = parse_line_from_str(cp, &k, &v);
  679. test_streq(k, "k5");
  680. test_streq(v, "");
  681. test_assert(!strcmpstart(cp, "k6"));
  682. cp = parse_line_from_str(cp, &k, &v);
  683. test_streq(k, "k6");
  684. test_streq(v, "val");
  685. test_streq(cp, "");
  686. /* Test for strcmpstart and strcmpend. */
  687. test_assert(strcmpstart("abcdef", "abcdef")==0);
  688. test_assert(strcmpstart("abcdef", "abc")==0);
  689. test_assert(strcmpstart("abcdef", "abd")<0);
  690. test_assert(strcmpstart("abcdef", "abb")>0);
  691. test_assert(strcmpstart("ab", "abb")<0);
  692. test_assert(strcmpend("abcdef", "abcdef")==0);
  693. test_assert(strcmpend("abcdef", "def")==0);
  694. test_assert(strcmpend("abcdef", "deg")<0);
  695. test_assert(strcmpend("abcdef", "dee")>0);
  696. test_assert(strcmpend("ab", "abb")<0);
  697. test_assert(strcasecmpend("AbcDEF", "abcdef")==0);
  698. test_assert(strcasecmpend("abcdef", "dEF")==0);
  699. test_assert(strcasecmpend("abcDEf", "deg")<0);
  700. test_assert(strcasecmpend("abcdef", "DEE")>0);
  701. test_assert(strcasecmpend("ab", "abB")<0);
  702. /* Test mem_is_zero */
  703. memset(buf,0,128);
  704. buf[128] = 'x';
  705. test_assert(tor_digest_is_zero(buf));
  706. test_assert(tor_mem_is_zero(buf, 10));
  707. test_assert(tor_mem_is_zero(buf, 20));
  708. test_assert(tor_mem_is_zero(buf, 128));
  709. test_assert(!tor_mem_is_zero(buf, 129));
  710. buf[60] = (char)255;
  711. test_assert(!tor_mem_is_zero(buf, 128));
  712. buf[0] = (char)1;
  713. test_assert(!tor_mem_is_zero(buf, 10));
  714. /* Test inet_ntoa */
  715. {
  716. char tmpbuf[INET_NTOA_BUF_LEN];
  717. struct in_addr in;
  718. tor_inet_aton("18.244.0.188",&in);
  719. tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
  720. test_streq(tmpbuf, "18.244.0.188");
  721. }
  722. /* Test 'escaped' */
  723. test_streq("\"\"", escaped(""));
  724. test_streq("\"abcd\"", escaped("abcd"));
  725. test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
  726. test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
  727. test_assert(NULL == escaped(NULL));
  728. /* Test strndup and memdup */
  729. {
  730. const char *s = "abcdefghijklmnopqrstuvwxyz";
  731. cp = tor_strndup(s, 30);
  732. test_streq(cp, s); /* same string, */
  733. test_neq(cp, s); /* but different pointers. */
  734. tor_free(cp);
  735. cp = tor_strndup(s, 5);
  736. test_streq(cp, "abcde");
  737. tor_free(cp);
  738. s = "a\0b\0c\0d\0e\0";
  739. cp = tor_memdup(s,10);
  740. test_memeq(cp, s, 10); /* same ram, */
  741. test_neq(cp, s); /* but different pointers. */
  742. tor_free(cp);
  743. }
  744. /* Test str-foo functions */
  745. cp = tor_strdup("abcdef");
  746. test_assert(tor_strisnonupper(cp));
  747. cp[3] = 'D';
  748. test_assert(!tor_strisnonupper(cp));
  749. tor_strupper(cp);
  750. test_streq(cp, "ABCDEF");
  751. test_assert(tor_strisprint(cp));
  752. cp[3] = 3;
  753. test_assert(!tor_strisprint(cp));
  754. tor_free(cp);
  755. /* Test eat_whitespace. */
  756. {
  757. const char *s = " \n a";
  758. test_eq_ptr(eat_whitespace(s), s+4);
  759. s = "abcd";
  760. test_eq_ptr(eat_whitespace(s), s);
  761. s = "#xyz\nab";
  762. test_eq_ptr(eat_whitespace(s), s+5);
  763. }
  764. /* Test memmem */
  765. {
  766. const char *haystack = "abcde";
  767. tor_assert(!tor_memmem(haystack, 5, "ef", 2));
  768. test_eq_ptr(tor_memmem(haystack, 5, "cd", 2), haystack + 2);
  769. test_eq_ptr(tor_memmem(haystack, 5, "cde", 3), haystack + 2);
  770. haystack = "ababcad";
  771. test_eq_ptr(tor_memmem(haystack, 7, "abc", 3), haystack + 2);
  772. }
  773. /* Test wrap_string */
  774. {
  775. smartlist_t *sl = smartlist_create();
  776. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  777. 10, "", "");
  778. cp = smartlist_join_strings(sl, "", 0, NULL);
  779. test_streq(cp,
  780. "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
  781. tor_free(cp);
  782. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  783. smartlist_clear(sl);
  784. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  785. 16, "### ", "# ");
  786. cp = smartlist_join_strings(sl, "", 0, NULL);
  787. test_streq(cp,
  788. "### This is a\n# test of string\n# wrapping\n# functionality:\n"
  789. "# woot.\n");
  790. tor_free(cp);
  791. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  792. smartlist_clear(sl);
  793. }
  794. /* now make sure time works. */
  795. tor_gettimeofday(&end);
  796. /* We might've timewarped a little. */
  797. test_assert(tv_udiff(&start, &end) >= -5000);
  798. }
  799. static void
  800. test_smartlist(void)
  801. {
  802. smartlist_t *sl;
  803. char *cp;
  804. /* XXXX test sort_strings, sort_digests, uniq_strings, uniq_digests */
  805. /* Test smartlist add, del_keeporder, insert, get. */
  806. sl = smartlist_create();
  807. smartlist_add(sl, (void*)1);
  808. smartlist_add(sl, (void*)2);
  809. smartlist_add(sl, (void*)3);
  810. smartlist_add(sl, (void*)4);
  811. smartlist_del_keeporder(sl, 1);
  812. smartlist_insert(sl, 1, (void*)22);
  813. smartlist_insert(sl, 0, (void*)0);
  814. smartlist_insert(sl, 5, (void*)555);
  815. test_eq_ptr((void*)0, smartlist_get(sl,0));
  816. test_eq_ptr((void*)1, smartlist_get(sl,1));
  817. test_eq_ptr((void*)22, smartlist_get(sl,2));
  818. test_eq_ptr((void*)3, smartlist_get(sl,3));
  819. test_eq_ptr((void*)4, smartlist_get(sl,4));
  820. test_eq_ptr((void*)555, smartlist_get(sl,5));
  821. /* Try deleting in the middle. */
  822. smartlist_del(sl, 1);
  823. test_eq_ptr((void*)555, smartlist_get(sl, 1));
  824. /* Try deleting at the end. */
  825. smartlist_del(sl, 4);
  826. test_eq(4, smartlist_len(sl));
  827. /* test isin. */
  828. test_assert(smartlist_isin(sl, (void*)3));
  829. test_assert(!smartlist_isin(sl, (void*)99));
  830. /* Test split and join */
  831. smartlist_clear(sl);
  832. test_eq(0, smartlist_len(sl));
  833. smartlist_split_string(sl, "abc", ":", 0, 0);
  834. test_eq(1, smartlist_len(sl));
  835. test_streq("abc", smartlist_get(sl, 0));
  836. smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  837. test_eq(4, smartlist_len(sl));
  838. test_streq("a", smartlist_get(sl, 1));
  839. test_streq("bc", smartlist_get(sl, 2));
  840. test_streq("", smartlist_get(sl, 3));
  841. cp = smartlist_join_strings(sl, "", 0, NULL);
  842. test_streq(cp, "abcabc");
  843. tor_free(cp);
  844. cp = smartlist_join_strings(sl, "!", 0, NULL);
  845. test_streq(cp, "abc!a!bc!");
  846. tor_free(cp);
  847. cp = smartlist_join_strings(sl, "XY", 0, NULL);
  848. test_streq(cp, "abcXYaXYbcXY");
  849. tor_free(cp);
  850. cp = smartlist_join_strings(sl, "XY", 1, NULL);
  851. test_streq(cp, "abcXYaXYbcXYXY");
  852. tor_free(cp);
  853. cp = smartlist_join_strings(sl, "", 1, NULL);
  854. test_streq(cp, "abcabc");
  855. tor_free(cp);
  856. smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
  857. test_eq(8, smartlist_len(sl));
  858. test_streq("", smartlist_get(sl, 4));
  859. test_streq("def", smartlist_get(sl, 5));
  860. test_streq(" ", smartlist_get(sl, 6));
  861. test_streq("ghijk", smartlist_get(sl, 7));
  862. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  863. smartlist_clear(sl);
  864. smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  865. test_eq(3, smartlist_len(sl));
  866. test_streq("a", smartlist_get(sl,0));
  867. test_streq("bbd", smartlist_get(sl,1));
  868. test_streq("cdef", smartlist_get(sl,2));
  869. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  870. SPLIT_SKIP_SPACE, 0);
  871. test_eq(8, smartlist_len(sl));
  872. test_streq("z", smartlist_get(sl,3));
  873. test_streq("zhasd", smartlist_get(sl,4));
  874. test_streq("", smartlist_get(sl,5));
  875. test_streq("bnud", smartlist_get(sl,6));
  876. test_streq("", smartlist_get(sl,7));
  877. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  878. smartlist_clear(sl);
  879. smartlist_split_string(sl, " ab\tc \td ef ", NULL,
  880. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  881. test_eq(4, smartlist_len(sl));
  882. test_streq("ab", smartlist_get(sl,0));
  883. test_streq("c", smartlist_get(sl,1));
  884. test_streq("d", smartlist_get(sl,2));
  885. test_streq("ef", smartlist_get(sl,3));
  886. smartlist_split_string(sl, "ghi\tj", NULL,
  887. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  888. test_eq(6, smartlist_len(sl));
  889. test_streq("ghi", smartlist_get(sl,4));
  890. test_streq("j", smartlist_get(sl,5));
  891. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  892. smartlist_clear(sl);
  893. cp = smartlist_join_strings(sl, "XY", 0, NULL);
  894. test_streq(cp, "");
  895. tor_free(cp);
  896. cp = smartlist_join_strings(sl, "XY", 1, NULL);
  897. test_streq(cp, "XY");
  898. tor_free(cp);
  899. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  900. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  901. test_eq(3, smartlist_len(sl));
  902. test_streq("z", smartlist_get(sl, 0));
  903. test_streq("zhasd", smartlist_get(sl, 1));
  904. test_streq("bnud", smartlist_get(sl, 2));
  905. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  906. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  907. test_eq(5, smartlist_len(sl));
  908. test_streq("z", smartlist_get(sl, 3));
  909. test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
  910. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  911. smartlist_clear(sl);
  912. smartlist_split_string(sl, "abcd\n", "\n",
  913. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  914. test_eq(1, smartlist_len(sl));
  915. test_streq("abcd", smartlist_get(sl, 0));
  916. smartlist_split_string(sl, "efgh", "\n",
  917. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  918. test_eq(2, smartlist_len(sl));
  919. test_streq("efgh", smartlist_get(sl, 1));
  920. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  921. smartlist_clear(sl);
  922. /* Test smartlist sorting. */
  923. smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
  924. test_eq(7, smartlist_len(sl));
  925. smartlist_sort(sl, _compare_strs);
  926. cp = smartlist_join_strings(sl, ",", 0, NULL);
  927. test_streq(cp,"and,arma,by,nickm,onion,router,the");
  928. tor_free(cp);
  929. test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
  930. _compare_without_first_ch));
  931. test_streq("and", smartlist_bsearch(sl, " AND", _compare_without_first_ch));
  932. test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", _compare_without_first_ch));
  933. /* Test reverse() and pop_last() */
  934. smartlist_reverse(sl);
  935. cp = smartlist_join_strings(sl, ",", 0, NULL);
  936. test_streq(cp,"the,router,onion,nickm,by,arma,and");
  937. tor_free(cp);
  938. cp = smartlist_pop_last(sl);
  939. test_streq(cp, "and");
  940. tor_free(cp);
  941. test_eq(smartlist_len(sl), 6);
  942. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  943. smartlist_clear(sl);
  944. /* Test uniq() */
  945. smartlist_split_string(sl,
  946. "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
  947. ",", 0, 0);
  948. smartlist_sort(sl, _compare_strs);
  949. smartlist_uniq(sl, _compare_strs, NULL);
  950. cp = smartlist_join_strings(sl, ",", 0, NULL);
  951. test_streq(cp, "50,a,canal,man,noon,panama,plan,radar");
  952. tor_free(cp);
  953. /* Test string_isin and isin_case and num_isin */
  954. test_assert(smartlist_string_isin(sl, "noon"));
  955. test_assert(!smartlist_string_isin(sl, "noonoon"));
  956. test_assert(smartlist_string_isin_case(sl, "nOOn"));
  957. test_assert(!smartlist_string_isin_case(sl, "nooNooN"));
  958. test_assert(smartlist_string_num_isin(sl, 50));
  959. test_assert(!smartlist_string_num_isin(sl, 60));
  960. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  961. smartlist_clear(sl);
  962. /* Test string_remove and remove and join_strings2 */
  963. smartlist_split_string(sl,
  964. "Some say the Earth will end in ice and some in fire",
  965. " ", 0, 0);
  966. cp = smartlist_get(sl, 4);
  967. test_streq(cp, "will");
  968. smartlist_add(sl, cp);
  969. smartlist_remove(sl, cp);
  970. cp = smartlist_join_strings(sl, ",", 0, NULL);
  971. test_streq(cp, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
  972. tor_free(cp);
  973. smartlist_string_remove(sl, "in");
  974. cp = smartlist_join_strings2(sl, "+XX", 1, 0, NULL);
  975. test_streq(cp, "Some+say+the+Earth+fire+end+some+ice+and");
  976. tor_free(cp);
  977. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  978. smartlist_clear(sl);
  979. {
  980. smartlist_t *ints = smartlist_create();
  981. smartlist_t *odds = smartlist_create();
  982. smartlist_t *evens = smartlist_create();
  983. smartlist_t *primes = smartlist_create();
  984. int i;
  985. for (i=1; i < 10; i += 2)
  986. smartlist_add(odds, (void*)(uintptr_t)i);
  987. for (i=0; i < 10; i += 2)
  988. smartlist_add(evens, (void*)(uintptr_t)i);
  989. /* add_all */
  990. smartlist_add_all(ints, odds);
  991. smartlist_add_all(ints, evens);
  992. test_eq(smartlist_len(ints), 10);
  993. smartlist_add(primes, (void*)2);
  994. smartlist_add(primes, (void*)3);
  995. smartlist_add(primes, (void*)5);
  996. smartlist_add(primes, (void*)7);
  997. /* overlap */
  998. test_assert(smartlist_overlap(ints, odds));
  999. test_assert(smartlist_overlap(odds, primes));
  1000. test_assert(smartlist_overlap(evens, primes));
  1001. test_assert(!smartlist_overlap(odds, evens));
  1002. /* intersect */
  1003. smartlist_add_all(sl, odds);
  1004. smartlist_intersect(sl, primes);
  1005. test_eq(smartlist_len(sl), 3);
  1006. test_assert(smartlist_isin(sl, (void*)3));
  1007. test_assert(smartlist_isin(sl, (void*)5));
  1008. test_assert(smartlist_isin(sl, (void*)7));
  1009. /* subtract */
  1010. smartlist_add_all(sl, primes);
  1011. smartlist_subtract(sl, odds);
  1012. test_eq(smartlist_len(sl), 1);
  1013. test_assert(smartlist_isin(sl, (void*)2));
  1014. smartlist_free(odds);
  1015. smartlist_free(evens);
  1016. smartlist_free(ints);
  1017. smartlist_free(primes);
  1018. smartlist_clear(sl);
  1019. }
  1020. smartlist_free(sl);
  1021. }
  1022. /* stop threads running at once. */
  1023. static tor_mutex_t *_thread_test_mutex = NULL;
  1024. /* make sure that threads have to run at the same time. */
  1025. static tor_mutex_t *_thread_test_start1 = NULL;
  1026. static tor_mutex_t *_thread_test_start2 = NULL;
  1027. static strmap_t *_thread_test_strmap = NULL;
  1028. static void
  1029. _thread_test_func(void* _s)
  1030. {
  1031. char *s = _s;
  1032. int i;
  1033. tor_mutex_t *m;
  1034. char buf[64];
  1035. char *cp;
  1036. if (!strcmp(s, "thread 1"))
  1037. m = _thread_test_start1;
  1038. else
  1039. m = _thread_test_start2;
  1040. tor_mutex_acquire(m);
  1041. tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
  1042. cp = tor_strdup(buf);
  1043. for (i=0; i<100000; ++i) {
  1044. tor_mutex_acquire(_thread_test_mutex);
  1045. strmap_set(_thread_test_strmap, "last to run", cp);
  1046. tor_mutex_release(_thread_test_mutex);
  1047. }
  1048. strmap_set(_thread_test_strmap, s, tor_strdup(buf));
  1049. tor_mutex_release(m);
  1050. spawn_exit();
  1051. }
  1052. static void
  1053. test_threads(void)
  1054. {
  1055. char *s1, *s2;
  1056. int done = 0;
  1057. #ifndef TOR_IS_MULTITHREADED
  1058. /* Skip this test if we aren't threading. We should be threading most
  1059. * everywhere by now. */
  1060. if (1)
  1061. return 0;
  1062. #endif
  1063. _thread_test_mutex = tor_mutex_new();
  1064. _thread_test_start1 = tor_mutex_new();
  1065. _thread_test_start2 = tor_mutex_new();
  1066. _thread_test_strmap = strmap_new();
  1067. s1 = tor_strdup("thread 1");
  1068. s2 = tor_strdup("thread 2");
  1069. tor_mutex_acquire(_thread_test_start1);
  1070. tor_mutex_acquire(_thread_test_start2);
  1071. spawn_func(_thread_test_func, s1);
  1072. spawn_func(_thread_test_func, s2);
  1073. tor_mutex_release(_thread_test_start2);
  1074. tor_mutex_release(_thread_test_start1);
  1075. while (!done) {
  1076. tor_mutex_acquire(_thread_test_mutex);
  1077. strmap_assert_ok(_thread_test_strmap);
  1078. if (strmap_get(_thread_test_strmap, "thread 1") &&
  1079. strmap_get(_thread_test_strmap, "thread 2"))
  1080. done = 1;
  1081. tor_mutex_release(_thread_test_mutex);
  1082. }
  1083. tor_mutex_free(_thread_test_mutex);
  1084. /* different thread IDs. */
  1085. test_assert(strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  1086. strmap_get(_thread_test_strmap, "thread 2")));
  1087. test_assert(!strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  1088. strmap_get(_thread_test_strmap, "last to run")) ||
  1089. !strcmp(strmap_get(_thread_test_strmap, "thread 2"),
  1090. strmap_get(_thread_test_strmap, "last to run")));
  1091. strmap_free(_thread_test_strmap, _tor_free);
  1092. tor_free(s1);
  1093. tor_free(s2);
  1094. }
  1095. static int
  1096. _compare_strings_for_pqueue(const void *s1, const void *s2)
  1097. {
  1098. return strcmp((const char*)s1, (const char*)s2);
  1099. }
  1100. static void
  1101. test_pqueue(void)
  1102. {
  1103. smartlist_t *sl;
  1104. int (*cmp)(const void *, const void*);
  1105. #define OK() smartlist_pqueue_assert_ok(sl, cmp)
  1106. cmp = _compare_strings_for_pqueue;
  1107. sl = smartlist_create();
  1108. smartlist_pqueue_add(sl, cmp, (char*)"cows");
  1109. smartlist_pqueue_add(sl, cmp, (char*)"zebras");
  1110. smartlist_pqueue_add(sl, cmp, (char*)"fish");
  1111. smartlist_pqueue_add(sl, cmp, (char*)"frogs");
  1112. smartlist_pqueue_add(sl, cmp, (char*)"apples");
  1113. smartlist_pqueue_add(sl, cmp, (char*)"squid");
  1114. smartlist_pqueue_add(sl, cmp, (char*)"daschunds");
  1115. smartlist_pqueue_add(sl, cmp, (char*)"eggplants");
  1116. smartlist_pqueue_add(sl, cmp, (char*)"weissbier");
  1117. smartlist_pqueue_add(sl, cmp, (char*)"lobsters");
  1118. smartlist_pqueue_add(sl, cmp, (char*)"roquefort");
  1119. OK();
  1120. test_eq(smartlist_len(sl), 11);
  1121. test_streq(smartlist_get(sl, 0), "apples");
  1122. test_streq(smartlist_pqueue_pop(sl, cmp), "apples");
  1123. test_eq(smartlist_len(sl), 10);
  1124. OK();
  1125. test_streq(smartlist_pqueue_pop(sl, cmp), "cows");
  1126. test_streq(smartlist_pqueue_pop(sl, cmp), "daschunds");
  1127. smartlist_pqueue_add(sl, cmp, (char*)"chinchillas");
  1128. OK();
  1129. smartlist_pqueue_add(sl, cmp, (char*)"fireflies");
  1130. OK();
  1131. test_streq(smartlist_pqueue_pop(sl, cmp), "chinchillas");
  1132. test_streq(smartlist_pqueue_pop(sl, cmp), "eggplants");
  1133. test_streq(smartlist_pqueue_pop(sl, cmp), "fireflies");
  1134. OK();
  1135. test_streq(smartlist_pqueue_pop(sl, cmp), "fish");
  1136. test_streq(smartlist_pqueue_pop(sl, cmp), "frogs");
  1137. test_streq(smartlist_pqueue_pop(sl, cmp), "lobsters");
  1138. test_streq(smartlist_pqueue_pop(sl, cmp), "roquefort");
  1139. OK();
  1140. test_eq(smartlist_len(sl), 3);
  1141. test_streq(smartlist_pqueue_pop(sl, cmp), "squid");
  1142. test_streq(smartlist_pqueue_pop(sl, cmp), "weissbier");
  1143. test_streq(smartlist_pqueue_pop(sl, cmp), "zebras");
  1144. test_eq(smartlist_len(sl), 0);
  1145. OK();
  1146. #undef OK
  1147. smartlist_free(sl);
  1148. }
  1149. static void
  1150. test_gzip(void)
  1151. {
  1152. char *buf1, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
  1153. const char *ccp2;
  1154. size_t len1, len2;
  1155. tor_zlib_state_t *state;
  1156. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
  1157. test_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
  1158. if (is_gzip_supported()) {
  1159. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1160. GZIP_METHOD));
  1161. test_assert(buf2);
  1162. test_assert(!memcmp(buf2, "\037\213", 2)); /* Gztip magic. */
  1163. test_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
  1164. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  1165. GZIP_METHOD, 1, LOG_INFO));
  1166. test_assert(buf3);
  1167. test_streq(buf1,buf3);
  1168. tor_free(buf2);
  1169. tor_free(buf3);
  1170. }
  1171. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1172. ZLIB_METHOD));
  1173. test_assert(buf2);
  1174. test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
  1175. test_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
  1176. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  1177. ZLIB_METHOD, 1, LOG_INFO));
  1178. test_assert(buf3);
  1179. test_streq(buf1,buf3);
  1180. /* Check whether we can uncompress concatenated, compresed strings. */
  1181. tor_free(buf3);
  1182. buf2 = tor_realloc(buf2, len1*2);
  1183. memcpy(buf2+len1, buf2, len1);
  1184. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
  1185. ZLIB_METHOD, 1, LOG_INFO));
  1186. test_eq(len2, (strlen(buf1)+1)*2);
  1187. test_memeq(buf3,
  1188. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
  1189. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
  1190. (strlen(buf1)+1)*2);
  1191. tor_free(buf1);
  1192. tor_free(buf2);
  1193. tor_free(buf3);
  1194. /* Check whether we can uncompress partial strings. */
  1195. buf1 =
  1196. tor_strdup("String with low redundancy that won't be compressed much.");
  1197. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1198. ZLIB_METHOD));
  1199. tor_assert(len1>16);
  1200. /* when we allow an uncomplete string, we should succeed.*/
  1201. tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  1202. ZLIB_METHOD, 0, LOG_INFO));
  1203. buf3[len2]='\0';
  1204. tor_assert(len2 > 5);
  1205. tor_assert(!strcmpstart(buf1, buf3));
  1206. /* when we demand a complete string, this must fail. */
  1207. tor_free(buf3);
  1208. tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  1209. ZLIB_METHOD, 1, LOG_INFO));
  1210. tor_assert(!buf3);
  1211. /* Now, try streaming compression. */
  1212. tor_free(buf1);
  1213. tor_free(buf2);
  1214. tor_free(buf3);
  1215. state = tor_zlib_new(1, ZLIB_METHOD);
  1216. tor_assert(state);
  1217. cp1 = buf1 = tor_malloc(1024);
  1218. len1 = 1024;
  1219. ccp2 = "ABCDEFGHIJABCDEFGHIJ";
  1220. len2 = 21;
  1221. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
  1222. == TOR_ZLIB_OK);
  1223. test_eq(len2, 0); /* Make sure we compressed it all. */
  1224. test_assert(cp1 > buf1);
  1225. len2 = 0;
  1226. cp2 = cp1;
  1227. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
  1228. == TOR_ZLIB_DONE);
  1229. test_eq(len2, 0);
  1230. test_assert(cp1 > cp2); /* Make sure we really added something. */
  1231. tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
  1232. ZLIB_METHOD, 1, LOG_WARN));
  1233. test_streq(buf3, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
  1234. tor_free(buf3);
  1235. tor_zlib_free(state);
  1236. tor_free(buf2);
  1237. tor_free(buf3);
  1238. tor_free(buf1);
  1239. }
  1240. static void
  1241. test_strmap(void)
  1242. {
  1243. strmap_t *map;
  1244. strmap_iter_t *iter;
  1245. const char *k;
  1246. void *v;
  1247. char *visited;
  1248. smartlist_t *found_keys;
  1249. map = strmap_new();
  1250. v = strmap_set(map, "K1", (void*)99);
  1251. test_eq(v, NULL);
  1252. v = strmap_set(map, "K2", (void*)101);
  1253. test_eq(v, NULL);
  1254. v = strmap_set(map, "K1", (void*)100);
  1255. test_eq(v, (void*)99);
  1256. test_eq_ptr(strmap_get(map,"K1"), (void*)100);
  1257. test_eq_ptr(strmap_get(map,"K2"), (void*)101);
  1258. test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
  1259. strmap_assert_ok(map);
  1260. v = strmap_remove(map,"K2");
  1261. strmap_assert_ok(map);
  1262. test_eq_ptr(v, (void*)101);
  1263. test_eq_ptr(strmap_get(map,"K2"), NULL);
  1264. test_eq_ptr(strmap_remove(map,"K2"), NULL);
  1265. strmap_set(map, "K2", (void*)101);
  1266. strmap_set(map, "K3", (void*)102);
  1267. strmap_set(map, "K4", (void*)103);
  1268. strmap_assert_ok(map);
  1269. strmap_set(map, "K5", (void*)104);
  1270. strmap_set(map, "K6", (void*)105);
  1271. strmap_assert_ok(map);
  1272. /* Test iterator. */
  1273. iter = strmap_iter_init(map);
  1274. found_keys = smartlist_create();
  1275. while (!strmap_iter_done(iter)) {
  1276. strmap_iter_get(iter,&k,&v);
  1277. smartlist_add(found_keys, tor_strdup(k));
  1278. test_eq_ptr(v, strmap_get(map, k));
  1279. if (!strcmp(k, "K2")) {
  1280. iter = strmap_iter_next_rmv(map,iter);
  1281. } else {
  1282. iter = strmap_iter_next(map,iter);
  1283. }
  1284. }
  1285. /* Make sure we removed K2, but not the others. */
  1286. test_eq_ptr(strmap_get(map, "K2"), NULL);
  1287. test_eq_ptr(strmap_get(map, "K5"), (void*)104);
  1288. /* Make sure we visited everyone once */
  1289. smartlist_sort_strings(found_keys);
  1290. visited = smartlist_join_strings(found_keys, ":", 0, NULL);
  1291. test_streq(visited, "K1:K2:K3:K4:K5:K6");
  1292. tor_free(visited);
  1293. SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
  1294. smartlist_free(found_keys);
  1295. strmap_assert_ok(map);
  1296. /* Clean up after ourselves. */
  1297. strmap_free(map, NULL);
  1298. /* Now try some lc functions. */
  1299. map = strmap_new();
  1300. strmap_set_lc(map,"Ab.C", (void*)1);
  1301. test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
  1302. strmap_assert_ok(map);
  1303. test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
  1304. test_eq_ptr(strmap_get(map,"AB.C"), NULL);
  1305. test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
  1306. strmap_assert_ok(map);
  1307. test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
  1308. strmap_free(map,NULL);
  1309. }
  1310. static void
  1311. test_mmap(void)
  1312. {
  1313. char *fname1 = tor_strdup(get_fname("mapped_1"));
  1314. char *fname2 = tor_strdup(get_fname("mapped_2"));
  1315. char *fname3 = tor_strdup(get_fname("mapped_3"));
  1316. const size_t buflen = 17000;
  1317. char *buf = tor_malloc(17000);
  1318. tor_mmap_t *mapping;
  1319. crypto_rand(buf, buflen);
  1320. write_str_to_file(fname1, "Short file.", 1);
  1321. write_bytes_to_file(fname2, buf, buflen, 1);
  1322. write_bytes_to_file(fname3, buf, 16384, 1);
  1323. mapping = tor_mmap_file(fname1);
  1324. test_assert(mapping);
  1325. test_eq(mapping->size, strlen("Short file."));
  1326. test_streq(mapping->data, "Short file.");
  1327. #ifdef MS_WINDOWS
  1328. tor_munmap_file(mapping);
  1329. test_assert(unlink(fname1) == 0);
  1330. #else
  1331. /* make sure we can unlink. */
  1332. test_assert(unlink(fname1) == 0);
  1333. test_streq(mapping->data, "Short file.");
  1334. tor_munmap_file(mapping);
  1335. #endif
  1336. /* Make sure that we fail to map a no-longer-existent file. */
  1337. mapping = tor_mmap_file(fname1);
  1338. test_assert(mapping == NULL);
  1339. /* Now try a big file that stretches across a few pages and isn't aligned */
  1340. mapping = tor_mmap_file(fname2);
  1341. test_assert(mapping);
  1342. test_eq(mapping->size, buflen);
  1343. test_memeq(mapping->data, buf, buflen);
  1344. tor_munmap_file(mapping);
  1345. /* Now try a big aligned file. */
  1346. mapping = tor_mmap_file(fname3);
  1347. test_assert(mapping);
  1348. test_eq(mapping->size, 16384);
  1349. test_memeq(mapping->data, buf, 16384);
  1350. tor_munmap_file(mapping);
  1351. /* fname1 got unlinked above */
  1352. unlink(fname2);
  1353. unlink(fname3);
  1354. tor_free(fname1);
  1355. tor_free(fname2);
  1356. tor_free(fname3);
  1357. tor_free(buf);
  1358. }
  1359. static void
  1360. test_control_formats(void)
  1361. {
  1362. char *out;
  1363. const char *inp =
  1364. "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
  1365. size_t sz;
  1366. sz = read_escaped_data(inp, strlen(inp), 1, &out);
  1367. test_streq(out,
  1368. ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
  1369. test_eq(sz, strlen(out));
  1370. tor_free(out);
  1371. }
  1372. static void
  1373. test_onion_handshake(void)
  1374. {
  1375. /* client-side */
  1376. crypto_dh_env_t *c_dh = NULL;
  1377. char c_buf[ONIONSKIN_CHALLENGE_LEN];
  1378. char c_keys[40];
  1379. /* server-side */
  1380. char s_buf[ONIONSKIN_REPLY_LEN];
  1381. char s_keys[40];
  1382. /* shared */
  1383. crypto_pk_env_t *pk = NULL;
  1384. pk = pk_generate(0);
  1385. /* client handshake 1. */
  1386. memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
  1387. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  1388. /* server handshake */
  1389. memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
  1390. memset(s_keys, 0, 40);
  1391. test_assert(! onion_skin_server_handshake(c_buf, pk, NULL,
  1392. s_buf, s_keys, 40));
  1393. /* client handshake 2 */
  1394. memset(c_keys, 0, 40);
  1395. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  1396. crypto_dh_free(c_dh);
  1397. if (memcmp(c_keys, s_keys, 40)) {
  1398. puts("Aiiiie");
  1399. exit(1);
  1400. }
  1401. test_memeq(c_keys, s_keys, 40);
  1402. memset(s_buf, 0, 40);
  1403. test_memneq(c_keys, s_buf, 40);
  1404. crypto_free_pk_env(pk);
  1405. }
  1406. extern smartlist_t *fingerprint_list;
  1407. static void
  1408. test_dir_format(void)
  1409. {
  1410. char buf[8192], buf2[8192];
  1411. char platform[256];
  1412. char fingerprint[FINGERPRINT_LEN+1];
  1413. char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
  1414. size_t pk1_str_len, pk2_str_len, pk3_str_len;
  1415. routerinfo_t r1, r2;
  1416. crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
  1417. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  1418. addr_policy_t ex1, ex2;
  1419. routerlist_t *dir1 = NULL, *dir2 = NULL;
  1420. tor_version_t ver1;
  1421. char *bw_lines = NULL;
  1422. pk1 = pk_generate(0);
  1423. pk2 = pk_generate(1);
  1424. pk3 = pk_generate(2);
  1425. test_assert( is_legal_nickname("a"));
  1426. test_assert(!is_legal_nickname(""));
  1427. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  1428. test_assert(!is_legal_nickname("hyphen-")); /* bad char */
  1429. test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  1430. test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1431. /* valid */
  1432. test_assert( is_legal_nickname_or_hexdigest(
  1433. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1434. test_assert( is_legal_nickname_or_hexdigest(
  1435. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  1436. test_assert( is_legal_nickname_or_hexdigest(
  1437. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
  1438. /* too short */
  1439. test_assert(!is_legal_nickname_or_hexdigest(
  1440. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1441. /* illegal char */
  1442. test_assert(!is_legal_nickname_or_hexdigest(
  1443. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1444. /* hex part too long */
  1445. test_assert(!is_legal_nickname_or_hexdigest(
  1446. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1447. test_assert(!is_legal_nickname_or_hexdigest(
  1448. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  1449. /* Bad nickname */
  1450. test_assert(!is_legal_nickname_or_hexdigest(
  1451. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
  1452. test_assert(!is_legal_nickname_or_hexdigest(
  1453. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
  1454. test_assert(!is_legal_nickname_or_hexdigest(
  1455. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
  1456. test_assert(!is_legal_nickname_or_hexdigest(
  1457. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
  1458. "abcdefghijklmnoppqrst"));
  1459. /* Bad extra char. */
  1460. test_assert(!is_legal_nickname_or_hexdigest(
  1461. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
  1462. test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  1463. test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  1464. test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  1465. get_platform_str(platform, sizeof(platform));
  1466. memset(&r1,0,sizeof(r1));
  1467. memset(&r2,0,sizeof(r2));
  1468. r1.address = tor_strdup("18.244.0.1");
  1469. r1.addr = 0xc0a80001u; /* 192.168.0.1 */
  1470. r1.cache_info.published_on = 0;
  1471. r1.or_port = 9000;
  1472. r1.dir_port = 9003;
  1473. r1.onion_pkey = pk1;
  1474. r1.identity_pkey = pk2;
  1475. r1.bandwidthrate = 1000;
  1476. r1.bandwidthburst = 5000;
  1477. r1.bandwidthcapacity = 10000;
  1478. r1.exit_policy = NULL;
  1479. r1.nickname = tor_strdup("Magri");
  1480. r1.platform = tor_strdup(platform);
  1481. ex1.policy_type = ADDR_POLICY_ACCEPT;
  1482. ex1.string = NULL;
  1483. ex1.addr = 0;
  1484. ex1.msk = 0;
  1485. ex1.prt_min = ex1.prt_max = 80;
  1486. ex1.next = &ex2;
  1487. ex2.policy_type = ADDR_POLICY_REJECT;
  1488. ex2.addr = 18 << 24;
  1489. ex2.msk = 0xFF000000u;
  1490. ex2.prt_min = ex2.prt_max = 24;
  1491. ex2.next = NULL;
  1492. r2.address = tor_strdup("1.1.1.1");
  1493. r2.addr = 0x0a030201u; /* 10.3.2.1 */
  1494. r2.platform = tor_strdup(platform);
  1495. r2.cache_info.published_on = 5;
  1496. r2.or_port = 9005;
  1497. r2.dir_port = 0;
  1498. r2.onion_pkey = pk2;
  1499. r2.identity_pkey = pk1;
  1500. r2.bandwidthrate = r2.bandwidthburst = r2.bandwidthcapacity = 3000;
  1501. r2.exit_policy = &ex1;
  1502. r2.nickname = tor_strdup("Fred");
  1503. bw_lines = rep_hist_get_bandwidth_lines(0);
  1504. test_assert(bw_lines);
  1505. test_assert(!strcmpstart(bw_lines, "opt write-history "));
  1506. test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  1507. &pk1_str_len));
  1508. test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  1509. &pk2_str_len));
  1510. test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
  1511. &pk3_str_len));
  1512. memset(buf, 0, 2048);
  1513. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1514. strlcpy(buf2, "router Magri 18.244.0.1 9000 0 0\n"
  1515. "platform Tor "VERSION" on ", sizeof(buf2));
  1516. strlcat(buf2, get_uname(), sizeof(buf2));
  1517. strlcat(buf2, "\n"
  1518. "published 1970-01-01 00:00:00\n"
  1519. "opt fingerprint ", sizeof(buf2));
  1520. test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  1521. strlcat(buf2, fingerprint, sizeof(buf2));
  1522. strlcat(buf2, "\nuptime 0\n"
  1523. /* XXX the "0" above is hardcoded, but even if we made it reflect
  1524. * uptime, that still wouldn't make it right, because the two
  1525. * descriptors might be made on different seconds... hm. */
  1526. "bandwidth 1000 5000 10000\n"
  1527. "opt extra-info-digest 0000000000000000000000000000000000000000\n"
  1528. "onion-key\n", sizeof(buf2));
  1529. strlcat(buf2, pk1_str, sizeof(buf2));
  1530. strlcat(buf2, "signing-key\n", sizeof(buf2));
  1531. strlcat(buf2, pk2_str, sizeof(buf2));
  1532. strlcat(buf2, bw_lines, sizeof(buf2));
  1533. strlcat(buf2, "router-signature\n", sizeof(buf2));
  1534. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  1535. * twice */
  1536. test_streq(buf, buf2);
  1537. tor_free(bw_lines);
  1538. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1539. cp = buf;
  1540. rp1 = router_parse_entry_from_string((const char*)cp,NULL,1);
  1541. test_assert(rp1);
  1542. test_streq(rp1->address, r1.address);
  1543. test_eq(rp1->or_port, r1.or_port);
  1544. //test_eq(rp1->dir_port, r1.dir_port);
  1545. test_eq(rp1->bandwidthrate, r1.bandwidthrate);
  1546. test_eq(rp1->bandwidthburst, r1.bandwidthburst);
  1547. test_eq(rp1->bandwidthcapacity, r1.bandwidthcapacity);
  1548. test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  1549. test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  1550. test_assert(rp1->exit_policy == NULL);
  1551. #if 0
  1552. /* XXX Once we have exit policies, test this again. XXX */
  1553. strlcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n", sizeof(buf2));
  1554. strlcat(buf2, pk2_str, sizeof(buf2));
  1555. strlcat(buf2, "signing-key\n", sizeof(buf2));
  1556. strlcat(buf2, pk1_str, sizeof(buf2));
  1557. strlcat(buf2, "accept *:80\nreject 18.*:24\n\n", sizeof(buf2));
  1558. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
  1559. test_streq(buf, buf2);
  1560. cp = buf;
  1561. rp2 = router_parse_entry_from_string(&cp,1);
  1562. test_assert(rp2);
  1563. test_streq(rp2->address, r2.address);
  1564. test_eq(rp2->or_port, r2.or_port);
  1565. test_eq(rp2->dir_port, r2.dir_port);
  1566. test_eq(rp2->bandwidth, r2.bandwidth);
  1567. test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  1568. test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  1569. test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
  1570. test_streq(rp2->exit_policy->string, "accept *:80");
  1571. test_streq(rp2->exit_policy->address, "*");
  1572. test_streq(rp2->exit_policy->port, "80");
  1573. test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
  1574. test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
  1575. test_streq(rp2->exit_policy->next->address, "18.*");
  1576. test_streq(rp2->exit_policy->next->port, "24");
  1577. test_assert(rp2->exit_policy->next->next == NULL);
  1578. /* Okay, now for the directories. */
  1579. {
  1580. fingerprint_list = smartlist_create();
  1581. crypto_pk_get_fingerprint(pk2, buf, 1);
  1582. add_fingerprint_to_dir("Magri", buf, fingerprint_list);
  1583. crypto_pk_get_fingerprint(pk1, buf, 1);
  1584. add_fingerprint_to_dir("Fred", buf, fingerprint_list);
  1585. }
  1586. {
  1587. char d[DIGEST_LEN];
  1588. const char *m;
  1589. /* XXXX NM re-enable. */
  1590. /* Make sure routers aren't too far in the past any more. */
  1591. r1.cache_info.published_on = time(NULL);
  1592. r2.cache_info.published_on = time(NULL)-3*60*60;
  1593. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1594. test_eq(dirserv_add_descriptor(buf,&m), 2);
  1595. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk1)>0);
  1596. test_eq(dirserv_add_descriptor(buf,&m), 2);
  1597. get_options()->Nickname = tor_strdup("DirServer");
  1598. test_assert(!dirserv_dump_directory_to_string(&cp,pk3, 0));
  1599. crypto_pk_get_digest(pk3, d);
  1600. test_assert(!router_parse_directory(cp));
  1601. test_eq(2, smartlist_len(dir1->routers));
  1602. tor_free(cp);
  1603. }
  1604. #endif
  1605. dirserv_free_fingerprint_list();
  1606. tor_free(pk1_str);
  1607. tor_free(pk2_str);
  1608. if (pk1) crypto_free_pk_env(pk1);
  1609. if (pk2) crypto_free_pk_env(pk2);
  1610. if (rp1) routerinfo_free(rp1);
  1611. if (rp2) routerinfo_free(rp2);
  1612. tor_free(dir1); /* XXXX And more !*/
  1613. tor_free(dir2); /* And more !*/
  1614. /* Try out version parsing functionality */
  1615. test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
  1616. test_eq(0, ver1.major);
  1617. test_eq(3, ver1.minor);
  1618. test_eq(4, ver1.micro);
  1619. test_eq(VER_PRE, ver1.status);
  1620. test_eq(2, ver1.patchlevel);
  1621. test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
  1622. test_eq(0, ver1.major);
  1623. test_eq(3, ver1.minor);
  1624. test_eq(4, ver1.micro);
  1625. test_eq(VER_RC, ver1.status);
  1626. test_eq(1, ver1.patchlevel);
  1627. test_eq(0, tor_version_parse("1.3.4", &ver1));
  1628. test_eq(1, ver1.major);
  1629. test_eq(3, ver1.minor);
  1630. test_eq(4, ver1.micro);
  1631. test_eq(VER_RELEASE, ver1.status);
  1632. test_eq(0, ver1.patchlevel);
  1633. test_eq(0, tor_version_parse("1.3.4.999", &ver1));
  1634. test_eq(1, ver1.major);
  1635. test_eq(3, ver1.minor);
  1636. test_eq(4, ver1.micro);
  1637. test_eq(VER_RELEASE, ver1.status);
  1638. test_eq(999, ver1.patchlevel);
  1639. test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
  1640. test_eq(0, ver1.major);
  1641. test_eq(1, ver1.minor);
  1642. test_eq(2, ver1.micro);
  1643. test_eq(4, ver1.patchlevel);
  1644. test_eq(VER_RELEASE, ver1.status);
  1645. test_streq("alpha", ver1.status_tag);
  1646. test_eq(0, tor_version_parse("0.1.2.4", &ver1));
  1647. test_eq(0, ver1.major);
  1648. test_eq(1, ver1.minor);
  1649. test_eq(2, ver1.micro);
  1650. test_eq(4, ver1.patchlevel);
  1651. test_eq(VER_RELEASE, ver1.status);
  1652. test_streq("", ver1.status_tag);
  1653. #define test_eq_vs(vs1, vs2) test_eq_type(version_status_t, "%d", (vs1), (vs2))
  1654. #define test_v_i_o(val, ver, lst) \
  1655. test_eq_vs(val, tor_version_is_obsolete(ver, lst))
  1656. /* make sure tor_version_is_obsolete() works */
  1657. test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
  1658. test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
  1659. test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
  1660. test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
  1661. test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
  1662. test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
  1663. test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
  1664. test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
  1665. test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
  1666. test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
  1667. test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
  1668. /* Not on list, but newer than any in same series. */
  1669. test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
  1670. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1671. /* Series newer than any on list. */
  1672. test_v_i_o(VS_NEW, "0.1.2.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1673. /* Series older than any on list. */
  1674. test_v_i_o(VS_OLD, "0.0.1.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1675. /* Not on list, not newer than any on same series. */
  1676. test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
  1677. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1678. /* On list, not newer than any on same series. */
  1679. test_v_i_o(VS_UNRECOMMENDED,
  1680. "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1681. test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  1682. test_eq(1, tor_version_as_new_as(
  1683. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  1684. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
  1685. "0.0.8rc2"));
  1686. test_eq(0, tor_version_as_new_as(
  1687. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  1688. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
  1689. }
  1690. static void
  1691. test_policies(void)
  1692. {
  1693. addr_policy_t *policy, *policy2;
  1694. config_line_t line;
  1695. policy = router_parse_addr_policy_from_string("reject 192.168.0.0/16:*",-1);
  1696. test_eq(NULL, policy->next);
  1697. test_eq(ADDR_POLICY_REJECT, policy->policy_type);
  1698. test_eq(0xc0a80000u, policy->addr);
  1699. test_eq(0xffff0000u, policy->msk);
  1700. test_eq(1, policy->prt_min);
  1701. test_eq(65535, policy->prt_max);
  1702. test_streq("reject 192.168.0.0/16:*", policy->string);
  1703. test_assert(ADDR_POLICY_ACCEPTED ==
  1704. compare_addr_to_addr_policy(0x01020304u, 2, policy));
  1705. test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  1706. compare_addr_to_addr_policy(0, 2, policy));
  1707. test_assert(ADDR_POLICY_REJECTED ==
  1708. compare_addr_to_addr_policy(0xc0a80102, 2, policy));
  1709. policy2 = NULL;
  1710. test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1));
  1711. test_assert(policy2);
  1712. test_assert(!exit_policy_is_general_exit(policy));
  1713. test_assert(exit_policy_is_general_exit(policy2));
  1714. test_assert(cmp_addr_policies(policy, policy2));
  1715. test_assert(!cmp_addr_policies(policy2, policy2));
  1716. test_assert(!policy_is_reject_star(policy2));
  1717. test_assert(policy_is_reject_star(policy));
  1718. addr_policy_free(policy);
  1719. addr_policy_free(policy2);
  1720. /* make sure compacting logic works. */
  1721. policy = NULL;
  1722. line.key = (char*)"foo";
  1723. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  1724. line.next = NULL;
  1725. test_assert(0 == policies_parse_exit_policy(&line, &policy, 0));
  1726. test_assert(policy);
  1727. test_streq(policy->string, "accept *:80");
  1728. test_streq(policy->next->string, "reject *:*");
  1729. test_eq_ptr(policy->next->next, NULL);
  1730. addr_policy_free(policy);
  1731. }
  1732. static void
  1733. test_rend_fns(void)
  1734. {
  1735. char address1[] = "fooaddress.onion";
  1736. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  1737. char address3[] = "fooaddress.exit";
  1738. char address4[] = "tor.eff.org";
  1739. rend_service_descriptor_t *d1, *d2;
  1740. char *encoded;
  1741. size_t len;
  1742. crypto_pk_env_t *pk1, *pk2;
  1743. time_t now;
  1744. pk1 = pk_generate(0);
  1745. pk2 = pk_generate(1);
  1746. /* Test unversioned descriptor */
  1747. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  1748. d1->pk = crypto_pk_dup_key(pk1);
  1749. now = time(NULL);
  1750. d1->timestamp = now;
  1751. d1->n_intro_points = 3;
  1752. d1->version = 0;
  1753. d1->intro_points = tor_malloc(sizeof(char*)*3);
  1754. d1->intro_points[0] = tor_strdup("tom");
  1755. d1->intro_points[1] = tor_strdup("crow");
  1756. d1->intro_points[2] = tor_strdup("joel");
  1757. test_assert(! rend_encode_service_descriptor(d1, 0, pk1, &encoded, &len));
  1758. d2 = rend_parse_service_descriptor(encoded, len);
  1759. test_assert(d2);
  1760. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  1761. test_eq(d2->timestamp, now);
  1762. test_eq(d2->version, 0);
  1763. test_eq(d2->protocols, 1);
  1764. test_eq(d2->n_intro_points, 3);
  1765. test_streq(d2->intro_points[0], "tom");
  1766. test_streq(d2->intro_points[1], "crow");
  1767. test_streq(d2->intro_points[2], "joel");
  1768. test_eq(NULL, d2->intro_point_extend_info);
  1769. rend_service_descriptor_free(d1);
  1770. rend_service_descriptor_free(d2);
  1771. tor_free(encoded);
  1772. /* Test versioned descriptor. */
  1773. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  1774. d1->pk = crypto_pk_dup_key(pk1);
  1775. now = time(NULL);
  1776. d1->timestamp = now;
  1777. d1->n_intro_points = 2;
  1778. d1->version = 1;
  1779. d1->protocols = 60;
  1780. d1->intro_points = tor_malloc(sizeof(char*)*2);
  1781. d1->intro_point_extend_info = tor_malloc(sizeof(extend_info_t*)*2);
  1782. d1->intro_points[0] = tor_strdup("tom");
  1783. d1->intro_points[1] = tor_strdup("crow");
  1784. d1->intro_point_extend_info[0] = tor_malloc_zero(sizeof(extend_info_t));
  1785. strlcpy(d1->intro_point_extend_info[0]->nickname, "tom", 4);
  1786. d1->intro_point_extend_info[0]->addr = 1234;
  1787. d1->intro_point_extend_info[0]->port = 4567;
  1788. d1->intro_point_extend_info[0]->onion_key = crypto_pk_dup_key(pk1);
  1789. memset(d1->intro_point_extend_info[0]->identity_digest, 'a', DIGEST_LEN);
  1790. d1->intro_point_extend_info[1] = tor_malloc_zero(sizeof(extend_info_t));
  1791. strlcpy(d1->intro_point_extend_info[1]->nickname, "crow", 5);
  1792. d1->intro_point_extend_info[1]->addr = 6060842;
  1793. d1->intro_point_extend_info[1]->port = 8000;
  1794. d1->intro_point_extend_info[1]->onion_key = crypto_pk_dup_key(pk2);
  1795. memset(d1->intro_point_extend_info[1]->identity_digest, 'b', DIGEST_LEN);
  1796. test_assert(! rend_encode_service_descriptor(d1, 1, pk1, &encoded, &len));
  1797. d2 = rend_parse_service_descriptor(encoded, len);
  1798. test_assert(d2);
  1799. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  1800. test_eq(d2->timestamp, now);
  1801. test_eq(d2->version, 1);
  1802. test_eq(d2->protocols, 60);
  1803. test_eq(d2->n_intro_points, 2);
  1804. test_streq(d2->intro_points[0], d2->intro_point_extend_info[0]->nickname);
  1805. test_streq(d2->intro_points[1], d2->intro_point_extend_info[1]->nickname);
  1806. test_eq(d2->intro_point_extend_info[0]->addr, 1234);
  1807. test_eq(d2->intro_point_extend_info[0]->port, 4567);
  1808. test_assert(!crypto_pk_cmp_keys(pk1,
  1809. d2->intro_point_extend_info[0]->onion_key));
  1810. test_memeq(d2->intro_point_extend_info[0]->identity_digest,
  1811. d1->intro_point_extend_info[0]->identity_digest, DIGEST_LEN);
  1812. test_eq(d2->intro_point_extend_info[1]->addr, 6060842);
  1813. test_eq(d2->intro_point_extend_info[1]->port, 8000);
  1814. test_memeq(d2->intro_point_extend_info[1]->identity_digest,
  1815. d1->intro_point_extend_info[1]->identity_digest, DIGEST_LEN);
  1816. test_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
  1817. test_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
  1818. test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
  1819. test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
  1820. rend_service_descriptor_free(d1);
  1821. rend_service_descriptor_free(d2);
  1822. crypto_free_pk_env(pk1);
  1823. crypto_free_pk_env(pk2);
  1824. }
  1825. static void
  1826. bench_aes(void)
  1827. {
  1828. int len, i;
  1829. char *b1, *b2;
  1830. crypto_cipher_env_t *c;
  1831. struct timeval start, end;
  1832. const int iters = 100000;
  1833. uint64_t nsec;
  1834. c = crypto_new_cipher_env();
  1835. crypto_cipher_generate_key(c);
  1836. crypto_cipher_encrypt_init_cipher(c);
  1837. for (len = 1; len <= 8192; len *= 2) {
  1838. b1 = tor_malloc_zero(len);
  1839. b2 = tor_malloc_zero(len);
  1840. tor_gettimeofday(&start);
  1841. for (i = 0; i < iters; ++i) {
  1842. crypto_cipher_encrypt(c, b1, b2, len);
  1843. }
  1844. tor_gettimeofday(&end);
  1845. tor_free(b1);
  1846. tor_free(b2);
  1847. nsec = (uint64_t) tv_udiff(&start,&end);
  1848. nsec *= 1000;
  1849. nsec /= (iters*len);
  1850. printf("%d bytes: "U64_FORMAT" nsec per byte\n", len,
  1851. U64_PRINTF_ARG(nsec));
  1852. }
  1853. crypto_free_cipher_env(c);
  1854. }
  1855. static void
  1856. test_mempool(void)
  1857. {
  1858. mp_pool_t *pool;
  1859. smartlist_t *allocated;
  1860. int i;
  1861. pool = mp_pool_new(1, 100);
  1862. test_assert(pool->new_chunk_capacity >= 100);
  1863. test_assert(pool->item_alloc_size >= sizeof(void*)+1);
  1864. mp_pool_destroy(pool);
  1865. pool = mp_pool_new(241, 2500);
  1866. test_assert(pool->new_chunk_capacity >= 10);
  1867. test_assert(pool->item_alloc_size >= sizeof(void*)+241);
  1868. test_eq(pool->item_alloc_size & 0x03, 0);
  1869. test_assert(pool->new_chunk_capacity < 60);
  1870. allocated = smartlist_create();
  1871. for (i = 0; i < 100000; ++i) {
  1872. if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) {
  1873. void *m = mp_pool_get(pool);
  1874. memset(m, 0x09, 241);
  1875. smartlist_add(allocated, m);
  1876. //printf("%d: %p\n", i, m);
  1877. //mp_pool_assert_ok(pool);
  1878. } else {
  1879. int idx = crypto_rand_int(smartlist_len(allocated));
  1880. void *m = smartlist_get(allocated, idx);
  1881. //printf("%d: free %p\n", i, m);
  1882. smartlist_del(allocated, idx);
  1883. mp_pool_release(m);
  1884. //mp_pool_assert_ok(pool);
  1885. }
  1886. if (crypto_rand_int(777)==0)
  1887. mp_pool_clean(pool, -1);
  1888. if (i % 777)
  1889. mp_pool_assert_ok(pool);
  1890. }
  1891. SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m));
  1892. mp_pool_assert_ok(pool);
  1893. mp_pool_clean(pool, 0);
  1894. mp_pool_assert_ok(pool);
  1895. mp_pool_destroy(pool);
  1896. smartlist_free(allocated);
  1897. }
  1898. int
  1899. main(int c, char**v)
  1900. {
  1901. or_options_t *options = options_new();
  1902. char *errmsg = NULL;
  1903. (void) c;
  1904. (void) v;
  1905. options->command = CMD_RUN_UNITTESTS;
  1906. rep_hist_init();
  1907. network_init();
  1908. setup_directory();
  1909. options_init(options);
  1910. options->DataDirectory = tor_strdup(temp_dir);
  1911. if (set_options(options, &errmsg) < 0) {
  1912. printf("Failed to set initial options: %s\n", errmsg);
  1913. tor_free(errmsg);
  1914. return 1;
  1915. }
  1916. crypto_seed_rng();
  1917. if (0) {
  1918. bench_aes();
  1919. return 0;
  1920. }
  1921. atexit(remove_directory);
  1922. printf("Running Tor unit tests on %s\n", get_uname());
  1923. puts("========================== Buffers =========================");
  1924. test_buffers();
  1925. puts("\n========================== Crypto ==========================");
  1926. // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
  1927. test_crypto();
  1928. test_crypto_dh();
  1929. test_crypto_s2k();
  1930. puts("\n========================= Util ============================");
  1931. test_gzip();
  1932. test_util();
  1933. test_smartlist();
  1934. test_mempool();
  1935. test_strmap();
  1936. test_control_formats();
  1937. test_pqueue();
  1938. test_mmap();
  1939. test_threads();
  1940. puts("\n========================= Onion Skins =====================");
  1941. test_onion_handshake();
  1942. puts("\n========================= Directory Formats ===============");
  1943. test_dir_format();
  1944. puts("\n========================= Policies ===================");
  1945. test_policies();
  1946. puts("\n========================= Rendezvous functionality ========");
  1947. test_rend_fns();
  1948. puts("");
  1949. if (have_failed)
  1950. return 1;
  1951. else
  1952. return 0;
  1953. }