summaryrefslogtreecommitdiff
path: root/source4/torture/ui.c
blob: 8e1a2a1c934ba838c1ba18db29a6c62f5cc55641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* 
   Unix SMB/CIFS implementation.
   SMB torture UI functions

   Copyright (C) Jelmer Vernooij 2006
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "torture/ui.h"
#include "dlinklist.h"

void torture_comment(struct torture_context *context, const char *comment, ...)
{
	va_list ap;
	char *tmp;

	if (!context->ui_ops->comment)
		return;

	va_start(ap, comment);
	tmp = talloc_vasprintf(context, comment, ap);
		
	context->ui_ops->comment(context, tmp);
	
	talloc_free(tmp);
}

void torture_fail(struct torture_context *context, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	context->last_reason = talloc_vasprintf(context, fmt, ap);
	va_end(ap);
	context->last_result = TORTURE_FAIL;
}

void torture_skip(struct torture_context *context, const char *fmt, ...)
{
	va_list ap;
	context->skipped++;

	va_start(ap, fmt);
	context->last_result = TORTURE_SKIP;
	context->last_reason = talloc_vasprintf(context, fmt, ap);
	va_end(ap);
}

struct torture_suite *torture_suite_create(TALLOC_CTX *ctx, const char *name)
{
	struct torture_suite *suite = talloc(ctx, struct torture_suite);

	suite->name = talloc_strdup(suite, name);
	suite->testcases = NULL;

	return suite;
}

void torture_tcase_set_fixture(struct torture_tcase *tcase, 
		BOOL (*setup) (struct torture_context *, void **),
		BOOL (*teardown) (struct torture_context *, void *))
{
	tcase->setup = setup;
	tcase->teardown = teardown;
}

struct torture_test *torture_tcase_add_test(struct torture_tcase *tcase, 
						const char *name, 
						BOOL (*run) (struct torture_context *, 
									 const void *tcase_data,
									 const void *test_data),
						const void *data)
{
	struct torture_test *test = talloc(tcase, struct torture_test);

	test->name = talloc_strdup(test, name);
	test->description = NULL;
	test->run = run;
	test->dangerous = False;
	test->data = data;

	DLIST_ADD(tcase->tests, test);

	return test;
}

struct torture_tcase *torture_suite_add_tcase(struct torture_suite *suite, 
							 const char *name)
{
	struct torture_tcase *tcase = talloc(suite, struct torture_tcase);

	tcase->name = talloc_strdup(tcase, name);
	tcase->description = NULL;
	tcase->setup = NULL;
	tcase->teardown = NULL;
	tcase->fixture_persistent = True;
	tcase->tests = NULL;

	DLIST_ADD(suite->testcases, tcase);

	return tcase;
}

BOOL torture_run_suite(struct torture_context *context, 
					   struct torture_suite *suite)
{
	BOOL ret = True;
	struct torture_tcase *tcase;

	if (context->ui_ops->suite_start)
		context->ui_ops->suite_start(context, suite);

	for (tcase = suite->testcases; tcase; tcase = tcase->next) {
		ret &= torture_run_tcase(context, tcase);
	}

	if (context->ui_ops->suite_finish)
		context->ui_ops->suite_finish(context, suite);
	
	return ret;
}

static BOOL internal_torture_run_test(struct torture_context *context, 
					  struct torture_tcase *tcase,
					  struct torture_test *test,
					  BOOL already_setup,
					  const void *tcase_data)
{
	BOOL ret;
	void *data = NULL;

	if (test->dangerous && !lp_parm_bool(-1, "torture", "dangerous", False)) {
		torture_skip(context, "disabled %s - enable dangerous tests to use", 
					 test->name);
		return True;
	}

	if (!already_setup && tcase->setup && !tcase->setup(context, &data))
		return False;

	context->active_tcase = tcase;
	context->active_test = test;

	if (context->ui_ops->test_start)
		context->ui_ops->test_start(context, tcase, test);

	context->last_reason = NULL;
	context->last_result = TORTURE_OK;

	ret = test->run(context, !already_setup?data:tcase_data, test->data);
	if (!ret) {
		context->last_reason = talloc_strdup(context, "...");
		context->last_result = TORTURE_FAIL;
	}

	if (context->ui_ops->test_result)
		context->ui_ops->test_result(context, context->last_result, 
									 context->last_reason);


	switch (context->last_result) {
		case TORTURE_SKIP: context->success++; break;
		case TORTURE_FAIL: context->failed++; break;
		case TORTURE_TODO: context->todo++; break;
		case TORTURE_OK: context->success++; break;
	}

	talloc_free(context->last_reason);

	context->active_test = NULL;
	context->active_tcase = NULL;

	if (!already_setup && tcase->teardown && !tcase->teardown(context, data))
		return False;

	return ret;
}

BOOL torture_run_tcase(struct torture_context *context, 
					   struct torture_tcase *tcase)
{
	BOOL ret = True;
	void *data = NULL;
	struct torture_test *test;

	context->active_tcase = tcase;
	if (context->ui_ops->tcase_start)
		context->ui_ops->tcase_start(context, tcase);

	if (tcase->fixture_persistent && tcase->setup 
		&& !tcase->setup(context, &data)) {
		ret = False;
		goto done;
	}

	for (test = tcase->tests; test; test = test->next) {
		ret &= internal_torture_run_test(context, tcase, test, 
				tcase->fixture_persistent,
				(tcase->setup?data:tcase->data));
	}

	if (tcase->fixture_persistent && tcase->teardown &&
		!tcase->teardown(context, data))
		ret = False;

done:
	context->active_tcase = NULL;

	if (context->ui_ops->tcase_finish)
		context->ui_ops->tcase_finish(context, tcase);

	return ret;
}

BOOL torture_run_test(struct torture_context *context, 
					  struct torture_tcase *tcase,
					  struct torture_test *test)
{
	return internal_torture_run_test(context, tcase, test, False, NULL);
}

const char *torture_setting(struct torture_context *test, const char *name, 
							const char *default_value)
{
	const char *ret = lp_parm_string(-1, "torture", name);

	if (ret == NULL)
		return default_value;

	return ret;
}

static BOOL simple_tcase_helper(struct torture_context *test, 
								const void *tcase_data,
								const void *test_data)
{
	BOOL (*run) (struct torture_context *, const void *) = test_data;

	return run(test, tcase_data);
}

struct torture_tcase *torture_suite_add_simple_tcase(
					struct torture_suite *suite, 
					const char *name,
					BOOL (*run) (struct torture_context *test, const void *),
					const void *data)
{
	struct torture_tcase *tcase;
	
	tcase = torture_suite_add_tcase(suite, name);
	tcase->data = data;

	torture_tcase_add_test(tcase, name, simple_tcase_helper, run);

	return tcase;
}

BOOL torture_teardown_free(struct torture_context *torture, void *data)
{
	return talloc_free(data);
}