1 | /** @file
|
---|
2 | *
|
---|
3 | * Simple VBox HDD container test utility. Only fast tests.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 <VBox/err.h>
|
---|
19 | #include <VBox/vd.h>
|
---|
20 | #include <iprt/string.h>
|
---|
21 | #include <iprt/stream.h>
|
---|
22 | #include <iprt/file.h>
|
---|
23 | #include <iprt/mem.h>
|
---|
24 | #include <iprt/initterm.h>
|
---|
25 | #include <iprt/rand.h>
|
---|
26 | #include "stdio.h"
|
---|
27 | #include "stdlib.h"
|
---|
28 |
|
---|
29 | /*******************************************************************************
|
---|
30 | * Global Variables *
|
---|
31 | *******************************************************************************/
|
---|
32 | /** The error count. */
|
---|
33 | unsigned g_cErrors = 0;
|
---|
34 |
|
---|
35 | static struct KeyValuePair {
|
---|
36 | const char *key;
|
---|
37 | const char *value;
|
---|
38 | } aCfgNode[] = {
|
---|
39 | { "TargetName", "test" },
|
---|
40 | { "LUN", "1" },
|
---|
41 | { "TargetAddress", "address" },
|
---|
42 | { NULL, NULL }
|
---|
43 | };
|
---|
44 |
|
---|
45 | static bool tstAreKeysValid(void *pvUser, const char *pszzValid)
|
---|
46 | {
|
---|
47 | return true;
|
---|
48 | }
|
---|
49 |
|
---|
50 | static const char *tstGetValueByKey(const char *pszKey)
|
---|
51 | {
|
---|
52 | for (int i = 0; aCfgNode[i].key; i++)
|
---|
53 | if (!strcmp(aCfgNode[i].key, pszKey))
|
---|
54 | return aCfgNode[i].value;
|
---|
55 | return NULL;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static int tstQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
|
---|
59 | {
|
---|
60 | const char *pszValue = tstGetValueByKey(pszName);
|
---|
61 | if (!pszValue)
|
---|
62 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
63 | *pcbValue = strlen(pszValue) + 1;
|
---|
64 | return VINF_SUCCESS;
|
---|
65 | }
|
---|
66 |
|
---|
67 | static int tstQuery(void *pvUser, const char *pszName, char *pszValue, size_t cchValue)
|
---|
68 | {
|
---|
69 | const char *pszTmp = tstGetValueByKey(pszName);
|
---|
70 | if (!pszValue)
|
---|
71 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
72 | size_t cchTmp = strlen(pszTmp) + 1;
|
---|
73 | if (cchValue < cchTmp)
|
---|
74 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
75 | memcpy(pszValue, pszTmp, cchTmp);
|
---|
76 | return VINF_SUCCESS;
|
---|
77 | }
|
---|
78 |
|
---|
79 | static const char *tstVDDeviceType(VDTYPE enmType)
|
---|
80 | {
|
---|
81 | switch (enmType)
|
---|
82 | {
|
---|
83 | case VDTYPE_HDD:
|
---|
84 | return "HardDisk";
|
---|
85 | case VDTYPE_DVD:
|
---|
86 | return "DVD";
|
---|
87 | case VDTYPE_FLOPPY:
|
---|
88 | return "Floppy";
|
---|
89 | default:
|
---|
90 | return "Unknown";
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | static int tstVDBackendInfo(void)
|
---|
95 | {
|
---|
96 | int rc;
|
---|
97 | #define MAX_BACKENDS 100
|
---|
98 | VDBACKENDINFO aVDInfo[MAX_BACKENDS];
|
---|
99 | unsigned cEntries;
|
---|
100 |
|
---|
101 | #define CHECK(str) \
|
---|
102 | do \
|
---|
103 | { \
|
---|
104 | RTPrintf("%s rc=%Rrc\n", str, rc); \
|
---|
105 | if (RT_FAILURE(rc)) \
|
---|
106 | return rc; \
|
---|
107 | } while (0)
|
---|
108 |
|
---|
109 | rc = VDBackendInfo(MAX_BACKENDS, aVDInfo, &cEntries);
|
---|
110 | CHECK("VDBackendInfo()");
|
---|
111 |
|
---|
112 | for (unsigned i=0; i < cEntries; i++)
|
---|
113 | {
|
---|
114 | RTPrintf("Backend %u: name=%s capabilities=%#06x extensions=",
|
---|
115 | i, aVDInfo[i].pszBackend, aVDInfo[i].uBackendCaps);
|
---|
116 | if (aVDInfo[i].paFileExtensions)
|
---|
117 | {
|
---|
118 | PCVDFILEEXTENSION pa = aVDInfo[i].paFileExtensions;
|
---|
119 | while (pa->pszExtension != NULL)
|
---|
120 | {
|
---|
121 | if (pa != aVDInfo[i].paFileExtensions)
|
---|
122 | RTPrintf(",");
|
---|
123 | RTPrintf("%s (%s)", pa->pszExtension, tstVDDeviceType(pa->enmType));
|
---|
124 | pa++;
|
---|
125 | }
|
---|
126 | if (pa == aVDInfo[i].paFileExtensions)
|
---|
127 | RTPrintf("<EMPTY>");
|
---|
128 | }
|
---|
129 | else
|
---|
130 | RTPrintf("<NONE>");
|
---|
131 | RTPrintf(" config=");
|
---|
132 | if (aVDInfo[i].paConfigInfo)
|
---|
133 | {
|
---|
134 | PCVDCONFIGINFO pa = aVDInfo[i].paConfigInfo;
|
---|
135 | while (pa->pszKey != NULL)
|
---|
136 | {
|
---|
137 | if (pa != aVDInfo[i].paConfigInfo)
|
---|
138 | RTPrintf(",");
|
---|
139 | RTPrintf("(key=%s type=", pa->pszKey);
|
---|
140 | switch (pa->enmValueType)
|
---|
141 | {
|
---|
142 | case VDCFGVALUETYPE_INTEGER:
|
---|
143 | RTPrintf("integer");
|
---|
144 | break;
|
---|
145 | case VDCFGVALUETYPE_STRING:
|
---|
146 | RTPrintf("string");
|
---|
147 | break;
|
---|
148 | case VDCFGVALUETYPE_BYTES:
|
---|
149 | RTPrintf("bytes");
|
---|
150 | break;
|
---|
151 | default:
|
---|
152 | RTPrintf("INVALID!");
|
---|
153 | }
|
---|
154 | RTPrintf(" default=");
|
---|
155 | if (pa->pszDefaultValue)
|
---|
156 | RTPrintf("%s", pa->pszDefaultValue);
|
---|
157 | else
|
---|
158 | RTPrintf("<NONE>");
|
---|
159 | RTPrintf(" flags=");
|
---|
160 | if (!pa->uKeyFlags)
|
---|
161 | RTPrintf("none");
|
---|
162 | unsigned cFlags = 0;
|
---|
163 | if (pa->uKeyFlags & VD_CFGKEY_MANDATORY)
|
---|
164 | {
|
---|
165 | if (cFlags)
|
---|
166 | RTPrintf(",");
|
---|
167 | RTPrintf("mandatory");
|
---|
168 | cFlags++;
|
---|
169 | }
|
---|
170 | if (pa->uKeyFlags & VD_CFGKEY_EXPERT)
|
---|
171 | {
|
---|
172 | if (cFlags)
|
---|
173 | RTPrintf(",");
|
---|
174 | RTPrintf("expert");
|
---|
175 | cFlags++;
|
---|
176 | }
|
---|
177 | RTPrintf(")");
|
---|
178 | pa++;
|
---|
179 | }
|
---|
180 | if (pa == aVDInfo[i].paConfigInfo)
|
---|
181 | RTPrintf("<EMPTY>");
|
---|
182 | }
|
---|
183 | else
|
---|
184 | RTPrintf("<NONE>");
|
---|
185 | RTPrintf("\n");
|
---|
186 |
|
---|
187 | PVDINTERFACE pVDIfs = NULL;
|
---|
188 | VDINTERFACECONFIG ic;
|
---|
189 |
|
---|
190 | ic.pfnAreKeysValid = tstAreKeysValid;
|
---|
191 | ic.pfnQuerySize = tstQuerySize;
|
---|
192 | ic.pfnQuery = tstQuery;
|
---|
193 |
|
---|
194 | rc = VDInterfaceAdd(&ic.Core, "tstVD-2_Config", VDINTERFACETYPE_CONFIG,
|
---|
195 | NULL, sizeof(VDINTERFACECONFIG), &pVDIfs);
|
---|
196 | AssertRC(rc);
|
---|
197 |
|
---|
198 | char *pszLocation, *pszName;
|
---|
199 | rc = aVDInfo[i].pfnComposeLocation(pVDIfs, &pszLocation);
|
---|
200 | CHECK("pfnComposeLocation()");
|
---|
201 | if (pszLocation)
|
---|
202 | {
|
---|
203 | RTMemFree(pszLocation);
|
---|
204 | if (aVDInfo[i].uBackendCaps & VD_CAP_FILE)
|
---|
205 | {
|
---|
206 | RTPrintf("Non-NULL location returned for file-based backend!\n");
|
---|
207 | return VERR_INTERNAL_ERROR;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | rc = aVDInfo[i].pfnComposeName(pVDIfs, &pszName);
|
---|
211 | CHECK("pfnComposeName()");
|
---|
212 | if (pszName)
|
---|
213 | {
|
---|
214 | RTMemFree(pszName);
|
---|
215 | if (aVDInfo[i].uBackendCaps & VD_CAP_FILE)
|
---|
216 | {
|
---|
217 | RTPrintf("Non-NULL name returned for file-based backend!\n");
|
---|
218 | return VERR_INTERNAL_ERROR;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | #undef CHECK
|
---|
224 | return 0;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | int main(int argc, char *argv[])
|
---|
229 | {
|
---|
230 | int rc;
|
---|
231 |
|
---|
232 | RTR3Init();
|
---|
233 | RTPrintf("tstVD-2: TESTING...\n");
|
---|
234 |
|
---|
235 | rc = tstVDBackendInfo();
|
---|
236 | if (RT_FAILURE(rc))
|
---|
237 | {
|
---|
238 | RTPrintf("tstVD-2: getting backend info test failed! rc=%Rrc\n", rc);
|
---|
239 | g_cErrors++;
|
---|
240 | }
|
---|
241 |
|
---|
242 | rc = VDShutdown();
|
---|
243 | if (RT_FAILURE(rc))
|
---|
244 | {
|
---|
245 | RTPrintf("tstVD-2: unloading backends failed! rc=%Rrc\n", rc);
|
---|
246 | g_cErrors++;
|
---|
247 | }
|
---|
248 | /*
|
---|
249 | * Summary
|
---|
250 | */
|
---|
251 | if (!g_cErrors)
|
---|
252 | RTPrintf("tstVD-2: SUCCESS\n");
|
---|
253 | else
|
---|
254 | RTPrintf("tstVD-2: FAILURE - %d errors\n", g_cErrors);
|
---|
255 |
|
---|
256 | return !!g_cErrors;
|
---|
257 | }
|
---|
258 |
|
---|