VirtualBox

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

Last change on this file since 45391 was 45391, checked in by vboxsync, 12 years ago

IPRT: Changed the RTPATHPARSE_FLAGS_ into generic ones as they will be shared with RTPathSplit*. Introduced a new character called RTPATH_STYLE with can have two values (currently) RTPATH_STR_F_STYLE_DOS or RTPATH_STR_F_STYLE_UNIX. Some other minor adjustments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.1 KB
Line 
1/* $Id: tstRTPath.cpp 45391 2013-04-07 17:15:29Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Test various path functions.
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <iprt/path.h>
31
32#include <iprt/err.h>
33#include <iprt/initterm.h>
34#include <iprt/param.h>
35#include <iprt/process.h>
36#include <iprt/stream.h>
37#include <iprt/string.h>
38#include <iprt/test.h>
39
40
41static void testParser(RTTEST hTest)
42{
43 RTTestSub(hTest, "RTPathParse");
44
45 static struct
46 {
47 uint16_t cComps;
48 uint16_t cchPath;
49 uint16_t offSuffix;
50 const char *pszPath;
51 uint16_t fProps;
52 uint32_t fFlags;
53 } const s_aTests[] =
54 {
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_ROOT_SLASH | RTPATH_PROP_ABSOLUTE, RTPATH_STR_F_STYLE_DOS },
60 { 2, 5, 5, "//./f", RTPATH_PROP_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_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_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, 4, 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, 3, 4, "bin/", RTPATH_PROP_RELATIVE | RTPATH_PROP_DIR_SLASH, RTPATH_STR_F_STYLE_UNIX },
75 { 1, 3, 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 union
91 {
92 RTPATHPARSED Parsed;
93 uint8_t ab[4096];
94 } u;
95
96 for (uint32_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
97 {
98 int rc = RTPathParse(s_aTests[i].pszPath, &u.Parsed, sizeof(u), s_aTests[i].fFlags);
99 if ( rc != VINF_SUCCESS
100 || s_aTests[i].cComps != u.Parsed.cComps
101 || s_aTests[i].fProps != u.Parsed.fProps
102 || s_aTests[i].offSuffix != u.Parsed.offSuffix
103 || s_aTests[i].cchPath != u.Parsed.cchPath)
104 {
105 RTTestFailed(hTest, "i=%d rc=%Rrc %s", i, rc, s_aTests[i].pszPath);
106 RTTestFailureDetails(hTest,
107 " cComps %u, got %u\n"
108 " fProps %#x, got %#x, xor=>%#x\n"
109 " offSuffix %u, got %u\n"
110 " cchPath %u, got %u\n"
111 ,
112 s_aTests[i].cComps, u.Parsed.cComps,
113 s_aTests[i].fProps, u.Parsed.fProps, s_aTests[i].fProps ^ u.Parsed.fProps,
114 s_aTests[i].offSuffix, u.Parsed.offSuffix,
115 s_aTests[i].cchPath, u.Parsed.cchPath);
116 }
117 }
118}
119
120
121int main()
122{
123 char szPath[RTPATH_MAX];
124
125 /*
126 * Init RT+Test.
127 */
128 RTTEST hTest;
129 int rc = RTTestInitAndCreate("tstRTPath", &hTest);
130 if (rc)
131 return rc;
132 RTTestBanner(hTest);
133
134 RTTestSub(hTest, "Environment");
135#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
136 RTTESTI_CHECK(RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS);
137# if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
138# else
139 RTTestIFailed("#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS");
140# endif
141 RTTESTI_CHECK(strcmp(RTPATH_SLASH_STR, "\\") == 0);
142 RTTESTI_CHECK(RTPATH_SLASH == '\\');
143 RTTESTI_CHECK(RTPATH_IS_SEP('/'));
144 RTTESTI_CHECK(RTPATH_IS_SEP('\\'));
145 RTTESTI_CHECK(RTPATH_IS_SEP(':'));
146
147#else
148 RTTESTI_CHECK(RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX);
149# if RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX
150# else
151 RTTestIFailed("#if RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX");
152# endif
153 RTTESTI_CHECK(strcmp(RTPATH_SLASH_STR, "/") == 0);
154 RTTESTI_CHECK(RTPATH_SLASH == '/');
155 RTTESTI_CHECK(RTPATH_IS_SEP('/'));
156 RTTESTI_CHECK(!RTPATH_IS_SEP('\\'));
157 RTTESTI_CHECK(!RTPATH_IS_SEP(':'));
158#endif
159
160 /*
161 * RTPathExecDir, RTPathUserHome and RTProcGetExecutablePath.
162 */
163 RTTestSub(hTest, "RTPathExecDir");
164 RTTESTI_CHECK_RC(rc = RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS);
165 if (RT_SUCCESS(rc))
166 RTTestIPrintf(RTTESTLVL_INFO, "ExecDir={%s}\n", szPath);
167
168 RTTestSub(hTest, "RTProcGetExecutablePath");
169 if (RTProcGetExecutablePath(szPath, sizeof(szPath)) == szPath)
170 RTTestIPrintf(RTTESTLVL_INFO, "ExecutableName={%s}\n", szPath);
171 else
172 RTTestIFailed("RTProcGetExecutablePath -> NULL");
173
174 RTTestSub(hTest, "RTPathUserHome");
175 RTTESTI_CHECK_RC(rc = RTPathUserHome(szPath, sizeof(szPath)), VINF_SUCCESS);
176 if (RT_SUCCESS(rc))
177 RTTestIPrintf(RTTESTLVL_INFO, "UserHome={%s}\n", szPath);
178
179 RTTestSub(hTest, "RTPathUserDocuments");
180 RTTESTI_CHECK_RC(rc = RTPathUserDocuments(szPath, sizeof(szPath)), VINF_SUCCESS);
181 if (RT_SUCCESS(rc))
182 RTTestIPrintf(RTTESTLVL_INFO, "UserDocuments={%s}\n", szPath);
183
184 RTTestSub(hTest, "RTPathTemp");
185 RTTESTI_CHECK_RC(rc = RTPathTemp(szPath, sizeof(szPath)), VINF_SUCCESS);
186 if (RT_SUCCESS(rc))
187 RTTestIPrintf(RTTESTLVL_INFO, "PathTemp={%s}\n", szPath);
188 size_t cch = strlen(szPath);
189 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch), VERR_BUFFER_OVERFLOW);
190 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+1), VINF_SUCCESS);
191 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+2), VINF_SUCCESS);
192
193
194 /*
195 * RTPathAbsEx
196 */
197 RTTestSub(hTest, "RTPathAbsEx");
198 static const struct
199 {
200 const char *pcszInputBase;
201 const char *pcszInputPath;
202 int rc;
203 const char *pcszOutput;
204 }
205 s_aRTPathAbsExTests[] =
206 {
207#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
208 { NULL, "", VERR_INVALID_PARAMETER, NULL },
209 { NULL, ".", VINF_SUCCESS, "%p" },
210 { NULL, "\\", VINF_SUCCESS, "%d\\" },
211 { NULL, "\\..", VINF_SUCCESS, "%d\\" },
212 { NULL, "/absolute/..", VINF_SUCCESS, "%d\\" },
213 { NULL, "/absolute\\\\../..", VINF_SUCCESS, "%d\\" },
214 { NULL, "/absolute//../path\\", VINF_SUCCESS, "%d\\path" },
215 { NULL, "/absolute/../../path", VINF_SUCCESS, "%d\\path" },
216 { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p\\dir\\file.txt" },
217 { NULL, "\\data\\", VINF_SUCCESS, "%d\\data" },
218 { "relative_base/dir\\", "\\from_root", VINF_SUCCESS, "%d\\from_root" },
219 { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p\\relative_base\\dir\\relative_also" },
220#else
221 { NULL, "", VERR_INVALID_PARAMETER, NULL },
222 { NULL, ".", VINF_SUCCESS, "%p" },
223 { NULL, "/", VINF_SUCCESS, "/" },
224 { NULL, "/..", VINF_SUCCESS, "/" },
225 { NULL, "/absolute/..", VINF_SUCCESS, "/" },
226 { NULL, "/absolute\\\\../..", VINF_SUCCESS, "/" },
227 { NULL, "/absolute//../path/", VINF_SUCCESS, "/path" },
228 { NULL, "/absolute/../../path", VINF_SUCCESS, "/path" },
229 { NULL, "relative/../dir/./././file.txt", VINF_SUCCESS, "%p/dir/file.txt" },
230 { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p/dir\\.\\.\\.\\file.txt" }, /* linux-specific */
231 { NULL, "/data/", VINF_SUCCESS, "/data" },
232 { "relative_base/dir/", "/from_root", VINF_SUCCESS, "/from_root" },
233 { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p/relative_base/dir/relative_also" },
234#endif
235#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
236 { NULL, "C:\\", VINF_SUCCESS, "C:\\" },
237 { "C:\\", "..", VINF_SUCCESS, "C:\\" },
238 { "C:\\temp", "..", VINF_SUCCESS, "C:\\" },
239 { "C:\\VirtualBox/Machines", "..\\VirtualBox.xml", VINF_SUCCESS, "C:\\VirtualBox\\VirtualBox.xml" },
240 { "C:\\MustDie", "\\from_root/dir/..", VINF_SUCCESS, "C:\\from_root" },
241 { "C:\\temp", "D:\\data", VINF_SUCCESS, "D:\\data" },
242 { NULL, "\\\\server\\..\\share", VINF_SUCCESS, "\\\\server\\..\\share" /* kind of strange */ },
243 { NULL, "\\\\server/", VINF_SUCCESS, "\\\\server" },
244 { NULL, "\\\\", VINF_SUCCESS, "\\\\" },
245 { NULL, "\\\\\\something", VINF_SUCCESS, "\\\\\\something" /* kind of strange */ },
246 { "\\\\server\\share_as_base", "/from_root", VINF_SUCCESS, "\\\\server\\from_root" },
247 { "\\\\just_server", "/from_root", VINF_SUCCESS, "\\\\just_server\\from_root" },
248 { "\\\\server\\share_as_base", "relative\\data", VINF_SUCCESS, "\\\\server\\share_as_base\\relative\\data" },
249 { "base", "\\\\?\\UNC\\relative/edwef/..", VINF_SUCCESS, "\\\\?\\UNC\\relative" },
250 { "\\\\?\\UNC\\base", "/from_root", VERR_INVALID_NAME, NULL },
251#else
252 { "/temp", "..", VINF_SUCCESS, "/" },
253 { "/VirtualBox/Machines", "../VirtualBox.xml", VINF_SUCCESS, "/VirtualBox/VirtualBox.xml" },
254 { "/MustDie", "/from_root/dir/..", VINF_SUCCESS, "/from_root" },
255 { "\\temp", "\\data", VINF_SUCCESS, "%p/\\temp/\\data" },
256#endif
257 };
258
259 for (unsigned i = 0; i < RT_ELEMENTS(s_aRTPathAbsExTests); ++ i)
260 {
261 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase,
262 s_aRTPathAbsExTests[i].pcszInputPath,
263 szPath, sizeof(szPath));
264 if (rc != s_aRTPathAbsExTests[i].rc)
265 {
266 RTTestIFailed("unexpected result code!\n"
267 " input base: '%s'\n"
268 " input path: '%s'\n"
269 " output: '%s'\n"
270 " rc: %Rrc\n"
271 " expected rc: %Rrc",
272 s_aRTPathAbsExTests[i].pcszInputBase,
273 s_aRTPathAbsExTests[i].pcszInputPath,
274 szPath, rc,
275 s_aRTPathAbsExTests[i].rc);
276 continue;
277 }
278
279 char szTmp[RTPATH_MAX];
280 char *pszExpected = NULL;
281 if (s_aRTPathAbsExTests[i].pcszOutput != NULL)
282 {
283 if (s_aRTPathAbsExTests[i].pcszOutput[0] == '%')
284 {
285 RTTESTI_CHECK_RC(rc = RTPathGetCurrent(szTmp, sizeof(szTmp)), VINF_SUCCESS);
286 if (RT_FAILURE(rc))
287 break;
288
289 pszExpected = szTmp;
290
291 if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'p')
292 {
293 cch = strlen(szTmp);
294 if (cch + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
295 strcpy(szTmp + cch, s_aRTPathAbsExTests[i].pcszOutput + 2);
296 }
297#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
298 else if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'd')
299 {
300 if (2 + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
301 strcpy(szTmp + 2, s_aRTPathAbsExTests[i].pcszOutput + 2);
302 }
303#endif
304 }
305 else
306 {
307 strcpy(szTmp, s_aRTPathAbsExTests[i].pcszOutput);
308 pszExpected = szTmp;
309 }
310
311 if (strcmp(szPath, pszExpected))
312 {
313 RTTestIFailed("Unexpected result\n"
314 " input base: '%s'\n"
315 " input path: '%s'\n"
316 " output: '%s'\n"
317 " expected: '%s'",
318 s_aRTPathAbsExTests[i].pcszInputBase,
319 s_aRTPathAbsExTests[i].pcszInputPath,
320 szPath,
321 s_aRTPathAbsExTests[i].pcszOutput);
322 }
323 }
324 }
325
326 /*
327 * RTPathStripFilename
328 */
329 RTTestSub(hTest, "RTPathStripFilename");
330 static const char *s_apszStripFilenameTests[] =
331 {
332 "/usr/include///", "/usr/include//",
333 "/usr/include/", "/usr/include",
334 "/usr/include", "/usr",
335 "/usr", "/",
336 "usr", ".",
337#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
338 "c:/windows", "c:/",
339 "c:/", "c:/",
340 "D:", "D:",
341 "C:\\OS2\\DLLS", "C:\\OS2",
342#endif
343 };
344 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripFilenameTests); i += 2)
345 {
346 const char *pszInput = s_apszStripFilenameTests[i];
347 const char *pszExpect = s_apszStripFilenameTests[i + 1];
348 strcpy(szPath, pszInput);
349 RTPathStripFilename(szPath);
350 if (strcmp(szPath, pszExpect))
351 {
352 RTTestIFailed("Unexpected result\n"
353 " input: '%s'\n"
354 " output: '%s'\n"
355 "expected: '%s'",
356 pszInput, szPath, pszExpect);
357 }
358 }
359
360 /*
361 * RTPathAppend.
362 */
363 RTTestSub(hTest, "RTPathAppend");
364 static const char *s_apszAppendTests[] =
365 {
366 /* base append result */
367 "/", "", "/",
368 "", "/", "/",
369 "/", "/", "/",
370 "/x", "", "/x",
371 "/x", "/", "/x/",
372 "/", "x", "/x",
373 "dir", "file", "dir/file",
374 "dir", "/file", "dir/file",
375 "dir", "//file", "dir/file",
376 "dir", "///file", "dir/file",
377 "dir/", "/file", "dir/file",
378 "dir/", "//file", "dir/file",
379 "dir/", "///file", "dir/file",
380 "dir//", "file", "dir/file",
381 "dir//", "/file", "dir/file",
382 "dir//", "//file", "dir/file",
383 "dir///", "///file", "dir/file",
384 "/bin/testcase", "foo.r0", "/bin/testcase/foo.r0",
385#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
386 "/", "\\", "/",
387 "\\", "/", "\\",
388 "\\\\srv\\shr", "dir//", "\\\\srv\\shr/dir//",
389 "\\\\srv\\shr", "dir//file", "\\\\srv\\shr/dir//file",
390 "\\\\srv\\shr", "//dir//", "\\\\srv\\shr/dir//",
391 "\\\\srv\\shr", "/\\dir//", "\\\\srv\\shr\\dir//",
392 "\\\\", "not-srv/not-shr/file", "\\not-srv/not-shr/file",
393 "C:", "autoexec.bat", "C:autoexec.bat",
394 "C:", "/autoexec.bat", "C:/autoexec.bat",
395 "C:", "\\autoexec.bat", "C:\\autoexec.bat",
396 "C:\\", "/autoexec.bat", "C:\\autoexec.bat",
397 "C:\\\\", "autoexec.bat", "C:\\autoexec.bat",
398 "E:\\bin\\testcase", "foo.r0", "E:\\bin\\testcase/foo.r0",
399#endif
400 };
401 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
402 {
403 const char *pszInput = s_apszAppendTests[i];
404 const char *pszAppend = s_apszAppendTests[i + 1];
405 const char *pszExpect = s_apszAppendTests[i + 2];
406 strcpy(szPath, pszInput);
407 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, sizeof(szPath), pszAppend), VINF_SUCCESS);
408 if (RT_FAILURE(rc))
409 continue;
410 if (strcmp(szPath, pszExpect))
411 {
412 RTTestIFailed("Unexpected result\n"
413 " input: '%s'\n"
414 " append: '%s'\n"
415 " output: '%s'\n"
416 "expected: '%s'",
417 pszInput, pszAppend, szPath, pszExpect);
418 }
419 else
420 {
421 size_t const cchResult = strlen(szPath);
422
423 strcpy(szPath, pszInput);
424 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 2, pszAppend), VINF_SUCCESS);
425 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
426
427 strcpy(szPath, pszInput);
428 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 1, pszAppend), VINF_SUCCESS);
429 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
430
431 if (strlen(pszInput) < cchResult)
432 {
433 strcpy(szPath, pszInput);
434 RTTESTI_CHECK_RC(RTPathAppend(szPath, cchResult, pszAppend), VERR_BUFFER_OVERFLOW);
435 }
436 }
437 }
438
439 /*
440 * RTPathJoin - reuse the append tests.
441 */
442 RTTestSub(hTest, "RTPathJoin");
443 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
444 {
445 const char *pszInput = s_apszAppendTests[i];
446 const char *pszAppend = s_apszAppendTests[i + 1];
447 const char *pszExpect = s_apszAppendTests[i + 2];
448
449 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
450
451 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, sizeof(szPath), pszInput, pszAppend), VINF_SUCCESS);
452 if (RT_FAILURE(rc))
453 continue;
454 if (strcmp(szPath, pszExpect))
455 {
456 RTTestIFailed("Unexpected result\n"
457 " input: '%s'\n"
458 " append: '%s'\n"
459 " output: '%s'\n"
460 "expected: '%s'",
461 pszInput, pszAppend, szPath, pszExpect);
462 }
463 else
464 {
465 size_t const cchResult = strlen(szPath);
466
467 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
468 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult + 2, pszInput, pszAppend), VINF_SUCCESS);
469 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
470
471 memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
472 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult + 1, pszInput, pszAppend), VINF_SUCCESS);
473 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
474
475 RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult, pszInput, pszAppend), VERR_BUFFER_OVERFLOW);
476 }
477 }
478
479 /*
480 * RTPathJoinA - reuse the append tests.
481 */
482 RTTestSub(hTest, "RTPathJoinA");
483 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
484 {
485 const char *pszInput = s_apszAppendTests[i];
486 const char *pszAppend = s_apszAppendTests[i + 1];
487 const char *pszExpect = s_apszAppendTests[i + 2];
488
489 char *pszPathDst;
490 RTTESTI_CHECK(pszPathDst = RTPathJoinA(pszInput, pszAppend));
491 if (!pszPathDst)
492 continue;
493 if (strcmp(pszPathDst, pszExpect))
494 {
495 RTTestIFailed("Unexpected result\n"
496 " input: '%s'\n"
497 " append: '%s'\n"
498 " output: '%s'\n"
499 "expected: '%s'",
500 pszInput, pszAppend, pszPathDst, pszExpect);
501 }
502 RTStrFree(pszPathDst);
503 }
504
505 /*
506 * RTPathStripTrailingSlash
507 */
508 static const char *s_apszStripTrailingSlash[] =
509 {
510 /* input result */
511 "/", "/",
512 "//", "/",
513 "////////////////////", "/",
514 "/tmp", "/tmp",
515 "/tmp////////////////", "/tmp",
516 "tmp", "tmp",
517 "tmp////////////////", "tmp",
518 "./", ".",
519#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
520 "////////////////////", "/",
521 "D:", "D:",
522 "D:/", "D:/",
523 "D:\\", "D:\\",
524 "D:\\/\\", "D:\\",
525 "D:/\\/\\", "D:/",
526 "C:/Temp", "D:/Temp",
527 "C:/Temp/", "D:/Temp/",
528 "C:/Temp\\/", "D:/Temp",
529#endif
530 };
531 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripTrailingSlash); i += 2)
532 {
533 const char *pszInput = s_apszStripTrailingSlash[i];
534 const char *pszExpect = s_apszStripTrailingSlash[i + 1];
535
536 strcpy(szPath, pszInput);
537 cch = RTPathStripTrailingSlash(szPath);
538 if (strcmp(szPath, pszExpect))
539 RTTestIFailed("Unexpected result\n"
540 " input: '%s'\n"
541 " output: '%s'\n"
542 "expected: '%s'",
543 pszInput, szPath, pszExpect);
544 else
545 RTTESTI_CHECK(cch == strlen(szPath));
546 }
547
548 /*
549 * RTPathCountComponents
550 */
551 RTTestSub(hTest, "RTPathCountComponents");
552 RTTESTI_CHECK(RTPathCountComponents("") == 0);
553 RTTESTI_CHECK(RTPathCountComponents("/") == 1);
554 RTTESTI_CHECK(RTPathCountComponents("//") == 1);
555 RTTESTI_CHECK(RTPathCountComponents("//////////////") == 1);
556 RTTESTI_CHECK(RTPathCountComponents("//////////////bin") == 2);
557 RTTESTI_CHECK(RTPathCountComponents("//////////////bin/") == 2);
558 RTTESTI_CHECK(RTPathCountComponents("//////////////bin/////") == 2);
559 RTTESTI_CHECK(RTPathCountComponents("..") == 1);
560 RTTESTI_CHECK(RTPathCountComponents("../") == 1);
561 RTTESTI_CHECK(RTPathCountComponents("../..") == 2);
562 RTTESTI_CHECK(RTPathCountComponents("../../") == 2);
563#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
564 RTTESTI_CHECK(RTPathCountComponents("d:") == 1);
565 RTTESTI_CHECK(RTPathCountComponents("d:/") == 1);
566 RTTESTI_CHECK(RTPathCountComponents("d:/\\") == 1);
567 RTTESTI_CHECK(RTPathCountComponents("d:\\") == 1);
568 RTTESTI_CHECK(RTPathCountComponents("c:\\config.sys") == 2);
569 RTTESTI_CHECK(RTPathCountComponents("c:\\windows") == 2);
570 RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\") == 2);
571 RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\system32") == 3);
572 RTTESTI_CHECK(RTPathCountComponents("//./C$") == 1);
573 RTTESTI_CHECK(RTPathCountComponents("\\\\.\\C$") == 1);
574 RTTESTI_CHECK(RTPathCountComponents("/\\.\\C$") == 1);
575 RTTESTI_CHECK(RTPathCountComponents("//myserver") == 1);
576 RTTESTI_CHECK(RTPathCountComponents("//myserver/") == 1);
577 RTTESTI_CHECK(RTPathCountComponents("//myserver/share") == 1);
578 RTTESTI_CHECK(RTPathCountComponents("//myserver/share/") == 1);
579 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\") == 1);
580 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x") == 2);
581 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y") == 3);
582 RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y\\") == 3);
583#endif
584
585 /*
586 * RTPathCopyComponents
587 */
588 struct
589 {
590 const char *pszSrc;
591 size_t cComponents;
592 const char *pszResult;
593 } s_aCopyComponents[] =
594 {
595 { "", 0, "" },
596 { "", 5, "" },
597 { "/", 0, "" },
598 { "/", 1, "/" },
599 { "/", 2, "/" },
600 { "/usr/bin/sed", 0, "" },
601 { "/usr/bin/sed", 1, "/" },
602 { "/usr/bin/sed", 2, "/usr/" },
603 { "/usr/bin/sed", 3, "/usr/bin/" },
604 { "/usr/bin/sed", 4, "/usr/bin/sed" },
605 { "/usr/bin/sed", 5, "/usr/bin/sed" },
606 { "/usr/bin/sed", 6, "/usr/bin/sed" },
607 { "/usr///bin/sed", 2, "/usr///" },
608 };
609 for (unsigned i = 0; i < RT_ELEMENTS(s_aCopyComponents); i++)
610 {
611 const char *pszInput = s_aCopyComponents[i].pszSrc;
612 size_t cComponents = s_aCopyComponents[i].cComponents;
613 const char *pszResult = s_aCopyComponents[i].pszResult;
614
615 memset(szPath, 'a', sizeof(szPath));
616 rc = RTPathCopyComponents(szPath, sizeof(szPath), pszInput, cComponents);
617 RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
618 if (RT_SUCCESS(rc) && strcmp(szPath, pszResult))
619 RTTestIFailed("Unexpected result\n"
620 " input: '%s' cComponents=%u\n"
621 " output: '%s'\n"
622 "expected: '%s'",
623 pszInput, cComponents, szPath, pszResult);
624 else if (RT_SUCCESS(rc))
625 {
626 RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult) + 1, pszInput, cComponents), VINF_SUCCESS);
627 RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult), pszInput, cComponents), VERR_BUFFER_OVERFLOW);
628 }
629 }
630
631
632 /*
633 * RTPathStripExt
634 */
635 RTTestSub(hTest, "RTPathStripExt");
636 struct
637 {
638 const char *pszSrc;
639 const char *pszResult;
640 } s_aStripExt[] =
641 {
642 { "filename.ext", "filename" },
643 { "filename.ext1.ext2.ext3", "filename.ext1.ext2" },
644 { "filename..ext", "filename." },
645 { "filename.ext.", "filename.ext" }, /** @todo This is a bit weird/wrong, but not half as weird as the way Windows+OS/2 deals with a trailing dots. */
646 };
647 for (unsigned i = 0; i < RT_ELEMENTS(s_aStripExt); i++)
648 {
649 const char *pszInput = s_aStripExt[i].pszSrc;
650 const char *pszResult = s_aStripExt[i].pszResult;
651
652 strcpy(szPath, pszInput);
653 RTPathStripExt(szPath);
654 if (strcmp(szPath, pszResult))
655 RTTestIFailed("Unexpected result\n"
656 " input: '%s'\n"
657 " output: '%s'\n"
658 "expected: '%s'",
659 pszInput, szPath, pszResult);
660 }
661
662 /*
663 * RTPathCalcRelative
664 */
665 RTTestSub(hTest, "RTPathCalcRelative");
666 struct
667 {
668 const char *pszFrom;
669 const char *pszTo;
670 int rc;
671 const char *pszExpected;
672 } s_aRelPath[] =
673 {
674 { "/home/test.ext", "/home/test2.ext", VINF_SUCCESS, "test2.ext"},
675 { "/dir/test.ext", "/dir/dir2/test2.ext", VINF_SUCCESS, "dir2/test2.ext"},
676 { "/dir/dir2/test.ext", "/dir/test2.ext", VINF_SUCCESS, "../test2.ext"},
677 { "/dir/dir2/test.ext", "/dir/dir3/test2.ext", VINF_SUCCESS, "../dir3/test2.ext"},
678#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
679 { "\\\\server\\share\\test.ext", "\\\\server\\share2\\test2.ext", VERR_NOT_SUPPORTED, ""},
680 { "c:\\dir\\test.ext", "f:\\dir\\test.ext", VERR_NOT_SUPPORTED, ""}
681#endif
682 };
683 for (unsigned i = 0; i < RT_ELEMENTS(s_aRelPath); i++)
684 {
685 const char *pszFrom = s_aRelPath[i].pszFrom;
686 const char *pszTo = s_aRelPath[i].pszTo;
687
688 rc = RTPathCalcRelative(szPath, sizeof(szPath), pszFrom, pszTo);
689 if (rc != s_aRelPath[i].rc)
690 RTTestIFailed("Unexpected return code\n"
691 " got: %Rrc\n"
692 "expected: %Rrc",
693 rc, s_aRelPath[i].rc);
694 else if ( RT_SUCCESS(rc)
695 && strcmp(szPath, s_aRelPath[i].pszExpected))
696 RTTestIFailed("Unexpected result\n"
697 " from: '%s'\n"
698 " to: '%s'\n"
699 " output: '%s'\n"
700 "expected: '%s'",
701 pszFrom, pszTo, szPath, s_aRelPath[i].pszExpected);
702 }
703
704 testParser(hTest);
705
706 /*
707 * Summary.
708 */
709 return RTTestSummaryAndDestroy(hTest);
710}
711
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