test_buffers.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, 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 *cp = tor_malloc_zero(65536);
  167. buf = buf_new();
  168. write_to_buf(cp, 65536, buf);
  169. tor_free(cp);
  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. /* Note: this test doesn't check the nulterminate argument to buf_pullup,
  191. since nothing actually uses it. We should remove it some time. */
  192. buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
  193. tt_assert(buf);
  194. tt_int_op(buf_get_default_chunk_size(buf), OP_EQ, 4096);
  195. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  196. /* There are a bunch of cases for pullup. One is the trivial case. Let's
  197. mess around with an empty buffer. */
  198. buf_pullup(buf, 16, 1);
  199. buf_get_first_chunk_data(buf, &cp, &sz);
  200. tt_ptr_op(cp, OP_EQ, NULL);
  201. tt_uint_op(sz, OP_EQ, 0);
  202. /* Let's make sure nothing got allocated */
  203. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  204. /* Case 1: everything puts into the first chunk with some moving. */
  205. /* Let's add some data. */
  206. crypto_rand(stuff, 16384);
  207. write_to_buf(stuff, 3000, buf);
  208. write_to_buf(stuff+3000, 3000, buf);
  209. buf_get_first_chunk_data(buf, &cp, &sz);
  210. tt_ptr_op(cp, OP_NE, NULL);
  211. tt_int_op(sz, OP_LE, 4096);
  212. /* Make room for 3000 bytes in the first chunk, so that the pullup-move code
  213. * can get tested. */
  214. tt_int_op(fetch_from_buf(tmp, 3000, buf), OP_EQ, 3000);
  215. tt_mem_op(tmp,OP_EQ, stuff, 3000);
  216. buf_pullup(buf, 2048, 0);
  217. assert_buf_ok(buf);
  218. buf_get_first_chunk_data(buf, &cp, &sz);
  219. tt_ptr_op(cp, OP_NE, NULL);
  220. tt_int_op(sz, OP_GE, 2048);
  221. tt_mem_op(cp,OP_EQ, stuff+3000, 2048);
  222. tt_int_op(3000, OP_EQ, buf_datalen(buf));
  223. tt_int_op(fetch_from_buf(tmp, 3000, buf), OP_EQ, 0);
  224. tt_mem_op(tmp,OP_EQ, stuff+3000, 2048);
  225. buf_free(buf);
  226. /* Now try the large-chunk case. */
  227. buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
  228. write_to_buf(stuff, 4000, buf);
  229. write_to_buf(stuff+4000, 4000, buf);
  230. write_to_buf(stuff+8000, 4000, buf);
  231. write_to_buf(stuff+12000, 4000, buf);
  232. tt_int_op(buf_datalen(buf), OP_EQ, 16000);
  233. buf_get_first_chunk_data(buf, &cp, &sz);
  234. tt_ptr_op(cp, OP_NE, NULL);
  235. tt_int_op(sz, OP_LE, 4096);
  236. buf_pullup(buf, 12500, 0);
  237. assert_buf_ok(buf);
  238. buf_get_first_chunk_data(buf, &cp, &sz);
  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, 0); /* Way too much. */
  257. assert_buf_ok(buf);
  258. buf_get_first_chunk_data(buf, &cp, &sz);
  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. generic_buffer_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 = generic_buffer_new();
  280. tt_assert(buf);
  281. /* Copy an empty buffer. */
  282. tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
  283. tt_assert(buf2);
  284. tt_int_op(0, OP_EQ, generic_buffer_len(buf2));
  285. /* Now try with a short buffer. */
  286. s = "And now comes an act of enormous enormance!";
  287. len = strlen(s);
  288. generic_buffer_add(buf, s, len);
  289. tt_int_op(len, OP_EQ, generic_buffer_len(buf));
  290. /* Add junk to buf2 so we can test replacing.*/
  291. generic_buffer_add(buf2, "BLARG", 5);
  292. tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
  293. tt_int_op(len, OP_EQ, generic_buffer_len(buf2));
  294. generic_buffer_get(buf2, b, len);
  295. tt_mem_op(b, OP_EQ, s, len);
  296. /* Now free buf2 and retry so we can test allocating */
  297. generic_buffer_free(buf2);
  298. buf2 = NULL;
  299. tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
  300. tt_int_op(len, OP_EQ, generic_buffer_len(buf2));
  301. generic_buffer_get(buf2, b, len);
  302. tt_mem_op(b, OP_EQ, s, len);
  303. /* Clear buf for next test */
  304. generic_buffer_get(buf, b, len);
  305. tt_int_op(generic_buffer_len(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. generic_buffer_add(buf, b, 1);
  314. generic_buffer_add(buf, s, len);
  315. }
  316. tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
  317. tt_int_op(generic_buffer_len(buf2), OP_EQ, generic_buffer_len(buf));
  318. for (i = 0; i < 256; ++i) {
  319. generic_buffer_get(buf2, b, len+1);
  320. tt_int_op((unsigned char)b[0],OP_EQ,i);
  321. tt_mem_op(b+1, OP_EQ, s, len);
  322. }
  323. done:
  324. if (buf)
  325. generic_buffer_free(buf);
  326. if (buf2)
  327. generic_buffer_free(buf2);
  328. }
  329. static void
  330. test_buffer_ext_or_cmd(void *arg)
  331. {
  332. ext_or_cmd_t *cmd = NULL;
  333. generic_buffer_t *buf = generic_buffer_new();
  334. char *tmp = NULL;
  335. (void) arg;
  336. /* Empty -- should give "not there. */
  337. tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
  338. tt_ptr_op(NULL, OP_EQ, cmd);
  339. /* Three bytes: shouldn't work. */
  340. generic_buffer_add(buf, "\x00\x20\x00", 3);
  341. tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
  342. tt_ptr_op(NULL, OP_EQ, cmd);
  343. tt_int_op(3, OP_EQ, generic_buffer_len(buf));
  344. /* 0020 0000: That's a nil command. It should work. */
  345. generic_buffer_add(buf, "\x00", 1);
  346. tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(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, generic_buffer_len(buf));
  351. ext_or_cmd_free(cmd);
  352. cmd = NULL;
  353. /* Now try a length-6 command with one byte missing. */
  354. generic_buffer_add(buf, "\x10\x21\x00\x06""abcde", 9);
  355. tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
  356. tt_ptr_op(NULL, OP_EQ, cmd);
  357. generic_buffer_add(buf, "f", 1);
  358. tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(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, generic_buffer_len(buf));
  364. ext_or_cmd_free(cmd);
  365. cmd = NULL;
  366. /* Now try a length-10 command with 4 extra bytes. */
  367. generic_buffer_add(buf, "\xff\xff\x00\x0a"
  368. "loremipsum\x10\x00\xff\xff", 18);
  369. tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
  370. tt_ptr_op(NULL, OP_NE, cmd);
  371. tt_int_op(0xffff, OP_EQ, cmd->cmd);
  372. tt_int_op(10, OP_EQ, cmd->len);
  373. tt_mem_op("loremipsum", OP_EQ, cmd->body, 10);
  374. tt_int_op(4, OP_EQ, generic_buffer_len(buf));
  375. ext_or_cmd_free(cmd);
  376. cmd = NULL;
  377. /* Finally, let's try a maximum-length command. We already have the header
  378. * waiting. */
  379. tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
  380. tmp = tor_malloc_zero(65535);
  381. generic_buffer_add(buf, tmp, 65535);
  382. tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
  383. tt_ptr_op(NULL, OP_NE, cmd);
  384. tt_int_op(0x1000, OP_EQ, cmd->cmd);
  385. tt_int_op(0xffff, OP_EQ, cmd->len);
  386. tt_mem_op(tmp, OP_EQ, cmd->body, 65535);
  387. tt_int_op(0, OP_EQ, generic_buffer_len(buf));
  388. ext_or_cmd_free(cmd);
  389. cmd = NULL;
  390. done:
  391. ext_or_cmd_free(cmd);
  392. generic_buffer_free(buf);
  393. tor_free(tmp);
  394. }
  395. static void
  396. test_buffer_allocation_tracking(void *arg)
  397. {
  398. char *junk = tor_malloc(16384);
  399. buf_t *buf1 = NULL, *buf2 = NULL;
  400. int i;
  401. (void)arg;
  402. crypto_rand(junk, 16384);
  403. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  404. buf1 = buf_new();
  405. tt_assert(buf1);
  406. buf2 = buf_new();
  407. tt_assert(buf2);
  408. tt_int_op(buf_allocation(buf1), OP_EQ, 0);
  409. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  410. write_to_buf(junk, 4000, buf1);
  411. write_to_buf(junk, 4000, buf1);
  412. write_to_buf(junk, 4000, buf1);
  413. write_to_buf(junk, 4000, buf1);
  414. tt_int_op(buf_allocation(buf1), OP_EQ, 16384);
  415. fetch_from_buf(junk, 100, buf1);
  416. tt_int_op(buf_allocation(buf1), OP_EQ, 16384); /* still 4 4k chunks */
  417. tt_int_op(buf_get_total_allocation(), OP_EQ, 16384);
  418. fetch_from_buf(junk, 4096, buf1); /* drop a 1k chunk... */
  419. tt_int_op(buf_allocation(buf1), OP_EQ, 3*4096); /* now 3 4k chunks */
  420. tt_int_op(buf_get_total_allocation(), OP_EQ, 12288); /* that chunk was really
  421. freed. */
  422. write_to_buf(junk, 4000, buf2);
  423. tt_int_op(buf_allocation(buf2), OP_EQ, 4096); /* another 4k chunk. */
  424. /*
  425. * We bounce back up to 16384 by allocating a new chunk.
  426. */
  427. tt_int_op(buf_get_total_allocation(), OP_EQ, 16384);
  428. write_to_buf(junk, 4000, buf2);
  429. tt_int_op(buf_allocation(buf2), OP_EQ, 8192); /* another 4k chunk. */
  430. tt_int_op(buf_get_total_allocation(),
  431. OP_EQ, 5*4096); /* that chunk was new. */
  432. /* Make a really huge buffer */
  433. for (i = 0; i < 1000; ++i) {
  434. write_to_buf(junk, 4000, buf2);
  435. }
  436. tt_int_op(buf_allocation(buf2), OP_GE, 4008000);
  437. tt_int_op(buf_get_total_allocation(), OP_GE, 4008000);
  438. buf_free(buf2);
  439. buf2 = NULL;
  440. tt_int_op(buf_get_total_allocation(), OP_LT, 4008000);
  441. tt_int_op(buf_get_total_allocation(), OP_EQ, buf_allocation(buf1));
  442. buf_free(buf1);
  443. buf1 = NULL;
  444. tt_int_op(buf_get_total_allocation(), OP_EQ, 0);
  445. done:
  446. buf_free(buf1);
  447. buf_free(buf2);
  448. tor_free(junk);
  449. }
  450. static void
  451. test_buffer_time_tracking(void *arg)
  452. {
  453. buf_t *buf=NULL, *buf2=NULL;
  454. struct timeval tv0;
  455. const time_t START = 1389288246;
  456. const uint32_t START_MSEC = (uint32_t) ((uint64_t)START * 1000);
  457. int i;
  458. char tmp[4096];
  459. (void)arg;
  460. crypto_rand(tmp, sizeof(tmp));
  461. tv0.tv_sec = START;
  462. tv0.tv_usec = 0;
  463. buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
  464. tt_assert(buf);
  465. /* Empty buffer means the timestamp is 0. */
  466. tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC));
  467. tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+1000));
  468. tor_gettimeofday_cache_set(&tv0);
  469. write_to_buf("ABCDEFG", 7, buf);
  470. tt_int_op(1000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+1000));
  471. buf2 = buf_copy(buf);
  472. tt_assert(buf2);
  473. tt_int_op(1234, OP_EQ,
  474. buf_get_oldest_chunk_timestamp(buf2, START_MSEC+1234));
  475. /* Now add more bytes; enough to overflow the first chunk. */
  476. tv0.tv_usec += 123 * 1000;
  477. tor_gettimeofday_cache_set(&tv0);
  478. for (i = 0; i < 600; ++i)
  479. write_to_buf("ABCDEFG", 7, buf);
  480. tt_int_op(4207, OP_EQ, buf_datalen(buf));
  481. /* The oldest bytes are still in the front. */
  482. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2000));
  483. /* Once those bytes are dropped, the chunk is still on the first
  484. * timestamp. */
  485. fetch_from_buf(tmp, 100, buf);
  486. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2000));
  487. /* But once we discard the whole first chunk, we get the data in the second
  488. * chunk. */
  489. fetch_from_buf(tmp, 4000, buf);
  490. tt_int_op(107, OP_EQ, buf_datalen(buf));
  491. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2123));
  492. /* This time we'll be grabbing a chunk from the freelist, and making sure
  493. its time gets updated */
  494. tv0.tv_sec += 5;
  495. tv0.tv_usec = 617*1000;
  496. tor_gettimeofday_cache_set(&tv0);
  497. for (i = 0; i < 600; ++i)
  498. write_to_buf("ABCDEFG", 7, buf);
  499. tt_int_op(4307, OP_EQ, buf_datalen(buf));
  500. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2123));
  501. fetch_from_buf(tmp, 4000, buf);
  502. fetch_from_buf(tmp, 306, buf);
  503. tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+5617));
  504. tt_int_op(383, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+6000));
  505. done:
  506. buf_free(buf);
  507. buf_free(buf2);
  508. }
  509. static void
  510. test_buffers_zlib_impl(int finalize_with_nil)
  511. {
  512. char *msg = NULL;
  513. char *contents = NULL;
  514. char *expanded = NULL;
  515. buf_t *buf = NULL;
  516. tor_zlib_state_t *zlib_state = NULL;
  517. size_t out_len, in_len;
  518. int done;
  519. buf = buf_new_with_capacity(128); /* will round up */
  520. zlib_state = tor_zlib_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
  521. msg = tor_malloc(512);
  522. crypto_rand(msg, 512);
  523. tt_int_op(write_to_buf_zlib(buf, zlib_state, msg, 128, 0), OP_EQ, 0);
  524. tt_int_op(write_to_buf_zlib(buf, zlib_state, msg+128, 128, 0), OP_EQ, 0);
  525. tt_int_op(write_to_buf_zlib(buf, zlib_state, msg+256, 256, 0), OP_EQ, 0);
  526. done = !finalize_with_nil;
  527. tt_int_op(write_to_buf_zlib(buf, zlib_state, "all done", 9, done), OP_EQ, 0);
  528. if (finalize_with_nil) {
  529. tt_int_op(write_to_buf_zlib(buf, zlib_state, "", 0, 1), OP_EQ, 0);
  530. }
  531. in_len = buf_datalen(buf);
  532. contents = tor_malloc(in_len);
  533. tt_int_op(fetch_from_buf(contents, in_len, buf), OP_EQ, 0);
  534. tt_int_op(0, OP_EQ, tor_gzip_uncompress(&expanded, &out_len,
  535. contents, in_len,
  536. ZLIB_METHOD, 1,
  537. LOG_WARN));
  538. tt_int_op(out_len, OP_GE, 128);
  539. tt_mem_op(msg, OP_EQ, expanded, 128);
  540. tt_int_op(out_len, OP_GE, 512);
  541. tt_mem_op(msg, OP_EQ, expanded, 512);
  542. tt_int_op(out_len, OP_EQ, 512+9);
  543. tt_mem_op("all done", OP_EQ, expanded+512, 9);
  544. done:
  545. buf_free(buf);
  546. tor_zlib_free(zlib_state);
  547. tor_free(contents);
  548. tor_free(expanded);
  549. tor_free(msg);
  550. }
  551. static void
  552. test_buffers_zlib(void *arg)
  553. {
  554. (void) arg;
  555. test_buffers_zlib_impl(0);
  556. }
  557. static void
  558. test_buffers_zlib_fin_with_nil(void *arg)
  559. {
  560. (void) arg;
  561. test_buffers_zlib_impl(1);
  562. }
  563. static void
  564. test_buffers_zlib_fin_at_chunk_end(void *arg)
  565. {
  566. char *msg = NULL;
  567. char *contents = NULL;
  568. char *expanded = NULL;
  569. buf_t *buf = NULL;
  570. tor_zlib_state_t *zlib_state = NULL;
  571. size_t out_len, in_len;
  572. size_t sz, headerjunk;
  573. (void) arg;
  574. buf = buf_new_with_capacity(128); /* will round up */
  575. sz = buf_get_default_chunk_size(buf);
  576. msg = tor_malloc_zero(sz);
  577. write_to_buf(msg, 1, buf);
  578. tt_assert(buf->head);
  579. /* Fill up the chunk so the zlib stuff won't fit in one chunk. */
  580. tt_uint_op(buf->head->memlen, OP_LT, sz);
  581. headerjunk = buf->head->memlen - 7;
  582. write_to_buf(msg, headerjunk-1, buf);
  583. tt_uint_op(buf->head->datalen, OP_EQ, headerjunk);
  584. tt_uint_op(buf_datalen(buf), OP_EQ, headerjunk);
  585. /* Write an empty string, with finalization on. */
  586. zlib_state = tor_zlib_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
  587. tt_int_op(write_to_buf_zlib(buf, zlib_state, "", 0, 1), OP_EQ, 0);
  588. in_len = buf_datalen(buf);
  589. contents = tor_malloc(in_len);
  590. tt_int_op(fetch_from_buf(contents, in_len, buf), OP_EQ, 0);
  591. tt_uint_op(in_len, OP_GT, headerjunk);
  592. tt_int_op(0, OP_EQ, tor_gzip_uncompress(&expanded, &out_len,
  593. contents + headerjunk, in_len - headerjunk,
  594. ZLIB_METHOD, 1,
  595. LOG_WARN));
  596. tt_int_op(out_len, OP_EQ, 0);
  597. tt_assert(expanded);
  598. done:
  599. buf_free(buf);
  600. tor_zlib_free(zlib_state);
  601. tor_free(contents);
  602. tor_free(expanded);
  603. tor_free(msg);
  604. }
  605. const uint8_t *tls_read_ptr;
  606. int n_remaining;
  607. int next_reply_val[16];
  608. static int
  609. mock_tls_read(tor_tls_t *tls, char *cp, size_t len)
  610. {
  611. (void)tls;
  612. int rv = next_reply_val[0];
  613. if (rv > 0) {
  614. int max = rv > (int)len ? (int)len : rv;
  615. if (max > n_remaining)
  616. max = n_remaining;
  617. memcpy(cp, tls_read_ptr, max);
  618. rv = max;
  619. n_remaining -= max;
  620. tls_read_ptr += max;
  621. }
  622. memmove(next_reply_val, next_reply_val + 1, 15*sizeof(int));
  623. return rv;
  624. }
  625. static void
  626. test_buffers_tls_read_mocked(void *arg)
  627. {
  628. uint8_t *mem;
  629. buf_t *buf;
  630. (void)arg;
  631. mem = tor_malloc(64*1024);
  632. crypto_rand((char*)mem, 64*1024);
  633. tls_read_ptr = mem;
  634. n_remaining = 64*1024;
  635. MOCK(tor_tls_read, mock_tls_read);
  636. buf = buf_new();
  637. next_reply_val[0] = 1024;
  638. tt_int_op(128, ==, read_to_buf_tls(NULL, 128, buf));
  639. next_reply_val[0] = 5000;
  640. next_reply_val[1] = 5000;
  641. tt_int_op(6000, ==, read_to_buf_tls(NULL, 6000, buf));
  642. done:
  643. UNMOCK(tor_tls_read);
  644. tor_free(mem);
  645. buf_free(buf);
  646. }
  647. struct testcase_t buffer_tests[] = {
  648. { "basic", test_buffers_basic, TT_FORK, NULL, NULL },
  649. { "copy", test_buffer_copy, TT_FORK, NULL, NULL },
  650. { "pullup", test_buffer_pullup, TT_FORK, NULL, NULL },
  651. { "ext_or_cmd", test_buffer_ext_or_cmd, TT_FORK, NULL, NULL },
  652. { "allocation_tracking", test_buffer_allocation_tracking, TT_FORK,
  653. NULL, NULL },
  654. { "time_tracking", test_buffer_time_tracking, TT_FORK, NULL, NULL },
  655. { "zlib", test_buffers_zlib, TT_FORK, NULL, NULL },
  656. { "zlib_fin_with_nil", test_buffers_zlib_fin_with_nil, TT_FORK, NULL, NULL },
  657. { "zlib_fin_at_chunk_end", test_buffers_zlib_fin_at_chunk_end, TT_FORK,
  658. NULL, NULL},
  659. { "tls_read_mocked", test_buffers_tls_read_mocked, 0,
  660. NULL, NULL },
  661. END_OF_TESTCASES
  662. };