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: Negative test the MapViewOfFile API.
10** Call MapViewOfFile with all access modes, except
11** read-only, on a read only map.
12**
13** Depends: CreateFile,
14** CreateFileMapping,
15** CloseHandle,
16** UnMapViewOfFile.
17**
18
19**
20**============================================================*/
21#include <palsuite.h>
22
23int __cdecl main(int argc, char *argv[])
24{
25
26 HANDLE hFile;
27 BOOL err;
28 HANDLE hFileMapping;
29 LPVOID lpMapViewAddress;
30 DWORD dwBytesWritten;
31 const int MAPPINGSIZE = 2048;
32 char buf[] = "this is a test string";
33 char lpFileName[] = "test.tmp";
34
35 /* Initialize the PAL environment.
36 */
37 if(0 != PAL_Initialize(argc, argv))
38 {
39 return FAIL;
40 }
41
42 /* Create a file handle with CreateFile.
43 */
44 hFile = CreateFile( lpFileName,
45 GENERIC_WRITE|GENERIC_READ,
46 FILE_SHARE_READ|FILE_SHARE_WRITE,
47 NULL,
48 CREATE_ALWAYS,
49 FILE_ATTRIBUTE_NORMAL,
50 NULL);
51 if (hFile == INVALID_HANDLE_VALUE)
52 {
53 Fail("ERROR: %u :unable to create file \"%s\".\n",
54 GetLastError(),
55 lpFileName);
56 }
57
58 /* Write to the File handle.
59 */
60 err = WriteFile(hFile,
61 buf,
62 strlen(buf),
63 &dwBytesWritten,
64 NULL);
65
66 if ( !FlushFileBuffers( hFile ) )
67 {
68 CloseHandle(hFile);
69 Fail("ERROR: Unable to flush the buffers\n");
70 }
71
72 if (err == FALSE)
73 {
74 Trace("ERROR: %u :unable to write to file handle "
75 "hFile=0x%lx\n",
76 GetLastError(),
77 hFile);
78 CloseHandle(hFile);
79 Fail("");
80 }
81
82 /* Create a unnamed file-mapping object with file handle FileHandle
83 * and with PAGE_READWRITE protection.
84 */
85 hFileMapping = CreateFileMapping(
86 hFile,
87 NULL, /*not inherited*/
88 PAGE_READONLY, /*read and wite*/
89 0, /*high-order of object size*/
90 0, /*low-orger of object size*/
91 NULL); /*unnamed object*/
92
93 if(NULL == hFileMapping)
94 {
95 Trace("ERROR:%u: Failed to create File Mapping.\n",
96 GetLastError());
97 CloseHandle(hFile);
98 Fail("");
99 }
100
101 /* map a writeable view of a file to a read-only file map.
102 */
103 lpMapViewAddress = MapViewOfFile(
104 hFileMapping,
105 FILE_MAP_WRITE, /* access code */
106 0, /* high order offset */
107 0, /* low order offset */
108 MAPPINGSIZE); /* number of bytes for map */
109
110 if(NULL != lpMapViewAddress)
111 {
112 Trace("ERROR:%u: Able to create a writeable MapViewOfFile"
113 " to a read-only file.\n",
114 GetLastError());
115 CloseHandle(hFile);
116 CloseHandle(hFileMapping);
117 UnmapViewOfFile(lpMapViewAddress);
118 Fail("");
119 }
120
121 /* map an all access view of a file to a read-only file map.
122 */
123 lpMapViewAddress = MapViewOfFile(
124 hFileMapping,
125 FILE_MAP_ALL_ACCESS, /* access code */
126 0, /* high order offset */
127 0, /* low order offset */
128 MAPPINGSIZE); /* number of bytes for map */
129
130 if(NULL != lpMapViewAddress)
131 {
132 Trace("ERROR:%u: Able to create an all access MapViewOfFile"
133 " to a read-only file.\n",
134 GetLastError());
135 CloseHandle(hFile);
136 CloseHandle(hFileMapping);
137 UnmapViewOfFile(lpMapViewAddress);
138 Fail("");
139 }
140
141 /* map an copy view of a file to a read-only file map.
142 */
143 lpMapViewAddress = MapViewOfFile(
144 hFileMapping,
145 FILE_MAP_COPY, /* access code */
146 0, /* high order offset */
147 0, /* low order offset */
148 MAPPINGSIZE); /* number of bytes for map */
149
150 if(NULL != lpMapViewAddress)
151 {
152 Trace("ERROR:%u: Able to create a copy access MapViewOfFile "
153 "to a read-only file.\n",
154 GetLastError());
155 CloseHandle(hFile);
156 CloseHandle(hFileMapping);
157 Fail("");
158 }
159
160 /* Clean-up and Teminate. */
161 CloseHandle(hFile);
162 CloseHandle(hFileMapping);
163 PAL_Terminate();
164 return PASS;
165}
166
167