test_buffers.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define BUFFERS_PRIVATE
  6. #define PROTO_HTTP_PRIVATE
  7. #include "or.h"
  8. #include "buffers.h"
  9. #include "buffers_tls.h"
  10. #include "crypto_rand.h"
  11. #include "proto_http.h"
  12. #include "proto_socks.h"
  13. #include "test.h"
  14. /** Run unit tests for buffers.c */
  15. static void
  16. test_buffers_basic(void *arg)
  17. {
  18. char str[256];
  19. char str2[256];
  20. buf_t *buf = NULL, *buf2 = NULL;
  21. const char *cp;
  22. int j;
  23. size_t r;
  24. (void) arg;
  25. /****
  26. * buf_new
  27. ****/
  28. if (!(buf = buf_new()))
  29. TT_DIE(("Assertion failed."));
  30. //test_eq(buf_capacity(buf), 4096);
  31. tt_int_op(buf_datalen(buf),OP_EQ, 0);
  32. /****
  33. * General pointer frobbing
  34. */
  35. for (j=0;j<256;++j) {
  36. str[j] = (char)j;
  37. }
  38. buf_add(buf, str, 256);
  39. buf_add(buf, str, 256);
  40. tt_int_op(buf_datalen(buf),OP_EQ, 512);
  41. buf_get_bytes(buf, str2, 200);
  42. tt_mem_op(str,OP_EQ, str2, 200);
  43. tt_int_op(buf_datalen(buf),OP_EQ, 312);
  44. memset(str2, 0, sizeof(str2));
  45. buf_get_bytes(buf, str2, 256);
  46. tt_mem_op(str+200,OP_EQ, str2, 56);
  47. tt_mem_op(str,OP_EQ, str2+56, 200);
  48. tt_int_op(buf_datalen(buf),OP_EQ, 56);
  49. memset(str2, 0, sizeof(str2));
  50. /* Okay, now we should be 512 bytes into the 4096-byte buffer. If we add
  51. * another 3584 bytes, we hit the end. */
  52. for (j=0;j<15;++j) {
  53. buf_add(buf, str, 256);
  54. }
  55. buf_assert_ok(buf);
  56. tt_int_op(buf_datalen(buf),OP_EQ, 3896);
  57. buf_get_bytes(buf, str2, 56);
  58. tt_int_op(buf_datalen(buf),OP_EQ, 3840);
  59. tt_mem_op(str+200,OP_EQ, str2, 56);
  60. for (j=0;j<15;++j) {
  61. memset(str2, 0, sizeof(str2));
  62. buf_get_bytes(buf, str2, 256);
  63. tt_mem_op(str,OP_EQ, str2, 256);
  64. }
  65. tt_int_op(buf_datalen(buf),OP_EQ, 0);
  66. buf_free(buf);
  67. buf = NULL;
  68. /* Okay, now make sure growing can work. */
  69. buf = buf_new_with_capacity(16);
  70. //test_eq(buf_capacity(buf), 16);
  71. buf_add(buf, str+1, 255);
  72. //test_eq(buf_capacity(buf), 256);
  73. buf_get_bytes(buf, str2, 254);
  74. tt_mem_op(str+1,OP_EQ, str2, 254);
  75. //test_eq(buf_capacity(buf), 256);
  76. buf_assert_ok(buf);
  77. buf_add(buf, str, 32);
  78. //test_eq(buf_capacity(buf), 256);
  79. buf_assert_ok(buf);
  80. buf_add(buf, str, 256);
  81. buf_assert_ok(buf);
  82. //test_eq(buf_capacity(buf), 512);
  83. tt_int_op(buf_datalen(buf),OP_EQ, 33+256);
  84. buf_get_bytes(buf, str2, 33);
  85. tt_int_op(*str2,OP_EQ, str[255]);
  86. tt_mem_op(str2+1,OP_EQ, str, 32);
  87. //test_eq(buf_capacity(buf), 512);
  88. tt_int_op(buf_datalen(buf),OP_EQ, 256);
  89. buf_get_bytes(buf, str2, 256);
  90. tt_mem_op(str,OP_EQ, str2, 256);
  91. /* now try shrinking: case 1. */
  92. buf_free(buf);
  93. buf = buf_new_with_capacity(33668);
  94. for (j=0;j<67;++j) {
  95. buf_add(buf, str,255);
  96. }
  97. //test_eq(buf_capacity(buf), 33668);
  98. tt_int_op(buf_datalen(buf),OP_EQ, 17085);
  99. for (j=0; j < 40; ++j) {
  100. buf_get_bytes(buf, str2, 255);
  101. tt_mem_op(str2,OP_EQ, str, 255);
  102. }
  103. /* now try shrinking: case 2. */
  104. buf_free(buf);
  105. buf = buf_new_with_capacity(33668);
  106. for (j=0;j<67;++j) {
  107. buf_add(buf, str, 255);
  108. }
  109. for (j=0; j < 20; ++j) {
  110. buf_get_bytes(buf, str2, 255);
  111. tt_mem_op(str2,OP_EQ, str, 255);
  112. }
  113. for (j=0;j<80;++j) {
  114. buf_add(buf, str, 255);
  115. }
  116. //test_eq(buf_capacity(buf),33668);
  117. for (j=0; j < 120; ++j) {
  118. buf_get_bytes(buf, str2, 255);
  119. tt_mem_op(str2,OP_EQ, str, 255);
  120. }
  121. /* Move from buf to buf. */
  122. buf_free(buf);
  123. buf = buf_new_with_capacity(4096);
  124. buf2 = buf_new_with_capacity(4096);
  125. for (j=0;j<100;++j)
  126. buf_add(buf, str, 255);
  127. tt_int_op(buf_datalen(buf),OP_EQ, 25500);
  128. for (j=0;j<100;++j) {
  129. r = 10;
  130. buf_move_to_buf(buf2, buf, &r);
  131. tt_int_op(r,OP_EQ, 0);
  132. }
  133. tt_int_op(buf_datalen(buf),OP_EQ, 24500);
  134. tt_int_op(buf_datalen(buf2),OP_EQ, 1000);
  135. for (j=0;j<3;++j) {
  136. buf_get_bytes(buf2, str2, 255);
  137. tt_mem_op(str2,OP_EQ, str, 255);
  138. }
  139. r = 8192; /*big move*/
  140. buf_move_to_buf(buf2, buf, &r);
  141. tt_int_op(r,OP_EQ, 0);
  142. r = 30000; /* incomplete move */
  143. buf_move_to_buf(buf2, buf, &r);
  144. tt_int_op(r,OP_EQ, 13692);
  145. for (j=0;j<97;++j) {
  146. buf_get_bytes(buf2, str2, 255);
  147. tt_mem_op(str2,OP_EQ, str, 255);
  148. }
  149. buf_free(buf);
  150. buf_free(buf2);
  151. buf = buf2 = NULL;
  152. buf = buf_new_with_capacity(5);
  153. cp = "Testing. This is a moderately long Testing string.";
  154. for (j = 0; cp[j]; j++)
  155. buf_add(buf, cp+j, 1);
  156. tt_int_op(0,OP_EQ, buf_find_string_offset(buf, "Testing", 7));
  157. tt_int_op(1,OP_EQ, buf_find_string_offset(buf, "esting", 6));
  158. tt_int_op(1,OP_EQ, buf_find_string_offset(buf, "est", 3));
  159. tt_int_op(39,OP_EQ, buf_find_string_offset(buf, "ing str", 7));
  160. tt_int_op(35,OP_EQ, buf_find_string_offset(buf, "Testing str", 11));
  161. tt_int_op(32,OP_EQ, buf_find_string_offset(buf, "ng ", 3));
  162. tt_int_op(43,OP_EQ, buf_find_string_offset(buf, "string.", 7));
  163. tt_int_op(-1,OP_EQ, buf_find_string_offset(buf, "shrdlu", 6));
  164. tt_int_op(-1,OP_EQ, buf_find_string_offset(buf, "Testing thing", 13));
  165. tt_int_op(-1,OP_EQ, buf_find_string_offset(buf, "ngx", 3));
  166. buf_free(buf);
  167. buf = NULL;
  168. /* Try adding a string too long for any freelist. */
  169. {
  170. char *mem = tor_malloc_zero(65536);
  171. buf = buf_new();
  172. buf_add(buf, mem, 65536);
  173. tor_free(mem);
  174. tt_int_op(buf_datalen(buf), OP_EQ, 65536);
  175. buf_free(buf);
  176. buf = NULL;
  177. }
  178. done:
  179. if (buf)
  180. buf_free(buf);
  181. if (buf2)
  182. buf_free(buf2);
  183. }
  184. static void
  185. test_buffer_pullup(void *arg)
  186. {
  187. buf_t *buf;
  188. char *stuff, *tmp;
  189. const char *cp;
  190. size_t sz;
  191. (void)arg;
  192. stuff = tor_malloc(16384);
  193. tmp = tor_malloc(16384);
  194. buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
  195. tt_assert(buf);
  196. tt_int_op(buf_get_default_chunk_size(buf), OP_EQ, 4096);
  197. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  198. /* There are a bunch of cases for pullup. One is the trivial case. Let's
  199. mess around with an empty buffer. */
  200. buf_pullup(buf, 16, &cp, &sz);
  201. tt_ptr_op(cp, OP_EQ, NULL);
  202. tt_uint_op(sz, OP_EQ, 0);
  203. /* Let's make sure nothing got allocated */
  204. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  205. /* Case 1: everything puts into the first chunk with some moving. */
  206. /* Let's add some data. */
  207. crypto_rand(stuff, 16384);
  208. buf_add(buf, stuff, 3000);
  209. buf_add(buf, stuff+3000, 3000);
  210. buf_pullup(buf, 0, &cp, &sz);
  211. tt_ptr_op(cp, OP_NE, NULL);
  212. tt_int_op(sz, OP_LE, 4096);
  213. /* Make room for 3000 bytes in the first chunk, so that the pullup-move code
  214. * can get tested. */
  215. tt_int_op(buf_get_bytes(buf, tmp, 3000), OP_EQ, 3000);
  216. tt_mem_op(tmp,OP_EQ, stuff, 3000);
  217. buf_pullup(buf, 2048, &cp, &sz);
  218. buf_assert_ok(buf);
  219. tt_ptr_op(cp, OP_NE, NULL);
  220. tt_int_op(sz, OP_GE, 2048);
  221. tt_mem_op(cp,OP_EQ, stuff+3000, 2048);
  222. tt_int_op(3000, OP_EQ, buf_datalen(buf));
  223. tt_int_op(buf_get_bytes(buf, tmp, 3000), OP_EQ, 0);
  224. tt_mem_op(tmp,OP_EQ, stuff+3000, 2048);
  225. buf_free(buf);
  226. /* Now try the large-chunk case. */
  227. buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
  228. buf_add(buf, stuff, 4000);
  229. buf_add(buf, stuff+4000, 4000);
  230. buf_add(buf, stuff+8000, 4000);
  231. buf_add(buf, stuff+12000, 4000);
  232. tt_int_op(buf_datalen(buf), OP_EQ, 16000);
  233. buf_pullup(buf, 0, &cp, &sz);
  234. tt_ptr_op(cp, OP_NE, NULL);
  235. tt_int_op(sz, OP_LE, 4096);
  236. buf_pullup(buf, 12500, &cp, &sz);
  237. buf_assert_ok(buf);
  238. tt_ptr_op(cp, OP_NE, NULL);
  239. tt_int_op(sz, OP_GE, 12500);
  240. tt_mem_op(cp,OP_EQ, stuff, 12500);
  241. tt_int_op(buf_datalen(buf), OP_EQ, 16000);
  242. buf_get_bytes(buf, tmp, 12400);
  243. tt_mem_op(tmp,OP_EQ, stuff, 12400);
  244. tt_int_op(buf_datalen(buf), OP_EQ, 3600);
  245. buf_get_bytes(buf, tmp, 3500);
  246. tt_mem_op(tmp,OP_EQ, stuff+12400, 3500);
  247. buf_get_bytes(buf, tmp, 100);
  248. tt_mem_op(tmp,OP_EQ, stuff+15900, 10);
  249. buf_free(buf);
  250. /* Make sure that the pull-up-whole-buffer case works */
  251. buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
  252. buf_add(buf, stuff, 4000);
  253. buf_add(buf, stuff+4000, 4000);
  254. buf_get_bytes(buf, tmp, 100); /* dump 100 bytes from first chunk */
  255. buf_pullup(buf, 16000, &cp, &sz);
  256. buf_assert_ok(buf);
  257. tt_ptr_op(cp, OP_NE, NULL);
  258. tt_int_op(sz, OP_EQ, 7900);
  259. tt_mem_op(cp,OP_EQ, stuff+100, 7900);
  260. buf_free(buf);
  261. buf = NULL;
  262. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  263. done:
  264. buf_free(buf);
  265. tor_free(stuff);
  266. tor_free(tmp);
  267. }
  268. static void
  269. test_buffer_copy(void *arg)
  270. {
  271. buf_t *buf=NULL, *buf2=NULL;
  272. const char *s;
  273. size_t len;
  274. char b[256];
  275. int i;
  276. (void)arg;
  277. buf = buf_new();
  278. tt_assert(buf);
  279. /* Copy an empty buffer. */
  280. tt_int_op(0, OP_EQ, buf_set_to_copy(&buf2, buf));
  281. tt_assert(buf2);
  282. tt_int_op(0, OP_EQ, buf_datalen(buf2));
  283. /* Now try with a short buffer. */
  284. s = "And now comes an act of enormous enormance!";
  285. len = strlen(s);
  286. buf_add(buf, s, len);
  287. tt_int_op(len, OP_EQ, buf_datalen(buf));
  288. /* Add junk to buf2 so we can test replacing.*/
  289. buf_add(buf2, "BLARG", 5);
  290. tt_int_op(0, OP_EQ, buf_set_to_copy(&buf2, buf));
  291. tt_int_op(len, OP_EQ, buf_datalen(buf2));
  292. buf_get_bytes(buf2, b, len);
  293. tt_mem_op(b, OP_EQ, s, len);
  294. /* Now free buf2 and retry so we can test allocating */
  295. buf_free(buf2);
  296. buf2 = NULL;
  297. tt_int_op(0, OP_EQ, buf_set_to_copy(&buf2, buf));
  298. tt_int_op(len, OP_EQ, buf_datalen(buf2));
  299. buf_get_bytes(buf2, b, len);
  300. tt_mem_op(b, OP_EQ, s, len);
  301. /* Clear buf for next test */
  302. buf_get_bytes(buf, b, len);
  303. tt_int_op(buf_datalen(buf),OP_EQ,0);
  304. /* Okay, now let's try a bigger buffer. */
  305. s = "Quis autem vel eum iure reprehenderit qui in ea voluptate velit "
  306. "esse quam nihil molestiae consequatur, vel illum qui dolorem eum "
  307. "fugiat quo voluptas nulla pariatur?";
  308. len = strlen(s);
  309. for (i = 0; i < 256; ++i) {
  310. b[0]=i;
  311. buf_add(buf, b, 1);
  312. buf_add(buf, s, len);
  313. }
  314. tt_int_op(0, OP_EQ, buf_set_to_copy(&buf2, buf));
  315. tt_int_op(buf_datalen(buf2), OP_EQ, buf_datalen(buf));
  316. for (i = 0; i < 256; ++i) {
  317. buf_get_bytes(buf2, b, len+1);
  318. tt_int_op((unsigned char)b[0],OP_EQ,i);
  319. tt_mem_op(b+1, OP_EQ, s, len);
  320. }
  321. done:
  322. if (buf)
  323. buf_free(buf);
  324. if (buf2)
  325. buf_free(buf2);
  326. }
  327. static void
  328. test_buffer_allocation_tracking(void *arg)
  329. {
  330. char *junk = tor_malloc(16384);
  331. buf_t *buf1 = NULL, *buf2 = NULL;
  332. int i;
  333. (void)arg;
  334. crypto_rand(junk, 16384);
  335. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  336. buf1 = buf_new();
  337. tt_assert(buf1);
  338. buf2 = buf_new();
  339. tt_assert(buf2);
  340. tt_int_op(buf_allocation(buf1), OP_EQ, 0);
  341. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  342. buf_add(buf1, junk, 4000);
  343. buf_add(buf1, junk, 4000);
  344. buf_add(buf1, junk, 4000);
  345. buf_add(buf1, junk, 4000);
  346. tt_int_op(buf_allocation(buf1), OP_EQ, 16384);
  347. buf_get_bytes(buf1, junk, 100);
  348. tt_int_op(buf_allocation(buf1), OP_EQ, 16384); /* still 4 4k chunks */
  349. tt_int_op(buf_get_total_allocation(), OP_EQ, 16384);
  350. buf_get_bytes(buf1, junk, 4096); /* drop a 1k chunk... */
  351. tt_int_op(buf_allocation(buf1), OP_EQ, 3*4096); /* now 3 4k chunks */
  352. tt_int_op(buf_get_total_allocation(), OP_EQ, 12288); /* that chunk was really
  353. freed. */
  354. buf_add(buf2, junk, 4000);
  355. tt_int_op(buf_allocation(buf2), OP_EQ, 4096); /* another 4k chunk. */
  356. /*
  357. * We bounce back up to 16384 by allocating a new chunk.
  358. */
  359. tt_int_op(buf_get_total_allocation(), OP_EQ, 16384);
  360. buf_add(buf2, junk, 4000);
  361. tt_int_op(buf_allocation(buf2), OP_EQ, 8192); /* another 4k chunk. */
  362. tt_int_op(buf_get_total_allocation(),
  363. OP_EQ, 5*4096); /* that chunk was new. */
  364. /* Make a really huge buffer */
  365. for (i = 0; i < 1000; ++i) {
  366. buf_add(buf2, junk, 4000);
  367. }
  368. tt_int_op(buf_allocation(buf2), OP_GE, 4008000);
  369. tt_int_op(buf_get_total_allocation(), OP_GE, 4008000);
  370. buf_free(buf2);
  371. buf2 = NULL;
  372. tt_int_op(buf_get_total_allocation(), OP_LT, 4008000);
  373. tt_int_op(buf_get_total_allocation(), OP_EQ, buf_allocation(buf1));
  374. buf_free(buf1);
  375. buf1 = NULL;
  376. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  377. done:
  378. buf_free(buf1);
  379. buf_free(buf2);
  380. tor_free(junk);
  381. }
  382. static void
  383. test_buffer_time_tracking(void *arg)
  384. {
  385. buf_t *buf=NULL, *buf2=NULL;
  386. const time_t START = 1389288246;
  387. const uint64_t START_NSEC = ((uint64_t)START) * 1000000000;
  388. int i;
  389. char tmp[4096];
  390. (void)arg;
  391. crypto_rand(tmp, sizeof(tmp));
  392. monotime_enable_test_mocking();
  393. buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
  394. tt_assert(buf);
  395. monotime_coarse_set_mock_time_nsec(START_NSEC);
  396. const uint32_t START_TS = monotime_coarse_get_stamp();
  397. /* Empty buffer means the timestamp is 0. */
  398. tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_TS));
  399. tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_TS+1000));
  400. buf_add(buf, "ABCDEFG", 7);
  401. tt_int_op(1000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_TS+1000));
  402. buf2 = buf_copy(buf);
  403. tt_assert(buf2);
  404. tt_int_op(1234, OP_EQ,
  405. buf_get_oldest_chunk_timestamp(buf2, START_TS+1234));
  406. /* Now add more bytes; enough to overflow the first chunk. */
  407. monotime_coarse_set_mock_time_nsec(START_NSEC + 123 * (uint64_t)1000000);
  408. const uint32_t TS2 = monotime_coarse_get_stamp();
  409. for (i = 0; i < 600; ++i)
  410. buf_add(buf, "ABCDEFG", 7);
  411. tt_int_op(4207, OP_EQ, buf_datalen(buf));
  412. /* The oldest bytes are still in the front. */
  413. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_TS+2000));
  414. /* Once those bytes are dropped, the chunk is still on the first
  415. * timestamp. */
  416. buf_get_bytes(buf, tmp, 100);
  417. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_TS+2000));
  418. /* But once we discard the whole first chunk, we get the data in the second
  419. * chunk. */
  420. buf_get_bytes(buf, tmp, 4000);
  421. tt_int_op(107, OP_EQ, buf_datalen(buf));
  422. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, TS2+2000));
  423. /* This time we'll be grabbing a chunk from the freelist, and making sure
  424. its time gets updated */
  425. monotime_coarse_set_mock_time_nsec(START_NSEC + 5617 * (uint64_t)1000000);
  426. const uint32_t TS3 = monotime_coarse_get_stamp();
  427. for (i = 0; i < 600; ++i)
  428. buf_add(buf, "ABCDEFG", 7);
  429. tt_int_op(4307, OP_EQ, buf_datalen(buf));
  430. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, TS2+2000));
  431. buf_get_bytes(buf, tmp, 4000);
  432. buf_get_bytes(buf, tmp, 306);
  433. tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, TS3));
  434. tt_int_op(383, OP_EQ, buf_get_oldest_chunk_timestamp(buf, TS3+383));
  435. done:
  436. buf_free(buf);
  437. buf_free(buf2);
  438. monotime_disable_test_mocking();
  439. }
  440. static void
  441. test_buffers_compress_fin_at_chunk_end_impl(compress_method_t method,
  442. compression_level_t level)
  443. {
  444. char *msg = NULL;
  445. char *contents = NULL;
  446. char *expanded = NULL;
  447. buf_t *buf = NULL;
  448. tor_compress_state_t *compress_state = NULL;
  449. size_t out_len, in_len;
  450. size_t sz, headerjunk;
  451. buf = buf_new_with_capacity(128); /* will round up */
  452. sz = buf_get_default_chunk_size(buf);
  453. msg = tor_malloc_zero(sz);
  454. buf_add(buf, msg, 1);
  455. tt_assert(buf->head);
  456. /* Fill up the chunk so the compression stuff won't fit in one chunk. */
  457. tt_uint_op(buf->head->memlen, OP_LT, sz);
  458. headerjunk = buf->head->memlen - 7;
  459. buf_add(buf, msg, headerjunk-1);
  460. tt_uint_op(buf->head->datalen, OP_EQ, headerjunk);
  461. tt_uint_op(buf_datalen(buf), OP_EQ, headerjunk);
  462. /* Write an empty string, with finalization on. */
  463. compress_state = tor_compress_new(1, method, level);
  464. tt_int_op(buf_add_compress(buf, compress_state, "", 0, 1), OP_EQ, 0);
  465. in_len = buf_datalen(buf);
  466. contents = tor_malloc(in_len);
  467. tt_int_op(buf_get_bytes(buf, contents, in_len), OP_EQ, 0);
  468. if (method == NO_METHOD) {
  469. tt_uint_op(in_len, OP_EQ, headerjunk);
  470. } else {
  471. tt_uint_op(in_len, OP_GT, headerjunk);
  472. }
  473. tt_int_op(0, OP_EQ, tor_uncompress(&expanded, &out_len,
  474. contents + headerjunk,
  475. in_len - headerjunk,
  476. method, 1,
  477. LOG_WARN));
  478. tt_int_op(out_len, OP_EQ, 0);
  479. tt_assert(expanded);
  480. done:
  481. buf_free(buf);
  482. tor_compress_free(compress_state);
  483. tor_free(contents);
  484. tor_free(expanded);
  485. tor_free(msg);
  486. }
  487. static void
  488. test_buffers_compress_impl(compress_method_t method,
  489. compression_level_t level,
  490. int finalize_with_nil)
  491. {
  492. char *msg = NULL;
  493. char *contents = NULL;
  494. char *expanded = NULL;
  495. buf_t *buf = NULL;
  496. tor_compress_state_t *compress_state = NULL;
  497. size_t out_len, in_len;
  498. int done;
  499. buf = buf_new_with_capacity(128); /* will round up */
  500. compress_state = tor_compress_new(1, method, level);
  501. msg = tor_malloc(512);
  502. crypto_rand(msg, 512);
  503. tt_int_op(buf_add_compress(buf, compress_state,
  504. msg, 128, 0), OP_EQ, 0);
  505. tt_int_op(buf_add_compress(buf, compress_state,
  506. msg+128, 128, 0), OP_EQ, 0);
  507. tt_int_op(buf_add_compress(buf, compress_state,
  508. msg+256, 256, 0), OP_EQ, 0);
  509. done = !finalize_with_nil;
  510. tt_int_op(buf_add_compress(buf, compress_state,
  511. "all done", 9, done), OP_EQ, 0);
  512. if (finalize_with_nil) {
  513. tt_int_op(buf_add_compress(buf, compress_state, "", 0, 1), OP_EQ, 0);
  514. }
  515. in_len = buf_datalen(buf);
  516. contents = tor_malloc(in_len);
  517. tt_int_op(buf_get_bytes(buf, contents, in_len), OP_EQ, 0);
  518. tt_int_op(0, OP_EQ, tor_uncompress(&expanded, &out_len,
  519. contents, in_len,
  520. method, 1,
  521. LOG_WARN));
  522. tt_int_op(out_len, OP_GE, 128);
  523. tt_mem_op(msg, OP_EQ, expanded, 128);
  524. tt_int_op(out_len, OP_GE, 512);
  525. tt_mem_op(msg, OP_EQ, expanded, 512);
  526. tt_int_op(out_len, OP_EQ, 512+9);
  527. tt_mem_op("all done", OP_EQ, expanded+512, 9);
  528. done:
  529. buf_free(buf);
  530. tor_compress_free(compress_state);
  531. tor_free(contents);
  532. tor_free(expanded);
  533. tor_free(msg);
  534. }
  535. static void
  536. test_buffers_compress(void *arg)
  537. {
  538. const char *methodname = arg;
  539. tt_assert(methodname);
  540. compress_method_t method = compression_method_get_by_name(methodname);
  541. tt_int_op(method, OP_NE, UNKNOWN_METHOD);
  542. if (! tor_compress_supports_method(method)) {
  543. tt_skip();
  544. }
  545. compression_level_t levels[] = {
  546. BEST_COMPRESSION,
  547. HIGH_COMPRESSION,
  548. MEDIUM_COMPRESSION,
  549. LOW_COMPRESSION
  550. };
  551. for (unsigned l = 0; l < ARRAY_LENGTH(levels); ++l) {
  552. compression_level_t level = levels[l];
  553. test_buffers_compress_impl(method, level, 0);
  554. test_buffers_compress_impl(method, level, 1);
  555. test_buffers_compress_fin_at_chunk_end_impl(method, level);
  556. }
  557. done:
  558. ;
  559. }
  560. static const uint8_t *tls_read_ptr;
  561. static int n_remaining;
  562. static int next_reply_val[16];
  563. static int
  564. mock_tls_read(tor_tls_t *tls, char *cp, size_t len)
  565. {
  566. (void)tls;
  567. int rv = next_reply_val[0];
  568. if (rv > 0) {
  569. int max = rv > (int)len ? (int)len : rv;
  570. if (max > n_remaining)
  571. max = n_remaining;
  572. memcpy(cp, tls_read_ptr, max);
  573. rv = max;
  574. n_remaining -= max;
  575. tls_read_ptr += max;
  576. }
  577. memmove(next_reply_val, next_reply_val + 1, 15*sizeof(int));
  578. return rv;
  579. }
  580. static void
  581. test_buffers_tls_read_mocked(void *arg)
  582. {
  583. uint8_t *mem;
  584. buf_t *buf;
  585. (void)arg;
  586. mem = tor_malloc(64*1024);
  587. crypto_rand((char*)mem, 64*1024);
  588. tls_read_ptr = mem;
  589. n_remaining = 64*1024;
  590. MOCK(tor_tls_read, mock_tls_read);
  591. buf = buf_new();
  592. next_reply_val[0] = 1024;
  593. tt_int_op(128, OP_EQ, buf_read_from_tls(buf, NULL, 128));
  594. next_reply_val[0] = 5000;
  595. next_reply_val[1] = 5000;
  596. tt_int_op(6000, OP_EQ, buf_read_from_tls(buf, NULL, 6000));
  597. done:
  598. UNMOCK(tor_tls_read);
  599. tor_free(mem);
  600. buf_free(buf);
  601. }
  602. static void
  603. test_buffers_chunk_size(void *arg)
  604. {
  605. (void)arg;
  606. const int min = 256;
  607. const int max = 65536;
  608. tt_uint_op(buf_preferred_chunk_size(3), OP_EQ, min);
  609. tt_uint_op(buf_preferred_chunk_size(25), OP_EQ, min);
  610. tt_uint_op(buf_preferred_chunk_size(0), OP_EQ, min);
  611. tt_uint_op(buf_preferred_chunk_size(256), OP_EQ, 512);
  612. tt_uint_op(buf_preferred_chunk_size(65400), OP_EQ, max);
  613. /* Here, we're implicitly saying that the chunk header overhead is
  614. * between 1 and 100 bytes. 24..48 would probably be more accurate. */
  615. tt_uint_op(buf_preferred_chunk_size(65536), OP_GT, 65536);
  616. tt_uint_op(buf_preferred_chunk_size(65536), OP_LT, 65536+100);
  617. tt_uint_op(buf_preferred_chunk_size(165536), OP_GT, 165536);
  618. tt_uint_op(buf_preferred_chunk_size(165536), OP_LT, 165536+100);
  619. done:
  620. ;
  621. }
  622. static void
  623. test_buffers_find_contentlen(void *arg)
  624. {
  625. static const struct {
  626. const char *headers;
  627. int r;
  628. int contentlen;
  629. } results[] = {
  630. { "Blah blah\r\nContent-Length: 1\r\n\r\n", 1, 1 },
  631. { "Blah blah\r\n\r\n", 0, 0 }, /* no content-len */
  632. { "Blah blah Content-Length: 1\r\n", 0, 0 }, /* no content-len. */
  633. { "Blah blah\r\nContent-Length: 100000\r\n", 1, 100000},
  634. { "Blah blah\r\nContent-Length: 1000000000000000000000000\r\n", -1, 0},
  635. { "Blah blah\r\nContent-Length: 0\r\n", 1, 0},
  636. { "Blah blah\r\nContent-Length: -1\r\n", -1, 0},
  637. { "Blah blah\r\nContent-Length: 1x\r\n", -1, 0},
  638. { "Blah blah\r\nContent-Length: 1 x\r\n", -1, 0},
  639. { "Blah blah\r\nContent-Length: 1 \r\n", 1, 1},
  640. { "Blah blah\r\nContent-Length: \r\n", -1, 0},
  641. { "Blah blah\r\nContent-Length: ", -1, 0},
  642. { "Blah blah\r\nContent-Length: 5050", -1, 0},
  643. { NULL, 0, 0 }
  644. };
  645. int i;
  646. (void)arg;
  647. for (i = 0; results[i].headers; ++i) {
  648. int r;
  649. size_t sz;
  650. size_t headerlen = strlen(results[i].headers);
  651. char * tmp = tor_memdup(results[i].headers, headerlen);/* ensure no eos */
  652. sz = 999; /* to ensure it gets set */
  653. r = buf_http_find_content_length(tmp, headerlen, &sz);
  654. tor_free(tmp);
  655. log_debug(LD_DIR, "%d: %s", i, escaped(results[i].headers));
  656. tt_int_op(r, OP_EQ, results[i].r);
  657. tt_int_op(sz, OP_EQ, results[i].contentlen);
  658. }
  659. done:
  660. ;
  661. }
  662. static void
  663. test_buffer_peek_startswith(void *arg)
  664. {
  665. (void)arg;
  666. buf_t *buf;
  667. buf = buf_new();
  668. tt_ptr_op(buf, OP_NE, NULL);
  669. tt_assert(buf_peek_startswith(buf, ""));
  670. tt_assert(! buf_peek_startswith(buf, "X"));
  671. buf_add(buf, "Tor", 3);
  672. tt_assert(buf_peek_startswith(buf, ""));
  673. tt_assert(buf_peek_startswith(buf, "T"));
  674. tt_assert(buf_peek_startswith(buf, "To"));
  675. tt_assert(buf_peek_startswith(buf, "Tor"));
  676. tt_assert(! buf_peek_startswith(buf, "Top"));
  677. tt_assert(! buf_peek_startswith(buf, "For"));
  678. tt_assert(! buf_peek_startswith(buf, "Tork"));
  679. tt_assert(! buf_peek_startswith(buf, "Torpor"));
  680. done:
  681. buf_free(buf);
  682. }
  683. struct testcase_t buffer_tests[] = {
  684. { "basic", test_buffers_basic, TT_FORK, NULL, NULL },
  685. { "copy", test_buffer_copy, TT_FORK, NULL, NULL },
  686. { "pullup", test_buffer_pullup, TT_FORK, NULL, NULL },
  687. { "startswith", test_buffer_peek_startswith, 0, NULL, NULL },
  688. { "allocation_tracking", test_buffer_allocation_tracking, TT_FORK,
  689. NULL, NULL },
  690. { "time_tracking", test_buffer_time_tracking, TT_FORK, NULL, NULL },
  691. { "tls_read_mocked", test_buffers_tls_read_mocked, 0,
  692. NULL, NULL },
  693. { "chunk_size", test_buffers_chunk_size, 0, NULL, NULL },
  694. { "find_contentlen", test_buffers_find_contentlen, 0, NULL, NULL },
  695. { "compress/zlib", test_buffers_compress, TT_FORK,
  696. &passthrough_setup, (char*)"deflate" },
  697. { "compress/gzip", test_buffers_compress, TT_FORK,
  698. &passthrough_setup, (char*)"gzip" },
  699. { "compress/zstd", test_buffers_compress, TT_FORK,
  700. &passthrough_setup, (char*)"x-zstd" },
  701. { "compress/lzma", test_buffers_compress, TT_FORK,
  702. &passthrough_setup, (char*)"x-tor-lzma" },
  703. { "compress/none", test_buffers_compress, TT_FORK,
  704. &passthrough_setup, (char*)"identity" },
  705. END_OF_TESTCASES
  706. };