test.c 28 KB

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