19853cc4dSMark Jansen /*
29853cc4dSMark Jansen * PROJECT: ReactOS api tests
39853cc4dSMark Jansen * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
49853cc4dSMark Jansen * PURPOSE: Test for CAtlFileMapping
59853cc4dSMark Jansen * COPYRIGHT: Copyright 2019 Mark Jansen (mark.jansen@reactos.org)
69853cc4dSMark Jansen */
79853cc4dSMark Jansen
89853cc4dSMark Jansen #include <atlfile.h>
99853cc4dSMark Jansen
10c714b7fdSKatayama Hirofumi MZ #ifdef HAVE_APITEST
119853cc4dSMark Jansen #include <apitest.h>
129853cc4dSMark Jansen #else
13*a800fa7bSKatayama Hirofumi MZ #include "atltest.h"
149853cc4dSMark Jansen #endif
159853cc4dSMark Jansen
169853cc4dSMark Jansen struct TestData
179853cc4dSMark Jansen {
189853cc4dSMark Jansen int data[4];
199853cc4dSMark Jansen };
209853cc4dSMark Jansen
219853cc4dSMark Jansen
test_SharedMem()229853cc4dSMark Jansen static void test_SharedMem()
239853cc4dSMark Jansen {
249853cc4dSMark Jansen CAtlFileMapping<TestData> test1, test2;
259853cc4dSMark Jansen CAtlFileMappingBase test3;
269853cc4dSMark Jansen BOOL bAlreadyExisted;
279853cc4dSMark Jansen HRESULT hr;
289853cc4dSMark Jansen
299853cc4dSMark Jansen ok(test1.GetData() == NULL, "Expected NULL, got %p\n", test1.GetData());
309853cc4dSMark Jansen ok(test3.GetData() == NULL, "Expected NULL, got %p\n", test3.GetData());
319853cc4dSMark Jansen ok(test1.GetHandle() == NULL, "Expected NULL, got %p\n", test1.GetHandle());
329853cc4dSMark Jansen ok(test3.GetHandle() == NULL, "Expected NULL, got %p\n", test3.GetHandle());
339853cc4dSMark Jansen ok(test1.GetMappingSize() == 0, "Expected 0, got %lu\n", test1.GetMappingSize());
349853cc4dSMark Jansen ok(test3.GetMappingSize() == 0, "Expected 0, got %lu\n", test3.GetMappingSize());
359853cc4dSMark Jansen
369853cc4dSMark Jansen test1 = test1;
379853cc4dSMark Jansen //test1 = test2; // Asserts on orig.m_pData != NULL, same with CopyFrom
389853cc4dSMark Jansen //test1 = test3; // does not compile
399853cc4dSMark Jansen hr = test1.Unmap();
409853cc4dSMark Jansen ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
419853cc4dSMark Jansen
429853cc4dSMark Jansen // Asserts on name == NULL
439853cc4dSMark Jansen hr = test1.OpenMapping(_T("TEST_MAPPING"), 123, 0, FILE_MAP_ALL_ACCESS);
449853cc4dSMark Jansen ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got 0x%lx\n", hr);
459853cc4dSMark Jansen ok(test1.GetData() == NULL, "Expected NULL, got %p\n", test1.GetData());
469853cc4dSMark Jansen ok(test1.GetHandle() == NULL, "Expected NULL, got %p\n", test1.GetHandle());
479853cc4dSMark Jansen ok(test1.GetMappingSize() == 123, "Expected 123, got %lu\n", test1.GetMappingSize());
489853cc4dSMark Jansen
499853cc4dSMark Jansen hr = test1.Unmap();
509853cc4dSMark Jansen ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
519853cc4dSMark Jansen ok(test1.GetData() == NULL, "Expected NULL, got %p\n", test1.GetData());
529853cc4dSMark Jansen ok(test1.GetHandle() == NULL, "Expected NULL, got %p\n", test1.GetHandle());
539853cc4dSMark Jansen ok(test1.GetMappingSize() == 123, "Expected 123, got %lu\n", test1.GetMappingSize());
549853cc4dSMark Jansen
559853cc4dSMark Jansen bAlreadyExisted = 123;
569853cc4dSMark Jansen hr = test1.MapSharedMem(sizeof(TestData), _T("TEST_MAPPING"), &bAlreadyExisted, (LPSECURITY_ATTRIBUTES)0, PAGE_READWRITE, FILE_MAP_ALL_ACCESS);
579853cc4dSMark Jansen ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
589853cc4dSMark Jansen ok(test1.GetData() != NULL, "Expected ptr, got %p\n", test1.GetData());
599853cc4dSMark Jansen ok(test1.GetHandle() != NULL, "Expected handle, got %p\n", test1.GetHandle());
609853cc4dSMark Jansen ok(test1.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test1.GetMappingSize());
619853cc4dSMark Jansen ok(bAlreadyExisted == FALSE, "Expected FALSE, got %u\n", bAlreadyExisted);
629853cc4dSMark Jansen
639853cc4dSMark Jansen if (test1.GetData())
649853cc4dSMark Jansen {
659853cc4dSMark Jansen memset(test1.GetData(), 0x35, sizeof(TestData));
669853cc4dSMark Jansen }
679853cc4dSMark Jansen
689853cc4dSMark Jansen hr = test2.CopyFrom(test1);
699853cc4dSMark Jansen ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
709853cc4dSMark Jansen ok(test2.GetData() != NULL, "Expected ptr, got %p\n", test2.GetData());
719853cc4dSMark Jansen ok(test2.GetHandle() != NULL, "Expected handle, got %p\n", test2.GetHandle());
729853cc4dSMark Jansen ok(test2.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test2.GetMappingSize());
739853cc4dSMark Jansen
749853cc4dSMark Jansen // test1 is not closed:
759853cc4dSMark Jansen ok(test1.GetData() != NULL, "Expected ptr, got %p\n", test1.GetData());
769853cc4dSMark Jansen ok(test1.GetHandle() != NULL, "Expected handle, got %p\n", test1.GetHandle());
779853cc4dSMark Jansen ok(test1.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test1.GetMappingSize());
789853cc4dSMark Jansen
799853cc4dSMark Jansen // test2 does not equal test1
809853cc4dSMark Jansen ok(test1.GetData() != test2.GetData(), "Expected different ptrs\n");
819853cc4dSMark Jansen ok(test1.GetHandle() != test2.GetHandle(), "Expected different handles\n");
829853cc4dSMark Jansen
839853cc4dSMark Jansen TestData* t1 = test1;
849853cc4dSMark Jansen TestData* t2 = test2;
859853cc4dSMark Jansen if (t1 && t2)
869853cc4dSMark Jansen {
879853cc4dSMark Jansen ok(t1->data[0] == 0x35353535, "Expected 0x35353535, got 0x%x\n", t1->data[0]);
889853cc4dSMark Jansen ok(t2->data[0] == 0x35353535, "Expected 0x35353535, got 0x%x\n", t2->data[0]);
899853cc4dSMark Jansen
909853cc4dSMark Jansen t1->data[0] = 0xbeefbeef;
919853cc4dSMark Jansen ok(t1->data[0] == (int)0xbeefbeef, "Expected 0xbeefbeef, got 0x%x\n", t1->data[0]);
929853cc4dSMark Jansen ok(t2->data[0] == (int)0xbeefbeef, "Expected 0xbeefbeef, got 0x%x\n", t2->data[0]);
939853cc4dSMark Jansen }
949853cc4dSMark Jansen
959853cc4dSMark Jansen hr = test3.OpenMapping(_T("TEST_MAPPING"), sizeof(TestData), offsetof(TestData, data[1]));
969853cc4dSMark Jansen ok(hr == HRESULT_FROM_WIN32(ERROR_MAPPED_ALIGNMENT), "Expected HRESULT_FROM_WIN32(ERROR_MAPPED_ALIGNMENT), got 0x%lx\n", hr);
979853cc4dSMark Jansen ok(test3.GetData() == NULL, "Expected NULL, got %p\n", test3.GetData());
989853cc4dSMark Jansen ok(test3.GetHandle() == NULL, "Expected NULL, got %p\n", test3.GetHandle());
999853cc4dSMark Jansen ok(test3.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test3.GetMappingSize());
1009853cc4dSMark Jansen
1019853cc4dSMark Jansen hr = test2.Unmap();
1029853cc4dSMark Jansen ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
1039853cc4dSMark Jansen ok(test2.GetData() == NULL, "Expected NULL, got %p\n", test2.GetData());
1049853cc4dSMark Jansen ok(test2.GetHandle() == NULL, "Expected NULL, got %p\n", test2.GetHandle());
1059853cc4dSMark Jansen ok(test2.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test2.GetMappingSize());
1069853cc4dSMark Jansen
1079853cc4dSMark Jansen bAlreadyExisted = 123;
1089853cc4dSMark Jansen // Wrong access flag
1099853cc4dSMark Jansen hr = test2.MapSharedMem(sizeof(TestData), _T("TEST_MAPPING"), &bAlreadyExisted, (LPSECURITY_ATTRIBUTES)0, PAGE_EXECUTE_READ, FILE_MAP_ALL_ACCESS);
1109853cc4dSMark Jansen ok(hr == E_ACCESSDENIED, "Expected E_ACCESSDENIED, got 0x%lx\n", hr);
1119853cc4dSMark Jansen ok(test2.GetData() == NULL, "Expected NULL, got %p\n", test2.GetData());
1129853cc4dSMark Jansen ok(test2.GetHandle() == NULL, "Expected NULL, got %p\n", test2.GetHandle());
1139853cc4dSMark Jansen ok(test2.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test2.GetMappingSize());
1149853cc4dSMark Jansen ok(bAlreadyExisted == TRUE, "Expected TRUE, got %u\n", bAlreadyExisted);
1159853cc4dSMark Jansen
1169853cc4dSMark Jansen bAlreadyExisted = 123;
1179853cc4dSMark Jansen hr = test2.MapSharedMem(sizeof(TestData), _T("TEST_MAPPING"), &bAlreadyExisted, (LPSECURITY_ATTRIBUTES)0, PAGE_READWRITE, FILE_MAP_ALL_ACCESS);
1189853cc4dSMark Jansen ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
1199853cc4dSMark Jansen ok(test2.GetData() != NULL, "Expected ptr, got %p\n", test2.GetData());
1209853cc4dSMark Jansen ok(test2.GetHandle() != NULL, "Expected handle, got %p\n", test2.GetHandle());
1219853cc4dSMark Jansen ok(test2.GetMappingSize() == sizeof(TestData), "Expected sizeof(TestData), got %lu\n", test2.GetMappingSize());
1229853cc4dSMark Jansen ok(bAlreadyExisted == TRUE, "Expected TRUE, got %u\n", bAlreadyExisted);
1239853cc4dSMark Jansen
1249853cc4dSMark Jansen // test2 does not equal test1
1259853cc4dSMark Jansen ok(test1.GetData() != test2.GetData(), "Expected different ptrs\n");
1269853cc4dSMark Jansen ok(test1.GetHandle() != test2.GetHandle(), "Expected different handles\n");
1279853cc4dSMark Jansen
1289853cc4dSMark Jansen t2 = test2;
1299853cc4dSMark Jansen if (t1 && t2)
1309853cc4dSMark Jansen {
1319853cc4dSMark Jansen ok(t1->data[0] == (int)0xbeefbeef, "Expected 0xbeefbeef, got 0x%x\n", t1->data[0]);
1329853cc4dSMark Jansen ok(t2->data[0] == (int)0xbeefbeef, "Expected 0xbeefbeef, got 0x%x\n", t2->data[0]);
1339853cc4dSMark Jansen
1349853cc4dSMark Jansen t1->data[0] = 0xdeaddead;
1359853cc4dSMark Jansen ok(t1->data[0] == (int)0xdeaddead, "Expected 0xdeaddead, got 0x%x\n", t1->data[0]);
1369853cc4dSMark Jansen ok(t2->data[0] == (int)0xdeaddead, "Expected 0xdeaddead, got 0x%x\n", t2->data[0]);
1379853cc4dSMark Jansen }
1389853cc4dSMark Jansen }
1399853cc4dSMark Jansen
test_FileMapping()1409853cc4dSMark Jansen static void test_FileMapping()
1419853cc4dSMark Jansen {
1429853cc4dSMark Jansen WCHAR Buf[MAX_PATH] = {0};
1439853cc4dSMark Jansen ULARGE_INTEGER FileSize;
1449853cc4dSMark Jansen
1459853cc4dSMark Jansen GetModuleFileNameW(NULL, Buf, _countof(Buf));
1469853cc4dSMark Jansen
1479853cc4dSMark Jansen CAtlFileMapping<IMAGE_DOS_HEADER> test1, test2;
1489853cc4dSMark Jansen HRESULT hr;
1499853cc4dSMark Jansen
1509853cc4dSMark Jansen {
1519853cc4dSMark Jansen CHandle hFile(CreateFileW(Buf, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL));
1529853cc4dSMark Jansen ok(hFile != INVALID_HANDLE_VALUE, "Could not open %S, aborting test\n", Buf);
1539853cc4dSMark Jansen if (hFile == INVALID_HANDLE_VALUE)
1549853cc4dSMark Jansen return;
1559853cc4dSMark Jansen
1569853cc4dSMark Jansen FileSize.LowPart = GetFileSize(hFile, &FileSize.HighPart);
1579853cc4dSMark Jansen
1589853cc4dSMark Jansen hr = test1.MapFile(hFile, 0, 0, PAGE_READONLY, FILE_MAP_READ);
1599853cc4dSMark Jansen ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
1609853cc4dSMark Jansen ok(test1.GetData() != NULL, "Expected ptr, got %p\n", test1.GetData());
1619853cc4dSMark Jansen ok(test1.GetHandle() != NULL, "Expected handle, got %p\n", test1.GetHandle());
1629853cc4dSMark Jansen ok(test1.GetMappingSize() == FileSize.LowPart, "Expected %lu, got %lu\n", FileSize.LowPart, test1.GetMappingSize());
1639853cc4dSMark Jansen
1649853cc4dSMark Jansen hr = test2.MapFile(hFile, 0, 0x10000, PAGE_READONLY, FILE_MAP_READ);
1659853cc4dSMark Jansen ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
1669853cc4dSMark Jansen ok(test2.GetData() != NULL, "Expected ptr, got %p\n", test2.GetData());
1679853cc4dSMark Jansen ok(test2.GetHandle() != NULL, "Expected handle, got %p\n", test2.GetHandle());
1689853cc4dSMark Jansen // Offset is subtracted
1699853cc4dSMark Jansen ok(test2.GetMappingSize() == FileSize.LowPart - 0x10000, "Expected %lu, got %lu\n", FileSize.LowPart-0x10000, test2.GetMappingSize());
1709853cc4dSMark Jansen
1719853cc4dSMark Jansen hr = test1.Unmap();
1729853cc4dSMark Jansen ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
1739853cc4dSMark Jansen ok(test1.GetData() == NULL, "Expected NULL, got %p\n", test1.GetData());
1749853cc4dSMark Jansen ok(test1.GetHandle() == NULL, "Expected NULL, got %p\n", test1.GetHandle());
1759853cc4dSMark Jansen ok(test1.GetMappingSize() == FileSize.LowPart, "Expected %lu, got %lu\n", FileSize.LowPart, test1.GetMappingSize());
1769853cc4dSMark Jansen
1779853cc4dSMark Jansen hr = test1.MapFile(hFile, 0x1000);
1789853cc4dSMark Jansen ok(hr == S_OK, "Expected S_OK, got 0x%lx\n", hr);
1799853cc4dSMark Jansen ok(test1.GetData() != NULL, "Expected ptr, got %p\n", test1.GetData());
1809853cc4dSMark Jansen ok(test1.GetHandle() != NULL, "Expected handle, got %p\n", test1.GetHandle());
1819853cc4dSMark Jansen ok(test1.GetMappingSize() == 0x1000, "Expected 0x1000, got %lu\n", test1.GetMappingSize());
1829853cc4dSMark Jansen }
1839853cc4dSMark Jansen
1849853cc4dSMark Jansen // We can still access it after the file is closed
1859853cc4dSMark Jansen PIMAGE_DOS_HEADER dos = test1;
1869853cc4dSMark Jansen if (dos)
1879853cc4dSMark Jansen {
1889853cc4dSMark Jansen ok(dos->e_magic == IMAGE_DOS_SIGNATURE, "Expected IMAGE_DOS_SIGNATURE, got 0x%x\n", dos->e_magic);
1899853cc4dSMark Jansen }
1909853cc4dSMark Jansen }
1919853cc4dSMark Jansen
1929853cc4dSMark Jansen
START_TEST(CAtlFileMapping)1939853cc4dSMark Jansen START_TEST(CAtlFileMapping)
1949853cc4dSMark Jansen {
1959853cc4dSMark Jansen test_SharedMem();
1969853cc4dSMark Jansen test_FileMapping();
1979853cc4dSMark Jansen }
198