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** Passing invalid values for the hFileMappingObject.
11**
12** Depends: CreatePipe,
13** CreateFile,
14** CreateFileMapping,
15** CloseHandle.
16**
17
18**
19**============================================================*/
20#include <palsuite.h>
21
22int __cdecl main(int argc, char *argv[])
23{
24
25 const int MAPPINGSIZE = 2048;
26 HANDLE hFileMapping;
27 LPVOID lpMapViewAddress;
28 HANDLE hReadPipe = NULL;
29 HANDLE hWritePipe = NULL;
30 BOOL bRetVal;
31
32 SECURITY_ATTRIBUTES lpPipeAttributes;
33
34 /* Initialize the PAL environment.
35 */
36 if(0 != PAL_Initialize(argc, argv))
37 {
38 return FAIL;
39 }
40
41
42 /* Attempt to create a MapViewOfFile with a NULL handle.
43 */
44 hFileMapping = NULL;
45
46 lpMapViewAddress = MapViewOfFile(
47 hFileMapping,
48 FILE_MAP_WRITE, /* access code */
49 0, /* high order offset */
50 0, /* low order offset */
51 MAPPINGSIZE); /* number of bytes for map */
52
53 if((NULL != lpMapViewAddress) &&
54 (GetLastError() != ERROR_INVALID_HANDLE))
55 {
56 Trace("ERROR:%u: Able to create a MapViewOfFile with "
57 "hFileMapping=0x%lx.\n",
58 GetLastError());
59 UnmapViewOfFile(lpMapViewAddress);
60 Fail("");
61 }
62
63 /* Attempt to create a MapViewOfFile with an invalid handle.
64 */
65 hFileMapping = INVALID_HANDLE_VALUE;
66
67 lpMapViewAddress = MapViewOfFile(
68 hFileMapping,
69 FILE_MAP_WRITE, /* access code */
70 0, /* high order offset */
71 0, /* low order offset */
72 MAPPINGSIZE); /* number of bytes for map */
73
74 if((NULL != lpMapViewAddress) &&
75 (GetLastError() != ERROR_INVALID_HANDLE))
76 {
77 Trace("ERROR:%u: Able to create a MapViewOfFile with "
78 "hFileMapping=0x%lx.\n",
79 GetLastError());
80 UnmapViewOfFile(lpMapViewAddress);
81 Fail("");
82 }
83
84 /* Setup SECURITY_ATTRIBUTES structure for CreatePipe.
85 */
86 lpPipeAttributes.nLength = sizeof(lpPipeAttributes);
87 lpPipeAttributes.lpSecurityDescriptor = NULL;
88 lpPipeAttributes.bInheritHandle = TRUE;
89
90 /* Create a Pipe.
91 */
92 bRetVal = CreatePipe(&hReadPipe, /* read handle*/
93 &hWritePipe, /* write handle */
94 &lpPipeAttributes,/* security attributes*/
95 0); /* pipe size*/
96 if (bRetVal == FALSE)
97 {
98 Fail("ERROR: %ld :Unable to create pipe\n",
99 GetLastError());
100 }
101
102 /* Attempt creating a MapViewOfFile with a Pipe Handle.
103 */
104 lpMapViewAddress = MapViewOfFile(
105 hReadPipe,
106 FILE_MAP_WRITE, /* 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 (GetLastError() != ERROR_INVALID_HANDLE))
113 {
114 Trace("ERROR:%u: Able to create a MapViewOfFile with "
115 "hFileMapping=0x%lx.\n",
116 GetLastError());
117 CloseHandle(hReadPipe);
118 CloseHandle(hWritePipe);
119 UnmapViewOfFile(lpMapViewAddress);
120 Fail("");
121 }
122
123 /* Clean-up and Terminate the PAL.
124 */
125 CloseHandle(hReadPipe);
126 CloseHandle(hWritePipe);
127 PAL_Terminate();
128 return PASS;
129}
130
131
132