[wasm][debugger][test] Change DateTime format test (#42136)
[platform/upstream/dotnet/runtime.git] / src / coreclr / src / pal / tests / palsuite / filemapping_memmgt / CreateFileMappingA / test7 / createfilemapping.cpp
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
4 /*=============================================================
5 **
6 ** Source:  createfilemapping.c (test 7)
7 **
8 ** Purpose: Positive test the CreateFileMapping API.
9 **          Test CreateFileMapping to a "swap" handle with 
10 **          access PAGE_READWRITE.
11 **
12 **
13 **============================================================*/
14 #include <palsuite.h>
15
16 const   int MAPPINGSIZE = 2048;
17 HANDLE  SWAP_HANDLE     = ((VOID *)(-1));
18
19 int __cdecl main(int argc, char *argv[])
20 {
21     char    testString[] = "this is a test string";
22     char    lpObjectName[] = "myMappingObject";
23     char    results[2048];
24     int     RetVal = PASS;
25     
26     HANDLE hFileMapRW;
27     LPVOID lpMapViewRW;
28     LPVOID lpMapViewRO;
29
30     /* Initialize the PAL environment.
31      */
32     if(0 != PAL_Initialize(argc, argv))
33     {
34         return FAIL;
35     }
36
37     /* Initialize the buffers.
38      */
39     memset(results,  0, MAPPINGSIZE);
40
41     /* Create a named file-mapping object with file handle FileHandle
42      * and with PAGE_READWRITE protection.
43     */
44     hFileMapRW = CreateFileMapping(
45                             SWAP_HANDLE,
46                             NULL,               /*not inherited*/
47                             PAGE_READWRITE,     /*read write*/
48                             0,                  /*high-order size*/
49                             MAPPINGSIZE,        /*low-order size*/
50                             lpObjectName);      /*unnamed object*/
51
52     if(NULL == hFileMapRW)
53     {
54         Fail("ERROR:%u: Failed to create File Mapping.\n", 
55              GetLastError());
56     }
57
58     /* Create a map view to the READWRITE file mapping.
59      */
60     lpMapViewRW = MapViewOfFile(
61                             hFileMapRW,
62                             FILE_MAP_ALL_ACCESS,/* access code */
63                             0,                  /* high order offset*/
64                             0,                  /* low order offset*/
65                             MAPPINGSIZE);       /* number of bytes for map */
66
67     if(NULL == lpMapViewRW)
68     {
69         Trace("ERROR:%u: Failed to call MapViewOfFile "
70               "API to map a view of file!\n", 
71               GetLastError());
72         RetVal = FAIL;
73         goto CleanUpOne;
74     }
75
76
77     /* Create a map view to the READWRITE file mapping.
78      */
79     lpMapViewRO = MapViewOfFile(
80                             hFileMapRW,
81                             FILE_MAP_READ,        /* access code */
82                             0,                    /* high order offset*/
83                             0,                    /* low order offset*/
84                             MAPPINGSIZE);         /* number of bytes for map */
85
86     if(NULL == lpMapViewRO)
87     {
88         Trace("ERROR:%u: Failed to call MapViewOfFile "
89               "API to map a view of file!\n", 
90               GetLastError());
91         RetVal = FAIL;
92         goto CleanUpTwo;
93     }
94
95     /* Write the test string to the Map view.
96     */    
97     memcpy(lpMapViewRW, testString, strlen(testString));
98
99     /* Read from the second Map view.
100     */
101     memcpy(results, (LPCSTR)lpMapViewRO, MAPPINGSIZE);
102
103     /* Verify the contents of the file mapping,
104      * by comparing what was written to what was read.
105      */
106     if (memcmp(results, testString, strlen(testString))!= 0)
107     {
108         Trace("ERROR: MapViewOfFile not equal to file contents "
109               "retrieved \"%s\", expected \"%s\".\n",
110               results,
111               testString);
112         RetVal = FAIL;
113         goto CleanUpThree;
114     }
115
116     /* Test successful.
117      */
118     RetVal = PASS;
119
120 CleanUpThree:
121         
122     /* Unmap the view of file.
123      */
124     if ( UnmapViewOfFile(lpMapViewRO) == FALSE )
125     {
126         Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
127                 GetLastError(),
128                 lpMapViewRO);
129         RetVal = FAIL;
130     }   
131
132 CleanUpTwo:
133
134     /* Unmap the view of file.
135      */
136     if ( UnmapViewOfFile(lpMapViewRW) == FALSE )
137     {
138         Trace("ERROR:%u: Failed to UnmapViewOfFile of \"%0x%lx\".\n",
139                 GetLastError(),
140                 lpMapViewRW);
141         RetVal = FAIL;
142     }
143
144
145 CleanUpOne:
146         
147     /* Close Handle to create file mapping.
148      */
149     if ( CloseHandle(hFileMapRW) == FALSE )
150     {
151         Trace("ERROR:%u: Failed to CloseHandle \"0x%lx\".\n",
152                 GetLastError(),
153                 hFileMapRW);
154         RetVal = FAIL;
155     }
156
157
158     /* Terminate the PAL.
159      */ 
160     PAL_TerminateEx(RetVal);
161     return RetVal;
162 }
163