1251876Speter/* Licensed to the Apache Software Foundation (ASF) under one or more
2251876Speter * contributor license agreements.  See the NOTICE file distributed with
3251876Speter * this work for additional information regarding copyright ownership.
4251876Speter * The ASF licenses this file to You under the Apache License, Version 2.0
5251876Speter * (the "License"); you may not use this file except in compliance with
6251876Speter * the License.  You may obtain a copy of the License at
7251876Speter *
8251876Speter *     http://www.apache.org/licenses/LICENSE-2.0
9251876Speter *
10251876Speter * Unless required by applicable law or agreed to in writing, software
11251876Speter * distributed under the License is distributed on an "AS IS" BASIS,
12251876Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13251876Speter * See the License for the specific language governing permissions and
14251876Speter * limitations under the License.
15251876Speter */
16251876Speter
17251876Speter#include <stdio.h>
18251876Speter#include <stdlib.h>
19251876Speter
20251876Speter#include "testutil.h"
21251876Speter#include "apr_general.h"
22251876Speter#include "apr_strings.h"
23251876Speter#include "apr_uri.h"
24251876Speter
25251876Speterstruct aup_test {
26251876Speter    const char *uri;
27251876Speter    apr_status_t rv;
28251876Speter    const char *scheme;
29251876Speter    const char *hostinfo;
30251876Speter    const char *user;
31251876Speter    const char *password;
32251876Speter    const char *hostname;
33251876Speter    const char *port_str;
34251876Speter    const char *path;
35251876Speter    const char *query;
36251876Speter    const char *fragment;
37251876Speter    apr_port_t  port;
38251876Speter};
39251876Speter
40251876Speterstruct aup_test aup_tests[] =
41251876Speter{
42251876Speter    { "http://[/::1]/index.html", APR_EGENERAL },
43251876Speter    { "http://[", APR_EGENERAL },
44251876Speter    { "http://[?::1]/index.html", APR_EGENERAL },
45251876Speter
46251876Speter    {
47251876Speter        "http://127.0.0.1:9999/asdf.html",
48251876Speter        0, "http", "127.0.0.1:9999", NULL, NULL, "127.0.0.1", "9999", "/asdf.html", NULL, NULL, 9999
49251876Speter    },
50251876Speter    {
51251876Speter        "http://127.0.0.1:9999a/asdf.html",
52251876Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
53251876Speter    },
54251876Speter    {
55251876Speter        "http://[::127.0.0.1]:9999/asdf.html",
56251876Speter        0, "http", "[::127.0.0.1]:9999", NULL, NULL, "::127.0.0.1", "9999", "/asdf.html", NULL, NULL, 9999
57251876Speter    },
58251876Speter    {
59251876Speter        "http://[::127.0.0.1]:9999a/asdf.html",
60251876Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
61251876Speter    },
62251876Speter    {
63251876Speter        "/error/include/top.html",
64251876Speter        0, NULL, NULL, NULL, NULL, NULL, NULL, "/error/include/top.html", NULL, NULL, 0
65251876Speter    },
66251876Speter    {
67251876Speter        "/error/include/../contact.html.var",
68251876Speter        0, NULL, NULL, NULL, NULL, NULL, NULL, "/error/include/../contact.html.var", NULL, NULL, 0
69251876Speter    },
70251876Speter    {
71251876Speter        "/",
72251876Speter        0, NULL, NULL, NULL, NULL, NULL, NULL, "/", NULL, NULL, 0
73251876Speter    },
74251876Speter    {
75251876Speter        "/manual/",
76251876Speter        0, NULL, NULL, NULL, NULL, NULL, NULL, "/manual/", NULL, NULL, 0
77251876Speter    },
78251876Speter    {
79251876Speter        "/cocoon/developing/graphics/Using%20Databases-label_over.jpg",
80251876Speter        0, NULL, NULL, NULL, NULL, NULL, NULL, "/cocoon/developing/graphics/Using%20Databases-label_over.jpg", NULL, NULL, 0
81251876Speter    },
82251876Speter    {
83251876Speter        "http://sonyamt:garbage@127.0.0.1/filespace/",
84251876Speter        0, "http", "sonyamt:garbage@127.0.0.1", "sonyamt", "garbage", "127.0.0.1", NULL, "/filespace/", NULL, NULL, 0
85251876Speter    },
86251876Speter    {
87251876Speter        "http://sonyamt:garbage@[fe80::1]/filespace/",
88251876Speter        0, "http", "sonyamt:garbage@[fe80::1]", "sonyamt", "garbage", "fe80::1", NULL, "/filespace/", NULL, NULL, 0
89251876Speter    },
90251876Speter    {
91251876Speter        "http://sonyamt@[fe80::1]/filespace/?arg1=store",
92251876Speter        0, "http", "sonyamt@[fe80::1]", "sonyamt", NULL, "fe80::1", NULL, "/filespace/", "arg1=store", NULL, 0
93251876Speter    },
94251876Speter    {
95251876Speter        "http://localhost",
96251876Speter        0, "http", "localhost", NULL, NULL, "localhost", NULL, NULL, NULL, NULL, 0
97251876Speter    },
98251876Speter    {
99251876Speter        "//www.apache.org/",
100251876Speter        0, NULL, "www.apache.org", NULL, NULL, "www.apache.org", NULL, "/", NULL, NULL, 0
101251876Speter    },
102251876Speter    {
103251876Speter        "file:image.jpg",
104251876Speter        0, "file", NULL, NULL, NULL, NULL, NULL, "image.jpg", NULL, NULL, 0
105251876Speter    },
106251876Speter    {
107251876Speter        "file:/image.jpg",
108251876Speter        0, "file", NULL, NULL, NULL, NULL, NULL, "/image.jpg", NULL, NULL, 0
109251876Speter    },
110251876Speter    {
111251876Speter        "file:///image.jpg",
112251876Speter        0, "file", "", NULL, NULL, "", NULL, "/image.jpg", NULL, NULL, 0
113251876Speter    },
114251876Speter    {
115251876Speter        "file:///tmp/photos/image.jpg",
116251876Speter        0, "file", "", NULL, NULL, "", NULL, "/tmp/photos/image.jpg", NULL, NULL, 0
117251876Speter    },
118251876Speter    {
119251876Speter        "file:./image.jpg",
120251876Speter        0, "file", NULL, NULL, NULL, NULL, NULL, "./image.jpg", NULL, NULL, 0
121251876Speter    },
122251876Speter    {
123251876Speter        "file:../photos/image.jpg",
124251876Speter        0, "file", NULL, NULL, NULL, NULL, NULL, "../photos/image.jpg", NULL, NULL, 0
125251876Speter    },
126253734Speter    {
127253734Speter        "file+ssh-2:../photos/image.jpg",
128253734Speter        0, "file+ssh-2", NULL, NULL, NULL, NULL, NULL, "../photos/image.jpg", NULL, NULL, 0
129253734Speter    },
130253734Speter    {
131253734Speter        "script/foo.js",
132253734Speter        0, NULL, NULL, NULL, NULL, NULL, NULL, "script/foo.js", NULL, NULL, 0
133253734Speter    },
134253734Speter    {
135253734Speter        "../foo2.js",
136253734Speter        0, NULL, NULL, NULL, NULL, NULL, NULL, "../foo2.js", NULL, NULL, 0
137253734Speter    },
138253734Speter    {
139253734Speter        "foo3.js",
140253734Speter        0, NULL, NULL, NULL, NULL, NULL, NULL, "foo3.js", NULL, NULL, 0
141253734Speter    },
142253734Speter    {
143253734Speter        "_foo/bar",
144253734Speter        0, NULL, NULL, NULL, NULL, NULL, NULL, "_foo/bar", NULL, NULL, 0
145253734Speter    },
146253734Speter    {
147253734Speter        "_foo:/bar",
148253734Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
149253734Speter    },
150253734Speter    {
151253734Speter        "2foo:/bar",
152253734Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
153253734Speter    },
154253734Speter    {
155253734Speter        ".foo:/bar",
156253734Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
157253734Speter    },
158253734Speter    {
159253734Speter        "-foo:/bar",
160253734Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
161253734Speter    },
162253734Speter    {
163253734Speter        "+foo:/bar",
164253734Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
165253734Speter    },
166253734Speter    {
167253734Speter        "::/bar",
168253734Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
169253734Speter    },
170253734Speter    {
171253734Speter        ":/bar",
172253734Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
173253734Speter    },
174253734Speter    {
175253734Speter        ":foo",
176253734Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
177253734Speter    },
178253734Speter    {
179253734Speter        ":",
180253734Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
181253734Speter    },
182253734Speter    {
183253734Speter        "@localhost::8080",
184253734Speter        APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
185253734Speter    },
186251876Speter};
187251876Speter
188251876Speterstruct uph_test {
189251876Speter    const char *hostinfo;
190251876Speter    apr_status_t rv;
191251876Speter    const char *hostname;
192251876Speter    const char *port_str;
193251876Speter    apr_port_t port;
194251876Speter};
195251876Speter
196251876Speterstruct uph_test uph_tests[] =
197251876Speter{
198251876Speter    {
199251876Speter        "www.ibm.com:443",
200251876Speter        0, "www.ibm.com", "443", 443
201251876Speter    },
202251876Speter    {
203251876Speter        "[fe80::1]:443",
204251876Speter        0, "fe80::1", "443", 443
205251876Speter    },
206251876Speter    {
207251876Speter        "127.0.0.1:443",
208251876Speter        0, "127.0.0.1", "443", 443
209251876Speter    },
210251876Speter    {
211251876Speter        "127.0.0.1",
212251876Speter        APR_EGENERAL, NULL, NULL, 0
213251876Speter    },
214251876Speter    {
215251876Speter        "[fe80:80",
216251876Speter        APR_EGENERAL, NULL, NULL, 0
217251876Speter    },
218251876Speter    {
219251876Speter        "fe80::80]:443",
220251876Speter        APR_EGENERAL, NULL, NULL, 0
221251876Speter    }
222251876Speter};
223251876Speter
224251876Speter#if 0
225251876Speterstatic void show_info(apr_status_t rv, apr_status_t expected, const apr_uri_t *info)
226251876Speter{
227251876Speter    if (rv != expected) {
228251876Speter        fprintf(stderr, "  actual rv: %d    expected rv:  %d\n", rv, expected);
229251876Speter    }
230251876Speter    else {
231251876Speter        fprintf(stderr,
232251876Speter                "  scheme:           %s\n"
233251876Speter                "  hostinfo:         %s\n"
234251876Speter                "  user:             %s\n"
235251876Speter                "  password:         %s\n"
236251876Speter                "  hostname:         %s\n"
237251876Speter                "  port_str:         %s\n"
238251876Speter                "  path:             %s\n"
239251876Speter                "  query:            %s\n"
240251876Speter                "  fragment:         %s\n"
241251876Speter                "  hostent:          %p\n"
242251876Speter                "  port:             %u\n"
243251876Speter                "  is_initialized:   %u\n"
244251876Speter                "  dns_looked_up:    %u\n"
245251876Speter                "  dns_resolved:     %u\n",
246251876Speter                info->scheme, info->hostinfo, info->user, info->password,
247251876Speter                info->hostname, info->port_str, info->path, info->query,
248251876Speter                info->fragment, info->hostent, info->port, info->is_initialized,
249251876Speter                info->dns_looked_up, info->dns_resolved);
250251876Speter    }
251251876Speter}
252251876Speter#endif
253251876Speter
254251876Speterstatic void test_aup(abts_case *tc, void *data)
255251876Speter{
256251876Speter    int i;
257251876Speter    apr_status_t rv;
258251876Speter    apr_uri_t info;
259251876Speter    struct aup_test *t;
260251876Speter    const char *s = NULL;
261251876Speter
262251876Speter    for (i = 0; i < sizeof(aup_tests) / sizeof(aup_tests[0]); i++) {
263251876Speter        char msg[256];
264251876Speter
265251876Speter        memset(&info, 0, sizeof(info));
266251876Speter        t = &aup_tests[i];
267251876Speter        rv = apr_uri_parse(p, t->uri, &info);
268251876Speter        apr_snprintf(msg, sizeof msg, "uri '%s': rv=%d not %d", t->uri,
269251876Speter                     rv, t->rv);
270251876Speter        ABTS_ASSERT(tc, msg, rv == t->rv);
271251876Speter        if (t->rv == APR_SUCCESS) {
272251876Speter            ABTS_STR_EQUAL(tc, t->scheme, info.scheme);
273251876Speter            ABTS_STR_EQUAL(tc, t->hostinfo, info.hostinfo);
274251876Speter            ABTS_STR_EQUAL(tc, t->user, info.user);
275251876Speter            ABTS_STR_EQUAL(tc, t->password, info.password);
276251876Speter            ABTS_STR_EQUAL(tc, t->hostname, info.hostname);
277251876Speter            ABTS_STR_EQUAL(tc, t->port_str, info.port_str);
278251876Speter            ABTS_STR_EQUAL(tc, t->path, info.path);
279251876Speter            ABTS_STR_EQUAL(tc, t->query, info.query);
280251876Speter            ABTS_STR_EQUAL(tc, t->user, info.user);
281251876Speter            ABTS_INT_EQUAL(tc, t->port, info.port);
282251876Speter
283251876Speter            s = apr_uri_unparse(p, &info, APR_URI_UNP_REVEALPASSWORD);
284251876Speter            ABTS_STR_EQUAL(tc, t->uri, s);
285251876Speter
286251876Speter            s = apr_uri_unparse(p, &info, APR_URI_UNP_OMITSITEPART);
287251876Speter            rv = apr_uri_parse(p, s, &info);
288251876Speter            ABTS_STR_EQUAL(tc, info.scheme, NULL);
289251876Speter            ABTS_STR_EQUAL(tc, info.hostinfo, NULL);
290251876Speter            ABTS_STR_EQUAL(tc, info.user, NULL);
291251876Speter            ABTS_STR_EQUAL(tc, info.password, NULL);
292251876Speter            ABTS_STR_EQUAL(tc, info.hostname, NULL);
293251876Speter            ABTS_STR_EQUAL(tc, info.port_str, NULL);
294251876Speter            ABTS_STR_EQUAL(tc, info.path, t->path);
295251876Speter            ABTS_STR_EQUAL(tc, info.query, t->query);
296251876Speter            ABTS_STR_EQUAL(tc, info.user, NULL);
297251876Speter            ABTS_INT_EQUAL(tc, info.port, 0);
298251876Speter        }
299251876Speter    }
300251876Speter}
301251876Speter
302251876Speterstatic void test_uph(abts_case *tc, void *data)
303251876Speter{
304251876Speter    int i;
305251876Speter    apr_status_t rv;
306251876Speter    apr_uri_t info;
307251876Speter    struct uph_test *t;
308251876Speter
309251876Speter    for (i = 0; i < sizeof(uph_tests) / sizeof(uph_tests[0]); i++) {
310251876Speter        memset(&info, 0, sizeof(info));
311251876Speter        t = &uph_tests[i];
312251876Speter        rv = apr_uri_parse_hostinfo(p, t->hostinfo, &info);
313251876Speter        ABTS_INT_EQUAL(tc, t->rv, rv);
314251876Speter        if (t->rv == APR_SUCCESS) {
315251876Speter            ABTS_STR_EQUAL(tc, t->hostname, info.hostname);
316251876Speter            ABTS_STR_EQUAL(tc, t->port_str, info.port_str);
317251876Speter            ABTS_INT_EQUAL(tc, t->port, info.port);
318251876Speter        }
319251876Speter    }
320251876Speter}
321251876Speter
322251876Speterabts_suite *testuri(abts_suite *suite)
323251876Speter{
324251876Speter    suite = ADD_SUITE(suite);
325251876Speter
326251876Speter    abts_run_test(suite, test_aup, NULL);
327251876Speter    abts_run_test(suite, test_uph, NULL);
328251876Speter
329251876Speter    return suite;
330251876Speter}
331251876Speter
332