r16174: Couple of fixes to the UI code - make 'torture_ok()' optional, be more verbos...
[bbaumbach/samba-autobuild/.git] / source4 / torture / ui.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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "torture/ui.h"
24
25 static int test_destructor(void *_test)
26 {
27         struct torture_test *test = _test;
28
29         if (test->result == TORTURE_OK)
30                 torture_ok(test);
31
32         return 0;       
33 }
34
35
36 struct torture_test *torture_test(struct torture_context *ctx, const char *name, const char *description)
37 {
38         struct torture_test *test = talloc(ctx, struct torture_test);
39
40         test->name = talloc_strdup(test, name);
41         test->description = talloc_strdup(test, description);
42         test->context = ctx;
43
44         ctx->ui_ops->test_start(test);
45
46         talloc_set_destructor(test, test_destructor);
47
48         return test;
49 }
50
51 struct torture_test *torture_subtest(struct torture_test *parent, const char *name, const char *description)
52 {
53         struct torture_test *test = talloc(parent, struct torture_test);
54
55         test->name = talloc_strdup(test, name);
56         test->description = talloc_strdup(test, description);
57         test->context = parent->context;
58
59         test->context->ui_ops->test_start(test);
60
61         talloc_set_destructor(test, test_destructor);
62         
63         return test;
64 }
65
66 void torture_comment(struct torture_test *test, const char *comment, ...) _PRINTF_ATTRIBUTE(2,3)
67 {
68         va_list ap;
69         char *tmp;
70         va_start(ap, comment);
71         tmp = talloc_vasprintf(test, comment, ap);
72                 
73         test->context->ui_ops->comment(test, tmp);
74         
75         talloc_free(tmp);
76 }
77
78
79 void torture_ok(struct torture_test *test)
80 {
81         test->context->ui_ops->test_result(test, TORTURE_OK, NULL);
82         test->context->success++;
83 }
84
85 void torture_fail(struct torture_test *test, const char *fmt, ...) _PRINTF_ATTRIBUTE(2,3)
86 {
87         va_list ap;
88         char *reason;
89         va_start(ap, fmt);
90         reason = talloc_vasprintf(test, fmt, ap);
91         va_end(ap);
92         test->context->ui_ops->test_result(test, TORTURE_FAIL, reason);
93         talloc_free(reason);
94
95         test->context->failed++;
96 }
97
98 BOOL torture_result(struct torture_context *torture)
99 {
100         return (torture->failed == 0);
101 }
102
103 void torture_skip(struct torture_test *test, const char *fmt, ...) _PRINTF_ATTRIBUTE(2,3)
104 {
105         va_list ap;
106         char *reason;
107         va_start(ap, fmt);
108         reason = talloc_vasprintf(test, fmt, ap);
109         va_end(ap);
110         test->context->ui_ops->test_result(test, TORTURE_SKIP, reason);
111         talloc_free(reason);
112         test->context->skipped++;
113 }