VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTSystemQueryDmi.cpp@ 76553

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

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

© 2023 Oracle
ContactPrivacy policyTerms of Use