1*1323ec57SEd Maste /* $OpenBSD: test_argv.c,v 1.4 2021/12/14 21:25:27 deraadt Exp $ */
219261079SEd Maste /*
319261079SEd Maste * Regress test for misc argv handling functions.
419261079SEd Maste *
519261079SEd Maste * Placed in the public domain.
619261079SEd Maste */
719261079SEd Maste
819261079SEd Maste #include "includes.h"
919261079SEd Maste
1019261079SEd Maste #include <sys/types.h>
1119261079SEd Maste #include <stdio.h>
1219261079SEd Maste #ifdef HAVE_STDINT_H
1319261079SEd Maste #include <stdint.h>
1419261079SEd Maste #endif
1519261079SEd Maste #include <stdlib.h>
1619261079SEd Maste #include <string.h>
1719261079SEd Maste
1819261079SEd Maste #include "../test_helper/test_helper.h"
1919261079SEd Maste
2019261079SEd Maste #include "log.h"
2119261079SEd Maste #include "misc.h"
2219261079SEd Maste
2319261079SEd Maste void test_argv(void);
2419261079SEd Maste
2519261079SEd Maste void
test_argv(void)2619261079SEd Maste test_argv(void)
2719261079SEd Maste {
2819261079SEd Maste char **av = NULL;
2919261079SEd Maste int ac = 0;
3019261079SEd Maste
3119261079SEd Maste #define RESET_ARGV() \
3219261079SEd Maste do { \
3319261079SEd Maste argv_free(av, ac); \
3419261079SEd Maste av = NULL; \
3519261079SEd Maste ac = -1; \
3619261079SEd Maste } while (0)
3719261079SEd Maste
3819261079SEd Maste TEST_START("empty args");
3919261079SEd Maste ASSERT_INT_EQ(argv_split("", &ac, &av, 0), 0);
4019261079SEd Maste ASSERT_INT_EQ(ac, 0);
4119261079SEd Maste ASSERT_PTR_NE(av, NULL);
4219261079SEd Maste ASSERT_PTR_EQ(av[0], NULL);
4319261079SEd Maste RESET_ARGV();
4419261079SEd Maste ASSERT_INT_EQ(argv_split(" ", &ac, &av, 0), 0);
4519261079SEd Maste ASSERT_INT_EQ(ac, 0);
4619261079SEd Maste ASSERT_PTR_NE(av, NULL);
4719261079SEd Maste ASSERT_PTR_EQ(av[0], NULL);
4819261079SEd Maste RESET_ARGV();
4919261079SEd Maste TEST_DONE();
5019261079SEd Maste
5119261079SEd Maste TEST_START("trivial args");
5219261079SEd Maste ASSERT_INT_EQ(argv_split("leamas", &ac, &av, 0), 0);
5319261079SEd Maste ASSERT_INT_EQ(ac, 1);
5419261079SEd Maste ASSERT_PTR_NE(av, NULL);
5519261079SEd Maste ASSERT_STRING_EQ(av[0], "leamas");
5619261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
5719261079SEd Maste RESET_ARGV();
5819261079SEd Maste ASSERT_INT_EQ(argv_split("smiley leamas", &ac, &av, 0), 0);
5919261079SEd Maste ASSERT_INT_EQ(ac, 2);
6019261079SEd Maste ASSERT_PTR_NE(av, NULL);
6119261079SEd Maste ASSERT_STRING_EQ(av[0], "smiley");
6219261079SEd Maste ASSERT_STRING_EQ(av[1], "leamas");
6319261079SEd Maste ASSERT_PTR_EQ(av[2], NULL);
6419261079SEd Maste RESET_ARGV();
6519261079SEd Maste TEST_DONE();
6619261079SEd Maste
6719261079SEd Maste TEST_START("quoted");
6819261079SEd Maste ASSERT_INT_EQ(argv_split("\"smiley\"", &ac, &av, 0), 0);
6919261079SEd Maste ASSERT_INT_EQ(ac, 1);
7019261079SEd Maste ASSERT_PTR_NE(av, NULL);
7119261079SEd Maste ASSERT_STRING_EQ(av[0], "smiley");
7219261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
7319261079SEd Maste RESET_ARGV();
7419261079SEd Maste ASSERT_INT_EQ(argv_split("leamas \" smiley \"", &ac, &av, 0), 0);
7519261079SEd Maste ASSERT_INT_EQ(ac, 2);
7619261079SEd Maste ASSERT_PTR_NE(av, NULL);
7719261079SEd Maste ASSERT_STRING_EQ(av[0], "leamas");
7819261079SEd Maste ASSERT_STRING_EQ(av[1], " smiley ");
7919261079SEd Maste ASSERT_PTR_EQ(av[2], NULL);
8019261079SEd Maste RESET_ARGV();
8119261079SEd Maste ASSERT_INT_EQ(argv_split("\"smiley leamas\"", &ac, &av, 0), 0);
8219261079SEd Maste ASSERT_INT_EQ(ac, 1);
8319261079SEd Maste ASSERT_PTR_NE(av, NULL);
8419261079SEd Maste ASSERT_STRING_EQ(av[0], "smiley leamas");
8519261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
8619261079SEd Maste RESET_ARGV();
8719261079SEd Maste ASSERT_INT_EQ(argv_split("smiley\" leamas\" liz", &ac, &av, 0), 0);
8819261079SEd Maste ASSERT_INT_EQ(ac, 2);
8919261079SEd Maste ASSERT_PTR_NE(av, NULL);
9019261079SEd Maste ASSERT_STRING_EQ(av[0], "smiley leamas");
9119261079SEd Maste ASSERT_STRING_EQ(av[1], "liz");
9219261079SEd Maste ASSERT_PTR_EQ(av[2], NULL);
9319261079SEd Maste RESET_ARGV();
9419261079SEd Maste TEST_DONE();
9519261079SEd Maste
9619261079SEd Maste TEST_START("escaped");
9719261079SEd Maste ASSERT_INT_EQ(argv_split("\\\"smiley\\'", &ac, &av, 0), 0);
9819261079SEd Maste ASSERT_INT_EQ(ac, 1);
9919261079SEd Maste ASSERT_PTR_NE(av, NULL);
10019261079SEd Maste ASSERT_STRING_EQ(av[0], "\"smiley'");
10119261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
10219261079SEd Maste RESET_ARGV();
10319261079SEd Maste ASSERT_INT_EQ(argv_split("'\\'smiley\\\"'", &ac, &av, 0), 0);
10419261079SEd Maste ASSERT_INT_EQ(ac, 1);
10519261079SEd Maste ASSERT_PTR_NE(av, NULL);
10619261079SEd Maste ASSERT_STRING_EQ(av[0], "'smiley\"");
10719261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
10819261079SEd Maste RESET_ARGV();
10919261079SEd Maste ASSERT_INT_EQ(argv_split("smiley\\'s leamas\\'", &ac, &av, 0), 0);
11019261079SEd Maste ASSERT_INT_EQ(ac, 2);
11119261079SEd Maste ASSERT_PTR_NE(av, NULL);
11219261079SEd Maste ASSERT_STRING_EQ(av[0], "smiley's");
11319261079SEd Maste ASSERT_STRING_EQ(av[1], "leamas'");
11419261079SEd Maste ASSERT_PTR_EQ(av[2], NULL);
11519261079SEd Maste RESET_ARGV();
11619261079SEd Maste ASSERT_INT_EQ(argv_split("leamas\\\\smiley", &ac, &av, 0), 0);
11719261079SEd Maste ASSERT_INT_EQ(ac, 1);
11819261079SEd Maste ASSERT_PTR_NE(av, NULL);
11919261079SEd Maste ASSERT_STRING_EQ(av[0], "leamas\\smiley");
12019261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
12119261079SEd Maste RESET_ARGV();
12219261079SEd Maste ASSERT_INT_EQ(argv_split("leamas\\\\ \\\\smiley", &ac, &av, 0), 0);
12319261079SEd Maste ASSERT_INT_EQ(ac, 2);
12419261079SEd Maste ASSERT_PTR_NE(av, NULL);
12519261079SEd Maste ASSERT_STRING_EQ(av[0], "leamas\\");
12619261079SEd Maste ASSERT_STRING_EQ(av[1], "\\smiley");
12719261079SEd Maste ASSERT_PTR_EQ(av[2], NULL);
12819261079SEd Maste RESET_ARGV();
12919261079SEd Maste ASSERT_INT_EQ(argv_split("smiley\\ leamas", &ac, &av, 0), 0);
13019261079SEd Maste ASSERT_INT_EQ(ac, 1);
13119261079SEd Maste ASSERT_PTR_NE(av, NULL);
13219261079SEd Maste ASSERT_STRING_EQ(av[0], "smiley leamas");
13319261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
13419261079SEd Maste RESET_ARGV();
13519261079SEd Maste TEST_DONE();
13619261079SEd Maste
13719261079SEd Maste TEST_START("quoted escaped");
13819261079SEd Maste ASSERT_INT_EQ(argv_split("'smiley\\ leamas'", &ac, &av, 0), 0);
13919261079SEd Maste ASSERT_INT_EQ(ac, 1);
14019261079SEd Maste ASSERT_PTR_NE(av, NULL);
14119261079SEd Maste ASSERT_STRING_EQ(av[0], "smiley\\ leamas");
14219261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
14319261079SEd Maste RESET_ARGV();
14419261079SEd Maste ASSERT_INT_EQ(argv_split("\"smiley\\ leamas\"", &ac, &av, 0), 0);
14519261079SEd Maste ASSERT_INT_EQ(ac, 1);
14619261079SEd Maste ASSERT_PTR_NE(av, NULL);
14719261079SEd Maste ASSERT_STRING_EQ(av[0], "smiley\\ leamas");
14819261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
14919261079SEd Maste RESET_ARGV();
15019261079SEd Maste TEST_DONE();
15119261079SEd Maste
15219261079SEd Maste TEST_START("comments");
15319261079SEd Maste ASSERT_INT_EQ(argv_split("# gold", &ac, &av, 0), 0);
15419261079SEd Maste ASSERT_INT_EQ(ac, 2);
15519261079SEd Maste ASSERT_PTR_NE(av, NULL);
15619261079SEd Maste ASSERT_STRING_EQ(av[0], "#");
15719261079SEd Maste ASSERT_STRING_EQ(av[1], "gold");
15819261079SEd Maste ASSERT_PTR_EQ(av[2], NULL);
15919261079SEd Maste RESET_ARGV();
16019261079SEd Maste ASSERT_INT_EQ(argv_split("# gold", &ac, &av, 1), 0);
16119261079SEd Maste ASSERT_INT_EQ(ac, 0);
16219261079SEd Maste ASSERT_PTR_NE(av, NULL);
16319261079SEd Maste ASSERT_PTR_EQ(av[0], NULL);
16419261079SEd Maste RESET_ARGV();
16519261079SEd Maste ASSERT_INT_EQ(argv_split("leamas#gold", &ac, &av, 1), 0);
16619261079SEd Maste ASSERT_INT_EQ(ac, 1);
16719261079SEd Maste ASSERT_PTR_NE(av, NULL);
16819261079SEd Maste ASSERT_STRING_EQ(av[0], "leamas#gold");
16919261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
17019261079SEd Maste RESET_ARGV();
17119261079SEd Maste ASSERT_INT_EQ(argv_split("\"leamas # gold\"", &ac, &av, 1), 0);
17219261079SEd Maste ASSERT_INT_EQ(ac, 1);
17319261079SEd Maste ASSERT_PTR_NE(av, NULL);
17419261079SEd Maste ASSERT_STRING_EQ(av[0], "leamas # gold");
17519261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
17619261079SEd Maste RESET_ARGV();
17719261079SEd Maste ASSERT_INT_EQ(argv_split("\"leamas\"#gold", &ac, &av, 1), 0);
17819261079SEd Maste ASSERT_INT_EQ(ac, 1);
17919261079SEd Maste ASSERT_PTR_NE(av, NULL);
18019261079SEd Maste ASSERT_STRING_EQ(av[0], "leamas#gold");
18119261079SEd Maste ASSERT_PTR_EQ(av[1], NULL);
18219261079SEd Maste RESET_ARGV();
18319261079SEd Maste TEST_DONE();
18419261079SEd Maste
18519261079SEd Maste /* XXX test char *argv_assemble(int argc, char **argv) */
18619261079SEd Maste }
187