aboutsummaryrefslogtreecommitdiff
path: root/src/test/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/test.c')
-rw-r--r--src/test/test.c141
1 files changed, 66 insertions, 75 deletions
diff --git a/src/test/test.c b/src/test/test.c
index 9948ecf99..16ac1e4a6 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -61,6 +61,7 @@ double fabs(double x);
#ifdef USE_DMALLOC
#include <dmalloc.h>
#include <openssl/crypto.h>
+#include "main.h"
#endif
/** Set to true if any unit test has failed. Mostly, this is set by the macros
@@ -70,6 +71,7 @@ int have_failed = 0;
/** Temporary directory (set up by setup_directory) under which we store all
* our files during testing. */
static char temp_dir[256];
+static pid_t temp_dir_setup_in_pid = 0;
/** Select and create the temporary directory we'll use to run our unit tests.
* Store it in <b>temp_dir</b>. Exit immediately if we can't create it.
@@ -96,6 +98,7 @@ setup_directory(void)
exit(1);
}
is_setup = 1;
+ temp_dir_setup_in_pid = getpid();
}
/** Return a filename relative to our testing temporary directory */
@@ -109,11 +112,16 @@ get_fname(const char *name)
}
/** Remove all files stored under the temporary directory, and the directory
- * itself. */
+ * itself. Called by atexit(). */
static void
remove_directory(void)
{
- smartlist_t *elements = tor_listdir(temp_dir);
+ smartlist_t *elements;
+ if (getpid() != temp_dir_setup_in_pid) {
+ /* Only clean out the tempdir when the main process is exiting. */
+ return;
+ }
+ elements = tor_listdir(temp_dir);
if (elements) {
SMARTLIST_FOREACH(elements, const char *, cp,
{
@@ -337,76 +345,6 @@ test_buffers(void)
buf_free(buf);
buf = NULL;
-#if 0
- {
- int s;
- int eof;
- int i;
- buf_t *buf2;
- /****
- * read_to_buf
- ****/
- s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
- write(s, str, 256);
- close(s);
-
- s = open(get_fname("data"), O_RDONLY, 0);
- eof = 0;
- errno = 0; /* XXXX */
- i = read_to_buf(s, 10, buf, &eof);
- printf("%s\n", strerror(errno));
- test_eq(i, 10);
- test_eq(eof, 0);
- //test_eq(buf_capacity(buf), 4096);
- test_eq(buf_datalen(buf), 10);
-
- test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
-
- /* Test reading 0 bytes. */
- i = read_to_buf(s, 0, buf, &eof);
- //test_eq(buf_capacity(buf), 512*1024);
- test_eq(buf_datalen(buf), 10);
- test_eq(eof, 0);
- test_eq(i, 0);
-
- /* Now test when buffer is filled exactly. */
- buf2 = buf_new_with_capacity(6);
- i = read_to_buf(s, 6, buf2, &eof);
- //test_eq(buf_capacity(buf2), 6);
- test_eq(buf_datalen(buf2), 6);
- test_eq(eof, 0);
- test_eq(i, 6);
- test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
- buf_free(buf2);
- buf2 = NULL;
-
- /* Now test when buffer is filled with more data to read. */
- buf2 = buf_new_with_capacity(32);
- i = read_to_buf(s, 128, buf2, &eof);
- //test_eq(buf_capacity(buf2), 128);
- test_eq(buf_datalen(buf2), 32);
- test_eq(eof, 0);
- test_eq(i, 32);
- buf_free(buf2);
- buf2 = NULL;
-
- /* Now read to eof. */
- test_assert(buf_capacity(buf) > 256);
- i = read_to_buf(s, 1024, buf, &eof);
- test_eq(i, (256-32-10-6));
- test_eq(buf_capacity(buf), MAX_BUF_SIZE);
- test_eq(buf_datalen(buf), 256-6-32);
- test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
- test_eq(eof, 0);
-
- i = read_to_buf(s, 1024, buf, &eof);
- test_eq(i, 0);
- test_eq(buf_capacity(buf), MAX_BUF_SIZE);
- test_eq(buf_datalen(buf), 256-6-32);
- test_eq(eof, 1);
- }
-#endif
-
done:
if (buf)
buf_free(buf);
@@ -478,7 +416,6 @@ test_circuit_timeout(void)
circuit_build_times_t final;
double timeout1, timeout2;
or_state_t state;
- char *msg;
int i, runs;
double close_ms;
circuit_build_times_init(&initial);
@@ -518,7 +455,7 @@ test_circuit_timeout(void)
test_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
circuit_build_times_update_state(&estimate, &state);
- test_assert(circuit_build_times_parse_state(&final, &state, &msg) == 0);
+ test_assert(circuit_build_times_parse_state(&final, &state) == 0);
circuit_build_times_update_alpha(&final);
timeout2 = circuit_build_times_calculate_timeout(&final,
@@ -620,7 +557,7 @@ test_circuit_timeout(void)
circuit_build_times_count_timeout(&final, 1);
}
-done:
+ done:
return;
}
@@ -1150,6 +1087,57 @@ test_geoip(void)
tor_free(s);
}
+/** Run unit tests for stats code. */
+static void
+test_stats(void)
+{
+ time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
+ char *s = NULL;
+
+ /* We shouldn't collect exit stats without initializing them. */
+ rep_hist_note_exit_stream_opened(80);
+ rep_hist_note_exit_bytes(80, 100, 10000);
+ s = rep_hist_format_exit_stats(now + 86400);
+ test_assert(!s);
+
+ /* Initialize stats, note some streams and bytes, and generate history
+ * string. */
+ rep_hist_exit_stats_init(now);
+ rep_hist_note_exit_stream_opened(80);
+ rep_hist_note_exit_bytes(80, 100, 10000);
+ rep_hist_note_exit_stream_opened(443);
+ rep_hist_note_exit_bytes(443, 100, 10000);
+ rep_hist_note_exit_bytes(443, 100, 10000);
+ s = rep_hist_format_exit_stats(now + 86400);
+ test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
+ "exit-kibibytes-written 80=1,443=1,other=0\n"
+ "exit-kibibytes-read 80=10,443=20,other=0\n"
+ "exit-streams-opened 80=4,443=4,other=0\n", s);
+ tor_free(s);
+
+ /* Stop collecting stats, add some bytes, and ensure we don't generate
+ * a history string. */
+ rep_hist_exit_stats_term();
+ rep_hist_note_exit_bytes(80, 100, 10000);
+ s = rep_hist_format_exit_stats(now + 86400);
+ test_assert(!s);
+
+ /* Re-start stats, add some bytes, reset stats, and see what history we
+ * get when observing no streams or bytes at all. */
+ rep_hist_exit_stats_init(now);
+ rep_hist_note_exit_stream_opened(80);
+ rep_hist_note_exit_bytes(80, 100, 10000);
+ rep_hist_reset_exit_stats(now);
+ s = rep_hist_format_exit_stats(now + 86400);
+ test_streq("exit-stats-end 2010-08-12 13:27:30 (86400 s)\n"
+ "exit-kibibytes-written other=0\n"
+ "exit-kibibytes-read other=0\n"
+ "exit-streams-opened other=0\n", s);
+
+ done:
+ tor_free(s);
+}
+
static void *
legacy_test_setup(const struct testcase_t *testcase)
{
@@ -1182,6 +1170,8 @@ const struct testcase_setup_t legacy_setup = {
test_ ## group ## _ ## name }
#define DISABLED(name) \
{ #name, legacy_test_helper, TT_SKIP, &legacy_setup, name }
+#define FORK(name) \
+ { #name, legacy_test_helper, TT_FORK, &legacy_setup, test_ ## name }
static struct testcase_t test_array[] = {
ENT(buffers),
@@ -1190,6 +1180,7 @@ static struct testcase_t test_array[] = {
ENT(policies),
ENT(rend_fns),
ENT(geoip),
+ FORK(stats),
DISABLED(bench_aes),
DISABLED(bench_dmap),