2009-09-21 20:23:13 +02:00
|
|
|
/* Copyright (c) 2001-2003, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2018-06-20 14:13:28 +02:00
|
|
|
* Copyright (c) 2007-2018, The Tor Project, Inc. */
|
2009-09-21 20:23:13 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2012-10-12 18:13:10 +02:00
|
|
|
#ifndef TOR_TEST_H
|
|
|
|
#define TOR_TEST_H
|
2009-09-21 20:23:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file test.h
|
2009-09-22 19:06:47 +02:00
|
|
|
* \brief Macros and functions used by unit tests.
|
2009-09-21 20:23:13 +02:00
|
|
|
*/
|
|
|
|
|
2018-06-03 13:33:31 +02:00
|
|
|
#define DEBUG_SMARTLIST 1
|
|
|
|
|
2009-09-21 20:23:13 +02:00
|
|
|
#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
|
|
|
|
|
2009-09-27 18:07:33 +02:00
|
|
|
/* As test_mem_op, but decodes 'hex' before comparing. There must be a
|
|
|
|
* local char* variable called mem_op_hex_tmp for this to work. */
|
2009-09-21 20:23:13 +02:00
|
|
|
#define test_mem_op_hex(expr1, op, hex) \
|
|
|
|
STMT_BEGIN \
|
|
|
|
size_t length = strlen(hex); \
|
2009-09-27 18:07:33 +02:00
|
|
|
tor_free(mem_op_hex_tmp); \
|
|
|
|
mem_op_hex_tmp = tor_malloc(length/2); \
|
2009-09-21 20:23:13 +02:00
|
|
|
tor_assert((length&1)==0); \
|
2009-09-27 18:07:33 +02:00
|
|
|
base16_decode(mem_op_hex_tmp, length/2, hex, length); \
|
2014-09-16 03:27:23 +02:00
|
|
|
tt_mem_op(expr1, op, mem_op_hex_tmp, length/2); \
|
2009-09-21 20:23:13 +02:00
|
|
|
STMT_END
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
#define test_memeq_hex(expr1, hex) test_mem_op_hex(expr1, OP_EQ, hex)
|
2009-09-21 20:23:13 +02:00
|
|
|
|
2012-08-09 19:47:42 +02:00
|
|
|
#define tt_double_op(a,op,b) \
|
2015-02-24 16:05:34 +01:00
|
|
|
tt_assert_test_type(a,b,#a" "#op" "#b,double,(val1_ op val2_),"%g", \
|
2012-08-09 19:47:42 +02:00
|
|
|
TT_EXIT_TEST_FUNCTION)
|
|
|
|
|
2015-08-14 14:48:26 +02:00
|
|
|
/* Declare "double equal" in a sneaky way, so compiler won't complain about
|
|
|
|
* comparing floats with == or !=. Of course, only do this if you know what
|
|
|
|
* you're doing. */
|
|
|
|
#define tt_double_eq(a,b) \
|
|
|
|
STMT_BEGIN \
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_double_op((a), OP_GE, (b)); \
|
|
|
|
tt_double_op((a), OP_LE, (b)); \
|
2015-08-14 14:48:26 +02:00
|
|
|
STMT_END
|
|
|
|
|
2014-05-08 20:01:17 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define U64_PRINTF_TYPE uint64_t
|
2014-08-13 21:11:00 +02:00
|
|
|
#define I64_PRINTF_TYPE int64_t
|
2014-05-08 20:01:17 +02:00
|
|
|
#else
|
|
|
|
#define U64_PRINTF_TYPE unsigned long long
|
|
|
|
#define I64_PRINTF_TYPE long long
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(_MSC_VER) */
|
2014-05-08 20:01:17 +02:00
|
|
|
|
|
|
|
#define tt_size_op(a,op,b) \
|
|
|
|
tt_assert_test_fmt_type(a,b,#a" "#op" "#b,size_t,(val1_ op val2_), \
|
|
|
|
U64_PRINTF_TYPE, U64_FORMAT, \
|
2014-05-12 05:40:48 +02:00
|
|
|
{print_ = (U64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
|
2014-05-08 20:01:17 +02:00
|
|
|
|
|
|
|
#define tt_u64_op(a,op,b) \
|
|
|
|
tt_assert_test_fmt_type(a,b,#a" "#op" "#b,uint64_t,(val1_ op val2_), \
|
|
|
|
U64_PRINTF_TYPE, U64_FORMAT, \
|
2014-05-12 05:40:48 +02:00
|
|
|
{print_ = (U64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
|
2014-05-08 20:01:17 +02:00
|
|
|
|
|
|
|
#define tt_i64_op(a,op,b) \
|
|
|
|
tt_assert_test_fmt_type(a,b,#a" "#op" "#b,int64_t,(val1_ op val2_), \
|
|
|
|
I64_PRINTF_TYPE, I64_FORMAT, \
|
2014-05-12 05:40:48 +02:00
|
|
|
{print_ = (I64_PRINTF_TYPE) value_;}, {}, TT_EXIT_TEST_FUNCTION)
|
2014-05-08 20:01:17 +02:00
|
|
|
|
2018-03-30 01:27:42 +02:00
|
|
|
/**
|
|
|
|
* Declare that the test is done, even though no tt___op() calls were made.
|
|
|
|
*
|
|
|
|
* For use when you only want to test calling something, but not check
|
|
|
|
* any values/pointers/etc afterwards.
|
|
|
|
*/
|
|
|
|
#define tt_finished() TT_EXIT_TEST_FUNCTION
|
|
|
|
|
2009-09-22 19:06:47 +02:00
|
|
|
const char *get_fname(const char *name);
|
2016-11-02 04:14:19 +01:00
|
|
|
const char *get_fname_rnd(const char *name);
|
2016-02-12 00:34:29 +01:00
|
|
|
struct crypto_pk_t *pk_generate(int idx);
|
2016-09-11 18:36:09 +02:00
|
|
|
void init_pregenerated_keys(void);
|
|
|
|
void free_pregenerated_keys(void);
|
2009-09-22 19:06:47 +02:00
|
|
|
|
2014-04-15 14:20:34 +02:00
|
|
|
#define US2_CONCAT_2__(a, b) a ## __ ## b
|
|
|
|
#define US_CONCAT_2__(a, b) a ## _ ## b
|
|
|
|
#define US_CONCAT_3__(a, b, c) a ## _ ## b ## _ ## c
|
|
|
|
#define US_CONCAT_2_(a, b) US_CONCAT_2__(a, b)
|
|
|
|
#define US_CONCAT_3_(a, b, c) US_CONCAT_3__(a, b, c)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These macros are helpful for streamlining the authorship of several test
|
|
|
|
* cases that use mocks.
|
|
|
|
*
|
|
|
|
* The pattern is as follows.
|
|
|
|
* * Declare a top level namespace:
|
|
|
|
* #define NS_MODULE foo
|
|
|
|
*
|
|
|
|
* * For each test case you want to write, create a new submodule in the
|
|
|
|
* namespace. All mocks and other information should belong to a single
|
|
|
|
* submodule to avoid interference with other test cases.
|
|
|
|
* You can simply name the submodule after the function in the module you
|
|
|
|
* are testing:
|
|
|
|
* #define NS_SUBMODULE some_function
|
|
|
|
* or, if you're wanting to write several tests against the same function,
|
|
|
|
* ie., you are testing an aspect of that function, you can use:
|
|
|
|
* #define NS_SUBMODULE ASPECT(some_function, behavior)
|
|
|
|
*
|
|
|
|
* * Declare all the mocks you will use. The NS_DECL macro serves to declare
|
|
|
|
* the mock in the current namespace (defined by NS_MODULE and NS_SUBMODULE).
|
|
|
|
* It behaves like MOCK_DECL:
|
|
|
|
* NS_DECL(int, dependent_function, (void *));
|
|
|
|
* Here, dependent_function must be declared and implemented with the
|
|
|
|
* MOCK_DECL and MOCK_IMPL macros. The NS_DECL macro also defines an integer
|
|
|
|
* global for use for tracking how many times a mock was called, and can be
|
|
|
|
* accessed by CALLED(mock_name). For example, you might put
|
|
|
|
* CALLED(dependent_function)++;
|
|
|
|
* in your mock body.
|
|
|
|
*
|
|
|
|
* * Define a function called NS(main) that will contain the body of the
|
|
|
|
* test case. The NS macro can be used to reference a name in the current
|
|
|
|
* namespace.
|
|
|
|
*
|
|
|
|
* * In NS(main), indicate that a mock function in the current namespace,
|
|
|
|
* declared with NS_DECL is to override that in the global namespace,
|
|
|
|
* with the NS_MOCK macro:
|
|
|
|
* NS_MOCK(dependent_function)
|
|
|
|
* Unmock with:
|
|
|
|
* NS_UNMOCK(dependent_function)
|
|
|
|
*
|
|
|
|
* * Define the mocks with the NS macro, eg.,
|
|
|
|
* int
|
|
|
|
* NS(dependent_function)(void *)
|
|
|
|
* {
|
|
|
|
* CALLED(dependent_function)++;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* * In the struct testcase_t array, you can use the TEST_CASE and
|
|
|
|
* TEST_CASE_ASPECT macros to define the cases without having to do so
|
|
|
|
* explicitly nor without having to reset NS_SUBMODULE, eg.,
|
|
|
|
* struct testcase_t foo_tests[] = {
|
|
|
|
* TEST_CASE_ASPECT(some_function, behavior),
|
|
|
|
* ...
|
|
|
|
* END_OF_TESTCASES
|
|
|
|
* which will define a test case named "some_function__behavior".
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define NAME_TEST_(name) #name
|
|
|
|
#define NAME_TEST(name) NAME_TEST_(name)
|
|
|
|
#define ASPECT(test_module, test_name) US2_CONCAT_2__(test_module, test_name)
|
|
|
|
#define TEST_CASE(function) \
|
|
|
|
{ \
|
|
|
|
NAME_TEST(function), \
|
|
|
|
NS_FULL(NS_MODULE, function, test_main), \
|
|
|
|
TT_FORK, \
|
|
|
|
NULL, \
|
|
|
|
NULL, \
|
|
|
|
}
|
|
|
|
#define TEST_CASE_ASPECT(function, aspect) \
|
|
|
|
{ \
|
|
|
|
NAME_TEST(ASPECT(function, aspect)), \
|
|
|
|
NS_FULL(NS_MODULE, ASPECT(function, aspect), test_main), \
|
|
|
|
TT_FORK, \
|
|
|
|
NULL, \
|
|
|
|
NULL, \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NS(name) US_CONCAT_3_(NS_MODULE, NS_SUBMODULE, name)
|
|
|
|
#define NS_FULL(module, submodule, name) US_CONCAT_3_(module, submodule, name)
|
|
|
|
|
|
|
|
#define CALLED(mock_name) US_CONCAT_2_(NS(mock_name), called)
|
|
|
|
#define NS_DECL(retval, mock_fn, args) \
|
2016-06-02 15:46:12 +02:00
|
|
|
extern int CALLED(mock_fn); \
|
2014-04-15 14:20:34 +02:00
|
|
|
static retval NS(mock_fn) args; int CALLED(mock_fn) = 0
|
|
|
|
#define NS_MOCK(name) MOCK(name, NS(name))
|
|
|
|
#define NS_UNMOCK(name) UNMOCK(name)
|
|
|
|
|
2013-09-28 05:15:53 +02:00
|
|
|
extern const struct testcase_setup_t passthrough_setup;
|
2016-09-09 16:20:34 +02:00
|
|
|
extern const struct testcase_setup_t ed25519_test_setup;
|
2013-09-28 05:15:53 +02:00
|
|
|
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t accounting_tests[];
|
|
|
|
extern struct testcase_t addr_tests[];
|
|
|
|
extern struct testcase_t address_tests[];
|
2018-02-08 20:35:22 +01:00
|
|
|
extern struct testcase_t address_set_tests[];
|
2018-03-05 21:43:26 +01:00
|
|
|
extern struct testcase_t bridges_tests[];
|
2018-04-10 17:23:14 +02:00
|
|
|
extern struct testcase_t bwmgt_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t buffer_tests[];
|
|
|
|
extern struct testcase_t cell_format_tests[];
|
|
|
|
extern struct testcase_t cell_queue_tests[];
|
|
|
|
extern struct testcase_t channel_tests[];
|
2016-09-06 20:35:53 +02:00
|
|
|
extern struct testcase_t channelpadding_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t channeltls_tests[];
|
|
|
|
extern struct testcase_t checkdir_tests[];
|
2017-03-28 23:34:54 +02:00
|
|
|
extern struct testcase_t circuitbuild_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t circuitlist_tests[];
|
|
|
|
extern struct testcase_t circuitmux_tests[];
|
2016-10-14 14:00:35 +02:00
|
|
|
extern struct testcase_t circuituse_tests[];
|
2017-09-20 22:24:59 +02:00
|
|
|
extern struct testcase_t circuitstats_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t compat_libevent_tests[];
|
|
|
|
extern struct testcase_t config_tests[];
|
|
|
|
extern struct testcase_t connection_tests[];
|
2017-04-04 19:51:34 +02:00
|
|
|
extern struct testcase_t conscache_tests[];
|
2017-03-07 15:58:30 +01:00
|
|
|
extern struct testcase_t consdiff_tests[];
|
2017-04-14 18:35:02 +02:00
|
|
|
extern struct testcase_t consdiffmgr_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t container_tests[];
|
|
|
|
extern struct testcase_t controller_tests[];
|
|
|
|
extern struct testcase_t controller_event_tests[];
|
|
|
|
extern struct testcase_t crypto_tests[];
|
2017-03-25 12:04:11 +01:00
|
|
|
extern struct testcase_t crypto_openssl_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t dir_tests[];
|
|
|
|
extern struct testcase_t dir_handle_get_tests[];
|
2018-01-30 15:33:12 +01:00
|
|
|
extern struct testcase_t dos_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t entryconn_tests[];
|
|
|
|
extern struct testcase_t entrynodes_tests[];
|
|
|
|
extern struct testcase_t guardfraction_tests[];
|
|
|
|
extern struct testcase_t extorport_tests[];
|
2018-03-15 14:47:01 +01:00
|
|
|
extern struct testcase_t geoip_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t hs_tests[];
|
2016-08-10 22:40:06 +02:00
|
|
|
extern struct testcase_t hs_cache[];
|
2017-04-20 15:58:21 +02:00
|
|
|
extern struct testcase_t hs_cell_tests[];
|
2017-04-20 17:20:02 +02:00
|
|
|
extern struct testcase_t hs_common_tests[];
|
2017-01-13 17:20:31 +01:00
|
|
|
extern struct testcase_t hs_config_tests[];
|
2017-11-20 18:10:07 +01:00
|
|
|
extern struct testcase_t hs_control_tests[];
|
2016-03-15 19:18:03 +01:00
|
|
|
extern struct testcase_t hs_descriptor[];
|
2017-04-20 16:04:28 +02:00
|
|
|
extern struct testcase_t hs_ntor_tests[];
|
2016-09-05 18:03:30 +02:00
|
|
|
extern struct testcase_t hs_service_tests[];
|
2017-05-02 15:36:59 +02:00
|
|
|
extern struct testcase_t hs_client_tests[];
|
2016-09-05 18:03:30 +02:00
|
|
|
extern struct testcase_t hs_intropoint_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t introduce_tests[];
|
|
|
|
extern struct testcase_t keypin_tests[];
|
|
|
|
extern struct testcase_t link_handshake_tests[];
|
|
|
|
extern struct testcase_t logging_tests[];
|
2018-05-03 16:53:19 +02:00
|
|
|
extern struct testcase_t mainloop_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t microdesc_tests[];
|
|
|
|
extern struct testcase_t nodelist_tests[];
|
|
|
|
extern struct testcase_t oom_tests[];
|
2016-07-02 06:39:45 +02:00
|
|
|
extern struct testcase_t oos_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t options_tests[];
|
2018-04-18 20:50:07 +02:00
|
|
|
extern struct testcase_t periodic_event_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t policy_tests[];
|
|
|
|
extern struct testcase_t procmon_tests[];
|
2017-09-28 03:45:55 +02:00
|
|
|
extern struct testcase_t proto_http_tests[];
|
2017-09-27 15:09:00 +02:00
|
|
|
extern struct testcase_t proto_misc_tests[];
|
2016-08-10 01:11:47 +02:00
|
|
|
extern struct testcase_t protover_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t pt_tests[];
|
|
|
|
extern struct testcase_t relay_tests[];
|
|
|
|
extern struct testcase_t relaycell_tests[];
|
2018-03-17 16:23:05 +01:00
|
|
|
extern struct testcase_t relaycrypt_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t rend_cache_tests[];
|
|
|
|
extern struct testcase_t replaycache_tests[];
|
|
|
|
extern struct testcase_t router_tests[];
|
|
|
|
extern struct testcase_t routerkeys_tests[];
|
|
|
|
extern struct testcase_t routerlist_tests[];
|
|
|
|
extern struct testcase_t routerset_tests[];
|
|
|
|
extern struct testcase_t scheduler_tests[];
|
2017-03-10 19:22:01 +01:00
|
|
|
extern struct testcase_t storagedir_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t socks_tests[];
|
|
|
|
extern struct testcase_t status_tests[];
|
|
|
|
extern struct testcase_t thread_tests[];
|
|
|
|
extern struct testcase_t tortls_tests[];
|
|
|
|
extern struct testcase_t util_tests[];
|
|
|
|
extern struct testcase_t util_format_tests[];
|
|
|
|
extern struct testcase_t util_process_tests[];
|
2018-05-07 02:42:18 +02:00
|
|
|
extern struct testcase_t voting_schedule_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
extern struct testcase_t dns_tests[];
|
|
|
|
extern struct testcase_t handle_tests[];
|
2016-05-03 17:42:50 +02:00
|
|
|
extern struct testcase_t sr_tests[];
|
2016-06-02 15:46:12 +02:00
|
|
|
|
|
|
|
extern struct testcase_t slow_crypto_tests[];
|
|
|
|
extern struct testcase_t slow_util_tests[];
|
|
|
|
|
|
|
|
extern struct testgroup_t testgroups[];
|
|
|
|
|
|
|
|
extern const char AUTHORITY_CERT_1[];
|
|
|
|
extern const char AUTHORITY_SIGNKEY_1[];
|
|
|
|
extern const char AUTHORITY_SIGNKEY_A_DIGEST[];
|
|
|
|
extern const char AUTHORITY_SIGNKEY_A_DIGEST256[];
|
|
|
|
extern const char AUTHORITY_CERT_2[];
|
|
|
|
extern const char AUTHORITY_SIGNKEY_2[];
|
|
|
|
extern const char AUTHORITY_SIGNKEY_B_DIGEST[];
|
|
|
|
extern const char AUTHORITY_SIGNKEY_B_DIGEST256[];
|
|
|
|
extern const char AUTHORITY_CERT_3[];
|
|
|
|
extern const char AUTHORITY_SIGNKEY_3[];
|
|
|
|
extern const char AUTHORITY_SIGNKEY_C_DIGEST[];
|
|
|
|
extern const char AUTHORITY_SIGNKEY_C_DIGEST256[];
|
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(TOR_TEST_H) */
|