VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstPath.cpp@ 19945

Last change on this file since 19945 was 19929, checked in by vboxsync, 16 years ago

RTPathAppend: bugfix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.6 KB
Line 
1/* $Id: tstPath.cpp 19929 2009-05-23 00:15:49Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Test various path functions.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/path.h>
35
36#include <iprt/err.h>
37#include <iprt/initterm.h>
38#include <iprt/param.h>
39#include <iprt/process.h>
40#include <iprt/stream.h>
41#include <iprt/string.h>
42#include <iprt/test.h>
43
44
45int main()
46{
47 char szPath[RTPATH_MAX];
48
49 /*
50 * Init RT+Test.
51 */
52 int rc = RTR3Init();
53 if (RT_FAILURE(rc))
54 return 1;
55
56 RTTEST hTest;
57 rc = RTTestCreate("tstPath", &hTest);
58 if (RT_FAILURE(rc))
59 return 1;
60 RTTestBanner(hTest);
61
62 /*
63 * RTPathExecDir, RTPathUserHome and RTProcGetExecutableName.
64 */
65 RTTestSub(hTest, "RTPathExecDir");
66 RTTESTI_CHECK_RC(RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS);
67 if (RT_SUCCESS(rc))
68 RTTestIPrintf(RTTESTLVL_INFO, "ExecDir={%s}\n", szPath);
69
70 RTTestSub(hTest, "RTProcGetExecutableName");
71 if (RTProcGetExecutableName(szPath, sizeof(szPath)) == szPath)
72 RTTestIPrintf(RTTESTLVL_INFO, "ExecutableName={%s}\n", szPath);
73 else
74 RTTestIFailed("RTProcGetExecutableName -> NULL");
75
76 RTTestSub(hTest, "RTPathUserHome");
77 RTTESTI_CHECK_RC(RTPathUserHome(szPath, sizeof(szPath)), VINF_SUCCESS);
78 if (RT_SUCCESS(rc))
79 RTTestIPrintf(RTTESTLVL_INFO, "UserHome={%s}\n", szPath);
80
81 /*
82 * RTPathAbsEx
83 */
84 RTTestSub(hTest, "RTPathAbsEx");
85 static const struct
86 {
87 const char *pcszInputBase;
88 const char *pcszInputPath;
89 int rc;
90 const char *pcszOutput;
91 }
92 s_aRTPathAbsExTests[] =
93 {
94#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
95 { NULL, "", VERR_INVALID_PARAMETER, NULL },
96 { NULL, ".", VINF_SUCCESS, "%p" },
97 { NULL, "\\", VINF_SUCCESS, "%d\\" },
98 { NULL, "\\..", VINF_SUCCESS, "%d\\" },
99 { NULL, "/absolute/..", VINF_SUCCESS, "%d\\" },
100 { NULL, "/absolute\\\\../..", VINF_SUCCESS, "%d\\" },
101 { NULL, "/absolute//../path\\", VINF_SUCCESS, "%d\\path" },
102 { NULL, "/absolute/../../path", VINF_SUCCESS, "%d\\path" },
103 { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p\\dir\\file.txt" },
104 { NULL, "\\data\\", VINF_SUCCESS, "%d\\data" },
105 { "relative_base/dir\\", "\\from_root", VINF_SUCCESS, "%d\\from_root" },
106 { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p\\relative_base\\dir\\relative_also" },
107#else
108 { NULL, "", VERR_INVALID_PARAMETER, NULL },
109 { NULL, ".", VINF_SUCCESS, "%p" },
110 { NULL, "/", VINF_SUCCESS, "/" },
111 { NULL, "/..", VINF_SUCCESS, "/" },
112 { NULL, "/absolute/..", VINF_SUCCESS, "/" },
113 { NULL, "/absolute\\\\../..", VINF_SUCCESS, "/" },
114 { NULL, "/absolute//../path/", VINF_SUCCESS, "/path" },
115 { NULL, "/absolute/../../path", VINF_SUCCESS, "/path" },
116 { NULL, "relative/../dir/./././file.txt", VINF_SUCCESS, "%p/dir/file.txt" },
117 { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p/dir\\.\\.\\.\\file.txt" }, /* linux-specific */
118 { NULL, "/data/", VINF_SUCCESS, "/data" },
119 { "relative_base/dir/", "/from_root", VINF_SUCCESS, "/from_root" },
120 { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p/relative_base/dir/relative_also" },
121#endif
122#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
123 { NULL, "C:\\", VINF_SUCCESS, "C:\\" },
124 { "C:\\", "..", VINF_SUCCESS, "C:\\" },
125 { "C:\\temp", "..", VINF_SUCCESS, "C:\\" },
126 { "C:\\VirtualBox/Machines", "..\\VirtualBox.xml", VINF_SUCCESS, "C:\\VirtualBox\\VirtualBox.xml" },
127 { "C:\\MustDie", "\\from_root/dir/..", VINF_SUCCESS, "C:\\from_root" },
128 { "C:\\temp", "D:\\data", VINF_SUCCESS, "D:\\data" },
129 { NULL, "\\\\server\\..\\share", VINF_SUCCESS, "\\\\server\\..\\share" /* kind of strange */ },
130 { NULL, "\\\\server/", VINF_SUCCESS, "\\\\server" },
131 { NULL, "\\\\", VINF_SUCCESS, "\\\\" },
132 { NULL, "\\\\\\something", VINF_SUCCESS, "\\\\\\something" /* kind of strange */ },
133 { "\\\\server\\share_as_base", "/from_root", VINF_SUCCESS, "\\\\server\\from_root" },
134 { "\\\\just_server", "/from_root", VINF_SUCCESS, "\\\\just_server\\from_root" },
135 { "\\\\server\\share_as_base", "relative\\data", VINF_SUCCESS, "\\\\server\\share_as_base\\relative\\data" },
136 { "base", "\\\\?\\UNC\\relative/edwef/..", VINF_SUCCESS, "\\\\?\\UNC\\relative" },
137 { "\\\\?\\UNC\\base", "/from_root", VERR_INVALID_NAME, NULL },
138#else
139 { "/temp", "..", VINF_SUCCESS, "/" },
140 { "/VirtualBox/Machines", "../VirtualBox.xml", VINF_SUCCESS, "/VirtualBox/VirtualBox.xml" },
141 { "/MustDie", "/from_root/dir/..", VINF_SUCCESS, "/from_root" },
142 { "\\temp", "\\data", VINF_SUCCESS, "%p/\\temp/\\data" },
143#endif
144 };
145
146 for (unsigned i = 0; i < RT_ELEMENTS(s_aRTPathAbsExTests); ++ i)
147 {
148 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase,
149 s_aRTPathAbsExTests[i].pcszInputPath,
150 szPath, sizeof(szPath));
151 if (rc != s_aRTPathAbsExTests[i].rc)
152 {
153 RTTestIFailed("unexpected result code!\n"
154 " input base: '%s'\n"
155 " input path: '%s'\n"
156 " output: '%s'\n"
157 " rc: %Rrc\n"
158 " expected rc: %Rrc",
159 s_aRTPathAbsExTests[i].pcszInputBase,
160 s_aRTPathAbsExTests[i].pcszInputPath,
161 szPath, rc,
162 s_aRTPathAbsExTests[i].rc);
163 continue;
164 }
165
166 char szTmp[RTPATH_MAX];
167 char *pszExpected = NULL;
168 if (s_aRTPathAbsExTests[i].pcszOutput != NULL)
169 {
170 if (s_aRTPathAbsExTests[i].pcszOutput[0] == '%')
171 {
172 RTTESTI_CHECK_RC(rc = RTPathGetCurrent(szTmp, sizeof(szTmp)), VINF_SUCCESS);
173 if (RT_FAILURE(rc))
174 break;
175
176 pszExpected = szTmp;
177
178 if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'p')
179 {
180 size_t cch = strlen(szTmp);
181 if (cch + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
182 strcpy(szTmp + cch, s_aRTPathAbsExTests[i].pcszOutput + 2);
183 }
184#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
185 else if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'd')
186 {
187 if (2 + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
188 strcpy(szTmp + 2, s_aRTPathAbsExTests[i].pcszOutput + 2);
189 }
190#endif
191 }
192 else
193 {
194 strcpy(szTmp, s_aRTPathAbsExTests[i].pcszOutput);
195 pszExpected = szTmp;
196 }
197
198 if (strcmp(szPath, pszExpected))
199 {
200 RTTestIFailed("Unexpected result\n"
201 " input base: '%s'\n"
202 " input path: '%s'\n"
203 " output: '%s'\n"
204 " expected: '%s'",
205 s_aRTPathAbsExTests[i].pcszInputBase,
206 s_aRTPathAbsExTests[i].pcszInputPath,
207 szPath,
208 s_aRTPathAbsExTests[i].pcszOutput);
209 }
210 }
211 }
212
213 /*
214 * RTPathStripFilename
215 */
216 RTTestSub(hTest, "RTPathStripFilename");
217 static const char *s_apszStripFilenameTests[] =
218 {
219 "/usr/include///", "/usr/include//",
220 "/usr/include/", "/usr/include",
221 "/usr/include", "/usr",
222 "/usr", "/",
223 "usr", ".",
224#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
225 "c:/windows", "c:/",
226 "c:/", "c:/",
227 "D:", "D:",
228 "C:\\OS2\\DLLS", "C:\\OS2",
229#endif
230 };
231 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripFilenameTests); i += 2)
232 {
233 const char *pszInput = s_apszStripFilenameTests[i];
234 const char *pszExpect = s_apszStripFilenameTests[i + 1];
235 strcpy(szPath, pszInput);
236 RTPathStripFilename(szPath);
237 if (strcmp(szPath, pszExpect))
238 {
239 RTTestIFailed("Unexpected result\n"
240 " input: '%s'\n"
241 " output: '%s'\n"
242 "expected: '%s'",
243 pszInput, szPath, pszExpect);
244 }
245 }
246
247 /*
248 * RTPathAppend.
249 */
250 RTTestSub(hTest, "RTPathAppend");
251 static const char *s_apszAppendTests[] =
252 {
253 /* base append result */
254 "/", "", "/",
255 "", "/", "/",
256 "/", "/", "/",
257 "/x", "", "/x",
258 "/x", "/", "/x/",
259 "/", "x", "/x",
260 "dir", "file", "dir/file",
261 "dir", "/file", "dir/file",
262 "dir", "//file", "dir/file",
263 "dir", "///file", "dir/file",
264 "dir/", "/file", "dir/file",
265 "dir/", "//file", "dir/file",
266 "dir/", "///file", "dir/file",
267 "dir//", "file", "dir/file",
268 "dir//", "/file", "dir/file",
269 "dir//", "//file", "dir/file",
270 "dir///", "///file", "dir/file",
271 "/bin/testcase", "foo.r0", "/bin/testcase/foo.r0",
272#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
273 "/", "\\", "/",
274 "\\", "/", "\\",
275 "\\\\srv\\shr", "dir//", "\\\\srv\\shr/dir//",
276 "\\\\srv\\shr", "dir//file", "\\\\srv\\shr/dir//file",
277 "\\\\srv\\shr", "//dir//", "\\\\srv\\shr/dir//",
278 "\\\\srv\\shr", "/\\dir//", "\\\\srv\\shr\\dir//",
279 "\\\\", "not-srv/not-shr/file", "\\not-srv/not-shr/file",
280 "C:", "autoexec.bat", "C:autoexec.bat",
281 "C:", "/autoexec.bat", "C:/autoexec.bat",
282 "C:", "\\autoexec.bat", "C:\\autoexec.bat",
283 "C:\\", "/autoexec.bat", "C:\\autoexec.bat",
284 "C:\\\\", "autoexec.bat", "C:\\autoexec.bat",
285 "E:\\bin\\testcase", "foo.r0", "E:\\bin\\testcase/foo.r0",
286#endif
287 };
288 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
289 {
290 const char *pszInput = s_apszAppendTests[i];
291 const char *pszAppend = s_apszAppendTests[i + 1];
292 const char *pszExpect = s_apszAppendTests[i + 2];
293 strcpy(szPath, pszInput);
294 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, sizeof(szPath), pszAppend), VINF_SUCCESS);
295 if (RT_FAILURE(rc))
296 continue;
297 if (strcmp(szPath, pszExpect))
298 {
299 RTTestIFailed("Unexpected result\n"
300 " input: '%s'\n"
301 " append: '%s'\n"
302 " output: '%s'\n"
303 "expected: '%s'",
304 pszInput, pszAppend, szPath, pszExpect);
305 }
306 else
307 {
308 size_t const cchResult = strlen(szPath);
309
310 strcpy(szPath, pszInput);
311 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 2, pszAppend), VINF_SUCCESS);
312 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
313
314 strcpy(szPath, pszInput);
315 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 1, pszAppend), VINF_SUCCESS);
316 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
317
318 if (strlen(pszInput) < cchResult)
319 {
320 strcpy(szPath, pszInput);
321 RTTESTI_CHECK_RC(RTPathAppend(szPath, cchResult, pszAppend), VERR_BUFFER_OVERFLOW);
322 }
323 }
324 }
325
326
327
328 /*
329 * Summary.
330 */
331 return RTTestSummaryAndDestroy(hTest);
332}
333
Note: See TracBrowser for help on using the repository browser.

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