1 | /* $Id: tstPath.cpp 15754 2008-12-25 10:52:24Z 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.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 | * 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 |
|
---|
59 | int 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 | 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, "/" },
|
---|
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(aRTPathAbsExTests); ++ i)
|
---|
155 | {
|
---|
156 | rc = RTPathAbsEx(aRTPathAbsExTests[i].pcszInputBase,
|
---|
157 | aRTPathAbsExTests[i].pcszInputPath,
|
---|
158 | szPath, sizeof(szPath));
|
---|
159 | if (rc != 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 | aRTPathAbsExTests[i].pcszInputBase,
|
---|
168 | aRTPathAbsExTests[i].pcszInputPath,
|
---|
169 | szPath, rc,
|
---|
170 | aRTPathAbsExTests[i].rc);
|
---|
171 | cErrors++;
|
---|
172 | continue;
|
---|
173 | }
|
---|
174 |
|
---|
175 | char szTmp[RTPATH_MAX];
|
---|
176 | char *pszExpected = NULL;
|
---|
177 | if (aRTPathAbsExTests[i].pcszOutput != NULL)
|
---|
178 | {
|
---|
179 | if (aRTPathAbsExTests[i].pcszOutput[0] == '%')
|
---|
180 | {
|
---|
181 | if (getcwd(szTmp, sizeof(szTmp)) == NULL)
|
---|
182 | {
|
---|
183 | RTPrintf("tstPath: getcwd failed with errno=%d!\n", errno);
|
---|
184 | cErrors++;
|
---|
185 | break;
|
---|
186 | }
|
---|
187 |
|
---|
188 | pszExpected = szTmp;
|
---|
189 |
|
---|
190 | if (aRTPathAbsExTests[i].pcszOutput[1] == 'p')
|
---|
191 | {
|
---|
192 | size_t cch = strlen(szTmp);
|
---|
193 | if (cch + strlen (aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
|
---|
194 | strcpy (szTmp + cch, aRTPathAbsExTests[i].pcszOutput + 2);
|
---|
195 | }
|
---|
196 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
197 | else if (aRTPathAbsExTests[i].pcszOutput[1] == 'd')
|
---|
198 | {
|
---|
199 | if (2 + strlen (aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
|
---|
200 | strcpy (szTmp + 2, aRTPathAbsExTests[i].pcszOutput + 2);
|
---|
201 | }
|
---|
202 | #endif
|
---|
203 | }
|
---|
204 | else
|
---|
205 | {
|
---|
206 | strcpy(szTmp, aRTPathAbsExTests[i].pcszOutput);
|
---|
207 | pszExpected = szTmp;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (strcmp(szPath, pszExpected))
|
---|
211 | {
|
---|
212 | RTPrintf("tstPath: RTPathAbsEx failed!\n"
|
---|
213 | " input base: '%s'\n"
|
---|
214 | " input path: '%s'\n"
|
---|
215 | " output: '%s'\n"
|
---|
216 | " expected: '%s'\n",
|
---|
217 | aRTPathAbsExTests[i].pcszInputBase,
|
---|
218 | aRTPathAbsExTests[i].pcszInputPath,
|
---|
219 | szPath,
|
---|
220 | aRTPathAbsExTests[i].pcszOutput);
|
---|
221 | cErrors++;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * RTPathStripFilename
|
---|
228 | */
|
---|
229 | RTPrintf("tstPath: RTPathStripFilename...\n");
|
---|
230 | static const char *apszStripFilenameTests[] =
|
---|
231 | {
|
---|
232 | "/usr/include///", "/usr/include//",
|
---|
233 | "/usr/include/", "/usr/include",
|
---|
234 | "/usr/include", "/usr",
|
---|
235 | "/usr", "/",
|
---|
236 | "usr", ".",
|
---|
237 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
238 | "c:/windows", "c:/",
|
---|
239 | "c:/", "c:/",
|
---|
240 | "D:", "D:",
|
---|
241 | "C:\\OS2\\DLLS", "C:\\OS2",
|
---|
242 | #endif
|
---|
243 | };
|
---|
244 | for (unsigned i = 0; i < RT_ELEMENTS(apszStripFilenameTests); i += 2)
|
---|
245 | {
|
---|
246 | const char *pszInput = apszStripFilenameTests[i];
|
---|
247 | const char *pszExpect = apszStripFilenameTests[i + 1];
|
---|
248 | char szPath[RTPATH_MAX];
|
---|
249 | strcpy(szPath, pszInput);
|
---|
250 | RTPathStripFilename(szPath);
|
---|
251 | if (strcmp(szPath, pszExpect))
|
---|
252 | {
|
---|
253 | RTPrintf("tstPath: RTPathStripFilename failed!\n"
|
---|
254 | " input: '%s'\n"
|
---|
255 | " output: '%s'\n"
|
---|
256 | "expected: '%s'\n",
|
---|
257 | pszInput, szPath, pszExpect);
|
---|
258 | cErrors++;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * Summary.
|
---|
264 | */
|
---|
265 | if (!cErrors)
|
---|
266 | RTPrintf("tstPath: SUCCESS\n");
|
---|
267 | else
|
---|
268 | RTPrintf("tstPath: FAILURE %d errors\n", cErrors);
|
---|
269 | return !!cErrors;
|
---|
270 | }
|
---|
271 |
|
---|