test.c 61 KB

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