VirtualBox

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

最後變更 在這個檔案從78090是 78090,由 vboxsync 提交於 6 年 前

*,IPRT: Use new RTPathAbsExEx function instead of RTPathAbsEx. bugref:9172

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette