VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstMediumLock.cpp@ 63495

Last change on this file since 63495 was 60063, checked in by vboxsync, 9 years ago

Frontends etc: convert more code to use VirtualBoxClient instead of direct creation of VirtualBox instances, to improve error handling

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/* $Id: tstMediumLock.cpp 60063 2016-03-16 15:00:06Z vboxsync $ */
2/** @file
3 * Medium lock test cases.
4 */
5
6/*
7 * Copyright (C) 2013-2016 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#define LOG_ENABLED
19#define LOG_GROUP LOG_GROUP_MAIN
20#include <VBox/log.h>
21
22#include <VBox/com/com.h>
23#include <VBox/com/ptr.h>
24#include <VBox/com/defs.h>
25#include <VBox/com/array.h>
26#include <VBox/com/string.h>
27#include <VBox/com/VirtualBox.h>
28
29#include <iprt/assert.h>
30#include <iprt/uuid.h>
31#include <iprt/path.h>
32#include <iprt/string.h>
33#include <iprt/initterm.h>
34#include <iprt/test.h>
35
36using namespace com;
37
38
39#define TEST_RT_SUCCESS(x,y,z) \
40 do \
41 { \
42 int rc = (y); \
43 if (RT_FAILURE(rc)) \
44 RTTestFailed((x), "%s %Rrc", (z), rc); \
45 } while (0)
46
47#define TEST_COM_SUCCESS(x,y,z) \
48 do \
49 { \
50 HRESULT hrc = (y); \
51 if (FAILED(hrc)) \
52 RTTestFailed((x), "%s %Rhrc", (z), hrc); \
53 } while (0)
54
55#define TEST_COM_FAILURE(x,y,z) \
56 do \
57 { \
58 HRESULT hrc = (y); \
59 if (SUCCEEDED(hrc)) \
60 RTTestFailed((x), "%s", (z)); \
61 } while (0)
62
63int main(int argc, char *argv[])
64{
65 /* Init the runtime without loading the support driver. */
66 RTR3InitExe(argc, &argv, 0);
67
68 RTTEST hTest;
69 RTEXITCODE rcExit = RTTestInitAndCreate("tstMediumLock", &hTest);
70 if (rcExit)
71 return rcExit;
72 RTTestBanner(hTest);
73
74 bool fComInit = false;
75 ComPtr<IVirtualBoxClient> pVirtualBoxClient;
76 ComPtr<IVirtualBox> pVirtualBox;
77 char szPathTemp[RTPATH_MAX] = "";
78 ComPtr<IMedium> pMedium;
79
80 if (!RTTestSubErrorCount(hTest))
81 {
82 RTTestSub(hTest, "Constructing temp image name");
83 TEST_RT_SUCCESS(hTest, RTPathTemp(szPathTemp, sizeof(szPathTemp)), "temp directory");
84 RTUUID uuid;
85 RTUuidCreate(&uuid);
86 char szFile[50];
87 RTStrPrintf(szFile, sizeof(szFile), "%RTuuid.vdi", &uuid);
88 TEST_RT_SUCCESS(hTest, RTPathAppend(szPathTemp, sizeof(szPathTemp), szFile), "concatenate image name");
89 }
90
91 if (!RTTestSubErrorCount(hTest))
92 {
93 RTTestSub(hTest, "Initializing COM");
94 TEST_COM_SUCCESS(hTest, Initialize(), "init");
95 }
96
97 if (!RTTestSubErrorCount(hTest))
98 {
99 fComInit = true;
100
101 RTTestSub(hTest, "Getting VirtualBox reference");
102 TEST_COM_SUCCESS(hTest, pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient), "vboxclient reference");
103 TEST_COM_SUCCESS(hTest, pVirtualBoxClient->COMGETTER(VirtualBox)(pVirtualBox.asOutParam()), "vbox reference");
104 }
105
106 if (!RTTestSubErrorCount(hTest))
107 {
108 RTTestSub(hTest, "Creating temp hard disk medium");
109 TEST_COM_SUCCESS(hTest, pVirtualBox->CreateMedium(Bstr("VDI").raw(), Bstr(szPathTemp).raw(), AccessMode_ReadWrite, DeviceType_HardDisk, pMedium.asOutParam()), "create medium");
110 if (!pMedium.isNull())
111 {
112 ComPtr<IProgress> pProgress;
113 SafeArray<MediumVariant_T> variant;
114 variant.push_back(MediumVariant_Standard);
115 TEST_COM_SUCCESS(hTest, pMedium->CreateBaseStorage(_1M, ComSafeArrayAsInParam(variant), pProgress.asOutParam()), "create base storage");
116 if (!pProgress.isNull())
117 TEST_COM_SUCCESS(hTest, pProgress->WaitForCompletion(30000), "waiting for completion of create");
118 }
119 }
120
121 if (!RTTestSubErrorCount(hTest))
122 {
123 RTTestSub(hTest, "Write locks");
124 ComPtr<IToken> pToken1, pToken2;
125
126 MediumState_T mediumState = MediumState_NotCreated;
127 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
128 if (mediumState != MediumState_Created)
129 RTTestFailed(hTest, "wrong medium state %d", mediumState);
130
131 TEST_COM_SUCCESS(hTest, pMedium->LockWrite(pToken1.asOutParam()), "write lock");
132
133 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock write state");
134 if (mediumState != MediumState_LockedWrite)
135 RTTestFailed(hTest, "wrong lock write medium state %d", mediumState);
136
137 TEST_COM_FAILURE(hTest, pMedium->LockWrite(pToken2.asOutParam()), "nested write lock succeeded");
138 if (!pToken2.isNull())
139 RTTestFailed(hTest, "pToken2 is not null");
140
141 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock write state");
142 if (mediumState != MediumState_LockedWrite)
143 RTTestFailed(hTest, "wrong after nested lock write medium state %d", mediumState);
144
145 if (!pToken1.isNull())
146 TEST_COM_SUCCESS(hTest, pToken1->Abandon(), "write unlock");
147 else
148 RTTestFailed(hTest, "pToken1 is null");
149
150 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock write state");
151 if (mediumState != MediumState_Created)
152 RTTestFailed(hTest, "wrong unlock write medium state %d", mediumState);
153 }
154
155 if (!RTTestSubErrorCount(hTest))
156 {
157 RTTestSub(hTest, "Read locks");
158 ComPtr<IToken> pToken1, pToken2;
159
160 MediumState_T mediumState = MediumState_NotCreated;
161 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
162 if (mediumState != MediumState_Created)
163 RTTestFailed(hTest, "wrong medium state %d", mediumState);
164
165 TEST_COM_SUCCESS(hTest, pMedium->LockRead(pToken1.asOutParam()), "read lock");
166
167 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock read state");
168 if (mediumState != MediumState_LockedRead)
169 RTTestFailed(hTest, "wrong lock read medium state %d", mediumState);
170
171 TEST_COM_SUCCESS(hTest, pMedium->LockRead(pToken2.asOutParam()), "nested read lock failed");
172
173 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock read state");
174 if (mediumState != MediumState_LockedRead)
175 RTTestFailed(hTest, "wrong after nested lock read medium state %d", mediumState);
176
177 if (!pToken2.isNull())
178 TEST_COM_SUCCESS(hTest, pToken2->Abandon(), "read nested unlock");
179 else
180 RTTestFailed(hTest, "pToken2 is null");
181
182 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock read state");
183 if (mediumState != MediumState_LockedRead)
184 RTTestFailed(hTest, "wrong after nested lock read medium state %d", mediumState);
185
186 if (!pToken1.isNull())
187 TEST_COM_SUCCESS(hTest, pToken1->Abandon(), "read nested unlock");
188 else
189 RTTestFailed(hTest, "pToken1 is null");
190
191 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock read state");
192 if (mediumState != MediumState_Created)
193 RTTestFailed(hTest, "wrong unlock read medium state %d", mediumState);
194 }
195
196 if (!RTTestSubErrorCount(hTest))
197 {
198 RTTestSub(hTest, "Mixing write and read locks");
199 ComPtr<IToken> pToken1, pToken2;
200
201 MediumState_T mediumState = MediumState_NotCreated;
202 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
203 if (mediumState != MediumState_Created)
204 RTTestFailed(hTest, "wrong medium state %d", mediumState);
205
206 TEST_COM_SUCCESS(hTest, pMedium->LockWrite(pToken1.asOutParam()), "write lock");
207
208 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock write state");
209 if (mediumState != MediumState_LockedWrite)
210 RTTestFailed(hTest, "wrong lock write medium state %d", mediumState);
211
212 TEST_COM_FAILURE(hTest, pMedium->LockRead(pToken2.asOutParam()), "write+read lock succeeded");
213 if (!pToken2.isNull())
214 RTTestFailed(hTest, "pToken2 is not null");
215
216 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock write state");
217 if (mediumState != MediumState_LockedWrite)
218 RTTestFailed(hTest, "wrong after nested lock write medium state %d", mediumState);
219
220 if (!pToken1.isNull())
221 TEST_COM_SUCCESS(hTest, pToken1->Abandon(), "write unlock");
222 else
223 RTTestFailed(hTest, "pToken1 is null");
224
225 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock write state");
226 if (mediumState != MediumState_Created)
227 RTTestFailed(hTest, "wrong unlock write medium state %d", mediumState);
228 }
229
230 if (!RTTestSubErrorCount(hTest))
231 {
232 RTTestSub(hTest, "Mixing read and write locks");
233 ComPtr<IToken> pToken1, pToken2;
234
235 MediumState_T mediumState = MediumState_NotCreated;
236 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
237 if (mediumState != MediumState_Created)
238 RTTestFailed(hTest, "wrong medium state %d", mediumState);
239
240 TEST_COM_SUCCESS(hTest, pMedium->LockRead(pToken1.asOutParam()), "read lock");
241
242 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock read state");
243 if (mediumState != MediumState_LockedRead)
244 RTTestFailed(hTest, "wrong lock read medium state %d", mediumState);
245
246 TEST_COM_FAILURE(hTest, pMedium->LockWrite(pToken2.asOutParam()), "read+write lock succeeded");
247 if (!pToken2.isNull())
248 RTTestFailed(hTest, "pToken2 is not null");
249
250 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock read state");
251 if (mediumState != MediumState_LockedRead)
252 RTTestFailed(hTest, "wrong after nested lock read medium state %d", mediumState);
253
254 if (!pToken1.isNull())
255 TEST_COM_SUCCESS(hTest, pToken1->Abandon(), "read unlock");
256 else
257 RTTestFailed(hTest, "pToken1 is null");
258
259 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock read state");
260 if (mediumState != MediumState_Created)
261 RTTestFailed(hTest, "wrong unlock read medium state %d", mediumState);
262 }
263
264 /* Cleanup, also part of the testcase */
265
266 if (!pMedium.isNull())
267 {
268 RTTestSub(hTest, "Closing medium");
269 MediumState_T mediumState = MediumState_NotCreated;
270 TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
271 if (mediumState == MediumState_Created)
272 {
273 ComPtr<IProgress> pProgress;
274 TEST_COM_SUCCESS(hTest, pMedium->DeleteStorage(pProgress.asOutParam()), "deleting storage");
275 if (!pProgress.isNull())
276 TEST_COM_SUCCESS(hTest, pProgress->WaitForCompletion(30000), "waiting for completion of delete");
277 }
278 TEST_COM_SUCCESS(hTest, pMedium->Close(), "closing");
279 pMedium.setNull();
280 }
281
282 pVirtualBox.setNull();
283 pVirtualBoxClient.setNull();
284
285 /* Make sure that there are no object references alive here, XPCOM does
286 * a very bad job at cleaning up such leftovers, spitting out warning
287 * messages in a debug build. */
288
289 if (fComInit)
290 {
291 RTTestIPrintf(RTTESTLVL_DEBUG, "Shutting down COM...\n");
292 Shutdown();
293 }
294
295 /*
296 * Summary.
297 */
298 return RTTestSummaryAndDestroy(hTest);
299}
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