ReactOS  0.4.14-dev-1314-gacf135d
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  {
41  CacheEntry.ImageHandle = INVALID_HANDLE_VALUE;
42  }
43  Status = pNtApphelpCacheControl(Service, &CacheEntry);
44  if (CacheEntry.ImageHandle != INVALID_HANDLE_VALUE)
45  NtClose(CacheEntry.ImageHandle);
46  return Status;
47 }
48 
49 int 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 
97 static 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;
110  BOOLEAN Result;
112  APPHELP_CACHE_SERVICE_LOOKUP CacheEntry;
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 
189 static SERVICE_STATUS_HANDLE (WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR,LPHANDLER_FUNCTION_EX,LPVOID);
190 static char service_name[100] = "apphelp_test_service";
193 
195 {
197  UNICODE_STRING ntPath;
198  BOOLEAN Result;
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 
220 static DWORD WINAPI service_handler(DWORD ctrl, DWORD event_type, void *event_data, void *context)
221 {
222  SERVICE_STATUS status = {0};
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 
256 static void WINAPI service_main(DWORD argc, char **argv)
257 {
258  SERVICE_STATUS status = {0};
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;
265  status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
267 
269 
270  status.dwCurrentState = SERVICE_STOPPED;
271  status.dwControlsAccepted = 0;
273 }
274 
275 static 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 
317 static 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 LPVOID
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:600
static DWORD WINAPI service_handler(DWORD ctrl, DWORD event_type, void *event_data, void *context)
static int argc
Definition: ServiceArgs.c:12
#define SERVICE_ERROR_IGNORE
Definition: cmtypes.h:979
#define GENERIC_ALL
Definition: nt_native.h:92
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:406
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
static PAPPHELP_CACHE_SERVICE_LOOKUP
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define DbgPrint
Definition: loader.c:25
Definition: http.c:7094
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
static SC_HANDLE scm_handle
Definition: ServiceArgs.c:20
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
static SERVICE_STATUS_HANDLE service_status
int InitEnv(UNICODE_STRING *PathName)
NTSTATUS CallCacheControl(UNICODE_STRING *PathName, BOOLEAN WithMapping, APPHELPCACHESERVICECLASS Service)
LONG NTSTATUS
Definition: precomp.h:26
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
static LPHANDLER_FUNCTION_EX
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:455
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
static HANDLE service_stop_event
static SERVICE_STATUS_HANDLE service_handle
Definition: rpcss_main.c:38
BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2830
BOOL WINAPI DeleteService(SC_HANDLE hService)
Definition: scm.c:916
#define argv
Definition: mplay32.c:18
#define NO_ERROR
Definition: dderror.h:5
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:677
BOOL WINAPI StartServiceCtrlDispatcherA(const SERVICE_TABLE_ENTRYA *lpServiceStartTable)
Definition: sctrl.c:1033
DWORD WINAPI GetModuleFileNameA(HINSTANCE hModule, LPSTR lpFilename, DWORD nSize)
Definition: loader.c:539
#define FILE_SHARE_READ
Definition: compat.h:125
static void WINAPI service_main(DWORD argc, char **argv)
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:995
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define SERVICE_STOPPED
Definition: winsvc.h:21
unsigned char * LPBYTE
Definition: typedefs.h:53
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
SC_HANDLE WINAPI OpenSCManagerA(LPCSTR lpMachineName, LPCSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:1967
unsigned int BOOL
Definition: ntddk_ex.h:94
static BOOLEAN RegisterInShimCache(BOOLEAN WithMapping)
#define FILE_READ_DATA
Definition: nt_native.h:628
#define SERVICE_RUNNING
Definition: winsvc.h:24
static PVOID ptr
Definition: dispmode.c:27
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:577
static NTSTATUS(NTAPI *pNtApphelpCacheControl)(APPHELPCACHESERVICECLASS
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
static void RunTest()
_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:426
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventA(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCSTR lpName OPTIONAL)
Definition: synch.c:637
const char * LPCSTR
Definition: xmlstorage.h:183
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:960
#define ctrl
Definition: input.c:1757
BOOL WINAPI StartServiceA(SC_HANDLE hService, DWORD dwNumServiceArgs, LPCSTR *lpServiceArgVectors)
Definition: scm.c:2885
static SERVICE_STATUS_HANDLE(WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR
UNICODE_STRING ImageName
Definition: pstypes.h:934
START_TEST(NtApphelpCacheControl)
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:3951
#define STATUS_NOT_FOUND
Definition: shellext.h:72
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:26
#define WINAPI
Definition: msvc.h:6
static char service_name[100]
NTSTATUS NTAPI NtApphelpCacheControl(_In_ APPHELPCACHESERVICECLASS Service, _In_opt_ PAPPHELP_CACHE_SERVICE_LOOKUP ServiceData)
Definition: apphelp.c:729
unsigned long DWORD
Definition: ntddk_ex.h:95
PVOID HANDLE
Definition: typedefs.h:72
int winetest_get_mainargs(char ***pargv)
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#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 FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
#define SYNCHRONIZE
Definition: nt_native.h:61
Status
Definition: gdiplustypes.h:24
static void WaitService(SC_HANDLE service_handle, DWORD Status, SERVICE_STATUS_PROCESS *ssp)
LPCWSTR szPath
Definition: env.c:35
static void RunApphelpCacheControlTests(SC_HANDLE service_handle)
#define ok(value,...)
Definition: atltest.h:57
#define SERVICE_WIN32
Definition: cmtypes.h:962
enum _APPHELPCACHESERVICECLASS APPHELPCACHESERVICECLASS
static SC_HANDLE InstallService(SC_HANDLE scm_handle)
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
BOOL WINAPI ControlService(SC_HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)
Definition: scm.c:619
#define SC_MANAGER_ALL_ACCESS
Definition: winsvc.h:13
#define skip(...)
Definition: atltest.h:64
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
VERSIONHELPERAPI IsWindows7OrGreater()
static BOOLEAN RequestAddition(SC_HANDLE service_handle, BOOLEAN WithMapping)
#define ok_ntstatus(status, expected)
Definition: atltest.h:135
GLuint res
Definition: glext.h:9613
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define GetProcAddress(x, y)
Definition: compat.h:418
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
#define SERVICE_DEMAND_START
Definition: cmtypes.h:976
#define INFINITE
Definition: serial.h:102
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define win_skip
Definition: test.h:149
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
void CheckValidation(UNICODE_STRING *PathName)
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)
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40
Definition: ps.c:97