VirtualBox

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

Last change on this file since 52320 was 30320, checked in by vboxsync, 15 years ago

*: Replaced memchr(psz, '\0', cb) with RTStrEnd(psz, cb) and worked around memchr( RTSTR_MAX) issue in RTStrEnd. Here (linux.amd64 / glibc-2.10.1-r1) memchr fails for cb > ~(size_t)11. Since RTSTR_MAX is ~(size_t)0, this behavior breaks several IPRT string APIs.

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