test.c 44 KB

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