2016-02-27 18:48:19 +01:00
|
|
|
/* Copyright (c) 2013-2016, The Tor Project, Inc. */
|
2013-06-06 20:56:05 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#ifndef TOR_TESTSUPPORT_H
|
|
|
|
#define TOR_TESTSUPPORT_H
|
|
|
|
|
|
|
|
#ifdef TOR_UNIT_TESTS
|
2013-06-06 23:58:28 +02:00
|
|
|
#define STATIC
|
2016-06-25 09:47:25 +02:00
|
|
|
#define EXTERN(type, name) extern type name;
|
2013-06-06 20:56:05 +02:00
|
|
|
#else
|
2013-06-06 23:58:28 +02:00
|
|
|
#define STATIC static
|
2016-06-25 09:47:25 +02:00
|
|
|
#define EXTERN(type, name)
|
2013-06-06 20:56:05 +02:00
|
|
|
#endif
|
|
|
|
|
2013-07-10 21:03:01 +02:00
|
|
|
/** 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:
|
|
|
|
*
|
2014-04-26 15:53:28 +02:00
|
|
|
* MOCK_IMPL(void,
|
|
|
|
* writebuf,(size_t n, char *buf))
|
2013-07-10 21:03:01 +02:00
|
|
|
* {
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* 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(func, replacement) \
|
|
|
|
do { \
|
|
|
|
(func) = (replacement); \
|
|
|
|
} while (0)
|
|
|
|
#define UNMOCK(func) \
|
|
|
|
do { \
|
|
|
|
func = func ##__real; \
|
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define MOCK_DECL(rv, funcname, arglist) \
|
|
|
|
rv funcname arglist
|
|
|
|
#define MOCK_IMPL(rv, funcname, arglist) \
|
|
|
|
rv funcname arglist
|
|
|
|
#endif
|
|
|
|
/** @} */
|
|
|
|
|
2013-06-06 20:56:05 +02:00
|
|
|
#endif
|
|
|
|
|