test_buffers.c 23 KB

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