File: test_cmdline_lib.c

package info (click to toggle)
dpdk 25.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 127,892 kB
  • sloc: ansic: 2,358,479; python: 16,426; sh: 4,474; makefile: 1,713; awk: 70
file content (254 lines) | stat: -rw-r--r-- 5,466 bytes parent folder | download | duplicates (3)
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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2014 Intel Corporation
 */

#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <sys/queue.h>

#include <rte_common.h>

#include <cmdline_vt100.h>
#include <cmdline_rdline.h>
#include <cmdline_parse.h>
#include <cmdline_socket.h>
#include <cmdline.h>

#include "test_cmdline.h"

#ifndef RTE_EXEC_ENV_WINDOWS
#define NULL_INPUT "/dev/null"
#else
#define NULL_INPUT "NUL"
#endif

/****************************************************************/
/* static functions required for some tests */
static void
valid_buffer(__rte_unused struct rdline *rdl,
			__rte_unused const char *buf,
			__rte_unused unsigned int size)
{
}

static int
complete_buffer(__rte_unused struct rdline *rdl,
			__rte_unused const char *buf,
			__rte_unused char *dstbuf,
			__rte_unused unsigned int dstsize,
			__rte_unused int *state)
{
	return 0;
}

/****************************************************************/

static int
test_cmdline_parse_fns(void)
{
	struct cmdline *cl;
	cmdline_parse_ctx_t ctx;
	int i = 0;
	char dst[CMDLINE_TEST_BUFSIZE];

	cl = cmdline_new(&ctx, "prompt", -1, -1);
	if (cl == NULL) {
		printf("Error: cannot create cmdline to test parse fns!\n");
		return -1;
	}

	if (cmdline_parse(NULL, "buffer") >= 0)
		goto error;
	if (cmdline_parse(cl, NULL) >= 0)
		goto error;

	if (cmdline_complete(NULL, "buffer", &i, dst, sizeof(dst)) >= 0)
		goto error;
	if (cmdline_complete(cl, NULL, &i, dst, sizeof(dst)) >= 0)
		goto error;
	if (cmdline_complete(cl, "buffer", NULL, dst, sizeof(dst)) >= 0)
		goto error;
	if (cmdline_complete(cl, "buffer", &i, NULL, sizeof(dst)) >= 0)
		goto error;

	cmdline_free(cl);
	return 0;

error:
	printf("Error: function accepted null parameter!\n");
	cmdline_free(cl);
	return -1;
}

static int
test_cmdline_rdline_fns(void)
{
	struct rdline *rdl;
	rdline_write_char_t *wc = &cmdline_write_char;
	rdline_validate_t *v = &valid_buffer;
	rdline_complete_t *c = &complete_buffer;

	rdl = rdline_new(NULL, v, c, NULL);
	if (rdl != NULL)
		goto error;
	rdl = rdline_new(wc, NULL, c, NULL);
	if (rdl != NULL)
		goto error;
	rdl = rdline_new(wc, v, NULL, NULL);
	if (rdl != NULL)
		goto error;
	if (rdline_char_in(NULL, 0) >= 0)
		goto error;
	if (rdline_get_buffer(NULL) != NULL)
		goto error;
	if (rdline_add_history(NULL, "history") >= 0)
		goto error;
	if (rdline_add_history(rdl, NULL) >= 0)
		goto error;
	if (rdline_get_history_item(NULL, 0) != NULL)
		goto error;

	/* void functions */
	rdline_get_history_buffer_size(NULL);
	rdline_get_opaque(NULL);
	rdline_newline(NULL, "prompt");
	rdline_newline(rdl, NULL);
	rdline_stop(NULL);
	rdline_quit(NULL);
	rdline_restart(NULL);
	rdline_redisplay(NULL);
	rdline_reset(NULL);
	rdline_clear_history(NULL);
	rdline_free(NULL);

	rdline_free(rdl);
	return 0;

error:
	printf("Error: function accepted null parameter!\n");
	rdline_free(rdl);
	return -1;
}

static int
test_cmdline_vt100_fns(void)
{
	if (vt100_parser(NULL, 0) >= 0) {
		printf("Error: function accepted null parameter!\n");
		return -1;
	}

	/* void functions */
	vt100_init(NULL);

	return 0;
}

static int
test_cmdline_socket_fns(void)
{
	cmdline_parse_ctx_t ctx;
	struct cmdline *cl;

	cl = cmdline_stdin_new(NULL, "prompt");
	if (cl != NULL)
		goto error;
	cl = cmdline_stdin_new(&ctx, NULL);
	if (cl != NULL)
		goto error;
	cl = cmdline_file_new(NULL, "prompt", NULL_INPUT);
	if (cl != NULL)
		goto error;
	cl = cmdline_file_new(&ctx, NULL, NULL_INPUT);
	if (cl != NULL)
		goto error;
	cl = cmdline_file_new(&ctx, "prompt", NULL);
	if (cl != NULL)
		goto error;
	cl = cmdline_file_new(&ctx, "prompt", "-/invalid/~/path");
	if (cl != NULL) {
		printf("Error: succeeded in opening invalid file for reading!");
		cmdline_free(cl);
		return -1;
	}
	cl = cmdline_file_new(&ctx, "prompt", NULL_INPUT);
	if (cl == NULL) {
		printf("Error: failed to open /dev/null for reading!");
		return -1;
	}
	cmdline_free(cl);
	cl = NULL;

	/* void functions */
	cmdline_stdin_exit(NULL);

	return 0;
error:
	printf("Error: function accepted null parameter!\n");
	cmdline_free(cl);
	return -1;
}

static int
test_cmdline_fns(void)
{
	cmdline_parse_ctx_t ctx;
	struct cmdline *cl;

	memset(&ctx, 0, sizeof(ctx));
	cl = cmdline_new(NULL, "prompt", 0, 0);
	if (cl != NULL)
		goto error;
	cl = cmdline_new(&ctx, NULL, 0, 0);
	if (cl != NULL)
		goto error;
	cl = cmdline_new(&ctx, "test", -1, -1);
	if (cl == NULL)
		goto error;
	if (cmdline_in(NULL, "buffer", CMDLINE_TEST_BUFSIZE) >= 0)
		goto error;
	if (cmdline_in(cl, NULL, CMDLINE_TEST_BUFSIZE) >= 0)
		goto error;
	if (cmdline_write_char(NULL, 0) >= 0)
		goto error;

	/* void functions */
	cmdline_set_prompt(NULL, "prompt");
	cmdline_free(NULL);
	cmdline_printf(NULL, "format");
	cmdline_interact(NULL);
	cmdline_quit(NULL);

	cmdline_free(cl);
	return 0;

error:
	printf("Error: function accepted null parameter!\n");
	cmdline_free(cl);
	return -1;
}

/* test library functions. the point of these tests is not so much to test
 * functions' behaviour as it is to make sure there are no segfaults if
 * they are called with invalid parameters.
 */
int
test_cmdline_lib(void)
{
	if (test_cmdline_parse_fns() < 0)
		return -1;
	if (test_cmdline_rdline_fns() < 0)
		return -1;
	if (test_cmdline_vt100_fns() < 0)
		return -1;
	if (test_cmdline_socket_fns() < 0)
		return -1;
	if (test_cmdline_fns() < 0)
		return -1;
	return 0;
}