ReactOS 0.4.15-dev-7842-g558ab78
register.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: apphelp_apitest
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Tests for shim database registration
5 * COPYRIGHT: Copyright 2017-2019 Mark Jansen (mark.jansen@reactos.org)
6 */
7
8#include <ntstatus.h>
9#define WIN32_NO_STATUS
10#include <windef.h>
11#include <ntndk.h>
12#include <atlbase.h>
13#include <strsafe.h>
14#include "wine/test.h"
15
16static const unsigned char rawDB[] =
17{
18 /* Header: Major, Minor, 'sdbf' */
19 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x73, 0x64, 0x62, 0x66,
20
21 /* TAG_DATABASE, Length */
22 0x01, 0x70, 0x22, 0x00, 0x00, 0x00,
23 /* TAG_NAME, Value */
24 0x01, 0x60, 0x06, 0x00, 0x00, 0x00,
25
26 /* TAG_DATABASE_ID, Length, Value*/
27 0x07, 0x90, 0x10, 0x00, 0x00, 0x00,
28 /* offset 30 */
29 0xEB, 0x75, 0xDD, 0x79, 0x98, 0xC0, 0x57, 0x47, 0x99, 0x65, 0x9E, 0x83, 0xC4, 0xCA, 0x9D, 0xA4,
30
31 /* TAG_LIBRARY, Length */
32 0x02, 0x70, 0x00, 0x00, 0x00, 0x00,
33
34 /* TAG_STRINGTABLE, Length */
35 0x01, 0x78, 0x0E, 0x00, 0x00, 0x00,
36 /* TAG_STRINGTABLE_ITEM, Length, Value */
37 0x01, 0x88, 0x08, 0x00, 0x00, 0x00,
38 0x49, 0x00, 0x43, 0x00, 0x53, 0x00, 0x00, 0x00
39};
40
41static BOOL WriteSdbFile(const WCHAR* FileName, const unsigned char* Data, DWORD Size, const GUID* CustomID)
42{
44 DWORD dwWritten;
46
48 {
49 skip("Failed to create temp file %ls, error %u\n", FileName, GetLastError());
50 return FALSE;
51 }
52 Success = WriteFile(Handle, Data, Size, &dwWritten, NULL);
53 ok(Success == TRUE, "WriteFile failed with %u\n", GetLastError());
54 ok(dwWritten == Size, "WriteFile wrote %u bytes instead of %u\n", dwWritten, Size);
55 if (CustomID)
56 {
57 DWORD dwGuidSize;
59 Success = WriteFile(Handle, CustomID, sizeof(*CustomID), &dwGuidSize, NULL);
60 ok(dwGuidSize == sizeof(GUID), "WriteFile wrote %u bytes instead of %u\n", dwGuidSize, sizeof(GUID));
61 }
63 return Success && (dwWritten == Size);
64}
65
66
67
68static const GUID GUID_DATABASE_SHIM = { 0x11111111, 0x1111, 0x1111, { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 } };
69static const GUID GUID_DATABASE_MSI = { 0xd8ff6d16, 0x6a3a, 0x468a, { 0x8b, 0x44, 0x01, 0x71, 0x4d, 0xdc, 0x49, 0xea } };
70static const GUID GUID_DATABASE_DRIVERS = { 0xf9ab2228, 0x3312, 0x4a73, { 0xb6, 0xf9, 0x93, 0x6d, 0x70, 0xe1, 0x12, 0xef } };
71static const GUID TEST_DB_GUID = { 0x79dd75eb, 0xc098, 0x4757, { 0x99, 0x65, 0x9e, 0x83, 0xc4, 0xca, 0x9d, 0xa4 } };
72
73#define SDB_DATABASE_MAIN 0x80000000
74
77BOOL (WINAPI *pSdbUnregisterDatabase)(REFGUID pguidDB);
78
79
80extern "C"
82{
85 PSID AdministratorsGroup;
86
90 0, 0, 0, 0, 0, 0,
91 &AdministratorsGroup);
92 if (Result)
93 {
94 if (!CheckTokenMembership(NULL, AdministratorsGroup, &Result))
95 Result = FALSE;
96 FreeSid(AdministratorsGroup);
97 }
98
99 return Result;
100}
101
102static DWORD g_QueryFlag = 0xffffffff;
103static DWORD QueryFlag(void)
104{
105 if (g_QueryFlag == 0xffffffff)
106 {
107 ULONG_PTR wow64_ptr = 0;
109 g_QueryFlag = (NT_SUCCESS(status) && wow64_ptr != 0) ? KEY_WOW64_64KEY : 0;
110 }
111 return g_QueryFlag;
112}
113
115{
116 FILETIME TimeBuffer;
117
118 GetSystemTimeAsFileTime(&TimeBuffer);
119 Result.HighPart = TimeBuffer.dwHighDateTime;
120 Result.LowPart = TimeBuffer.dwLowDateTime;
121}
122
124{
126 WCHAR StringBuffer[200];
127 DWORD ValueBuffer;
128 ULARGE_INTEGER LargeUIntBuffer;
129
130 CRegKey key;
131 LSTATUS Status = key.Open(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\InstalledSDB", KEY_READ | QueryFlag());
132 winetest_ok(!Status, "Unable to open InstalledSDB key\n");
133 if (Status)
134 return;
135
137 {
138 ok(0, "Unable to format guid\n");
139 return;
140 }
141
142 Status = key.Open(key.m_hKey, GuidString.Buffer, KEY_READ);
143 winetest_ok(!Status, "Unable to open %s key (0x%x)\n", wine_dbgstr_w(GuidString.Buffer), Status);
145 if (Status)
146 return;
147
148 ULONG nChars = _countof(StringBuffer);
149 Status = key.QueryStringValue(L"DatabaseDescription", StringBuffer, &nChars);
150 winetest_ok(!Status, "Unable to read DatabaseDescription (0x%x)\n", Status);
151 if (!Status)
152 winetest_ok(!wcscmp(DisplayName, StringBuffer), "Expected DatabaseDescription to be %s, was %s\n", wine_dbgstr_w(DisplayName), wine_dbgstr_w(StringBuffer));
153
154 nChars = _countof(StringBuffer);
155 Status = key.QueryStringValue(L"DatabasePath", StringBuffer, &nChars);
156 winetest_ok(!Status, "Unable to read DatabasePath (0x%x)\n", Status);
157 if (!Status)
158 winetest_ok(!wcscmp(Path, StringBuffer), "Expected DatabasePath to be %s, was %s\n", wine_dbgstr_w(Path), wine_dbgstr_w(StringBuffer));
159
160 Status = key.QueryDWORDValue(L"DatabaseType", ValueBuffer);
161 winetest_ok(!Status, "Unable to read DatabaseType (0x%x)\n", Status);
162 if (!Status)
163 winetest_ok(ValueBuffer == Type, "Expected DatabaseType to be 0x%x, was 0x%x\n", Type, ValueBuffer);
164
165 Status = key.QueryQWORDValue(L"DatabaseInstallTimeStamp", LargeUIntBuffer.QuadPart);
166 winetest_ok(!Status, "Unable to read DatabaseInstallTimeStamp (0x%x)\n", Status);
167 if (!Status)
168 {
169 if (TimeStamp)
170 {
171 winetest_ok(LargeUIntBuffer.QuadPart == *TimeStamp, "Expected DatabaseInstallTimeStamp to be %s, was %s\n",
173 }
174 else
175 {
176 ULARGE_INTEGER CurrentTime;
177 FileTimeNow(CurrentTime);
178 ULONG DiffMS = (ULONG)((CurrentTime.QuadPart - LargeUIntBuffer.QuadPart) / 10000);
179 winetest_ok(DiffMS < 5000 , "Expected DatabaseInstallTimeStamp to be less than 5 seconds before now (was: %u)\n", DiffMS);
180 }
181 }
182}
183
184
185#define ok_keys (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_keys_
186
187
188START_TEST(register)
189{
190 WCHAR TempPath[MAX_PATH * 2];
193
194 SetEnvironmentVariableA("SHIM_DEBUG_LEVEL", "4");
195 SetEnvironmentVariableA("SHIMENG_DEBUG_LEVEL", "4");
196 SetEnvironmentVariableA("DEBUGCHANNEL", "+apphelp");
197
198 //silence_debug_output();
199 hdll = LoadLibraryA("apphelp.dll");
200
201 *(void**)&pSdbRegisterDatabase = (void*)GetProcAddress(hdll, "SdbRegisterDatabase");
202 *(void**)&pSdbRegisterDatabaseEx = (void*)GetProcAddress(hdll, "SdbRegisterDatabaseEx");
203 *(void**)&pSdbUnregisterDatabase = (void*)GetProcAddress(hdll, "SdbUnregisterDatabase");
204
205 if (!pSdbRegisterDatabase || !pSdbRegisterDatabaseEx || !pSdbUnregisterDatabase)
206 {
207 skip("Not all functions present: %p, %p, %p\n", pSdbRegisterDatabase, pSdbRegisterDatabaseEx, pSdbUnregisterDatabase);
208 return;
209 }
210
211 /* [Err ][SdbUnregisterDatabase] Failed to open key "\Registry\Machine\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\InstalledSDB\{11111111-1111-1111-1111-111111111111}" Status 0xc0000034 */
212 ok_int(pSdbUnregisterDatabase(GUID_DATABASE_SHIM), FALSE);
213 ok_int(pSdbUnregisterDatabase(GUID_DATABASE_MSI), FALSE);
214 ok_int(pSdbUnregisterDatabase(GUID_DATABASE_DRIVERS), FALSE);
215
216
217 if (!IsUserAdmin())
218 {
219 skip("Not running as admin, unable to install databases!\n");
220 return;
221 }
222
223 GetTempPathW(_countof(TempPath), TempPath);
224 StringCchCatW(TempPath, _countof(TempPath), L"\\shim_db.sdb");
225 if (!WriteSdbFile(TempPath, rawDB, sizeof(rawDB), NULL))
226 {
227 skip("Cannot write %s\n", wine_dbgstr_w(TempPath));
228 return;
229 }
230
231 /* No Type */
232 Success = pSdbRegisterDatabase(TempPath, 0);
234 if (Success)
235 {
236 ok_keys(TEST_DB_GUID, L"ICS", TempPath, 0, NULL);
237 Success = pSdbUnregisterDatabase(TEST_DB_GUID);
239 }
240
241 /* Unknown type */
242 Success = pSdbRegisterDatabase(TempPath, 1);
244 if (Success)
245 {
246 ok_keys(TEST_DB_GUID, L"ICS", TempPath, 1, NULL);
247 Success = pSdbUnregisterDatabase(TEST_DB_GUID);
249 }
250
251 /* System type */
252 Success = pSdbRegisterDatabase(TempPath, SDB_DATABASE_MAIN);
254 if (Success)
255 {
256 ok_keys(TEST_DB_GUID, L"ICS", TempPath, SDB_DATABASE_MAIN, NULL);
257 Success = pSdbUnregisterDatabase(TEST_DB_GUID);
259 }
260
261 /* No type, null time */
262 Success = pSdbRegisterDatabaseEx(TempPath, 0, NULL);
264 if (Success)
265 {
266 ok_keys(TEST_DB_GUID, L"ICS", TempPath, 0, NULL);
267 Success = pSdbUnregisterDatabase(TEST_DB_GUID);
269 }
270
271 /* Unknown type, null time */
272 Success = pSdbRegisterDatabaseEx(TempPath, 1, NULL);
274 if (Success)
275 {
276 ok_keys(TEST_DB_GUID, L"ICS", TempPath, 1, NULL);
277 Success = pSdbUnregisterDatabase(TEST_DB_GUID);
279 }
280
281
282 /* System type, null time */
283 Success = pSdbRegisterDatabaseEx(TempPath, SDB_DATABASE_MAIN, NULL);
285 if (Success)
286 {
287 ok_keys(TEST_DB_GUID, L"ICS", TempPath, SDB_DATABASE_MAIN, NULL);
288 Success = pSdbUnregisterDatabase(TEST_DB_GUID);
290 }
291
294 Time.QuadPart ^= 0xffffffffffffffffll;
295 /* No type, random time */
296 Success = pSdbRegisterDatabaseEx(TempPath, 0, &Time.QuadPart);
298 if (Success)
299 {
300 ok_keys(TEST_DB_GUID, L"ICS", TempPath, 0, &Time.QuadPart);
301 Success = pSdbUnregisterDatabase(TEST_DB_GUID);
303 }
304
305 /* Unknown type, random time */
306 Success = pSdbRegisterDatabaseEx(TempPath, 1, &Time.QuadPart);
308 if (Success)
309 {
310 ok_keys(TEST_DB_GUID, L"ICS", TempPath, 1, &Time.QuadPart);
311 Success = pSdbUnregisterDatabase(TEST_DB_GUID);
313 }
314
315 /* System type, random time */
316 Success = pSdbRegisterDatabaseEx(TempPath, SDB_DATABASE_MAIN, &Time.QuadPart);
318 if (Success)
319 {
321 Success = pSdbUnregisterDatabase(TEST_DB_GUID);
323 }
324
325 /* System reserved ID's */
326 if (!WriteSdbFile(TempPath, rawDB, sizeof(rawDB), &GUID_DATABASE_SHIM))
327 {
328 skip("Cannot write %s\n", wine_dbgstr_w(TempPath));
329 DeleteFileW(TempPath);
330 return;
331 }
332
333 Success = pSdbRegisterDatabase(TempPath, 0);
335 if (Success)
336 {
337 ok_keys(GUID_DATABASE_SHIM, L"ICS", TempPath, 0, NULL);
338 Success = pSdbUnregisterDatabase(GUID_DATABASE_SHIM);
340 }
341
342 if (!WriteSdbFile(TempPath, rawDB, sizeof(rawDB), &GUID_DATABASE_MSI))
343 {
344 skip("Cannot write %s\n", wine_dbgstr_w(TempPath));
345 DeleteFileW(TempPath);
346 return;
347 }
348
349 Success = pSdbRegisterDatabase(TempPath, 0);
351 if (Success)
352 {
353 ok_keys(GUID_DATABASE_MSI, L"ICS", TempPath, 0, NULL);
354 Success = pSdbUnregisterDatabase(GUID_DATABASE_MSI);
356 }
357
358 if (!WriteSdbFile(TempPath, rawDB, sizeof(rawDB), &GUID_DATABASE_DRIVERS))
359 {
360 skip("Cannot write %s\n", wine_dbgstr_w(TempPath));
361 DeleteFileW(TempPath);
362 return;
363 }
364
365 Success = pSdbRegisterDatabase(TempPath, 0);
367 if (Success)
368 {
369 ok_keys(GUID_DATABASE_DRIVERS, L"ICS", TempPath, 0, NULL);
370 Success = pSdbUnregisterDatabase(GUID_DATABASE_DRIVERS);
372 }
373
374 DeleteFileW(TempPath);
375}
PRTL_UNICODE_STRING_BUFFER Path
Type
Definition: Type.h:7
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
#define ok_int(expression, result)
Definition: atltest.h:134
LONG NTSTATUS
Definition: precomp.h:26
static SID_IDENTIFIER_AUTHORITY NtAuthority
Definition: security.c:40
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
BOOL WINAPI CheckTokenMembership(IN HANDLE ExistingTokenHandle, IN PSID SidToCheck, OUT PBOOL IsMember)
Definition: token.c:21
BOOL WINAPI AllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, BYTE nSubAuthorityCount, DWORD nSubAuthority0, DWORD nSubAuthority1, DWORD nSubAuthority2, DWORD nSubAuthority3, DWORD nSubAuthority4, DWORD nSubAuthority5, DWORD nSubAuthority6, DWORD nSubAuthority7, PSID *pSid)
Definition: security.c:674
PVOID WINAPI FreeSid(PSID pSid)
Definition: security.c:698
#define CloseHandle
Definition: compat.h:739
#define FILE_BEGIN
Definition: compat.h:761
#define SetFilePointer
Definition: compat.h:743
#define GetProcAddress(x, y)
Definition: compat.h:753
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:49
#define MAX_PATH
Definition: compat.h:34
#define CreateFileW
Definition: compat.h:741
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
BOOL WINAPI DECLSPEC_HOTPATCH SetEnvironmentVariableA(IN LPCSTR lpName, IN LPCSTR lpValue)
Definition: environ.c:218
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
DWORD WINAPI GetTempPathW(IN DWORD count, OUT LPWSTR path)
Definition: path.c:2080
VOID WINAPI GetSystemTimeAsFileTime(OUT PFILETIME lpFileTime)
Definition: time.c:128
@ Success
Definition: eventcreate.c:712
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
ULONG Handle
Definition: gdb_input.c:15
Status
Definition: gdiplustypes.h:25
@ ProcessWow64Information
Definition: winternl.h:396
NTSYSAPI NTSTATUS WINAPI RtlStringFromGUID(REFGUID, PUNICODE_STRING)
#define SUCCEEDED(hr)
Definition: intsafe.h:50
#define wine_dbgstr_w
Definition: kernel32.h:34
WCHAR StringBuffer[156]
Definition: ldrinit.c:41
#define CREATE_ALWAYS
Definition: disk.h:72
static PWSTR GuidString
Definition: apphelp.c:93
LPCWSTR pszDatabasePath
Definition: env.c:38
static PLARGE_INTEGER Time
Definition: time.c:105
#define BOOL
Definition: nt_native.h:43
#define KEY_READ
Definition: nt_native.h:1023
#define NtCurrentProcess()
Definition: nt_native.h:1657
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
#define GENERIC_WRITE
Definition: nt_native.h:90
__GNU_EXTENSION typedef unsigned __int64 * PULONGLONG
Definition: ntbasedef.h:383
NTSTATUS NTAPI NtQueryInformationProcess(_In_ HANDLE ProcessHandle, _In_ PROCESSINFOCLASS ProcessInformationClass, _Out_ PVOID ProcessInformation, _In_ ULONG ProcessInformationLength, _Out_opt_ PULONG ReturnLength)
Definition: query.c:59
#define L(x)
Definition: ntvdm.h:50
BOOL IsUserAdmin(VOID)
Definition: register.cpp:81
static DWORD QueryFlag(void)
Definition: register.cpp:103
#define ok_keys
Definition: register.cpp:185
DWORD dwDatabaseType
Definition: register.cpp:75
DWORD const PULONGLONG pTimeStamp
Definition: register.cpp:76
static void FileTimeNow(ULARGE_INTEGER &Result)
Definition: register.cpp:114
static const GUID TEST_DB_GUID
Definition: register.cpp:71
static const GUID GUID_DATABASE_DRIVERS
Definition: register.cpp:70
#define SDB_DATABASE_MAIN
Definition: register.cpp:73
static void ok_keys_(REFGUID Guid, LPCWSTR DisplayName, LPCWSTR Path, DWORD Type, PULONGLONG TimeStamp)
Definition: register.cpp:123
static const GUID GUID_DATABASE_MSI
Definition: register.cpp:69
static const unsigned char rawDB[]
Definition: register.cpp:16
static DWORD g_QueryFlag
Definition: register.cpp:102
static BOOL WriteSdbFile(const WCHAR *FileName, const unsigned char *Data, DWORD Size, const GUID *CustomID)
Definition: register.cpp:41
static const GUID GUID_DATABASE_SHIM
Definition: register.cpp:68
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
void __winetest_cdecl winetest_ok(int condition, const char *msg,...)
static PVOID hdll
Definition: shimdbg.c:126
#define _countof(array)
Definition: sndvol32.h:68
STRSAFEAPI StringCchCatW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:325
DWORD dwHighDateTime
Definition: mapidefs.h:66
DWORD dwLowDateTime
Definition: mapidefs.h:65
Definition: scsiwmi.h:51
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
Definition: copy.c:22
Definition: ps.c:97
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
LONGLONG QuadPart
Definition: typedefs.h:114
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
_Must_inspect_result_ _In_ WDFOBJECT _In_ CONST GUID * Guid
Definition: wdfobject.h:762
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WINAPI
Definition: msvc.h:6
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
#define KEY_WOW64_64KEY
Definition: cmtypes.h:46
#define SECURITY_BUILTIN_DOMAIN_RID
Definition: setypes.h:581
#define SECURITY_NT_AUTHORITY
Definition: setypes.h:554
#define DOMAIN_ALIAS_RID_ADMINS
Definition: setypes.h:652
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185