1 | /* $Id: tstDnDTransferList.cpp 98103 2023-01-17 14:15:46Z 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 | #ifdef RT_OS_WINDOWS
|
---|
141 | RTStrPrintf(szPathTest, sizeof(szPathTest), "C:/Windows/");
|
---|
142 | const char szURI[] =
|
---|
143 | "file:///C:/Windows/System32/Boot/\r\n"
|
---|
144 | "file:///C:/Windows/System/\r\n";
|
---|
145 | #else
|
---|
146 | RTStrPrintf(szPathTest, sizeof(szPathTest), "/usr/");
|
---|
147 | const char szURI[] =
|
---|
148 | "file:///usr/bin/\r\n"
|
---|
149 | "file:///usr/lib/\r\n";
|
---|
150 | #endif
|
---|
151 |
|
---|
152 | RTTEST_CHECK_RC(hTest, DnDTransferListAppendPathsFromBuffer(&list, DNDTRANSFERLISTFMT_URI, szURI, sizeof(szURI), "\r\n",
|
---|
153 | DNDTRANSFERLIST_FLAGS_NONE), VINF_SUCCESS);
|
---|
154 | RTTEST_CHECK(hTest, DnDTransferListGetRootCount(&list) == 2);
|
---|
155 | RTTEST_CHECK(hTest, RTPathCompare(DnDTransferListGetRootPathAbs(&list), szPathTest) == 0);
|
---|
156 |
|
---|
157 | /* Validate returned lengths. */
|
---|
158 | pszBuf = NULL;
|
---|
159 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_URI, "/base/", "\r\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
160 | RTTEST_CHECK_MSG(hTest, RTStrCmp(pszBuf, "file:///base/bin/\r\nfile:///base/lib/\r\n") == 0, (hTest, "Got '%s'", pszBuf));
|
---|
161 | RTTEST_CHECK_MSG(hTest, cbBuf == strlen(pszBuf) + 1, (hTest, "Got %d, expected %d\n", cbBuf, strlen(pszBuf) + 1));
|
---|
162 | RTStrFree(pszBuf);
|
---|
163 |
|
---|
164 | pszBuf = NULL;
|
---|
165 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_NATIVE, "/base/", "\r\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
166 | RTTEST_CHECK_MSG(hTest, RTStrCmp(pszBuf, "/base/bin/\r\n/base/lib/\r\n") == 0, (hTest, "Got '%s'", pszBuf));
|
---|
167 | RTTEST_CHECK_MSG(hTest, cbBuf == strlen(pszBuf) + 1, (hTest, "Got %d, expected %d\n", cbBuf, strlen(pszBuf) + 1));
|
---|
168 | RTStrFree(pszBuf);
|
---|
169 |
|
---|
170 | /* Validate roots with a new base. */
|
---|
171 | pszBuf = NULL;
|
---|
172 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_NATIVE, "/native/base/path", "\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
173 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Roots (URI, new base):\n%s\n", pszBuf);
|
---|
174 | RTStrFree(pszBuf);
|
---|
175 |
|
---|
176 | pszBuf = NULL;
|
---|
177 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_NATIVE, "\\windows\\path", "\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
178 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Roots (URI, new base):\n%s\n", pszBuf);
|
---|
179 | RTStrFree(pszBuf);
|
---|
180 |
|
---|
181 | pszBuf = NULL;
|
---|
182 | RTTEST_CHECK_RC(hTest, DnDTransferListGetRootsEx(&list, DNDTRANSFERLISTFMT_NATIVE, "\\\\windows\\\\path", "\n", &pszBuf, &cbBuf), VINF_SUCCESS);
|
---|
183 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Roots (URI, new base):\n%s\n", pszBuf);
|
---|
184 | RTStrFree(pszBuf);
|
---|
185 |
|
---|
186 | DnDTransferListDestroy(&list);
|
---|
187 | DnDTransferListDestroy(&list); /* Doing this twice here is intentional. */
|
---|
188 |
|
---|
189 | return RTTestSummaryAndDestroy(hTest);
|
---|
190 | }
|
---|
191 |
|
---|