1 | /* $Id: tstPath.cpp 13836 2008-11-05 02:42:54Z 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/err.h>
|
---|
39 | #include <iprt/param.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | #define CHECK_RC(method) \
|
---|
43 | do { \
|
---|
44 | rc = method; \
|
---|
45 | if (RT_FAILURE(rc)) \
|
---|
46 | { \
|
---|
47 | cErrors++; \
|
---|
48 | RTPrintf("\ntstPath: FAILED calling " #method " at line %d: rc=%Rrc\n", __LINE__, rc); \
|
---|
49 | } \
|
---|
50 | } while (0)
|
---|
51 |
|
---|
52 | int main()
|
---|
53 | {
|
---|
54 | /*
|
---|
55 | * Init RT.
|
---|
56 | */
|
---|
57 | int rc;
|
---|
58 | int cErrors = 0;
|
---|
59 | CHECK_RC(RTR3Init());
|
---|
60 | if (RT_FAILURE(rc))
|
---|
61 | return 1;
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * RTPathProgram, RTPathUserHome and RTProcGetExecutableName.
|
---|
65 | */
|
---|
66 | char szPath[RTPATH_MAX];
|
---|
67 | CHECK_RC(RTPathProgram(szPath, sizeof(szPath)));
|
---|
68 | if (RT_SUCCESS(rc))
|
---|
69 | RTPrintf("Program={%s}\n", szPath);
|
---|
70 | CHECK_RC(RTPathUserHome(szPath, sizeof(szPath)));
|
---|
71 | if (RT_SUCCESS(rc))
|
---|
72 | RTPrintf("UserHome={%s}\n", szPath);
|
---|
73 | if (RTProcGetExecutableName(szPath, sizeof(szPath)) == szPath)
|
---|
74 | RTPrintf("ExecutableName={%s}\n", szPath);
|
---|
75 | else
|
---|
76 | {
|
---|
77 | RTPrintf("tstPath: FAILED - RTProcGetExecutableName\n");
|
---|
78 | cErrors++;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * RTPathAbsEx
|
---|
84 | */
|
---|
85 | RTPrintf("tstPath: TESTING RTPathAbsEx()\n");
|
---|
86 | static const char *aInput[] =
|
---|
87 | {
|
---|
88 | // NULL, NULL, -- assertion in RTStrToUtf16
|
---|
89 | NULL, "/absolute/..",
|
---|
90 | NULL, "/absolute\\\\../..",
|
---|
91 | NULL, "/absolute//../path",
|
---|
92 | NULL, "/absolute/../../path",
|
---|
93 | NULL, "relative/../dir\\.\\.\\.\\file.txt",
|
---|
94 | NULL, "\\",
|
---|
95 | "relative_base/dir\\", "\\from_root",
|
---|
96 | "relative_base/dir/", "relative_also",
|
---|
97 | #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
|
---|
98 | NULL, "C:\\",
|
---|
99 | "C:\\", "..",
|
---|
100 | "C:\\temp", "..",
|
---|
101 | "C:\\VirtualBox/Machines", "..\\VirtualBox.xml",
|
---|
102 | "C:\\MustDie", "\\from_root/dir/..",
|
---|
103 | "C:\\temp", "D:\\data",
|
---|
104 | NULL, "\\\\server\\../share", // -- on Win32, GetFullPathName doesn't remove .. here
|
---|
105 | /* the three below use cases should fail with VERR_INVALID_NAME */
|
---|
106 | //NULL, "\\\\server",
|
---|
107 | //NULL, "\\\\",
|
---|
108 | //NULL, "\\\\\\something",
|
---|
109 | "\\\\server\\share_as_base", "/from_root",
|
---|
110 | "\\\\just_server", "/from_root",
|
---|
111 | "\\\\server\\share_as_base", "relative\\data",
|
---|
112 | "base", "\\\\?\\UNC\\relative/edwef/..",
|
---|
113 | "base", "\\\\?\\UNC\\relative/edwef/..",
|
---|
114 | /* this is not (and I guess should not be) supported, should fail */
|
---|
115 | ///@todo "\\\\?\\UNC\\base", "/from_root",
|
---|
116 | #else
|
---|
117 | "\\temp", "..",
|
---|
118 | "\\VirtualBox/Machines", "..\\VirtualBox.xml",
|
---|
119 | "\\MustDie", "\\from_root/dir/..",
|
---|
120 | "\\temp", "\\data",
|
---|
121 | #endif
|
---|
122 | };
|
---|
123 |
|
---|
124 | for (unsigned i = 0; i < RT_ELEMENTS(aInput); i += 2)
|
---|
125 | {
|
---|
126 | RTPrintf("tstPath: base={%s}, path={%s}, ", aInput[i], aInput[i + 1]);
|
---|
127 | CHECK_RC(RTPathAbsEx(aInput[i], aInput[i + 1], szPath, sizeof(szPath)));
|
---|
128 | if (RT_SUCCESS(rc))
|
---|
129 | RTPrintf("abs={%s}\n", szPath);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Summary.
|
---|
134 | */
|
---|
135 | if (!cErrors)
|
---|
136 | RTPrintf("tstTimer: SUCCESS\n");
|
---|
137 | else
|
---|
138 | RTPrintf("tstTimer: FAILURE %d errors\n", cErrors);
|
---|
139 | return !!cErrors;
|
---|
140 | }
|
---|
141 |
|
---|