1 | /* $Id: tstRTDvm.cpp 36816 2011-04-22 17:57:57Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - IPRT Disk Volume Management (DVM)
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 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/initterm.h>
|
---|
32 | #include <iprt/err.h>
|
---|
33 | #include <iprt/test.h>
|
---|
34 | #include <iprt/dvm.h>
|
---|
35 | #include <iprt/file.h>
|
---|
36 |
|
---|
37 | RTFILE g_hDisk = NIL_RTFILE; /**< The disk image. */
|
---|
38 | uint64_t g_cbDisk = 0; /**< Size of the image. */
|
---|
39 |
|
---|
40 | static int dvmDiskRead(void *pvUser, uint64_t off, void *pvBuf, size_t cbRead)
|
---|
41 | {
|
---|
42 | return RTFileReadAt(g_hDisk, off, pvBuf, cbRead, NULL);
|
---|
43 | }
|
---|
44 |
|
---|
45 | static int dvmDiskWrite(void *pvUser, uint64_t off, const void *pvBuf, size_t cbWrite)
|
---|
46 | {
|
---|
47 | return RTFileWriteAt(g_hDisk, off, pvBuf, cbWrite, NULL);
|
---|
48 | }
|
---|
49 |
|
---|
50 | int main(int argc, char **argv)
|
---|
51 | {
|
---|
52 | /*
|
---|
53 | * Initialize IPRT and create the test.
|
---|
54 | */
|
---|
55 | RTTEST hTest;
|
---|
56 | int rc = RTTestInitAndCreate("tstRTDvm", &hTest);
|
---|
57 | if (rc)
|
---|
58 | return rc;
|
---|
59 | RTTestBanner(hTest);
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * If no args, display usage.
|
---|
63 | */
|
---|
64 | if (argc < 2)
|
---|
65 | {
|
---|
66 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Syntax: %s <image>\n", argv[0]);
|
---|
67 | return RTTestSkipAndDestroy(hTest, "Missing required arguments\n");
|
---|
68 | }
|
---|
69 |
|
---|
70 | /* Open image. */
|
---|
71 | rc = RTFileOpen(&g_hDisk, argv[1], RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READWRITE);
|
---|
72 | if (RT_FAILURE(rc))
|
---|
73 | {
|
---|
74 | RTTestIFailed("RTFileOpen -> %Rrc", rc);
|
---|
75 | return RTTestSummaryAndDestroy(hTest);
|
---|
76 | }
|
---|
77 |
|
---|
78 | rc = RTFileGetSize(g_hDisk, &g_cbDisk);
|
---|
79 | if ( RT_FAILURE(rc)
|
---|
80 | || g_cbDisk % 512 != 0) /* Assume 512 byte sector size. */
|
---|
81 | {
|
---|
82 | RTTestIFailed("RTFileGetSize -> %Rrc", rc);
|
---|
83 | return RTTestSummaryAndDestroy(hTest);
|
---|
84 | }
|
---|
85 |
|
---|
86 | RTTestSubF(hTest, "Create DVM");
|
---|
87 | RTDVM hVolMgr;
|
---|
88 | rc = RTDvmCreate(&hVolMgr, dvmDiskRead, dvmDiskWrite, g_cbDisk, 512, NULL);
|
---|
89 | if (RT_FAILURE(rc))
|
---|
90 | {
|
---|
91 | RTTestIFailed("RTDvmCreate -> %Rrc", rc);
|
---|
92 | return RTTestSummaryAndDestroy(hTest);
|
---|
93 | }
|
---|
94 |
|
---|
95 | RTTestSubF(hTest, "Open volume map");
|
---|
96 | rc = RTDvmMapOpen(hVolMgr);
|
---|
97 | if (RT_FAILURE(rc))
|
---|
98 | {
|
---|
99 | RTTestIFailed("RTDvmOpen -> %Rrc", rc);
|
---|
100 | return RTTestSummaryAndDestroy(hTest);
|
---|
101 | }
|
---|
102 |
|
---|
103 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Successfully opened map with format: %s.\n", RTDvmMapGetFormat(hVolMgr));
|
---|
104 |
|
---|
105 | /* Dump all volumes. */
|
---|
106 | RTTestSubF(hTest, "Dump volumes");
|
---|
107 | uint32_t cVolume = 0;
|
---|
108 | RTDVMVOLUME hVol;
|
---|
109 |
|
---|
110 | rc = RTDvmMapQueryFirstVolume(hVolMgr, &hVol);
|
---|
111 |
|
---|
112 | while (RT_SUCCESS(rc))
|
---|
113 | {
|
---|
114 | RTDVMVOLTYPE enmVolType = RTDvmVolumeGetType(hVol);
|
---|
115 | uint64_t fVolFlags = RTDvmVolumeGetFlags(hVol);
|
---|
116 |
|
---|
117 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Volume %u:\n", cVolume);
|
---|
118 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Volume type %s.\n", RTDvmVolumeTypeGetDescr(enmVolType));
|
---|
119 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Volume size %llu.\n", RTDvmVolumeGetSize(hVol));
|
---|
120 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Volume flags %s %s.\n\n",
|
---|
121 | fVolFlags & DVMVOLUME_FLAGS_BOOTABLE ? "Bootable" : "",
|
---|
122 | fVolFlags & DVMVOLUME_FLAGS_ACTIVE ? "Active" : "");
|
---|
123 |
|
---|
124 | RTDVMVOLUME hVolNext;
|
---|
125 | rc = RTDvmMapQueryNextVolume(hVolMgr, hVol, &hVolNext);
|
---|
126 | RTDvmVolumeRelease(hVol);
|
---|
127 | hVol = hVolNext;
|
---|
128 | cVolume++;
|
---|
129 | }
|
---|
130 |
|
---|
131 | RTTestIPrintf(RTTESTLVL_ALWAYS, " Dumped %u volumes\n", cVolume);
|
---|
132 |
|
---|
133 | if ( rc == VERR_DVM_MAP_EMPTY
|
---|
134 | || rc == VERR_DVM_MAP_NO_VOLUME)
|
---|
135 | rc = VINF_SUCCESS;
|
---|
136 |
|
---|
137 | RTTESTI_CHECK(rc == VINF_SUCCESS);
|
---|
138 |
|
---|
139 | RTDvmRelease(hVolMgr);
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * Summary
|
---|
143 | */
|
---|
144 | return RTTestSummaryAndDestroy(hTest);
|
---|
145 | }
|
---|
146 |
|
---|