VirtualBox

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

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

IPRT: Rewrote RTPathTemp and added a testcase for it.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.1 KB
Line 
1/* $Id: tstPath.cpp 20101 2009-05-27 16:43:08Z 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 RTTestSub(hTest, "RTPathTemp");
82 RTTESTI_CHECK_RC(RTPathTemp(szPath, sizeof(szPath)), VINF_SUCCESS);
83 if (RT_SUCCESS(rc))
84 RTTestIPrintf(RTTESTLVL_INFO, "PathTemp={%s}\n", szPath);
85 size_t cch = strlen(szPath);
86 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch), VERR_BUFFER_OVERFLOW);
87 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+1), VINF_SUCCESS);
88 RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+2), VINF_SUCCESS);
89
90
91 /*
92 * RTPathAbsEx
93 */
94 RTTestSub(hTest, "RTPathAbsEx");
95 static const struct
96 {
97 const char *pcszInputBase;
98 const char *pcszInputPath;
99 int rc;
100 const char *pcszOutput;
101 }
102 s_aRTPathAbsExTests[] =
103 {
104#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
105 { NULL, "", VERR_INVALID_PARAMETER, NULL },
106 { NULL, ".", VINF_SUCCESS, "%p" },
107 { NULL, "\\", VINF_SUCCESS, "%d\\" },
108 { NULL, "\\..", VINF_SUCCESS, "%d\\" },
109 { NULL, "/absolute/..", VINF_SUCCESS, "%d\\" },
110 { NULL, "/absolute\\\\../..", VINF_SUCCESS, "%d\\" },
111 { NULL, "/absolute//../path\\", VINF_SUCCESS, "%d\\path" },
112 { NULL, "/absolute/../../path", VINF_SUCCESS, "%d\\path" },
113 { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p\\dir\\file.txt" },
114 { NULL, "\\data\\", VINF_SUCCESS, "%d\\data" },
115 { "relative_base/dir\\", "\\from_root", VINF_SUCCESS, "%d\\from_root" },
116 { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p\\relative_base\\dir\\relative_also" },
117#else
118 { NULL, "", VERR_INVALID_PARAMETER, NULL },
119 { NULL, ".", VINF_SUCCESS, "%p" },
120 { NULL, "/", VINF_SUCCESS, "/" },
121 { NULL, "/..", VINF_SUCCESS, "/" },
122 { NULL, "/absolute/..", VINF_SUCCESS, "/" },
123 { NULL, "/absolute\\\\../..", VINF_SUCCESS, "/" },
124 { NULL, "/absolute//../path/", VINF_SUCCESS, "/path" },
125 { NULL, "/absolute/../../path", VINF_SUCCESS, "/path" },
126 { NULL, "relative/../dir/./././file.txt", VINF_SUCCESS, "%p/dir/file.txt" },
127 { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p/dir\\.\\.\\.\\file.txt" }, /* linux-specific */
128 { NULL, "/data/", VINF_SUCCESS, "/data" },
129 { "relative_base/dir/", "/from_root", VINF_SUCCESS, "/from_root" },
130 { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p/relative_base/dir/relative_also" },
131#endif
132#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
133 { NULL, "C:\\", VINF_SUCCESS, "C:\\" },
134 { "C:\\", "..", VINF_SUCCESS, "C:\\" },
135 { "C:\\temp", "..", VINF_SUCCESS, "C:\\" },
136 { "C:\\VirtualBox/Machines", "..\\VirtualBox.xml", VINF_SUCCESS, "C:\\VirtualBox\\VirtualBox.xml" },
137 { "C:\\MustDie", "\\from_root/dir/..", VINF_SUCCESS, "C:\\from_root" },
138 { "C:\\temp", "D:\\data", VINF_SUCCESS, "D:\\data" },
139 { NULL, "\\\\server\\..\\share", VINF_SUCCESS, "\\\\server\\..\\share" /* kind of strange */ },
140 { NULL, "\\\\server/", VINF_SUCCESS, "\\\\server" },
141 { NULL, "\\\\", VINF_SUCCESS, "\\\\" },
142 { NULL, "\\\\\\something", VINF_SUCCESS, "\\\\\\something" /* kind of strange */ },
143 { "\\\\server\\share_as_base", "/from_root", VINF_SUCCESS, "\\\\server\\from_root" },
144 { "\\\\just_server", "/from_root", VINF_SUCCESS, "\\\\just_server\\from_root" },
145 { "\\\\server\\share_as_base", "relative\\data", VINF_SUCCESS, "\\\\server\\share_as_base\\relative\\data" },
146 { "base", "\\\\?\\UNC\\relative/edwef/..", VINF_SUCCESS, "\\\\?\\UNC\\relative" },
147 { "\\\\?\\UNC\\base", "/from_root", VERR_INVALID_NAME, NULL },
148#else
149 { "/temp", "..", VINF_SUCCESS, "/" },
150 { "/VirtualBox/Machines", "../VirtualBox.xml", VINF_SUCCESS, "/VirtualBox/VirtualBox.xml" },
151 { "/MustDie", "/from_root/dir/..", VINF_SUCCESS, "/from_root" },
152 { "\\temp", "\\data", VINF_SUCCESS, "%p/\\temp/\\data" },
153#endif
154 };
155
156 for (unsigned i = 0; i < RT_ELEMENTS(s_aRTPathAbsExTests); ++ i)
157 {
158 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase,
159 s_aRTPathAbsExTests[i].pcszInputPath,
160 szPath, sizeof(szPath));
161 if (rc != s_aRTPathAbsExTests[i].rc)
162 {
163 RTTestIFailed("unexpected result code!\n"
164 " input base: '%s'\n"
165 " input path: '%s'\n"
166 " output: '%s'\n"
167 " rc: %Rrc\n"
168 " expected rc: %Rrc",
169 s_aRTPathAbsExTests[i].pcszInputBase,
170 s_aRTPathAbsExTests[i].pcszInputPath,
171 szPath, rc,
172 s_aRTPathAbsExTests[i].rc);
173 continue;
174 }
175
176 char szTmp[RTPATH_MAX];
177 char *pszExpected = NULL;
178 if (s_aRTPathAbsExTests[i].pcszOutput != NULL)
179 {
180 if (s_aRTPathAbsExTests[i].pcszOutput[0] == '%')
181 {
182 RTTESTI_CHECK_RC(rc = RTPathGetCurrent(szTmp, sizeof(szTmp)), VINF_SUCCESS);
183 if (RT_FAILURE(rc))
184 break;
185
186 pszExpected = szTmp;
187
188 if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'p')
189 {
190 size_t cch = strlen(szTmp);
191 if (cch + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
192 strcpy(szTmp + cch, s_aRTPathAbsExTests[i].pcszOutput + 2);
193 }
194#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
195 else if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'd')
196 {
197 if (2 + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
198 strcpy(szTmp + 2, s_aRTPathAbsExTests[i].pcszOutput + 2);
199 }
200#endif
201 }
202 else
203 {
204 strcpy(szTmp, s_aRTPathAbsExTests[i].pcszOutput);
205 pszExpected = szTmp;
206 }
207
208 if (strcmp(szPath, pszExpected))
209 {
210 RTTestIFailed("Unexpected result\n"
211 " input base: '%s'\n"
212 " input path: '%s'\n"
213 " output: '%s'\n"
214 " expected: '%s'",
215 s_aRTPathAbsExTests[i].pcszInputBase,
216 s_aRTPathAbsExTests[i].pcszInputPath,
217 szPath,
218 s_aRTPathAbsExTests[i].pcszOutput);
219 }
220 }
221 }
222
223 /*
224 * RTPathStripFilename
225 */
226 RTTestSub(hTest, "RTPathStripFilename");
227 static const char *s_apszStripFilenameTests[] =
228 {
229 "/usr/include///", "/usr/include//",
230 "/usr/include/", "/usr/include",
231 "/usr/include", "/usr",
232 "/usr", "/",
233 "usr", ".",
234#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
235 "c:/windows", "c:/",
236 "c:/", "c:/",
237 "D:", "D:",
238 "C:\\OS2\\DLLS", "C:\\OS2",
239#endif
240 };
241 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripFilenameTests); i += 2)
242 {
243 const char *pszInput = s_apszStripFilenameTests[i];
244 const char *pszExpect = s_apszStripFilenameTests[i + 1];
245 strcpy(szPath, pszInput);
246 RTPathStripFilename(szPath);
247 if (strcmp(szPath, pszExpect))
248 {
249 RTTestIFailed("Unexpected result\n"
250 " input: '%s'\n"
251 " output: '%s'\n"
252 "expected: '%s'",
253 pszInput, szPath, pszExpect);
254 }
255 }
256
257 /*
258 * RTPathAppend.
259 */
260 RTTestSub(hTest, "RTPathAppend");
261 static const char *s_apszAppendTests[] =
262 {
263 /* base append result */
264 "/", "", "/",
265 "", "/", "/",
266 "/", "/", "/",
267 "/x", "", "/x",
268 "/x", "/", "/x/",
269 "/", "x", "/x",
270 "dir", "file", "dir/file",
271 "dir", "/file", "dir/file",
272 "dir", "//file", "dir/file",
273 "dir", "///file", "dir/file",
274 "dir/", "/file", "dir/file",
275 "dir/", "//file", "dir/file",
276 "dir/", "///file", "dir/file",
277 "dir//", "file", "dir/file",
278 "dir//", "/file", "dir/file",
279 "dir//", "//file", "dir/file",
280 "dir///", "///file", "dir/file",
281 "/bin/testcase", "foo.r0", "/bin/testcase/foo.r0",
282#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
283 "/", "\\", "/",
284 "\\", "/", "\\",
285 "\\\\srv\\shr", "dir//", "\\\\srv\\shr/dir//",
286 "\\\\srv\\shr", "dir//file", "\\\\srv\\shr/dir//file",
287 "\\\\srv\\shr", "//dir//", "\\\\srv\\shr/dir//",
288 "\\\\srv\\shr", "/\\dir//", "\\\\srv\\shr\\dir//",
289 "\\\\", "not-srv/not-shr/file", "\\not-srv/not-shr/file",
290 "C:", "autoexec.bat", "C:autoexec.bat",
291 "C:", "/autoexec.bat", "C:/autoexec.bat",
292 "C:", "\\autoexec.bat", "C:\\autoexec.bat",
293 "C:\\", "/autoexec.bat", "C:\\autoexec.bat",
294 "C:\\\\", "autoexec.bat", "C:\\autoexec.bat",
295 "E:\\bin\\testcase", "foo.r0", "E:\\bin\\testcase/foo.r0",
296#endif
297 };
298 for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
299 {
300 const char *pszInput = s_apszAppendTests[i];
301 const char *pszAppend = s_apszAppendTests[i + 1];
302 const char *pszExpect = s_apszAppendTests[i + 2];
303 strcpy(szPath, pszInput);
304 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, sizeof(szPath), pszAppend), VINF_SUCCESS);
305 if (RT_FAILURE(rc))
306 continue;
307 if (strcmp(szPath, pszExpect))
308 {
309 RTTestIFailed("Unexpected result\n"
310 " input: '%s'\n"
311 " append: '%s'\n"
312 " output: '%s'\n"
313 "expected: '%s'",
314 pszInput, pszAppend, szPath, pszExpect);
315 }
316 else
317 {
318 size_t const cchResult = strlen(szPath);
319
320 strcpy(szPath, pszInput);
321 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 2, pszAppend), VINF_SUCCESS);
322 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
323
324 strcpy(szPath, pszInput);
325 RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 1, pszAppend), VINF_SUCCESS);
326 RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
327
328 if (strlen(pszInput) < cchResult)
329 {
330 strcpy(szPath, pszInput);
331 RTTESTI_CHECK_RC(RTPathAppend(szPath, cchResult, pszAppend), VERR_BUFFER_OVERFLOW);
332 }
333 }
334 }
335
336
337
338 /*
339 * Summary.
340 */
341 return RTTestSummaryAndDestroy(hTest);
342}
343
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