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