s3: Remove unused get_sam_group_entries
[Samba.git] / lib / torture / torture.c
blob392cb0ad4c0ccebbdd12aa87c50c413a9890e241
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture UI functions
5 Copyright (C) Jelmer Vernooij 2006-2008
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.
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, see <http://www.gnu.org/licenses/>.
21 #include "source4/include/includes.h"
22 #include "../torture/torture.h"
23 #include "../lib/util/dlinklist.h"
24 #include "param/param.h"
25 #include "system/filesys.h"
27 struct torture_results *torture_results_init(TALLOC_CTX *mem_ctx, const struct torture_ui_ops *ui_ops)
29 struct torture_results *results = talloc_zero(mem_ctx, struct torture_results);
31 results->ui_ops = ui_ops;
32 results->returncode = true;
34 if (ui_ops->init)
35 ui_ops->init(results);
37 return results;
40 /**
41 * Initialize a torture context
43 struct torture_context *torture_context_init(struct tevent_context *event_ctx,
44 struct torture_results *results)
46 struct torture_context *torture = talloc_zero(event_ctx,
47 struct torture_context);
49 if (torture == NULL)
50 return NULL;
52 torture->ev = event_ctx;
53 torture->results = talloc_reference(torture, results);
55 return torture;
58 /**
59 * Create a sub torture context
61 struct torture_context *torture_context_child(struct torture_context *parent)
63 struct torture_context *subtorture = talloc_zero(parent, struct torture_context);
65 if (subtorture == NULL)
66 return NULL;
68 subtorture->ev = talloc_reference(subtorture, parent->ev);
69 subtorture->lp_ctx = talloc_reference(subtorture, parent->lp_ctx);
70 subtorture->outputdir = talloc_reference(subtorture, parent->outputdir);
71 subtorture->results = talloc_reference(subtorture, parent->results);
73 return subtorture;
76 /**
77 create a temporary directory.
79 _PUBLIC_ NTSTATUS torture_temp_dir(struct torture_context *tctx,
80 const char *prefix,
81 char **tempdir)
83 SMB_ASSERT(tctx->outputdir != NULL);
85 *tempdir = talloc_asprintf(tctx, "%s/%s.XXXXXX", tctx->outputdir,
86 prefix);
87 NT_STATUS_HAVE_NO_MEMORY(*tempdir);
89 if (mkdtemp(*tempdir) == NULL) {
90 return map_nt_error_from_unix(errno);
93 return NT_STATUS_OK;
96 /**
97 * Comment on the status/progress of a test
99 void torture_comment(struct torture_context *context, const char *comment, ...)
101 va_list ap;
102 char *tmp;
104 if (!context->results->ui_ops->comment)
105 return;
107 va_start(ap, comment);
108 tmp = talloc_vasprintf(context, comment, ap);
109 va_end(ap);
111 context->results->ui_ops->comment(context, tmp);
113 talloc_free(tmp);
117 * Print a warning about the current test
119 void torture_warning(struct torture_context *context, const char *comment, ...)
121 va_list ap;
122 char *tmp;
124 if (!context->results->ui_ops->warning)
125 return;
127 va_start(ap, comment);
128 tmp = talloc_vasprintf(context, comment, ap);
129 va_end(ap);
131 context->results->ui_ops->warning(context, tmp);
133 talloc_free(tmp);
137 * Store the result of a torture test.
139 void torture_result(struct torture_context *context,
140 enum torture_result result, const char *fmt, ...)
142 va_list ap;
144 va_start(ap, fmt);
146 if (context->last_reason) {
147 torture_warning(context, "%s", context->last_reason);
148 talloc_free(context->last_reason);
151 context->last_result = result;
152 context->last_reason = talloc_vasprintf(context, fmt, ap);
153 va_end(ap);
157 * Create a new torture suite
159 struct torture_suite *torture_suite_create(TALLOC_CTX *ctx, const char *name)
161 struct torture_suite *suite = talloc_zero(ctx, struct torture_suite);
163 suite->name = talloc_strdup(suite, name);
164 suite->testcases = NULL;
165 suite->children = NULL;
167 return suite;
171 * Set the setup() and teardown() functions for a testcase.
173 void torture_tcase_set_fixture(struct torture_tcase *tcase,
174 bool (*setup) (struct torture_context *, void **),
175 bool (*teardown) (struct torture_context *, void *))
177 tcase->setup = setup;
178 tcase->teardown = teardown;
181 static bool wrap_test_with_testcase_const(struct torture_context *torture_ctx,
182 struct torture_tcase *tcase,
183 struct torture_test *test)
185 bool (*fn) (struct torture_context *,
186 const void *tcase_data,
187 const void *test_data);
189 fn = test->fn;
191 return fn(torture_ctx, tcase->data, test->data);
195 * Add a test that uses const data to a testcase
197 struct torture_test *torture_tcase_add_test_const(struct torture_tcase *tcase,
198 const char *name,
199 bool (*run) (struct torture_context *, const void *tcase_data,
200 const void *test_data),
201 const void *data)
203 struct torture_test *test = talloc(tcase, struct torture_test);
205 test->name = talloc_strdup(test, name);
206 test->description = NULL;
207 test->run = wrap_test_with_testcase_const;
208 test->fn = run;
209 test->dangerous = false;
210 test->data = data;
212 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
214 return test;
218 * Add a new testcase
220 bool torture_suite_init_tcase(struct torture_suite *suite,
221 struct torture_tcase *tcase,
222 const char *name)
224 tcase->name = talloc_strdup(tcase, name);
225 tcase->description = NULL;
226 tcase->setup = NULL;
227 tcase->teardown = NULL;
228 tcase->fixture_persistent = true;
229 tcase->tests = NULL;
231 DLIST_ADD_END(suite->testcases, tcase, struct torture_tcase *);
233 return true;
237 struct torture_tcase *torture_suite_add_tcase(struct torture_suite *suite,
238 const char *name)
240 struct torture_tcase *tcase = talloc(suite, struct torture_tcase);
242 if (!torture_suite_init_tcase(suite, tcase, name))
243 return NULL;
245 return tcase;
249 * Run a torture test suite.
251 bool torture_run_suite(struct torture_context *context,
252 struct torture_suite *suite)
254 bool ret = true;
255 struct torture_tcase *tcase;
256 struct torture_suite *tsuite;
257 char *old_testname;
259 if (context->results->ui_ops->suite_start)
260 context->results->ui_ops->suite_start(context, suite);
262 old_testname = context->active_testname;
263 if (old_testname != NULL)
264 context->active_testname = talloc_asprintf(context, "%s-%s",
265 old_testname, suite->name);
266 else
267 context->active_testname = talloc_strdup(context, suite->name);
269 for (tcase = suite->testcases; tcase; tcase = tcase->next) {
270 ret &= torture_run_tcase(context, tcase);
273 for (tsuite = suite->children; tsuite; tsuite = tsuite->next) {
274 ret &= torture_run_suite(context, tsuite);
277 talloc_free(context->active_testname);
278 context->active_testname = old_testname;
280 if (context->results->ui_ops->suite_finish)
281 context->results->ui_ops->suite_finish(context, suite);
283 return ret;
286 void torture_ui_test_start(struct torture_context *context,
287 struct torture_tcase *tcase,
288 struct torture_test *test)
290 if (context->results->ui_ops->test_start)
291 context->results->ui_ops->test_start(context, tcase, test);
294 void torture_ui_test_result(struct torture_context *context,
295 enum torture_result result,
296 const char *comment)
298 if (context->results->ui_ops->test_result)
299 context->results->ui_ops->test_result(context, result, comment);
301 if (result == TORTURE_ERROR || result == TORTURE_FAIL)
302 context->results->returncode = false;
305 static bool internal_torture_run_test(struct torture_context *context,
306 struct torture_tcase *tcase,
307 struct torture_test *test,
308 bool already_setup)
310 bool success;
311 char *old_testname = NULL;
313 if (tcase == NULL || strcmp(test->name, tcase->name) != 0) {
314 old_testname = context->active_testname;
315 context->active_testname = talloc_asprintf(context, "%s-%s", old_testname, test->name);
318 context->active_tcase = tcase;
319 context->active_test = test;
321 torture_ui_test_start(context, tcase, test);
323 context->last_reason = NULL;
324 context->last_result = TORTURE_OK;
326 if (!already_setup && tcase->setup &&
327 !tcase->setup(context, &(tcase->data))) {
328 if (context->last_reason == NULL)
329 context->last_reason = talloc_strdup(context, "Setup failure");
330 context->last_result = TORTURE_ERROR;
331 success = false;
332 } else if (test->dangerous &&
333 !torture_setting_bool(context, "dangerous", false)) {
334 context->last_result = TORTURE_SKIP;
335 context->last_reason = talloc_asprintf(context,
336 "disabled %s - enable dangerous tests to use", test->name);
337 success = true;
338 } else {
339 success = test->run(context, tcase, test);
341 if (!success && context->last_result == TORTURE_OK) {
342 if (context->last_reason == NULL)
343 context->last_reason = talloc_strdup(context, "Unknown error/failure");
344 context->last_result = TORTURE_ERROR;
348 if (!already_setup && tcase->teardown && !tcase->teardown(context, tcase->data)) {
349 if (context->last_reason == NULL)
350 context->last_reason = talloc_strdup(context, "Setup failure");
351 context->last_result = TORTURE_ERROR;
352 success = false;
355 torture_ui_test_result(context, context->last_result,
356 context->last_reason);
358 talloc_free(context->last_reason);
360 if (tcase == NULL || strcmp(test->name, tcase->name) != 0) {
361 talloc_free(context->active_testname);
362 context->active_testname = old_testname;
364 context->active_test = NULL;
365 context->active_tcase = NULL;
367 return success;
370 bool torture_run_tcase(struct torture_context *context,
371 struct torture_tcase *tcase)
373 bool ret = true;
374 char *old_testname;
375 struct torture_test *test;
377 context->active_tcase = tcase;
378 if (context->results->ui_ops->tcase_start)
379 context->results->ui_ops->tcase_start(context, tcase);
381 if (tcase->fixture_persistent && tcase->setup
382 && !tcase->setup(context, &tcase->data)) {
383 /* FIXME: Use torture ui ops for reporting this error */
384 fprintf(stderr, "Setup failed: ");
385 if (context->last_reason != NULL)
386 fprintf(stderr, "%s", context->last_reason);
387 fprintf(stderr, "\n");
388 ret = false;
389 goto done;
392 old_testname = context->active_testname;
393 context->active_testname = talloc_asprintf(context, "%s-%s",
394 old_testname, tcase->name);
395 for (test = tcase->tests; test; test = test->next) {
396 ret &= internal_torture_run_test(context, tcase, test,
397 tcase->fixture_persistent);
399 talloc_free(context->active_testname);
400 context->active_testname = old_testname;
402 if (tcase->fixture_persistent && tcase->teardown &&
403 !tcase->teardown(context, tcase->data))
404 ret = false;
406 done:
407 context->active_tcase = NULL;
409 if (context->results->ui_ops->tcase_finish)
410 context->results->ui_ops->tcase_finish(context, tcase);
412 return ret;
415 bool torture_run_test(struct torture_context *context,
416 struct torture_tcase *tcase,
417 struct torture_test *test)
419 return internal_torture_run_test(context, tcase, test, false);
422 int torture_setting_int(struct torture_context *test, const char *name,
423 int default_value)
425 return lp_parm_int(test->lp_ctx, NULL, "torture", name, default_value);
428 double torture_setting_double(struct torture_context *test, const char *name,
429 double default_value)
431 return lp_parm_double(test->lp_ctx, NULL, "torture", name, default_value);
434 bool torture_setting_bool(struct torture_context *test, const char *name,
435 bool default_value)
437 return lp_parm_bool(test->lp_ctx, NULL, "torture", name, default_value);
440 const char *torture_setting_string(struct torture_context *test,
441 const char *name,
442 const char *default_value)
444 const char *ret;
446 SMB_ASSERT(test != NULL);
447 SMB_ASSERT(test->lp_ctx != NULL);
449 ret = lp_parm_string(test->lp_ctx, NULL, "torture", name);
451 if (ret == NULL)
452 return default_value;
454 return ret;
457 static bool wrap_test_with_simple_tcase_const (
458 struct torture_context *torture_ctx,
459 struct torture_tcase *tcase,
460 struct torture_test *test)
462 bool (*fn) (struct torture_context *, const void *tcase_data);
464 fn = test->fn;
466 return fn(torture_ctx, test->data);
469 struct torture_tcase *torture_suite_add_simple_tcase_const(
470 struct torture_suite *suite, const char *name,
471 bool (*run) (struct torture_context *test, const void *),
472 const void *data)
474 struct torture_tcase *tcase;
475 struct torture_test *test;
477 tcase = torture_suite_add_tcase(suite, name);
479 test = talloc(tcase, struct torture_test);
481 test->name = talloc_strdup(test, name);
482 test->description = NULL;
483 test->run = wrap_test_with_simple_tcase_const;
484 test->fn = run;
485 test->data = data;
486 test->dangerous = false;
488 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
490 return tcase;
493 static bool wrap_simple_test(struct torture_context *torture_ctx,
494 struct torture_tcase *tcase,
495 struct torture_test *test)
497 bool (*fn) (struct torture_context *);
499 fn = test->fn;
501 return fn(torture_ctx);
504 struct torture_tcase *torture_suite_add_simple_test(
505 struct torture_suite *suite,
506 const char *name,
507 bool (*run) (struct torture_context *test))
509 struct torture_test *test;
510 struct torture_tcase *tcase;
512 tcase = torture_suite_add_tcase(suite, name);
514 test = talloc(tcase, struct torture_test);
516 test->name = talloc_strdup(test, name);
517 test->description = NULL;
518 test->run = wrap_simple_test;
519 test->fn = run;
520 test->dangerous = false;
522 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
524 return tcase;
528 * Add a child testsuite to a testsuite.
530 bool torture_suite_add_suite(struct torture_suite *suite,
531 struct torture_suite *child)
533 if (child == NULL)
534 return false;
536 DLIST_ADD_END(suite->children, child, struct torture_suite *);
538 /* FIXME: Check for duplicates and return false if the
539 * added suite already exists as a child */
541 return true;
545 * Find the child testsuite with the specified name.
547 struct torture_suite *torture_find_suite(struct torture_suite *parent,
548 const char *name)
550 struct torture_suite *child;
552 for (child = parent->children; child; child = child->next)
553 if (!strcmp(child->name, name))
554 return child;
556 return NULL;
559 static bool wrap_test_with_simple_test_const(struct torture_context *torture_ctx,
560 struct torture_tcase *tcase,
561 struct torture_test *test)
563 bool (*fn) (struct torture_context *, const void *tcase_data);
565 fn = test->fn;
567 return fn(torture_ctx, tcase->data);
570 struct torture_test *torture_tcase_add_simple_test_const(
571 struct torture_tcase *tcase,
572 const char *name,
573 bool (*run) (struct torture_context *test,
574 const void *tcase_data))
576 struct torture_test *test;
578 test = talloc(tcase, struct torture_test);
580 test->name = talloc_strdup(test, name);
581 test->description = NULL;
582 test->run = wrap_test_with_simple_test_const;
583 test->fn = run;
584 test->data = NULL;
585 test->dangerous = false;
587 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
589 return test;
592 static bool wrap_test_with_simple_test(struct torture_context *torture_ctx,
593 struct torture_tcase *tcase,
594 struct torture_test *test)
596 bool (*fn) (struct torture_context *, void *tcase_data);
598 fn = test->fn;
600 return fn(torture_ctx, tcase->data);
603 struct torture_test *torture_tcase_add_simple_test(struct torture_tcase *tcase,
604 const char *name,
605 bool (*run) (struct torture_context *test, void *tcase_data))
607 struct torture_test *test;
609 test = talloc(tcase, struct torture_test);
611 test->name = talloc_strdup(test, name);
612 test->description = NULL;
613 test->run = wrap_test_with_simple_test;
614 test->fn = run;
615 test->data = NULL;
616 test->dangerous = false;
618 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
620 return test;