ReactOS  0.4.13-dev-242-g611e6d7
CAtlFileMapping.cpp File Reference
#include <atlfile.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <windows.h>
Include dependency graph for CAtlFileMapping.cpp:

Go to the source code of this file.

Classes

struct  TestData
 

Macros

#define ok(value, ...)
 
#define ok_(x1, x2)   set_location(x1,x2); ok_func
 
#define skip(...)
 
#define START_TEST(x)   int main()
 

Functions

void set_location (const char *file, int line)
 
void ok_func (int value, const char *fmt,...)
 
void skip_func (const char *fmt,...)
 
charwine_dbgstr_w (const wchar_t *wstr)
 
static void test_SharedMem ()
 
static void test_FileMapping ()
 
 START_TEST (CAtlFileMapping)
 

Variables

int g_tests_executed = 0
 
int g_tests_failed = 0
 
int g_tests_skipped = 0
 
const charg_file = NULL
 
int g_line = 0
 

Macro Definition Documentation

◆ ok

#define ok (   value,
  ... 
)
Value:
do { \
set_location(__FILE__, __LINE__); \
ok_func(value, __VA_ARGS__); \
} while (0)

Definition at line 50 of file CAtlFileMapping.cpp.

◆ ok_

#define ok_ (   x1,
  x2 
)    set_location(x1,x2); ok_func

Definition at line 54 of file CAtlFileMapping.cpp.

◆ skip

#define skip (   ...)
Value:
do { \
set_location(__FILE__, __LINE__); \
skip_func(__VA_ARGS__); \
} while (0)

Definition at line 55 of file CAtlFileMapping.cpp.

◆ START_TEST

#define START_TEST (   x)    int main()

Definition at line 59 of file CAtlFileMapping.cpp.

Function Documentation

◆ ok_func()

void ok_func ( int  value,
const char fmt,
  ... 
)

Definition at line 27 of file CAtlFileMapping.cpp.

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  }
int g_tests_executed
const char * g_file
int g_tests_failed
int g_line
#define va_end(ap)
Definition: acmsvcex.h:90
char * va_list
Definition: acmsvcex.h:78
_Check_return_opt_ _CRTIMP int __cdecl vprintf(_In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
#define va_start(ap, A)
Definition: acmsvcex.h:91
Definition: dsound.c:943
#define printf
Definition: config.h:203

◆ set_location()

void set_location ( const char file,
int  line 
)

Definition at line 22 of file CAtlFileMapping.cpp.

23  {
24  g_file = file;
25  g_line = line;
26  }
const char * g_file
int g_line
const char file[]
Definition: icontest.c:11
char line[200]
Definition: main.c:97

◆ skip_func()

void skip_func ( const char fmt,
  ... 
)

Definition at line 40 of file CAtlFileMapping.cpp.

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  }
const char * g_file
int g_line
#define va_end(ap)
Definition: acmsvcex.h:90
char * va_list
Definition: acmsvcex.h:78
int g_tests_skipped
_Check_return_opt_ _CRTIMP int __cdecl vprintf(_In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
#define va_start(ap, A)
Definition: acmsvcex.h:91
Definition: dsound.c:943
#define printf
Definition: config.h:203

◆ START_TEST()

START_TEST ( CAtlFileMapping  )

Definition at line 245 of file CAtlFileMapping.cpp.

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 }
int g_tests_executed
static void test_SharedMem()
int g_tests_failed
int g_tests_skipped
static void test_FileMapping()
#define printf
Definition: config.h:203

◆ test_FileMapping()

static void test_FileMapping ( )
static

Definition at line 192 of file CAtlFileMapping.cpp.

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 }
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:607
HRESULT hr
Definition: shlfolder.c:183
#define _countof(array)
Definition: fontsub.cpp:30
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
#define FILE_SHARE_READ
Definition: compat.h:125
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
#define ok(value,...)
smooth NULL
Definition: ftsmooth.c:416
#define FILE_MAP_READ
Definition: compat.h:427
#define OPEN_EXISTING
Definition: compat.h:426
__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
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:481
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
void test2(void)
Definition: regtest.c:173
#define S_OK
Definition: intsafe.h:59
#define PAGE_READONLY
Definition: compat.h:127
#define CreateFileW
Definition: compat.h:400

Referenced by START_TEST().

◆ test_SharedMem()

static void test_SharedMem ( )
static

Definition at line 74 of file CAtlFileMapping.cpp.

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 }
#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
HRESULT hr
Definition: shlfolder.c:183
void test3(void)
Definition: hivetest.c:423
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
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define _T(x)
Definition: vfdio.h:22
LONG HRESULT
Definition: typedefs.h:77
void test1(void)
Definition: hivetest.c:366
struct _TestData TestData
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
void test2(void)
Definition: regtest.c:173
#define S_OK
Definition: intsafe.h:59
#define PAGE_EXECUTE_READ
Definition: nt_native.h:1307
#define memset(x, y, z)
Definition: compat.h:39
#define PAGE_READWRITE
Definition: nt_native.h:1304

Referenced by START_TEST().

◆ wine_dbgstr_w()

char* wine_dbgstr_w ( const wchar_t wstr)

Definition at line 60 of file CAtlFileMapping.cpp.

61  {
62  static char buf[512];
63  WideCharToMultiByte(CP_ACP, 0, wstr, -1, buf, _countof(buf), NULL, NULL);
64  return buf;
65  }
#define WideCharToMultiByte
Definition: compat.h:101
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define CP_ACP
Definition: compat.h:99
#define _countof(array)
Definition: fontsub.cpp:30
smooth NULL
Definition: ftsmooth.c:416

Variable Documentation

◆ g_file

const char* g_file = NULL

Definition at line 20 of file CAtlFileMapping.cpp.

Referenced by ok_func(), set_location(), and skip_func().

◆ g_line

int g_line = 0

Definition at line 21 of file CAtlFileMapping.cpp.

Referenced by ok_func(), set_location(), and skip_func().

◆ g_tests_executed

int g_tests_executed = 0

Definition at line 17 of file CAtlFileMapping.cpp.

Referenced by ok_func(), and START_TEST().

◆ g_tests_failed

int g_tests_failed = 0

Definition at line 18 of file CAtlFileMapping.cpp.

Referenced by ok_func(), and START_TEST().

◆ g_tests_skipped

int g_tests_skipped = 0

Definition at line 19 of file CAtlFileMapping.cpp.

Referenced by skip_func(), and START_TEST().