1 | /* $Id: tstRTSystemQueryDmi.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTSystemQueryDmi*.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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/system.h>
|
---|
42 |
|
---|
43 | #include <iprt/assert.h>
|
---|
44 | #include <iprt/errcore.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include <iprt/test.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | int main()
|
---|
50 | {
|
---|
51 | RTTEST hTest;
|
---|
52 | int rc = RTTestInitAndCreate("tstRTSystemQueryDmi", &hTest);
|
---|
53 | if (rc)
|
---|
54 | return rc;
|
---|
55 | RTTestBanner(hTest);
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * Simple stuff.
|
---|
59 | */
|
---|
60 | char szInfo[_4K];
|
---|
61 |
|
---|
62 | rc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_NAME, szInfo, sizeof(szInfo));
|
---|
63 | RTTestIPrintf(RTTESTLVL_ALWAYS, "PRODUCT_NAME: \"%s\", rc=%Rrc\n", szInfo, rc);
|
---|
64 |
|
---|
65 | rc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_VERSION, szInfo, sizeof(szInfo));
|
---|
66 | RTTestIPrintf(RTTESTLVL_ALWAYS, "PRODUCT_VERSION: \"%s\", rc=%Rrc\n", szInfo, rc);
|
---|
67 |
|
---|
68 | rc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_UUID, szInfo, sizeof(szInfo));
|
---|
69 | RTTestIPrintf(RTTESTLVL_ALWAYS, "PRODUCT_UUID: \"%s\", rc=%Rrc\n", szInfo, rc);
|
---|
70 |
|
---|
71 | rc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_SERIAL, szInfo, sizeof(szInfo));
|
---|
72 | RTTestIPrintf(RTTESTLVL_ALWAYS, "PRODUCT_SERIAL: \"%s\", rc=%Rrc\n", szInfo, rc);
|
---|
73 |
|
---|
74 | rc = RTSystemQueryDmiString(RTSYSDMISTR_MANUFACTURER, szInfo, sizeof(szInfo));
|
---|
75 | RTTestIPrintf(RTTESTLVL_ALWAYS, "MANUFACTURER: \"%s\", rc=%Rrc\n", szInfo, rc);
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Check that unsupported stuff is terminated correctly.
|
---|
79 | */
|
---|
80 | for (int i = RTSYSDMISTR_INVALID + 1; i < RTSYSDMISTR_END; i++)
|
---|
81 | {
|
---|
82 | memset(szInfo, ' ', sizeof(szInfo));
|
---|
83 | rc = RTSystemQueryDmiString((RTSYSDMISTR)i, szInfo, sizeof(szInfo));
|
---|
84 | if ( (rc == VERR_NOT_SUPPORTED || rc == VERR_ACCESS_DENIED)
|
---|
85 | && szInfo[0] != '\0')
|
---|
86 | RTTestIFailed("level=%d; unterminated buffer on VERR_NOT_SUPPORTED\n", i);
|
---|
87 | else if (RT_SUCCESS(rc) || rc == VERR_BUFFER_OVERFLOW)
|
---|
88 | RTTESTI_CHECK(RTStrEnd(szInfo, sizeof(szInfo)) != NULL);
|
---|
89 | else if (rc != VERR_NOT_SUPPORTED && rc != VERR_ACCESS_DENIED)
|
---|
90 | RTTestIFailed("level=%d unexpected rc=%Rrc\n", i, rc);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * Check buffer overflow
|
---|
95 | */
|
---|
96 | RTAssertSetQuiet(true);
|
---|
97 | RTAssertSetMayPanic(false);
|
---|
98 | for (int i = RTSYSDMISTR_INVALID + 1; i < RTSYSDMISTR_END; i++)
|
---|
99 | {
|
---|
100 | RTTESTI_CHECK_RC(RTSystemQueryDmiString((RTSYSDMISTR)i, szInfo, 0), VERR_INVALID_PARAMETER);
|
---|
101 |
|
---|
102 | /* Get the length of the info and check that we get overflow errors for
|
---|
103 | everything less that it. */
|
---|
104 | rc = RTSystemQueryDmiString((RTSYSDMISTR)i, szInfo, sizeof(szInfo));
|
---|
105 | if (RT_FAILURE(rc))
|
---|
106 | continue;
|
---|
107 | size_t const cchInfo = strlen(szInfo);
|
---|
108 |
|
---|
109 | for (size_t cch = 1; cch < sizeof(szInfo) && cch < cchInfo; cch++)
|
---|
110 | {
|
---|
111 | memset(szInfo, 0x7f, sizeof(szInfo));
|
---|
112 | RTTESTI_CHECK_RC(RTSystemQueryDmiString((RTSYSDMISTR)i, szInfo, cch), VERR_BUFFER_OVERFLOW);
|
---|
113 |
|
---|
114 | /* check the padding. */
|
---|
115 | for (size_t off = cch; off < sizeof(szInfo); off++)
|
---|
116 | if (szInfo[off] != 0x7f)
|
---|
117 | {
|
---|
118 | RTTestIFailed("level=%d, rc=%Rrc, cch=%zu, off=%zu: Wrote too much!\n", i, rc, cch, off);
|
---|
119 | break;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /* check for zero terminator. */
|
---|
123 | if (!RTStrEnd(szInfo, cch))
|
---|
124 | RTTestIFailed("level=%d, rc=%Rrc, cch=%zu: Buffer not terminated!\n", i, rc, cch);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /* Check that the exact length works. */
|
---|
128 | rc = RTSystemQueryDmiString((RTSYSDMISTR)i, szInfo, cchInfo + 1);
|
---|
129 | if (rc != VINF_SUCCESS)
|
---|
130 | RTTestIFailed("level=%d: rc=%Rrc when specifying exactly right buffer length (%zu)\n", i, rc, cchInfo + 1);
|
---|
131 | }
|
---|
132 |
|
---|
133 | return RTTestSummaryAndDestroy(hTest);
|
---|
134 | }
|
---|
135 |
|
---|