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 9) |
8 | ** |
9 | ** Purpose: Negative test the CreateFileMapping API. |
10 | ** |
11 | ** |
12 | **============================================================*/ |
13 | #include <palsuite.h> |
14 | |
15 | const int MAPPINGSIZE = 2048; |
16 | |
17 | int __cdecl main(int argc, char *argv[]) |
18 | { |
19 | |
20 | HANDLE hFile; |
21 | char lpFileName[] = "test.tmp" ; |
22 | |
23 | HANDLE hFileMapping; |
24 | |
25 | /* Initialize the PAL environment. |
26 | */ |
27 | if(0 != PAL_Initialize(argc, argv)) |
28 | { |
29 | return FAIL; |
30 | } |
31 | |
32 | /* Create a file handle with CreateFile, as READONLY |
33 | */ |
34 | hFile = CreateFile( lpFileName, |
35 | GENERIC_READ, |
36 | FILE_SHARE_READ, |
37 | NULL, |
38 | CREATE_ALWAYS, |
39 | FILE_ATTRIBUTE_NORMAL, |
40 | NULL); |
41 | |
42 | if (hFile == INVALID_HANDLE_VALUE) |
43 | { |
44 | Fail("ERROR: %u :unable to create file \"%s\".\n" , |
45 | GetLastError(), |
46 | lpFileName); |
47 | } |
48 | |
49 | /* Attempt to create a unnamed file-mapping object to a READONLY file |
50 | * as READWRITE access. |
51 | */ |
52 | hFileMapping = CreateFileMapping( |
53 | hFile, |
54 | NULL, /*not inherited*/ |
55 | PAGE_READWRITE, /*read and write*/ |
56 | 0, /*high-order size*/ |
57 | MAPPINGSIZE, /*low-order size*/ |
58 | NULL); /*unnamed object*/ |
59 | |
60 | if(NULL != hFileMapping) |
61 | { |
62 | Trace("ERROR: Able to create READWRITE mapping to a " |
63 | "READONLY file.\n" ); |
64 | if( 0 == CloseHandle(hFile) ) |
65 | { |
66 | Trace("Unexpected Error: Unable to close file handle\n" ); |
67 | } |
68 | Fail("" ); |
69 | } |
70 | |
71 | /* Attempt to create a unnamed file-mapping object to a zero lenght |
72 | * file. |
73 | */ |
74 | hFileMapping = CreateFileMapping( |
75 | hFile, |
76 | NULL, /*not inherited*/ |
77 | PAGE_READWRITE, /*read and write*/ |
78 | 0, /*high-order size*/ |
79 | 0, /*low-order size*/ |
80 | NULL); /*unnamed object*/ |
81 | |
82 | if( NULL != hFileMapping ) |
83 | { |
84 | Trace("ERROR: Able to create READWRITE mapping to a " |
85 | "READONLY file.\n" ); |
86 | if( 0 == CloseHandle(hFile) ) |
87 | { |
88 | Trace("Unexpected Error: Unable to close file handle\n" ); |
89 | } |
90 | Fail("" ); |
91 | } |
92 | if(GetLastError() != ERROR_ACCESS_DENIED) |
93 | { |
94 | Trace("ERROR: Expected GetLastError() to return " |
95 | "ERROR_FILE_INVALID (%d), it returned %u.\n" , |
96 | ERROR_FILE_INVALID, |
97 | GetLastError()); |
98 | if( 0 == CloseHandle(hFile) ) |
99 | { |
100 | Trace("Unexpected Error: Unable to close file handle\n" ); |
101 | } |
102 | Fail("" ); |
103 | } |
104 | |
105 | /* Attempt to create a file mapping that is larger than |
106 | * the file. |
107 | */ |
108 | hFileMapping = CreateFileMapping( |
109 | hFile, |
110 | NULL, /*not inherited*/ |
111 | PAGE_READONLY, /*read only*/ |
112 | 0, /*high-order size*/ |
113 | MAPPINGSIZE, /*low-order size*/ |
114 | NULL); /*unnamed object*/ |
115 | if(NULL != hFileMapping) |
116 | { |
117 | Trace("ERROR: Able to create file mapping of size %d to " |
118 | "file of size 0.\n" , |
119 | MAPPINGSIZE); |
120 | if( 0 == CloseHandle(hFile) ) |
121 | { |
122 | Trace("Unexpected Error: Unable to close file handle\n" ); |
123 | } |
124 | Fail("" ); |
125 | } |
126 | |
127 | if(GetLastError() != ERROR_NOT_ENOUGH_MEMORY ) |
128 | { |
129 | Trace("ERROR: Expected GetLastError() to return " |
130 | "ERROR_NOT_ENOUGH_MEMORY (%d), it returned %u.\n" , |
131 | ERROR_NOT_ENOUGH_MEMORY, |
132 | GetLastError()); |
133 | if( 0 == CloseHandle(hFile) ) |
134 | { |
135 | Trace("Unexpected Error: Unable to close file handle\n" ); |
136 | } |
137 | Fail("" ); |
138 | } |
139 | |
140 | if( 0 == CloseHandle(hFile) ) |
141 | { |
142 | Fail("Unexpected Error: Unable to close file handle\n" ); |
143 | } |
144 | |
145 | /* Terminate the PAL. |
146 | */ |
147 | PAL_Terminate(); |
148 | return PASS; |
149 | } |
150 | |
151 | |