test.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582
  1. /* Copyright 2001-2004 Roger Dingledine.
  2. * Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char test_c_id[] =
  6. "$Id$";
  7. const char tor_svn_revision[] = "";
  8. /**
  9. * \file test.c
  10. * \brief Unit tests for many pieces of the lower level Tor modules.
  11. **/
  12. #include "orconfig.h"
  13. #include <stdio.h>
  14. #ifdef HAVE_FCNTL_H
  15. #include <fcntl.h>
  16. #endif
  17. #ifdef MS_WINDOWS
  18. /* For mkdir() */
  19. #include <direct.h>
  20. #else
  21. #include <dirent.h>
  22. #endif
  23. /* These macros pull in declarations for some functions and structures that
  24. * are typically file-private. */
  25. #define CONFIG_PRIVATE
  26. #define CONTROL_PRIVATE
  27. #define CRYPTO_PRIVATE
  28. #define DIRSERV_PRIVATE
  29. #define MEMPOOL_PRIVATE
  30. #define ROUTER_PRIVATE
  31. #include "or.h"
  32. #include "../common/test.h"
  33. #include "../common/torgzip.h"
  34. #include "../common/mempool.h"
  35. int have_failed = 0;
  36. static char temp_dir[256];
  37. static void
  38. setup_directory(void)
  39. {
  40. static int is_setup = 0;
  41. int r;
  42. if (is_setup) return;
  43. #ifdef MS_WINDOWS
  44. // XXXX
  45. tor_snprintf(temp_dir, sizeof(temp_dir),
  46. "c:\\windows\\temp\\tor_test_%d", (int)getpid());
  47. r = mkdir(temp_dir);
  48. #else
  49. tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
  50. r = mkdir(temp_dir, 0700);
  51. #endif
  52. if (r) {
  53. fprintf(stderr, "Can't create directory %s:", temp_dir);
  54. perror("");
  55. exit(1);
  56. }
  57. is_setup = 1;
  58. }
  59. static const char *
  60. get_fname(const char *name)
  61. {
  62. static char buf[1024];
  63. setup_directory();
  64. tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
  65. return buf;
  66. }
  67. static void
  68. remove_directory(void)
  69. {
  70. smartlist_t *elements = tor_listdir(temp_dir);
  71. if (elements) {
  72. SMARTLIST_FOREACH(elements, const char *, cp,
  73. {
  74. size_t len = strlen(cp)+strlen(temp_dir)+16;
  75. char *tmp = tor_malloc(len);
  76. tor_snprintf(tmp, len, "%s"PATH_SEPARATOR"%s", temp_dir, cp);
  77. unlink(tmp);
  78. tor_free(tmp);
  79. });
  80. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  81. smartlist_free(elements);
  82. }
  83. rmdir(temp_dir);
  84. }
  85. static crypto_pk_env_t *
  86. pk_generate(int idx)
  87. {
  88. static crypto_pk_env_t *pregen[3] = {NULL, NULL, NULL};
  89. tor_assert(idx < (int)(sizeof(pregen)/sizeof(pregen[0])));
  90. if (! pregen[idx]) {
  91. pregen[idx] = crypto_new_pk_env();
  92. tor_assert(!crypto_pk_generate_key(pregen[idx]));
  93. }
  94. return crypto_pk_dup_key(pregen[idx]);
  95. }
  96. static void
  97. test_buffers(void)
  98. {
  99. char str[256];
  100. char str2[256];
  101. buf_t *buf, *buf2;
  102. int j;
  103. size_t r;
  104. /****
  105. * buf_new
  106. ****/
  107. if (!(buf = buf_new()))
  108. test_fail();
  109. test_eq(buf_capacity(buf), 4096);
  110. test_eq(buf_datalen(buf), 0);
  111. /****
  112. * General pointer frobbing
  113. */
  114. for (j=0;j<256;++j) {
  115. str[j] = (char)j;
  116. }
  117. write_to_buf(str, 256, buf);
  118. write_to_buf(str, 256, buf);
  119. test_eq(buf_datalen(buf), 512);
  120. fetch_from_buf(str2, 200, buf);
  121. test_memeq(str, str2, 200);
  122. test_eq(buf_datalen(buf), 312);
  123. memset(str2, 0, sizeof(str2));
  124. fetch_from_buf(str2, 256, buf);
  125. test_memeq(str+200, str2, 56);
  126. test_memeq(str, str2+56, 200);
  127. test_eq(buf_datalen(buf), 56);
  128. memset(str2, 0, sizeof(str2));
  129. /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
  130. * another 3584 bytes, we hit the end. */
  131. for (j=0;j<15;++j) {
  132. write_to_buf(str, 256, buf);
  133. }
  134. assert_buf_ok(buf);
  135. test_eq(buf_datalen(buf), 3896);
  136. fetch_from_buf(str2, 56, buf);
  137. test_eq(buf_datalen(buf), 3840);
  138. test_memeq(str+200, str2, 56);
  139. for (j=0;j<15;++j) {
  140. memset(str2, 0, sizeof(str2));
  141. fetch_from_buf(str2, 256, buf);
  142. test_memeq(str, str2, 256);
  143. }
  144. test_eq(buf_datalen(buf), 0);
  145. buf_free(buf);
  146. /* Okay, now make sure growing can work. */
  147. buf = buf_new_with_capacity(16);
  148. test_eq(buf_capacity(buf), 16);
  149. write_to_buf(str+1, 255, buf);
  150. test_eq(buf_capacity(buf), 256);
  151. fetch_from_buf(str2, 254, buf);
  152. test_memeq(str+1, str2, 254);
  153. test_eq(buf_capacity(buf), 256);
  154. assert_buf_ok(buf);
  155. write_to_buf(str, 32, buf);
  156. test_eq(buf_capacity(buf), 256);
  157. assert_buf_ok(buf);
  158. write_to_buf(str, 256, buf);
  159. assert_buf_ok(buf);
  160. test_eq(buf_capacity(buf), 512);
  161. test_eq(buf_datalen(buf), 33+256);
  162. fetch_from_buf(str2, 33, buf);
  163. test_eq(*str2, str[255]);
  164. test_memeq(str2+1, str, 32);
  165. test_eq(buf_capacity(buf), 512);
  166. test_eq(buf_datalen(buf), 256);
  167. fetch_from_buf(str2, 256, buf);
  168. test_memeq(str, str2, 256);
  169. /* now try shrinking: case 1. */
  170. buf_free(buf);
  171. buf = buf_new_with_capacity(33668);
  172. for (j=0;j<67;++j) {
  173. write_to_buf(str,255, buf);
  174. }
  175. test_eq(buf_capacity(buf), 33668);
  176. test_eq(buf_datalen(buf), 17085);
  177. for (j=0; j < 40; ++j) {
  178. fetch_from_buf(str2, 255,buf);
  179. test_memeq(str2, str, 255);
  180. }
  181. /* now try shrinking: case 2. */
  182. buf_free(buf);
  183. buf = buf_new_with_capacity(33668);
  184. for (j=0;j<67;++j) {
  185. write_to_buf(str,255, buf);
  186. }
  187. for (j=0; j < 20; ++j) {
  188. fetch_from_buf(str2, 255,buf);
  189. test_memeq(str2, str, 255);
  190. }
  191. for (j=0;j<80;++j) {
  192. write_to_buf(str,255, buf);
  193. }
  194. test_eq(buf_capacity(buf),33668);
  195. for (j=0; j < 120; ++j) {
  196. fetch_from_buf(str2, 255,buf);
  197. test_memeq(str2, str, 255);
  198. }
  199. /* Move from buf to buf. */
  200. buf_free(buf);
  201. buf = buf_new_with_capacity(4096);
  202. buf2 = buf_new_with_capacity(4096);
  203. for (j=0;j<100;++j)
  204. write_to_buf(str, 255, buf);
  205. test_eq(buf_datalen(buf), 25500);
  206. for (j=0;j<100;++j) {
  207. r = 10;
  208. move_buf_to_buf(buf2, buf, &r);
  209. test_eq(r, 0);
  210. }
  211. test_eq(buf_datalen(buf), 24500);
  212. test_eq(buf_datalen(buf2), 1000);
  213. for (j=0;j<3;++j) {
  214. fetch_from_buf(str2, 255, buf2);
  215. test_memeq(str2, str, 255);
  216. }
  217. r = 8192; /*big move*/
  218. move_buf_to_buf(buf2, buf, &r);
  219. test_eq(r, 0);
  220. r = 30000; /* incomplete move */
  221. move_buf_to_buf(buf2, buf, &r);
  222. test_eq(r, 13692);
  223. for (j=0;j<97;++j) {
  224. fetch_from_buf(str2, 255, buf2);
  225. test_memeq(str2, str, 255);
  226. }
  227. buf_free(buf);
  228. buf_free(buf2);
  229. #if 0
  230. {
  231. int s;
  232. int eof;
  233. int i;
  234. buf_t *buf2;
  235. /****
  236. * read_to_buf
  237. ****/
  238. s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
  239. write(s, str, 256);
  240. close(s);
  241. s = open(get_fname("data"), O_RDONLY, 0);
  242. eof = 0;
  243. errno = 0; /* XXXX */
  244. i = read_to_buf(s, 10, buf, &eof);
  245. printf("%s\n", strerror(errno));
  246. test_eq(i, 10);
  247. test_eq(eof, 0);
  248. test_eq(buf_capacity(buf), 4096);
  249. test_eq(buf_datalen(buf), 10);
  250. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
  251. /* Test reading 0 bytes. */
  252. i = read_to_buf(s, 0, buf, &eof);
  253. test_eq(buf_capacity(buf), 512*1024);
  254. test_eq(buf_datalen(buf), 10);
  255. test_eq(eof, 0);
  256. test_eq(i, 0);
  257. /* Now test when buffer is filled exactly. */
  258. buf2 = buf_new_with_capacity(6);
  259. i = read_to_buf(s, 6, buf2, &eof);
  260. test_eq(buf_capacity(buf2), 6);
  261. test_eq(buf_datalen(buf2), 6);
  262. test_eq(eof, 0);
  263. test_eq(i, 6);
  264. test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
  265. buf_free(buf2);
  266. /* Now test when buffer is filled with more data to read. */
  267. buf2 = buf_new_with_capacity(32);
  268. i = read_to_buf(s, 128, buf2, &eof);
  269. test_eq(buf_capacity(buf2), 128);
  270. test_eq(buf_datalen(buf2), 32);
  271. test_eq(eof, 0);
  272. test_eq(i, 32);
  273. buf_free(buf2);
  274. /* Now read to eof. */
  275. test_assert(buf_capacity(buf) > 256);
  276. i = read_to_buf(s, 1024, buf, &eof);
  277. test_eq(i, (256-32-10-6));
  278. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  279. test_eq(buf_datalen(buf), 256-6-32);
  280. test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
  281. test_eq(eof, 0);
  282. i = read_to_buf(s, 1024, buf, &eof);
  283. test_eq(i, 0);
  284. test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  285. test_eq(buf_datalen(buf), 256-6-32);
  286. test_eq(eof, 1);
  287. }
  288. #endif
  289. }
  290. static void
  291. test_crypto_dh(void)
  292. {
  293. crypto_dh_env_t *dh1, *dh2;
  294. char p1[DH_BYTES];
  295. char p2[DH_BYTES];
  296. char s1[DH_BYTES];
  297. char s2[DH_BYTES];
  298. int s1len, s2len;
  299. dh1 = crypto_dh_new();
  300. dh2 = crypto_dh_new();
  301. test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
  302. test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
  303. memset(p1, 0, DH_BYTES);
  304. memset(p2, 0, DH_BYTES);
  305. test_memeq(p1, p2, DH_BYTES);
  306. test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  307. test_memneq(p1, p2, DH_BYTES);
  308. test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  309. test_memneq(p1, p2, DH_BYTES);
  310. memset(s1, 0, DH_BYTES);
  311. memset(s2, 0xFF, DH_BYTES);
  312. s1len = crypto_dh_compute_secret(dh1, p2, DH_BYTES, s1, 50);
  313. s2len = crypto_dh_compute_secret(dh2, p1, DH_BYTES, s2, 50);
  314. test_assert(s1len > 0);
  315. test_eq(s1len, s2len);
  316. test_memeq(s1, s2, s1len);
  317. crypto_dh_free(dh1);
  318. crypto_dh_free(dh2);
  319. }
  320. static void
  321. test_crypto(void)
  322. {
  323. crypto_cipher_env_t *env1, *env2;
  324. crypto_pk_env_t *pk1, *pk2;
  325. char *data1, *data2, *data3, *cp;
  326. int i, j, p, len;
  327. size_t size;
  328. data1 = tor_malloc(1024);
  329. data2 = tor_malloc(1024);
  330. data3 = tor_malloc(1024);
  331. test_assert(data1 && data2 && data3);
  332. /* Try out RNG. */
  333. test_assert(! crypto_seed_rng());
  334. crypto_rand(data1, 100);
  335. crypto_rand(data2, 100);
  336. test_memneq(data1,data2,100);
  337. /* Now, test encryption and decryption with stream cipher. */
  338. data1[0]='\0';
  339. for (i = 1023; i>0; i -= 35)
  340. strncat(data1, "Now is the time for all good onions", i);
  341. memset(data2, 0, 1024);
  342. memset(data3, 0, 1024);
  343. env1 = crypto_new_cipher_env();
  344. test_neq(env1, 0);
  345. env2 = crypto_new_cipher_env();
  346. test_neq(env2, 0);
  347. j = crypto_cipher_generate_key(env1);
  348. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  349. crypto_cipher_encrypt_init_cipher(env1);
  350. crypto_cipher_decrypt_init_cipher(env2);
  351. /* Try encrypting 512 chars. */
  352. crypto_cipher_encrypt(env1, data2, data1, 512);
  353. crypto_cipher_decrypt(env2, data3, data2, 512);
  354. test_memeq(data1, data3, 512);
  355. test_memneq(data1, data2, 512);
  356. /* Now encrypt 1 at a time, and get 1 at a time. */
  357. for (j = 512; j < 560; ++j) {
  358. crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
  359. }
  360. for (j = 512; j < 560; ++j) {
  361. crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
  362. }
  363. test_memeq(data1, data3, 560);
  364. /* Now encrypt 3 at a time, and get 5 at a time. */
  365. for (j = 560; j < 1024-5; j += 3) {
  366. crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
  367. }
  368. for (j = 560; j < 1024-5; j += 5) {
  369. crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
  370. }
  371. test_memeq(data1, data3, 1024-5);
  372. /* Now make sure that when we encrypt with different chunk sizes, we get
  373. the same results. */
  374. crypto_free_cipher_env(env2);
  375. memset(data3, 0, 1024);
  376. env2 = crypto_new_cipher_env();
  377. test_neq(env2, 0);
  378. crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  379. crypto_cipher_encrypt_init_cipher(env2);
  380. for (j = 0; j < 1024-16; j += 17) {
  381. crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
  382. }
  383. for (j= 0; j < 1024-16; ++j) {
  384. if (data2[j] != data3[j]) {
  385. printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
  386. }
  387. }
  388. test_memeq(data2, data3, 1024-16);
  389. crypto_free_cipher_env(env1);
  390. crypto_free_cipher_env(env2);
  391. /* Test vectors for stream ciphers. */
  392. /* XXXX Look up some test vectors for the ciphers and make sure we match. */
  393. /* Test SHA-1 with a test vector from the specification. */
  394. i = crypto_digest(data1, "abc", 3);
  395. test_memeq(data1,
  396. "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
  397. "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
  398. /* Public-key ciphers */
  399. pk1 = pk_generate(0);
  400. pk2 = crypto_new_pk_env();
  401. test_assert(pk1 && pk2);
  402. test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &size));
  403. test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, size));
  404. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  405. tor_free(cp);
  406. test_eq(128, crypto_pk_keysize(pk1));
  407. test_eq(128, crypto_pk_keysize(pk2));
  408. test_eq(128, crypto_pk_public_encrypt(pk2, data1, "Hello whirled.", 15,
  409. PK_PKCS1_OAEP_PADDING));
  410. test_eq(128, crypto_pk_public_encrypt(pk1, data2, "Hello whirled.", 15,
  411. PK_PKCS1_OAEP_PADDING));
  412. /* oaep padding should make encryption not match */
  413. test_memneq(data1, data2, 128);
  414. test_eq(15, crypto_pk_private_decrypt(pk1, data3, data1, 128,
  415. PK_PKCS1_OAEP_PADDING,1));
  416. test_streq(data3, "Hello whirled.");
  417. memset(data3, 0, 1024);
  418. test_eq(15, crypto_pk_private_decrypt(pk1, data3, data2, 128,
  419. PK_PKCS1_OAEP_PADDING,1));
  420. test_streq(data3, "Hello whirled.");
  421. /* Can't decrypt with public key. */
  422. test_eq(-1, crypto_pk_private_decrypt(pk2, data3, data2, 128,
  423. PK_PKCS1_OAEP_PADDING,1));
  424. /* Try again with bad padding */
  425. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  426. test_eq(-1, crypto_pk_private_decrypt(pk1, data3, data2, 128,
  427. PK_PKCS1_OAEP_PADDING,1));
  428. /* File operations: save and load private key */
  429. test_assert(! crypto_pk_write_private_key_to_filename(pk1,
  430. get_fname("pkey1")));
  431. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  432. get_fname("pkey1")));
  433. test_eq(15, crypto_pk_private_decrypt(pk2, data3, data1, 128,
  434. PK_PKCS1_OAEP_PADDING,1));
  435. /* Now try signing. */
  436. strlcpy(data1, "Ossifrage", 1024);
  437. test_eq(128, crypto_pk_private_sign(pk1, data2, data1, 10));
  438. test_eq(10, crypto_pk_public_checksig(pk1, data3, data2, 128));
  439. test_streq(data3, "Ossifrage");
  440. /* Try signing digests. */
  441. test_eq(128, crypto_pk_private_sign_digest(pk1, data2, data1, 10));
  442. test_eq(20, crypto_pk_public_checksig(pk1, data3, data2, 128));
  443. test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  444. test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  445. /*XXXX test failed signing*/
  446. /* Try encoding */
  447. crypto_free_pk_env(pk2);
  448. pk2 = NULL;
  449. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  450. test_assert(i>0);
  451. pk2 = crypto_pk_asn1_decode(data1, i);
  452. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  453. /* Try with hybrid encryption wrappers. */
  454. crypto_rand(data1, 1024);
  455. for (i = 0; i < 3; ++i) {
  456. for (j = 85; j < 140; ++j) {
  457. memset(data2,0,1024);
  458. memset(data3,0,1024);
  459. if (i == 0 && j < 129)
  460. continue;
  461. p = (i==0)?PK_NO_PADDING:
  462. (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
  463. len = crypto_pk_public_hybrid_encrypt(pk1,data2,data1,j,p,0);
  464. test_assert(len>=0);
  465. len = crypto_pk_private_hybrid_decrypt(pk1,data3,data2,len,p,1);
  466. test_eq(len,j);
  467. test_memeq(data1,data3,j);
  468. }
  469. }
  470. crypto_free_pk_env(pk1);
  471. crypto_free_pk_env(pk2);
  472. /* Base64 tests */
  473. strlcpy(data1, "Test string that contains 35 chars.", 1024);
  474. strlcat(data1, " 2nd string that contains 35 chars.", 1024);
  475. i = base64_encode(data2, 1024, data1, 71);
  476. j = base64_decode(data3, 1024, data2, i);
  477. test_streq(data3, data1);
  478. test_eq(j, 71);
  479. test_assert(data2[i] == '\0');
  480. crypto_rand(data1, DIGEST_LEN);
  481. memset(data2, 100, 1024);
  482. digest_to_base64(data2, data1);
  483. test_eq(BASE64_DIGEST_LEN, strlen(data2));
  484. test_eq(100, data2[BASE64_DIGEST_LEN+2]);
  485. memset(data3, 99, 1024);
  486. digest_from_base64(data3, data2);
  487. test_memeq(data1, data3, DIGEST_LEN);
  488. test_eq(99, data3[DIGEST_LEN+1]);
  489. /* Base32 tests */
  490. strlcpy(data1, "5chrs", 1024);
  491. /* bit pattern is: [35 63 68 72 73] ->
  492. * [00110101 01100011 01101000 01110010 01110011]
  493. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  494. */
  495. base32_encode(data2, 9, data1, 5);
  496. test_streq(data2, "gvrwq4tt");
  497. strlcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
  498. base32_encode(data2, 30, data1, 10);
  499. test_streq(data2, "772w2rfobvomsywe");
  500. /* Base16 tests */
  501. strlcpy(data1, "6chrs\xff", 1024);
  502. base16_encode(data2, 13, data1, 6);
  503. test_streq(data2, "3663687273FF");
  504. strlcpy(data1, "f0d678affc000100", 1024);
  505. i = base16_decode(data2, 8, data1, 16);
  506. test_eq(i,0);
  507. test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  508. /* now try some failing base16 decodes */
  509. test_eq(-1, base16_decode(data2, 8, data1, 15)); /* odd input len */
  510. test_eq(-1, base16_decode(data2, 7, data1, 16)); /* dest too short */
  511. strlcpy(data1, "f0dz!8affc000100", 1024);
  512. test_eq(-1, base16_decode(data2, 8, data1, 16));
  513. tor_free(data1);
  514. tor_free(data2);
  515. tor_free(data3);
  516. }
  517. static void
  518. test_crypto_s2k(void)
  519. {
  520. char buf[29];
  521. char buf2[29];
  522. char *buf3;
  523. int i;
  524. memset(buf, 0, sizeof(buf));
  525. memset(buf2, 0, sizeof(buf2));
  526. buf3 = tor_malloc(65536);
  527. memset(buf3, 0, 65536);
  528. secret_to_key(buf+9, 20, "", 0, buf);
  529. crypto_digest(buf2+9, buf3, 1024);
  530. test_memeq(buf, buf2, 29);
  531. memcpy(buf,"vrbacrda",8);
  532. memcpy(buf2,"vrbacrda",8);
  533. buf[8] = 96;
  534. buf2[8] = 96;
  535. secret_to_key(buf+9, 20, "12345678", 8, buf);
  536. for (i = 0; i < 65536; i += 16) {
  537. memcpy(buf3+i, "vrbacrda12345678", 16);
  538. }
  539. crypto_digest(buf2+9, buf3, 65536);
  540. test_memeq(buf, buf2, 29);
  541. }
  542. static int
  543. _compare_strs(const void **a, const void **b)
  544. {
  545. const char *s1 = *a, *s2 = *b;
  546. return strcmp(s1, s2);
  547. }
  548. static int
  549. _compare_without_first_ch(const void *a, const void **b)
  550. {
  551. const char *s1 = a, *s2 = *b;
  552. return strcasecmp(s1+1, s2);
  553. }
  554. static void
  555. test_util(void)
  556. {
  557. struct timeval start, end;
  558. struct tm a_time;
  559. char timestr[RFC1123_TIME_LEN+1];
  560. char buf[1024];
  561. time_t t_res;
  562. int i;
  563. uint32_t u32;
  564. uint16_t u16;
  565. char *cp, *k, *v;
  566. start.tv_sec = 5;
  567. start.tv_usec = 5000;
  568. end.tv_sec = 5;
  569. end.tv_usec = 5000;
  570. test_eq(0L, tv_udiff(&start, &end));
  571. end.tv_usec = 7000;
  572. test_assert(tv_cmp(&start, &end)<0);
  573. test_assert(tv_cmp(&end, &start)>0);
  574. test_assert(tv_cmp(&end, &end)==0);
  575. test_eq(2000L, tv_udiff(&start, &end));
  576. end.tv_sec = 6;
  577. test_eq(1002000L, tv_udiff(&start, &end));
  578. end.tv_usec = 0;
  579. test_eq(995000L, tv_udiff(&start, &end));
  580. end.tv_sec = 4;
  581. test_eq(-1005000L, tv_udiff(&start, &end));
  582. tv_addms(&end, 5090);
  583. test_eq(end.tv_sec, 9);
  584. test_eq(end.tv_usec, 90000);
  585. end.tv_usec = 999990;
  586. start.tv_sec = 1;
  587. start.tv_usec = 500;
  588. tv_add(&start, &end);
  589. test_eq(start.tv_sec, 11);
  590. test_eq(start.tv_usec, 490);
  591. /* The test values here are confirmed to be correct on a platform
  592. * with a working timegm. */
  593. a_time.tm_year = 2003-1900;
  594. a_time.tm_mon = 7;
  595. a_time.tm_mday = 30;
  596. a_time.tm_hour = 6;
  597. a_time.tm_min = 14;
  598. a_time.tm_sec = 55;
  599. test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  600. a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  601. test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  602. a_time.tm_mon = 1; /* Try a leap year, in feb. */
  603. a_time.tm_mday = 10;
  604. test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
  605. format_rfc1123_time(timestr, 0);
  606. test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
  607. format_rfc1123_time(timestr, (time_t)1091580502UL);
  608. test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
  609. t_res = 0;
  610. i = parse_rfc1123_time(timestr, &t_res);
  611. test_eq(i,0);
  612. test_eq(t_res, (time_t)1091580502UL);
  613. test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
  614. tor_gettimeofday(&start);
  615. /* Test tor_strstrip() */
  616. strlcpy(buf, "Testing 1 2 3", sizeof(buf));
  617. test_eq(0, tor_strstrip(buf, ",!"));
  618. test_streq(buf, "Testing 1 2 3");
  619. strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
  620. test_eq(5, tor_strstrip(buf, "!? "));
  621. test_streq(buf, "Testing123");
  622. /* Test tor_strpartition() */
  623. test_assert(! tor_strpartition(buf, sizeof(buf), "abcdefghi", "##", 3));
  624. test_streq(buf, "abc##def##ghi");
  625. /* Test parse_addr_port */
  626. cp = NULL; u32 = 3; u16 = 3;
  627. test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
  628. test_streq(cp, "1.2.3.4");
  629. test_eq(u32, 0x01020304u);
  630. test_eq(u16, 0);
  631. tor_free(cp);
  632. test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
  633. test_streq(cp, "4.3.2.1");
  634. test_eq(u32, 0x04030201u);
  635. test_eq(u16, 99);
  636. tor_free(cp);
  637. test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
  638. &cp, NULL, &u16));
  639. test_streq(cp, "nonexistent.address");
  640. test_eq(u16, 4040);
  641. tor_free(cp);
  642. test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
  643. test_streq(cp, "localhost");
  644. test_eq(u32, 0x7f000001u);
  645. test_eq(u16, 9999);
  646. tor_free(cp);
  647. u32 = 3;
  648. test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
  649. test_eq(cp, NULL);
  650. test_eq(u32, 0x7f000001u);
  651. test_eq(u16, 0);
  652. tor_free(cp);
  653. test_eq(0, addr_mask_get_bits(0x0u));
  654. test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
  655. test_eq(16, addr_mask_get_bits(0xFFFF0000u));
  656. test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
  657. test_eq(1, addr_mask_get_bits(0x80000000u));
  658. /* Test tor_parse_long. */
  659. test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
  660. test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
  661. test_eq(-50L, tor_parse_long("-50",10,-100,100,NULL,NULL));
  662. /* Test tor_parse_ulong */
  663. test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL,NULL));
  664. test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL,NULL));
  665. /* Test tor_parse_uint64. */
  666. test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
  667. test_assert(i == 1);
  668. test_streq(cp, " x");
  669. test_assert(U64_LITERAL(12345678901) ==
  670. tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
  671. test_assert(i == 1);
  672. test_streq(cp, "");
  673. test_assert(U64_LITERAL(0) ==
  674. tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
  675. test_assert(i == 0);
  676. /* Test printf with uint64 */
  677. tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
  678. U64_PRINTF_ARG(U64_LITERAL(12345678901)));
  679. test_streq(buf, "x!12345678901!x");
  680. /* Test parse_line_from_str */
  681. strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
  682. "k2\n"
  683. "k3 \n" "\n" " \n" "#comment\n"
  684. "k4#a\n" "k5#abc\n" "k6 val #with comment\n", sizeof(buf));
  685. cp = buf;
  686. cp = parse_line_from_str(cp, &k, &v);
  687. test_streq(k, "k");
  688. test_streq(v, "v");
  689. test_assert(!strcmpstart(cp, " key value with"));
  690. cp = parse_line_from_str(cp, &k, &v);
  691. test_streq(k, "key");
  692. test_streq(v, "value with spaces");
  693. test_assert(!strcmpstart(cp, "keykey"));
  694. cp = parse_line_from_str(cp, &k, &v);
  695. test_streq(k, "keykey");
  696. test_streq(v, "val");
  697. test_assert(!strcmpstart(cp, "k2\n"));
  698. cp = parse_line_from_str(cp, &k, &v);
  699. test_streq(k, "k2");
  700. test_streq(v, "");
  701. test_assert(!strcmpstart(cp, "k3 \n"));
  702. cp = parse_line_from_str(cp, &k, &v);
  703. test_streq(k, "k3");
  704. test_streq(v, "");
  705. test_assert(!strcmpstart(cp, "\n \n"));
  706. cp = parse_line_from_str(cp, &k, &v);
  707. test_streq(k, "k4");
  708. test_streq(v, "");
  709. test_assert(!strcmpstart(cp, "k5#abc"));
  710. cp = parse_line_from_str(cp, &k, &v);
  711. test_streq(k, "k5");
  712. test_streq(v, "");
  713. test_assert(!strcmpstart(cp, "k6"));
  714. cp = parse_line_from_str(cp, &k, &v);
  715. test_streq(k, "k6");
  716. test_streq(v, "val");
  717. test_streq(cp, "");
  718. /* Test for strcmpstart and strcmpend. */
  719. test_assert(strcmpstart("abcdef", "abcdef")==0);
  720. test_assert(strcmpstart("abcdef", "abc")==0);
  721. test_assert(strcmpstart("abcdef", "abd")<0);
  722. test_assert(strcmpstart("abcdef", "abb")>0);
  723. test_assert(strcmpstart("ab", "abb")<0);
  724. test_assert(strcmpend("abcdef", "abcdef")==0);
  725. test_assert(strcmpend("abcdef", "def")==0);
  726. test_assert(strcmpend("abcdef", "deg")<0);
  727. test_assert(strcmpend("abcdef", "dee")>0);
  728. test_assert(strcmpend("ab", "abb")<0);
  729. test_assert(strcasecmpend("AbcDEF", "abcdef")==0);
  730. test_assert(strcasecmpend("abcdef", "dEF")==0);
  731. test_assert(strcasecmpend("abcDEf", "deg")<0);
  732. test_assert(strcasecmpend("abcdef", "DEE")>0);
  733. test_assert(strcasecmpend("ab", "abB")<0);
  734. /* Test mem_is_zero */
  735. memset(buf,0,128);
  736. buf[128] = 'x';
  737. test_assert(tor_digest_is_zero(buf));
  738. test_assert(tor_mem_is_zero(buf, 10));
  739. test_assert(tor_mem_is_zero(buf, 20));
  740. test_assert(tor_mem_is_zero(buf, 128));
  741. test_assert(!tor_mem_is_zero(buf, 129));
  742. buf[60] = (char)255;
  743. test_assert(!tor_mem_is_zero(buf, 128));
  744. buf[0] = (char)1;
  745. test_assert(!tor_mem_is_zero(buf, 10));
  746. /* Test inet_ntoa */
  747. {
  748. char tmpbuf[INET_NTOA_BUF_LEN];
  749. struct in_addr in;
  750. tor_inet_aton("18.244.0.188",&in);
  751. tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
  752. test_streq(tmpbuf, "18.244.0.188");
  753. }
  754. /* Test 'escaped' */
  755. test_streq("\"\"", escaped(""));
  756. test_streq("\"abcd\"", escaped("abcd"));
  757. test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
  758. test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
  759. test_assert(NULL == escaped(NULL));
  760. /* Test strndup and memdup */
  761. {
  762. const char *s = "abcdefghijklmnopqrstuvwxyz";
  763. cp = tor_strndup(s, 30);
  764. test_streq(cp, s); /* same string, */
  765. test_neq(cp, s); /* but different pointers. */
  766. tor_free(cp);
  767. cp = tor_strndup(s, 5);
  768. test_streq(cp, "abcde");
  769. tor_free(cp);
  770. s = "a\0b\0c\0d\0e\0";
  771. cp = tor_memdup(s,10);
  772. test_memeq(cp, s, 10); /* same ram, */
  773. test_neq(cp, s); /* but different pointers. */
  774. tor_free(cp);
  775. }
  776. /* Test str-foo functions */
  777. cp = tor_strdup("abcdef");
  778. test_assert(tor_strisnonupper(cp));
  779. cp[3] = 'D';
  780. test_assert(!tor_strisnonupper(cp));
  781. tor_strupper(cp);
  782. test_streq(cp, "ABCDEF");
  783. test_assert(tor_strisprint(cp));
  784. cp[3] = 3;
  785. test_assert(!tor_strisprint(cp));
  786. tor_free(cp);
  787. /* Test eat_whitespace. */
  788. {
  789. const char *s = " \n a";
  790. test_eq_ptr(eat_whitespace(s), s+4);
  791. s = "abcd";
  792. test_eq_ptr(eat_whitespace(s), s);
  793. s = "#xyz\nab";
  794. test_eq_ptr(eat_whitespace(s), s+5);
  795. }
  796. /* Test memmem */
  797. {
  798. const char *haystack = "abcde";
  799. tor_assert(!tor_memmem(haystack, 5, "ef", 2));
  800. test_eq_ptr(tor_memmem(haystack, 5, "cd", 2), haystack + 2);
  801. test_eq_ptr(tor_memmem(haystack, 5, "cde", 3), haystack + 2);
  802. haystack = "ababcad";
  803. test_eq_ptr(tor_memmem(haystack, 7, "abc", 3), haystack + 2);
  804. }
  805. /* Test wrap_string */
  806. {
  807. smartlist_t *sl = smartlist_create();
  808. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  809. 10, "", "");
  810. cp = smartlist_join_strings(sl, "", 0, NULL);
  811. test_streq(cp,
  812. "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
  813. tor_free(cp);
  814. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  815. smartlist_clear(sl);
  816. wrap_string(sl, "This is a test of string wrapping functionality: woot.",
  817. 16, "### ", "# ");
  818. cp = smartlist_join_strings(sl, "", 0, NULL);
  819. test_streq(cp,
  820. "### This is a\n# test of string\n# wrapping\n# functionality:\n"
  821. "# woot.\n");
  822. tor_free(cp);
  823. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  824. smartlist_clear(sl);
  825. }
  826. /* now make sure time works. */
  827. tor_gettimeofday(&end);
  828. /* We might've timewarped a little. */
  829. test_assert(tv_udiff(&start, &end) >= -5000);
  830. /* Test tor_log2(). */
  831. test_eq(tor_log2(64), 6);
  832. test_eq(tor_log2(65), 6);
  833. test_eq(tor_log2(63), 5);
  834. test_eq(tor_log2(1), 0);
  835. test_eq(tor_log2(2), 1);
  836. test_eq(tor_log2(3), 1);
  837. test_eq(tor_log2(4), 2);
  838. test_eq(tor_log2(5), 2);
  839. test_eq(tor_log2(U64_LITERAL(40000000000000000)), 55);
  840. test_eq(tor_log2(UINT64_MAX), 63);
  841. /* Test round_to_power_of_2 */
  842. test_eq(round_to_power_of_2(120), 128);
  843. test_eq(round_to_power_of_2(128), 128);
  844. test_eq(round_to_power_of_2(130), 128);
  845. test_eq(round_to_power_of_2(U64_LITERAL(40000000000000000)),
  846. U64_LITERAL(1)<<55);
  847. test_eq(round_to_power_of_2(0), 2);
  848. }
  849. static void
  850. _test_eq_ip6(struct in6_addr *a, struct in6_addr *b, const char *e1,
  851. const char *e2, int line)
  852. {
  853. int i;
  854. int ok = 1;
  855. for (i = 0; i < 16; ++i) {
  856. if (a->s6_addr[i] != b->s6_addr[i]) {
  857. ok = 0;
  858. break;
  859. }
  860. }
  861. if (ok) {
  862. printf("."); fflush(stdout);
  863. } else {
  864. char buf1[128], *cp1;
  865. char buf2[128], *cp2;
  866. have_failed = 1;
  867. cp1 = buf1; cp2 = buf2;
  868. for (i=0; i<16; ++i) {
  869. tor_snprintf(cp1, sizeof(buf1)-(cp1-buf1), "%02x", a->s6_addr[i]);
  870. tor_snprintf(cp2, sizeof(buf2)-(cp2-buf2), "%02x", b->s6_addr[i]);
  871. cp1 += 2; cp2 += 2;
  872. if ((i%2)==1 && i != 15) {
  873. *cp1++ = ':';
  874. *cp2++ = ':';
  875. }
  876. }
  877. *cp1 = *cp2 = '\0';
  878. printf("Line %d: assertion failed: (%s == %s)\n"
  879. " %s != %s\n", line, e1, e2, buf1, buf2);
  880. fflush(stdout);
  881. }
  882. }
  883. #define test_eq_ip6(a,b) _test_eq_ip6((a),(b),#a,#b,__LINE__)
  884. #define test_pton6_same(a,b) do { \
  885. r = tor_inet_pton(AF_INET6, a, &a1); \
  886. test_assert(r==1); \
  887. r = tor_inet_pton(AF_INET6, b, &a2); \
  888. test_assert(r==1); \
  889. test_eq_ip6(&a1,&a2); \
  890. } while (0)
  891. #define test_pton6_bad(a) \
  892. test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
  893. #define test_ntop6_reduces(a,b) do { \
  894. r = tor_inet_pton(AF_INET6, a, &a1); \
  895. test_assert(r==1); \
  896. test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
  897. r = tor_inet_pton(AF_INET6, b, &a2); \
  898. test_assert(r==1); \
  899. test_eq_ip6(&a1, &a2); \
  900. } while (0)
  901. static void
  902. test_ip6_helpers(void)
  903. {
  904. char buf[64];
  905. struct in6_addr a1, a2;
  906. int r, i;
  907. // struct in_addr b1, b2;
  908. /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
  909. /* === Test pton: valid af_inet6 */
  910. /* Simple, valid parsing. */
  911. r = tor_inet_pton(AF_INET6,
  912. "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
  913. test_assert(r==1);
  914. for (i=0;i<16;++i) { test_eq(i+1, (int)a1.s6_addr[i]); }
  915. /* ipv4 ending. */
  916. test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
  917. "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
  918. /* shortened words. */
  919. test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
  920. "1:99:BEEF:0:0123:FFFF:1:1");
  921. /* zeros at the beginning */
  922. test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
  923. "::9:c0a8:1:1");
  924. test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
  925. "::9:c0a8:0.1.0.1");
  926. /* zeros in the middle. */
  927. test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
  928. "fe80::202:1111:1:1");
  929. /* zeros at the end. */
  930. test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
  931. "1000:1:0:7::");
  932. /* === Test ntop: af_inet6 */
  933. test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
  934. test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
  935. "1:99:beef:6:123:ffff:1:1");
  936. test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
  937. test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
  938. test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
  939. "::9:c0a8:1:1");
  940. test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
  941. "fe80::202:1111:1:1");
  942. test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
  943. "1000:1:0:7::");
  944. /* === Test pton: invalid in6. */
  945. test_pton6_bad("foobar.");
  946. test_pton6_bad("55555::");
  947. test_pton6_bad("9:-60::");
  948. test_pton6_bad("1:2:33333:4:0002:3::");
  949. // test_pton6_bad("1:2:3333:4:00002:3::"); //XXXX not bad.
  950. test_pton6_bad("1:2:3333:4:fish:3::");
  951. test_pton6_bad("1:2:3:4:5:6:7:8:9");
  952. test_pton6_bad("1:2:3:4:5:6:7");
  953. test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
  954. test_pton6_bad("1:2:3:4:5:6:1.2.3");
  955. test_pton6_bad("::1.2.3");
  956. test_pton6_bad("::1.2.3.4.5");
  957. test_pton6_bad("99");
  958. test_pton6_bad("");
  959. }
  960. static void
  961. test_smartlist(void)
  962. {
  963. smartlist_t *sl;
  964. char *cp;
  965. /* XXXX test sort_strings, sort_digests, uniq_strings, uniq_digests */
  966. /* Test smartlist add, del_keeporder, insert, get. */
  967. sl = smartlist_create();
  968. smartlist_add(sl, (void*)1);
  969. smartlist_add(sl, (void*)2);
  970. smartlist_add(sl, (void*)3);
  971. smartlist_add(sl, (void*)4);
  972. smartlist_del_keeporder(sl, 1);
  973. smartlist_insert(sl, 1, (void*)22);
  974. smartlist_insert(sl, 0, (void*)0);
  975. smartlist_insert(sl, 5, (void*)555);
  976. test_eq_ptr((void*)0, smartlist_get(sl,0));
  977. test_eq_ptr((void*)1, smartlist_get(sl,1));
  978. test_eq_ptr((void*)22, smartlist_get(sl,2));
  979. test_eq_ptr((void*)3, smartlist_get(sl,3));
  980. test_eq_ptr((void*)4, smartlist_get(sl,4));
  981. test_eq_ptr((void*)555, smartlist_get(sl,5));
  982. /* Try deleting in the middle. */
  983. smartlist_del(sl, 1);
  984. test_eq_ptr((void*)555, smartlist_get(sl, 1));
  985. /* Try deleting at the end. */
  986. smartlist_del(sl, 4);
  987. test_eq(4, smartlist_len(sl));
  988. /* test isin. */
  989. test_assert(smartlist_isin(sl, (void*)3));
  990. test_assert(!smartlist_isin(sl, (void*)99));
  991. /* Test split and join */
  992. smartlist_clear(sl);
  993. test_eq(0, smartlist_len(sl));
  994. smartlist_split_string(sl, "abc", ":", 0, 0);
  995. test_eq(1, smartlist_len(sl));
  996. test_streq("abc", smartlist_get(sl, 0));
  997. smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  998. test_eq(4, smartlist_len(sl));
  999. test_streq("a", smartlist_get(sl, 1));
  1000. test_streq("bc", smartlist_get(sl, 2));
  1001. test_streq("", smartlist_get(sl, 3));
  1002. cp = smartlist_join_strings(sl, "", 0, NULL);
  1003. test_streq(cp, "abcabc");
  1004. tor_free(cp);
  1005. cp = smartlist_join_strings(sl, "!", 0, NULL);
  1006. test_streq(cp, "abc!a!bc!");
  1007. tor_free(cp);
  1008. cp = smartlist_join_strings(sl, "XY", 0, NULL);
  1009. test_streq(cp, "abcXYaXYbcXY");
  1010. tor_free(cp);
  1011. cp = smartlist_join_strings(sl, "XY", 1, NULL);
  1012. test_streq(cp, "abcXYaXYbcXYXY");
  1013. tor_free(cp);
  1014. cp = smartlist_join_strings(sl, "", 1, NULL);
  1015. test_streq(cp, "abcabc");
  1016. tor_free(cp);
  1017. smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
  1018. test_eq(8, smartlist_len(sl));
  1019. test_streq("", smartlist_get(sl, 4));
  1020. test_streq("def", smartlist_get(sl, 5));
  1021. test_streq(" ", smartlist_get(sl, 6));
  1022. test_streq("ghijk", smartlist_get(sl, 7));
  1023. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1024. smartlist_clear(sl);
  1025. smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  1026. test_eq(3, smartlist_len(sl));
  1027. test_streq("a", smartlist_get(sl,0));
  1028. test_streq("bbd", smartlist_get(sl,1));
  1029. test_streq("cdef", smartlist_get(sl,2));
  1030. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  1031. SPLIT_SKIP_SPACE, 0);
  1032. test_eq(8, smartlist_len(sl));
  1033. test_streq("z", smartlist_get(sl,3));
  1034. test_streq("zhasd", smartlist_get(sl,4));
  1035. test_streq("", smartlist_get(sl,5));
  1036. test_streq("bnud", smartlist_get(sl,6));
  1037. test_streq("", smartlist_get(sl,7));
  1038. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1039. smartlist_clear(sl);
  1040. smartlist_split_string(sl, " ab\tc \td ef ", NULL,
  1041. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1042. test_eq(4, smartlist_len(sl));
  1043. test_streq("ab", smartlist_get(sl,0));
  1044. test_streq("c", smartlist_get(sl,1));
  1045. test_streq("d", smartlist_get(sl,2));
  1046. test_streq("ef", smartlist_get(sl,3));
  1047. smartlist_split_string(sl, "ghi\tj", NULL,
  1048. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1049. test_eq(6, smartlist_len(sl));
  1050. test_streq("ghi", smartlist_get(sl,4));
  1051. test_streq("j", smartlist_get(sl,5));
  1052. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1053. smartlist_clear(sl);
  1054. cp = smartlist_join_strings(sl, "XY", 0, NULL);
  1055. test_streq(cp, "");
  1056. tor_free(cp);
  1057. cp = smartlist_join_strings(sl, "XY", 1, NULL);
  1058. test_streq(cp, "XY");
  1059. tor_free(cp);
  1060. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  1061. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1062. test_eq(3, smartlist_len(sl));
  1063. test_streq("z", smartlist_get(sl, 0));
  1064. test_streq("zhasd", smartlist_get(sl, 1));
  1065. test_streq("bnud", smartlist_get(sl, 2));
  1066. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  1067. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  1068. test_eq(5, smartlist_len(sl));
  1069. test_streq("z", smartlist_get(sl, 3));
  1070. test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
  1071. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1072. smartlist_clear(sl);
  1073. smartlist_split_string(sl, "abcd\n", "\n",
  1074. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1075. test_eq(1, smartlist_len(sl));
  1076. test_streq("abcd", smartlist_get(sl, 0));
  1077. smartlist_split_string(sl, "efgh", "\n",
  1078. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1079. test_eq(2, smartlist_len(sl));
  1080. test_streq("efgh", smartlist_get(sl, 1));
  1081. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1082. smartlist_clear(sl);
  1083. /* Test swapping, shuffling, and sorting. */
  1084. smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
  1085. test_eq(7, smartlist_len(sl));
  1086. smartlist_sort(sl, _compare_strs);
  1087. cp = smartlist_join_strings(sl, ",", 0, NULL);
  1088. test_streq(cp,"and,arma,by,nickm,onion,router,the");
  1089. tor_free(cp);
  1090. smartlist_swap(sl, 1, 5);
  1091. cp = smartlist_join_strings(sl, ",", 0, NULL);
  1092. test_streq(cp,"and,router,by,nickm,onion,arma,the");
  1093. tor_free(cp);
  1094. smartlist_shuffle(sl);
  1095. test_eq(7, smartlist_len(sl));
  1096. test_assert(smartlist_string_isin(sl, "and"));
  1097. test_assert(smartlist_string_isin(sl, "router"));
  1098. test_assert(smartlist_string_isin(sl, "by"));
  1099. test_assert(smartlist_string_isin(sl, "nickm"));
  1100. test_assert(smartlist_string_isin(sl, "onion"));
  1101. test_assert(smartlist_string_isin(sl, "arma"));
  1102. test_assert(smartlist_string_isin(sl, "the"));
  1103. /* Test bsearch. */
  1104. smartlist_sort(sl, _compare_strs);
  1105. test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
  1106. _compare_without_first_ch));
  1107. test_streq("and", smartlist_bsearch(sl, " AND", _compare_without_first_ch));
  1108. test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", _compare_without_first_ch));
  1109. /* Test reverse() and pop_last() */
  1110. smartlist_reverse(sl);
  1111. cp = smartlist_join_strings(sl, ",", 0, NULL);
  1112. test_streq(cp,"the,router,onion,nickm,by,arma,and");
  1113. tor_free(cp);
  1114. cp = smartlist_pop_last(sl);
  1115. test_streq(cp, "and");
  1116. tor_free(cp);
  1117. test_eq(smartlist_len(sl), 6);
  1118. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1119. smartlist_clear(sl);
  1120. /* Test uniq() */
  1121. smartlist_split_string(sl,
  1122. "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
  1123. ",", 0, 0);
  1124. smartlist_sort(sl, _compare_strs);
  1125. smartlist_uniq(sl, _compare_strs, NULL);
  1126. cp = smartlist_join_strings(sl, ",", 0, NULL);
  1127. test_streq(cp, "50,a,canal,man,noon,panama,plan,radar");
  1128. tor_free(cp);
  1129. /* Test string_isin and isin_case and num_isin */
  1130. test_assert(smartlist_string_isin(sl, "noon"));
  1131. test_assert(!smartlist_string_isin(sl, "noonoon"));
  1132. test_assert(smartlist_string_isin_case(sl, "nOOn"));
  1133. test_assert(!smartlist_string_isin_case(sl, "nooNooN"));
  1134. test_assert(smartlist_string_num_isin(sl, 50));
  1135. test_assert(!smartlist_string_num_isin(sl, 60));
  1136. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1137. smartlist_clear(sl);
  1138. /* Test string_remove and remove and join_strings2 */
  1139. smartlist_split_string(sl,
  1140. "Some say the Earth will end in ice and some in fire",
  1141. " ", 0, 0);
  1142. cp = smartlist_get(sl, 4);
  1143. test_streq(cp, "will");
  1144. smartlist_add(sl, cp);
  1145. smartlist_remove(sl, cp);
  1146. cp = smartlist_join_strings(sl, ",", 0, NULL);
  1147. test_streq(cp, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
  1148. tor_free(cp);
  1149. smartlist_string_remove(sl, "in");
  1150. cp = smartlist_join_strings2(sl, "+XX", 1, 0, NULL);
  1151. test_streq(cp, "Some+say+the+Earth+fire+end+some+ice+and");
  1152. tor_free(cp);
  1153. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1154. smartlist_clear(sl);
  1155. {
  1156. smartlist_t *ints = smartlist_create();
  1157. smartlist_t *odds = smartlist_create();
  1158. smartlist_t *evens = smartlist_create();
  1159. smartlist_t *primes = smartlist_create();
  1160. int i;
  1161. for (i=1; i < 10; i += 2)
  1162. smartlist_add(odds, (void*)(uintptr_t)i);
  1163. for (i=0; i < 10; i += 2)
  1164. smartlist_add(evens, (void*)(uintptr_t)i);
  1165. /* add_all */
  1166. smartlist_add_all(ints, odds);
  1167. smartlist_add_all(ints, evens);
  1168. test_eq(smartlist_len(ints), 10);
  1169. smartlist_add(primes, (void*)2);
  1170. smartlist_add(primes, (void*)3);
  1171. smartlist_add(primes, (void*)5);
  1172. smartlist_add(primes, (void*)7);
  1173. /* overlap */
  1174. test_assert(smartlist_overlap(ints, odds));
  1175. test_assert(smartlist_overlap(odds, primes));
  1176. test_assert(smartlist_overlap(evens, primes));
  1177. test_assert(!smartlist_overlap(odds, evens));
  1178. /* intersect */
  1179. smartlist_add_all(sl, odds);
  1180. smartlist_intersect(sl, primes);
  1181. test_eq(smartlist_len(sl), 3);
  1182. test_assert(smartlist_isin(sl, (void*)3));
  1183. test_assert(smartlist_isin(sl, (void*)5));
  1184. test_assert(smartlist_isin(sl, (void*)7));
  1185. /* subtract */
  1186. smartlist_add_all(sl, primes);
  1187. smartlist_subtract(sl, odds);
  1188. test_eq(smartlist_len(sl), 1);
  1189. test_assert(smartlist_isin(sl, (void*)2));
  1190. smartlist_free(odds);
  1191. smartlist_free(evens);
  1192. smartlist_free(ints);
  1193. smartlist_free(primes);
  1194. smartlist_clear(sl);
  1195. }
  1196. smartlist_free(sl);
  1197. }
  1198. /* stop threads running at once. */
  1199. static tor_mutex_t *_thread_test_mutex = NULL;
  1200. /* make sure that threads have to run at the same time. */
  1201. static tor_mutex_t *_thread_test_start1 = NULL;
  1202. static tor_mutex_t *_thread_test_start2 = NULL;
  1203. static strmap_t *_thread_test_strmap = NULL;
  1204. static void _thread_test_func(void* _s) ATTR_NORETURN;
  1205. static void
  1206. _thread_test_func(void* _s)
  1207. {
  1208. char *s = _s;
  1209. int i;
  1210. tor_mutex_t *m;
  1211. char buf[64];
  1212. char *cp;
  1213. if (!strcmp(s, "thread 1"))
  1214. m = _thread_test_start1;
  1215. else
  1216. m = _thread_test_start2;
  1217. tor_mutex_acquire(m);
  1218. tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
  1219. cp = tor_strdup(buf);
  1220. for (i=0; i<100000; ++i) {
  1221. tor_mutex_acquire(_thread_test_mutex);
  1222. strmap_set(_thread_test_strmap, "last to run", cp);
  1223. tor_mutex_release(_thread_test_mutex);
  1224. }
  1225. tor_mutex_acquire(_thread_test_mutex);
  1226. strmap_set(_thread_test_strmap, s, tor_strdup(buf));
  1227. tor_mutex_release(_thread_test_mutex);
  1228. tor_mutex_release(m);
  1229. spawn_exit();
  1230. }
  1231. static void
  1232. test_threads(void)
  1233. {
  1234. char *s1, *s2;
  1235. int done = 0;
  1236. #ifndef TOR_IS_MULTITHREADED
  1237. /* Skip this test if we aren't threading. We should be threading most
  1238. * everywhere by now. */
  1239. if (1)
  1240. return 0;
  1241. #endif
  1242. _thread_test_mutex = tor_mutex_new();
  1243. _thread_test_start1 = tor_mutex_new();
  1244. _thread_test_start2 = tor_mutex_new();
  1245. _thread_test_strmap = strmap_new();
  1246. s1 = tor_strdup("thread 1");
  1247. s2 = tor_strdup("thread 2");
  1248. tor_mutex_acquire(_thread_test_start1);
  1249. tor_mutex_acquire(_thread_test_start2);
  1250. spawn_func(_thread_test_func, s1);
  1251. spawn_func(_thread_test_func, s2);
  1252. tor_mutex_release(_thread_test_start2);
  1253. tor_mutex_release(_thread_test_start1);
  1254. while (!done) {
  1255. tor_mutex_acquire(_thread_test_mutex);
  1256. strmap_assert_ok(_thread_test_strmap);
  1257. if (strmap_get(_thread_test_strmap, "thread 1") &&
  1258. strmap_get(_thread_test_strmap, "thread 2"))
  1259. done = 1;
  1260. tor_mutex_release(_thread_test_mutex);
  1261. }
  1262. tor_mutex_free(_thread_test_mutex);
  1263. /* different thread IDs. */
  1264. test_assert(strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  1265. strmap_get(_thread_test_strmap, "thread 2")));
  1266. test_assert(!strcmp(strmap_get(_thread_test_strmap, "thread 1"),
  1267. strmap_get(_thread_test_strmap, "last to run")) ||
  1268. !strcmp(strmap_get(_thread_test_strmap, "thread 2"),
  1269. strmap_get(_thread_test_strmap, "last to run")));
  1270. strmap_free(_thread_test_strmap, _tor_free);
  1271. tor_free(s1);
  1272. tor_free(s2);
  1273. }
  1274. static int
  1275. _compare_strings_for_pqueue(const void *s1, const void *s2)
  1276. {
  1277. return strcmp((const char*)s1, (const char*)s2);
  1278. }
  1279. static void
  1280. test_pqueue(void)
  1281. {
  1282. smartlist_t *sl;
  1283. int (*cmp)(const void *, const void*);
  1284. #define OK() smartlist_pqueue_assert_ok(sl, cmp)
  1285. cmp = _compare_strings_for_pqueue;
  1286. sl = smartlist_create();
  1287. smartlist_pqueue_add(sl, cmp, (char*)"cows");
  1288. smartlist_pqueue_add(sl, cmp, (char*)"zebras");
  1289. smartlist_pqueue_add(sl, cmp, (char*)"fish");
  1290. smartlist_pqueue_add(sl, cmp, (char*)"frogs");
  1291. smartlist_pqueue_add(sl, cmp, (char*)"apples");
  1292. smartlist_pqueue_add(sl, cmp, (char*)"squid");
  1293. smartlist_pqueue_add(sl, cmp, (char*)"daschunds");
  1294. smartlist_pqueue_add(sl, cmp, (char*)"eggplants");
  1295. smartlist_pqueue_add(sl, cmp, (char*)"weissbier");
  1296. smartlist_pqueue_add(sl, cmp, (char*)"lobsters");
  1297. smartlist_pqueue_add(sl, cmp, (char*)"roquefort");
  1298. OK();
  1299. test_eq(smartlist_len(sl), 11);
  1300. test_streq(smartlist_get(sl, 0), "apples");
  1301. test_streq(smartlist_pqueue_pop(sl, cmp), "apples");
  1302. test_eq(smartlist_len(sl), 10);
  1303. OK();
  1304. test_streq(smartlist_pqueue_pop(sl, cmp), "cows");
  1305. test_streq(smartlist_pqueue_pop(sl, cmp), "daschunds");
  1306. smartlist_pqueue_add(sl, cmp, (char*)"chinchillas");
  1307. OK();
  1308. smartlist_pqueue_add(sl, cmp, (char*)"fireflies");
  1309. OK();
  1310. test_streq(smartlist_pqueue_pop(sl, cmp), "chinchillas");
  1311. test_streq(smartlist_pqueue_pop(sl, cmp), "eggplants");
  1312. test_streq(smartlist_pqueue_pop(sl, cmp), "fireflies");
  1313. OK();
  1314. test_streq(smartlist_pqueue_pop(sl, cmp), "fish");
  1315. test_streq(smartlist_pqueue_pop(sl, cmp), "frogs");
  1316. test_streq(smartlist_pqueue_pop(sl, cmp), "lobsters");
  1317. test_streq(smartlist_pqueue_pop(sl, cmp), "roquefort");
  1318. OK();
  1319. test_eq(smartlist_len(sl), 3);
  1320. test_streq(smartlist_pqueue_pop(sl, cmp), "squid");
  1321. test_streq(smartlist_pqueue_pop(sl, cmp), "weissbier");
  1322. test_streq(smartlist_pqueue_pop(sl, cmp), "zebras");
  1323. test_eq(smartlist_len(sl), 0);
  1324. OK();
  1325. #undef OK
  1326. smartlist_free(sl);
  1327. }
  1328. static void
  1329. test_gzip(void)
  1330. {
  1331. char *buf1, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
  1332. const char *ccp2;
  1333. size_t len1, len2;
  1334. tor_zlib_state_t *state;
  1335. buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
  1336. test_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
  1337. if (is_gzip_supported()) {
  1338. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1339. GZIP_METHOD));
  1340. test_assert(buf2);
  1341. test_assert(!memcmp(buf2, "\037\213", 2)); /* Gztip magic. */
  1342. test_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
  1343. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  1344. GZIP_METHOD, 1, LOG_INFO));
  1345. test_assert(buf3);
  1346. test_streq(buf1,buf3);
  1347. tor_free(buf2);
  1348. tor_free(buf3);
  1349. }
  1350. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1351. ZLIB_METHOD));
  1352. test_assert(buf2);
  1353. test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
  1354. test_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
  1355. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
  1356. ZLIB_METHOD, 1, LOG_INFO));
  1357. test_assert(buf3);
  1358. test_streq(buf1,buf3);
  1359. /* Check whether we can uncompress concatenated, compresed strings. */
  1360. tor_free(buf3);
  1361. buf2 = tor_realloc(buf2, len1*2);
  1362. memcpy(buf2+len1, buf2, len1);
  1363. test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
  1364. ZLIB_METHOD, 1, LOG_INFO));
  1365. test_eq(len2, (strlen(buf1)+1)*2);
  1366. test_memeq(buf3,
  1367. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
  1368. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
  1369. (strlen(buf1)+1)*2);
  1370. tor_free(buf1);
  1371. tor_free(buf2);
  1372. tor_free(buf3);
  1373. /* Check whether we can uncompress partial strings. */
  1374. buf1 =
  1375. tor_strdup("String with low redundancy that won't be compressed much.");
  1376. test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
  1377. ZLIB_METHOD));
  1378. tor_assert(len1>16);
  1379. /* when we allow an uncomplete string, we should succeed.*/
  1380. tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  1381. ZLIB_METHOD, 0, LOG_INFO));
  1382. buf3[len2]='\0';
  1383. tor_assert(len2 > 5);
  1384. tor_assert(!strcmpstart(buf1, buf3));
  1385. /* when we demand a complete string, this must fail. */
  1386. tor_free(buf3);
  1387. tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
  1388. ZLIB_METHOD, 1, LOG_INFO));
  1389. tor_assert(!buf3);
  1390. /* Now, try streaming compression. */
  1391. tor_free(buf1);
  1392. tor_free(buf2);
  1393. tor_free(buf3);
  1394. state = tor_zlib_new(1, ZLIB_METHOD);
  1395. tor_assert(state);
  1396. cp1 = buf1 = tor_malloc(1024);
  1397. len1 = 1024;
  1398. ccp2 = "ABCDEFGHIJABCDEFGHIJ";
  1399. len2 = 21;
  1400. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
  1401. == TOR_ZLIB_OK);
  1402. test_eq(len2, 0); /* Make sure we compressed it all. */
  1403. test_assert(cp1 > buf1);
  1404. len2 = 0;
  1405. cp2 = cp1;
  1406. test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
  1407. == TOR_ZLIB_DONE);
  1408. test_eq(len2, 0);
  1409. test_assert(cp1 > cp2); /* Make sure we really added something. */
  1410. tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
  1411. ZLIB_METHOD, 1, LOG_WARN));
  1412. test_streq(buf3, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
  1413. tor_free(buf3);
  1414. tor_zlib_free(state);
  1415. tor_free(buf2);
  1416. tor_free(buf3);
  1417. tor_free(buf1);
  1418. }
  1419. static void
  1420. test_strmap(void)
  1421. {
  1422. strmap_t *map;
  1423. strmap_iter_t *iter;
  1424. const char *k;
  1425. void *v;
  1426. char *visited;
  1427. smartlist_t *found_keys;
  1428. map = strmap_new();
  1429. v = strmap_set(map, "K1", (void*)99);
  1430. test_eq(v, NULL);
  1431. v = strmap_set(map, "K2", (void*)101);
  1432. test_eq(v, NULL);
  1433. v = strmap_set(map, "K1", (void*)100);
  1434. test_eq(v, (void*)99);
  1435. test_eq_ptr(strmap_get(map,"K1"), (void*)100);
  1436. test_eq_ptr(strmap_get(map,"K2"), (void*)101);
  1437. test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
  1438. strmap_assert_ok(map);
  1439. v = strmap_remove(map,"K2");
  1440. strmap_assert_ok(map);
  1441. test_eq_ptr(v, (void*)101);
  1442. test_eq_ptr(strmap_get(map,"K2"), NULL);
  1443. test_eq_ptr(strmap_remove(map,"K2"), NULL);
  1444. strmap_set(map, "K2", (void*)101);
  1445. strmap_set(map, "K3", (void*)102);
  1446. strmap_set(map, "K4", (void*)103);
  1447. strmap_assert_ok(map);
  1448. strmap_set(map, "K5", (void*)104);
  1449. strmap_set(map, "K6", (void*)105);
  1450. strmap_assert_ok(map);
  1451. /* Test iterator. */
  1452. iter = strmap_iter_init(map);
  1453. found_keys = smartlist_create();
  1454. while (!strmap_iter_done(iter)) {
  1455. strmap_iter_get(iter,&k,&v);
  1456. smartlist_add(found_keys, tor_strdup(k));
  1457. test_eq_ptr(v, strmap_get(map, k));
  1458. if (!strcmp(k, "K2")) {
  1459. iter = strmap_iter_next_rmv(map,iter);
  1460. } else {
  1461. iter = strmap_iter_next(map,iter);
  1462. }
  1463. }
  1464. /* Make sure we removed K2, but not the others. */
  1465. test_eq_ptr(strmap_get(map, "K2"), NULL);
  1466. test_eq_ptr(strmap_get(map, "K5"), (void*)104);
  1467. /* Make sure we visited everyone once */
  1468. smartlist_sort_strings(found_keys);
  1469. visited = smartlist_join_strings(found_keys, ":", 0, NULL);
  1470. test_streq(visited, "K1:K2:K3:K4:K5:K6");
  1471. tor_free(visited);
  1472. SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
  1473. smartlist_free(found_keys);
  1474. strmap_assert_ok(map);
  1475. /* Clean up after ourselves. */
  1476. strmap_free(map, NULL);
  1477. /* Now try some lc functions. */
  1478. map = strmap_new();
  1479. strmap_set_lc(map,"Ab.C", (void*)1);
  1480. test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
  1481. strmap_assert_ok(map);
  1482. test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
  1483. test_eq_ptr(strmap_get(map,"AB.C"), NULL);
  1484. test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
  1485. strmap_assert_ok(map);
  1486. test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
  1487. strmap_free(map,NULL);
  1488. }
  1489. static void
  1490. test_mmap(void)
  1491. {
  1492. char *fname1 = tor_strdup(get_fname("mapped_1"));
  1493. char *fname2 = tor_strdup(get_fname("mapped_2"));
  1494. char *fname3 = tor_strdup(get_fname("mapped_3"));
  1495. const size_t buflen = 17000;
  1496. char *buf = tor_malloc(17000);
  1497. tor_mmap_t *mapping;
  1498. crypto_rand(buf, buflen);
  1499. write_str_to_file(fname1, "Short file.", 1);
  1500. write_bytes_to_file(fname2, buf, buflen, 1);
  1501. write_bytes_to_file(fname3, buf, 16384, 1);
  1502. mapping = tor_mmap_file(fname1);
  1503. test_assert(mapping);
  1504. test_eq(mapping->size, strlen("Short file."));
  1505. test_streq(mapping->data, "Short file.");
  1506. #ifdef MS_WINDOWS
  1507. tor_munmap_file(mapping);
  1508. test_assert(unlink(fname1) == 0);
  1509. #else
  1510. /* make sure we can unlink. */
  1511. test_assert(unlink(fname1) == 0);
  1512. test_streq(mapping->data, "Short file.");
  1513. tor_munmap_file(mapping);
  1514. #endif
  1515. /* Make sure that we fail to map a no-longer-existent file. */
  1516. mapping = tor_mmap_file(fname1);
  1517. test_assert(mapping == NULL);
  1518. /* Now try a big file that stretches across a few pages and isn't aligned */
  1519. mapping = tor_mmap_file(fname2);
  1520. test_assert(mapping);
  1521. test_eq(mapping->size, buflen);
  1522. test_memeq(mapping->data, buf, buflen);
  1523. tor_munmap_file(mapping);
  1524. /* Now try a big aligned file. */
  1525. mapping = tor_mmap_file(fname3);
  1526. test_assert(mapping);
  1527. test_eq(mapping->size, 16384);
  1528. test_memeq(mapping->data, buf, 16384);
  1529. tor_munmap_file(mapping);
  1530. /* fname1 got unlinked above */
  1531. unlink(fname2);
  1532. unlink(fname3);
  1533. tor_free(fname1);
  1534. tor_free(fname2);
  1535. tor_free(fname3);
  1536. tor_free(buf);
  1537. }
  1538. static void
  1539. test_control_formats(void)
  1540. {
  1541. char *out;
  1542. const char *inp =
  1543. "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
  1544. size_t sz;
  1545. sz = read_escaped_data(inp, strlen(inp), 1, &out);
  1546. test_streq(out,
  1547. ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
  1548. test_eq(sz, strlen(out));
  1549. tor_free(out);
  1550. }
  1551. static void
  1552. test_onion_handshake(void)
  1553. {
  1554. /* client-side */
  1555. crypto_dh_env_t *c_dh = NULL;
  1556. char c_buf[ONIONSKIN_CHALLENGE_LEN];
  1557. char c_keys[40];
  1558. /* server-side */
  1559. char s_buf[ONIONSKIN_REPLY_LEN];
  1560. char s_keys[40];
  1561. /* shared */
  1562. crypto_pk_env_t *pk = NULL;
  1563. pk = pk_generate(0);
  1564. /* client handshake 1. */
  1565. memset(c_buf, 0, ONIONSKIN_CHALLENGE_LEN);
  1566. test_assert(! onion_skin_create(pk, &c_dh, c_buf));
  1567. /* server handshake */
  1568. memset(s_buf, 0, ONIONSKIN_REPLY_LEN);
  1569. memset(s_keys, 0, 40);
  1570. test_assert(! onion_skin_server_handshake(c_buf, pk, NULL,
  1571. s_buf, s_keys, 40));
  1572. /* client handshake 2 */
  1573. memset(c_keys, 0, 40);
  1574. test_assert(! onion_skin_client_handshake(c_dh, s_buf, c_keys, 40));
  1575. crypto_dh_free(c_dh);
  1576. if (memcmp(c_keys, s_keys, 40)) {
  1577. puts("Aiiiie");
  1578. exit(1);
  1579. }
  1580. test_memeq(c_keys, s_keys, 40);
  1581. memset(s_buf, 0, 40);
  1582. test_memneq(c_keys, s_buf, 40);
  1583. crypto_free_pk_env(pk);
  1584. }
  1585. extern smartlist_t *fingerprint_list;
  1586. static void
  1587. test_dir_format(void)
  1588. {
  1589. char buf[8192], buf2[8192];
  1590. char platform[256];
  1591. char fingerprint[FINGERPRINT_LEN+1];
  1592. char *pk1_str = NULL, *pk2_str = NULL, *pk3_str = NULL, *cp;
  1593. size_t pk1_str_len, pk2_str_len, pk3_str_len;
  1594. routerinfo_t r1, r2;
  1595. crypto_pk_env_t *pk1 = NULL, *pk2 = NULL, *pk3 = NULL;
  1596. routerinfo_t *rp1 = NULL, *rp2 = NULL;
  1597. addr_policy_t ex1, ex2;
  1598. routerlist_t *dir1 = NULL, *dir2 = NULL;
  1599. tor_version_t ver1;
  1600. char *bw_lines = NULL;
  1601. pk1 = pk_generate(0);
  1602. pk2 = pk_generate(1);
  1603. pk3 = pk_generate(2);
  1604. test_assert( is_legal_nickname("a"));
  1605. test_assert(!is_legal_nickname(""));
  1606. test_assert(!is_legal_nickname("abcdefghijklmnopqrst")); /* 20 chars */
  1607. test_assert(!is_legal_nickname("hyphen-")); /* bad char */
  1608. test_assert( is_legal_nickname("abcdefghijklmnopqrs")); /* 19 chars */
  1609. test_assert(!is_legal_nickname("$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1610. /* valid */
  1611. test_assert( is_legal_nickname_or_hexdigest(
  1612. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1613. test_assert( is_legal_nickname_or_hexdigest(
  1614. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  1615. test_assert( is_legal_nickname_or_hexdigest(
  1616. "$AAAAAAAA01234AAAAAAAAAAAAAAAAAAAAAAAAAAA~fred"));
  1617. /* too short */
  1618. test_assert(!is_legal_nickname_or_hexdigest(
  1619. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1620. /* illegal char */
  1621. test_assert(!is_legal_nickname_or_hexdigest(
  1622. "$AAAAAAzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1623. /* hex part too long */
  1624. test_assert(!is_legal_nickname_or_hexdigest(
  1625. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
  1626. test_assert(!is_legal_nickname_or_hexdigest(
  1627. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=fred"));
  1628. /* Bad nickname */
  1629. test_assert(!is_legal_nickname_or_hexdigest(
  1630. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="));
  1631. test_assert(!is_legal_nickname_or_hexdigest(
  1632. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"));
  1633. test_assert(!is_legal_nickname_or_hexdigest(
  1634. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~hyphen-"));
  1635. test_assert(!is_legal_nickname_or_hexdigest(
  1636. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~"
  1637. "abcdefghijklmnoppqrst"));
  1638. /* Bad extra char. */
  1639. test_assert(!is_legal_nickname_or_hexdigest(
  1640. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!"));
  1641. test_assert(is_legal_nickname_or_hexdigest("xyzzy"));
  1642. test_assert(is_legal_nickname_or_hexdigest("abcdefghijklmnopqrs"));
  1643. test_assert(!is_legal_nickname_or_hexdigest("abcdefghijklmnopqrst"));
  1644. get_platform_str(platform, sizeof(platform));
  1645. memset(&r1,0,sizeof(r1));
  1646. memset(&r2,0,sizeof(r2));
  1647. r1.address = tor_strdup("18.244.0.1");
  1648. r1.addr = 0xc0a80001u; /* 192.168.0.1 */
  1649. r1.cache_info.published_on = 0;
  1650. r1.or_port = 9000;
  1651. r1.dir_port = 9003;
  1652. r1.onion_pkey = pk1;
  1653. r1.identity_pkey = pk2;
  1654. r1.bandwidthrate = 1000;
  1655. r1.bandwidthburst = 5000;
  1656. r1.bandwidthcapacity = 10000;
  1657. r1.exit_policy = NULL;
  1658. r1.nickname = tor_strdup("Magri");
  1659. r1.platform = tor_strdup(platform);
  1660. ex1.policy_type = ADDR_POLICY_ACCEPT;
  1661. ex1.string = NULL;
  1662. ex1.addr = 0;
  1663. ex1.msk = 0;
  1664. ex1.prt_min = ex1.prt_max = 80;
  1665. ex1.next = &ex2;
  1666. ex2.policy_type = ADDR_POLICY_REJECT;
  1667. ex2.addr = 18 << 24;
  1668. ex2.msk = 0xFF000000u;
  1669. ex2.prt_min = ex2.prt_max = 24;
  1670. ex2.next = NULL;
  1671. r2.address = tor_strdup("1.1.1.1");
  1672. r2.addr = 0x0a030201u; /* 10.3.2.1 */
  1673. r2.platform = tor_strdup(platform);
  1674. r2.cache_info.published_on = 5;
  1675. r2.or_port = 9005;
  1676. r2.dir_port = 0;
  1677. r2.onion_pkey = pk2;
  1678. r2.identity_pkey = pk1;
  1679. r2.bandwidthrate = r2.bandwidthburst = r2.bandwidthcapacity = 3000;
  1680. r2.exit_policy = &ex1;
  1681. r2.nickname = tor_strdup("Fred");
  1682. bw_lines = rep_hist_get_bandwidth_lines(0);
  1683. test_assert(bw_lines);
  1684. test_assert(!strcmpstart(bw_lines, "opt write-history "));
  1685. test_assert(!crypto_pk_write_public_key_to_string(pk1, &pk1_str,
  1686. &pk1_str_len));
  1687. test_assert(!crypto_pk_write_public_key_to_string(pk2 , &pk2_str,
  1688. &pk2_str_len));
  1689. test_assert(!crypto_pk_write_public_key_to_string(pk3 , &pk3_str,
  1690. &pk3_str_len));
  1691. memset(buf, 0, 2048);
  1692. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1693. strlcpy(buf2, "router Magri 18.244.0.1 9000 0 0\n"
  1694. "platform Tor "VERSION" on ", sizeof(buf2));
  1695. strlcat(buf2, get_uname(), sizeof(buf2));
  1696. strlcat(buf2, "\n"
  1697. "published 1970-01-01 00:00:00\n"
  1698. "opt fingerprint ", sizeof(buf2));
  1699. test_assert(!crypto_pk_get_fingerprint(pk2, fingerprint, 1));
  1700. strlcat(buf2, fingerprint, sizeof(buf2));
  1701. strlcat(buf2, "\nuptime 0\n"
  1702. /* XXX the "0" above is hardcoded, but even if we made it reflect
  1703. * uptime, that still wouldn't make it right, because the two
  1704. * descriptors might be made on different seconds... hm. */
  1705. "bandwidth 1000 5000 10000\n"
  1706. "opt extra-info-digest 0000000000000000000000000000000000000000\n"
  1707. "onion-key\n", sizeof(buf2));
  1708. strlcat(buf2, pk1_str, sizeof(buf2));
  1709. strlcat(buf2, "signing-key\n", sizeof(buf2));
  1710. strlcat(buf2, pk2_str, sizeof(buf2));
  1711. strlcat(buf2, bw_lines, sizeof(buf2));
  1712. strlcat(buf2, "router-signature\n", sizeof(buf2));
  1713. buf[strlen(buf2)] = '\0'; /* Don't compare the sig; it's never the same
  1714. * twice */
  1715. test_streq(buf, buf2);
  1716. tor_free(bw_lines);
  1717. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1718. cp = buf;
  1719. rp1 = router_parse_entry_from_string((const char*)cp,NULL,1);
  1720. test_assert(rp1);
  1721. test_streq(rp1->address, r1.address);
  1722. test_eq(rp1->or_port, r1.or_port);
  1723. //test_eq(rp1->dir_port, r1.dir_port);
  1724. test_eq(rp1->bandwidthrate, r1.bandwidthrate);
  1725. test_eq(rp1->bandwidthburst, r1.bandwidthburst);
  1726. test_eq(rp1->bandwidthcapacity, r1.bandwidthcapacity);
  1727. test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
  1728. test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
  1729. test_assert(rp1->exit_policy == NULL);
  1730. #if 0
  1731. /* XXX Once we have exit policies, test this again. XXX */
  1732. strlcpy(buf2, "router tor.tor.tor 9005 0 0 3000\n", sizeof(buf2));
  1733. strlcat(buf2, pk2_str, sizeof(buf2));
  1734. strlcat(buf2, "signing-key\n", sizeof(buf2));
  1735. strlcat(buf2, pk1_str, sizeof(buf2));
  1736. strlcat(buf2, "accept *:80\nreject 18.*:24\n\n", sizeof(buf2));
  1737. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk2)>0);
  1738. test_streq(buf, buf2);
  1739. cp = buf;
  1740. rp2 = router_parse_entry_from_string(&cp,1);
  1741. test_assert(rp2);
  1742. test_streq(rp2->address, r2.address);
  1743. test_eq(rp2->or_port, r2.or_port);
  1744. test_eq(rp2->dir_port, r2.dir_port);
  1745. test_eq(rp2->bandwidth, r2.bandwidth);
  1746. test_assert(crypto_pk_cmp_keys(rp2->onion_pkey, pk2) == 0);
  1747. test_assert(crypto_pk_cmp_keys(rp2->identity_pkey, pk1) == 0);
  1748. test_eq(rp2->exit_policy->policy_type, EXIT_POLICY_ACCEPT);
  1749. test_streq(rp2->exit_policy->string, "accept *:80");
  1750. test_streq(rp2->exit_policy->address, "*");
  1751. test_streq(rp2->exit_policy->port, "80");
  1752. test_eq(rp2->exit_policy->next->policy_type, EXIT_POLICY_REJECT);
  1753. test_streq(rp2->exit_policy->next->string, "reject 18.*:24");
  1754. test_streq(rp2->exit_policy->next->address, "18.*");
  1755. test_streq(rp2->exit_policy->next->port, "24");
  1756. test_assert(rp2->exit_policy->next->next == NULL);
  1757. /* Okay, now for the directories. */
  1758. {
  1759. fingerprint_list = smartlist_create();
  1760. crypto_pk_get_fingerprint(pk2, buf, 1);
  1761. add_fingerprint_to_dir("Magri", buf, fingerprint_list);
  1762. crypto_pk_get_fingerprint(pk1, buf, 1);
  1763. add_fingerprint_to_dir("Fred", buf, fingerprint_list);
  1764. }
  1765. {
  1766. char d[DIGEST_LEN];
  1767. const char *m;
  1768. /* XXXX NM re-enable. */
  1769. /* Make sure routers aren't too far in the past any more. */
  1770. r1.cache_info.published_on = time(NULL);
  1771. r2.cache_info.published_on = time(NULL)-3*60*60;
  1772. test_assert(router_dump_router_to_string(buf, 2048, &r1, pk2)>0);
  1773. test_eq(dirserv_add_descriptor(buf,&m), 2);
  1774. test_assert(router_dump_router_to_string(buf, 2048, &r2, pk1)>0);
  1775. test_eq(dirserv_add_descriptor(buf,&m), 2);
  1776. get_options()->Nickname = tor_strdup("DirServer");
  1777. test_assert(!dirserv_dump_directory_to_string(&cp,pk3, 0));
  1778. crypto_pk_get_digest(pk3, d);
  1779. test_assert(!router_parse_directory(cp));
  1780. test_eq(2, smartlist_len(dir1->routers));
  1781. tor_free(cp);
  1782. }
  1783. #endif
  1784. dirserv_free_fingerprint_list();
  1785. tor_free(pk1_str);
  1786. tor_free(pk2_str);
  1787. if (pk1) crypto_free_pk_env(pk1);
  1788. if (pk2) crypto_free_pk_env(pk2);
  1789. if (rp1) routerinfo_free(rp1);
  1790. if (rp2) routerinfo_free(rp2);
  1791. tor_free(dir1); /* XXXX And more !*/
  1792. tor_free(dir2); /* And more !*/
  1793. /* Try out version parsing functionality */
  1794. test_eq(0, tor_version_parse("0.3.4pre2-cvs", &ver1));
  1795. test_eq(0, ver1.major);
  1796. test_eq(3, ver1.minor);
  1797. test_eq(4, ver1.micro);
  1798. test_eq(VER_PRE, ver1.status);
  1799. test_eq(2, ver1.patchlevel);
  1800. test_eq(0, tor_version_parse("0.3.4rc1", &ver1));
  1801. test_eq(0, ver1.major);
  1802. test_eq(3, ver1.minor);
  1803. test_eq(4, ver1.micro);
  1804. test_eq(VER_RC, ver1.status);
  1805. test_eq(1, ver1.patchlevel);
  1806. test_eq(0, tor_version_parse("1.3.4", &ver1));
  1807. test_eq(1, ver1.major);
  1808. test_eq(3, ver1.minor);
  1809. test_eq(4, ver1.micro);
  1810. test_eq(VER_RELEASE, ver1.status);
  1811. test_eq(0, ver1.patchlevel);
  1812. test_eq(0, tor_version_parse("1.3.4.999", &ver1));
  1813. test_eq(1, ver1.major);
  1814. test_eq(3, ver1.minor);
  1815. test_eq(4, ver1.micro);
  1816. test_eq(VER_RELEASE, ver1.status);
  1817. test_eq(999, ver1.patchlevel);
  1818. test_eq(0, tor_version_parse("0.1.2.4-alpha", &ver1));
  1819. test_eq(0, ver1.major);
  1820. test_eq(1, ver1.minor);
  1821. test_eq(2, ver1.micro);
  1822. test_eq(4, ver1.patchlevel);
  1823. test_eq(VER_RELEASE, ver1.status);
  1824. test_streq("alpha", ver1.status_tag);
  1825. test_eq(0, tor_version_parse("0.1.2.4", &ver1));
  1826. test_eq(0, ver1.major);
  1827. test_eq(1, ver1.minor);
  1828. test_eq(2, ver1.micro);
  1829. test_eq(4, ver1.patchlevel);
  1830. test_eq(VER_RELEASE, ver1.status);
  1831. test_streq("", ver1.status_tag);
  1832. #define test_eq_vs(vs1, vs2) test_eq_type(version_status_t, "%d", (vs1), (vs2))
  1833. #define test_v_i_o(val, ver, lst) \
  1834. test_eq_vs(val, tor_version_is_obsolete(ver, lst))
  1835. /* make sure tor_version_is_obsolete() works */
  1836. test_v_i_o(VS_OLD, "0.0.1", "Tor 0.0.2");
  1837. test_v_i_o(VS_OLD, "0.0.1", "0.0.2, Tor 0.0.3");
  1838. test_v_i_o(VS_OLD, "0.0.1", "0.0.2,Tor 0.0.3");
  1839. test_v_i_o(VS_OLD, "0.0.1","0.0.3,BetterTor 0.0.1");
  1840. test_v_i_o(VS_RECOMMENDED, "0.0.2", "Tor 0.0.2,Tor 0.0.3");
  1841. test_v_i_o(VS_NEW_IN_SERIES, "0.0.2", "Tor 0.0.2pre1,Tor 0.0.3");
  1842. test_v_i_o(VS_OLD, "0.0.2", "Tor 0.0.2.1,Tor 0.0.3");
  1843. test_v_i_o(VS_NEW, "0.1.0", "Tor 0.0.2,Tor 0.0.3");
  1844. test_v_i_o(VS_RECOMMENDED, "0.0.7rc2", "0.0.7,Tor 0.0.7rc2,Tor 0.0.8");
  1845. test_v_i_o(VS_OLD, "0.0.5.0", "0.0.5.1-cvs");
  1846. test_v_i_o(VS_NEW_IN_SERIES, "0.0.5.1-cvs", "0.0.5, 0.0.6");
  1847. /* Not on list, but newer than any in same series. */
  1848. test_v_i_o(VS_NEW_IN_SERIES, "0.1.0.3",
  1849. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1850. /* Series newer than any on list. */
  1851. 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");
  1852. /* Series older than any on list. */
  1853. 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");
  1854. /* Not on list, not newer than any on same series. */
  1855. test_v_i_o(VS_UNRECOMMENDED, "0.1.0.1",
  1856. "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1857. /* On list, not newer than any on same series. */
  1858. test_v_i_o(VS_UNRECOMMENDED,
  1859. "0.1.0.1", "Tor 0.1.0.2,Tor 0.0.9.5,Tor 0.1.1.0");
  1860. test_eq(0, tor_version_as_new_as("Tor 0.0.5", "0.0.9pre1-cvs"));
  1861. test_eq(1, tor_version_as_new_as(
  1862. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  1863. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh",
  1864. "0.0.8rc2"));
  1865. test_eq(0, tor_version_as_new_as(
  1866. "Tor 0.0.8 on Darwin 64-121-192-100.c3-0."
  1867. "sfpo-ubr1.sfrn-sfpo.ca.cable.rcn.com Power Macintosh", "0.0.8.2"));
  1868. /* Now try svn revisions. */
  1869. test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  1870. "Tor 0.2.1.0-dev (r99)"));
  1871. test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100) on Banana Jr",
  1872. "Tor 0.2.1.0-dev (r99) on Hal 9000"));
  1873. test_eq(1, tor_version_as_new_as("Tor 0.2.1.0-dev (r100)",
  1874. "Tor 0.2.1.0-dev on Colossus"));
  1875. test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99)",
  1876. "Tor 0.2.1.0-dev (r100)"));
  1877. test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev (r99) on MCP",
  1878. "Tor 0.2.1.0-dev (r100) on AM"));
  1879. test_eq(0, tor_version_as_new_as("Tor 0.2.1.0-dev",
  1880. "Tor 0.2.1.0-dev (r99)"));
  1881. test_eq(1, tor_version_as_new_as("Tor 0.2.1.1",
  1882. "Tor 0.2.1.0-dev (r99)"));
  1883. }
  1884. extern const char AUTHORITY_CERT_1[];
  1885. extern const char AUTHORITY_IDKEY_1[];
  1886. extern const char AUTHORITY_SIGNKEY_1[];
  1887. extern const char AUTHORITY_CERT_2[];
  1888. extern const char AUTHORITY_IDKEY_2[];
  1889. extern const char AUTHORITY_SIGNKEY_2[];
  1890. static void
  1891. test_v3_networkstatus(void)
  1892. {
  1893. authority_cert_t *cert1, *cert2;//, *cert_tmp;
  1894. crypto_pk_env_t *id_skey_1, *id_skey_2;
  1895. crypto_pk_env_t *sign_skey_1, *sign_skey_2;
  1896. time_t now = time(NULL);
  1897. networkstatus_voter_info_t *voter;
  1898. networkstatus_vote_t *vote, *v1;
  1899. vote_routerstatus_t *vrs;
  1900. routerstatus_t *rs;
  1901. char *v1_text, *cp;
  1902. add_stream_log(LOG_NOTICE, LOG_ERR, "", stdout);
  1903. /* Parse certificates and keys. */
  1904. cert1 = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  1905. test_assert(cert1);
  1906. cert2 = authority_cert_parse_from_string(AUTHORITY_CERT_2, NULL);
  1907. test_assert(cert2);
  1908. id_skey_1 = crypto_new_pk_env();
  1909. id_skey_2 = crypto_new_pk_env();
  1910. sign_skey_1 = crypto_new_pk_env();
  1911. sign_skey_2 = crypto_new_pk_env();
  1912. test_assert(!crypto_pk_read_private_key_from_string(id_skey_1,
  1913. AUTHORITY_IDKEY_1));
  1914. test_assert(!crypto_pk_read_private_key_from_string(id_skey_2,
  1915. AUTHORITY_IDKEY_2));
  1916. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_1,
  1917. AUTHORITY_SIGNKEY_1));
  1918. test_assert(!crypto_pk_read_private_key_from_string(sign_skey_2,
  1919. AUTHORITY_SIGNKEY_2));
  1920. test_assert(!crypto_pk_cmp_keys(id_skey_1, cert1->identity_key));
  1921. test_assert(!crypto_pk_cmp_keys(sign_skey_1, cert1->signing_key));
  1922. test_assert(!crypto_pk_cmp_keys(id_skey_2, cert2->identity_key));
  1923. test_assert(!crypto_pk_cmp_keys(sign_skey_2, cert2->signing_key));
  1924. /*
  1925. * Set up a vote; generate it; try to parse it.
  1926. */
  1927. vote = tor_malloc_zero(sizeof(networkstatus_vote_t));
  1928. vote->is_vote = 1;
  1929. vote->published = now;
  1930. vote->valid_after = now+100;
  1931. vote->fresh_until = now+200;
  1932. vote->valid_until = now+300;
  1933. vote->vote_seconds = 100;
  1934. vote->dist_seconds = 200;
  1935. vote->client_versions = tor_strdup("0.1.2.14,0.1.2.15");
  1936. vote->server_versions = tor_strdup("0.1.2.14,0.1.2.15,0.1.2.16");
  1937. vote->known_flags = smartlist_create();
  1938. smartlist_split_string(vote->known_flags,
  1939. "Authority Exit Fast Guard Running Stable V2Dir Valid",
  1940. 0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1941. vote->voters = smartlist_create();
  1942. voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
  1943. voter->nickname = tor_strdup("Voter1");
  1944. voter->address = tor_strdup("1.2.3.4");
  1945. voter->addr = 0x01020304;
  1946. voter->dir_port = 80;
  1947. voter->or_port = 9000;
  1948. voter->contact = tor_strdup("voter1@example.com");
  1949. crypto_pk_get_digest(id_skey_1, voter->identity_digest);
  1950. smartlist_add(vote->voters, voter);
  1951. vote->cert = authority_cert_dup(cert1);
  1952. vote->routerstatus_list = smartlist_create();
  1953. /* add the first routerstatus. */
  1954. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1955. rs = &vrs->status;
  1956. vrs->version = tor_strdup("0.1.2.14");
  1957. rs->published_on = now-1500;
  1958. strlcpy(rs->nickname, "router2", sizeof(rs->nickname));
  1959. memset(rs->identity_digest, 3, DIGEST_LEN);
  1960. memset(rs->descriptor_digest, 11, DIGEST_LEN);
  1961. rs->addr = 0x99008801;
  1962. rs->or_port = 443;
  1963. rs->dir_port = 8000;
  1964. /* all flags cleared */
  1965. smartlist_add(vote->routerstatus_list, vrs);
  1966. /* add the second routerstatus. */
  1967. vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
  1968. rs = &vrs->status;
  1969. vrs->version = tor_strdup("0.2.0.5");
  1970. rs->published_on = now-1000;
  1971. strlcpy(rs->nickname, "router1", sizeof(rs->nickname));
  1972. memset(rs->identity_digest, 5, DIGEST_LEN);
  1973. memset(rs->descriptor_digest, 2, DIGEST_LEN);
  1974. rs->addr = 0x99009901;
  1975. rs->or_port = 443;
  1976. rs->dir_port = 0;
  1977. rs->is_exit = rs->is_stable = rs->is_fast = rs->is_running =
  1978. rs->is_v2_dir = rs->is_possible_guard = 1;
  1979. // rs->named = rs->is_bad_exit = rs->is_bad_directory = 1;
  1980. smartlist_add(vote->routerstatus_list, vrs);
  1981. /* dump the vote and try to parse it. */
  1982. v1_text = format_networkstatus_vote(sign_skey_1, vote);
  1983. test_assert(v1_text);
  1984. v1 = networkstatus_parse_vote_from_string(v1_text, 1);
  1985. test_assert(v1);
  1986. /* Make sure the parsed thing was right. */
  1987. test_eq(v1->is_vote, 1);
  1988. test_eq(v1->published, vote->published);
  1989. test_eq(v1->valid_after, vote->valid_after);
  1990. test_eq(v1->fresh_until, vote->fresh_until);
  1991. test_eq(v1->valid_until, vote->valid_until);
  1992. test_eq(v1->vote_seconds, vote->vote_seconds);
  1993. test_eq(v1->dist_seconds, vote->dist_seconds);
  1994. test_streq(v1->client_versions, vote->client_versions);
  1995. test_streq(v1->server_versions, vote->server_versions);
  1996. test_assert(v1->voters && smartlist_len(v1->voters));
  1997. voter = smartlist_get(v1->voters, 0);
  1998. test_streq(voter->nickname, "Voter1");
  1999. test_streq(voter->address, "1.2.3.4");
  2000. test_eq(voter->addr, 0x01020304);
  2001. test_eq(voter->dir_port, 80);
  2002. test_eq(voter->or_port, 9000);
  2003. test_streq(voter->contact, "voter1@example.com");
  2004. test_assert(v1->cert);
  2005. test_assert(!crypto_pk_cmp_keys(id_skey_1, v1->cert->identity_key));
  2006. test_assert(!crypto_pk_cmp_keys(sign_skey_1, v1->cert->signing_key));
  2007. cp = smartlist_join_strings(v1->known_flags, ":", 0, NULL);
  2008. test_streq(cp, "Authority:Exit:Fast:Guard:Running:Stable:V2Dir:Valid");
  2009. tor_free(cp);
  2010. test_eq(smartlist_len(v1->routerstatus_list), 2);
  2011. /*XXXX020 test contents of v1->routerstatus_list. */
  2012. /* XXXXX020 Generate 2 more votes */
  2013. /* XXXXX020 compute, write, and parse a consensus. */
  2014. tor_free(v1_text);
  2015. networkstatus_vote_free(vote);
  2016. networkstatus_vote_free(v1);
  2017. crypto_free_pk_env(id_skey_1);
  2018. crypto_free_pk_env(id_skey_2);
  2019. crypto_free_pk_env(sign_skey_1);
  2020. crypto_free_pk_env(sign_skey_2);
  2021. authority_cert_free(cert1);
  2022. authority_cert_free(cert2);
  2023. }
  2024. static void
  2025. test_policies(void)
  2026. {
  2027. addr_policy_t *policy, *policy2;
  2028. config_line_t line;
  2029. policy = router_parse_addr_policy_from_string("reject 192.168.0.0/16:*",-1);
  2030. test_eq(NULL, policy->next);
  2031. test_eq(ADDR_POLICY_REJECT, policy->policy_type);
  2032. test_eq(0xc0a80000u, policy->addr);
  2033. test_eq(0xffff0000u, policy->msk);
  2034. test_eq(1, policy->prt_min);
  2035. test_eq(65535, policy->prt_max);
  2036. test_streq("reject 192.168.0.0/16:*", policy->string);
  2037. test_assert(ADDR_POLICY_ACCEPTED ==
  2038. compare_addr_to_addr_policy(0x01020304u, 2, policy));
  2039. test_assert(ADDR_POLICY_PROBABLY_ACCEPTED ==
  2040. compare_addr_to_addr_policy(0, 2, policy));
  2041. test_assert(ADDR_POLICY_REJECTED ==
  2042. compare_addr_to_addr_policy(0xc0a80102, 2, policy));
  2043. policy2 = NULL;
  2044. test_assert(0 == policies_parse_exit_policy(NULL, &policy2, 1));
  2045. test_assert(policy2);
  2046. test_assert(!exit_policy_is_general_exit(policy));
  2047. test_assert(exit_policy_is_general_exit(policy2));
  2048. test_assert(cmp_addr_policies(policy, policy2));
  2049. test_assert(!cmp_addr_policies(policy2, policy2));
  2050. test_assert(!policy_is_reject_star(policy2));
  2051. test_assert(policy_is_reject_star(policy));
  2052. addr_policy_free(policy);
  2053. addr_policy_free(policy2);
  2054. /* make sure compacting logic works. */
  2055. policy = NULL;
  2056. line.key = (char*)"foo";
  2057. line.value = (char*)"accept *:80,reject private:*,reject *:*";
  2058. line.next = NULL;
  2059. test_assert(0 == policies_parse_exit_policy(&line, &policy, 0));
  2060. test_assert(policy);
  2061. test_streq(policy->string, "accept *:80");
  2062. test_streq(policy->next->string, "reject *:*");
  2063. test_eq_ptr(policy->next->next, NULL);
  2064. addr_policy_free(policy);
  2065. }
  2066. static void
  2067. test_rend_fns(void)
  2068. {
  2069. char address1[] = "fooaddress.onion";
  2070. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  2071. char address3[] = "fooaddress.exit";
  2072. char address4[] = "tor.eff.org";
  2073. rend_service_descriptor_t *d1, *d2;
  2074. char *encoded;
  2075. size_t len;
  2076. crypto_pk_env_t *pk1, *pk2;
  2077. time_t now;
  2078. pk1 = pk_generate(0);
  2079. pk2 = pk_generate(1);
  2080. /* Test unversioned descriptor */
  2081. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  2082. d1->pk = crypto_pk_dup_key(pk1);
  2083. now = time(NULL);
  2084. d1->timestamp = now;
  2085. d1->n_intro_points = 3;
  2086. d1->version = 0;
  2087. d1->intro_points = tor_malloc(sizeof(char*)*3);
  2088. d1->intro_points[0] = tor_strdup("tom");
  2089. d1->intro_points[1] = tor_strdup("crow");
  2090. d1->intro_points[2] = tor_strdup("joel");
  2091. test_assert(! rend_encode_service_descriptor(d1, 0, pk1, &encoded, &len));
  2092. d2 = rend_parse_service_descriptor(encoded, len);
  2093. test_assert(d2);
  2094. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  2095. test_eq(d2->timestamp, now);
  2096. test_eq(d2->version, 0);
  2097. test_eq(d2->protocols, 1);
  2098. test_eq(d2->n_intro_points, 3);
  2099. test_streq(d2->intro_points[0], "tom");
  2100. test_streq(d2->intro_points[1], "crow");
  2101. test_streq(d2->intro_points[2], "joel");
  2102. test_eq(NULL, d2->intro_point_extend_info);
  2103. rend_service_descriptor_free(d1);
  2104. rend_service_descriptor_free(d2);
  2105. tor_free(encoded);
  2106. /* Test versioned descriptor. */
  2107. d1 = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  2108. d1->pk = crypto_pk_dup_key(pk1);
  2109. now = time(NULL);
  2110. d1->timestamp = now;
  2111. d1->n_intro_points = 2;
  2112. d1->version = 1;
  2113. d1->protocols = 60;
  2114. d1->intro_points = tor_malloc(sizeof(char*)*2);
  2115. d1->intro_point_extend_info = tor_malloc(sizeof(extend_info_t*)*2);
  2116. d1->intro_points[0] = tor_strdup("tom");
  2117. d1->intro_points[1] = tor_strdup("crow");
  2118. d1->intro_point_extend_info[0] = tor_malloc_zero(sizeof(extend_info_t));
  2119. strlcpy(d1->intro_point_extend_info[0]->nickname, "tom", 4);
  2120. d1->intro_point_extend_info[0]->addr = 1234;
  2121. d1->intro_point_extend_info[0]->port = 4567;
  2122. d1->intro_point_extend_info[0]->onion_key = crypto_pk_dup_key(pk1);
  2123. memset(d1->intro_point_extend_info[0]->identity_digest, 'a', DIGEST_LEN);
  2124. d1->intro_point_extend_info[1] = tor_malloc_zero(sizeof(extend_info_t));
  2125. strlcpy(d1->intro_point_extend_info[1]->nickname, "crow", 5);
  2126. d1->intro_point_extend_info[1]->addr = 6060842;
  2127. d1->intro_point_extend_info[1]->port = 8000;
  2128. d1->intro_point_extend_info[1]->onion_key = crypto_pk_dup_key(pk2);
  2129. memset(d1->intro_point_extend_info[1]->identity_digest, 'b', DIGEST_LEN);
  2130. test_assert(! rend_encode_service_descriptor(d1, 1, pk1, &encoded, &len));
  2131. d2 = rend_parse_service_descriptor(encoded, len);
  2132. test_assert(d2);
  2133. test_assert(!crypto_pk_cmp_keys(d1->pk, d2->pk));
  2134. test_eq(d2->timestamp, now);
  2135. test_eq(d2->version, 1);
  2136. test_eq(d2->protocols, 60);
  2137. test_eq(d2->n_intro_points, 2);
  2138. test_streq(d2->intro_points[0], d2->intro_point_extend_info[0]->nickname);
  2139. test_streq(d2->intro_points[1], d2->intro_point_extend_info[1]->nickname);
  2140. test_eq(d2->intro_point_extend_info[0]->addr, 1234);
  2141. test_eq(d2->intro_point_extend_info[0]->port, 4567);
  2142. test_assert(!crypto_pk_cmp_keys(pk1,
  2143. d2->intro_point_extend_info[0]->onion_key));
  2144. test_memeq(d2->intro_point_extend_info[0]->identity_digest,
  2145. d1->intro_point_extend_info[0]->identity_digest, DIGEST_LEN);
  2146. test_eq(d2->intro_point_extend_info[1]->addr, 6060842);
  2147. test_eq(d2->intro_point_extend_info[1]->port, 8000);
  2148. test_memeq(d2->intro_point_extend_info[1]->identity_digest,
  2149. d1->intro_point_extend_info[1]->identity_digest, DIGEST_LEN);
  2150. test_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
  2151. test_assert(ONION_HOSTNAME == parse_extended_hostname(address2));
  2152. test_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
  2153. test_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
  2154. rend_service_descriptor_free(d1);
  2155. rend_service_descriptor_free(d2);
  2156. crypto_free_pk_env(pk1);
  2157. crypto_free_pk_env(pk2);
  2158. }
  2159. static void
  2160. bench_aes(void)
  2161. {
  2162. int len, i;
  2163. char *b1, *b2;
  2164. crypto_cipher_env_t *c;
  2165. struct timeval start, end;
  2166. const int iters = 100000;
  2167. uint64_t nsec;
  2168. c = crypto_new_cipher_env();
  2169. crypto_cipher_generate_key(c);
  2170. crypto_cipher_encrypt_init_cipher(c);
  2171. for (len = 1; len <= 8192; len *= 2) {
  2172. b1 = tor_malloc_zero(len);
  2173. b2 = tor_malloc_zero(len);
  2174. tor_gettimeofday(&start);
  2175. for (i = 0; i < iters; ++i) {
  2176. crypto_cipher_encrypt(c, b1, b2, len);
  2177. }
  2178. tor_gettimeofday(&end);
  2179. tor_free(b1);
  2180. tor_free(b2);
  2181. nsec = (uint64_t) tv_udiff(&start,&end);
  2182. nsec *= 1000;
  2183. nsec /= (iters*len);
  2184. printf("%d bytes: "U64_FORMAT" nsec per byte\n", len,
  2185. U64_PRINTF_ARG(nsec));
  2186. }
  2187. crypto_free_cipher_env(c);
  2188. }
  2189. static void
  2190. test_mempool(void)
  2191. {
  2192. mp_pool_t *pool;
  2193. smartlist_t *allocated;
  2194. int i;
  2195. pool = mp_pool_new(1, 100);
  2196. test_assert(pool->new_chunk_capacity >= 100);
  2197. test_assert(pool->item_alloc_size >= sizeof(void*)+1);
  2198. mp_pool_destroy(pool);
  2199. pool = mp_pool_new(241, 2500);
  2200. test_assert(pool->new_chunk_capacity >= 10);
  2201. test_assert(pool->item_alloc_size >= sizeof(void*)+241);
  2202. test_eq(pool->item_alloc_size & 0x03, 0);
  2203. test_assert(pool->new_chunk_capacity < 60);
  2204. allocated = smartlist_create();
  2205. for (i = 0; i < 100000; ++i) {
  2206. if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) {
  2207. void *m = mp_pool_get(pool);
  2208. memset(m, 0x09, 241);
  2209. smartlist_add(allocated, m);
  2210. //printf("%d: %p\n", i, m);
  2211. //mp_pool_assert_ok(pool);
  2212. } else {
  2213. int idx = crypto_rand_int(smartlist_len(allocated));
  2214. void *m = smartlist_get(allocated, idx);
  2215. //printf("%d: free %p\n", i, m);
  2216. smartlist_del(allocated, idx);
  2217. mp_pool_release(m);
  2218. //mp_pool_assert_ok(pool);
  2219. }
  2220. if (crypto_rand_int(777)==0)
  2221. mp_pool_clean(pool, -1);
  2222. if (i % 777)
  2223. mp_pool_assert_ok(pool);
  2224. }
  2225. SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m));
  2226. mp_pool_assert_ok(pool);
  2227. mp_pool_clean(pool, 0);
  2228. mp_pool_assert_ok(pool);
  2229. mp_pool_destroy(pool);
  2230. smartlist_free(allocated);
  2231. }
  2232. int
  2233. main(int c, char**v)
  2234. {
  2235. or_options_t *options = options_new();
  2236. char *errmsg = NULL;
  2237. (void) c;
  2238. (void) v;
  2239. options->command = CMD_RUN_UNITTESTS;
  2240. rep_hist_init();
  2241. network_init();
  2242. setup_directory();
  2243. options_init(options);
  2244. options->DataDirectory = tor_strdup(temp_dir);
  2245. if (set_options(options, &errmsg) < 0) {
  2246. printf("Failed to set initial options: %s\n", errmsg);
  2247. tor_free(errmsg);
  2248. return 1;
  2249. }
  2250. crypto_seed_rng();
  2251. if (0) {
  2252. bench_aes();
  2253. return 0;
  2254. }
  2255. atexit(remove_directory);
  2256. printf("Running Tor unit tests on %s\n", get_uname());
  2257. puts("========================== Buffers =========================");
  2258. test_buffers();
  2259. puts("\n========================== Crypto ==========================");
  2260. // add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout);
  2261. test_crypto();
  2262. test_crypto_dh();
  2263. test_crypto_s2k();
  2264. puts("\n========================= Util ============================");
  2265. test_ip6_helpers();
  2266. test_gzip();
  2267. test_util();
  2268. test_smartlist();
  2269. test_mempool();
  2270. test_strmap();
  2271. test_control_formats();
  2272. test_pqueue();
  2273. test_mmap();
  2274. test_threads();
  2275. puts("\n========================= Onion Skins =====================");
  2276. test_onion_handshake();
  2277. puts("\n========================= Directory Formats ===============");
  2278. test_dir_format();
  2279. test_v3_networkstatus();
  2280. puts("\n========================= Policies ===================");
  2281. test_policies();
  2282. puts("\n========================= Rendezvous functionality ========");
  2283. test_rend_fns();
  2284. puts("");
  2285. if (have_failed)
  2286. return 1;
  2287. else
  2288. return 0;
  2289. }