VirtualBox

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

Last change on this file since 78253 was 78153, checked in by vboxsync, 6 years ago

IPRT: Added RTPATHABS_F_ENSURE_TRAILING_SLASH to RTPathAbsEx and fixed a couple of issues related to VERR_BUFFER_OVERFLOW code paths. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 47.5 KB
Line 
1/* $Id: tstRTPath.cpp 78153 2019-04-17 12:30:08Z 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
196int main()
197{
198 char szPath[RTPATH_MAX];
199
200 /*
201 * Init RT+Test.
202 */
203 RTTEST hTest;
204 int rc = RTTestInitAndCreate("tstRTPath", &hTest);
205 if (rc)
206 return rc;
207 RTTestBanner(hTest);
208
209 RTTestSub(hTest, "Environment");
210#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
211 RTTESTI_CHECK(RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS);
212# if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
213# else
214 RTTestIFailed("#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS");
215# endif
216 RTTESTI_CHECK(strcmp(RTPATH_SLASH_STR, "\\") == 0);
217 RTTESTI_CHECK(RTPATH_SLASH == '\\');
218 RTTESTI_CHECK(RTPATH_IS_SEP('/'));
219 RTTESTI_CHECK(RTPATH_IS_SEP('\\'));
220 RTTESTI_CHECK(RTPATH_IS_SEP(':'));
221
222#else
223 RTTESTI_CHECK(RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX);
224# if RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX
225# else
226 RTTestIFailed("#if RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX");
227# endif
228 RTTESTI_CHECK(strcmp(RTPATH_SLASH_STR, "/") == 0);
229 RTTESTI_CHECK(RTPATH_SLASH == '/');
230 RTTESTI_CHECK(RTPATH_IS_SEP('/'));
231 RTTESTI_CHECK(!RTPATH_IS_SEP('\\'));
232 RTTESTI_CHECK(!RTPATH_IS_SEP(':'));
233#endif
234
235 /*
236 * RTPathExecDir, RTPathUserHome and RTProcGetExecutablePath.
237 */
238 RTTestSub(hTest, "RTPathExecDir");
239 RTTESTI_CHECK_RC(rc = RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS);
240 if (RT_SUCCESS(rc))
241 RTTestIPrintf(RTTESTLVL_INFO, "ExecDir={%s}\n", szPath);
242
243 RTTestSub(hTest, "RTProcGetExecutablePath");
244 if (RTProcGetExecutablePath(szPath, sizeof(szPath)) == szPath)
245 RTTestIPrintf(RTTESTLVL_INFO, "ExecutableName={%s}\n", szPath);
246 else
247 RTTestIFailed("RTProcGetExecutablePath -> NULL");
248
249 RTTestSub(hTest, "RTPathUserHome");
250 RTTESTI_CHECK_RC(rc = RTPathUserHome(szPath, sizeof(szPath)), VINF_SUCCESS);
251 if (RT_SUCCESS(rc))
252 RTTestIPrintf(RTTESTLVL_INFO, "UserHome={%s}\n", szPath);
253
254 RTTestSub(hTest, "RTPathUserDocuments");
255 RTTESTI_CHECK_RC(rc = RTPathUserDocuments(szPath, sizeof(szPath)), VINF_SUCCESS);
256 if (RT_SUCCESS(rc))
257 RTTestIPrintf(RTTESTLVL_INFO, "UserDocuments={%s}\n", szPath);
258
259 RTTestSub(hTest, "RTPathTemp");
260 RTTESTI_CHECK_RC(rc = RTPathTemp(szPath, sizeof(szPath)), VINF_SUCCESS);
261 if (RT_SUCCESS(rc))
262 RTTestIPrintf(RTTESTLVL_INFO, "PathTemp={%s}\n", szPath);
263 size_t cch = strlen(szPath);
264 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch), VERR_BUFFER_OVERFLOW);
265 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+1), VINF_SUCCESS);
266 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+2), VINF_SUCCESS);
267
268
269 /*
270 * RTPathAbsEx.
271 */
272 RTTestSub(hTest, "RTPathAbsEx");
273 static const struct
274 {
275 uint32_t fFlags;
276 const char *pcszInputBase;
277 const char *pcszInputPath;
278 int rc;
279 const char *pcszOutput;
280 }
281 s_aRTPathAbsExTests[] =
282 {
283 { RTPATH_STR_F_STYLE_HOST, NULL, "", VERR_PATH_ZERO_LENGTH, NULL },
284 { RTPATH_STR_F_STYLE_HOST, NULL, ".", VINF_SUCCESS, "%p" },
285#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
286 { RTPATH_STR_F_STYLE_DOS, NULL, "\\", VINF_SUCCESS, "%d\\" },
287 { RTPATH_STR_F_STYLE_DOS, NULL, "\\..", VINF_SUCCESS, "%d\\" },
288 { RTPATH_STR_F_STYLE_DOS, NULL, "/absolute/..", VINF_SUCCESS, "%d\\" },
289 { RTPATH_STR_F_STYLE_DOS, NULL, "/absolute\\\\../..", VINF_SUCCESS, "%d\\" },
290 { RTPATH_STR_F_STYLE_DOS, NULL, "/absolute//../path\\", VINF_SUCCESS, "%d\\path\\" },
291 { RTPATH_STR_F_STYLE_DOS, NULL, "/absolute/../../path", VINF_SUCCESS, "%d\\path" },
292 { RTPATH_STR_F_STYLE_DOS, NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p\\dir\\file.txt" },
293 { RTPATH_STR_F_STYLE_DOS, NULL, "\\data\\", VINF_SUCCESS, "%d\\data\\" },
294 { RTPATH_STR_F_STYLE_DOS, "relative_base/dir\\", "\\from_root", VINF_SUCCESS, "%d\\from_root" },
295 { RTPATH_STR_F_STYLE_DOS, "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p\\relative_base\\dir\\relative_also" },
296#else
297 { RTPATH_STR_F_STYLE_UNIX, NULL, ".", VINF_SUCCESS, "%p" },
298 { RTPATH_STR_F_STYLE_UNIX, NULL, "relative/../dir/./././file.txt", VINF_SUCCESS, "%p/dir/file.txt" },
299 { RTPATH_STR_F_STYLE_UNIX, NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p/dir\\.\\.\\.\\file.txt" }, /* linux-specific */
300 { RTPATH_STR_F_STYLE_UNIX, "relative_base/dir/", "/from_root", VINF_SUCCESS, "/from_root" },
301 { RTPATH_STR_F_STYLE_UNIX, "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p/relative_base/dir/relative_also" },
302#endif
303 { RTPATH_STR_F_STYLE_UNIX, NULL, "/", VINF_SUCCESS, "/" },
304 { RTPATH_STR_F_STYLE_UNIX, NULL, "/..", VINF_SUCCESS, "/" },
305 { RTPATH_STR_F_STYLE_UNIX, NULL, "/absolute/..", VINF_SUCCESS, "/" },
306 { RTPATH_STR_F_STYLE_UNIX, NULL, "/absolute\\\\../..", VINF_SUCCESS, "/" },
307 { RTPATH_STR_F_STYLE_UNIX, NULL, "/absolute//../path/", VINF_SUCCESS, "/path/" },
308 { RTPATH_STR_F_STYLE_UNIX, NULL, "/absolute/../../path", VINF_SUCCESS, "/path" },
309 { RTPATH_STR_F_STYLE_UNIX, NULL, "/data/", VINF_SUCCESS, "/data/" },
310#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
311 { RTPATH_STR_F_STYLE_DOS, NULL, "C:\\", VINF_SUCCESS, "C:\\" },
312 { RTPATH_STR_F_STYLE_DOS, "C:\\", "..", VINF_SUCCESS, "C:\\" },
313 { RTPATH_STR_F_STYLE_DOS, "C:\\temp", "..", VINF_SUCCESS, "C:\\" },
314 { RTPATH_STR_F_STYLE_DOS, "C:\\VirtualBox/Machines", "..\\VirtualBox.xml", VINF_SUCCESS, "C:\\VirtualBox\\VirtualBox.xml" },
315 { RTPATH_STR_F_STYLE_DOS, "C:\\MustDie", "\\from_root/dir/..", VINF_SUCCESS, "C:\\from_root" },
316 { RTPATH_STR_F_STYLE_DOS, "C:\\temp", "D:\\data", VINF_SUCCESS, "D:\\data" },
317 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\server\\..\\share", VINF_SUCCESS, "\\\\server\\..\\share" /* kind of strange */ },
318 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\server/", VINF_SUCCESS, "\\\\server\\" },
319 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\", VINF_SUCCESS, "\\\\" },
320 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\\\something", VINF_SUCCESS, "\\\\\\something" /* kind of strange */ },
321 { RTPATH_STR_F_STYLE_DOS, "\\\\server\\share_as_base", "/from_root", VINF_SUCCESS, "\\\\server\\share_as_base\\from_root" },
322 { RTPATH_STR_F_STYLE_DOS, "\\\\just_server", "/from_root", VINF_SUCCESS, "\\\\just_server\\from_root" },
323 { RTPATH_STR_F_STYLE_DOS, "\\\\server\\share_as_base", "relative\\data", VINF_SUCCESS, "\\\\server\\share_as_base\\relative\\data" },
324 { RTPATH_STR_F_STYLE_DOS, "base", "\\\\?\\UNC\\relative/edwef/..", VINF_SUCCESS, "\\\\?\\UNC\\relative" },
325 { RTPATH_STR_F_STYLE_DOS, "\\\\?\\UNC\\base", "/from_root", VINF_SUCCESS, "\\\\?\\from_root" },
326 { RTPATH_STR_F_STYLE_DOS, "\\\\?\\UNC\\base", "./..", VINF_SUCCESS, "\\\\?\\UNC" },
327 { RTPATH_STR_F_STYLE_DOS | RTPATHABS_F_STOP_AT_BASE, "\\\\?\\UNC\\base", "./..", VINF_SUCCESS, "\\\\?\\UNC\\base" },
328 { RTPATH_STR_F_STYLE_DOS | RTPATHABS_F_STOP_AT_BASE, "\\\\?\\UNC\\base", "/..", VINF_SUCCESS, "\\\\?\\" },
329 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\.\\asdf\\..", VINF_SUCCESS, "\\\\.\\" },
330 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\?\\asdf\\..", VINF_SUCCESS, "\\\\?\\" },
331 { RTPATH_STR_F_STYLE_DOS, NULL, "\\\\x\\asdf\\..", VINF_SUCCESS, "\\\\x\\asdf" },
332#else
333 { RTPATH_STR_F_STYLE_UNIX, "\\temp", "\\data", VINF_SUCCESS, "%p/\\temp/\\data" },
334#endif
335 { RTPATH_STR_F_STYLE_UNIX, "/VirtualBox/Machines", "../VirtualBox.xml", VINF_SUCCESS, "/VirtualBox/VirtualBox.xml" },
336 { RTPATH_STR_F_STYLE_UNIX, "/MustDie", "/from_root/dir/..", VINF_SUCCESS, "/from_root" },
337 { RTPATH_STR_F_STYLE_UNIX, "/temp", "..", VINF_SUCCESS, "/" },
338 };
339
340 char *pszGuardedBuf = NULL;
341 rc = RTTestGuardedAlloc(hTest, RTPATH_MAX, 0, false /*fHead*/, (void **)&pszGuardedBuf);
342 if (RT_FAILURE(rc))
343 pszGuardedBuf = szPath;
344
345 for (unsigned i = 0; i < RT_ELEMENTS(s_aRTPathAbsExTests); ++ i)
346 {
347 if (RT_FAILURE(s_aRTPathAbsExTests[i].rc))
348 RTTestDisableAssertions(hTest);
349
350 size_t cbAbsPath = sizeof(szPath);
351 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase,
352 s_aRTPathAbsExTests[i].pcszInputPath,
353 s_aRTPathAbsExTests[i].fFlags,
354 szPath, &cbAbsPath);
355
356 if (RT_FAILURE(s_aRTPathAbsExTests[i].rc))
357 RTTestRestoreAssertions(hTest);
358
359 if (rc != s_aRTPathAbsExTests[i].rc)
360 {
361 RTTestIFailed("#%u: unexpected result code!\n"
362 " flags: %#x\n"
363 " input base: '%s'\n"
364 " input path: '%s'\n"
365 " output: '%s'\n"
366 " rc: %Rrc\n"
367 " expected rc: %Rrc",
368 i,
369 s_aRTPathAbsExTests[i].fFlags,
370 s_aRTPathAbsExTests[i].pcszInputBase,
371 s_aRTPathAbsExTests[i].pcszInputPath,
372 szPath, rc,
373 s_aRTPathAbsExTests[i].rc);
374 continue;
375 }
376
377 char szTmp[RTPATH_MAX];
378 char *pszExpected = NULL;
379 if (s_aRTPathAbsExTests[i].pcszOutput != NULL)
380 {
381 if (s_aRTPathAbsExTests[i].pcszOutput[0] == '%')
382 {
383 RTTESTI_CHECK_RC(rc = RTPathGetCurrent(szTmp, sizeof(szTmp)), VINF_SUCCESS);
384 if (RT_FAILURE(rc))
385 break;
386
387 pszExpected = szTmp;
388
389 if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'p')
390 {
391 cch = strlen(szTmp);
392 if (cch + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
393 strcpy(szTmp + cch, s_aRTPathAbsExTests[i].pcszOutput + 2);
394 }
395#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
396 else if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'd')
397 {
398 if (2 + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
399 strcpy(szTmp + 2, s_aRTPathAbsExTests[i].pcszOutput + 2);
400 }
401#endif
402 }
403 else
404 {
405 strcpy(szTmp, s_aRTPathAbsExTests[i].pcszOutput);
406 pszExpected = szTmp;
407 }
408
409 if ( strcmp(szPath, pszExpected)
410 || strlen(szPath) != cbAbsPath)
411 {
412 RTTestIFailed("#%u: Unexpected result\n"
413 " flags: %#x\n"
414 " input base: '%s'\n"
415 " input path: '%s'\n"
416 " output: '%s'\n"
417 " expected: '%s' ('%s')\n"
418 " cchResult: %#x, actual %#x",
419 i,
420 s_aRTPathAbsExTests[i].fFlags,
421 s_aRTPathAbsExTests[i].pcszInputBase,
422 s_aRTPathAbsExTests[i].pcszInputPath,
423 szPath,
424 pszExpected, s_aRTPathAbsExTests[i].pcszOutput,
425 cbAbsPath, strlen(szPath));
426 continue;
427 }
428
429 if (RT_SUCCESS(s_aRTPathAbsExTests[i].rc))
430 {
431 /* Test the RTPATHABS_F_ENSURE_TRAILING_SLASH flag: */
432 cbAbsPath = sizeof(szPath);
433 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase,
434 s_aRTPathAbsExTests[i].pcszInputPath,
435 s_aRTPathAbsExTests[i].fFlags | RTPATHABS_F_ENSURE_TRAILING_SLASH,
436 szPath, &cbAbsPath);
437 char chSlash = (s_aRTPathAbsExTests[i].fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STR_F_STYLE_DOS ? '\\'
438 : (s_aRTPathAbsExTests[i].fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STR_F_STYLE_UNIX ? '/'
439 : RTPATH_SLASH;
440 if ( RT_FAILURE(rc)
441 || strlen(szPath) != cbAbsPath
442 || szPath[cbAbsPath - 1] != chSlash)
443 RTTestIFailed("#%u: Unexpected RTPATHABS_F_ENSURE_TRAILING_SLASH result: %Rrc\n"
444 " flags: %#x | RTPATHABS_F_ENSURE_TRAILING_SLASH\n"
445 " input base: '%s'\n"
446 " input path: '%s'\n"
447 " output: '%s' ('%c' vs '%c')\n"
448 " cchResult: %#x, actual %#x",
449 i, rc,
450 s_aRTPathAbsExTests[i].fFlags,
451 s_aRTPathAbsExTests[i].pcszInputBase,
452 s_aRTPathAbsExTests[i].pcszInputPath,
453 szPath, szPath[cbAbsPath - 1], chSlash,
454 cbAbsPath, strlen(szPath));
455
456 /* Do overflow testing: */
457 size_t const cbNeeded = strlen(pszExpected) + 1;
458 for (size_t cbBuf = 0; cbBuf < cbNeeded + 64; cbBuf++)
459 {
460 char *pszBuf = &pszGuardedBuf[RTPATH_MAX - cbBuf];
461 memset(pszBuf, 0x33, cbBuf);
462 cbAbsPath = cbBuf;
463 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase, s_aRTPathAbsExTests[i].pcszInputPath,
464 s_aRTPathAbsExTests[i].fFlags, pszBuf, &cbAbsPath);
465 if ( cbBuf < cbNeeded
466 && ( rc != VERR_BUFFER_OVERFLOW
467 || cbAbsPath < cbNeeded))
468 RTTestIFailed("#%u: Unexpected overflow result: %Rrc%s\n"
469 " flags: %#x\n"
470 " input base: '%s'\n"
471 " input path: '%s'\n"
472 " cbBuf[in]: %#x\n"
473 " cbBuf[out]: %#x\n"
474 " cbNeeded: %#x\n",
475 i, rc, rc != VERR_BUFFER_OVERFLOW ? " - expected VERR_BUFFER_OVERFLOW" : "",
476 s_aRTPathAbsExTests[i].fFlags,
477 s_aRTPathAbsExTests[i].pcszInputBase,
478 s_aRTPathAbsExTests[i].pcszInputPath,
479 cbBuf,
480 cbAbsPath,
481 cbNeeded);
482 else if ( cbBuf >= cbNeeded
483 && ( rc != s_aRTPathAbsExTests[i].rc
484 || cbAbsPath != cbNeeded - 1
485 || strcmp(pszBuf, pszExpected)
486 || strlen(pszBuf) != cbAbsPath))
487 RTTestIFailed("#%u: Unexpected result: %Rrc (expected %Rrc)\n"
488 " flags: %#x\n"
489 " input base: '%s'\n"
490 " input path: '%s'\n"
491 " cbBuf[in]: %#x\n"
492 " cbBuf[out]: %#x\n"
493 " cbNeeded: %#x\n",
494 i, rc, s_aRTPathAbsExTests[i].rc,
495 s_aRTPathAbsExTests[i].fFlags,
496 s_aRTPathAbsExTests[i].pcszInputBase,
497 s_aRTPathAbsExTests[i].pcszInputPath,
498 cbBuf,
499 cbAbsPath,
500 cbNeeded);
501
502 }
503 }
504
505 /* RTPathAbsExDup */
506 char *pszDup = RTPathAbsExDup(s_aRTPathAbsExTests[i].pcszInputBase,
507 s_aRTPathAbsExTests[i].pcszInputPath,
508 s_aRTPathAbsExTests[i].fFlags);
509 if ( (RT_SUCCESS(s_aRTPathAbsExTests[i].rc) ? pszDup == NULL : pszDup != NULL)
510 || RTStrCmp(pszDup, pszExpected))
511 RTTestIFailed("#%u: Unexpected RTPathAbsExDup result: %p%s\n"
512 " flags: %#x\n"
513 " input base: '%s'\n"
514 " input path: '%s'\n"
515 " output: '%s'\n"
516 " expected: '%s' ('%s')\n",
517 i, pszDup,
518 (RT_SUCCESS(s_aRTPathAbsExTests[i].rc) ? pszDup == NULL : pszDup != NULL) ? pszDup ? "NULL" : "!NULL" : "",
519 s_aRTPathAbsExTests[i].fFlags,
520 s_aRTPathAbsExTests[i].pcszInputBase,
521 s_aRTPathAbsExTests[i].pcszInputPath,
522 pszDup,
523 pszExpected, s_aRTPathAbsExTests[i].pcszOutput);
524 RTStrFree(pszDup);
525 }
526 }
527
528 if (pszGuardedBuf != szPath)
529 RTTestGuardedFree(hTest, pszGuardedBuf);
530
531
532 /*
533 * RTPathStripFilename
534 */
535 RTTestSub(hTest, "RTPathStripFilename");
536 static const char *s_apszStripFilenameTests[] =
537 {
538 "/usr/include///", "/usr/include//",
539 "/usr/include/", "/usr/include",
540 "/usr/include", "/usr",
541 "/usr", "/",
542 "usr", ".",
543#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
544 "c:/windows", "c:/",
545 "c:/", "c:/",
546 "D:", "D:",
547 "C:\\OS2\\DLLS", "C:\\OS2",
548#endif
549 };
550 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripFilenameTests); i += 2)
551 {
552 const char *pszInput = s_apszStripFilenameTests[i];
553 const char *pszExpect = s_apszStripFilenameTests[i + 1];
554 strcpy(szPath, pszInput);
555 RTPathStripFilename(szPath);
556 if (strcmp(szPath, pszExpect))
557 {
558 RTTestIFailed("Unexpected result\n"
559 " input: '%s'\n"
560 " output: '%s'\n"
561 "expected: '%s'",
562 pszInput, szPath, pszExpect);
563 }
564 }
565
566 /*
567 * RTPathAppend.
568 */
569 RTTestSub(hTest, "RTPathAppend");
570 static const char *s_apszAppendTests[] =
571 {
572 /* base append result */
573 "/", "", "/",
574 "", "/", "/",
575 "/", "/", "/",
576 "/x", "", "/x",
577 "/x", "/", "/x/",
578 "/", "x", "/x",
579 "dir", "file", "dir" RTPATH_SLASH_STR "file",
580 "dir", "/file", "dir/file",
581 "dir", "//file", "dir/file",
582 "dir", "///file", "dir/file",
583 "dir/", "/file", "dir/file",
584 "dir/", "//file", "dir/file",
585 "dir/", "///file", "dir/file",
586 "dir//", "file", "dir/file",
587 "dir//", "/file", "dir/file",
588 "dir//", "//file", "dir/file",
589 "dir///", "///file", "dir/file",
590 "/bin/testcase", "foo.r0", "/bin/testcase" RTPATH_SLASH_STR "foo.r0",
591#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
592 "/", "\\", "/",
593 "\\", "/", "\\",
594 "\\\\srv\\shr", "dir//", "\\\\srv\\shr" RTPATH_SLASH_STR "dir//",
595 "\\\\srv\\shr", "dir//file", "\\\\srv\\shr" RTPATH_SLASH_STR "dir//file",
596 "\\\\srv\\shr", "//dir//", "\\\\srv\\shr/dir//",
597 "\\\\srv\\shr", "/\\dir//", "\\\\srv\\shr\\dir//",
598 "\\\\", "not-srv/not-shr/file", "\\not-srv/not-shr/file",
599 "C:", "autoexec.bat", "C:autoexec.bat",
600 "C:", "/autoexec.bat", "C:/autoexec.bat",
601 "C:", "\\autoexec.bat", "C:\\autoexec.bat",
602 "C:\\", "/autoexec.bat", "C:\\autoexec.bat",
603 "C:\\\\", "autoexec.bat", "C:\\autoexec.bat",
604 "E:\\bin\\testcase", "foo.r0", "E:\\bin\\testcase" RTPATH_SLASH_STR "foo.r0",
605#endif
606 };
607 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
608 {
609 const char *pszInput = s_apszAppendTests[i];
610 const char *pszAppend = s_apszAppendTests[i + 1];
611 const char *pszExpect = s_apszAppendTests[i + 2];
612 strcpy(szPath, pszInput);
613 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, sizeof(szPath), pszAppend), VINF_SUCCESS);
614 if (RT_FAILURE(rc))
615 continue;
616 if (strcmp(szPath, pszExpect))
617 {
618 RTTestIFailed("Unexpected result\n"
619 " input: '%s'\n"
620 " append: '%s'\n"
621 " output: '%s'\n"
622 "expected: '%s'",
623 pszInput, pszAppend, szPath, pszExpect);
624 }
625 else
626 {
627 size_t const cchResult = strlen(szPath);
628
629 strcpy(szPath, pszInput);
630 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 2, pszAppend), VINF_SUCCESS);
631 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
632
633 strcpy(szPath, pszInput);
634 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 1, pszAppend), VINF_SUCCESS);
635 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
636
637 if (strlen(pszInput) < cchResult)
638 {
639 strcpy(szPath, pszInput);
640 RTTESTI_CHECK_RC(RTPathAppend(szPath, cchResult, pszAppend), VERR_BUFFER_OVERFLOW);
641 }
642 }
643 }
644
645 /*
646 * RTPathJoin - reuse the append tests.
647 */
648 RTTestSub(hTest, "RTPathJoin");
649 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
650 {
651 const char *pszInput = s_apszAppendTests[i];
652 const char *pszAppend = s_apszAppendTests[i + 1];
653 const char *pszExpect = s_apszAppendTests[i + 2];
654
655 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
656
657 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, sizeof(szPath), pszInput, pszAppend), VINF_SUCCESS);
658 if (RT_FAILURE(rc))
659 continue;
660 if (strcmp(szPath, pszExpect))
661 {
662 RTTestIFailed("Unexpected result\n"
663 " input: '%s'\n"
664 " append: '%s'\n"
665 " output: '%s'\n"
666 "expected: '%s'",
667 pszInput, pszAppend, szPath, pszExpect);
668 }
669 else
670 {
671 size_t const cchResult = strlen(szPath);
672
673 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
674 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult + 2, pszInput, pszAppend), VINF_SUCCESS);
675 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
676
677 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
678 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult + 1, pszInput, pszAppend), VINF_SUCCESS);
679 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
680
681 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult, pszInput, pszAppend), VERR_BUFFER_OVERFLOW);
682 }
683 }
684
685 /*
686 * RTPathJoinA - reuse the append tests.
687 */
688 RTTestSub(hTest, "RTPathJoinA");
689 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
690 {
691 const char *pszInput = s_apszAppendTests[i];
692 const char *pszAppend = s_apszAppendTests[i + 1];
693 const char *pszExpect = s_apszAppendTests[i + 2];
694
695 char *pszPathDst;
696 RTTESTI_CHECK(pszPathDst = RTPathJoinA(pszInput, pszAppend));
697 if (!pszPathDst)
698 continue;
699 if (strcmp(pszPathDst, pszExpect))
700 {
701 RTTestIFailed("Unexpected result\n"
702 " input: '%s'\n"
703 " append: '%s'\n"
704 " output: '%s'\n"
705 "expected: '%s'",
706 pszInput, pszAppend, pszPathDst, pszExpect);
707 }
708 RTStrFree(pszPathDst);
709 }
710
711 /*
712 * RTPathStripTrailingSlash
713 */
714 static const char *s_apszStripTrailingSlash[] =
715 {
716 /* input result */
717 "/", "/",
718 "//", "/",
719 "////////////////////", "/",
720 "/tmp", "/tmp",
721 "/tmp////////////////", "/tmp",
722 "tmp", "tmp",
723 "tmp////////////////", "tmp",
724 "./", ".",
725#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
726 "////////////////////", "/",
727 "D:", "D:",
728 "D:/", "D:/",
729 "D:\\", "D:\\",
730 "D:\\/\\", "D:\\",
731 "D:/\\/\\", "D:/",
732 "C:/Temp", "C:/Temp",
733 "C:/Temp/", "C:/Temp",
734 "C:/Temp\\/", "C:/Temp",
735#endif
736 };
737 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripTrailingSlash); i += 2)
738 {
739 const char *pszInput = s_apszStripTrailingSlash[i];
740 const char *pszExpect = s_apszStripTrailingSlash[i + 1];
741
742 strcpy(szPath, pszInput);
743 cch = RTPathStripTrailingSlash(szPath);
744 if (strcmp(szPath, pszExpect))
745 RTTestIFailed("Unexpected result\n"
746 " input: '%s'\n"
747 " output: '%s'\n"
748 "expected: '%s'",
749 pszInput, szPath, pszExpect);
750 else
751 RTTESTI_CHECK(cch == strlen(szPath));
752 }
753
754 /*
755 * RTPathCountComponents
756 */
757 RTTestSub(hTest, "RTPathCountComponents");
758 RTTESTI_CHECK(RTPathCountComponents("") == 0);
759 RTTESTI_CHECK(RTPathCountComponents("/") == 1);
760 RTTESTI_CHECK(RTPathCountComponents("//") == 1);
761 RTTESTI_CHECK(RTPathCountComponents("//////////////") == 1);
762 RTTESTI_CHECK(RTPathCountComponents("//////////////bin") == 2);
763 RTTESTI_CHECK(RTPathCountComponents("//////////////bin/") == 2);
764 RTTESTI_CHECK(RTPathCountComponents("//////////////bin/////") == 2);
765 RTTESTI_CHECK(RTPathCountComponents("..") == 1);
766 RTTESTI_CHECK(RTPathCountComponents("../") == 1);
767 RTTESTI_CHECK(RTPathCountComponents("../..") == 2);
768 RTTESTI_CHECK(RTPathCountComponents("../../") == 2);
769#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
770 RTTESTI_CHECK(RTPathCountComponents("d:") == 1);
771 RTTESTI_CHECK(RTPathCountComponents("d:/") == 1);
772 RTTESTI_CHECK(RTPathCountComponents("d:/\\") == 1);
773 RTTESTI_CHECK(RTPathCountComponents("d:\\") == 1);
774 RTTESTI_CHECK(RTPathCountComponents("c:\\config.sys") == 2);
775 RTTESTI_CHECK(RTPathCountComponents("c:\\windows") == 2);
776 RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\") == 2);
777 RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\system32") == 3);
778 RTTESTI_CHECK(RTPathCountComponents("//./C$") == 1);
779 RTTESTI_CHECK(RTPathCountComponents("\\\\.\\C$") == 1);
780 RTTESTI_CHECK(RTPathCountComponents("/\\.\\C$") == 1);
781 RTTESTI_CHECK(RTPathCountComponents("//myserver") == 1);
782 RTTESTI_CHECK(RTPathCountComponents("//myserver/") == 1);
783 RTTESTI_CHECK(RTPathCountComponents("//myserver/share") == 1);
784 RTTESTI_CHECK(RTPathCountComponents("//myserver/share/") == 1);
785 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\") == 1);
786 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x") == 2);
787 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y") == 3);
788 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y\\") == 3);
789#endif
790
791 /*
792 * RTPathCopyComponents
793 */
794 struct
795 {
796 const char *pszSrc;
797 size_t cComponents;
798 const char *pszResult;
799 } s_aCopyComponents[] =
800 {
801 { "", 0, "" },
802 { "", 5, "" },
803 { "/", 0, "" },
804 { "/", 1, "/" },
805 { "/", 2, "/" },
806 { "/usr/bin/sed", 0, "" },
807 { "/usr/bin/sed", 1, "/" },
808 { "/usr/bin/sed", 2, "/usr/" },
809 { "/usr/bin/sed", 3, "/usr/bin/" },
810 { "/usr/bin/sed", 4, "/usr/bin/sed" },
811 { "/usr/bin/sed", 5, "/usr/bin/sed" },
812 { "/usr/bin/sed", 6, "/usr/bin/sed" },
813 { "/usr///bin/sed", 2, "/usr///" },
814 };
815 for (unsigned i = 0; i < RT_ELEMENTS(s_aCopyComponents); i++)
816 {
817 const char *pszInput = s_aCopyComponents[i].pszSrc;
818 size_t cComponents = s_aCopyComponents[i].cComponents;
819 const char *pszResult = s_aCopyComponents[i].pszResult;
820
821 memset(szPath, 'a', sizeof(szPath));
822 rc = RTPathCopyComponents(szPath, sizeof(szPath), pszInput, cComponents);
823 RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
824 if (RT_SUCCESS(rc) && strcmp(szPath, pszResult))
825 RTTestIFailed("Unexpected result\n"
826 " input: '%s' cComponents=%u\n"
827 " output: '%s'\n"
828 "expected: '%s'",
829 pszInput, cComponents, szPath, pszResult);
830 else if (RT_SUCCESS(rc))
831 {
832 RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult) + 1, pszInput, cComponents), VINF_SUCCESS);
833 RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult), pszInput, cComponents), VERR_BUFFER_OVERFLOW);
834 }
835 }
836
837
838 /*
839 * RTPathStripSuffix
840 */
841 RTTestSub(hTest, "RTPathStripSuffix");
842 struct
843 {
844 const char *pszSrc;
845 const char *pszResult;
846 } s_aStripExt[] =
847 {
848 { "filename.ext", "filename" },
849 { "filename.ext1.ext2.ext3", "filename.ext1.ext2" },
850 { "filename..ext", "filename." },
851 { "filename.ext.", "filename.ext." },
852 };
853 for (unsigned i = 0; i < RT_ELEMENTS(s_aStripExt); i++)
854 {
855 const char *pszInput = s_aStripExt[i].pszSrc;
856 const char *pszResult = s_aStripExt[i].pszResult;
857
858 strcpy(szPath, pszInput);
859 RTPathStripSuffix(szPath);
860 if (strcmp(szPath, pszResult))
861 RTTestIFailed("Unexpected result\n"
862 " input: '%s'\n"
863 " output: '%s'\n"
864 "expected: '%s'",
865 pszInput, szPath, pszResult);
866 }
867
868 /*
869 * RTPathCalcRelative
870 */
871 RTTestSub(hTest, "RTPathCalcRelative");
872 struct
873 {
874 const char *pszFrom;
875 bool fFromFile;
876 const char *pszTo;
877 int rc;
878 const char *pszExpected;
879 } s_aRelPath[] =
880 {
881 { "/home/test.ext", true, "/home/test2.ext", VINF_SUCCESS, "test2.ext" },
882 { "/dir/test.ext", true, "/dir/dir2/test2.ext", VINF_SUCCESS, "dir2/test2.ext" },
883 { "/dir/dir2/test.ext", true, "/dir/test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "test2.ext" },
884 { "/dir/dir2/test.ext", true, "/dir/dir3/test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "dir3/test2.ext" },
885 { "/dir/dir2", false, "/dir/dir3/test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "dir3/test2.ext" },
886 { "/dir/dir2", false, "/dir/dir3//test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "dir3//test2.ext" },
887 { "/dir/dir2/", false, "/dir/dir3/test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "dir3/test2.ext" },
888 { "/dir/dir2////", false, "/dir//dir3/test2.ext", VINF_SUCCESS, ".." RTPATH_SLASH_STR "dir3/test2.ext" },
889 { "/include/iprt", false, "/include/iprt/cdefs.h", VINF_SUCCESS, "cdefs.h" },
890 { "/include/iprt/", false, "/include/iprt/cdefs.h", VINF_SUCCESS, "cdefs.h" },
891 { "/include/iprt/tt.h", true, "/include/iprt/cdefs.h", VINF_SUCCESS, "cdefs.h" },
892#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
893 { "\\\\server\\share\\test.ext", true, "\\\\server\\share2\\test2.ext", VERR_NOT_SUPPORTED, "" },
894 { "c:\\dir\\test.ext", true, "f:\\dir\\test.ext", VERR_NOT_SUPPORTED, "" },
895 { "F:\\dir\\test.ext", false, "f:/dir//test.ext", VINF_SUCCESS, "." } ,
896 { "F:\\diR\\Test.exT", true, "f:/dir//test.ext", VINF_SUCCESS, "Test.exT" } ,
897 { "F:\\K\xc3\x85RE\\Test.exT", true, "f:/k\xc3\xa5re//test.ext", VINF_SUCCESS, "Test.exT" } ,
898#endif
899 };
900 for (unsigned i = 0; i < RT_ELEMENTS(s_aRelPath); i++)
901 {
902 const char *pszFrom = s_aRelPath[i].pszFrom;
903 bool fFromFile = s_aRelPath[i].fFromFile;
904 const char *pszTo = s_aRelPath[i].pszTo;
905
906 rc = RTPathCalcRelative(szPath, sizeof(szPath), pszFrom, fFromFile, pszTo);
907 if (rc != s_aRelPath[i].rc)
908 RTTestIFailed("Unexpected return code for %s .. %s\n"
909 " got: %Rrc\n"
910 "expected: %Rrc",
911 pszFrom, pszTo, rc, s_aRelPath[i].rc);
912 else if ( RT_SUCCESS(rc)
913 && strcmp(szPath, s_aRelPath[i].pszExpected))
914 RTTestIFailed("Unexpected result\n"
915 " from: '%s' (%s)\n"
916 " to: '%s'\n"
917 " output: '%s'\n"
918 "expected: '%s'",
919 pszFrom, fFromFile ? "file" : "dir", pszTo, szPath, s_aRelPath[i].pszExpected);
920 }
921
922 testParserAndSplitter(hTest);
923
924 /*
925 * Summary.
926 */
927 return RTTestSummaryAndDestroy(hTest);
928}
929
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