1 | /* $Id: tstSettings.cpp 103147 2024-01-31 15:08:03Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Settings testcases - No Main API involved.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 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 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <typeinfo>
|
---|
33 | #include <stdexcept>
|
---|
34 |
|
---|
35 | #include <iprt/errcore.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/getopt.h>
|
---|
38 | #include <iprt/path.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/test.h>
|
---|
41 |
|
---|
42 | #include <VBox/com/string.h>
|
---|
43 | #include <VBox/settings.h>
|
---|
44 |
|
---|
45 | using namespace com;
|
---|
46 | using namespace settings;
|
---|
47 |
|
---|
48 | /** The current logging verbosity level. */
|
---|
49 | static unsigned g_uVerbosity = 0;
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Tests a single .vbox machine configuration file.
|
---|
54 | *
|
---|
55 | * @param strFileSrc Absolute path of machine configuration file to test.
|
---|
56 | *
|
---|
57 | * @note The source configuration file will not be modified (i.e. written to disk).
|
---|
58 | */
|
---|
59 | static void tstFileSingle(const Utf8Str &strFileSrc)
|
---|
60 | {
|
---|
61 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Testing file: %s\n", strFileSrc.c_str());
|
---|
62 |
|
---|
63 | char szFileDst[RTPATH_MAX] = { 0 };
|
---|
64 |
|
---|
65 | try
|
---|
66 | {
|
---|
67 | MachineConfigFile fileSrc(&strFileSrc);
|
---|
68 | if (fileSrc == fileSrc) /* Only have a == operator. */
|
---|
69 | {
|
---|
70 | }
|
---|
71 | else
|
---|
72 | throw "Source file doesn't match itself! == operator bug!?";
|
---|
73 |
|
---|
74 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Source file version is: v%u\n", fileSrc.getSettingsVersion());
|
---|
75 |
|
---|
76 | RTTESTI_CHECK_RC_OK_RETV(RTPathTemp(szFileDst, sizeof(szFileDst)));
|
---|
77 | RTTESTI_CHECK_RC_OK_RETV(RTPathAppend(szFileDst, sizeof(szFileDst), "tstSettings-XXXXXX.vbox"));
|
---|
78 | RTTESTI_CHECK_RC_OK_RETV(RTFileCreateTemp(szFileDst, 0600));
|
---|
79 |
|
---|
80 | #ifdef DEBUG_andy
|
---|
81 | RTStrPrintf(szFileDst, sizeof(szFileDst), "/tmp/out.vbox");
|
---|
82 | #endif
|
---|
83 | /* Write source file to a temporary destination file. */
|
---|
84 | fileSrc.write(szFileDst);
|
---|
85 | RTTestIPrintf(RTTESTLVL_DEBUG, "Destination file written to: %s\n", szFileDst);
|
---|
86 |
|
---|
87 | try
|
---|
88 | {
|
---|
89 | /* Load destination file to see if it works. */
|
---|
90 | Utf8Str strFileDst(szFileDst);
|
---|
91 | MachineConfigFile fileDst(&strFileDst);
|
---|
92 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Destination file version is: v%u\n", fileDst.getSettingsVersion());
|
---|
93 | if (fileSrc == fileDst) /* Only have a == operator. */
|
---|
94 | {
|
---|
95 | }
|
---|
96 | else
|
---|
97 | throw std::runtime_error("Source and destination files don't match!");
|
---|
98 | }
|
---|
99 | catch (const std::exception &err)
|
---|
100 | {
|
---|
101 | RTTestIFailed("Testing destination file failed: %s\n", err.what());
|
---|
102 | }
|
---|
103 | }
|
---|
104 | catch (const std::exception &err)
|
---|
105 | {
|
---|
106 | RTTestIFailed("Testing source file failed: %s\n", err.what());
|
---|
107 | }
|
---|
108 |
|
---|
109 | #ifndef DEBUG_andy
|
---|
110 | /* Clean up. */
|
---|
111 | if (strlen(szFileDst))
|
---|
112 | RTTESTI_CHECK_RC_OK_RETV(RTFileDelete(szFileDst));
|
---|
113 | #endif
|
---|
114 | }
|
---|
115 |
|
---|
116 | int main(int argc, char *argv[])
|
---|
117 | {
|
---|
118 | RTTEST hTest;
|
---|
119 | RTEXITCODE rcExit = RTTestInitAndCreate("tstSettings", &hTest);
|
---|
120 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
121 | return rcExit;
|
---|
122 | RTTestBanner(hTest);
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Process options.
|
---|
126 | */
|
---|
127 | static const RTGETOPTDEF aOpts[] =
|
---|
128 | {
|
---|
129 | { "--verbose", 'v', RTGETOPT_REQ_STRING },
|
---|
130 | };
|
---|
131 |
|
---|
132 | RTGETOPTSTATE GetState;
|
---|
133 | int rc = RTGetOptInit(&GetState, argc, argv, aOpts, RT_ELEMENTS(aOpts), 1 /*idxFirst*/, 0 /*fFlags - must not sort! */);
|
---|
134 | AssertRCReturn(rc, RTEXITCODE_INIT);
|
---|
135 |
|
---|
136 | unsigned cFiles = 0;
|
---|
137 |
|
---|
138 | int ch;
|
---|
139 | RTGETOPTUNION ValueUnion;
|
---|
140 | while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
|
---|
141 | {
|
---|
142 | switch (ch)
|
---|
143 | {
|
---|
144 | case 'v':
|
---|
145 | g_uVerbosity++;
|
---|
146 | break;
|
---|
147 |
|
---|
148 | case VINF_GETOPT_NOT_OPTION:
|
---|
149 | {
|
---|
150 | cFiles++;
|
---|
151 | tstFileSingle(ValueUnion.psz);
|
---|
152 | break;
|
---|
153 | }
|
---|
154 |
|
---|
155 | default:
|
---|
156 | return RTGetOptPrintError(ch, &ValueUnion);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (!cFiles)
|
---|
161 | return RTTestSkipAndDestroy(hTest, "At least one .vbox machine file must be specified to test!\n");
|
---|
162 |
|
---|
163 | return RTTestSummaryAndDestroy(hTest);
|
---|
164 | }
|
---|
165 |
|
---|