1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include "apr_escape.h"
22 #include "apr_strings.h"
23
24 #include "abts.h"
25 #include "testutil.h"
26
test_escape(abts_case * tc,void * data)27 static void test_escape(abts_case *tc, void *data)
28 {
29 apr_pool_t *pool;
30 const char *src, *target;
31 const char *dest;
32 const void *vdest;
33 apr_size_t len, vlen;
34
35 apr_pool_create(&pool, NULL);
36
37 src = "Hello World &;`'\"|*?~<>^()[]{}$\\";
38 target = "Hello World \\&\\;\\`\\'\\\"\\|\\*\\?\\~\\<\\>\\^\\(\\)\\[\\]\\{\\}\\$\\\\";
39 dest = apr_pescape_shell(pool, src);
40 ABTS_ASSERT(tc,
41 apr_psprintf(pool, "shell escaped (%s) does not match expected output (%s)",
42 dest, target),
43 (strcmp(dest, target) == 0));
44 apr_escape_shell(NULL, src, APR_ESCAPE_STRING, &len);
45 ABTS_ASSERT(tc,
46 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
47 (len == strlen(dest) + 1));
48
49 #if !(defined(OS2) || defined(WIN32))
50 /* Now try with newline, which is converted to a space on OS/2 and Windows.
51 */
52 src = "Hello World &;`'\"|*?~<>^()[]{}$\\\n";
53 target = "Hello World \\&\\;\\`\\'\\\"\\|\\*\\?\\~\\<\\>\\^\\(\\)\\[\\]\\{\\}\\$\\\\\\\n";
54 dest = apr_pescape_shell(pool, src);
55 ABTS_ASSERT(tc,
56 apr_psprintf(pool, "shell escaped (%s) does not match expected output (%s)",
57 dest, target),
58 (strcmp(dest, target) == 0));
59 apr_escape_shell(NULL, src, APR_ESCAPE_STRING, &len);
60 ABTS_ASSERT(tc,
61 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
62 (len == strlen(dest) + 1));
63 #endif
64
65 src = "Hello";
66 dest = apr_punescape_url(pool, src, NULL, NULL, 0);
67 ABTS_PTR_EQUAL(tc, src, dest);
68
69 src = "Hello";
70 dest = apr_punescape_url(pool, src, NULL, NULL, 1);
71 ABTS_PTR_EQUAL(tc, src, dest);
72
73 src = "Hello%20";
74 dest = apr_punescape_url(pool, src, " ", NULL, 0);
75 ABTS_PTR_EQUAL(tc, NULL, dest);
76
77 src = "Hello%20World";
78 target = "Hello World";
79 dest = apr_punescape_url(pool, src, NULL, NULL, 0);
80 ABTS_STR_EQUAL(tc, target, dest);
81 apr_unescape_url(NULL, src, APR_ESCAPE_STRING, NULL, NULL, 0, &len);
82 ABTS_ASSERT(tc,
83 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
84 (len == strlen(dest) + 1));
85
86 src = "Hello+World";
87 target = "Hello World";
88 dest = apr_punescape_url(pool, src, NULL, NULL, 1);
89 ABTS_STR_EQUAL(tc, target, dest);
90 apr_unescape_url(NULL, src, APR_ESCAPE_STRING, NULL, NULL, 1, &len);
91 ABTS_ASSERT(tc,
92 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
93 (len == strlen(dest) + 1));
94
95 src = "Hello%20World";
96 target = "Hello%20World";
97 dest = apr_punescape_url(pool, src, NULL, " ", 0);
98 ABTS_STR_EQUAL(tc, target, dest);
99 apr_unescape_url(NULL, src, APR_ESCAPE_STRING, NULL, " ", 0, &len);
100 ABTS_ASSERT(tc,
101 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
102 (len == strlen(dest) + 1));
103
104 src = "Hello";
105 dest = apr_pescape_path_segment(pool, src);
106 ABTS_PTR_EQUAL(tc, src, dest);
107
108 src = "$-_.+!*'(),:@&=/~Hello World";
109 target = "$-_.+!*'(),:@&=%2f~Hello%20World";
110 dest = apr_pescape_path_segment(pool, src);
111 ABTS_STR_EQUAL(tc, target, dest);
112 apr_escape_path_segment(NULL, src, APR_ESCAPE_STRING, &len);
113 ABTS_ASSERT(tc,
114 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
115 (len == strlen(dest) + 1));
116
117 src = "Hello";
118 dest = apr_pescape_path(pool, src, 0);
119 ABTS_PTR_EQUAL(tc, src, dest);
120
121 src = "$-_.+!*'(),:@&=/~Hello World";
122 target = "./$-_.+!*'(),:@&=/~Hello%20World";
123 dest = apr_pescape_path(pool, src, 0);
124 ABTS_STR_EQUAL(tc, target, dest);
125 apr_escape_path(NULL, src, APR_ESCAPE_STRING, 0, &len);
126 ABTS_ASSERT(tc,
127 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
128 (len == strlen(dest) + 1));
129
130 src = "Hello";
131 dest = apr_pescape_path(pool, src, 1);
132 ABTS_PTR_EQUAL(tc, src, dest);
133
134 src = "$-_.+!*'(),:@&=/~Hello World";
135 target = "$-_.+!*'(),:@&=/~Hello%20World";
136 dest = apr_pescape_path(pool, src, 1);
137 ABTS_STR_EQUAL(tc, target, dest);
138 apr_escape_path(NULL, src, APR_ESCAPE_STRING, 1, &len);
139 ABTS_ASSERT(tc,
140 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
141 (len == strlen(dest) + 1));
142
143 src = "Hello";
144 dest = apr_pescape_urlencoded(pool, src);
145 ABTS_PTR_EQUAL(tc, src, dest);
146
147 src = "$-_.+!*'(),:@&=/~Hello World";
148 target = "%24-_.%2b%21*%27%28%29%2c%3a%40%26%3d%2f%7eHello+World";
149 dest = apr_pescape_urlencoded(pool, src);
150 ABTS_STR_EQUAL(tc, target, dest);
151 apr_escape_urlencoded(NULL, src, APR_ESCAPE_STRING, &len);
152 ABTS_ASSERT(tc,
153 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
154 (len == strlen(dest) + 1));
155
156 src = "Hello";
157 dest = apr_pescape_entity(pool, src, 0);
158 ABTS_PTR_EQUAL(tc, src, dest);
159
160 src = "\xFF<>&\'\"Hello World";
161 target = "\xFF<>&'"Hello World";
162 dest = apr_pescape_entity(pool, src, 0);
163 ABTS_STR_EQUAL(tc, target, dest);
164 apr_escape_entity(NULL, src, APR_ESCAPE_STRING, 0, &len);
165 ABTS_ASSERT(tc,
166 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
167 (len == strlen(dest) + 1));
168
169 #if !APR_CHARSET_EBCDIC
170 src = "Hello";
171 dest = apr_pescape_entity(pool, src, 1);
172 ABTS_PTR_EQUAL(tc, src, dest);
173
174 src = "\xFF<>&\'\"Hello World";
175 target = "ÿ<>&'"Hello World";
176 dest = apr_pescape_entity(pool, src, 1);
177 ABTS_STR_EQUAL(tc, target, dest);
178 apr_escape_entity(NULL, src, APR_ESCAPE_STRING, 1, &len);
179 ABTS_ASSERT(tc,
180 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
181 (len == strlen(dest) + 1));
182
183 src = "Hello";
184 dest = apr_punescape_entity(pool, src);
185 ABTS_PTR_EQUAL(tc, src, dest);
186
187 src = "\xFF<>&'"Hello World";
188 target = "\xFF<>&\'\"Hello World";
189 dest = apr_punescape_entity(pool, src);
190 ABTS_STR_EQUAL(tc, target, dest);
191 apr_unescape_entity(NULL, src, APR_ESCAPE_STRING, &len);
192 ABTS_ASSERT(tc,
193 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
194 (len == strlen(dest) + 1));
195
196 src = "ÿ<>&'"Hello World";
197 target = "\xFF<>&\'\"Hello World";
198 dest = apr_punescape_entity(pool, src);
199 ABTS_STR_EQUAL(tc, target, dest);
200 apr_unescape_entity(NULL, src, APR_ESCAPE_STRING, &len);
201 ABTS_ASSERT(tc,
202 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
203 (len == strlen(dest) + 1));
204
205 src = " <>&'"Hello World";
206 target = " <>&\'\"Hello World";
207 dest = apr_punescape_entity(pool, src);
208 ABTS_STR_EQUAL(tc, target, dest);
209 apr_unescape_entity(NULL, src, APR_ESCAPE_STRING, &len);
210 ABTS_ASSERT(tc,
211 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
212 (len == strlen(dest) + 1));
213 #endif
214
215 src = "Hello";
216 dest = apr_pescape_echo(pool, src, 0);
217 ABTS_PTR_EQUAL(tc, src, dest);
218
219 src = "\a\b\f\\n\r\t\v\"Hello World\"";
220 target = "\\a\\b\\f\\\\n\\r\\t\\v\"Hello World\"";
221 dest = apr_pescape_echo(pool, src, 0);
222 ABTS_STR_EQUAL(tc, target, dest);
223 apr_escape_echo(NULL, src, APR_ESCAPE_STRING, 0, &len);
224 ABTS_ASSERT(tc,
225 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
226 (len == strlen(dest) + 1));
227
228 src = "\a\b\f\\n\r\t\v\"Hello World\"";
229 target = "\\a\\b\\f\\\\n\\r\\t\\v\\\"Hello World\\\"";
230 dest = apr_pescape_echo(pool, src, 1);
231 ABTS_STR_EQUAL(tc, target, dest);
232 apr_escape_echo(NULL, src, APR_ESCAPE_STRING, 1, &len);
233 ABTS_ASSERT(tc,
234 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
235 (len == strlen(dest) + 1));
236
237 src = "\xFF\x00\xFF\x00";
238 target = "ff00ff00";
239 dest = apr_pescape_hex(pool, src, 4, 0);
240 ABTS_STR_EQUAL(tc, target, dest);
241 apr_escape_hex(NULL, src, 4, 0, &len);
242 ABTS_ASSERT(tc,
243 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
244 (len == strlen(dest) + 1));
245
246 src = "\xFF\x00\xFF\x00";
247 target = "ff:00:ff:00";
248 dest = apr_pescape_hex(pool, src, 4, 1);
249 ABTS_STR_EQUAL(tc, target, dest);
250 apr_escape_hex(NULL, src, 4, 1, &len);
251 ABTS_ASSERT(tc,
252 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
253 (len == strlen(dest) + 1));
254
255 src = "ff:00:ff:00";
256 target = "\xFF\x00\xFF\x00";
257 vdest = apr_punescape_hex(pool, src, 1, &vlen);
258 ABTS_ASSERT(tc, "apr_punescape_hex target!=dest", memcmp(target, vdest, 4) == 0);
259 ABTS_INT_EQUAL(tc, (int)vlen, 4);
260 apr_unescape_hex(NULL, src, APR_ESCAPE_STRING, 1, &len);
261 ABTS_ASSERT(tc,
262 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, (apr_size_t)4),
263 (len == 4));
264
265 apr_pool_destroy(pool);
266 }
267
testescape(abts_suite * suite)268 abts_suite *testescape(abts_suite *suite)
269 {
270 suite = ADD_SUITE(suite);
271
272 abts_run_test(suite, test_escape, NULL);
273
274 return suite;
275 }
276