1 | /* $Id: tstRTPath.cpp 26824 2010-02-26 10:36:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Test various path functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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 |
|
---|
45 | int main()
|
---|
46 | {
|
---|
47 | char szPath[RTPATH_MAX];
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Init RT+Test.
|
---|
51 | */
|
---|
52 | RTTEST hTest;
|
---|
53 | int rc = RTTestInitAndCreate("tstRTPath", &hTest);
|
---|
54 | if (rc)
|
---|
55 | return rc;
|
---|
56 | RTTestBanner(hTest);
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * RTPathExecDir, RTPathUserHome and RTProcGetExecutableName.
|
---|
60 | */
|
---|
61 | RTTestSub(hTest, "RTPathExecDir");
|
---|
62 | RTTESTI_CHECK_RC(RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS);
|
---|
63 | if (RT_SUCCESS(rc))
|
---|
64 | RTTestIPrintf(RTTESTLVL_INFO, "ExecDir={%s}\n", szPath);
|
---|
65 |
|
---|
66 | RTTestSub(hTest, "RTProcGetExecutableName");
|
---|
67 | if (RTProcGetExecutableName(szPath, sizeof(szPath)) == szPath)
|
---|
68 | RTTestIPrintf(RTTESTLVL_INFO, "ExecutableName={%s}\n", szPath);
|
---|
69 | else
|
---|
70 | RTTestIFailed("RTProcGetExecutableName -> NULL");
|
---|
71 |
|
---|
72 | RTTestSub(hTest, "RTPathUserHome");
|
---|
73 | RTTESTI_CHECK_RC(RTPathUserHome(szPath, sizeof(szPath)), VINF_SUCCESS);
|
---|
74 | if (RT_SUCCESS(rc))
|
---|
75 | RTTestIPrintf(RTTESTLVL_INFO, "UserHome={%s}\n", szPath);
|
---|
76 |
|
---|
77 | RTTestSub(hTest, "RTPathTemp");
|
---|
78 | RTTESTI_CHECK_RC(RTPathTemp(szPath, sizeof(szPath)), VINF_SUCCESS);
|
---|
79 | if (RT_SUCCESS(rc))
|
---|
80 | RTTestIPrintf(RTTESTLVL_INFO, "PathTemp={%s}\n", szPath);
|
---|
81 | size_t cch = strlen(szPath);
|
---|
82 | RTTESTI_CHECK_RC(RTPathTemp(szPath, cch), VERR_BUFFER_OVERFLOW);
|
---|
83 | RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+1), VINF_SUCCESS);
|
---|
84 | RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+2), VINF_SUCCESS);
|
---|
85 |
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * RTPathAbsEx
|
---|
89 | */
|
---|
90 | RTTestSub(hTest, "RTPathAbsEx");
|
---|
91 | static const struct
|
---|
92 | {
|
---|
93 | const char *pcszInputBase;
|
---|
94 | const char *pcszInputPath;
|
---|
95 | int rc;
|
---|
96 | const char *pcszOutput;
|
---|
97 | }
|
---|
98 | s_aRTPathAbsExTests[] =
|
---|
99 | {
|
---|
100 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
101 | { NULL, "", VERR_INVALID_PARAMETER, NULL },
|
---|
102 | { NULL, ".", VINF_SUCCESS, "%p" },
|
---|
103 | { NULL, "\\", VINF_SUCCESS, "%d\\" },
|
---|
104 | { NULL, "\\..", VINF_SUCCESS, "%d\\" },
|
---|
105 | { NULL, "/absolute/..", VINF_SUCCESS, "%d\\" },
|
---|
106 | { NULL, "/absolute\\\\../..", VINF_SUCCESS, "%d\\" },
|
---|
107 | { NULL, "/absolute//../path\\", VINF_SUCCESS, "%d\\path" },
|
---|
108 | { NULL, "/absolute/../../path", VINF_SUCCESS, "%d\\path" },
|
---|
109 | { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p\\dir\\file.txt" },
|
---|
110 | { NULL, "\\data\\", VINF_SUCCESS, "%d\\data" },
|
---|
111 | { "relative_base/dir\\", "\\from_root", VINF_SUCCESS, "%d\\from_root" },
|
---|
112 | { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p\\relative_base\\dir\\relative_also" },
|
---|
113 | #else
|
---|
114 | { NULL, "", VERR_INVALID_PARAMETER, NULL },
|
---|
115 | { NULL, ".", VINF_SUCCESS, "%p" },
|
---|
116 | { NULL, "/", VINF_SUCCESS, "/" },
|
---|
117 | { NULL, "/..", VINF_SUCCESS, "/" },
|
---|
118 | { NULL, "/absolute/..", VINF_SUCCESS, "/" },
|
---|
119 | { NULL, "/absolute\\\\../..", VINF_SUCCESS, "/" },
|
---|
120 | { NULL, "/absolute//../path/", VINF_SUCCESS, "/path" },
|
---|
121 | { NULL, "/absolute/../../path", VINF_SUCCESS, "/path" },
|
---|
122 | { NULL, "relative/../dir/./././file.txt", VINF_SUCCESS, "%p/dir/file.txt" },
|
---|
123 | { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p/dir\\.\\.\\.\\file.txt" }, /* linux-specific */
|
---|
124 | { NULL, "/data/", VINF_SUCCESS, "/data" },
|
---|
125 | { "relative_base/dir/", "/from_root", VINF_SUCCESS, "/from_root" },
|
---|
126 | { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p/relative_base/dir/relative_also" },
|
---|
127 | #endif
|
---|
128 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
129 | { NULL, "C:\\", VINF_SUCCESS, "C:\\" },
|
---|
130 | { "C:\\", "..", VINF_SUCCESS, "C:\\" },
|
---|
131 | { "C:\\temp", "..", VINF_SUCCESS, "C:\\" },
|
---|
132 | { "C:\\VirtualBox/Machines", "..\\VirtualBox.xml", VINF_SUCCESS, "C:\\VirtualBox\\VirtualBox.xml" },
|
---|
133 | { "C:\\MustDie", "\\from_root/dir/..", VINF_SUCCESS, "C:\\from_root" },
|
---|
134 | { "C:\\temp", "D:\\data", VINF_SUCCESS, "D:\\data" },
|
---|
135 | { NULL, "\\\\server\\..\\share", VINF_SUCCESS, "\\\\server\\..\\share" /* kind of strange */ },
|
---|
136 | { NULL, "\\\\server/", VINF_SUCCESS, "\\\\server" },
|
---|
137 | { NULL, "\\\\", VINF_SUCCESS, "\\\\" },
|
---|
138 | { NULL, "\\\\\\something", VINF_SUCCESS, "\\\\\\something" /* kind of strange */ },
|
---|
139 | { "\\\\server\\share_as_base", "/from_root", VINF_SUCCESS, "\\\\server\\from_root" },
|
---|
140 | { "\\\\just_server", "/from_root", VINF_SUCCESS, "\\\\just_server\\from_root" },
|
---|
141 | { "\\\\server\\share_as_base", "relative\\data", VINF_SUCCESS, "\\\\server\\share_as_base\\relative\\data" },
|
---|
142 | { "base", "\\\\?\\UNC\\relative/edwef/..", VINF_SUCCESS, "\\\\?\\UNC\\relative" },
|
---|
143 | { "\\\\?\\UNC\\base", "/from_root", VERR_INVALID_NAME, NULL },
|
---|
144 | #else
|
---|
145 | { "/temp", "..", VINF_SUCCESS, "/" },
|
---|
146 | { "/VirtualBox/Machines", "../VirtualBox.xml", VINF_SUCCESS, "/VirtualBox/VirtualBox.xml" },
|
---|
147 | { "/MustDie", "/from_root/dir/..", VINF_SUCCESS, "/from_root" },
|
---|
148 | { "\\temp", "\\data", VINF_SUCCESS, "%p/\\temp/\\data" },
|
---|
149 | #endif
|
---|
150 | };
|
---|
151 |
|
---|
152 | for (unsigned i = 0; i < RT_ELEMENTS(s_aRTPathAbsExTests); ++ i)
|
---|
153 | {
|
---|
154 | rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase,
|
---|
155 | s_aRTPathAbsExTests[i].pcszInputPath,
|
---|
156 | szPath, sizeof(szPath));
|
---|
157 | if (rc != s_aRTPathAbsExTests[i].rc)
|
---|
158 | {
|
---|
159 | RTTestIFailed("unexpected result code!\n"
|
---|
160 | " input base: '%s'\n"
|
---|
161 | " input path: '%s'\n"
|
---|
162 | " output: '%s'\n"
|
---|
163 | " rc: %Rrc\n"
|
---|
164 | " expected rc: %Rrc",
|
---|
165 | s_aRTPathAbsExTests[i].pcszInputBase,
|
---|
166 | s_aRTPathAbsExTests[i].pcszInputPath,
|
---|
167 | szPath, rc,
|
---|
168 | s_aRTPathAbsExTests[i].rc);
|
---|
169 | continue;
|
---|
170 | }
|
---|
171 |
|
---|
172 | char szTmp[RTPATH_MAX];
|
---|
173 | char *pszExpected = NULL;
|
---|
174 | if (s_aRTPathAbsExTests[i].pcszOutput != NULL)
|
---|
175 | {
|
---|
176 | if (s_aRTPathAbsExTests[i].pcszOutput[0] == '%')
|
---|
177 | {
|
---|
178 | RTTESTI_CHECK_RC(rc = RTPathGetCurrent(szTmp, sizeof(szTmp)), VINF_SUCCESS);
|
---|
179 | if (RT_FAILURE(rc))
|
---|
180 | break;
|
---|
181 |
|
---|
182 | pszExpected = szTmp;
|
---|
183 |
|
---|
184 | if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'p')
|
---|
185 | {
|
---|
186 | cch = strlen(szTmp);
|
---|
187 | if (cch + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
|
---|
188 | strcpy(szTmp + cch, s_aRTPathAbsExTests[i].pcszOutput + 2);
|
---|
189 | }
|
---|
190 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
191 | else if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'd')
|
---|
192 | {
|
---|
193 | if (2 + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp))
|
---|
194 | strcpy(szTmp + 2, s_aRTPathAbsExTests[i].pcszOutput + 2);
|
---|
195 | }
|
---|
196 | #endif
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | strcpy(szTmp, s_aRTPathAbsExTests[i].pcszOutput);
|
---|
201 | pszExpected = szTmp;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (strcmp(szPath, pszExpected))
|
---|
205 | {
|
---|
206 | RTTestIFailed("Unexpected result\n"
|
---|
207 | " input base: '%s'\n"
|
---|
208 | " input path: '%s'\n"
|
---|
209 | " output: '%s'\n"
|
---|
210 | " expected: '%s'",
|
---|
211 | s_aRTPathAbsExTests[i].pcszInputBase,
|
---|
212 | s_aRTPathAbsExTests[i].pcszInputPath,
|
---|
213 | szPath,
|
---|
214 | s_aRTPathAbsExTests[i].pcszOutput);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * RTPathStripFilename
|
---|
221 | */
|
---|
222 | RTTestSub(hTest, "RTPathStripFilename");
|
---|
223 | static const char *s_apszStripFilenameTests[] =
|
---|
224 | {
|
---|
225 | "/usr/include///", "/usr/include//",
|
---|
226 | "/usr/include/", "/usr/include",
|
---|
227 | "/usr/include", "/usr",
|
---|
228 | "/usr", "/",
|
---|
229 | "usr", ".",
|
---|
230 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
231 | "c:/windows", "c:/",
|
---|
232 | "c:/", "c:/",
|
---|
233 | "D:", "D:",
|
---|
234 | "C:\\OS2\\DLLS", "C:\\OS2",
|
---|
235 | #endif
|
---|
236 | };
|
---|
237 | for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripFilenameTests); i += 2)
|
---|
238 | {
|
---|
239 | const char *pszInput = s_apszStripFilenameTests[i];
|
---|
240 | const char *pszExpect = s_apszStripFilenameTests[i + 1];
|
---|
241 | strcpy(szPath, pszInput);
|
---|
242 | RTPathStripFilename(szPath);
|
---|
243 | if (strcmp(szPath, pszExpect))
|
---|
244 | {
|
---|
245 | RTTestIFailed("Unexpected result\n"
|
---|
246 | " input: '%s'\n"
|
---|
247 | " output: '%s'\n"
|
---|
248 | "expected: '%s'",
|
---|
249 | pszInput, szPath, pszExpect);
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * RTPathAppend.
|
---|
255 | */
|
---|
256 | RTTestSub(hTest, "RTPathAppend");
|
---|
257 | static const char *s_apszAppendTests[] =
|
---|
258 | {
|
---|
259 | /* base append result */
|
---|
260 | "/", "", "/",
|
---|
261 | "", "/", "/",
|
---|
262 | "/", "/", "/",
|
---|
263 | "/x", "", "/x",
|
---|
264 | "/x", "/", "/x/",
|
---|
265 | "/", "x", "/x",
|
---|
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 | "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 | "/bin/testcase", "foo.r0", "/bin/testcase/foo.r0",
|
---|
278 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
279 | "/", "\\", "/",
|
---|
280 | "\\", "/", "\\",
|
---|
281 | "\\\\srv\\shr", "dir//", "\\\\srv\\shr/dir//",
|
---|
282 | "\\\\srv\\shr", "dir//file", "\\\\srv\\shr/dir//file",
|
---|
283 | "\\\\srv\\shr", "//dir//", "\\\\srv\\shr/dir//",
|
---|
284 | "\\\\srv\\shr", "/\\dir//", "\\\\srv\\shr\\dir//",
|
---|
285 | "\\\\", "not-srv/not-shr/file", "\\not-srv/not-shr/file",
|
---|
286 | "C:", "autoexec.bat", "C:autoexec.bat",
|
---|
287 | "C:", "/autoexec.bat", "C:/autoexec.bat",
|
---|
288 | "C:", "\\autoexec.bat", "C:\\autoexec.bat",
|
---|
289 | "C:\\", "/autoexec.bat", "C:\\autoexec.bat",
|
---|
290 | "C:\\\\", "autoexec.bat", "C:\\autoexec.bat",
|
---|
291 | "E:\\bin\\testcase", "foo.r0", "E:\\bin\\testcase/foo.r0",
|
---|
292 | #endif
|
---|
293 | };
|
---|
294 | for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
|
---|
295 | {
|
---|
296 | const char *pszInput = s_apszAppendTests[i];
|
---|
297 | const char *pszAppend = s_apszAppendTests[i + 1];
|
---|
298 | const char *pszExpect = s_apszAppendTests[i + 2];
|
---|
299 | strcpy(szPath, pszInput);
|
---|
300 | RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, sizeof(szPath), pszAppend), VINF_SUCCESS);
|
---|
301 | if (RT_FAILURE(rc))
|
---|
302 | continue;
|
---|
303 | if (strcmp(szPath, pszExpect))
|
---|
304 | {
|
---|
305 | RTTestIFailed("Unexpected result\n"
|
---|
306 | " input: '%s'\n"
|
---|
307 | " append: '%s'\n"
|
---|
308 | " output: '%s'\n"
|
---|
309 | "expected: '%s'",
|
---|
310 | pszInput, pszAppend, szPath, pszExpect);
|
---|
311 | }
|
---|
312 | else
|
---|
313 | {
|
---|
314 | size_t const cchResult = strlen(szPath);
|
---|
315 |
|
---|
316 | strcpy(szPath, pszInput);
|
---|
317 | RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 2, pszAppend), VINF_SUCCESS);
|
---|
318 | RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
|
---|
319 |
|
---|
320 | strcpy(szPath, pszInput);
|
---|
321 | RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 1, pszAppend), VINF_SUCCESS);
|
---|
322 | RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
|
---|
323 |
|
---|
324 | if (strlen(pszInput) < cchResult)
|
---|
325 | {
|
---|
326 | strcpy(szPath, pszInput);
|
---|
327 | RTTESTI_CHECK_RC(RTPathAppend(szPath, cchResult, pszAppend), VERR_BUFFER_OVERFLOW);
|
---|
328 | }
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | /*
|
---|
333 | * RTPathJoin - reuse the append tests.
|
---|
334 | */
|
---|
335 | RTTestSub(hTest, "RTPathJoin");
|
---|
336 | for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3)
|
---|
337 | {
|
---|
338 | const char *pszInput = s_apszAppendTests[i];
|
---|
339 | const char *pszAppend = s_apszAppendTests[i + 1];
|
---|
340 | const char *pszExpect = s_apszAppendTests[i + 2];
|
---|
341 |
|
---|
342 | memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
|
---|
343 |
|
---|
344 | RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, sizeof(szPath), pszInput, pszAppend), VINF_SUCCESS);
|
---|
345 | if (RT_FAILURE(rc))
|
---|
346 | continue;
|
---|
347 | if (strcmp(szPath, pszExpect))
|
---|
348 | {
|
---|
349 | RTTestIFailed("Unexpected result\n"
|
---|
350 | " input: '%s'\n"
|
---|
351 | " append: '%s'\n"
|
---|
352 | " output: '%s'\n"
|
---|
353 | "expected: '%s'",
|
---|
354 | pszInput, pszAppend, szPath, pszExpect);
|
---|
355 | }
|
---|
356 | else
|
---|
357 | {
|
---|
358 | size_t const cchResult = strlen(szPath);
|
---|
359 |
|
---|
360 | memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
|
---|
361 | RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult + 2, pszInput, pszAppend), VINF_SUCCESS);
|
---|
362 | RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
|
---|
363 |
|
---|
364 | memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0';
|
---|
365 | RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult + 1, pszInput, pszAppend), VINF_SUCCESS);
|
---|
366 | RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect));
|
---|
367 |
|
---|
368 | RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult, pszInput, pszAppend), VERR_BUFFER_OVERFLOW);
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | /*
|
---|
373 | * RTPathStripTrailingSlash
|
---|
374 | */
|
---|
375 | static const char *s_apszStripTrailingSlash[] =
|
---|
376 | {
|
---|
377 | /* input result */
|
---|
378 | "/", "/",
|
---|
379 | "//", "/",
|
---|
380 | "////////////////////", "/",
|
---|
381 | "/tmp", "/tmp",
|
---|
382 | "/tmp////////////////", "/tmp",
|
---|
383 | "tmp", "tmp",
|
---|
384 | "tmp////////////////", "tmp",
|
---|
385 | "./", ".",
|
---|
386 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
387 | "////////////////////", "/",
|
---|
388 | "D:", "D:",
|
---|
389 | "D:/", "D:/",
|
---|
390 | "D:\\", "D:\\",
|
---|
391 | "D:\\/\\", "D:\\",
|
---|
392 | "D:/\\/\\", "D:/",
|
---|
393 | "C:/Temp", "D:/Temp",
|
---|
394 | "C:/Temp/", "D:/Temp/",
|
---|
395 | "C:/Temp\\/", "D:/Temp",
|
---|
396 | #endif
|
---|
397 | };
|
---|
398 | for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripTrailingSlash); i += 2)
|
---|
399 | {
|
---|
400 | const char *pszInput = s_apszStripTrailingSlash[i];
|
---|
401 | const char *pszExpect = s_apszStripTrailingSlash[i + 1];
|
---|
402 |
|
---|
403 | strcpy(szPath, pszInput);
|
---|
404 | cch = RTPathStripTrailingSlash(szPath);
|
---|
405 | if (strcmp(szPath, pszExpect))
|
---|
406 | RTTestIFailed("Unexpected result\n"
|
---|
407 | " input: '%s'\n"
|
---|
408 | " output: '%s'\n"
|
---|
409 | "expected: '%s'",
|
---|
410 | pszInput, szPath, pszExpect);
|
---|
411 | else
|
---|
412 | RTTESTI_CHECK(cch == strlen(szPath));
|
---|
413 | }
|
---|
414 |
|
---|
415 | /*
|
---|
416 | * RTPathCountComponents
|
---|
417 | */
|
---|
418 | RTTestSub(hTest, "RTPathCountComponents");
|
---|
419 | RTTESTI_CHECK(RTPathCountComponents("") == 0);
|
---|
420 | RTTESTI_CHECK(RTPathCountComponents("/") == 1);
|
---|
421 | RTTESTI_CHECK(RTPathCountComponents("//") == 1);
|
---|
422 | RTTESTI_CHECK(RTPathCountComponents("//////////////") == 1);
|
---|
423 | RTTESTI_CHECK(RTPathCountComponents("//////////////bin") == 2);
|
---|
424 | RTTESTI_CHECK(RTPathCountComponents("//////////////bin/") == 2);
|
---|
425 | RTTESTI_CHECK(RTPathCountComponents("//////////////bin/////") == 2);
|
---|
426 | RTTESTI_CHECK(RTPathCountComponents("..") == 1);
|
---|
427 | RTTESTI_CHECK(RTPathCountComponents("../") == 1);
|
---|
428 | RTTESTI_CHECK(RTPathCountComponents("../..") == 2);
|
---|
429 | RTTESTI_CHECK(RTPathCountComponents("../../") == 2);
|
---|
430 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
431 | RTTESTI_CHECK(RTPathCountComponents("d:") == 1);
|
---|
432 | RTTESTI_CHECK(RTPathCountComponents("d:/") == 1);
|
---|
433 | RTTESTI_CHECK(RTPathCountComponents("d:/\\") == 1);
|
---|
434 | RTTESTI_CHECK(RTPathCountComponents("d:\\") == 1);
|
---|
435 | RTTESTI_CHECK(RTPathCountComponents("c:\\config.sys") == 2);
|
---|
436 | RTTESTI_CHECK(RTPathCountComponents("c:\\windows") == 2);
|
---|
437 | RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\") == 2);
|
---|
438 | RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\system32") == 3);
|
---|
439 | RTTESTI_CHECK(RTPathCountComponents("//./C$") == 1);
|
---|
440 | RTTESTI_CHECK(RTPathCountComponents("\\\\.\\C$") == 1);
|
---|
441 | RTTESTI_CHECK(RTPathCountComponents("/\\.\\C$") == 1);
|
---|
442 | RTTESTI_CHECK(RTPathCountComponents("//myserver") == 1);
|
---|
443 | RTTESTI_CHECK(RTPathCountComponents("//myserver/") == 1);
|
---|
444 | RTTESTI_CHECK(RTPathCountComponents("//myserver/share") == 1);
|
---|
445 | RTTESTI_CHECK(RTPathCountComponents("//myserver/share/") == 1);
|
---|
446 | RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\") == 1);
|
---|
447 | RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x") == 2);
|
---|
448 | RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y") == 3);
|
---|
449 | RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y\\") == 3);
|
---|
450 | #endif
|
---|
451 |
|
---|
452 | /*
|
---|
453 | * RTPathCopyComponents
|
---|
454 | */
|
---|
455 | struct
|
---|
456 | {
|
---|
457 | const char *pszSrc;
|
---|
458 | size_t cComponents;
|
---|
459 | const char *pszResult;
|
---|
460 | } s_aCopyComponents[] =
|
---|
461 | {
|
---|
462 | { "", 0, "" },
|
---|
463 | { "", 5, "" },
|
---|
464 | { "/", 0, "" },
|
---|
465 | { "/", 1, "/" },
|
---|
466 | { "/", 2, "/" },
|
---|
467 | { "/usr/bin/sed", 0, "" },
|
---|
468 | { "/usr/bin/sed", 1, "/" },
|
---|
469 | { "/usr/bin/sed", 2, "/usr/" },
|
---|
470 | { "/usr/bin/sed", 3, "/usr/bin/" },
|
---|
471 | { "/usr/bin/sed", 4, "/usr/bin/sed" },
|
---|
472 | { "/usr/bin/sed", 5, "/usr/bin/sed" },
|
---|
473 | { "/usr/bin/sed", 6, "/usr/bin/sed" },
|
---|
474 | { "/usr///bin/sed", 2, "/usr///" },
|
---|
475 | };
|
---|
476 | for (unsigned i = 0; i < RT_ELEMENTS(s_aCopyComponents); i++)
|
---|
477 | {
|
---|
478 | const char *pszInput = s_aCopyComponents[i].pszSrc;
|
---|
479 | size_t cComponents = s_aCopyComponents[i].cComponents;
|
---|
480 | const char *pszResult = s_aCopyComponents[i].pszResult;
|
---|
481 |
|
---|
482 | memset(szPath, 'a', sizeof(szPath));
|
---|
483 | rc = RTPathCopyComponents(szPath, sizeof(szPath), pszInput, cComponents);
|
---|
484 | RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
|
---|
485 | if (RT_SUCCESS(rc) && strcmp(szPath, pszResult))
|
---|
486 | RTTestIFailed("Unexpected result\n"
|
---|
487 | " input: '%s' cComponents=%u\n"
|
---|
488 | " output: '%s'\n"
|
---|
489 | "expected: '%s'",
|
---|
490 | pszInput, cComponents, szPath, pszResult);
|
---|
491 | else if (RT_SUCCESS(rc))
|
---|
492 | {
|
---|
493 | RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult) + 1, pszInput, cComponents), VINF_SUCCESS);
|
---|
494 | RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult), pszInput, cComponents), VERR_BUFFER_OVERFLOW);
|
---|
495 | }
|
---|
496 | }
|
---|
497 |
|
---|
498 | /*
|
---|
499 | * Summary.
|
---|
500 | */
|
---|
501 | return RTTestSummaryAndDestroy(hTest);
|
---|
502 | }
|
---|
503 |
|
---|