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: MapViewOfFile.c |
8 | ** |
9 | ** Purpose: Positive test the MapViewOfFile API. |
10 | ** Call MapViewOfFile with access FILE_MAP_WRITE. |
11 | ** |
12 | ** Depends: CreateFile, |
13 | ** GetFileSize, |
14 | ** memset, |
15 | ** CreateFileMapping, |
16 | ** CloseHandle, |
17 | ** memcpy, |
18 | ** ReadFile, |
19 | ** memcmp, |
20 | ** UnMapViewOfFile. |
21 | ** |
22 | ** |
23 | **============================================================*/ |
24 | #include <palsuite.h> |
25 | |
26 | int __cdecl main(int argc, char *argv[]) |
27 | { |
28 | |
29 | HANDLE hFile; |
30 | HANDLE hFileMapping; |
31 | LPVOID lpMapViewAddress; |
32 | char buf[] = "this is a test string" ; |
33 | const int MAPPINGSIZE = 2048; |
34 | char ch[2048]; |
35 | char readString[2048]; |
36 | char lpFileName[] = "test.tmp" ; |
37 | DWORD dwBytesRead; |
38 | DWORD dwInitialSize = 0; |
39 | DWORD dwFinalSize = 0; |
40 | BOOL bRetVal; |
41 | |
42 | /* Initialize the PAL environment. |
43 | */ |
44 | if(0 != PAL_Initialize(argc, argv)) |
45 | { |
46 | return FAIL; |
47 | } |
48 | |
49 | /* Create a file handle with CreateFile. |
50 | */ |
51 | hFile = CreateFile( lpFileName, |
52 | GENERIC_WRITE|GENERIC_READ, |
53 | FILE_SHARE_READ|FILE_SHARE_WRITE, |
54 | NULL, |
55 | CREATE_ALWAYS, |
56 | FILE_ATTRIBUTE_NORMAL, |
57 | NULL); |
58 | if (hFile == INVALID_HANDLE_VALUE) |
59 | { |
60 | Fail("ERROR: %u :unable to create file \"%s\".\n" , |
61 | GetLastError(), |
62 | lpFileName); |
63 | } |
64 | |
65 | /* Get the initial size of file, for latter tests. |
66 | */ |
67 | dwInitialSize = GetFileSize (hFile, NULL); |
68 | if ( dwInitialSize == INVALID_FILE_SIZE ) |
69 | { |
70 | Fail("ERROR:%u: The created file \"%s\" has an invalid " |
71 | "file size.\n" , |
72 | GetLastError(), |
73 | lpFileName); |
74 | } |
75 | |
76 | /* Initialize the buffers. |
77 | */ |
78 | memset(ch, 0, MAPPINGSIZE); |
79 | memset(readString, 0, MAPPINGSIZE); |
80 | |
81 | /* Create a unnamed file-mapping object with file handle FileHandle |
82 | * and with PAGE_READWRITE protection. |
83 | */ |
84 | hFileMapping = CreateFileMapping( |
85 | hFile, |
86 | NULL, /*not inherited*/ |
87 | PAGE_READWRITE, /*read and wite*/ |
88 | 0, /*high-order of object size*/ |
89 | MAPPINGSIZE, /*low-orger of object size*/ |
90 | NULL); /*unnamed object*/ |
91 | |
92 | if(NULL == hFileMapping) |
93 | { |
94 | Trace("ERROR:%u: Failed to create File Mapping.\n" , |
95 | GetLastError()); |
96 | CloseHandle(hFile); |
97 | Fail("" ); |
98 | } |
99 | |
100 | /* maps a view of a file into the address space of the calling process. |
101 | */ |
102 | lpMapViewAddress = MapViewOfFile( |
103 | hFileMapping, |
104 | FILE_MAP_WRITE, /* access code */ |
105 | 0, /*high order offset*/ |
106 | 0, /*low order offset*/ |
107 | MAPPINGSIZE); /* number of bytes for map */ |
108 | |
109 | if(NULL == lpMapViewAddress) |
110 | { |
111 | Trace("ERROR:%u: Failed to call MapViewOfFile API to map a view" |
112 | " of file!\n" , |
113 | GetLastError()); |
114 | CloseHandle(hFile); |
115 | CloseHandle(hFileMapping); |
116 | Fail("" ); |
117 | } |
118 | |
119 | /* Verify that the size of the file has increased to |
120 | * accomidate the MapView. |
121 | */ |
122 | dwFinalSize = GetFileSize (hFile, NULL); |
123 | if ( (dwFinalSize <= dwInitialSize) && (dwFinalSize != MAPPINGSIZE)) |
124 | { |
125 | CloseHandle(hFile); |
126 | CloseHandle(hFileMapping); |
127 | Fail("ERROR: Size of the file was expected to " |
128 | "increase from \"%d\", to \"%d\".\n " , |
129 | dwInitialSize, |
130 | MAPPINGSIZE); |
131 | } |
132 | |
133 | /* Write to the MapView and copy the MapViewOfFile |
134 | * to buffer, so we can compare with value read from |
135 | * file directly. |
136 | */ |
137 | memcpy(lpMapViewAddress, buf, strlen(buf)); |
138 | |
139 | /* Read from the File handle. |
140 | */ |
141 | bRetVal = ReadFile(hFile, |
142 | readString, |
143 | strlen(buf), |
144 | &dwBytesRead, |
145 | NULL); |
146 | |
147 | if (bRetVal == FALSE) |
148 | { |
149 | Trace("ERROR: %u :unable to read from file handle " |
150 | "hFile=0x%lx\n" , |
151 | GetLastError(), |
152 | hFile); |
153 | CloseHandle(hFile); |
154 | CloseHandle(hFileMapping); |
155 | Fail("" ); |
156 | } |
157 | |
158 | if (memcmp(buf, readString, strlen(readString)) != 0) |
159 | { |
160 | CloseHandle(hFile); |
161 | CloseHandle(hFileMapping); |
162 | Fail("ERROR: Read string from file \"%s\", is " |
163 | "not equal to string written through MapView " |
164 | "\"%s\".\n" , |
165 | readString, |
166 | ch); |
167 | } |
168 | |
169 | /* Unmap the view of file. |
170 | */ |
171 | if(UnmapViewOfFile(lpMapViewAddress) == FALSE) |
172 | { |
173 | Trace("ERROR: Failed to call UnmapViewOfFile API to" |
174 | " unmap the view of a file, error code=%u\n" , |
175 | GetLastError()); |
176 | CloseHandle(hFile); |
177 | CloseHandle(hFileMapping); |
178 | Fail("" ); |
179 | } |
180 | |
181 | /* Close handle to create file. |
182 | */ |
183 | if(CloseHandle(hFile) == FALSE) |
184 | { |
185 | Trace("ERROR:%u:Failed to call CloseHandle API " |
186 | "to close a file handle." , |
187 | GetLastError()); |
188 | CloseHandle(hFileMapping); |
189 | Fail("" ); |
190 | } |
191 | |
192 | if(CloseHandle(hFileMapping) == FALSE) |
193 | { |
194 | Fail("ERROR:%u:Failed to call CloseHandle API " |
195 | "to close a file mapping handle." , |
196 | GetLastError()); |
197 | } |
198 | |
199 | PAL_Terminate(); |
200 | return PASS; |
201 | } |
202 | |
203 | |
204 | |