1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=============================================================
6**
7** Source: createfilemappingw.c (test 5)
8**
9** Purpose: Positive test the CreateFileMappingW API.
10** Test CreateFileMappingW to a "swap" handle with
11** access PAGE_READONLY.
12**
13**
14**============================================================*/
15#define UNICODE
16#include <palsuite.h>
17
18const int MAPPINGSIZE = 2048;
19HANDLE SWAP_HANDLE = ((VOID *)(-1));
20
21int __cdecl main(int argc, char *argv[])
22{
23 char testString[] = "this is a test string";
24 WCHAR lpObjectName[] = {'m','y','O','b','j','e','c','t','\0'};
25 int RetVal = FAIL;
26 char results[2048];
27
28 HANDLE hFileMapRO;
29 HANDLE hFileMapRW;
30 LPVOID lpMapViewRO;
31 LPVOID lpMapViewRW;
32
33 /* Initialize the PAL environment.
34 */
35 if(0 != PAL_Initialize(argc, argv))
36 {
37 return FAIL;
38 }
39
40 /* Initialize the buffers.
41 */
42 memset(results, 0, MAPPINGSIZE);
43
44 /* Create a named file-mapping object with file handle FileHandle
45 * and with PAGE_READWRITE protection.
46 */
47 hFileMapRW = CreateFileMapping(
48 SWAP_HANDLE,
49 NULL, /*not inherited*/
50 PAGE_READWRITE, /*read only*/
51 0, /*high-order size*/
52 MAPPINGSIZE, /*low-order size*/
53 lpObjectName); /*named object*/
54
55 if(NULL == hFileMapRW)
56 {
57 Fail("ERROR:%u: Failed to create File Mapping.\n",
58 GetLastError());
59 }
60
61 /* maps a view of a file into the address space of the calling process.
62 */
63 lpMapViewRW = MapViewOfFile(
64 hFileMapRW,
65 FILE_MAP_ALL_ACCESS, /* access code */
66 0, /* high order offset*/
67 0, /* low order offset*/
68 MAPPINGSIZE); /* number of bytes for map */
69
70 if(NULL == lpMapViewRW)
71 {
72 Trace("ERROR:%u: Failed to call MapViewOfFile "
73 "API to map a view of file!\n",
74 GetLastError());
75 RetVal = FAIL;
76 goto CleanUpOne;
77 }
78
79
80 hFileMapRO = CreateFileMapping(
81 SWAP_HANDLE,
82 NULL, /*not inherited*/
83 PAGE_READONLY, /*read and write*/
84 0, /*high-order size*/
85 MAPPINGSIZE, /*low-order size*/
86 lpObjectName); /*named object*/
87
88 if(NULL == hFileMapRO)
89 {
90 Trace("ERROR:%u: Failed to create File Mapping.\n",
91 GetLastError());
92 RetVal = FAIL;
93 goto CleanUpTwo;
94 }
95
96 /* maps a view of a file into the address space of the calling process.
97 */
98 lpMapViewRO = MapViewOfFile(
99 hFileMapRO,
100 FILE_MAP_READ, /* access code */
101 0, /* high order offset*/
102 0, /* low order offset*/
103 MAPPINGSIZE); /* number of bytes for map */
104
105 if(NULL == lpMapViewRO)
106 {
107 Trace("ERROR:%u: Failed to call MapViewOfFile "
108 "API to map a view of file!\n",
109 GetLastError());
110 RetVal = FAIL;
111 goto CleanUpThree;
112 }
113
114 /* Write the test string to the Map view.
115 */
116 memcpy(lpMapViewRW, testString, strlen(testString));
117
118 /* Read from the second Map view.
119 */
120 memcpy(results, (LPCSTR)lpMapViewRO, MAPPINGSIZE);
121
122 /* Verify the contents of the file mapping,
123 * by comparing what was written to what was read.
124 */
125 if (memcmp(results, testString, strlen(testString))!= 0)
126 {
127 Trace("ERROR: MapViewOfFile not equal to file contents "
128 "retrieved \"%s\", expected \"%s\".\n",
129 results,
130 testString);
131 RetVal = FAIL;
132 goto CleanUpFour;
133 }
134
135 /* Test was successful.
136 */
137 RetVal = PASS;
138
139CleanUpFour:
140
141 /* Unmap the view of file.
142 */
143 if ( UnmapViewOfFile(lpMapViewRO) == FALSE )
144 {
145 Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
146 GetLastError(),
147 lpMapViewRO);
148 RetVal = FAIL;
149 }
150
151CleanUpThree:
152
153 /* Close Handle to opend file mapping.
154 */
155 if ( CloseHandle(hFileMapRO) == FALSE )
156 {
157 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
158 GetLastError(),
159 hFileMapRO);
160 RetVal = FAIL;
161 }
162
163
164CleanUpTwo:
165 /* Unmap the view of file.
166 */
167 if ( UnmapViewOfFile(lpMapViewRW) == FALSE )
168 {
169 Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
170 GetLastError(),
171 lpMapViewRW);
172 RetVal = FAIL;
173 }
174
175
176CleanUpOne:
177
178 /* Close Handle to opend file mapping.
179 */
180 if ( CloseHandle(hFileMapRW) == FALSE )
181 {
182 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
183 GetLastError(),
184 hFileMapRW);
185 RetVal = FAIL;
186 }
187
188
189 /* Terminate the PAL.
190 */
191 PAL_TerminateEx(RetVal);
192 return RetVal;
193}
194
195