test_buffers.c 25 KB

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