This source file includes following definitions.
- check_one
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22 #include "sh-quote.h"
23
24 #include <limits.h>
25 #include <string.h>
26
27 #include "macros.h"
28
29 static void
30 check_one (const char *input, const char *expected)
31 {
32 char buf[1000];
33 size_t output_len;
34 char *output;
35 char *bufend;
36
37 output_len = shell_quote_length (input);
38
39 output = shell_quote (input);
40 ASSERT (strlen (output) == output_len);
41
42 ASSERT (output_len <= sizeof (buf) - 2);
43 memset (buf, '\0', output_len + 1);
44 buf[output_len + 1] = '%';
45 bufend = shell_quote_copy (buf, input);
46 ASSERT (bufend == buf + output_len);
47 ASSERT (memcmp (buf, output, output_len + 1) == 0);
48 ASSERT (buf[output_len + 1] == '%');
49
50 ASSERT (strcmp (output, expected) == 0);
51
52 free (output);
53 }
54
55 int
56 main (void)
57 {
58
59 {
60 int c;
61
62
63 check_one ("", "''");
64
65
66 check_one ("foo", "foo");
67 check_one ("phr0ck", "phr0ck");
68
69
70 check_one ("foo\tbar", "'foo\tbar'");
71 check_one ("foo\nbar", "'foo\nbar'");
72 check_one ("foo\rbar", "'foo\rbar'");
73 check_one ("foo bar", "'foo bar'");
74
75
76 check_one ("!foo", "'!foo'");
77
78
79 check_one ("\"foo\"bar", "'\"foo\"bar'");
80
81
82
83 check_one ("#foo", "'#foo'");
84
85
86
87 check_one ("$foo", "'$foo'");
88
89
90
91 check_one ("&", "'&'");
92
93
94 check_one ("'foo'bar", "\"'foo'bar\"");
95
96
97 check_one ("(", "'('");
98
99
100
101 check_one (")", "')'");
102
103
104 check_one ("*", "'*'");
105 check_one ("*foo", "'*foo'");
106
107
108
109 check_one (";", "';'");
110 check_one ("foo;", "'foo;'");
111
112
113 check_one ("<", "'<'");
114
115
116
117 check_one ("foo=bar", "'foo=bar'");
118
119
120 check_one (">", "'>'");
121
122
123 check_one ("?", "'?'");
124 check_one ("foo?bar", "'foo?bar'");
125
126
127 check_one ("^", "'^'");
128
129
130 check_one ("[", "'['");
131 check_one ("]", "]");
132
133
134 check_one ("\\foo", "'\\foo'");
135
136
137 check_one ("`foo", "'`foo'");
138
139
140 check_one ("{", "'{'");
141
142
143
144 check_one ("|", "'|'");
145
146
147
148 check_one ("}", "'}'");
149
150
151
152 check_one ("~", "'~'");
153 check_one ("~foo", "'~foo'");
154
155
156 check_one ("foo'bar\"baz", "'foo'\\''bar\"baz'");
157
158
159 for (c = 1; c <= UCHAR_MAX; c++)
160 if (strchr ("\t\n\r !\"#$&'()*;<=>?^[\\]`{|}~", c) == NULL)
161 {
162 char s[5];
163 s[0] = 'a';
164 s[1] = (char) c;
165 s[2] = 'z';
166 s[3] = (char) c;
167 s[4] = '\0';
168
169 check_one (s, s);
170 }
171 }
172
173
174 {
175 const char *argv[1];
176 char *result;
177 argv[0] = NULL;
178 result = shell_quote_argv (argv);
179 ASSERT (strcmp (result, "") == 0);
180 free (result);
181 }
182 {
183 const char *argv[2];
184 char *result;
185 argv[0] = "foo bar/baz";
186 argv[1] = NULL;
187 result = shell_quote_argv (argv);
188 ASSERT (strcmp (result, "'foo bar/baz'") == 0);
189 free (result);
190 }
191 {
192 const char *argv[3];
193 char *result;
194 argv[0] = "foo bar/baz";
195 argv[1] = "$";
196 argv[2] = NULL;
197 result = shell_quote_argv (argv);
198 ASSERT (strcmp (result, "'foo bar/baz' '$'") == 0);
199 free (result);
200 }
201
202 return 0;
203 }