xref: /openbsd-src/regress/usr.bin/ssh/unittests/misc/test_argv.c (revision 097a140d792de8b2bbe59ad827d39eabf9b4280a)
1 /* 	$OpenBSD: test_argv.c,v 1.1 2021/03/19 04:23:50 djm Exp $ */
2 /*
3  * Regress test for misc argv handling functions.
4  *
5  * Placed in the public domain.
6  */
7 
8 #include <sys/types.h>
9 #include <sys/param.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "test_helper.h"
16 
17 #include "log.h"
18 #include "misc.h"
19 
20 void test_argv(void);
21 
22 static void
23 free_argv(char **av, int ac)
24 {
25 	int i;
26 
27 	for (i = 0; i < ac; i++)
28 		free(av[i]);
29 	free(av);
30 }
31 
32 void
33 test_argv(void)
34 {
35 	char **av = NULL;
36 	int ac = 0;
37 
38 #define RESET_ARGV() \
39 	do { \
40 		free_argv(av, ac); \
41 		av = NULL; \
42 		ac = -1; \
43 	} while (0)
44 
45 	TEST_START("empty args");
46 	RESET_ARGV();
47 	ASSERT_INT_EQ(argv_split("", &ac, &av), 0);
48 	ASSERT_INT_EQ(ac, 0);
49 	ASSERT_PTR_NE(av, NULL);
50 	ASSERT_PTR_EQ(av[0], NULL);
51 	RESET_ARGV();
52 	ASSERT_INT_EQ(argv_split("    ", &ac, &av), 0);
53 	ASSERT_INT_EQ(ac, 0);
54 	ASSERT_PTR_NE(av, NULL);
55 	ASSERT_PTR_EQ(av[0], NULL);
56 	TEST_DONE();
57 
58 	TEST_START("trivial args");
59 	RESET_ARGV();
60 	ASSERT_INT_EQ(argv_split("leamas", &ac, &av), 0);
61 	ASSERT_INT_EQ(ac, 1);
62 	ASSERT_PTR_NE(av, NULL);
63 	ASSERT_STRING_EQ(av[0], "leamas");
64 	ASSERT_PTR_EQ(av[1], NULL);
65 	RESET_ARGV();
66 	ASSERT_INT_EQ(argv_split("smiley leamas", &ac, &av), 0);
67 	ASSERT_INT_EQ(ac, 2);
68 	ASSERT_PTR_NE(av, NULL);
69 	ASSERT_STRING_EQ(av[0], "smiley");
70 	ASSERT_STRING_EQ(av[1], "leamas");
71 	ASSERT_PTR_EQ(av[2], NULL);
72 	TEST_DONE();
73 
74 	TEST_START("quoted");
75 	RESET_ARGV();
76 	ASSERT_INT_EQ(argv_split("\"smiley\"", &ac, &av), 0);
77 	ASSERT_INT_EQ(ac, 1);
78 	ASSERT_PTR_NE(av, NULL);
79 	ASSERT_STRING_EQ(av[0], "smiley");
80 	ASSERT_PTR_EQ(av[1], NULL);
81 	RESET_ARGV();
82 	ASSERT_INT_EQ(argv_split("leamas \" smiley \"", &ac, &av), 0);
83 	ASSERT_INT_EQ(ac, 2);
84 	ASSERT_PTR_NE(av, NULL);
85 	ASSERT_STRING_EQ(av[0], "leamas");
86 	ASSERT_STRING_EQ(av[1], " smiley ");
87 	ASSERT_PTR_EQ(av[2], NULL);
88 	RESET_ARGV();
89 	ASSERT_INT_EQ(argv_split("\"smiley leamas\"", &ac, &av), 0);
90 	ASSERT_INT_EQ(ac, 1);
91 	ASSERT_PTR_NE(av, NULL);
92 	ASSERT_STRING_EQ(av[0], "smiley leamas");
93 	ASSERT_PTR_EQ(av[1], NULL);
94 	RESET_ARGV();
95 	ASSERT_INT_EQ(argv_split("smiley\" leamas\" liz", &ac, &av), 0);
96 	ASSERT_INT_EQ(ac, 2);
97 	ASSERT_PTR_NE(av, NULL);
98 	ASSERT_STRING_EQ(av[0], "smiley leamas");
99 	ASSERT_STRING_EQ(av[1], "liz");
100 	ASSERT_PTR_EQ(av[2], NULL);
101 	TEST_DONE();
102 
103 	TEST_START("escaped");
104 	RESET_ARGV();
105 	ASSERT_INT_EQ(argv_split("\\\"smiley\\'", &ac, &av), 0);
106 	ASSERT_INT_EQ(ac, 1);
107 	ASSERT_PTR_NE(av, NULL);
108 	ASSERT_STRING_EQ(av[0], "\"smiley'");
109 	ASSERT_PTR_EQ(av[1], NULL);
110 	RESET_ARGV();
111 	ASSERT_INT_EQ(argv_split("'\\'smiley\\\"'", &ac, &av), 0);
112 	ASSERT_INT_EQ(ac, 1);
113 	ASSERT_PTR_NE(av, NULL);
114 	ASSERT_STRING_EQ(av[0], "'smiley\"");
115 	ASSERT_PTR_EQ(av[1], NULL);
116 	RESET_ARGV();
117 	ASSERT_INT_EQ(argv_split("smiley\\'s leamas\\'", &ac, &av), 0);
118 	ASSERT_INT_EQ(ac, 2);
119 	ASSERT_PTR_NE(av, NULL);
120 	ASSERT_STRING_EQ(av[0], "smiley's");
121 	ASSERT_STRING_EQ(av[1], "leamas'");
122 	ASSERT_PTR_EQ(av[2], NULL);
123 	RESET_ARGV();
124 	ASSERT_INT_EQ(argv_split("leamas\\\\smiley", &ac, &av), 0);
125 	ASSERT_INT_EQ(ac, 1);
126 	ASSERT_PTR_NE(av, NULL);
127 	ASSERT_STRING_EQ(av[0], "leamas\\smiley");
128 	ASSERT_PTR_EQ(av[1], NULL);
129 	RESET_ARGV();
130 	ASSERT_INT_EQ(argv_split("leamas\\\\ \\\\smiley", &ac, &av), 0);
131 	ASSERT_INT_EQ(ac, 2);
132 	ASSERT_PTR_NE(av, NULL);
133 	ASSERT_STRING_EQ(av[0], "leamas\\");
134 	ASSERT_STRING_EQ(av[1], "\\smiley");
135 	ASSERT_PTR_EQ(av[2], NULL);
136 	TEST_DONE();
137 
138 	/* XXX test char *argv_assemble(int argc, char **argv) */
139 }
140