/* ** Copyright (C) 2004-2007 by Carnegie Mellon University. ** ** @OPENSOURCE_HEADER_START@ ** ** Use of the SILK system and related source code is subject to the terms ** of the following licenses: ** ** GNU Public License (GPL) Rights pursuant to Version 2, June 1991 ** Government Purpose License Rights (GPLR) pursuant to DFARS 252.225-7013 ** ** NO WARRANTY ** ** ANY INFORMATION, MATERIALS, SERVICES, INTELLECTUAL PROPERTY OR OTHER ** PROPERTY OR RIGHTS GRANTED OR PROVIDED BY CARNEGIE MELLON UNIVERSITY ** PURSUANT TO THIS LICENSE (HEREINAFTER THE "DELIVERABLES") ARE ON AN ** "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY ** KIND, EITHER EXPRESS OR IMPLIED AS TO ANY MATTER INCLUDING, BUT NOT ** LIMITED TO, WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE, ** MERCHANTABILITY, INFORMATIONAL CONTENT, NONINFRINGEMENT, OR ERROR-FREE ** OPERATION. CARNEGIE MELLON UNIVERSITY SHALL NOT BE LIABLE FOR INDIRECT, ** SPECIAL OR CONSEQUENTIAL DAMAGES, SUCH AS LOSS OF PROFITS OR INABILITY ** TO USE SAID INTELLECTUAL PROPERTY, UNDER THIS LICENSE, REGARDLESS OF ** WHETHER SUCH PARTY WAS AWARE OF THE POSSIBILITY OF SUCH DAMAGES. ** LICENSEE AGREES THAT IT WILL NOT MAKE ANY WARRANTY ON BEHALF OF ** CARNEGIE MELLON UNIVERSITY, EXPRESS OR IMPLIED, TO ANY PERSON ** CONCERNING THE APPLICATION OF OR THE RESULTS TO BE OBTAINED WITH THE ** DELIVERABLES UNDER THIS LICENSE. ** ** Licensee hereby agrees to defend, indemnify, and hold harmless Carnegie ** Mellon University, its trustees, officers, employees, and agents from ** all claims or demands made against them (and any related losses, ** expenses, or attorney's fees) arising out of, or relating to Licensee's ** and/or its sub licensees' negligent use or willful misuse of or ** negligent conduct or willful misconduct regarding the Software, ** facilities, or other rights or assistance granted by Carnegie Mellon ** University under this License, including, but not limited to, any ** claims of product liability, personal injury, death, damage to ** property, or violation of any laws or regulations. ** ** Carnegie Mellon University Software Engineering Institute authored ** documents are sponsored by the U.S. Department of Defense under ** Contract F19628-00-C-0003. Carnegie Mellon University retains ** copyrights in all material produced under this contract. The U.S. ** Government retains a non-exclusive, royalty-free license to publish or ** reproduce these documents, or allow others to do so, for U.S. ** Government purposes only pursuant to the copyright license under the ** contract clause at 252.227.7013. ** ** @OPENSOURCE_HEADER_END@ */ /* ** Tests for the parsing routines in silkstring.c ** */ #include "silk.h" RCSIDENT("$SiLK: parse-tests.c 6604 2007-03-09 20:13:50Z mthomas $"); #include "utils.h" #define P_HEADER(str) \ printf("\n>>>>> RUNNING TESTS ON " str "()\n\n") #define P_WARNS(val) \ if ((val) == 0) {} else printf("\t EXPECT WARNINGS") #define P_NULL(str) \ (((str) == NULL) ? printf("NULL") : printf("'%s'", str)) #define P_STATUS(failed) \ printf(" %s", (((failed) == 0) ? "TEST PASSED" : "TEST FAILED")) #define P_BEGIN \ printf("TEST_BEGIN: ") #define P_END \ printf("TEST END\n\n") #define P_NL \ printf("\n") #define GOT_STR " got: " #define EXP_STR " expected: " #define SENTINEL "END_OF_INPUT" #define IS_SENTINEL(str) \ (((str) != NULL) && (0 == strcmp((str), SENTINEL))) /* Test for skStringParseNumberList() */ static int nl_parser(void) { int rv; uint32_t *result_val; uint32_t result_count; uint32_t i, j; int failed, print_results; static struct { int exp_retval; uint32_t exp_count; uint32_t exp_array[16]; uint32_t min; uint32_t max; uint32_t count; const char *str; } input[] = { { 0, 6, {0,1,2,3,4,5,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "0,1,2,3,4,5" }, { 0, 6, {5,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "5,4,3,2,1,0" }, { 0, 6, {0,1,2,3,4,5,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "0-5" }, { 0, 1, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "2" }, { 0, 1, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "2-2" }, { 0, 2, {2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "2,2" }, { 0, 10, {0,1,2,3,4,5,0,1,2,3,0,0,0,0,0,0}, 0, 5, 10, "0-5,0-3" }, {-1, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "0-5,0-4" }, {-1, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "1-6" }, {-1, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "2-3-" }, {-1, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "2-" }, {-1, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "3-2" }, {-1, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 1, 5, 10, "0-5" }, {-1, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, "" }, {-1, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, " " }, {-1, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 5, 10, NULL }, { 0, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 0, 0, 0, SENTINEL } }; P_HEADER("skStringParseNumberList"); for (i = 0; !IS_SENTINEL(input[i].str); ++i) { P_BEGIN; printf("min=%3u, max=%3u, count=%3u, str=", input[i].min, input[i].max, input[i].count); P_NULL(input[i].str); P_WARNS(input[i].exp_retval); P_NL; result_val = NULL; result_count = 0; failed = 0; print_results = 0; rv = skStringParseNumberList(&result_val, &result_count, input[i].str, input[i].min, input[i].max, input[i].count); if (rv != input[i].exp_retval) { failed = 1; } else if (rv != 0) { print_results = 1; } else if (result_count != input[i].exp_count) { failed = 1; } else if (result_count != 0 && result_val == NULL) { failed = 1; } else { for (j = 0; j < result_count; ++j) { if (result_val[j] != input[i].exp_array[j]) { failed = 1; break; } } } P_STATUS(failed); if ( failed || print_results) { printf(GOT_STR "ret=%3d, count=%3lu, res=", rv, (unsigned long)result_count); if (result_val == NULL) { printf("NULL"); } else if (result_count == 0) { printf("[]"); } else { printf("[%lu", (unsigned long)result_val[0]); for (j = 1; j < result_count; ++j) { printf(",%lu", (unsigned long)result_val[j]); } printf("]"); } } P_NL; if (failed) { printf(EXP_STR "ret=%3d, count=%3lu, res=", input[i].exp_retval, (unsigned long)input[i].exp_count); if (input[i].exp_array == NULL) { printf("NULL"); } else if (input[i].exp_count == 0) { printf("[]"); } else { printf("[%lu", (unsigned long)input[i].exp_array[0]); for (j = 1; j < input[i].exp_count; ++j) { printf(",%lu", (unsigned long)input[i].exp_array[j]); } printf("]"); } P_NL; } if (result_val != NULL) { free(result_val); } P_END; } return 0; } /* Test for skStringParseNumberListToBitmap() */ static int bt_parser(void) { int rv; BITMAP_DECLARE(result_val, 95); uint32_t i, j; int failed, print_results; static struct { int exp_retval; uint32_t exp_array[3]; uint32_t max; const char *str; } input[] = { { 0, { 63, 0, 0}, 63, "0,1,2,3,4,5" }, { 0, { 63, 0, 0}, 64, "5,4,3,2,1,0" }, { 0, { 63, 0, 0}, 65, "0-5" }, { 0, { 63, 0, 0}, 96, "0-5,0-3" }, { 0, { 0, 1, 0}, 64, "32" }, { 0, { 0, 1, 0}, 64, "32-32" }, { 0, { 0, 1, 0}, 64, "32,32" }, { 0, { 4294967295u, 0, 0}, 64, "0-31" }, { 0, { 0, 1073741824u, 0}, 63, "62" }, { 0, { 0, 2147483648u, 0}, 64, "63" }, { 0, { 0, 3221225472u, 0}, 64, "62-" }, { 0, { 0, 0, 1}, 65, "64" }, { 0, { 0, 0, 2}, 66, "65" }, {-1, {0,0,0}, 64, "1-65" }, {-1, {0,0,0}, 64, "2-3-" }, {-1, {0,0,0}, 64, "3-2" }, {-1, {0,0,0}, 64, "64" }, {-1, {0,0,0}, 64, "" }, {-1, {0,0,0}, 64, " " }, {-1, {0,0,0}, 64, NULL }, { 0, {0,0,0}, 0, SENTINEL } }; P_HEADER("skStringParseNumberListToBitmap"); for (i = 0; !IS_SENTINEL(input[i].str); ++i) { P_BEGIN; printf("max=%3u, str=", input[i].max); P_NULL(input[i].str); P_NL; BITMAP_INIT(result_val); failed = 0; print_results = 0; rv = skStringParseNumberListToBitArray(result_val, input[i].str, input[i].max); if (rv != input[i].exp_retval) { failed = 1; } else if (rv != 0) { print_results = 1; } else { for (j = 0; j < 3; ++j) { if (result_val[j] != input[i].exp_array[j]) { failed = 1; break; } } } P_STATUS(failed); if ( failed || print_results) { printf(GOT_STR "ret=%3d, res=", rv); printf("[%lu", (unsigned long)result_val[0]); for (j = 1; j < 3; ++j) { printf(",%lu", (unsigned long)result_val[j]); } printf("]"); } P_NL; if (failed) { printf(EXP_STR "ret=%3d, res=", input[i].exp_retval); printf("[%lu", (unsigned long)input[i].exp_array[0]); for (j = 1; j < 3; ++j) { printf(",%lu", (unsigned long)input[i].exp_array[j]); } printf("]"); P_NL; } P_END; } return 0; } /* Tests for skStringParseDatetime() */ static int dt_parser(void) { uint32_t i; int rv; struct timeval result_val; int precision; int failed, print_results; static struct { int exp_retval; time_t exp_result; int exp_prec; const char *str; } input[] = { { 0, 1099526400, 3, "2004/11/04"}, { 0, 1099526400, 3, "2004/11/04 "}, { 0, 1099526400, 3, " 2004/11/04"}, { 0, 1099526400, 3, " 2004/11/04 "}, { 0, 1099566000, 4, "2004/11/04:11"}, { 0, 1099566720, 5, "2004/11/4:11:12"}, { 0, 1099566733, 6, "2004/11/4:11:12:13"}, { -1, 0, 6, "2004/11/4:11:12:13:14"}, { -1, 0, 6, "2004/11/4:11:12:13-2004/11/4:11:12:14"}, { -1, 0, 0, "2004-11-4"}, { -1, 0, 0, "2004.11.4"}, { -1, 0, 1, "2004"}, { -1, 0, 1, "2004/"}, { -1, 0, 3, " 2004/11/4 11:12:13 "}, { -1, 0, 2, "2004/11"}, { -1, 0, 2, "2004/11/"}, { -1, 0, 0, "2004/0/4"}, { -1, 0, 0, "2004/13/4"}, { -1, 0, 0, "1959/01/01"}, { -1, 0, 0, "2004/11/4:-3:-3:-3"}, { -1, 0, 0, "2004/11/4::11:12"}, { -1, 0, 0, "2004/11/31"}, { -1, 0, 0, "2004/11/4:24"}, { -1, 0, 0, "2004/11/4:23:60:59"}, { -1, 0, 0, "2004/11/4:23:59:60"}, { -1, 0, 2, "2004/11/40"}, { -1, 0, 3, "2004/11/4:110"}, { -1, 0, 4, "2004/11/4:11:120"}, { -1, 0, 5, "2004/11/4:11:12:130"}, { -1, 0, 0, " "}, { -1, 0, 0, ""}, { -1, 0, 0, NULL}, { 0, 0, 0, SENTINEL} }; P_HEADER("skStringParseDatetime"); for (i = 0; !IS_SENTINEL(input[i].str); ++i) { P_BEGIN; printf("str="); P_NULL(input[i].str); P_WARNS(input[i].exp_retval); P_NL; memset(&result_val, 0, sizeof(struct timeval)); precision = 0; failed = 0; print_results = 0; rv = skStringParseDatetime(&result_val, input[i].str, &precision); if (rv != input[i].exp_retval) { failed = 1; } else if (rv != 0) { print_results = 1; } else if (result_val.tv_sec != input[i].exp_result) { failed = 1; } else if (precision != input[i].exp_prec) { failed = 1; } P_STATUS(failed); if (failed || print_results) { printf(GOT_STR "ret=%3d, precision=%3d, result=%10lu", rv, precision, (unsigned long)result_val.tv_sec); } P_NL; if (failed) { printf(EXP_STR "ret=%3d, precision=%3d, result=%10lu\n", input[i].exp_retval, input[i].exp_prec, (unsigned long)input[i].exp_result); } P_END; } return 0; } /* Tests for skStringParseDatetimeRange() */ static int tr_parser(void) { uint32_t i; int rv; struct timeval s_time, e_time; int s_precision, e_precision; int failed, print_results; static struct { int exp_retval; time_t exp_start_time; time_t exp_end_time; int exp_start_prec; int exp_end_prec; const char *str; } input[] = { { 0, 1099526400, LONG_MAX, 3, 0, "2004/11/04"}, { 0, 1099526400, LONG_MAX, 3, 0, "2004/11/04 "}, { 0, 1099526400, LONG_MAX, 3, 0, " 2004/11/04"}, { 0, 1099526400, LONG_MAX, 3, 0, " 2004/11/04 "}, { 0, 1099566000, LONG_MAX, 4, 0, "2004/11/04:11"}, { 0, 1099566720, LONG_MAX, 5, 0, "2004/11/4:11:12"}, { 0, 1099566733, LONG_MAX, 6, 0, "2004/11/4:11:12:13"}, { -1, 0, 0, 6, 0, "2004/11/4:11:12:13:14"}, { 0, 1099526400, 1099612800, 3, 3, "2004/11/04-2004/11/05"}, { 0, 1099566733, 1099566733, 6, 6, "2004/11/4:11:12:13-2004/11/4:11:12:13"}, { 0, 1099566733, 1099566734, 6, 6, "2004/11/4:11:12:13- 2004/11/4:11:12:14"}, { 0, 1099566733, 1099566734, 6, 6, "2004/11/4:11:12:13.000-2004/11/4:11:12:14.000"}, { 0, 1099566733, 1099566734, 6, 6, "2004/11/4:11:12:13-2004/11/4:11:12:14"}, { 0, 1099566733, 1099566733, 6, 6, "2004/11/4:11:12:13-2004/11/4:11:12:13"}, { 1, 1099566733, 1099566732, 6, 6, "2004/11/4:11:12:13-2004/11/4:11:12:12"}, { 0, 1099566733, 1099566780, 6, 5, "2004/11/4:11:12:13-2004/11/4:11:13"}, { 0, 1099566733, 1099569600, 6, 4, "2004/11/4:11:12:13-2004/11/4:12"}, { 0, 1099566733, 1099612800, 6, 3, "2004/11/4:11:12:13-2004/11/5"}, { -1, 1099566733, 0, 6, 1, "2004/11/4:11:12:13-2004"}, { -1, 1099566733, 0, 6, 1, "2004/11/4:11:12:13-2004/"}, { -1, 1099566733, 0, 6, 3, "2004/11/4:11:12:13- 2004/11/4 11:12:13 "}, { -1, 1099566733, 0, 6, 2, "2004/11/4:11:12:13-2004/11/"}, { -1, 1099566733, 0, 6, 0, "2004/11/4:11:12:13-2004/0/4"}, { -1, 1099566733, 0, 6, 0, "2004/11/4:11:12:13-2004/13/4"}, { -1, 1099566733, 0, 6, 0, "2004/11/4:11:12:13-1959/01/01"}, { -1, 1099566733, 0, 6, 0, "2004/11/4:11:12:13-2004/11/4:-3:-3:-3"}, { -1, 1099566733, 0, 6, 0, "2004/11/4:11:12:13-2004/11/4::11:12"}, { -1, 1099566733, 0, 6, 0, "2004/11/4:11:12:13-2004/11/31"}, { -1, 1099566733, 0, 6, 0, "2004/11/4:11:12:13-2004/11/4:24"}, { -1, 1099566733, 0, 6, 0, "2004/11/4:11:12:13-2004/11/4:23:60:59"}, { -1, 1099566733, 0, 6, 0, "2004/11/4:11:12:13-2004/11/4:23:59:60"}, { -1, 0, 0, 0, 0, " "}, { -1, 0, 0, 0, 0, ""}, { -1, 0, 0, 0, 0, NULL}, { 0, 0, 0, 0, 0, SENTINEL} }; P_HEADER("skStringParseDatetimeRange"); for (i = 0; !IS_SENTINEL(input[i].str); ++i) { P_BEGIN; printf("str="); P_NULL(input[i].str); P_WARNS(input[i].exp_retval); P_NL; memset(&s_time, 0, sizeof(struct timeval)); memset(&e_time, 0, sizeof(struct timeval)); s_precision = 0; e_precision = 0; failed = 0; print_results = 0; rv = skStringParseDatetimeRange(&s_time, &e_time, input[i].str, &s_precision, &e_precision); if (rv != input[i].exp_retval) { failed = 1; } else if (rv < 0) { print_results = 1; } else if (s_time.tv_sec != input[i].exp_start_time) { failed = 1; } else if (e_time.tv_sec != input[i].exp_end_time) { failed = 1; } else if (s_precision != input[i].exp_start_prec) { failed = 1; } else if (e_precision != input[i].exp_end_prec) { failed = 1; } P_STATUS(failed); if (failed || print_results) { printf((GOT_STR "ret=%3d, s_prec=%3d, e_prec=%3d, " "s_time=%10lu, e_time=%10lu"), rv, s_precision, e_precision, (unsigned long)s_time.tv_sec, (unsigned long)e_time.tv_sec); } P_NL; if (failed) { printf((EXP_STR "ret=%3d, s_prec=%3d, e_prec=%3d, " "s_time=%10lu, e_time=%10lu\n"), input[i].exp_retval, input[i].exp_start_prec, input[i].exp_end_prec, (unsigned long)input[i].exp_start_time, (unsigned long)input[i].exp_end_time); } P_END; } return 0; } /* Tests for skStringParseUint32() */ static int u32_parser(void) { uint32_t i; int rv; uint32_t result_val; int failed, print_results; static struct { uint32_t min; uint32_t max; int exp_retval; uint32_t exp_result; const char *str; } input[] = { { 0, 20, 0, 10, " 10 "}, { 0, 20, -12, 100, " 100 "}, { 0, 20, -3, 0, " -10 "}, { 30, 50, 0, 40, " 40 "}, { 30, 50, -11, 10, " 10 "}, { 0, 20, 0, 0, " 0 "}, { 0, 20, 0, 20, " 20 "}, { 0, 20, -3, 0, " x1 "}, { 0, 20, 0, 11, " 011 "}, { 0, 20, 3, 2, " 2x"}, { 0, 20, -3, 0, ":2x"}, { 0, 20, -2, 0, ""}, { 0, 20, -2, 0, " "}, { 0, 0, 0, 10, "10"}, { 0, 20, -12, 4294967295u, "4294967295"}, { 0, 20, -4, 0, "42949672954294967295"}, { 0, 4294967295u, 0, 4294967295u, "4294967295"}, { 0, 0, 0, 4294967295u, "4294967295"}, { 0, 0, -1, 0, NULL}, { 0, 0, 0, 0, SENTINEL} }; P_HEADER("skStringParseUint32"); for (i = 0; !IS_SENTINEL(input[i].str); ++i) { P_BEGIN; printf("min=%3u, max=%10u, str=", input[i].min, input[i].max); P_NULL(input[i].str); P_NL; result_val = 0; failed = 0; print_results = 0; rv = skStringParseUint32(&result_val, input[i].str, input[i].min, input[i].max); if (rv != input[i].exp_retval) { failed = 1; } else if (rv < 0) { print_results = 1; } else if (result_val != input[i].exp_result) { failed = 1; } P_STATUS(failed); if (failed || print_results) { printf(GOT_STR "ret=%3d; result=%10lu", rv, (unsigned long)result_val); } P_NL; if (failed) { printf(EXP_STR "ret=%3d, result=%10lu\n", input[i].exp_retval, (unsigned long)input[i].exp_result); } P_END; } return 0; } /* Tests for skStringParseHumanUint64() */ static int hu_parser(void) { uint32_t i; int rv; uint64_t result_val; int failed, print_results; static struct { int exp_retval; uint64_t exp_result; skHumanFlags_t flags; const char *str; } input[] = { { 0, 256, SK_HUMAN_NORMAL, "256" }, { 0, 256, SK_HUMAN_NORMAL, " 256" }, { 0, 256, SK_HUMAN_NORMAL, " 256 " }, { -3, 0, SK_HUMAN_NORMAL, ":256" }, { 4, 1024, SK_HUMAN_NORMAL, " 1k " }, { 3, 1, SK_HUMAN_NORMAL, " 1 k " }, { 0, 1024, SK_HUMAN_MID_WS, " 1 k " }, { 0, 1048576, SK_HUMAN_NORMAL, "1m" }, { 0, 1048576, SK_HUMAN_NORMAL, "1M" }, { 0, 1572864, SK_HUMAN_NORMAL, "1.5m" }, { 0, 1000000, SK_HUMAN_LOWER_SI, "1m" }, { 0, 1048576, SK_HUMAN_LOWER_SI, "1M" }, { 0, 1048576, SK_HUMAN_UPPER_SI, "1m" }, { 0, 1000000, SK_HUMAN_UPPER_SI, "1M" }, { 3, 1073741824, SK_HUMAN_NORMAL, "1gbit" }, { 5, 536870912, SK_HUMAN_NORMAL, "0.5g " }, { 5, 536870912, SK_HUMAN_END_NO_WS, "0.5g " }, { -4, 0, SK_HUMAN_NORMAL, "42949672954294967295" }, { 0, 4398046511104ULL, SK_HUMAN_NORMAL, "4096g" }, { -5, 0, SK_HUMAN_NORMAL, "-50k" }, { -6, 0, SK_HUMAN_NORMAL, " NaN(Not a number) " }, { -1, 0, SK_HUMAN_NORMAL, NULL }, { -2, 0, SK_HUMAN_NORMAL, "" }, { -2, 0, SK_HUMAN_NORMAL, " " }, { 0, 0, SK_HUMAN_NORMAL, SENTINEL } }; P_HEADER("skStringParseHumanUint64"); for (i = 0; !IS_SENTINEL(input[i].str); ++i) { P_BEGIN; printf("flags=%3u, str=", input[i].flags); P_NULL(input[i].str); P_NL; result_val = 0; failed = 0; print_results = 0; rv = skStringParseHumanUint64(&result_val, input[i].str, input[i].flags); if (rv != input[i].exp_retval) { failed = 1; } else if (rv < 0) { print_results = 1; } else if (result_val != input[i].exp_result) { failed = 1; } P_STATUS(failed); if (failed || print_results) { printf(GOT_STR "ret=%3d; result=%20llu", rv, (unsigned long long)result_val); } P_NL; if (failed) { printf(EXP_STR "ret=%3d, result=%20llu\n", input[i].exp_retval, (unsigned long long)input[i].exp_result); } P_END; } return 0; } /* Tests for skStringParseTCPFlags() */ static int fl_parser(void) { uint32_t i; int rv; uint8_t result_val; int failed, print_results; static struct { int exp_retval; uint8_t exp_result; const char *str; } input[] = { { 0, 1, "F"}, { 0, 2, "S"}, { 0, 4, "R"}, { 0, 8, "P"}, { 0, 16, "A"}, { 0, 32, "U"}, { 0, 64, "E"}, { 0, 128, "C"}, { 3, 17, "FA/FAS"}, { 0, 19, "FAFAS"}, { 0, 251, "FSPUAEC"}, { 0, 255, "FSrpauEC"}, { 1, 0, ".A"}, { 0, 17, " FA "}, { 5, 17, " FAT "}, { 0, 0, " "}, { 0, 0, ""}, { -1, 0, NULL}, { 0, 0, SENTINEL} }; P_HEADER("skStringParseTCPFlags"); for (i = 0; !IS_SENTINEL(input[i].str); ++i) { P_BEGIN; printf("str="); P_NULL(input[i].str); P_NL; result_val = 0; failed = 0; print_results = 0; rv = skStringParseTCPFlags(&result_val, input[i].str); if (rv != input[i].exp_retval) { failed = 1; } else if (rv < 0) { print_results = 1; } else if (result_val != input[i].exp_result) { failed = 1; } P_STATUS(failed); if (failed || print_results) { printf(GOT_STR "ret=%3d; result=%3u", rv, result_val); } P_NL; if (failed) { printf(EXP_STR "ret=%3d, result=%3u\n", input[i].exp_retval, input[i].exp_result); } P_END; } return 0; } /* Tests for skStringParseTCPFlagsHighMask() */ static int cm_parser(void) { uint32_t i; int rv; uint8_t high, mask; int failed, print_results; static struct { int exp_retval; uint8_t exp_high; uint8_t exp_mask; const char *str; } input[] = { { 0, 18, 23, "AS/ASRF" }, { 0, 32, 32, "U / U" }, { -1, 0, 0, "G / U" }, { -1, 0, 0, "AFTSR" }, { 0, 17, 17, "AF/AF" }, { 0, 17, 19, "AF/ASF" }, { 0, 0, 17, "/AF"}, { -1, 18, 176, " AS / AUC" }, { -1, 18, 0, "AS" }, { -1, 0, 0, " " }, { -1, 0, 0, "" }, { -1, 0, 0, NULL }, { 0, 0, 0, SENTINEL } }; P_HEADER("skStringParseTCPFlagsHighMask"); for (i = 0; !IS_SENTINEL(input[i].str); ++i) { P_BEGIN; printf("str="); P_NULL(input[i].str); P_NL; high = 0; mask = 0; failed = 0; print_results = 0; rv = skStringParseTCPFlagsHighMask(&high, &mask, input[i].str); if (rv != input[i].exp_retval) { failed = 1; } else if (rv < 0) { print_results = 1; } else if (high != input[i].exp_high) { failed = 1; } else if (mask != input[i].exp_mask) { failed = 1; } P_STATUS(failed); if (failed || print_results) { printf(GOT_STR "ret=%3d; high=%3u, mask=%3u", rv, high, mask); } P_NL; if (failed) { printf(EXP_STR "ret=%3d, high=%3u, mask=%3u\n", input[i].exp_retval, input[i].exp_high, input[i].exp_mask); } P_END; } return 0; } /* Tests for skStringParseIP() */ static int ip_parser(void) { uint32_t i; int rv; uint32_t ip; int failed, print_results; static struct { int exp_retval; uint32_t exp_ip; const char *str; } input[] = { { 0, 0, "0.0.0.0" }, { 0, 4294967295u, "255.255.255.255" }, { 0, 167772160, "10.0.0.0" }, { 0, 168430090, "10.10.10.10" }, { 0, 168496141, "10.11.12.13" }, { 0, 167772160, " 10.0.0.0" }, { 0, 167772160, "10.0.0.0 " }, { 0, 167772160, " 10.0.0.0 " }, { 0, 167772160, "010.000.000.000" }, { 16, 167772160, "010.000.000.000." }, { 16, 167772160, "10.0.0.0 ." }, { 0, 167772160, "167772160" }, { 0, 167772160, " 167772160" }, { 0, 167772160, "167772160 " }, { 0, 167772160, " 167772160 " }, { 16, 167772160, "167772160 ." }, { 16, 167772160, " 167772160|" }, { -1, 0, NULL}, { -2, 0, ""}, { -2, 0, " "}, { -3, 0, " -167772160|" }, { -3, 0, " 167772160." }, { -4, 0, " 256.256.256.256" }, { -3, 0, " 10." }, { -3, 0, " 10.x.x.x " }, { -4, 0, "10.0.0.987529387459834759834750392487530924879" }, { -3, 0, "10.0|0.0"}, { -3, 0, " 10. 0. 0. 0"}, { 4, 10, "10 . 0. 0. 0"}, { 0, 0, SENTINEL } }; P_HEADER("skStringParseIP"); for (i = 0; !IS_SENTINEL(input[i].str); ++i) { P_BEGIN; printf("str="); P_NULL(input[i].str); P_NL; ip = 0; print_results = 0; failed = 0; rv = skStringParseIP(&ip, input[i].str); if (rv != input[i].exp_retval) { failed = 1; } else if (rv < 0) { print_results = 1; } else if (ip != input[i].exp_ip) { failed = 1; } P_STATUS(failed); if (failed || print_results) { printf(GOT_STR "ret=%3d; ip=%10lu", rv, (unsigned long)ip); } P_NL; if (failed) { printf(EXP_STR "ret=%3d, ip=%10lu\n", input[i].exp_retval, (unsigned long)input[i].exp_ip); } P_END; } return 0; } int main(int UNUSED(argc), char **argv) { /* register the application */ skAppRegister(argv[0]); nl_parser(); bt_parser(); dt_parser(); tr_parser(); u32_parser(); hu_parser(); fl_parser(); cm_parser(); ip_parser(); skAppUnregister(); return 0; } /* ** Local variables: ** mode:c ** indent-tabs-mode:nil ** c-basic-offset:4 ** End: */