[tor-commits] [tor/master] Several tests for buffer allocation

nickm at torproject.org nickm at torproject.org
Tue Mar 4 16:04:42 UTC 2014


commit d379fc6e0ffce916753e5ef1ac0783703d150fa5
Author: Nick Mathewson <nickm at torproject.org>
Date:   Sun Jan 5 22:27:37 2014 -0500

    Several tests for buffer allocation
---
 src/test/test_buffers.c |   46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

diff --git a/src/test/test_buffers.c b/src/test/test_buffers.c
index bd958b6..ab1e5df 100644
--- a/src/test/test_buffers.c
+++ b/src/test/test_buffers.c
@@ -451,10 +451,13 @@ test_buffer_ext_or_cmd(void *arg)
 static void
 test_buffer_allocation_tracking(void *arg)
 {
+  char *junk = tor_malloc(16384);
   buf_t *buf1 = NULL, *buf2 = NULL;
+  int i;
 
   (void)arg;
 
+  crypto_rand(junk, 16384);
   tt_int_op(buf_get_total_allocation(), ==, 0);
 
   buf1 = buf_new();
@@ -462,8 +465,49 @@ test_buffer_allocation_tracking(void *arg)
   buf2 = buf_new();
   tt_assert(buf2);
 
-  //xXXXXXX buf_add
+  tt_int_op(buf_allocation(buf1), ==, 0);
+  tt_int_op(buf_get_total_allocation(), ==, 0);
+
+  write_to_buf(junk, 4000, buf1);
+  write_to_buf(junk, 4000, buf1);
+  write_to_buf(junk, 4000, buf1);
+  write_to_buf(junk, 4000, buf1);
+  tt_int_op(buf_allocation(buf1), ==, 16384);
+  fetch_from_buf(junk, 100, buf1);
+  tt_int_op(buf_allocation(buf1), ==, 16384); /* still 4 4k chunks */
+
+  tt_int_op(buf_get_total_allocation(), ==, 16384);
+
+  fetch_from_buf(junk, 4096, buf1); /* drop a 1k chunk... */
+  tt_int_op(buf_allocation(buf1), ==, 3*4096); /* now 3 4k chunks */
+
+  tt_int_op(buf_get_total_allocation(), ==, 16384); /* that chunk went onto
+                                                       the freelist. */
+
+  write_to_buf(junk, 4000, buf2);
+  tt_int_op(buf_allocation(buf2), ==, 4096); /* another 4k chunk. */
+  tt_int_op(buf_get_total_allocation(), ==, 16384); /* that chunk came from
+                                                       the freelist. */
+  write_to_buf(junk, 4000, buf2);
+  tt_int_op(buf_allocation(buf2), ==, 8192); /* another 4k chunk. */
+  tt_int_op(buf_get_total_allocation(), ==, 5*4096); /* that chunk was new. */
+
 
+  /* Make a really huge buffer */
+  for (i = 0; i < 1000; ++i) {
+    write_to_buf(junk, 4000, buf2);
+  }
+  tt_int_op(buf_allocation(buf2), >=, 4008000);
+  tt_int_op(buf_get_total_allocation(), >=, 4008000);
+  buf_free(buf2);
+  buf2 = NULL;
+
+  tt_int_op(buf_get_total_allocation(), <, 4008000);
+  buf_shrink_freelists(1);
+  tt_int_op(buf_get_total_allocation(), ==, buf_allocation(buf1));
+  buf_free(buf1);
+  buf1 = NULL;
+  buf_shrink_freelists(1);
   tt_int_op(buf_get_total_allocation(), ==, 0);
 
  done:





More information about the tor-commits mailing list