summaryrefslogtreecommitdiff
path: root/src/or/test.h
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2009-09-21 14:23:13 -0400
committerNick Mathewson <nickm@torproject.org>2009-09-23 00:24:43 -0400
commitd4b54549b83bbbfeb9c1d7d843ea244eed389c61 (patch)
tree07456ee45551880371239a77621bd7e35ed2ab0a /src/or/test.h
parent1c2d7732f0a2d74e61bc4276421bec78b2256f64 (diff)
downloadtor-d4b54549b83bbbfeb9c1d7d843ea244eed389c61.tar.gz
tor-d4b54549b83bbbfeb9c1d7d843ea244eed389c61.zip
Refactor unit tests to use the tinytest framework.
"Tinytest" is a minimalist C unit testing framework I wrote for Libevent. It supports some generally useful features, like being able to run separate unit tests in their own processes. I tried to do the refactoring to change test.c as little as possible. Thus, we mostly don't call the tinytest macros directly. Instead, the test.h header is now a wrapper on tinytest.h to make our existing test_foo() macros work. The next step(s) here will be: - To break test.c into separate files, each with its own test group. - To look into which things we can test - To refactor the more fiddly tests to use the tinytest macros directly and/or run forked. - To see about writing unit tests for things we couldn't previously test without forking.
Diffstat (limited to 'src/or/test.h')
-rw-r--r--src/or/test.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/or/test.h b/src/or/test.h
new file mode 100644
index 0000000000..e4bae6d78a
--- /dev/null
+++ b/src/or/test.h
@@ -0,0 +1,66 @@
+/* Copyright (c) 2001-2003, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2009, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef _TOR_TEST_H
+#define _TOR_TEST_H
+
+/**
+ * \file test.h
+ * \brief Macros used by unit tests.
+ */
+
+#include "compat.h"
+#include "tinytest.h"
+#define TT_EXIT_TEST_FUNCTION STMT_BEGIN goto done; STMT_END
+#include "tinytest_macros.h"
+
+#ifdef __GNUC__
+#define PRETTY_FUNCTION __PRETTY_FUNCTION__
+#else
+#define PRETTY_FUNCTION ""
+#endif
+
+#define test_fail_msg(msg) TT_DIE((msg))
+
+#define test_fail() test_fail_msg("Assertion failed.")
+
+#define test_assert(expr) tt_assert(expr)
+
+#define test_eq(expr1, expr2) tt_int_op((expr1), ==, (expr2))
+#define test_eq_ptr(expr1, expr2) tt_ptr_op((expr1), ==, (expr2))
+#define test_neq(expr1, expr2) tt_int_op((expr1), !=, (expr2))
+#define test_neq_ptr(expr1, expr2) tt_ptr_op((expr1), !=, (expr2))
+#define test_streq(expr1, expr2) tt_str_op((expr1), ==, (expr2))
+#define test_strneq(expr1, expr2) tt_str_op((expr1), !=, (expr2))
+#define test_streq(expr1, expr2) tt_str_op((expr1), ==, (expr2))
+
+#define test_mem_op(expr1, op, expr2, len) \
+ tt_assert_test_fmt_type(expr1,expr2,#expr1" "#op" "#expr2, \
+ const char *, \
+ (memcmp(_val1, _val2, len) op 0), \
+ char *, "%s", \
+ { size_t printlen = (len)*2+1; \
+ _print = tor_malloc(printlen); \
+ base16_encode(_print, printlen, _value, \
+ (len)); }, \
+ { tor_free(_print); } \
+ );
+
+#define test_memeq(expr1, expr2, len) test_mem_op((expr1), ==, (expr2), len)
+#define test_memneq(expr1, expr2, len) test_mem_op((expr1), !=, (expr2), len)
+
+#define test_mem_op_hex(expr1, op, hex) \
+ STMT_BEGIN \
+ size_t length = strlen(hex); \
+ char *value2 = tor_malloc(length/2); \
+ tor_assert((length&1)==0); \
+ base16_decode(value2, length/2, hex, length); \
+ test_mem_op(expr1, op, value2, length/2); \
+ STMT_END
+
+#define test_memeq_hex(expr1, hex) test_mem_op_hex(expr1, ==, hex)
+
+#endif
+