1 | /* $Id: tstRTDvm.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - IPRT Disk Volume Management (DVM)
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/dvm.h>
|
---|
42 |
|
---|
43 | #include <iprt/err.h>
|
---|
44 | #include <iprt/test.h>
|
---|
45 | #include <iprt/file.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 | #include <iprt/vfs.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Structures and Typedefs *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 |
|
---|
54 |
|
---|
55 | static int tstRTDvmVolume(RTTEST hTest, RTVFSFILE hVfsDisk, unsigned cNesting)
|
---|
56 | {
|
---|
57 | char szPrefix[100];
|
---|
58 | int rc = VINF_SUCCESS;
|
---|
59 |
|
---|
60 | RT_ZERO(szPrefix);
|
---|
61 |
|
---|
62 | if (cNesting < sizeof(szPrefix) - 1)
|
---|
63 | {
|
---|
64 | for (unsigned i = 0; i < cNesting; i++)
|
---|
65 | szPrefix[i] = '\t';
|
---|
66 | }
|
---|
67 |
|
---|
68 | RTTestSubF(hTest, "Create DVM");
|
---|
69 | RTDVM hVolMgr;
|
---|
70 | rc = RTDvmCreate(&hVolMgr, hVfsDisk, 512, 0 /*fFlags*/);
|
---|
71 | if (RT_FAILURE(rc))
|
---|
72 | {
|
---|
73 | RTTestIFailed("RTDvmCreate -> %Rrc", rc);
|
---|
74 | return RTTestSummaryAndDestroy(hTest);
|
---|
75 | }
|
---|
76 |
|
---|
77 | RTTestSubF(hTest, "Open volume map");
|
---|
78 | rc = RTDvmMapOpen(hVolMgr);
|
---|
79 | if ( RT_FAILURE(rc)
|
---|
80 | && rc != VERR_NOT_SUPPORTED)
|
---|
81 | {
|
---|
82 | RTTestIFailed("RTDvmOpen -> %Rrc", rc);
|
---|
83 | RTDvmRelease(hVolMgr);
|
---|
84 | return RTTestSummaryAndDestroy(hTest);
|
---|
85 | }
|
---|
86 | if (rc == VERR_NOT_SUPPORTED)
|
---|
87 | {
|
---|
88 | RTDvmRelease(hVolMgr);
|
---|
89 | return VINF_SUCCESS;
|
---|
90 | }
|
---|
91 |
|
---|
92 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Successfully opened map with format: %s.\n", szPrefix, RTDvmMapGetFormatName(hVolMgr));
|
---|
93 |
|
---|
94 | /* Dump all volumes. */
|
---|
95 | RTTestSubF(hTest, "Dump volumes");
|
---|
96 | uint32_t cVolume = 0;
|
---|
97 | RTDVMVOLUME hVol;
|
---|
98 |
|
---|
99 | rc = RTDvmMapQueryFirstVolume(hVolMgr, &hVol);
|
---|
100 |
|
---|
101 | while (RT_SUCCESS(rc))
|
---|
102 | {
|
---|
103 | char *pszVolName = NULL;
|
---|
104 | RTDVMVOLTYPE enmVolType = RTDvmVolumeGetType(hVol);
|
---|
105 | uint64_t fVolFlags = RTDvmVolumeGetFlags(hVol);
|
---|
106 |
|
---|
107 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume %u:\n", szPrefix, cVolume);
|
---|
108 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume type %s\n", szPrefix, RTDvmVolumeTypeGetDescr(enmVolType));
|
---|
109 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume size %llu\n", szPrefix, RTDvmVolumeGetSize(hVol));
|
---|
110 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume flags %s %s %s\n", szPrefix,
|
---|
111 | fVolFlags & DVMVOLUME_FLAGS_BOOTABLE ? "Bootable" : "",
|
---|
112 | fVolFlags & DVMVOLUME_FLAGS_ACTIVE ? "Active" : "",
|
---|
113 | fVolFlags & DVMVOLUME_F_CONTIGUOUS ? "Contiguous" : "");
|
---|
114 |
|
---|
115 | rc = RTDvmVolumeQueryName(hVol, &pszVolName);
|
---|
116 | if (RT_SUCCESS(rc))
|
---|
117 | {
|
---|
118 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume name %s.\n", szPrefix, pszVolName);
|
---|
119 | RTStrFree(pszVolName);
|
---|
120 | }
|
---|
121 | else if (rc != VERR_NOT_SUPPORTED)
|
---|
122 | RTTestIFailed("RTDvmVolumeQueryName -> %Rrc", rc);
|
---|
123 | else
|
---|
124 | rc = VINF_SUCCESS;
|
---|
125 |
|
---|
126 | if (fVolFlags & DVMVOLUME_F_CONTIGUOUS)
|
---|
127 | {
|
---|
128 | uint64_t offStart, offEnd;
|
---|
129 | rc = RTDvmVolumeQueryRange(hVol, &offStart, &offEnd);
|
---|
130 | if (RT_SUCCESS(rc))
|
---|
131 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume range %llu:%llu\n", szPrefix, offStart, offEnd);
|
---|
132 | else
|
---|
133 | RTTestIFailed("RTDvmVolumeQueryRange -> %Rrc", rc);
|
---|
134 | }
|
---|
135 |
|
---|
136 | RTTestIPrintf(RTTESTLVL_ALWAYS, "\n");
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * Query all volumes which might be inside this.
|
---|
140 | * (think of MBR partitions with a bsdlabel inside)
|
---|
141 | */
|
---|
142 | RTVFSFILE hVfsVol;
|
---|
143 | rc = RTDvmVolumeCreateVfsFile(hVol, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE, &hVfsVol);
|
---|
144 | if (RT_SUCCESS(rc))
|
---|
145 | {
|
---|
146 | rc = tstRTDvmVolume(hTest, hVfsVol, cNesting + 1);
|
---|
147 | RTVfsFileRelease(hVfsVol);
|
---|
148 | }
|
---|
149 | else
|
---|
150 | RTTestIFailed("RTDvmVolumeCreateVfsFile -> %Rrc", rc);
|
---|
151 |
|
---|
152 | RTDVMVOLUME hVolNext;
|
---|
153 | rc = RTDvmMapQueryNextVolume(hVolMgr, hVol, &hVolNext);
|
---|
154 | RTDvmVolumeRelease(hVol);
|
---|
155 | hVol = hVolNext;
|
---|
156 | cVolume++;
|
---|
157 | }
|
---|
158 |
|
---|
159 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Dumped %u volumes\n", szPrefix, cVolume);
|
---|
160 |
|
---|
161 | if ( rc == VERR_DVM_MAP_EMPTY
|
---|
162 | || rc == VERR_DVM_MAP_NO_VOLUME)
|
---|
163 | rc = VINF_SUCCESS;
|
---|
164 |
|
---|
165 | RTTESTI_CHECK(rc == VINF_SUCCESS);
|
---|
166 |
|
---|
167 | RTDvmRelease(hVolMgr);
|
---|
168 |
|
---|
169 | return rc;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | int main(int argc, char **argv)
|
---|
174 | {
|
---|
175 | /*
|
---|
176 | * Initialize IPRT and create the test.
|
---|
177 | */
|
---|
178 | RTTEST hTest;
|
---|
179 | int rc = RTTestInitAndCreate("tstRTDvm", &hTest);
|
---|
180 | if (rc)
|
---|
181 | return rc;
|
---|
182 | RTTestBanner(hTest);
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * If no args, display usage.
|
---|
186 | */
|
---|
187 | if (argc < 2)
|
---|
188 | {
|
---|
189 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Syntax: %s <image>\n", argv[0]);
|
---|
190 | return RTTestSkipAndDestroy(hTest, "Missing required arguments\n");
|
---|
191 | }
|
---|
192 |
|
---|
193 | RTVFSFILE hVfsDisk;
|
---|
194 | rc = RTVfsFileOpenNormal(argv[1], RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE, &hVfsDisk);
|
---|
195 | if (RT_FAILURE(rc))
|
---|
196 | {
|
---|
197 | RTTestIFailed("RTVfsFileOpenNormal -> %Rrc", rc);
|
---|
198 | return RTTestSummaryAndDestroy(hTest);
|
---|
199 | }
|
---|
200 |
|
---|
201 | uint64_t cb = 0;
|
---|
202 | rc = RTVfsFileQuerySize(hVfsDisk, &cb);
|
---|
203 | if ( RT_FAILURE(rc)
|
---|
204 | || cb % 512 != 0) /* Assume 512 byte sector size. */
|
---|
205 | {
|
---|
206 | RTTestIFailed("RTVfsFileQuerySize -> %Rrc", rc);
|
---|
207 | return RTTestSummaryAndDestroy(hTest);
|
---|
208 | }
|
---|
209 |
|
---|
210 | rc = tstRTDvmVolume(hTest, hVfsDisk, 0);
|
---|
211 |
|
---|
212 | RTTESTI_CHECK(rc == VINF_SUCCESS);
|
---|
213 |
|
---|
214 | RTVfsFileRelease(hVfsDisk);
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Summary
|
---|
218 | */
|
---|
219 | return RTTestSummaryAndDestroy(hTest);
|
---|
220 | }
|
---|
221 |
|
---|