summaryrefslogtreecommitdiff
path: root/ctdb/tests/src/cmdline_test.c
blob: 72c34acd6542a758526021e320056c484c8b3af3 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
   Command line processing tests

   Copyright (C) Amitay Isaacs  2018

   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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"

#include <popt.h>
#include <talloc.h>

#include <assert.h>

#include "common/cmdline.c"

static int dummy_func(TALLOC_CTX *mem_ctx,
		      int argc,
		      const char **argv,
		      void *private_data)
{
	return 0;
}

static struct poptOption dummy_options[] = {
	POPT_TABLEEND
};

static struct cmdline_command dummy_commands[] = {
	CMDLINE_TABLEEND
};

static void test1(void)
{
	TALLOC_CTX *mem_ctx;
	struct cmdline_context *cmdline;
	int ret;

	mem_ctx = talloc_new(NULL);
	assert(mem_ctx != NULL);

	ret = cmdline_init(mem_ctx, NULL, NULL, NULL, NULL, &cmdline);
	assert(ret == EINVAL);

	ret = cmdline_init(mem_ctx, "test1", NULL, NULL, NULL, &cmdline);
	assert(ret == EINVAL);

	ret = cmdline_init(mem_ctx,
			   "test1",
			   dummy_options,
			   NULL,
			   NULL,
			   &cmdline);
	assert(ret == EINVAL);

	talloc_free(mem_ctx);
}

static struct cmdline_command test2_nofunc[] = {
	{ "nofunc", NULL, NULL, NULL },
	CMDLINE_TABLEEND
};

static struct cmdline_command test2_nohelp[] = {
	{ "nohelp", dummy_func, NULL, NULL },
	CMDLINE_TABLEEND
};

static struct cmdline_command test2_long[] = {
	{ "really really long command with lots of words",
	  dummy_func, "long command help",
	  "<and lots of really long long arguments>" },
	CMDLINE_TABLEEND
};

static struct cmdline_command test2_longhelp[] = {
	{ "longhelp", dummy_func,
	  "this is a really really really long help message" \
	  "with lots of words and lots of description",
	  NULL },
	CMDLINE_TABLEEND
};

static struct cmdline_command test2_twowords[] = {
	{ "multiple words", dummy_func, "multiple words help", NULL },
	CMDLINE_TABLEEND
};

static void test2(void)
{
	TALLOC_CTX *mem_ctx;
	struct cmdline_context *cmdline;
	int ret;

	mem_ctx = talloc_new(NULL);
	assert(mem_ctx != NULL);

	ret = cmdline_init(mem_ctx,
			   "test2",
			   NULL,
			   NULL,
			   test2_nofunc,
			   &cmdline);
	assert(ret == EINVAL);

	ret = cmdline_init(mem_ctx,
			   "test2",
			   NULL,
			   NULL,
			   test2_nohelp,
			   &cmdline);
	assert(ret == EINVAL);

	ret = cmdline_init(mem_ctx,
			   "test2",
			   NULL,
			   NULL,
			   test2_long,
			   &cmdline);
	assert(ret == EINVAL);

	ret = cmdline_init(mem_ctx,
			   "test2",
			   NULL,
			   NULL,
			   test2_longhelp,
			   &cmdline);
	assert(ret == EINVAL);

	ret = cmdline_init(mem_ctx,
			   "test2",
			   NULL,
			   NULL,
			   test2_twowords,
			   &cmdline);
	assert(ret == 0);

	talloc_free(mem_ctx);
}

struct {
	const char *str;
} test3_data;

static struct poptOption test3_noname[] = {
	{ NULL, 'o', POPT_ARG_STRING, &test3_data.str, 0,
	  "Noname option", NULL },
	POPT_TABLEEND
};

static struct poptOption test3_notype[] = {
	{ "debug", 'd', POPT_ARG_NONE, NULL, 0,
	  "No argument option", NULL },
	POPT_TABLEEND
};

static struct poptOption test3_noarg[] = {
	{ "debug", 'd', POPT_ARG_STRING, NULL, 0,
	  "No argument option", NULL },
	POPT_TABLEEND
};

static void test3(void)
{
	TALLOC_CTX *mem_ctx;
	struct cmdline_context *cmdline;
	int ret;

	mem_ctx = talloc_new(NULL);
	assert(mem_ctx != NULL);

	ret = cmdline_init(mem_ctx,
			   "test3",
			   test3_noname,
			   NULL,
			   dummy_commands,
			   &cmdline);
	assert(ret == EINVAL);

	ret = cmdline_init(mem_ctx,
			   "test3",
			   test3_notype,
			   NULL,
			   dummy_commands,
			   &cmdline);
	assert(ret == EINVAL);

	ret = cmdline_init(mem_ctx,
			   "test3",
			   test3_noarg,
			   NULL,
			   dummy_commands,
			   &cmdline);
	assert(ret == EINVAL);

	talloc_free(mem_ctx);
}

static int test4_count;
static int test4_value;

static struct poptOption test4_options[] = {
	{ "count", 'c', POPT_ARG_INT, &test4_count, 0,
	  "Option help of length thirty.", NULL },
	{ "value", 'v', POPT_ARG_INT, &test4_value, 0,
	  "Short description", "Value help of length 23" },
	POPT_TABLEEND
};

static struct cmdline_command test4_commands[] = {
	{ "A really really long command", dummy_func,
	  "This is a really long help message",
	  "<a long arguments message>" },
	{ "short command", dummy_func,
	  "short msg for short command", "<short arg msg>" },
	CMDLINE_TABLEEND
};

static void test4(void)
{
	TALLOC_CTX *mem_ctx;
	struct cmdline_context *cmdline;
	int ret;

	mem_ctx = talloc_new(NULL);
	assert(mem_ctx != NULL);

	ret = cmdline_init(mem_ctx,
			   "test4",
			   test4_options,
			   NULL,
			   test4_commands,
			   &cmdline);
	assert(ret == 0);

	cmdline_usage(cmdline, NULL);
	cmdline_usage(cmdline, "short command");

	talloc_free(mem_ctx);
}

static int action_func(TALLOC_CTX *mem_ctx,
		       int argc,
		       const char **argv,
		       void *private_data)
{
	if (argc != 1) {
		return 100;
	}

	printf("%s\n", argv[0]);
	return 200;
}

static struct cmdline_command action_commands[] = {
	{ "action one", dummy_func, "action one help", NULL },
	{ "action two", action_func, "action two help", NULL },
	CMDLINE_TABLEEND
};

static void test5(void)
{
	TALLOC_CTX *mem_ctx;
	struct cmdline_context *cmdline;
	const char *argv1[] = { "test5", "--help" };
	const char *argv2[] = { "test5", "action" };
	const char *argv3[] = { "test5", "action", "--help" };
	const char *argv4[] = { "test5", "action", "one" };
	int ret;

	mem_ctx = talloc_new(NULL);
	assert(mem_ctx != NULL);

	ret = cmdline_init(mem_ctx,
			   "test5",
			   NULL,
			   "Action",
			   action_commands,
			   &cmdline);
	assert(ret == 0);

	ret = cmdline_parse(cmdline, 2, argv1, true);
	assert(ret == EAGAIN);

	ret = cmdline_parse(cmdline, 2, argv2, true);
	assert(ret == ENOENT);

	ret = cmdline_parse(cmdline, 3, argv3, true);
	assert(ret == EAGAIN);

	ret = cmdline_parse(cmdline, 3, argv4, true);
	assert(ret == 0);

	talloc_free(mem_ctx);
}

static void test6(void)
{
	TALLOC_CTX *mem_ctx;
	struct cmdline_context *cmdline;
	const char *argv1[] = { "action", "two" };
	const char *argv2[] = { "action", "two", "arg1" };
	const char *argv3[] = { "action", "two", "arg1", "arg2" };
	int ret, result;

	mem_ctx = talloc_new(NULL);
	assert(mem_ctx != NULL);

	ret = cmdline_init(mem_ctx,
			   "test6",
			   NULL,
			   NULL,
			   action_commands,
			   &cmdline);
	assert(ret == 0);

	ret = cmdline_parse(cmdline, 2, argv1, false);
	assert(ret == 0);

	ret = cmdline_run(cmdline, NULL, &result);
	assert(ret == 0);
	assert(result == 100);

	ret = cmdline_parse(cmdline, 3, argv2, false);
	assert(ret == 0);

	ret = cmdline_run(cmdline, NULL, &result);
	assert(ret == 0);
	assert(result == 200);

	ret = cmdline_parse(cmdline, 4, argv3, false);
	assert(ret == 0);

	ret = cmdline_run(cmdline, NULL, &result);
	assert(ret == 0);
	assert(result == 100);

	talloc_free(mem_ctx);
}

int main(int argc, const char **argv)
{
	int num;

	if (argc < 2) {
		fprintf(stderr, "Usage %s <testnum>\n", argv[0]);
		exit(1);
	}

	num = atoi(argv[1]);

	switch (num) {
	case 1:
		test1();
		break;

	case 2:
		test2();
		break;

	case 3:
		test3();
		break;

	case 4:
		test4();
		break;

	case 5:
		test5();
		break;

	case 6:
		test6();
		break;
	}

	return 0;
}