ReactOS  0.4.13-dev-242-g611e6d7
CAtlFileMapping.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: Test for CAtlFileMapping
5  * COPYRIGHT: Copyright 2019 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include <atlfile.h>
9 
10 #ifdef HAVE_APITEST
11  #include <apitest.h>
12 #else
13  #include <stdlib.h>
14  #include <stdio.h>
15  #include <stdarg.h>
16  #include <windows.h>
18  int g_tests_failed = 0;
19  int g_tests_skipped = 0;
20  const char *g_file = NULL;
21  int g_line = 0;
22  void set_location(const char *file, int line)
23  {
24  g_file = file;
25  g_line = line;
26  }
27  void ok_func(int value, const char *fmt, ...)
28  {
29  va_list va;
30  va_start(va, fmt);
31  if (!value)
32  {
33  printf("%s (%d): ", g_file, g_line);
34  vprintf(fmt, va);
36  }
38  va_end(va);
39  }
40  void skip_func(const char *fmt, ...)
41  {
42  va_list va;
43  va_start(va, fmt);
44  printf("%s (%d): test skipped: ", g_file, g_line);
45  vprintf(fmt, va);
47  va_end(va);
48  }
49  #undef ok
50  #define ok(value, ...) do { \
51  set_location(__FILE__, __LINE__); \
52  ok_func(value, __VA_ARGS__); \
53  } while (0)
54  #define ok_(x1,x2) set_location(x1,x2); ok_func
55  #define skip(...) do { \
56  set_location(__FILE__, __LINE__); \
57  skip_func(__VA_ARGS__); \
58  } while (0)
59  #define START_TEST(x) int main()
60  char *wine_dbgstr_w(const wchar_t *wstr)
61  {
62  static char buf[512];
63  WideCharToMultiByte(CP_ACP, 0, wstr, -1, buf, _countof(buf), NULL, NULL);
64  return buf;
65  }
66 #endif
67 
68 struct TestData
69 {
70  int data[4];
71 };
72 
73 
74 static void test_SharedMem()
75 {
78  BOOL bAlreadyExisted;
79  HRESULT hr;
80 
81  ok(test1.GetData() == NULL, "Expected NULL, got %p\n", test1.GetData());
82  ok(test3.GetData() == NULL, "Expected NULL, got %p\n", test3.GetData());
83  ok(test1.GetHandle() == NULL, "Expected NULL, got %p\n", test1.GetHandle());
84  ok(test3.GetHandle() == NULL, "Expected NULL, got %p\n", test3.GetHandle());
85  ok(test1.GetMappingSize() == 0, "Expected 0, got %lu\n", test1.GetMappingSize());
86  ok(test3.GetMappingSize() == 0, "Expected 0, got %lu\n", test3.GetMappingSize());
87 
88  test1 = test1;
89  //test1 = test2; // Asserts on orig.m_pData != NULL, same with CopyFrom
90  //test1 = test3; // does not compile
91  hr = test1.Unmap();
92  ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
93 
94  // Asserts on name == NULL
95  hr = test1.OpenMapping(_T("TEST_MAPPING"), 123, 0, FILE_MAP_ALL_ACCESS);
96  ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got 0x%lx\n", hr);
97  ok(test1.GetData() == NULL, "Expected NULL, got %p\n", test1.GetData());
98  ok(test1.GetHandle() == NULL, "Expected NULL, got %p\n", test1.GetHandle());
99  ok(test1.GetMappingSize() == 123, "Expected 123, got %lu\n", test1.GetMappingSize());
100 
101  hr = test1.Unmap();
102  ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
103  ok(test1.GetData() == NULL, "Expected NULL, got %p\n", test1.GetData());
104  ok(test1.GetHandle() == NULL, "Expected NULL, got %p\n", test1.GetHandle());
105  ok(test1.GetMappingSize() == 123, "Expected 123, got %lu\n", test1.GetMappingSize());
106 
107  bAlreadyExisted = 123;
108  hr = test1.MapSharedMem(sizeof(TestData), _T("TEST_MAPPING"), &bAlreadyExisted, (LPSECURITY_ATTRIBUTES)0, PAGE_READWRITE, FILE_MAP_ALL_ACCESS);
109  ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
110  ok(test1.GetData() != NULL, "Expected ptr, got %p\n", test1.GetData());
111  ok(test1.GetHandle() != NULL, "Expected handle, got %p\n", test1.GetHandle());
112  ok(test1.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test1.GetMappingSize());
113  ok(bAlreadyExisted == FALSE, "Expected FALSE, got %u\n", bAlreadyExisted);
114 
115  if (test1.GetData())
116  {
117  memset(test1.GetData(), 0x35, sizeof(TestData));
118  }
119 
120  hr = test2.CopyFrom(test1);
121  ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
122  ok(test2.GetData() != NULL, "Expected ptr, got %p\n", test2.GetData());
123  ok(test2.GetHandle() != NULL, "Expected handle, got %p\n", test2.GetHandle());
124  ok(test2.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test2.GetMappingSize());
125 
126  // test1 is not closed:
127  ok(test1.GetData() != NULL, "Expected ptr, got %p\n", test1.GetData());
128  ok(test1.GetHandle() != NULL, "Expected handle, got %p\n", test1.GetHandle());
129  ok(test1.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test1.GetMappingSize());
130 
131  // test2 does not equal test1
132  ok(test1.GetData() != test2.GetData(), "Expected different ptrs\n");
133  ok(test1.GetHandle() != test2.GetHandle(), "Expected different handles\n");
134 
135  TestData* t1 = test1;
136  TestData* t2 = test2;
137  if (t1 && t2)
138  {
139  ok(t1->data[0] == 0x35353535, "Expected 0x35353535, got 0x%x\n", t1->data[0]);
140  ok(t2->data[0] == 0x35353535, "Expected 0x35353535, got 0x%x\n", t2->data[0]);
141 
142  t1->data[0] = 0xbeefbeef;
143  ok(t1->data[0] == (int)0xbeefbeef, "Expected 0xbeefbeef, got 0x%x\n", t1->data[0]);
144  ok(t2->data[0] == (int)0xbeefbeef, "Expected 0xbeefbeef, got 0x%x\n", t2->data[0]);
145  }
146 
147  hr = test3.OpenMapping(_T("TEST_MAPPING"), sizeof(TestData), offsetof(TestData, data[1]));
148  ok(hr == HRESULT_FROM_WIN32(ERROR_MAPPED_ALIGNMENT), "Expected HRESULT_FROM_WIN32(ERROR_MAPPED_ALIGNMENT), got 0x%lx\n", hr);
149  ok(test3.GetData() == NULL, "Expected NULL, got %p\n", test3.GetData());
150  ok(test3.GetHandle() == NULL, "Expected NULL, got %p\n", test3.GetHandle());
151  ok(test3.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test3.GetMappingSize());
152 
153  hr = test2.Unmap();
154  ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
155  ok(test2.GetData() == NULL, "Expected NULL, got %p\n", test2.GetData());
156  ok(test2.GetHandle() == NULL, "Expected NULL, got %p\n", test2.GetHandle());
157  ok(test2.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test2.GetMappingSize());
158 
159  bAlreadyExisted = 123;
160  // Wrong access flag
161  hr = test2.MapSharedMem(sizeof(TestData), _T("TEST_MAPPING"), &bAlreadyExisted, (LPSECURITY_ATTRIBUTES)0, PAGE_EXECUTE_READ, FILE_MAP_ALL_ACCESS);
162  ok(hr == E_ACCESSDENIED, "Expected E_ACCESSDENIED, got 0x%lx\n", hr);
163  ok(test2.GetData() == NULL, "Expected NULL, got %p\n", test2.GetData());
164  ok(test2.GetHandle() == NULL, "Expected NULL, got %p\n", test2.GetHandle());
165  ok(test2.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test2.GetMappingSize());
166  ok(bAlreadyExisted == TRUE, "Expected TRUE, got %u\n", bAlreadyExisted);
167 
168  bAlreadyExisted = 123;
169  hr = test2.MapSharedMem(sizeof(TestData), _T("TEST_MAPPING"), &bAlreadyExisted, (LPSECURITY_ATTRIBUTES)0, PAGE_READWRITE, FILE_MAP_ALL_ACCESS);
170  ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
171  ok(test2.GetData() != NULL, "Expected ptr, got %p\n", test2.GetData());
172  ok(test2.GetHandle() != NULL, "Expected handle, got %p\n", test2.GetHandle());
173  ok(test2.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test2.GetMappingSize());
174  ok(bAlreadyExisted == TRUE, "Expected TRUE, got %u\n", bAlreadyExisted);
175 
176  // test2 does not equal test1
177  ok(test1.GetData() != test2.GetData(), "Expected different ptrs\n");
178  ok(test1.GetHandle() != test2.GetHandle(), "Expected different handles\n");
179 
180  t2 = test2;
181  if (t1 && t2)
182  {
183  ok(t1->data[0] == (int)0xbeefbeef, "Expected 0xbeefbeef, got 0x%x\n", t1->data[0]);
184  ok(t2->data[0] == (int)0xbeefbeef, "Expected 0xbeefbeef, got 0x%x\n", t2->data[0]);
185 
186  t1->data[0] = 0xdeaddead;
187  ok(t1->data[0] == (int)0xdeaddead, "Expected 0xdeaddead, got 0x%x\n", t1->data[0]);
188  ok(t2->data[0] == (int)0xdeaddead, "Expected 0xdeaddead, got 0x%x\n", t2->data[0]);
189  }
190 }
191 
192 static void test_FileMapping()
193 {
194  WCHAR Buf[MAX_PATH] = {0};
196 
197  GetModuleFileNameW(NULL, Buf, _countof(Buf));
198 
200  HRESULT hr;
201 
202  {
204  ok(hFile != INVALID_HANDLE_VALUE, "Could not open %S, aborting test\n", Buf);
206  return;
207 
209 
210  hr = test1.MapFile(hFile, 0, 0, PAGE_READONLY, FILE_MAP_READ);
211  ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
212  ok(test1.GetData() != NULL, "Expected ptr, got %p\n", test1.GetData());
213  ok(test1.GetHandle() != NULL, "Expected handle, got %p\n", test1.GetHandle());
214  ok(test1.GetMappingSize() == FileSize.LowPart, "Expected %lu, got %lu\n", FileSize.LowPart, test1.GetMappingSize());
215 
216  hr = test2.MapFile(hFile, 0, 0x10000, PAGE_READONLY, FILE_MAP_READ);
217  ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
218  ok(test2.GetData() != NULL, "Expected ptr, got %p\n", test2.GetData());
219  ok(test2.GetHandle() != NULL, "Expected handle, got %p\n", test2.GetHandle());
220  // Offset is subtracted
221  ok(test2.GetMappingSize() == FileSize.LowPart - 0x10000, "Expected %lu, got %lu\n", FileSize.LowPart-0x10000, test2.GetMappingSize());
222 
223  hr = test1.Unmap();
224  ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
225  ok(test1.GetData() == NULL, "Expected NULL, got %p\n", test1.GetData());
226  ok(test1.GetHandle() == NULL, "Expected NULL, got %p\n", test1.GetHandle());
227  ok(test1.GetMappingSize() == FileSize.LowPart, "Expected %lu, got %lu\n", FileSize.LowPart, test1.GetMappingSize());
228 
229  hr = test1.MapFile(hFile, 0x1000);
230  ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
231  ok(test1.GetData() != NULL, "Expected ptr, got %p\n", test1.GetData());
232  ok(test1.GetHandle() != NULL, "Expected handle, got %p\n", test1.GetHandle());
233  ok(test1.GetMappingSize() == 0x1000, "Expected 0x1000, got %lu\n", test1.GetMappingSize());
234  }
235 
236  // We can still access it after the file is closed
238  if (dos)
239  {
240  ok(dos->e_magic == IMAGE_DOS_SIGNATURE, "Expected IMAGE_DOS_SIGNATURE, got 0x%x\n", dos->e_magic);
241  }
242 }
243 
244 
246 {
247  test_SharedMem();
249 
250 #ifndef HAVE_APITEST
251  printf("CAtlFile: %i tests executed (0 marked as todo, %i failures), %i skipped.\n", g_tests_executed, g_tests_failed, g_tests_skipped);
252  return g_tests_failed;
253 #endif
254 }
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:607
#define E_ACCESSDENIED
Definition: winerror.h:2849
#define TRUE
Definition: types.h:120
#define ERROR_MAPPED_ALIGNMENT
Definition: winerror.h:668
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
int g_tests_executed
static void test_SharedMem()
#define WideCharToMultiByte
Definition: compat.h:101
HRESULT hr
Definition: shlfolder.c:183
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
void test3(void)
Definition: hivetest.c:423
const char * g_file
#define CP_ACP
Definition: compat.h:99
#define _countof(array)
Definition: fontsub.cpp:30
int g_tests_failed
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
int g_line
void set_location(const char *file, int line)
#define FILE_SHARE_READ
Definition: compat.h:125
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
#define va_end(ap)
Definition: acmsvcex.h:90
unsigned int BOOL
Definition: ntddk_ex.h:94
#define ok(value,...)
smooth NULL
Definition: ftsmooth.c:416
#define offsetof(TYPE, MEMBER)
#define FILE_MAP_ALL_ACCESS
Definition: winbase.h:156
Definition: parser.c:48
char * va_list
Definition: acmsvcex.h:78
#define FILE_MAP_READ
Definition: compat.h:427
int g_tests_skipped
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define OPEN_EXISTING
Definition: compat.h:426
#define _T(x)
Definition: vfdio.h:22
_Check_return_opt_ _CRTIMP int __cdecl vprintf(_In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
__wchar_t WCHAR
Definition: xmlstorage.h:180
LONG HRESULT
Definition: typedefs.h:77
#define MAX_PATH
Definition: compat.h:26
void test1(void)
Definition: hivetest.c:366
const char file[]
Definition: icontest.c:11
struct _TestData TestData
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:481
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
static void test_FileMapping()
char line[200]
Definition: main.c:97
IMAGE_DOS_HEADER dos
Definition: module.c:49
ULONG LowPart
Definition: typedefs.h:104
_Must_inspect_result_ _Out_ PLARGE_INTEGER FileSize
Definition: fsrtlfuncs.h:108
#define GENERIC_READ
Definition: compat.h:124
_In_ HANDLE hFile
Definition: mswsock.h:90
char * wine_dbgstr_w(const wchar_t *wstr)
void skip_func(const char *fmt,...)
void test2(void)
Definition: regtest.c:173
#define S_OK
Definition: intsafe.h:59
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define START_TEST(x)
#define PAGE_READONLY
Definition: compat.h:127
#define CreateFileW
Definition: compat.h:400
void ok_func(int value, const char *fmt,...)
#define PAGE_EXECUTE_READ
Definition: nt_native.h:1307
#define memset(x, y, z)
Definition: compat.h:39
Definition: dsound.c:943
#define printf
Definition: config.h:203
#define PAGE_READWRITE
Definition: nt_native.h:1304
Definition: fci.c:126