test.c 36 KB

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