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