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