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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2014 Intel Corporation
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <rte_string_fns.h>
#include <cmdline_parse.h>
#include <cmdline_parse_num.h>
#include "test_cmdline.h"
struct num_unsigned_str {
const char * str;
uint64_t result;
};
struct num_signed_str {
const char * str;
int64_t result;
};
const struct num_unsigned_str num_valid_positive_strs[] = {
/* decimal positive */
{"0", 0 },
{"127", INT8_MAX },
{"128", INT8_MAX + 1 },
{"255", UINT8_MAX },
{"256", UINT8_MAX + 1 },
{"32767", INT16_MAX },
{"32768", INT16_MAX + 1 },
{"65535", UINT16_MAX },
{"65536", UINT16_MAX + 1 },
{"2147483647", INT32_MAX },
{"2147483648", INT32_MAX + 1U },
{"4294967295", UINT32_MAX },
{"4294967296", UINT32_MAX + 1ULL },
{"9223372036854775807", INT64_MAX },
{"9223372036854775808", INT64_MAX + 1ULL},
{"18446744073709551615", UINT64_MAX },
/* hexadecimal (no leading zeroes) */
{"0x0", 0 },
{"0x7F", INT8_MAX },
{"0x80", INT8_MAX + 1 },
{"0xFF", UINT8_MAX },
{"0x100", UINT8_MAX + 1 },
{"0x7FFF", INT16_MAX },
{"0x8000", INT16_MAX + 1 },
{"0xFFFF", UINT16_MAX },
{"0x10000", UINT16_MAX + 1 },
{"0x7FFFFFFF", INT32_MAX },
{"0x80000000", INT32_MAX + 1U },
{"0xFFFFFFFF", UINT32_MAX },
{"0x100000000", UINT32_MAX + 1ULL },
{"0x7FFFFFFFFFFFFFFF", INT64_MAX },
{"0x8000000000000000", INT64_MAX + 1ULL},
{"0xFFFFFFFFFFFFFFFF", UINT64_MAX },
/* hexadecimal (with leading zeroes) */
{"0x00", 0 },
{"0x7F", INT8_MAX },
{"0x80", INT8_MAX + 1 },
{"0xFF", UINT8_MAX },
{"0x0100", UINT8_MAX + 1 },
{"0x7FFF", INT16_MAX },
{"0x8000", INT16_MAX + 1 },
{"0xFFFF", UINT16_MAX },
{"0x00010000", UINT16_MAX + 1 },
{"0x7FFFFFFF", INT32_MAX },
{"0x80000000", INT32_MAX + 1U },
{"0xFFFFFFFF", UINT32_MAX },
{"0x0000000100000000", UINT32_MAX + 1ULL },
{"0x7FFFFFFFFFFFFFFF", INT64_MAX },
{"0x8000000000000000", INT64_MAX + 1ULL},
{"0xFFFFFFFFFFFFFFFF", UINT64_MAX },
/* check all characters */
{"0x1234567890ABCDEF", 0x1234567890ABCDEFULL },
{"0x1234567890abcdef", 0x1234567890ABCDEFULL },
/* binary (no leading zeroes) */
{"0b0", 0 },
{"0b1111111", INT8_MAX },
{"0b10000000", INT8_MAX + 1 },
{"0b11111111", UINT8_MAX },
{"0b100000000", UINT8_MAX + 1 },
{"0b111111111111111", INT16_MAX },
{"0b1000000000000000", INT16_MAX + 1 },
{"0b1111111111111111", UINT16_MAX },
{"0b10000000000000000", UINT16_MAX + 1 },
{"0b1111111111111111111111111111111", INT32_MAX },
{"0b10000000000000000000000000000000", INT32_MAX + 1U },
{"0b11111111111111111111111111111111", UINT32_MAX },
{"0b100000000000000000000000000000000", UINT32_MAX + 1ULL },
{"0b111111111111111111111111111111111111111111111111111111111111111",
INT64_MAX },
{"0b1000000000000000000000000000000000000000000000000000000000000000",
INT64_MAX + 1ULL},
{"0b1111111111111111111111111111111111111111111111111111111111111111",
UINT64_MAX },
/* binary (with leading zeroes) */
{"0b01111111", INT8_MAX },
{"0b0000000100000000", UINT8_MAX + 1 },
{"0b0111111111111111", INT16_MAX },
{"0b00000000000000010000000000000000", UINT16_MAX + 1 },
{"0b01111111111111111111111111111111", INT32_MAX },
{"0b0000000000000000000000000000000100000000000000000000000000000000",
UINT32_MAX + 1ULL },
{"0b0111111111111111111111111111111111111111111111111111111111111111",
INT64_MAX },
/* octal */
{"00", 0 },
{"0177", INT8_MAX },
{"0200", INT8_MAX + 1 },
{"0377", UINT8_MAX },
{"0400", UINT8_MAX + 1 },
{"077777", INT16_MAX },
{"0100000", INT16_MAX + 1 },
{"0177777", UINT16_MAX },
{"0200000", UINT16_MAX + 1 },
{"017777777777", INT32_MAX },
{"020000000000", INT32_MAX + 1U },
{"037777777777", UINT32_MAX },
{"040000000000", UINT32_MAX + 1ULL },
{"0777777777777777777777", INT64_MAX },
{"01000000000000000000000", INT64_MAX + 1ULL},
{"01777777777777777777777", UINT64_MAX },
/* check all numbers */
{"012345670", 012345670 },
{"076543210", 076543210 },
};
const struct num_signed_str num_valid_negative_strs[] = {
/* deciman negative */
{"-128", INT8_MIN },
{"-129", INT8_MIN - 1 },
{"-32768", INT16_MIN },
{"-32769", INT16_MIN - 1 },
{"-2147483648", INT32_MIN },
{"-2147483649", INT32_MIN - 1LL },
{"-9223372036854775808", INT64_MIN },
};
const struct num_unsigned_str num_garbage_positive_strs[] = {
/* valid strings with garbage on the end, should still be valid */
/* decimal */
{"9223372036854775807\0garbage", INT64_MAX },
{"9223372036854775807\tgarbage", INT64_MAX },
{"9223372036854775807\rgarbage", INT64_MAX },
{"9223372036854775807\ngarbage", INT64_MAX },
{"9223372036854775807#garbage", INT64_MAX },
{"9223372036854775807 garbage", INT64_MAX },
/* hex */
{"0x7FFFFFFFFFFFFFFF\0garbage", INT64_MAX },
{"0x7FFFFFFFFFFFFFFF\tgarbage", INT64_MAX },
{"0x7FFFFFFFFFFFFFFF\rgarbage", INT64_MAX },
{"0x7FFFFFFFFFFFFFFF\ngarbage", INT64_MAX },
{"0x7FFFFFFFFFFFFFFF#garbage", INT64_MAX },
{"0x7FFFFFFFFFFFFFFF garbage", INT64_MAX },
/* binary */
{"0b1111111111111111111111111111111\0garbage", INT32_MAX },
{"0b1111111111111111111111111111111\rgarbage", INT32_MAX },
{"0b1111111111111111111111111111111\tgarbage", INT32_MAX },
{"0b1111111111111111111111111111111\ngarbage", INT32_MAX },
{"0b1111111111111111111111111111111#garbage", INT32_MAX },
{"0b1111111111111111111111111111111 garbage", INT32_MAX },
/* octal */
{"01777777777777777777777\0garbage", UINT64_MAX },
{"01777777777777777777777\rgarbage", UINT64_MAX },
{"01777777777777777777777\tgarbage", UINT64_MAX },
{"01777777777777777777777\ngarbage", UINT64_MAX },
{"01777777777777777777777#garbage", UINT64_MAX },
{"01777777777777777777777 garbage", UINT64_MAX },
};
const struct num_signed_str num_garbage_negative_strs[] = {
/* valid strings with garbage on the end, should still be valid */
{"-9223372036854775808\0garbage", INT64_MIN },
{"-9223372036854775808\rgarbage", INT64_MIN },
{"-9223372036854775808\tgarbage", INT64_MIN },
{"-9223372036854775808\ngarbage", INT64_MIN },
{"-9223372036854775808#garbage", INT64_MIN },
{"-9223372036854775808 garbage", INT64_MIN },
};
const char * num_invalid_strs[] = {
"18446744073709551616", /* out of range unsigned */
"-9223372036854775809", /* out of range negative signed */
"0x10000000000000000", /* out of range hex */
/* out of range binary */
"0b10000000000000000000000000000000000000000000000000000000000000000",
"020000000000000000000000", /* out of range octal */
/* wrong chars */
"0123456239",
"0x1234580AGE",
"0b0111010101g001",
"0b01110101017001",
/* false negative numbers */
"-12345F623",
"-0x1234580A",
"-0b0111010101",
/* too long (128+ chars) */
("0b1111000011110000111100001111000011110000111100001111000011110000"
"1111000011110000111100001111000011110000111100001111000011110000"),
"1E3",
"0A",
"-B",
"+4",
"1.23G",
"",
" ",
"#",
"\r",
"\t",
"\n",
"\0",
};
static int
can_parse_unsigned(uint64_t expected_result, enum cmdline_numtype type)
{
switch (type) {
case RTE_UINT8:
if (expected_result > UINT8_MAX)
return 0;
break;
case RTE_UINT16:
if (expected_result > UINT16_MAX)
return 0;
break;
case RTE_UINT32:
if (expected_result > UINT32_MAX)
return 0;
break;
case RTE_INT8:
if (expected_result > INT8_MAX)
return 0;
break;
case RTE_INT16:
if (expected_result > INT16_MAX)
return 0;
break;
case RTE_INT32:
if (expected_result > INT32_MAX)
return 0;
break;
case RTE_INT64:
if (expected_result > INT64_MAX)
return 0;
break;
default:
return 1;
}
return 1;
}
static int
can_parse_signed(int64_t expected_result, enum cmdline_numtype type)
{
switch (type) {
case RTE_UINT8:
if (expected_result > UINT8_MAX || expected_result < 0)
return 0;
break;
case RTE_UINT16:
if (expected_result > UINT16_MAX || expected_result < 0)
return 0;
break;
case RTE_UINT32:
if (expected_result > UINT32_MAX || expected_result < 0)
return 0;
break;
case RTE_UINT64:
if (expected_result < 0)
return 0;
break;
case RTE_INT8:
if (expected_result > INT8_MAX || expected_result < INT8_MIN)
return 0;
break;
case RTE_INT16:
if (expected_result > INT16_MAX || expected_result < INT16_MIN)
return 0;
break;
case RTE_INT32:
if (expected_result > INT32_MAX || expected_result < INT32_MIN)
return 0;
break;
default:
return 1;
}
return 1;
}
/* test invalid parameters */
int
test_parse_num_invalid_param(void)
{
char buf[CMDLINE_TEST_BUFSIZE];
uint32_t result;
cmdline_parse_token_num_t token;
int ret = 0;
/* set up a token */
token.num_data.type = RTE_UINT32;
/* copy string to buffer */
strlcpy(buf, num_valid_positive_strs[0].str, sizeof(buf));
/* try all null */
ret = cmdline_parse_num(NULL, NULL, NULL, 0);
if (ret != -1) {
printf("Error: parser accepted null parameters!\n");
return -1;
}
/* try null token */
ret = cmdline_parse_num(NULL, buf, (void*)&result, sizeof(result));
if (ret != -1) {
printf("Error: parser accepted null token!\n");
return -1;
}
/* try null buf */
ret = cmdline_parse_num((cmdline_parse_token_hdr_t*)&token, NULL,
(void*)&result, sizeof(result));
if (ret != -1) {
printf("Error: parser accepted null string!\n");
return -1;
}
/* try null result */
ret = cmdline_parse_num((cmdline_parse_token_hdr_t*)&token, buf,
NULL, 0);
if (ret == -1) {
printf("Error: parser rejected null result!\n");
return -1;
}
/* test help function */
memset(&buf, 0, sizeof(buf));
/* try all null */
ret = cmdline_get_help_num(NULL, NULL, 0);
if (ret != -1) {
printf("Error: help function accepted null parameters!\n");
return -1;
}
/* try null token */
ret = cmdline_get_help_num(NULL, buf, sizeof(buf));
if (ret != -1) {
printf("Error: help function accepted null token!\n");
return -1;
}
/* coverage! */
ret = cmdline_get_help_num((cmdline_parse_token_hdr_t*)&token, buf, sizeof(buf));
if (ret < 0) {
printf("Error: help function failed with valid parameters!\n");
return -1;
}
return 0;
}
/* test valid parameters but invalid data */
int
test_parse_num_invalid_data(void)
{
enum cmdline_numtype type;
int ret = 0;
unsigned i;
char buf[CMDLINE_TEST_BUFSIZE];
uint64_t result; /* pick largest buffer */
cmdline_parse_token_num_t token;
/* cycle through all possible parsed types */
for (type = RTE_UINT8; type <= RTE_INT64; type++) {
token.num_data.type = type;
/* test full strings */
for (i = 0; i < RTE_DIM(num_invalid_strs); i++) {
memset(&result, 0, sizeof(uint64_t));
memset(&buf, 0, sizeof(buf));
ret = cmdline_parse_num((cmdline_parse_token_hdr_t*)&token,
num_invalid_strs[i], (void*)&result, sizeof(result));
if (ret != -1) {
/* get some info about what we are trying to parse */
cmdline_get_help_num((cmdline_parse_token_hdr_t*)&token,
buf, sizeof(buf));
printf("Error: parsing %s as %s succeeded!\n",
num_invalid_strs[i], buf);
return -1;
}
}
}
return 0;
}
/* test valid parameters and data */
int
test_parse_num_valid(void)
{
int ret = 0;
enum cmdline_numtype type;
unsigned i;
char buf[CMDLINE_TEST_BUFSIZE];
uint64_t result;
cmdline_parse_token_num_t token;
/** valid strings **/
/* cycle through all possible parsed types */
for (type = RTE_UINT8; type <= RTE_INT64; type++) {
token.num_data.type = type;
/* test positive strings */
for (i = 0; i < RTE_DIM(num_valid_positive_strs); i++) {
result = 0;
memset(&buf, 0, sizeof(buf));
cmdline_get_help_num((cmdline_parse_token_hdr_t*)&token,
buf, sizeof(buf));
ret = cmdline_parse_num((cmdline_parse_token_hdr_t*) &token,
num_valid_positive_strs[i].str,
(void*)&result, sizeof(result));
/* if it should have passed but didn't, or if it should have failed but didn't */
if ((ret < 0) == (can_parse_unsigned(num_valid_positive_strs[i].result, type) > 0)) {
printf("Error: parser behaves unexpectedly when parsing %s as %s!\n",
num_valid_positive_strs[i].str, buf);
return -1;
}
/* check if result matches what it should have matched
* since unsigned numbers don't care about number of bits, we can just convert
* everything to uint64_t without any worries. */
if (ret > 0 && num_valid_positive_strs[i].result != result) {
printf("Error: parsing %s as %s failed: result mismatch!\n",
num_valid_positive_strs[i].str, buf);
return -1;
}
}
/* test negative strings */
for (i = 0; i < RTE_DIM(num_valid_negative_strs); i++) {
result = 0;
memset(&buf, 0, sizeof(buf));
cmdline_get_help_num((cmdline_parse_token_hdr_t*)&token,
buf, sizeof(buf));
ret = cmdline_parse_num((cmdline_parse_token_hdr_t*) &token,
num_valid_negative_strs[i].str,
(void*)&result, sizeof(result));
/* if it should have passed but didn't, or if it should have failed but didn't */
if ((ret < 0) == (can_parse_signed(num_valid_negative_strs[i].result, type) > 0)) {
printf("Error: parser behaves unexpectedly when parsing %s as %s!\n",
num_valid_negative_strs[i].str, buf);
return -1;
}
/* check if result matches what it should have matched
* the result is signed in this case, so we have to account for that */
if (ret > 0) {
/* detect negative */
switch (type) {
case RTE_INT8:
result = (int8_t) result;
break;
case RTE_INT16:
result = (int16_t) result;
break;
case RTE_INT32:
result = (int32_t) result;
break;
default:
break;
}
if (num_valid_negative_strs[i].result == (int64_t) result)
continue;
printf("Error: parsing %s as %s failed: result mismatch!\n",
num_valid_negative_strs[i].str, buf);
return -1;
}
}
}
/** garbage strings **/
/* cycle through all possible parsed types */
for (type = RTE_UINT8; type <= RTE_INT64; type++) {
token.num_data.type = type;
/* test positive garbage strings */
for (i = 0; i < RTE_DIM(num_garbage_positive_strs); i++) {
result = 0;
memset(&buf, 0, sizeof(buf));
cmdline_get_help_num((cmdline_parse_token_hdr_t*)&token,
buf, sizeof(buf));
ret = cmdline_parse_num((cmdline_parse_token_hdr_t*) &token,
num_garbage_positive_strs[i].str,
(void*)&result, sizeof(result));
/* if it should have passed but didn't, or if it should have failed but didn't */
if ((ret < 0) == (can_parse_unsigned(num_garbage_positive_strs[i].result, type) > 0)) {
printf("Error: parser behaves unexpectedly when parsing %s as %s!\n",
num_garbage_positive_strs[i].str, buf);
return -1;
}
/* check if result matches what it should have matched
* since unsigned numbers don't care about number of bits, we can just convert
* everything to uint64_t without any worries. */
if (ret > 0 && num_garbage_positive_strs[i].result != result) {
printf("Error: parsing %s as %s failed: result mismatch!\n",
num_garbage_positive_strs[i].str, buf);
return -1;
}
}
/* test negative strings */
for (i = 0; i < RTE_DIM(num_garbage_negative_strs); i++) {
result = 0;
memset(&buf, 0, sizeof(buf));
cmdline_get_help_num((cmdline_parse_token_hdr_t*)&token,
buf, sizeof(buf));
ret = cmdline_parse_num((cmdline_parse_token_hdr_t*) &token,
num_garbage_negative_strs[i].str,
(void*)&result, sizeof(result));
/* if it should have passed but didn't, or if it should have failed but didn't */
if ((ret < 0) == (can_parse_signed(num_garbage_negative_strs[i].result, type) > 0)) {
printf("Error: parser behaves unexpectedly when parsing %s as %s!\n",
num_garbage_negative_strs[i].str, buf);
return -1;
}
/* check if result matches what it should have matched
* the result is signed in this case, so we have to account for that */
if (ret > 0) {
/* detect negative */
switch (type) {
case RTE_INT8:
if (result & (INT8_MAX + 1))
result |= 0xFFFFFFFFFFFFFF00ULL;
break;
case RTE_INT16:
if (result & (INT16_MAX + 1))
result |= 0xFFFFFFFFFFFF0000ULL;
break;
case RTE_INT32:
if (result & (INT32_MAX + 1ULL))
result |= 0xFFFFFFFF00000000ULL;
break;
default:
break;
}
if (num_garbage_negative_strs[i].result == (int64_t) result)
continue;
printf("Error: parsing %s as %s failed: result mismatch!\n",
num_garbage_negative_strs[i].str, buf);
return -1;
}
}
}
memset(&buf, 0, sizeof(buf));
/* coverage! */
cmdline_get_help_num((cmdline_parse_token_hdr_t*)&token,
buf, sizeof(buf));
return 0;
}
|