test.c 75 KB

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