From ffaecee633aaa44114543f8f445f21b1ae66871c Mon Sep 17 00:00:00 2001 From: Albert Chu Date: Tue, 13 Dec 2016 13:36:00 -0800 Subject: [PATCH] test/optparse: Add corner case test Add regression test for segfault corner case. --- src/common/liboptparse/test/optparse.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/common/liboptparse/test/optparse.c b/src/common/liboptparse/test/optparse.c index eb72002bbd32..233540202c8e 100644 --- a/src/common/liboptparse/test/optparse.c +++ b/src/common/liboptparse/test/optparse.c @@ -838,10 +838,31 @@ Usage: test one [OPTIONS]\n\ optparse_destroy (a); } +void test_corner_case (void) +{ + optparse_err_t e; + optparse_t *p = optparse_create ("optarg"); + char *av[] = { "cornercase", NULL }; + int ac = sizeof (av) / sizeof (av[0]) - 1; + int optindex; + + ok (p != NULL, "optparse_create"); + + /* Test empty options do not segfault */ + + e = optparse_remove_option (p, "help"); + ok (e == OPTPARSE_SUCCESS, "optparse_remove_option"); + + optindex = optparse_parse_args (p, ac, av); + ok (optindex == ac, "parse options, verify optindex"); + + optparse_destroy (p); +} + int main (int argc, char *argv[]) { - plan (193); + plan (196); test_convenience_accessors (); /* 35 tests */ test_usage_output (); /* 42 tests */ @@ -851,6 +872,7 @@ int main (int argc, char *argv[]) test_subcommand (); /* 56 tests */ test_long_only (); /* 13 tests */ test_optional_argument (); /* 9 tests */ + test_corner_case (); /* 3 tests */ done_testing (); return (0);