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