VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTPath.cpp@ 45397

Last change on this file since 45397 was 45394, checked in by vboxsync, 12 years ago

IPRT: A Study in Paths - Chapter 2: The splitting of a path into component strings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.8 KB
Line 
1/* $Id: tstRTPath.cpp 45394 2013-04-07 19:03:00Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Test various path functions.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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/path.h>
31
32#include <iprt/err.h>
33#include <iprt/initterm.h>
34#include <iprt/param.h>
35#include <iprt/process.h>
36#include <iprt/stream.h>
37#include <iprt/string.h>
38#include <iprt/test.h>
39
40
41static void testParserAndSplitter(RTTEST hTest)
42{
43 static struct
44 {
45 uint16_t cComps;
46 uint16_t cchPath;
47 uint16_t offSuffix;
48 const char *pszPath;
49 uint16_t fProps;
50 uint32_t fFlags;
51 } const s_aTests[] =
52 {
53 { 2, 5, 5, "/bin/", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_DIR_SLASH, RTPATH_STR_F_STYLE_UNIX },
54 { 2, 13, 9, "C:/Config.sys", RTPATH_PROP_VOLUME | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX, RTPATH_STR_F_STYLE_DOS },
55 { 2, 13, 10, "C://Config.sys", RTPATH_PROP_VOLUME | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX | RTPATH_PROP_EXTRA_SLASHES, RTPATH_STR_F_STYLE_DOS },
56 { 2, 12, 8, "C:Config.sys", RTPATH_PROP_VOLUME | RTPATH_PROP_RELATIVE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX, RTPATH_STR_F_STYLE_DOS },
57 { 1, 10, 6, "Config.sys", RTPATH_PROP_RELATIVE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX, RTPATH_STR_F_STYLE_DOS },
58 { 1, 4, 4, "//./", RTPATH_PROP_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE, RTPATH_STR_F_STYLE_DOS },
59 { 2, 5, 5, "//./f", RTPATH_PROP_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_DOS },
60 { 2, 5, 6, "//.//f", RTPATH_PROP_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_EXTRA_SLASHES, RTPATH_STR_F_STYLE_DOS },
61 { 3, 7, 7, "//././f", RTPATH_PROP_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_DOT_REFS, RTPATH_STR_F_STYLE_DOS },
62 { 3, 8, 8, "//.././f", RTPATH_PROP_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_DOT_REFS, RTPATH_STR_F_STYLE_DOS },
63 { 3, 9, 9, "//../../f", RTPATH_PROP_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE | RTPATH_PROP_FILENAME | RTPATH_PROP_DOTDOT_REFS, RTPATH_STR_F_STYLE_DOS },
64 { 1, 1, 1, "/", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE, RTPATH_STR_F_STYLE_UNIX },
65 { 2, 4, 4, "/bin", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
66 { 2, 5, 5, "/bin/", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_DIR_SLASH, RTPATH_STR_F_STYLE_UNIX },
67 { 3, 7, 7, "/bin/ls", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
68 { 3, 12, 7, "/etc/rc.conf", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX, RTPATH_STR_F_STYLE_UNIX },
69 { 1, 1, 2, "//", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_EXTRA_SLASHES, RTPATH_STR_F_STYLE_UNIX },
70 { 1, 1, 3, "///", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_EXTRA_SLASHES, RTPATH_STR_F_STYLE_UNIX },
71 { 3, 6, 7, "/.//bin", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_EXTRA_SLASHES | RTPATH_PROP_DOT_REFS | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
72 { 1, 3, 3, "bin", RTPATH_PROP_RELATIVE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
73 { 1, 4, 4, "bin/", RTPATH_PROP_RELATIVE | RTPATH_PROP_DIR_SLASH, RTPATH_STR_F_STYLE_UNIX },
74 { 1, 4, 7, "bin////", RTPATH_PROP_RELATIVE | RTPATH_PROP_DIR_SLASH | RTPATH_PROP_EXTRA_SLASHES, RTPATH_STR_F_STYLE_UNIX },
75 { 3, 10, 10, "bin/../usr", RTPATH_PROP_RELATIVE | RTPATH_PROP_DOTDOT_REFS | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
76 { 4, 11, 11, "/bin/../usr", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE | RTPATH_PROP_DOTDOT_REFS | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
77 { 4, 8, 8, "/a/.../u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
78 { 4, 8, 8, "/a/.b./u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
79 { 4, 8, 8, "/a/..c/u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
80 { 4, 8, 8, "/a/d../u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
81 { 4, 8, 8, "/a/.e/.u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
82 { 4, 8, 8, "/a/.f/.u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
83 { 4, 8, 8, "/a/.g/u.", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
84 { 3, 9, 10, "/a/h/u.ext", RTPATH_PROP_EXTRA_SLASHES | RTPATH_PROP_RELATIVE, RTPATH_STR_F_STYLE_UNIX | RTPATH_STR_F_MIDDLE },
85 { 3, 9, 9, "a/h/u.ext", RTPATH_PROP_RELATIVE, RTPATH_STR_F_STYLE_UNIX | RTPATH_STR_F_MIDDLE },
86 { 3, 9, 10, "a/h/u.ext/", RTPATH_PROP_EXTRA_SLASHES | RTPATH_PROP_RELATIVE, RTPATH_STR_F_STYLE_UNIX | RTPATH_STR_F_MIDDLE },
87 };
88
89 union
90 {
91 RTPATHPARSED Parsed;
92 RTPATHSPLIT Split;
93 uint8_t ab[4096];
94 } u;
95
96 RTTestSub(hTest, "RTPathParse");
97 for (uint32_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
98 {
99 memset(&u, i & 1 ? 0xff : 0, sizeof(u));
100 int rc = RTPathParse(s_aTests[i].pszPath, &u.Parsed, sizeof(u), s_aTests[i].fFlags);
101 if ( rc != VINF_SUCCESS
102 || s_aTests[i].cComps != u.Parsed.cComps
103 || s_aTests[i].fProps != u.Parsed.fProps
104 || s_aTests[i].offSuffix != u.Parsed.offSuffix
105 || s_aTests[i].cchPath != u.Parsed.cchPath)
106 {
107 RTTestFailed(hTest, "i=%d rc=%Rrc %s", i, rc, s_aTests[i].pszPath);
108 RTTestFailureDetails(hTest,
109 " cComps %u, got %u\n"
110 " fProps %#x, got %#x, xor=>%#x\n"
111 " offSuffix %u, got %u\n"
112 " cchPath %u, got %u\n"
113 ,
114 s_aTests[i].cComps, u.Parsed.cComps,
115 s_aTests[i].fProps, u.Parsed.fProps, s_aTests[i].fProps ^ u.Parsed.fProps,
116 s_aTests[i].offSuffix, u.Parsed.offSuffix,
117 s_aTests[i].cchPath, u.Parsed.cchPath);
118 }
119 }
120
121 RTTestSub(hTest, "RTPathSplit");
122 for (uint32_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
123 {
124 memset(&u, i & 1 ? 0xff : 0, sizeof(u));
125 int rc = RTPathSplit(s_aTests[i].pszPath, &u.Split, sizeof(u), s_aTests[i].fFlags);
126 if ( rc != VINF_SUCCESS
127 || s_aTests[i].cComps != u.Split.cComps
128 || s_aTests[i].fProps != u.Split.fProps
129 || s_aTests[i].cchPath != u.Split.cchPath)
130 {
131 RTTestFailed(hTest, "i=%d rc=%Rrc %s", i, rc, s_aTests[i].pszPath);
132 RTTestFailureDetails(hTest,
133 " cComps %u, got %u\n"
134 " fProps %#x, got %#x, xor=>%#x\n"
135 " cchPath %u, got %u\n"
136 ,
137 s_aTests[i].cComps, u.Split.cComps,
138 s_aTests[i].fProps, u.Split.fProps, s_aTests[i].fProps ^ u.Split.fProps,
139 s_aTests[i].cchPath, u.Split.cchPath);
140 }
141 else
142 {
143 RTTESTI_CHECK_MSG(*u.Split.pszSuffix == '\0' || *u.Split.pszSuffix == '.', ("%s", u.Split.pszSuffix));
144 for (uint32_t idxComp = RTPATH_PROP_HAS_ROOT_SPEC(u.Split.fProps); idxComp < u.Split.cComps; idxComp++)
145 if ( (s_aTests[i].fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STR_F_STYLE_DOS
146 ? strpbrk(u.Split.apszComps[idxComp], "/\\")
147 : strchr(u.Split.apszComps[idxComp], RTPATH_SLASH) )
148 RTTestFailed(hTest, "i=%d idxComp=%d '%s'", i, idxComp, u.Split.apszComps[idxComp]);
149
150 PRTPATHSPLIT pSplit = NULL;
151 RTTESTI_CHECK_RC(rc = RTPathSplitA(s_aTests[i].pszPath, &pSplit, s_aTests[i].fFlags), VINF_SUCCESS);
152 if (RT_SUCCESS(rc))
153 {
154 RTTESTI_CHECK(pSplit);
155 RTTESTI_CHECK(pSplit->cComps == u.Split.cComps);
156 RTTESTI_CHECK(pSplit->fProps == u.Split.fProps);
157 RTTESTI_CHECK(pSplit->cchPath == u.Split.cchPath);
158 RTTESTI_CHECK(pSplit->cbNeeded == u.Split.cbNeeded);
159 RTTESTI_CHECK(!strcmp(pSplit->pszSuffix, u.Split.pszSuffix));
160 for (uint32_t idxComp = 0; idxComp < u.Split.cComps; idxComp++)
161 RTTESTI_CHECK(!strcmp(pSplit->apszComps[idxComp], pSplit->apszComps[idxComp]));
162 RTPathSplitFree(pSplit);
163 }
164 }
165 }
166}
167
168
169int main()
170{
171 char szPath[RTPATH_MAX];
172
173 /*
174 * Init RT+Test.
175 */
176 RTTEST hTest;
177 int rc = RTTestInitAndCreate("tstRTPath", &hTest);
178 if (rc)
179 return rc;
180 RTTestBanner(hTest);
181
182 RTTestSub(hTest, "Environment");
183#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
184 RTTESTI_CHECK(RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS);
185# if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
186# else
187 RTTestIFailed("#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS");
188# endif
189 RTTESTI_CHECK(strcmp(RTPATH_SLASH_STR, "\\") == 0);
190 RTTESTI_CHECK(RTPATH_SLASH == '\\');
191 RTTESTI_CHECK(RTPATH_IS_SEP('/'));
192 RTTESTI_CHECK(RTPATH_IS_SEP('\\'));
193 RTTESTI_CHECK(RTPATH_IS_SEP(':'));
194
195#else
196 RTTESTI_CHECK(RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX);
197# if RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX
198# else
199 RTTestIFailed("#if RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX");
200# endif
201 RTTESTI_CHECK(strcmp(RTPATH_SLASH_STR, "/") == 0);
202 RTTESTI_CHECK(RTPATH_SLASH == '/');
203 RTTESTI_CHECK(RTPATH_IS_SEP('/'));
204 RTTESTI_CHECK(!RTPATH_IS_SEP('\\'));
205 RTTESTI_CHECK(!RTPATH_IS_SEP(':'));
206#endif
207
208 /*
209 * RTPathExecDir, RTPathUserHome and RTProcGetExecutablePath.
210 */
211 RTTestSub(hTest, "RTPathExecDir");
212 RTTESTI_CHECK_RC(rc = RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS);
213 if (RT_SUCCESS(rc))
214 RTTestIPrintf(RTTESTLVL_INFO, "ExecDir={%s}\n", szPath);
215
216 RTTestSub(hTest, "RTProcGetExecutablePath");
217 if (RTProcGetExecutablePath(szPath, sizeof(szPath)) == szPath)
218 RTTestIPrintf(RTTESTLVL_INFO, "ExecutableName={%s}\n", szPath);
219 else
220 RTTestIFailed("RTProcGetExecutablePath -> NULL");
221
222 RTTestSub(hTest, "RTPathUserHome");
223 RTTESTI_CHECK_RC(rc = RTPathUserHome(szPath, sizeof(szPath)), VINF_SUCCESS);
224 if (RT_SUCCESS(rc))
225 RTTestIPrintf(RTTESTLVL_INFO, "UserHome={%s}\n", szPath);
226
227 RTTestSub(hTest, "RTPathUserDocuments");
228 RTTESTI_CHECK_RC(rc = RTPathUserDocuments(szPath, sizeof(szPath)), VINF_SUCCESS);
229 if (RT_SUCCESS(rc))
230 RTTestIPrintf(RTTESTLVL_INFO, "UserDocuments={%s}\n", szPath);
231
232 RTTestSub(hTest, "RTPathTemp");
233 RTTESTI_CHECK_RC(rc = RTPathTemp(szPath, sizeof(szPath)), VINF_SUCCESS);
234 if (RT_SUCCESS(rc))
235 RTTestIPrintf(RTTESTLVL_INFO, "PathTemp={%s}\n", szPath);
236 size_t cch = strlen(szPath);
237 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch), VERR_BUFFER_OVERFLOW);
238 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+1), VINF_SUCCESS);
239 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+2), VINF_SUCCESS);
240
241
242 /*
243 * RTPathAbsEx
244 */
245 RTTestSub(hTest, "RTPathAbsEx");
246 static const struct
247 {
248 const char *pcszInputBase;
249 const char *pcszInputPath;
250 int rc;
251 const char *pcszOutput;
252 }
253 s_aRTPathAbsExTests[] =
254 {
255#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
256 { NULL, "", VERR_INVALID_PARAMETER, NULL },
257 { NULL, ".", VINF_SUCCESS, "%p" },
258 { NULL, "\\", VINF_SUCCESS, "%d\\" },
259 { NULL, "\\..", VINF_SUCCESS, "%d\\" },
260 { NULL, "/absolute/..", VINF_SUCCESS, "%d\\" },
261 { NULL, "/absolute\\\\../..", VINF_SUCCESS, "%d\\" },
262 { NULL, "/absolute//../path\\", VINF_SUCCESS, "%d\\path" },
263 { NULL, "/absolute/../../path", VINF_SUCCESS, "%d\\path" },
264 { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p\\dir\\file.txt" },
265 { NULL, "\\data\\", VINF_SUCCESS, "%d\\data" },
266 { "relative_base/dir\\", "\\from_root", VINF_SUCCESS, "%d\\from_root" },
267 { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p\\relative_base\\dir\\relative_also" },
268#else
269 { NULL, "", VERR_INVALID_PARAMETER, NULL },
270 { NULL, ".", VINF_SUCCESS, "%p" },
271 { NULL, "/", VINF_SUCCESS, "/" },
272 { NULL, "/..", VINF_SUCCESS, "/" },
273 { NULL, "/absolute/..", VINF_SUCCESS, "/" },
274 { NULL, "/absolute\\\\../..", VINF_SUCCESS, "/" },
275 { NULL, "/absolute//../path/", VINF_SUCCESS, "/path" },
276 { NULL, "/absolute/../../path", VINF_SUCCESS, "/path" },
277 { NULL, "relative/../dir/./././file.txt", VINF_SUCCESS, "%p/dir/file.txt" },
278 { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p/dir\\.\\.\\.\\file.txt" }, /* linux-specific */
279 { NULL, "/data/", VINF_SUCCESS, "/data" },
280 { "relative_base/dir/", "/from_root", VINF_SUCCESS, "/from_root" },
281 { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p/relative_base/dir/relative_also" },
282#endif
283#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
284 { NULL, "C:\\", VINF_SUCCESS, "C:\\" },
285 { "C:\\", "..", VINF_SUCCESS, "C:\\" },
286 { "C:\\temp", "..", VINF_SUCCESS, "C:\\" },
287 { "C:\\VirtualBox/Machines", "..\\VirtualBox.xml", VINF_SUCCESS, "C:\\VirtualBox\\VirtualBox.xml" },
288 { "C:\\MustDie", "\\from_root/dir/..", VINF_SUCCESS, "C:\\from_root" },
289 { "C:\\temp", "D:\\data", VINF_SUCCESS, "D:\\data" },
290 { NULL, "\\\\server\\..\\share", VINF_SUCCESS, "\\\\server\\..\\share" /* kind of strange */ },
291 { NULL, "\\\\server/", VINF_SUCCESS, "\\\\server" },
292 { NULL, "\\\\", VINF_SUCCESS, "\\\\" },
293 { NULL, "\\\\\\something", VINF_SUCCESS, "\\\\\\something" /* kind of strange */ },
294 { "\\\\server\\share_as_base", "/from_root", VINF_SUCCESS, "\\\\server\\from_root" },
295 { "\\\\just_server", "/from_root", VINF_SUCCESS, "\\\\just_server\\from_root" },
296 { "\\\\server\\share_as_base", "relative\\data", VINF_SUCCESS, "\\\\server\\share_as_base\\relative\\data" },
297 { "base", "\\\\?\\UNC\\relative/edwef/..", VINF_SUCCESS, "\\\\?\\UNC\\relative" },
298 { "\\\\?\\UNC\\base", "/from_root", VERR_INVALID_NAME, NULL },
299#else
300 { "/temp", "..", VINF_SUCCESS, "/" },
301 { "/VirtualBox/Machines", "../VirtualBox.xml", VINF_SUCCESS, "/VirtualBox/VirtualBox.xml" },
302 { "/MustDie", "/from_root/dir/..", VINF_SUCCESS, "/from_root" },
303 { "\\temp", "\\data", VINF_SUCCESS, "%p/\\temp/\\data" },
304#endif
305 };
306
307 for (unsigned i = 0; i < RT_ELEMENTS(s_aRTPathAbsExTests); ++ i)
308 {
309 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase,
310 s_aRTPathAbsExTests[i].pcszInputPath,
311 szPath, sizeof(szPath));
312 if (rc != s_aRTPathAbsExTests[i].rc)
313 {
314 RTTestIFailed("unexpected result code!\n"
315 " input base: '%s'\n"
316 " input path: '%s'\n"
317 " output: '%s'\n"
318 " rc: %Rrc\n"
319 " expected rc: %Rrc",
320 s_aRTPathAbsExTests[i].pcszInputBase,
321 s_aRTPathAbsExTests[i].pcszInputPath,
322 szPath, rc,
323 s_aRTPathAbsExTests[i].rc);
324 continue;
325 }
326
327 char szTmp[RTPATH_MAX];
328 char *pszExpected = NULL;
329 if (s_aRTPathAbsExTests[i].pcszOutput != NULL)
330 {
331 if (s_aRTPathAbsExTests[i].pcszOutput[0] == '%')
332 {
333 RTTESTI_CHECK_RC(rc = RTPathGetCurrent(szTmp, sizeof(szTmp)), VINF_SUCCESS);
334 if (RT_FAILURE(rc))
335 break;
336
337 pszExpected = szTmp;
338
339 if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'p')
340 {
341 cch = strlen(szTmp);
342 if (cch + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
343 strcpy(szTmp + cch, s_aRTPathAbsExTests[i].pcszOutput + 2);
344 }
345#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
346 else if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'd')
347 {
348 if (2 + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
349 strcpy(szTmp + 2, s_aRTPathAbsExTests[i].pcszOutput + 2);
350 }
351#endif
352 }
353 else
354 {
355 strcpy(szTmp, s_aRTPathAbsExTests[i].pcszOutput);
356 pszExpected = szTmp;
357 }
358
359 if (strcmp(szPath, pszExpected))
360 {
361 RTTestIFailed("Unexpected result\n"
362 " input base: '%s'\n"
363 " input path: '%s'\n"
364 " output: '%s'\n"
365 " expected: '%s'",
366 s_aRTPathAbsExTests[i].pcszInputBase,
367 s_aRTPathAbsExTests[i].pcszInputPath,
368 szPath,
369 s_aRTPathAbsExTests[i].pcszOutput);
370 }
371 }
372 }
373
374 /*
375 * RTPathStripFilename
376 */
377 RTTestSub(hTest, "RTPathStripFilename");
378 static const char *s_apszStripFilenameTests[] =
379 {
380 "/usr/include///", "/usr/include//",
381 "/usr/include/", "/usr/include",
382 "/usr/include", "/usr",
383 "/usr", "/",
384 "usr", ".",
385#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
386 "c:/windows", "c:/",
387 "c:/", "c:/",
388 "D:", "D:",
389 "C:\\OS2\\DLLS", "C:\\OS2",
390#endif
391 };
392 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripFilenameTests); i += 2)
393 {
394 const char *pszInput = s_apszStripFilenameTests[i];
395 const char *pszExpect = s_apszStripFilenameTests[i + 1];
396 strcpy(szPath, pszInput);
397 RTPathStripFilename(szPath);
398 if (strcmp(szPath, pszExpect))
399 {
400 RTTestIFailed("Unexpected result\n"
401 " input: '%s'\n"
402 " output: '%s'\n"
403 "expected: '%s'",
404 pszInput, szPath, pszExpect);
405 }
406 }
407
408 /*
409 * RTPathAppend.
410 */
411 RTTestSub(hTest, "RTPathAppend");
412 static const char *s_apszAppendTests[] =
413 {
414 /* base append result */
415 "/", "", "/",
416 "", "/", "/",
417 "/", "/", "/",
418 "/x", "", "/x",
419 "/x", "/", "/x/",
420 "/", "x", "/x",
421 "dir", "file", "dir/file",
422 "dir", "/file", "dir/file",
423 "dir", "//file", "dir/file",
424 "dir", "///file", "dir/file",
425 "dir/", "/file", "dir/file",
426 "dir/", "//file", "dir/file",
427 "dir/", "///file", "dir/file",
428 "dir//", "file", "dir/file",
429 "dir//", "/file", "dir/file",
430 "dir//", "//file", "dir/file",
431 "dir///", "///file", "dir/file",
432 "/bin/testcase", "foo.r0", "/bin/testcase/foo.r0",
433#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
434 "/", "\\", "/",
435 "\\", "/", "\\",
436 "\\\\srv\\shr", "dir//", "\\\\srv\\shr/dir//",
437 "\\\\srv\\shr", "dir//file", "\\\\srv\\shr/dir//file",
438 "\\\\srv\\shr", "//dir//", "\\\\srv\\shr/dir//",
439 "\\\\srv\\shr", "/\\dir//", "\\\\srv\\shr\\dir//",
440 "\\\\", "not-srv/not-shr/file", "\\not-srv/not-shr/file",
441 "C:", "autoexec.bat", "C:autoexec.bat",
442 "C:", "/autoexec.bat", "C:/autoexec.bat",
443 "C:", "\\autoexec.bat", "C:\\autoexec.bat",
444 "C:\\", "/autoexec.bat", "C:\\autoexec.bat",
445 "C:\\\\", "autoexec.bat", "C:\\autoexec.bat",
446 "E:\\bin\\testcase", "foo.r0", "E:\\bin\\testcase/foo.r0",
447#endif
448 };
449 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
450 {
451 const char *pszInput = s_apszAppendTests[i];
452 const char *pszAppend = s_apszAppendTests[i + 1];
453 const char *pszExpect = s_apszAppendTests[i + 2];
454 strcpy(szPath, pszInput);
455 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, sizeof(szPath), pszAppend), VINF_SUCCESS);
456 if (RT_FAILURE(rc))
457 continue;
458 if (strcmp(szPath, pszExpect))
459 {
460 RTTestIFailed("Unexpected result\n"
461 " input: '%s'\n"
462 " append: '%s'\n"
463 " output: '%s'\n"
464 "expected: '%s'",
465 pszInput, pszAppend, szPath, pszExpect);
466 }
467 else
468 {
469 size_t const cchResult = strlen(szPath);
470
471 strcpy(szPath, pszInput);
472 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 2, pszAppend), VINF_SUCCESS);
473 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
474
475 strcpy(szPath, pszInput);
476 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 1, pszAppend), VINF_SUCCESS);
477 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
478
479 if (strlen(pszInput) < cchResult)
480 {
481 strcpy(szPath, pszInput);
482 RTTESTI_CHECK_RC(RTPathAppend(szPath, cchResult, pszAppend), VERR_BUFFER_OVERFLOW);
483 }
484 }
485 }
486
487 /*
488 * RTPathJoin - reuse the append tests.
489 */
490 RTTestSub(hTest, "RTPathJoin");
491 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
492 {
493 const char *pszInput = s_apszAppendTests[i];
494 const char *pszAppend = s_apszAppendTests[i + 1];
495 const char *pszExpect = s_apszAppendTests[i + 2];
496
497 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
498
499 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, sizeof(szPath), pszInput, pszAppend), VINF_SUCCESS);
500 if (RT_FAILURE(rc))
501 continue;
502 if (strcmp(szPath, pszExpect))
503 {
504 RTTestIFailed("Unexpected result\n"
505 " input: '%s'\n"
506 " append: '%s'\n"
507 " output: '%s'\n"
508 "expected: '%s'",
509 pszInput, pszAppend, szPath, pszExpect);
510 }
511 else
512 {
513 size_t const cchResult = strlen(szPath);
514
515 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
516 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult + 2, pszInput, pszAppend), VINF_SUCCESS);
517 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
518
519 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
520 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult + 1, pszInput, pszAppend), VINF_SUCCESS);
521 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
522
523 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult, pszInput, pszAppend), VERR_BUFFER_OVERFLOW);
524 }
525 }
526
527 /*
528 * RTPathJoinA - reuse the append tests.
529 */
530 RTTestSub(hTest, "RTPathJoinA");
531 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
532 {
533 const char *pszInput = s_apszAppendTests[i];
534 const char *pszAppend = s_apszAppendTests[i + 1];
535 const char *pszExpect = s_apszAppendTests[i + 2];
536
537 char *pszPathDst;
538 RTTESTI_CHECK(pszPathDst = RTPathJoinA(pszInput, pszAppend));
539 if (!pszPathDst)
540 continue;
541 if (strcmp(pszPathDst, pszExpect))
542 {
543 RTTestIFailed("Unexpected result\n"
544 " input: '%s'\n"
545 " append: '%s'\n"
546 " output: '%s'\n"
547 "expected: '%s'",
548 pszInput, pszAppend, pszPathDst, pszExpect);
549 }
550 RTStrFree(pszPathDst);
551 }
552
553 /*
554 * RTPathStripTrailingSlash
555 */
556 static const char *s_apszStripTrailingSlash[] =
557 {
558 /* input result */
559 "/", "/",
560 "//", "/",
561 "////////////////////", "/",
562 "/tmp", "/tmp",
563 "/tmp////////////////", "/tmp",
564 "tmp", "tmp",
565 "tmp////////////////", "tmp",
566 "./", ".",
567#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
568 "////////////////////", "/",
569 "D:", "D:",
570 "D:/", "D:/",
571 "D:\\", "D:\\",
572 "D:\\/\\", "D:\\",
573 "D:/\\/\\", "D:/",
574 "C:/Temp", "D:/Temp",
575 "C:/Temp/", "D:/Temp/",
576 "C:/Temp\\/", "D:/Temp",
577#endif
578 };
579 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripTrailingSlash); i += 2)
580 {
581 const char *pszInput = s_apszStripTrailingSlash[i];
582 const char *pszExpect = s_apszStripTrailingSlash[i + 1];
583
584 strcpy(szPath, pszInput);
585 cch = RTPathStripTrailingSlash(szPath);
586 if (strcmp(szPath, pszExpect))
587 RTTestIFailed("Unexpected result\n"
588 " input: '%s'\n"
589 " output: '%s'\n"
590 "expected: '%s'",
591 pszInput, szPath, pszExpect);
592 else
593 RTTESTI_CHECK(cch == strlen(szPath));
594 }
595
596 /*
597 * RTPathCountComponents
598 */
599 RTTestSub(hTest, "RTPathCountComponents");
600 RTTESTI_CHECK(RTPathCountComponents("") == 0);
601 RTTESTI_CHECK(RTPathCountComponents("/") == 1);
602 RTTESTI_CHECK(RTPathCountComponents("//") == 1);
603 RTTESTI_CHECK(RTPathCountComponents("//////////////") == 1);
604 RTTESTI_CHECK(RTPathCountComponents("//////////////bin") == 2);
605 RTTESTI_CHECK(RTPathCountComponents("//////////////bin/") == 2);
606 RTTESTI_CHECK(RTPathCountComponents("//////////////bin/////") == 2);
607 RTTESTI_CHECK(RTPathCountComponents("..") == 1);
608 RTTESTI_CHECK(RTPathCountComponents("../") == 1);
609 RTTESTI_CHECK(RTPathCountComponents("../..") == 2);
610 RTTESTI_CHECK(RTPathCountComponents("../../") == 2);
611#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
612 RTTESTI_CHECK(RTPathCountComponents("d:") == 1);
613 RTTESTI_CHECK(RTPathCountComponents("d:/") == 1);
614 RTTESTI_CHECK(RTPathCountComponents("d:/\\") == 1);
615 RTTESTI_CHECK(RTPathCountComponents("d:\\") == 1);
616 RTTESTI_CHECK(RTPathCountComponents("c:\\config.sys") == 2);
617 RTTESTI_CHECK(RTPathCountComponents("c:\\windows") == 2);
618 RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\") == 2);
619 RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\system32") == 3);
620 RTTESTI_CHECK(RTPathCountComponents("//./C$") == 1);
621 RTTESTI_CHECK(RTPathCountComponents("\\\\.\\C$") == 1);
622 RTTESTI_CHECK(RTPathCountComponents("/\\.\\C$") == 1);
623 RTTESTI_CHECK(RTPathCountComponents("//myserver") == 1);
624 RTTESTI_CHECK(RTPathCountComponents("//myserver/") == 1);
625 RTTESTI_CHECK(RTPathCountComponents("//myserver/share") == 1);
626 RTTESTI_CHECK(RTPathCountComponents("//myserver/share/") == 1);
627 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\") == 1);
628 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x") == 2);
629 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y") == 3);
630 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y\\") == 3);
631#endif
632
633 /*
634 * RTPathCopyComponents
635 */
636 struct
637 {
638 const char *pszSrc;
639 size_t cComponents;
640 const char *pszResult;
641 } s_aCopyComponents[] =
642 {
643 { "", 0, "" },
644 { "", 5, "" },
645 { "/", 0, "" },
646 { "/", 1, "/" },
647 { "/", 2, "/" },
648 { "/usr/bin/sed", 0, "" },
649 { "/usr/bin/sed", 1, "/" },
650 { "/usr/bin/sed", 2, "/usr/" },
651 { "/usr/bin/sed", 3, "/usr/bin/" },
652 { "/usr/bin/sed", 4, "/usr/bin/sed" },
653 { "/usr/bin/sed", 5, "/usr/bin/sed" },
654 { "/usr/bin/sed", 6, "/usr/bin/sed" },
655 { "/usr///bin/sed", 2, "/usr///" },
656 };
657 for (unsigned i = 0; i < RT_ELEMENTS(s_aCopyComponents); i++)
658 {
659 const char *pszInput = s_aCopyComponents[i].pszSrc;
660 size_t cComponents = s_aCopyComponents[i].cComponents;
661 const char *pszResult = s_aCopyComponents[i].pszResult;
662
663 memset(szPath, 'a', sizeof(szPath));
664 rc = RTPathCopyComponents(szPath, sizeof(szPath), pszInput, cComponents);
665 RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
666 if (RT_SUCCESS(rc) && strcmp(szPath, pszResult))
667 RTTestIFailed("Unexpected result\n"
668 " input: '%s' cComponents=%u\n"
669 " output: '%s'\n"
670 "expected: '%s'",
671 pszInput, cComponents, szPath, pszResult);
672 else if (RT_SUCCESS(rc))
673 {
674 RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult) + 1, pszInput, cComponents), VINF_SUCCESS);
675 RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult), pszInput, cComponents), VERR_BUFFER_OVERFLOW);
676 }
677 }
678
679
680 /*
681 * RTPathStripExt
682 */
683 RTTestSub(hTest, "RTPathStripExt");
684 struct
685 {
686 const char *pszSrc;
687 const char *pszResult;
688 } s_aStripExt[] =
689 {
690 { "filename.ext", "filename" },
691 { "filename.ext1.ext2.ext3", "filename.ext1.ext2" },
692 { "filename..ext", "filename." },
693 { "filename.ext.", "filename.ext" }, /** @todo This is a bit weird/wrong, but not half as weird as the way Windows+OS/2 deals with a trailing dots. */
694 };
695 for (unsigned i = 0; i < RT_ELEMENTS(s_aStripExt); i++)
696 {
697 const char *pszInput = s_aStripExt[i].pszSrc;
698 const char *pszResult = s_aStripExt[i].pszResult;
699
700 strcpy(szPath, pszInput);
701 RTPathStripExt(szPath);
702 if (strcmp(szPath, pszResult))
703 RTTestIFailed("Unexpected result\n"
704 " input: '%s'\n"
705 " output: '%s'\n"
706 "expected: '%s'",
707 pszInput, szPath, pszResult);
708 }
709
710 /*
711 * RTPathCalcRelative
712 */
713 RTTestSub(hTest, "RTPathCalcRelative");
714 struct
715 {
716 const char *pszFrom;
717 const char *pszTo;
718 int rc;
719 const char *pszExpected;
720 } s_aRelPath[] =
721 {
722 { "/home/test.ext", "/home/test2.ext", VINF_SUCCESS, "test2.ext"},
723 { "/dir/test.ext", "/dir/dir2/test2.ext", VINF_SUCCESS, "dir2/test2.ext"},
724 { "/dir/dir2/test.ext", "/dir/test2.ext", VINF_SUCCESS, "../test2.ext"},
725 { "/dir/dir2/test.ext", "/dir/dir3/test2.ext", VINF_SUCCESS, "../dir3/test2.ext"},
726#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
727 { "\\\\server\\share\\test.ext", "\\\\server\\share2\\test2.ext", VERR_NOT_SUPPORTED, ""},
728 { "c:\\dir\\test.ext", "f:\\dir\\test.ext", VERR_NOT_SUPPORTED, ""}
729#endif
730 };
731 for (unsigned i = 0; i < RT_ELEMENTS(s_aRelPath); i++)
732 {
733 const char *pszFrom = s_aRelPath[i].pszFrom;
734 const char *pszTo = s_aRelPath[i].pszTo;
735
736 rc = RTPathCalcRelative(szPath, sizeof(szPath), pszFrom, pszTo);
737 if (rc != s_aRelPath[i].rc)
738 RTTestIFailed("Unexpected return code\n"
739 " got: %Rrc\n"
740 "expected: %Rrc",
741 rc, s_aRelPath[i].rc);
742 else if ( RT_SUCCESS(rc)
743 && strcmp(szPath, s_aRelPath[i].pszExpected))
744 RTTestIFailed("Unexpected result\n"
745 " from: '%s'\n"
746 " to: '%s'\n"
747 " output: '%s'\n"
748 "expected: '%s'",
749 pszFrom, pszTo, szPath, s_aRelPath[i].pszExpected);
750 }
751
752 testParserAndSplitter(hTest);
753
754 /*
755 * Summary.
756 */
757 return RTTestSummaryAndDestroy(hTest);
758}
759
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