test.c 50 KB

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