test_buffers.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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_ext_or.h"
  12. #include "proto_http.h"
  13. #include "proto_control0.h"
  14. #include "proto_socks.h"
  15. #include "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. write_to_buf(str, 256, buf);
  41. write_to_buf(str, 256, buf);
  42. tt_int_op(buf_datalen(buf),OP_EQ, 512);
  43. fetch_from_buf(str2, 200, buf);
  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. fetch_from_buf(str2, 256, buf);
  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. write_to_buf(str, 256, buf);
  56. }
  57. assert_buf_ok(buf);
  58. tt_int_op(buf_datalen(buf),OP_EQ, 3896);
  59. fetch_from_buf(str2, 56, buf);
  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. fetch_from_buf(str2, 256, buf);
  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. write_to_buf(str+1, 255, buf);
  74. //test_eq(buf_capacity(buf), 256);
  75. fetch_from_buf(str2, 254, buf);
  76. tt_mem_op(str+1,OP_EQ, str2, 254);
  77. //test_eq(buf_capacity(buf), 256);
  78. assert_buf_ok(buf);
  79. write_to_buf(str, 32, buf);
  80. //test_eq(buf_capacity(buf), 256);
  81. assert_buf_ok(buf);
  82. write_to_buf(str, 256, buf);
  83. assert_buf_ok(buf);
  84. //test_eq(buf_capacity(buf), 512);
  85. tt_int_op(buf_datalen(buf),OP_EQ, 33+256);
  86. fetch_from_buf(str2, 33, buf);
  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. fetch_from_buf(str2, 256, buf);
  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. write_to_buf(str,255, buf);
  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. fetch_from_buf(str2, 255,buf);
  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. write_to_buf(str,255, buf);
  110. }
  111. for (j=0; j < 20; ++j) {
  112. fetch_from_buf(str2, 255,buf);
  113. tt_mem_op(str2,OP_EQ, str, 255);
  114. }
  115. for (j=0;j<80;++j) {
  116. write_to_buf(str,255, buf);
  117. }
  118. //test_eq(buf_capacity(buf),33668);
  119. for (j=0; j < 120; ++j) {
  120. fetch_from_buf(str2, 255,buf);
  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. write_to_buf(str, 255, buf);
  129. tt_int_op(buf_datalen(buf),OP_EQ, 25500);
  130. for (j=0;j<100;++j) {
  131. r = 10;
  132. move_buf_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. fetch_from_buf(str2, 255, buf2);
  139. tt_mem_op(str2,OP_EQ, str, 255);
  140. }
  141. r = 8192; /*big move*/
  142. move_buf_to_buf(buf2, buf, &r);
  143. tt_int_op(r,OP_EQ, 0);
  144. r = 30000; /* incomplete move */
  145. move_buf_to_buf(buf2, buf, &r);
  146. tt_int_op(r,OP_EQ, 13692);
  147. for (j=0;j<97;++j) {
  148. fetch_from_buf(str2, 255, buf2);
  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. write_to_buf(cp+j, 1, buf);
  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. write_to_buf(mem, 65536, buf);
  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. write_to_buf(stuff, 3000, buf);
  211. write_to_buf(stuff+3000, 3000, buf);
  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(fetch_from_buf(tmp, 3000, buf), OP_EQ, 3000);
  218. tt_mem_op(tmp,OP_EQ, stuff, 3000);
  219. buf_pullup(buf, 2048, &cp, &sz);
  220. assert_buf_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(fetch_from_buf(tmp, 3000, buf), 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. write_to_buf(stuff, 4000, buf);
  231. write_to_buf(stuff+4000, 4000, buf);
  232. write_to_buf(stuff+8000, 4000, buf);
  233. write_to_buf(stuff+12000, 4000, buf);
  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. assert_buf_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. fetch_from_buf(tmp, 12400, buf);
  245. tt_mem_op(tmp,OP_EQ, stuff, 12400);
  246. tt_int_op(buf_datalen(buf), OP_EQ, 3600);
  247. fetch_from_buf(tmp, 3500, buf);
  248. tt_mem_op(tmp,OP_EQ, stuff+12400, 3500);
  249. fetch_from_buf(tmp, 100, buf);
  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. write_to_buf(stuff, 4000, buf);
  255. write_to_buf(stuff+4000, 4000, buf);
  256. fetch_from_buf(tmp, 100, buf); /* dump 100 bytes from first chunk */
  257. buf_pullup(buf, 16000, &cp, &sz);
  258. assert_buf_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. write_to_buf(s, len, buf);
  289. tt_int_op(len, OP_EQ, buf_datalen(buf));
  290. /* Add junk to buf2 so we can test replacing.*/
  291. write_to_buf("BLARG", 5, buf2);
  292. tt_int_op(0, OP_EQ, buf_set_to_copy(&buf2, buf));
  293. tt_int_op(len, OP_EQ, buf_datalen(buf2));
  294. fetch_from_buf(b, len, buf2);
  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. fetch_from_buf(b, len, buf2);
  302. tt_mem_op(b, OP_EQ, s, len);
  303. /* Clear buf for next test */
  304. fetch_from_buf(b, len, buf);
  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. write_to_buf(b, 1, buf);
  314. write_to_buf(s, len, buf);
  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. fetch_from_buf(b, len+1, buf2);
  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_ext_or_cmd(void *arg)
  331. {
  332. ext_or_cmd_t *cmd = NULL;
  333. buf_t *buf = buf_new();
  334. char *tmp = NULL;
  335. (void) arg;
  336. /* Empty -- should give "not there. */
  337. tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  338. tt_ptr_op(NULL, OP_EQ, cmd);
  339. /* Three bytes: shouldn't work. */
  340. write_to_buf("\x00\x20\x00", 3, buf);
  341. tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  342. tt_ptr_op(NULL, OP_EQ, cmd);
  343. tt_int_op(3, OP_EQ, buf_datalen(buf));
  344. /* 0020 0000: That's a nil command. It should work. */
  345. write_to_buf("\x00", 1, buf);
  346. tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  347. tt_ptr_op(NULL, OP_NE, cmd);
  348. tt_int_op(0x20, OP_EQ, cmd->cmd);
  349. tt_int_op(0, OP_EQ, cmd->len);
  350. tt_int_op(0, OP_EQ, buf_datalen(buf));
  351. ext_or_cmd_free(cmd);
  352. cmd = NULL;
  353. /* Now try a length-6 command with one byte missing. */
  354. write_to_buf("\x10\x21\x00\x06""abcde", 9, buf);
  355. tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  356. tt_ptr_op(NULL, OP_EQ, cmd);
  357. write_to_buf("f", 1, buf);
  358. tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  359. tt_ptr_op(NULL, OP_NE, cmd);
  360. tt_int_op(0x1021, OP_EQ, cmd->cmd);
  361. tt_int_op(6, OP_EQ, cmd->len);
  362. tt_mem_op("abcdef", OP_EQ, cmd->body, 6);
  363. tt_int_op(0, OP_EQ, buf_datalen(buf));
  364. ext_or_cmd_free(cmd);
  365. cmd = NULL;
  366. /* Now try a length-10 command with 4 extra bytes. */
  367. write_to_buf("\xff\xff\x00\x0aloremipsum\x10\x00\xff\xff", 18, buf);
  368. tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  369. tt_ptr_op(NULL, OP_NE, cmd);
  370. tt_int_op(0xffff, OP_EQ, cmd->cmd);
  371. tt_int_op(10, OP_EQ, cmd->len);
  372. tt_mem_op("loremipsum", OP_EQ, cmd->body, 10);
  373. tt_int_op(4, OP_EQ, buf_datalen(buf));
  374. ext_or_cmd_free(cmd);
  375. cmd = NULL;
  376. /* Finally, let's try a maximum-length command. We already have the header
  377. * waiting. */
  378. tt_int_op(0, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  379. tmp = tor_malloc_zero(65535);
  380. write_to_buf(tmp, 65535, buf);
  381. tt_int_op(1, OP_EQ, fetch_ext_or_command_from_buf(buf, &cmd));
  382. tt_ptr_op(NULL, OP_NE, cmd);
  383. tt_int_op(0x1000, OP_EQ, cmd->cmd);
  384. tt_int_op(0xffff, OP_EQ, cmd->len);
  385. tt_mem_op(tmp, OP_EQ, cmd->body, 65535);
  386. tt_int_op(0, OP_EQ, buf_datalen(buf));
  387. ext_or_cmd_free(cmd);
  388. cmd = NULL;
  389. done:
  390. ext_or_cmd_free(cmd);
  391. buf_free(buf);
  392. tor_free(tmp);
  393. }
  394. static void
  395. test_buffer_allocation_tracking(void *arg)
  396. {
  397. char *junk = tor_malloc(16384);
  398. buf_t *buf1 = NULL, *buf2 = NULL;
  399. int i;
  400. (void)arg;
  401. crypto_rand(junk, 16384);
  402. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  403. buf1 = buf_new();
  404. tt_assert(buf1);
  405. buf2 = buf_new();
  406. tt_assert(buf2);
  407. tt_int_op(buf_allocation(buf1), OP_EQ, 0);
  408. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  409. write_to_buf(junk, 4000, buf1);
  410. write_to_buf(junk, 4000, buf1);
  411. write_to_buf(junk, 4000, buf1);
  412. write_to_buf(junk, 4000, buf1);
  413. tt_int_op(buf_allocation(buf1), OP_EQ, 16384);
  414. fetch_from_buf(junk, 100, buf1);
  415. tt_int_op(buf_allocation(buf1), OP_EQ, 16384); /* still 4 4k chunks */
  416. tt_int_op(buf_get_total_allocation(), OP_EQ, 16384);
  417. fetch_from_buf(junk, 4096, buf1); /* drop a 1k chunk... */
  418. tt_int_op(buf_allocation(buf1), OP_EQ, 3*4096); /* now 3 4k chunks */
  419. tt_int_op(buf_get_total_allocation(), OP_EQ, 12288); /* that chunk was really
  420. freed. */
  421. write_to_buf(junk, 4000, buf2);
  422. tt_int_op(buf_allocation(buf2), OP_EQ, 4096); /* another 4k chunk. */
  423. /*
  424. * We bounce back up to 16384 by allocating a new chunk.
  425. */
  426. tt_int_op(buf_get_total_allocation(), OP_EQ, 16384);
  427. write_to_buf(junk, 4000, buf2);
  428. tt_int_op(buf_allocation(buf2), OP_EQ, 8192); /* another 4k chunk. */
  429. tt_int_op(buf_get_total_allocation(),
  430. OP_EQ, 5*4096); /* that chunk was new. */
  431. /* Make a really huge buffer */
  432. for (i = 0; i < 1000; ++i) {
  433. write_to_buf(junk, 4000, buf2);
  434. }
  435. tt_int_op(buf_allocation(buf2), OP_GE, 4008000);
  436. tt_int_op(buf_get_total_allocation(), OP_GE, 4008000);
  437. buf_free(buf2);
  438. buf2 = NULL;
  439. tt_int_op(buf_get_total_allocation(), OP_LT, 4008000);
  440. tt_int_op(buf_get_total_allocation(), OP_EQ, buf_allocation(buf1));
  441. buf_free(buf1);
  442. buf1 = NULL;
  443. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  444. done:
  445. buf_free(buf1);
  446. buf_free(buf2);
  447. tor_free(junk);
  448. }
  449. static void
  450. test_buffer_time_tracking(void *arg)
  451. {
  452. buf_t *buf=NULL, *buf2=NULL;
  453. const time_t START = 1389288246;
  454. const uint64_t START_NSEC = ((uint64_t)START) * 1000000000;
  455. int i;
  456. char tmp[4096];
  457. (void)arg;
  458. crypto_rand(tmp, sizeof(tmp));
  459. monotime_enable_test_mocking();
  460. buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
  461. tt_assert(buf);
  462. monotime_coarse_set_mock_time_nsec(START_NSEC);
  463. const uint32_t START_MSEC = (uint32_t)monotime_coarse_absolute_msec();
  464. /* Empty buffer means the timestamp is 0. */
  465. tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC));
  466. tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+1000));
  467. write_to_buf("ABCDEFG", 7, buf);
  468. tt_int_op(1000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+1000));
  469. buf2 = buf_copy(buf);
  470. tt_assert(buf2);
  471. tt_int_op(1234, OP_EQ,
  472. buf_get_oldest_chunk_timestamp(buf2, START_MSEC+1234));
  473. /* Now add more bytes; enough to overflow the first chunk. */
  474. monotime_coarse_set_mock_time_nsec(START_NSEC + 123 * (uint64_t)1000000);
  475. for (i = 0; i < 600; ++i)
  476. write_to_buf("ABCDEFG", 7, buf);
  477. tt_int_op(4207, OP_EQ, buf_datalen(buf));
  478. /* The oldest bytes are still in the front. */
  479. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2000));
  480. /* Once those bytes are dropped, the chunk is still on the first
  481. * timestamp. */
  482. fetch_from_buf(tmp, 100, buf);
  483. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2000));
  484. /* But once we discard the whole first chunk, we get the data in the second
  485. * chunk. */
  486. fetch_from_buf(tmp, 4000, buf);
  487. tt_int_op(107, OP_EQ, buf_datalen(buf));
  488. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2123));
  489. /* This time we'll be grabbing a chunk from the freelist, and making sure
  490. its time gets updated */
  491. monotime_coarse_set_mock_time_nsec(START_NSEC + 5617 * (uint64_t)1000000);
  492. for (i = 0; i < 600; ++i)
  493. write_to_buf("ABCDEFG", 7, buf);
  494. tt_int_op(4307, OP_EQ, buf_datalen(buf));
  495. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2123));
  496. fetch_from_buf(tmp, 4000, buf);
  497. fetch_from_buf(tmp, 306, buf);
  498. tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+5617));
  499. tt_int_op(383, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+6000));
  500. done:
  501. buf_free(buf);
  502. buf_free(buf2);
  503. monotime_disable_test_mocking();
  504. }
  505. static void
  506. test_buffers_compress_fin_at_chunk_end_impl(compress_method_t method,
  507. compression_level_t level)
  508. {
  509. char *msg = NULL;
  510. char *contents = NULL;
  511. char *expanded = NULL;
  512. buf_t *buf = NULL;
  513. tor_compress_state_t *compress_state = NULL;
  514. size_t out_len, in_len;
  515. size_t sz, headerjunk;
  516. buf = buf_new_with_capacity(128); /* will round up */
  517. sz = buf_get_default_chunk_size(buf);
  518. msg = tor_malloc_zero(sz);
  519. write_to_buf(msg, 1, buf);
  520. tt_assert(buf->head);
  521. /* Fill up the chunk so the compression stuff won't fit in one chunk. */
  522. tt_uint_op(buf->head->memlen, OP_LT, sz);
  523. headerjunk = buf->head->memlen - 7;
  524. write_to_buf(msg, headerjunk-1, buf);
  525. tt_uint_op(buf->head->datalen, OP_EQ, headerjunk);
  526. tt_uint_op(buf_datalen(buf), OP_EQ, headerjunk);
  527. /* Write an empty string, with finalization on. */
  528. compress_state = tor_compress_new(1, method, level);
  529. tt_int_op(write_to_buf_compress(buf, compress_state, "", 0, 1), OP_EQ, 0);
  530. in_len = buf_datalen(buf);
  531. contents = tor_malloc(in_len);
  532. tt_int_op(fetch_from_buf(contents, in_len, buf), OP_EQ, 0);
  533. if (method == NO_METHOD) {
  534. tt_uint_op(in_len, OP_EQ, headerjunk);
  535. } else {
  536. tt_uint_op(in_len, OP_GT, headerjunk);
  537. }
  538. tt_int_op(0, OP_EQ, tor_uncompress(&expanded, &out_len,
  539. contents + headerjunk,
  540. in_len - headerjunk,
  541. method, 1,
  542. LOG_WARN));
  543. tt_int_op(out_len, OP_EQ, 0);
  544. tt_assert(expanded);
  545. done:
  546. buf_free(buf);
  547. tor_compress_free(compress_state);
  548. tor_free(contents);
  549. tor_free(expanded);
  550. tor_free(msg);
  551. }
  552. static void
  553. test_buffers_compress_impl(compress_method_t method,
  554. compression_level_t level,
  555. int finalize_with_nil)
  556. {
  557. char *msg = NULL;
  558. char *contents = NULL;
  559. char *expanded = NULL;
  560. buf_t *buf = NULL;
  561. tor_compress_state_t *compress_state = NULL;
  562. size_t out_len, in_len;
  563. int done;
  564. buf = buf_new_with_capacity(128); /* will round up */
  565. compress_state = tor_compress_new(1, method, level);
  566. msg = tor_malloc(512);
  567. crypto_rand(msg, 512);
  568. tt_int_op(write_to_buf_compress(buf, compress_state,
  569. msg, 128, 0), OP_EQ, 0);
  570. tt_int_op(write_to_buf_compress(buf, compress_state,
  571. msg+128, 128, 0), OP_EQ, 0);
  572. tt_int_op(write_to_buf_compress(buf, compress_state,
  573. msg+256, 256, 0), OP_EQ, 0);
  574. done = !finalize_with_nil;
  575. tt_int_op(write_to_buf_compress(buf, compress_state,
  576. "all done", 9, done), OP_EQ, 0);
  577. if (finalize_with_nil) {
  578. tt_int_op(write_to_buf_compress(buf, compress_state, "", 0, 1), OP_EQ, 0);
  579. }
  580. in_len = buf_datalen(buf);
  581. contents = tor_malloc(in_len);
  582. tt_int_op(fetch_from_buf(contents, in_len, buf), OP_EQ, 0);
  583. tt_int_op(0, OP_EQ, tor_uncompress(&expanded, &out_len,
  584. contents, in_len,
  585. method, 1,
  586. LOG_WARN));
  587. tt_int_op(out_len, OP_GE, 128);
  588. tt_mem_op(msg, OP_EQ, expanded, 128);
  589. tt_int_op(out_len, OP_GE, 512);
  590. tt_mem_op(msg, OP_EQ, expanded, 512);
  591. tt_int_op(out_len, OP_EQ, 512+9);
  592. tt_mem_op("all done", OP_EQ, expanded+512, 9);
  593. done:
  594. buf_free(buf);
  595. tor_compress_free(compress_state);
  596. tor_free(contents);
  597. tor_free(expanded);
  598. tor_free(msg);
  599. }
  600. static void
  601. test_buffers_compress(void *arg)
  602. {
  603. const char *methodname = arg;
  604. tt_assert(methodname);
  605. compress_method_t method = compression_method_get_by_name(methodname);
  606. tt_int_op(method, OP_NE, UNKNOWN_METHOD);
  607. if (! tor_compress_supports_method(method)) {
  608. tt_skip();
  609. }
  610. compression_level_t levels[] = {
  611. BEST_COMPRESSION,
  612. HIGH_COMPRESSION,
  613. MEDIUM_COMPRESSION,
  614. LOW_COMPRESSION
  615. };
  616. for (unsigned l = 0; l < ARRAY_LENGTH(levels); ++l) {
  617. compression_level_t level = levels[l];
  618. test_buffers_compress_impl(method, level, 0);
  619. test_buffers_compress_impl(method, level, 1);
  620. test_buffers_compress_fin_at_chunk_end_impl(method, level);
  621. }
  622. done:
  623. ;
  624. }
  625. static const uint8_t *tls_read_ptr;
  626. static int n_remaining;
  627. static int next_reply_val[16];
  628. static int
  629. mock_tls_read(tor_tls_t *tls, char *cp, size_t len)
  630. {
  631. (void)tls;
  632. int rv = next_reply_val[0];
  633. if (rv > 0) {
  634. int max = rv > (int)len ? (int)len : rv;
  635. if (max > n_remaining)
  636. max = n_remaining;
  637. memcpy(cp, tls_read_ptr, max);
  638. rv = max;
  639. n_remaining -= max;
  640. tls_read_ptr += max;
  641. }
  642. memmove(next_reply_val, next_reply_val + 1, 15*sizeof(int));
  643. return rv;
  644. }
  645. static void
  646. test_buffers_tls_read_mocked(void *arg)
  647. {
  648. uint8_t *mem;
  649. buf_t *buf;
  650. (void)arg;
  651. mem = tor_malloc(64*1024);
  652. crypto_rand((char*)mem, 64*1024);
  653. tls_read_ptr = mem;
  654. n_remaining = 64*1024;
  655. MOCK(tor_tls_read, mock_tls_read);
  656. buf = buf_new();
  657. next_reply_val[0] = 1024;
  658. tt_int_op(128, OP_EQ, read_to_buf_tls(NULL, 128, buf));
  659. next_reply_val[0] = 5000;
  660. next_reply_val[1] = 5000;
  661. tt_int_op(6000, OP_EQ, read_to_buf_tls(NULL, 6000, buf));
  662. done:
  663. UNMOCK(tor_tls_read);
  664. tor_free(mem);
  665. buf_free(buf);
  666. }
  667. static void
  668. test_buffers_chunk_size(void *arg)
  669. {
  670. (void)arg;
  671. const int min = 256;
  672. const int max = 65536;
  673. tt_uint_op(preferred_chunk_size(3), OP_EQ, min);
  674. tt_uint_op(preferred_chunk_size(25), OP_EQ, min);
  675. tt_uint_op(preferred_chunk_size(0), OP_EQ, min);
  676. tt_uint_op(preferred_chunk_size(256), OP_EQ, 512);
  677. tt_uint_op(preferred_chunk_size(65400), OP_EQ, max);
  678. /* Here, we're implicitly saying that the chunk header overhead is
  679. * between 1 and 100 bytes. 24..48 would probably be more accurate. */
  680. tt_uint_op(preferred_chunk_size(65536), OP_GT, 65536);
  681. tt_uint_op(preferred_chunk_size(65536), OP_LT, 65536+100);
  682. tt_uint_op(preferred_chunk_size(165536), OP_GT, 165536);
  683. tt_uint_op(preferred_chunk_size(165536), OP_LT, 165536+100);
  684. done:
  685. ;
  686. }
  687. static void
  688. test_buffers_find_contentlen(void *arg)
  689. {
  690. static const struct {
  691. const char *headers;
  692. int r;
  693. int contentlen;
  694. } results[] = {
  695. { "Blah blah\r\nContent-Length: 1\r\n\r\n", 1, 1 },
  696. { "Blah blah\r\n\r\n", 0, 0 }, /* no content-len */
  697. { "Blah blah Content-Length: 1\r\n", 0, 0 }, /* no content-len. */
  698. { "Blah blah\r\nContent-Length: 100000\r\n", 1, 100000},
  699. { "Blah blah\r\nContent-Length: 1000000000000000000000000\r\n", -1, 0},
  700. { "Blah blah\r\nContent-Length: 0\r\n", 1, 0},
  701. { "Blah blah\r\nContent-Length: -1\r\n", -1, 0},
  702. { "Blah blah\r\nContent-Length: 1x\r\n", -1, 0},
  703. { "Blah blah\r\nContent-Length: 1 x\r\n", -1, 0},
  704. { "Blah blah\r\nContent-Length: 1 \r\n", 1, 1},
  705. { "Blah blah\r\nContent-Length: \r\n", -1, 0},
  706. { "Blah blah\r\nContent-Length: ", -1, 0},
  707. { "Blah blah\r\nContent-Length: 5050", -1, 0},
  708. { NULL, 0, 0 }
  709. };
  710. int i;
  711. (void)arg;
  712. for (i = 0; results[i].headers; ++i) {
  713. int r;
  714. size_t sz;
  715. size_t headerlen = strlen(results[i].headers);
  716. char * tmp = tor_memdup(results[i].headers, headerlen);/* ensure no eos */
  717. sz = 999; /* to ensure it gets set */
  718. r = buf_http_find_content_length(tmp, headerlen, &sz);
  719. tor_free(tmp);
  720. log_debug(LD_DIR, "%d: %s", i, escaped(results[i].headers));
  721. tt_int_op(r, OP_EQ, results[i].r);
  722. tt_int_op(sz, OP_EQ, results[i].contentlen);
  723. }
  724. done:
  725. ;
  726. }
  727. static void
  728. test_buffer_peek_startswith(void *arg)
  729. {
  730. (void)arg;
  731. buf_t *buf;
  732. buf = buf_new();
  733. tt_ptr_op(buf, OP_NE, NULL);
  734. tt_assert(peek_buf_startswith(buf, ""));
  735. tt_assert(! peek_buf_startswith(buf, "X"));
  736. write_to_buf("Tor", 3, buf);
  737. tt_assert(peek_buf_startswith(buf, ""));
  738. tt_assert(peek_buf_startswith(buf, "T"));
  739. tt_assert(peek_buf_startswith(buf, "To"));
  740. tt_assert(peek_buf_startswith(buf, "Tor"));
  741. tt_assert(! peek_buf_startswith(buf, "Top"));
  742. tt_assert(! peek_buf_startswith(buf, "For"));
  743. tt_assert(! peek_buf_startswith(buf, "Tork"));
  744. tt_assert(! peek_buf_startswith(buf, "Torpor"));
  745. done:
  746. buf_free(buf);
  747. }
  748. struct testcase_t buffer_tests[] = {
  749. { "basic", test_buffers_basic, TT_FORK, NULL, NULL },
  750. { "copy", test_buffer_copy, TT_FORK, NULL, NULL },
  751. { "pullup", test_buffer_pullup, TT_FORK, NULL, NULL },
  752. { "startswith", test_buffer_peek_startswith, 0, NULL, NULL },
  753. { "ext_or_cmd", test_buffer_ext_or_cmd, TT_FORK, NULL, NULL },
  754. { "allocation_tracking", test_buffer_allocation_tracking, TT_FORK,
  755. NULL, NULL },
  756. { "time_tracking", test_buffer_time_tracking, TT_FORK, NULL, NULL },
  757. { "tls_read_mocked", test_buffers_tls_read_mocked, 0,
  758. NULL, NULL },
  759. { "chunk_size", test_buffers_chunk_size, 0, NULL, NULL },
  760. { "find_contentlen", test_buffers_find_contentlen, 0, NULL, NULL },
  761. { "compress/zlib", test_buffers_compress, TT_FORK,
  762. &passthrough_setup, (char*)"deflate" },
  763. { "compress/gzip", test_buffers_compress, TT_FORK,
  764. &passthrough_setup, (char*)"gzip" },
  765. { "compress/zstd", test_buffers_compress, TT_FORK,
  766. &passthrough_setup, (char*)"x-zstd" },
  767. { "compress/lzma", test_buffers_compress, TT_FORK,
  768. &passthrough_setup, (char*)"x-tor-lzma" },
  769. { "compress/none", test_buffers_compress, TT_FORK,
  770. &passthrough_setup, (char*)"identity" },
  771. END_OF_TESTCASES
  772. };