test.c 61 KB

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