VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstGuestCtrlPaths.cpp

Last change on this file was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 KB
Line 
1/* $Id */
2/** @file
3 * Guest Control path test cases.
4 */
5
6/*
7 * Copyright (C) 2022-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#define LOG_ENABLED
29#define LOG_GROUP LOG_GROUP_MAIN
30#include <VBox/log.h>
31
32#include "../include/GuestCtrlImplPrivate.h"
33
34using namespace com;
35
36#include <iprt/assert.h>
37#include <iprt/env.h>
38#include <iprt/rand.h>
39#include <iprt/stream.h>
40#include <iprt/test.h>
41
42
43DECLINLINE(void) tstPathBuildDestination(const Utf8Str &strSrcPath, PathStyle_T enmSrcPathStyle,
44 const Utf8Str &strDstPath, PathStyle_T enmDstPathStyle,
45 int rcExp, Utf8Str strPathExp)
46{
47 Utf8Str strDstPath2 = strDstPath;
48 int vrc = GuestPath::BuildDestinationPath(strSrcPath, enmSrcPathStyle, strDstPath2, enmDstPathStyle);
49 RTTESTI_CHECK_MSG_RETV(vrc == rcExp, ("Expected %Rrc, got %Rrc for '%s'\n", rcExp, vrc, strDstPath.c_str()));
50 RTTESTI_CHECK_MSG_RETV(strDstPath2 == strPathExp, ("Expected '%s', got '%s'\n", strPathExp.c_str(), strDstPath2.c_str()));
51}
52
53DECLINLINE(void) tstPathTranslate(Utf8Str strPath, PathStyle_T enmSrcPathStyle, PathStyle_T enmDstPathStyle, int rcExp, Utf8Str strPathExp)
54{
55 Utf8Str strPath2 = strPath;
56 int vrc = GuestPath::Translate(strPath2, enmSrcPathStyle, enmDstPathStyle);
57 RTTESTI_CHECK_MSG_RETV(vrc == rcExp, ("Expected %Rrc, got %Rrc for '%s'\n", rcExp, vrc, strPath.c_str()));
58 RTTESTI_CHECK_MSG_RETV(strPath2 == strPathExp, ("Expected '%s', got '%s'\n", strPathExp.c_str(), strPath2.c_str()));
59}
60
61int main()
62{
63 RTTEST hTest;
64 int vrc = RTTestInitAndCreate("tstGuestCtrlPaths", &hTest);
65 if (vrc)
66 return vrc;
67 RTTestBanner(hTest);
68
69 RTTestIPrintf(RTTESTLVL_DEBUG, "Initializing COM...\n");
70 HRESULT hrc = com::Initialize();
71 if (FAILED(hrc))
72 {
73 RTTestFailed(hTest, "Failed to initialize COM (%Rhrc)!\n", hrc);
74 return RTEXITCODE_FAILURE;
75 }
76
77 /* Don't let the assertions trigger here
78 * -- we rely on the return values in the test(s) below. */
79 RTAssertSetQuiet(true);
80
81 /*
82 * Path translation testing.
83 */
84 tstPathTranslate("", PathStyle_DOS, PathStyle_DOS, VINF_SUCCESS, "");
85
86 tstPathTranslate("foo", PathStyle_DOS, PathStyle_DOS, VINF_SUCCESS, "foo");
87 tstPathTranslate("foo", PathStyle_UNIX, PathStyle_UNIX, VINF_SUCCESS, "foo");
88 tstPathTranslate("foo", PathStyle_DOS, PathStyle_UNIX, VINF_SUCCESS, "foo");
89 tstPathTranslate("foo", PathStyle_UNIX, PathStyle_UNIX, VINF_SUCCESS, "foo");
90
91 tstPathTranslate("foo\\bar", PathStyle_DOS, PathStyle_DOS, VINF_SUCCESS, "foo\\bar");
92 tstPathTranslate("foo/bar", PathStyle_UNIX, PathStyle_UNIX, VINF_SUCCESS, "foo/bar");
93
94 tstPathTranslate("foo\\bar\\", PathStyle_DOS, PathStyle_DOS, VINF_SUCCESS, "foo\\bar\\");
95 tstPathTranslate("foo/bar/", PathStyle_UNIX, PathStyle_UNIX, VINF_SUCCESS, "foo/bar/");
96 /* Actually also allowed on Windows. */
97 tstPathTranslate("foo/bar/", PathStyle_DOS, PathStyle_UNIX, VINF_SUCCESS, "foo/bar/");
98
99 tstPathTranslate("foo\\bar\\BAZ", PathStyle_DOS, PathStyle_DOS, VINF_SUCCESS, "foo\\bar\\BAZ");
100 tstPathTranslate("foo/bar/BAZ", PathStyle_UNIX, PathStyle_UNIX, VINF_SUCCESS, "foo/bar/BAZ");
101
102 tstPathTranslate("foo\\bar\\dir with space\\", PathStyle_DOS, PathStyle_UNIX, VINF_SUCCESS, "foo/bar/dir with space/");
103 tstPathTranslate("foo/bar/dir with space/", PathStyle_UNIX, PathStyle_UNIX, VINF_SUCCESS, "foo/bar/dir with space/");
104
105#if 0
106 /** Do a mapping of "\", which marks an escape sequence for paths on UNIX-y OSes to DOS-based OSes (like Windows),
107 * however, on DOS "\" is a path separator. See @bugref{21095} */
108 tstPathTranslate("foo/bar/dir_with_escape_sequence\\ space", PathStyle_UNIX, PathStyle_UNIX, VINF_SUCCESS, "foo/bar/dir_with_escape_sequence\\ space");
109 tstPathTranslate("foo/bar/dir_with_escape_sequence\\ space", PathStyle_UNIX, PathStyle_DOS, VINF_SUCCESS, "foo\\bar\\dir_with_escape_sequence space");
110 tstPathTranslate("foo/bar/1_dir_with_escape_sequence/the\\ space", PathStyle_UNIX, PathStyle_DOS, VINF_SUCCESS, "foo\\bar\\1_dir_with_escape_sequence\\the space");
111 tstPathTranslate("foo/bar/2_dir_with_escape_sequence/the\\ \\ space", PathStyle_UNIX, PathStyle_DOS, VINF_SUCCESS, "foo\\bar\\2_dir_with_escape_sequence\\the space");
112 tstPathTranslate("foo/bar/dir_with_escape_sequence/spaces at end\\ \\ ", PathStyle_UNIX, PathStyle_DOS, VINF_SUCCESS, "foo\\bar\\dir_with_escape_sequence\\spaces at end ");
113#endif
114
115 /* Filter out double slashes (cosmetic only). */
116 tstPathTranslate("\\\\", PathStyle_DOS, PathStyle_DOS, VINF_SUCCESS, "\\");
117 tstPathTranslate("foo\\\\bar\\", PathStyle_DOS, PathStyle_DOS, VINF_SUCCESS, "foo\\bar\\");
118
119 /* Mixed slashes. */
120 tstPathTranslate("\\\\foo/bar\\\\baz", PathStyle_UNIX, PathStyle_UNIX, VINF_SUCCESS, "\\\\foo/bar\\\\baz");
121#if 0 /** @todo Not clear what to expect here. */
122 tstPathTranslate("with spaces\\ foo/\\ bar", PathStyle_UNIX, PathStyle_DOS, VINF_SUCCESS, "with spaces foo\\ bar");
123#endif
124
125 /*
126 * Destination path building testing.
127 */
128 bool fQuiet = RTAssertSetQuiet(true);
129 bool fMayPanic = RTAssertSetMayPanic(false);
130 tstPathBuildDestination("", PathStyle_UNIX, "", PathStyle_UNIX, VERR_PATH_ZERO_LENGTH, "");
131 tstPathBuildDestination(".", PathStyle_UNIX, ".", PathStyle_UNIX, VINF_SUCCESS, ".");
132 tstPathBuildDestination("..", PathStyle_UNIX, "..", PathStyle_UNIX, VERR_INVALID_PARAMETER, "..");
133 tstPathBuildDestination("/tmp/", PathStyle_UNIX, "/root/../foo", PathStyle_UNIX, VERR_INVALID_PARAMETER, "/root/../foo");
134 /* ".." in actual file names are allowed. */
135 tstPathBuildDestination("/tmp/", PathStyle_UNIX, "/root/foo..bar", PathStyle_UNIX, VINF_SUCCESS, "/root/foo..bar");
136 /* Ditto for path names which consist of more than just "..". */
137 tstPathBuildDestination("/tmp/", PathStyle_UNIX, "/root/foo..bar/baz", PathStyle_UNIX, VINF_SUCCESS, "/root/foo..bar/baz");
138 tstPathBuildDestination("...", PathStyle_UNIX, "...", PathStyle_UNIX, VINF_SUCCESS, "...");
139 tstPathBuildDestination("foo", PathStyle_UNIX, "bar", PathStyle_UNIX, VINF_SUCCESS, "bar");
140 tstPathBuildDestination("foo/", PathStyle_UNIX, "bar/", PathStyle_UNIX, VINF_SUCCESS, "bar/");
141 tstPathBuildDestination("foo/", PathStyle_UNIX, "bar/baz", PathStyle_UNIX, VINF_SUCCESS, "bar/baz");
142 tstPathBuildDestination("foo/baz", PathStyle_UNIX, "bar/", PathStyle_UNIX, VINF_SUCCESS, "bar/baz");
143 tstPathBuildDestination("foo/baz", PathStyle_UNIX, "bar\\", PathStyle_DOS, VINF_SUCCESS, "bar\\baz");
144
145 tstPathBuildDestination("c:\\temp\\", PathStyle_DOS, "/tmp/", PathStyle_UNIX, VINF_SUCCESS, "/tmp/");
146 tstPathBuildDestination("c:\\TEMP\\", PathStyle_DOS, "/TmP/", PathStyle_UNIX, VINF_SUCCESS, "/TmP/");
147 tstPathBuildDestination("c:\\temp\\foo.txt", PathStyle_DOS, "/tmp/foo.txt", PathStyle_UNIX, VINF_SUCCESS, "/tmp/foo.txt");
148 tstPathBuildDestination("c:\\temp\\bar\\foo.txt", PathStyle_DOS, "/tmp/foo2.txt", PathStyle_UNIX, VINF_SUCCESS, "/tmp/foo2.txt");
149 tstPathBuildDestination("c:\\temp\\bar\\foo3.txt", PathStyle_DOS, "/tmp/", PathStyle_UNIX, VINF_SUCCESS, "/tmp/foo3.txt");
150
151 tstPathBuildDestination("/tmp/bar/", PathStyle_UNIX, "c:\\temp\\", PathStyle_DOS, VINF_SUCCESS, "c:\\temp\\");
152 tstPathBuildDestination("/tmp/BaR/", PathStyle_UNIX, "c:\\tEmP\\", PathStyle_DOS, VINF_SUCCESS, "c:\\tEmP\\");
153 tstPathBuildDestination("/tmp/foo.txt", PathStyle_UNIX, "c:\\temp\\foo.txt", PathStyle_DOS, VINF_SUCCESS, "c:\\temp\\foo.txt");
154 tstPathBuildDestination("/tmp/bar/foo.txt", PathStyle_UNIX, "c:\\temp\\foo2.txt", PathStyle_DOS, VINF_SUCCESS, "c:\\temp\\foo2.txt");
155 tstPathBuildDestination("/tmp/bar/foo3.txt", PathStyle_UNIX, "c:\\temp\\", PathStyle_DOS, VINF_SUCCESS, "c:\\temp\\foo3.txt");
156 RTAssertSetMayPanic(fMayPanic);
157 RTAssertSetQuiet(fQuiet);
158
159 RTTestIPrintf(RTTESTLVL_DEBUG, "Shutting down COM...\n");
160 com::Shutdown();
161
162 /*
163 * Summary.
164 */
165 return RTTestSummaryAndDestroy(hTest);
166}
167
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette