test.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  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 "or.h"
  13. #include "../common/test.h"
  14. void
  15. dump_hex(char *s, int len)
  16. {
  17. static const char TABLE[] = "0123456789ABCDEF";
  18. unsigned char *d = s;
  19. int i, j, nyb;
  20. for(i=0;i<len;++i) {
  21. for (j=1;j>=0;--j) {
  22. nyb = (((int) d[i]) >> (j*4)) & 0x0f;
  23. assert(0<=nyb && nyb <=15);
  24. putchar(TABLE[nyb]);
  25. }
  26. }
  27. }
  28. void
  29. setup_directory() {
  30. char buf[256];
  31. int r;
  32. sprintf(buf, "/tmp/tor_test");
  33. #ifdef _MSC_VER
  34. r = mkdir(buf);
  35. #else
  36. r = mkdir(buf, 0700);
  37. #endif
  38. if (r && errno != EEXIST)
  39. fprintf(stderr, "Can't create directory %s", buf);
  40. }
  41. void
  42. test_buffers() {
  43. #define MAX_BUF_SIZE 1024*1024
  44. char str[256];
  45. char str2[256];
  46. buf_t *buf;
  47. buf_t *buf2;
  48. int s, i, j, eof;
  49. /****
  50. * buf_new
  51. ****/
  52. if (!(buf = buf_new()))
  53. test_fail();
  54. test_eq(buf_capacity(buf), 512*1024);
  55. test_eq(buf_datalen(buf), 0);
  56. /****
  57. * read_to_buf
  58. ****/
  59. s = open("/tmp/tor_test/data", O_WRONLY|O_CREAT|O_TRUNC, 0600);
  60. for (j=0;j<256;++j) {
  61. str[j] = (char)j;
  62. }
  63. write(s, str, 256);
  64. close(s);
  65. s = open("/tmp/tor_test/data", O_RDONLY, 0);
  66. eof = 0;
  67. i = read_to_buf(s, 10, buf, &eof);
  68. test_eq(buf_capacity(buf), 512*1024);
  69. test_eq(buf_datalen(buf), 10);
  70. test_eq(eof, 0);
  71. test_eq(i, 10);
  72. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
  73. /* Test reading 0 bytes. */
  74. i = read_to_buf(s, 0, buf, &eof);
  75. test_eq(buf_capacity(buf), 512*1024);
  76. test_eq(buf_datalen(buf), 10);
  77. test_eq(eof, 0);
  78. test_eq(i, 0);
  79. /* Now test when buffer is filled exactly. */
  80. buf2 = buf_new_with_capacity(6);
  81. i = read_to_buf(s, 6, buf2, &eof);
  82. test_eq(buf_capacity(buf2), 6);
  83. test_eq(buf_datalen(buf2), 6);
  84. test_eq(eof, 0);
  85. test_eq(i, 6);
  86. test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
  87. buf_free(buf2);
  88. /* Now test when buffer is filled with more data to read. */
  89. buf2 = buf_new_with_capacity(32);
  90. i = read_to_buf(s, 128, buf2, &eof);
  91. test_eq(buf_capacity(buf2), 128);
  92. test_eq(buf_datalen(buf2), 32);
  93. test_eq(eof, 0);
  94. test_eq(i, 32);
  95. buf_free(buf2);
  96. /* Now read to eof. */
  97. test_assert(buf_capacity(buf) > 256);
  98. i = read_to_buf(s, 1024, buf, &eof);
  99. test_eq(i, (256-32-10-6));
  100. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  101. test_eq(buf_datalen(buf), 256-6-32);
  102. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
  103. test_eq(eof, 0);
  104. i = read_to_buf(s, 1024, buf, &eof);
  105. test_eq(i, 0);
  106. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  107. test_eq(buf_datalen(buf), 256-6-32);
  108. test_eq(eof, 1);
  109. close(s);
  110. /****
  111. * find_on_inbuf
  112. ****/
  113. buf_free(buf);
  114. buf = buf_new();
  115. s = open("/tmp/tor_test/data", O_RDONLY, 0);
  116. eof = 0;
  117. i = read_to_buf(s, 1024, buf, &eof);
  118. test_eq(256, i);
  119. close(s);
  120. test_eq(((int)'d') + 1, find_on_inbuf("abcd", 4, buf));
  121. test_eq(-1, find_on_inbuf("xyzzy", 5, buf));
  122. /* Make sure we don't look off the end of the buffef */
  123. ((char*)_buf_peek_raw_buffer(buf))[256] = 'A';
  124. ((char*)_buf_peek_raw_buffer(buf))[257] = 'X';
  125. test_eq(-1, find_on_inbuf("\xff" "A", 2, buf));
  126. test_eq(-1, find_on_inbuf("AX", 2, buf));
  127. /* Make sure we use the string length */
  128. test_eq(((int)'d')+1, find_on_inbuf("abcdX", 4, buf));
  129. /****
  130. * fetch_from_buf
  131. ****/
  132. memset(str2, 255, 256);
  133. test_eq(246, fetch_from_buf(str2, 10, buf));
  134. test_memeq(str2, str, 10);
  135. test_memeq(str+10,(char*)_buf_peek_raw_buffer(buf),246);
  136. test_eq(buf_datalen(buf),246);
  137. test_eq(0, fetch_from_buf(str2, 246, buf));
  138. test_memeq(str2, str+10, 246);
  139. test_eq(buf_capacity(buf),MAX_BUF_SIZE);
  140. test_eq(buf_datalen(buf),0);
  141. /****
  142. * write_to_buf
  143. ****/
  144. memset((char *)_buf_peek_raw_buffer(buf), (int)'-', 256);
  145. i = write_to_buf("Hello world", 11, buf);
  146. test_eq(i, 11);
  147. test_eq(buf_datalen(buf), 11);
  148. test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello world", 11);
  149. i = write_to_buf("XYZZY", 5, buf);
  150. test_eq(i, 16);
  151. test_eq(buf_datalen(buf), 16);
  152. test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello worldXYZZY", 16);
  153. /* Test when buffer is overfull. */
  154. #if 0
  155. buflen = 18;
  156. test_eq(-1, write_to_buf("This string will not fit.", 25,
  157. &buf, &buflen, &buf_datalen));
  158. test_eq(buf_datalen, 16);
  159. test_memeq(buf, "Hello worldXYZZY--", 18);
  160. buflen = MAX_BUF_SIZE;
  161. #endif
  162. /****
  163. * flush_buf
  164. ****/
  165. /* XXXX Needs tests. */
  166. buf_free(buf);
  167. }
  168. void
  169. test_crypto_dh()
  170. {
  171. crypto_dh_env_t *dh1, *dh2;
  172. char p1[CRYPTO_DH_SIZE];
  173. char p2[CRYPTO_DH_SIZE];
  174. char s1[CRYPTO_DH_SIZE];
  175. char s2[CRYPTO_DH_SIZE];
  176. int s1len, s2len;
  177. dh1 = crypto_dh_new();
  178. dh2 = crypto_dh_new();
  179. test_eq(crypto_dh_get_bytes(dh1), CRYPTO_DH_SIZE);
  180. test_eq(crypto_dh_get_bytes(dh2), CRYPTO_DH_SIZE);
  181. memset(p1, 0, CRYPTO_DH_SIZE);
  182. memset(p2, 0, CRYPTO_DH_SIZE);
  183. test_memeq(p1, p2, CRYPTO_DH_SIZE);
  184. test_assert(! crypto_dh_get_public(dh1, p1, CRYPTO_DH_SIZE));
  185. test_memneq(p1, p2, CRYPTO_DH_SIZE);
  186. test_assert(! crypto_dh_get_public(dh2, p2, CRYPTO_DH_SIZE));
  187. test_memneq(p1, p2, CRYPTO_DH_SIZE);
  188. memset(s1, 0, CRYPTO_DH_SIZE);
  189. memset(s2, 0xFF, CRYPTO_DH_SIZE);
  190. s1len = crypto_dh_compute_secret(dh1, p2, CRYPTO_DH_SIZE, s1, 50);
  191. s2len = crypto_dh_compute_secret(dh2, p1, CRYPTO_DH_SIZE, s2, 50);
  192. test_assert(s1len > 0);
  193. test_eq(s1len, s2len);
  194. test_memeq(s1, s2, s1len);
  195. crypto_dh_free(dh1);
  196. crypto_dh_free(dh2);
  197. }
  198. void
  199. test_crypto()
  200. {
  201. crypto_cipher_env_t *env1, *env2;
  202. crypto_pk_env_t *pk1, *pk2;
  203. char *data1, *data2, *data3, *cp;
  204. FILE *f;
  205. int i, j;
  206. int str_ciphers[] = { CRYPTO_CIPHER_IDENTITY,
  207. CRYPTO_CIPHER_DES,
  208. CRYPTO_CIPHER_RC4,
  209. CRYPTO_CIPHER_3DES,
  210. CRYPTO_CIPHER_AES_CTR,
  211. -1 };
  212. data1 = tor_malloc(1024);
  213. data2 = tor_malloc(1024);
  214. data3 = tor_malloc(1024);
  215. test_assert(data1 && data2 && data3);
  216. /* Try out RNG. */
  217. test_assert(! crypto_seed_rng());
  218. crypto_rand(100, data1);
  219. crypto_rand(100, data2);
  220. test_memneq(data1,data2,100);
  221. /* Try out identity ciphers. */
  222. env1 = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  223. test_neq(env1, 0);
  224. test_eq(crypto_cipher_generate_key(env1), 0);
  225. test_eq(crypto_cipher_set_iv(env1, ""), 0);
  226. test_eq(crypto_cipher_encrypt_init_cipher(env1), 0);
  227. for(i = 0; i < 1024; ++i) {
  228. data1[i] = (char) i*73;
  229. }
  230. crypto_cipher_encrypt(env1, data1, 1024, data2);
  231. test_memeq(data1, data2, 1024);
  232. crypto_free_cipher_env(env1);
  233. /* Now, test encryption and decryption with stream ciphers. */
  234. data1[0]='\0';
  235. for(i = 1023; i>0; i -= 35)
  236. strncat(data1, "Now is the time for all good onions", i);
  237. for(i=0; str_ciphers[i] >= 0; ++i) {
  238. /* For each cipher... */
  239. memset(data2, 0, 1024);
  240. memset(data3, 0, 1024);
  241. env1 = crypto_new_cipher_env(str_ciphers[i]);
  242. test_neq(env1, 0);
  243. env2 = crypto_new_cipher_env(str_ciphers[i]);
  244. test_neq(env2, 0);
  245. j = crypto_cipher_generate_key(env1);
  246. if (str_ciphers[i] != CRYPTO_CIPHER_IDENTITY) {
  247. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  248. }
  249. crypto_cipher_set_iv(env1, "12345678901234567890");
  250. crypto_cipher_set_iv(env2, "12345678901234567890");
  251. crypto_cipher_encrypt_init_cipher(env1);
  252. crypto_cipher_decrypt_init_cipher(env2);
  253. /* Try encrypting 512 chars. */
  254. crypto_cipher_encrypt(env1, data1, 512, data2);
  255. crypto_cipher_decrypt(env2, data2, 512, data3);
  256. test_memeq(data1, data3, 512);
  257. if (str_ciphers[i] == CRYPTO_CIPHER_IDENTITY) {
  258. test_memeq(data1, data2, 512);
  259. } else {
  260. test_memneq(data1, data2, 512);
  261. }
  262. /* Now encrypt 1 at a time, and get 1 at a time. */
  263. for (j = 512; j < 560; ++j) {
  264. crypto_cipher_encrypt(env1, data1+j, 1, data2+j);
  265. }
  266. for (j = 512; j < 560; ++j) {
  267. crypto_cipher_decrypt(env2, data2+j, 1, data3+j);
  268. }
  269. test_memeq(data1, data3, 560);
  270. /* Now encrypt 3 at a time, and get 5 at a time. */
  271. for (j = 560; j < 1024-5; j += 3) {
  272. crypto_cipher_encrypt(env1, data1+j, 3, data2+j);
  273. }
  274. for (j = 560; j < 1024-5; j += 5) {
  275. crypto_cipher_decrypt(env2, data2+j, 5, data3+j);
  276. }
  277. test_memeq(data1, data3, 1024-5);
  278. /* Now make sure that when we encrypt with different chunk sizes, we get
  279. the same results. */
  280. crypto_free_cipher_env(env2);
  281. memset(data3, 0, 1024);
  282. env2 = crypto_new_cipher_env(str_ciphers[i]);
  283. test_neq(env2, 0);
  284. if (str_ciphers[i] != CRYPTO_CIPHER_IDENTITY) {
  285. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  286. }
  287. crypto_cipher_set_iv(env2, "12345678901234567890");
  288. crypto_cipher_encrypt_init_cipher(env2);
  289. for (j = 0; j < 1024-16; j += 17) {
  290. crypto_cipher_encrypt(env2, data1+j, 17, data3+j);
  291. }
  292. for (j= 0; j < 1024-16; ++j) {
  293. if (data2[j] != data3[j]) {
  294. printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
  295. }
  296. }
  297. test_memeq(data2, data3, 1024-16);
  298. crypto_free_cipher_env(env1);
  299. crypto_free_cipher_env(env2);
  300. }
  301. /* Test vectors for stream ciphers. */
  302. /* XXXX Look up some test vectors for the ciphers and make sure we match. */
  303. /* Test SHA-1 with a test vector from the specification. */
  304. i = crypto_SHA_digest("abc", 3, data1);
  305. test_memeq(data1,
  306. "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
  307. "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
  308. /* Public-key ciphers */
  309. pk1 = crypto_new_pk_env(CRYPTO_PK_RSA);
  310. pk2 = crypto_new_pk_env(CRYPTO_PK_RSA);
  311. test_assert(pk1 && pk2);
  312. test_assert(! crypto_pk_generate_key(pk1));
  313. test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &i));
  314. test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, i));
  315. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  316. test_eq(128, crypto_pk_keysize(pk1));
  317. test_eq(128, crypto_pk_keysize(pk2));
  318. test_eq(128, crypto_pk_public_encrypt(pk2, "Hello whirled.", 15, data1,
  319. RSA_PKCS1_OAEP_PADDING));
  320. test_eq(128, crypto_pk_public_encrypt(pk1, "Hello whirled.", 15, data2,
  321. RSA_PKCS1_OAEP_PADDING));
  322. /* oaep padding should make encryption not match */
  323. test_memneq(data1, data2, 128);
  324. test_eq(15, crypto_pk_private_decrypt(pk1, data1, 128, data3,
  325. RSA_PKCS1_OAEP_PADDING));
  326. test_streq(data3, "Hello whirled.");
  327. memset(data3, 0, 1024);
  328. test_eq(15, crypto_pk_private_decrypt(pk1, data2, 128, data3,
  329. RSA_PKCS1_OAEP_PADDING));
  330. test_streq(data3, "Hello whirled.");
  331. /* Can't decrypt with public key. */
  332. test_eq(-1, crypto_pk_private_decrypt(pk2, data2, 128, data3,
  333. RSA_PKCS1_OAEP_PADDING));
  334. /* Try again with bad padding */
  335. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  336. test_eq(-1, crypto_pk_private_decrypt(pk1, data2, 128, data3,
  337. RSA_PKCS1_OAEP_PADDING));
  338. /* File operations: save and load private key */
  339. f = fopen("/tmp/tor_test/pkey1", "wb");
  340. test_assert(! crypto_pk_write_private_key_to_file(pk1, f));
  341. fclose(f);
  342. f = fopen("/tmp/tor_test/pkey1", "rb");
  343. test_assert(! crypto_pk_read_private_key_from_file(pk2, f));
  344. fclose(f);
  345. test_eq(15, crypto_pk_private_decrypt(pk2, data1, 128, data3,
  346. RSA_PKCS1_OAEP_PADDING));
  347. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  348. "/tmp/tor_test/pkey1"));
  349. test_eq(15, crypto_pk_private_decrypt(pk2, data1, 128, data3,
  350. RSA_PKCS1_OAEP_PADDING));
  351. /* Now try signing. */
  352. strcpy(data1, "Ossifrage");
  353. test_eq(128, crypto_pk_private_sign(pk1, data1, 10, data2));
  354. test_eq(10, crypto_pk_public_checksig(pk1, data2, 128, data3));
  355. test_streq(data3, "Ossifrage");
  356. /*XXXX test failed signing*/
  357. crypto_free_pk_env(pk1);
  358. crypto_free_pk_env(pk2);
  359. /* Base64 tests */
  360. strcpy(data1, "Test string that contains 35 chars.");
  361. strcat(data1, " 2nd string that contains 35 chars.");
  362. i = base64_encode(data2, 1024, data1, 71);
  363. j = base64_decode(data3, 1024, data2, i);
  364. test_streq(data3, data1);
  365. test_eq(j, 71);
  366. test_assert(data2[i] == '\0');
  367. free(data1);
  368. free(data2);
  369. free(data3);
  370. }
  371. void
  372. test_util() {
  373. struct timeval start, end;
  374. struct tm a_time;
  375. start.tv_sec = 5;
  376. start.tv_usec = 5000;
  377. end.tv_sec = 5;
  378. end.tv_usec = 5000;
  379. test_eq(0L, tv_udiff(&start, &end));
  380. end.tv_usec = 7000;
  381. test_eq(2000L, tv_udiff(&start, &end));
  382. end.tv_sec = 6;
  383. test_eq(1002000L, tv_udiff(&start, &end));
  384. end.tv_usec = 0;
  385. test_eq(995000L, tv_udiff(&start, &end));
  386. end.tv_sec = 4;
  387. test_eq(0L, tv_udiff(&start, &end));
  388. /* The test values here are confirmed to be correct on a platform
  389. * with a working timegm. */
  390. a_time.tm_year = 2003-1900;
  391. a_time.tm_mon = 7;
  392. a_time.tm_mday = 30;
  393. a_time.tm_hour = 6;
  394. a_time.tm_min = 14;
  395. a_time.tm_sec = 55;
  396. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  397. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  398. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  399. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  400. a_time.tm_mday = 10;
  401. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  402. }
  403. void
  404. test_onion_handshake() {
  405. /* client-side */
  406. crypto_dh_env_t *c_dh = NULL;
  407. char c_buf[DH_ONIONSKIN_LEN];
  408. char c_keys[40];
  409. /* server-side */
  410. char s_buf[DH_KEY_LEN];
  411. char s_keys[40];
  412. /* shared */
  413. crypto_pk_env_t *pk = NULL;
  414. pk = crypto_new_pk_env(CRYPTO_PK_RSA);
  415. test_assert(! crypto_pk_generate_key(pk));
  416. /* client handshake 1. */
  417. memset(c_buf, 0, DH_ONIONSKIN_LEN);
  418. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  419. /* server handshake */
  420. memset(s_buf, 0, DH_KEY_LEN);
  421. memset(s_keys, 0, 40);
  422. test_assert(! onion_skin_server_handshake(c_buf, pk, s_buf, s_keys, 40));
  423. /* client handshake 2 */
  424. memset(c_keys, 0, 40);
  425. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  426. crypto_dh_free(c_dh);
  427. if (memcmp(c_keys, s_keys, 40)) {
  428. puts("Aiiiie");
  429. exit(1);
  430. }
  431. test_memeq(c_keys, s_keys, 40);
  432. memset(s_buf, 0, 40);
  433. test_memneq(c_keys, s_buf, 40);
  434. crypto_free_pk_env(pk);
  435. }
  436. /* from routers.c */
  437. int compare_recommended_versions(char *myversion, char *start);
  438. void
  439. test_dir_format()
  440. {
  441. char buf[8192], buf2[8192];
  442. char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
  443. int pk1_str_len, pk2_str_len, pk3_str_len;
  444. routerinfo_t r1, r2;
  445. crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
  446. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  447. struct exit_policy_t ex1, ex2;
  448. directory_t *dir1 = NULL, *dir2 = NULL;
  449. test_assert( (pk1 = crypto_new_pk_env(CRYPTO_PK_RSA)) );
  450. test_assert( (pk2 = crypto_new_pk_env(CRYPTO_PK_RSA)) );
  451. test_assert( (pk3 = crypto_new_pk_env(CRYPTO_PK_RSA)) );
  452. test_assert(! crypto_pk_generate_key(pk1));
  453. test_assert(! crypto_pk_generate_key(pk2));
  454. test_assert(! crypto_pk_generate_key(pk3));
  455. r1.address = "testaddr1.foo.bar";
  456. r1.addr = 0xc0a80001u; /* 192.168.0.1 */
  457. r1.published_on = 0;
  458. r1.or_port = 9000;
  459. r1.socks_port = 9002;
  460. r1.dir_port = 9003;
  461. r1.onion_pkey = pk1;
  462. r1.identity_pkey = pk2;
  463. r1.link_pkey = pk3;
  464. r1.bandwidth = 1000;
  465. r1.exit_policy = NULL;
  466. r1.nickname = "Magri";
  467. ex1.policy_type = EXIT_POLICY_ACCEPT;
  468. ex1.string = NULL;
  469. ex1.address = "*";
  470. ex1.port = "80";
  471. ex1.next = &ex2;
  472. ex2.policy_type = EXIT_POLICY_REJECT;
  473. ex2.address = "18.*";
  474. ex2.port = "24";
  475. ex2.next = NULL;
  476. r2.address = "tor.tor.tor";
  477. r2.addr = 0x0a030201u; /* 10.3.2.1 */
  478. r2.published_on = 5;
  479. r2.or_port = 9005;
  480. r2.socks_port = 0;
  481. r2.dir_port = 0;
  482. r2.onion_pkey = pk2;
  483. r2.identity_pkey = pk1;
  484. r2.link_pkey = pk2;
  485. r2.bandwidth = 3000;
  486. r2.exit_policy = &ex1;
  487. test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  488. &pk1_str_len));
  489. test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  490. &pk2_str_len));
  491. test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
  492. &pk3_str_len));
  493. memset(buf, 0, 2048);
  494. log_set_severity(LOG_WARN);
  495. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  496. strcpy(buf2, "router Magri testaddr1.foo.bar 9000 9002 9003 1000\n"
  497. "platform Tor "VERSION" on ");
  498. strcat(buf2, get_uname());
  499. strcat(buf2, "\n"
  500. "published 1970-01-01 00:00:00\n"
  501. "onion-key\n");
  502. strcat(buf2, pk1_str);
  503. strcat(buf2, "link-key\n");
  504. strcat(buf2, pk3_str);
  505. strcat(buf2, "signing-key\n");
  506. strcat(buf2, pk2_str);
  507. strcat(buf2, "router-signature\n");
  508. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same twice*/
  509. test_streq(buf, buf2);
  510. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  511. cp = buf;
  512. rp1 = router_get_entry_from_string(&cp);
  513. test_assert(rp1);
  514. test_streq(rp1->address, r1.address);
  515. test_eq(rp1->or_port, r1.or_port);
  516. test_eq(rp1->socks_port, r1.socks_port);
  517. test_eq(rp1->dir_port, r1.dir_port);
  518. test_eq(rp1->bandwidth, r1.bandwidth);
  519. test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  520. test_assert(crypto_pk_cmp_keys(rp1->link_pkey, pk3) == 0);
  521. test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  522. test_assert(rp1->exit_policy == NULL);
  523. #if 0
  524. /* XXX Once we have exit policies, test this again. XXX */
  525. strcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n");
  526. strcat(buf2, pk2_str);
  527. strcat(buf2, "signing-key\n");
  528. strcat(buf2, pk1_str);
  529. strcat(buf2, "accept *:80\nreject 18.*:24\n\n");
  530. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
  531. test_streq(buf, buf2);
  532. cp = buf;
  533. rp2 = router_get_entry_from_string(&cp);
  534. test_assert(rp2);
  535. test_streq(rp2->address, r2.address);
  536. test_eq(rp2->or_port, r2.or_port);
  537. test_eq(rp2->socks_port, r2.socks_port);
  538. test_eq(rp2->dir_port, r2.dir_port);
  539. test_eq(rp2->bandwidth, r2.bandwidth);
  540. test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  541. test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  542. test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
  543. test_streq(rp2->exit_policy->string, "accept *:80");
  544. test_streq(rp2->exit_policy->address, "*");
  545. test_streq(rp2->exit_policy->port, "80");
  546. test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
  547. test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
  548. test_streq(rp2->exit_policy->next->address, "18.*");
  549. test_streq(rp2->exit_policy->next->port, "24");
  550. test_assert(rp2->exit_policy->next->next == NULL);
  551. #endif
  552. #if 0
  553. /* XXX To re-enable this test, we need to separate directory generation
  554. * XXX from the directory backend again. Do this the next time we have
  555. * XXX directory trouble. */
  556. /* Okay, now for the directories. */
  557. dir1 = (directory_t*) tor_malloc(sizeof(directory_t));
  558. dir1->n_routers = 2;
  559. dir1->routers = (routerinfo_t**) tor_malloc(sizeof(routerinfo_t*)*2);
  560. dir1->routers[0] = &r1;
  561. dir1->routers[1] = &r2;
  562. test_assert(! dump_signed_directory_to_string_impl(buf, 4096, dir1, pk1));
  563. /* puts(buf); */
  564. test_assert(! router_get_dir_from_string_impl(buf, &dir2, pk1));
  565. test_eq(2, dir2->n_routers);
  566. #endif
  567. tor_free(pk1_str);
  568. tor_free(pk2_str);
  569. if (pk1) crypto_free_pk_env(pk1);
  570. if (pk2) crypto_free_pk_env(pk2);
  571. if (rp1) routerinfo_free(rp1);
  572. if (rp2) routerinfo_free(rp2);
  573. tor_free(dir1); /* And more !*/
  574. tor_free(dir2); /* And more !*/
  575. /* make sure compare_recommended_versions() works */
  576. test_eq(0, compare_recommended_versions("abc", "abc"));
  577. test_eq(0, compare_recommended_versions("abc", "ab,abd,abde,abc,abcde"));
  578. test_eq(0, compare_recommended_versions("abc", "ab,abd,abde,abcde,abc"));
  579. test_eq(0, compare_recommended_versions("abc", "abc,abd,abde,abc,abcde"));
  580. test_eq(0, compare_recommended_versions("a", "a,ab,abd,abde,abc,abcde"));
  581. test_eq(-1, compare_recommended_versions("a", "ab,abd,abde,abc,abcde"));
  582. test_eq(-1, compare_recommended_versions("abb", "ab,abd,abde,abc,abcde"));
  583. test_eq(-1, compare_recommended_versions("a", ""));
  584. test_eq(0, compare_recommended_versions(VERSION, RECOMMENDED_SOFTWARE_VERSIONS));
  585. }
  586. int
  587. main(int c, char**v){
  588. #if 0
  589. or_options_t options; /* command-line and config-file options */
  590. if(getconfig(c,v,&options))
  591. exit(1);
  592. #endif
  593. log_set_severity(LOG_ERR); /* make logging quieter */
  594. crypto_seed_rng();
  595. setup_directory();
  596. puts("========================== Buffers =========================");
  597. test_buffers();
  598. puts("\n========================== Crypto ==========================");
  599. test_crypto();
  600. test_crypto_dh();
  601. puts("\n========================= Util ============================");
  602. test_util();
  603. puts("\n========================= Onion Skins =====================");
  604. test_onion_handshake();
  605. puts("\n========================= Directory Formats ===============");
  606. test_dir_format();
  607. puts("");
  608. return 0;
  609. }
  610. /*
  611. Local Variables:
  612. mode:c
  613. indent-tabs-mode:nil
  614. c-basic-offset:2
  615. End:
  616. */