VirtualBox

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

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

IPRT: Separated out RTPathAbs from the posix, fixing it up for windows + OS/2, and made it generic. Also added some NT path routines.

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