VirtualBox

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

Last change on this file since 96781 was 96609, checked in by vboxsync, 2 years ago

IPRT/path: Added fFlags parameter to RTPathAppendEx and RTPathJoinEx to make it possible to select the path style. bugref:10286

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 63.6 KB
Line 
1/* $Id: tstRTPath.cpp 96609 2022-09-06 14:13:23Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Test various path functions.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/path.h>
42
43#include <iprt/err.h>
44#include <iprt/initterm.h>
45#include <iprt/param.h>
46#include <iprt/process.h>
47#include <iprt/stream.h>
48#include <iprt/string.h>
49#include <iprt/test.h>
50
51
52static void testParserAndSplitter(RTTEST hTest)
53{
54 static struct
55 {
56 uint16_t cComps;
57 uint16_t cchPath;
58 uint16_t offSuffix;
59 int16_t offName; /**< RTPathParseSimple */
60 uint16_t cchDir; /**< RTPathParseSimple */
61 const char *pszPath;
62 uint16_t fProps;
63 uint32_t fFlags;
64 } const s_aTests[] =
65 {
66 { 2, 5, 5, -1, 4, "/bin/", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_DIR_SLASH, RTPATH_STR_F_STYLE_UNIX },
67 { 2, 13, 9, 3, 3, "C:/Config.sys", RTPATH_PROP_VOLUME | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX, RTPATH_STR_F_STYLE_DOS },
68 { 2, 13, 10, 4, 4, "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 },
69 { 2, 12, 8, 2, 2, "C:Config.sys", RTPATH_PROP_VOLUME | RTPATH_PROP_RELATIVE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX, RTPATH_STR_F_STYLE_DOS },
70 { 1, 10, 6, 0, 0, "Config.sys", RTPATH_PROP_RELATIVE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX, RTPATH_STR_F_STYLE_DOS },
71 { 3, 15, 11, 7, 6, "C:/Win/file.ext", RTPATH_PROP_VOLUME | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX , RTPATH_STR_F_STYLE_DOS },
72 { 1, 4, 4, -1, 4, "//./", RTPATH_PROP_UNC | RTPATH_PROP_SPECIAL_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE, RTPATH_STR_F_STYLE_DOS },
73 { 2, 5, 5, 4, 4, "//./f", RTPATH_PROP_UNC | RTPATH_PROP_SPECIAL_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_DOS },
74 { 2, 5, 6, 5, 5, "//.//f", RTPATH_PROP_UNC | RTPATH_PROP_SPECIAL_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_EXTRA_SLASHES, RTPATH_STR_F_STYLE_DOS },
75 { 3, 7, 7, 6, 5, "//././f", RTPATH_PROP_UNC | RTPATH_PROP_SPECIAL_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_DOT_REFS, RTPATH_STR_F_STYLE_DOS },
76 { 3, 8, 8, 7, 6, "//.././f", RTPATH_PROP_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_DOT_REFS, RTPATH_STR_F_STYLE_DOS },
77 { 3, 9, 9, 8, 7, "//../../f", RTPATH_PROP_UNC | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE | RTPATH_PROP_FILENAME | RTPATH_PROP_DOTDOT_REFS, RTPATH_STR_F_STYLE_DOS },
78 { 1, 1, 1, -1, 1, "/", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE, RTPATH_STR_F_STYLE_UNIX },
79 { 2, 4, 4, 1, 1, "/bin", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
80 { 2, 5, 5, -1, 4, "/bin/", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_DIR_SLASH, RTPATH_STR_F_STYLE_UNIX },
81 { 3, 7, 7, 5, 4, "/bin/ls", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
82 { 3, 12, 7, 5, 4, "/etc/rc.conf", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX, RTPATH_STR_F_STYLE_UNIX },
83 { 1, 1, 2, -1, 2, "//", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_EXTRA_SLASHES, RTPATH_STR_F_STYLE_UNIX },
84 { 1, 1, 3, -1, 3, "///", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_EXTRA_SLASHES, RTPATH_STR_F_STYLE_UNIX },
85 { 3, 6, 7, 4, 2, "/.//bin", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_EXTRA_SLASHES | RTPATH_PROP_DOT_REFS | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
86 { 1, 3, 3, 0, 0, "bin", RTPATH_PROP_RELATIVE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
87 { 1, 4, 4, -1, 3, "bin/", RTPATH_PROP_RELATIVE | RTPATH_PROP_DIR_SLASH, RTPATH_STR_F_STYLE_UNIX },
88 { 1, 4, 7, -1, 3, "bin////", RTPATH_PROP_RELATIVE | RTPATH_PROP_DIR_SLASH | RTPATH_PROP_EXTRA_SLASHES, RTPATH_STR_F_STYLE_UNIX },
89 { 3, 10, 10, 7, 6, "bin/../usr", RTPATH_PROP_RELATIVE | RTPATH_PROP_DOTDOT_REFS | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
90 { 4, 11, 11, 8, 7, "/bin/../usr", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE | RTPATH_PROP_DOTDOT_REFS | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
91 { 4, 8, 8, 7, 6, "/a/.../u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
92 { 4, 8, 8, 7, 6, "/a/.b./u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
93 { 4, 8, 8, 7, 6, "/a/..c/u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
94 { 4, 8, 8, 7, 6, "/a/d../u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
95 { 4, 8, 8, 6, 5, "/a/.e/.u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
96 { 4, 8, 8, 6, 5, "/a/.f/.u", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
97 { 4, 11, 7, 6, 5, "/a/.f/u.ext", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME | RTPATH_PROP_SUFFIX, RTPATH_STR_F_STYLE_UNIX },
98 { 4, 8, 8, 6, 5, "/a/.g/u.", RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE | RTPATH_PROP_FILENAME, RTPATH_STR_F_STYLE_UNIX },
99 { 3, 9, 10, 5, 4, "/a/h/u.ext", RTPATH_PROP_EXTRA_SLASHES | RTPATH_PROP_RELATIVE, RTPATH_STR_F_STYLE_UNIX | RTPATH_STR_F_MIDDLE },
100 { 3, 9, 9, 5, 3, "a/h/u.ext", RTPATH_PROP_RELATIVE, RTPATH_STR_F_STYLE_UNIX | RTPATH_STR_F_MIDDLE },
101 { 3, 9, 10, -1, 10, "a/h/u.ext/", RTPATH_PROP_EXTRA_SLASHES | RTPATH_PROP_RELATIVE, RTPATH_STR_F_STYLE_UNIX | RTPATH_STR_F_MIDDLE },
102 };
103
104 char szPath1[RTPATH_MAX];
105 union
106 {
107 RTPATHPARSED Parsed;
108 RTPATHSPLIT Split;
109 uint8_t ab[4096];
110 } u;
111
112 RTTestSub(hTest, "RTPathParse");
113 for (uint32_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
114 {
115 memset(&u, i & 1 ? 0xff : 0, sizeof(u));
116 int rc = RTPathParse(s_aTests[i].pszPath, &u.Parsed, sizeof(u), s_aTests[i].fFlags);
117 if ( rc != VINF_SUCCESS
118 || s_aTests[i].cComps != u.Parsed.cComps
119 || s_aTests[i].fProps != u.Parsed.fProps
120 || s_aTests[i].offSuffix != u.Parsed.offSuffix
121 || s_aTests[i].cchPath != u.Parsed.cchPath)
122 {
123 RTTestFailed(hTest, "i=%d rc=%Rrc %s", i, rc, s_aTests[i].pszPath);
124 RTTestFailureDetails(hTest,
125 " cComps %u, got %u\n"
126 " fProps %#x, got %#x, xor=>%#x\n"
127 " offSuffix %u, got %u\n"
128 " cchPath %u, got %u\n"
129 ,
130 s_aTests[i].cComps, u.Parsed.cComps,
131 s_aTests[i].fProps, u.Parsed.fProps, s_aTests[i].fProps ^ u.Parsed.fProps,
132 s_aTests[i].offSuffix, u.Parsed.offSuffix,
133 s_aTests[i].cchPath, u.Parsed.cchPath);
134 }
135 else
136 {
137 rc = RTPathParsedReassemble(s_aTests[i].pszPath, &u.Parsed, s_aTests[i].fFlags & ~RTPATH_STR_F_MIDDLE,
138 szPath1, sizeof(szPath1));
139 if (rc == VINF_SUCCESS)
140 {
141 RTTESTI_CHECK_MSG(strlen(szPath1) == s_aTests[i].cchPath, ("%s\n", szPath1));
142 if ( !(u.Parsed.fProps & RTPATH_PROP_EXTRA_SLASHES)
143 && (s_aTests[i].fFlags & RTPATH_STR_F_STYLE_MASK) != RTPATH_STR_F_STYLE_DOS)
144 RTTESTI_CHECK_MSG(strcmp(szPath1, s_aTests[i].pszPath) == 0, ("%s\n", szPath1));
145 }
146 else
147 RTTestIFailed("RTPathParsedReassemble -> %Rrc", rc);
148 }
149 }
150
151 RTTestSub(hTest, "RTPathSplit");
152 for (uint32_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
153 {
154 memset(&u, i & 1 ? 0xff : 0, sizeof(u));
155 int rc = RTPathSplit(s_aTests[i].pszPath, &u.Split, sizeof(u), s_aTests[i].fFlags);
156 if ( rc != VINF_SUCCESS
157 || s_aTests[i].cComps != u.Split.cComps
158 || s_aTests[i].fProps != u.Split.fProps
159 || s_aTests[i].cchPath != u.Split.cchPath)
160 {
161 RTTestFailed(hTest, "i=%d rc=%Rrc %s", i, rc, s_aTests[i].pszPath);
162 RTTestFailureDetails(hTest,
163 " cComps %u, got %u\n"
164 " fProps %#x, got %#x, xor=>%#x\n"
165 " cchPath %u, got %u\n"
166 ,
167 s_aTests[i].cComps, u.Split.cComps,
168 s_aTests[i].fProps, u.Split.fProps, s_aTests[i].fProps ^ u.Split.fProps,
169 s_aTests[i].cchPath, u.Split.cchPath);
170 }
171 else
172 {
173 RTTESTI_CHECK_MSG(*u.Split.pszSuffix == '\0' || *u.Split.pszSuffix == '.', ("%s", u.Split.pszSuffix));
174 for (uint32_t idxComp = RTPATH_PROP_HAS_ROOT_SPEC(u.Split.fProps); idxComp < u.Split.cComps; idxComp++)
175 if ( (s_aTests[i].fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STR_F_STYLE_DOS
176 ? strpbrk(u.Split.apszComps[idxComp], "/\\")
177 : strchr(u.Split.apszComps[idxComp], RTPATH_SLASH) )
178 RTTestFailed(hTest, "i=%d idxComp=%d '%s'", i, idxComp, u.Split.apszComps[idxComp]);
179
180 PRTPATHSPLIT pSplit = NULL;
181 RTTESTI_CHECK_RC(rc = RTPathSplitA(s_aTests[i].pszPath, &pSplit, s_aTests[i].fFlags), VINF_SUCCESS);
182 if (RT_SUCCESS(rc))
183 {
184 RTTESTI_CHECK(pSplit);
185 RTTESTI_CHECK(pSplit->cComps == u.Split.cComps);
186 RTTESTI_CHECK(pSplit->fProps == u.Split.fProps);
187 RTTESTI_CHECK(pSplit->cchPath == u.Split.cchPath);
188 RTTESTI_CHECK(pSplit->cbNeeded == u.Split.cbNeeded);
189 RTTESTI_CHECK(!strcmp(pSplit->pszSuffix, u.Split.pszSuffix));
190 for (uint32_t idxComp = 0; idxComp < u.Split.cComps; idxComp++)
191 RTTESTI_CHECK(!strcmp(pSplit->apszComps[idxComp], u.Split.apszComps[idxComp]));
192 RTPathSplitFree(pSplit);
193 }
194
195 rc = RTPathSplitReassemble(&u.Split, s_aTests[i].fFlags & ~RTPATH_STR_F_MIDDLE, szPath1, sizeof(szPath1));
196 if (rc == VINF_SUCCESS)
197 {
198 RTTESTI_CHECK_MSG(strlen(szPath1) == s_aTests[i].cchPath, ("%s\n", szPath1));
199 if ( !(u.Parsed.fProps & RTPATH_PROP_EXTRA_SLASHES)
200 && (s_aTests[i].fFlags & RTPATH_STR_F_STYLE_MASK) != RTPATH_STR_F_STYLE_DOS)
201 RTTESTI_CHECK_MSG(strcmp(szPath1, s_aTests[i].pszPath) == 0, ("%s\n", szPath1));
202 }
203 else
204 RTTestIFailed("RTPathSplitReassemble -> %Rrc", rc);
205 }
206 }
207
208 RTTestSub(hTest, "RTPathParseSimple");
209 for (uint32_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
210 {
211 if ( (s_aTests[i].fFlags & RTPATH_STR_F_STYLE_MASK) != RTPATH_STR_F_STYLE_HOST
212 && (s_aTests[i].fFlags & RTPATH_STR_F_STYLE_MASK) != RTPATH_STYLE)
213 continue;
214 if (s_aTests[i].fFlags & ~RTPATH_STR_F_STYLE_MASK)
215 continue;
216
217 size_t const cchPathIn = strlen(s_aTests[i].pszPath);
218 size_t cchDir = ~(size_t)1;
219 ssize_t offName = -97;
220 ssize_t offSuff = -99;
221 size_t cchPath = RTPathParseSimple(s_aTests[i].pszPath, &cchDir, &offName, &offSuff);
222 if ( cchPath != cchPathIn
223 || offSuff != (s_aTests[i].offSuffix >= cchPathIn ? -1 : s_aTests[i].offSuffix)
224 || offName != s_aTests[i].offName
225 || cchDir != s_aTests[i].cchDir)
226 {
227 RTTestFailed(hTest, "i=%u %s", i, s_aTests[i].pszPath);
228 RTTestFailureDetails(hTest,
229 " cchPath %zu, got %zu\n"
230 " cchDir %u, got %zu\n"
231 " offName %d, got %zd\n"
232 " offSuff %d, got %zd\n"
233 ,
234 cchPathIn, cchPath,
235 s_aTests[i].cchDir, cchDir,
236 s_aTests[i].offName, offName,
237 (s_aTests[i].offSuffix >= cchPathIn ? -1 : s_aTests[i].offSuffix), offSuff);
238 }
239 }
240}
241
242
243static void testParentLength(RTTEST hTest)
244{
245 static struct
246 {
247 const char *pszPath;
248 uint32_t cchNonParent;
249 uint32_t fFlags;
250 } const s_aTests[] =
251 {
252 { "/usr/bin", 3, RTPATH_STR_F_STYLE_UNIX },
253 { "/usr/bin", 3, RTPATH_STR_F_STYLE_DOS },
254 { "\\usr\\bin", 3, RTPATH_STR_F_STYLE_DOS },
255 { "/usr/bin/", 4, RTPATH_STR_F_STYLE_UNIX },
256 { "/usr/bin/", 4, RTPATH_STR_F_STYLE_DOS },
257 { "\\usr\\bin\\", 4, RTPATH_STR_F_STYLE_DOS },
258 { "A:\\usr\\bin\\", 4, RTPATH_STR_F_STYLE_DOS },
259 { "/bin", 3, RTPATH_STR_F_STYLE_UNIX },
260 { "/bin", 3, RTPATH_STR_F_STYLE_DOS },
261 { "\\bin", 3, RTPATH_STR_F_STYLE_DOS },
262 { "A:\\bin", 3, RTPATH_STR_F_STYLE_DOS },
263 { "A:/bin", 3, RTPATH_STR_F_STYLE_DOS },
264 { "A:bin", 3, RTPATH_STR_F_STYLE_DOS },
265 { "/bin/", 4, RTPATH_STR_F_STYLE_UNIX },
266 { "/bin/", 4, RTPATH_STR_F_STYLE_DOS },
267 { "A:\\bin\\", 4, RTPATH_STR_F_STYLE_DOS },
268 { "A:/bin\\", 4, RTPATH_STR_F_STYLE_DOS },
269 { "A:bin\\", 4, RTPATH_STR_F_STYLE_DOS },
270 { "/", 0, RTPATH_STR_F_STYLE_UNIX },
271 { "/", 0, RTPATH_STR_F_STYLE_DOS },
272 { "\\", 0, RTPATH_STR_F_STYLE_DOS },
273 { "A:\\", 0, RTPATH_STR_F_STYLE_DOS },
274 { "A:", 0, RTPATH_STR_F_STYLE_DOS },
275 { "bin", 3, RTPATH_STR_F_STYLE_UNIX },
276 { "bin", 3, RTPATH_STR_F_STYLE_DOS },
277 { "//unc/bin/bin", 3, RTPATH_STR_F_STYLE_DOS },
278 { "//unc/bin/bin/", 4, RTPATH_STR_F_STYLE_DOS },
279 { "//unc/bin", 3, RTPATH_STR_F_STYLE_DOS },
280 { "//unc/bin/", 4, RTPATH_STR_F_STYLE_DOS },
281 { "//unc/", 0, RTPATH_STR_F_STYLE_DOS },
282 { "//unc", 0, RTPATH_STR_F_STYLE_DOS },
283 };
284
285 RTTestSub(hTest, "RTPathParentLength");
286 for (uint32_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
287 {
288 size_t const cchParent = RTPathParentLengthEx(s_aTests[i].pszPath, s_aTests[i].fFlags);
289 size_t const cchExpected = strlen(s_aTests[i].pszPath) - s_aTests[i].cchNonParent;
290 if (cchParent != cchExpected)
291 RTTestFailed(hTest, "sub-test #%u: got %u, expected %u (%s)",
292 i, cchParent, cchExpected, s_aTests[i].pszPath);
293 if (s_aTests[i].fFlags == RTPATH_STYLE)
294 {
295 size_t const cchParent2 = RTPathParentLength(s_aTests[i].pszPath);
296 if (cchParent2 != cchExpected)
297 RTTestFailed(hTest, "sub-test #%u: RTPathParentLength returned %u, expected %u (%s)",
298 i, cchParent2, cchExpected, s_aTests[i].pszPath);
299 }
300 }
301}
302
303
304static void testPurgeFilename(RTTEST hTest)
305{
306 static struct
307 {
308 const char *pszIn, *pszOut;
309 uint32_t fFlags;
310 } const s_aTests[] =
311 {
312 { "start///end", "start___end", RTPATH_STR_F_STYLE_UNIX },
313 { "start///end", "start___end", RTPATH_STR_F_STYLE_DOS },
314 { "start///end", "start___end", RTPATH_STR_F_STYLE_HOST },
315 { "1:<>\\9", "1:<>\\9", RTPATH_STR_F_STYLE_UNIX },
316 { "1:<>\\9", "1____9", RTPATH_STR_F_STYLE_DOS },
317 { "\t\r\n", "\t\r\n", RTPATH_STR_F_STYLE_UNIX },
318 { "\t\r\n", "___", RTPATH_STR_F_STYLE_DOS },
319 };
320 RTTestSub(hTest, "RTPathPurgeFilename");
321 for (uint32_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
322 {
323 char szPath[RTPATH_MAX];
324 strcpy(szPath, s_aTests[i].pszIn);
325 char *pszRet = RTPathPurgeFilename(szPath, s_aTests[i].fFlags);
326 RTTEST_CHECK(hTest, pszRet == &szPath[0]);
327 if (strcmp(szPath, s_aTests[i].pszOut) != 0)
328 RTTestFailed(hTest, "sub-test #%u: got '%s', expected '%s' (style %#x)",
329 i, szPath, s_aTests[i].pszOut, s_aTests[i].fFlags);
330 }
331}
332
333
334static void testEnsureTrailingSeparator(RTTEST hTest)
335{
336 static struct
337 {
338 const char *pszIn, *pszOut;
339 uint32_t fFlags;
340 } const s_aTests[] =
341 {
342 { "/foo", "/foo/", RTPATH_STR_F_STYLE_UNIX },
343 { "/foo\\", "/foo\\/", RTPATH_STR_F_STYLE_UNIX },
344 { "/foo:", "/foo:/", RTPATH_STR_F_STYLE_UNIX },
345 { "/foo/", "/foo/", RTPATH_STR_F_STYLE_UNIX },
346 { "D:/foo", "D:/foo\\", RTPATH_STR_F_STYLE_DOS },
347 { "D:/foo\\", "D:/foo\\", RTPATH_STR_F_STYLE_DOS },
348 { "", "./", RTPATH_STR_F_STYLE_UNIX},
349 { "", ".\\", RTPATH_STR_F_STYLE_DOS },
350 { "", "." RTPATH_SLASH_STR, RTPATH_STR_F_STYLE_HOST },
351 { ".", "." RTPATH_SLASH_STR, RTPATH_STR_F_STYLE_HOST },
352 { "x", "x" RTPATH_SLASH_STR, RTPATH_STR_F_STYLE_HOST },
353 { "y" RTPATH_SLASH_STR, "y" RTPATH_SLASH_STR, RTPATH_STR_F_STYLE_HOST },
354 };
355 RTTestSub(hTest, "RTPathEnsureTrailingSeparatorEx");
356 for (uint32_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
357 {
358 char szPath[RTPATH_MAX];
359 strcpy(szPath, s_aTests[i].pszIn);
360 size_t cchRet = RTPathEnsureTrailingSeparatorEx(szPath, sizeof(szPath), s_aTests[i].fFlags);
361 RTTEST_CHECK(hTest, cchRet == strlen(s_aTests[i].pszOut));
362 if (strcmp(szPath, s_aTests[i].pszOut) != 0)
363 RTTestFailed(hTest, "sub-test #%u: got '%s', expected '%s' (style %#x)",
364 i, szPath, s_aTests[i].pszOut, s_aTests[i].fFlags);
365 }
366}
367
368
369static void testFindCommon(RTTEST hTest)
370{
371 RTTestSub(hTest, "RTPathFindCommon");
372
373 static struct
374 {
375 char const *apszPaths[4];
376 uint32_t fFlags;
377 char const *pszCommon;
378 } const aTests[] =
379 {
380 /* Simple stuff first. */
381 { { "", "", "", NULL, }, RTPATH_STR_F_STYLE_UNIX,
382 "" },
383 { { "", "", "", NULL, }, RTPATH_STR_F_STYLE_DOS,
384 "" },
385 { { "none", "none", "", NULL, }, RTPATH_STR_F_STYLE_UNIX,
386 "" },
387 { { "none", "none", "", NULL, }, RTPATH_STR_F_STYLE_DOS,
388 "" },
389 { { "same", "same", "same", "same", }, RTPATH_STR_F_STYLE_UNIX,
390 "same" },
391 { { "same", "same", "same", "same", }, RTPATH_STR_F_STYLE_DOS,
392 "same" },
393 /* More complicated. */
394 { { "/path/to/stuff1", "path/to/stuff2", NULL, NULL, }, RTPATH_STR_F_STYLE_UNIX,
395 "" },
396 { { "/path/to/stuff1", "/path/to/stuff2", "/path/to/stuff3", NULL, }, RTPATH_STR_F_STYLE_UNIX,
397 "/path/to/" },
398 { { "/path/to/stuff1", "/path/to/", "/path/", NULL, }, RTPATH_STR_F_STYLE_UNIX,
399 "/path/" },
400 { { "/path/to/stuff1", "/", "/path/", NULL, }, RTPATH_STR_F_STYLE_UNIX,
401 "/" },
402 { { "/path/to/../stuff1", "./../", "/path/to/stuff2/..", NULL, }, RTPATH_STR_F_STYLE_UNIX,
403 "" },
404 { { "a/single/path", NULL, NULL, NULL, }, RTPATH_STR_F_STYLE_UNIX,
405 "a/single/path" },
406 { { "a/single\\path", NULL, NULL, NULL, }, RTPATH_STR_F_STYLE_DOS,
407 "a/single\\path" },
408 { { "C:\\Windows", NULL, NULL, NULL, }, RTPATH_STR_F_STYLE_DOS,
409 "C:\\Windows" },
410 { { "c:/windows", "c:\\program files", "C:\\AppData", NULL, }, RTPATH_STR_F_STYLE_DOS,
411 "c:/" },
412 { { "c:/windows", "c:windows", "C:system32", NULL, }, RTPATH_STR_F_STYLE_DOS,
413 "c:" },
414 { { "c:/windows", "d:windows", "e:windows", NULL, }, RTPATH_STR_F_STYLE_DOS,
415 "" },
416 { { "//usr/bin/env", "/usr//bin/env", "/usr/bin///env", "/usr/bin/env", }, RTPATH_STR_F_STYLE_UNIX,
417 "//usr/bin/env" },
418 { { "//usr/bin/env", "/usr//./././bin/env", "/usr/bin///env", "/usr/bin/env", }, RTPATH_STR_F_STYLE_UNIX,
419 "//usr/bin/env" },
420 { { "//./what/ever", "\\\\.\\what\\is\\up", "\\\\.\\\\what\\is\\up", NULL, }, RTPATH_STR_F_STYLE_DOS,
421 "//./what/" },
422 { { "//./unc/is/weird", "///./unc/is/weird", NULL, NULL, }, RTPATH_STR_F_STYLE_DOS,
423 "" },
424 { { "//system360/share", "//system370/share", "//system390/share", NULL, }, RTPATH_STR_F_STYLE_DOS,
425 "" },
426 { { "//system370/share1", "//sysTEM370/share2", "//SYsTeM370/share3", NULL, }, RTPATH_STR_F_STYLE_DOS,
427 "//system370/" },
428 { { "//system370/share1", "Z:/", NULL, NULL, }, RTPATH_STR_F_STYLE_DOS,
429 "" },
430 { { "//system370/share1", "/", NULL, NULL, }, RTPATH_STR_F_STYLE_DOS,
431 "" },
432 { { "//system370/share1", "somedir", NULL, NULL, }, RTPATH_STR_F_STYLE_DOS,
433 "" },
434 { { "/path/to/stuff1", "path/to/stuff2", NULL, NULL, }, RTPATH_STR_F_STYLE_UNIX | RTPATH_STR_F_NO_START,
435 "/path/to/" },
436 { { "path/to/stuff1", "//path\\/to\\stuff2", NULL, NULL, }, RTPATH_STR_F_STYLE_DOS | RTPATH_STR_F_NO_START,
437 "path/to/" },
438 /* '..' elements are not supported for now and leads to zero return, unless RTPATHFINDCOMMON_F_IGNORE_DOTDOT is given. */
439 { { "/usr/bin/env", "/usr/../usr/bin/env", "/usr/bin/../bin/env", NULL, }, RTPATH_STR_F_STYLE_UNIX,
440 "" },
441 { { "/lib/", "/lib/amd64/../lib.so", "/lib/i386/../libdl.so", NULL, }, RTPATH_STR_F_STYLE_UNIX,
442 "" },
443 { { "/lib/", "/lib/amd64/../lib.so", "/lib/i386/../libdl.so", NULL, }, RTPATH_STR_F_STYLE_UNIX | RTPATHFINDCOMMON_F_IGNORE_DOTDOT,
444 "/lib/" },
445 };
446
447 for (size_t i = 0; i < RT_ELEMENTS(aTests); i++)
448 {
449 size_t cPaths = RT_ELEMENTS(aTests[i].apszPaths);
450 while (cPaths > 0 && aTests[i].apszPaths[cPaths - 1] == NULL)
451 cPaths--;
452
453 size_t const cchCommon = RTPathFindCommonEx(cPaths, aTests[i].apszPaths, aTests[i].fFlags);
454 size_t const cchExpect = strlen(aTests[i].pszCommon);
455 if (cchCommon != cchExpect)
456 RTTestFailed(hTest,
457 "Test %zu failed: got %zu, expected %zu (cPaths=%zu: '%s' '%s' '%s' '%s', fFlags=%#x)", i, cchCommon,
458 cchExpect, cPaths, aTests[i].apszPaths[0], aTests[i].apszPaths[1], aTests[i].apszPaths[2],
459 aTests[i].apszPaths[3], aTests[i].fFlags);
460 }
461}
462
463
464int main()
465{
466 char szPath[RTPATH_MAX];
467
468 /*
469 * Init RT+Test.
470 */
471 RTTEST hTest;
472 int rc = RTTestInitAndCreate("tstRTPath", &hTest);
473 if (rc)
474 return rc;
475 RTTestBanner(hTest);
476
477 RTTestSub(hTest, "Environment");
478#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
479 RTTESTI_CHECK(RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS);
480# if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
481# else
482 RTTestIFailed("#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS");
483# endif
484 RTTESTI_CHECK(strcmp(RTPATH_SLASH_STR, "\\") == 0);
485 RTTESTI_CHECK(RTPATH_SLASH == '\\');
486 RTTESTI_CHECK(RTPATH_IS_SEP('/'));
487 RTTESTI_CHECK(RTPATH_IS_SEP('\\'));
488 RTTESTI_CHECK(RTPATH_IS_SEP(':'));
489
490#else
491 RTTESTI_CHECK(RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX);
492# if RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX
493# else
494 RTTestIFailed("#if RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX");
495# endif
496 RTTESTI_CHECK(strcmp(RTPATH_SLASH_STR, "/") == 0);
497 RTTESTI_CHECK(RTPATH_SLASH == '/');
498 RTTESTI_CHECK(RTPATH_IS_SEP('/'));
499 RTTESTI_CHECK(!RTPATH_IS_SEP('\\'));
500 RTTESTI_CHECK(!RTPATH_IS_SEP(':'));
501#endif
502
503 /*
504 * RTPathExecDir, RTPathUserHome and RTProcGetExecutablePath.
505 */
506 RTTestSub(hTest, "RTPathExecDir");
507 RTTESTI_CHECK_RC(rc = RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS);
508 if (RT_SUCCESS(rc))
509 RTTestIPrintf(RTTESTLVL_INFO, "ExecDir={%s}\n", szPath);
510
511 RTTestSub(hTest, "RTProcGetExecutablePath");
512 if (RTProcGetExecutablePath(szPath, sizeof(szPath)) == szPath)
513 RTTestIPrintf(RTTESTLVL_INFO, "ExecutableName={%s}\n", szPath);
514 else
515 RTTestIFailed("RTProcGetExecutablePath -> NULL");
516
517 RTTestSub(hTest, "RTPathUserHome");
518 RTTESTI_CHECK_RC(rc = RTPathUserHome(szPath, sizeof(szPath)), VINF_SUCCESS);
519 if (RT_SUCCESS(rc))
520 RTTestIPrintf(RTTESTLVL_INFO, "UserHome={%s}\n", szPath);
521
522 RTTestSub(hTest, "RTPathUserDocuments");
523 RTTESTI_CHECK_RC(rc = RTPathUserDocuments(szPath, sizeof(szPath)), VINF_SUCCESS);
524 if (RT_SUCCESS(rc))
525 RTTestIPrintf(RTTESTLVL_INFO, "UserDocuments={%s}\n", szPath);
526
527 RTTestSub(hTest, "RTPathTemp");
528 RTTESTI_CHECK_RC(rc = RTPathTemp(szPath, sizeof(szPath)), VINF_SUCCESS);
529 if (RT_SUCCESS(rc))
530 RTTestIPrintf(RTTESTLVL_INFO, "PathTemp={%s}\n", szPath);
531 size_t cch = strlen(szPath);
532 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch), VERR_BUFFER_OVERFLOW);
533 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+1), VINF_SUCCESS);
534 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+2), VINF_SUCCESS);
535
536
537 /*
538 * RTPathAbsEx.
539 */
540 RTTestSub(hTest, "RTPathAbsEx");
541 static const struct
542 {
543 uint32_t fFlags;
544 const char *pcszInputBase;
545 const char *pcszInputPath;
546 int rc;
547 const char *pcszOutput;
548 }
549 s_aRTPathAbsExTests[] =
550 {
551 { RTPATH_STR_F_STYLE_HOST, NULL, "", VERR_PATH_ZERO_LENGTH, NULL },
552 { RTPATH_STR_F_STYLE_HOST, NULL, ".", VINF_SUCCESS, "%p" },
553#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
554 { RTPATH_STR_F_STYLE_DOS, NULL, "\\", VINF_SUCCESS, "%d\\" },
555 { RTPATH_STR_F_STYLE_DOS, NULL, "\\..", VINF_SUCCESS, "%d\\" },
556 { RTPATH_STR_F_STYLE_DOS, NULL, "/absolute/..", VINF_SUCCESS, "%d\\" },
557 { RTPATH_STR_F_STYLE_DOS, NULL, "/absolute\\\\../..", VINF_SUCCESS, "%d\\" },
558 { RTPATH_STR_F_STYLE_DOS, NULL, "/absolute//../path\\", VINF_SUCCESS, "%d\\path\\" },
559 { RTPATH_STR_F_STYLE_DOS, NULL, "/absolute/../../path", VINF_SUCCESS, "%d\\path" },
560 { RTPATH_STR_F_STYLE_DOS, NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p\\dir\\file.txt" },
561 { RTPATH_STR_F_STYLE_DOS, NULL, "\\data\\", VINF_SUCCESS, "%d\\data\\" },
562 { RTPATH_STR_F_STYLE_DOS, "relative_base/dir\\", "\\from_root", VINF_SUCCESS, "%d\\from_root" },
563 { RTPATH_STR_F_STYLE_DOS, "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p\\relative_base\\dir\\relative_also" },
564#else
565 { RTPATH_STR_F_STYLE_UNIX, NULL, ".", VINF_SUCCESS, "%p" },
566 { RTPATH_STR_F_STYLE_UNIX, NULL, "relative/../dir/./././file.txt", VINF_SUCCESS, "%p/dir/file.txt" },
567 { RTPATH_STR_F_STYLE_UNIX, NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p/dir\\.\\.\\.\\file.txt" }, /* linux-specific */
568 { RTPATH_STR_F_STYLE_UNIX, "relative_base/dir/", "/from_root", VINF_SUCCESS, "/from_root" },
569 { RTPATH_STR_F_STYLE_UNIX, "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p/relative_base/dir/relative_also" },
570#endif
571 { RTPATH_STR_F_STYLE_UNIX, NULL, "/", VINF_SUCCESS, "/" },
572 { RTPATH_STR_F_STYLE_UNIX, NULL, "/..", VINF_SUCCESS, "/" },
573 { RTPATH_STR_F_STYLE_UNIX, NULL, "/absolute/..", VINF_SUCCESS, "/" },
574 { RTPATH_STR_F_STYLE_UNIX, NULL, "/absolute\\\\../..", VINF_SUCCESS, "/" },
575 { RTPATH_STR_F_STYLE_UNIX, NULL, "/absolute//../path/", VINF_SUCCESS, "/path/" },
576 { RTPATH_STR_F_STYLE_UNIX, NULL, "/absolute/../../path", VINF_SUCCESS, "/path" },
577 { RTPATH_STR_F_STYLE_UNIX, NULL, "/data/", VINF_SUCCESS, "/data/" },
578#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
579 { RTPATH_STR_F_STYLE_DOS, NULL, "C:\\", VINF_SUCCESS, "C:\\" },
580 { RTPATH_STR_F_STYLE_DOS, "C:\\", "..", VINF_SUCCESS, "C:\\" },
581 { RTPATH_STR_F_STYLE_DOS, "C:\\temp", "..", VINF_SUCCESS, "C:\\" },
582 { RTPATH_STR_F_STYLE_DOS, "C:\\VirtualBox/Machines", "..\\VirtualBox.xml", VINF_SUCCESS, "C:\\VirtualBox\\VirtualBox.xml" },
583 { RTPATH_STR_F_STYLE_DOS, "C:\\MustDie", "\\from_root/dir/..", VINF_SUCCESS, "C:\\from_root" },
584 { RTPATH_STR_F_STYLE_DOS, "C:\\temp", "D:\\data", VINF_SUCCESS, "D:\\data" },
585 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\server\\..\\share", VINF_SUCCESS, "\\\\server\\..\\share" /* kind of strange */ },
586 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\server/", VINF_SUCCESS, "\\\\server\\" },
587 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\", VINF_SUCCESS, "\\\\" },
588 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\\\something", VINF_SUCCESS, "\\\\\\something" /* kind of strange */ },
589 { RTPATH_STR_F_STYLE_DOS, "\\\\server\\share_as_base", "/from_root", VINF_SUCCESS, "\\\\server\\share_as_base\\from_root" },
590 { RTPATH_STR_F_STYLE_DOS, "\\\\just_server", "/from_root", VINF_SUCCESS, "\\\\just_server\\from_root" },
591 { RTPATH_STR_F_STYLE_DOS, "\\\\server\\share_as_base", "relative\\data", VINF_SUCCESS, "\\\\server\\share_as_base\\relative\\data" },
592 { RTPATH_STR_F_STYLE_DOS, "base", "\\\\?\\UNC\\relative/edwef/..", VINF_SUCCESS, "\\\\?\\UNC\\relative" },
593 { RTPATH_STR_F_STYLE_DOS, "\\\\?\\UNC\\base", "/from_root", VINF_SUCCESS, "\\\\?\\from_root" },
594 { RTPATH_STR_F_STYLE_DOS, "\\\\?\\UNC\\base", "./..", VINF_SUCCESS, "\\\\?\\UNC" },
595 { RTPATH_STR_F_STYLE_DOS | RTPATHABS_F_STOP_AT_BASE, "\\\\?\\UNC\\base", "./..", VINF_SUCCESS, "\\\\?\\UNC\\base" },
596 { RTPATH_STR_F_STYLE_DOS | RTPATHABS_F_STOP_AT_BASE, "\\\\?\\UNC\\base", "/..", VINF_SUCCESS, "\\\\?\\" },
597 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\.\\asdf\\..", VINF_SUCCESS, "\\\\.\\" },
598 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\?\\asdf\\..", VINF_SUCCESS, "\\\\?\\" },
599 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\x\\asdf\\..", VINF_SUCCESS, "\\\\x\\asdf" },
600#else
601 { RTPATH_STR_F_STYLE_UNIX, "\\temp", "\\data", VINF_SUCCESS, "%p/\\temp/\\data" },
602#endif
603 { RTPATH_STR_F_STYLE_UNIX, "/VirtualBox/Machines", "../VirtualBox.xml", VINF_SUCCESS, "/VirtualBox/VirtualBox.xml" },
604 { RTPATH_STR_F_STYLE_UNIX, "/MustDie", "/from_root/dir/..", VINF_SUCCESS, "/from_root" },
605 { RTPATH_STR_F_STYLE_UNIX, "/temp", "..", VINF_SUCCESS, "/" },
606 };
607
608 char *pszGuardedBuf = NULL;
609 rc = RTTestGuardedAlloc(hTest, RTPATH_MAX, 0, false /*fHead*/, (void **)&pszGuardedBuf);
610 if (RT_FAILURE(rc))
611 pszGuardedBuf = szPath;
612
613 for (unsigned i = 0; i < RT_ELEMENTS(s_aRTPathAbsExTests); ++ i)
614 {
615 if (RT_FAILURE(s_aRTPathAbsExTests[i].rc))
616 RTTestDisableAssertions(hTest);
617
618 size_t cbAbsPath = sizeof(szPath);
619 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase,
620 s_aRTPathAbsExTests[i].pcszInputPath,
621 s_aRTPathAbsExTests[i].fFlags,
622 szPath, &cbAbsPath);
623
624 if (RT_FAILURE(s_aRTPathAbsExTests[i].rc))
625 RTTestRestoreAssertions(hTest);
626
627 if (rc != s_aRTPathAbsExTests[i].rc)
628 {
629 RTTestIFailed("#%u: unexpected result code!\n"
630 " flags: %#x\n"
631 " input base: '%s'\n"
632 " input path: '%s'\n"
633 " output: '%s'\n"
634 " rc: %Rrc\n"
635 " expected rc: %Rrc",
636 i,
637 s_aRTPathAbsExTests[i].fFlags,
638 s_aRTPathAbsExTests[i].pcszInputBase,
639 s_aRTPathAbsExTests[i].pcszInputPath,
640 szPath, rc,
641 s_aRTPathAbsExTests[i].rc);
642 continue;
643 }
644
645 char szTmp[RTPATH_MAX];
646 char *pszExpected = NULL;
647 if (s_aRTPathAbsExTests[i].pcszOutput != NULL)
648 {
649 if (s_aRTPathAbsExTests[i].pcszOutput[0] == '%')
650 {
651 RTTESTI_CHECK_RC(rc = RTPathGetCurrent(szTmp, sizeof(szTmp)), VINF_SUCCESS);
652 if (RT_FAILURE(rc))
653 break;
654
655 pszExpected = szTmp;
656
657 if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'p')
658 {
659 cch = strlen(szTmp);
660 if (cch + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
661 strcpy(szTmp + cch, s_aRTPathAbsExTests[i].pcszOutput + 2);
662 }
663#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
664 else if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'd')
665 {
666 if (2 + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
667 strcpy(szTmp + 2, s_aRTPathAbsExTests[i].pcszOutput + 2);
668 }
669#endif
670 }
671 else
672 {
673 strcpy(szTmp, s_aRTPathAbsExTests[i].pcszOutput);
674 pszExpected = szTmp;
675 }
676
677 if ( strcmp(szPath, pszExpected)
678 || strlen(szPath) != cbAbsPath)
679 {
680 RTTestIFailed("#%u: Unexpected result\n"
681 " flags: %#x\n"
682 " input base: '%s'\n"
683 " input path: '%s'\n"
684 " output: '%s'\n"
685 " expected: '%s' ('%s')\n"
686 " cchResult: %#x, actual %#x",
687 i,
688 s_aRTPathAbsExTests[i].fFlags,
689 s_aRTPathAbsExTests[i].pcszInputBase,
690 s_aRTPathAbsExTests[i].pcszInputPath,
691 szPath,
692 pszExpected, s_aRTPathAbsExTests[i].pcszOutput,
693 cbAbsPath, strlen(szPath));
694 continue;
695 }
696
697 if (RT_SUCCESS(s_aRTPathAbsExTests[i].rc))
698 {
699 /* Test the RTPATHABS_F_ENSURE_TRAILING_SLASH flag: */
700 cbAbsPath = sizeof(szPath);
701 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase,
702 s_aRTPathAbsExTests[i].pcszInputPath,
703 s_aRTPathAbsExTests[i].fFlags | RTPATHABS_F_ENSURE_TRAILING_SLASH,
704 szPath, &cbAbsPath);
705 char chSlash = (s_aRTPathAbsExTests[i].fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STR_F_STYLE_DOS ? '\\'
706 : (s_aRTPathAbsExTests[i].fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STR_F_STYLE_UNIX ? '/'
707 : RTPATH_SLASH;
708 if ( RT_FAILURE(rc)
709 || strlen(szPath) != cbAbsPath
710 || szPath[cbAbsPath - 1] != chSlash)
711 RTTestIFailed("#%u: Unexpected RTPATHABS_F_ENSURE_TRAILING_SLASH result: %Rrc\n"
712 " flags: %#x | RTPATHABS_F_ENSURE_TRAILING_SLASH\n"
713 " input base: '%s'\n"
714 " input path: '%s'\n"
715 " output: '%s' ('%c' vs '%c')\n"
716 " cchResult: %#x, actual %#x",
717 i, rc,
718 s_aRTPathAbsExTests[i].fFlags,
719 s_aRTPathAbsExTests[i].pcszInputBase,
720 s_aRTPathAbsExTests[i].pcszInputPath,
721 szPath, szPath[cbAbsPath - 1], chSlash,
722 cbAbsPath, strlen(szPath));
723
724 /* Do overflow testing: */
725 size_t const cbNeeded = strlen(pszExpected) + 1;
726 for (size_t cbBuf = 0; cbBuf < cbNeeded + 64; cbBuf++)
727 {
728 char *pszBuf = &pszGuardedBuf[RTPATH_MAX - cbBuf];
729 memset(pszBuf, 0x33, cbBuf);
730 cbAbsPath = cbBuf;
731 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase, s_aRTPathAbsExTests[i].pcszInputPath,
732 s_aRTPathAbsExTests[i].fFlags, pszBuf, &cbAbsPath);
733 if ( cbBuf < cbNeeded
734 && ( rc != VERR_BUFFER_OVERFLOW
735 || cbAbsPath < cbNeeded))
736 RTTestIFailed("#%u: Unexpected overflow result: %Rrc%s\n"
737 " flags: %#x\n"
738 " input base: '%s'\n"
739 " input path: '%s'\n"
740 " cbBuf[in]: %#x\n"
741 " cbBuf[out]: %#x\n"
742 " cbNeeded: %#x\n",
743 i, rc, rc != VERR_BUFFER_OVERFLOW ? " - expected VERR_BUFFER_OVERFLOW" : "",
744 s_aRTPathAbsExTests[i].fFlags,
745 s_aRTPathAbsExTests[i].pcszInputBase,
746 s_aRTPathAbsExTests[i].pcszInputPath,
747 cbBuf,
748 cbAbsPath,
749 cbNeeded);
750 else if ( cbBuf >= cbNeeded
751 && ( rc != s_aRTPathAbsExTests[i].rc
752 || cbAbsPath != cbNeeded - 1
753 || strcmp(pszBuf, pszExpected)
754 || strlen(pszBuf) != cbAbsPath))
755 RTTestIFailed("#%u: Unexpected result: %Rrc (expected %Rrc)\n"
756 " flags: %#x\n"
757 " input base: '%s'\n"
758 " input path: '%s'\n"
759 " cbBuf[in]: %#x\n"
760 " cbBuf[out]: %#x\n"
761 " cbNeeded: %#x\n",
762 i, rc, s_aRTPathAbsExTests[i].rc,
763 s_aRTPathAbsExTests[i].fFlags,
764 s_aRTPathAbsExTests[i].pcszInputBase,
765 s_aRTPathAbsExTests[i].pcszInputPath,
766 cbBuf,
767 cbAbsPath,
768 cbNeeded);
769
770 }
771 }
772
773 /* RTPathAbsExDup */
774 char *pszDup = RTPathAbsExDup(s_aRTPathAbsExTests[i].pcszInputBase,
775 s_aRTPathAbsExTests[i].pcszInputPath,
776 s_aRTPathAbsExTests[i].fFlags);
777 if ( (RT_SUCCESS(s_aRTPathAbsExTests[i].rc) ? pszDup == NULL : pszDup != NULL)
778 || RTStrCmp(pszDup, pszExpected))
779 RTTestIFailed("#%u: Unexpected RTPathAbsExDup result: %p%s\n"
780 " flags: %#x\n"
781 " input base: '%s'\n"
782 " input path: '%s'\n"
783 " output: '%s'\n"
784 " expected: '%s' ('%s')\n",
785 i, pszDup,
786 (RT_SUCCESS(s_aRTPathAbsExTests[i].rc) ? pszDup == NULL : pszDup != NULL) ? pszDup ? "NULL" : "!NULL" : "",
787 s_aRTPathAbsExTests[i].fFlags,
788 s_aRTPathAbsExTests[i].pcszInputBase,
789 s_aRTPathAbsExTests[i].pcszInputPath,
790 pszDup,
791 pszExpected, s_aRTPathAbsExTests[i].pcszOutput);
792 RTStrFree(pszDup);
793 }
794 }
795
796 if (pszGuardedBuf != szPath)
797 RTTestGuardedFree(hTest, pszGuardedBuf);
798
799
800 /*
801 * RTPathStripFilename
802 */
803 RTTestSub(hTest, "RTPathStripFilename");
804 static const char *s_apszStripFilenameTests[] =
805 {
806 "/usr/include///", "/usr/include//",
807 "/usr/include/", "/usr/include",
808 "/usr/include", "/usr",
809 "/usr", "/",
810 "usr", ".",
811#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
812 "c:/windows", "c:/",
813 "c:/", "c:/",
814 "D:", "D:",
815 "C:\\OS2\\DLLS", "C:\\OS2",
816#endif
817 };
818 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripFilenameTests); i += 2)
819 {
820 const char *pszInput = s_apszStripFilenameTests[i];
821 const char *pszExpect = s_apszStripFilenameTests[i + 1];
822 strcpy(szPath, pszInput);
823 RTPathStripFilename(szPath);
824 if (strcmp(szPath, pszExpect))
825 {
826 RTTestIFailed("Unexpected result\n"
827 " input: '%s'\n"
828 " output: '%s'\n"
829 "expected: '%s'",
830 pszInput, szPath, pszExpect);
831 }
832 }
833
834 /*
835 * RTPathAppend.
836 */
837 RTTestSub(hTest, "RTPathAppend");
838 static struct { uint32_t fFlags; const char *pszInput, *pszAppend, *pszExpect; } s_aAppendTests[] =
839 {
840 /* fFlags, input append expected result */
841 { RTPATH_STR_F_STYLE_HOST, "/", "", "/" },
842 { RTPATH_STR_F_STYLE_HOST, "", "/", "/" },
843 { RTPATH_STR_F_STYLE_HOST, "/", "/", "/" },
844 { RTPATH_STR_F_STYLE_HOST, "/x", "", "/x" },
845 { RTPATH_STR_F_STYLE_HOST, "/x", "/", "/x/" },
846 { RTPATH_STR_F_STYLE_HOST, "/", "x", "/x" },
847 { RTPATH_STR_F_STYLE_HOST, "dir", "file", "dir" RTPATH_SLASH_STR "file" },
848 { RTPATH_STR_F_STYLE_HOST, "dir", "/file", "dir/file" },
849 { RTPATH_STR_F_STYLE_HOST, "dir", "//file", "dir/file" },
850 { RTPATH_STR_F_STYLE_HOST, "dir", "///file", "dir/file" },
851 { RTPATH_STR_F_STYLE_HOST, "dir/", "/file", "dir/file" },
852 { RTPATH_STR_F_STYLE_HOST, "dir/", "//file", "dir/file" },
853 { RTPATH_STR_F_STYLE_HOST, "dir/", "///file", "dir/file" },
854 { RTPATH_STR_F_STYLE_HOST, "dir//", "file", "dir/file" },
855 { RTPATH_STR_F_STYLE_HOST, "dir//", "/file", "dir/file" },
856 { RTPATH_STR_F_STYLE_HOST, "dir//", "//file", "dir/file" },
857 { RTPATH_STR_F_STYLE_HOST, "dir///", "///file", "dir/file" },
858 { RTPATH_STR_F_STYLE_HOST, "/bin/testcase", "foo.r0", "/bin/testcase" RTPATH_SLASH_STR "foo.r0" },
859 { RTPATH_STR_F_STYLE_DOS, "/", "\\", "/" },
860 { RTPATH_STR_F_STYLE_DOS, "\\", "/", "\\" },
861 { RTPATH_STR_F_STYLE_DOS, "\\\\srv\\shr", "dir//", "\\\\srv\\shr\\dir//" },
862 { RTPATH_STR_F_STYLE_DOS, "\\\\srv\\shr", "dir//file", "\\\\srv\\shr\\dir//file" },
863 { RTPATH_STR_F_STYLE_DOS, "\\\\srv\\shr", "//dir//", "\\\\srv\\shr/dir//" },
864 { RTPATH_STR_F_STYLE_DOS, "\\\\srv\\shr", "/\\dir//", "\\\\srv\\shr\\dir//" },
865 { RTPATH_STR_F_STYLE_DOS, "\\\\", "not-srv/not-shr/file", "\\not-srv/not-shr/file" },
866 { RTPATH_STR_F_STYLE_DOS, "C:", "autoexec.bat", "C:autoexec.bat" },
867 { RTPATH_STR_F_STYLE_DOS, "C:", "/autoexec.bat", "C:/autoexec.bat" },
868 { RTPATH_STR_F_STYLE_DOS, "C:", "\\autoexec.bat", "C:\\autoexec.bat" },
869 { RTPATH_STR_F_STYLE_DOS, "C:\\", "/autoexec.bat", "C:\\autoexec.bat" },
870 { RTPATH_STR_F_STYLE_DOS, "C:\\\\", "autoexec.bat", "C:\\autoexec.bat" },
871 { RTPATH_STR_F_STYLE_DOS, "E:\\bin\\testcase", "foo.r0", "E:\\bin\\testcase\\foo.r0" },
872 { RTPATH_STR_F_STYLE_UNIX, "dir\\", "\\file", "dir\\/\\file" },
873 };
874 for (unsigned i = 0; i < RT_ELEMENTS(s_aAppendTests); i++)
875 {
876 const char * const pszInput = s_aAppendTests[i].pszInput;
877 const char * const pszAppend = s_aAppendTests[i].pszAppend;
878 const char * const pszExpect = s_aAppendTests[i].pszExpect;
879 uint32_t const fFlags = s_aAppendTests[i].fFlags;
880
881 strcpy(szPath, pszInput);
882 RTTESTI_CHECK_RC(rc = RTPathAppendEx(szPath, sizeof(szPath), pszAppend, RTSTR_MAX, fFlags), VINF_SUCCESS);
883 if (RT_FAILURE(rc))
884 continue;
885 if (strcmp(szPath, pszExpect))
886 RTTestIFailed("Unexpected result\n"
887 " input: '%s', fFlags=%#x\n"
888 " append: '%s'\n"
889 " output: '%s'\n"
890 "expected: '%s'",
891 pszInput, fFlags, pszAppend, szPath, pszExpect);
892 else
893 {
894 size_t const cchResult = strlen(szPath);
895
896 strcpy(szPath, pszInput);
897 RTTESTI_CHECK_RC(rc = RTPathAppendEx(szPath, cchResult + 2, pszAppend, RTSTR_MAX, fFlags), VINF_SUCCESS);
898 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
899
900 strcpy(szPath, pszInput);
901 RTTESTI_CHECK_RC(rc = RTPathAppendEx(szPath, cchResult + 1, pszAppend, RTSTR_MAX, fFlags), VINF_SUCCESS);
902 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
903
904 if (strlen(pszInput) < cchResult)
905 {
906 strcpy(szPath, pszInput);
907 RTTESTI_CHECK_RC(RTPathAppendEx(szPath, cchResult, pszAppend, RTSTR_MAX, fFlags), VERR_BUFFER_OVERFLOW);
908 }
909 }
910 }
911
912 /*
913 * RTPathJoin - reuse the append tests.
914 */
915 RTTestSub(hTest, "RTPathJoin");
916 for (unsigned i = 0; i < RT_ELEMENTS(s_aAppendTests); i++)
917 {
918 const char * const pszInput = s_aAppendTests[i].pszInput;
919 const char * const pszAppend = s_aAppendTests[i].pszAppend;
920 const char * const pszExpect = s_aAppendTests[i].pszExpect;
921 uint32_t const fFlags = s_aAppendTests[i].fFlags;
922
923 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
924
925 RTTESTI_CHECK_RC(rc = RTPathJoinEx(szPath, sizeof(szPath), pszInput, RTSTR_MAX, pszAppend, RTSTR_MAX, fFlags), VINF_SUCCESS);
926 if (RT_FAILURE(rc))
927 continue;
928 if (strcmp(szPath, pszExpect))
929 RTTestIFailed("Unexpected result\n"
930 " input: '%s', fFlags=%#x\n"
931 " append: '%s'\n"
932 " output: '%s'\n"
933 "expected: '%s'",
934 pszInput, fFlags, pszAppend, szPath, pszExpect);
935 else
936 {
937 size_t const cchResult = strlen(szPath);
938
939 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
940 RTTESTI_CHECK_RC(rc = RTPathJoinEx(szPath, cchResult + 2, pszInput, RTSTR_MAX, pszAppend, RTSTR_MAX, fFlags), VINF_SUCCESS);
941 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
942
943 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
944 RTTESTI_CHECK_RC(rc = RTPathJoinEx(szPath, cchResult + 1, pszInput, RTSTR_MAX, pszAppend, RTSTR_MAX, fFlags), VINF_SUCCESS);
945 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
946
947 RTTESTI_CHECK_RC(rc = RTPathJoinEx(szPath, cchResult, pszInput, RTSTR_MAX, pszAppend, RTSTR_MAX, fFlags), VERR_BUFFER_OVERFLOW);
948 }
949 }
950
951 /*
952 * RTPathJoinA - reuse the append tests.
953 */
954 RTTestSub(hTest, "RTPathJoinA");
955 for (unsigned i = 0; i < RT_ELEMENTS(s_aAppendTests); i++)
956 {
957 const char * const pszInput = s_aAppendTests[i].pszInput;
958 const char * const pszAppend = s_aAppendTests[i].pszAppend;
959 const char * const pszExpect = s_aAppendTests[i].pszExpect;
960 uint32_t const fFlags = s_aAppendTests[i].fFlags;
961 if ( (fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STR_F_STYLE_HOST
962 || (fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STYLE)
963
964 {
965 char *pszPathDst;
966 RTTESTI_CHECK(pszPathDst = RTPathJoinA(pszInput, pszAppend));
967 if (!pszPathDst)
968 continue;
969 if (strcmp(pszPathDst, pszExpect))
970 RTTestIFailed("Unexpected result\n"
971 " input: '%s'\n"
972 " append: '%s'\n"
973 " output: '%s'\n"
974 "expected: '%s'",
975 pszInput, pszAppend, pszPathDst, pszExpect);
976 RTStrFree(pszPathDst);
977 }
978 }
979
980 /*
981 * RTPathStripTrailingSlash
982 */
983 static const char *s_apszStripTrailingSlash[] =
984 {
985 /* input result */
986 "/", "/",
987 "//", "/",
988 "////////////////////", "/",
989 "/tmp", "/tmp",
990 "/tmp////////////////", "/tmp",
991 "tmp", "tmp",
992 "tmp////////////////", "tmp",
993 "./", ".",
994#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
995 "////////////////////", "/",
996 "D:", "D:",
997 "D:/", "D:/",
998 "D:\\", "D:\\",
999 "D:\\/\\", "D:\\",
1000 "D:/\\/\\", "D:/",
1001 "C:/Temp", "C:/Temp",
1002 "C:/Temp/", "C:/Temp",
1003 "C:/Temp\\/", "C:/Temp",
1004#endif
1005 };
1006 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripTrailingSlash); i += 2)
1007 {
1008 const char *pszInput = s_apszStripTrailingSlash[i];
1009 const char *pszExpect = s_apszStripTrailingSlash[i + 1];
1010
1011 strcpy(szPath, pszInput);
1012 cch = RTPathStripTrailingSlash(szPath);
1013 if (strcmp(szPath, pszExpect))
1014 RTTestIFailed("Unexpected result\n"
1015 " input: '%s'\n"
1016 " output: '%s'\n"
1017 "expected: '%s'",
1018 pszInput, szPath, pszExpect);
1019 else
1020 RTTESTI_CHECK(cch == strlen(szPath));
1021 }
1022
1023 /*
1024 * RTPathCountComponents
1025 */
1026 RTTestSub(hTest, "RTPathCountComponents");
1027 RTTESTI_CHECK(RTPathCountComponents("") == 0);
1028 RTTESTI_CHECK(RTPathCountComponents("/") == 1);
1029 RTTESTI_CHECK(RTPathCountComponents("//") == 1);
1030 RTTESTI_CHECK(RTPathCountComponents("//////////////") == 1);
1031 RTTESTI_CHECK(RTPathCountComponents("//////////////bin") == 2);
1032 RTTESTI_CHECK(RTPathCountComponents("//////////////bin/") == 2);
1033 RTTESTI_CHECK(RTPathCountComponents("//////////////bin/////") == 2);
1034 RTTESTI_CHECK(RTPathCountComponents("..") == 1);
1035 RTTESTI_CHECK(RTPathCountComponents("../") == 1);
1036 RTTESTI_CHECK(RTPathCountComponents("../..") == 2);
1037 RTTESTI_CHECK(RTPathCountComponents("../../") == 2);
1038#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
1039 RTTESTI_CHECK(RTPathCountComponents("d:") == 1);
1040 RTTESTI_CHECK(RTPathCountComponents("d:/") == 1);
1041 RTTESTI_CHECK(RTPathCountComponents("d:/\\") == 1);
1042 RTTESTI_CHECK(RTPathCountComponents("d:\\") == 1);
1043 RTTESTI_CHECK(RTPathCountComponents("c:\\config.sys") == 2);
1044 RTTESTI_CHECK(RTPathCountComponents("c:\\windows") == 2);
1045 RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\") == 2);
1046 RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\system32") == 3);
1047 RTTESTI_CHECK(RTPathCountComponents("//./C$") == 1);
1048 RTTESTI_CHECK(RTPathCountComponents("\\\\.\\C$") == 1);
1049 RTTESTI_CHECK(RTPathCountComponents("/\\.\\C$") == 1);
1050 RTTESTI_CHECK(RTPathCountComponents("//myserver") == 1);
1051 RTTESTI_CHECK(RTPathCountComponents("//myserver/") == 1);
1052 RTTESTI_CHECK(RTPathCountComponents("//myserver/share") == 1);
1053 RTTESTI_CHECK(RTPathCountComponents("//myserver/share/") == 1);
1054 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\") == 1);
1055 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x") == 2);
1056 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y") == 3);
1057 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y\\") == 3);
1058#endif
1059
1060 /*
1061 * RTPathCopyComponents
1062 */
1063 struct
1064 {
1065 const char *pszSrc;
1066 size_t cComponents;
1067 const char *pszResult;
1068 } s_aCopyComponents[] =
1069 {
1070 { "", 0, "" },
1071 { "", 5, "" },
1072 { "/", 0, "" },
1073 { "/", 1, "/" },
1074 { "/", 2, "/" },
1075 { "/usr/bin/sed", 0, "" },
1076 { "/usr/bin/sed", 1, "/" },
1077 { "/usr/bin/sed", 2, "/usr/" },
1078 { "/usr/bin/sed", 3, "/usr/bin/" },
1079 { "/usr/bin/sed", 4, "/usr/bin/sed" },
1080 { "/usr/bin/sed", 5, "/usr/bin/sed" },
1081 { "/usr/bin/sed", 6, "/usr/bin/sed" },
1082 { "/usr///bin/sed", 2, "/usr///" },
1083 };
1084 for (unsigned i = 0; i < RT_ELEMENTS(s_aCopyComponents); i++)
1085 {
1086 const char *pszInput = s_aCopyComponents[i].pszSrc;
1087 size_t cComponents = s_aCopyComponents[i].cComponents;
1088 const char *pszResult = s_aCopyComponents[i].pszResult;
1089
1090 memset(szPath, 'a', sizeof(szPath));
1091 rc = RTPathCopyComponents(szPath, sizeof(szPath), pszInput, cComponents);
1092 RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
1093 if (RT_SUCCESS(rc) && strcmp(szPath, pszResult))
1094 RTTestIFailed("Unexpected result\n"
1095 " input: '%s' cComponents=%u\n"
1096 " output: '%s'\n"
1097 "expected: '%s'",
1098 pszInput, cComponents, szPath, pszResult);
1099 else if (RT_SUCCESS(rc))
1100 {
1101 RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult) + 1, pszInput, cComponents), VINF_SUCCESS);
1102 RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult), pszInput, cComponents), VERR_BUFFER_OVERFLOW);
1103 }
1104 }
1105
1106
1107 /*
1108 * RTPathStripSuffix
1109 */
1110 RTTestSub(hTest, "RTPathStripSuffix");
1111 struct
1112 {
1113 const char *pszSrc;
1114 const char *pszResult;
1115 } s_aStripExt[] =
1116 {
1117 { "filename.ext", "filename" },
1118 { "filename.ext1.ext2.ext3", "filename.ext1.ext2" },
1119 { "filename..ext", "filename." },
1120 { "filename.ext.", "filename.ext." },
1121 };
1122 for (unsigned i = 0; i < RT_ELEMENTS(s_aStripExt); i++)
1123 {
1124 const char *pszInput = s_aStripExt[i].pszSrc;
1125 const char *pszResult = s_aStripExt[i].pszResult;
1126
1127 strcpy(szPath, pszInput);
1128 RTPathStripSuffix(szPath);
1129 if (strcmp(szPath, pszResult))
1130 RTTestIFailed("Unexpected result\n"
1131 " input: '%s'\n"
1132 " output: '%s'\n"
1133 "expected: '%s'",
1134 pszInput, szPath, pszResult);
1135 }
1136
1137 /*
1138 * RTPathCalcRelative
1139 */
1140 RTTestSub(hTest, "RTPathCalcRelative");
1141 struct
1142 {
1143 const char *pszFrom;
1144 bool fFromFile;
1145 const char *pszTo;
1146 int rc;
1147 const char *pszExpected;
1148 } s_aRelPath[] =
1149 {
1150 { "/home/test.ext", true, "/home/test2.ext", VINF_SUCCESS, "test2.ext" },
1151 { "/dir/test.ext", true, "/dir/dir2/test2.ext", VINF_SUCCESS, "dir2/test2.ext" },
1152 { "/dir/dir2/test.ext", true, "/dir/test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "test2.ext" },
1153 { "/dir/dir2/test.ext", true, "/dir/dir3/test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "dir3/test2.ext" },
1154 { "/dir/dir2", false, "/dir/dir3/test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "dir3/test2.ext" },
1155 { "/dir/dir2", false, "/dir/dir3//test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "dir3//test2.ext" },
1156 { "/dir/dir2/", false, "/dir/dir3/test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "dir3/test2.ext" },
1157 { "/dir/dir2////", false, "/dir//dir3/test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "dir3/test2.ext" },
1158 { "/include/iprt", false, "/include/iprt/cdefs.h", VINF_SUCCESS, "cdefs.h" },
1159 { "/include/iprt/", false, "/include/iprt/cdefs.h", VINF_SUCCESS, "cdefs.h" },
1160 { "/include/iprt/tt.h", true, "/include/iprt/cdefs.h", VINF_SUCCESS, "cdefs.h" },
1161#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
1162 { "\\\\server\\share\\test.ext", true, "\\\\server\\share2\\test2.ext", VERR_NOT_SUPPORTED, "" },
1163 { "c:\\dir\\test.ext", true, "f:\\dir\\test.ext", VERR_NOT_SUPPORTED, "" },
1164 { "F:\\dir\\test.ext", false, "f:/dir//test.ext", VINF_SUCCESS, "." } ,
1165 { "F:\\diR\\Test.exT", true, "f:/dir//test.ext", VINF_SUCCESS, "Test.exT" } ,
1166 { "F:\\K\xc3\x85RE\\Test.exT", true, "f:/k\xc3\xa5re//test.ext", VINF_SUCCESS, "Test.exT" } ,
1167#endif
1168 };
1169 for (unsigned i = 0; i < RT_ELEMENTS(s_aRelPath); i++)
1170 {
1171 const char *pszFrom = s_aRelPath[i].pszFrom;
1172 bool fFromFile = s_aRelPath[i].fFromFile;
1173 const char *pszTo = s_aRelPath[i].pszTo;
1174
1175 rc = RTPathCalcRelative(szPath, sizeof(szPath), pszFrom, fFromFile, pszTo);
1176 if (rc != s_aRelPath[i].rc)
1177 RTTestIFailed("Unexpected return code for %s .. %s\n"
1178 " got: %Rrc\n"
1179 "expected: %Rrc",
1180 pszFrom, pszTo, rc, s_aRelPath[i].rc);
1181 else if ( RT_SUCCESS(rc)
1182 && strcmp(szPath, s_aRelPath[i].pszExpected))
1183 RTTestIFailed("Unexpected result\n"
1184 " from: '%s' (%s)\n"
1185 " to: '%s'\n"
1186 " output: '%s'\n"
1187 "expected: '%s'",
1188 pszFrom, fFromFile ? "file" : "dir", pszTo, szPath, s_aRelPath[i].pszExpected);
1189 }
1190
1191 testParserAndSplitter(hTest);
1192 testParentLength(hTest);
1193 testPurgeFilename(hTest);
1194 testEnsureTrailingSeparator(hTest);
1195 testFindCommon(hTest);
1196
1197 /*
1198 * Summary.
1199 */
1200 return RTTestSummaryAndDestroy(hTest);
1201}
1202
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