test.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /* Copyright 2001,2002,2003 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include <stdio.h>
  5. #ifdef HAVE_FCNTL_H
  6. #include <fcntl.h>
  7. #endif
  8. #ifdef MS_WINDOWS
  9. /* For mkdir() */
  10. #include <direct.h>
  11. #endif
  12. #include <dirent.h>
  13. #include "or.h"
  14. #include "../common/test.h"
  15. #include "../common/torgzip.h"
  16. extern or_options_t options;
  17. int have_failed = 0;
  18. /* These functions are file-local, but are exposed so we can test. */
  19. void add_fingerprint_to_dir(const char *nickname, const char *fp);
  20. void get_platform_str(char *platform, int len);
  21. void
  22. dump_hex(char *s, int len)
  23. {
  24. static const char TABLE[] = "0123456789ABCDEF";
  25. unsigned char *d = s;
  26. int i, j, nyb;
  27. for(i=0;i<len;++i) {
  28. for (j=1;j>=0;--j) {
  29. nyb = (((int) d[i]) >> (j*4)) & 0x0f;
  30. tor_assert(0<=nyb && nyb <=15);
  31. putchar(TABLE[nyb]);
  32. }
  33. }
  34. }
  35. static char temp_dir[256];
  36. void
  37. setup_directory()
  38. {
  39. static int is_setup = 0;
  40. int r;
  41. if (is_setup) return;
  42. sprintf(temp_dir, "/tmp/tor_test_%d", (int) getpid());
  43. #ifdef _MSC_VER
  44. r = mkdir(temp_dir);
  45. #else
  46. r = mkdir(temp_dir, 0700);
  47. #endif
  48. if (r) {
  49. fprintf(stderr, "Can't create directory %s:", temp_dir);
  50. perror("");
  51. exit(1);
  52. }
  53. is_setup = 1;
  54. }
  55. const char *
  56. get_fname(const char *name)
  57. {
  58. static char buf[1024];
  59. setup_directory();
  60. sprintf(buf,"%s/%s",temp_dir,name);
  61. return buf;
  62. }
  63. void
  64. remove_directory()
  65. {
  66. DIR *dirp;
  67. struct dirent *de;
  68. setup_directory();
  69. if (!(dirp = opendir(temp_dir))) {
  70. perror("Can't open temporary directory to remove files");
  71. return;
  72. }
  73. while ((de = readdir(dirp)) != NULL) {
  74. /* Only "." and ".." start with ., since we don't create any dotfiles. */
  75. if (de->d_name[0] == '.') continue;
  76. if (unlink(get_fname(de->d_name))) {
  77. perror("Error removing file");
  78. }
  79. #if 0
  80. printf("==%s\n", de->d_name);
  81. #endif
  82. }
  83. closedir(dirp);
  84. rmdir(temp_dir);
  85. }
  86. void
  87. test_buffers() {
  88. #define MAX_BUF_SIZE 1024*1024
  89. char str[256];
  90. char str2[256];
  91. buf_t *buf;
  92. buf_t *buf2;
  93. int s, i, j, eof;
  94. /****
  95. * buf_new
  96. ****/
  97. if (!(buf = buf_new()))
  98. test_fail();
  99. test_eq(buf_capacity(buf), 512*1024);
  100. test_eq(buf_datalen(buf), 0);
  101. /****
  102. * read_to_buf
  103. ****/
  104. s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
  105. for (j=0;j<256;++j) {
  106. str[j] = (char)j;
  107. }
  108. write(s, str, 256);
  109. close(s);
  110. s = open(get_fname("data"), O_RDONLY, 0);
  111. eof = 0;
  112. i = read_to_buf(s, 10, buf, &eof);
  113. test_eq(buf_capacity(buf), 512*1024);
  114. test_eq(buf_datalen(buf), 10);
  115. test_eq(eof, 0);
  116. test_eq(i, 10);
  117. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
  118. /* Test reading 0 bytes. */
  119. i = read_to_buf(s, 0, buf, &eof);
  120. test_eq(buf_capacity(buf), 512*1024);
  121. test_eq(buf_datalen(buf), 10);
  122. test_eq(eof, 0);
  123. test_eq(i, 0);
  124. /* Now test when buffer is filled exactly. */
  125. buf2 = buf_new_with_capacity(6);
  126. i = read_to_buf(s, 6, buf2, &eof);
  127. test_eq(buf_capacity(buf2), 6);
  128. test_eq(buf_datalen(buf2), 6);
  129. test_eq(eof, 0);
  130. test_eq(i, 6);
  131. test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
  132. buf_free(buf2);
  133. /* Now test when buffer is filled with more data to read. */
  134. buf2 = buf_new_with_capacity(32);
  135. i = read_to_buf(s, 128, buf2, &eof);
  136. test_eq(buf_capacity(buf2), 128);
  137. test_eq(buf_datalen(buf2), 32);
  138. test_eq(eof, 0);
  139. test_eq(i, 32);
  140. buf_free(buf2);
  141. /* Now read to eof. */
  142. test_assert(buf_capacity(buf) > 256);
  143. i = read_to_buf(s, 1024, buf, &eof);
  144. test_eq(i, (256-32-10-6));
  145. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  146. test_eq(buf_datalen(buf), 256-6-32);
  147. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
  148. test_eq(eof, 0);
  149. i = read_to_buf(s, 1024, buf, &eof);
  150. test_eq(i, 0);
  151. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  152. test_eq(buf_datalen(buf), 256-6-32);
  153. test_eq(eof, 1);
  154. close(s);
  155. /****
  156. * fetch_from_buf
  157. ****/
  158. memset(str2, 255, 256);
  159. test_eq(246, fetch_from_buf(str2, 10, buf));
  160. test_memeq(str2, str, 10);
  161. test_memeq(str+10,(char*)_buf_peek_raw_buffer(buf),246);
  162. test_eq(buf_datalen(buf),246);
  163. test_eq(0, fetch_from_buf(str2, 246, buf));
  164. test_memeq(str2, str+10, 246);
  165. test_eq(buf_capacity(buf),MAX_BUF_SIZE);
  166. test_eq(buf_datalen(buf),0);
  167. /****
  168. * write_to_buf
  169. ****/
  170. memset((char *)_buf_peek_raw_buffer(buf), (int)'-', 256);
  171. i = write_to_buf("Hello world", 11, buf);
  172. test_eq(i, 11);
  173. test_eq(buf_datalen(buf), 11);
  174. test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello world", 11);
  175. i = write_to_buf("XYZZY", 5, buf);
  176. test_eq(i, 16);
  177. test_eq(buf_datalen(buf), 16);
  178. test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello worldXYZZY", 16);
  179. /* Test when buffer is overfull. */
  180. #if 0
  181. buflen = 18;
  182. test_eq(-1, write_to_buf("This string will not fit.", 25,
  183. &buf, &buflen, &buf_datalen));
  184. test_eq(buf_datalen, 16);
  185. test_memeq(buf, "Hello worldXYZZY--", 18);
  186. buflen = MAX_BUF_SIZE;
  187. #endif
  188. /****
  189. * flush_buf
  190. ****/
  191. /* XXXX Needs tests. */
  192. buf_free(buf);
  193. }
  194. void
  195. test_crypto_dh()
  196. {
  197. crypto_dh_env_t *dh1, *dh2;
  198. char p1[DH_BYTES];
  199. char p2[DH_BYTES];
  200. char s1[DH_BYTES];
  201. char s2[DH_BYTES];
  202. int s1len, s2len;
  203. dh1 = crypto_dh_new();
  204. dh2 = crypto_dh_new();
  205. test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
  206. test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
  207. memset(p1, 0, DH_BYTES);
  208. memset(p2, 0, DH_BYTES);
  209. test_memeq(p1, p2, DH_BYTES);
  210. test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  211. test_memneq(p1, p2, DH_BYTES);
  212. test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  213. test_memneq(p1, p2, DH_BYTES);
  214. memset(s1, 0, DH_BYTES);
  215. memset(s2, 0xFF, DH_BYTES);
  216. s1len = crypto_dh_compute_secret(dh1, p2, DH_BYTES, s1, 50);
  217. s2len = crypto_dh_compute_secret(dh2, p1, DH_BYTES, s2, 50);
  218. test_assert(s1len > 0);
  219. test_eq(s1len, s2len);
  220. test_memeq(s1, s2, s1len);
  221. crypto_dh_free(dh1);
  222. crypto_dh_free(dh2);
  223. }
  224. void
  225. test_crypto()
  226. {
  227. crypto_cipher_env_t *env1, *env2;
  228. crypto_pk_env_t *pk1, *pk2;
  229. char *data1, *data2, *data3, *cp;
  230. int i, j, p, len;
  231. data1 = tor_malloc(1024);
  232. data2 = tor_malloc(1024);
  233. data3 = tor_malloc(1024);
  234. test_assert(data1 && data2 && data3);
  235. /* Try out RNG. */
  236. test_assert(! crypto_seed_rng());
  237. crypto_rand(100, data1);
  238. crypto_rand(100, data2);
  239. test_memneq(data1,data2,100);
  240. #if 0
  241. /* Try out identity ciphers. */
  242. env1 = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  243. test_neq(env1, 0);
  244. test_eq(crypto_cipher_generate_key(env1), 0);
  245. test_eq(crypto_cipher_encrypt_init_cipher(env1), 0);
  246. for(i = 0; i < 1024; ++i) {
  247. data1[i] = (char) i*73;
  248. }
  249. crypto_cipher_encrypt(env1, data1, 1024, data2);
  250. test_memeq(data1, data2, 1024);
  251. crypto_free_cipher_env(env1);
  252. #endif
  253. /* Now, test encryption and decryption with stream cipher. */
  254. data1[0]='\0';
  255. for(i = 1023; i>0; i -= 35)
  256. strncat(data1, "Now is the time for all good onions", i);
  257. memset(data2, 0, 1024);
  258. memset(data3, 0, 1024);
  259. env1 = crypto_new_cipher_env();
  260. test_neq(env1, 0);
  261. env2 = crypto_new_cipher_env();
  262. test_neq(env2, 0);
  263. j = crypto_cipher_generate_key(env1);
  264. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  265. crypto_cipher_encrypt_init_cipher(env1);
  266. crypto_cipher_decrypt_init_cipher(env2);
  267. /* Try encrypting 512 chars. */
  268. crypto_cipher_encrypt(env1, data1, 512, data2);
  269. crypto_cipher_decrypt(env2, data2, 512, data3);
  270. test_memeq(data1, data3, 512);
  271. test_memneq(data1, data2, 512);
  272. /* Now encrypt 1 at a time, and get 1 at a time. */
  273. for (j = 512; j < 560; ++j) {
  274. crypto_cipher_encrypt(env1, data1+j, 1, data2+j);
  275. }
  276. for (j = 512; j < 560; ++j) {
  277. crypto_cipher_decrypt(env2, data2+j, 1, data3+j);
  278. }
  279. test_memeq(data1, data3, 560);
  280. /* Now encrypt 3 at a time, and get 5 at a time. */
  281. for (j = 560; j < 1024-5; j += 3) {
  282. crypto_cipher_encrypt(env1, data1+j, 3, data2+j);
  283. }
  284. for (j = 560; j < 1024-5; j += 5) {
  285. crypto_cipher_decrypt(env2, data2+j, 5, data3+j);
  286. }
  287. test_memeq(data1, data3, 1024-5);
  288. /* Now make sure that when we encrypt with different chunk sizes, we get
  289. the same results. */
  290. crypto_free_cipher_env(env2);
  291. memset(data3, 0, 1024);
  292. env2 = crypto_new_cipher_env();
  293. test_neq(env2, 0);
  294. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  295. crypto_cipher_encrypt_init_cipher(env2);
  296. for (j = 0; j < 1024-16; j += 17) {
  297. crypto_cipher_encrypt(env2, data1+j, 17, data3+j);
  298. }
  299. for (j= 0; j < 1024-16; ++j) {
  300. if (data2[j] != data3[j]) {
  301. printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
  302. }
  303. }
  304. test_memeq(data2, data3, 1024-16);
  305. crypto_free_cipher_env(env1);
  306. crypto_free_cipher_env(env2);
  307. /* Test vectors for stream ciphers. */
  308. /* XXXX Look up some test vectors for the ciphers and make sure we match. */
  309. /* Test SHA-1 with a test vector from the specification. */
  310. i = crypto_digest("abc", 3, data1);
  311. test_memeq(data1,
  312. "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
  313. "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
  314. /* Public-key ciphers */
  315. pk1 = crypto_new_pk_env();
  316. pk2 = crypto_new_pk_env();
  317. test_assert(pk1 && pk2);
  318. test_assert(! crypto_pk_generate_key(pk1));
  319. test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &i));
  320. test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, i));
  321. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  322. tor_free(cp);
  323. /* Check DER encoding */
  324. i=crypto_pk_DER64_encode_public_key(pk1, &cp);
  325. test_assert(i>0);
  326. test_assert(cp);
  327. test_assert(!strchr(cp, ' '));
  328. test_assert(!strchr(cp, '\n'));
  329. test_eq(0, crypto_pk_cmp_keys(pk1, pk1));
  330. crypto_free_pk_env(pk2);
  331. pk2 = crypto_pk_DER64_decode_public_key(cp);
  332. test_assert(pk2);
  333. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  334. tor_free(cp);
  335. test_eq(128, crypto_pk_keysize(pk1));
  336. test_eq(128, crypto_pk_keysize(pk2));
  337. test_eq(128, crypto_pk_public_encrypt(pk2, "Hello whirled.", 15, data1,
  338. PK_PKCS1_OAEP_PADDING));
  339. test_eq(128, crypto_pk_public_encrypt(pk1, "Hello whirled.", 15, data2,
  340. PK_PKCS1_OAEP_PADDING));
  341. /* oaep padding should make encryption not match */
  342. test_memneq(data1, data2, 128);
  343. test_eq(15, crypto_pk_private_decrypt(pk1, data1, 128, data3,
  344. PK_PKCS1_OAEP_PADDING,1));
  345. test_streq(data3, "Hello whirled.");
  346. memset(data3, 0, 1024);
  347. test_eq(15, crypto_pk_private_decrypt(pk1, data2, 128, data3,
  348. PK_PKCS1_OAEP_PADDING,1));
  349. test_streq(data3, "Hello whirled.");
  350. /* Can't decrypt with public key. */
  351. test_eq(-1, crypto_pk_private_decrypt(pk2, data2, 128, data3,
  352. PK_PKCS1_OAEP_PADDING,1));
  353. /* Try again with bad padding */
  354. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  355. test_eq(-1, crypto_pk_private_decrypt(pk1, data2, 128, data3,
  356. PK_PKCS1_OAEP_PADDING,1));
  357. /* File operations: save and load private key */
  358. test_assert(! crypto_pk_write_private_key_to_filename(pk1,
  359. get_fname("pkey1")));
  360. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  361. get_fname("pkey1")));
  362. test_eq(15, crypto_pk_private_decrypt(pk2, data1, 128, data3,
  363. PK_PKCS1_OAEP_PADDING,1));
  364. /* Now try signing. */
  365. strcpy(data1, "Ossifrage");
  366. test_eq(128, crypto_pk_private_sign(pk1, data1, 10, data2));
  367. test_eq(10, crypto_pk_public_checksig(pk1, data2, 128, data3));
  368. test_streq(data3, "Ossifrage");
  369. /* Try signing digests. */
  370. test_eq(128, crypto_pk_private_sign_digest(pk1, data1, 10, data2));
  371. test_eq(20, crypto_pk_public_checksig(pk1, data2, 128, data3));
  372. test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  373. test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  374. /*XXXX test failed signing*/
  375. /* Try encoding */
  376. crypto_free_pk_env(pk2);
  377. pk2 = NULL;
  378. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  379. test_assert(i>0);
  380. pk2 = crypto_pk_asn1_decode(data1, i);
  381. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  382. /* Try with hybrid encryption wrappers. */
  383. crypto_rand(1024, data1);
  384. for (i = 0; i < 3; ++i) {
  385. for (j = 85; j < 140; ++j) {
  386. memset(data2,0,1024);
  387. memset(data3,0,1024);
  388. if (i == 0 && j < 129)
  389. continue;
  390. p = (i==0)?PK_NO_PADDING:
  391. (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
  392. len = crypto_pk_public_hybrid_encrypt(pk1,data1,j,data2,p,0);
  393. test_assert(len>=0);
  394. len = crypto_pk_private_hybrid_decrypt(pk1,data2,len,data3,p,1);
  395. test_eq(len,j);
  396. test_memeq(data1,data3,j);
  397. }
  398. }
  399. crypto_free_pk_env(pk1);
  400. crypto_free_pk_env(pk2);
  401. /* Base64 tests */
  402. strcpy(data1, "Test string that contains 35 chars.");
  403. strcat(data1, " 2nd string that contains 35 chars.");
  404. i = base64_encode(data2, 1024, data1, 71);
  405. j = base64_decode(data3, 1024, data2, i);
  406. test_streq(data3, data1);
  407. test_eq(j, 71);
  408. test_assert(data2[i] == '\0');
  409. /* Base32 tests */
  410. strcpy(data1, "5chrs");
  411. /* bit pattern is: [35 63 68 72 73] ->
  412. * [00110101 01100011 01101000 01110010 01110011]
  413. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  414. */
  415. base32_encode(data2, 9, data1, 5);
  416. test_streq(data2, "gvrwq4tt");
  417. strcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4");
  418. base32_encode(data2, 30, data1, 10);
  419. test_streq(data2, "772w2rfobvomsywe");
  420. /* Base16 tests */
  421. strcpy(data1, "6chrs\xff");
  422. base16_encode(data2, 13, data1, 6);
  423. test_streq(data2, "3663687273FF");
  424. strcpy(data1, "f0d678affc000100");
  425. i = base16_decode(data2, 8, data1, 16);
  426. test_eq(i,0);
  427. test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  428. free(data1);
  429. free(data2);
  430. free(data3);
  431. }
  432. void
  433. test_util() {
  434. struct timeval start, end;
  435. struct tm a_time;
  436. smartlist_t *sl;
  437. char timestr[RFC1123_TIME_LEN+1];
  438. char buf[1024];
  439. time_t t_res;
  440. int i;
  441. uint32_t u32;
  442. uint16_t u16;
  443. char *cp;
  444. start.tv_sec = 5;
  445. start.tv_usec = 5000;
  446. end.tv_sec = 5;
  447. end.tv_usec = 5000;
  448. test_eq(0L, tv_udiff(&start, &end));
  449. end.tv_usec = 7000;
  450. test_eq(2000L, tv_udiff(&start, &end));
  451. end.tv_sec = 6;
  452. test_eq(1002000L, tv_udiff(&start, &end));
  453. end.tv_usec = 0;
  454. test_eq(995000L, tv_udiff(&start, &end));
  455. end.tv_sec = 4;
  456. test_eq(0L, tv_udiff(&start, &end));
  457. /* The test values here are confirmed to be correct on a platform
  458. * with a working timegm. */
  459. a_time.tm_year = 2003-1900;
  460. a_time.tm_mon = 7;
  461. a_time.tm_mday = 30;
  462. a_time.tm_hour = 6;
  463. a_time.tm_min = 14;
  464. a_time.tm_sec = 55;
  465. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  466. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  467. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  468. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  469. a_time.tm_mday = 10;
  470. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  471. format_rfc1123_time(timestr, 0);
  472. test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
  473. format_rfc1123_time(timestr, (time_t)1091580502UL);
  474. test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
  475. t_res = 0;
  476. i = parse_rfc1123_time(timestr, &t_res);
  477. test_eq(i,0);
  478. test_eq(t_res, (time_t)1091580502UL);
  479. /* Test smartlist */
  480. sl = smartlist_create();
  481. smartlist_add(sl, (void*)1);
  482. smartlist_add(sl, (void*)2);
  483. smartlist_add(sl, (void*)3);
  484. smartlist_add(sl, (void*)4);
  485. test_eq((void*)2, smartlist_del_keeporder(sl, 1));
  486. smartlist_insert(sl, 1, (void*)22);
  487. smartlist_insert(sl, 0, (void*)0);
  488. smartlist_insert(sl, 5, (void*)555);
  489. test_eq((void*)0, smartlist_get(sl,0));
  490. test_eq((void*)1, smartlist_get(sl,1));
  491. test_eq((void*)22, smartlist_get(sl,2));
  492. test_eq((void*)3, smartlist_get(sl,3));
  493. test_eq((void*)4, smartlist_get(sl,4));
  494. test_eq((void*)555, smartlist_get(sl,5));
  495. smartlist_clear(sl);
  496. smartlist_split_string(sl, "abc", ":", 0, 0);
  497. test_eq(1, smartlist_len(sl));
  498. test_streq("abc", smartlist_get(sl, 0));
  499. smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  500. test_eq(4, smartlist_len(sl));
  501. test_streq("a", smartlist_get(sl, 1));
  502. test_streq("bc", smartlist_get(sl, 2));
  503. test_streq("", smartlist_get(sl, 3));
  504. smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
  505. test_eq(8, smartlist_len(sl));
  506. test_streq("", smartlist_get(sl, 4));
  507. test_streq("def", smartlist_get(sl, 5));
  508. test_streq(" ", smartlist_get(sl, 6));
  509. test_streq("ghijk", smartlist_get(sl, 7));
  510. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  511. smartlist_clear(sl);
  512. smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  513. test_eq(3, smartlist_len(sl));
  514. test_streq("a", smartlist_get(sl,0));
  515. test_streq("bbd", smartlist_get(sl,1));
  516. test_streq("cdef", smartlist_get(sl,2));
  517. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE, 0);
  518. test_eq(8, smartlist_len(sl));
  519. test_streq("z", smartlist_get(sl,3));
  520. test_streq("zhasd", smartlist_get(sl,4));
  521. test_streq("", smartlist_get(sl,5));
  522. test_streq("bnud", smartlist_get(sl,6));
  523. test_streq("", smartlist_get(sl,7));
  524. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  525. smartlist_clear(sl);
  526. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  527. test_eq(3, smartlist_len(sl));
  528. test_streq("z", smartlist_get(sl, 0));
  529. test_streq("zhasd", smartlist_get(sl, 1));
  530. test_streq("bnud", smartlist_get(sl, 2));
  531. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  532. test_eq(5, smartlist_len(sl));
  533. test_streq("z", smartlist_get(sl, 3));
  534. test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
  535. /* Test tor_strstrip() */
  536. strcpy(buf, "Testing 1 2 3");
  537. test_eq(0, tor_strstrip(buf, ",!"));
  538. test_streq(buf, "Testing 1 2 3");
  539. strcpy(buf, "!Testing 1 2 3?");
  540. test_eq(5, tor_strstrip(buf, "!? "));
  541. test_streq(buf, "Testing123");
  542. /* Test tor_strpartition() */
  543. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
  544. TERMINATE_IF_EVEN));
  545. test_streq(buf, "abc##def##g");
  546. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
  547. ALWAYS_TERMINATE));
  548. test_streq(buf, "abc##def##g##");
  549. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
  550. TERMINATE_IF_EVEN));
  551. test_streq(buf, "abc##def##ghi##");
  552. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
  553. NEVER_TERMINATE));
  554. test_streq(buf, "abc##def##ghi");
  555. /* Test parse_addr_port */
  556. cp = NULL; u32 = 3; u16 = 3;
  557. test_assert(!parse_addr_port("1.2.3.4", &cp, &u32, &u16));
  558. test_streq(cp, "1.2.3.4");
  559. test_eq(u32, htonl(0x01020304u));
  560. test_eq(u16, 0);
  561. tor_free(cp);
  562. test_assert(!parse_addr_port("4.3.2.1:99", &cp, &u32, &u16));
  563. test_streq(cp, "4.3.2.1");
  564. test_eq(u32, htonl(0x04030201u));
  565. test_eq(u16, 99);
  566. tor_free(cp);
  567. test_assert(!parse_addr_port("nonexistent.address:4040", &cp, NULL, &u16));
  568. test_streq(cp, "nonexistent.address");
  569. test_eq(u16, 4040);
  570. tor_free(cp);
  571. test_assert(!parse_addr_port("localhost:9999", &cp, &u32, &u16));
  572. test_streq(cp, "localhost");
  573. test_eq(u32, htonl(0x7f000001u));
  574. test_eq(u16, 9999);
  575. tor_free(cp);
  576. u32 = 3;
  577. test_assert(!parse_addr_port("localhost", NULL, &u32, &u16));
  578. test_eq(cp, NULL);
  579. test_eq(u32, htonl(0x7f000001u));
  580. test_eq(u16, 0);
  581. tor_free(cp);
  582. /* Test tor_parse_long. */
  583. test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
  584. test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
  585. /* XXXX test older functions. */
  586. smartlist_free(sl);
  587. }
  588. void
  589. test_gzip() {
  590. char *buf1, *buf2=NULL, *buf3=NULL;
  591. size_t len1, len2;
  592. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
  593. if (is_gzip_supported()) {
  594. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  595. GZIP_METHOD));
  596. test_assert(buf2);
  597. test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
  598. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, GZIP_METHOD));
  599. test_assert(buf3);
  600. test_streq(buf1,buf3);
  601. tor_free(buf2);
  602. tor_free(buf3);
  603. }
  604. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  605. ZLIB_METHOD));
  606. test_assert(buf2);
  607. test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
  608. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, ZLIB_METHOD));
  609. test_assert(buf3);
  610. test_streq(buf1,buf3);
  611. tor_free(buf2);
  612. tor_free(buf3);
  613. tor_free(buf1);
  614. }
  615. static void* _squareAndRemoveK4(const char *key, void*val, void *data)
  616. {
  617. int *ip = (int*)data;
  618. intptr_t v;
  619. if (strcmp(key,"K4") == 0) {
  620. ++(*ip);
  621. return NULL;
  622. }
  623. v = (intptr_t)val;
  624. return (void*)(v*v);
  625. }
  626. void test_strmap() {
  627. strmap_t *map;
  628. strmap_iter_t *iter;
  629. const char *k;
  630. void *v;
  631. int count;
  632. map = strmap_new();
  633. v = strmap_set(map, "K1", (void*)99);
  634. test_eq(v, NULL);
  635. v = strmap_set(map, "K2", (void*)101);
  636. test_eq(v, NULL);
  637. v = strmap_set(map, "K1", (void*)100);
  638. test_eq(v, (void*)99);
  639. test_eq(strmap_get(map,"K1"), (void*)100);
  640. test_eq(strmap_get(map,"K2"), (void*)101);
  641. test_eq(strmap_get(map,"K-not-there"), NULL);
  642. v = strmap_remove(map,"K2");
  643. test_eq(v, (void*)101);
  644. test_eq(strmap_get(map,"K2"), NULL);
  645. test_eq(strmap_remove(map,"K2"), NULL);
  646. strmap_set(map, "K2", (void*)101);
  647. strmap_set(map, "K3", (void*)102);
  648. strmap_set(map, "K4", (void*)103);
  649. strmap_set(map, "K5", (void*)104);
  650. strmap_set(map, "K6", (void*)105);
  651. count = 0;
  652. strmap_foreach(map, _squareAndRemoveK4, &count);
  653. test_eq(count, 1);
  654. test_eq(strmap_get(map, "K4"), NULL);
  655. test_eq(strmap_get(map, "K1"), (void*)10000);
  656. test_eq(strmap_get(map, "K6"), (void*)11025);
  657. iter = strmap_iter_init(map);
  658. strmap_iter_get(iter,&k,&v);
  659. test_streq(k, "K1");
  660. test_eq(v, (void*)10000);
  661. iter = strmap_iter_next(map,iter);
  662. strmap_iter_get(iter,&k,&v);
  663. test_streq(k, "K2");
  664. test_eq(v, (void*)10201);
  665. iter = strmap_iter_next_rmv(map,iter);
  666. strmap_iter_get(iter,&k,&v);
  667. test_streq(k, "K3");
  668. test_eq(v, (void*)10404);
  669. iter = strmap_iter_next(map,iter); /* K5 */
  670. test_assert(!strmap_iter_done(iter));
  671. iter = strmap_iter_next(map,iter); /* K6 */
  672. test_assert(!strmap_iter_done(iter));
  673. iter = strmap_iter_next(map,iter); /* done */
  674. test_assert(strmap_iter_done(iter));
  675. /* Make sure we removed K2, but not the others. */
  676. test_eq(strmap_get(map, "K2"), NULL);
  677. test_eq(strmap_get(map, "K5"), (void*)10816);
  678. /* Clean up after ourselves. */
  679. strmap_free(map, NULL);
  680. /* Now try some lc functions. */
  681. map = strmap_new();
  682. strmap_set_lc(map,"Ab.C", (void*)1);
  683. test_eq(strmap_get(map,"ab.c"), (void*)1);
  684. test_eq(strmap_get_lc(map,"AB.C"), (void*)1);
  685. test_eq(strmap_get(map,"AB.C"), NULL);
  686. test_eq(strmap_remove_lc(map,"aB.C"), (void*)1);
  687. test_eq(strmap_get_lc(map,"AB.C"), NULL);
  688. strmap_free(map,NULL);
  689. }
  690. void test_onion() {
  691. #if 0
  692. char **names;
  693. int i,num;
  694. names = parse_nickname_list(" foo bar\t baz quux ", &num);
  695. test_eq(num,4);
  696. test_streq(names[0],"foo");
  697. test_streq(names[1],"bar");
  698. test_streq(names[2],"baz");
  699. test_streq(names[3],"quux");
  700. for(i=0;i<num;i++)
  701. tor_free(names[i]);
  702. tor_free(names);
  703. #endif
  704. }
  705. void
  706. test_onion_handshake() {
  707. /* client-side */
  708. crypto_dh_env_t *c_dh = NULL;
  709. char c_buf[ONIONSKIN_CHALLENGE_LEN];
  710. char c_keys[40];
  711. /* server-side */
  712. char s_buf[ONIONSKIN_REPLY_LEN];
  713. char s_keys[40];
  714. /* shared */
  715. crypto_pk_env_t *pk = NULL;
  716. pk = crypto_new_pk_env();
  717. test_assert(! crypto_pk_generate_key(pk));
  718. /* client handshake 1. */
  719. memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
  720. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  721. /* server handshake */
  722. memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
  723. memset(s_keys, 0, 40);
  724. test_assert(! onion_skin_server_handshake(c_buf, pk, NULL, s_buf, s_keys, 40));
  725. /* client handshake 2 */
  726. memset(c_keys, 0, 40);
  727. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  728. crypto_dh_free(c_dh);
  729. if (memcmp(c_keys, s_keys, 40)) {
  730. puts("Aiiiie");
  731. exit(1);
  732. }
  733. test_memeq(c_keys, s_keys, 40);
  734. memset(s_buf, 0, 40);
  735. test_memneq(c_keys, s_buf, 40);
  736. crypto_free_pk_env(pk);
  737. }
  738. /* from routerparse.c */
  739. int is_obsolete_version(const char *myversion, const char *start);
  740. void
  741. test_dir_format()
  742. {
  743. char buf[8192], buf2[8192];
  744. char platform[256];
  745. char fingerprint[FINGERPRINT_LEN+1];
  746. char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
  747. int pk1_str_len, pk2_str_len, pk3_str_len;
  748. routerinfo_t r1, r2;
  749. crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
  750. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  751. struct exit_policy_t ex1, ex2;
  752. routerlist_t *dir1 = NULL, *dir2 = NULL;
  753. tor_version_t ver1;
  754. char *bw_lines = NULL;
  755. test_assert( (pk1 = crypto_new_pk_env()) );
  756. test_assert( (pk2 = crypto_new_pk_env()) );
  757. test_assert( (pk3 = crypto_new_pk_env()) );
  758. test_assert(! crypto_pk_generate_key(pk1));
  759. test_assert(! crypto_pk_generate_key(pk2));
  760. test_assert(! crypto_pk_generate_key(pk3));
  761. test_assert( is_legal_nickname("a"));
  762. test_assert(!is_legal_nickname(""));
  763. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  764. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  765. test_assert(!is_legal_nickname("hyphen-")); /* bad char */
  766. test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  767. test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  768. /* valid */
  769. test_assert( is_legal_nickname_or_hexdigest(
  770. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  771. /* too short */
  772. test_assert(!is_legal_nickname_or_hexdigest(
  773. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  774. /* illegal char */
  775. test_assert(!is_legal_nickname_or_hexdigest(
  776. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  777. test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  778. test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  779. test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  780. get_platform_str(platform, sizeof(platform));
  781. r1.address = "testaddr1.foo.bar";
  782. r1.addr = 0xc0a80001u; /* 192.168.0.1 */
  783. r1.published_on = 0;
  784. r1.or_port = 9000;
  785. r1.socks_port = 9002;
  786. r1.dir_port = 9003;
  787. r1.is_trusted_dir = 1;
  788. r1.onion_pkey = pk1;
  789. r1.identity_pkey = pk2;
  790. r1.bandwidthrate = 1000;
  791. r1.bandwidthburst = 5000;
  792. r1.bandwidthcapacity = 10000;
  793. r1.exit_policy = NULL;
  794. r1.nickname = "Magri";
  795. r1.platform = tor_strdup(platform);
  796. ex1.policy_type = EXIT_POLICY_ACCEPT;
  797. ex1.string = NULL;
  798. ex1.addr = 0;
  799. ex1.msk = 0;
  800. ex1.prt_min = ex1.prt_max = 80;
  801. ex1.next = &ex2;
  802. ex2.policy_type = EXIT_POLICY_REJECT;
  803. ex2.addr = 18 << 24;
  804. ex2.msk = 0xFF000000u;
  805. ex2.prt_min = ex2.prt_max = 24;
  806. ex2.next = NULL;
  807. r2.address = "tor.tor.tor";
  808. r2.addr = 0x0a030201u; /* 10.3.2.1 */
  809. r2.platform = tor_strdup(platform);
  810. r2.published_on = 5;
  811. r2.or_port = 9005;
  812. r2.socks_port = 0;
  813. r2.dir_port = 0;
  814. r2.onion_pkey = pk2;
  815. r2.identity_pkey = pk1;
  816. r2.bandwidthrate = r2.bandwidthburst = r2.bandwidthcapacity = 3000;
  817. r2.exit_policy = &ex1;
  818. r2.nickname = "Fred";
  819. bw_lines = rep_hist_get_bandwidth_lines();
  820. test_assert(bw_lines);
  821. test_assert(!strcmpstart(bw_lines, "opt write-history "));
  822. test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  823. &pk1_str_len));
  824. test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  825. &pk2_str_len));
  826. test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
  827. &pk3_str_len));
  828. memset(buf, 0, 2048);
  829. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  830. strcpy(buf2, "router Magri testaddr1.foo.bar 9000 9002 9003\n"
  831. "platform Tor "VERSION" on ");
  832. strcat(buf2, get_uname());
  833. strcat(buf2, "\n"
  834. "published 1970-01-01 00:00:00\n"
  835. "opt fingerprint ");
  836. test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  837. strcat(buf2, fingerprint);
  838. strcat(buf2, "\nopt uptime 0\n"
  839. /* XXX the "0" above is hardcoded, but even if we made it reflect
  840. * uptime, that still wouldn't make it right, because the two
  841. * descriptors might be made on different seconds... hm. */
  842. "bandwidth 1000 5000 10000\n"
  843. "onion-key\n");
  844. strcat(buf2, pk1_str);
  845. strcat(buf2, "signing-key\n");
  846. strcat(buf2, pk2_str);
  847. strcat(buf2, bw_lines);
  848. strcat(buf2, "router-signature\n");
  849. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same twice*/
  850. test_streq(buf, buf2);
  851. tor_free(bw_lines);
  852. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  853. cp = buf;
  854. rp1 = router_parse_entry_from_string((const char*)cp,NULL);
  855. test_assert(rp1);
  856. test_streq(rp1->address, r1.address);
  857. test_eq(rp1->or_port, r1.or_port);
  858. test_eq(rp1->socks_port, r1.socks_port);
  859. test_eq(rp1->dir_port, r1.dir_port);
  860. test_eq(rp1->bandwidthrate, r1.bandwidthrate);
  861. test_eq(rp1->bandwidthburst, r1.bandwidthburst);
  862. test_eq(rp1->bandwidthcapacity, r1.bandwidthcapacity);
  863. test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  864. test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  865. test_assert(rp1->exit_policy == NULL);
  866. #if 0
  867. /* XXX Once we have exit policies, test this again. XXX */
  868. strcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n");
  869. strcat(buf2, pk2_str);
  870. strcat(buf2, "signing-key\n");
  871. strcat(buf2, pk1_str);
  872. strcat(buf2, "accept *:80\nreject 18.*:24\n\n");
  873. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
  874. test_streq(buf, buf2);
  875. cp = buf;
  876. rp2 = router_parse_entry_from_string(&cp);
  877. test_assert(rp2);
  878. test_streq(rp2->address, r2.address);
  879. test_eq(rp2->or_port, r2.or_port);
  880. test_eq(rp2->socks_port, r2.socks_port);
  881. test_eq(rp2->dir_port, r2.dir_port);
  882. test_eq(rp2->bandwidth, r2.bandwidth);
  883. test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  884. test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  885. test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
  886. test_streq(rp2->exit_policy->string, "accept *:80");
  887. test_streq(rp2->exit_policy->address, "*");
  888. test_streq(rp2->exit_policy->port, "80");
  889. test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
  890. test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
  891. test_streq(rp2->exit_policy->next->address, "18.*");
  892. test_streq(rp2->exit_policy->next->port, "24");
  893. test_assert(rp2->exit_policy->next->next == NULL);
  894. #endif
  895. /* Okay, now for the directories. */
  896. crypto_pk_get_fingerprint(pk2, buf, 1);
  897. add_fingerprint_to_dir("Magri", buf);
  898. crypto_pk_get_fingerprint(pk1, buf, 1);
  899. add_fingerprint_to_dir("Fred", buf);
  900. /* Make sure routers aren't too far in the past any more. */
  901. r1.published_on = time(NULL);
  902. r2.published_on = time(NULL)-3*60*60;
  903. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  904. cp = buf;
  905. test_eq(dirserv_add_descriptor((const char**)&cp), 1);
  906. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk1)>0);
  907. cp = buf;
  908. test_eq(dirserv_add_descriptor((const char**)&cp), 1);
  909. options.Nickname = "DirServer";
  910. test_assert(!dirserv_dump_directory_to_string(buf,8192,pk3));
  911. cp = buf;
  912. test_assert(!router_parse_routerlist_from_directory(buf, &dir1, pk3, 1));
  913. test_eq(2, smartlist_len(dir1->routers));
  914. dirserv_free_fingerprint_list();
  915. tor_free(pk1_str);
  916. tor_free(pk2_str);
  917. if (pk1) crypto_free_pk_env(pk1);
  918. if (pk2) crypto_free_pk_env(pk2);
  919. if (rp1) routerinfo_free(rp1);
  920. if (rp2) routerinfo_free(rp2);
  921. tor_free(dir1); /* XXXX And more !*/
  922. tor_free(dir2); /* And more !*/
  923. /* Try out version parsing functionality */
  924. test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
  925. test_eq(0, ver1.major);
  926. test_eq(3, ver1.minor);
  927. test_eq(4, ver1.micro);
  928. test_eq(VER_PRE, ver1.status);
  929. test_eq(2, ver1.patchlevel);
  930. test_eq(IS_CVS, ver1.cvs);
  931. test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
  932. test_eq(0, ver1.major);
  933. test_eq(3, ver1.minor);
  934. test_eq(4, ver1.micro);
  935. test_eq(VER_RC, ver1.status);
  936. test_eq(1, ver1.patchlevel);
  937. test_eq(IS_NOT_CVS, ver1.cvs);
  938. test_eq(0, tor_version_parse("1.3.4", &ver1));
  939. test_eq(1, ver1.major);
  940. test_eq(3, ver1.minor);
  941. test_eq(4, ver1.micro);
  942. test_eq(VER_RELEASE, ver1.status);
  943. test_eq(0, ver1.patchlevel);
  944. test_eq(IS_NOT_CVS, ver1.cvs);
  945. test_eq(0, tor_version_parse("1.3.4.999", &ver1));
  946. test_eq(1, ver1.major);
  947. test_eq(3, ver1.minor);
  948. test_eq(4, ver1.micro);
  949. test_eq(VER_RELEASE, ver1.status);
  950. test_eq(999, ver1.patchlevel);
  951. test_eq(IS_NOT_CVS, ver1.cvs);
  952. /* make sure is_obsolete_version() works */
  953. test_eq(1, is_obsolete_version("0.0.1", "Tor 0.0.2"));
  954. test_eq(1, is_obsolete_version("0.0.1", "0.0.2, Tor 0.0.3"));
  955. test_eq(1, is_obsolete_version("0.0.1", "0.0.2,Tor 0.0.3"));
  956. test_eq(1, is_obsolete_version("0.0.1", "0.0.3,BetterTor 0.0.1"));
  957. test_eq(0, is_obsolete_version("0.0.2", "Tor 0.0.2,Tor 0.0.3"));
  958. test_eq(1, is_obsolete_version("0.0.2", "Tor 0.0.2pre1,Tor 0.0.3"));
  959. test_eq(0, is_obsolete_version("0.1.0", "Tor 0.0.2,Tor 0.0.3"));
  960. test_eq(0, is_obsolete_version("0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8"));
  961. test_eq(0, is_obsolete_version("0.0.5", "0.0.5-cvs"));
  962. test_eq(0, is_obsolete_version("0.0.5.1-cvs", "0.0.5"));
  963. test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  964. test_eq(1, tor_version_as_new_as(
  965. "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"));
  966. test_eq(0, tor_version_as_new_as(
  967. "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"));
  968. }
  969. void test_rend_fns()
  970. {
  971. char address1[] = "fooaddress.onion";
  972. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  973. rend_service_descriptor_t *d1, *d2;
  974. char *encoded;
  975. int len;
  976. crypto_pk_env_t *pk1;
  977. time_t now;
  978. pk1 = crypto_new_pk_env();
  979. test_assert(!crypto_pk_generate_key(pk1));
  980. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  981. d1->pk = pk1;
  982. now = time(NULL);
  983. d1->timestamp = now;
  984. d1->n_intro_points = 3;
  985. d1->intro_points = tor_malloc(sizeof(char*)*3);
  986. d1->intro_points[0] = tor_strdup("tom");
  987. d1->intro_points[1] = tor_strdup("crow");
  988. d1->intro_points[2] = tor_strdup("joel");
  989. test_assert(! rend_encode_service_descriptor(d1, pk1, &encoded, &len));
  990. d2 = rend_parse_service_descriptor(encoded, len);
  991. test_assert(d2);
  992. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  993. test_eq(d2->timestamp, now);
  994. test_eq(d2->n_intro_points, 3);
  995. test_streq(d2->intro_points[0], "tom");
  996. test_streq(d2->intro_points[1], "crow");
  997. test_streq(d2->intro_points[2], "joel");
  998. test_eq(-1, rend_parse_rendezvous_address(address1));
  999. test_eq( 0, rend_parse_rendezvous_address(address2));
  1000. rend_service_descriptor_free(d1);
  1001. rend_service_descriptor_free(d2);
  1002. }
  1003. int
  1004. main(int c, char**v){
  1005. #if 0
  1006. or_options_t options; /* command-line and config-file options */
  1007. if(getconfig(c,v,&options))
  1008. exit(1);
  1009. #endif
  1010. crypto_seed_rng();
  1011. setup_directory();
  1012. rep_hist_init();
  1013. atexit(remove_directory);
  1014. // puts("========================== Buffers =========================");
  1015. // test_buffers();
  1016. puts("\n========================== Crypto ==========================");
  1017. // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
  1018. test_crypto();
  1019. test_crypto_dh();
  1020. puts("\n========================= Util ============================");
  1021. test_gzip();
  1022. test_util();
  1023. test_strmap();
  1024. puts("\n========================= Onion Skins =====================");
  1025. test_onion();
  1026. test_onion_handshake();
  1027. puts("\n========================= Directory Formats ===============");
  1028. test_dir_format();
  1029. puts("\n========================= Rendezvous functionality ========");
  1030. test_rend_fns();
  1031. puts("");
  1032. if (have_failed)
  1033. return 1;
  1034. else
  1035. return 0;
  1036. }
  1037. /*
  1038. Local Variables:
  1039. mode:c
  1040. indent-tabs-mode:nil
  1041. c-basic-offset:2
  1042. End:
  1043. */