test_buffers.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, 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. 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. generic_buffer_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 = generic_buffer_new();
  278. tt_assert(buf);
  279. /* Copy an empty buffer. */
  280. tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
  281. tt_assert(buf2);
  282. tt_int_op(0, OP_EQ, generic_buffer_len(buf2));
  283. /* Now try with a short buffer. */
  284. s = "And now comes an act of enormous enormance!";
  285. len = strlen(s);
  286. generic_buffer_add(buf, s, len);
  287. tt_int_op(len, OP_EQ, generic_buffer_len(buf));
  288. /* Add junk to buf2 so we can test replacing.*/
  289. generic_buffer_add(buf2, "BLARG", 5);
  290. tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
  291. tt_int_op(len, OP_EQ, generic_buffer_len(buf2));
  292. generic_buffer_get(buf2, b, len);
  293. tt_mem_op(b, OP_EQ, s, len);
  294. /* Now free buf2 and retry so we can test allocating */
  295. generic_buffer_free(buf2);
  296. buf2 = NULL;
  297. tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
  298. tt_int_op(len, OP_EQ, generic_buffer_len(buf2));
  299. generic_buffer_get(buf2, b, len);
  300. tt_mem_op(b, OP_EQ, s, len);
  301. /* Clear buf for next test */
  302. generic_buffer_get(buf, b, len);
  303. tt_int_op(generic_buffer_len(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. generic_buffer_add(buf, b, 1);
  312. generic_buffer_add(buf, s, len);
  313. }
  314. tt_int_op(0, OP_EQ, generic_buffer_set_to_copy(&buf2, buf));
  315. tt_int_op(generic_buffer_len(buf2), OP_EQ, generic_buffer_len(buf));
  316. for (i = 0; i < 256; ++i) {
  317. generic_buffer_get(buf2, b, len+1);
  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. generic_buffer_free(buf);
  324. if (buf2)
  325. generic_buffer_free(buf2);
  326. }
  327. static void
  328. test_buffer_ext_or_cmd(void *arg)
  329. {
  330. ext_or_cmd_t *cmd = NULL;
  331. generic_buffer_t *buf = generic_buffer_new();
  332. char *tmp = NULL;
  333. (void) arg;
  334. /* Empty -- should give "not there. */
  335. tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
  336. tt_ptr_op(NULL, OP_EQ, cmd);
  337. /* Three bytes: shouldn't work. */
  338. generic_buffer_add(buf, "\x00\x20\x00", 3);
  339. tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
  340. tt_ptr_op(NULL, OP_EQ, cmd);
  341. tt_int_op(3, OP_EQ, generic_buffer_len(buf));
  342. /* 0020 0000: That's a nil command. It should work. */
  343. generic_buffer_add(buf, "\x00", 1);
  344. tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(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, generic_buffer_len(buf));
  349. ext_or_cmd_free(cmd);
  350. cmd = NULL;
  351. /* Now try a length-6 command with one byte missing. */
  352. generic_buffer_add(buf, "\x10\x21\x00\x06""abcde", 9);
  353. tt_int_op(0, OP_EQ, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
  354. tt_ptr_op(NULL, OP_EQ, cmd);
  355. generic_buffer_add(buf, "f", 1);
  356. tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(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, generic_buffer_len(buf));
  362. ext_or_cmd_free(cmd);
  363. cmd = NULL;
  364. /* Now try a length-10 command with 4 extra bytes. */
  365. generic_buffer_add(buf, "\xff\xff\x00\x0a"
  366. "loremipsum\x10\x00\xff\xff", 18);
  367. tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(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, generic_buffer_len(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, generic_buffer_fetch_ext_or_cmd(buf, &cmd));
  378. tmp = tor_malloc_zero(65535);
  379. generic_buffer_add(buf, tmp, 65535);
  380. tt_int_op(1, OP_EQ, generic_buffer_fetch_ext_or_cmd(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, generic_buffer_len(buf));
  386. ext_or_cmd_free(cmd);
  387. cmd = NULL;
  388. done:
  389. ext_or_cmd_free(cmd);
  390. generic_buffer_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. struct timeval tv0;
  453. const time_t START = 1389288246;
  454. const uint32_t START_MSEC = (uint32_t) ((uint64_t)START * 1000);
  455. int i;
  456. char tmp[4096];
  457. (void)arg;
  458. crypto_rand(tmp, sizeof(tmp));
  459. tv0.tv_sec = START;
  460. tv0.tv_usec = 0;
  461. buf = buf_new_with_capacity(3000); /* rounds up to next power of 2. */
  462. tt_assert(buf);
  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. tor_gettimeofday_cache_set(&tv0);
  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. tv0.tv_usec += 123 * 1000;
  475. tor_gettimeofday_cache_set(&tv0);
  476. for (i = 0; i < 600; ++i)
  477. write_to_buf("ABCDEFG", 7, buf);
  478. tt_int_op(4207, OP_EQ, buf_datalen(buf));
  479. /* The oldest bytes are still in the front. */
  480. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2000));
  481. /* Once those bytes are dropped, the chunk is still on the first
  482. * timestamp. */
  483. fetch_from_buf(tmp, 100, buf);
  484. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2000));
  485. /* But once we discard the whole first chunk, we get the data in the second
  486. * chunk. */
  487. fetch_from_buf(tmp, 4000, buf);
  488. tt_int_op(107, OP_EQ, buf_datalen(buf));
  489. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2123));
  490. /* This time we'll be grabbing a chunk from the freelist, and making sure
  491. its time gets updated */
  492. tv0.tv_sec += 5;
  493. tv0.tv_usec = 617*1000;
  494. tor_gettimeofday_cache_set(&tv0);
  495. for (i = 0; i < 600; ++i)
  496. write_to_buf("ABCDEFG", 7, buf);
  497. tt_int_op(4307, OP_EQ, buf_datalen(buf));
  498. tt_int_op(2000, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+2123));
  499. fetch_from_buf(tmp, 4000, buf);
  500. fetch_from_buf(tmp, 306, buf);
  501. tt_int_op(0, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+5617));
  502. tt_int_op(383, OP_EQ, buf_get_oldest_chunk_timestamp(buf, START_MSEC+6000));
  503. done:
  504. buf_free(buf);
  505. buf_free(buf2);
  506. }
  507. static void
  508. test_buffers_zlib_impl(int finalize_with_nil)
  509. {
  510. char *msg = NULL;
  511. char *contents = NULL;
  512. char *expanded = NULL;
  513. buf_t *buf = NULL;
  514. tor_zlib_state_t *zlib_state = NULL;
  515. size_t out_len, in_len;
  516. int done;
  517. buf = buf_new_with_capacity(128); /* will round up */
  518. zlib_state = tor_zlib_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
  519. msg = tor_malloc(512);
  520. crypto_rand(msg, 512);
  521. tt_int_op(write_to_buf_zlib(buf, zlib_state, msg, 128, 0), OP_EQ, 0);
  522. tt_int_op(write_to_buf_zlib(buf, zlib_state, msg+128, 128, 0), OP_EQ, 0);
  523. tt_int_op(write_to_buf_zlib(buf, zlib_state, msg+256, 256, 0), OP_EQ, 0);
  524. done = !finalize_with_nil;
  525. tt_int_op(write_to_buf_zlib(buf, zlib_state, "all done", 9, done), OP_EQ, 0);
  526. if (finalize_with_nil) {
  527. tt_int_op(write_to_buf_zlib(buf, zlib_state, "", 0, 1), OP_EQ, 0);
  528. }
  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. tt_int_op(0, OP_EQ, tor_gzip_uncompress(&expanded, &out_len,
  533. contents, in_len,
  534. ZLIB_METHOD, 1,
  535. LOG_WARN));
  536. tt_int_op(out_len, OP_GE, 128);
  537. tt_mem_op(msg, OP_EQ, expanded, 128);
  538. tt_int_op(out_len, OP_GE, 512);
  539. tt_mem_op(msg, OP_EQ, expanded, 512);
  540. tt_int_op(out_len, OP_EQ, 512+9);
  541. tt_mem_op("all done", OP_EQ, expanded+512, 9);
  542. done:
  543. buf_free(buf);
  544. tor_zlib_free(zlib_state);
  545. tor_free(contents);
  546. tor_free(expanded);
  547. tor_free(msg);
  548. }
  549. static void
  550. test_buffers_zlib(void *arg)
  551. {
  552. (void) arg;
  553. test_buffers_zlib_impl(0);
  554. }
  555. static void
  556. test_buffers_zlib_fin_with_nil(void *arg)
  557. {
  558. (void) arg;
  559. test_buffers_zlib_impl(1);
  560. }
  561. static void
  562. test_buffers_zlib_fin_at_chunk_end(void *arg)
  563. {
  564. char *msg = NULL;
  565. char *contents = NULL;
  566. char *expanded = NULL;
  567. buf_t *buf = NULL;
  568. tor_zlib_state_t *zlib_state = NULL;
  569. size_t out_len, in_len;
  570. size_t sz, headerjunk;
  571. (void) arg;
  572. buf = buf_new_with_capacity(128); /* will round up */
  573. sz = buf_get_default_chunk_size(buf);
  574. msg = tor_malloc_zero(sz);
  575. write_to_buf(msg, 1, buf);
  576. tt_assert(buf->head);
  577. /* Fill up the chunk so the zlib stuff won't fit in one chunk. */
  578. tt_uint_op(buf->head->memlen, OP_LT, sz);
  579. headerjunk = buf->head->memlen - 7;
  580. write_to_buf(msg, headerjunk-1, buf);
  581. tt_uint_op(buf->head->datalen, OP_EQ, headerjunk);
  582. tt_uint_op(buf_datalen(buf), OP_EQ, headerjunk);
  583. /* Write an empty string, with finalization on. */
  584. zlib_state = tor_zlib_new(1, ZLIB_METHOD, HIGH_COMPRESSION);
  585. tt_int_op(write_to_buf_zlib(buf, zlib_state, "", 0, 1), OP_EQ, 0);
  586. in_len = buf_datalen(buf);
  587. contents = tor_malloc(in_len);
  588. tt_int_op(fetch_from_buf(contents, in_len, buf), OP_EQ, 0);
  589. tt_uint_op(in_len, OP_GT, headerjunk);
  590. tt_int_op(0, OP_EQ, tor_gzip_uncompress(&expanded, &out_len,
  591. contents + headerjunk, in_len - headerjunk,
  592. ZLIB_METHOD, 1,
  593. LOG_WARN));
  594. tt_int_op(out_len, OP_EQ, 0);
  595. tt_assert(expanded);
  596. done:
  597. buf_free(buf);
  598. tor_zlib_free(zlib_state);
  599. tor_free(contents);
  600. tor_free(expanded);
  601. tor_free(msg);
  602. }
  603. static const uint8_t *tls_read_ptr;
  604. static int n_remaining;
  605. static int next_reply_val[16];
  606. static int
  607. mock_tls_read(tor_tls_t *tls, char *cp, size_t len)
  608. {
  609. (void)tls;
  610. int rv = next_reply_val[0];
  611. if (rv > 0) {
  612. int max = rv > (int)len ? (int)len : rv;
  613. if (max > n_remaining)
  614. max = n_remaining;
  615. memcpy(cp, tls_read_ptr, max);
  616. rv = max;
  617. n_remaining -= max;
  618. tls_read_ptr += max;
  619. }
  620. memmove(next_reply_val, next_reply_val + 1, 15*sizeof(int));
  621. return rv;
  622. }
  623. static void
  624. test_buffers_tls_read_mocked(void *arg)
  625. {
  626. uint8_t *mem;
  627. buf_t *buf;
  628. (void)arg;
  629. mem = tor_malloc(64*1024);
  630. crypto_rand((char*)mem, 64*1024);
  631. tls_read_ptr = mem;
  632. n_remaining = 64*1024;
  633. MOCK(tor_tls_read, mock_tls_read);
  634. buf = buf_new();
  635. next_reply_val[0] = 1024;
  636. tt_int_op(128, ==, read_to_buf_tls(NULL, 128, buf));
  637. next_reply_val[0] = 5000;
  638. next_reply_val[1] = 5000;
  639. tt_int_op(6000, ==, read_to_buf_tls(NULL, 6000, buf));
  640. done:
  641. UNMOCK(tor_tls_read);
  642. tor_free(mem);
  643. buf_free(buf);
  644. }
  645. struct testcase_t buffer_tests[] = {
  646. { "basic", test_buffers_basic, TT_FORK, NULL, NULL },
  647. { "copy", test_buffer_copy, TT_FORK, NULL, NULL },
  648. { "pullup", test_buffer_pullup, TT_FORK, NULL, NULL },
  649. { "ext_or_cmd", test_buffer_ext_or_cmd, TT_FORK, NULL, NULL },
  650. { "allocation_tracking", test_buffer_allocation_tracking, TT_FORK,
  651. NULL, NULL },
  652. { "time_tracking", test_buffer_time_tracking, TT_FORK, NULL, NULL },
  653. { "zlib", test_buffers_zlib, TT_FORK, NULL, NULL },
  654. { "zlib_fin_with_nil", test_buffers_zlib_fin_with_nil, TT_FORK, NULL, NULL },
  655. { "zlib_fin_at_chunk_end", test_buffers_zlib_fin_at_chunk_end, TT_FORK,
  656. NULL, NULL},
  657. { "tls_read_mocked", test_buffers_tls_read_mocked, 0,
  658. NULL, NULL },
  659. END_OF_TESTCASES
  660. };