8cf79b3c57b9919a89ba4e6dd6c05ab795aa1db6
[platform/core/dotnet/diagnostics.git] /
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_neg.c
8 **
9 ** Purpose: Negative test the CreateFileMapping API.
10 **          Call CreateFileMapping to create a unnamed
11 **          file-mapping object with PAGE_READONLY
12 **          protection and try to map a zero length file
13 **          in UNICODE
14 **
15 **
16 **============================================================*/
17 #define UNICODE
18 #include <palsuite.h>
19
20 int __cdecl main(int argc, char *argv[])
21 {
22
23     HANDLE FileHandle;
24     HANDLE FileMappingHandle;
25     int err;
26     WCHAR *lpFileName = NULL;
27
28     //Initialize the PAL environment
29     err = PAL_Initialize(argc, argv);
30     if(0 != err)
31     {
32         ExitProcess(FAIL);
33     }
34
35     //conver string to a unicode one
36     lpFileName = convert("temp.txt");
37
38
39     //create a file and return the file handle
40     FileHandle = CreateFile(lpFileName,
41         GENERIC_READ,
42         FILE_SHARE_READ,
43         NULL,
44         CREATE_ALWAYS,
45         FILE_ATTRIBUTE_ARCHIVE,
46         NULL);
47
48     //free this memory
49     free(lpFileName);
50    
51     if(INVALID_HANDLE_VALUE == FileHandle)
52     {
53         Fail("Failed to call CreateFile to create a file\n");
54     }
55
56     //create a unnamed file-mapping object with file handle FileHandle
57     //and with PAGE_READONLY protection
58     //try to map a file which is zero length.
59     FileMappingHandle = CreateFileMapping(
60         FileHandle,         //File Handle
61         NULL,               //not inherited
62         PAGE_READONLY,      //access protection 
63         0,                  //high-order of object size
64         0,                  //low-orger of object size
65         NULL);              //unnamed object
66
67
68     if(NULL != FileMappingHandle || ERROR_FILE_INVALID != GetLastError()) 
69     {//no error occurred 
70         Trace("\nFailed to call CreateFileMapping API for a negative test!\n");
71         err = CloseHandle(FileHandle);
72         if(0 == err)
73         {
74             Fail("\nFailed to call CloseHandle API\n");
75         }
76         err = CloseHandle(FileMappingHandle);
77         if(0 == err)
78         {
79             Fail("\nFailed to call CloseHandle API\n");
80         }
81         Fail("");
82     }
83     
84     //close the file handle
85     err = CloseHandle(FileHandle);
86     if(0 == err)
87     {
88         Fail("\nFailed to call CloseHandle API\n");
89     }
90
91     PAL_Terminate();
92     return PASS;
93 }