VirtualBox

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

Last change on this file since 42699 was 42261, checked in by vboxsync, 12 years ago

enabled shared clipboard support for Linux hosts (guest=>host only)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
Line 
1/* $Id: tstRTUri.cpp 42261 2012-07-20 13:27:47Z vboxsync $ */
2/** @file
3 * IPRT Testcase - URI parsing and creation.
4 */
5
6/*
7 * Copyright (C) 2011-2012 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* Header Files *
29*******************************************************************************/
30#include <iprt/uri.h>
31
32#include <iprt/string.h>
33#include <iprt/err.h>
34#include <iprt/mem.h>
35#include <iprt/test.h>
36
37/*******************************************************************************
38* Test data *
39*******************************************************************************/
40
41static const char *gs_apcszTestURIs[] =
42{
43 "foo://tt:tt@example.com: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",
44 "foo://tt:tt@example.com: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",
45 "foo://tt:tt@example.com:8042/over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there",
46 "foo:tt@example.com",
47 "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",
48 "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",
49 "urn:example:animal:ferret:nose",
50 "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",
51 "foo:#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
52 "foo://tt:tt@example.com: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",
53 "foo://tt:tt@example.com:8042/",
54 "foo://tt:tt@example.com: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",
55 "foo://tt:tt@example.com:8042#nose%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60",
56 "foo://tt:tt@example.com:8042",
57 "foo:///",
58 "foo://"
59};
60
61static const char *gs_apcszSchemeResult[] =
62{
63 "foo",
64 "foo",
65 "foo",
66 "foo",
67 "foo",
68 "foo",
69 "urn",
70 "foo",
71 "foo",
72 "foo",
73 "foo",
74 "foo",
75 "foo",
76 "foo",
77 "foo",
78 "foo"
79};
80
81static const char *gs_apcszAuthorityResult[] =
82{
83 "tt:tt@example.com:8042",
84 "tt:tt@example.com:8042",
85 "tt:tt@example.com:8042",
86 NULL,
87 NULL,
88 NULL,
89 NULL,
90 NULL,
91 NULL,
92 "tt:tt@example.com:8042",
93 "tt:tt@example.com:8042",
94 "tt:tt@example.com:8042",
95 "tt:tt@example.com:8042",
96 "tt:tt@example.com:8042",
97 NULL,
98 NULL
99};
100
101static const char *gs_apcszPathResult[] =
102{
103 "/over/ <>#%\"{}|^[]`/there",
104 "/over/ <>#%\"{}|^[]`/there",
105 "/over/ <>#%\"{}|^[]`/there",
106 "tt@example.com",
107 "/over/ <>#%\"{}|^[]`/there",
108 "/over/ <>#%\"{}|^[]`/there",
109 "example:animal:ferret:nose",
110 NULL,
111 NULL,
112 "/",
113 "/",
114 NULL,
115 NULL,
116 NULL,
117 "/",
118 NULL
119};
120
121static const char *gs_apcszQueryResult[] =
122{
123 "name= <>#%\"{}|^[]`ferret",
124 "name= <>#%\"{}|^[]`ferret",
125 NULL,
126 NULL,
127 "name= <>#%\"{}|^[]`ferret",
128 NULL,
129 NULL,
130 "name= <>#%\"{}|^[]`ferret",
131 NULL,
132 "name= <>#%\"{}|^[]`ferret",
133 NULL,
134 "name= <>#%\"{}|^[]`ferret",
135 NULL,
136 NULL,
137 NULL,
138 NULL
139};
140
141static const char *gs_apcszFragmentResult[] =
142{
143 "nose <>#%\"{}|^[]`",
144 NULL,
145 NULL,
146 NULL,
147 "nose <>#%\"{}|^[]`",
148 "nose <>#%\"{}|^[]`",
149 NULL,
150 "nose <>#%\"{}|^[]`",
151 "nose <>#%\"{}|^[]`",
152 "nose <>#%\"{}|^[]`",
153 NULL,
154 "nose <>#%\"{}|^[]`",
155 "nose <>#%\"{}|^[]`",
156 NULL,
157 NULL,
158 NULL
159};
160
161static const char *gs_apcszCreateURIs[][5] =
162{
163 { "foo", "tt:tt@example.com:8042", "/over/ <>#%\"{}|^[]`/there", "name= <>#%\"{}|^[]`ferret", "nose <>#%\"{}|^[]`" },
164 { "foo", "tt:tt@example.com:8042", "/over/ <>#%\"{}|^[]`/there", "name= <>#%\"{}|^[]`ferret", NULL },
165 { "foo", "tt:tt@example.com:8042", "/over/ <>#%\"{}|^[]`/there", NULL, NULL },
166 { "foo", NULL, "tt@example.com", NULL, NULL },
167 { "foo", NULL, "/over/ <>#%\"{}|^[]`/there", "name= <>#%\"{}|^[]`ferret", "nose <>#%\"{}|^[]`" },
168 { "foo", NULL, "/over/ <>#%\"{}|^[]`/there", NULL, "nose <>#%\"{}|^[]`" },
169 { "urn", NULL, "example:animal:ferret:nose", NULL, NULL },
170 { "foo", NULL, NULL, "name= <>#%\"{}|^[]`ferret", "nose <>#%\"{}|^[]`" },
171 { "foo", NULL, NULL, NULL, "nose <>#%\"{}|^[]`" },
172 { "foo", "tt:tt@example.com:8042", "/", "name= <>#%\"{}|^[]`ferret", "nose <>#%\"{}|^[]`" },
173 { "foo", "tt:tt@example.com:8042", "/", NULL, NULL },
174 { "foo", "tt:tt@example.com:8042", NULL, "name= <>#%\"{}|^[]`ferret", "nose <>#%\"{}|^[]`" },
175 { "foo", "tt:tt@example.com:8042", NULL, NULL, "nose <>#%\"{}|^[]`" },
176 { "foo", "tt:tt@example.com:8042", NULL, NULL, NULL },
177 { "foo", "", "/", NULL, NULL },
178 { "foo", "", NULL, NULL, NULL }
179};
180
181struct URIFILETEST
182{
183 const char *pcszPath;
184 const char *pcszUri;
185 uint32_t uFormat;
186}
187gs_apCreateFileURIs[] =
188{
189 { "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 },
190 { "/over/ <>#%\"{}|^[]`/there", "file:///over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there", URI_FILE_FORMAT_UNIX },
191 { "/", "file:///", URI_FILE_FORMAT_UNIX },
192 { "/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 },
193 { "over\\ <>#%\"{}|^[]`\\there", "file:///over/%20%3C%3E%23%25%22%7B%7D%7C%5E%5B%5D%60/there", URI_FILE_FORMAT_WIN }
194};
195
196/**
197 * Basic API checks.
198 */
199static void tstScheme(size_t iCount, const char *pszUri, const char *pszTest)
200{
201 RTTestISubF("Uri scheme parsing %2u: '%s' -> '%s'", iCount, pszUri, pszTest);
202
203 char *pszResult = RTUriScheme(pszUri);
204 if (pszTest)
205 {
206 RTTESTI_CHECK_MSG_RETV(pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
207 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("Result '%s' != '%s'", pszResult, pszTest));
208 }
209 else
210 RTTESTI_CHECK_MSG(!pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
211
212 if (pszResult)
213 RTStrFree(pszResult);
214}
215
216static void tstAuthority(size_t iCount, const char *pszUri, const char *pszTest)
217{
218 RTTestISubF("Uri authority parsing %2u: '%s' -> '%s'", iCount, pszUri, pszTest);
219
220 char *pszResult = RTUriAuthority(pszUri);
221 if (pszTest)
222 {
223 RTTESTI_CHECK_MSG_RETV(pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
224 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("Result '%s' != '%s'", pszResult, pszTest));
225 }
226 else
227 RTTESTI_CHECK_MSG(!pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
228
229 if (pszResult)
230 RTStrFree(pszResult);
231}
232
233static void tstPath(size_t iCount, const char *pszUri, const char *pszTest)
234{
235 RTTestISubF("Uri path parsing %2u: '%s' -> '%s'", iCount, pszUri, pszTest);
236
237 char *pszResult = RTUriPath(pszUri);
238 if (pszTest)
239 {
240 RTTESTI_CHECK_MSG_RETV(pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
241 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("Result '%s' != '%s'", pszResult, pszTest));
242 }
243 else
244 RTTESTI_CHECK_MSG(!pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
245
246 if (pszResult)
247 RTStrFree(pszResult);
248}
249
250static void tstQuery(size_t iCount, const char *pszUri, const char *pszTest)
251{
252 RTTestISubF("Uri query parsing %2u: '%s' -> '%s'", iCount, pszUri, pszTest);
253
254 char *pszResult = RTUriQuery(pszUri);
255 if (pszTest)
256 {
257 RTTESTI_CHECK_MSG_RETV(pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
258 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("Result '%s' != '%s'", pszResult, pszTest));
259 }
260 else
261 RTTESTI_CHECK_MSG(!pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
262
263 if (pszResult)
264 RTStrFree(pszResult);
265}
266
267static void tstFragment(size_t iCount, const char *pszUri, const char *pszTest)
268{
269 RTTestISubF("Uri fragment parsing %2u: '%s' -> '%s'", iCount, pszUri, pszTest);
270
271 char *pszResult = RTUriFragment(pszUri);
272 if (pszTest)
273 {
274 RTTESTI_CHECK_MSG_RETV(pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
275 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("Result '%s' != '%s'", pszResult, pszTest));
276 }
277 else
278 RTTESTI_CHECK_MSG(!pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
279
280 if (pszResult)
281 RTStrFree(pszResult);
282}
283
284static void tstCreate(size_t iCount, const char *pszScheme, const char *pszAuthority, const char *pszPath, const char *pszQuery, const char *pszFragment, const char *pszTest)
285{
286 RTTestISubF("Uri creating %2u: Scheme: '%s', Authority: '%s', Path: '%s', Query: '%s', Fragment: '%s'", iCount, pszScheme, pszAuthority, pszPath, pszQuery, pszFragment);
287
288 char *pszResult = RTUriCreate(pszScheme, pszAuthority, pszPath, pszQuery, pszFragment);
289 if (pszTest)
290 {
291 RTTESTI_CHECK_MSG_RETV(pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
292 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("Result '%s' != '%s'", pszResult, pszTest));
293 }
294 else
295 RTTESTI_CHECK_MSG(!pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
296
297 if (pszResult)
298 RTStrFree(pszResult);
299 return;
300}
301
302static void tstFileCreate(size_t iCount, const char *pszPath, const char *pszTest)
303{
304 RTTestISubF("Uri file creating %2u: Path: '%s'", iCount, pszPath);
305
306 char *pszResult = RTUriFileCreate(pszPath);
307 if (pszTest)
308 {
309 RTTESTI_CHECK_MSG_RETV(pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
310 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("Result '%s' != '%s'", pszResult, pszTest));
311 }
312 else
313 RTTESTI_CHECK_MSG(!pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
314
315 if (pszResult)
316 RTStrFree(pszResult);
317 return;
318}
319
320static void tstFilePath(size_t iCount, const char *pszUri, const char *pszTest, uint32_t uFormat)
321{
322 RTTestISubF("Uri file path parsing %2u: '%s' -> '%s'", iCount, pszUri, pszTest);
323
324 char *pszResult = RTUriFilePath(pszUri, uFormat);
325 if (pszTest)
326 {
327 RTTESTI_CHECK_MSG_RETV(pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
328 RTTESTI_CHECK_MSG(RTStrCmp(pszResult, pszTest) == 0, ("Result '%s' != '%s'", pszResult, pszTest));
329 }
330 else
331 RTTESTI_CHECK_MSG(!pszResult, ("Result '%s' != '%s'", pszResult, pszTest));
332
333 if (pszResult)
334 RTStrFree(pszResult);
335 return;
336}
337
338int main()
339{
340 RTTEST hTest;
341 int rc = RTTestInitAndCreate("tstRTUri", &hTest);
342 if (rc)
343 return rc;
344 RTTestBanner(hTest);
345
346 /* Scheme */
347 Assert(RT_ELEMENTS(gs_apcszTestURIs) == RT_ELEMENTS(gs_apcszSchemeResult));
348 for (size_t i = 0; i < RT_ELEMENTS(gs_apcszTestURIs); ++i)
349 tstScheme(i+1, gs_apcszTestURIs[i], gs_apcszSchemeResult[i]);
350
351 /* Authority */
352 Assert(RT_ELEMENTS(gs_apcszTestURIs) == RT_ELEMENTS(gs_apcszAuthorityResult));
353 for (size_t i = 0; i < RT_ELEMENTS(gs_apcszTestURIs); ++i)
354 tstAuthority(i+1, gs_apcszTestURIs[i], gs_apcszAuthorityResult[i]);
355
356 /* Path */
357 Assert(RT_ELEMENTS(gs_apcszTestURIs) == RT_ELEMENTS(gs_apcszPathResult));
358 for (size_t i = 0; i < RT_ELEMENTS(gs_apcszTestURIs); ++i)
359 tstPath(i+1, gs_apcszTestURIs[i], gs_apcszPathResult[i]);
360
361 /* Query */
362 Assert(RT_ELEMENTS(gs_apcszTestURIs) == RT_ELEMENTS(gs_apcszQueryResult));
363 for (size_t i = 0; i < RT_ELEMENTS(gs_apcszTestURIs); ++i)
364 tstQuery(i+1, gs_apcszTestURIs[i], gs_apcszQueryResult[i]);
365
366 /* Fragment */
367 Assert(RT_ELEMENTS(gs_apcszTestURIs) == RT_ELEMENTS(gs_apcszFragmentResult));
368 for (size_t i = 0; i < RT_ELEMENTS(gs_apcszTestURIs); ++i)
369 tstFragment(i+1, gs_apcszTestURIs[i], gs_apcszFragmentResult[i]);
370
371 /* Creation */
372 Assert(RT_ELEMENTS(gs_apcszTestURIs) == RT_ELEMENTS(gs_apcszCreateURIs));
373 for (size_t i = 0; i < RT_ELEMENTS(gs_apcszTestURIs); ++i)
374 tstCreate(i+1, gs_apcszCreateURIs[i][0], gs_apcszCreateURIs[i][1], gs_apcszCreateURIs[i][2], gs_apcszCreateURIs[i][3], gs_apcszCreateURIs[i][4], gs_apcszTestURIs[i]);
375
376 /* File Uri path */
377 for (size_t i = 0; i < RT_ELEMENTS(gs_apCreateFileURIs); ++i)
378 tstFilePath(i+1, gs_apCreateFileURIs[i].pcszUri, gs_apCreateFileURIs[i].pcszPath, gs_apCreateFileURIs[i].uFormat);
379
380 /* File Uri creation */
381 for (size_t i = 0; i < 3; ++i)
382 tstFileCreate(i+1, gs_apCreateFileURIs[i].pcszPath, gs_apCreateFileURIs[i].pcszUri);
383
384 return RTTestSummaryAndDestroy(hTest);
385}
386
Note: See TracBrowser for help on using the repository browser.

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