6eb2c1593d2a0e3806a58db18adb8c379bfa27a9
[samba.git] / lib / torture / torture.h
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture UI functions
4
5    Copyright (C) Jelmer Vernooij 2006
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef __TORTURE_UI_H__
22 #define __TORTURE_UI_H__
23
24 struct torture_test;
25 struct torture_context;
26 struct torture_suite;
27 struct torture_tcase;
28 struct torture_results;
29
30 enum torture_result { 
31         TORTURE_OK=0, 
32         TORTURE_FAIL=1,
33         TORTURE_ERROR=2,
34         TORTURE_SKIP=3
35 };
36
37 enum torture_progress_whence {
38         TORTURE_PROGRESS_SET,
39         TORTURE_PROGRESS_CUR,
40         TORTURE_PROGRESS_POP,
41         TORTURE_PROGRESS_PUSH,
42 };
43
44 /* 
45  * These callbacks should be implemented by any backend that wishes 
46  * to listen to reports from the torture tests.
47  */
48 struct torture_ui_ops
49 {
50         void (*init) (struct torture_results *);
51         void (*comment) (struct torture_context *, const char *);
52         void (*warning) (struct torture_context *, const char *);
53         void (*suite_start) (struct torture_context *, struct torture_suite *);
54         void (*suite_finish) (struct torture_context *, struct torture_suite *);
55         void (*tcase_start) (struct torture_context *, struct torture_tcase *); 
56         void (*tcase_finish) (struct torture_context *, struct torture_tcase *);
57         void (*test_start) (struct torture_context *, 
58                                                 struct torture_tcase *,
59                                                 struct torture_test *);
60         void (*test_result) (struct torture_context *, 
61                                                  enum torture_result, const char *reason);
62         void (*progress) (struct torture_context *, int offset, enum torture_progress_whence whence);
63 };
64
65 void torture_ui_test_start(struct torture_context *context,
66                                                            struct torture_tcase *tcase,
67                                                            struct torture_test *test);
68
69 void torture_ui_test_result(struct torture_context *context,
70                                                                 enum torture_result result,
71                                                                 const char *comment);
72
73 /*
74  * Holds information about a specific run of the testsuite. 
75  * The data in this structure should be considered private to 
76  * the torture tests and should only be used directly by the torture 
77  * code and the ui backends.
78  *
79  * Torture tests should instead call the torture_*() macros and functions 
80  * specified below.
81  */
82
83 struct torture_context
84 {
85         struct torture_results *results;
86
87         char *active_testname;
88         struct torture_test *active_test;
89         struct torture_tcase *active_tcase;
90
91         enum torture_result last_result;
92         char *last_reason;
93
94         /** Directory used for temporary test data */
95         const char *outputdir;
96         
97         /** Event context */
98         struct tevent_context *ev;
99
100         /** Loadparm context (will go away in favor of torture_setting_ at some point) */
101         struct loadparm_context *lp_ctx;
102 };
103
104 struct torture_results
105 {
106         const struct torture_ui_ops *ui_ops;
107         void *ui_data;
108
109         /** Whether tests should avoid writing output to stdout */
110         bool quiet;
111
112         bool returncode;
113 };
114
115 /* 
116  * Describes a particular torture test
117  */
118 struct torture_test {
119         /** Short unique name for the test. */
120         const char *name;
121
122         /** Long description for the test. */
123         const char *description;
124
125         /** Whether this is a dangerous test 
126          * (can corrupt the remote servers data or bring it down). */
127         bool dangerous;
128
129         /** Function to call to run this test */
130         bool (*run) (struct torture_context *torture_ctx, 
131                                  struct torture_tcase *tcase,
132                                  struct torture_test *test);
133
134         struct torture_test *prev, *next;
135
136         /** Pointer to the actual test function. This is run by the 
137           * run() function above. */
138         void *fn;
139
140         /** Use data for this test */
141         const void *data;
142 };
143
144 /* 
145  * Describes a particular test case.
146  */
147 struct torture_tcase {
148     const char *name;
149         const char *description;
150         bool (*setup) (struct torture_context *tcase, void **data);
151         bool (*teardown) (struct torture_context *tcase, void *data); 
152         bool fixture_persistent;
153         void *data;
154         struct torture_test *tests;
155         struct torture_tcase *prev, *next;
156 };
157
158 struct torture_suite
159 {
160         const char *name;
161         const char *description;
162         struct torture_tcase *testcases;
163         struct torture_suite *children;
164
165         /* Pointers to siblings of this torture suite */
166         struct torture_suite *prev, *next;
167 };
168
169 /** Create a new torture suite */
170 struct torture_suite *torture_suite_create(TALLOC_CTX *mem_ctx,
171                 const char *name);
172
173 /** Change the setup and teardown functions for a testcase */
174 void torture_tcase_set_fixture(struct torture_tcase *tcase,
175                 bool (*setup) (struct torture_context *, void **),
176                 bool (*teardown) (struct torture_context *, void *));
177
178 /* Add another test to run for a particular testcase */
179 struct torture_test *torture_tcase_add_test_const(struct torture_tcase *tcase,
180                 const char *name,
181                 bool (*run) (struct torture_context *test,
182                         const void *tcase_data, const void *test_data),
183                 const void *test_data);
184
185 /* Add a testcase to a testsuite */
186 struct torture_tcase *torture_suite_add_tcase(struct torture_suite *suite,
187                                                          const char *name);
188
189 /* Convenience wrapper that adds a testcase against only one
190  * test will be run */
191 struct torture_tcase *torture_suite_add_simple_tcase_const(
192                 struct torture_suite *suite,
193                 const char *name,
194                 bool (*run) (struct torture_context *test,
195                         const void *test_data),
196                 const void *data);
197
198 /* Convenience function that adds a test which only
199  * gets the test case data */
200 struct torture_test *torture_tcase_add_simple_test_const(
201                 struct torture_tcase *tcase,
202                 const char *name,
203                 bool (*run) (struct torture_context *test,
204                         const void *tcase_data));
205
206 /* Convenience wrapper that adds a test that doesn't need any
207  * testcase data */
208 struct torture_tcase *torture_suite_add_simple_test(
209                 struct torture_suite *suite,
210                 const char *name,
211                 bool (*run) (struct torture_context *test));
212
213 /* Add a child testsuite to an existing testsuite */
214 bool torture_suite_add_suite(struct torture_suite *suite,
215                 struct torture_suite *child);
216
217 /* Run the specified testsuite recursively */
218 bool torture_run_suite(struct torture_context *context,
219                                            struct torture_suite *suite);
220
221 /* Run the specified testcase */
222 bool torture_run_tcase(struct torture_context *context,
223                                            struct torture_tcase *tcase);
224
225 /* Run the specified test */
226 bool torture_run_test(struct torture_context *context,
227                                           struct torture_tcase *tcase,
228                                           struct torture_test *test);
229
230 void torture_comment(struct torture_context *test, const char *comment, ...) PRINTF_ATTRIBUTE(2,3);
231 void torture_warning(struct torture_context *test, const char *comment, ...) PRINTF_ATTRIBUTE(2,3);
232 void torture_result(struct torture_context *test,
233                         enum torture_result, const char *reason, ...) PRINTF_ATTRIBUTE(3,4);
234
235 #define torture_assert(torture_ctx,expr,cmt) \
236         if (!(expr)) { \
237                 torture_result(torture_ctx, TORTURE_FAIL, __location__": Expression `%s' failed: %s", __STRING(expr), cmt); \
238                 return false; \
239         }
240
241 #define torture_assert_goto(torture_ctx,expr,ret,label,cmt) \
242         if (!(expr)) { \
243                 torture_result(torture_ctx, TORTURE_FAIL, __location__": Expression `%s' failed: %s", __STRING(expr), cmt); \
244                 ret = false; \
245                 goto label; \
246         }
247
248 #define torture_assert_werr_equal(torture_ctx, got, expected, cmt) \
249         do { WERROR __got = got, __expected = expected; \
250         if (!W_ERROR_EQUAL(__got, __expected)) { \
251                 torture_result(torture_ctx, TORTURE_FAIL, __location__": "#got" was %s, expected %s: %s", win_errstr(__got), win_errstr(__expected), cmt); \
252                 return false; \
253         } \
254         } while (0)
255
256 #define torture_assert_ntstatus_equal(torture_ctx,got,expected,cmt) \
257         do { NTSTATUS __got = got, __expected = expected; \
258         if (!NT_STATUS_EQUAL(__got, __expected)) { \
259                 torture_result(torture_ctx, TORTURE_FAIL, __location__": "#got" was %s, expected %s: %s", nt_errstr(__got), nt_errstr(__expected), cmt); \
260                 return false; \
261         }\
262         } while(0)
263
264 #define torture_assert_ntstatus_equal_goto(torture_ctx,got,expected,ret,label,cmt) \
265         do { NTSTATUS __got = got, __expected = expected; \
266         if (!NT_STATUS_EQUAL(__got, __expected)) { \
267                 torture_result(torture_ctx, TORTURE_FAIL, __location__": "#got" was %s, expected %s: %s", nt_errstr(__got), nt_errstr(__expected), cmt); \
268                 ret = false; \
269                 goto label; \
270         }\
271         } while(0)
272
273 #define torture_assert_ndr_err_equal(torture_ctx,got,expected,cmt) \
274         do { enum ndr_err_code __got = got, __expected = expected; \
275         if (__got != __expected) { \
276                 torture_result(torture_ctx, TORTURE_FAIL, __location__": "#got" was %d, expected %d (%s): %s", __got, __expected, __STRING(expected), cmt); \
277                 return false; \
278         }\
279         } while(0)
280
281 #define torture_assert_casestr_equal(torture_ctx,got,expected,cmt) \
282         do { const char *__got = (got), *__expected = (expected); \
283         if (!strequal(__got, __expected)) { \
284                 torture_result(torture_ctx, TORTURE_FAIL, __location__": "#got" was %s, expected %s: %s", __got, __expected, cmt); \
285                 return false; \
286         } \
287         } while(0)
288
289 #define torture_assert_str_equal(torture_ctx,got,expected,cmt)\
290         do { const char *__got = (got), *__expected = (expected); \
291         if (strcmp_safe(__got, __expected) != 0) { \
292                 torture_result(torture_ctx, TORTURE_FAIL, \
293                                            __location__": "#got" was %s, expected %s: %s", \
294                                            __got, __expected, cmt); \
295                 return false; \
296         } \
297         } while(0)
298
299 #define torture_assert_str_equal_goto(torture_ctx,got,expected,ret,label,cmt)\
300         do { const char *__got = (got), *__expected = (expected); \
301         if (strcmp_safe(__got, __expected) != 0) { \
302                 torture_result(torture_ctx, TORTURE_FAIL, \
303                                            __location__": "#got" was %s, expected %s: %s", \
304                                            __got, __expected, cmt); \
305                 ret = false; \
306                 goto label; \
307         } \
308         } while(0)
309
310 #define torture_assert_mem_equal(torture_ctx,got,expected,len,cmt)\
311         do { const void *__got = (got), *__expected = (expected); \
312         if (memcmp(__got, __expected, len) != 0) { \
313                 torture_result(torture_ctx, TORTURE_FAIL, \
314                                __location__": "#got" of len %d did not match "#expected": %s", (int)len, cmt); \
315                 return false; \
316         } \
317         } while(0)
318
319 #define torture_assert_data_blob_equal(torture_ctx,got,expected,cmt)\
320         do { const DATA_BLOB __got = (got), __expected = (expected); \
321         if (__got.length != __expected.length) { \
322                 torture_result(torture_ctx, TORTURE_FAIL, \
323                                __location__": "#got".len %d did not match "#expected" len %d: %s", \
324                                (int)__got.length, (int)__expected.length, cmt); \
325                 return false; \
326         } \
327         if (memcmp(__got.data, __expected.data, __got.length) != 0) { \
328                 torture_result(torture_ctx, TORTURE_FAIL, \
329                                __location__": "#got" of len %d did not match "#expected": %s", (int)__got.length, cmt); \
330                 return false; \
331         } \
332         } while(0)
333
334 #define torture_assert_file_contains_text(torture_ctx,filename,expected,cmt)\
335         do { \
336         char *__got; \
337         const char *__expected = (expected); \
338         size_t __size; \
339         __got = file_load(filename, &__size, 0, torture_ctx); \
340         if (__got == NULL) { \
341                 torture_result(torture_ctx, TORTURE_FAIL, \
342                                __location__": unable to open %s: %s\n", \
343                                filename, cmt); \
344                 return false; \
345         } \
346         \
347         if (strcmp_safe(__got, __expected) != 0) { \
348                 torture_result(torture_ctx, TORTURE_FAIL, \
349                         __location__": %s contained:\n%sExpected: %s%s\n", \
350                         filename, __got, __expected, cmt); \
351                 talloc_free(__got); \
352                 return false; \
353         } \
354         talloc_free(__got); \
355         } while(0)
356
357 #define torture_assert_file_contains(torture_ctx,filename,expected,cmt)\
358         do { const char *__got, *__expected = (expected); \
359         size_t __size; \
360         __got = file_load(filename, *size, 0, torture_ctx); \
361         if (strcmp_safe(__got, __expected) != 0) { \
362                 torture_result(torture_ctx, TORTURE_FAIL, \
363                                            __location__": %s contained:\n%sExpected: %s%s\n", \
364                                            __got, __expected, cmt); \
365                 talloc_free(__got); \
366                 return false; \
367         } \
368         talloc_free(__got); \
369         } while(0)
370
371 #define torture_assert_int_equal(torture_ctx,got,expected,cmt)\
372         do { int __got = (got), __expected = (expected); \
373         if (__got != __expected) { \
374                 torture_result(torture_ctx, TORTURE_FAIL, \
375                         __location__": "#got" was %d (0x%X), expected %d (0x%X): %s", \
376                         __got, __got, __expected, __expected, cmt); \
377                 return false; \
378         } \
379         } while(0)
380
381 #define torture_assert_int_equal_goto(torture_ctx,got,expected,ret,label,cmt)\
382         do { int __got = (got), __expected = (expected); \
383         if (__got != __expected) { \
384                 torture_result(torture_ctx, TORTURE_FAIL, \
385                         __location__": "#got" was %d (0x%X), expected %d (0x%X): %s", \
386                         __got, __got, __expected, __expected, cmt); \
387                 ret = false; \
388                 goto label; \
389         } \
390         } while(0)
391
392 #define torture_assert_u64_equal(torture_ctx,got,expected,cmt)\
393         do { uint64_t __got = (got), __expected = (expected); \
394         if (__got != __expected) { \
395                 torture_result(torture_ctx, TORTURE_FAIL, \
396                         __location__": "#got" was %llu (0x%llX), expected %llu (0x%llX): %s", \
397                         (unsigned long long)__got, (unsigned long long)__got, \
398                         (unsigned long long)__expected, (unsigned long long)__expected, \
399                         cmt); \
400                 return false; \
401         } \
402         } while(0)
403
404 #define torture_assert_errno_equal(torture_ctx,expected,cmt)\
405         do { int __expected = (expected); \
406         if (errno != __expected) { \
407                 torture_result(torture_ctx, TORTURE_FAIL, \
408                         __location__": errno was %d (%s), expected %d: %s: %s", \
409                                            errno, strerror(errno), __expected, \
410                                            strerror(__expected), cmt); \
411                 return false; \
412         } \
413         } while(0)
414
415
416
417 #define torture_skip(torture_ctx,cmt) do {\
418                 torture_result(torture_ctx, TORTURE_SKIP, __location__": %s", cmt);\
419                 return true; \
420         } while(0)
421 #define torture_skip_goto(torture_ctx,label,cmt) do {\
422                 torture_result(torture_ctx, TORTURE_SKIP, __location__": %s", cmt);\
423                 goto label; \
424         } while(0)
425 #define torture_fail(torture_ctx,cmt) do {\
426                 torture_result(torture_ctx, TORTURE_FAIL, __location__": %s", cmt);\
427                 return false; \
428         } while (0)
429 #define torture_fail_goto(torture_ctx,label,cmt) do {\
430                 torture_result(torture_ctx, TORTURE_FAIL, __location__": %s", cmt);\
431                 goto label; \
432         } while (0)
433
434 #define torture_out stderr
435
436 /* Convenience macros */
437 #define torture_assert_ntstatus_ok(torture_ctx,expr,cmt) \
438                 torture_assert_ntstatus_equal(torture_ctx,expr,NT_STATUS_OK,cmt)
439
440 #define torture_assert_ntstatus_ok_goto(torture_ctx,expr,ret,label,cmt) \
441                 torture_assert_ntstatus_equal_goto(torture_ctx,expr,NT_STATUS_OK,ret,label,cmt)
442
443 #define torture_assert_werr_ok(torture_ctx,expr,cmt) \
444                 torture_assert_werr_equal(torture_ctx,expr,WERR_OK,cmt)
445
446 #define torture_assert_ndr_success(torture_ctx,expr,cmt) \
447                 torture_assert_ndr_err_equal(torture_ctx,expr,NDR_ERR_SUCCESS,cmt)
448
449 /* Getting settings */
450 const char *torture_setting_string(struct torture_context *test, \
451                                                                    const char *name, 
452                                                                    const char *default_value);
453
454 int torture_setting_int(struct torture_context *test, 
455                                                 const char *name, 
456                                                 int default_value);
457
458 double torture_setting_double(struct torture_context *test, 
459                                                 const char *name, 
460                                                 double default_value);
461
462 bool torture_setting_bool(struct torture_context *test, 
463                                                   const char *name, 
464                                                   bool default_value);
465
466 struct torture_suite *torture_find_suite(struct torture_suite *parent, 
467                                                                                  const char *name);
468
469 unsigned long torture_setting_ulong(struct torture_context *test,
470                                     const char *name,
471                                     unsigned long default_value);
472
473 NTSTATUS torture_temp_dir(struct torture_context *tctx, 
474                                    const char *prefix, 
475                                    char **tempdir);
476
477 struct torture_test *torture_tcase_add_simple_test(struct torture_tcase *tcase,
478                 const char *name,
479                 bool (*run) (struct torture_context *test, void *tcase_data));
480
481
482 bool torture_suite_init_tcase(struct torture_suite *suite, 
483                               struct torture_tcase *tcase, 
484                               const char *name);
485 int torture_suite_children_count(const struct torture_suite *suite);
486
487 struct torture_context *torture_context_init(struct tevent_context *event_ctx, struct torture_results *results);
488
489 struct torture_results *torture_results_init(TALLOC_CTX *mem_ctx, const struct torture_ui_ops *ui_ops);
490
491 struct torture_context *torture_context_child(struct torture_context *tctx);
492
493 extern const struct torture_ui_ops torture_subunit_ui_ops;
494
495 #endif /* __TORTURE_UI_H__ */