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: createfilemapping.c (test 6)
8**
9** Purpose: Positive test the CreateFileMapping API.
10** Test CreateFileMapping to a "swap" handle with
11** access PAGE_READWRITE.
12**
13**
14**============================================================*/
15#include <palsuite.h>
16
17const int MAPPINGSIZE = 2048;
18HANDLE SWAP_HANDLE = ((VOID *)(-1));
19
20int __cdecl main(int argc, char *argv[])
21{
22 char testString[] = "this is a test string";
23 char lpObjectName[] = "myMappingObject";
24 char results[2048];
25 int RetVal = PASS;
26
27 HANDLE hFileMapRW;
28 LPVOID lpMapViewRW;
29 LPVOID lpMapViewRW2;
30
31 /* Initialize the PAL environment.
32 */
33 if(0 != PAL_Initialize(argc, argv))
34 {
35 return FAIL;
36 }
37
38 /* Initialize the buffers.
39 */
40 memset(results, 0, MAPPINGSIZE);
41
42 /* Create a named file-mapping object with file handle FileHandle
43 * and with PAGE_READWRITE protection.
44 */
45 hFileMapRW = CreateFileMapping(
46 SWAP_HANDLE,
47 NULL, /*not inherited*/
48 PAGE_READWRITE, /*read write*/
49 0, /*high-order size*/
50 MAPPINGSIZE, /*low-order size*/
51 lpObjectName); /*unnamed object*/
52
53 if(NULL == hFileMapRW)
54 {
55 Fail("ERROR:%u: Failed to create File Mapping.\n",
56 GetLastError());
57 }
58
59 /* Create a map view of the READWRITE file mapping.
60 */
61 lpMapViewRW = MapViewOfFile(
62 hFileMapRW,
63 FILE_MAP_ALL_ACCESS,/* access code */
64 0, /* high order offset*/
65 0, /* low order offset*/
66 MAPPINGSIZE); /* number of bytes for map */
67
68 if(NULL == lpMapViewRW)
69 {
70 Trace("ERROR:%u: Failed to call MapViewOfFile "
71 "API to map a view of file!\n",
72 GetLastError());
73 RetVal = FAIL;
74 goto CleanUpOne;
75 }
76
77
78 /* maps a view of a file into the address space of the calling process.
79 */
80 lpMapViewRW2 = MapViewOfFile(
81 hFileMapRW,
82 FILE_MAP_ALL_ACCESS, /* access code */
83 0, /* high order offset*/
84 0, /* low order offset*/
85 MAPPINGSIZE); /* number of bytes for map */
86
87 if(NULL == lpMapViewRW2)
88 {
89 Trace("ERROR:%u: Failed to call MapViewOfFile "
90 "API to map a view of file!\n",
91 GetLastError());
92 RetVal = FAIL;
93 goto CleanUpTwo;
94 }
95
96 /* Write the test string to the Map view.
97 */
98 memcpy(lpMapViewRW, testString, strlen(testString));
99
100 /* Read from the second Map view.
101 */
102 memcpy(results, (LPCSTR)lpMapViewRW2, MAPPINGSIZE);
103
104 /* Verify the contents of the file mapping,
105 * by comparing what was written to what was read.
106 */
107 if (memcmp(results, testString, strlen(testString))!= 0)
108 {
109 Trace("ERROR: MapViewOfFile not equal to file contents "
110 "retrieved \"%s\", expected \"%s\".\n",
111 results,
112 testString);
113 RetVal = FAIL;
114 goto CleanUpThree;
115 }
116
117 /* Test successful.
118 */
119 RetVal = PASS;
120
121CleanUpThree:
122
123 /* Unmap the view of file.
124 */
125 if ( UnmapViewOfFile(lpMapViewRW2) == FALSE )
126 {
127 Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
128 GetLastError(),
129 lpMapViewRW2);
130 RetVal = FAIL;
131 }
132
133CleanUpTwo:
134
135 /* Unmap the view of file.
136 */
137 if ( UnmapViewOfFile(lpMapViewRW) == FALSE )
138 {
139 Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
140 GetLastError(),
141 lpMapViewRW);
142 RetVal = FAIL;
143 }
144
145
146CleanUpOne:
147
148 /* Close Handle to create file mapping.
149 */
150 if ( CloseHandle(hFileMapRW) == FALSE )
151 {
152 Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
153 GetLastError(),
154 hFileMapRW);
155 RetVal = FAIL;
156 }
157
158
159 /* Terminate the PAL.
160 */
161 PAL_TerminateEx(RetVal);
162 return RetVal;
163}
164
165