VirtualBox

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

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