2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for service process environment block
5 * PROGRAMMER: Hermes Belusca-Maito
13 /*** Service part of the test ***/
15 static SERVICE_STATUS_HANDLE status_handle
;
18 report_service_status(DWORD dwCurrentState
,
19 DWORD dwWin32ExitCode
,
23 SERVICE_STATUS status
;
25 status
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
26 status
.dwCurrentState
= dwCurrentState
;
27 status
.dwWin32ExitCode
= dwWin32ExitCode
;
28 status
.dwWaitHint
= dwWaitHint
;
30 status
.dwServiceSpecificExitCode
= 0;
31 status
.dwCheckPoint
= 0;
33 if ( (dwCurrentState
== SERVICE_START_PENDING
) ||
34 (dwCurrentState
== SERVICE_STOP_PENDING
) ||
35 (dwCurrentState
== SERVICE_STOPPED
) )
37 status
.dwControlsAccepted
= 0;
41 status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN
;
45 if ( (dwCurrentState
== SERVICE_RUNNING
) || (dwCurrentState
== SERVICE_STOPPED
) )
46 status
.dwCheckPoint
= 0;
48 status
.dwCheckPoint
= dwCheckPoint
++;
51 res
= SetServiceStatus(status_handle
, &status
);
52 service_ok(res
, "SetServiceStatus(%d) failed: %lu\n", dwCurrentState
, GetLastError());
55 static VOID WINAPI
service_handler(DWORD ctrl
)
59 case SERVICE_CONTROL_STOP
:
60 case SERVICE_CONTROL_SHUTDOWN
:
61 report_service_status(SERVICE_STOP_PENDING
, NO_ERROR
, 0);
63 report_service_status(SERVICE_RUNNING
, NO_ERROR
, 0);
68 service_main(DWORD dwArgc
, LPWSTR
* lpszArgv
)
70 // SERVICE_STATUS_HANDLE status_handle;
71 LPWSTR lpEnvironment
, lpEnvStr
;
74 UNREFERENCED_PARAMETER(dwArgc
);
75 UNREFERENCED_PARAMETER(lpszArgv
);
77 /* Register our service for control (lpszArgv[0] holds the service name) */
78 status_handle
= RegisterServiceCtrlHandlerW(lpszArgv
[0], service_handler
);
79 service_ok(status_handle
!= NULL
, "RegisterServiceCtrlHandler failed: %lu\n", GetLastError());
83 /* Report SERVICE_RUNNING status */
84 report_service_status(SERVICE_RUNNING
, NO_ERROR
, 4000);
86 /* Display our current environment for informative purposes */
87 lpEnvironment
= GetEnvironmentStringsW();
88 lpEnvStr
= lpEnvironment
;
91 service_trace("%S\n", lpEnvStr
);
92 lpEnvStr
+= wcslen(lpEnvStr
) + 1;
94 FreeEnvironmentStringsW(lpEnvironment
);
96 /* Check the presence of the user-related environment variables */
97 dwSize
= GetEnvironmentVariableW(L
"ALLUSERSPROFILE", NULL
, 0);
98 service_ok(dwSize
!= 0, "ALLUSERSPROFILE envvar not found, or GetEnvironmentVariableW failed: %lu\n", GetLastError());
99 dwSize
= GetEnvironmentVariableW(L
"USERPROFILE", NULL
, 0);
100 service_ok(dwSize
!= 0, "USERPROFILE envvar not found, or GetEnvironmentVariableW failed: %lu\n", GetLastError());
101 #if 0 // May not always exist
102 dwSize
= GetEnvironmentVariableW(L
"USERNAME", NULL
, 0);
103 service_ok(dwSize
!= 0, "USERNAME envvar not found, or GetEnvironmentVariableW failed: %lu\n", GetLastError());
107 report_service_status(SERVICE_STOPPED
, NO_ERROR
, 0);
110 static BOOL
start_service(PCSTR service_nameA
, PCWSTR service_nameW
)
114 SERVICE_TABLE_ENTRYW servtbl
[] =
116 { (PWSTR
)service_nameW
, service_main
},
120 res
= StartServiceCtrlDispatcherW(servtbl
);
121 service_ok(res
, "StartServiceCtrlDispatcherW failed: %lu\n", GetLastError());
126 /*** Tester part of the test ***/
129 my_test_server(PCSTR service_nameA
,
130 PCWSTR service_nameW
,
134 SC_HANDLE hSC
= NULL
;
135 SC_HANDLE hService
= NULL
;
136 SERVICE_STATUS ServiceStatus
;
139 hSC
= OpenSCManagerW(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
142 skip("OpenSCManagerW failed with error %lu!\n", GetLastError());
146 /* First create ourselves as a service running in the default LocalSystem account */
147 hService
= register_service_exW(hSC
, L
"ServiceEnv", service_nameW
, NULL
,
149 SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
,
150 SERVICE_DEMAND_START
,
151 SERVICE_ERROR_IGNORE
,
156 skip("CreateServiceW failed with error %lu!\n", GetLastError());
161 if (!StartServiceW(hService
, 0, NULL
))
163 skip("StartServiceW failed with error %lu!\n", GetLastError());
167 /* Wait for the service to stop by itself */
171 ZeroMemory(&ServiceStatus
, sizeof(ServiceStatus
));
172 res
= QueryServiceStatus(hService
, &ServiceStatus
);
173 } while (res
&& ServiceStatus
.dwCurrentState
!= SERVICE_STOPPED
);
174 ok(res
, "QueryServiceStatus failed: %lu\n", GetLastError());
175 ok(ServiceStatus
.dwCurrentState
== SERVICE_STOPPED
, "ServiceStatus.dwCurrentState = %lx\n", ServiceStatus
.dwCurrentState
);
177 /* Be sure the service is really stopped */
178 res
= ControlService(hService
, SERVICE_CONTROL_STOP
, &ServiceStatus
);
179 if (!res
&& ServiceStatus
.dwCurrentState
!= SERVICE_STOPPED
&&
180 ServiceStatus
.dwCurrentState
!= SERVICE_STOP_PENDING
&&
181 GetLastError() != ERROR_SERVICE_NOT_ACTIVE
)
183 skip("ControlService failed with error %lu!\n", GetLastError());
188 trace("Service stopped. Going to restart it...\n");
190 /* Now change the service configuration to make it start under the NetworkService account */
191 if (!ChangeServiceConfigW(hService
,
195 NULL
, NULL
, NULL
, NULL
,
196 L
"NT AUTHORITY\\NetworkService", L
"",
199 skip("ChangeServiceConfigW failed with error %lu!\n", GetLastError());
204 if (!StartServiceW(hService
, 0, NULL
))
206 skip("StartServiceW failed with error %lu!\n", GetLastError());
210 /* Wait for the service to stop by itself */
214 ZeroMemory(&ServiceStatus
, sizeof(ServiceStatus
));
215 res
= QueryServiceStatus(hService
, &ServiceStatus
);
216 } while (res
&& ServiceStatus
.dwCurrentState
!= SERVICE_STOPPED
);
217 ok(res
, "QueryServiceStatus failed: %lu\n", GetLastError());
218 ok(ServiceStatus
.dwCurrentState
== SERVICE_STOPPED
, "ServiceStatus.dwCurrentState = %lx\n", ServiceStatus
.dwCurrentState
);
220 /* Be sure the service is really stopped */
221 res
= ControlService(hService
, SERVICE_CONTROL_STOP
, &ServiceStatus
);
222 if (!res
&& ServiceStatus
.dwCurrentState
!= SERVICE_STOPPED
&&
223 ServiceStatus
.dwCurrentState
!= SERVICE_STOP_PENDING
&&
224 GetLastError() != ERROR_SERVICE_NOT_ACTIVE
)
226 skip("ControlService failed with error %lu!\n", GetLastError());
234 res
= DeleteService(hService
);
235 ok(res
, "DeleteService failed: %lu\n", GetLastError());
236 CloseServiceHandle(hService
);
240 CloseServiceHandle(hSC
);
243 START_TEST(ServiceEnv
)
248 /* Check whether this test is started as a separated service process */
249 argc
= winetest_get_mainargs(&argv
);
252 service_process(start_service
, argc
, argv
);
256 /* We are started as the real test */
257 test_runner(my_test_server
, NULL
);
258 // trace("Returned from test_runner\n");