1 | /* $Id: tstMediumLock.cpp 54438 2015-02-24 11:09:17Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * Medium lock test cases.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2013-2015 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->CreateMedium(Bstr("VDI").raw(), Bstr(szPathTemp).raw(), AccessMode_ReadWrite, DeviceType_HardDisk, 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 | ComPtr<IToken> pToken1, pToken2;
|
---|
126 |
|
---|
127 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
128 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
129 | if (mediumState != MediumState_Created)
|
---|
130 | RTTestFailed(hTest, "wrong medium state %d", mediumState);
|
---|
131 |
|
---|
132 | TEST_COM_SUCCESS(hTest, pMedium->LockWrite(pToken1.asOutParam()), "write lock");
|
---|
133 |
|
---|
134 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock write state");
|
---|
135 | if (mediumState != MediumState_LockedWrite)
|
---|
136 | RTTestFailed(hTest, "wrong lock write medium state %d", mediumState);
|
---|
137 |
|
---|
138 | TEST_COM_FAILURE(hTest, pMedium->LockWrite(pToken2.asOutParam()), "nested write lock succeeded");
|
---|
139 | if (!pToken2.isNull())
|
---|
140 | RTTestFailed(hTest, "pToken2 is not null");
|
---|
141 |
|
---|
142 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock write state");
|
---|
143 | if (mediumState != MediumState_LockedWrite)
|
---|
144 | RTTestFailed(hTest, "wrong after nested lock write medium state %d", mediumState);
|
---|
145 |
|
---|
146 | if (!pToken1.isNull())
|
---|
147 | TEST_COM_SUCCESS(hTest, pToken1->Abandon(), "write unlock");
|
---|
148 | else
|
---|
149 | RTTestFailed(hTest, "pToken1 is null");
|
---|
150 |
|
---|
151 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock write state");
|
---|
152 | if (mediumState != MediumState_Created)
|
---|
153 | RTTestFailed(hTest, "wrong unlock write medium state %d", mediumState);
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (!RTTestSubErrorCount(hTest))
|
---|
157 | {
|
---|
158 | RTTestSub(hTest, "Read locks");
|
---|
159 | ComPtr<IToken> pToken1, pToken2;
|
---|
160 |
|
---|
161 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
162 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
163 | if (mediumState != MediumState_Created)
|
---|
164 | RTTestFailed(hTest, "wrong medium state %d", mediumState);
|
---|
165 |
|
---|
166 | TEST_COM_SUCCESS(hTest, pMedium->LockRead(pToken1.asOutParam()), "read lock");
|
---|
167 |
|
---|
168 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock read state");
|
---|
169 | if (mediumState != MediumState_LockedRead)
|
---|
170 | RTTestFailed(hTest, "wrong lock read medium state %d", mediumState);
|
---|
171 |
|
---|
172 | TEST_COM_SUCCESS(hTest, pMedium->LockRead(pToken2.asOutParam()), "nested read lock failed");
|
---|
173 |
|
---|
174 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock read state");
|
---|
175 | if (mediumState != MediumState_LockedRead)
|
---|
176 | RTTestFailed(hTest, "wrong after nested lock read medium state %d", mediumState);
|
---|
177 |
|
---|
178 | if (!pToken2.isNull())
|
---|
179 | TEST_COM_SUCCESS(hTest, pToken2->Abandon(), "read nested unlock");
|
---|
180 | else
|
---|
181 | RTTestFailed(hTest, "pToken2 is null");
|
---|
182 |
|
---|
183 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock read state");
|
---|
184 | if (mediumState != MediumState_LockedRead)
|
---|
185 | RTTestFailed(hTest, "wrong after nested lock read medium state %d", mediumState);
|
---|
186 |
|
---|
187 | if (!pToken1.isNull())
|
---|
188 | TEST_COM_SUCCESS(hTest, pToken1->Abandon(), "read nested unlock");
|
---|
189 | else
|
---|
190 | RTTestFailed(hTest, "pToken1 is null");
|
---|
191 |
|
---|
192 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock read state");
|
---|
193 | if (mediumState != MediumState_Created)
|
---|
194 | RTTestFailed(hTest, "wrong unlock read medium state %d", mediumState);
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (!RTTestSubErrorCount(hTest))
|
---|
198 | {
|
---|
199 | RTTestSub(hTest, "Mixing write and read locks");
|
---|
200 | ComPtr<IToken> pToken1, pToken2;
|
---|
201 |
|
---|
202 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
203 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
204 | if (mediumState != MediumState_Created)
|
---|
205 | RTTestFailed(hTest, "wrong medium state %d", mediumState);
|
---|
206 |
|
---|
207 | TEST_COM_SUCCESS(hTest, pMedium->LockWrite(pToken1.asOutParam()), "write lock");
|
---|
208 |
|
---|
209 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock write state");
|
---|
210 | if (mediumState != MediumState_LockedWrite)
|
---|
211 | RTTestFailed(hTest, "wrong lock write medium state %d", mediumState);
|
---|
212 |
|
---|
213 | TEST_COM_FAILURE(hTest, pMedium->LockRead(pToken2.asOutParam()), "write+read lock succeeded");
|
---|
214 | if (!pToken2.isNull())
|
---|
215 | RTTestFailed(hTest, "pToken2 is not null");
|
---|
216 |
|
---|
217 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock write state");
|
---|
218 | if (mediumState != MediumState_LockedWrite)
|
---|
219 | RTTestFailed(hTest, "wrong after nested lock write medium state %d", mediumState);
|
---|
220 |
|
---|
221 | if (!pToken1.isNull())
|
---|
222 | TEST_COM_SUCCESS(hTest, pToken1->Abandon(), "write unlock");
|
---|
223 | else
|
---|
224 | RTTestFailed(hTest, "pToken1 is null");
|
---|
225 |
|
---|
226 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock write state");
|
---|
227 | if (mediumState != MediumState_Created)
|
---|
228 | RTTestFailed(hTest, "wrong unlock write medium state %d", mediumState);
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (!RTTestSubErrorCount(hTest))
|
---|
232 | {
|
---|
233 | RTTestSub(hTest, "Mixing read and write locks");
|
---|
234 | ComPtr<IToken> pToken1, pToken2;
|
---|
235 |
|
---|
236 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
237 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
238 | if (mediumState != MediumState_Created)
|
---|
239 | RTTestFailed(hTest, "wrong medium state %d", mediumState);
|
---|
240 |
|
---|
241 | TEST_COM_SUCCESS(hTest, pMedium->LockRead(pToken1.asOutParam()), "read lock");
|
---|
242 |
|
---|
243 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting lock read state");
|
---|
244 | if (mediumState != MediumState_LockedRead)
|
---|
245 | RTTestFailed(hTest, "wrong lock read medium state %d", mediumState);
|
---|
246 |
|
---|
247 | TEST_COM_FAILURE(hTest, pMedium->LockWrite(pToken2.asOutParam()), "read+write lock succeeded");
|
---|
248 | if (!pToken2.isNull())
|
---|
249 | RTTestFailed(hTest, "pToken2 is not null");
|
---|
250 |
|
---|
251 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting after nested lock read state");
|
---|
252 | if (mediumState != MediumState_LockedRead)
|
---|
253 | RTTestFailed(hTest, "wrong after nested lock read medium state %d", mediumState);
|
---|
254 |
|
---|
255 | if (!pToken1.isNull())
|
---|
256 | TEST_COM_SUCCESS(hTest, pToken1->Abandon(), "read unlock");
|
---|
257 | else
|
---|
258 | RTTestFailed(hTest, "pToken1 is null");
|
---|
259 |
|
---|
260 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting unlock read state");
|
---|
261 | if (mediumState != MediumState_Created)
|
---|
262 | RTTestFailed(hTest, "wrong unlock read medium state %d", mediumState);
|
---|
263 | }
|
---|
264 |
|
---|
265 | /* Cleanup, also part of the testcase */
|
---|
266 |
|
---|
267 | if (!pMedium.isNull())
|
---|
268 | {
|
---|
269 | RTTestSub(hTest, "Closing medium");
|
---|
270 | MediumState_T mediumState = MediumState_NotCreated;
|
---|
271 | TEST_COM_SUCCESS(hTest, pMedium->COMGETTER(State)(&mediumState), "getting state");
|
---|
272 | if (mediumState == MediumState_Created)
|
---|
273 | {
|
---|
274 | ComPtr<IProgress> pProgress;
|
---|
275 | TEST_COM_SUCCESS(hTest, pMedium->DeleteStorage(pProgress.asOutParam()), "deleting storage");
|
---|
276 | if (!pProgress.isNull())
|
---|
277 | TEST_COM_SUCCESS(hTest, pProgress->WaitForCompletion(30000), "waiting for completion of delete");
|
---|
278 | }
|
---|
279 | TEST_COM_SUCCESS(hTest, pMedium->Close(), "closing");
|
---|
280 | pMedium.setNull();
|
---|
281 | }
|
---|
282 |
|
---|
283 | pVirtualBox.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 | }
|
---|