Fix include paths to new location of libutil.
[kai/samba-autobuild/.git] / source4 / lib / torture / torture.c
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 #include "includes.h"
22 #include "torture/torture.h"
23 #include "../lib/util/dlinklist.h"
24 #include "param/param.h"
25 #include "system/filesys.h"
26
27 struct torture_context *torture_context_init(struct event_context *event_ctx, 
28                                              const struct torture_ui_ops *ui_ops)
29 {
30         struct torture_context *torture = talloc_zero(event_ctx, 
31                                                       struct torture_context);
32         torture->ui_ops = ui_ops;
33         torture->returncode = true;
34         torture->ev = event_ctx;
35
36         if (ui_ops->init)
37                 ui_ops->init(torture);
38
39         return torture;
40 }
41
42 /**
43  create a temporary directory.
44 */
45 _PUBLIC_ NTSTATUS torture_temp_dir(struct torture_context *tctx, 
46                                    const char *prefix, 
47                                    char **tempdir)
48 {
49         SMB_ASSERT(tctx->outputdir != NULL);
50
51         *tempdir = talloc_asprintf(tctx, "%s/%s.XXXXXX", tctx->outputdir, 
52                                    prefix);
53         NT_STATUS_HAVE_NO_MEMORY(*tempdir);
54
55         if (mkdtemp(*tempdir) == NULL) {
56                 return map_nt_error_from_unix(errno);
57         }
58
59         return NT_STATUS_OK;
60 }
61
62 void torture_comment(struct torture_context *context, const char *comment, ...)
63 {
64         va_list ap;
65         char *tmp;
66
67         if (!context->ui_ops->comment)
68                 return;
69
70         va_start(ap, comment);
71         tmp = talloc_vasprintf(context, comment, ap);
72                 
73         context->ui_ops->comment(context, tmp);
74         
75         talloc_free(tmp);
76 }
77
78 void torture_warning(struct torture_context *context, const char *comment, ...)
79 {
80         va_list ap;
81         char *tmp;
82
83         if (!context->ui_ops->warning)
84                 return;
85
86         va_start(ap, comment);
87         tmp = talloc_vasprintf(context, comment, ap);
88
89         context->ui_ops->warning(context, tmp);
90
91         talloc_free(tmp);
92 }
93
94 void torture_result(struct torture_context *context, 
95                     enum torture_result result, const char *fmt, ...)
96 {
97         va_list ap;
98
99         va_start(ap, fmt);
100
101         if (context->last_reason) {
102                 torture_warning(context, "%s", context->last_reason);
103                 talloc_free(context->last_reason);
104         }
105
106         context->last_result = result;
107         context->last_reason = talloc_vasprintf(context, fmt, ap);
108         va_end(ap);
109 }
110
111 struct torture_suite *torture_suite_create(TALLOC_CTX *ctx, const char *name)
112 {
113         struct torture_suite *suite = talloc_zero(ctx, struct torture_suite);
114
115         suite->name = talloc_strdup(suite, name);
116         suite->testcases = NULL;
117         suite->children = NULL;
118
119         return suite;
120 }
121
122 void torture_tcase_set_fixture(struct torture_tcase *tcase, 
123                 bool (*setup) (struct torture_context *, void **),
124                 bool (*teardown) (struct torture_context *, void *))
125 {
126         tcase->setup = setup;
127         tcase->teardown = teardown;
128 }
129
130 static bool wrap_test_with_testcase_const(struct torture_context *torture_ctx,
131                                     struct torture_tcase *tcase,
132                                     struct torture_test *test)
133 {
134         bool (*fn) (struct torture_context *,
135                     const void *tcase_data,
136                     const void *test_data);
137
138         fn = test->fn;
139
140         return fn(torture_ctx, tcase->data, test->data);
141 }
142
143 struct torture_test *torture_tcase_add_test_const(struct torture_tcase *tcase,
144                 const char *name,
145                 bool (*run) (struct torture_context *, const void *tcase_data,
146                         const void *test_data),
147                 const void *data)
148 {
149         struct torture_test *test = talloc(tcase, struct torture_test);
150
151         test->name = talloc_strdup(test, name);
152         test->description = NULL;
153         test->run = wrap_test_with_testcase_const;
154         test->fn = run;
155         test->dangerous = false;
156         test->data = data;
157
158         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
159
160         return test;
161 }
162
163
164 bool torture_suite_init_tcase(struct torture_suite *suite, 
165                               struct torture_tcase *tcase, 
166                               const char *name)
167 {
168         tcase->name = talloc_strdup(tcase, name);
169         tcase->description = NULL;
170         tcase->setup = NULL;
171         tcase->teardown = NULL;
172         tcase->fixture_persistent = true;
173         tcase->tests = NULL;
174
175         DLIST_ADD_END(suite->testcases, tcase, struct torture_tcase *);
176
177         return true;
178 }
179
180
181 struct torture_tcase *torture_suite_add_tcase(struct torture_suite *suite, 
182                                                          const char *name)
183 {
184         struct torture_tcase *tcase = talloc(suite, struct torture_tcase);
185
186         if (!torture_suite_init_tcase(suite, tcase, name))
187                 return NULL;
188
189         return tcase;
190 }
191
192 bool torture_run_suite(struct torture_context *context, 
193                        struct torture_suite *suite)
194 {
195         bool ret = true;
196         struct torture_tcase *tcase;
197         struct torture_suite *tsuite;
198         char *old_testname;
199
200         context->level++;
201         if (context->ui_ops->suite_start)
202                 context->ui_ops->suite_start(context, suite);
203
204         old_testname = context->active_testname;
205         if (old_testname != NULL)
206                 context->active_testname = talloc_asprintf(context, "%s-%s", 
207                                                            old_testname, suite->name);
208         else
209                 context->active_testname = talloc_strdup(context, suite->name);
210
211         for (tcase = suite->testcases; tcase; tcase = tcase->next) {
212                 ret &= torture_run_tcase(context, tcase);
213         }
214
215         for (tsuite = suite->children; tsuite; tsuite = tsuite->next) {
216                 ret &= torture_run_suite(context, tsuite);
217         }
218
219         talloc_free(context->active_testname);
220         context->active_testname = old_testname;
221
222         if (context->ui_ops->suite_finish)
223                 context->ui_ops->suite_finish(context, suite);
224
225         context->level--;
226         
227         return ret;
228 }
229
230 void torture_ui_test_start(struct torture_context *context, 
231                            struct torture_tcase *tcase, 
232                            struct torture_test *test)
233 {
234         if (context->ui_ops->test_start)
235                 context->ui_ops->test_start(context, tcase, test);
236 }
237
238 int str_list_match(const char *name, char **list)
239 {
240         int i, ret = 0;
241         if (list == NULL)
242                 return 0;
243
244         for (i = 0; list[i]; i++) {
245                 if (gen_fnmatch(list[i], name) == 0)
246                         ret++;
247         }
248         return ret;
249 }
250
251 void torture_ui_test_result(struct torture_context *context, 
252                             enum torture_result result,
253                             const char *comment)
254 {
255         if (context->ui_ops->test_result)
256                 context->ui_ops->test_result(context, result, comment);
257
258         if (result == TORTURE_ERROR || result == TORTURE_FAIL)
259                 context->returncode = false;
260 }
261
262 static bool internal_torture_run_test(struct torture_context *context, 
263                                           struct torture_tcase *tcase,
264                                           struct torture_test *test,
265                                           bool already_setup)
266 {
267         bool success;
268         char *old_testname;
269
270         if (tcase == NULL || strcmp(test->name, tcase->name) != 0) { 
271                 old_testname = context->active_testname;
272                 context->active_testname = talloc_asprintf(context, "%s-%s", old_testname, test->name);
273         }
274
275         context->active_tcase = tcase;
276         context->active_test = test;
277
278         torture_ui_test_start(context, tcase, test);
279
280         context->last_reason = NULL;
281         context->last_result = TORTURE_OK;
282
283         if (!already_setup && tcase->setup && 
284                 !tcase->setup(context, &(tcase->data))) {
285                 if (context->last_reason == NULL)
286                         context->last_reason = talloc_strdup(context, "Setup failure");
287                 context->last_result = TORTURE_ERROR;
288                 success = false;
289         } else if (test->dangerous && 
290             !torture_setting_bool(context, "dangerous", false)) {
291             context->last_result = TORTURE_SKIP;
292             context->last_reason = talloc_asprintf(context, 
293                 "disabled %s - enable dangerous tests to use", test->name);
294             success = true;
295         } else {
296             success = test->run(context, tcase, test);
297
298             if (!success && context->last_result == TORTURE_OK) {
299                     if (context->last_reason == NULL)
300                             context->last_reason = talloc_strdup(context, "Unknown error/failure");
301                     context->last_result = TORTURE_ERROR;
302             }
303         }
304
305         if (!already_setup && tcase->teardown && !tcase->teardown(context, tcase->data)) {
306                 if (context->last_reason == NULL)
307                     context->last_reason = talloc_strdup(context, "Setup failure");
308                 context->last_result = TORTURE_ERROR;
309                 success = false;
310         }
311
312         torture_ui_test_result(context, context->last_result, 
313                                context->last_reason);
314         
315         talloc_free(context->last_reason);
316
317         if (tcase == NULL || strcmp(test->name, tcase->name) != 0) { 
318                 talloc_free(context->active_testname);
319                 context->active_testname = old_testname;
320         }
321         context->active_test = NULL;
322         context->active_tcase = NULL;
323
324         return success;
325 }
326
327 bool torture_run_tcase(struct torture_context *context, 
328                        struct torture_tcase *tcase)
329 {
330         bool ret = true;
331         char *old_testname;
332         struct torture_test *test;
333
334         context->level++;
335
336         context->active_tcase = tcase;
337         if (context->ui_ops->tcase_start) 
338                 context->ui_ops->tcase_start(context, tcase);
339
340         if (tcase->fixture_persistent && tcase->setup 
341                 && !tcase->setup(context, &tcase->data)) {
342                 /* FIXME: Use torture ui ops for reporting this error */
343                 fprintf(stderr, "Setup failed: ");
344                 if (context->last_reason != NULL)
345                         fprintf(stderr, "%s", context->last_reason);
346                 fprintf(stderr, "\n");
347                 ret = false;
348                 goto done;
349         }
350
351         old_testname = context->active_testname;
352         context->active_testname = talloc_asprintf(context, "%s-%s", 
353                                                    old_testname, tcase->name);
354         for (test = tcase->tests; test; test = test->next) {
355                 ret &= internal_torture_run_test(context, tcase, test, 
356                                 tcase->fixture_persistent);
357         }
358         talloc_free(context->active_testname);
359         context->active_testname = old_testname;
360
361         if (tcase->fixture_persistent && tcase->teardown &&
362                 !tcase->teardown(context, tcase->data))
363                 ret = false;
364
365 done:
366         context->active_tcase = NULL;
367
368         if (context->ui_ops->tcase_finish)
369                 context->ui_ops->tcase_finish(context, tcase);
370
371         context->level--;
372
373         return ret;
374 }
375
376 bool torture_run_test(struct torture_context *context, 
377                                           struct torture_tcase *tcase,
378                                           struct torture_test *test)
379 {
380         return internal_torture_run_test(context, tcase, test, false);
381 }
382
383 int torture_setting_int(struct torture_context *test, const char *name, 
384                                                         int default_value)
385 {
386         return lp_parm_int(test->lp_ctx, NULL, "torture", name, default_value);
387 }
388
389 double torture_setting_double(struct torture_context *test, const char *name, 
390                                                         double default_value)
391 {
392         return lp_parm_double(test->lp_ctx, NULL, "torture", name, default_value);
393 }
394
395 bool torture_setting_bool(struct torture_context *test, const char *name, 
396                                                         bool default_value)
397 {
398         return lp_parm_bool(test->lp_ctx, NULL, "torture", name, default_value);
399 }
400
401 const char *torture_setting_string(struct torture_context *test, 
402                                    const char *name, 
403                                    const char *default_value)
404 {
405         const char *ret;
406
407         SMB_ASSERT(test != NULL);
408         SMB_ASSERT(test->lp_ctx != NULL);
409         
410         ret = lp_parm_string(test->lp_ctx, NULL, "torture", name);
411
412         if (ret == NULL)
413                 return default_value;
414
415         return ret;
416 }
417
418 static bool wrap_test_with_simple_tcase_const (
419                 struct torture_context *torture_ctx,
420                 struct torture_tcase *tcase,
421                 struct torture_test *test)
422 {
423         bool (*fn) (struct torture_context *, const void *tcase_data);
424
425         fn = test->fn;
426
427         return fn(torture_ctx, test->data);
428 }
429
430 struct torture_tcase *torture_suite_add_simple_tcase_const(
431                 struct torture_suite *suite, const char *name,
432                 bool (*run) (struct torture_context *test, const void *),
433                 const void *data)
434 {
435         struct torture_tcase *tcase;
436         struct torture_test *test;
437
438         tcase = torture_suite_add_tcase(suite, name);
439
440         test = talloc(tcase, struct torture_test);
441
442         test->name = talloc_strdup(test, name);
443         test->description = NULL;
444         test->run = wrap_test_with_simple_tcase_const;
445         test->fn = run;
446         test->data = data;
447         test->dangerous = false;
448
449         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
450
451         return tcase;
452 }
453
454 static bool wrap_simple_test(struct torture_context *torture_ctx,
455                              struct torture_tcase *tcase,
456                              struct torture_test *test)
457 {
458         bool (*fn) (struct torture_context *);
459
460         fn = test->fn;
461
462         return fn(torture_ctx);
463 }
464
465 struct torture_tcase *torture_suite_add_simple_test(
466                                         struct torture_suite *suite, 
467                                         const char *name,
468                                         bool (*run) (struct torture_context *test))
469 {
470         struct torture_test *test; 
471         struct torture_tcase *tcase;
472         
473         tcase = torture_suite_add_tcase(suite, name);
474
475         test = talloc(tcase, struct torture_test);
476
477         test->name = talloc_strdup(test, name);
478         test->description = NULL;
479         test->run = wrap_simple_test;
480         test->fn = run;
481         test->dangerous = false;
482
483         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
484
485         return tcase;
486 }
487
488 bool torture_suite_add_suite(struct torture_suite *suite, 
489                              struct torture_suite *child)
490 {
491         if (child == NULL)
492                 return false;
493
494         DLIST_ADD_END(suite->children, child, struct torture_suite *);
495
496         /* FIXME: Check for duplicates and return false if the 
497          * added suite already exists as a child */
498
499         return true;
500 }
501
502
503 struct torture_suite *torture_find_suite(struct torture_suite *parent, 
504                                          const char *name)
505 {
506         struct torture_suite *child;
507
508         for (child = parent->children; child; child = child->next) 
509                 if (!strcmp(child->name, name))
510                         return child;
511
512         return NULL;
513 }
514
515 static bool wrap_test_with_simple_test_const(struct torture_context *torture_ctx,
516                                        struct torture_tcase *tcase,
517                                        struct torture_test *test)
518 {
519         bool (*fn) (struct torture_context *, const void *tcase_data);
520
521         fn = test->fn;
522
523         return fn(torture_ctx, tcase->data);
524 }
525
526 struct torture_test *torture_tcase_add_simple_test_const(
527                 struct torture_tcase *tcase,
528                 const char *name,
529                 bool (*run) (struct torture_context *test,
530                         const void *tcase_data))
531 {
532         struct torture_test *test;
533
534         test = talloc(tcase, struct torture_test);
535
536         test->name = talloc_strdup(test, name);
537         test->description = NULL;
538         test->run = wrap_test_with_simple_test_const;
539         test->fn = run;
540         test->data = NULL;
541         test->dangerous = false;
542
543         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
544
545         return test;
546 }
547
548 static bool wrap_test_with_simple_test(struct torture_context *torture_ctx,
549                                        struct torture_tcase *tcase,
550                                        struct torture_test *test)
551 {
552         bool (*fn) (struct torture_context *, void *tcase_data);
553
554         fn = test->fn;
555
556         return fn(torture_ctx, tcase->data);
557 }
558
559 struct torture_test *torture_tcase_add_simple_test(struct torture_tcase *tcase,
560                 const char *name,
561                 bool (*run) (struct torture_context *test, void *tcase_data))
562 {
563         struct torture_test *test;
564
565         test = talloc(tcase, struct torture_test);
566
567         test->name = talloc_strdup(test, name);
568         test->description = NULL;
569         test->run = wrap_test_with_simple_test;
570         test->fn = run;
571         test->data = NULL;
572         test->dangerous = false;
573
574         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
575
576         return test;
577 }