test.c 71 KB

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