From 5b8f4769dc93bf3c934dd6b2fdfec2977b8bee55 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 21 Jun 2018 13:12:06 -0400 Subject: Move testsupport.h to its own directory --- src/common/include.am | 1 - src/common/testsupport.h | 90 --------------------------------------- src/include.am | 1 + src/lib/testsupport/include.am | 3 ++ src/lib/testsupport/testsupport.h | 90 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 91 deletions(-) delete mode 100644 src/common/testsupport.h create mode 100644 src/lib/testsupport/include.am create mode 100644 src/lib/testsupport/testsupport.h diff --git a/src/common/include.am b/src/common/include.am index 3b49220cec..29bbdd7696 100644 --- a/src/common/include.am +++ b/src/common/include.am @@ -94,7 +94,6 @@ COMMONHEADERS = \ src/common/procmon.h \ src/common/sandbox.h \ src/common/storagedir.h \ - src/common/testsupport.h \ src/common/timers.h \ src/common/token_bucket.h \ src/common/torlog.h \ diff --git a/src/common/testsupport.h b/src/common/testsupport.h deleted file mode 100644 index 9a55d306fc..0000000000 --- a/src/common/testsupport.h +++ /dev/null @@ -1,90 +0,0 @@ -/* Copyright (c) 2013-2018, The Tor Project, Inc. */ -/* See LICENSE for licensing information */ - -#ifndef TOR_TESTSUPPORT_H -#define TOR_TESTSUPPORT_H - -#ifdef TOR_UNIT_TESTS -#define STATIC -#define EXTERN(type, name) extern type name; -#else -#define STATIC static -#define EXTERN(type, name) -#endif /* defined(TOR_UNIT_TESTS) */ - -/** Quick and dirty macros to implement test mocking. - * - * To use them, suppose that you have a function you'd like to mock - * with the signature "void writebuf(size_t n, char *buf)". You can then - * declare the function as: - * - * MOCK_DECL(void, writebuf, (size_t n, char *buf)); - * - * and implement it as: - * - * MOCK_IMPL(void, - * writebuf,(size_t n, char *buf)) - * { - * ... - * } - * - * For the non-testing build, this will expand simply into: - * - * void writebuf(size_t n, char *buf); - * void - * writebuf(size_t n, char *buf) - * { - * ... - * } - * - * But for the testing case, it will expand into: - * - * void writebuf__real(size_t n, char *buf); - * extern void (*writebuf)(size_t n, char *buf); - * - * void (*writebuf)(size_t n, char *buf) = writebuf__real; - * void - * writebuf__real(size_t n, char *buf) - * { - * ... - * } - * - * This is not a great mocking system! It is deliberately "the simplest - * thing that could work", and pays for its simplicity in its lack of - * features, and in its uglification of the Tor code. Replacing it with - * something clever would be a fine thing. - * - * @{ */ -#ifdef TOR_UNIT_TESTS -#define MOCK_DECL(rv, funcname, arglist) \ - rv funcname ##__real arglist; \ - extern rv(*funcname) arglist -#define MOCK_IMPL(rv, funcname, arglist) \ - rv(*funcname) arglist = funcname ##__real; \ - rv funcname ##__real arglist -#define MOCK_DECL_ATTR(rv, funcname, arglist, attr) \ - rv funcname ##__real arglist attr; \ - extern rv(*funcname) arglist -#define MOCK_IMPL(rv, funcname, arglist) \ - rv(*funcname) arglist = funcname ##__real; \ - rv funcname ##__real arglist -#define MOCK(func, replacement) \ - do { \ - (func) = (replacement); \ - } while (0) -#define UNMOCK(func) \ - do { \ - func = func ##__real; \ - } while (0) -#else /* !(defined(TOR_UNIT_TESTS)) */ -#define MOCK_DECL(rv, funcname, arglist) \ - rv funcname arglist -#define MOCK_DECL_ATTR(rv, funcname, arglist, attr) \ - rv funcname arglist attr -#define MOCK_IMPL(rv, funcname, arglist) \ - rv funcname arglist -#endif /* defined(TOR_UNIT_TESTS) */ -/** @} */ - -#endif /* !defined(TOR_TESTSUPPORT_H) */ - diff --git a/src/include.am b/src/include.am index 685fe34337..c70a681887 100644 --- a/src/include.am +++ b/src/include.am @@ -5,6 +5,7 @@ include src/lib/ctime/include.am include src/lib/compress/include.am include src/lib/crypt_ops/include.am include src/lib/include.libdonna.am +include src/lib/testsupport/include.am include src/lib/tls/include.am include src/lib/trace/include.am include src/common/include.am diff --git a/src/lib/testsupport/include.am b/src/lib/testsupport/include.am new file mode 100644 index 0000000000..b2aa620985 --- /dev/null +++ b/src/lib/testsupport/include.am @@ -0,0 +1,3 @@ + +noinst_HEADERS += \ + src/lib/testsupport/testsupport.h diff --git a/src/lib/testsupport/testsupport.h b/src/lib/testsupport/testsupport.h new file mode 100644 index 0000000000..9a55d306fc --- /dev/null +++ b/src/lib/testsupport/testsupport.h @@ -0,0 +1,90 @@ +/* Copyright (c) 2013-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#ifndef TOR_TESTSUPPORT_H +#define TOR_TESTSUPPORT_H + +#ifdef TOR_UNIT_TESTS +#define STATIC +#define EXTERN(type, name) extern type name; +#else +#define STATIC static +#define EXTERN(type, name) +#endif /* defined(TOR_UNIT_TESTS) */ + +/** Quick and dirty macros to implement test mocking. + * + * To use them, suppose that you have a function you'd like to mock + * with the signature "void writebuf(size_t n, char *buf)". You can then + * declare the function as: + * + * MOCK_DECL(void, writebuf, (size_t n, char *buf)); + * + * and implement it as: + * + * MOCK_IMPL(void, + * writebuf,(size_t n, char *buf)) + * { + * ... + * } + * + * For the non-testing build, this will expand simply into: + * + * void writebuf(size_t n, char *buf); + * void + * writebuf(size_t n, char *buf) + * { + * ... + * } + * + * But for the testing case, it will expand into: + * + * void writebuf__real(size_t n, char *buf); + * extern void (*writebuf)(size_t n, char *buf); + * + * void (*writebuf)(size_t n, char *buf) = writebuf__real; + * void + * writebuf__real(size_t n, char *buf) + * { + * ... + * } + * + * This is not a great mocking system! It is deliberately "the simplest + * thing that could work", and pays for its simplicity in its lack of + * features, and in its uglification of the Tor code. Replacing it with + * something clever would be a fine thing. + * + * @{ */ +#ifdef TOR_UNIT_TESTS +#define MOCK_DECL(rv, funcname, arglist) \ + rv funcname ##__real arglist; \ + extern rv(*funcname) arglist +#define MOCK_IMPL(rv, funcname, arglist) \ + rv(*funcname) arglist = funcname ##__real; \ + rv funcname ##__real arglist +#define MOCK_DECL_ATTR(rv, funcname, arglist, attr) \ + rv funcname ##__real arglist attr; \ + extern rv(*funcname) arglist +#define MOCK_IMPL(rv, funcname, arglist) \ + rv(*funcname) arglist = funcname ##__real; \ + rv funcname ##__real arglist +#define MOCK(func, replacement) \ + do { \ + (func) = (replacement); \ + } while (0) +#define UNMOCK(func) \ + do { \ + func = func ##__real; \ + } while (0) +#else /* !(defined(TOR_UNIT_TESTS)) */ +#define MOCK_DECL(rv, funcname, arglist) \ + rv funcname arglist +#define MOCK_DECL_ATTR(rv, funcname, arglist, attr) \ + rv funcname arglist attr +#define MOCK_IMPL(rv, funcname, arglist) \ + rv funcname arglist +#endif /* defined(TOR_UNIT_TESTS) */ +/** @} */ + +#endif /* !defined(TOR_TESTSUPPORT_H) */ + -- cgit v1.2.3-54-g00ecf