test.c 51 KB

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