VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTUri.cpp@ 57724

Last change on this file since 57724 was 57724, checked in by vboxsync, 10 years ago

tstRTUri: Added test with UTF-8 chars both in escaped and plain forms.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/* $Id: tstRTUri.cpp 57724 2015-09-11 20:16:53Z 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
39/*********************************************************************************************************************************
40* Test data *
41*********************************************************************************************************************************/
42
43static struct
44{
45 const char *pszUri;
46 const char *pszScheme;
47 const char *pszAuthority;
48 const char *pszPath;
49 const char *pszQuery;
50 const char *pszFragment;
51
52 const char *pszUsername;
53 const char *pszPassword;
54 const char *pszHost;
55 uint32_t uPort;
56
57 const char *pszCreated;
58} g_aTests[] =
59{
60 { /* #0 */
61 "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",
62 /*.pszScheme =*/ "foo",
63 /*.pszAuthority =*/ "tt:[email protected]:8042",
64 /*.pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
65 /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
66 /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
67 /*.pszUsername =*/ "tt",
68 /*.pszPassword =*/ "yt",
69 /*.pszHost =*/ "example.com",
70 /*.uPort =*/ 8042,
71 /*.pszCreated =*/ NULL /* same as pszUri*/,
72 },
73 { /* #1 */
74 "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",
75 /*.pszScheme =*/ "foo",
76 /*.pszAuthority =*/ "tt:[email protected]:8042",
77 /*.pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
78 /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
79 /*.pszFragment =*/ NULL,
80 /*.pszUsername =*/ "tt",
81 /*.pszPassword =*/ "yt",
82 /*.pszHost =*/ "example.com",
83 /*.uPort =*/ 8042,
84 /*.pszCreated =*/ NULL /* same as pszUri*/,
85 },
86 { /* #2 */
87 "foo://tt:[email protected]:8042/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there",
88 /*.pszScheme =*/ "foo",
89 /*.pszAuthority =*/ "tt:[email protected]:8042",
90 /*.pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
91 /*.pszQuery =*/ NULL,
92 /*.pszFragment =*/ NULL,
93 /*.pszUsername =*/ "tt",
94 /*.pszPassword =*/ "yt",
95 /*.pszHost =*/ "example.com",
96 /*.uPort =*/ 8042,
97 /*.pszCreated =*/ NULL /* same as pszUri*/,
98 },
99 { /* #3 */
100 "foo:[email protected]",
101 /*.pszScheme =*/ "foo",
102 /*.pszAuthority =*/ NULL,
103 /*.pszPath =*/ "[email protected]",
104 /*.pszQuery =*/ NULL,
105 /*.pszFragment =*/ NULL,
106 /*.pszUsername =*/ NULL,
107 /*.pszPassword =*/ NULL,
108 /*.pszHost =*/ NULL,
109 /*.uPort =*/ UINT32_MAX,
110 /*.pszCreated =*/ NULL /* same as pszUri*/,
111 },
112 { /* #4 */
113 "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",
114 /*.pszScheme =*/ "foo",
115 /*.pszAuthority =*/ NULL,
116 /*.pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
117 /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
118 /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
119 /*.pszUsername =*/ NULL,
120 /*.pszPassword =*/ NULL,
121 /*.pszHost =*/ NULL,
122 /*.uPort =*/ UINT32_MAX,
123 /*.pszCreated =*/ NULL /* same as pszUri*/,
124 },
125 { /* #5 */
126 "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",
127 /*.pszScheme =*/ "foo",
128 /*.pszAuthority =*/ NULL,
129 /*.pszPath =*/ "/over/ <>#%\"{}|^[]`/there",
130 /*.pszQuery =*/ NULL,
131 /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
132 /*.pszUsername =*/ NULL,
133 /*.pszPassword =*/ NULL,
134 /*.pszHost =*/ NULL,
135 /*.uPort =*/ UINT32_MAX,
136 /*.pszCreated =*/ NULL /* same as pszUri*/,
137 },
138 { /* #6 */
139 "urn:example:animal:ferret:nose",
140 /*.pszScheme =*/ "urn",
141 /*.pszAuthority =*/ NULL,
142 /*.pszPath =*/ "example:animal:ferret:nose",
143 /*.pszQuery =*/ NULL,
144 /*.pszFragment =*/ NULL,
145 /*.pszUsername =*/ NULL,
146 /*.pszPassword =*/ NULL,
147 /*.pszHost =*/ NULL,
148 /*.uPort =*/ UINT32_MAX,
149 /*.pszCreated =*/ NULL /* same as pszUri*/,
150 },
151 { /* #7 */
152 "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",
153 /*.pszScheme =*/ "foo",
154 /*.pszAuthority =*/ NULL,
155 /*.pszPath =*/ NULL,
156 /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
157 /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
158 /*.pszUsername =*/ NULL,
159 /*.pszPassword =*/ NULL,
160 /*.pszHost =*/ NULL,
161 /*.uPort =*/ UINT32_MAX,
162 /*.pszCreated =*/ NULL /* same as pszUri*/,
163 },
164 { /* #8 */
165 "foo:#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
166 /*.pszScheme =*/ "foo",
167 /*.pszAuthority =*/ NULL,
168 /*.pszPath =*/ NULL,
169 /*.pszQuery =*/ NULL,
170 /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
171 /*.pszUsername =*/ NULL,
172 /*.pszPassword =*/ NULL,
173 /*.pszHost =*/ NULL,
174 /*.uPort =*/ UINT32_MAX,
175 /*.pszCreated =*/ NULL /* same as pszUri*/,
176 },
177 { /* #9 */
178 "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",
179 /*.pszScheme =*/ "foo",
180 /*.pszAuthority =*/ "tt:[email protected]:8042",
181 /*.pszPath =*/ "/",
182 /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
183 /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
184 /*.pszUsername =*/ "tt",
185 /*.pszPassword =*/ "yt",
186 /*.pszHost =*/ "example.com",
187 /*.uPort =*/ 8042,
188 /*.pszCreated =*/ NULL /* same as pszUri*/,
189 },
190 { /* #10 */
191 "foo://tt:[email protected]:8042/",
192 /*.pszScheme =*/ "foo",
193 /*.pszAuthority =*/ "tt:[email protected]:8042",
194 /*.pszPath =*/ "/",
195 /*.pszQuery =*/ NULL,
196 /*.pszFragment =*/ NULL,
197 /*.pszUsername =*/ "tt",
198 /*.pszPassword =*/ "yt",
199 /*.pszHost =*/ "example.com",
200 /*.uPort =*/ 8042,
201 /*.pszCreated =*/ NULL /* same as pszUri*/,
202 },
203 { /* #11 */
204 "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",
205 /*.pszScheme =*/ "foo",
206 /*.pszAuthority =*/ "tt:[email protected]:8042",
207 /*.pszPath =*/ NULL,
208 /*.pszQuery =*/ "name= <>#%\"{}|^[]`ferret",
209 /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
210 /*.pszUsername =*/ "tt",
211 /*.pszPassword =*/ "yt",
212 /*.pszHost =*/ "example.com",
213 /*.uPort =*/ 8042,
214 /*.pszCreated =*/ NULL /* same as pszUri*/,
215 },
216 { /* #12 */
217 "foo://tt:[email protected]:8042#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
218 /*.pszScheme =*/ "foo",
219 /*.pszAuthority =*/ "tt:[email protected]:8042",
220 /*.pszPath =*/ NULL,
221 /*.pszQuery =*/ NULL,
222 /*.pszFragment =*/ "nose <>#%\"{}|^[]`",
223 /*.pszUsername =*/ "tt",
224 /*.pszPassword =*/ "yt",
225 /*.pszHost =*/ "example.com",
226 /*.uPort =*/ 8042,
227 /*.pszCreated =*/ NULL /* same as pszUri*/,
228 },
229 { /* #13 */
230 "foo://tt:[email protected]:8042",
231 /*.pszScheme =*/ "foo",
232 /*.pszAuthority =*/ "tt:[email protected]:8042",
233 /*.pszPath =*/ NULL,
234 /*.pszQuery =*/ NULL,
235 /*.pszFragment =*/ NULL,
236 /*.pszUsername =*/ "tt",
237 /*.pszPassword =*/ "yt",
238 /*.pszHost =*/ "example.com",
239 /*.uPort =*/ 8042,
240 /*.pszCreated =*/ NULL /* same as pszUri*/,
241 },
242 { /* #14 */
243 "foo:///",
244 /*.pszScheme =*/ "foo",
245 /*.pszAuthority =*/ "",
246 /*.pszPath =*/ "/",
247 /*.pszQuery =*/ NULL,
248 /*.pszFragment =*/ NULL,
249 /*.pszUsername =*/ NULL,
250 /*.pszPassword =*/ NULL,
251 /*.pszHost =*/ NULL,
252 /*.uPort =*/ UINT32_MAX,
253 /*.pszCreated =*/ NULL /* same as pszUri*/,
254 },
255 { /* #15 */
256 "foo://",
257 /*.pszScheme =*/ "foo",
258 /*.pszAuthority =*/ "",
259 /*.pszPath =*/ NULL,
260 /*.pszQuery =*/ NULL,
261 /*.pszFragment =*/ NULL,
262 /*.pszUsername =*/ NULL,
263 /*.pszPassword =*/ NULL,
264 /*.pszHost =*/ NULL,
265 /*.uPort =*/ UINT32_MAX,
266 /*.pszCreated =*/ NULL /* same as pszUri*/,
267 },
268 { /* #16 - UTF-8 escape sequences. */
269 "http://example.com/%ce%b3%ce%bb%cf%83%ce%b1%20%e0%a4%95\xe0\xa4\x95",
270 /*.pszScheme =*/ "http",
271 /*.pszAuthority =*/ "example.com",
272 /*.pszPath =*/ "/\xce\xb3\xce\xbb\xcf\x83\xce\xb1 \xe0\xa4\x95\xe0\xa4\x95",
273 /*.pszQuery =*/ NULL,
274 /*.pszFragment =*/ NULL,
275 /*.pszUsername =*/ NULL,
276 /*.pszPassword =*/ NULL,
277 /*.pszHost =*/ "example.com",
278 /*.uPort =*/ UINT32_MAX,
279 /*.pszCreated =*/ "http://example.com/\xce\xb3\xce\xbb\xcf\x83\xce\xb1%20\xe0\xa4\x95\xe0\xa4\x95",
280 },
281};
282
283
284struct URIFILETEST
285{
286 const char *pcszPath;
287 const char *pcszUri;
288 uint32_t uFormat;
289}
290g_apCreateFileURIs[] =
291{
292 { "C:\\over\\ <>#%\"{}|^[]`\\there", "file:///C:%5Cover%5C%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60%5Cthere", URI_FILE_FORMAT_WIN },
293 { "/over/ <>#%\"{}|^[]`/there", "file:///over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there", URI_FILE_FORMAT_UNIX },
294 { "/", "file:///", URI_FILE_FORMAT_UNIX },
295 { "/C:/over/ <>#%\"{}|^[]`/there", "file:///C:%5Cover%5C%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60%5Cthere", URI_FILE_FORMAT_UNIX },
296 { "\\over\\ <>#%\"{}|^[]`\\there", "file:///over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there", URI_FILE_FORMAT_WIN }
297};
298
299
300
301static void tstCreate(size_t idxTest, const char *pszScheme, const char *pszAuthority, const char *pszPath, const char *pszQuery, const char *pszFragment, const char *pszTest)
302{
303 char *pszResult = RTUriCreate(pszScheme, pszAuthority, pszPath, pszQuery, pszFragment);
304 if (pszTest)
305 {
306 RTTESTI_CHECK_MSG_RETV(pszResult, ("#%u: Result '%s' != '%s'", idxTest, pszResult, pszTest));
307 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("#%u: Result '%s' != '%s'", idxTest, pszResult, pszTest));
308 }
309 else
310 RTTESTI_CHECK_MSG(!pszResult, ("#%u: Result '%s' != '%s'", idxTest, pszResult, pszTest));
311
312 if (pszResult)
313 RTStrFree(pszResult);
314 return;
315}
316
317static void tstFileCreate(size_t idxTest, const char *pszPath, const char *pszTest)
318{
319 char *pszResult = RTUriFileCreate(pszPath);
320 if (pszTest)
321 {
322 RTTESTI_CHECK_MSG_RETV(pszResult, ("#%u: Result '%s' != '%s'", idxTest, pszResult, pszTest));
323 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("#%u: Result '%s' != '%s'", idxTest, pszResult, pszTest));
324 }
325 else
326 RTTESTI_CHECK_MSG(!pszResult, ("#%u: Result '%s' != '%s'", idxTest, pszResult, pszTest));
327
328 if (pszResult)
329 RTStrFree(pszResult);
330 return;
331}
332
333static void tstFilePath(size_t idxTest, const char *pszUri, const char *pszTest, uint32_t uFormat)
334{
335 char *pszResult = RTUriFilePath(pszUri, uFormat);
336 if (pszTest)
337 {
338 RTTESTI_CHECK_MSG_RETV(pszResult, ("#%u: Result '%s' != '%s'", idxTest, pszResult, pszTest));
339 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("#%u: Result '%s' != '%s'", idxTest, pszResult, pszTest));
340 }
341 else
342 RTTESTI_CHECK_MSG(!pszResult, ("#%u: Result '%s' != '%s'", idxTest, pszResult, pszTest));
343
344 if (pszResult)
345 RTStrFree(pszResult);
346 return;
347}
348
349int main()
350{
351 RTTEST hTest;
352 int rc = RTTestInitAndCreate("tstRTUri", &hTest);
353 if (rc)
354 return rc;
355 RTTestBanner(hTest);
356
357#define CHECK_STR_API(a_Call, a_pszExpected) \
358 do { \
359 char *pszTmp = a_Call; \
360 if (a_pszExpected) \
361 { \
362 if (!pszTmp) \
363 RTTestIFailed("#%u: %s returns NULL, expected '%s'", i, #a_Call, a_pszExpected); \
364 else if (strcmp(pszTmp, a_pszExpected)) \
365 RTTestIFailed("#%u: %s returns '%s', expected '%s'", i, #a_Call, pszTmp, a_pszExpected); \
366 } \
367 else if (pszTmp) \
368 RTTestIFailed("#%u: %s returns '%s', expected NULL", i, #a_Call, pszTmp); \
369 RTStrFree(pszTmp); \
370 } while (0)
371
372 RTTestISub("RTUriParse & RTUriParsed*");
373 for (uint32_t i = 0; i < RT_ELEMENTS(g_aTests); i++)
374 {
375 RTURIPARSED Parsed;
376 RTTESTI_CHECK_RC(rc = RTUriParse(g_aTests[i].pszUri, &Parsed), VINF_SUCCESS);
377 if (RT_SUCCESS(rc))
378 {
379 CHECK_STR_API(RTUriParsedScheme(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszScheme);
380 CHECK_STR_API(RTUriParsedAuthority(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszAuthority);
381 CHECK_STR_API(RTUriParsedAuthorityUsername(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszUsername);
382 CHECK_STR_API(RTUriParsedAuthorityPassword(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszPassword);
383 CHECK_STR_API(RTUriParsedAuthorityHost(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszHost);
384 CHECK_STR_API(RTUriParsedPath(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszPath);
385 CHECK_STR_API(RTUriParsedQuery(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszQuery);
386 CHECK_STR_API(RTUriParsedFragment(g_aTests[i].pszUri, &Parsed), g_aTests[i].pszFragment);
387 uint32_t uPort = RTUriParsedAuthorityPort(g_aTests[i].pszUri, &Parsed);
388 if (uPort != g_aTests[i].uPort)
389 RTTestIFailed("#%u: RTUriParsedAuthorityPort returns %#x, expected %#x", i, uPort, g_aTests[i].uPort);
390 }
391 }
392
393 /* Creation */
394 RTTestISub("RTUriCreate");
395 for (uint32_t i = 0; i < RT_ELEMENTS(g_aTests); i++)
396 CHECK_STR_API(RTUriCreate(g_aTests[i].pszScheme, g_aTests[i].pszAuthority, g_aTests[i].pszPath,
397 g_aTests[i].pszQuery, g_aTests[i].pszFragment),
398 g_aTests[i].pszCreated ? g_aTests[i].pszCreated : g_aTests[i].pszUri);
399
400 /* File Uri path */
401 RTTestISub("RTUriFilePath");
402 for (size_t i = 0; i < RT_ELEMENTS(g_apCreateFileURIs); ++i)
403 tstFilePath(i, g_apCreateFileURIs[i].pcszUri, g_apCreateFileURIs[i].pcszPath, g_apCreateFileURIs[i].uFormat);
404
405 /* File Uri creation */
406 RTTestISub("RTUriFileCreate");
407 for (size_t i = 0; i < 3; ++i)
408 tstFileCreate(i, g_apCreateFileURIs[i].pcszPath, g_apCreateFileURIs[i].pcszUri);
409
410 return RTTestSummaryAndDestroy(hTest);
411}
412
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette