VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTSystemQueryOsInfo.cpp@ 46474

Last change on this file since 46474 was 46318, checked in by vboxsync, 12 years ago

Runtime/testcase/tstRTSystemQueryOsInfo: added output of RTSystemQueryTotalRam() and RTSystemQueryAvailableRam()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/* $Id: tstRTSystemQueryOsInfo.cpp 46318 2013-05-30 08:43:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTSystemQueryOSInfo.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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* Header Files *
29*******************************************************************************/
30#include <iprt/system.h>
31
32#include <iprt/assert.h>
33#include <iprt/string.h>
34#include <iprt/test.h>
35
36
37/*******************************************************************************
38* Global Variables *
39*******************************************************************************/
40static int g_cErrors = 0;
41
42
43int main()
44{
45 RTTEST hTest;
46 int rc = RTTestInitAndCreate("tstRTSystemQueryOsInfo", &hTest);
47 if (rc)
48 return rc;
49 RTTestBanner(hTest);
50
51 /*
52 * Simple stuff.
53 */
54 char szInfo[_4K];
55
56 rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szInfo, sizeof(szInfo));
57 RTTestIPrintf(RTTESTLVL_ALWAYS, "PRODUCT: \"%s\", rc=%Rrc\n", szInfo, rc);
58
59 rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szInfo, sizeof(szInfo));
60 RTTestIPrintf(RTTESTLVL_ALWAYS, "RELEASE: \"%s\", rc=%Rrc\n", szInfo, rc);
61
62 rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szInfo, sizeof(szInfo));
63 RTTestIPrintf(RTTESTLVL_ALWAYS, "VERSION: \"%s\", rc=%Rrc\n", szInfo, rc);
64
65 rc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szInfo, sizeof(szInfo));
66 RTTestIPrintf(RTTESTLVL_ALWAYS, "SERVICE_PACK: \"%s\", rc=%Rrc\n", szInfo, rc);
67
68 uint64_t cbTotal;
69 rc = RTSystemQueryTotalRam(&cbTotal);
70 RTTestIPrintf(RTTESTLVL_ALWAYS, "Total RAM: %'RU64 Bytes (%RU64 KB, %RU64 MB)\n",
71 cbTotal, cbTotal / _1K, cbTotal / _1M);
72
73 uint64_t cbAvailable;
74 rc = RTSystemQueryAvailableRam(&cbAvailable);
75 RTTestIPrintf(RTTESTLVL_ALWAYS, "Available RAM: %'RU64 Bytes (%RU64 KB, %RU64 MB)\n",
76 cbAvailable, cbAvailable / _1K, cbAvailable / _1M);
77
78 /*
79 * Check that unsupported stuff is terminated correctly.
80 */
81 for (int i = RTSYSOSINFO_INVALID + 1; i < RTSYSOSINFO_END; i++)
82 {
83 memset(szInfo, ' ', sizeof(szInfo));
84 rc = RTSystemQueryOSInfo((RTSYSOSINFO)i, szInfo, sizeof(szInfo));
85 if ( rc == VERR_NOT_SUPPORTED
86 && szInfo[0] != '\0')
87 RTTestIFailed("level=%d; unterminated buffer on VERR_NOT_SUPPORTED\n", i);
88 else if (RT_SUCCESS(rc) || rc == VERR_BUFFER_OVERFLOW)
89 RTTESTI_CHECK(RTStrEnd(szInfo, sizeof(szInfo)) != NULL);
90 else if (rc != VERR_NOT_SUPPORTED)
91 RTTestIFailed("level=%d unexpected rc=%Rrc\n", i, rc);
92 }
93
94 /*
95 * Check buffer overflow
96 */
97 RTAssertSetQuiet(true);
98 RTAssertSetMayPanic(false);
99 for (int i = RTSYSDMISTR_INVALID + 1; i < RTSYSDMISTR_END; i++)
100 {
101 RTTESTI_CHECK_RC(RTSystemQueryDmiString((RTSYSDMISTR)i, szInfo, 0), VERR_INVALID_PARAMETER);
102
103 /* Get the length of the info and check that we get overflow errors for
104 everything less that it. */
105 rc = RTSystemQueryOSInfo((RTSYSOSINFO)i, szInfo, sizeof(szInfo));
106 if (RT_FAILURE(rc))
107 continue;
108 size_t const cchInfo = strlen(szInfo);
109
110 for (size_t cch = 1; cch < sizeof(szInfo) && cch < cchInfo; cch++)
111 {
112 memset(szInfo, 0x7f, sizeof(szInfo));
113 RTTESTI_CHECK_RC(RTSystemQueryOSInfo((RTSYSOSINFO)i, szInfo, cch), VERR_BUFFER_OVERFLOW);
114
115 /* check the padding. */
116 for (size_t off = cch; off < sizeof(szInfo); off++)
117 if (szInfo[off] != 0x7f)
118 {
119 RTTestIFailed("level=%d, rc=%Rrc, cch=%zu, off=%zu: Wrote too much!\n", i, rc, cch, off);
120 break;
121 }
122
123 /* check for zero terminator. */
124 if (!RTStrEnd(szInfo, cch))
125 RTTestIFailed("level=%d, rc=%Rrc, cch=%zu: Buffer not terminated!\n", i, rc, cch);
126 }
127
128 /* Check that the exact length works. */
129 rc = RTSystemQueryOSInfo((RTSYSOSINFO)i, szInfo, cchInfo + 1);
130 if (rc != VINF_SUCCESS)
131 RTTestIFailed("level=%d: rc=%Rrc when specifying exactly right buffer length (%zu)\n", i, rc, cchInfo + 1);
132 }
133
134 return RTTestSummaryAndDestroy(hTest);
135}
136
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