test.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char test_c_id[] =
  6. "$Id$";
  7. /**
  8. * \file test.c
  9. * \brief Unit tests for many pieces of the lower level Tor modules.
  10. **/
  11. #include "orconfig.h"
  12. #include <stdio.h>
  13. #ifdef HAVE_FCNTL_H
  14. #include <fcntl.h>
  15. #endif
  16. #ifdef MS_WINDOWS
  17. /* For mkdir() */
  18. #include <direct.h>
  19. #else
  20. #include <dirent.h>
  21. #endif
  22. #include "or.h"
  23. #include "../common/test.h"
  24. #include "../common/torgzip.h"
  25. int have_failed = 0;
  26. /* These functions are file-local, but are exposed so we can test. */
  27. void add_fingerprint_to_dir(const char *nickname, const char *fp,
  28. smartlist_t *list);
  29. void get_platform_str(char *platform, size_t len);
  30. size_t read_escaped_data(const char *data, size_t len, int translate_newlines,
  31. char **out);
  32. or_options_t *options_new(void);
  33. static char temp_dir[256];
  34. static void
  35. setup_directory(void)
  36. {
  37. static int is_setup = 0;
  38. int r;
  39. if (is_setup) return;
  40. #ifdef MS_WINDOWS
  41. // XXXX
  42. tor_snprintf(temp_dir, sizeof(temp_dir),
  43. "c:\\windows\\temp\\tor_test_%d", (int)getpid());
  44. r = mkdir(temp_dir);
  45. #else
  46. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
  47. r = mkdir(temp_dir, 0700);
  48. #endif
  49. if (r) {
  50. fprintf(stderr, "Can't create directory %s:", temp_dir);
  51. perror("");
  52. exit(1);
  53. }
  54. is_setup = 1;
  55. }
  56. static const char *
  57. get_fname(const char *name)
  58. {
  59. static char buf[1024];
  60. setup_directory();
  61. tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
  62. return buf;
  63. }
  64. static void
  65. remove_directory(void)
  66. {
  67. smartlist_t *elements = tor_listdir(temp_dir);
  68. if (elements) {
  69. SMARTLIST_FOREACH(elements, const char *, cp,
  70. {
  71. size_t len = strlen(cp)+strlen(temp_dir)+16;
  72. char *tmp = tor_malloc(len);
  73. tor_snprintf(tmp, len, "%s"PATH_SEPARATOR"%s", temp_dir, cp);
  74. unlink(tmp);
  75. tor_free(tmp);
  76. });
  77. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  78. smartlist_free(elements);
  79. }
  80. rmdir(temp_dir);
  81. }
  82. static void
  83. test_buffers(void)
  84. {
  85. char str[256];
  86. char str2[256];
  87. buf_t *buf;
  88. int j;
  89. /****
  90. * buf_new
  91. ****/
  92. if (!(buf = buf_new()))
  93. test_fail();
  94. test_eq(buf_capacity(buf), 4096);
  95. test_eq(buf_datalen(buf), 0);
  96. /****
  97. * General pointer frobbing
  98. */
  99. for (j=0;j<256;++j) {
  100. str[j] = (char)j;
  101. }
  102. write_to_buf(str, 256, buf);
  103. write_to_buf(str, 256, buf);
  104. test_eq(buf_datalen(buf), 512);
  105. fetch_from_buf(str2, 200, buf);
  106. test_memeq(str, str2, 200);
  107. test_eq(buf_datalen(buf), 312);
  108. memset(str2, 0, sizeof(str2));
  109. fetch_from_buf(str2, 256, buf);
  110. test_memeq(str+200, str2, 56);
  111. test_memeq(str, str2+56, 200);
  112. test_eq(buf_datalen(buf), 56);
  113. memset(str2, 0, sizeof(str2));
  114. /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
  115. * another 3584 bytes, we hit the end. */
  116. for (j=0;j<15;++j) {
  117. write_to_buf(str, 256, buf);
  118. }
  119. assert_buf_ok(buf);
  120. test_eq(buf_datalen(buf), 3896);
  121. fetch_from_buf(str2, 56, buf);
  122. test_eq(buf_datalen(buf), 3840);
  123. test_memeq(str+200, str2, 56);
  124. for (j=0;j<15;++j) {
  125. memset(str2, 0, sizeof(str2));
  126. fetch_from_buf(str2, 256, buf);
  127. test_memeq(str, str2, 256);
  128. }
  129. test_eq(buf_datalen(buf), 0);
  130. buf_free(buf);
  131. /* Okay, now make sure growing can work. */
  132. buf = buf_new_with_capacity(16);
  133. test_eq(buf_capacity(buf), 16);
  134. write_to_buf(str+1, 255, buf);
  135. test_eq(buf_capacity(buf), 256);
  136. fetch_from_buf(str2, 254, buf);
  137. test_memeq(str+1, str2, 254);
  138. test_eq(buf_capacity(buf), 256);
  139. assert_buf_ok(buf);
  140. write_to_buf(str, 32, buf);
  141. test_eq(buf_capacity(buf), 256);
  142. assert_buf_ok(buf);
  143. write_to_buf(str, 256, buf);
  144. assert_buf_ok(buf);
  145. test_eq(buf_capacity(buf), 512);
  146. test_eq(buf_datalen(buf), 33+256);
  147. fetch_from_buf(str2, 33, buf);
  148. test_eq(*str2, str[255]);
  149. test_memeq(str2+1, str, 32);
  150. test_eq(buf_capacity(buf), 512);
  151. test_eq(buf_datalen(buf), 256);
  152. fetch_from_buf(str2, 256, buf);
  153. test_memeq(str, str2, 256);
  154. /* now try shrinking: case 1. */
  155. buf_free(buf);
  156. buf = buf_new_with_capacity(33668);
  157. for (j=0;j<67;++j) {
  158. write_to_buf(str,255, buf);
  159. }
  160. test_eq(buf_capacity(buf), 33668);
  161. test_eq(buf_datalen(buf), 17085);
  162. for (j=0; j < 40; ++j) {
  163. fetch_from_buf(str2, 255,buf);
  164. test_memeq(str2, str, 255);
  165. }
  166. /* now try shrinking: case 2. */
  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. for (j=0; j < 20; ++j) {
  173. fetch_from_buf(str2, 255,buf);
  174. test_memeq(str2, str, 255);
  175. }
  176. for (j=0;j<80;++j) {
  177. write_to_buf(str,255, buf);
  178. }
  179. test_eq(buf_capacity(buf),33668);
  180. for (j=0; j < 120; ++j) {
  181. fetch_from_buf(str2, 255,buf);
  182. test_memeq(str2, str, 255);
  183. }
  184. #if 0
  185. /****
  186. * read_to_buf
  187. ****/
  188. s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
  189. write(s, str, 256);
  190. close(s);
  191. s = open(get_fname("data"), O_RDONLY, 0);
  192. eof = 0;
  193. errno = 0; /* XXXX */
  194. i = read_to_buf(s, 10, buf, &eof);
  195. printf("%s\n", strerror(errno));
  196. test_eq(i, 10);
  197. test_eq(eof, 0);
  198. test_eq(buf_capacity(buf), 4096);
  199. test_eq(buf_datalen(buf), 10);
  200. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
  201. /* Test reading 0 bytes. */
  202. i = read_to_buf(s, 0, buf, &eof);
  203. test_eq(buf_capacity(buf), 512*1024);
  204. test_eq(buf_datalen(buf), 10);
  205. test_eq(eof, 0);
  206. test_eq(i, 0);
  207. /* Now test when buffer is filled exactly. */
  208. buf2 = buf_new_with_capacity(6);
  209. i = read_to_buf(s, 6, buf2, &eof);
  210. test_eq(buf_capacity(buf2), 6);
  211. test_eq(buf_datalen(buf2), 6);
  212. test_eq(eof, 0);
  213. test_eq(i, 6);
  214. test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
  215. buf_free(buf2);
  216. /* Now test when buffer is filled with more data to read. */
  217. buf2 = buf_new_with_capacity(32);
  218. i = read_to_buf(s, 128, buf2, &eof);
  219. test_eq(buf_capacity(buf2), 128);
  220. test_eq(buf_datalen(buf2), 32);
  221. test_eq(eof, 0);
  222. test_eq(i, 32);
  223. buf_free(buf2);
  224. /* Now read to eof. */
  225. test_assert(buf_capacity(buf) > 256);
  226. i = read_to_buf(s, 1024, buf, &eof);
  227. test_eq(i, (256-32-10-6));
  228. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  229. test_eq(buf_datalen(buf), 256-6-32);
  230. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
  231. test_eq(eof, 0);
  232. i = read_to_buf(s, 1024, buf, &eof);
  233. test_eq(i, 0);
  234. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  235. test_eq(buf_datalen(buf), 256-6-32);
  236. test_eq(eof, 1);
  237. #endif
  238. buf_free(buf);
  239. }
  240. static void
  241. test_crypto_dh(void)
  242. {
  243. crypto_dh_env_t *dh1, *dh2;
  244. char p1[DH_BYTES];
  245. char p2[DH_BYTES];
  246. char s1[DH_BYTES];
  247. char s2[DH_BYTES];
  248. int s1len, s2len;
  249. dh1 = crypto_dh_new();
  250. dh2 = crypto_dh_new();
  251. test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
  252. test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
  253. memset(p1, 0, DH_BYTES);
  254. memset(p2, 0, DH_BYTES);
  255. test_memeq(p1, p2, DH_BYTES);
  256. test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  257. test_memneq(p1, p2, DH_BYTES);
  258. test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  259. test_memneq(p1, p2, DH_BYTES);
  260. memset(s1, 0, DH_BYTES);
  261. memset(s2, 0xFF, DH_BYTES);
  262. s1len = crypto_dh_compute_secret(dh1, p2, DH_BYTES, s1, 50);
  263. s2len = crypto_dh_compute_secret(dh2, p1, DH_BYTES, s2, 50);
  264. test_assert(s1len > 0);
  265. test_eq(s1len, s2len);
  266. test_memeq(s1, s2, s1len);
  267. crypto_dh_free(dh1);
  268. crypto_dh_free(dh2);
  269. }
  270. static void
  271. test_crypto(void)
  272. {
  273. crypto_cipher_env_t *env1, *env2;
  274. crypto_pk_env_t *pk1, *pk2;
  275. char *data1, *data2, *data3, *cp;
  276. int i, j, p, len;
  277. size_t size;
  278. data1 = tor_malloc(1024);
  279. data2 = tor_malloc(1024);
  280. data3 = tor_malloc(1024);
  281. test_assert(data1 && data2 && data3);
  282. /* Try out RNG. */
  283. test_assert(! crypto_seed_rng());
  284. crypto_rand(data1, 100);
  285. crypto_rand(data2, 100);
  286. test_memneq(data1,data2,100);
  287. #if 0
  288. /* Try out identity ciphers. */
  289. env1 = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  290. test_neq(env1, 0);
  291. test_eq(crypto_cipher_generate_key(env1), 0);
  292. test_eq(crypto_cipher_encrypt_init_cipher(env1), 0);
  293. for (i = 0; i < 1024; ++i) {
  294. data1[i] = (char) i*73;
  295. }
  296. crypto_cipher_encrypt(env1, data2, data1, 1024);
  297. test_memeq(data1, data2, 1024);
  298. crypto_free_cipher_env(env1);
  299. #endif
  300. /* Now, test encryption and decryption with stream cipher. */
  301. data1[0]='\0';
  302. for (i = 1023; i>0; i -= 35)
  303. strncat(data1, "Now is the time for all good onions", i);
  304. memset(data2, 0, 1024);
  305. memset(data3, 0, 1024);
  306. env1 = crypto_new_cipher_env();
  307. test_neq(env1, 0);
  308. env2 = crypto_new_cipher_env();
  309. test_neq(env2, 0);
  310. j = crypto_cipher_generate_key(env1);
  311. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  312. crypto_cipher_encrypt_init_cipher(env1);
  313. crypto_cipher_decrypt_init_cipher(env2);
  314. /* Try encrypting 512 chars. */
  315. crypto_cipher_encrypt(env1, data2, data1, 512);
  316. crypto_cipher_decrypt(env2, data3, data2, 512);
  317. test_memeq(data1, data3, 512);
  318. test_memneq(data1, data2, 512);
  319. /* Now encrypt 1 at a time, and get 1 at a time. */
  320. for (j = 512; j < 560; ++j) {
  321. crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
  322. }
  323. for (j = 512; j < 560; ++j) {
  324. crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
  325. }
  326. test_memeq(data1, data3, 560);
  327. /* Now encrypt 3 at a time, and get 5 at a time. */
  328. for (j = 560; j < 1024-5; j += 3) {
  329. crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
  330. }
  331. for (j = 560; j < 1024-5; j += 5) {
  332. crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
  333. }
  334. test_memeq(data1, data3, 1024-5);
  335. /* Now make sure that when we encrypt with different chunk sizes, we get
  336. the same results. */
  337. crypto_free_cipher_env(env2);
  338. memset(data3, 0, 1024);
  339. env2 = crypto_new_cipher_env();
  340. test_neq(env2, 0);
  341. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  342. crypto_cipher_encrypt_init_cipher(env2);
  343. for (j = 0; j < 1024-16; j += 17) {
  344. crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
  345. }
  346. for (j= 0; j < 1024-16; ++j) {
  347. if (data2[j] != data3[j]) {
  348. printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
  349. }
  350. }
  351. test_memeq(data2, data3, 1024-16);
  352. crypto_free_cipher_env(env1);
  353. crypto_free_cipher_env(env2);
  354. /* Test vectors for stream ciphers. */
  355. /* XXXX Look up some test vectors for the ciphers and make sure we match. */
  356. /* Test SHA-1 with a test vector from the specification. */
  357. i = crypto_digest(data1, "abc", 3);
  358. test_memeq(data1,
  359. "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
  360. "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
  361. /* Public-key ciphers */
  362. pk1 = crypto_new_pk_env();
  363. pk2 = crypto_new_pk_env();
  364. test_assert(pk1 && pk2);
  365. test_assert(! crypto_pk_generate_key(pk1));
  366. test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &size));
  367. test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, size));
  368. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  369. tor_free(cp);
  370. /* Check DER encoding */
  371. i=crypto_pk_DER64_encode_public_key(pk1, &cp);
  372. test_assert(i>0);
  373. test_assert(cp);
  374. test_assert(!strchr(cp, ' '));
  375. test_assert(!strchr(cp, '\n'));
  376. test_eq(0, crypto_pk_cmp_keys(pk1, pk1));
  377. crypto_free_pk_env(pk2);
  378. pk2 = crypto_pk_DER64_decode_public_key(cp);
  379. test_assert(pk2);
  380. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  381. tor_free(cp);
  382. test_eq(128, crypto_pk_keysize(pk1));
  383. test_eq(128, crypto_pk_keysize(pk2));
  384. test_eq(128, crypto_pk_public_encrypt(pk2, data1, "Hello whirled.", 15,
  385. PK_PKCS1_OAEP_PADDING));
  386. test_eq(128, crypto_pk_public_encrypt(pk1, data2, "Hello whirled.", 15,
  387. PK_PKCS1_OAEP_PADDING));
  388. /* oaep padding should make encryption not match */
  389. test_memneq(data1, data2, 128);
  390. test_eq(15, crypto_pk_private_decrypt(pk1, data3, data1, 128,
  391. PK_PKCS1_OAEP_PADDING,1));
  392. test_streq(data3, "Hello whirled.");
  393. memset(data3, 0, 1024);
  394. test_eq(15, crypto_pk_private_decrypt(pk1, data3, data2, 128,
  395. PK_PKCS1_OAEP_PADDING,1));
  396. test_streq(data3, "Hello whirled.");
  397. /* Can't decrypt with public key. */
  398. test_eq(-1, crypto_pk_private_decrypt(pk2, data3, data2, 128,
  399. PK_PKCS1_OAEP_PADDING,1));
  400. /* Try again with bad padding */
  401. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  402. test_eq(-1, crypto_pk_private_decrypt(pk1, data3, data2, 128,
  403. PK_PKCS1_OAEP_PADDING,1));
  404. /* File operations: save and load private key */
  405. test_assert(! crypto_pk_write_private_key_to_filename(pk1,
  406. get_fname("pkey1")));
  407. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  408. get_fname("pkey1")));
  409. test_eq(15, crypto_pk_private_decrypt(pk2, data3, data1, 128,
  410. PK_PKCS1_OAEP_PADDING,1));
  411. /* Now try signing. */
  412. strcpy(data1, "Ossifrage");
  413. test_eq(128, crypto_pk_private_sign(pk1, data2, data1, 10));
  414. test_eq(10, crypto_pk_public_checksig(pk1, data3, data2, 128));
  415. test_streq(data3, "Ossifrage");
  416. /* Try signing digests. */
  417. test_eq(128, crypto_pk_private_sign_digest(pk1, data2, data1, 10));
  418. test_eq(20, crypto_pk_public_checksig(pk1, data3, data2, 128));
  419. test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  420. test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  421. /*XXXX test failed signing*/
  422. /* Try encoding */
  423. crypto_free_pk_env(pk2);
  424. pk2 = NULL;
  425. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  426. test_assert(i>0);
  427. pk2 = crypto_pk_asn1_decode(data1, i);
  428. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  429. /* Try with hybrid encryption wrappers. */
  430. crypto_rand(data1, 1024);
  431. for (i = 0; i < 3; ++i) {
  432. for (j = 85; j < 140; ++j) {
  433. memset(data2,0,1024);
  434. memset(data3,0,1024);
  435. if (i == 0 && j < 129)
  436. continue;
  437. p = (i==0)?PK_NO_PADDING:
  438. (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
  439. len = crypto_pk_public_hybrid_encrypt(pk1,data2,data1,j,p,0);
  440. test_assert(len>=0);
  441. len = crypto_pk_private_hybrid_decrypt(pk1,data3,data2,len,p,1);
  442. test_eq(len,j);
  443. test_memeq(data1,data3,j);
  444. }
  445. }
  446. crypto_free_pk_env(pk1);
  447. crypto_free_pk_env(pk2);
  448. /* Base64 tests */
  449. strcpy(data1, "Test string that contains 35 chars.");
  450. strcat(data1, " 2nd string that contains 35 chars.");
  451. i = base64_encode(data2, 1024, data1, 71);
  452. j = base64_decode(data3, 1024, data2, i);
  453. test_streq(data3, data1);
  454. test_eq(j, 71);
  455. test_assert(data2[i] == '\0');
  456. crypto_rand(data1, DIGEST_LEN);
  457. memset(data2, 100, 1024);
  458. digest_to_base64(data2, data1);
  459. test_eq(BASE64_DIGEST_LEN, strlen(data2));
  460. test_eq(100, data2[BASE64_DIGEST_LEN+2]);
  461. memset(data3, 99, 1024);
  462. digest_from_base64(data3, data2);
  463. test_memeq(data1, data3, DIGEST_LEN);
  464. test_eq(99, data3[DIGEST_LEN+1]);
  465. /* Base32 tests */
  466. strcpy(data1, "5chrs");
  467. /* bit pattern is: [35 63 68 72 73] ->
  468. * [00110101 01100011 01101000 01110010 01110011]
  469. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  470. */
  471. base32_encode(data2, 9, data1, 5);
  472. test_streq(data2, "gvrwq4tt");
  473. strcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4");
  474. base32_encode(data2, 30, data1, 10);
  475. test_streq(data2, "772w2rfobvomsywe");
  476. /* Base16 tests */
  477. strcpy(data1, "6chrs\xff");
  478. base16_encode(data2, 13, data1, 6);
  479. test_streq(data2, "3663687273FF");
  480. strcpy(data1, "f0d678affc000100");
  481. i = base16_decode(data2, 8, data1, 16);
  482. test_eq(i,0);
  483. test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  484. tor_free(data1);
  485. tor_free(data2);
  486. tor_free(data3);
  487. }
  488. static void
  489. test_crypto_s2k(void)
  490. {
  491. char buf[29];
  492. char buf2[29];
  493. char *buf3;
  494. int i;
  495. memset(buf, 0, sizeof(buf));
  496. memset(buf2, 0, sizeof(buf2));
  497. buf3 = tor_malloc(65536);
  498. memset(buf3, 0, 65536);
  499. secret_to_key(buf+9, 20, "", 0, buf);
  500. crypto_digest(buf2+9, buf3, 1024);
  501. test_memeq(buf, buf2, 29);
  502. memcpy(buf,"vrbacrda",8);
  503. memcpy(buf2,"vrbacrda",8);
  504. buf[8] = 96;
  505. buf2[8] = 96;
  506. secret_to_key(buf+9, 20, "12345678", 8, buf);
  507. for (i = 0; i < 65536; i += 16) {
  508. memcpy(buf3+i, "vrbacrda12345678", 16);
  509. }
  510. crypto_digest(buf2+9, buf3, 65536);
  511. test_memeq(buf, buf2, 29);
  512. }
  513. static int
  514. _compare_strs(const void **a, const void **b)
  515. {
  516. const char *s1 = *a, *s2 = *b;
  517. return strcmp(s1, s2);
  518. }
  519. static int
  520. _compare_without_first_ch(const void *a, const void **b)
  521. {
  522. const char *s1 = a, *s2 = *b;
  523. return strcasecmp(s1+1, s2);
  524. }
  525. static void
  526. test_util(void)
  527. {
  528. struct timeval start, end;
  529. struct tm a_time;
  530. smartlist_t *sl;
  531. char timestr[RFC1123_TIME_LEN+1];
  532. char buf[1024];
  533. time_t t_res;
  534. int i;
  535. uint32_t u32;
  536. uint16_t u16;
  537. char *cp, *k, *v;
  538. start.tv_sec = 5;
  539. start.tv_usec = 5000;
  540. end.tv_sec = 5;
  541. end.tv_usec = 5000;
  542. test_eq(0L, tv_udiff(&start, &end));
  543. end.tv_usec = 7000;
  544. test_eq(2000L, tv_udiff(&start, &end));
  545. end.tv_sec = 6;
  546. test_eq(1002000L, tv_udiff(&start, &end));
  547. end.tv_usec = 0;
  548. test_eq(995000L, tv_udiff(&start, &end));
  549. end.tv_sec = 4;
  550. test_eq(-1005000L, tv_udiff(&start, &end));
  551. /* The test values here are confirmed to be correct on a platform
  552. * with a working timegm. */
  553. a_time.tm_year = 2003-1900;
  554. a_time.tm_mon = 7;
  555. a_time.tm_mday = 30;
  556. a_time.tm_hour = 6;
  557. a_time.tm_min = 14;
  558. a_time.tm_sec = 55;
  559. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  560. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  561. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  562. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  563. a_time.tm_mday = 10;
  564. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  565. format_rfc1123_time(timestr, 0);
  566. test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
  567. format_rfc1123_time(timestr, (time_t)1091580502UL);
  568. test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
  569. t_res = 0;
  570. i = parse_rfc1123_time(timestr, &t_res);
  571. test_eq(i,0);
  572. test_eq(t_res, (time_t)1091580502UL);
  573. /* Test smartlist */
  574. sl = smartlist_create();
  575. smartlist_add(sl, (void*)1);
  576. smartlist_add(sl, (void*)2);
  577. smartlist_add(sl, (void*)3);
  578. smartlist_add(sl, (void*)4);
  579. smartlist_del_keeporder(sl, 1);
  580. smartlist_insert(sl, 1, (void*)22);
  581. smartlist_insert(sl, 0, (void*)0);
  582. smartlist_insert(sl, 5, (void*)555);
  583. test_eq_ptr((void*)0, smartlist_get(sl,0));
  584. test_eq_ptr((void*)1, smartlist_get(sl,1));
  585. test_eq_ptr((void*)22, smartlist_get(sl,2));
  586. test_eq_ptr((void*)3, smartlist_get(sl,3));
  587. test_eq_ptr((void*)4, smartlist_get(sl,4));
  588. test_eq_ptr((void*)555, smartlist_get(sl,5));
  589. smartlist_clear(sl);
  590. smartlist_split_string(sl, "abc", ":", 0, 0);
  591. test_eq(1, smartlist_len(sl));
  592. test_streq("abc", smartlist_get(sl, 0));
  593. smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  594. test_eq(4, smartlist_len(sl));
  595. test_streq("a", smartlist_get(sl, 1));
  596. test_streq("bc", smartlist_get(sl, 2));
  597. test_streq("", smartlist_get(sl, 3));
  598. cp = smartlist_join_strings(sl, "", 0, NULL);
  599. test_streq(cp, "abcabc");
  600. tor_free(cp);
  601. cp = smartlist_join_strings(sl, "!", 0, NULL);
  602. test_streq(cp, "abc!a!bc!");
  603. tor_free(cp);
  604. cp = smartlist_join_strings(sl, "XY", 0, NULL);
  605. test_streq(cp, "abcXYaXYbcXY");
  606. tor_free(cp);
  607. cp = smartlist_join_strings(sl, "XY", 1, NULL);
  608. test_streq(cp, "abcXYaXYbcXYXY");
  609. tor_free(cp);
  610. cp = smartlist_join_strings(sl, "", 1, NULL);
  611. test_streq(cp, "abcabc");
  612. tor_free(cp);
  613. smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
  614. test_eq(8, smartlist_len(sl));
  615. test_streq("", smartlist_get(sl, 4));
  616. test_streq("def", smartlist_get(sl, 5));
  617. test_streq(" ", smartlist_get(sl, 6));
  618. test_streq("ghijk", smartlist_get(sl, 7));
  619. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  620. smartlist_clear(sl);
  621. smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  622. test_eq(3, smartlist_len(sl));
  623. test_streq("a", smartlist_get(sl,0));
  624. test_streq("bbd", smartlist_get(sl,1));
  625. test_streq("cdef", smartlist_get(sl,2));
  626. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  627. SPLIT_SKIP_SPACE, 0);
  628. test_eq(8, smartlist_len(sl));
  629. test_streq("z", smartlist_get(sl,3));
  630. test_streq("zhasd", smartlist_get(sl,4));
  631. test_streq("", smartlist_get(sl,5));
  632. test_streq("bnud", smartlist_get(sl,6));
  633. test_streq("", smartlist_get(sl,7));
  634. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  635. smartlist_clear(sl);
  636. smartlist_split_string(sl, " ab\tc \td ef ", NULL,
  637. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  638. test_eq(4, smartlist_len(sl));
  639. test_streq("ab", smartlist_get(sl,0));
  640. test_streq("c", smartlist_get(sl,1));
  641. test_streq("d", smartlist_get(sl,2));
  642. test_streq("ef", smartlist_get(sl,3));
  643. smartlist_split_string(sl, "ghi\tj", NULL,
  644. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  645. test_eq(6, smartlist_len(sl));
  646. test_streq("ghi", smartlist_get(sl,4));
  647. test_streq("j", smartlist_get(sl,5));
  648. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  649. smartlist_clear(sl);
  650. cp = smartlist_join_strings(sl, "XY", 0, NULL);
  651. test_streq(cp, "");
  652. tor_free(cp);
  653. cp = smartlist_join_strings(sl, "XY", 1, NULL);
  654. test_streq(cp, "XY");
  655. tor_free(cp);
  656. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  657. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  658. test_eq(3, smartlist_len(sl));
  659. test_streq("z", smartlist_get(sl, 0));
  660. test_streq("zhasd", smartlist_get(sl, 1));
  661. test_streq("bnud", smartlist_get(sl, 2));
  662. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  663. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  664. test_eq(5, smartlist_len(sl));
  665. test_streq("z", smartlist_get(sl, 3));
  666. test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
  667. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  668. smartlist_clear(sl);
  669. smartlist_split_string(sl, "abcd\n", "\n",
  670. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  671. test_eq(1, smartlist_len(sl));
  672. test_streq("abcd", smartlist_get(sl, 0));
  673. smartlist_split_string(sl, "efgh", "\n",
  674. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  675. test_eq(2, smartlist_len(sl));
  676. test_streq("efgh", smartlist_get(sl, 1));
  677. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  678. smartlist_clear(sl);
  679. /* Test smartlist sorting. */
  680. smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
  681. test_eq(7, smartlist_len(sl));
  682. smartlist_sort(sl, _compare_strs);
  683. cp = smartlist_join_strings(sl, ",", 0, NULL);
  684. test_streq(cp,"and,arma,by,nickm,onion,router,the");
  685. tor_free(cp);
  686. test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
  687. _compare_without_first_ch));
  688. test_streq("and", smartlist_bsearch(sl, " AND", _compare_without_first_ch));
  689. test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", _compare_without_first_ch));
  690. /* Test tor_strstrip() */
  691. strcpy(buf, "Testing 1 2 3");
  692. test_eq(0, tor_strstrip(buf, ",!"));
  693. test_streq(buf, "Testing 1 2 3");
  694. strcpy(buf, "!Testing 1 2 3?");
  695. test_eq(5, tor_strstrip(buf, "!? "));
  696. test_streq(buf, "Testing123");
  697. /* Test tor_strpartition() */
  698. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
  699. TERMINATE_IF_EVEN));
  700. test_streq(buf, "abc##def##g");
  701. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
  702. ALWAYS_TERMINATE));
  703. test_streq(buf, "abc##def##g##");
  704. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
  705. TERMINATE_IF_EVEN));
  706. test_streq(buf, "abc##def##ghi##");
  707. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
  708. NEVER_TERMINATE));
  709. test_streq(buf, "abc##def##ghi");
  710. /* Test parse_addr_port */
  711. cp = NULL; u32 = 3; u16 = 3;
  712. test_assert(!parse_addr_port("1.2.3.4", &cp, &u32, &u16));
  713. test_streq(cp, "1.2.3.4");
  714. test_eq(u32, 0x01020304u);
  715. test_eq(u16, 0);
  716. tor_free(cp);
  717. test_assert(!parse_addr_port("4.3.2.1:99", &cp, &u32, &u16));
  718. test_streq(cp, "4.3.2.1");
  719. test_eq(u32, 0x04030201u);
  720. test_eq(u16, 99);
  721. tor_free(cp);
  722. test_assert(!parse_addr_port("nonexistent.address:4040", &cp, NULL, &u16));
  723. test_streq(cp, "nonexistent.address");
  724. test_eq(u16, 4040);
  725. tor_free(cp);
  726. test_assert(!parse_addr_port("localhost:9999", &cp, &u32, &u16));
  727. test_streq(cp, "localhost");
  728. test_eq(u32, 0x7f000001u);
  729. test_eq(u16, 9999);
  730. tor_free(cp);
  731. u32 = 3;
  732. test_assert(!parse_addr_port("localhost", NULL, &u32, &u16));
  733. test_eq(cp, NULL);
  734. test_eq(u32, 0x7f000001u);
  735. test_eq(u16, 0);
  736. tor_free(cp);
  737. /* Test tor_parse_long. */
  738. test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
  739. test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
  740. /* Test parse_line_from_str */
  741. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  742. "k2\n"
  743. "k3 \n" "\n" " \n" "#comment\n"
  744. "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf));
  745. cp = buf;
  746. cp = parse_line_from_str(cp, &k, &v);
  747. test_streq(k, "k");
  748. test_streq(v, "v");
  749. test_assert(!strcmpstart(cp, " key value with"));
  750. cp = parse_line_from_str(cp, &k, &v);
  751. test_streq(k, "key");
  752. test_streq(v, "value with spaces");
  753. test_assert(!strcmpstart(cp, "keykey"));
  754. cp = parse_line_from_str(cp, &k, &v);
  755. test_streq(k, "keykey");
  756. test_streq(v, "val");
  757. test_assert(!strcmpstart(cp, "k2\n"));
  758. cp = parse_line_from_str(cp, &k, &v);
  759. test_streq(k, "k2");
  760. test_streq(v, "");
  761. test_assert(!strcmpstart(cp, "k3 \n"));
  762. cp = parse_line_from_str(cp, &k, &v);
  763. test_streq(k, "k3");
  764. test_streq(v, "");
  765. test_assert(!strcmpstart(cp, "\n \n"));
  766. cp = parse_line_from_str(cp, &k, &v);
  767. test_streq(k, "k4");
  768. test_streq(v, "");
  769. test_assert(!strcmpstart(cp, "k5#abc"));
  770. cp = parse_line_from_str(cp, &k, &v);
  771. test_streq(k, "k5");
  772. test_streq(v, "");
  773. test_assert(!strcmpstart(cp, "k6"));
  774. cp = parse_line_from_str(cp, &k, &v);
  775. test_streq(k, "k6");
  776. test_streq(v, "val");
  777. test_streq(cp, "");
  778. /* Test for strcmpstart and strcmpend. */
  779. test_assert(strcmpstart("abcdef", "abcdef")==0);
  780. test_assert(strcmpstart("abcdef", "abc")==0);
  781. test_assert(strcmpstart("abcdef", "abd")<0);
  782. test_assert(strcmpstart("abcdef", "abb")>0);
  783. test_assert(strcmpstart("ab", "abb")<0);
  784. test_assert(strcmpend("abcdef", "abcdef")==0);
  785. test_assert(strcmpend("abcdef", "def")==0);
  786. test_assert(strcmpend("abcdef", "deg")<0);
  787. test_assert(strcmpend("abcdef", "dee")>0);
  788. test_assert(strcmpend("ab", "abb")<0);
  789. {
  790. char tmpbuf[INET_NTOA_BUF_LEN];
  791. struct in_addr in;
  792. tor_inet_aton("18.244.0.188",&in);
  793. tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
  794. test_streq(tmpbuf, "18.244.0.188");
  795. }
  796. /* XXXX test older functions. */
  797. smartlist_free(sl);
  798. }
  799. static void
  800. test_gzip(void)
  801. {
  802. char *buf1, *buf2=NULL, *buf3=NULL;
  803. size_t len1, len2;
  804. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
  805. test_eq(detect_compression_method(buf1, strlen(buf1)), 0);
  806. if (is_gzip_supported()) {
  807. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  808. GZIP_METHOD));
  809. test_assert(buf2);
  810. test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
  811. test_eq(detect_compression_method(buf2, len1), GZIP_METHOD);
  812. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  813. GZIP_METHOD, 1));
  814. test_assert(buf3);
  815. test_streq(buf1,buf3);
  816. tor_free(buf2);
  817. tor_free(buf3);
  818. }
  819. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  820. ZLIB_METHOD));
  821. test_assert(buf2);
  822. test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
  823. test_eq(detect_compression_method(buf2, len1), ZLIB_METHOD);
  824. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, ZLIB_METHOD, 1));
  825. test_assert(buf3);
  826. test_streq(buf1,buf3);
  827. /* Check whether we can uncompress concatenated, compresed strings. */
  828. tor_free(buf3);
  829. buf2 = tor_realloc(buf2, len1*2);
  830. memcpy(buf2+len1, buf2, len1);
  831. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
  832. ZLIB_METHOD, 1));
  833. test_eq(len2, (strlen(buf1)+1)*2);
  834. test_memeq(buf3,
  835. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
  836. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
  837. (strlen(buf1)+1)*2);
  838. tor_free(buf1);
  839. tor_free(buf2);
  840. tor_free(buf3);
  841. /* Check whether we can uncompress partial strings. */
  842. buf1 =
  843. tor_strdup("String with low redundancy that won't be compressed much.");
  844. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  845. ZLIB_METHOD));
  846. tor_assert(len1>16);
  847. /* when we allow an uncomplete string, we should succeed.*/
  848. tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  849. ZLIB_METHOD, 0));
  850. buf3[len2]='\0';
  851. tor_assert(len2 > 5);
  852. tor_assert(!strcmpstart(buf1, buf3));
  853. /* when we demand a complete string, this must fail. */
  854. tor_free(buf3);
  855. tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16, ZLIB_METHOD, 1));
  856. tor_assert(!buf3);
  857. tor_free(buf2);
  858. tor_free(buf3);
  859. tor_free(buf1);
  860. }
  861. static void
  862. test_strmap(void)
  863. {
  864. strmap_t *map;
  865. // strmap_iter_t *iter;
  866. // const char *k;
  867. void *v;
  868. map = strmap_new();
  869. v = strmap_set(map, "K1", (void*)99);
  870. test_eq(v, NULL);
  871. v = strmap_set(map, "K2", (void*)101);
  872. test_eq(v, NULL);
  873. v = strmap_set(map, "K1", (void*)100);
  874. test_eq(v, (void*)99);
  875. test_eq_ptr(strmap_get(map,"K1"), (void*)100);
  876. test_eq_ptr(strmap_get(map,"K2"), (void*)101);
  877. test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
  878. v = strmap_remove(map,"K2");
  879. test_eq_ptr(v, (void*)101);
  880. test_eq_ptr(strmap_get(map,"K2"), NULL);
  881. test_eq_ptr(strmap_remove(map,"K2"), NULL);
  882. strmap_set(map, "K2", (void*)101);
  883. strmap_set(map, "K3", (void*)102);
  884. strmap_set(map, "K4", (void*)103);
  885. strmap_set(map, "K5", (void*)104);
  886. strmap_set(map, "K6", (void*)105);
  887. #if 0
  888. iter = strmap_iter_init(map);
  889. strmap_iter_get(iter,&k,&v);
  890. test_streq(k, "K1");
  891. test_eq(v, (void*)10000);
  892. iter = strmap_iter_next(map,iter);
  893. strmap_iter_get(iter,&k,&v);
  894. test_streq(k, "K2");
  895. test_eq(v, (void*)10201);
  896. iter = strmap_iter_next_rmv(map,iter);
  897. strmap_iter_get(iter,&k,&v);
  898. test_streq(k, "K3");
  899. test_eq(v, (void*)10404);
  900. iter = strmap_iter_next(map,iter); /* K5 */
  901. test_assert(!strmap_iter_done(iter));
  902. iter = strmap_iter_next(map,iter); /* K6 */
  903. test_assert(!strmap_iter_done(iter));
  904. iter = strmap_iter_next(map,iter); /* done */
  905. test_assert(strmap_iter_done(iter));
  906. /* Make sure we removed K2, but not the others. */
  907. test_eq_ptr(strmap_get(map, "K2"), NULL);
  908. test_eq_ptr(strmap_get(map, "K5"), (void*)10816);
  909. #endif
  910. /* Clean up after ourselves. */
  911. strmap_free(map, NULL);
  912. /* Now try some lc functions. */
  913. map = strmap_new();
  914. strmap_set_lc(map,"Ab.C", (void*)1);
  915. test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
  916. test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
  917. test_eq_ptr(strmap_get(map,"AB.C"), NULL);
  918. test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
  919. test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
  920. strmap_free(map,NULL);
  921. }
  922. static void
  923. test_control_formats(void)
  924. {
  925. char *out;
  926. const char *inp =
  927. "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
  928. size_t sz;
  929. sz = read_escaped_data(inp, strlen(inp), 1, &out);
  930. test_streq(out,
  931. ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
  932. tor_free(out);
  933. }
  934. static void
  935. test_onion(void)
  936. {
  937. #if 0
  938. char **names;
  939. int i,num;
  940. names = parse_nickname_list(" foo bar\t baz quux ", &num);
  941. test_eq(num,4);
  942. test_streq(names[0],"foo");
  943. test_streq(names[1],"bar");
  944. test_streq(names[2],"baz");
  945. test_streq(names[3],"quux");
  946. for (i=0;i<num;i++)
  947. tor_free(names[i]);
  948. tor_free(names);
  949. #endif
  950. }
  951. static void
  952. test_onion_handshake(void)
  953. {
  954. /* client-side */
  955. crypto_dh_env_t *c_dh = NULL;
  956. char c_buf[ONIONSKIN_CHALLENGE_LEN];
  957. char c_keys[40];
  958. /* server-side */
  959. char s_buf[ONIONSKIN_REPLY_LEN];
  960. char s_keys[40];
  961. /* shared */
  962. crypto_pk_env_t *pk = NULL;
  963. pk = crypto_new_pk_env();
  964. test_assert(! crypto_pk_generate_key(pk));
  965. /* client handshake 1. */
  966. memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
  967. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  968. /* server handshake */
  969. memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
  970. memset(s_keys, 0, 40);
  971. test_assert(! onion_skin_server_handshake(c_buf, pk, NULL,
  972. s_buf, s_keys, 40));
  973. /* client handshake 2 */
  974. memset(c_keys, 0, 40);
  975. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  976. crypto_dh_free(c_dh);
  977. if (memcmp(c_keys, s_keys, 40)) {
  978. puts("Aiiiie");
  979. exit(1);
  980. }
  981. test_memeq(c_keys, s_keys, 40);
  982. memset(s_buf, 0, 40);
  983. test_memneq(c_keys, s_buf, 40);
  984. crypto_free_pk_env(pk);
  985. }
  986. static void
  987. test_dir_format(void)
  988. {
  989. char buf[8192], buf2[8192];
  990. char platform[256];
  991. char fingerprint[FINGERPRINT_LEN+1];
  992. char d[DIGEST_LEN];
  993. char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
  994. size_t pk1_str_len, pk2_str_len, pk3_str_len;
  995. routerinfo_t r1, r2;
  996. crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
  997. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  998. addr_policy_t ex1, ex2;
  999. routerlist_t *dir1 = NULL, *dir2 = NULL;
  1000. tor_version_t ver1;
  1001. char *bw_lines = NULL;
  1002. const char *m;
  1003. test_assert( (pk1 = crypto_new_pk_env()) );
  1004. test_assert( (pk2 = crypto_new_pk_env()) );
  1005. test_assert( (pk3 = crypto_new_pk_env()) );
  1006. test_assert(! crypto_pk_generate_key(pk1));
  1007. test_assert(! crypto_pk_generate_key(pk2));
  1008. test_assert(! crypto_pk_generate_key(pk3));
  1009. test_assert( is_legal_nickname("a"));
  1010. test_assert(!is_legal_nickname(""));
  1011. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  1012. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  1013. test_assert(!is_legal_nickname("hyphen-")); /* bad char */
  1014. test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  1015. test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1016. /* valid */
  1017. test_assert( is_legal_nickname_or_hexdigest(
  1018. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1019. /* too short */
  1020. test_assert(!is_legal_nickname_or_hexdigest(
  1021. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1022. /* illegal char */
  1023. test_assert(!is_legal_nickname_or_hexdigest(
  1024. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1025. test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  1026. test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  1027. test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  1028. get_platform_str(platform, sizeof(platform));
  1029. memset(&r1,0,sizeof(r1));
  1030. memset(&r2,0,sizeof(r2));
  1031. r1.address = tor_strdup("18.244.0.1");
  1032. r1.addr = 0xc0a80001u; /* 192.168.0.1 */
  1033. r1.cache_info.published_on = 0;
  1034. r1.or_port = 9000;
  1035. r1.dir_port = 9003;
  1036. r1.onion_pkey = pk1;
  1037. r1.identity_pkey = pk2;
  1038. r1.bandwidthrate = 1000;
  1039. r1.bandwidthburst = 5000;
  1040. r1.bandwidthcapacity = 10000;
  1041. r1.exit_policy = NULL;
  1042. r1.nickname = tor_strdup("Magri");
  1043. r1.platform = tor_strdup(platform);
  1044. ex1.policy_type = ADDR_POLICY_ACCEPT;
  1045. ex1.string = NULL;
  1046. ex1.addr = 0;
  1047. ex1.msk = 0;
  1048. ex1.prt_min = ex1.prt_max = 80;
  1049. ex1.next = &ex2;
  1050. ex2.policy_type = ADDR_POLICY_REJECT;
  1051. ex2.addr = 18 << 24;
  1052. ex2.msk = 0xFF000000u;
  1053. ex2.prt_min = ex2.prt_max = 24;
  1054. ex2.next = NULL;
  1055. r2.address = tor_strdup("1.1.1.1");
  1056. r2.addr = 0x0a030201u; /* 10.3.2.1 */
  1057. r2.platform = tor_strdup(platform);
  1058. r2.cache_info.published_on = 5;
  1059. r2.or_port = 9005;
  1060. r2.dir_port = 0;
  1061. r2.onion_pkey = pk2;
  1062. r2.identity_pkey = pk1;
  1063. r2.bandwidthrate = r2.bandwidthburst = r2.bandwidthcapacity = 3000;
  1064. r2.exit_policy = &ex1;
  1065. r2.nickname = tor_strdup("Fred");
  1066. bw_lines = rep_hist_get_bandwidth_lines();
  1067. test_assert(bw_lines);
  1068. test_assert(!strcmpstart(bw_lines, "opt write-history "));
  1069. test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  1070. &pk1_str_len));
  1071. test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  1072. &pk2_str_len));
  1073. test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
  1074. &pk3_str_len));
  1075. memset(buf, 0, 2048);
  1076. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1077. strcpy(buf2, "router Magri 18.244.0.1 9000 0 0\n"
  1078. "platform Tor "VERSION" on ");
  1079. strcat(buf2, get_uname());
  1080. strcat(buf2, "\n"
  1081. "published 1970-01-01 00:00:00\n"
  1082. "opt fingerprint ");
  1083. test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  1084. strcat(buf2, fingerprint);
  1085. strcat(buf2, "\nuptime 0\n"
  1086. /* XXX the "0" above is hardcoded, but even if we made it reflect
  1087. * uptime, that still wouldn't make it right, because the two
  1088. * descriptors might be made on different seconds... hm. */
  1089. "bandwidth 1000 5000 10000\n"
  1090. "onion-key\n");
  1091. strcat(buf2, pk1_str);
  1092. strcat(buf2, "signing-key\n");
  1093. strcat(buf2, pk2_str);
  1094. strcat(buf2, bw_lines);
  1095. strcat(buf2, "router-signature\n");
  1096. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  1097. * twice */
  1098. test_streq(buf, buf2);
  1099. tor_free(bw_lines);
  1100. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1101. cp = buf;
  1102. rp1 = router_parse_entry_from_string((const char*)cp,NULL);
  1103. test_assert(rp1);
  1104. test_streq(rp1->address, r1.address);
  1105. test_eq(rp1->or_port, r1.or_port);
  1106. //test_eq(rp1->dir_port, r1.dir_port);
  1107. test_eq(rp1->bandwidthrate, r1.bandwidthrate);
  1108. test_eq(rp1->bandwidthburst, r1.bandwidthburst);
  1109. test_eq(rp1->bandwidthcapacity, r1.bandwidthcapacity);
  1110. test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  1111. test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  1112. test_assert(rp1->exit_policy == NULL);
  1113. #if 0
  1114. /* XXX Once we have exit policies, test this again. XXX */
  1115. strcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n");
  1116. strcat(buf2, pk2_str);
  1117. strcat(buf2, "signing-key\n");
  1118. strcat(buf2, pk1_str);
  1119. strcat(buf2, "accept *:80\nreject 18.*:24\n\n");
  1120. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
  1121. test_streq(buf, buf2);
  1122. cp = buf;
  1123. rp2 = router_parse_entry_from_string(&cp);
  1124. test_assert(rp2);
  1125. test_streq(rp2->address, r2.address);
  1126. test_eq(rp2->or_port, r2.or_port);
  1127. test_eq(rp2->dir_port, r2.dir_port);
  1128. test_eq(rp2->bandwidth, r2.bandwidth);
  1129. test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  1130. test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  1131. test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
  1132. test_streq(rp2->exit_policy->string, "accept *:80");
  1133. test_streq(rp2->exit_policy->address, "*");
  1134. test_streq(rp2->exit_policy->port, "80");
  1135. test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
  1136. test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
  1137. test_streq(rp2->exit_policy->next->address, "18.*");
  1138. test_streq(rp2->exit_policy->next->port, "24");
  1139. test_assert(rp2->exit_policy->next->next == NULL);
  1140. #endif
  1141. /* Okay, now for the directories. */
  1142. {
  1143. extern smartlist_t *fingerprint_list;
  1144. fingerprint_list = smartlist_create();
  1145. crypto_pk_get_fingerprint(pk2, buf, 1);
  1146. add_fingerprint_to_dir("Magri", buf, fingerprint_list);
  1147. crypto_pk_get_fingerprint(pk1, buf, 1);
  1148. add_fingerprint_to_dir("Fred", buf, fingerprint_list);
  1149. }
  1150. /* Make sure routers aren't too far in the past any more. */
  1151. r1.cache_info.published_on = time(NULL);
  1152. r2.cache_info.published_on = time(NULL)-3*60*60;
  1153. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1154. test_eq(dirserv_add_descriptor(buf,&m), 2);
  1155. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk1)>0);
  1156. test_eq(dirserv_add_descriptor(buf,&m), 2);
  1157. get_options()->Nickname = tor_strdup("DirServer");
  1158. test_assert(!dirserv_dump_directory_to_string(&cp,pk3));
  1159. crypto_pk_get_digest(pk3, d);
  1160. #if 0
  1161. /* XXXX NM re-enable. 011 */
  1162. test_assert(!router_parse_directory(cp));
  1163. test_eq(2, smartlist_len(dir1->routers));
  1164. #endif
  1165. dirserv_free_fingerprint_list();
  1166. tor_free(cp);
  1167. tor_free(pk1_str);
  1168. tor_free(pk2_str);
  1169. if (pk1) crypto_free_pk_env(pk1);
  1170. if (pk2) crypto_free_pk_env(pk2);
  1171. if (rp1) routerinfo_free(rp1);
  1172. if (rp2) routerinfo_free(rp2);
  1173. tor_free(dir1); /* XXXX And more !*/
  1174. tor_free(dir2); /* And more !*/
  1175. /* Try out version parsing functionality */
  1176. test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
  1177. test_eq(0, ver1.major);
  1178. test_eq(3, ver1.minor);
  1179. test_eq(4, ver1.micro);
  1180. test_eq(VER_PRE, ver1.status);
  1181. test_eq(2, ver1.patchlevel);
  1182. test_eq(IS_CVS, ver1.cvs);
  1183. test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
  1184. test_eq(0, ver1.major);
  1185. test_eq(3, ver1.minor);
  1186. test_eq(4, ver1.micro);
  1187. test_eq(VER_RC, ver1.status);
  1188. test_eq(1, ver1.patchlevel);
  1189. test_eq(IS_NOT_CVS, ver1.cvs);
  1190. test_eq(0, tor_version_parse("1.3.4", &ver1));
  1191. test_eq(1, ver1.major);
  1192. test_eq(3, ver1.minor);
  1193. test_eq(4, ver1.micro);
  1194. test_eq(VER_RELEASE, ver1.status);
  1195. test_eq(0, ver1.patchlevel);
  1196. test_eq(IS_NOT_CVS, ver1.cvs);
  1197. test_eq(0, tor_version_parse("1.3.4.999", &ver1));
  1198. test_eq(1, ver1.major);
  1199. test_eq(3, ver1.minor);
  1200. test_eq(4, ver1.micro);
  1201. test_eq(VER_RELEASE, ver1.status);
  1202. test_eq(999, ver1.patchlevel);
  1203. test_eq(IS_NOT_CVS, ver1.cvs);
  1204. test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
  1205. test_eq(0, ver1.major);
  1206. test_eq(1, ver1.minor);
  1207. test_eq(2, ver1.micro);
  1208. test_eq(4, ver1.patchlevel);
  1209. test_eq(VER_RELEASE, ver1.status);
  1210. test_eq(IS_NOT_CVS, ver1.cvs);
  1211. test_streq("alpha", ver1.status_tag);
  1212. test_eq(0, tor_version_parse("0.1.2.4", &ver1));
  1213. test_eq(0, ver1.major);
  1214. test_eq(1, ver1.minor);
  1215. test_eq(2, ver1.micro);
  1216. test_eq(4, ver1.patchlevel);
  1217. test_eq(VER_RELEASE, ver1.status);
  1218. test_eq(IS_NOT_CVS, ver1.cvs);
  1219. test_streq("", ver1.status_tag);
  1220. #define test_eq_vs(vs1, vs2) test_eq_type(version_status_t, "%d", (vs1), (vs2))
  1221. #define test_v_i_o(val, ver, lst) \
  1222. test_eq_vs(val, tor_version_is_obsolete(ver, lst))
  1223. /* make sure tor_version_is_obsolete() works */
  1224. test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
  1225. test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
  1226. test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
  1227. test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
  1228. test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
  1229. test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
  1230. test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
  1231. test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
  1232. test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
  1233. test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
  1234. test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5");
  1235. /* Not on list, but newer than any in same series. */
  1236. test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
  1237. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1238. /* Series newer than any on list. */
  1239. 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");
  1240. /* Series older than any on list. */
  1241. 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");
  1242. /* Not on list, not newer than any on same series. */
  1243. test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
  1244. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1245. /* On list, not newer than any on same series. */
  1246. test_v_i_o(VS_UNRECOMMENDED,
  1247. "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1248. test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  1249. test_eq(1, tor_version_as_new_as(
  1250. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  1251. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
  1252. "0.0.8rc2"));
  1253. test_eq(0, tor_version_as_new_as(
  1254. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  1255. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
  1256. }
  1257. static void
  1258. test_exit_policies(void)
  1259. {
  1260. addr_policy_t *policy;
  1261. policy = router_parse_addr_policy_from_string("reject 192.168.0.0/16:*",-1);
  1262. test_eq(NULL, policy->next);
  1263. test_eq(ADDR_POLICY_REJECT, policy->policy_type);
  1264. test_eq(0xc0a80000u, policy->addr);
  1265. test_eq(0xffff0000u, policy->msk);
  1266. test_eq(1, policy->prt_min);
  1267. test_eq(65535, policy->prt_max);
  1268. test_streq("reject 192.168.0.0/16:*", policy->string);
  1269. test_assert(exit_policy_implicitly_allows_local_networks(policy, 0));
  1270. test_assert(ADDR_POLICY_ACCEPTED ==
  1271. router_compare_addr_to_addr_policy(0x01020304u, 2, policy));
  1272. test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  1273. router_compare_addr_to_addr_policy(0, 2, policy));
  1274. test_assert(ADDR_POLICY_REJECTED ==
  1275. router_compare_addr_to_addr_policy(0xc0a80102, 2, policy));
  1276. addr_policy_free(policy);
  1277. /* Copied from router.c */
  1278. policy = NULL;
  1279. options_append_default_exit_policy(&policy);
  1280. test_assert(policy);
  1281. test_assert(!exit_policy_implicitly_allows_local_networks(policy, 1));
  1282. addr_policy_free(policy);
  1283. }
  1284. static void
  1285. test_rend_fns(void)
  1286. {
  1287. char address1[] = "fooaddress.onion";
  1288. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  1289. char address3[] = "fooaddress.exit";
  1290. char address4[] = "tor.eff.org";
  1291. rend_service_descriptor_t *d1, *d2;
  1292. char *encoded;
  1293. size_t len;
  1294. crypto_pk_env_t *pk1, *pk2;
  1295. time_t now;
  1296. pk1 = crypto_new_pk_env();
  1297. pk2 = crypto_new_pk_env();
  1298. test_assert(!crypto_pk_generate_key(pk1));
  1299. test_assert(!crypto_pk_generate_key(pk2));
  1300. /* Test unversioned descriptor */
  1301. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  1302. d1->pk = crypto_pk_dup_key(pk1);
  1303. now = time(NULL);
  1304. d1->timestamp = now;
  1305. d1->n_intro_points = 3;
  1306. d1->version = 0;
  1307. d1->intro_points = tor_malloc(sizeof(char*)*3);
  1308. d1->intro_points[0] = tor_strdup("tom");
  1309. d1->intro_points[1] = tor_strdup("crow");
  1310. d1->intro_points[2] = tor_strdup("joel");
  1311. test_assert(! rend_encode_service_descriptor(d1, 0, pk1, &encoded, &len));
  1312. d2 = rend_parse_service_descriptor(encoded, len);
  1313. test_assert(d2);
  1314. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  1315. test_eq(d2->timestamp, now);
  1316. test_eq(d2->version, 0);
  1317. test_eq(d2->protocols, 1);
  1318. test_eq(d2->n_intro_points, 3);
  1319. test_streq(d2->intro_points[0], "tom");
  1320. test_streq(d2->intro_points[1], "crow");
  1321. test_streq(d2->intro_points[2], "joel");
  1322. test_eq(NULL, d2->intro_point_extend_info);
  1323. rend_service_descriptor_free(d1);
  1324. rend_service_descriptor_free(d2);
  1325. tor_free(encoded);
  1326. /* Test versioned descriptor. */
  1327. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  1328. d1->pk = crypto_pk_dup_key(pk1);
  1329. now = time(NULL);
  1330. d1->timestamp = now;
  1331. d1->n_intro_points = 2;
  1332. d1->version = 1;
  1333. d1->protocols = 60;
  1334. d1->intro_points = tor_malloc(sizeof(char*)*2);
  1335. d1->intro_point_extend_info = tor_malloc(sizeof(extend_info_t*)*2);
  1336. d1->intro_points[0] = tor_strdup("tom");
  1337. d1->intro_points[1] = tor_strdup("crow");
  1338. d1->intro_point_extend_info[0] = tor_malloc_zero(sizeof(extend_info_t));
  1339. strcpy(d1->intro_point_extend_info[0]->nickname, "tom");
  1340. d1->intro_point_extend_info[0]->addr = 1234;
  1341. d1->intro_point_extend_info[0]->port = 4567;
  1342. d1->intro_point_extend_info[0]->onion_key = crypto_pk_dup_key(pk1);
  1343. memset(d1->intro_point_extend_info[0]->identity_digest, 'a', DIGEST_LEN);
  1344. d1->intro_point_extend_info[1] = tor_malloc_zero(sizeof(extend_info_t));
  1345. strcpy(d1->intro_point_extend_info[1]->nickname, "crow");
  1346. d1->intro_point_extend_info[1]->addr = 6060842;
  1347. d1->intro_point_extend_info[1]->port = 8000;
  1348. d1->intro_point_extend_info[1]->onion_key = crypto_pk_dup_key(pk2);
  1349. memset(d1->intro_point_extend_info[1]->identity_digest, 'b', DIGEST_LEN);
  1350. test_assert(! rend_encode_service_descriptor(d1, 1, pk1, &encoded, &len));
  1351. d2 = rend_parse_service_descriptor(encoded, len);
  1352. test_assert(d2);
  1353. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  1354. test_eq(d2->timestamp, now);
  1355. test_eq(d2->version, 1);
  1356. test_eq(d2->protocols, 60);
  1357. test_eq(d2->n_intro_points, 2);
  1358. test_streq(d2->intro_points[0], d2->intro_point_extend_info[0]->nickname);
  1359. test_streq(d2->intro_points[1], d2->intro_point_extend_info[1]->nickname);
  1360. test_eq(d2->intro_point_extend_info[0]->addr, 1234);
  1361. test_eq(d2->intro_point_extend_info[0]->port, 4567);
  1362. test_assert(!crypto_pk_cmp_keys(pk1,
  1363. d2->intro_point_extend_info[0]->onion_key));
  1364. test_memeq(d2->intro_point_extend_info[0]->identity_digest,
  1365. d1->intro_point_extend_info[0]->identity_digest, DIGEST_LEN);
  1366. test_eq(d2->intro_point_extend_info[1]->addr, 6060842);
  1367. test_eq(d2->intro_point_extend_info[1]->port, 8000);
  1368. test_memeq(d2->intro_point_extend_info[1]->identity_digest,
  1369. d1->intro_point_extend_info[1]->identity_digest, DIGEST_LEN);
  1370. test_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
  1371. test_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
  1372. test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
  1373. test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
  1374. rend_service_descriptor_free(d1);
  1375. rend_service_descriptor_free(d2);
  1376. crypto_free_pk_env(pk1);
  1377. crypto_free_pk_env(pk2);
  1378. }
  1379. static void
  1380. bench_aes(void)
  1381. {
  1382. int len, i;
  1383. char *b1, *b2;
  1384. crypto_cipher_env_t *c;
  1385. struct timeval start, end;
  1386. const int iters = 100000;
  1387. uint64_t nsec;
  1388. c = crypto_new_cipher_env();
  1389. crypto_cipher_generate_key(c);
  1390. crypto_cipher_encrypt_init_cipher(c);
  1391. for (len = 1; len <= 8192; len *= 2) {
  1392. b1 = tor_malloc_zero(len);
  1393. b2 = tor_malloc_zero(len);
  1394. tor_gettimeofday(&start);
  1395. for (i = 0; i < iters; ++i) {
  1396. crypto_cipher_encrypt(c, b1, b2, len);
  1397. }
  1398. tor_gettimeofday(&end);
  1399. tor_free(b1);
  1400. tor_free(b2);
  1401. nsec = (uint64_t) tv_udiff(&start,&end);
  1402. nsec *= 1000;
  1403. nsec /= (iters*len);
  1404. printf("%d bytes: "U64_FORMAT" nsec per byte\n", len,
  1405. U64_PRINTF_ARG(nsec));
  1406. }
  1407. crypto_free_cipher_env(c);
  1408. }
  1409. int
  1410. main(int c, char**v)
  1411. {
  1412. or_options_t *options = options_new();
  1413. options->command = CMD_RUN_UNITTESTS;
  1414. network_init();
  1415. setup_directory();
  1416. options_init(options);
  1417. options->DataDirectory = tor_strdup(temp_dir);
  1418. set_options(options);
  1419. crypto_seed_rng();
  1420. if (0) {
  1421. bench_aes();
  1422. return 0;
  1423. }
  1424. rep_hist_init();
  1425. atexit(remove_directory);
  1426. printf("Running Tor unit tests on %s\n", get_uname());
  1427. puts("========================== Buffers =========================");
  1428. test_buffers();
  1429. puts("\n========================== Crypto ==========================");
  1430. // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
  1431. test_crypto();
  1432. test_crypto_dh();
  1433. test_crypto_s2k();
  1434. puts("\n========================= Util ============================");
  1435. test_gzip();
  1436. test_util();
  1437. test_strmap();
  1438. test_control_formats();
  1439. puts("\n========================= Onion Skins =====================");
  1440. test_onion();
  1441. test_onion_handshake();
  1442. puts("\n========================= Directory Formats ===============");
  1443. test_dir_format();
  1444. puts("\n========================= Exit policies ===================");
  1445. test_exit_policies();
  1446. puts("\n========================= Rendezvous functionality ========");
  1447. test_rend_fns();
  1448. puts("");
  1449. if (have_failed)
  1450. return 1;
  1451. else
  1452. return 0;
  1453. }