4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_string_fns.h>
40 #include <cmdline_parse.h>
41 #include <cmdline_parse_string.h>
43 #include "test_cmdline.h"
45 /* structures needed to run tests */
47 struct string_elt_str {
48 const char * str; /* parsed string */
49 const char * result; /* expected string */
50 int idx; /* position at which result is expected to be */
53 struct string_elt_str string_elt_strs[] = {
54 {"one#two#three", "three", 2},
55 {"one#two with spaces#three", "three", 2},
56 {"one#two\twith\ttabs#three", "three", 2},
57 {"one#two\rwith\rreturns#three", "three", 2},
58 {"one#two\nwith\nnewlines#three", "three", 2},
59 {"one#two#three", "one", 0},
60 {"one#two#three", "two", 1},
61 {"one#two\0three", "two", 1},
62 {"one#two with spaces#three", "two with spaces", 1},
63 {"one#two\twith\ttabs#three", "two\twith\ttabs", 1},
64 {"one#two\rwith\rreturns#three", "two\rwith\rreturns", 1},
65 {"one#two\nwith\nnewlines#three", "two\nwith\nnewlines", 1},
68 #if CMDLINE_TEST_BUFSIZE < STR_TOKEN_SIZE
69 #undef CMDLINE_TEST_BUFSIZE
70 #define CMDLINE_TEST_BUFSIZE STR_TOKEN_SIZE
73 struct string_nb_str {
74 const char * str; /* parsed string */
75 int nb_strs; /* expected number of strings in str */
78 struct string_nb_str string_nb_strs[] = {
81 {"one# \t two \r # three \n #four", 4},
86 struct string_parse_str {
87 const char * str; /* parsed string */
88 const char * fixed_str; /* parsing mode (any, fixed or multi) */
89 const char * result; /* expected result */
92 struct string_parse_str string_parse_strs[] = {
93 {"one", NULL, "one"}, /* any string */
94 {"two", "one#two#three", "two"}, /* multiple choice string */
95 {"three", "three", "three"}, /* fixed string */
96 {"three", "one#two with\rgarbage\tcharacters\n#three", "three"},
97 {"two with\rgarbage\tcharacters\n",
98 "one#two with\rgarbage\tcharacters\n#three",
99 "two with\rgarbage\tcharacters\n"},
104 struct string_invalid_str {
105 const char * str; /* parsed string */
106 const char * fixed_str; /* parsing mode (any, fixed or multi) */
109 struct string_invalid_str string_invalid_strs[] = {
110 {"invalid", "one"}, /* fixed string */
111 {"invalid", "one#two#three"}, /* multiple choice string */
112 {"invalid", "invalidone"}, /* string that starts the same */
113 {"invalidone", "invalid"}, /* string that starts the same */
114 {"toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!"
115 "toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!"
116 "toolong!!!", NULL },
117 {"toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!"
118 "toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!"
119 "toolong!!!", "fixed" },
120 {"toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!"
121 "toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!"
122 "toolong!!!", "multi#choice#string" },
124 "toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!"
125 "toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!toolong!!!"
133 const char * string_help_strs[] = {
141 #define STRING_PARSE_STRS_SIZE \
142 (sizeof(string_parse_strs) / sizeof(string_parse_strs[0]))
143 #define STRING_HELP_STRS_SIZE \
144 (sizeof(string_help_strs) / sizeof(string_help_strs[0]))
145 #define STRING_ELT_STRS_SIZE \
146 (sizeof(string_elt_strs) / sizeof(string_elt_strs[0]))
147 #define STRING_NB_STRS_SIZE \
148 (sizeof(string_nb_strs) / sizeof(string_nb_strs[0]))
149 #define STRING_INVALID_STRS_SIZE \
150 (sizeof(string_invalid_strs) / sizeof(string_invalid_strs[0]))
154 /* test invalid parameters */
156 test_parse_string_invalid_param(void)
158 cmdline_parse_token_string_t token;
160 char buf[CMDLINE_TEST_BUFSIZE];
162 memset(&token, 0, sizeof(token));
164 snprintf(buf, sizeof(buf), "buffer");
166 /* test null token */
167 if (cmdline_get_help_string(
168 NULL, buf, 0) != -1) {
169 printf("Error: function accepted null token!\n");
172 if (cmdline_complete_get_elt_string(
173 NULL, 0, buf, 0) != -1) {
174 printf("Error: function accepted null token!\n");
177 if (cmdline_complete_get_nb_string(NULL) != -1) {
178 printf("Error: function accepted null token!\n");
181 if (cmdline_parse_string(NULL, buf, NULL, 0) != -1) {
182 printf("Error: function accepted null token!\n");
185 /* test null buffer */
186 if (cmdline_complete_get_elt_string(
187 (cmdline_parse_token_hdr_t*)&token, 0, NULL, 0) != -1) {
188 printf("Error: function accepted null buffer!\n");
191 if (cmdline_parse_string(
192 (cmdline_parse_token_hdr_t*)&token, NULL,
193 (void*)&result, sizeof(result)) != -1) {
194 printf("Error: function accepted null buffer!\n");
197 if (cmdline_get_help_string(
198 (cmdline_parse_token_hdr_t*)&token, NULL, 0) != -1) {
199 printf("Error: function accepted null buffer!\n");
202 /* test null result */
203 if (cmdline_parse_string(
204 (cmdline_parse_token_hdr_t*)&token, buf, NULL, 0) == -1) {
205 printf("Error: function rejected null result!\n");
208 /* test negative index */
209 if (cmdline_complete_get_elt_string(
210 (cmdline_parse_token_hdr_t*)&token, -1, buf, 0) != -1) {
211 printf("Error: function accepted negative index!\n");
217 /* test valid parameters but invalid data */
219 test_parse_string_invalid_data(void)
221 cmdline_parse_token_string_t token;
222 cmdline_parse_token_string_t help_token;
223 char buf[CMDLINE_TEST_BUFSIZE];
224 char help_str[CMDLINE_TEST_BUFSIZE];
225 char small_buf[SMALL_BUF];
228 /* test parsing invalid strings */
229 for (i = 0; i < STRING_INVALID_STRS_SIZE; i++) {
230 memset(&token, 0, sizeof(token));
231 memset(buf, 0, sizeof(buf));
233 /* prepare test token data */
234 token.string_data.str = string_invalid_strs[i].fixed_str;
236 if (cmdline_parse_string((cmdline_parse_token_hdr_t*)&token,
237 string_invalid_strs[i].str, (void*)buf,
238 sizeof(buf)) != -1) {
239 memset(help_str, 0, sizeof(help_str));
240 memset(&help_token, 0, sizeof(help_token));
242 help_token.string_data.str = string_invalid_strs[i].fixed_str;
244 /* get parse type so we can give a good error message */
245 cmdline_get_help_string((cmdline_parse_token_hdr_t*)&token, help_str,
248 printf("Error: parsing %s as %s succeeded!\n",
249 string_invalid_strs[i].str, help_str);
254 /* misc tests (big comments signify test cases) */
255 memset(&token, 0, sizeof(token));
256 memset(small_buf, 0, sizeof(small_buf));
259 * try to get element from a null token
261 token.string_data.str = NULL;
262 if (cmdline_complete_get_elt_string(
263 (cmdline_parse_token_hdr_t*)&token, 1,
264 buf, sizeof(buf)) != -1) {
265 printf("Error: getting token from null token string!\n");
270 * try to get element into a buffer that is too small
272 token.string_data.str = "too_small_buffer";
273 if (cmdline_complete_get_elt_string(
274 (cmdline_parse_token_hdr_t*)&token, 0,
275 small_buf, sizeof(small_buf)) != -1) {
276 printf("Error: writing token into too small a buffer succeeded!\n");
281 * get help string written into a buffer smaller than help string
282 * truncation should occur
284 token.string_data.str = NULL;
285 if (cmdline_get_help_string(
286 (cmdline_parse_token_hdr_t*)&token,
287 small_buf, sizeof(small_buf)) == -1) {
288 printf("Error: writing help string into too small a buffer failed!\n");
291 /* get help string for "any string" so we can compare it with small_buf */
292 cmdline_get_help_string((cmdline_parse_token_hdr_t*)&token, help_str,
294 if (strncmp(small_buf, help_str, sizeof(small_buf) - 1)) {
295 printf("Error: help string mismatch!\n");
298 /* check null terminator */
299 if (small_buf[sizeof(small_buf) - 1] != '\0') {
300 printf("Error: small buffer doesn't have a null terminator!\n");
305 * try to count tokens in a null token
307 token.string_data.str = NULL;
308 if (cmdline_complete_get_nb_string(
309 (cmdline_parse_token_hdr_t*)&token) != 0) {
310 printf("Error: getting token count from null token succeeded!\n");
317 /* test valid parameters and data */
319 test_parse_string_valid(void)
321 cmdline_parse_token_string_t token;
322 cmdline_parse_token_string_t help_token;
323 char buf[CMDLINE_TEST_BUFSIZE];
324 char help_str[CMDLINE_TEST_BUFSIZE];
327 /* test parsing strings */
328 for (i = 0; i < STRING_PARSE_STRS_SIZE; i++) {
329 memset(&token, 0, sizeof(token));
330 memset(buf, 0, sizeof(buf));
332 token.string_data.str = string_parse_strs[i].fixed_str;
334 if (cmdline_parse_string((cmdline_parse_token_hdr_t*)&token,
335 string_parse_strs[i].str, (void*)buf,
338 /* clean help data */
339 memset(&help_token, 0, sizeof(help_token));
340 memset(help_str, 0, sizeof(help_str));
342 /* prepare help token */
343 help_token.string_data.str = string_parse_strs[i].fixed_str;
345 /* get help string so that we get an informative error message */
346 cmdline_get_help_string((cmdline_parse_token_hdr_t*)&token, help_str,
349 printf("Error: parsing %s as %s failed!\n",
350 string_parse_strs[i].str, help_str);
353 if (strncmp(buf, string_parse_strs[i].result,
354 sizeof(string_parse_strs[i].result) - 1) != 0) {
355 printf("Error: result mismatch!\n");
360 /* get number of string tokens and verify it's correct */
361 for (i = 0; i < STRING_NB_STRS_SIZE; i++) {
362 memset(&token, 0, sizeof(token));
364 token.string_data.str = string_nb_strs[i].str;
366 if (cmdline_complete_get_nb_string(
367 (cmdline_parse_token_hdr_t*)&token) <
368 string_nb_strs[i].nb_strs) {
369 printf("Error: strings count mismatch!\n");
374 /* get token at specified position and verify it's correct */
375 for (i = 0; i < STRING_ELT_STRS_SIZE; i++) {
376 memset(&token, 0, sizeof(token));
377 memset(buf, 0, sizeof(buf));
379 token.string_data.str = string_elt_strs[i].str;
381 if (cmdline_complete_get_elt_string(
382 (cmdline_parse_token_hdr_t*)&token, string_elt_strs[i].idx,
383 buf, sizeof(buf)) < 0) {
384 printf("Error: getting string element failed!\n");
387 if (strncmp(buf, string_elt_strs[i].result,
389 printf("Error: result mismatch!\n");
394 /* cover all cases with help strings */
395 for (i = 0; i < STRING_HELP_STRS_SIZE; i++) {
396 memset(&help_token, 0, sizeof(help_token));
397 memset(help_str, 0, sizeof(help_str));
398 help_token.string_data.str = string_help_strs[i];
399 if (cmdline_get_help_string((cmdline_parse_token_hdr_t*)&help_token,
400 help_str, sizeof(help_str)) < 0) {
401 printf("Error: help operation failed!\n");