r16527: Add target argument for smbtorture.
[ira/wip.git] / source / torture / ui.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 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 #ifndef __TORTURE_UI_H__
23 #define __TORTURE_UI_H__
24
25 struct torture_test;
26 struct torture_context;
27 struct torture_suite;
28 struct torture_tcase;
29
30 enum torture_result { 
31         TORTURE_OK=0, 
32         TORTURE_FAIL=1, 
33         TORTURE_TODO=2, 
34         TORTURE_SKIP=3
35 };
36
37 struct torture_ui_ops
38 {
39         void (*comment) (struct torture_context *, const char *);
40         void (*suite_start) (struct torture_context *, struct torture_suite *);
41         void (*suite_finish) (struct torture_context *, struct torture_suite *);
42         void (*tcase_start) (struct torture_context *, struct torture_tcase *); 
43         void (*tcase_finish) (struct torture_context *, struct torture_tcase *);
44         void (*test_start) (struct torture_context *, 
45                                                 struct torture_tcase *,
46                                                 struct torture_test *);
47         void (*test_result) (struct torture_context *, enum torture_result, 
48                                                  const char *reason);
49 };
50
51 struct torture_context
52 {
53         const struct torture_ui_ops *ui_ops;
54         void *ui_data;
55
56         struct torture_test *active_test;
57         struct torture_tcase *active_tcase;
58
59         int skipped;
60         int todo;
61         int success;
62         int failed;
63
64         enum torture_result last_result;
65         char *last_reason;
66
67         char *outputdir;
68 };
69
70 struct torture_suite
71 {
72         const char *name;
73         const char *description;
74         struct torture_tcase {
75             const char *name;
76                 const char *description;
77                 BOOL (*setup) (struct torture_context *tcase, void **data);
78                 BOOL (*teardown) (struct torture_context *tcase, void *data); 
79                 BOOL fixture_persistent;
80                 const void *data;
81                 struct torture_test {
82                         const char *name;
83                         const char *description;
84                         const void *data;
85                         BOOL dangerous;
86                         BOOL (*run) (struct torture_context *test, 
87                                                  const void *tcase_data,
88                                                  const void *test_data);
89                         struct torture_test *prev, *next;
90                 } *tests;
91                 struct torture_tcase *prev, *next;
92         } *testcases;
93 };
94
95 struct torture_suite *torture_suite_create(TALLOC_CTX *ctx, const char *name);
96 void torture_tcase_set_fixture(struct torture_tcase *tcase, 
97                 BOOL (*setup) (struct torture_context *, void **),
98                 BOOL (*teardown) (struct torture_context *, void *));
99 struct torture_test *torture_tcase_add_test(struct torture_tcase *tcase, 
100                 const char *name, 
101                 BOOL (*run) (struct torture_context *test, const void *tcase_data,
102                                          const void *test_data),
103                 const void *test_data);
104 struct torture_tcase *torture_suite_add_tcase(struct torture_suite *suite, 
105                                                          const char *name);
106 struct torture_tcase *torture_suite_add_simple_tcase(
107                 struct torture_suite *suite, 
108                 const char *name,
109                 BOOL (*run) (struct torture_context *test, const void *test_data),
110                 const void *data);
111
112 BOOL torture_run_suite(struct torture_context *context, 
113                                            struct torture_suite *suite);
114
115 BOOL torture_run_tcase(struct torture_context *context, 
116                                            struct torture_tcase *tcase);
117
118 BOOL torture_run_test(struct torture_context *context, 
119                                           struct torture_tcase *tcase,
120                                           struct torture_test *test);
121
122 #define torture_assert(ctx,expr,string) \
123         if (!(expr)) { \
124                 torture_fail(ctx, "%s:%d (%s): %s", __FILE__, __LINE__, string, \
125                                          __STRING(expr)); \
126                 return False; \
127         }
128
129 #define torture_assert_werr_equal(ctx,got,expected,string) \
130         do { WERROR __got = got, __expected = expected; \
131         if (!W_ERROR_EQUAL(__got, __expected)) { \
132                 torture_fail(ctx, "%s:%d (%s): got %s, expected %s", __FILE__, \
133                                          __LINE__, string, win_errstr(__got), win_errstr(__expected)); \
134                 return False; \
135         } \
136         } while (0)
137
138 #define torture_assert_ntstatus_equal(ctx,got,expected,string) \
139         do { NTSTATUS __got = got, __expected = expected; \
140         if (!NT_STATUS_EQUAL(__got, __expected)) { \
141                 torture_fail(ctx, "%s:%d (%s): got %s, expected %s", __FILE__, \
142                                          __LINE__, string, nt_errstr(__got), nt_errstr(__expected)); \
143                 return False; \
144         }\
145         } while(0)
146
147
148 #define torture_assert_casestr_equal(ctx,got,expected,string) \
149         do { const char *__got = got, __expected = expected; \
150         if (strcasecmp(__got, __expected) != 0) { \
151                 torture_fail(ctx, "%s:%d (%s): got %s, expected %s", __FILE__, \
152                                          __LINE__, string, got, expected); \
153                 return False; \
154         } \
155         } while(0)
156
157 #define torture_assert_str_equal(ctx,got,expected,string) \
158         do { const char *__got = got, __expected = expected; \
159         if (strcmp(__got, __expected) != 0) { \
160                 torture_fail(ctx, "%s:%d (%s): got %s, expected %s", __FILE__, \
161                                          __LINE__, string, __got, __expected); \
162                 return False; \
163         } \
164         } while(0)
165
166
167 /* Convenience macros */
168
169 #define torture_assert_ntstatus_ok(ctx,expr,string) \
170                 torture_assert_ntstatus_equal(ctx,expr,NT_STATUS_OK,string)
171
172 #define torture_assert_werr_ok(ctx,expr,string) \
173                 torture_assert_werr_equal(ctx,expr,WERR_OK,string)
174
175 void torture_comment(struct torture_context *test, const char *comment, ...) PRINTF_ATTRIBUTE(2,3);
176 void torture_fail(struct torture_context *test, const char *reason, ...) PRINTF_ATTRIBUTE(2,3);
177 void torture_skip(struct torture_context *test, const char *reason, ...) PRINTF_ATTRIBUTE(2,3);
178 const char *torture_setting(struct torture_context *test, const char *name, 
179                                                         const char *default_value);
180
181 /* Helper function commonly used */
182 BOOL torture_teardown_free(struct torture_context *torture, void *data);
183
184 #endif /* __TORTURE_UI_H__ */