From 259d648e63e82ade4fe2c2c73c8b67fe57d9d049 Mon Sep 17 00:00:00 2001 From: "djm@openbsd.org" Date: Fri, 19 Mar 2021 04:23:50 +0000 Subject: upstream: add a test for misc.c:argv_split(), currently fails OpenBSD-Regress-ID: ad6b96d6ebeb9643b698b3575bdd6f78bb144200 --- regress/unittests/misc/test_argv.c | 139 +++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 regress/unittests/misc/test_argv.c diff --git a/regress/unittests/misc/test_argv.c b/regress/unittests/misc/test_argv.c new file mode 100644 index 00000000..0ce86694 --- /dev/null +++ b/regress/unittests/misc/test_argv.c @@ -0,0 +1,139 @@ +/* $OpenBSD: test_argv.c,v 1.1 2021/03/19 04:23:50 djm Exp $ */ +/* + * Regress test for misc argv handling functions. + * + * Placed in the public domain. + */ + +#include +#include +#include +#include +#include +#include + +#include "test_helper.h" + +#include "log.h" +#include "misc.h" + +void test_argv(void); + +static void +free_argv(char **av, int ac) +{ + int i; + + for (i = 0; i < ac; i++) + free(av[i]); + free(av); +} + +void +test_argv(void) +{ + char **av = NULL; + int ac = 0; + +#define RESET_ARGV() \ + do { \ + free_argv(av, ac); \ + av = NULL; \ + ac = -1; \ + } while (0) + + TEST_START("empty args"); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("", &ac, &av), 0); + ASSERT_INT_EQ(ac, 0); + ASSERT_PTR_NE(av, NULL); + ASSERT_PTR_EQ(av[0], NULL); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split(" ", &ac, &av), 0); + ASSERT_INT_EQ(ac, 0); + ASSERT_PTR_NE(av, NULL); + ASSERT_PTR_EQ(av[0], NULL); + TEST_DONE(); + + TEST_START("trivial args"); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("leamas", &ac, &av), 0); + ASSERT_INT_EQ(ac, 1); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "leamas"); + ASSERT_PTR_EQ(av[1], NULL); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("smiley leamas", &ac, &av), 0); + ASSERT_INT_EQ(ac, 2); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "smiley"); + ASSERT_STRING_EQ(av[1], "leamas"); + ASSERT_PTR_EQ(av[2], NULL); + TEST_DONE(); + + TEST_START("quoted"); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("\"smiley\"", &ac, &av), 0); + ASSERT_INT_EQ(ac, 1); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "smiley"); + ASSERT_PTR_EQ(av[1], NULL); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("leamas \" smiley \"", &ac, &av), 0); + ASSERT_INT_EQ(ac, 2); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "leamas"); + ASSERT_STRING_EQ(av[1], " smiley "); + ASSERT_PTR_EQ(av[2], NULL); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("\"smiley leamas\"", &ac, &av), 0); + ASSERT_INT_EQ(ac, 1); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "smiley leamas"); + ASSERT_PTR_EQ(av[1], NULL); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("smiley\" leamas\" liz", &ac, &av), 0); + ASSERT_INT_EQ(ac, 2); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "smiley leamas"); + ASSERT_STRING_EQ(av[1], "liz"); + ASSERT_PTR_EQ(av[2], NULL); + TEST_DONE(); + + TEST_START("escaped"); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("\\\"smiley\\'", &ac, &av), 0); + ASSERT_INT_EQ(ac, 1); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "\"smiley'"); + ASSERT_PTR_EQ(av[1], NULL); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("'\\'smiley\\\"'", &ac, &av), 0); + ASSERT_INT_EQ(ac, 1); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "'smiley\""); + ASSERT_PTR_EQ(av[1], NULL); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("smiley\\'s leamas\\'", &ac, &av), 0); + ASSERT_INT_EQ(ac, 2); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "smiley's"); + ASSERT_STRING_EQ(av[1], "leamas'"); + ASSERT_PTR_EQ(av[2], NULL); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("leamas\\\\smiley", &ac, &av), 0); + ASSERT_INT_EQ(ac, 1); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "leamas\\smiley"); + ASSERT_PTR_EQ(av[1], NULL); + RESET_ARGV(); + ASSERT_INT_EQ(argv_split("leamas\\\\ \\\\smiley", &ac, &av), 0); + ASSERT_INT_EQ(ac, 2); + ASSERT_PTR_NE(av, NULL); + ASSERT_STRING_EQ(av[0], "leamas\\"); + ASSERT_STRING_EQ(av[1], "\\smiley"); + ASSERT_PTR_EQ(av[2], NULL); + TEST_DONE(); + + /* XXX test char *argv_assemble(int argc, char **argv) */ +} -- cgit v1.2.3