1 | /* $Id: tstMediumLock.cpp 47779 2013-08-15 17:59:50Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * Medium lock test cases.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2013 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #define LOG_ENABLED
|
---|
21 | #define LOG_GROUP LOG_GROUP_MAIN
|
---|
22 | #define LOG_INSTANCE NULL
|
---|
23 | #include <VBox/log.h>
|
---|
24 |
|
---|
25 | #include <VBox/com/com.h>
|
---|
26 | #include <VBox/com/ptr.h>
|
---|
27 | #include <VBox/com/defs.h>
|
---|
28 | #include <VBox/com/array.h>
|
---|
29 | #include <VBox/com/string.h>
|
---|
30 | #include <VBox/com/VirtualBox.h>
|
---|
31 |
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/uuid.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 | #include <iprt/test.h>
|
---|
38 |
|
---|
39 | using namespace com;
|
---|
40 |
|
---|
41 |
|
---|
42 | #define TEST_RT_SUCCESS(x,y,z) \
|
---|
43 | do \
|
---|
44 | { \
|
---|
45 | int rc = (y); \
|
---|
46 | if (RT_FAILURE(rc)) \
|
---|
47 | RTTestFailed((x), "%s %Rrc", (z), rc); \
|
---|
48 | } while (0)
|
---|
49 |
|
---|
50 | #define TEST_COM_SUCCESS(x,y,z) \
|
---|
51 | do \
|
---|
52 | { \
|
---|
53 | HRESULT hrc = (y); \
|
---|
54 | if (FAILED(hrc)) \
|
---|
55 | RTTestFailed((x), "%s %Rhrc", (z), hrc); \
|
---|
56 | } while (0)
|
---|
57 |
|
---|
58 | #define TEST_COM_FAILURE(x,y,z) \
|
---|
59 | do \
|
---|
60 | { \
|
---|
61 | HRESULT hrc = (y); \
|
---|
62 | if (SUCCEEDED(hrc)) \
|
---|
63 | RTTestFailed((x), "%s", (z)); \
|
---|
64 | } while (0)
|
---|
65 |
|
---|
66 | int main(int argc, char *argv[])
|
---|
67 | {
|
---|
68 | /* Init the runtime without loading the support driver. */
|
---|
69 | RTR3InitExe(argc, &argv, 0);
|
---|
70 |
|
---|
71 | RTTEST hTest;
|
---|
72 | RTEXITCODE rcExit = RTTestInitAndCreate("tstMediumLock", &hTest);
|
---|
73 | if (rcExit)
|
---|
74 | return rcExit;
|
---|
75 | RTTestBanner(hTest);
|
---|
76 |
|
---|
77 | bool fComInit = false;
|
---|
78 | ComPtr<IVirtualBox> pVirtualBox;
|
---|
79 | char szPathTemp[RTPATH_MAX] = "";
|
---|
80 | ComPtr<IMedium> pMedium;
|
---|
81 |
|
---|
82 | if (!RTTestSubErrorCount(hTest))
|
---|
83 | {
|
---|
84 | RTTestSub(hTest, "Constructing temp image name");
|
---|
85 | TEST_RT_SUCCESS(hTest, RTPathTemp(szPathTemp, sizeof(szPathTemp)), "temp directory");
|
---|
86 | RTUUID uuid;
|
---|
87 | RTUuidCreate(&uuid);
|
---|
88 | char szFile[50];
|
---|
89 | RTStrPrintf(szFile, sizeof(szFile), "%RTuuid.vdi", &uuid);
|
---|
90 | TEST_RT_SUCCESS(hTest, RTPathAppend(szPathTemp, sizeof(szPathTemp), szFile), "concatenate image name");
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (!RTTestSubErrorCount(hTest))
|
---|
94 | {
|
---|
95 | RTTestSub(hTest, "Initializing COM");
|
---|
96 | TEST_COM_SUCCESS(hTest, Initialize(), "init");
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (!RTTestSubErrorCount(hTest))
|
---|
100 | {
|
---|
101 | fComInit = true;
|
---|
102 |
|
---|
103 | RTTestSub(hTest, "Getting VirtualBox reference");
|
---|
104 | TEST_COM_SUCCESS(hTest, pVirtualBox.createLocalObject(CLSID_VirtualBox), "vbox reference");
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (!RTTestSubErrorCount(hTest))
|
---|
108 | {
|
---|
109 | RTTestSub(hTest, "Creating temp hard disk medium");
|
---|
110 | TEST_COM_SUCCESS(hTest, pVirtualBox->CreateHardDisk(Bstr("VDI").raw(), Bstr(szPathTemp).raw(), pMedium.asOutParam()), "create medium");
|
---|
111 | if (!pMedium.isNull())
|
---|
112 | {
|
---|
113 | ComPtr<IProgress> pProgress;
|
---|
114 | SafeArray<MediumVariant_T> variant;
|
---|
115 | variant.push_back(MediumVariant_Standard);
|
---|
116 | TEST_COM_SUCCESS(hTest, pMedium->CreateBaseStorage(_1M, ComSafeArrayAsInParam(variant), pProgress.asOutParam()), "create base storage");
|
---|
117 | if (!pProgress.isNull())
|
---|
118 | TEST_COM_SUCCESS(hTest, pProgress->WaitForCompletion(30000), "waiting for completion of create");
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (!RTTestSubErrorCount(hTest))
|
---|
123 | {
|
---|
124 | RTTestSub(hTest, "Write locks");
|
---|
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(&mediumState), "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(&mediumState), "nested write lock succeeded");
|
---|
138 |
|
---|
139 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock write state");
|
---|
140 | if (mediumState != MediumState_LockedWrite)
|
---|
141 | RTTestFailed(hTest, "wrong after nested lock write medium state %d", mediumState);
|
---|
142 |
|
---|
143 | TEST_COM_SUCCESS(hTest, pMedium->UnlockWrite(&mediumState), "write unlock");
|
---|
144 |
|
---|
145 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock write state");
|
---|
146 | if (mediumState != MediumState_Created)
|
---|
147 | RTTestFailed(hTest, "wrong unlock write medium state %d", mediumState);
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (!RTTestSubErrorCount(hTest))
|
---|
151 | {
|
---|
152 | RTTestSub(hTest, "Read locks");
|
---|
153 |
|
---|
154 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
155 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
156 | if (mediumState != MediumState_Created)
|
---|
157 | RTTestFailed(hTest, "wrong medium state %d", mediumState);
|
---|
158 |
|
---|
159 | TEST_COM_SUCCESS(hTest, pMedium->LockRead(&mediumState), "read lock");
|
---|
160 |
|
---|
161 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock read state");
|
---|
162 | if (mediumState != MediumState_LockedRead)
|
---|
163 | RTTestFailed(hTest, "wrong lock read medium state %d", mediumState);
|
---|
164 |
|
---|
165 | TEST_COM_SUCCESS(hTest, pMedium->LockRead(&mediumState), "nested read lock failed");
|
---|
166 |
|
---|
167 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock read state");
|
---|
168 | if (mediumState != MediumState_LockedRead)
|
---|
169 | RTTestFailed(hTest, "wrong after nested lock read medium state %d", mediumState);
|
---|
170 |
|
---|
171 | TEST_COM_SUCCESS(hTest, pMedium->UnlockRead(&mediumState), "read nested unlock");
|
---|
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 | TEST_COM_SUCCESS(hTest, pMedium->UnlockRead(&mediumState), "read unlock");
|
---|
178 |
|
---|
179 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock read state");
|
---|
180 | if (mediumState != MediumState_Created)
|
---|
181 | RTTestFailed(hTest, "wrong unlock read medium state %d", mediumState);
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (!RTTestSubErrorCount(hTest))
|
---|
185 | {
|
---|
186 | RTTestSub(hTest, "Mixing write and read locks");
|
---|
187 |
|
---|
188 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
189 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
190 | if (mediumState != MediumState_Created)
|
---|
191 | RTTestFailed(hTest, "wrong medium state %d", mediumState);
|
---|
192 |
|
---|
193 | TEST_COM_SUCCESS(hTest, pMedium->LockWrite(&mediumState), "write lock");
|
---|
194 |
|
---|
195 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock write state");
|
---|
196 | if (mediumState != MediumState_LockedWrite)
|
---|
197 | RTTestFailed(hTest, "wrong lock write medium state %d", mediumState);
|
---|
198 |
|
---|
199 | TEST_COM_FAILURE(hTest, pMedium->LockRead(&mediumState), "write+read lock succeeded");
|
---|
200 |
|
---|
201 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock write state");
|
---|
202 | if (mediumState != MediumState_LockedWrite)
|
---|
203 | RTTestFailed(hTest, "wrong after nested lock write medium state %d", mediumState);
|
---|
204 |
|
---|
205 | TEST_COM_SUCCESS(hTest, pMedium->UnlockWrite(&mediumState), "write unlock");
|
---|
206 |
|
---|
207 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock write state");
|
---|
208 | if (mediumState != MediumState_Created)
|
---|
209 | RTTestFailed(hTest, "wrong unlock write medium state %d", mediumState);
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (!RTTestSubErrorCount(hTest))
|
---|
213 | {
|
---|
214 | RTTestSub(hTest, "Mixing read and write locks");
|
---|
215 |
|
---|
216 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
217 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
218 | if (mediumState != MediumState_Created)
|
---|
219 | RTTestFailed(hTest, "wrong medium state %d", mediumState);
|
---|
220 |
|
---|
221 | TEST_COM_SUCCESS(hTest, pMedium->LockRead(&mediumState), "read lock");
|
---|
222 |
|
---|
223 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock read state");
|
---|
224 | if (mediumState != MediumState_LockedRead)
|
---|
225 | RTTestFailed(hTest, "wrong lock read medium state %d", mediumState);
|
---|
226 |
|
---|
227 | TEST_COM_FAILURE(hTest, pMedium->LockWrite(&mediumState), "read+write lock succeeded");
|
---|
228 |
|
---|
229 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock read state");
|
---|
230 | if (mediumState != MediumState_LockedRead)
|
---|
231 | RTTestFailed(hTest, "wrong after nested lock read medium state %d", mediumState);
|
---|
232 |
|
---|
233 | TEST_COM_SUCCESS(hTest, pMedium->UnlockRead(&mediumState), "read unlock");
|
---|
234 |
|
---|
235 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock read state");
|
---|
236 | if (mediumState != MediumState_Created)
|
---|
237 | RTTestFailed(hTest, "wrong unlock read medium state %d", mediumState);
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (!RTTestSubErrorCount(hTest))
|
---|
241 | {
|
---|
242 | RTTestSub(hTest, "Write locks, read unlocks");
|
---|
243 |
|
---|
244 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
245 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
246 | if (mediumState != MediumState_Created)
|
---|
247 | RTTestFailed(hTest, "wrong medium state %d", mediumState);
|
---|
248 |
|
---|
249 | TEST_COM_SUCCESS(hTest, pMedium->LockWrite(&mediumState), "write lock");
|
---|
250 |
|
---|
251 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock write state");
|
---|
252 | if (mediumState != MediumState_LockedWrite)
|
---|
253 | RTTestFailed(hTest, "wrong lock write medium state %d", mediumState);
|
---|
254 |
|
---|
255 | TEST_COM_FAILURE(hTest, pMedium->UnlockRead(&mediumState), "read unlocking a write lock succeeded");
|
---|
256 |
|
---|
257 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after read unlock write state");
|
---|
258 | if (mediumState != MediumState_LockedWrite)
|
---|
259 | RTTestFailed(hTest, "wrong after read lock write medium state %d", mediumState);
|
---|
260 |
|
---|
261 | TEST_COM_SUCCESS(hTest, pMedium->UnlockWrite(&mediumState), "write unlock");
|
---|
262 |
|
---|
263 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock write state");
|
---|
264 | if (mediumState != MediumState_Created)
|
---|
265 | RTTestFailed(hTest, "wrong unlock write medium state %d", mediumState);
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (!RTTestSubErrorCount(hTest))
|
---|
269 | {
|
---|
270 | RTTestSub(hTest, "Read locks, write unlocks");
|
---|
271 |
|
---|
272 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
273 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
274 | if (mediumState != MediumState_Created)
|
---|
275 | RTTestFailed(hTest, "wrong medium state %d", mediumState);
|
---|
276 |
|
---|
277 | TEST_COM_SUCCESS(hTest, pMedium->LockRead(&mediumState), "read lock");
|
---|
278 |
|
---|
279 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock read state");
|
---|
280 | if (mediumState != MediumState_LockedRead)
|
---|
281 | RTTestFailed(hTest, "wrong lock write medium state %d", mediumState);
|
---|
282 |
|
---|
283 | TEST_COM_FAILURE(hTest, pMedium->UnlockWrite(&mediumState), "write unlocking a read lock succeeded");
|
---|
284 |
|
---|
285 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after write unlock read state");
|
---|
286 | if (mediumState != MediumState_LockedRead)
|
---|
287 | RTTestFailed(hTest, "wrong after write lock read medium state %d", mediumState);
|
---|
288 |
|
---|
289 | TEST_COM_SUCCESS(hTest, pMedium->UnlockRead(&mediumState), "read unlock");
|
---|
290 |
|
---|
291 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock read state");
|
---|
292 | if (mediumState != MediumState_Created)
|
---|
293 | RTTestFailed(hTest, "wrong unlock read medium state %d", mediumState);
|
---|
294 | }
|
---|
295 |
|
---|
296 | /* Cleanup, also part of the testcase */
|
---|
297 |
|
---|
298 | if (!pMedium.isNull())
|
---|
299 | {
|
---|
300 | RTTestSub(hTest, "Closing medium");
|
---|
301 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
302 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
303 | if (mediumState == MediumState_Created)
|
---|
304 | {
|
---|
305 | ComPtr<IProgress> pProgress;
|
---|
306 | TEST_COM_SUCCESS(hTest, pMedium->DeleteStorage(pProgress.asOutParam()), "deleting storage");
|
---|
307 | if (!pProgress.isNull())
|
---|
308 | TEST_COM_SUCCESS(hTest, pProgress->WaitForCompletion(30000), "waiting for completion of delete");
|
---|
309 | }
|
---|
310 | TEST_COM_SUCCESS(hTest, pMedium->Close(), "closing");
|
---|
311 | pMedium.setNull();
|
---|
312 | }
|
---|
313 |
|
---|
314 | pVirtualBox.setNull();
|
---|
315 |
|
---|
316 | /* Make sure that there are no object references alive here, XPCOM does
|
---|
317 | * a very bad job at cleaning up such leftovers, spitting out warning
|
---|
318 | * messages in a debug build. */
|
---|
319 |
|
---|
320 | if (fComInit)
|
---|
321 | {
|
---|
322 | RTTestIPrintf(RTTESTLVL_DEBUG, "Shutting down COM...\n");
|
---|
323 | Shutdown();
|
---|
324 | }
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * Summary.
|
---|
328 | */
|
---|
329 | return RTTestSummaryAndDestroy(hTest);
|
---|
330 | }
|
---|