test.c 36 KB

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