summaryrefslogtreecommitdiffstats
path: root/src/VBox/Storage/testcase/tstVD-2.cpp
blob: 146b1be600ac162ae21de352c27995a4bb00ec68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/** @file
 *
 * Simple VBox HDD container test utility. Only fast tests.
 */

/*
 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include <VBox/err.h>
#include <VBox/vd.h>
#include <iprt/string.h>
#include <iprt/stream.h>
#include <iprt/file.h>
#include <iprt/mem.h>
#include <iprt/initterm.h>
#include <iprt/rand.h>
#include "stdio.h"
#include "stdlib.h"


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** The error count. */
unsigned g_cErrors = 0;

static struct KeyValuePair {
    const char *key;
    const char *value;
} aCfgNode[] = {
    { "TargetName", "test" },
    { "LUN", "1" },
    { "TargetAddress", "address" },
    { NULL, NULL }
};

static DECLCALLBACK(bool) tstAreKeysValid(void *pvUser, const char *pszzValid)
{
    RT_NOREF2(pvUser, pszzValid);
    return true;
}

static const char *tstGetValueByKey(const char *pszKey)
{
    for (int i = 0; aCfgNode[i].key; i++)
        if (!strcmp(aCfgNode[i].key, pszKey))
            return aCfgNode[i].value;
    return NULL;
}

static DECLCALLBACK(int) tstQuerySize(void *pvUser, const char *pszName, size_t *pcbValue)
{
    RT_NOREF1(pvUser);
    const char *pszValue = tstGetValueByKey(pszName);
    if (!pszValue)
        return VERR_CFGM_VALUE_NOT_FOUND;
    *pcbValue = strlen(pszValue) + 1;
    return VINF_SUCCESS;
}

static DECLCALLBACK(int) tstQuery(void *pvUser, const char *pszName, char *pszValue, size_t cchValue)
{
    RT_NOREF1(pvUser);
    const char *pszTmp = tstGetValueByKey(pszName);
    if (!pszValue)
        return VERR_CFGM_VALUE_NOT_FOUND;
    size_t cchTmp = strlen(pszTmp) + 1;
    if (cchValue < cchTmp)
        return VERR_CFGM_NOT_ENOUGH_SPACE;
    memcpy(pszValue, pszTmp, cchTmp);
    return VINF_SUCCESS;
}

static const char *tstVDDeviceType(VDTYPE enmType)
{
    switch (enmType)
    {
        case VDTYPE_HDD:
            return "HardDisk";
        case VDTYPE_OPTICAL_DISC:
            return "OpticalDisc";
        case VDTYPE_FLOPPY:
            return "Floppy";
        default:
            return "Unknown";
    }
}

static int tstVDBackendInfo(void)
{
    int rc;
#define MAX_BACKENDS 100
    VDBACKENDINFO aVDInfo[MAX_BACKENDS];
    unsigned cEntries;

#define CHECK(str) \
    do \
    { \
        RTPrintf("%s rc=%Rrc\n", str, rc); \
        if (RT_FAILURE(rc)) \
            return rc; \
    } while (0)

    rc = VDBackendInfo(MAX_BACKENDS, aVDInfo, &cEntries);
    CHECK("VDBackendInfo()");

    for (unsigned i=0; i < cEntries; i++)
    {
        RTPrintf("Backend %u: name=%s capabilities=%#06x extensions=",
                 i, aVDInfo[i].pszBackend, aVDInfo[i].uBackendCaps);
        if (aVDInfo[i].paFileExtensions)
        {
            PCVDFILEEXTENSION pa = aVDInfo[i].paFileExtensions;
            while (pa->pszExtension != NULL)
            {
                if (pa != aVDInfo[i].paFileExtensions)
                    RTPrintf(",");
                RTPrintf("%s (%s)", pa->pszExtension, tstVDDeviceType(pa->enmType));
                pa++;
            }
            if (pa == aVDInfo[i].paFileExtensions)
                RTPrintf("<EMPTY>");
        }
        else
            RTPrintf("<NONE>");
        RTPrintf(" config=");
        if (aVDInfo[i].paConfigInfo)
        {
            PCVDCONFIGINFO pa = aVDInfo[i].paConfigInfo;
            while (pa->pszKey != NULL)
            {
                if (pa != aVDInfo[i].paConfigInfo)
                    RTPrintf(",");
                RTPrintf("(key=%s type=", pa->pszKey);
                switch (pa->enmValueType)
                {
                    case VDCFGVALUETYPE_INTEGER:
                        RTPrintf("integer");
                        break;
                    case VDCFGVALUETYPE_STRING:
                        RTPrintf("string");
                        break;
                    case VDCFGVALUETYPE_BYTES:
                        RTPrintf("bytes");
                        break;
                    default:
                        RTPrintf("INVALID!");
                }
                RTPrintf(" default=");
                if (pa->pszDefaultValue)
                    RTPrintf("%s", pa->pszDefaultValue);
                else
                    RTPrintf("<NONE>");
                RTPrintf(" flags=");
                if (!pa->uKeyFlags)
                    RTPrintf("none");
                unsigned cFlags = 0;
                if (pa->uKeyFlags & VD_CFGKEY_MANDATORY)
                {
                    if (cFlags)
                        RTPrintf(",");
                    RTPrintf("mandatory");
                    cFlags++;
                }
                if (pa->uKeyFlags & VD_CFGKEY_EXPERT)
                {
                    if (cFlags)
                        RTPrintf(",");
                    RTPrintf("expert");
                    cFlags++;
                }
                RTPrintf(")");
                pa++;
            }
            if (pa == aVDInfo[i].paConfigInfo)
                RTPrintf("<EMPTY>");
        }
        else
            RTPrintf("<NONE>");
        RTPrintf("\n");

        PVDINTERFACE pVDIfs = NULL;
        VDINTERFACECONFIG ic;

        ic.pfnAreKeysValid = tstAreKeysValid;
        ic.pfnQuerySize    = tstQuerySize;
        ic.pfnQuery        = tstQuery;

        rc = VDInterfaceAdd(&ic.Core, "tstVD-2_Config", VDINTERFACETYPE_CONFIG,
                            NULL, sizeof(VDINTERFACECONFIG), &pVDIfs);
        AssertRC(rc);

        char *pszLocation, *pszName;
        rc = aVDInfo[i].pfnComposeLocation(pVDIfs, &pszLocation);
        CHECK("pfnComposeLocation()");
        if (pszLocation)
        {
            RTMemFree(pszLocation);
            if (aVDInfo[i].uBackendCaps & VD_CAP_FILE)
            {
                RTPrintf("Non-NULL location returned for file-based backend!\n");
                return VERR_INTERNAL_ERROR;
            }
        }
        rc = aVDInfo[i].pfnComposeName(pVDIfs, &pszName);
        CHECK("pfnComposeName()");
        if (pszName)
        {
            RTMemFree(pszName);
            if (aVDInfo[i].uBackendCaps & VD_CAP_FILE)
            {
                RTPrintf("Non-NULL name returned for file-based backend!\n");
                return VERR_INTERNAL_ERROR;
            }
        }
    }

#undef CHECK
    return 0;
}


int main(int argc, char *argv[])
{
    int rc;

    RTR3InitExe(argc, &argv, 0);
    RTPrintf("tstVD-2: TESTING...\n");

    rc = tstVDBackendInfo();
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstVD-2: getting backend info test failed! rc=%Rrc\n", rc);
        g_cErrors++;
    }

    rc = VDShutdown();
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstVD-2: unloading backends failed! rc=%Rrc\n", rc);
        g_cErrors++;
    }
    /*
     * Summary
     */
    if (!g_cErrors)
        RTPrintf("tstVD-2: SUCCESS\n");
    else
        RTPrintf("tstVD-2: FAILURE - %d errors\n", g_cErrors);

    return !!g_cErrors;
}