upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr-util / test / testuri.c
1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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 <stdio.h>
18 #include <stdlib.h>
19
20 #include "apr_general.h"
21 #include "apr_uri.h"
22
23 struct aup_test {
24     const char *uri;
25     apr_status_t rv;
26     const char *scheme;
27     const char *hostinfo;
28     const char *user;
29     const char *password;
30     const char *hostname;
31     const char *port_str;
32     const char *path;
33     const char *query;
34     const char *fragment;
35     apr_port_t  port;
36 };
37
38 struct aup_test aup_tests[] =
39 {
40     { "http://[/::1]/index.html", APR_EGENERAL },
41     { "http://[", APR_EGENERAL },
42     { "http://[?::1]/index.html", APR_EGENERAL },
43
44
45     {
46         "http://127.0.0.1:9999/asdf.html",
47         0, "http", "127.0.0.1:9999", NULL, NULL, "127.0.0.1", "9999", "/asdf.html", NULL, NULL, 9999
48     },
49     {
50         "http://127.0.0.1:9999a/asdf.html",
51         APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
52     },
53     {
54         "http://[::127.0.0.1]:9999/asdf.html",
55         0, "http", "[::127.0.0.1]:9999", NULL, NULL, "::127.0.0.1", "9999", "/asdf.html", NULL, NULL, 9999
56     },
57     {
58         "http://[::127.0.0.1]:9999a/asdf.html",
59         APR_EGENERAL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
60     },
61     {
62         "/error/include/top.html",
63         0, NULL, NULL, NULL, NULL, NULL, NULL, "/error/include/top.html", NULL, NULL, 0
64     },
65     {
66         "/error/include/../contact.html.var",
67         0, NULL, NULL, NULL, NULL, NULL, NULL, "/error/include/../contact.html.var", NULL, NULL, 0
68     },
69     {
70         "/",
71         0, NULL, NULL, NULL, NULL, NULL, NULL, "/", NULL, NULL, 0
72     },
73     {
74         "/manual/",
75         0, NULL, NULL, NULL, NULL, NULL, NULL, "/manual/", NULL, NULL, 0
76     },
77     {
78         "/cocoon/developing/graphics/Using%20Databases-label_over.jpg",
79         0, NULL, NULL, NULL, NULL, NULL, NULL, "/cocoon/developing/graphics/Using%20Databases-label_over.jpg", NULL, NULL, 0
80     },
81     {
82         "http://sonyamt:garbage@127.0.0.1/filespace/",
83         0, "http", "sonyamt:garbage@127.0.0.1", "sonyamt", "garbage", "127.0.0.1", NULL, "/filespace/", NULL, NULL, 0
84     },
85     {
86         "http://sonyamt:garbage@[fe80::1]/filespace/",
87         0, "http", "sonyamt:garbage@[fe80::1]", "sonyamt", "garbage", "fe80::1", NULL, "/filespace/", NULL, NULL, 0
88     },
89     {
90         "http://sonyamt@[fe80::1]/filespace/?arg1=store",
91         0, "http", "sonyamt@[fe80::1]", "sonyamt", NULL, "fe80::1", NULL, "/filespace/", "arg1=store", NULL, 0
92     }
93 };
94
95 struct uph_test {
96     const char *hostinfo;
97     apr_status_t rv;
98     const char *hostname;
99     const char *port_str;
100     apr_port_t port;
101 };
102
103 struct uph_test uph_tests[] =
104 {
105     {
106         "www.ibm.com:443",
107         0, "www.ibm.com", "443", 443
108     },
109     {
110         "[fe80::1]:443",
111         0, "fe80::1", "443", 443
112     },
113     {
114         "127.0.0.1:443",
115         0, "127.0.0.1", "443", 443
116     },
117     {
118         "127.0.0.1",
119         APR_EGENERAL, NULL, NULL, 0
120     },
121     {
122         "[fe80:80",
123         APR_EGENERAL, NULL, NULL, 0
124     },
125     {
126         "fe80::80]:443",
127         APR_EGENERAL, NULL, NULL, 0
128     }
129 };
130
131 static void show_info(apr_status_t rv, apr_status_t expected, const apr_uri_t *info)
132 {
133     if (rv != expected) {
134         fprintf(stderr, "  actual rv: %d    expected rv:  %d\n", rv, expected);
135     }
136     else {
137         fprintf(stderr, 
138                 "  scheme:           %s\n"
139                 "  hostinfo:         %s\n"
140                 "  user:             %s\n"
141                 "  password:         %s\n"
142                 "  hostname:         %s\n"
143                 "  port_str:         %s\n"
144                 "  path:             %s\n"
145                 "  query:            %s\n"
146                 "  fragment:         %s\n"
147                 "  hostent:          %p\n"
148                 "  port:             %u\n"
149                 "  is_initialized:   %u\n"
150                 "  dns_looked_up:    %u\n"
151                 "  dns_resolved:     %u\n",
152                 info->scheme, info->hostinfo, info->user, info->password,
153                 info->hostname, info->port_str, info->path, info->query,
154                 info->fragment, info->hostent, info->port, info->is_initialized,
155                 info->dns_looked_up, info->dns_resolved);
156     }
157 }
158
159 static int same_str(const char *s1, const char *s2)
160 {
161     if (s1 == s2) { /* e.g., NULL and NULL */
162         return 1;
163     }
164     else if (!s1 || !s2) { /* only 1 is NULL */
165         return 0;
166     }
167     else {
168         return strcmp(s1, s2) == 0;
169     }
170 }
171
172 static int test_aup(apr_pool_t *p)
173 {
174     int i;
175     apr_status_t rv;
176     apr_uri_t info;
177     struct aup_test *t;
178     const char *failed;
179     int rc = 0;
180
181     for (i = 0; i < sizeof(aup_tests) / sizeof(aup_tests[0]); i++) {
182         memset(&info, 0, sizeof(info));
183         t = &aup_tests[i];
184         rv = apr_uri_parse(p, t->uri, &info);
185         failed = (rv != t->rv) ? "bad rc" : NULL;
186         if (!failed && t->rv == APR_SUCCESS) {
187             if (!same_str(info.scheme, t->scheme))
188                 failed = "bad scheme";
189             if (!same_str(info.hostinfo, t->hostinfo))
190                 failed = "bad hostinfo";
191             if (!same_str(info.user, t->user))
192                 failed = "bad user";
193             if (!same_str(info.password, t->password))
194                 failed = "bad password";
195             if (!same_str(info.hostname, t->hostname))
196                 failed = "bad hostname";
197             if (!same_str(info.port_str, t->port_str))
198                 failed = "bad port_str";
199             if (!same_str(info.path, t->path))
200                 failed = "bad path";
201             if (!same_str(info.query, t->query))
202                 failed = "bad query";
203             if (!same_str(info.fragment, t->fragment))
204                 failed = "bad fragment";
205             if (info.port != t->port)
206                 failed = "bad port";
207         }
208         if (failed) {
209             ++rc;
210             fprintf(stderr, "failure for testcase %d/uri %s: %s\n", i,
211                     t->uri, failed);
212             show_info(rv, t->rv, &info);
213         }
214         else if (t->rv == APR_SUCCESS) {
215             const char *s = apr_uri_unparse(p, &info,
216                                             APR_URI_UNP_REVEALPASSWORD);
217
218             if (strcmp(s, t->uri)) {
219                 fprintf(stderr, "apr_uri_unparsed failed for testcase %d\n", i);
220                 fprintf(stderr, "  got %s, expected %s\n", s, t->uri);
221             }
222         }
223     }
224
225     return rc;
226 }
227
228 static int test_uph(apr_pool_t *p)
229 {
230     int i;
231     apr_status_t rv;
232     apr_uri_t info;
233     struct uph_test *t;
234     const char *failed;
235     int rc = 0;
236
237     for (i = 0; i < sizeof(uph_tests) / sizeof(uph_tests[0]); i++) {
238         memset(&info, 0, sizeof(info));
239         t = &uph_tests[i];
240         rv = apr_uri_parse_hostinfo(p, t->hostinfo, &info);
241         failed = (rv != t->rv) ? "bad rc" : NULL;
242         if (!failed && t->rv == APR_SUCCESS) {
243             if (!same_str(info.hostname, t->hostname))
244                 failed = "bad hostname";
245             if (!same_str(info.port_str, t->port_str))
246                 failed = "bad port_str";
247             if (info.port != t->port)
248                 failed = "bad port";
249         }
250         if (failed) {
251             ++rc;
252             fprintf(stderr, "failure for testcase %d/hostinfo %s: %s\n", i,
253                     t->hostinfo, failed);
254             show_info(rv, t->rv, &info);
255         }
256     }
257
258     return rc;
259 }
260
261 int main(void)
262 {
263     apr_pool_t *pool;
264     int rc;
265
266     apr_initialize();
267     atexit(apr_terminate);
268
269     apr_pool_create(&pool, NULL);
270
271     rc = test_aup(pool);
272
273     if (!rc) {
274         rc = test_uph(pool);
275     }
276     
277     return rc;
278 }