VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/testcase/tstClipboardTransfers.cpp@ 92736

Last change on this file since 92736 was 92736, checked in by vboxsync, 3 years ago

Shared Clipboard/Transfers: Added very basic testing for transfer and event source objects. bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 KB
Line 
1/* $Id: tstClipboardTransfers.cpp 92736 2021-12-03 16:04:38Z vboxsync $ */
2/** @file
3 * Shared Clipboard transfers test case.
4 */
5
6/*
7 * Copyright (C) 2019-2021 Oracle Corporation
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
18#include "../VBoxSharedClipboardSvc-internal.h"
19
20#include <VBox/HostServices/VBoxClipboardSvc.h>
21
22#include <iprt/assert.h>
23#include <iprt/dir.h>
24#include <iprt/file.h>
25#include <iprt/path.h>
26#include <iprt/string.h>
27#include <iprt/test.h>
28
29
30static int testCreateTempDir(RTTEST hTest, const char *pszTestcase, char *pszTempDir, size_t cbTempDir)
31{
32 char szTempDir[RTPATH_MAX];
33 int rc = RTPathTemp(szTempDir, sizeof(szTempDir));
34 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
35
36 rc = RTPathAppend(szTempDir, sizeof(szTempDir), "tstClipboardTransfers");
37 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
38
39 rc = RTDirCreate(szTempDir, 0700, 0);
40 if (rc == VERR_ALREADY_EXISTS)
41 rc = VINF_SUCCESS;
42 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
43
44 rc = RTPathAppend(szTempDir, sizeof(szTempDir), "XXXXX");
45 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
46
47 rc = RTDirCreateTemp(szTempDir, 0700);
48 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
49
50 rc = RTPathJoin(pszTempDir, cbTempDir, szTempDir, pszTestcase);
51 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
52
53 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Created temporary directory: %s\n", pszTempDir);
54
55 return rc;
56}
57
58static int testRemoveTempDir(RTTEST hTest)
59{
60 char szTempDir[RTPATH_MAX];
61 int rc = RTPathTemp(szTempDir, sizeof(szTempDir));
62 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
63
64 rc = RTPathAppend(szTempDir, sizeof(szTempDir), "tstClipboardTransfers");
65 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
66
67 rc = RTDirRemoveRecursive(szTempDir, RTDIRRMREC_F_CONTENT_AND_DIR);
68 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
69
70 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Removed temporary directory: %s\n", szTempDir);
71
72 return rc;
73}
74
75static int testCreateDir(RTTEST hTest, const char *pszPathToCreate)
76{
77 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Creating directory: %s\n", pszPathToCreate);
78
79 int rc = RTDirCreateFullPath(pszPathToCreate, 0700);
80 if (rc == VERR_ALREADY_EXISTS)
81 rc = VINF_SUCCESS;
82 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
83
84 return rc;
85}
86
87static int testCreateFile(RTTEST hTest, const char *pszTempDir, const char *pszFileName, uint32_t fOpen, size_t cbSize,
88 char **ppszFilePathAbs)
89{
90 char szFilePath[RTPATH_MAX];
91
92 int rc = RTStrCopy(szFilePath, sizeof(szFilePath), pszTempDir);
93 RTTESTI_CHECK_RC_OK_RET(rc, rc);
94
95 rc = RTPathAppend(szFilePath, sizeof(szFilePath), pszFileName);
96 RTTESTI_CHECK_RC_OK_RET(rc, rc);
97
98 char *pszDirToCreate = RTStrDup(szFilePath);
99 RTTESTI_CHECK_RET(pszDirToCreate, VERR_NO_MEMORY);
100
101 RTPathStripFilename(pszDirToCreate);
102
103 rc = testCreateDir(hTest, pszDirToCreate);
104 RTTESTI_CHECK_RC_OK_RET(rc, rc);
105
106 RTStrFree(pszDirToCreate);
107 pszDirToCreate = NULL;
108
109 if (!fOpen)
110 fOpen = RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_NONE;
111
112 RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Creating file: %s\n", szFilePath);
113
114 RTFILE hFile;
115 rc = RTFileOpen(&hFile, szFilePath, fOpen);
116 if (RT_SUCCESS(rc))
117 {
118 if (cbSize)
119 {
120 /** @todo Fill in some random stuff. */
121 }
122
123 rc = RTFileClose(hFile);
124 RTTESTI_CHECK_RC_RET(rc, VINF_SUCCESS, rc);
125 }
126
127 if (ppszFilePathAbs)
128 *ppszFilePathAbs = RTStrDup(szFilePath);
129
130 return rc;
131}
132
133typedef struct TESTTRANSFERROOTENTRY
134{
135 TESTTRANSFERROOTENTRY(const RTCString &a_strPath)
136 : strPath(a_strPath) { }
137
138 RTCString strPath;
139} TESTTRANSFERROOTENTRY;
140
141static int testAddRootEntry(RTTEST hTest, const char *pszTempDir,
142 const TESTTRANSFERROOTENTRY &rootEntry, char **ppszRoots)
143{
144 char *pszRoots = NULL;
145
146 const char *pszPath = rootEntry.strPath.c_str();
147
148 char *pszPathAbs;
149 int rc = testCreateFile(hTest, pszTempDir, pszPath, 0, 0, &pszPathAbs);
150 RTTESTI_CHECK_RC_OK_RET(rc, rc);
151
152 rc = RTStrAAppend(&pszRoots, pszPathAbs);
153 RTTESTI_CHECK_RC_OK(rc);
154
155 rc = RTStrAAppend(&pszRoots, "\r\n");
156 RTTESTI_CHECK_RC_OK(rc);
157
158 RTStrFree(pszPathAbs);
159
160 *ppszRoots = pszRoots;
161
162 return rc;
163}
164
165static int testAddRootEntries(RTTEST hTest, const char *pszTempDir,
166 RTCList<TESTTRANSFERROOTENTRY> &lstBase, RTCList<TESTTRANSFERROOTENTRY> lstToExtend,
167 char **ppszRoots)
168{
169 int rc = VINF_SUCCESS;
170
171 char *pszRoots = NULL;
172
173 for (size_t i = 0; i < lstBase.size(); ++i)
174 {
175 char *pszEntry = NULL;
176 rc = testAddRootEntry(hTest, pszTempDir, lstBase.at(i), &pszEntry);
177 RTTESTI_CHECK_RC_OK_BREAK(rc);
178 rc = RTStrAAppend(&pszRoots, pszEntry);
179 RTTESTI_CHECK_RC_OK_BREAK(rc);
180 RTStrFree(pszEntry);
181 }
182
183 for (size_t i = 0; i < lstToExtend.size(); ++i)
184 {
185 char *pszEntry = NULL;
186 rc = testAddRootEntry(hTest, pszTempDir, lstToExtend.at(i), &pszEntry);
187 RTTESTI_CHECK_RC_OK_BREAK(rc);
188 rc = RTStrAAppend(&pszRoots, pszEntry);
189 RTTESTI_CHECK_RC_OK_BREAK(rc);
190 RTStrFree(pszEntry);
191 }
192
193 if (RT_SUCCESS(rc))
194 *ppszRoots = pszRoots;
195
196 return rc;
197}
198
199static void testTransferRootsSetSingle(RTTEST hTest,
200 RTCList<TESTTRANSFERROOTENTRY> &lstBase, RTCList<TESTTRANSFERROOTENTRY> lstToExtend,
201 int rcExpected)
202{
203 PSHCLTRANSFER pTransfer;
204 int rc = ShClTransferCreate(&pTransfer);
205 RTTESTI_CHECK_RC_OK(rc);
206
207 char szTestTransferRootsSetDir[RTPATH_MAX];
208 rc = testCreateTempDir(hTest, "testTransferRootsSet", szTestTransferRootsSetDir, sizeof(szTestTransferRootsSetDir));
209 RTTESTI_CHECK_RC_OK_RETV(rc);
210
211 /* This is the file we're trying to access (but not supposed to). */
212 rc = testCreateFile(hTest, szTestTransferRootsSetDir, "must-not-access-this", 0, 0, NULL);
213 RTTESTI_CHECK_RC_OK(rc);
214
215 char *pszRoots;
216 rc = testAddRootEntries(hTest, szTestTransferRootsSetDir, lstBase, lstToExtend, &pszRoots);
217 RTTESTI_CHECK_RC_OK_RETV(rc);
218
219 rc = ShClTransferRootsSet(pTransfer, pszRoots, strlen(pszRoots) + 1);
220 RTTESTI_CHECK_RC(rc, rcExpected);
221
222 RTStrFree(pszRoots);
223
224 rc = ShClTransferDestroy(pTransfer);
225 RTTESTI_CHECK_RC_OK(rc);
226}
227
228static void testTransferObjOpenSingle(RTTEST hTest,
229 RTCList<TESTTRANSFERROOTENTRY> &lstRoots, const char *pszObjPath, int rcExpected)
230{
231 RT_NOREF(hTest);
232
233 PSHCLTRANSFER pTransfer;
234 int rc = ShClTransferCreate(&pTransfer);
235 RTTESTI_CHECK_RC_OK(rc);
236
237 rc = ShClTransferInit(pTransfer, SHCLTRANSFERDIR_FROM_REMOTE, SHCLSOURCE_LOCAL);
238 RTTESTI_CHECK_RC_OK(rc);
239
240 char szTestTransferObjOpenDir[RTPATH_MAX];
241 rc = testCreateTempDir(hTest, "testTransferObjOpen", szTestTransferObjOpenDir, sizeof(szTestTransferObjOpenDir));
242 RTTESTI_CHECK_RC_OK_RETV(rc);
243
244 /* This is the file we're trying to access (but not supposed to). */
245 rc = testCreateFile(hTest, szTestTransferObjOpenDir, "file1.txt", 0, 0, NULL);
246 RTTESTI_CHECK_RC_OK(rc);
247
248 RTCList<TESTTRANSFERROOTENTRY> lstToExtendEmpty;
249
250 char *pszRoots;
251 rc = testAddRootEntries(hTest, szTestTransferObjOpenDir, lstRoots, lstToExtendEmpty, &pszRoots);
252 RTTESTI_CHECK_RC_OK_RETV(rc);
253
254 rc = ShClTransferRootsSet(pTransfer, pszRoots, strlen(pszRoots) + 1);
255 RTTESTI_CHECK_RC_OK(rc);
256
257 RTStrFree(pszRoots);
258
259 SHCLOBJOPENCREATEPARMS openCreateParms;
260 rc = ShClTransferObjOpenParmsInit(&openCreateParms);
261 RTTESTI_CHECK_RC_OK(rc);
262
263 rc = RTStrCopy(openCreateParms.pszPath, openCreateParms.cbPath, pszObjPath);
264 RTTESTI_CHECK_RC_OK(rc);
265
266 SHCLOBJHANDLE hObj;
267 rc = ShClTransferObjOpen(pTransfer, &openCreateParms, &hObj);
268 RTTESTI_CHECK_RC(rc, rcExpected);
269 if (RT_SUCCESS(rc))
270 {
271 rc = ShClTransferObjClose(pTransfer, hObj);
272 RTTESTI_CHECK_RC_OK(rc);
273 }
274
275 rc = ShClTransferDestroy(pTransfer);
276 RTTESTI_CHECK_RC_OK(rc);
277}
278
279static void testTransferBasics(RTTEST hTest)
280{
281 RT_NOREF(hTest);
282
283 RTTestISub("Testing transfer basics");
284
285 SHCLEVENTSOURCE Source;
286 int rc = ShClEventSourceCreate(&Source, 0);
287 RTTESTI_CHECK_RC_OK(rc);
288 rc = ShClEventSourceDestroy(&Source);
289 RTTESTI_CHECK_RC_OK(rc);
290 PSHCLTRANSFER pTransfer;
291 rc = ShClTransferCreate(&pTransfer);
292 RTTESTI_CHECK_RC_OK(rc);
293 rc = ShClTransferDestroy(pTransfer);
294 RTTESTI_CHECK_RC_OK(rc);
295}
296
297static void testTransferRootsSet(RTTEST hTest)
298{
299 RTTestISub("Testing setting transfer roots");
300
301 /* Define the (valid) transfer root set. */
302 RTCList<TESTTRANSFERROOTENTRY> lstBase;
303 lstBase.append(TESTTRANSFERROOTENTRY("my-transfer-1/file1.txt"));
304 lstBase.append(TESTTRANSFERROOTENTRY("my-transfer-1/dir1/file1.txt"));
305 lstBase.append(TESTTRANSFERROOTENTRY("my-transfer-1/dir1/sub1/file1.txt"));
306 lstBase.append(TESTTRANSFERROOTENTRY("my-transfer-1/dir2/file1.txt"));
307 lstBase.append(TESTTRANSFERROOTENTRY("my-transfer-1/dir2/sub1/file1.txt"));
308
309 RTCList<TESTTRANSFERROOTENTRY> lstBreakout;
310 testTransferRootsSetSingle(hTest, lstBase, lstBreakout, VINF_SUCCESS);
311
312 lstBreakout.clear();
313 lstBase.append(TESTTRANSFERROOTENTRY("../must-not-access-this"));
314 testTransferRootsSetSingle(hTest, lstBase, lstBreakout, VERR_INVALID_PARAMETER);
315
316 lstBreakout.clear();
317 lstBase.append(TESTTRANSFERROOTENTRY("does-not-exist/file1.txt"));
318 testTransferRootsSetSingle(hTest, lstBase, lstBreakout, VERR_INVALID_PARAMETER);
319
320 lstBreakout.clear();
321 lstBase.append(TESTTRANSFERROOTENTRY("my-transfer-1/../must-not-access-this"));
322 testTransferRootsSetSingle(hTest, lstBase, lstBreakout, VERR_INVALID_PARAMETER);
323
324 lstBreakout.clear();
325 lstBase.append(TESTTRANSFERROOTENTRY("my-transfer-1/./../must-not-access-this"));
326 testTransferRootsSetSingle(hTest, lstBase, lstBreakout, VERR_INVALID_PARAMETER);
327
328 lstBreakout.clear();
329 lstBase.append(TESTTRANSFERROOTENTRY("../does-not-exist"));
330 testTransferRootsSetSingle(hTest, lstBase, lstBreakout, VERR_INVALID_PARAMETER);
331}
332
333static void testTransferObjOpen(RTTEST hTest)
334{
335 RTTestISub("Testing setting transfer object open");
336
337 /* Define the (valid) transfer root set. */
338 RTCList<TESTTRANSFERROOTENTRY> lstRoots;
339 lstRoots.append(TESTTRANSFERROOTENTRY("my-transfer-1/file1.txt"));
340 lstRoots.append(TESTTRANSFERROOTENTRY("my-transfer-1/dir1/file1.txt"));
341 lstRoots.append(TESTTRANSFERROOTENTRY("my-transfer-1/dir1/sub1/file1.txt"));
342 lstRoots.append(TESTTRANSFERROOTENTRY("my-transfer-1/dir2/file1.txt"));
343 lstRoots.append(TESTTRANSFERROOTENTRY("my-transfer-1/dir2/sub1/file1.txt"));
344
345 testTransferObjOpenSingle(hTest, lstRoots, "file1.txt", VINF_SUCCESS);
346 testTransferObjOpenSingle(hTest, lstRoots, "does-not-exist.txt", VERR_PATH_NOT_FOUND);
347 testTransferObjOpenSingle(hTest, lstRoots, "dir1/does-not-exist.txt", VERR_PATH_NOT_FOUND);
348 testTransferObjOpenSingle(hTest, lstRoots, "../must-not-access-this.txt", VERR_INVALID_PARAMETER);
349 testTransferObjOpenSingle(hTest, lstRoots, "dir1/../../must-not-access-this.txt", VERR_INVALID_PARAMETER);
350}
351
352int main(int argc, char *argv[])
353{
354 /*
355 * Init the runtime, test and say hello.
356 */
357 const char *pcszExecName;
358 NOREF(argc);
359 pcszExecName = strrchr(argv[0], '/');
360 pcszExecName = pcszExecName ? pcszExecName + 1 : argv[0];
361 RTTEST hTest;
362 RTEXITCODE rcExit = RTTestInitAndCreate(pcszExecName, &hTest);
363 if (rcExit != RTEXITCODE_SUCCESS)
364 return rcExit;
365 RTTestBanner(hTest);
366
367 testTransferBasics(hTest);
368 testTransferRootsSet(hTest);
369 testTransferObjOpen(hTest);
370
371 int rc = testRemoveTempDir(hTest);
372 RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
373
374 /*
375 * Summary
376 */
377 return RTTestSummaryAndDestroy(hTest);
378}
379
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