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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
21
#include "apr_escape.h"
22
#include "apr_strings.h"
27
static void test_escape(abts_case *tc, void *data)
30
const char *src, *target;
35
apr_pool_create(&pool, NULL);
37
src = "Hello World &;`'\"|*?~<>^()[]{}$\\";
38
target = "Hello World \\&\\;\\`\\'\\\"\\|\\*\\?\\~\\<\\>\\^\\(\\)\\[\\]\\{\\}\\$\\\\";
39
dest = apr_pescape_shell(pool, src);
41
apr_psprintf(pool, "shell escaped (%s) does not match expected output (%s)",
43
(strcmp(dest, target) == 0));
44
apr_escape_shell(NULL, src, APR_ESCAPE_STRING, &len);
46
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
47
(len == strlen(dest) + 1));
49
#if !(defined(OS2) || defined(WIN32))
50
/* Now try with newline, which is converted to a space on OS/2 and Windows.
52
src = "Hello World &;`'\"|*?~<>^()[]{}$\\\n";
53
target = "Hello World \\&\\;\\`\\'\\\"\\|\\*\\?\\~\\<\\>\\^\\(\\)\\[\\]\\{\\}\\$\\\\\\\n";
54
dest = apr_pescape_shell(pool, src);
56
apr_psprintf(pool, "shell escaped (%s) does not match expected output (%s)",
58
(strcmp(dest, target) == 0));
59
apr_escape_shell(NULL, src, APR_ESCAPE_STRING, &len);
61
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
62
(len == strlen(dest) + 1));
66
dest = apr_punescape_url(pool, src, NULL, NULL, 0);
67
ABTS_PTR_EQUAL(tc, src, dest);
70
dest = apr_punescape_url(pool, src, NULL, NULL, 1);
71
ABTS_PTR_EQUAL(tc, src, dest);
74
dest = apr_punescape_url(pool, src, " ", NULL, 0);
75
ABTS_PTR_EQUAL(tc, NULL, dest);
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);
83
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
84
(len == strlen(dest) + 1));
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);
92
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
93
(len == strlen(dest) + 1));
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);
101
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
102
(len == strlen(dest) + 1));
105
dest = apr_pescape_path_segment(pool, src);
106
ABTS_PTR_EQUAL(tc, src, dest);
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);
114
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
115
(len == strlen(dest) + 1));
118
dest = apr_pescape_path(pool, src, 0);
119
ABTS_PTR_EQUAL(tc, src, dest);
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);
127
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
128
(len == strlen(dest) + 1));
131
dest = apr_pescape_path(pool, src, 1);
132
ABTS_PTR_EQUAL(tc, src, dest);
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);
140
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
141
(len == strlen(dest) + 1));
144
dest = apr_pescape_urlencoded(pool, src);
145
ABTS_PTR_EQUAL(tc, src, dest);
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);
153
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
154
(len == strlen(dest) + 1));
157
dest = apr_pescape_entity(pool, src, 0);
158
ABTS_PTR_EQUAL(tc, src, dest);
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);
166
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
167
(len == strlen(dest) + 1));
169
#if !APR_CHARSET_EBCDIC
171
dest = apr_pescape_entity(pool, src, 1);
172
ABTS_PTR_EQUAL(tc, src, dest);
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);
180
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
181
(len == strlen(dest) + 1));
184
dest = apr_punescape_entity(pool, src);
185
ABTS_PTR_EQUAL(tc, src, dest);
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);
193
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
194
(len == strlen(dest) + 1));
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);
202
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
203
(len == strlen(dest) + 1));
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);
211
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
212
(len == strlen(dest) + 1));
216
dest = apr_pescape_echo(pool, src, 0);
217
ABTS_PTR_EQUAL(tc, src, dest);
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);
225
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
226
(len == strlen(dest) + 1));
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);
234
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
235
(len == strlen(dest) + 1));
237
src = "\xFF\x00\xFF\x00";
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);
243
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
244
(len == strlen(dest) + 1));
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);
252
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, strlen(dest) + 1),
253
(len == strlen(dest) + 1));
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);
262
apr_psprintf(pool, "size mismatch (%" APR_SIZE_T_FMT "!=%" APR_SIZE_T_FMT ")", len, (apr_size_t)4),
265
apr_pool_destroy(pool);
268
abts_suite *testescape(abts_suite *suite)
270
suite = ADD_SUITE(suite);
272
abts_run_test(suite, test_escape, NULL);