test.c 38 KB

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