1 #ifndef TEST_COMMON_H
2 #define TEST_COMMON_H
3 
4 struct istream *test_istream_create(const char *data);
5 struct istream *test_istream_create_data(const void *data, size_t size);
6 void test_istream_set_size(struct istream *input, uoff_t size);
7 void test_istream_set_allow_eof(struct istream *input, bool allow);
8 void test_istream_set_max_buffer_size(struct istream *input, size_t size);
9 
10 struct ostream *test_ostream_create(buffer_t *output);
11 struct ostream *test_ostream_create_nonblocking(buffer_t *output,
12 						size_t max_internal_buffer_size);
13 /* When output->used reaches max_size, start buffering output internally.
14    When internal buffer reaches max_internal_buffer_size, start returning 0 for
15    o_stream_send*(). */
16 void test_ostream_set_max_output_size(struct ostream *output, size_t max_size);
17 
18 void test_begin(const char *name);
19 #define test_assert(code) STMT_START { \
20 	if (!(code)) test_assert_failed(#code, __FILE__, __LINE__); \
21 	} STMT_END
22 /* Additional parameter may be int or unsigned int, to indicate which of
23  * a barrage of tests have failed (such as in a loop).
24  */
25 #define test_assert_idx(code, i) STMT_START { \
26 		if (!(code)) test_assert_failed_idx(#code, __FILE__, __LINE__, i); \
27 	} STMT_END
28 /* Additional parameters are s1 (source) and s2 (destination) string
29  * in strcmp().
30  */
31 #define test_assert_strcmp(s1, s2) STMT_START { \
32 		test_assert_strcmp_idx(s1, s2, LLONG_MIN); \
33 	} STMT_END
34 
35 /* Same as test_assert_strcmp expect that it takes an additional i as input.
36  * When i is greater than or equals 0 it is used to identify the barrage of
37  * tests failed like in test_assert_idx.
38 */
39 #define test_assert_strcmp_idx(_s1, _s2, i) STMT_START { \
40 		const char *_temp_s1 = (_s1); \
41 		const char *_temp_s2 = (_s2); \
42 		if ((null_strcmp(_temp_s1,_temp_s2) != 0)) \
43 			test_assert_failed_strcmp_idx("strcmp(" #_s1 ","  #_s2 ")", \
44 						      __FILE__, __LINE__, _temp_s1, _temp_s2, i); \
45 	} STMT_END
46 
47 #define test_assert_cmp(_value1, _op, _value2) \
48 	test_assert_cmp_idx(_value1, _op, _value2, LLONG_MIN)
49 #define test_assert_cmp_idx(_value1, _op, _value2, _idx) STMT_START { \
50 		intmax_t _temp_value1 = (_value1); \
51 		intmax_t _temp_value2 = (_value2); \
52 		if (!(_value1 _op _value2)) \
53 			test_assert_failed_cmp_intmax_idx( \
54 				#_value1 " " #_op " " #_value2, \
55 				__FILE__, __LINE__, _temp_value1, _temp_value2, \
56 				#_op, _idx); \
57 	} STMT_END
58 
59 #define test_assert_ucmp(_value1, _op, _value2) \
60 	test_assert_ucmp_idx(_value1, _op, _value2, LLONG_MIN)
61 #define test_assert_ucmp_idx(_value1, _op, _value2, _idx) STMT_START { \
62 		uintmax_t _temp_value1 = (_value1); \
63 		uintmax_t _temp_value2 = (_value2); \
64 		if (!(_value1 _op _value2)) \
65 			test_assert_failed_ucmp_intmax_idx( \
66 				#_value1 " " #_op " " #_value2, \
67 				__FILE__, __LINE__, _temp_value1, _temp_value2, \
68 				#_op, _idx); \
69 	} STMT_END
70 
71 #ifdef STATIC_CHECKER
72 #  define ATTR_STATIC_CHECKER_NORETURN ATTR_NORETURN
73 #else
74 #  define ATTR_STATIC_CHECKER_NORETURN
75 #endif
76 
77 void test_assert_failed(const char *code, const char *file, unsigned int line)
78 	ATTR_STATIC_CHECKER_NORETURN;
79 void test_assert_failed_idx(const char *code, const char *file, unsigned int line, long long i)
80 	ATTR_STATIC_CHECKER_NORETURN;
81 void test_assert_failed_strcmp_idx(const char *code, const char *file, unsigned int line,
82 				   const char * src, const char * dst, long long i)
83 	ATTR_STATIC_CHECKER_NORETURN;
84 void test_assert_failed_cmp_intmax_idx(const char *code, const char *file,
85 				       unsigned int line,
86 				       intmax_t src, intmax_t dst,
87 				       const char *op, long long i)
88 	ATTR_STATIC_CHECKER_NORETURN;
89 void test_assert_failed_ucmp_intmax_idx(const char *code, const char *file,
90 					unsigned int line,
91 					uintmax_t src, uintmax_t dst,
92 					const char *op, long long i)
93 	ATTR_STATIC_CHECKER_NORETURN;
94 bool test_has_failed(void);
95 /* If you're testing nasty cases which you want to warn, surround the noisy op with these */
96 void test_expect_errors(unsigned int expected);
97 void test_expect_error_string(const char *substr); /* expect just 1 message matching the printf format */
98 void test_expect_error_string_n_times(const char *substr, unsigned int times); /* expect just n messages matching the printf format */
99 void test_expect_no_more_errors(void);
100 /* Note that test_expect_error{s,_string}() effectively begin with a check equivalent
101    to test_expect_no_more_errors(), so you don't need the latter explicitly if following
102    it with either of the former.*/
103 
104 void test_end(void);
105 
106 void test_out(const char *name, bool success);
107 void test_out_quiet(const char *name, bool success); /* only prints failures */
108 void test_out_reason(const char *name, bool success, const char *reason)
109 	ATTR_NULL(3);
110 
111 int test_run(void (*const test_functions[])(void)) ATTR_WARN_UNUSED_RESULT;
112 struct named_test {
113 	const char *name;
114 	void (*func)(void);
115 };
116 int test_run_named(const struct named_test tests[], const char *match) ATTR_WARN_UNUSED_RESULT;
117 
118 #define TEST_DECL(x) void x(void);
119 #define TEST_NAMELESS(x) x, /* Were you to want to use the X trick but not name the tests */
120 #define TEST_NAMED(x) { .name = #x , .func = x },
121 
122 enum fatal_test_state {
123 	FATAL_TEST_FINISHED, /* no more test stages, don't call again */
124 	FATAL_TEST_FAILURE,  /* single stage has failed, continue */
125 	FATAL_TEST_ABORT,    /* something's gone horrifically wrong */
126 };
127 /* The fatal function is called first with stage=0. After each call the stage
128    is increased by 1. The idea is that each stage would be running an
129    individual test that is supposed to crash. The function is called until
130    FATAL_TEST_FINISHED or FATAL_TEST_ABORT is returned. */
131 typedef enum fatal_test_state test_fatal_func_t(unsigned int stage);
132 
133 typedef void test_fatal_callback_t(void *context);
134 
135 struct named_fatal {
136 	const char *name;
137 	test_fatal_func_t *func;
138 };
139 int test_run_with_fatals(void (*const test_functions[])(void),
140 			 test_fatal_func_t *const fatal_functions[]);
141 int test_run_named_with_fatals(const char *match, const struct named_test tests[],
142 			       const struct named_fatal fatals[]);
143 
144 /* Require the Fatal/Panic string to match this or the fatal test fails. */
145 void test_expect_fatal_string(const char *substr);
146 /* Call the specified callback when a fatal is being triggered. This is mainly
147    intended to allow freeing memory so valgrind won't complain about memory
148    leaks. */
149 void test_fatal_set_callback(test_fatal_callback_t *callback, void *context);
150 #define test_fatal_set_callback(callback, context) \
151 	test_fatal_set_callback(1 ? (test_fatal_callback_t *)callback : \
152 		CALLBACK_TYPECHECK(callback, void (*)(typeof(context))), \
153 		context)
154 
155 #define FATAL_DECL(x) enum fatal_test_state x(unsigned int);
156 #define FATAL_NAMELESS(x) x, /* Were you to want to use the X trick but not name the tests */
157 #define FATAL_NAMED(x) { .name = #x , .func = x },
158 
159 /* If a child is forked within test context, but wants to operate outside it,
160    then this will avoid valgrind leak errors */
161 void test_forked_end(void);
162 /* If a fork() wants to exit(), then this will avoid valgrind leak errors */
163 void test_exit(int status) ATTR_NORETURN;
164 
165 #endif
166