VirtualBox

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

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

tstPath: style.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.1 KB
Line 
1/* $Id: tstPath.cpp 15806 2009-01-05 15:09:48Z 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.215389.xyz. 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#include <iprt/process.h>
36#include <iprt/initterm.h>
37#include <iprt/stream.h>
38#include <iprt/string.h>
39#include <iprt/err.h>
40#include <iprt/param.h>
41
42#if defined (RT_OS_WINDOWS)
43# include <direct.h> // for getcwd
44#else
45# include <unistd.h> // for getcwd
46#endif
47#include <errno.h> // for getcwd
48
49#define CHECK_RC(method) \
50 do { \
51 rc = method; \
52 if (RT_FAILURE(rc)) \
53 { \
54 cErrors++; \
55 RTPrintf("\ntstPath: FAILED calling " #method " at line %d: rc=%Rrc\n", __LINE__, rc); \
56 } \
57 } while (0)
58
59int main()
60{
61 /*
62 * Init RT.
63 */
64 int rc;
65 int cErrors = 0;
66 CHECK_RC(RTR3Init());
67 if (RT_FAILURE(rc))
68 return 1;
69
70 /*
71 * RTPathProgram, RTPathUserHome and RTProcGetExecutableName.
72 */
73 char szPath[RTPATH_MAX];
74 CHECK_RC(RTPathProgram(szPath, sizeof(szPath)));
75 if (RT_SUCCESS(rc))
76 RTPrintf("Program={%s}\n", szPath);
77 CHECK_RC(RTPathUserHome(szPath, sizeof(szPath)));
78 if (RT_SUCCESS(rc))
79 RTPrintf("UserHome={%s}\n", szPath);
80 if (RTProcGetExecutableName(szPath, sizeof(szPath)) == szPath)
81 RTPrintf("ExecutableName={%s}\n", szPath);
82 else
83 {
84 RTPrintf("tstPath: FAILED - RTProcGetExecutableName\n");
85 cErrors++;
86 }
87
88
89 /*
90 * RTPathAbsEx
91 */
92 RTPrintf("tstPath: TESTING RTPathAbsEx()\n");
93 static const struct
94 {
95 const char *pcszInputBase;
96 const char *pcszInputPath;
97 int rc;
98 const char *pcszOutput;
99 }
100 s_aRTPathAbsExTests[] =
101 {
102#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
103 { NULL, "", VINF_SUCCESS, "%p" },
104 { NULL, ".", VINF_SUCCESS, "%p" },
105 { NULL, "\\", VINF_SUCCESS, "%d\\" },
106 { NULL, "\\..", VINF_SUCCESS, "%d\\" },
107 { NULL, "/absolute/..", VINF_SUCCESS, "%d\\" },
108 { NULL, "/absolute\\\\../..", VINF_SUCCESS, "%d\\" },
109 { NULL, "/absolute//../path\\", VINF_SUCCESS, "%d\\path" },
110 { NULL, "/absolute/../../path", VINF_SUCCESS, "%d\\path" },
111 { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p\\dir\\file.txt" },
112 { NULL, "\\data\\", VINF_SUCCESS, "%d\\data" },
113 { "relative_base/dir\\", "\\from_root", VINF_SUCCESS, "%d\\from_root" },
114 { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p\\relative_base\\dir\\relative_also" },
115#else
116 { NULL, "", VINF_SUCCESS, "%p" },
117 { NULL, ".", VINF_SUCCESS, "%p" },
118 { NULL, "/", VINF_SUCCESS, "/" },
119 { NULL, "/..", VINF_SUCCESS, "/" },
120 { NULL, "/absolute/..", VINF_SUCCESS, "/" },
121 { NULL, "/absolute\\\\../..", VINF_SUCCESS, "/" },
122 { NULL, "/absolute//../path/", VINF_SUCCESS, "/path" },
123 { NULL, "/absolute/../../path", VINF_SUCCESS, "/path" },
124 { NULL, "relative/../dir/./././file.txt", VINF_SUCCESS, "%p/dir/file.txt" },
125 { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p/dir\\.\\.\\.\\file.txt" }, /* linux-specific */
126 { NULL, "/data/", VINF_SUCCESS, "/data" },
127 { "relative_base/dir/", "/from_root", VINF_SUCCESS, "/from_root" },
128 { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p/relative_base/dir/relative_also" },
129#endif
130#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
131 { NULL, "C:\\", VINF_SUCCESS, "C:\\" },
132 { "C:\\", "..", VINF_SUCCESS, "C:\\" },
133 { "C:\\temp", "..", VINF_SUCCESS, "C:\\" },
134 { "C:\\VirtualBox/Machines", "..\\VirtualBox.xml", VINF_SUCCESS, "C:\\VirtualBox\\VirtualBox.xml" },
135 { "C:\\MustDie", "\\from_root/dir/..", VINF_SUCCESS, "C:\\from_root" },
136 { "C:\\temp", "D:\\data", VINF_SUCCESS, "D:\\data" },
137 { NULL, "\\\\server\\..\\share", VINF_SUCCESS, "\\\\server\\..\\share" /* kind of strange */ },
138 { NULL, "\\\\server/", VINF_SUCCESS, "\\\\server" },
139 { NULL, "\\\\", VINF_SUCCESS, "\\\\" },
140 { NULL, "\\\\\\something", VINF_SUCCESS, "\\\\\\something" /* kind of strange */ },
141 { "\\\\server\\share_as_base", "/from_root", VINF_SUCCESS, "\\\\server\\from_root" },
142 { "\\\\just_server", "/from_root", VINF_SUCCESS, "\\\\just_server\\from_root" },
143 { "\\\\server\\share_as_base", "relative\\data", VINF_SUCCESS, "\\\\server\\share_as_base\\relative\\data" },
144 { "base", "\\\\?\\UNC\\relative/edwef/..", VINF_SUCCESS, "\\\\?\\UNC\\relative" },
145 { "\\\\?\\UNC\\base", "/from_root", VERR_INVALID_NAME, NULL },
146#else
147 { "/temp", "..", VINF_SUCCESS, "/" },
148 { "/VirtualBox/Machines", "../VirtualBox.xml", VINF_SUCCESS, "/VirtualBox/VirtualBox.xml" },
149 { "/MustDie", "/from_root/dir/..", VINF_SUCCESS, "/from_root" },
150 { "\\temp", "\\data", VINF_SUCCESS, "%p/\\temp/\\data" },
151#endif
152 };
153
154 for (unsigned i = 0; i < RT_ELEMENTS(s_aRTPathAbsExTests); ++ i)
155 {
156 rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase,
157 s_aRTPathAbsExTests[i].pcszInputPath,
158 szPath, sizeof(szPath));
159 if (rc != s_aRTPathAbsExTests[i].rc)
160 {
161 RTPrintf("tstPath: RTPathAbsEx unexpected result code!\n"
162 " input base: '%s'\n"
163 " input path: '%s'\n"
164 " output: '%s'\n"
165 " rc: %Rrc\n"
166 " expected rc: %Rrc\n",
167 s_aRTPathAbsExTests[i].pcszInputBase,
168 s_aRTPathAbsExTests[i].pcszInputPath,
169 szPath, rc,
170 s_aRTPathAbsExTests[i].rc);
171 cErrors++;
172 continue;
173 }
174
175 char szTmp[RTPATH_MAX];
176 char *pszExpected = NULL;
177 if (s_aRTPathAbsExTests[i].pcszOutput != NULL)
178 {
179 if (s_aRTPathAbsExTests[i].pcszOutput[0] == '%')
180 {
181 /** @todo Use RTPathGetCurrent(). */
182 if (getcwd(szTmp, sizeof(szTmp)) == NULL)
183 {
184 RTPrintf("tstPath: getcwd failed with errno=%d!\n", errno);
185 cErrors++;
186 break;
187 }
188
189 pszExpected = szTmp;
190
191 if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'p')
192 {
193 size_t cch = strlen(szTmp);
194 if (cch + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
195 strcpy(szTmp + cch, s_aRTPathAbsExTests[i].pcszOutput + 2);
196 }
197#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
198 else if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'd')
199 {
200 if (2 + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
201 strcpy(szTmp + 2, s_aRTPathAbsExTests[i].pcszOutput + 2);
202 }
203#endif
204 }
205 else
206 {
207 strcpy(szTmp, s_aRTPathAbsExTests[i].pcszOutput);
208 pszExpected = szTmp;
209 }
210
211 if (strcmp(szPath, pszExpected))
212 {
213 RTPrintf("tstPath: RTPathAbsEx failed!\n"
214 " input base: '%s'\n"
215 " input path: '%s'\n"
216 " output: '%s'\n"
217 " expected: '%s'\n",
218 s_aRTPathAbsExTests[i].pcszInputBase,
219 s_aRTPathAbsExTests[i].pcszInputPath,
220 szPath,
221 s_aRTPathAbsExTests[i].pcszOutput);
222 cErrors++;
223 }
224 }
225 }
226
227 /*
228 * RTPathStripFilename
229 */
230 RTPrintf("tstPath: RTPathStripFilename...\n");
231 static const char *s_apszStripFilenameTests[] =
232 {
233 "/usr/include///", "/usr/include//",
234 "/usr/include/", "/usr/include",
235 "/usr/include", "/usr",
236 "/usr", "/",
237 "usr", ".",
238#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
239 "c:/windows", "c:/",
240 "c:/", "c:/",
241 "D:", "D:",
242 "C:\\OS2\\DLLS", "C:\\OS2",
243#endif
244 };
245 for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripFilenameTests); i += 2)
246 {
247 const char *pszInput = s_apszStripFilenameTests[i];
248 const char *pszExpect = s_apszStripFilenameTests[i + 1];
249 char szPath[RTPATH_MAX];
250 strcpy(szPath, pszInput);
251 RTPathStripFilename(szPath);
252 if (strcmp(szPath, pszExpect))
253 {
254 RTPrintf("tstPath: RTPathStripFilename failed!\n"
255 " input: '%s'\n"
256 " output: '%s'\n"
257 "expected: '%s'\n",
258 pszInput, szPath, pszExpect);
259 cErrors++;
260 }
261 }
262
263 /*
264 * Summary.
265 */
266 if (!cErrors)
267 RTPrintf("tstPath: SUCCESS\n");
268 else
269 RTPrintf("tstPath: FAILURE %d errors\n", cErrors);
270 return !!cErrors;
271}
272
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