VirtualBox

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

Last change on this file since 78750 was 78702, checked in by vboxsync, 6 years ago

IPRT/path: Added RTPathParentLength and RTPathParentLengthEx. bugref:9172

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