VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTShMem.cpp@ 106061

Last change on this file since 106061 was 106061, checked in by vboxsync, 5 days ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/* $Id: tstRTShMem.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTShMem.
4 */
5
6/*
7 * Copyright (C) 2018-2024 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/shmem.h>
42
43#include <iprt/err.h>
44#include <iprt/log.h>
45#include <iprt/string.h>
46#include <iprt/test.h>
47
48
49/*********************************************************************************************************************************
50* Global Variables *
51*********************************************************************************************************************************/
52/** Global shared memory object used across all tests. */
53static RTSHMEM g_hShMem = NIL_RTSHMEM;
54/** Data to read/write initially. */
55static char g_szDataBefore[] = "Data before modification!";
56/** Data to read/write for the modification. */
57static char g_szDataAfter[] = "Data after modification!";
58
59
60
61static void tstRTShMem2(void)
62{
63 RTTestISub("Negative");
64
65 /** @todo */
66}
67
68
69static void tstRTShMem1(void)
70{
71 RTTestISub("Basics");
72
73 /* create and destroy. */
74 int rc = RTShMemOpen(&g_hShMem, "tstRTShMem-Share", RTSHMEM_O_F_CREATE_EXCL | RTSHMEM_O_F_READWRITE | RTSHMEM_O_F_MAYBE_EXEC,
75 _512K, 0);
76 if (RT_FAILURE(rc))
77 {
78 RTTESTI_CHECK_RC_RETV(rc, VERR_ALREADY_EXISTS);
79 RTTESTI_CHECK_RC(RTShMemDelete("tstRTShMem-Share"), VINF_SUCCESS);
80 RTTESTI_CHECK_RC_RETV(RTShMemOpen(&g_hShMem, "tstRTShMem-Share", RTSHMEM_O_F_CREATE | RTSHMEM_O_F_READWRITE | RTSHMEM_O_F_MAYBE_EXEC,
81 _512K, 0),
82 VINF_SUCCESS);
83 }
84
85 RTTESTI_CHECK_RETV(g_hShMem != NIL_RTSHMEM);
86
87 /* Query the size. */
88 size_t cbShMem = 0;
89 RTTESTI_CHECK_RC(RTShMemQuerySize(g_hShMem, &cbShMem), VINF_SUCCESS);
90 RTTESTI_CHECK(cbShMem == _512K);
91
92 /* Create a mapping. */
93 void *pvMap;
94 RTTESTI_CHECK_RC_RETV(RTShMemMapRegion(g_hShMem, 0, cbShMem, RTSHMEM_MAP_F_READ | RTSHMEM_MAP_F_WRITE, &pvMap), VINF_SUCCESS);
95 memset(pvMap, 0, cbShMem);
96 memcpy(pvMap, &g_szDataBefore[0], sizeof(g_szDataBefore));
97
98 /* Open the shared memory object and create a second mapping. */
99 RTSHMEM hShMemRead = NIL_RTSHMEM;
100 RTTESTI_CHECK_RC_RETV(RTShMemOpen(&hShMemRead, "tstRTShMem-Share", RTSHMEM_O_F_READWRITE | RTSHMEM_O_F_MAYBE_EXEC,
101 0, 0),
102 VINF_SUCCESS);
103 RTTESTI_CHECK_RETV(hShMemRead != NIL_RTSHMEM);
104
105 void *pvMapRead = NULL;
106 RTTESTI_CHECK_RC(RTShMemQuerySize(hShMemRead, &cbShMem), VINF_SUCCESS);
107 RTTESTI_CHECK(cbShMem == _512K);
108 RTTESTI_CHECK_RC_RETV(RTShMemMapRegion(hShMemRead, 0, cbShMem, RTSHMEM_MAP_F_READ | RTSHMEM_MAP_F_WRITE, &pvMapRead), VINF_SUCCESS);
109 RTTESTI_CHECK(!memcmp(pvMapRead, &g_szDataBefore[0], sizeof(g_szDataBefore)));
110 RTTESTI_CHECK(!memcmp(pvMapRead, pvMap, cbShMem));
111
112 /* Alter the data in the first mapping and check that it is visible in the second one. */
113 memcpy(pvMap, &g_szDataAfter[0], sizeof(g_szDataAfter));
114 RTTESTI_CHECK(!memcmp(pvMapRead, &g_szDataAfter[0], sizeof(g_szDataAfter)));
115 RTTESTI_CHECK(!memcmp(pvMapRead, pvMap, cbShMem));
116
117 RTTESTI_CHECK_RC(RTShMemUnmapRegion(hShMemRead, pvMapRead), VINF_SUCCESS);
118 RTTESTI_CHECK_RC(RTShMemClose(hShMemRead), VINF_SUCCESS);
119 RTTESTI_CHECK_RC(RTShMemUnmapRegion(g_hShMem, pvMap), VINF_SUCCESS);
120 RTTESTI_CHECK_RC(RTShMemClose(g_hShMem), VINF_SUCCESS);
121 g_hShMem = NIL_RTSHMEM;
122}
123
124int main()
125{
126 RTTEST hTest;
127 int rc = RTTestInitAndCreate("tstRTShMem", &hTest);
128 if (rc)
129 return rc;
130 RTTestBanner(hTest);
131
132 /*
133 * The tests.
134 */
135 tstRTShMem1();
136 if (RTTestErrorCount(hTest) == 0)
137 {
138 bool fMayPanic = RTAssertMayPanic();
139 bool fQuiet = RTAssertAreQuiet();
140 RTAssertSetMayPanic(false);
141 RTAssertSetQuiet(true);
142 tstRTShMem2();
143 RTAssertSetQuiet(fQuiet);
144 RTAssertSetMayPanic(fMayPanic);
145 }
146
147 if (g_hShMem != NIL_RTSHMEM)
148 {
149 RTTESTI_CHECK_RC(RTShMemClose(g_hShMem), VINF_SUCCESS);
150 g_hShMem = NIL_RTSHMEM;
151 }
152
153 /*
154 * Summary.
155 */
156 return RTTestSummaryAndDestroy(hTest);
157}
158
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