test.c 19 KB

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