test.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /* Copyright 2001,2002,2003 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include <stdio.h>
  5. #ifdef HAVE_FCNTL_H
  6. #include <fcntl.h>
  7. #endif
  8. #ifdef MS_WINDOWS
  9. /* For mkdir() */
  10. #include <direct.h>
  11. #endif
  12. #include "or.h"
  13. #include "../common/test.h"
  14. extern or_options_t options;
  15. int have_failed = 0;
  16. /* These functions are file-local, but are exposed so we can test. */
  17. int router_get_routerlist_from_directory_impl(
  18. const char *s, routerlist_t **dest, crypto_pk_env_t *pkey);
  19. void add_fingerprint_to_dir(const char *nickname, const char *fp);
  20. void get_platform_str(char *platform, int len);
  21. void
  22. dump_hex(char *s, int len)
  23. {
  24. static const char TABLE[] = "0123456789ABCDEF";
  25. unsigned char *d = s;
  26. int i, j, nyb;
  27. for(i=0;i<len;++i) {
  28. for (j=1;j>=0;--j) {
  29. nyb = (((int) d[i]) >> (j*4)) & 0x0f;
  30. assert(0<=nyb && nyb <=15);
  31. putchar(TABLE[nyb]);
  32. }
  33. }
  34. }
  35. void
  36. setup_directory() {
  37. char buf[256];
  38. int r;
  39. sprintf(buf, "/tmp/tor_test");
  40. #ifdef _MSC_VER
  41. r = mkdir(buf);
  42. #else
  43. r = mkdir(buf, 0700);
  44. #endif
  45. if (r && errno != EEXIST)
  46. fprintf(stderr, "Can't create directory %s", buf);
  47. }
  48. void
  49. test_buffers() {
  50. #define MAX_BUF_SIZE 1024*1024
  51. char str[256];
  52. char str2[256];
  53. buf_t *buf;
  54. buf_t *buf2;
  55. int s, i, j, eof;
  56. /****
  57. * buf_new
  58. ****/
  59. if (!(buf = buf_new()))
  60. test_fail();
  61. test_eq(buf_capacity(buf), 512*1024);
  62. test_eq(buf_datalen(buf), 0);
  63. /****
  64. * read_to_buf
  65. ****/
  66. s = open("/tmp/tor_test/data", O_WRONLY|O_CREAT|O_TRUNC, 0600);
  67. for (j=0;j<256;++j) {
  68. str[j] = (char)j;
  69. }
  70. write(s, str, 256);
  71. close(s);
  72. s = open("/tmp/tor_test/data", O_RDONLY, 0);
  73. eof = 0;
  74. i = read_to_buf(s, 10, 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, 10);
  79. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
  80. /* Test reading 0 bytes. */
  81. i = read_to_buf(s, 0, buf, &eof);
  82. test_eq(buf_capacity(buf), 512*1024);
  83. test_eq(buf_datalen(buf), 10);
  84. test_eq(eof, 0);
  85. test_eq(i, 0);
  86. /* Now test when buffer is filled exactly. */
  87. buf2 = buf_new_with_capacity(6);
  88. i = read_to_buf(s, 6, buf2, &eof);
  89. test_eq(buf_capacity(buf2), 6);
  90. test_eq(buf_datalen(buf2), 6);
  91. test_eq(eof, 0);
  92. test_eq(i, 6);
  93. test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
  94. buf_free(buf2);
  95. /* Now test when buffer is filled with more data to read. */
  96. buf2 = buf_new_with_capacity(32);
  97. i = read_to_buf(s, 128, buf2, &eof);
  98. test_eq(buf_capacity(buf2), 128);
  99. test_eq(buf_datalen(buf2), 32);
  100. test_eq(eof, 0);
  101. test_eq(i, 32);
  102. buf_free(buf2);
  103. /* Now read to eof. */
  104. test_assert(buf_capacity(buf) > 256);
  105. i = read_to_buf(s, 1024, buf, &eof);
  106. test_eq(i, (256-32-10-6));
  107. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  108. test_eq(buf_datalen(buf), 256-6-32);
  109. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
  110. test_eq(eof, 0);
  111. i = read_to_buf(s, 1024, buf, &eof);
  112. test_eq(i, 0);
  113. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  114. test_eq(buf_datalen(buf), 256-6-32);
  115. test_eq(eof, 1);
  116. close(s);
  117. /****
  118. * find_on_inbuf
  119. ****/
  120. buf_free(buf);
  121. buf = buf_new();
  122. s = open("/tmp/tor_test/data", O_RDONLY, 0);
  123. eof = 0;
  124. i = read_to_buf(s, 1024, buf, &eof);
  125. test_eq(256, i);
  126. close(s);
  127. test_eq(((int)'d') + 1, find_on_inbuf("abcd", 4, buf));
  128. test_eq(-1, find_on_inbuf("xyzzy", 5, buf));
  129. /* Make sure we don't look off the end of the buffef */
  130. ((char*)_buf_peek_raw_buffer(buf))[256] = 'A';
  131. ((char*)_buf_peek_raw_buffer(buf))[257] = 'X';
  132. test_eq(-1, find_on_inbuf("\xff" "A", 2, buf));
  133. test_eq(-1, find_on_inbuf("AX", 2, buf));
  134. /* Make sure we use the string length */
  135. test_eq(((int)'d')+1, find_on_inbuf("abcdX", 4, buf));
  136. /****
  137. * fetch_from_buf
  138. ****/
  139. memset(str2, 255, 256);
  140. test_eq(246, fetch_from_buf(str2, 10, buf));
  141. test_memeq(str2, str, 10);
  142. test_memeq(str+10,(char*)_buf_peek_raw_buffer(buf),246);
  143. test_eq(buf_datalen(buf),246);
  144. test_eq(0, fetch_from_buf(str2, 246, buf));
  145. test_memeq(str2, str+10, 246);
  146. test_eq(buf_capacity(buf),MAX_BUF_SIZE);
  147. test_eq(buf_datalen(buf),0);
  148. /****
  149. * write_to_buf
  150. ****/
  151. memset((char *)_buf_peek_raw_buffer(buf), (int)'-', 256);
  152. i = write_to_buf("Hello world", 11, buf);
  153. test_eq(i, 11);
  154. test_eq(buf_datalen(buf), 11);
  155. test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello world", 11);
  156. i = write_to_buf("XYZZY", 5, buf);
  157. test_eq(i, 16);
  158. test_eq(buf_datalen(buf), 16);
  159. test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello worldXYZZY", 16);
  160. /* Test when buffer is overfull. */
  161. #if 0
  162. buflen = 18;
  163. test_eq(-1, write_to_buf("This string will not fit.", 25,
  164. &buf, &buflen, &buf_datalen));
  165. test_eq(buf_datalen, 16);
  166. test_memeq(buf, "Hello worldXYZZY--", 18);
  167. buflen = MAX_BUF_SIZE;
  168. #endif
  169. /****
  170. * flush_buf
  171. ****/
  172. /* XXXX Needs tests. */
  173. buf_free(buf);
  174. }
  175. void
  176. test_crypto_dh()
  177. {
  178. crypto_dh_env_t *dh1, *dh2;
  179. char p1[CRYPTO_DH_SIZE];
  180. char p2[CRYPTO_DH_SIZE];
  181. char s1[CRYPTO_DH_SIZE];
  182. char s2[CRYPTO_DH_SIZE];
  183. int s1len, s2len;
  184. dh1 = crypto_dh_new();
  185. dh2 = crypto_dh_new();
  186. test_eq(crypto_dh_get_bytes(dh1), CRYPTO_DH_SIZE);
  187. test_eq(crypto_dh_get_bytes(dh2), CRYPTO_DH_SIZE);
  188. memset(p1, 0, CRYPTO_DH_SIZE);
  189. memset(p2, 0, CRYPTO_DH_SIZE);
  190. test_memeq(p1, p2, CRYPTO_DH_SIZE);
  191. test_assert(! crypto_dh_get_public(dh1, p1, CRYPTO_DH_SIZE));
  192. test_memneq(p1, p2, CRYPTO_DH_SIZE);
  193. test_assert(! crypto_dh_get_public(dh2, p2, CRYPTO_DH_SIZE));
  194. test_memneq(p1, p2, CRYPTO_DH_SIZE);
  195. memset(s1, 0, CRYPTO_DH_SIZE);
  196. memset(s2, 0xFF, CRYPTO_DH_SIZE);
  197. s1len = crypto_dh_compute_secret(dh1, p2, CRYPTO_DH_SIZE, s1, 50);
  198. s2len = crypto_dh_compute_secret(dh2, p1, CRYPTO_DH_SIZE, s2, 50);
  199. test_assert(s1len > 0);
  200. test_eq(s1len, s2len);
  201. test_memeq(s1, s2, s1len);
  202. crypto_dh_free(dh1);
  203. crypto_dh_free(dh2);
  204. }
  205. void
  206. test_crypto()
  207. {
  208. crypto_cipher_env_t *env1, *env2;
  209. crypto_pk_env_t *pk1, *pk2;
  210. char *data1, *data2, *data3, *cp;
  211. FILE *f;
  212. int i, j, p, len;
  213. data1 = tor_malloc(1024);
  214. data2 = tor_malloc(1024);
  215. data3 = tor_malloc(1024);
  216. test_assert(data1 && data2 && data3);
  217. /* Try out RNG. */
  218. test_assert(! crypto_seed_rng());
  219. crypto_rand(100, data1);
  220. crypto_rand(100, data2);
  221. test_memneq(data1,data2,100);
  222. #if 0
  223. /* Try out identity ciphers. */
  224. env1 = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  225. test_neq(env1, 0);
  226. test_eq(crypto_cipher_generate_key(env1), 0);
  227. test_eq(crypto_cipher_set_iv(env1, ""), 0);
  228. test_eq(crypto_cipher_encrypt_init_cipher(env1), 0);
  229. for(i = 0; i < 1024; ++i) {
  230. data1[i] = (char) i*73;
  231. }
  232. crypto_cipher_encrypt(env1, data1, 1024, data2);
  233. test_memeq(data1, data2, 1024);
  234. crypto_free_cipher_env(env1);
  235. #endif
  236. /* Now, test encryption and decryption with stream cipher. */
  237. data1[0]='\0';
  238. for(i = 1023; i>0; i -= 35)
  239. strncat(data1, "Now is the time for all good onions", i);
  240. memset(data2, 0, 1024);
  241. memset(data3, 0, 1024);
  242. env1 = crypto_new_cipher_env();
  243. test_neq(env1, 0);
  244. env2 = crypto_new_cipher_env();
  245. test_neq(env2, 0);
  246. j = crypto_cipher_generate_key(env1);
  247. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  248. crypto_cipher_set_iv(env1, "12345678901234567890");
  249. crypto_cipher_set_iv(env2, "12345678901234567890");
  250. crypto_cipher_encrypt_init_cipher(env1);
  251. crypto_cipher_decrypt_init_cipher(env2);
  252. /* Try encrypting 512 chars. */
  253. crypto_cipher_encrypt(env1, data1, 512, data2);
  254. crypto_cipher_decrypt(env2, data2, 512, data3);
  255. test_memeq(data1, data3, 512);
  256. test_memneq(data1, data2, 512);
  257. /* Now encrypt 1 at a time, and get 1 at a time. */
  258. for (j = 512; j < 560; ++j) {
  259. crypto_cipher_encrypt(env1, data1+j, 1, data2+j);
  260. }
  261. for (j = 512; j < 560; ++j) {
  262. crypto_cipher_decrypt(env2, data2+j, 1, data3+j);
  263. }
  264. test_memeq(data1, data3, 560);
  265. /* Now encrypt 3 at a time, and get 5 at a time. */
  266. for (j = 560; j < 1024-5; j += 3) {
  267. crypto_cipher_encrypt(env1, data1+j, 3, data2+j);
  268. }
  269. for (j = 560; j < 1024-5; j += 5) {
  270. crypto_cipher_decrypt(env2, data2+j, 5, data3+j);
  271. }
  272. test_memeq(data1, data3, 1024-5);
  273. /* Now make sure that when we encrypt with different chunk sizes, we get
  274. the same results. */
  275. crypto_free_cipher_env(env2);
  276. memset(data3, 0, 1024);
  277. env2 = crypto_new_cipher_env();
  278. test_neq(env2, 0);
  279. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  280. crypto_cipher_set_iv(env2, "12345678901234567890");
  281. crypto_cipher_encrypt_init_cipher(env2);
  282. for (j = 0; j < 1024-16; j += 17) {
  283. crypto_cipher_encrypt(env2, data1+j, 17, data3+j);
  284. }
  285. for (j= 0; j < 1024-16; ++j) {
  286. if (data2[j] != data3[j]) {
  287. printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
  288. }
  289. }
  290. test_memeq(data2, data3, 1024-16);
  291. crypto_free_cipher_env(env1);
  292. crypto_free_cipher_env(env2);
  293. /* Test vectors for stream ciphers. */
  294. /* XXXX Look up some test vectors for the ciphers and make sure we match. */
  295. /* Test SHA-1 with a test vector from the specification. */
  296. i = crypto_digest("abc", 3, data1);
  297. test_memeq(data1,
  298. "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
  299. "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
  300. /* Public-key ciphers */
  301. pk1 = crypto_new_pk_env();
  302. pk2 = crypto_new_pk_env();
  303. test_assert(pk1 && pk2);
  304. test_assert(! crypto_pk_generate_key(pk1));
  305. test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &i));
  306. test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, i));
  307. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  308. test_eq(128, crypto_pk_keysize(pk1));
  309. test_eq(128, crypto_pk_keysize(pk2));
  310. test_eq(128, crypto_pk_public_encrypt(pk2, "Hello whirled.", 15, data1,
  311. PK_PKCS1_OAEP_PADDING));
  312. test_eq(128, crypto_pk_public_encrypt(pk1, "Hello whirled.", 15, data2,
  313. PK_PKCS1_OAEP_PADDING));
  314. /* oaep padding should make encryption not match */
  315. test_memneq(data1, data2, 128);
  316. test_eq(15, crypto_pk_private_decrypt(pk1, data1, 128, data3,
  317. PK_PKCS1_OAEP_PADDING));
  318. test_streq(data3, "Hello whirled.");
  319. memset(data3, 0, 1024);
  320. test_eq(15, crypto_pk_private_decrypt(pk1, data2, 128, data3,
  321. PK_PKCS1_OAEP_PADDING));
  322. test_streq(data3, "Hello whirled.");
  323. /* Can't decrypt with public key. */
  324. test_eq(-1, crypto_pk_private_decrypt(pk2, data2, 128, data3,
  325. PK_PKCS1_OAEP_PADDING));
  326. /* Try again with bad padding */
  327. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  328. test_eq(-1, crypto_pk_private_decrypt(pk1, data2, 128, data3,
  329. PK_PKCS1_OAEP_PADDING));
  330. /* File operations: save and load private key */
  331. f = fopen("/tmp/tor_test/pkey1", "wb");
  332. test_assert(! crypto_pk_write_private_key_to_file(pk1, f));
  333. fclose(f);
  334. f = fopen("/tmp/tor_test/pkey1", "rb");
  335. test_assert(! crypto_pk_read_private_key_from_file(pk2, f));
  336. fclose(f);
  337. test_eq(15, crypto_pk_private_decrypt(pk2, data1, 128, data3,
  338. PK_PKCS1_OAEP_PADDING));
  339. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  340. "/tmp/tor_test/pkey1"));
  341. test_eq(15, crypto_pk_private_decrypt(pk2, data1, 128, data3,
  342. PK_PKCS1_OAEP_PADDING));
  343. /* Now try signing. */
  344. strcpy(data1, "Ossifrage");
  345. test_eq(128, crypto_pk_private_sign(pk1, data1, 10, data2));
  346. test_eq(10, crypto_pk_public_checksig(pk1, data2, 128, data3));
  347. test_streq(data3, "Ossifrage");
  348. /* Try signing digests. */
  349. test_eq(128, crypto_pk_private_sign_digest(pk1, data1, 10, data2));
  350. test_eq(20, crypto_pk_public_checksig(pk1, data2, 128, data3));
  351. test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  352. test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  353. /*XXXX test failed signing*/
  354. /* Try encoding */
  355. crypto_free_pk_env(pk2);
  356. pk2 = NULL;
  357. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  358. test_assert(i>0);
  359. pk2 = crypto_pk_asn1_decode(data1, i);
  360. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  361. /* Try with hybrid encryption wrappers. */
  362. crypto_rand(1024, data1);
  363. for (i = 0; i < 3; ++i) {
  364. for (j = 85; j < 140; ++j) {
  365. memset(data2,0,1024);
  366. memset(data3,0,1024);
  367. if (i == 0 && j < 129)
  368. continue;
  369. p = (i==0)?PK_NO_PADDING:
  370. (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
  371. len = crypto_pk_public_hybrid_encrypt(pk1,data1,j,data2,p,0);
  372. test_assert(len>=0);
  373. len = crypto_pk_private_hybrid_decrypt(pk1,data2,len,data3,p);
  374. test_eq(len,j);
  375. test_memeq(data1,data3,j);
  376. }
  377. }
  378. crypto_free_pk_env(pk1);
  379. crypto_free_pk_env(pk2);
  380. /* Base64 tests */
  381. strcpy(data1, "Test string that contains 35 chars.");
  382. strcat(data1, " 2nd string that contains 35 chars.");
  383. i = base64_encode(data2, 1024, data1, 71);
  384. j = base64_decode(data3, 1024, data2, i);
  385. test_streq(data3, data1);
  386. test_eq(j, 71);
  387. test_assert(data2[i] == '\0');
  388. /* Base32 tests */
  389. strcpy(data1, "5chrs");
  390. /* bit pattern is: [35 63 68 72 73] ->
  391. * [00110101 01100011 01101000 01110010 01110011]
  392. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  393. */
  394. i = base32_encode(data2, 9, data1, 5);
  395. test_streq(data2, "gvrwq4tt");
  396. strcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4");
  397. printf("-------\n");
  398. i = base32_encode(data2, 30, data1, 10);
  399. test_eq(i,0);
  400. test_streq(data2, "772w2rfobvomsywe");
  401. free(data1);
  402. free(data2);
  403. free(data3);
  404. }
  405. void
  406. test_util() {
  407. struct timeval start, end;
  408. struct tm a_time;
  409. smartlist_t *sl;
  410. start.tv_sec = 5;
  411. start.tv_usec = 5000;
  412. end.tv_sec = 5;
  413. end.tv_usec = 5000;
  414. test_eq(0L, tv_udiff(&start, &end));
  415. end.tv_usec = 7000;
  416. test_eq(2000L, tv_udiff(&start, &end));
  417. end.tv_sec = 6;
  418. test_eq(1002000L, tv_udiff(&start, &end));
  419. end.tv_usec = 0;
  420. test_eq(995000L, tv_udiff(&start, &end));
  421. end.tv_sec = 4;
  422. test_eq(0L, tv_udiff(&start, &end));
  423. /* The test values here are confirmed to be correct on a platform
  424. * with a working timegm. */
  425. a_time.tm_year = 2003-1900;
  426. a_time.tm_mon = 7;
  427. a_time.tm_mday = 30;
  428. a_time.tm_hour = 6;
  429. a_time.tm_min = 14;
  430. a_time.tm_sec = 55;
  431. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  432. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  433. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  434. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  435. a_time.tm_mday = 10;
  436. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  437. /* Test smartlist */
  438. sl = smartlist_create();
  439. smartlist_add(sl, (void*)1);
  440. smartlist_add(sl, (void*)2);
  441. smartlist_add(sl, (void*)3);
  442. smartlist_add(sl, (void*)4);
  443. test_eq(2, (int)smartlist_del_keeporder(sl, 1));
  444. smartlist_insert(sl, 1, (void*)22);
  445. smartlist_insert(sl, 0, (void*)0);
  446. smartlist_insert(sl, 5, (void*)555);
  447. test_eq(0, (int)smartlist_get(sl,0));
  448. test_eq(1, (int)smartlist_get(sl,1));
  449. test_eq(22, (int)smartlist_get(sl,2));
  450. test_eq(3, (int)smartlist_get(sl,3));
  451. test_eq(4, (int)smartlist_get(sl,4));
  452. test_eq(555, (int)smartlist_get(sl,5));
  453. /* XXXX test older functions. */
  454. smartlist_free(sl);
  455. }
  456. static void* _squareAndRemoveK4(const char *key, void*val, void *data)
  457. {
  458. int *ip = (int*)data;
  459. int v;
  460. if (strcmp(key,"K4") == 0) {
  461. ++(*ip);
  462. return NULL;
  463. }
  464. v = (int)val;
  465. return (void*)(v*v);
  466. }
  467. void test_strmap() {
  468. strmap_t *map;
  469. strmap_iter_t *iter;
  470. const char *k;
  471. void *v;
  472. int count;
  473. map = strmap_new();
  474. v = strmap_set(map, "K1", (void*)99);
  475. test_eq(v, NULL);
  476. v = strmap_set(map, "K2", (void*)101);
  477. test_eq(v, NULL);
  478. v = strmap_set(map, "K1", (void*)100);
  479. test_eq(v, (void*)99);
  480. test_eq(strmap_get(map,"K1"), (void*)100);
  481. test_eq(strmap_get(map,"K2"), (void*)101);
  482. test_eq(strmap_get(map,"K-not-there"), NULL);
  483. v = strmap_remove(map,"K2");
  484. test_eq(v, (void*)101);
  485. test_eq(strmap_get(map,"K2"), NULL);
  486. test_eq(strmap_remove(map,"K2"), NULL);
  487. strmap_set(map, "K2", (void*)101);
  488. strmap_set(map, "K3", (void*)102);
  489. strmap_set(map, "K4", (void*)103);
  490. strmap_set(map, "K5", (void*)104);
  491. strmap_set(map, "K6", (void*)105);
  492. count = 0;
  493. strmap_foreach(map, _squareAndRemoveK4, &count);
  494. test_eq(count, 1);
  495. test_eq(strmap_get(map, "K4"), NULL);
  496. test_eq(strmap_get(map, "K1"), (void*)10000);
  497. test_eq(strmap_get(map, "K6"), (void*)11025);
  498. iter = strmap_iter_init(map);
  499. strmap_iter_get(iter,&k,&v);
  500. test_streq(k, "K1");
  501. test_eq(v, (void*)10000);
  502. iter = strmap_iter_next(map,iter);
  503. strmap_iter_get(iter,&k,&v);
  504. test_streq(k, "K2");
  505. test_eq(v, (void*)10201);
  506. iter = strmap_iter_next_rmv(map,iter);
  507. strmap_iter_get(iter,&k,&v);
  508. test_streq(k, "K3");
  509. test_eq(v, (void*)10404);
  510. iter = strmap_iter_next(map,iter); /* K5 */
  511. test_assert(!strmap_iter_done(iter));
  512. iter = strmap_iter_next(map,iter); /* K6 */
  513. test_assert(!strmap_iter_done(iter));
  514. iter = strmap_iter_next(map,iter); /* done */
  515. test_assert(strmap_iter_done(iter));
  516. /* Make sure we removed K2, but not the others. */
  517. test_eq(strmap_get(map, "K2"), NULL);
  518. test_eq(strmap_get(map, "K5"), (void*)10816);
  519. /* Clean up after ourselves. */
  520. strmap_free(map, NULL);
  521. /* Now try some lc functions. */
  522. map = strmap_new();
  523. strmap_set_lc(map,"Ab.C", (void*)1);
  524. test_eq(strmap_get(map,"ab.c"), (void*)1);
  525. test_eq(strmap_get_lc(map,"AB.C"), (void*)1);
  526. test_eq(strmap_get(map,"AB.C"), NULL);
  527. test_eq(strmap_remove_lc(map,"aB.C"), (void*)1);
  528. test_eq(strmap_get_lc(map,"AB.C"), NULL);
  529. strmap_free(map,NULL);
  530. }
  531. void test_onion() {
  532. #if 0
  533. char **names;
  534. int i,num;
  535. names = parse_nickname_list(" foo bar\t baz quux ", &num);
  536. test_eq(num,4);
  537. test_streq(names[0],"foo");
  538. test_streq(names[1],"bar");
  539. test_streq(names[2],"baz");
  540. test_streq(names[3],"quux");
  541. for(i=0;i<num;i++)
  542. tor_free(names[i]);
  543. tor_free(names);
  544. #endif
  545. }
  546. void
  547. test_onion_handshake() {
  548. /* client-side */
  549. crypto_dh_env_t *c_dh = NULL;
  550. char c_buf[ONIONSKIN_CHALLENGE_LEN];
  551. char c_keys[40];
  552. /* server-side */
  553. char s_buf[ONIONSKIN_REPLY_LEN];
  554. char s_keys[40];
  555. /* shared */
  556. crypto_pk_env_t *pk = NULL;
  557. pk = crypto_new_pk_env();
  558. test_assert(! crypto_pk_generate_key(pk));
  559. /* client handshake 1. */
  560. memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
  561. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  562. /* server handshake */
  563. memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
  564. memset(s_keys, 0, 40);
  565. test_assert(! onion_skin_server_handshake(c_buf, pk, s_buf, s_keys, 40));
  566. /* client handshake 2 */
  567. memset(c_keys, 0, 40);
  568. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  569. crypto_dh_free(c_dh);
  570. if (memcmp(c_keys, s_keys, 40)) {
  571. puts("Aiiiie");
  572. exit(1);
  573. }
  574. test_memeq(c_keys, s_keys, 40);
  575. memset(s_buf, 0, 40);
  576. test_memneq(c_keys, s_buf, 40);
  577. crypto_free_pk_env(pk);
  578. }
  579. /* from routers.c */
  580. int is_recommended_version(char *myversion, char *start);
  581. void
  582. test_dir_format()
  583. {
  584. char buf[8192], buf2[8192];
  585. char platform[256];
  586. char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
  587. int pk1_str_len, pk2_str_len, pk3_str_len;
  588. routerinfo_t r1, r2;
  589. crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
  590. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  591. struct exit_policy_t ex1, ex2;
  592. routerlist_t *dir1 = NULL, *dir2 = NULL;
  593. test_assert( (pk1 = crypto_new_pk_env()) );
  594. test_assert( (pk2 = crypto_new_pk_env()) );
  595. test_assert( (pk3 = crypto_new_pk_env()) );
  596. test_assert(! crypto_pk_generate_key(pk1));
  597. test_assert(! crypto_pk_generate_key(pk2));
  598. test_assert(! crypto_pk_generate_key(pk3));
  599. get_platform_str(platform, sizeof(platform));
  600. r1.address = "testaddr1.foo.bar";
  601. r1.addr = 0xc0a80001u; /* 192.168.0.1 */
  602. r1.published_on = 0;
  603. r1.or_port = 9000;
  604. r1.socks_port = 9002;
  605. r1.dir_port = 9003;
  606. r1.onion_pkey = pk1;
  607. r1.identity_pkey = pk2;
  608. r1.link_pkey = pk3;
  609. r1.bandwidthrate = r1.bandwidthburst = 1000;
  610. r1.exit_policy = NULL;
  611. r1.nickname = "Magri";
  612. r1.platform = tor_strdup(platform);
  613. ex1.policy_type = EXIT_POLICY_ACCEPT;
  614. ex1.string = NULL;
  615. ex1.addr = 0;
  616. ex1.msk = 0;
  617. ex1.prt_min = ex1.prt_max = 80;
  618. ex1.next = &ex2;
  619. ex2.policy_type = EXIT_POLICY_REJECT;
  620. ex2.addr = 18 << 24;
  621. ex2.msk = 0xFF000000u;
  622. ex2.prt_min = ex1.prt_max = 24;
  623. ex2.next = NULL;
  624. r2.address = "tor.tor.tor";
  625. r2.addr = 0x0a030201u; /* 10.3.2.1 */
  626. r2.published_on = 5;
  627. r2.or_port = 9005;
  628. r2.socks_port = 0;
  629. r2.dir_port = 0;
  630. r2.onion_pkey = pk2;
  631. r2.identity_pkey = pk1;
  632. r2.link_pkey = pk2;
  633. r2.bandwidthrate = r2.bandwidthburst = 3000;
  634. r2.exit_policy = &ex1;
  635. r2.nickname = "Fred";
  636. test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  637. &pk1_str_len));
  638. test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  639. &pk2_str_len));
  640. test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
  641. &pk3_str_len));
  642. memset(buf, 0, 2048);
  643. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  644. strcpy(buf2, "router Magri testaddr1.foo.bar 9000 9002 9003 1000\n"
  645. "platform Tor "VERSION" on ");
  646. strcat(buf2, get_uname());
  647. strcat(buf2, "\n"
  648. "published 1970-01-01 00:00:00\n"
  649. "onion-key\n");
  650. strcat(buf2, pk1_str);
  651. strcat(buf2, "link-key\n");
  652. strcat(buf2, pk3_str);
  653. strcat(buf2, "signing-key\n");
  654. strcat(buf2, pk2_str);
  655. strcat(buf2, "router-signature\n");
  656. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same twice*/
  657. test_streq(buf, buf2);
  658. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  659. cp = buf;
  660. rp1 = router_get_entry_from_string((const char*)cp,NULL);
  661. test_assert(rp1);
  662. test_streq(rp1->address, r1.address);
  663. test_eq(rp1->or_port, r1.or_port);
  664. test_eq(rp1->socks_port, r1.socks_port);
  665. test_eq(rp1->dir_port, r1.dir_port);
  666. test_eq(rp1->bandwidthrate, r1.bandwidthrate);
  667. // test_eq(rp1->bandwidthburst, r1.bandwidthburst);
  668. test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  669. test_assert(crypto_pk_cmp_keys(rp1->link_pkey, pk3) == 0);
  670. test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  671. test_assert(rp1->exit_policy == NULL);
  672. #if 0
  673. /* XXX Once we have exit policies, test this again. XXX */
  674. strcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n");
  675. strcat(buf2, pk2_str);
  676. strcat(buf2, "signing-key\n");
  677. strcat(buf2, pk1_str);
  678. strcat(buf2, "accept *:80\nreject 18.*:24\n\n");
  679. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
  680. test_streq(buf, buf2);
  681. cp = buf;
  682. rp2 = router_get_entry_from_string(&cp);
  683. test_assert(rp2);
  684. test_streq(rp2->address, r2.address);
  685. test_eq(rp2->or_port, r2.or_port);
  686. test_eq(rp2->socks_port, r2.socks_port);
  687. test_eq(rp2->dir_port, r2.dir_port);
  688. test_eq(rp2->bandwidth, r2.bandwidth);
  689. test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  690. test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  691. test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
  692. test_streq(rp2->exit_policy->string, "accept *:80");
  693. test_streq(rp2->exit_policy->address, "*");
  694. test_streq(rp2->exit_policy->port, "80");
  695. test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
  696. test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
  697. test_streq(rp2->exit_policy->next->address, "18.*");
  698. test_streq(rp2->exit_policy->next->port, "24");
  699. test_assert(rp2->exit_policy->next->next == NULL);
  700. #endif
  701. /* Okay, now for the directories. */
  702. crypto_pk_get_fingerprint(pk2, buf);
  703. add_fingerprint_to_dir("Magri", buf);
  704. crypto_pk_get_fingerprint(pk1, buf);
  705. add_fingerprint_to_dir("Fred", buf);
  706. /* Make sure routers aren't too far in the past any more. */
  707. r1.published_on = time(NULL);
  708. r2.published_on = time(NULL)-3*60*60;
  709. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  710. cp = buf;
  711. test_eq(dirserv_add_descriptor((const char**)&cp), 1);
  712. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk1)>0);
  713. cp = buf;
  714. test_eq(dirserv_add_descriptor((const char**)&cp), 1);
  715. options.Nickname = "DirServer";
  716. test_assert(!dirserv_dump_directory_to_string(buf,8192,pk3));
  717. cp = buf;
  718. test_assert(!router_get_routerlist_from_directory_impl(buf, &dir1, pk3));
  719. test_eq(2, smartlist_len(dir1->routers));
  720. dirserv_free_fingerprint_list();
  721. tor_free(pk1_str);
  722. tor_free(pk2_str);
  723. if (pk1) crypto_free_pk_env(pk1);
  724. if (pk2) crypto_free_pk_env(pk2);
  725. if (rp1) routerinfo_free(rp1);
  726. if (rp2) routerinfo_free(rp2);
  727. tor_free(dir1); /* XXXX And more !*/
  728. tor_free(dir2); /* And more !*/
  729. /* make sure is_recommended_version() works */
  730. test_eq(1, is_recommended_version("abc", "abc"));
  731. test_eq(1, is_recommended_version("abc", "ab,abd,abde,abc,abcde"));
  732. test_eq(1, is_recommended_version("abc", "ab,abd,abde,abcde,abc"));
  733. test_eq(1, is_recommended_version("abc", "abc,abd,abde,abc,abcde"));
  734. test_eq(1, is_recommended_version("a", "a,ab,abd,abde,abc,abcde"));
  735. test_eq(0, is_recommended_version("a", "ab,abd,abde,abc,abcde"));
  736. test_eq(0, is_recommended_version("abb", "ab,abd,abde,abc,abcde"));
  737. test_eq(0, is_recommended_version("a", ""));
  738. }
  739. void test_rend_fns()
  740. {
  741. char address1[] = "fooaddress.onion";
  742. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  743. rend_service_descriptor_t *d1, *d2;
  744. char *encoded;
  745. int len;
  746. crypto_pk_env_t *pk1;
  747. time_t now;
  748. pk1 = crypto_new_pk_env();
  749. test_assert(!crypto_pk_generate_key(pk1));
  750. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  751. d1->pk = pk1;
  752. now = time(NULL);
  753. d1->timestamp = now;
  754. d1->n_intro_points = 3;
  755. d1->intro_points = tor_malloc(sizeof(char*)*3);
  756. d1->intro_points[0] = tor_strdup("tom");
  757. d1->intro_points[1] = tor_strdup("crow");
  758. d1->intro_points[2] = tor_strdup("joel");
  759. test_assert(! rend_encode_service_descriptor(d1, pk1, &encoded, &len));
  760. d2 = rend_parse_service_descriptor(encoded, len);
  761. test_assert(d2);
  762. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  763. test_eq(d2->timestamp, now);
  764. test_eq(d2->n_intro_points, 3);
  765. test_streq(d2->intro_points[0], "tom");
  766. test_streq(d2->intro_points[1], "crow");
  767. test_streq(d2->intro_points[2], "joel");
  768. test_eq(-1, rend_parse_rendezvous_address(address1));
  769. test_eq( 0, rend_parse_rendezvous_address(address2));
  770. rend_service_descriptor_free(d1);
  771. rend_service_descriptor_free(d2);
  772. }
  773. int
  774. main(int c, char**v){
  775. #if 0
  776. or_options_t options; /* command-line and config-file options */
  777. if(getconfig(c,v,&options))
  778. exit(1);
  779. #endif
  780. crypto_seed_rng();
  781. setup_directory();
  782. // puts("========================== Buffers =========================");
  783. // test_buffers();
  784. puts("\n========================== Crypto ==========================");
  785. test_crypto();
  786. test_crypto_dh();
  787. puts("\n========================= Util ============================");
  788. test_util();
  789. test_strmap();
  790. puts("\n========================= Onion Skins =====================");
  791. test_onion();
  792. test_onion_handshake();
  793. puts("\n========================= Directory Formats ===============");
  794. // add_stream_log(LOG_DEBUG, NULL, stdout);
  795. test_dir_format();
  796. puts("\n========================= Rendezvous functionality ========");
  797. test_rend_fns();
  798. puts("");
  799. if (have_failed)
  800. return 1;
  801. else
  802. return 0;
  803. }
  804. /*
  805. Local Variables:
  806. mode:c
  807. indent-tabs-mode:nil
  808. c-basic-offset:2
  809. End:
  810. */