test.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char test_c_id[] = "$Id$";
  6. #include "orconfig.h"
  7. #include <stdio.h>
  8. #ifdef HAVE_FCNTL_H
  9. #include <fcntl.h>
  10. #endif
  11. #ifdef MS_WINDOWS
  12. /* For mkdir() */
  13. #include <direct.h>
  14. #else
  15. #include <dirent.h>
  16. #endif
  17. #include "or.h"
  18. #include "../common/test.h"
  19. #include "../common/torgzip.h"
  20. int have_failed = 0;
  21. /* These functions are file-local, but are exposed so we can test. */
  22. void add_fingerprint_to_dir(const char *nickname, const char *fp);
  23. void get_platform_str(char *platform, size_t len);
  24. int is_obsolete_version(const char *myversion, const char *start);
  25. static char temp_dir[256];
  26. static void
  27. setup_directory(void)
  28. {
  29. static int is_setup = 0;
  30. int r;
  31. if (is_setup) return;
  32. #ifdef MS_WINDOWS
  33. // XXXX
  34. tor_snprintf(temp_dir, sizeof(temp_dir), "c:\\windows\\temp\\tor_test_%d", (int)getpid());
  35. r = mkdir(temp_dir);
  36. #else
  37. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
  38. r = mkdir(temp_dir, 0700);
  39. #endif
  40. if (r) {
  41. fprintf(stderr, "Can't create directory %s:", temp_dir);
  42. perror("");
  43. exit(1);
  44. }
  45. is_setup = 1;
  46. }
  47. static const char *
  48. get_fname(const char *name)
  49. {
  50. static char buf[1024];
  51. setup_directory();
  52. tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
  53. return buf;
  54. }
  55. static void
  56. remove_directory(void)
  57. {
  58. #ifdef MS_WINDOWS
  59. char *pattern;
  60. HANDLE handle;
  61. WIN32_FIND_DATA findData;
  62. setup_directory();
  63. pattern = tor_malloc(strlen(temp_dir)+16);
  64. tor_snprintf(pattern, strlen(temp_dir)+16, "%s\\*", temp_dir);
  65. handle = FindFirstFile(pattern, &findData);
  66. if (handle == INVALID_HANDLE_VALUE) {
  67. perror("Can't remove");
  68. return;
  69. }
  70. while (1) {
  71. size_t dlen = strlen(findData.cFileName)+strlen(temp_dir)+16;
  72. char *deleteable = tor_malloc(dlen);
  73. tor_snprintf(deleteable, dlen, "%s\\%s", temp_dir, findData.cFileName);
  74. unlink(deleteable);
  75. tor_free(deleteable);
  76. if (!FindNextFile(handle, &findData)) {
  77. if (GetLastError() != ERROR_NO_MORE_FILES) {
  78. perror("error reading dir");
  79. }
  80. break;
  81. }
  82. }
  83. FindClose(handle);
  84. tor_free(pattern);
  85. #else
  86. DIR *dirp;
  87. struct dirent *de;
  88. setup_directory();
  89. if (!(dirp = opendir(temp_dir))) {
  90. perror("Can't open temporary directory to remove files");
  91. return;
  92. }
  93. while ((de = readdir(dirp)) != NULL) {
  94. /* Only "." and ".." start with ., since we don't create any dotfiles. */
  95. if (de->d_name[0] == '.') continue;
  96. if (unlink(get_fname(de->d_name))) {
  97. perror("Error removing file");
  98. }
  99. #if 0
  100. printf("==%s\n", de->d_name);
  101. #endif
  102. }
  103. closedir(dirp);
  104. #endif
  105. rmdir(temp_dir);
  106. }
  107. static void
  108. test_buffers(void) {
  109. #define MAX_BUF_SIZE 1024*1024
  110. char str[256];
  111. char str2[256];
  112. buf_t *buf;
  113. buf_t *buf2;
  114. int s, i, j, eof;
  115. /****
  116. * buf_new
  117. ****/
  118. if (!(buf = buf_new()))
  119. test_fail();
  120. test_eq(buf_capacity(buf), 512*1024);
  121. test_eq(buf_datalen(buf), 0);
  122. /****
  123. * read_to_buf
  124. ****/
  125. s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
  126. for (j=0;j<256;++j) {
  127. str[j] = (char)j;
  128. }
  129. write(s, str, 256);
  130. close(s);
  131. s = open(get_fname("data"), O_RDONLY, 0);
  132. eof = 0;
  133. i = read_to_buf(s, 10, buf, &eof);
  134. test_eq(buf_capacity(buf), 512*1024);
  135. test_eq(buf_datalen(buf), 10);
  136. test_eq(eof, 0);
  137. test_eq(i, 10);
  138. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
  139. /* Test reading 0 bytes. */
  140. i = read_to_buf(s, 0, buf, &eof);
  141. test_eq(buf_capacity(buf), 512*1024);
  142. test_eq(buf_datalen(buf), 10);
  143. test_eq(eof, 0);
  144. test_eq(i, 0);
  145. /* Now test when buffer is filled exactly. */
  146. buf2 = buf_new_with_capacity(6);
  147. i = read_to_buf(s, 6, buf2, &eof);
  148. test_eq(buf_capacity(buf2), 6);
  149. test_eq(buf_datalen(buf2), 6);
  150. test_eq(eof, 0);
  151. test_eq(i, 6);
  152. test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
  153. buf_free(buf2);
  154. /* Now test when buffer is filled with more data to read. */
  155. buf2 = buf_new_with_capacity(32);
  156. i = read_to_buf(s, 128, buf2, &eof);
  157. test_eq(buf_capacity(buf2), 128);
  158. test_eq(buf_datalen(buf2), 32);
  159. test_eq(eof, 0);
  160. test_eq(i, 32);
  161. buf_free(buf2);
  162. /* Now read to eof. */
  163. test_assert(buf_capacity(buf) > 256);
  164. i = read_to_buf(s, 1024, buf, &eof);
  165. test_eq(i, (256-32-10-6));
  166. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  167. test_eq(buf_datalen(buf), 256-6-32);
  168. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
  169. test_eq(eof, 0);
  170. i = read_to_buf(s, 1024, buf, &eof);
  171. test_eq(i, 0);
  172. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  173. test_eq(buf_datalen(buf), 256-6-32);
  174. test_eq(eof, 1);
  175. close(s);
  176. /****
  177. * fetch_from_buf
  178. ****/
  179. memset(str2, 255, 256);
  180. test_eq(246, fetch_from_buf(str2, 10, buf));
  181. test_memeq(str2, str, 10);
  182. test_memeq(str+10,(char*)_buf_peek_raw_buffer(buf),246);
  183. test_eq(buf_datalen(buf),246);
  184. test_eq(0, fetch_from_buf(str2, 246, buf));
  185. test_memeq(str2, str+10, 246);
  186. test_eq(buf_capacity(buf),MAX_BUF_SIZE);
  187. test_eq(buf_datalen(buf),0);
  188. /****
  189. * write_to_buf
  190. ****/
  191. memset((char *)_buf_peek_raw_buffer(buf), (int)'-', 256);
  192. i = write_to_buf("Hello world", 11, buf);
  193. test_eq(i, 11);
  194. test_eq(buf_datalen(buf), 11);
  195. test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello world", 11);
  196. i = write_to_buf("XYZZY", 5, buf);
  197. test_eq(i, 16);
  198. test_eq(buf_datalen(buf), 16);
  199. test_memeq((char*)_buf_peek_raw_buffer(buf), "Hello worldXYZZY", 16);
  200. /* Test when buffer is overfull. */
  201. #if 0
  202. buflen = 18;
  203. test_eq(-1, write_to_buf("This string will not fit.", 25,
  204. &buf, &buflen, &buf_datalen));
  205. test_eq(buf_datalen, 16);
  206. test_memeq(buf, "Hello worldXYZZY--", 18);
  207. buflen = MAX_BUF_SIZE;
  208. #endif
  209. /****
  210. * flush_buf
  211. ****/
  212. /* XXXX Needs tests. */
  213. buf_free(buf);
  214. }
  215. static void
  216. test_crypto_dh(void)
  217. {
  218. crypto_dh_env_t *dh1, *dh2;
  219. char p1[DH_BYTES];
  220. char p2[DH_BYTES];
  221. char s1[DH_BYTES];
  222. char s2[DH_BYTES];
  223. int s1len, s2len;
  224. dh1 = crypto_dh_new();
  225. dh2 = crypto_dh_new();
  226. test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
  227. test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
  228. memset(p1, 0, DH_BYTES);
  229. memset(p2, 0, DH_BYTES);
  230. test_memeq(p1, p2, DH_BYTES);
  231. test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  232. test_memneq(p1, p2, DH_BYTES);
  233. test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  234. test_memneq(p1, p2, DH_BYTES);
  235. memset(s1, 0, DH_BYTES);
  236. memset(s2, 0xFF, DH_BYTES);
  237. s1len = crypto_dh_compute_secret(dh1, p2, DH_BYTES, s1, 50);
  238. s2len = crypto_dh_compute_secret(dh2, p1, DH_BYTES, s2, 50);
  239. test_assert(s1len > 0);
  240. test_eq(s1len, s2len);
  241. test_memeq(s1, s2, s1len);
  242. crypto_dh_free(dh1);
  243. crypto_dh_free(dh2);
  244. }
  245. static void
  246. test_crypto(void)
  247. {
  248. crypto_cipher_env_t *env1, *env2;
  249. crypto_pk_env_t *pk1, *pk2;
  250. char *data1, *data2, *data3, *cp;
  251. int i, j, p, len;
  252. size_t size;
  253. data1 = tor_malloc(1024);
  254. data2 = tor_malloc(1024);
  255. data3 = tor_malloc(1024);
  256. test_assert(data1 && data2 && data3);
  257. /* Try out RNG. */
  258. test_assert(! crypto_seed_rng());
  259. crypto_rand(data1, 100);
  260. crypto_rand(data2, 100);
  261. test_memneq(data1,data2,100);
  262. #if 0
  263. /* Try out identity ciphers. */
  264. env1 = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  265. test_neq(env1, 0);
  266. test_eq(crypto_cipher_generate_key(env1), 0);
  267. test_eq(crypto_cipher_encrypt_init_cipher(env1), 0);
  268. for (i = 0; i < 1024; ++i) {
  269. data1[i] = (char) i*73;
  270. }
  271. crypto_cipher_encrypt(env1, data2, data1, 1024);
  272. test_memeq(data1, data2, 1024);
  273. crypto_free_cipher_env(env1);
  274. #endif
  275. /* Now, test encryption and decryption with stream cipher. */
  276. data1[0]='\0';
  277. for (i = 1023; i>0; i -= 35)
  278. strncat(data1, "Now is the time for all good onions", i);
  279. memset(data2, 0, 1024);
  280. memset(data3, 0, 1024);
  281. env1 = crypto_new_cipher_env();
  282. test_neq(env1, 0);
  283. env2 = crypto_new_cipher_env();
  284. test_neq(env2, 0);
  285. j = crypto_cipher_generate_key(env1);
  286. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  287. crypto_cipher_encrypt_init_cipher(env1);
  288. crypto_cipher_decrypt_init_cipher(env2);
  289. /* Try encrypting 512 chars. */
  290. crypto_cipher_encrypt(env1, data2, data1, 512);
  291. crypto_cipher_decrypt(env2, data3, data2, 512);
  292. test_memeq(data1, data3, 512);
  293. test_memneq(data1, data2, 512);
  294. /* Now encrypt 1 at a time, and get 1 at a time. */
  295. for (j = 512; j < 560; ++j) {
  296. crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
  297. }
  298. for (j = 512; j < 560; ++j) {
  299. crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
  300. }
  301. test_memeq(data1, data3, 560);
  302. /* Now encrypt 3 at a time, and get 5 at a time. */
  303. for (j = 560; j < 1024-5; j += 3) {
  304. crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
  305. }
  306. for (j = 560; j < 1024-5; j += 5) {
  307. crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
  308. }
  309. test_memeq(data1, data3, 1024-5);
  310. /* Now make sure that when we encrypt with different chunk sizes, we get
  311. the same results. */
  312. crypto_free_cipher_env(env2);
  313. memset(data3, 0, 1024);
  314. env2 = crypto_new_cipher_env();
  315. test_neq(env2, 0);
  316. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  317. crypto_cipher_encrypt_init_cipher(env2);
  318. for (j = 0; j < 1024-16; j += 17) {
  319. crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
  320. }
  321. for (j= 0; j < 1024-16; ++j) {
  322. if (data2[j] != data3[j]) {
  323. printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
  324. }
  325. }
  326. test_memeq(data2, data3, 1024-16);
  327. crypto_free_cipher_env(env1);
  328. crypto_free_cipher_env(env2);
  329. /* Test vectors for stream ciphers. */
  330. /* XXXX Look up some test vectors for the ciphers and make sure we match. */
  331. /* Test SHA-1 with a test vector from the specification. */
  332. i = crypto_digest(data1, "abc", 3);
  333. test_memeq(data1,
  334. "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
  335. "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
  336. /* Public-key ciphers */
  337. pk1 = crypto_new_pk_env();
  338. pk2 = crypto_new_pk_env();
  339. test_assert(pk1 && pk2);
  340. test_assert(! crypto_pk_generate_key(pk1));
  341. test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &size));
  342. test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, size));
  343. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  344. tor_free(cp);
  345. /* Check DER encoding */
  346. i=crypto_pk_DER64_encode_public_key(pk1, &cp);
  347. test_assert(i>0);
  348. test_assert(cp);
  349. test_assert(!strchr(cp, ' '));
  350. test_assert(!strchr(cp, '\n'));
  351. test_eq(0, crypto_pk_cmp_keys(pk1, pk1));
  352. crypto_free_pk_env(pk2);
  353. pk2 = crypto_pk_DER64_decode_public_key(cp);
  354. test_assert(pk2);
  355. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  356. tor_free(cp);
  357. test_eq(128, crypto_pk_keysize(pk1));
  358. test_eq(128, crypto_pk_keysize(pk2));
  359. test_eq(128, crypto_pk_public_encrypt(pk2, data1, "Hello whirled.", 15,
  360. PK_PKCS1_OAEP_PADDING));
  361. test_eq(128, crypto_pk_public_encrypt(pk1, data2, "Hello whirled.", 15,
  362. PK_PKCS1_OAEP_PADDING));
  363. /* oaep padding should make encryption not match */
  364. test_memneq(data1, data2, 128);
  365. test_eq(15, crypto_pk_private_decrypt(pk1, data3, data1, 128,
  366. PK_PKCS1_OAEP_PADDING,1));
  367. test_streq(data3, "Hello whirled.");
  368. memset(data3, 0, 1024);
  369. test_eq(15, crypto_pk_private_decrypt(pk1, data3, data2, 128,
  370. PK_PKCS1_OAEP_PADDING,1));
  371. test_streq(data3, "Hello whirled.");
  372. /* Can't decrypt with public key. */
  373. test_eq(-1, crypto_pk_private_decrypt(pk2, data3, data2, 128,
  374. PK_PKCS1_OAEP_PADDING,1));
  375. /* Try again with bad padding */
  376. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  377. test_eq(-1, crypto_pk_private_decrypt(pk1, data3, data2, 128,
  378. PK_PKCS1_OAEP_PADDING,1));
  379. /* File operations: save and load private key */
  380. test_assert(! crypto_pk_write_private_key_to_filename(pk1,
  381. get_fname("pkey1")));
  382. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  383. get_fname("pkey1")));
  384. test_eq(15, crypto_pk_private_decrypt(pk2, data3, data1, 128,
  385. PK_PKCS1_OAEP_PADDING,1));
  386. /* Now try signing. */
  387. strcpy(data1, "Ossifrage");
  388. test_eq(128, crypto_pk_private_sign(pk1, data2, data1, 10));
  389. test_eq(10, crypto_pk_public_checksig(pk1, data3, data2, 128));
  390. test_streq(data3, "Ossifrage");
  391. /* Try signing digests. */
  392. test_eq(128, crypto_pk_private_sign_digest(pk1, data2, data1, 10));
  393. test_eq(20, crypto_pk_public_checksig(pk1, data3, data2, 128));
  394. test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  395. test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  396. /*XXXX test failed signing*/
  397. /* Try encoding */
  398. crypto_free_pk_env(pk2);
  399. pk2 = NULL;
  400. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  401. test_assert(i>0);
  402. pk2 = crypto_pk_asn1_decode(data1, i);
  403. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  404. /* Try with hybrid encryption wrappers. */
  405. crypto_rand(data1, 1024);
  406. for (i = 0; i < 3; ++i) {
  407. for (j = 85; j < 140; ++j) {
  408. memset(data2,0,1024);
  409. memset(data3,0,1024);
  410. if (i == 0 && j < 129)
  411. continue;
  412. p = (i==0)?PK_NO_PADDING:
  413. (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
  414. len = crypto_pk_public_hybrid_encrypt(pk1,data2,data1,j,p,0);
  415. test_assert(len>=0);
  416. len = crypto_pk_private_hybrid_decrypt(pk1,data3,data2,len,p,1);
  417. test_eq(len,j);
  418. test_memeq(data1,data3,j);
  419. }
  420. }
  421. crypto_free_pk_env(pk1);
  422. crypto_free_pk_env(pk2);
  423. /* Base64 tests */
  424. strcpy(data1, "Test string that contains 35 chars.");
  425. strcat(data1, " 2nd string that contains 35 chars.");
  426. i = base64_encode(data2, 1024, data1, 71);
  427. j = base64_decode(data3, 1024, data2, i);
  428. test_streq(data3, data1);
  429. test_eq(j, 71);
  430. test_assert(data2[i] == '\0');
  431. /* Base32 tests */
  432. strcpy(data1, "5chrs");
  433. /* bit pattern is: [35 63 68 72 73] ->
  434. * [00110101 01100011 01101000 01110010 01110011]
  435. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  436. */
  437. base32_encode(data2, 9, data1, 5);
  438. test_streq(data2, "gvrwq4tt");
  439. strcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4");
  440. base32_encode(data2, 30, data1, 10);
  441. test_streq(data2, "772w2rfobvomsywe");
  442. /* Base16 tests */
  443. strcpy(data1, "6chrs\xff");
  444. base16_encode(data2, 13, data1, 6);
  445. test_streq(data2, "3663687273FF");
  446. strcpy(data1, "f0d678affc000100");
  447. i = base16_decode(data2, 8, data1, 16);
  448. test_eq(i,0);
  449. test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  450. free(data1);
  451. free(data2);
  452. free(data3);
  453. }
  454. static void
  455. test_util(void) {
  456. struct timeval start, end;
  457. struct tm a_time;
  458. smartlist_t *sl;
  459. char timestr[RFC1123_TIME_LEN+1];
  460. char buf[1024];
  461. time_t t_res;
  462. int i;
  463. uint32_t u32;
  464. uint16_t u16;
  465. char *cp, *k, *v;
  466. start.tv_sec = 5;
  467. start.tv_usec = 5000;
  468. end.tv_sec = 5;
  469. end.tv_usec = 5000;
  470. test_eq(0L, tv_udiff(&start, &end));
  471. end.tv_usec = 7000;
  472. test_eq(2000L, tv_udiff(&start, &end));
  473. end.tv_sec = 6;
  474. test_eq(1002000L, tv_udiff(&start, &end));
  475. end.tv_usec = 0;
  476. test_eq(995000L, tv_udiff(&start, &end));
  477. end.tv_sec = 4;
  478. test_eq(-1005000L, tv_udiff(&start, &end));
  479. /* The test values here are confirmed to be correct on a platform
  480. * with a working timegm. */
  481. a_time.tm_year = 2003-1900;
  482. a_time.tm_mon = 7;
  483. a_time.tm_mday = 30;
  484. a_time.tm_hour = 6;
  485. a_time.tm_min = 14;
  486. a_time.tm_sec = 55;
  487. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  488. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  489. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  490. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  491. a_time.tm_mday = 10;
  492. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  493. format_rfc1123_time(timestr, 0);
  494. test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
  495. format_rfc1123_time(timestr, (time_t)1091580502UL);
  496. test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
  497. t_res = 0;
  498. i = parse_rfc1123_time(timestr, &t_res);
  499. test_eq(i,0);
  500. test_eq(t_res, (time_t)1091580502UL);
  501. /* Test smartlist */
  502. sl = smartlist_create();
  503. smartlist_add(sl, (void*)1);
  504. smartlist_add(sl, (void*)2);
  505. smartlist_add(sl, (void*)3);
  506. smartlist_add(sl, (void*)4);
  507. smartlist_del_keeporder(sl, 1);
  508. smartlist_insert(sl, 1, (void*)22);
  509. smartlist_insert(sl, 0, (void*)0);
  510. smartlist_insert(sl, 5, (void*)555);
  511. test_eq((void*)0, smartlist_get(sl,0));
  512. test_eq((void*)1, smartlist_get(sl,1));
  513. test_eq((void*)22, smartlist_get(sl,2));
  514. test_eq((void*)3, smartlist_get(sl,3));
  515. test_eq((void*)4, smartlist_get(sl,4));
  516. test_eq((void*)555, smartlist_get(sl,5));
  517. smartlist_clear(sl);
  518. smartlist_split_string(sl, "abc", ":", 0, 0);
  519. test_eq(1, smartlist_len(sl));
  520. test_streq("abc", smartlist_get(sl, 0));
  521. smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  522. test_eq(4, smartlist_len(sl));
  523. test_streq("a", smartlist_get(sl, 1));
  524. test_streq("bc", smartlist_get(sl, 2));
  525. test_streq("", smartlist_get(sl, 3));
  526. cp = smartlist_join_strings(sl, "", 0, NULL);
  527. test_streq(cp, "abcabc");
  528. tor_free(cp);
  529. cp = smartlist_join_strings(sl, "!", 0, NULL);
  530. test_streq(cp, "abc!a!bc!");
  531. tor_free(cp);
  532. cp = smartlist_join_strings(sl, "XY", 0, NULL);
  533. test_streq(cp, "abcXYaXYbcXY");
  534. tor_free(cp);
  535. cp = smartlist_join_strings(sl, "XY", 1, NULL);
  536. test_streq(cp, "abcXYaXYbcXYXY");
  537. tor_free(cp);
  538. cp = smartlist_join_strings(sl, "", 1, NULL);
  539. test_streq(cp, "abcabc");
  540. tor_free(cp);
  541. smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
  542. test_eq(8, smartlist_len(sl));
  543. test_streq("", smartlist_get(sl, 4));
  544. test_streq("def", smartlist_get(sl, 5));
  545. test_streq(" ", smartlist_get(sl, 6));
  546. test_streq("ghijk", smartlist_get(sl, 7));
  547. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  548. smartlist_clear(sl);
  549. smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  550. test_eq(3, smartlist_len(sl));
  551. test_streq("a", smartlist_get(sl,0));
  552. test_streq("bbd", smartlist_get(sl,1));
  553. test_streq("cdef", smartlist_get(sl,2));
  554. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE, 0);
  555. test_eq(8, smartlist_len(sl));
  556. test_streq("z", smartlist_get(sl,3));
  557. test_streq("zhasd", smartlist_get(sl,4));
  558. test_streq("", smartlist_get(sl,5));
  559. test_streq("bnud", smartlist_get(sl,6));
  560. test_streq("", smartlist_get(sl,7));
  561. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  562. smartlist_clear(sl);
  563. smartlist_split_string(sl, " ab\tc \td ef ", NULL,
  564. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  565. test_eq(4, smartlist_len(sl));
  566. test_streq("ab", smartlist_get(sl,0));
  567. test_streq("c", smartlist_get(sl,1));
  568. test_streq("d", smartlist_get(sl,2));
  569. test_streq("ef", smartlist_get(sl,3));
  570. smartlist_split_string(sl, "ghi\tj", NULL,
  571. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  572. test_eq(6, smartlist_len(sl));
  573. test_streq("ghi", smartlist_get(sl,4));
  574. test_streq("j", smartlist_get(sl,5));
  575. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  576. smartlist_clear(sl);
  577. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  578. test_eq(3, smartlist_len(sl));
  579. test_streq("z", smartlist_get(sl, 0));
  580. test_streq("zhasd", smartlist_get(sl, 1));
  581. test_streq("bnud", smartlist_get(sl, 2));
  582. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  583. test_eq(5, smartlist_len(sl));
  584. test_streq("z", smartlist_get(sl, 3));
  585. test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
  586. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  587. smartlist_clear(sl);
  588. smartlist_split_string(sl, "abcd\n", "\n", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  589. test_eq(1, smartlist_len(sl));
  590. test_streq("abcd", smartlist_get(sl, 0));
  591. smartlist_split_string(sl, "efgh", "\n", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  592. test_eq(2, smartlist_len(sl));
  593. test_streq("efgh", smartlist_get(sl, 1));
  594. /* Test tor_strstrip() */
  595. strcpy(buf, "Testing 1 2 3");
  596. test_eq(0, tor_strstrip(buf, ",!"));
  597. test_streq(buf, "Testing 1 2 3");
  598. strcpy(buf, "!Testing 1 2 3?");
  599. test_eq(5, tor_strstrip(buf, "!? "));
  600. test_streq(buf, "Testing123");
  601. /* Test tor_strpartition() */
  602. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
  603. TERMINATE_IF_EVEN));
  604. test_streq(buf, "abc##def##g");
  605. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefg", "##", 3,
  606. ALWAYS_TERMINATE));
  607. test_streq(buf, "abc##def##g##");
  608. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
  609. TERMINATE_IF_EVEN));
  610. test_streq(buf, "abc##def##ghi##");
  611. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3,
  612. NEVER_TERMINATE));
  613. test_streq(buf, "abc##def##ghi");
  614. /* Test parse_addr_port */
  615. cp = NULL; u32 = 3; u16 = 3;
  616. test_assert(!parse_addr_port("1.2.3.4", &cp, &u32, &u16));
  617. test_streq(cp, "1.2.3.4");
  618. test_eq(u32, 0x01020304u);
  619. test_eq(u16, 0);
  620. tor_free(cp);
  621. test_assert(!parse_addr_port("4.3.2.1:99", &cp, &u32, &u16));
  622. test_streq(cp, "4.3.2.1");
  623. test_eq(u32, 0x04030201u);
  624. test_eq(u16, 99);
  625. tor_free(cp);
  626. test_assert(!parse_addr_port("nonexistent.address:4040", &cp, NULL, &u16));
  627. test_streq(cp, "nonexistent.address");
  628. test_eq(u16, 4040);
  629. tor_free(cp);
  630. test_assert(!parse_addr_port("localhost:9999", &cp, &u32, &u16));
  631. test_streq(cp, "localhost");
  632. test_eq(u32, 0x7f000001u);
  633. test_eq(u16, 9999);
  634. tor_free(cp);
  635. u32 = 3;
  636. test_assert(!parse_addr_port("localhost", NULL, &u32, &u16));
  637. test_eq(cp, NULL);
  638. test_eq(u32, 0x7f000001u);
  639. test_eq(u16, 0);
  640. tor_free(cp);
  641. /* Test tor_parse_long. */
  642. test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
  643. test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
  644. /* Test parse_line_from_str */
  645. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  646. "k2\n"
  647. "k3 \n" "\n" " \n" "#comment\n"
  648. "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf));
  649. cp = buf;
  650. cp = parse_line_from_str(cp, &k, &v);
  651. test_streq(k, "k");
  652. test_streq(v, "v");
  653. test_assert(!strcmpstart(cp, " key value with"));
  654. cp = parse_line_from_str(cp, &k, &v);
  655. test_streq(k, "key");
  656. test_streq(v, "value with spaces");
  657. test_assert(!strcmpstart(cp, "keykey"));
  658. cp = parse_line_from_str(cp, &k, &v);
  659. test_streq(k, "keykey");
  660. test_streq(v, "val");
  661. test_assert(!strcmpstart(cp, "k2\n"));
  662. cp = parse_line_from_str(cp, &k, &v);
  663. test_streq(k, "k2");
  664. test_streq(v, "");
  665. test_assert(!strcmpstart(cp, "k3 \n"));
  666. cp = parse_line_from_str(cp, &k, &v);
  667. test_streq(k, "k3");
  668. test_streq(v, "");
  669. test_assert(!strcmpstart(cp, "\n \n"));
  670. cp = parse_line_from_str(cp, &k, &v);
  671. test_streq(k, "k4");
  672. test_streq(v, "");
  673. test_assert(!strcmpstart(cp, "k5#abc"));
  674. cp = parse_line_from_str(cp, &k, &v);
  675. test_streq(k, "k5");
  676. test_streq(v, "");
  677. test_assert(!strcmpstart(cp, "k6"));
  678. cp = parse_line_from_str(cp, &k, &v);
  679. test_streq(k, "k6");
  680. test_streq(v, "val");
  681. test_streq(cp, "");
  682. /* Test for strcmpstart and strcmpend. */
  683. test_assert(strcmpstart("abcdef", "abcdef")==0);
  684. test_assert(strcmpstart("abcdef", "abc")==0);
  685. test_assert(strcmpstart("abcdef", "abd")<0);
  686. test_assert(strcmpstart("abcdef", "abb")>0);
  687. test_assert(strcmpstart("ab", "abb")<0);
  688. test_assert(strcmpend("abcdef", "abcdef")==0);
  689. test_assert(strcmpend("abcdef", "def")==0);
  690. test_assert(strcmpend("abcdef", "deg")<0);
  691. test_assert(strcmpend("abcdef", "dee")>0);
  692. test_assert(strcmpend("ab", "abb")<0);
  693. {
  694. char tmpbuf[INET_NTOA_BUF_LEN];
  695. struct in_addr in;
  696. tor_inet_aton("18.244.0.188",&in);
  697. tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
  698. test_streq(tmpbuf, "18.244.0.188");
  699. }
  700. /* XXXX test older functions. */
  701. smartlist_free(sl);
  702. }
  703. static void
  704. test_gzip(void)
  705. {
  706. char *buf1, *buf2=NULL, *buf3=NULL;
  707. size_t len1, len2;
  708. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
  709. test_eq(detect_compression_method(buf1, strlen(buf1)), 0);
  710. if (is_gzip_supported()) {
  711. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  712. GZIP_METHOD));
  713. test_assert(buf2);
  714. test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
  715. test_eq(detect_compression_method(buf2, len1), GZIP_METHOD);
  716. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, GZIP_METHOD));
  717. test_assert(buf3);
  718. test_streq(buf1,buf3);
  719. tor_free(buf2);
  720. tor_free(buf3);
  721. }
  722. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  723. ZLIB_METHOD));
  724. test_assert(buf2);
  725. test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
  726. test_eq(detect_compression_method(buf2, len1), ZLIB_METHOD);
  727. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, ZLIB_METHOD));
  728. test_assert(buf3);
  729. test_streq(buf1,buf3);
  730. tor_free(buf2);
  731. tor_free(buf3);
  732. tor_free(buf1);
  733. }
  734. static void *
  735. _squareAndRemoveK4(const char *key, void*val, void *data)
  736. {
  737. int *ip = (int*)data;
  738. intptr_t v;
  739. if (strcmp(key,"K4") == 0) {
  740. ++(*ip);
  741. return NULL;
  742. }
  743. v = (intptr_t)val;
  744. return (void*)(v*v);
  745. }
  746. static void
  747. test_strmap(void)
  748. {
  749. strmap_t *map;
  750. strmap_iter_t *iter;
  751. const char *k;
  752. void *v;
  753. int count;
  754. map = strmap_new();
  755. v = strmap_set(map, "K1", (void*)99);
  756. test_eq(v, NULL);
  757. v = strmap_set(map, "K2", (void*)101);
  758. test_eq(v, NULL);
  759. v = strmap_set(map, "K1", (void*)100);
  760. test_eq(v, (void*)99);
  761. test_eq(strmap_get(map,"K1"), (void*)100);
  762. test_eq(strmap_get(map,"K2"), (void*)101);
  763. test_eq(strmap_get(map,"K-not-there"), NULL);
  764. v = strmap_remove(map,"K2");
  765. test_eq(v, (void*)101);
  766. test_eq(strmap_get(map,"K2"), NULL);
  767. test_eq(strmap_remove(map,"K2"), NULL);
  768. strmap_set(map, "K2", (void*)101);
  769. strmap_set(map, "K3", (void*)102);
  770. strmap_set(map, "K4", (void*)103);
  771. strmap_set(map, "K5", (void*)104);
  772. strmap_set(map, "K6", (void*)105);
  773. count = 0;
  774. strmap_foreach(map, _squareAndRemoveK4, &count);
  775. test_eq(count, 1);
  776. test_eq(strmap_get(map, "K4"), NULL);
  777. test_eq(strmap_get(map, "K1"), (void*)10000);
  778. test_eq(strmap_get(map, "K6"), (void*)11025);
  779. iter = strmap_iter_init(map);
  780. strmap_iter_get(iter,&k,&v);
  781. test_streq(k, "K1");
  782. test_eq(v, (void*)10000);
  783. iter = strmap_iter_next(map,iter);
  784. strmap_iter_get(iter,&k,&v);
  785. test_streq(k, "K2");
  786. test_eq(v, (void*)10201);
  787. iter = strmap_iter_next_rmv(map,iter);
  788. strmap_iter_get(iter,&k,&v);
  789. test_streq(k, "K3");
  790. test_eq(v, (void*)10404);
  791. iter = strmap_iter_next(map,iter); /* K5 */
  792. test_assert(!strmap_iter_done(iter));
  793. iter = strmap_iter_next(map,iter); /* K6 */
  794. test_assert(!strmap_iter_done(iter));
  795. iter = strmap_iter_next(map,iter); /* done */
  796. test_assert(strmap_iter_done(iter));
  797. /* Make sure we removed K2, but not the others. */
  798. test_eq(strmap_get(map, "K2"), NULL);
  799. test_eq(strmap_get(map, "K5"), (void*)10816);
  800. /* Clean up after ourselves. */
  801. strmap_free(map, NULL);
  802. /* Now try some lc functions. */
  803. map = strmap_new();
  804. strmap_set_lc(map,"Ab.C", (void*)1);
  805. test_eq(strmap_get(map,"ab.c"), (void*)1);
  806. test_eq(strmap_get_lc(map,"AB.C"), (void*)1);
  807. test_eq(strmap_get(map,"AB.C"), NULL);
  808. test_eq(strmap_remove_lc(map,"aB.C"), (void*)1);
  809. test_eq(strmap_get_lc(map,"AB.C"), NULL);
  810. strmap_free(map,NULL);
  811. }
  812. static void
  813. test_onion(void)
  814. {
  815. #if 0
  816. char **names;
  817. int i,num;
  818. names = parse_nickname_list(" foo bar\t baz quux ", &num);
  819. test_eq(num,4);
  820. test_streq(names[0],"foo");
  821. test_streq(names[1],"bar");
  822. test_streq(names[2],"baz");
  823. test_streq(names[3],"quux");
  824. for (i=0;i<num;i++)
  825. tor_free(names[i]);
  826. tor_free(names);
  827. #endif
  828. }
  829. static void
  830. test_onion_handshake(void)
  831. {
  832. /* client-side */
  833. crypto_dh_env_t *c_dh = NULL;
  834. char c_buf[ONIONSKIN_CHALLENGE_LEN];
  835. char c_keys[40];
  836. /* server-side */
  837. char s_buf[ONIONSKIN_REPLY_LEN];
  838. char s_keys[40];
  839. /* shared */
  840. crypto_pk_env_t *pk = NULL;
  841. pk = crypto_new_pk_env();
  842. test_assert(! crypto_pk_generate_key(pk));
  843. /* client handshake 1. */
  844. memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
  845. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  846. /* server handshake */
  847. memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
  848. memset(s_keys, 0, 40);
  849. test_assert(! onion_skin_server_handshake(c_buf, pk, NULL, s_buf, s_keys, 40));
  850. /* client handshake 2 */
  851. memset(c_keys, 0, 40);
  852. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  853. crypto_dh_free(c_dh);
  854. if (memcmp(c_keys, s_keys, 40)) {
  855. puts("Aiiiie");
  856. exit(1);
  857. }
  858. test_memeq(c_keys, s_keys, 40);
  859. memset(s_buf, 0, 40);
  860. test_memneq(c_keys, s_buf, 40);
  861. crypto_free_pk_env(pk);
  862. }
  863. static void
  864. test_dir_format(void)
  865. {
  866. char buf[8192], buf2[8192];
  867. char platform[256];
  868. char fingerprint[FINGERPRINT_LEN+1];
  869. char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
  870. size_t pk1_str_len, pk2_str_len, pk3_str_len;
  871. routerinfo_t r1, r2;
  872. crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
  873. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  874. addr_policy_t ex1, ex2;
  875. routerlist_t *dir1 = NULL, *dir2 = NULL;
  876. tor_version_t ver1;
  877. char *bw_lines = NULL;
  878. const char *m;
  879. test_assert( (pk1 = crypto_new_pk_env()) );
  880. test_assert( (pk2 = crypto_new_pk_env()) );
  881. test_assert( (pk3 = crypto_new_pk_env()) );
  882. test_assert(! crypto_pk_generate_key(pk1));
  883. test_assert(! crypto_pk_generate_key(pk2));
  884. test_assert(! crypto_pk_generate_key(pk3));
  885. test_assert( is_legal_nickname("a"));
  886. test_assert(!is_legal_nickname(""));
  887. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  888. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  889. test_assert(!is_legal_nickname("hyphen-")); /* bad char */
  890. test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  891. test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  892. /* valid */
  893. test_assert( is_legal_nickname_or_hexdigest(
  894. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  895. /* too short */
  896. test_assert(!is_legal_nickname_or_hexdigest(
  897. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  898. /* illegal char */
  899. test_assert(!is_legal_nickname_or_hexdigest(
  900. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  901. test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  902. test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  903. test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  904. get_platform_str(platform, sizeof(platform));
  905. memset(&r1,0,sizeof(r1));
  906. memset(&r2,0,sizeof(r2));
  907. r1.address = tor_strdup("18.244.0.1");
  908. r1.addr = 0xc0a80001u; /* 192.168.0.1 */
  909. r1.published_on = 0;
  910. r1.or_port = 9000;
  911. r1.dir_port = 9003;
  912. r1.onion_pkey = pk1;
  913. r1.identity_pkey = pk2;
  914. r1.bandwidthrate = 1000;
  915. r1.bandwidthburst = 5000;
  916. r1.bandwidthcapacity = 10000;
  917. r1.exit_policy = NULL;
  918. r1.nickname = tor_strdup("Magri");
  919. r1.platform = tor_strdup(platform);
  920. ex1.policy_type = ADDR_POLICY_ACCEPT;
  921. ex1.string = NULL;
  922. ex1.addr = 0;
  923. ex1.msk = 0;
  924. ex1.prt_min = ex1.prt_max = 80;
  925. ex1.next = &ex2;
  926. ex2.policy_type = ADDR_POLICY_REJECT;
  927. ex2.addr = 18 << 24;
  928. ex2.msk = 0xFF000000u;
  929. ex2.prt_min = ex2.prt_max = 24;
  930. ex2.next = NULL;
  931. r2.address = tor_strdup("1.1.1.1");
  932. r2.addr = 0x0a030201u; /* 10.3.2.1 */
  933. r2.platform = tor_strdup(platform);
  934. r2.published_on = 5;
  935. r2.or_port = 9005;
  936. r2.dir_port = 0;
  937. r2.onion_pkey = pk2;
  938. r2.identity_pkey = pk1;
  939. r2.bandwidthrate = r2.bandwidthburst = r2.bandwidthcapacity = 3000;
  940. r2.exit_policy = &ex1;
  941. r2.nickname = tor_strdup("Fred");
  942. bw_lines = rep_hist_get_bandwidth_lines();
  943. test_assert(bw_lines);
  944. test_assert(!strcmpstart(bw_lines, "opt write-history "));
  945. test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  946. &pk1_str_len));
  947. test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  948. &pk2_str_len));
  949. test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
  950. &pk3_str_len));
  951. memset(buf, 0, 2048);
  952. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  953. strcpy(buf2, "router Magri 18.244.0.1 9000 0 9003\n"
  954. "platform Tor "VERSION" on ");
  955. strcat(buf2, get_uname());
  956. strcat(buf2, "\n"
  957. "published 1970-01-01 00:00:00\n"
  958. "opt fingerprint ");
  959. test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  960. strcat(buf2, fingerprint);
  961. strcat(buf2, "\nopt uptime 0\n"
  962. /* XXX the "0" above is hardcoded, but even if we made it reflect
  963. * uptime, that still wouldn't make it right, because the two
  964. * descriptors might be made on different seconds... hm. */
  965. "bandwidth 1000 5000 10000\n"
  966. "onion-key\n");
  967. strcat(buf2, pk1_str);
  968. strcat(buf2, "signing-key\n");
  969. strcat(buf2, pk2_str);
  970. strcat(buf2, bw_lines);
  971. strcat(buf2, "router-signature\n");
  972. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same twice*/
  973. test_streq(buf, buf2);
  974. tor_free(bw_lines);
  975. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  976. cp = buf;
  977. rp1 = router_parse_entry_from_string((const char*)cp,NULL);
  978. test_assert(rp1);
  979. test_streq(rp1->address, r1.address);
  980. test_eq(rp1->or_port, r1.or_port);
  981. test_eq(rp1->dir_port, r1.dir_port);
  982. test_eq(rp1->bandwidthrate, r1.bandwidthrate);
  983. test_eq(rp1->bandwidthburst, r1.bandwidthburst);
  984. test_eq(rp1->bandwidthcapacity, r1.bandwidthcapacity);
  985. test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  986. test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  987. test_assert(rp1->exit_policy == NULL);
  988. #if 0
  989. /* XXX Once we have exit policies, test this again. XXX */
  990. strcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n");
  991. strcat(buf2, pk2_str);
  992. strcat(buf2, "signing-key\n");
  993. strcat(buf2, pk1_str);
  994. strcat(buf2, "accept *:80\nreject 18.*:24\n\n");
  995. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
  996. test_streq(buf, buf2);
  997. cp = buf;
  998. rp2 = router_parse_entry_from_string(&cp);
  999. test_assert(rp2);
  1000. test_streq(rp2->address, r2.address);
  1001. test_eq(rp2->or_port, r2.or_port);
  1002. test_eq(rp2->dir_port, r2.dir_port);
  1003. test_eq(rp2->bandwidth, r2.bandwidth);
  1004. test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  1005. test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  1006. test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
  1007. test_streq(rp2->exit_policy->string, "accept *:80");
  1008. test_streq(rp2->exit_policy->address, "*");
  1009. test_streq(rp2->exit_policy->port, "80");
  1010. test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
  1011. test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
  1012. test_streq(rp2->exit_policy->next->address, "18.*");
  1013. test_streq(rp2->exit_policy->next->port, "24");
  1014. test_assert(rp2->exit_policy->next->next == NULL);
  1015. #endif
  1016. /* Okay, now for the directories. */
  1017. crypto_pk_get_fingerprint(pk2, buf, 1);
  1018. add_fingerprint_to_dir("Magri", buf);
  1019. crypto_pk_get_fingerprint(pk1, buf, 1);
  1020. add_fingerprint_to_dir("Fred", buf);
  1021. /* Make sure routers aren't too far in the past any more. */
  1022. r1.published_on = time(NULL);
  1023. r2.published_on = time(NULL)-3*60*60;
  1024. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1025. cp = buf;
  1026. test_eq(dirserv_add_descriptor((const char**)&cp,&m), 1);
  1027. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk1)>0);
  1028. cp = buf;
  1029. test_eq(dirserv_add_descriptor((const char**)&cp,&m), 1);
  1030. get_options()->Nickname = tor_strdup("DirServer");
  1031. test_assert(!dirserv_dump_directory_to_string(&cp,pk3));
  1032. test_assert(!router_parse_routerlist_from_directory(cp, &dir1, pk3, 1, 0));
  1033. test_eq(2, smartlist_len(dir1->routers));
  1034. dirserv_free_fingerprint_list();
  1035. tor_free(cp);
  1036. tor_free(pk1_str);
  1037. tor_free(pk2_str);
  1038. if (pk1) crypto_free_pk_env(pk1);
  1039. if (pk2) crypto_free_pk_env(pk2);
  1040. if (rp1) routerinfo_free(rp1);
  1041. if (rp2) routerinfo_free(rp2);
  1042. tor_free(dir1); /* XXXX And more !*/
  1043. tor_free(dir2); /* And more !*/
  1044. /* Try out version parsing functionality */
  1045. test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
  1046. test_eq(0, ver1.major);
  1047. test_eq(3, ver1.minor);
  1048. test_eq(4, ver1.micro);
  1049. test_eq(VER_PRE, ver1.status);
  1050. test_eq(2, ver1.patchlevel);
  1051. test_eq(IS_CVS, ver1.cvs);
  1052. test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
  1053. test_eq(0, ver1.major);
  1054. test_eq(3, ver1.minor);
  1055. test_eq(4, ver1.micro);
  1056. test_eq(VER_RC, ver1.status);
  1057. test_eq(1, ver1.patchlevel);
  1058. test_eq(IS_NOT_CVS, ver1.cvs);
  1059. test_eq(0, tor_version_parse("1.3.4", &ver1));
  1060. test_eq(1, ver1.major);
  1061. test_eq(3, ver1.minor);
  1062. test_eq(4, ver1.micro);
  1063. test_eq(VER_RELEASE, ver1.status);
  1064. test_eq(0, ver1.patchlevel);
  1065. test_eq(IS_NOT_CVS, ver1.cvs);
  1066. test_eq(0, tor_version_parse("1.3.4.999", &ver1));
  1067. test_eq(1, ver1.major);
  1068. test_eq(3, ver1.minor);
  1069. test_eq(4, ver1.micro);
  1070. test_eq(VER_RELEASE, ver1.status);
  1071. test_eq(999, ver1.patchlevel);
  1072. test_eq(IS_NOT_CVS, ver1.cvs);
  1073. test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
  1074. test_eq(0, ver1.major);
  1075. test_eq(1, ver1.minor);
  1076. test_eq(2, ver1.micro);
  1077. test_eq(4, ver1.patchlevel);
  1078. test_eq(VER_RELEASE, ver1.status);
  1079. test_eq(IS_NOT_CVS, ver1.cvs);
  1080. test_streq("alpha", ver1.status_tag);
  1081. test_eq(0, tor_version_parse("0.1.2.4", &ver1));
  1082. test_eq(0, ver1.major);
  1083. test_eq(1, ver1.minor);
  1084. test_eq(2, ver1.micro);
  1085. test_eq(4, ver1.patchlevel);
  1086. test_eq(VER_RELEASE, ver1.status);
  1087. test_eq(IS_NOT_CVS, ver1.cvs);
  1088. test_streq("", ver1.status_tag);
  1089. /* make sure is_obsolete_version() works */
  1090. test_eq(1, is_obsolete_version("0.0.1", "Tor 0.0.2"));
  1091. test_eq(1, is_obsolete_version("0.0.1", "0.0.2, Tor 0.0.3"));
  1092. test_eq(1, is_obsolete_version("0.0.1", "0.0.2,Tor 0.0.3"));
  1093. test_eq(1, is_obsolete_version("0.0.1", "0.0.3,BetterTor 0.0.1"));
  1094. test_eq(0, is_obsolete_version("0.0.2", "Tor 0.0.2,Tor 0.0.3"));
  1095. test_eq(0, is_obsolete_version("0.0.2", "Tor 0.0.2pre1,Tor 0.0.3"));
  1096. test_eq(1, is_obsolete_version("0.0.2", "Tor 0.0.2.1,Tor 0.0.3"));
  1097. test_eq(0, is_obsolete_version("0.1.0", "Tor 0.0.2,Tor 0.0.3"));
  1098. test_eq(0, is_obsolete_version("0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8"));
  1099. test_eq(0, is_obsolete_version("0.0.5", "0.0.5-cvs"));
  1100. test_eq(0, is_obsolete_version("0.0.5.1-cvs", "0.0.5"));
  1101. /* Not on list, but newer than any in same series. */
  1102. test_eq(0, is_obsolete_version("0.1.0.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0"));
  1103. /* Series newer than any on list. */
  1104. test_eq(0, is_obsolete_version("0.1.1.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0"));
  1105. /* Series older than any on list. */
  1106. test_eq(1, is_obsolete_version("0.0.1.3", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0"));
  1107. /* Not on list, not newer than any on same series. */
  1108. test_eq(1, is_obsolete_version("0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0"));
  1109. /* On list, not newer than any on same series. */
  1110. test_eq(1, is_obsolete_version("0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0"));
  1111. test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  1112. test_eq(1, tor_version_as_new_as(
  1113. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0.sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8rc2"));
  1114. test_eq(0, tor_version_as_new_as(
  1115. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0.sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
  1116. }
  1117. static void
  1118. test_rend_fns(void)
  1119. {
  1120. char address1[] = "fooaddress.onion";
  1121. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  1122. char address3[] = "fooaddress.exit";
  1123. rend_service_descriptor_t *d1, *d2;
  1124. char *encoded;
  1125. size_t len;
  1126. crypto_pk_env_t *pk1;
  1127. time_t now;
  1128. pk1 = crypto_new_pk_env();
  1129. test_assert(!crypto_pk_generate_key(pk1));
  1130. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  1131. d1->pk = pk1;
  1132. now = time(NULL);
  1133. d1->timestamp = now;
  1134. d1->n_intro_points = 3;
  1135. d1->intro_points = tor_malloc(sizeof(char*)*3);
  1136. d1->intro_points[0] = tor_strdup("tom");
  1137. d1->intro_points[1] = tor_strdup("crow");
  1138. d1->intro_points[2] = tor_strdup("joel");
  1139. test_assert(! rend_encode_service_descriptor(d1, pk1, &encoded, &len));
  1140. d2 = rend_parse_service_descriptor(encoded, len);
  1141. test_assert(d2);
  1142. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  1143. test_eq(d2->timestamp, now);
  1144. test_eq(d2->n_intro_points, 3);
  1145. test_streq(d2->intro_points[0], "tom");
  1146. test_streq(d2->intro_points[1], "crow");
  1147. test_streq(d2->intro_points[2], "joel");
  1148. test_eq(NORMAL_HOSTNAME, parse_extended_hostname(address1));
  1149. test_eq(ONION_HOSTNAME, parse_extended_hostname(address2));
  1150. test_eq(EXIT_HOSTNAME, parse_extended_hostname(address3));
  1151. rend_service_descriptor_free(d1);
  1152. rend_service_descriptor_free(d2);
  1153. }
  1154. int
  1155. main(int c, char**v) {
  1156. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  1157. network_init();
  1158. options_init(options);
  1159. set_options(options);
  1160. crypto_seed_rng();
  1161. setup_directory();
  1162. rep_hist_init();
  1163. atexit(remove_directory);
  1164. printf("Running Tor unit tests on %s\n", get_uname());
  1165. // puts("========================== Buffers =========================");
  1166. if (0) test_buffers();
  1167. puts("\n========================== Crypto ==========================");
  1168. // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
  1169. test_crypto();
  1170. test_crypto_dh();
  1171. puts("\n========================= Util ============================");
  1172. test_gzip();
  1173. test_util();
  1174. test_strmap();
  1175. puts("\n========================= Onion Skins =====================");
  1176. test_onion();
  1177. test_onion_handshake();
  1178. puts("\n========================= Directory Formats ===============");
  1179. test_dir_format();
  1180. puts("\n========================= Rendezvous functionality ========");
  1181. test_rend_fns();
  1182. puts("");
  1183. if (have_failed)
  1184. return 1;
  1185. else
  1186. return 0;
  1187. }