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_ALL_ACCESS. |
11 | ** |
12 | ** Depends: CreateFile, |
13 | ** GetFileSize, |
14 | ** memset, |
15 | ** memcpy, |
16 | ** memcmp, |
17 | ** ReadFile, |
18 | ** UnMapViewOfFile, |
19 | ** CreateFileMapping, |
20 | ** CloseHandle. |
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 | Trace("ERROR:%u: The created file \"%s\" has an invalid " |
71 | "file size.\n" , |
72 | GetLastError(), |
73 | lpFileName); |
74 | CloseHandle(hFile); |
75 | Fail("" ); |
76 | } |
77 | |
78 | /* Initialize the buffers. |
79 | */ |
80 | memset(ch, 0, MAPPINGSIZE); |
81 | memset(readString, 0, MAPPINGSIZE); |
82 | |
83 | /* Create a unnamed file-mapping object with file handle FileHandle |
84 | * and with PAGE_READWRITE protection. |
85 | */ |
86 | hFileMapping = CreateFileMapping( |
87 | hFile, |
88 | NULL, /*not inherited*/ |
89 | PAGE_READWRITE, /*read and wite*/ |
90 | 0, /*high-order of object size*/ |
91 | MAPPINGSIZE, /*low-orger of object size*/ |
92 | NULL); /*unnamed object*/ |
93 | |
94 | if(NULL == hFileMapping) |
95 | { |
96 | Trace("ERROR:%u: Failed to create File Mapping.\n" , |
97 | GetLastError()); |
98 | CloseHandle(hFile); |
99 | Fail("" ); |
100 | } |
101 | |
102 | /* maps a view of a file into the address space of the calling process. |
103 | */ |
104 | lpMapViewAddress = MapViewOfFile( |
105 | hFileMapping, |
106 | FILE_MAP_ALL_ACCESS, /*access code*/ |
107 | 0, /*high order offset*/ |
108 | 0, /*low order offset*/ |
109 | MAPPINGSIZE); /* number of bytes for map */ |
110 | |
111 | if(NULL == lpMapViewAddress) |
112 | { |
113 | Trace("ERROR:%u: Failed to call MapViewOfFile API to map a view" |
114 | " of file!\n" , |
115 | GetLastError()); |
116 | CloseHandle(hFile); |
117 | CloseHandle(hFileMapping); |
118 | Fail("" ); |
119 | } |
120 | |
121 | /* Verify that the size of the file has increased to |
122 | * accomidate the MapView. |
123 | */ |
124 | dwFinalSize = GetFileSize (hFile, NULL); |
125 | if ( (dwFinalSize <= dwInitialSize) && (dwFinalSize != MAPPINGSIZE)) |
126 | { |
127 | CloseHandle(hFile); |
128 | CloseHandle(hFileMapping); |
129 | Fail("ERROR: Size of the file was expected to " |
130 | "increase from \"%d\", to \"%d\".\n " , |
131 | dwInitialSize, |
132 | MAPPINGSIZE); |
133 | } |
134 | |
135 | /* Write to the MapView and copy the MapViewOfFile |
136 | * to buffer, so we can compare with value read from |
137 | * file directly. |
138 | */ |
139 | |
140 | memcpy(lpMapViewAddress, buf, strlen(buf)); |
141 | memcpy(ch, (LPCSTR)lpMapViewAddress, MAPPINGSIZE); |
142 | |
143 | /* Read from the File handle. |
144 | */ |
145 | bRetVal = ReadFile(hFile, |
146 | readString, |
147 | strlen(buf), |
148 | &dwBytesRead, |
149 | NULL); |
150 | |
151 | if (bRetVal == FALSE) |
152 | { |
153 | Trace("ERROR: %u :unable to read from file handle " |
154 | "hFile=0x%lx\n" , |
155 | GetLastError(), |
156 | hFile); |
157 | CloseHandle(hFile); |
158 | CloseHandle(hFileMapping); |
159 | Fail("" ); |
160 | } |
161 | |
162 | if (memcmp(ch, readString, strlen(readString)) != 0) |
163 | { |
164 | CloseHandle(hFile); |
165 | CloseHandle(hFileMapping); |
166 | Fail("ERROR: Read string from file \"%s\", is " |
167 | "not equal to string written through MapView " |
168 | "\"%s\".\n" , |
169 | readString, |
170 | ch); |
171 | } |
172 | |
173 | /* Unmap the view of file. |
174 | */ |
175 | if(UnmapViewOfFile(lpMapViewAddress) == FALSE) |
176 | { |
177 | Trace("ERROR: Failed to call UnmapViewOfFile API to" |
178 | " unmap the view of a file, error code=%u\n" , |
179 | GetLastError()); |
180 | CloseHandle(hFile); |
181 | CloseHandle(hFileMapping); |
182 | Fail("" ); |
183 | } |
184 | |
185 | /* Close handle to create file. |
186 | */ |
187 | if(CloseHandle(hFile) == FALSE) |
188 | { |
189 | Trace("ERROR:%u:Failed to call CloseHandle API " |
190 | "to close a file handle." , |
191 | GetLastError()); |
192 | CloseHandle(hFileMapping); |
193 | Fail("" ); |
194 | } |
195 | |
196 | /* Close handle to file mapping. |
197 | */ |
198 | if(CloseHandle(hFileMapping) == FALSE) |
199 | { |
200 | Fail("ERROR:%u:Failed to call CloseHandle API " |
201 | "to close a file handle." , |
202 | GetLastError()); |
203 | } |
204 | |
205 | PAL_Terminate(); |
206 | return PASS; |
207 | } |
208 | |
209 | |
210 | |