test.c 24 KB

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