test_buffers.c 26 KB

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