1 | /* $Id: tstPath.cpp 14324 2008-11-18 19:09:34Z 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 | #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 |
|
---|
43 | #define CHECK_RC(method) \
|
---|
44 | do { \
|
---|
45 | rc = method; \
|
---|
46 | if (RT_FAILURE(rc)) \
|
---|
47 | { \
|
---|
48 | cErrors++; \
|
---|
49 | RTPrintf("\ntstPath: FAILED calling " #method " at line %d: rc=%Rrc\n", __LINE__, rc); \
|
---|
50 | } \
|
---|
51 | } while (0)
|
---|
52 |
|
---|
53 | int main()
|
---|
54 | {
|
---|
55 | /*
|
---|
56 | * Init RT.
|
---|
57 | */
|
---|
58 | int rc;
|
---|
59 | int cErrors = 0;
|
---|
60 | CHECK_RC(RTR3Init());
|
---|
61 | if (RT_FAILURE(rc))
|
---|
62 | return 1;
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * RTPathProgram, RTPathUserHome and RTProcGetExecutableName.
|
---|
66 | */
|
---|
67 | char szPath[RTPATH_MAX];
|
---|
68 | CHECK_RC(RTPathProgram(szPath, sizeof(szPath)));
|
---|
69 | if (RT_SUCCESS(rc))
|
---|
70 | RTPrintf("Program={%s}\n", szPath);
|
---|
71 | CHECK_RC(RTPathUserHome(szPath, sizeof(szPath)));
|
---|
72 | if (RT_SUCCESS(rc))
|
---|
73 | RTPrintf("UserHome={%s}\n", szPath);
|
---|
74 | if (RTProcGetExecutableName(szPath, sizeof(szPath)) == szPath)
|
---|
75 | RTPrintf("ExecutableName={%s}\n", szPath);
|
---|
76 | else
|
---|
77 | {
|
---|
78 | RTPrintf("tstPath: FAILED - RTProcGetExecutableName\n");
|
---|
79 | cErrors++;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * RTPathAbsEx
|
---|
85 | */
|
---|
86 | RTPrintf("tstPath: TESTING RTPathAbsEx()\n");
|
---|
87 | static const char *aInput[] =
|
---|
88 | {
|
---|
89 | // NULL, NULL, -- assertion in RTStrToUtf16
|
---|
90 | NULL, "/absolute/..",
|
---|
91 | NULL, "/absolute\\\\../..",
|
---|
92 | NULL, "/absolute//../path",
|
---|
93 | NULL, "/absolute/../../path",
|
---|
94 | NULL, "relative/../dir\\.\\.\\.\\file.txt",
|
---|
95 | NULL, "\\",
|
---|
96 | "relative_base/dir\\", "\\from_root",
|
---|
97 | "relative_base/dir/", "relative_also",
|
---|
98 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
99 | NULL, "C:\\",
|
---|
100 | "C:\\", "..",
|
---|
101 | "C:\\temp", "..",
|
---|
102 | "C:\\VirtualBox/Machines", "..\\VirtualBox.xml",
|
---|
103 | "C:\\MustDie", "\\from_root/dir/..",
|
---|
104 | "C:\\temp", "D:\\data",
|
---|
105 | NULL, "\\\\server\\../share", // -- on Win32, GetFullPathName doesn't remove .. here
|
---|
106 | /* the three below use cases should fail with VERR_INVALID_NAME */
|
---|
107 | //NULL, "\\\\server",
|
---|
108 | //NULL, "\\\\",
|
---|
109 | //NULL, "\\\\\\something",
|
---|
110 | "\\\\server\\share_as_base", "/from_root",
|
---|
111 | "\\\\just_server", "/from_root",
|
---|
112 | "\\\\server\\share_as_base", "relative\\data",
|
---|
113 | "base", "\\\\?\\UNC\\relative/edwef/..",
|
---|
114 | "base", "\\\\?\\UNC\\relative/edwef/..",
|
---|
115 | /* this is not (and I guess should not be) supported, should fail */
|
---|
116 | ///@todo "\\\\?\\UNC\\base", "/from_root",
|
---|
117 | #else
|
---|
118 | "\\temp", "..",
|
---|
119 | "\\VirtualBox/Machines", "..\\VirtualBox.xml",
|
---|
120 | "\\MustDie", "\\from_root/dir/..",
|
---|
121 | "\\temp", "\\data",
|
---|
122 | #endif
|
---|
123 | };
|
---|
124 |
|
---|
125 | for (unsigned i = 0; i < RT_ELEMENTS(aInput); i += 2)
|
---|
126 | {
|
---|
127 | RTPrintf("tstPath: base={%s}, path={%s}, ", aInput[i], aInput[i + 1]);
|
---|
128 | CHECK_RC(RTPathAbsEx(aInput[i], aInput[i + 1], szPath, sizeof(szPath)));
|
---|
129 | if (RT_SUCCESS(rc))
|
---|
130 | RTPrintf("abs={%s}\n", szPath);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * RTPathStripFilename
|
---|
135 | */
|
---|
136 | RTPrintf("tstPath: RTPathStripFilename...\n");
|
---|
137 | static const char *apszStripFilenameTests[] =
|
---|
138 | {
|
---|
139 | "/usr/include///", "/usr/include//",
|
---|
140 | "/usr/include/", "/usr/include",
|
---|
141 | "/usr/include", "/usr",
|
---|
142 | "/usr", "/",
|
---|
143 | "usr", ".",
|
---|
144 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
145 | "c:/windows", "c:/",
|
---|
146 | "c:/", "c:/",
|
---|
147 | "D:", "D:",
|
---|
148 | "C:\\OS2\\DLLS", "C:\\OS2",
|
---|
149 | #endif
|
---|
150 | };
|
---|
151 | for (unsigned i = 0; i < RT_ELEMENTS(apszStripFilenameTests); i += 2)
|
---|
152 | {
|
---|
153 | const char *pszInput = apszStripFilenameTests[i];
|
---|
154 | const char *pszExpect = apszStripFilenameTests[i + 1];
|
---|
155 | char szPath[RTPATH_MAX];
|
---|
156 | strcpy(szPath, pszInput);
|
---|
157 | RTPathStripFilename(szPath);
|
---|
158 | if (strcmp(szPath, pszExpect))
|
---|
159 | {
|
---|
160 | RTPrintf("tstPath: RTPathStripFilename failed!\n"
|
---|
161 | " input: '%s'\n"
|
---|
162 | " output: '%s'\n"
|
---|
163 | "expected: '%s'\n",
|
---|
164 | pszInput, szPath, pszExpect);
|
---|
165 | cErrors++;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * Summary.
|
---|
171 | */
|
---|
172 | if (!cErrors)
|
---|
173 | RTPrintf("tstPath: SUCCESS\n");
|
---|
174 | else
|
---|
175 | RTPrintf("tstPath: FAILURE %d errors\n", cErrors);
|
---|
176 | return !!cErrors;
|
---|
177 | }
|
---|
178 |
|
---|