1 | /* $Id: tstDnDTransferList.cpp 98638 2023-02-20 09:30:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DnD transfer list tests.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2020-2023 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 | #include <iprt/assert.h>
|
---|
29 | #include <iprt/env.h>
|
---|
30 | #include <iprt/err.h>
|
---|
31 | #include <iprt/path.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <iprt/test.h>
|
---|
34 |
|
---|
35 | #include <VBox/GuestHost/DragAndDrop.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | int main()
|
---|
39 | {
|
---|
40 | /*
|
---|
41 | * Init the runtime, test and say hello.
|
---|
42 | */
|
---|
43 | RTTEST hTest;
|
---|
44 | int rc = RTTestInitAndCreate("tstDnDTransferList", &hTest);
|
---|
45 | if (rc)
|
---|
46 | return rc;
|
---|
47 | RTTestBanner(hTest);
|
---|
48 |
|
---|
49 | char szPathWellKnown[RTPATH_MAX];
|
---|
50 | RTStrCopy(szPathWellKnown, sizeof(szPathWellKnown),
|
---|
51 | #ifdef RT_OS_WINDOWS
|
---|
52 | "C:\\Windows\\System32\\Boot\\");
|
---|
53 | #else
|
---|
54 | "/bin/");
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | char szPathWellKnownURI[RTPATH_MAX];
|
---|
58 | RTStrPrintf(szPathWellKnownURI, sizeof(szPathWellKnownURI), "file:///%s", szPathWellKnown);
|
---|
59 |
|
---|
60 | DNDTRANSFERLIST list;
|
---|
61 | RT_ZERO(list);
|
---|
62 |
|
---|
63 | /* Invalid stuff. */
|
---|
64 | RTTestDisableAssertions(hTest);
|
---|
65 | RTTEST_CHECK_RC(hTest, DnDTransferListInitEx(&list, "", DNDTRANSFERLISTFMT_NATIVE), VERR_INVALID_PARAMETER);
|
---|
66 | RTTEST_CHECK_RC(hTest, DnDTransferListInitEx(&list, szPathWellKnown, DNDTRANSFERLISTFMT_NATIVE), VINF_SUCCESS);
|
---|
67 | RTTEST_CHECK_RC(hTest, DnDTransferListInitEx(&list, szPathWellKnown, DNDTRANSFERLISTFMT_NATIVE), VERR_WRONG_ORDER);
|
---|
68 | RTTestRestoreAssertions(hTest);
|
---|
69 | DnDTransferListDestroy(&list);
|
---|
70 |
|
---|
71 | /* Empty. */
|
---|
72 | RTTEST_CHECK_RC(hTest, DnDTransferListInit(&list), VINF_SUCCESS);
|
---|
73 | DnDTransferListDestroy(&list);
|
---|
74 |
|
---|
75 | /* Initial status. */
|
---|
76 | RTTEST_CHECK_RC(hTest, DnDTransferListInitEx(&list, szPathWellKnown, DNDTRANSFERLISTFMT_NATIVE), VINF_SUCCESS);
|
---|
77 | RTTEST_CHECK(hTest, DnDTransferListGetRootCount(&list) == 0);
|
---|
78 | RTTEST_CHECK(hTest, DnDTransferListObjCount(&list) == 0);
|
---|
79 | RTTEST_CHECK(hTest, DnDTransferListObjTotalBytes(&list) == 0);
|
---|
80 | RTTEST_CHECK(hTest, DnDTransferListObjGetFirst(&list) == NULL);
|
---|
81 | DnDTransferListDestroy(&list);
|
---|
82 |
|
---|
83 | char szPathTest[RTPATH_MAX];
|
---|
84 |
|
---|
85 | /* Root path handling. */
|
---|
86 | RTTEST_CHECK_RC(hTest, DnDTransferListInitEx(&list, szPathWellKnown, DNDTRANSFERLISTFMT_NATIVE), VINF_SUCCESS);
|
---|
87 | RTTEST_CHECK_RC(hTest, DnDTransferListAppendPath(&list, DNDTRANSFERLISTFMT_NATIVE, "/wrong/root/path", DNDTRANSFERLIST_FLAGS_NONE), VERR_INVALID_PARAMETER);
|
---|
88 | rc = RTPathJoin(szPathTest, sizeof(szPathTest), szPathWellKnown, "/non/existing");
|
---|
89 | AssertRCReturn(rc, RTEXITCODE_FAILURE);
|
---|
90 | RTTEST_CHECK_RC(hTest, DnDTransferListAppendPath(&list, DNDTRANSFERLISTFMT_NATIVE, szPathTest, DNDTRANSFERLIST_FLAGS_NONE), VERR_PATH_NOT_FOUND);
|
---|
91 | DnDTransferListDestroy(&list);
|
---|
92 |
|
---|
93 | /* Adding native stuff. */
|
---|
94 | /* No root path set yet and non-recursive -> will set root path to szPathWellKnown, but without any entries added. */
|
---|
95 | RTTEST_CHECK_RC(hTest, DnDTransferListInitEx(&list, szPathWellKnown, DNDTRANSFERLISTFMT_NATIVE), VINF_SUCCESS);
|
---|
96 | RTTEST_CHECK_RC(hTest, DnDTransferListAppendPath(&list, DNDTRANSFERLISTFMT_NATIVE, szPathWellKnown, DNDTRANSFERLIST_FLAGS_NONE), VINF_SUCCESS);
|
---|
97 | RTTEST_CHECK(hTest, DnDTransferListGetRootCount(&list));
|
---|
98 | RTTEST_CHECK(hTest, DnDTransferListObjCount(&list));
|
---|
99 |
|
---|
100 | /* Add szPathWellKnown again, this time recursively. */
|
---|
101 | RTTEST_CHECK_RC(hTest, DnDTransferListAppendPath(&list, DNDTRANSFERLISTFMT_NATIVE, szPathWellKnown, DNDTRANSFERLIST_FLAGS_RECURSIVE), VINF_SUCCESS);
|
---|
102 | RTTEST_CHECK(hTest, DnDTransferListGetRootCount(&list));
|
---|
103 | RTTEST_CHECK(hTest, DnDTransferListObjCount(&list));
|
---|
104 |
|
---|
105 | char *pszString = NULL;
|
---|
106 | size_t cbString = 0;
|
---|
107 | RTTEST_CHECK_RC_OK(hTest, DnDTransferListGetRoots(&list, DNDTRANSFERLISTFMT_NATIVE, &pszString, &cbString));
|
---|
108 | RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Roots:\n%s\n\n", pszString);
|
---|
109 | RTStrFree(pszString);
|
---|
110 |
|
---|
111 | PDNDTRANSFEROBJECT pObj;
|
---|
112 | while ((pObj = DnDTransferListObjGetFirst(&list)))
|
---|
113 | {
|
---|
114 | RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Obj: %s\n", DnDTransferObjectGetDestPath(pObj));
|
---|
115 | DnDTransferListObjRemoveFirst(&list);
|
---|
116 | }
|
---|
117 | DnDTransferListDestroy(&list);
|
---|
118 |
|
---|
119 | char *pszBuf;
|
---|
120 | size_t cbBuf;
|
---|
121 |
|
---|
122 | /* To URI data. */
|
---|
123 | RTTEST_CHECK_RC(hTest, DnDTransferListInitEx(&list, szPathWellKnownURI, DNDTRANSFERLISTFMT_URI), VINF_SUCCESS);
|
---|
124 | RTStrPrintf(szPathTest, sizeof(szPathTest), "%s/foo", szPathWellKnownURI);
|
---|
125 | RTTEST_CHECK_RC(hTest, DnDTransferListAppendPath(&list, DNDTRANSFERLISTFMT_URI, szPathWellKnownURI, DNDTRANSFERLIST_FLAGS_NONE), VINF_SUCCESS);
|
---|
126 | RTTEST_CHECK_RC(hTest, DnDTransferListAppendPath(&list, DNDTRANSFERLISTFMT_URI, szPathTest, DNDTRANSFERLIST_FLAGS_NONE), VERR_PATH_NOT_FOUND);
|
---|
127 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_NATIVE, "" /* pszBasePath */, "\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
128 | RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Roots (native):\n%s\n", pszBuf);
|
---|
129 | RTStrFree(pszBuf);
|
---|
130 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_URI, "" /* pszBasePath */, "\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
131 | RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Roots (URI):\n%s\n", pszBuf);
|
---|
132 | RTStrFree(pszBuf);
|
---|
133 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_URI, "\\new\\base\\path", "\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
134 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Roots (URI, new base):\n%s\n", pszBuf);
|
---|
135 | RTStrFree(pszBuf);
|
---|
136 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_URI, "\\..\\invalid\\path", "\n", &pszBuf, &cbBuf), VERR_INVALID_PARAMETER);
|
---|
137 | DnDTransferListDestroy(&list);
|
---|
138 |
|
---|
139 | /* From URI data. */
|
---|
140 | #if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
|
---|
141 | RTStrPrintf(szPathTest, sizeof(szPathTest), "C:/Windows/");
|
---|
142 | static const char s_szURI[] =
|
---|
143 | "file:///C:/Windows/System32/Boot/\r\n"
|
---|
144 | "file:///C:/Windows/System/\r\n";
|
---|
145 | static const char s_szURIFmtURI[] =
|
---|
146 | "file:///base/System32/Boot/\r\n"
|
---|
147 | "file:///base/System/\r\n";
|
---|
148 | static const char s_szURIFmtNative[] =
|
---|
149 | "\\base\\System32\\Boot\\\r\n"
|
---|
150 | "\\base\\System\\\r\n";
|
---|
151 | #else
|
---|
152 | RTStrPrintf(szPathTest, sizeof(szPathTest), "/usr/");
|
---|
153 | static const char s_szURI[] =
|
---|
154 | "file:///usr/bin/\r\n"
|
---|
155 | "file:///usr/lib/\r\n";
|
---|
156 | static const char s_szURIFmtURI[] =
|
---|
157 | "file:///base/bin/\r\n"
|
---|
158 | "file:///base/lib/\r\n";
|
---|
159 | static const char s_szURIFmtNative[] =
|
---|
160 | "/base/bin/\r\n"
|
---|
161 | "/base/lib/\r\n";
|
---|
162 | #endif
|
---|
163 |
|
---|
164 | RTTEST_CHECK_RC(hTest, DnDTransferListAppendPathsFromBuffer(&list, DNDTRANSFERLISTFMT_URI, s_szURI, sizeof(s_szURI), "\r\n",
|
---|
165 | DNDTRANSFERLIST_FLAGS_NONE), VINF_SUCCESS);
|
---|
166 | RTTEST_CHECK(hTest, DnDTransferListGetRootCount(&list) == 2);
|
---|
167 | RTTEST_CHECK(hTest, RTPathCompare(DnDTransferListGetRootPathAbs(&list), szPathTest) == 0);
|
---|
168 |
|
---|
169 | /* Validate returned lengths. */
|
---|
170 | pszBuf = NULL;
|
---|
171 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_URI, "/base/", "\r\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
172 | RTTEST_CHECK_MSG(hTest, RTStrCmp(pszBuf, s_szURIFmtURI) == 0, (hTest, "Got '%s'", pszBuf));
|
---|
173 | RTTEST_CHECK_MSG(hTest, cbBuf == strlen(pszBuf) + 1, (hTest, "Got %d, expected %d\n", cbBuf, strlen(pszBuf) + 1));
|
---|
174 | RTStrFree(pszBuf);
|
---|
175 |
|
---|
176 | pszBuf = NULL;
|
---|
177 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_NATIVE, "/base/", "\r\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
178 | RTTEST_CHECK_MSG(hTest, RTStrCmp(pszBuf, s_szURIFmtNative) == 0,
|
---|
179 | (hTest, "Expected %.*Rhxs\nGot %.*Rhxs\n '%s'",
|
---|
180 | sizeof(s_szURIFmtNative) - 1, s_szURIFmtNative,
|
---|
181 | strlen(pszBuf), pszBuf, pszBuf));
|
---|
182 | RTTEST_CHECK_MSG(hTest, cbBuf == strlen(pszBuf) + 1, (hTest, "Got %d, expected %d\n", cbBuf, strlen(pszBuf) + 1));
|
---|
183 | RTStrFree(pszBuf);
|
---|
184 |
|
---|
185 | /* Validate roots with a new base. */
|
---|
186 | pszBuf = NULL;
|
---|
187 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_NATIVE, "/native/base/path", "\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
188 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Roots (URI, new base):\n%s\n", pszBuf);
|
---|
189 | RTStrFree(pszBuf);
|
---|
190 |
|
---|
191 | pszBuf = NULL;
|
---|
192 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_NATIVE, "\\windows\\path", "\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
193 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Roots (URI, new base):\n%s\n", pszBuf);
|
---|
194 | RTStrFree(pszBuf);
|
---|
195 |
|
---|
196 | pszBuf = NULL;
|
---|
197 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_NATIVE, "\\\\windows\\\\path", "\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
198 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Roots (URI, new base):\n%s\n", pszBuf);
|
---|
199 | RTStrFree(pszBuf);
|
---|
200 |
|
---|
201 | DnDTransferListDestroy(&list);
|
---|
202 | DnDTransferListDestroy(&list); /* Doing this twice here is intentional. */
|
---|
203 |
|
---|
204 | return RTTestSummaryAndDestroy(hTest);
|
---|
205 | }
|
---|
206 |
|
---|