Apache HTTPD
testescape.c
Go to the documentation of this file.
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
27static void test_escape(abts_case *tc, void *data)
28{
30 const char *src, *target;
31 const char *dest;
32 const void *vdest;
34
36
37 src = "Hello World &;`'\"|*?~<>^()[]{}$\\";
38 target = "Hello World \\&\\;\\`\\'\\\"\\|\\*\\?\\~\\<\\>\\^\\(\\)\\[\\]\\{\\}\\$\\\\";
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));
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";
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));
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";
68
69 src = "Hello";
72
73 src = "Hello%20";
74 dest = apr_punescape_url(pool, src, " ", NULL, 0);
76
77 src = "Hello%20World";
78 target = "Hello World";
80 ABTS_STR_EQUAL(tc, target, dest);
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";
89 ABTS_STR_EQUAL(tc, target, dest);
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);
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";
106 ABTS_PTR_EQUAL(tc, src, dest);
107
108 src = "$-_.+!*'(),:@&=/~Hello World";
109 target = "$-_.+!*'(),:@&=%2f~Hello%20World";
111 ABTS_STR_EQUAL(tc, target, dest);
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";
119 ABTS_PTR_EQUAL(tc, src, dest);
120
121 src = "$-_.+!*'(),:@&=/~Hello World";
122 target = "./$-_.+!*'(),:@&=/~Hello%20World";
124 ABTS_STR_EQUAL(tc, target, dest);
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";
132 ABTS_PTR_EQUAL(tc, src, dest);
133
134 src = "$-_.+!*'(),:@&=/~Hello World";
135 target = "$-_.+!*'(),:@&=/~Hello%20World";
137 ABTS_STR_EQUAL(tc, target, dest);
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";
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";
150 ABTS_STR_EQUAL(tc, target, dest);
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";
158 ABTS_PTR_EQUAL(tc, src, dest);
159
160 src = "\xFF<>&\'\"Hello World";
161 target = "\xFF&lt;&gt;&amp;'&quot;Hello World";
163 ABTS_STR_EQUAL(tc, target, dest);
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";
172 ABTS_PTR_EQUAL(tc, src, dest);
173
174 src = "\xFF<>&\'\"Hello World";
175 target = "&#255;&lt;&gt;&amp;'&quot;Hello World";
177 ABTS_STR_EQUAL(tc, target, dest);
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";
185 ABTS_PTR_EQUAL(tc, src, dest);
186
187 src = "\xFF&lt;&gt;&amp;'&quot;Hello World";
188 target = "\xFF<>&\'\"Hello World";
190 ABTS_STR_EQUAL(tc, target, dest);
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 = "&#255;&lt;&gt;&amp;'&quot;Hello World";
197 target = "\xFF<>&\'\"Hello World";
199 ABTS_STR_EQUAL(tc, target, dest);
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 = "&#32;&lt;&gt;&amp;'&quot;Hello World";
206 target = " <>&\'\"Hello World";
208 ABTS_STR_EQUAL(tc, target, dest);
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";
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\"";
222 ABTS_STR_EQUAL(tc, target, dest);
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\\\"";
231 ABTS_STR_EQUAL(tc, target, dest);
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";
258 ABTS_ASSERT(tc, "apr_punescape_hex target!=dest", memcmp(target, vdest, 4) == 0);
259 ABTS_INT_EQUAL(tc, (int)vlen, 4);
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 src = "Parens R Us (for all your parenthetical needs) plus asterisk* \"+,;<>\\";
266 target = "Parens R Us (for all your parenthetical needs) plus asterisk* \\22\\2b\\2c\\3b\\3c\\3e\\5c";
268 ABTS_ASSERT(tc,
269 apr_psprintf(pool, "ldap escaped (%s) does not match expected output (%s)",
270 dest, target),
271 (strcmp(dest, target) == 0));
273 ABTS_ASSERT(tc,
274 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
275 (len == strlen(dest) + 1));
276
277 src = "Parens R Us (for all your parenthetical needs) plus asterisk* \"+,;<>\\";
278 target = "Parens R Us \\28for all your parenthetical needs\\29 plus asterisk\\2a \"+,;<>\\5c";
280 ABTS_ASSERT(tc,
281 apr_psprintf(pool, "ldap escaped (%s) does not match expected output (%s)",
282 dest, target),
283 (strcmp(dest, target) == 0));
285 ABTS_ASSERT(tc,
286 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
287 (len == strlen(dest) + 1));
288
289 src = "Parens R Us (for all your parenthetical needs) plus asterisk* \"+,;<>\\";
290 target = "Parens R Us \\28for all your parenthetical needs\\29 plus asterisk\\2a \\22\\2b\\2c\\3b\\3c\\3e\\5c";
292 ABTS_ASSERT(tc,
293 apr_psprintf(pool, "ldap escaped (%s) does not match expected output (%s)",
294 dest, target),
295 (strcmp(dest, target) == 0));
297 ABTS_ASSERT(tc,
298 apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
299 (len == strlen(dest) + 1));
300
302}
303
305{
306 suite = ADD_SUITE(suite);
307
309
310 return suite;
311}
const char apr_size_t len
Definition ap_regex.h:187
void abts_run_test(abts_suite *ts, test_func f, void *value)
Definition abts.c:175
#define ABTS_PTR_EQUAL(a, b, c)
Definition abts.h:126
#define ADD_SUITE(suite)
Definition abts.h:67
#define ABTS_STR_EQUAL(a, b, c)
Definition abts.h:123
#define ABTS_ASSERT(a, b, c)
Definition abts.h:130
#define ABTS_INT_EQUAL(a, b, c)
Definition abts.h:109
APR-UTIL Escaping.
APR Strings library.
const char * src
Definition apr_encode.h:167
#define APR_ESCAPE_LDAP_ALL
Definition apr_escape.h:71
#define APR_ESCAPE_STRING
Definition apr_escape.h:56
#define APR_ESCAPE_LDAP_FILTER
Definition apr_escape.h:66
#define APR_ESCAPE_LDAP_DN
Definition apr_escape.h:61
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
void * data
#define apr_pool_create(newpool, parent)
Definition apr_pools.h:322
return NULL
Definition mod_so.c:359
abts_suite * testescape(abts_suite *suite)
Definition testescape.c:304
static void test_escape(abts_case *tc, void *data)
Definition testescape.c:27