test.c 36 KB

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