1 | /* $Id: tstRTUri.cpp 58065 2015-10-06 18:49:19Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - URI parsing and creation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/uri.h>
|
---|
32 |
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include <iprt/test.h>
|
---|
37 |
|
---|
38 | #if 0 && defined(RT_OS_WINDOWS) /* Enable for windows API reference results. */
|
---|
39 | # define TSTRTURI_WITH_WINDOWS_REFERENCE_RESULTS
|
---|
40 | # include <Shlwapi.h>
|
---|
41 | # include <iprt/stream.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Test data *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 |
|
---|
49 | static struct
|
---|
50 | {
|
---|
51 | const char *pszUri;
|
---|
52 | const char *pszScheme;
|
---|
53 | const char *pszAuthority;
|
---|
54 | const char *pszPath;
|
---|
55 | const char *pszQuery;
|
---|
56 | const char *pszFragment;
|
---|
57 |
|
---|
58 | const char *pszUsername;
|
---|
59 | const char *pszPassword;
|
---|
60 | const char *pszHost;
|
---|
61 | uint32_t uPort;
|
---|
62 |
|
---|
63 | const char *pszCreated;
|
---|
64 | } g_aTests[] =
|
---|
65 | {
|
---|
66 | { /* #0 */
|
---|
67 | "foo://tt:[email protected]:8042/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there?name=%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60ferret#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
|
---|
68 | /*.pszScheme =*/ "foo",
|
---|
69 | /*.pszAuthority =*/ "tt:[email protected]:8042",
|
---|
70 | /*.pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
|
---|
71 | /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
|
---|
72 | /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
|
---|
73 | /*.pszUsername =*/ "tt",
|
---|
74 | /*.pszPassword =*/ "yt",
|
---|
75 | /*.pszHost =*/ "example.com",
|
---|
76 | /*.uPort =*/ 8042,
|
---|
77 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
78 | },
|
---|
79 | { /* #1 */
|
---|
80 | "foo://tt:[email protected]:8042/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there?name=%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60ferret",
|
---|
81 | /*.pszScheme =*/ "foo",
|
---|
82 | /*.pszAuthority =*/ "tt:[email protected]:8042",
|
---|
83 | /*.pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
|
---|
84 | /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
|
---|
85 | /*.pszFragment =*/ NULL,
|
---|
86 | /*.pszUsername =*/ "tt",
|
---|
87 | /*.pszPassword =*/ "yt",
|
---|
88 | /*.pszHost =*/ "example.com",
|
---|
89 | /*.uPort =*/ 8042,
|
---|
90 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
91 | },
|
---|
92 | { /* #2 */
|
---|
93 | "foo://tt:[email protected]:8042/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there",
|
---|
94 | /*.pszScheme =*/ "foo",
|
---|
95 | /*.pszAuthority =*/ "tt:[email protected]:8042",
|
---|
96 | /*.pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
|
---|
97 | /*.pszQuery =*/ NULL,
|
---|
98 | /*.pszFragment =*/ NULL,
|
---|
99 | /*.pszUsername =*/ "tt",
|
---|
100 | /*.pszPassword =*/ "yt",
|
---|
101 | /*.pszHost =*/ "example.com",
|
---|
102 | /*.uPort =*/ 8042,
|
---|
103 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
104 | },
|
---|
105 | { /* #3 */
|
---|
106 | "foo:[email protected]",
|
---|
107 | /*.pszScheme =*/ "foo",
|
---|
108 | /*.pszAuthority =*/ NULL,
|
---|
109 | /*.pszPath =*/ "[email protected]",
|
---|
110 | /*.pszQuery =*/ NULL,
|
---|
111 | /*.pszFragment =*/ NULL,
|
---|
112 | /*.pszUsername =*/ NULL,
|
---|
113 | /*.pszPassword =*/ NULL,
|
---|
114 | /*.pszHost =*/ NULL,
|
---|
115 | /*.uPort =*/ UINT32_MAX,
|
---|
116 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
117 | },
|
---|
118 | { /* #4 */
|
---|
119 | "foo:/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there?name=%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60ferret#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
|
---|
120 | /*.pszScheme =*/ "foo",
|
---|
121 | /*.pszAuthority =*/ NULL,
|
---|
122 | /*.pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
|
---|
123 | /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
|
---|
124 | /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
|
---|
125 | /*.pszUsername =*/ NULL,
|
---|
126 | /*.pszPassword =*/ NULL,
|
---|
127 | /*.pszHost =*/ NULL,
|
---|
128 | /*.uPort =*/ UINT32_MAX,
|
---|
129 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
130 | },
|
---|
131 | { /* #5 */
|
---|
132 | "foo:/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
|
---|
133 | /*.pszScheme =*/ "foo",
|
---|
134 | /*.pszAuthority =*/ NULL,
|
---|
135 | /*.pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
|
---|
136 | /*.pszQuery =*/ NULL,
|
---|
137 | /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
|
---|
138 | /*.pszUsername =*/ NULL,
|
---|
139 | /*.pszPassword =*/ NULL,
|
---|
140 | /*.pszHost =*/ NULL,
|
---|
141 | /*.uPort =*/ UINT32_MAX,
|
---|
142 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
143 | },
|
---|
144 | { /* #6 */
|
---|
145 | "urn:example:animal:ferret:nose",
|
---|
146 | /*.pszScheme =*/ "urn",
|
---|
147 | /*.pszAuthority =*/ NULL,
|
---|
148 | /*.pszPath =*/ "example:animal:ferret:nose",
|
---|
149 | /*.pszQuery =*/ NULL,
|
---|
150 | /*.pszFragment =*/ NULL,
|
---|
151 | /*.pszUsername =*/ NULL,
|
---|
152 | /*.pszPassword =*/ NULL,
|
---|
153 | /*.pszHost =*/ NULL,
|
---|
154 | /*.uPort =*/ UINT32_MAX,
|
---|
155 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
156 | },
|
---|
157 | { /* #7 */
|
---|
158 | "foo:?name=%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60ferret#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
|
---|
159 | /*.pszScheme =*/ "foo",
|
---|
160 | /*.pszAuthority =*/ NULL,
|
---|
161 | /*.pszPath =*/ NULL,
|
---|
162 | /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
|
---|
163 | /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
|
---|
164 | /*.pszUsername =*/ NULL,
|
---|
165 | /*.pszPassword =*/ NULL,
|
---|
166 | /*.pszHost =*/ NULL,
|
---|
167 | /*.uPort =*/ UINT32_MAX,
|
---|
168 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
169 | },
|
---|
170 | { /* #8 */
|
---|
171 | "foo:#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
|
---|
172 | /*.pszScheme =*/ "foo",
|
---|
173 | /*.pszAuthority =*/ NULL,
|
---|
174 | /*.pszPath =*/ NULL,
|
---|
175 | /*.pszQuery =*/ NULL,
|
---|
176 | /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
|
---|
177 | /*.pszUsername =*/ NULL,
|
---|
178 | /*.pszPassword =*/ NULL,
|
---|
179 | /*.pszHost =*/ NULL,
|
---|
180 | /*.uPort =*/ UINT32_MAX,
|
---|
181 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
182 | },
|
---|
183 | { /* #9 */
|
---|
184 | "foo://tt:[email protected]:8042/?name=%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60ferret#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
|
---|
185 | /*.pszScheme =*/ "foo",
|
---|
186 | /*.pszAuthority =*/ "tt:[email protected]:8042",
|
---|
187 | /*.pszPath =*/ "/",
|
---|
188 | /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
|
---|
189 | /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
|
---|
190 | /*.pszUsername =*/ "tt",
|
---|
191 | /*.pszPassword =*/ "yt",
|
---|
192 | /*.pszHost =*/ "example.com",
|
---|
193 | /*.uPort =*/ 8042,
|
---|
194 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
195 | },
|
---|
196 | { /* #10 */
|
---|
197 | "foo://tt:[email protected]:8042/",
|
---|
198 | /*.pszScheme =*/ "foo",
|
---|
199 | /*.pszAuthority =*/ "tt:[email protected]:8042",
|
---|
200 | /*.pszPath =*/ "/",
|
---|
201 | /*.pszQuery =*/ NULL,
|
---|
202 | /*.pszFragment =*/ NULL,
|
---|
203 | /*.pszUsername =*/ "tt",
|
---|
204 | /*.pszPassword =*/ "yt",
|
---|
205 | /*.pszHost =*/ "example.com",
|
---|
206 | /*.uPort =*/ 8042,
|
---|
207 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
208 | },
|
---|
209 | { /* #11 */
|
---|
210 | "foo://tt:[email protected]:8042?name=%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60ferret#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
|
---|
211 | /*.pszScheme =*/ "foo",
|
---|
212 | /*.pszAuthority =*/ "tt:[email protected]:8042",
|
---|
213 | /*.pszPath =*/ NULL,
|
---|
214 | /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
|
---|
215 | /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
|
---|
216 | /*.pszUsername =*/ "tt",
|
---|
217 | /*.pszPassword =*/ "yt",
|
---|
218 | /*.pszHost =*/ "example.com",
|
---|
219 | /*.uPort =*/ 8042,
|
---|
220 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
221 | },
|
---|
222 | { /* #12 */
|
---|
223 | "foo://tt:[email protected]:8042#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
|
---|
224 | /*.pszScheme =*/ "foo",
|
---|
225 | /*.pszAuthority =*/ "tt:[email protected]:8042",
|
---|
226 | /*.pszPath =*/ NULL,
|
---|
227 | /*.pszQuery =*/ NULL,
|
---|
228 | /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
|
---|
229 | /*.pszUsername =*/ "tt",
|
---|
230 | /*.pszPassword =*/ "yt",
|
---|
231 | /*.pszHost =*/ "example.com",
|
---|
232 | /*.uPort =*/ 8042,
|
---|
233 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
234 | },
|
---|
235 | { /* #13 */
|
---|
236 | "foo://tt:[email protected]:8042",
|
---|
237 | /*.pszScheme =*/ "foo",
|
---|
238 | /*.pszAuthority =*/ "tt:[email protected]:8042",
|
---|
239 | /*.pszPath =*/ NULL,
|
---|
240 | /*.pszQuery =*/ NULL,
|
---|
241 | /*.pszFragment =*/ NULL,
|
---|
242 | /*.pszUsername =*/ "tt",
|
---|
243 | /*.pszPassword =*/ "yt",
|
---|
244 | /*.pszHost =*/ "example.com",
|
---|
245 | /*.uPort =*/ 8042,
|
---|
246 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
247 | },
|
---|
248 | { /* #14 */
|
---|
249 | "file:///dir/dir/file",
|
---|
250 | /*.pszScheme =*/ "file",
|
---|
251 | /*.pszAuthority =*/ "",
|
---|
252 | /*.pszPath =*/ "/dir/dir/file",
|
---|
253 | /*.pszQuery =*/ NULL,
|
---|
254 | /*.pszFragment =*/ NULL,
|
---|
255 | /*.pszUsername =*/ NULL,
|
---|
256 | /*.pszPassword =*/ NULL,
|
---|
257 | /*.pszHost =*/ NULL,
|
---|
258 | /*.uPort =*/ UINT32_MAX,
|
---|
259 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
260 | },
|
---|
261 | { /* #15 */
|
---|
262 | "foo:///",
|
---|
263 | /*.pszScheme =*/ "foo",
|
---|
264 | /*.pszAuthority =*/ "",
|
---|
265 | /*.pszPath =*/ "/",
|
---|
266 | /*.pszQuery =*/ NULL,
|
---|
267 | /*.pszFragment =*/ NULL,
|
---|
268 | /*.pszUsername =*/ NULL,
|
---|
269 | /*.pszPassword =*/ NULL,
|
---|
270 | /*.pszHost =*/ NULL,
|
---|
271 | /*.uPort =*/ UINT32_MAX,
|
---|
272 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
273 | },
|
---|
274 | { /* #16 */
|
---|
275 | "foo://",
|
---|
276 | /*.pszScheme =*/ "foo",
|
---|
277 | /*.pszAuthority =*/ "",
|
---|
278 | /*.pszPath =*/ NULL,
|
---|
279 | /*.pszQuery =*/ NULL,
|
---|
280 | /*.pszFragment =*/ NULL,
|
---|
281 | /*.pszUsername =*/ NULL,
|
---|
282 | /*.pszPassword =*/ NULL,
|
---|
283 | /*.pszHost =*/ NULL,
|
---|
284 | /*.uPort =*/ UINT32_MAX,
|
---|
285 | /*.pszCreated =*/ NULL /* same as pszUri*/,
|
---|
286 | },
|
---|
287 | { /* #17 - UTF-8 escape sequences. */
|
---|
288 | "http://example.com/%ce%b3%ce%bb%cf%83%ce%b1%20%e0%a4%95\xe0\xa4\x95",
|
---|
289 | /*.pszScheme =*/ "http",
|
---|
290 | /*.pszAuthority =*/ "example.com",
|
---|
291 | /*.pszPath =*/ "/\xce\xb3\xce\xbb\xcf\x83\xce\xb1 \xe0\xa4\x95\xe0\xa4\x95",
|
---|
292 | /*.pszQuery =*/ NULL,
|
---|
293 | /*.pszFragment =*/ NULL,
|
---|
294 | /*.pszUsername =*/ NULL,
|
---|
295 | /*.pszPassword =*/ NULL,
|
---|
296 | /*.pszHost =*/ "example.com",
|
---|
297 | /*.uPort =*/ UINT32_MAX,
|
---|
298 | /*.pszCreated =*/ "http://example.com/\xce\xb3\xce\xbb\xcf\x83\xce\xb1%20\xe0\xa4\x95\xe0\xa4\x95",
|
---|
299 | },
|
---|
300 | };
|
---|
301 |
|
---|
302 |
|
---|
303 | static struct URIFILETEST
|
---|
304 | {
|
---|
305 | const char *pszPath;
|
---|
306 | const char *pszUri;
|
---|
307 | uint32_t uFormat;
|
---|
308 | const char *pszCreatedPath;
|
---|
309 | const char *pszCreatedUri;
|
---|
310 | } g_aCreateFileURIs[] =
|
---|
311 | {
|
---|
312 | { /* #0: */
|
---|
313 | /* .pszPath =*/ "C:\\over\\ <>#%\"{}|^[]`\\there",
|
---|
314 | /* .pszUri =*/ "file:///C:%5Cover%5C%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60%5Cthere",
|
---|
315 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
316 | /* .pszCreatedPath =*/ "C:\\over\\ <>#%\"{}|^[]`\\there",
|
---|
317 | /* .pszCreatedUri =*/ "file:///C:%5Cover%5C%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60%5Cthere",
|
---|
318 | /* PathCreateFromUrl = "C:\\over\\ <>#%\"{}|^[]`\\there" - same */
|
---|
319 | /* UrlCreateFromPath = "file:///C:/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there" - differs */
|
---|
320 | },
|
---|
321 | { /* #1: */
|
---|
322 | /* .pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
|
---|
323 | /* .pszUri =*/ "file:///over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there",
|
---|
324 | /* .uFormat =*/ URI_FILE_FORMAT_UNIX,
|
---|
325 | /* .pszCreatedPath =*/ "/over/ <>#%\"{}|^[]`/there",
|
---|
326 | /* .pszCreatedUri =*/ "file:///over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there",
|
---|
327 | /* PathCreateFromUrl = "\\over\\ <>#%\"{}|^[]`\\there" - differs */
|
---|
328 | /* UrlCreateFromPath = "file:///over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there" - same */
|
---|
329 | },
|
---|
330 | { /* #2: */
|
---|
331 | /* .pszPath =*/ NULL,
|
---|
332 | /* .pszUri =*/ "file://",
|
---|
333 | /* .uFormat =*/ URI_FILE_FORMAT_UNIX,
|
---|
334 | /* .pszCreatedPath =*/ NULL,
|
---|
335 | /* .pszCreatedUri =*/ "file://",
|
---|
336 | /* PathCreateFromUrl = "" - differs */
|
---|
337 | /* UrlCreateFromPath => 0x80070057 (E_INVALIDARG) */
|
---|
338 | },
|
---|
339 | { /* #3: */
|
---|
340 | /* .pszPath =*/ NULL,
|
---|
341 | /* .pszUri =*/ "file://",
|
---|
342 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
343 | /* .pszCreatedPath =*/ NULL,
|
---|
344 | /* .pszCreatedUri =*/ "file://",
|
---|
345 | /* PathCreateFromUrl = "" - differs */
|
---|
346 | /* UrlCreateFromPath => 0x80070057 (E_INVALIDARG) */
|
---|
347 | },
|
---|
348 | { /* #4: */
|
---|
349 | /* .pszPath =*/ "/",
|
---|
350 | /* .pszUri =*/ "file:///",
|
---|
351 | /* .uFormat =*/ URI_FILE_FORMAT_UNIX,
|
---|
352 | /* .pszCreatedPath =*/ "/",
|
---|
353 | /* .pszCreatedUri =*/ "file:///",
|
---|
354 | /* PathCreateFromUrl = "" - differs */
|
---|
355 | /* UrlCreateFromPath = "file:///" - same */
|
---|
356 | },
|
---|
357 | { /* #5: */
|
---|
358 | /* .pszPath =*/ "\\",
|
---|
359 | /* .pszUri =*/ "file:///",
|
---|
360 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
361 | /* .pszCreatedPath =*/ "\\",
|
---|
362 | /* .pszCreatedUri =*/ "file:///",
|
---|
363 | /* PathCreateFromUrl = "" - differs */
|
---|
364 | /* UrlCreateFromPath = "file:///" - same */
|
---|
365 | },
|
---|
366 | { /* #6: */
|
---|
367 | /* .pszPath =*/ "/foo/bar",
|
---|
368 | /* .pszUri =*/ "file:///foo/bar",
|
---|
369 | /* .uFormat =*/ URI_FILE_FORMAT_UNIX,
|
---|
370 | /* .pszCreatedPath =*/ "/foo/bar",
|
---|
371 | /* .pszCreatedUri =*/ "file:///foo/bar",
|
---|
372 | /* PathCreateFromUrl = "\\foo\\bar" - differs */
|
---|
373 | /* UrlCreateFromPath = "file:///foo/bar" - same */
|
---|
374 | },
|
---|
375 | { /* #7: */
|
---|
376 | /* .pszPath =*/ "\\foo\\bar",
|
---|
377 | /* .pszUri =*/ "file:///foo%5Cbar",
|
---|
378 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
379 | /* .pszCreatedPath =*/ "\\foo\\bar",
|
---|
380 | /* .pszCreatedUri =*/ "file:///foo%5Cbar",
|
---|
381 | /* PathCreateFromUrl = "\\foo\\bar" - same */
|
---|
382 | /* UrlCreateFromPath = "file:///foo/bar" - differs */
|
---|
383 | },
|
---|
384 | { /* #8: */
|
---|
385 | /* .pszPath =*/ "C:/over/ <>#%\"{}|^[]`/there",
|
---|
386 | /* .pszUri =*/ "file:///C:/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there",
|
---|
387 | /* .uFormat =*/ URI_FILE_FORMAT_UNIX,
|
---|
388 | /* .pszCreatedPath =*/ "C:/over/ <>#%\"{}|^[]`/there",
|
---|
389 | /* .pszCreatedUri =*/ "file:///C:/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there",
|
---|
390 | /* PathCreateFromUrl = "C:\\over\\ <>#%\"{}|^[]`\\there" - differs */
|
---|
391 | /* UrlCreateFromPath = "file:///C:/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there" - same */
|
---|
392 | },
|
---|
393 | { /* #9: */
|
---|
394 | /* .pszPath =*/ "\\over\\ <>#%\"{}|^[]`\\there",
|
---|
395 | /* .pszUri =*/ "file:///over%5C%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60%5Cthere",
|
---|
396 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
397 | /* .pszCreatedPath =*/ "\\over\\ <>#%\"{}|^[]`\\there",
|
---|
398 | /* .pszCreatedUri =*/ "file:///over%5C%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60%5Cthere",
|
---|
399 | /* PathCreateFromUrl = "\\over\\ <>#%\"{}|^[]`\\there" - same */
|
---|
400 | /* UrlCreateFromPath = "file:///over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there" - differs */
|
---|
401 | },
|
---|
402 | { /* #10: */
|
---|
403 | /* .pszPath =*/ "/usr/bin/grep",
|
---|
404 | /* .pszUri =*/ "file:///usr/bin/grep",
|
---|
405 | /* .uFormat =*/ URI_FILE_FORMAT_UNIX,
|
---|
406 | /* .pszCreatedPath =*/ "/usr/bin/grep",
|
---|
407 | /* .pszCreatedUri =*/ "file:///usr/bin/grep",
|
---|
408 | /* PathCreateFromUrl = "\\usr\\bin\\grep" - differs */
|
---|
409 | /* UrlCreateFromPath = "file:///usr/bin/grep" - same */
|
---|
410 | },
|
---|
411 | { /* #11: */
|
---|
412 | /* .pszPath =*/ "\\usr\\bin\\grep",
|
---|
413 | /* .pszUri =*/ "file:///usr%5Cbin%5Cgrep",
|
---|
414 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
415 | /* .pszCreatedPath =*/ "\\usr\\bin\\grep",
|
---|
416 | /* .pszCreatedUri =*/ "file:///usr%5Cbin%5Cgrep",
|
---|
417 | /* PathCreateFromUrl = "\\usr\\bin\\grep" - same */
|
---|
418 | /* UrlCreateFromPath = "file:///usr/bin/grep" - differs */
|
---|
419 | },
|
---|
420 | { /* #12: */
|
---|
421 | /* .pszPath =*/ "/somerootsubdir/isos/files.lst",
|
---|
422 | /* .pszUri =*/ "file:///somerootsubdir/isos/files.lst",
|
---|
423 | /* .uFormat =*/ URI_FILE_FORMAT_UNIX,
|
---|
424 | /* .pszCreatedPath =*/ "/somerootsubdir/isos/files.lst",
|
---|
425 | /* .pszCreatedUri =*/ "file:///somerootsubdir/isos/files.lst",
|
---|
426 | /* PathCreateFromUrl = "\\somerootsubdir\\isos\\files.lst" - differs */
|
---|
427 | /* UrlCreateFromPath = "file:///somerootsubdir/isos/files.lst" - same */
|
---|
428 | },
|
---|
429 | { /* #13: */
|
---|
430 | /* .pszPath =*/ "\\not-a-cifsserver\\isos\\files.lst",
|
---|
431 | /* .pszUri =*/ "file:///not-a-cifsserver%5Cisos%5Cfiles.lst",
|
---|
432 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
433 | /* .pszCreatedPath =*/ "\\not-a-cifsserver\\isos\\files.lst",
|
---|
434 | /* .pszCreatedUri =*/ "file:///not-a-cifsserver%5Cisos%5Cfiles.lst",
|
---|
435 | /* PathCreateFromUrl = "\\not-a-cifsserver\\isos\\files.lst" - same */
|
---|
436 | /* UrlCreateFromPath = "file:///not-a-cifsserver/isos/files.lst" - differs */
|
---|
437 | },
|
---|
438 | { /* #14: */
|
---|
439 | /* .pszPath =*/ "/rootsubdir/isos/files.lst",
|
---|
440 | /* .pszUri =*/ "file:///rootsubdir/isos/files.lst",
|
---|
441 | /* .uFormat =*/ URI_FILE_FORMAT_UNIX,
|
---|
442 | /* .pszCreatedPath =*/ "/rootsubdir/isos/files.lst",
|
---|
443 | /* .pszCreatedUri =*/ "file:///rootsubdir/isos/files.lst",
|
---|
444 | /* PathCreateFromUrl = "\\rootsubdir\\isos\\files.lst" - differs */
|
---|
445 | /* UrlCreateFromPath = "file:///rootsubdir/isos/files.lst" - same */
|
---|
446 | },
|
---|
447 | { /* #15: */
|
---|
448 | /* .pszPath =*/ "\\not-a-cifsserver-either\\isos\\files.lst",
|
---|
449 | /* .pszUri =*/ "file:///not-a-cifsserver-either%5Cisos%5Cfiles.lst",
|
---|
450 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
451 | /* .pszCreatedPath =*/ "\\not-a-cifsserver-either\\isos\\files.lst",
|
---|
452 | /* .pszCreatedUri =*/ "file:///not-a-cifsserver-either%5Cisos%5Cfiles.lst",
|
---|
453 | /* PathCreateFromUrl = "\\not-a-cifsserver-either\\isos\\files.lst" - same */
|
---|
454 | /* UrlCreateFromPath = "file:///not-a-cifsserver-either/isos/files.lst" - differs */
|
---|
455 | },
|
---|
456 | #if 0 /** @todo r=bird: this ain't working right. It's in the wikipedia article on file:// ... */
|
---|
457 | { /* #16: */
|
---|
458 | /* .pszPath =*/ "\\\\cifsserver\\isos\\files.lst",
|
---|
459 | /* .pszUri =*/ "file:////cifsserver/isos/files.lst",
|
---|
460 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
461 | /* .pszCreatedPath =*/ NULL, /* Same as pszPath. */
|
---|
462 | /* .pszCreatedUri =*/ "file://cifsserver/isos/files.lst",
|
---|
463 | /* PathCreateFromUrl = "\\\\cifsserver\\isos\\files.lst" - same */
|
---|
464 | /* UrlCreateFromPath = "file:////cifsserver/isos/files.lst" - differs */
|
---|
465 | },
|
---|
466 | { /* #17: */
|
---|
467 | /* .pszPath =*/ "c:boot.ini",
|
---|
468 | /* .pszUri =*/ "file://localhost/c:boot.ini",
|
---|
469 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
470 | /* .pszCreatedPath =*/ NULL, /* Same as pszPath. */
|
---|
471 | /* .pszCreatedUri =*/ "file:///c:boot.ini",
|
---|
472 | /* PathCreateFromUrl = "c:boot.ini" - same */
|
---|
473 | /* UrlCreateFromPath = "file:///c:boot.ini" - same */
|
---|
474 | },
|
---|
475 | { /* #18: */
|
---|
476 | /* .pszPath =*/ "c:boot.ini",
|
---|
477 | /* .pszUri =*/ "file:///c|boot.ini",
|
---|
478 | /* .uFormat =*/ URI_FILE_FORMAT_WIN,
|
---|
479 | /* .pszCreatedPath =*/ NULL, /* Same as pszPath. */
|
---|
480 | /* .pszCreatedUri =*/ "file:///c:boot.ini",
|
---|
481 | /* PathCreateFromUrl = "c:boot.ini" - same */
|
---|
482 | /* UrlCreateFromPath = "file:///c:boot.ini" - same */
|
---|
483 | },
|
---|
484 | #endif
|
---|
485 | };
|
---|
486 |
|
---|
487 | #ifdef TSTRTURI_WITH_WINDOWS_REFERENCE_RESULTS
|
---|
488 |
|
---|
489 | static void tstPrintCString(const char *pszString)
|
---|
490 | {
|
---|
491 | if (pszString)
|
---|
492 | {
|
---|
493 | char ch;
|
---|
494 | RTPrintf("\"");
|
---|
495 | while ((ch = *pszString++) != '\0')
|
---|
496 | {
|
---|
497 | if (ch >= 0x20 && ch < 0x7f)
|
---|
498 | switch (ch)
|
---|
499 | {
|
---|
500 | default:
|
---|
501 | RTPrintf("%c", ch);
|
---|
502 | break;
|
---|
503 | case '\\':
|
---|
504 | case '"':
|
---|
505 | RTPrintf("\\%c", ch);
|
---|
506 | break;
|
---|
507 | }
|
---|
508 | else
|
---|
509 | RTPrintf("\\x%02X", ch); /* good enough */
|
---|
510 | }
|
---|
511 | RTPrintf("\"");
|
---|
512 | }
|
---|
513 | else
|
---|
514 | RTPrintf("NULL");
|
---|
515 | }
|
---|
516 |
|
---|
517 | static void tstWindowsReferenceResults(void)
|
---|
518 | {
|
---|
519 | /*
|
---|
520 | * Feed the g_aCreateFileURIs values as input to the Windows
|
---|
521 | * PathCreateFromUrl and URlCreateFromPath APIs and print the results.
|
---|
522 | *
|
---|
523 | * We reproduce the entire source file content of g_aCreateFileURIs here.
|
---|
524 | */
|
---|
525 | for (size_t i = 0; i < RT_ELEMENTS(g_aCreateFileURIs); ++i)
|
---|
526 | {
|
---|
527 | RTPrintf(" { /* #%u: */\n", i);
|
---|
528 | RTPrintf(" /* .pszPath =*/ ");
|
---|
529 | tstPrintCString(g_aCreateFileURIs[i].pszPath);
|
---|
530 | RTPrintf(",\n");
|
---|
531 | RTPrintf(" /* .pszUri =*/ ");
|
---|
532 | tstPrintCString(g_aCreateFileURIs[i].pszUri);
|
---|
533 | RTPrintf(",\n");
|
---|
534 | RTPrintf(" /* .uFormat =*/ %s,\n",
|
---|
535 | g_aCreateFileURIs[i].uFormat == URI_FILE_FORMAT_WIN ? "URI_FILE_FORMAT_WIN"
|
---|
536 | : g_aCreateFileURIs[i].uFormat == URI_FILE_FORMAT_UNIX ? "URI_FILE_FORMAT_UNIX"
|
---|
537 | : g_aCreateFileURIs[i].uFormat == URI_FILE_FORMAT_AUTO ? "URI_FILE_FORMAT_AUTO" : "URI_FILE_FORMAT_INVALID");
|
---|
538 | RTPrintf(" /* .pszCreatedPath =*/ ");
|
---|
539 | tstPrintCString(g_aCreateFileURIs[i].pszCreatedPath);
|
---|
540 | RTPrintf(",\n");
|
---|
541 | RTPrintf(" /* .pszCreatedUri =*/ ");
|
---|
542 | tstPrintCString(g_aCreateFileURIs[i].pszCreatedUri);
|
---|
543 | RTPrintf(",\n");
|
---|
544 |
|
---|
545 | /*
|
---|
546 | * PathCreateFromUrl
|
---|
547 | */
|
---|
548 | PRTUTF16 pwszInput = NULL;
|
---|
549 | if (g_aCreateFileURIs[i].pszUri)
|
---|
550 | RTTESTI_CHECK_RC_OK_RETV(RTStrToUtf16(g_aCreateFileURIs[i].pszUri, &pwszInput));
|
---|
551 | WCHAR wszResult[_1K];
|
---|
552 | DWORD cwcResult = RT_ELEMENTS(wszResult);
|
---|
553 | RT_ZERO(wszResult);
|
---|
554 | HRESULT hrc = PathCreateFromUrlW(pwszInput, wszResult, &cwcResult, 0 /*dwFlags*/);
|
---|
555 | RTUtf16Free(pwszInput);
|
---|
556 |
|
---|
557 | if (SUCCEEDED(hrc))
|
---|
558 | {
|
---|
559 | char *pszResult;
|
---|
560 | RTTESTI_CHECK_RC_OK_RETV(RTUtf16ToUtf8(wszResult, &pszResult));
|
---|
561 | RTPrintf(" /* PathCreateFromUrl = ");
|
---|
562 | tstPrintCString(pszResult);
|
---|
563 | if ( g_aCreateFileURIs[i].pszPath
|
---|
564 | && strcmp(pszResult, g_aCreateFileURIs[i].pszCreatedPath) == 0)
|
---|
565 | RTPrintf(" - same */\n");
|
---|
566 | else
|
---|
567 | RTPrintf(" - differs */\n");
|
---|
568 | RTStrFree(pszResult);
|
---|
569 | }
|
---|
570 | else
|
---|
571 | RTPrintf(" /* PathCreateFromUrl => %#x (%Rhrc) */\n", hrc, hrc);
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * UrlCreateFromPath + UrlEscape
|
---|
575 | */
|
---|
576 | pwszInput = NULL;
|
---|
577 | if (g_aCreateFileURIs[i].pszPath)
|
---|
578 | RTTESTI_CHECK_RC_OK_RETV(RTStrToUtf16(g_aCreateFileURIs[i].pszPath, &pwszInput));
|
---|
579 | RT_ZERO(wszResult);
|
---|
580 | cwcResult = RT_ELEMENTS(wszResult);
|
---|
581 | hrc = UrlCreateFromPathW(pwszInput, wszResult, &cwcResult, 0 /*dwFlags*/);
|
---|
582 | RTUtf16Free(pwszInput);
|
---|
583 |
|
---|
584 | if (SUCCEEDED(hrc))
|
---|
585 | {
|
---|
586 | WCHAR wszResult2[_1K];
|
---|
587 | DWORD cwcResult2 = RT_ELEMENTS(wszResult2);
|
---|
588 | hrc = UrlEscapeW(wszResult, wszResult2, &cwcResult2, URL_DONT_ESCAPE_EXTRA_INFO );
|
---|
589 | if (SUCCEEDED(hrc))
|
---|
590 | {
|
---|
591 | char *pszResult;
|
---|
592 | RTTESTI_CHECK_RC_OK_RETV(RTUtf16ToUtf8(wszResult2, &pszResult));
|
---|
593 | RTPrintf(" /* UrlCreateFromPath = ");
|
---|
594 | tstPrintCString(pszResult);
|
---|
595 | if ( g_aCreateFileURIs[i].pszUri
|
---|
596 | && strcmp(pszResult, g_aCreateFileURIs[i].pszCreatedUri) == 0)
|
---|
597 | RTPrintf(" - same */\n");
|
---|
598 | else
|
---|
599 | RTPrintf(" - differs */\n");
|
---|
600 | RTStrFree(pszResult);
|
---|
601 | }
|
---|
602 | else
|
---|
603 | RTPrintf(" /* UrlEscapeW => %#x (%Rhrc) */\n", hrc, hrc);
|
---|
604 | }
|
---|
605 | else
|
---|
606 | RTPrintf(" /* UrlCreateFromPath => %#x (%Rhrc) */\n", hrc, hrc);
|
---|
607 | RTPrintf(" },\n");
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | #endif /* TSTRTURI_WITH_WINDOWS_REFERENCE_RESULTS */
|
---|
612 |
|
---|
613 |
|
---|
614 | int main()
|
---|
615 | {
|
---|
616 | RTTEST hTest;
|
---|
617 | int rc = RTTestInitAndCreate("tstRTUri", &hTest);
|
---|
618 | if (rc)
|
---|
619 | return rc;
|
---|
620 | RTTestBanner(hTest);
|
---|
621 |
|
---|
622 | #define CHECK_STR_API(a_Call, a_pszExpected) \
|
---|
623 | do { \
|
---|
624 | char *pszTmp = a_Call; \
|
---|
625 | if (a_pszExpected) \
|
---|
626 | { \
|
---|
627 | if (!pszTmp) \
|
---|
628 | RTTestIFailed("#%u: %s returns NULL, expected '%s'", i, #a_Call, a_pszExpected); \
|
---|
629 | else if (strcmp(pszTmp, a_pszExpected)) \
|
---|
630 | RTTestIFailed("#%u: %s returns '%s', expected '%s'", i, #a_Call, pszTmp, a_pszExpected); \
|
---|
631 | } \
|
---|
632 | else if (pszTmp) \
|
---|
633 | RTTestIFailed("#%u: %s returns '%s', expected NULL", i, #a_Call, pszTmp); \
|
---|
634 | RTStrFree(pszTmp); \
|
---|
635 | } while (0)
|
---|
636 |
|
---|
637 | RTTestISub("RTUriParse & RTUriParsed*");
|
---|
638 | for (uint32_t i = 0; i < RT_ELEMENTS(g_aTests); i++)
|
---|
639 | {
|
---|
640 | RTURIPARSED Parsed;
|
---|
641 | RTTESTI_CHECK_RC(rc = RTUriParse(g_aTests[i].pszUri, &Parsed), VINF_SUCCESS);
|
---|
642 | if (RT_SUCCESS(rc))
|
---|
643 | {
|
---|
644 | CHECK_STR_API(RTUriParsedScheme(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszScheme);
|
---|
645 | CHECK_STR_API(RTUriParsedAuthority(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszAuthority);
|
---|
646 | CHECK_STR_API(RTUriParsedAuthorityUsername(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszUsername);
|
---|
647 | CHECK_STR_API(RTUriParsedAuthorityPassword(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszPassword);
|
---|
648 | CHECK_STR_API(RTUriParsedAuthorityHost(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszHost);
|
---|
649 | CHECK_STR_API(RTUriParsedPath(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszPath);
|
---|
650 | CHECK_STR_API(RTUriParsedQuery(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszQuery);
|
---|
651 | CHECK_STR_API(RTUriParsedFragment(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszFragment);
|
---|
652 | uint32_t uPort = RTUriParsedAuthorityPort(g_aTests[i].pszUri, &Parsed);
|
---|
653 | if (uPort != g_aTests[i].uPort)
|
---|
654 | RTTestIFailed("#%u: RTUriParsedAuthorityPort returns %#x, expected %#x", i, uPort, g_aTests[i].uPort);
|
---|
655 | }
|
---|
656 | }
|
---|
657 |
|
---|
658 | /* Creation */
|
---|
659 | RTTestISub("RTUriCreate");
|
---|
660 | for (uint32_t i = 0; i < RT_ELEMENTS(g_aTests); i++)
|
---|
661 | CHECK_STR_API(RTUriCreate(g_aTests[i].pszScheme, g_aTests[i].pszAuthority, g_aTests[i].pszPath,
|
---|
662 | g_aTests[i].pszQuery, g_aTests[i].pszFragment),
|
---|
663 | g_aTests[i].pszCreated ? g_aTests[i].pszCreated : g_aTests[i].pszUri);
|
---|
664 |
|
---|
665 | #ifdef TSTRTURI_WITH_WINDOWS_REFERENCE_RESULTS
|
---|
666 | tstWindowsReferenceResults();
|
---|
667 | #endif
|
---|
668 |
|
---|
669 | bool fSavedMayPanic = RTAssertSetMayPanic(false);
|
---|
670 | bool fSavedQuiet = RTAssertSetQuiet(true);
|
---|
671 |
|
---|
672 | /* File Uri path */
|
---|
673 | RTTestISub("RTUriFilePath");
|
---|
674 | for (size_t i = 0; i < RT_ELEMENTS(g_aCreateFileURIs); ++i)
|
---|
675 | CHECK_STR_API(RTUriFilePath(g_aCreateFileURIs[i].pszUri, g_aCreateFileURIs[i].uFormat),
|
---|
676 | g_aCreateFileURIs[i].pszCreatedPath);
|
---|
677 |
|
---|
678 | /* File Uri creation */
|
---|
679 | RTTestISub("RTUriFileCreate");
|
---|
680 | for (size_t i = 0; i < RT_ELEMENTS(g_aCreateFileURIs); ++i)
|
---|
681 | CHECK_STR_API(RTUriFileCreate(g_aCreateFileURIs[i].pszPath), g_aCreateFileURIs[i].pszCreatedUri);
|
---|
682 |
|
---|
683 | RTAssertSetMayPanic(fSavedMayPanic);
|
---|
684 | RTAssertSetQuiet(fSavedQuiet);
|
---|
685 |
|
---|
686 | return RTTestSummaryAndDestroy(hTest);
|
---|
687 | }
|
---|
688 |
|
---|