VirtualBox

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

Last change on this file since 27721 was 26848, checked in by vboxsync, 15 years ago

tstRTSystemQueryOsInfo.cpp: Copied back the corrected overflow test from tstRTSystemQueryDmi.cpp.

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