VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTDvm.cpp@ 93138

Last change on this file since 93138 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1/* $Id: tstRTDvm.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - IPRT Disk Volume Management (DVM)
4 */
5
6/*
7 * Copyright (C) 2009-2022 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/dvm.h>
32
33#include <iprt/err.h>
34#include <iprt/test.h>
35#include <iprt/file.h>
36#include <iprt/string.h>
37#include <iprt/vfs.h>
38
39
40/*********************************************************************************************************************************
41* Structures and Typedefs *
42*********************************************************************************************************************************/
43
44
45static int tstRTDvmVolume(RTTEST hTest, RTVFSFILE hVfsDisk, unsigned cNesting)
46{
47 char szPrefix[100];
48 int rc = VINF_SUCCESS;
49
50 RT_ZERO(szPrefix);
51
52 if (cNesting < sizeof(szPrefix) - 1)
53 {
54 for (unsigned i = 0; i < cNesting; i++)
55 szPrefix[i] = '\t';
56 }
57
58 RTTestSubF(hTest, "Create DVM");
59 RTDVM hVolMgr;
60 rc = RTDvmCreate(&hVolMgr, hVfsDisk, 512, 0 /*fFlags*/);
61 if (RT_FAILURE(rc))
62 {
63 RTTestIFailed("RTDvmCreate -> %Rrc", rc);
64 return RTTestSummaryAndDestroy(hTest);
65 }
66
67 RTTestSubF(hTest, "Open volume map");
68 rc = RTDvmMapOpen(hVolMgr);
69 if ( RT_FAILURE(rc)
70 && rc != VERR_NOT_SUPPORTED)
71 {
72 RTTestIFailed("RTDvmOpen -> %Rrc", rc);
73 RTDvmRelease(hVolMgr);
74 return RTTestSummaryAndDestroy(hTest);
75 }
76 if (rc == VERR_NOT_SUPPORTED)
77 {
78 RTDvmRelease(hVolMgr);
79 return VINF_SUCCESS;
80 }
81
82 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Successfully opened map with format: %s.\n", szPrefix, RTDvmMapGetFormatName(hVolMgr));
83
84 /* Dump all volumes. */
85 RTTestSubF(hTest, "Dump volumes");
86 uint32_t cVolume = 0;
87 RTDVMVOLUME hVol;
88
89 rc = RTDvmMapQueryFirstVolume(hVolMgr, &hVol);
90
91 while (RT_SUCCESS(rc))
92 {
93 char *pszVolName = NULL;
94 RTDVMVOLTYPE enmVolType = RTDvmVolumeGetType(hVol);
95 uint64_t fVolFlags = RTDvmVolumeGetFlags(hVol);
96
97 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume %u:\n", szPrefix, cVolume);
98 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume type %s\n", szPrefix, RTDvmVolumeTypeGetDescr(enmVolType));
99 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume size %llu\n", szPrefix, RTDvmVolumeGetSize(hVol));
100 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume flags %s %s %s\n", szPrefix,
101 fVolFlags & DVMVOLUME_FLAGS_BOOTABLE ? "Bootable" : "",
102 fVolFlags & DVMVOLUME_FLAGS_ACTIVE ? "Active" : "",
103 fVolFlags & DVMVOLUME_F_CONTIGUOUS ? "Contiguous" : "");
104
105 rc = RTDvmVolumeQueryName(hVol, &pszVolName);
106 if (RT_SUCCESS(rc))
107 {
108 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume name %s.\n", szPrefix, pszVolName);
109 RTStrFree(pszVolName);
110 }
111 else if (rc != VERR_NOT_SUPPORTED)
112 RTTestIFailed("RTDvmVolumeQueryName -> %Rrc", rc);
113 else
114 rc = VINF_SUCCESS;
115
116 if (fVolFlags & DVMVOLUME_F_CONTIGUOUS)
117 {
118 uint64_t offStart, offEnd;
119 rc = RTDvmVolumeQueryRange(hVol, &offStart, &offEnd);
120 if (RT_SUCCESS(rc))
121 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Volume range %llu:%llu\n", szPrefix, offStart, offEnd);
122 else
123 RTTestIFailed("RTDvmVolumeQueryRange -> %Rrc", rc);
124 }
125
126 RTTestIPrintf(RTTESTLVL_ALWAYS, "\n");
127
128 /*
129 * Query all volumes which might be inside this.
130 * (think of MBR partitions with a bsdlabel inside)
131 */
132 RTVFSFILE hVfsVol;
133 rc = RTDvmVolumeCreateVfsFile(hVol, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE, &hVfsVol);
134 if (RT_SUCCESS(rc))
135 {
136 rc = tstRTDvmVolume(hTest, hVfsVol, cNesting + 1);
137 RTVfsFileRelease(hVfsVol);
138 }
139 else
140 RTTestIFailed("RTDvmVolumeCreateVfsFile -> %Rrc", rc);
141
142 RTDVMVOLUME hVolNext;
143 rc = RTDvmMapQueryNextVolume(hVolMgr, hVol, &hVolNext);
144 RTDvmVolumeRelease(hVol);
145 hVol = hVolNext;
146 cVolume++;
147 }
148
149 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s Dumped %u volumes\n", szPrefix, cVolume);
150
151 if ( rc == VERR_DVM_MAP_EMPTY
152 || rc == VERR_DVM_MAP_NO_VOLUME)
153 rc = VINF_SUCCESS;
154
155 RTTESTI_CHECK(rc == VINF_SUCCESS);
156
157 RTDvmRelease(hVolMgr);
158
159 return rc;
160}
161
162
163int main(int argc, char **argv)
164{
165 /*
166 * Initialize IPRT and create the test.
167 */
168 RTTEST hTest;
169 int rc = RTTestInitAndCreate("tstRTDvm", &hTest);
170 if (rc)
171 return rc;
172 RTTestBanner(hTest);
173
174 /*
175 * If no args, display usage.
176 */
177 if (argc < 2)
178 {
179 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Syntax: %s <image>\n", argv[0]);
180 return RTTestSkipAndDestroy(hTest, "Missing required arguments\n");
181 }
182
183 RTVFSFILE hVfsDisk;
184 rc = RTVfsFileOpenNormal(argv[1], RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE, &hVfsDisk);
185 if (RT_FAILURE(rc))
186 {
187 RTTestIFailed("RTVfsFileOpenNormal -> %Rrc", rc);
188 return RTTestSummaryAndDestroy(hTest);
189 }
190
191 uint64_t cb = 0;
192 rc = RTVfsFileQuerySize(hVfsDisk, &cb);
193 if ( RT_FAILURE(rc)
194 || cb % 512 != 0) /* Assume 512 byte sector size. */
195 {
196 RTTestIFailed("RTVfsFileQuerySize -> %Rrc", rc);
197 return RTTestSummaryAndDestroy(hTest);
198 }
199
200 rc = tstRTDvmVolume(hTest, hVfsDisk, 0);
201
202 RTTESTI_CHECK(rc == VINF_SUCCESS);
203
204 RTVfsFileRelease(hVfsDisk);
205
206 /*
207 * Summary
208 */
209 return RTTestSummaryAndDestroy(hTest);
210}
211
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette