ReactOS 0.4.15-dev-7711-g5627da4
NtApphelpCacheControl.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: LGPL - See COPYING.LIB in the top level directory
4 * PURPOSE: Tests for SHIM engine caching.
5 * PROGRAMMER: Mark Jansen
6 */
7
8#include "precomp.h"
9
10#include <winsvc.h>
11#include <versionhelpers.h>
12
14{
17};
18
20
22{
23 APPHELP_CACHE_SERVICE_LOOKUP CacheEntry = { {0} };
25 CacheEntry.ImageName = *PathName;
26 if (WithMapping)
27 {
28 OBJECT_ATTRIBUTES LocalObjectAttributes;
30 InitializeObjectAttributes(&LocalObjectAttributes, PathName,
32 Status = NtOpenFile(&CacheEntry.ImageHandle,
34 &LocalObjectAttributes, &IoStatusBlock,
38 }
39 else
40 {
42 }
43 Status = pNtApphelpCacheControl(Service, &CacheEntry);
44 if (CacheEntry.ImageHandle != INVALID_HANDLE_VALUE)
45 NtClose(CacheEntry.ImageHandle);
46 return Status;
47}
48
49int InitEnv(UNICODE_STRING* PathName)
50{
53 {
54 /* Windows Vista+ has a different layout for APPHELP_CACHE_SERVICE_LOOKUP */
55 return 0;
56 }
58 "Wrong value for Status, expected: SUCCESS or NOT_FOUND, got: 0x%lx\n",
59 Status);
60 return 1;
61}
62
64{
65 APPHELP_CACHE_SERVICE_LOOKUP CacheEntry = { {0} };
67
68 /* Validate the handling of a NULL pointer */
69 Status = pNtApphelpCacheControl(ApphelpCacheServiceRemove, NULL);
71 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, NULL);
73
74 /* Validate the handling of a NULL pointer inside the struct */
75 Status = pNtApphelpCacheControl(ApphelpCacheServiceRemove, &CacheEntry);
77 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, &CacheEntry);
79
80 /* Just call the dump function */
81 Status = pNtApphelpCacheControl(ApphelpCacheServiceDump, NULL);
83
84 /* Validate the handling of an invalid handle inside the struct */
85 CacheEntry.ImageName = *PathName;
86 CacheEntry.ImageHandle = (HANDLE)2;
87 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, &CacheEntry);
89
90 /* Validate the handling of an invalid service number */
91 Status = pNtApphelpCacheControl(999, NULL);
93 Status = pNtApphelpCacheControl(999, &CacheEntry);
95}
96
97static BOOLEAN RequestAddition(SC_HANDLE service_handle, BOOLEAN WithMapping)
98{
102 /* TODO: how to get a return code from the service? */
103 return TRUE;
104}
105
107{
109 UNICODE_STRING ntPath;
113
114 GetModuleFileNameW(NULL, szPath, sizeof(szPath) / sizeof(szPath[0]));
116 ok(Result == TRUE, "RtlDosPathNameToNtPathName_U\n");
117 if (!InitEnv(&ntPath))
118 {
119 skip("NtApphelpCacheControl expects a different structure layout\n");
120 return;
121 }
122 /* At this point we have made sure that our binary is not present in the cache,
123 and that the NtApphelpCacheControl function expects the struct layout we use. */
124 CheckValidation(&ntPath);
125
126 /* We expect not to find it */
131
132 /* First we add our process without a file handle (so it will be registered without file info) */
134
135 /* now we try to find it without validating file info */
138 /* when validating file info the cache notices the file is wrong, so it is dropped from the cache */
141 /* making the second check without info also fail. */
144
145
146 /* Now we add the file with file info */
148
149 /* so both checks should succeed */
154
155 /* We know the file is in the cache now (assuming previous tests succeeded,
156 let's test invalid handle behavior */
157 CacheEntry.ImageName = ntPath;
158 CacheEntry.ImageHandle = 0;
159 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, &CacheEntry);
161
162 /* re-add it for the next test */
166 CacheEntry.ImageHandle = (HANDLE)1;
167 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, &CacheEntry);
169
170 /* and again */
174#ifdef _WIN64
175 CacheEntry.ImageHandle = (HANDLE)0x8000000000000000ULL;
176#else
177 CacheEntry.ImageHandle = (HANDLE)0x80000000;
178#endif
179 Status = pNtApphelpCacheControl(ApphelpCacheServiceLookup, &CacheEntry);
181
182 RtlFreeHeap(RtlGetProcessHeap(), 0, ntPath.Buffer);
183}
184
185
186/* Most service related code was taken from services_winetest:service and modified for usage here
187 The rest came from MSDN */
188
189static SERVICE_STATUS_HANDLE (WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR,LPHANDLER_FUNCTION_EX,LPVOID);
190static char service_name[100] = "apphelp_test_service";
193
195{
197 UNICODE_STRING ntPath;
200 GetModuleFileNameW(NULL, szPath, sizeof(szPath) / sizeof(szPath[0]));
202 if (!Result)
203 {
204 DbgPrint("RegisterInShimCache: RtlDosPathNameToNtPathName_U failed\n");
205 return FALSE;
206 }
207
208 Status = CallCacheControl(&ntPath, WithMapping, ApphelpCacheServiceUpdate);
209 if (!NT_SUCCESS(Status))
210 {
211 DbgPrint("RegisterInShimCache: CallCacheControl failed\n");
212 RtlFreeHeap(RtlGetProcessHeap(), 0, ntPath.Buffer);
213 return FALSE;
214 }
215 RtlFreeHeap(RtlGetProcessHeap(), 0, ntPath.Buffer);
216 return TRUE;
217}
218
219
220static DWORD WINAPI service_handler(DWORD ctrl, DWORD event_type, void *event_data, void *context)
221{
223 status.dwServiceType = SERVICE_WIN32;
224 status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
225
226 switch(ctrl)
227 {
230 status.dwCurrentState = SERVICE_STOP_PENDING;
231 status.dwControlsAccepted = 0;
234 return NO_ERROR;
237 {
238 /* TODO: how should we communicate a failure? */
239 }
240 break;
243 {
244 /* TODO: how should we communicate a failure? */
245 }
246 break;
247 default:
248 DbgPrint("Unhandled: %d\n", ctrl);
249 break;
250 }
251 status.dwCurrentState = SERVICE_RUNNING;
253 return NO_ERROR;
254}
255
256static void WINAPI service_main(DWORD argc, char **argv)
257{
259 service_status = pRegisterServiceCtrlHandlerExA(service_name, service_handler, NULL);
260 if(!service_status)
261 return;
262
263 status.dwServiceType = SERVICE_WIN32;
264 status.dwCurrentState = SERVICE_RUNNING;
267
269
270 status.dwCurrentState = SERVICE_STOPPED;
271 status.dwControlsAccepted = 0;
273}
274
275static SC_HANDLE InstallService(SC_HANDLE scm_handle)
276{
277 char service_cmd[MAX_PATH+150], *ptr;
278 SC_HANDLE service;
279
280 ptr = service_cmd + GetModuleFileNameA(NULL, service_cmd, MAX_PATH);
281 strcpy(ptr, " NtApphelpCacheControl service");
282 ptr += strlen(ptr);
283
286 service_cmd, NULL, NULL, NULL, NULL, NULL);
287 if (!service)
288 {
289 skip("Could not create helper service\n");
290 return NULL;
291 }
292 return service;
293}
294
296{
297 DWORD dwBytesNeeded;
298 DWORD dwStartTime = GetTickCount();
299 while (ssp->dwCurrentState != Status)
300 {
301 Sleep(40);
303 (LPBYTE)ssp, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded ))
304 {
305 ok(0, "QueryServiceStatusEx failed waiting for %lu\n", Status);
306 break;
307 }
308 if ((GetTickCount() - dwStartTime) > 1000)
309 {
310 ok(0, "Timeout waiting for (%lu) from service, is: %lu.\n",
311 Status, ssp->dwCurrentState);
312 break;
313 }
314 }
315}
316
317static void RunTest()
318{
321 if (service_handle)
322 {
323 SERVICE_STATUS_PROCESS ssp = {0};
325 if (res)
326 {
331 }
332 else
333 {
334 skip("Could not start helper service\n");
335 }
337 }
339}
340
342{
343 char **argv;
344 int argc;
345
346 pRegisterServiceCtrlHandlerExA = (void*)GetProcAddress(GetModuleHandleA("advapi32.dll"), "RegisterServiceCtrlHandlerExA");
347 if (!pRegisterServiceCtrlHandlerExA)
348 {
349 win_skip("RegisterServiceCtrlHandlerExA not available, skipping tests\n");
350 return;
351 }
352
353 pNtApphelpCacheControl = (void*)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtApphelpCacheControl");
354 if (!pNtApphelpCacheControl)
355 {
356 win_skip("NtApphelpCacheControl not available, skipping tests\n");
357 return;
358 }
359
361 if(argc < 3)
362 {
363 RunTest();
364 }
365 else
366 {
367 SERVICE_TABLE_ENTRYA servtbl[] = {
369 {NULL, NULL}
370 };
373 Sleep(50);
375 }
376}
377
378
static void WINAPI service_main(DWORD argc, char **argv)
static void WaitService(SC_HANDLE service_handle, DWORD Status, SERVICE_STATUS_PROCESS *ssp)
@ RegisterShimCacheWithHandle
@ RegisterShimCacheWithoutHandle
static void RunApphelpCacheControlTests(SC_HANDLE service_handle)
static SC_HANDLE InstallService(SC_HANDLE scm_handle)
static PAPPHELP_CACHE_SERVICE_LOOKUP
static DWORD WINAPI service_handler(DWORD ctrl, DWORD event_type, void *event_data, void *context)
NTSTATUS CallCacheControl(UNICODE_STRING *PathName, BOOLEAN WithMapping, APPHELPCACHESERVICECLASS Service)
static HANDLE service_stop_event
static LPVOID
static SERVICE_STATUS_HANDLE(WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR
static void RunTest()
static BOOLEAN RequestAddition(SC_HANDLE service_handle, BOOLEAN WithMapping)
static LPHANDLER_FUNCTION_EX
static SERVICE_STATUS_HANDLE service_status
int InitEnv(UNICODE_STRING *PathName)
static BOOLEAN RegisterInShimCache(BOOLEAN WithMapping)
void CheckValidation(UNICODE_STRING *PathName)
static char service_name[100]
unsigned char BOOLEAN
static SC_HANDLE scm_handle
Definition: ServiceArgs.c:20
static int argc
Definition: ServiceArgs.c:12
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
LONG NTSTATUS
Definition: precomp.h:26
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
#define NO_ERROR
Definition: dderror.h:5
#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
#define NTSTATUS
Definition: precomp.h:21
#define CloseHandle
Definition: compat.h:739
#define GetProcAddress(x, y)
Definition: compat.h:753
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MAX_PATH
Definition: compat.h:34
#define FILE_SHARE_READ
Definition: compat.h:136
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:600
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
DWORD WINAPI GetModuleFileNameA(HINSTANCE hModule, LPSTR lpFilename, DWORD nSize)
Definition: loader.c:539
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:455
#define INFINITE
Definition: serial.h:102
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
Status
Definition: gdiplustypes.h:25
GLuint res
Definition: glext.h:9613
#define DbgPrint
Definition: hal.h:12
enum _APPHELPCACHESERVICECLASS APPHELPCACHESERVICECLASS
@ ApphelpCacheServiceLookup
Definition: pstypes.h:973
@ ApphelpCacheServiceRemove
Definition: pstypes.h:974
@ ApphelpCacheServiceUpdate
Definition: pstypes.h:975
@ ApphelpCacheServiceDump
Definition: pstypes.h:977
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
LPCWSTR szPath
Definition: env.c:37
static PVOID ptr
Definition: dispmode.c:27
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define ctrl
Definition: input.c:1756
#define argv
Definition: mplay32.c:18
NTSYSAPI BOOLEAN NTAPI RtlDosPathNameToNtPathName_U(_In_opt_z_ PCWSTR DosPathName, _Out_ PUNICODE_STRING NtPathName, _Out_opt_ PCWSTR *NtFileNamePart, _Out_opt_ PRTL_RELATIVE_NAME_U DirectoryInfo)
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3952
#define SYNCHRONIZE
Definition: nt_native.h:61
#define FILE_READ_DATA
Definition: nt_native.h:628
#define FILE_READ_ATTRIBUTES
Definition: nt_native.h:647
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
#define FILE_EXECUTE
Definition: nt_native.h:642
#define GENERIC_ALL
Definition: nt_native.h:92
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
NTSTATUS NTAPI NtApphelpCacheControl(_In_ APPHELPCACHESERVICECLASS Service, _In_opt_ PAPPHELP_CACHE_SERVICE_LOOKUP ServiceData)
Definition: apphelp.c:728
@ Service
Definition: ntsecapi.h:292
static SERVICE_STATUS_HANDLE service_handle
Definition: rpcss_main.c:38
BOOL WINAPI DeleteService(SC_HANDLE hService)
Definition: scm.c:921
BOOL WINAPI StartServiceA(SC_HANDLE hService, DWORD dwNumServiceArgs, LPCSTR *lpServiceArgVectors)
Definition: scm.c:2942
BOOL WINAPI ControlService(SC_HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)
Definition: scm.c:622
BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2887
SC_HANDLE WINAPI CreateServiceA(SC_HANDLE hSCManager, LPCSTR lpServiceName, LPCSTR lpDisplayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCSTR lpBinaryPathName, LPCSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCSTR lpDependencies, LPCSTR lpServiceStartName, LPCSTR lpPassword)
Definition: scm.c:680
SC_HANDLE WINAPI OpenSCManagerA(LPCSTR lpMachineName, LPCSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2024
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:580
BOOL WINAPI StartServiceCtrlDispatcherA(const SERVICE_TABLE_ENTRYA *lpServiceStartTable)
Definition: sctrl.c:1035
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:997
#define win_skip
Definition: test.h:160
int winetest_get_mainargs(char ***pargv)
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_NOT_FOUND
Definition: shellext.h:72
UNICODE_STRING ImageName
Definition: pstypes.h:986
Definition: http.c:7252
Definition: ps.c:97
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventA(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCSTR lpName OPTIONAL)
Definition: synch.c:637
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
unsigned char * LPBYTE
Definition: typedefs.h:53
#define NTAPI
Definition: typedefs.h:36
PVOID HANDLE
Definition: typedefs.h:73
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
VERSIONHELPERAPI IsWindows7OrGreater()
#define WINAPI
Definition: msvc.h:6
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
@ SC_STATUS_PROCESS_INFO
Definition: winsvc.h:119
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
#define SC_MANAGER_ALL_ACCESS
Definition: winsvc.h:13
_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 SERVICE_DEMAND_START
Definition: cmtypes.h:978
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:962
#define SERVICE_ERROR_IGNORE
Definition: cmtypes.h:981
#define SERVICE_WIN32
Definition: cmtypes.h:964
const char * LPCSTR
Definition: xmlstorage.h:183
__wchar_t WCHAR
Definition: xmlstorage.h:180