test.c 38 KB

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