2 Unix SMB/CIFS implementation.
3 SMB torture UI functions
5 Copyright (C) Jelmer Vernooij 2006
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "torture/ui.h"
24 #include "lib/util/dlinklist.h"
26 void torture_comment(struct torture_context *context,
27 const char *comment, ...)
32 if (!context->ui_ops->comment)
35 va_start(ap, comment);
36 tmp = talloc_vasprintf(context, comment, ap);
38 context->ui_ops->comment(context, tmp);
43 void _torture_fail_ext(struct torture_context *context,
49 context->last_reason = talloc_vasprintf(context, fmt, ap);
50 /* make sure the reason for the failure is displayed */
51 context->ui_ops->comment(context, context->last_reason);
53 context->last_result = TORTURE_FAIL;
56 void _torture_skip_ext(struct torture_context *context,
63 context->last_result = TORTURE_SKIP;
64 context->last_reason = talloc_vasprintf(context, fmt, ap);
68 struct torture_suite *torture_suite_create(TALLOC_CTX *ctx, const char *name)
70 struct torture_suite *suite = talloc(ctx, struct torture_suite);
72 suite->name = talloc_strdup(suite, name);
73 suite->testcases = NULL;
74 suite->children = NULL;
79 void torture_tcase_set_fixture(struct torture_tcase *tcase,
80 BOOL (*setup) (struct torture_context *, void **),
81 BOOL (*teardown) (struct torture_context *, void *))
84 tcase->teardown = teardown;
87 static bool wrap_test_with_testcase(struct torture_context *torture_ctx,
88 struct torture_tcase *tcase,
89 struct torture_test *test)
91 bool (*fn) (struct torture_context *,
92 const void *tcase_data,
93 const void *test_data);
97 return fn(torture_ctx, tcase->data, test->data);
100 struct torture_test *torture_tcase_add_test(struct torture_tcase *tcase,
102 bool (*run) (struct torture_context *,
103 const void *tcase_data,
104 const void *test_data),
107 struct torture_test *test = talloc(tcase, struct torture_test);
109 test->name = talloc_strdup(test, name);
110 test->description = NULL;
111 test->run = wrap_test_with_testcase;
113 test->dangerous = False;
116 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
121 struct torture_tcase *torture_suite_add_tcase(struct torture_suite *suite,
124 struct torture_tcase *tcase = talloc(suite, struct torture_tcase);
126 tcase->name = talloc_strdup(tcase, name);
127 tcase->description = NULL;
129 tcase->teardown = NULL;
130 tcase->fixture_persistent = True;
133 DLIST_ADD_END(suite->testcases, tcase, struct torture_tcase *);
138 BOOL torture_run_suite(struct torture_context *context,
139 struct torture_suite *suite)
142 struct torture_tcase *tcase;
143 struct torture_suite *tsuite;
146 if (context->ui_ops->suite_start)
147 context->ui_ops->suite_start(context, suite);
149 for (tcase = suite->testcases; tcase; tcase = tcase->next) {
150 ret &= torture_run_tcase(context, tcase);
153 for (tsuite = suite->children; tsuite; tsuite = tsuite->next) {
154 ret &= torture_run_suite(context, tsuite);
157 if (context->ui_ops->suite_finish)
158 context->ui_ops->suite_finish(context, suite);
165 static BOOL internal_torture_run_test(struct torture_context *context,
166 struct torture_tcase *tcase,
167 struct torture_test *test,
172 if (test->dangerous && !torture_setting_bool(context, "dangerous", False)) {
173 _torture_skip_ext(context,
174 "disabled %s - enable dangerous tests to use", test->name);
178 if (!already_setup && tcase->setup &&
179 !tcase->setup(context, &(tcase->data)))
182 context->active_tcase = tcase;
183 context->active_test = test;
185 if (context->ui_ops->test_start)
186 context->ui_ops->test_start(context, tcase, test);
188 context->last_reason = NULL;
189 context->last_result = TORTURE_OK;
191 ret = test->run(context, tcase, test);
193 if (context->last_reason == NULL)
194 context->last_reason = talloc_strdup(context, "...");
195 context->last_result = TORTURE_FAIL;
198 if (context->ui_ops->test_result)
199 context->ui_ops->test_result(context,
200 context->last_result,
201 context->last_reason);
204 switch (context->last_result) {
205 case TORTURE_SKIP: context->success++; break;
206 case TORTURE_FAIL: context->failed++; break;
207 case TORTURE_TODO: context->todo++; break;
208 case TORTURE_OK: context->success++; break;
211 talloc_free(context->last_reason);
213 context->active_test = NULL;
214 context->active_tcase = NULL;
216 if (!already_setup && tcase->teardown && !tcase->teardown(context, tcase->data))
222 BOOL torture_run_tcase(struct torture_context *context,
223 struct torture_tcase *tcase)
226 struct torture_test *test;
230 context->active_tcase = tcase;
231 if (context->ui_ops->tcase_start)
232 context->ui_ops->tcase_start(context, tcase);
234 if (tcase->fixture_persistent && tcase->setup
235 && !tcase->setup(context, &tcase->data)) {
240 for (test = tcase->tests; test; test = test->next) {
241 ret &= internal_torture_run_test(context, tcase, test,
242 tcase->fixture_persistent);
245 if (tcase->fixture_persistent && tcase->teardown &&
246 !tcase->teardown(context, tcase->data))
250 context->active_tcase = NULL;
252 if (context->ui_ops->tcase_finish)
253 context->ui_ops->tcase_finish(context, tcase);
260 BOOL torture_run_test(struct torture_context *context,
261 struct torture_tcase *tcase,
262 struct torture_test *test)
264 return internal_torture_run_test(context, tcase, test, False);
267 int torture_setting_int(struct torture_context *test, const char *name,
270 return lp_parm_int(-1, "torture", name, default_value);
273 bool torture_setting_bool(struct torture_context *test, const char *name,
276 return lp_parm_bool(-1, "torture", name, default_value);
279 const char *torture_setting_string(struct torture_context *test, const char *name,
280 const char *default_value)
282 const char *ret = lp_parm_string(-1, "torture", name);
285 return default_value;
290 static bool wrap_test_with_simple_tcase(struct torture_context *torture_ctx,
291 struct torture_tcase *tcase,
292 struct torture_test *test)
294 bool (*fn) (struct torture_context *, const void *tcase_data);
298 return fn(torture_ctx, test->data);
301 struct torture_tcase *torture_suite_add_simple_tcase(
302 struct torture_suite *suite,
304 bool (*run) (struct torture_context *test, const void *),
307 struct torture_tcase *tcase;
308 struct torture_test *test;
310 tcase = torture_suite_add_tcase(suite, name);
312 test = talloc(tcase, struct torture_test);
314 test->name = talloc_strdup(test, name);
315 test->description = NULL;
316 test->run = wrap_test_with_simple_tcase;
319 test->dangerous = False;
321 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
326 static bool wrap_simple_test(struct torture_context *torture_ctx,
327 struct torture_tcase *tcase,
328 struct torture_test *test)
330 bool (*fn) (struct torture_context *);
334 return fn(torture_ctx);
337 struct torture_tcase *torture_suite_add_simple_test(
338 struct torture_suite *suite,
340 bool (*run) (struct torture_context *test))
342 struct torture_test *test;
343 struct torture_tcase *tcase;
345 tcase = torture_suite_add_tcase(suite, name);
347 test = talloc(tcase, struct torture_test);
349 test->name = talloc_strdup(test, name);
350 test->description = NULL;
351 test->run = wrap_simple_test;
353 test->dangerous = false;
355 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
360 BOOL torture_teardown_free(struct torture_context *torture, void *data)
362 return talloc_free(data);
366 bool torture_suite_add_suite(struct torture_suite *suite,
367 struct torture_suite *child)
372 DLIST_ADD_END(suite->children, child, struct torture_suite *);
374 /* FIXME: Check for duplicates and return false if the
375 * added suite already exists as a child */
381 struct torture_suite *torture_find_suite(const char *name)