test.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2006 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char test_c_id[] =
  6. "$Id$";
  7. /**
  8. * \file test.c
  9. * \brief Unit tests for many pieces of the lower level Tor modules.
  10. **/
  11. #include "orconfig.h"
  12. #include <stdio.h>
  13. #ifdef HAVE_FCNTL_H
  14. #include <fcntl.h>
  15. #endif
  16. #ifdef MS_WINDOWS
  17. /* For mkdir() */
  18. #include <direct.h>
  19. #else
  20. #include <dirent.h>
  21. #endif
  22. #include "or.h"
  23. #include "../common/test.h"
  24. #include "../common/torgzip.h"
  25. int have_failed = 0;
  26. /* These functions are file-local, but are exposed so we can test. */
  27. void add_fingerprint_to_dir(const char *nickname, const char *fp,
  28. smartlist_t *list);
  29. void get_platform_str(char *platform, size_t len);
  30. size_t read_escaped_data(const char *data, size_t len, int translate_newlines,
  31. char **out);
  32. or_options_t *options_new(void);
  33. static char temp_dir[256];
  34. static void
  35. setup_directory(void)
  36. {
  37. static int is_setup = 0;
  38. int r;
  39. if (is_setup) return;
  40. #ifdef MS_WINDOWS
  41. // XXXX
  42. tor_snprintf(temp_dir, sizeof(temp_dir),
  43. "c:\\windows\\temp\\tor_test_%d", (int)getpid());
  44. r = mkdir(temp_dir);
  45. #else
  46. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
  47. r = mkdir(temp_dir, 0700);
  48. #endif
  49. if (r) {
  50. fprintf(stderr, "Can't create directory %s:", temp_dir);
  51. perror("");
  52. exit(1);
  53. }
  54. is_setup = 1;
  55. }
  56. static const char *
  57. get_fname(const char *name)
  58. {
  59. static char buf[1024];
  60. setup_directory();
  61. tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
  62. return buf;
  63. }
  64. static void
  65. remove_directory(void)
  66. {
  67. smartlist_t *elements = tor_listdir(temp_dir);
  68. if (elements) {
  69. SMARTLIST_FOREACH(elements, const char *, cp,
  70. {
  71. size_t len = strlen(cp)+strlen(temp_dir)+16;
  72. char *tmp = tor_malloc(len);
  73. tor_snprintf(tmp, len, "%s"PATH_SEPARATOR"%s", temp_dir, cp);
  74. unlink(tmp);
  75. tor_free(tmp);
  76. });
  77. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  78. smartlist_free(elements);
  79. }
  80. rmdir(temp_dir);
  81. }
  82. static void
  83. test_buffers(void)
  84. {
  85. char str[256];
  86. char str2[256];
  87. buf_t *buf;
  88. int j;
  89. /****
  90. * buf_new
  91. ****/
  92. if (!(buf = buf_new()))
  93. test_fail();
  94. test_eq(buf_capacity(buf), 4096);
  95. test_eq(buf_datalen(buf), 0);
  96. /****
  97. * General pointer frobbing
  98. */
  99. for (j=0;j<256;++j) {
  100. str[j] = (char)j;
  101. }
  102. write_to_buf(str, 256, buf);
  103. write_to_buf(str, 256, buf);
  104. test_eq(buf_datalen(buf), 512);
  105. fetch_from_buf(str2, 200, buf);
  106. test_memeq(str, str2, 200);
  107. test_eq(buf_datalen(buf), 312);
  108. memset(str2, 0, sizeof(str2));
  109. fetch_from_buf(str2, 256, buf);
  110. test_memeq(str+200, str2, 56);
  111. test_memeq(str, str2+56, 200);
  112. test_eq(buf_datalen(buf), 56);
  113. memset(str2, 0, sizeof(str2));
  114. /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
  115. * another 3584 bytes, we hit the end. */
  116. for (j=0;j<15;++j) {
  117. write_to_buf(str, 256, buf);
  118. }
  119. assert_buf_ok(buf);
  120. test_eq(buf_datalen(buf), 3896);
  121. fetch_from_buf(str2, 56, buf);
  122. test_eq(buf_datalen(buf), 3840);
  123. test_memeq(str+200, str2, 56);
  124. for (j=0;j<15;++j) {
  125. memset(str2, 0, sizeof(str2));
  126. fetch_from_buf(str2, 256, buf);
  127. test_memeq(str, str2, 256);
  128. }
  129. test_eq(buf_datalen(buf), 0);
  130. buf_free(buf);
  131. /* Okay, now make sure growing can work. */
  132. buf = buf_new_with_capacity(16);
  133. test_eq(buf_capacity(buf), 16);
  134. write_to_buf(str+1, 255, buf);
  135. test_eq(buf_capacity(buf), 256);
  136. fetch_from_buf(str2, 254, buf);
  137. test_memeq(str+1, str2, 254);
  138. test_eq(buf_capacity(buf), 256);
  139. assert_buf_ok(buf);
  140. write_to_buf(str, 32, buf);
  141. test_eq(buf_capacity(buf), 256);
  142. assert_buf_ok(buf);
  143. write_to_buf(str, 256, buf);
  144. assert_buf_ok(buf);
  145. test_eq(buf_capacity(buf), 512);
  146. test_eq(buf_datalen(buf), 33+256);
  147. fetch_from_buf(str2, 33, buf);
  148. test_eq(*str2, str[255]);
  149. test_memeq(str2+1, str, 32);
  150. test_eq(buf_capacity(buf), 512);
  151. test_eq(buf_datalen(buf), 256);
  152. fetch_from_buf(str2, 256, buf);
  153. test_memeq(str, str2, 256);
  154. /* now try shrinking: case 1. */
  155. buf_free(buf);
  156. buf = buf_new_with_capacity(33668);
  157. for (j=0;j<67;++j) {
  158. write_to_buf(str,255, buf);
  159. }
  160. test_eq(buf_capacity(buf), 33668);
  161. test_eq(buf_datalen(buf), 17085);
  162. for (j=0; j < 40; ++j) {
  163. fetch_from_buf(str2, 255,buf);
  164. test_memeq(str2, str, 255);
  165. }
  166. /* now try shrinking: case 2. */
  167. buf_free(buf);
  168. buf = buf_new_with_capacity(33668);
  169. for (j=0;j<67;++j) {
  170. write_to_buf(str,255, buf);
  171. }
  172. for (j=0; j < 20; ++j) {
  173. fetch_from_buf(str2, 255,buf);
  174. test_memeq(str2, str, 255);
  175. }
  176. for (j=0;j<80;++j) {
  177. write_to_buf(str,255, buf);
  178. }
  179. test_eq(buf_capacity(buf),33668);
  180. for (j=0; j < 120; ++j) {
  181. fetch_from_buf(str2, 255,buf);
  182. test_memeq(str2, str, 255);
  183. }
  184. #if 0
  185. /****
  186. * read_to_buf
  187. ****/
  188. s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
  189. write(s, str, 256);
  190. close(s);
  191. s = open(get_fname("data"), O_RDONLY, 0);
  192. eof = 0;
  193. errno = 0; /* XXXX */
  194. i = read_to_buf(s, 10, buf, &eof);
  195. printf("%s\n", strerror(errno));
  196. test_eq(i, 10);
  197. test_eq(eof, 0);
  198. test_eq(buf_capacity(buf), 4096);
  199. test_eq(buf_datalen(buf), 10);
  200. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
  201. /* Test reading 0 bytes. */
  202. i = read_to_buf(s, 0, buf, &eof);
  203. test_eq(buf_capacity(buf), 512*1024);
  204. test_eq(buf_datalen(buf), 10);
  205. test_eq(eof, 0);
  206. test_eq(i, 0);
  207. /* Now test when buffer is filled exactly. */
  208. buf2 = buf_new_with_capacity(6);
  209. i = read_to_buf(s, 6, buf2, &eof);
  210. test_eq(buf_capacity(buf2), 6);
  211. test_eq(buf_datalen(buf2), 6);
  212. test_eq(eof, 0);
  213. test_eq(i, 6);
  214. test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
  215. buf_free(buf2);
  216. /* Now test when buffer is filled with more data to read. */
  217. buf2 = buf_new_with_capacity(32);
  218. i = read_to_buf(s, 128, buf2, &eof);
  219. test_eq(buf_capacity(buf2), 128);
  220. test_eq(buf_datalen(buf2), 32);
  221. test_eq(eof, 0);
  222. test_eq(i, 32);
  223. buf_free(buf2);
  224. /* Now read to eof. */
  225. test_assert(buf_capacity(buf) > 256);
  226. i = read_to_buf(s, 1024, buf, &eof);
  227. test_eq(i, (256-32-10-6));
  228. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  229. test_eq(buf_datalen(buf), 256-6-32);
  230. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
  231. test_eq(eof, 0);
  232. i = read_to_buf(s, 1024, buf, &eof);
  233. test_eq(i, 0);
  234. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  235. test_eq(buf_datalen(buf), 256-6-32);
  236. test_eq(eof, 1);
  237. #endif
  238. buf_free(buf);
  239. }
  240. static void
  241. test_crypto_dh(void)
  242. {
  243. crypto_dh_env_t *dh1, *dh2;
  244. char p1[DH_BYTES];
  245. char p2[DH_BYTES];
  246. char s1[DH_BYTES];
  247. char s2[DH_BYTES];
  248. int s1len, s2len;
  249. dh1 = crypto_dh_new();
  250. dh2 = crypto_dh_new();
  251. test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
  252. test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
  253. memset(p1, 0, DH_BYTES);
  254. memset(p2, 0, DH_BYTES);
  255. test_memeq(p1, p2, DH_BYTES);
  256. test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  257. test_memneq(p1, p2, DH_BYTES);
  258. test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  259. test_memneq(p1, p2, DH_BYTES);
  260. memset(s1, 0, DH_BYTES);
  261. memset(s2, 0xFF, DH_BYTES);
  262. s1len = crypto_dh_compute_secret(dh1, p2, DH_BYTES, s1, 50);
  263. s2len = crypto_dh_compute_secret(dh2, p1, DH_BYTES, s2, 50);
  264. test_assert(s1len > 0);
  265. test_eq(s1len, s2len);
  266. test_memeq(s1, s2, s1len);
  267. crypto_dh_free(dh1);
  268. crypto_dh_free(dh2);
  269. }
  270. static void
  271. test_crypto(void)
  272. {
  273. crypto_cipher_env_t *env1, *env2;
  274. crypto_pk_env_t *pk1, *pk2;
  275. char *data1, *data2, *data3, *cp;
  276. int i, j, p, len;
  277. size_t size;
  278. data1 = tor_malloc(1024);
  279. data2 = tor_malloc(1024);
  280. data3 = tor_malloc(1024);
  281. test_assert(data1 && data2 && data3);
  282. /* Try out RNG. */
  283. test_assert(! crypto_seed_rng());
  284. crypto_rand(data1, 100);
  285. crypto_rand(data2, 100);
  286. test_memneq(data1,data2,100);
  287. #if 0
  288. /* Try out identity ciphers. */
  289. env1 = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  290. test_neq(env1, 0);
  291. test_eq(crypto_cipher_generate_key(env1), 0);
  292. test_eq(crypto_cipher_encrypt_init_cipher(env1), 0);
  293. for (i = 0; i < 1024; ++i) {
  294. data1[i] = (char) i*73;
  295. }
  296. crypto_cipher_encrypt(env1, data2, data1, 1024);
  297. test_memeq(data1, data2, 1024);
  298. crypto_free_cipher_env(env1);
  299. #endif
  300. /* Now, test encryption and decryption with stream cipher. */
  301. data1[0]='\0';
  302. for (i = 1023; i>0; i -= 35)
  303. strncat(data1, "Now is the time for all good onions", i);
  304. memset(data2, 0, 1024);
  305. memset(data3, 0, 1024);
  306. env1 = crypto_new_cipher_env();
  307. test_neq(env1, 0);
  308. env2 = crypto_new_cipher_env();
  309. test_neq(env2, 0);
  310. j = crypto_cipher_generate_key(env1);
  311. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  312. crypto_cipher_encrypt_init_cipher(env1);
  313. crypto_cipher_decrypt_init_cipher(env2);
  314. /* Try encrypting 512 chars. */
  315. crypto_cipher_encrypt(env1, data2, data1, 512);
  316. crypto_cipher_decrypt(env2, data3, data2, 512);
  317. test_memeq(data1, data3, 512);
  318. test_memneq(data1, data2, 512);
  319. /* Now encrypt 1 at a time, and get 1 at a time. */
  320. for (j = 512; j < 560; ++j) {
  321. crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
  322. }
  323. for (j = 512; j < 560; ++j) {
  324. crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
  325. }
  326. test_memeq(data1, data3, 560);
  327. /* Now encrypt 3 at a time, and get 5 at a time. */
  328. for (j = 560; j < 1024-5; j += 3) {
  329. crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
  330. }
  331. for (j = 560; j < 1024-5; j += 5) {
  332. crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
  333. }
  334. test_memeq(data1, data3, 1024-5);
  335. /* Now make sure that when we encrypt with different chunk sizes, we get
  336. the same results. */
  337. crypto_free_cipher_env(env2);
  338. memset(data3, 0, 1024);
  339. env2 = crypto_new_cipher_env();
  340. test_neq(env2, 0);
  341. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  342. crypto_cipher_encrypt_init_cipher(env2);
  343. for (j = 0; j < 1024-16; j += 17) {
  344. crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
  345. }
  346. for (j= 0; j < 1024-16; ++j) {
  347. if (data2[j] != data3[j]) {
  348. printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
  349. }
  350. }
  351. test_memeq(data2, data3, 1024-16);
  352. crypto_free_cipher_env(env1);
  353. crypto_free_cipher_env(env2);
  354. /* Test vectors for stream ciphers. */
  355. /* XXXX Look up some test vectors for the ciphers and make sure we match. */
  356. /* Test SHA-1 with a test vector from the specification. */
  357. i = crypto_digest(data1, "abc", 3);
  358. test_memeq(data1,
  359. "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
  360. "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
  361. /* Public-key ciphers */
  362. pk1 = crypto_new_pk_env();
  363. pk2 = crypto_new_pk_env();
  364. test_assert(pk1 && pk2);
  365. test_assert(! crypto_pk_generate_key(pk1));
  366. test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &size));
  367. test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, size));
  368. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  369. tor_free(cp);
  370. test_eq(128, crypto_pk_keysize(pk1));
  371. test_eq(128, crypto_pk_keysize(pk2));
  372. test_eq(128, crypto_pk_public_encrypt(pk2, data1, "Hello whirled.", 15,
  373. PK_PKCS1_OAEP_PADDING));
  374. test_eq(128, crypto_pk_public_encrypt(pk1, data2, "Hello whirled.", 15,
  375. PK_PKCS1_OAEP_PADDING));
  376. /* oaep padding should make encryption not match */
  377. test_memneq(data1, data2, 128);
  378. test_eq(15, crypto_pk_private_decrypt(pk1, data3, data1, 128,
  379. PK_PKCS1_OAEP_PADDING,1));
  380. test_streq(data3, "Hello whirled.");
  381. memset(data3, 0, 1024);
  382. test_eq(15, crypto_pk_private_decrypt(pk1, data3, data2, 128,
  383. PK_PKCS1_OAEP_PADDING,1));
  384. test_streq(data3, "Hello whirled.");
  385. /* Can't decrypt with public key. */
  386. test_eq(-1, crypto_pk_private_decrypt(pk2, data3, data2, 128,
  387. PK_PKCS1_OAEP_PADDING,1));
  388. /* Try again with bad padding */
  389. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  390. test_eq(-1, crypto_pk_private_decrypt(pk1, data3, data2, 128,
  391. PK_PKCS1_OAEP_PADDING,1));
  392. /* File operations: save and load private key */
  393. test_assert(! crypto_pk_write_private_key_to_filename(pk1,
  394. get_fname("pkey1")));
  395. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  396. get_fname("pkey1")));
  397. test_eq(15, crypto_pk_private_decrypt(pk2, data3, data1, 128,
  398. PK_PKCS1_OAEP_PADDING,1));
  399. /* Now try signing. */
  400. strcpy(data1, "Ossifrage");
  401. test_eq(128, crypto_pk_private_sign(pk1, data2, data1, 10));
  402. test_eq(10, crypto_pk_public_checksig(pk1, data3, data2, 128));
  403. test_streq(data3, "Ossifrage");
  404. /* Try signing digests. */
  405. test_eq(128, crypto_pk_private_sign_digest(pk1, data2, data1, 10));
  406. test_eq(20, crypto_pk_public_checksig(pk1, data3, data2, 128));
  407. test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  408. test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  409. /*XXXX test failed signing*/
  410. /* Try encoding */
  411. crypto_free_pk_env(pk2);
  412. pk2 = NULL;
  413. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  414. test_assert(i>0);
  415. pk2 = crypto_pk_asn1_decode(data1, i);
  416. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  417. /* Try with hybrid encryption wrappers. */
  418. crypto_rand(data1, 1024);
  419. for (i = 0; i < 3; ++i) {
  420. for (j = 85; j < 140; ++j) {
  421. memset(data2,0,1024);
  422. memset(data3,0,1024);
  423. if (i == 0 && j < 129)
  424. continue;
  425. p = (i==0)?PK_NO_PADDING:
  426. (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
  427. len = crypto_pk_public_hybrid_encrypt(pk1,data2,data1,j,p,0);
  428. test_assert(len>=0);
  429. len = crypto_pk_private_hybrid_decrypt(pk1,data3,data2,len,p,1);
  430. test_eq(len,j);
  431. test_memeq(data1,data3,j);
  432. }
  433. }
  434. crypto_free_pk_env(pk1);
  435. crypto_free_pk_env(pk2);
  436. /* Base64 tests */
  437. strcpy(data1, "Test string that contains 35 chars.");
  438. strcat(data1, " 2nd string that contains 35 chars.");
  439. i = base64_encode(data2, 1024, data1, 71);
  440. j = base64_decode(data3, 1024, data2, i);
  441. test_streq(data3, data1);
  442. test_eq(j, 71);
  443. test_assert(data2[i] == '\0');
  444. crypto_rand(data1, DIGEST_LEN);
  445. memset(data2, 100, 1024);
  446. digest_to_base64(data2, data1);
  447. test_eq(BASE64_DIGEST_LEN, strlen(data2));
  448. test_eq(100, data2[BASE64_DIGEST_LEN+2]);
  449. memset(data3, 99, 1024);
  450. digest_from_base64(data3, data2);
  451. test_memeq(data1, data3, DIGEST_LEN);
  452. test_eq(99, data3[DIGEST_LEN+1]);
  453. /* Base32 tests */
  454. strcpy(data1, "5chrs");
  455. /* bit pattern is: [35 63 68 72 73] ->
  456. * [00110101 01100011 01101000 01110010 01110011]
  457. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  458. */
  459. base32_encode(data2, 9, data1, 5);
  460. test_streq(data2, "gvrwq4tt");
  461. strcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4");
  462. base32_encode(data2, 30, data1, 10);
  463. test_streq(data2, "772w2rfobvomsywe");
  464. /* Base16 tests */
  465. strcpy(data1, "6chrs\xff");
  466. base16_encode(data2, 13, data1, 6);
  467. test_streq(data2, "3663687273FF");
  468. strcpy(data1, "f0d678affc000100");
  469. i = base16_decode(data2, 8, data1, 16);
  470. test_eq(i,0);
  471. test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  472. tor_free(data1);
  473. tor_free(data2);
  474. tor_free(data3);
  475. }
  476. static void
  477. test_crypto_s2k(void)
  478. {
  479. char buf[29];
  480. char buf2[29];
  481. char *buf3;
  482. int i;
  483. memset(buf, 0, sizeof(buf));
  484. memset(buf2, 0, sizeof(buf2));
  485. buf3 = tor_malloc(65536);
  486. memset(buf3, 0, 65536);
  487. secret_to_key(buf+9, 20, "", 0, buf);
  488. crypto_digest(buf2+9, buf3, 1024);
  489. test_memeq(buf, buf2, 29);
  490. memcpy(buf,"vrbacrda",8);
  491. memcpy(buf2,"vrbacrda",8);
  492. buf[8] = 96;
  493. buf2[8] = 96;
  494. secret_to_key(buf+9, 20, "12345678", 8, buf);
  495. for (i = 0; i < 65536; i += 16) {
  496. memcpy(buf3+i, "vrbacrda12345678", 16);
  497. }
  498. crypto_digest(buf2+9, buf3, 65536);
  499. test_memeq(buf, buf2, 29);
  500. }
  501. static int
  502. _compare_strs(const void **a, const void **b)
  503. {
  504. const char *s1 = *a, *s2 = *b;
  505. return strcmp(s1, s2);
  506. }
  507. static int
  508. _compare_without_first_ch(const void *a, const void **b)
  509. {
  510. const char *s1 = a, *s2 = *b;
  511. return strcasecmp(s1+1, s2);
  512. }
  513. static void
  514. test_util(void)
  515. {
  516. struct timeval start, end;
  517. struct tm a_time;
  518. smartlist_t *sl;
  519. char timestr[RFC1123_TIME_LEN+1];
  520. char buf[1024];
  521. time_t t_res;
  522. int i;
  523. uint32_t u32;
  524. uint16_t u16;
  525. char *cp, *k, *v;
  526. start.tv_sec = 5;
  527. start.tv_usec = 5000;
  528. end.tv_sec = 5;
  529. end.tv_usec = 5000;
  530. test_eq(0L, tv_udiff(&start, &end));
  531. end.tv_usec = 7000;
  532. test_eq(2000L, tv_udiff(&start, &end));
  533. end.tv_sec = 6;
  534. test_eq(1002000L, tv_udiff(&start, &end));
  535. end.tv_usec = 0;
  536. test_eq(995000L, tv_udiff(&start, &end));
  537. end.tv_sec = 4;
  538. test_eq(-1005000L, tv_udiff(&start, &end));
  539. /* The test values here are confirmed to be correct on a platform
  540. * with a working timegm. */
  541. a_time.tm_year = 2003-1900;
  542. a_time.tm_mon = 7;
  543. a_time.tm_mday = 30;
  544. a_time.tm_hour = 6;
  545. a_time.tm_min = 14;
  546. a_time.tm_sec = 55;
  547. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  548. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  549. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  550. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  551. a_time.tm_mday = 10;
  552. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  553. format_rfc1123_time(timestr, 0);
  554. test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
  555. format_rfc1123_time(timestr, (time_t)1091580502UL);
  556. test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
  557. t_res = 0;
  558. i = parse_rfc1123_time(timestr, &t_res);
  559. test_eq(i,0);
  560. test_eq(t_res, (time_t)1091580502UL);
  561. /* Test smartlist */
  562. sl = smartlist_create();
  563. smartlist_add(sl, (void*)1);
  564. smartlist_add(sl, (void*)2);
  565. smartlist_add(sl, (void*)3);
  566. smartlist_add(sl, (void*)4);
  567. smartlist_del_keeporder(sl, 1);
  568. smartlist_insert(sl, 1, (void*)22);
  569. smartlist_insert(sl, 0, (void*)0);
  570. smartlist_insert(sl, 5, (void*)555);
  571. test_eq_ptr((void*)0, smartlist_get(sl,0));
  572. test_eq_ptr((void*)1, smartlist_get(sl,1));
  573. test_eq_ptr((void*)22, smartlist_get(sl,2));
  574. test_eq_ptr((void*)3, smartlist_get(sl,3));
  575. test_eq_ptr((void*)4, smartlist_get(sl,4));
  576. test_eq_ptr((void*)555, smartlist_get(sl,5));
  577. smartlist_clear(sl);
  578. smartlist_split_string(sl, "abc", ":", 0, 0);
  579. test_eq(1, smartlist_len(sl));
  580. test_streq("abc", smartlist_get(sl, 0));
  581. smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  582. test_eq(4, smartlist_len(sl));
  583. test_streq("a", smartlist_get(sl, 1));
  584. test_streq("bc", smartlist_get(sl, 2));
  585. test_streq("", smartlist_get(sl, 3));
  586. cp = smartlist_join_strings(sl, "", 0, NULL);
  587. test_streq(cp, "abcabc");
  588. tor_free(cp);
  589. cp = smartlist_join_strings(sl, "!", 0, NULL);
  590. test_streq(cp, "abc!a!bc!");
  591. tor_free(cp);
  592. cp = smartlist_join_strings(sl, "XY", 0, NULL);
  593. test_streq(cp, "abcXYaXYbcXY");
  594. tor_free(cp);
  595. cp = smartlist_join_strings(sl, "XY", 1, NULL);
  596. test_streq(cp, "abcXYaXYbcXYXY");
  597. tor_free(cp);
  598. cp = smartlist_join_strings(sl, "", 1, NULL);
  599. test_streq(cp, "abcabc");
  600. tor_free(cp);
  601. smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
  602. test_eq(8, smartlist_len(sl));
  603. test_streq("", smartlist_get(sl, 4));
  604. test_streq("def", smartlist_get(sl, 5));
  605. test_streq(" ", smartlist_get(sl, 6));
  606. test_streq("ghijk", smartlist_get(sl, 7));
  607. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  608. smartlist_clear(sl);
  609. smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  610. test_eq(3, smartlist_len(sl));
  611. test_streq("a", smartlist_get(sl,0));
  612. test_streq("bbd", smartlist_get(sl,1));
  613. test_streq("cdef", smartlist_get(sl,2));
  614. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  615. SPLIT_SKIP_SPACE, 0);
  616. test_eq(8, smartlist_len(sl));
  617. test_streq("z", smartlist_get(sl,3));
  618. test_streq("zhasd", smartlist_get(sl,4));
  619. test_streq("", smartlist_get(sl,5));
  620. test_streq("bnud", smartlist_get(sl,6));
  621. test_streq("", smartlist_get(sl,7));
  622. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  623. smartlist_clear(sl);
  624. smartlist_split_string(sl, " ab\tc \td ef ", NULL,
  625. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  626. test_eq(4, smartlist_len(sl));
  627. test_streq("ab", smartlist_get(sl,0));
  628. test_streq("c", smartlist_get(sl,1));
  629. test_streq("d", smartlist_get(sl,2));
  630. test_streq("ef", smartlist_get(sl,3));
  631. smartlist_split_string(sl, "ghi\tj", NULL,
  632. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  633. test_eq(6, smartlist_len(sl));
  634. test_streq("ghi", smartlist_get(sl,4));
  635. test_streq("j", smartlist_get(sl,5));
  636. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  637. smartlist_clear(sl);
  638. cp = smartlist_join_strings(sl, "XY", 0, NULL);
  639. test_streq(cp, "");
  640. tor_free(cp);
  641. cp = smartlist_join_strings(sl, "XY", 1, NULL);
  642. test_streq(cp, "XY");
  643. tor_free(cp);
  644. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  645. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  646. test_eq(3, smartlist_len(sl));
  647. test_streq("z", smartlist_get(sl, 0));
  648. test_streq("zhasd", smartlist_get(sl, 1));
  649. test_streq("bnud", smartlist_get(sl, 2));
  650. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  651. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  652. test_eq(5, smartlist_len(sl));
  653. test_streq("z", smartlist_get(sl, 3));
  654. test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
  655. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  656. smartlist_clear(sl);
  657. smartlist_split_string(sl, "abcd\n", "\n",
  658. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  659. test_eq(1, smartlist_len(sl));
  660. test_streq("abcd", smartlist_get(sl, 0));
  661. smartlist_split_string(sl, "efgh", "\n",
  662. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  663. test_eq(2, smartlist_len(sl));
  664. test_streq("efgh", smartlist_get(sl, 1));
  665. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  666. smartlist_clear(sl);
  667. /* Test smartlist sorting. */
  668. smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
  669. test_eq(7, smartlist_len(sl));
  670. smartlist_sort(sl, _compare_strs);
  671. cp = smartlist_join_strings(sl, ",", 0, NULL);
  672. test_streq(cp,"and,arma,by,nickm,onion,router,the");
  673. tor_free(cp);
  674. test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
  675. _compare_without_first_ch));
  676. test_streq("and", smartlist_bsearch(sl, " AND", _compare_without_first_ch));
  677. test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", _compare_without_first_ch));
  678. /* Test tor_strstrip() */
  679. strcpy(buf, "Testing 1 2 3");
  680. test_eq(0, tor_strstrip(buf, ",!"));
  681. test_streq(buf, "Testing 1 2 3");
  682. strcpy(buf, "!Testing 1 2 3?");
  683. test_eq(5, tor_strstrip(buf, "!? "));
  684. test_streq(buf, "Testing123");
  685. /* Test tor_strpartition() */
  686. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
  687. TERMINATE_IF_EVEN));
  688. test_streq(buf, "abc##def##g");
  689. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
  690. ALWAYS_TERMINATE));
  691. test_streq(buf, "abc##def##g##");
  692. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
  693. TERMINATE_IF_EVEN));
  694. test_streq(buf, "abc##def##ghi##");
  695. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
  696. NEVER_TERMINATE));
  697. test_streq(buf, "abc##def##ghi");
  698. /* Test parse_addr_port */
  699. cp = NULL; u32 = 3; u16 = 3;
  700. test_assert(!parse_addr_port("1.2.3.4", &cp, &u32, &u16));
  701. test_streq(cp, "1.2.3.4");
  702. test_eq(u32, 0x01020304u);
  703. test_eq(u16, 0);
  704. tor_free(cp);
  705. test_assert(!parse_addr_port("4.3.2.1:99", &cp, &u32, &u16));
  706. test_streq(cp, "4.3.2.1");
  707. test_eq(u32, 0x04030201u);
  708. test_eq(u16, 99);
  709. tor_free(cp);
  710. test_assert(!parse_addr_port("nonexistent.address:4040", &cp, NULL, &u16));
  711. test_streq(cp, "nonexistent.address");
  712. test_eq(u16, 4040);
  713. tor_free(cp);
  714. test_assert(!parse_addr_port("localhost:9999", &cp, &u32, &u16));
  715. test_streq(cp, "localhost");
  716. test_eq(u32, 0x7f000001u);
  717. test_eq(u16, 9999);
  718. tor_free(cp);
  719. u32 = 3;
  720. test_assert(!parse_addr_port("localhost", NULL, &u32, &u16));
  721. test_eq(cp, NULL);
  722. test_eq(u32, 0x7f000001u);
  723. test_eq(u16, 0);
  724. tor_free(cp);
  725. test_eq(0, addr_mask_get_bits(0x0u));
  726. test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
  727. test_eq(16, addr_mask_get_bits(0xFFFF0000u));
  728. test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
  729. test_eq(1, addr_mask_get_bits(0x80000000u));
  730. /* Test tor_parse_long. */
  731. test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
  732. test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
  733. /* Test parse_line_from_str */
  734. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  735. "k2\n"
  736. "k3 \n" "\n" " \n" "#comment\n"
  737. "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf));
  738. cp = buf;
  739. cp = parse_line_from_str(cp, &k, &v);
  740. test_streq(k, "k");
  741. test_streq(v, "v");
  742. test_assert(!strcmpstart(cp, " key value with"));
  743. cp = parse_line_from_str(cp, &k, &v);
  744. test_streq(k, "key");
  745. test_streq(v, "value with spaces");
  746. test_assert(!strcmpstart(cp, "keykey"));
  747. cp = parse_line_from_str(cp, &k, &v);
  748. test_streq(k, "keykey");
  749. test_streq(v, "val");
  750. test_assert(!strcmpstart(cp, "k2\n"));
  751. cp = parse_line_from_str(cp, &k, &v);
  752. test_streq(k, "k2");
  753. test_streq(v, "");
  754. test_assert(!strcmpstart(cp, "k3 \n"));
  755. cp = parse_line_from_str(cp, &k, &v);
  756. test_streq(k, "k3");
  757. test_streq(v, "");
  758. test_assert(!strcmpstart(cp, "\n \n"));
  759. cp = parse_line_from_str(cp, &k, &v);
  760. test_streq(k, "k4");
  761. test_streq(v, "");
  762. test_assert(!strcmpstart(cp, "k5#abc"));
  763. cp = parse_line_from_str(cp, &k, &v);
  764. test_streq(k, "k5");
  765. test_streq(v, "");
  766. test_assert(!strcmpstart(cp, "k6"));
  767. cp = parse_line_from_str(cp, &k, &v);
  768. test_streq(k, "k6");
  769. test_streq(v, "val");
  770. test_streq(cp, "");
  771. /* Test for strcmpstart and strcmpend. */
  772. test_assert(strcmpstart("abcdef", "abcdef")==0);
  773. test_assert(strcmpstart("abcdef", "abc")==0);
  774. test_assert(strcmpstart("abcdef", "abd")<0);
  775. test_assert(strcmpstart("abcdef", "abb")>0);
  776. test_assert(strcmpstart("ab", "abb")<0);
  777. test_assert(strcmpend("abcdef", "abcdef")==0);
  778. test_assert(strcmpend("abcdef", "def")==0);
  779. test_assert(strcmpend("abcdef", "deg")<0);
  780. test_assert(strcmpend("abcdef", "dee")>0);
  781. test_assert(strcmpend("ab", "abb")<0);
  782. {
  783. char tmpbuf[INET_NTOA_BUF_LEN];
  784. struct in_addr in;
  785. tor_inet_aton("18.244.0.188",&in);
  786. tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
  787. test_streq(tmpbuf, "18.244.0.188");
  788. }
  789. /* Test 'escaped' */
  790. test_streq("\"\"", escaped(""));
  791. test_streq("\"abcd\"", escaped("abcd"));
  792. test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
  793. test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
  794. /* XXXX test older functions. */
  795. smartlist_free(sl);
  796. }
  797. static void
  798. test_gzip(void)
  799. {
  800. char *buf1, *buf2=NULL, *buf3=NULL;
  801. size_t len1, len2;
  802. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
  803. test_eq(detect_compression_method(buf1, strlen(buf1)), 0);
  804. if (is_gzip_supported()) {
  805. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  806. GZIP_METHOD));
  807. test_assert(buf2);
  808. test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
  809. test_eq(detect_compression_method(buf2, len1), GZIP_METHOD);
  810. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  811. GZIP_METHOD, 1, LOG_INFO));
  812. test_assert(buf3);
  813. test_streq(buf1,buf3);
  814. tor_free(buf2);
  815. tor_free(buf3);
  816. }
  817. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  818. ZLIB_METHOD));
  819. test_assert(buf2);
  820. test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
  821. test_eq(detect_compression_method(buf2, len1), ZLIB_METHOD);
  822. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  823. ZLIB_METHOD, 1, LOG_INFO));
  824. test_assert(buf3);
  825. test_streq(buf1,buf3);
  826. /* Check whether we can uncompress concatenated, compresed strings. */
  827. tor_free(buf3);
  828. buf2 = tor_realloc(buf2, len1*2);
  829. memcpy(buf2+len1, buf2, len1);
  830. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
  831. ZLIB_METHOD, 1, LOG_INFO));
  832. test_eq(len2, (strlen(buf1)+1)*2);
  833. test_memeq(buf3,
  834. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
  835. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
  836. (strlen(buf1)+1)*2);
  837. tor_free(buf1);
  838. tor_free(buf2);
  839. tor_free(buf3);
  840. /* Check whether we can uncompress partial strings. */
  841. buf1 =
  842. tor_strdup("String with low redundancy that won't be compressed much.");
  843. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  844. ZLIB_METHOD));
  845. tor_assert(len1>16);
  846. /* when we allow an uncomplete string, we should succeed.*/
  847. tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  848. ZLIB_METHOD, 0, LOG_INFO));
  849. buf3[len2]='\0';
  850. tor_assert(len2 > 5);
  851. tor_assert(!strcmpstart(buf1, buf3));
  852. /* when we demand a complete string, this must fail. */
  853. tor_free(buf3);
  854. tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  855. ZLIB_METHOD, 1, LOG_INFO));
  856. tor_assert(!buf3);
  857. tor_free(buf2);
  858. tor_free(buf3);
  859. tor_free(buf1);
  860. }
  861. static void
  862. test_strmap(void)
  863. {
  864. strmap_t *map;
  865. // strmap_iter_t *iter;
  866. // const char *k;
  867. void *v;
  868. map = strmap_new();
  869. v = strmap_set(map, "K1", (void*)99);
  870. test_eq(v, NULL);
  871. v = strmap_set(map, "K2", (void*)101);
  872. test_eq(v, NULL);
  873. v = strmap_set(map, "K1", (void*)100);
  874. test_eq(v, (void*)99);
  875. test_eq_ptr(strmap_get(map,"K1"), (void*)100);
  876. test_eq_ptr(strmap_get(map,"K2"), (void*)101);
  877. test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
  878. v = strmap_remove(map,"K2");
  879. test_eq_ptr(v, (void*)101);
  880. test_eq_ptr(strmap_get(map,"K2"), NULL);
  881. test_eq_ptr(strmap_remove(map,"K2"), NULL);
  882. strmap_set(map, "K2", (void*)101);
  883. strmap_set(map, "K3", (void*)102);
  884. strmap_set(map, "K4", (void*)103);
  885. strmap_set(map, "K5", (void*)104);
  886. strmap_set(map, "K6", (void*)105);
  887. #if 0
  888. iter = strmap_iter_init(map);
  889. strmap_iter_get(iter,&k,&v);
  890. test_streq(k, "K1");
  891. test_eq(v, (void*)10000);
  892. iter = strmap_iter_next(map,iter);
  893. strmap_iter_get(iter,&k,&v);
  894. test_streq(k, "K2");
  895. test_eq(v, (void*)10201);
  896. iter = strmap_iter_next_rmv(map,iter);
  897. strmap_iter_get(iter,&k,&v);
  898. test_streq(k, "K3");
  899. test_eq(v, (void*)10404);
  900. iter = strmap_iter_next(map,iter); /* K5 */
  901. test_assert(!strmap_iter_done(iter));
  902. iter = strmap_iter_next(map,iter); /* K6 */
  903. test_assert(!strmap_iter_done(iter));
  904. iter = strmap_iter_next(map,iter); /* done */
  905. test_assert(strmap_iter_done(iter));
  906. /* Make sure we removed K2, but not the others. */
  907. test_eq_ptr(strmap_get(map, "K2"), NULL);
  908. test_eq_ptr(strmap_get(map, "K5"), (void*)10816);
  909. #endif
  910. /* Clean up after ourselves. */
  911. strmap_free(map, NULL);
  912. /* Now try some lc functions. */
  913. map = strmap_new();
  914. strmap_set_lc(map,"Ab.C", (void*)1);
  915. test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
  916. test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
  917. test_eq_ptr(strmap_get(map,"AB.C"), NULL);
  918. test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
  919. test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
  920. strmap_free(map,NULL);
  921. }
  922. static void
  923. test_control_formats(void)
  924. {
  925. char *out;
  926. const char *inp =
  927. "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
  928. size_t sz;
  929. sz = read_escaped_data(inp, strlen(inp), 1, &out);
  930. test_streq(out,
  931. ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
  932. tor_free(out);
  933. }
  934. static void
  935. test_onion(void)
  936. {
  937. #if 0
  938. char **names;
  939. int i,num;
  940. names = parse_nickname_list(" foo bar\t baz quux ", &num);
  941. test_eq(num,4);
  942. test_streq(names[0],"foo");
  943. test_streq(names[1],"bar");
  944. test_streq(names[2],"baz");
  945. test_streq(names[3],"quux");
  946. for (i=0;i<num;i++)
  947. tor_free(names[i]);
  948. tor_free(names);
  949. #endif
  950. }
  951. static void
  952. test_onion_handshake(void)
  953. {
  954. /* client-side */
  955. crypto_dh_env_t *c_dh = NULL;
  956. char c_buf[ONIONSKIN_CHALLENGE_LEN];
  957. char c_keys[40];
  958. /* server-side */
  959. char s_buf[ONIONSKIN_REPLY_LEN];
  960. char s_keys[40];
  961. /* shared */
  962. crypto_pk_env_t *pk = NULL;
  963. pk = crypto_new_pk_env();
  964. test_assert(! crypto_pk_generate_key(pk));
  965. /* client handshake 1. */
  966. memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
  967. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  968. /* server handshake */
  969. memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
  970. memset(s_keys, 0, 40);
  971. test_assert(! onion_skin_server_handshake(c_buf, pk, NULL,
  972. s_buf, s_keys, 40));
  973. /* client handshake 2 */
  974. memset(c_keys, 0, 40);
  975. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  976. crypto_dh_free(c_dh);
  977. if (memcmp(c_keys, s_keys, 40)) {
  978. puts("Aiiiie");
  979. exit(1);
  980. }
  981. test_memeq(c_keys, s_keys, 40);
  982. memset(s_buf, 0, 40);
  983. test_memneq(c_keys, s_buf, 40);
  984. crypto_free_pk_env(pk);
  985. }
  986. static void
  987. test_dir_format(void)
  988. {
  989. char buf[8192], buf2[8192];
  990. char platform[256];
  991. char fingerprint[FINGERPRINT_LEN+1];
  992. char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
  993. size_t pk1_str_len, pk2_str_len, pk3_str_len;
  994. routerinfo_t r1, r2;
  995. crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
  996. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  997. addr_policy_t ex1, ex2;
  998. routerlist_t *dir1 = NULL, *dir2 = NULL;
  999. tor_version_t ver1;
  1000. char *bw_lines = NULL;
  1001. test_assert( (pk1 = crypto_new_pk_env()) );
  1002. test_assert( (pk2 = crypto_new_pk_env()) );
  1003. test_assert( (pk3 = crypto_new_pk_env()) );
  1004. test_assert(! crypto_pk_generate_key(pk1));
  1005. test_assert(! crypto_pk_generate_key(pk2));
  1006. test_assert(! crypto_pk_generate_key(pk3));
  1007. test_assert( is_legal_nickname("a"));
  1008. test_assert(!is_legal_nickname(""));
  1009. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  1010. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  1011. test_assert(!is_legal_nickname("hyphen-")); /* bad char */
  1012. test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  1013. test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1014. /* valid */
  1015. test_assert( is_legal_nickname_or_hexdigest(
  1016. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1017. /* too short */
  1018. test_assert(!is_legal_nickname_or_hexdigest(
  1019. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1020. /* illegal char */
  1021. test_assert(!is_legal_nickname_or_hexdigest(
  1022. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1023. test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  1024. test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  1025. test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  1026. get_platform_str(platform, sizeof(platform));
  1027. memset(&r1,0,sizeof(r1));
  1028. memset(&r2,0,sizeof(r2));
  1029. r1.address = tor_strdup("18.244.0.1");
  1030. r1.addr = 0xc0a80001u; /* 192.168.0.1 */
  1031. r1.cache_info.published_on = 0;
  1032. r1.or_port = 9000;
  1033. r1.dir_port = 9003;
  1034. r1.onion_pkey = pk1;
  1035. r1.identity_pkey = pk2;
  1036. r1.bandwidthrate = 1000;
  1037. r1.bandwidthburst = 5000;
  1038. r1.bandwidthcapacity = 10000;
  1039. r1.exit_policy = NULL;
  1040. r1.nickname = tor_strdup("Magri");
  1041. r1.platform = tor_strdup(platform);
  1042. ex1.policy_type = ADDR_POLICY_ACCEPT;
  1043. ex1.string = NULL;
  1044. ex1.addr = 0;
  1045. ex1.msk = 0;
  1046. ex1.prt_min = ex1.prt_max = 80;
  1047. ex1.next = &ex2;
  1048. ex2.policy_type = ADDR_POLICY_REJECT;
  1049. ex2.addr = 18 << 24;
  1050. ex2.msk = 0xFF000000u;
  1051. ex2.prt_min = ex2.prt_max = 24;
  1052. ex2.next = NULL;
  1053. r2.address = tor_strdup("1.1.1.1");
  1054. r2.addr = 0x0a030201u; /* 10.3.2.1 */
  1055. r2.platform = tor_strdup(platform);
  1056. r2.cache_info.published_on = 5;
  1057. r2.or_port = 9005;
  1058. r2.dir_port = 0;
  1059. r2.onion_pkey = pk2;
  1060. r2.identity_pkey = pk1;
  1061. r2.bandwidthrate = r2.bandwidthburst = r2.bandwidthcapacity = 3000;
  1062. r2.exit_policy = &ex1;
  1063. r2.nickname = tor_strdup("Fred");
  1064. bw_lines = rep_hist_get_bandwidth_lines();
  1065. test_assert(bw_lines);
  1066. test_assert(!strcmpstart(bw_lines, "opt write-history "));
  1067. test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  1068. &pk1_str_len));
  1069. test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  1070. &pk2_str_len));
  1071. test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
  1072. &pk3_str_len));
  1073. memset(buf, 0, 2048);
  1074. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1075. strcpy(buf2, "router Magri 18.244.0.1 9000 0 0\n"
  1076. "platform Tor "VERSION" on ");
  1077. strcat(buf2, get_uname());
  1078. strcat(buf2, "\n"
  1079. "published 1970-01-01 00:00:00\n"
  1080. "opt fingerprint ");
  1081. test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  1082. strcat(buf2, fingerprint);
  1083. strcat(buf2, "\nuptime 0\n"
  1084. /* XXX the "0" above is hardcoded, but even if we made it reflect
  1085. * uptime, that still wouldn't make it right, because the two
  1086. * descriptors might be made on different seconds... hm. */
  1087. "bandwidth 1000 5000 10000\n"
  1088. "onion-key\n");
  1089. strcat(buf2, pk1_str);
  1090. strcat(buf2, "signing-key\n");
  1091. strcat(buf2, pk2_str);
  1092. strcat(buf2, bw_lines);
  1093. strcat(buf2, "router-signature\n");
  1094. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  1095. * twice */
  1096. test_streq(buf, buf2);
  1097. tor_free(bw_lines);
  1098. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1099. cp = buf;
  1100. rp1 = router_parse_entry_from_string((const char*)cp,NULL);
  1101. test_assert(rp1);
  1102. test_streq(rp1->address, r1.address);
  1103. test_eq(rp1->or_port, r1.or_port);
  1104. //test_eq(rp1->dir_port, r1.dir_port);
  1105. test_eq(rp1->bandwidthrate, r1.bandwidthrate);
  1106. test_eq(rp1->bandwidthburst, r1.bandwidthburst);
  1107. test_eq(rp1->bandwidthcapacity, r1.bandwidthcapacity);
  1108. test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  1109. test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  1110. test_assert(rp1->exit_policy == NULL);
  1111. #if 0
  1112. /* XXX Once we have exit policies, test this again. XXX */
  1113. strcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n");
  1114. strcat(buf2, pk2_str);
  1115. strcat(buf2, "signing-key\n");
  1116. strcat(buf2, pk1_str);
  1117. strcat(buf2, "accept *:80\nreject 18.*:24\n\n");
  1118. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
  1119. test_streq(buf, buf2);
  1120. cp = buf;
  1121. rp2 = router_parse_entry_from_string(&cp);
  1122. test_assert(rp2);
  1123. test_streq(rp2->address, r2.address);
  1124. test_eq(rp2->or_port, r2.or_port);
  1125. test_eq(rp2->dir_port, r2.dir_port);
  1126. test_eq(rp2->bandwidth, r2.bandwidth);
  1127. test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  1128. test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  1129. test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
  1130. test_streq(rp2->exit_policy->string, "accept *:80");
  1131. test_streq(rp2->exit_policy->address, "*");
  1132. test_streq(rp2->exit_policy->port, "80");
  1133. test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
  1134. test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
  1135. test_streq(rp2->exit_policy->next->address, "18.*");
  1136. test_streq(rp2->exit_policy->next->port, "24");
  1137. test_assert(rp2->exit_policy->next->next == NULL);
  1138. #endif
  1139. /* Okay, now for the directories. */
  1140. {
  1141. extern smartlist_t *fingerprint_list;
  1142. fingerprint_list = smartlist_create();
  1143. crypto_pk_get_fingerprint(pk2, buf, 1);
  1144. add_fingerprint_to_dir("Magri", buf, fingerprint_list);
  1145. crypto_pk_get_fingerprint(pk1, buf, 1);
  1146. add_fingerprint_to_dir("Fred", buf, fingerprint_list);
  1147. }
  1148. #if 0
  1149. {
  1150. char d[DIGEST_LEN];
  1151. const char *m;
  1152. /* XXXX NM re-enable. */
  1153. /* Make sure routers aren't too far in the past any more. */
  1154. r1.cache_info.published_on = time(NULL);
  1155. r2.cache_info.published_on = time(NULL)-3*60*60;
  1156. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1157. test_eq(dirserv_add_descriptor(buf,&m), 2);
  1158. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk1)>0);
  1159. test_eq(dirserv_add_descriptor(buf,&m), 2);
  1160. get_options()->Nickname = tor_strdup("DirServer");
  1161. test_assert(!dirserv_dump_directory_to_string(&cp,pk3));
  1162. crypto_pk_get_digest(pk3, d);
  1163. test_assert(!router_parse_directory(cp));
  1164. test_eq(2, smartlist_len(dir1->routers));
  1165. tor_free(cp);
  1166. }
  1167. #endif
  1168. dirserv_free_fingerprint_list();
  1169. tor_free(pk1_str);
  1170. tor_free(pk2_str);
  1171. if (pk1) crypto_free_pk_env(pk1);
  1172. if (pk2) crypto_free_pk_env(pk2);
  1173. if (rp1) routerinfo_free(rp1);
  1174. if (rp2) routerinfo_free(rp2);
  1175. tor_free(dir1); /* XXXX And more !*/
  1176. tor_free(dir2); /* And more !*/
  1177. /* Try out version parsing functionality */
  1178. test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
  1179. test_eq(0, ver1.major);
  1180. test_eq(3, ver1.minor);
  1181. test_eq(4, ver1.micro);
  1182. test_eq(VER_PRE, ver1.status);
  1183. test_eq(2, ver1.patchlevel);
  1184. test_eq(IS_CVS, ver1.cvs);
  1185. test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
  1186. test_eq(0, ver1.major);
  1187. test_eq(3, ver1.minor);
  1188. test_eq(4, ver1.micro);
  1189. test_eq(VER_RC, ver1.status);
  1190. test_eq(1, ver1.patchlevel);
  1191. test_eq(IS_NOT_CVS, ver1.cvs);
  1192. test_eq(0, tor_version_parse("1.3.4", &ver1));
  1193. test_eq(1, ver1.major);
  1194. test_eq(3, ver1.minor);
  1195. test_eq(4, ver1.micro);
  1196. test_eq(VER_RELEASE, ver1.status);
  1197. test_eq(0, ver1.patchlevel);
  1198. test_eq(IS_NOT_CVS, ver1.cvs);
  1199. test_eq(0, tor_version_parse("1.3.4.999", &ver1));
  1200. test_eq(1, ver1.major);
  1201. test_eq(3, ver1.minor);
  1202. test_eq(4, ver1.micro);
  1203. test_eq(VER_RELEASE, ver1.status);
  1204. test_eq(999, ver1.patchlevel);
  1205. test_eq(IS_NOT_CVS, ver1.cvs);
  1206. test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
  1207. test_eq(0, ver1.major);
  1208. test_eq(1, ver1.minor);
  1209. test_eq(2, ver1.micro);
  1210. test_eq(4, ver1.patchlevel);
  1211. test_eq(VER_RELEASE, ver1.status);
  1212. test_eq(IS_NOT_CVS, ver1.cvs);
  1213. test_streq("alpha", ver1.status_tag);
  1214. test_eq(0, tor_version_parse("0.1.2.4", &ver1));
  1215. test_eq(0, ver1.major);
  1216. test_eq(1, ver1.minor);
  1217. test_eq(2, ver1.micro);
  1218. test_eq(4, ver1.patchlevel);
  1219. test_eq(VER_RELEASE, ver1.status);
  1220. test_eq(IS_NOT_CVS, ver1.cvs);
  1221. test_streq("", ver1.status_tag);
  1222. #define test_eq_vs(vs1, vs2) test_eq_type(version_status_t, "%d", (vs1), (vs2))
  1223. #define test_v_i_o(val, ver, lst) \
  1224. test_eq_vs(val, tor_version_is_obsolete(ver, lst))
  1225. /* make sure tor_version_is_obsolete() works */
  1226. test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
  1227. test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
  1228. test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
  1229. test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
  1230. test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
  1231. test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
  1232. test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
  1233. test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
  1234. test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
  1235. test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
  1236. test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5");
  1237. /* Not on list, but newer than any in same series. */
  1238. test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
  1239. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1240. /* Series newer than any on list. */
  1241. test_v_i_o(VS_NEW, "0.1.2.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1242. /* Series older than any on list. */
  1243. test_v_i_o(VS_OLD, "0.0.1.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1244. /* Not on list, not newer than any on same series. */
  1245. test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
  1246. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1247. /* On list, not newer than any on same series. */
  1248. test_v_i_o(VS_UNRECOMMENDED,
  1249. "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1250. test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  1251. test_eq(1, tor_version_as_new_as(
  1252. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  1253. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
  1254. "0.0.8rc2"));
  1255. test_eq(0, tor_version_as_new_as(
  1256. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  1257. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
  1258. }
  1259. static void
  1260. test_exit_policies(void)
  1261. {
  1262. addr_policy_t *policy;
  1263. policy = router_parse_addr_policy_from_string("reject 192.168.0.0/16:*",-1);
  1264. test_eq(NULL, policy->next);
  1265. test_eq(ADDR_POLICY_REJECT, policy->policy_type);
  1266. test_eq(0xc0a80000u, policy->addr);
  1267. test_eq(0xffff0000u, policy->msk);
  1268. test_eq(1, policy->prt_min);
  1269. test_eq(65535, policy->prt_max);
  1270. test_streq("reject 192.168.0.0/16:*", policy->string);
  1271. // test_assert(exit_policy_implicitly_allows_local_networks(policy, 0));
  1272. test_assert(ADDR_POLICY_ACCEPTED ==
  1273. compare_addr_to_addr_policy(0x01020304u, 2, policy));
  1274. test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  1275. compare_addr_to_addr_policy(0, 2, policy));
  1276. test_assert(ADDR_POLICY_REJECTED ==
  1277. compare_addr_to_addr_policy(0xc0a80102, 2, policy));
  1278. addr_policy_free(policy);
  1279. #if 0
  1280. /* Copied from router.c */
  1281. policy = NULL;
  1282. options_append_default_exit_policy(&policy);
  1283. test_assert(policy);
  1284. test_assert(!exit_policy_implicitly_allows_local_networks(policy, 1));
  1285. addr_policy_free(policy);
  1286. #endif
  1287. }
  1288. static void
  1289. test_rend_fns(void)
  1290. {
  1291. char address1[] = "fooaddress.onion";
  1292. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  1293. char address3[] = "fooaddress.exit";
  1294. char address4[] = "tor.eff.org";
  1295. rend_service_descriptor_t *d1, *d2;
  1296. char *encoded;
  1297. size_t len;
  1298. crypto_pk_env_t *pk1, *pk2;
  1299. time_t now;
  1300. pk1 = crypto_new_pk_env();
  1301. pk2 = crypto_new_pk_env();
  1302. test_assert(!crypto_pk_generate_key(pk1));
  1303. test_assert(!crypto_pk_generate_key(pk2));
  1304. /* Test unversioned descriptor */
  1305. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  1306. d1->pk = crypto_pk_dup_key(pk1);
  1307. now = time(NULL);
  1308. d1->timestamp = now;
  1309. d1->n_intro_points = 3;
  1310. d1->version = 0;
  1311. d1->intro_points = tor_malloc(sizeof(char*)*3);
  1312. d1->intro_points[0] = tor_strdup("tom");
  1313. d1->intro_points[1] = tor_strdup("crow");
  1314. d1->intro_points[2] = tor_strdup("joel");
  1315. test_assert(! rend_encode_service_descriptor(d1, 0, pk1, &encoded, &len));
  1316. d2 = rend_parse_service_descriptor(encoded, len);
  1317. test_assert(d2);
  1318. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  1319. test_eq(d2->timestamp, now);
  1320. test_eq(d2->version, 0);
  1321. test_eq(d2->protocols, 1);
  1322. test_eq(d2->n_intro_points, 3);
  1323. test_streq(d2->intro_points[0], "tom");
  1324. test_streq(d2->intro_points[1], "crow");
  1325. test_streq(d2->intro_points[2], "joel");
  1326. test_eq(NULL, d2->intro_point_extend_info);
  1327. rend_service_descriptor_free(d1);
  1328. rend_service_descriptor_free(d2);
  1329. tor_free(encoded);
  1330. /* Test versioned descriptor. */
  1331. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  1332. d1->pk = crypto_pk_dup_key(pk1);
  1333. now = time(NULL);
  1334. d1->timestamp = now;
  1335. d1->n_intro_points = 2;
  1336. d1->version = 1;
  1337. d1->protocols = 60;
  1338. d1->intro_points = tor_malloc(sizeof(char*)*2);
  1339. d1->intro_point_extend_info = tor_malloc(sizeof(extend_info_t*)*2);
  1340. d1->intro_points[0] = tor_strdup("tom");
  1341. d1->intro_points[1] = tor_strdup("crow");
  1342. d1->intro_point_extend_info[0] = tor_malloc_zero(sizeof(extend_info_t));
  1343. strcpy(d1->intro_point_extend_info[0]->nickname, "tom");
  1344. d1->intro_point_extend_info[0]->addr = 1234;
  1345. d1->intro_point_extend_info[0]->port = 4567;
  1346. d1->intro_point_extend_info[0]->onion_key = crypto_pk_dup_key(pk1);
  1347. memset(d1->intro_point_extend_info[0]->identity_digest, 'a', DIGEST_LEN);
  1348. d1->intro_point_extend_info[1] = tor_malloc_zero(sizeof(extend_info_t));
  1349. strcpy(d1->intro_point_extend_info[1]->nickname, "crow");
  1350. d1->intro_point_extend_info[1]->addr = 6060842;
  1351. d1->intro_point_extend_info[1]->port = 8000;
  1352. d1->intro_point_extend_info[1]->onion_key = crypto_pk_dup_key(pk2);
  1353. memset(d1->intro_point_extend_info[1]->identity_digest, 'b', DIGEST_LEN);
  1354. test_assert(! rend_encode_service_descriptor(d1, 1, pk1, &encoded, &len));
  1355. d2 = rend_parse_service_descriptor(encoded, len);
  1356. test_assert(d2);
  1357. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  1358. test_eq(d2->timestamp, now);
  1359. test_eq(d2->version, 1);
  1360. test_eq(d2->protocols, 60);
  1361. test_eq(d2->n_intro_points, 2);
  1362. test_streq(d2->intro_points[0], d2->intro_point_extend_info[0]->nickname);
  1363. test_streq(d2->intro_points[1], d2->intro_point_extend_info[1]->nickname);
  1364. test_eq(d2->intro_point_extend_info[0]->addr, 1234);
  1365. test_eq(d2->intro_point_extend_info[0]->port, 4567);
  1366. test_assert(!crypto_pk_cmp_keys(pk1,
  1367. d2->intro_point_extend_info[0]->onion_key));
  1368. test_memeq(d2->intro_point_extend_info[0]->identity_digest,
  1369. d1->intro_point_extend_info[0]->identity_digest, DIGEST_LEN);
  1370. test_eq(d2->intro_point_extend_info[1]->addr, 6060842);
  1371. test_eq(d2->intro_point_extend_info[1]->port, 8000);
  1372. test_memeq(d2->intro_point_extend_info[1]->identity_digest,
  1373. d1->intro_point_extend_info[1]->identity_digest, DIGEST_LEN);
  1374. test_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
  1375. test_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
  1376. test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
  1377. test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
  1378. rend_service_descriptor_free(d1);
  1379. rend_service_descriptor_free(d2);
  1380. crypto_free_pk_env(pk1);
  1381. crypto_free_pk_env(pk2);
  1382. }
  1383. static void
  1384. bench_aes(void)
  1385. {
  1386. int len, i;
  1387. char *b1, *b2;
  1388. crypto_cipher_env_t *c;
  1389. struct timeval start, end;
  1390. const int iters = 100000;
  1391. uint64_t nsec;
  1392. c = crypto_new_cipher_env();
  1393. crypto_cipher_generate_key(c);
  1394. crypto_cipher_encrypt_init_cipher(c);
  1395. for (len = 1; len <= 8192; len *= 2) {
  1396. b1 = tor_malloc_zero(len);
  1397. b2 = tor_malloc_zero(len);
  1398. tor_gettimeofday(&start);
  1399. for (i = 0; i < iters; ++i) {
  1400. crypto_cipher_encrypt(c, b1, b2, len);
  1401. }
  1402. tor_gettimeofday(&end);
  1403. tor_free(b1);
  1404. tor_free(b2);
  1405. nsec = (uint64_t) tv_udiff(&start,&end);
  1406. nsec *= 1000;
  1407. nsec /= (iters*len);
  1408. printf("%d bytes: "U64_FORMAT" nsec per byte\n", len,
  1409. U64_PRINTF_ARG(nsec));
  1410. }
  1411. crypto_free_cipher_env(c);
  1412. }
  1413. int
  1414. main(int c, char**v)
  1415. {
  1416. or_options_t *options = options_new();
  1417. char *errmsg = NULL;
  1418. options->command = CMD_RUN_UNITTESTS;
  1419. network_init();
  1420. setup_directory();
  1421. options_init(options);
  1422. options->DataDirectory = tor_strdup(temp_dir);
  1423. if (set_options(options, &errmsg) < 0) {
  1424. printf("Failed to set initial options: %s\n", errmsg);
  1425. tor_free(errmsg);
  1426. return 1;
  1427. }
  1428. crypto_seed_rng();
  1429. if (0) {
  1430. bench_aes();
  1431. return 0;
  1432. }
  1433. rep_hist_init();
  1434. atexit(remove_directory);
  1435. printf("Running Tor unit tests on %s\n", get_uname());
  1436. puts("========================== Buffers =========================");
  1437. test_buffers();
  1438. puts("\n========================== Crypto ==========================");
  1439. // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
  1440. test_crypto();
  1441. test_crypto_dh();
  1442. test_crypto_s2k();
  1443. puts("\n========================= Util ============================");
  1444. test_gzip();
  1445. test_util();
  1446. test_strmap();
  1447. test_control_formats();
  1448. puts("\n========================= Onion Skins =====================");
  1449. test_onion();
  1450. test_onion_handshake();
  1451. puts("\n========================= Directory Formats ===============");
  1452. test_dir_format();
  1453. puts("\n========================= Exit policies ===================");
  1454. test_exit_policies();
  1455. puts("\n========================= Rendezvous functionality ========");
  1456. test_rend_fns();
  1457. puts("");
  1458. if (have_failed)
  1459. return 1;
  1460. else
  1461. return 0;
  1462. }