mshtml: Default to previous script type if not given.
[wine] / programs / services / tests / service.c
1 /*
2  * Copyright 2012 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <windows.h>
20 #include <stdio.h>
21
22 #include "wine/test.h"
23
24 static SERVICE_STATUS_HANDLE (WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR,LPHANDLER_FUNCTION_EX,LPVOID);
25
26 static HANDLE pipe_handle = INVALID_HANDLE_VALUE;
27 static char service_name[100], named_pipe_name[100];
28 static SERVICE_STATUS_HANDLE service_handle;
29
30 /* Service process global variables */
31 static HANDLE service_stop_event;
32
33 static void send_msg(const char *type, const char *msg)
34 {
35     DWORD written = 0;
36     char buf[512];
37
38     sprintf(buf, "%s:%s", type, msg);
39     WriteFile(pipe_handle, buf, strlen(buf)+1, &written, NULL);
40 }
41
42 static inline void service_trace(const char *msg)
43 {
44     send_msg("TRACE", msg);
45 }
46
47 static inline void service_event(const char *event)
48 {
49     send_msg("EVENT", event);
50 }
51
52 static void service_ok(int cnd, const char *msg, ...)
53 {
54    va_list valist;
55    char buf[512];
56
57     va_start(valist, msg);
58     vsprintf(buf, msg, valist);
59     va_end(valist);
60
61     send_msg(cnd ? "OK" : "FAIL", buf);
62 }
63
64 static DWORD WINAPI service_handler(DWORD ctrl, DWORD event_type, void *event_data, void *context)
65 {
66     SERVICE_STATUS status;
67
68     status.dwServiceType             = SERVICE_WIN32;
69     status.dwControlsAccepted        = SERVICE_ACCEPT_STOP;
70     status.dwWin32ExitCode           = 0;
71     status.dwServiceSpecificExitCode = 0;
72     status.dwCheckPoint              = 0;
73     status.dwWaitHint                = 0;
74
75     switch(ctrl)
76     {
77     case SERVICE_CONTROL_STOP:
78     case SERVICE_CONTROL_SHUTDOWN:
79         service_event("STOP");
80         status.dwCurrentState     = SERVICE_STOP_PENDING;
81         status.dwControlsAccepted = 0;
82         SetServiceStatus(service_handle, &status);
83         SetEvent(service_stop_event);
84         return NO_ERROR;
85     default:
86         status.dwCurrentState = SERVICE_RUNNING;
87         SetServiceStatus( service_handle, &status );
88         return NO_ERROR;
89     }
90 }
91
92 static void WINAPI service_main(DWORD argc, char **argv)
93 {
94     SERVICE_STATUS status;
95     BOOL res;
96
97     service_ok(argc == 1, "argc = %d", argc);
98     if(argc)
99         service_ok(!strcmp(argv[0], service_name), "argv[0] = %s, expected %s", argv[0], service_name);
100
101     service_handle = pRegisterServiceCtrlHandlerExA(service_name, service_handler, NULL);
102     service_ok(service_handle != NULL, "RegisterServiceCtrlHandlerEx failed: %u\n", GetLastError());
103     if(!service_handle)
104         return;
105
106     status.dwServiceType             = SERVICE_WIN32;
107     status.dwCurrentState            = SERVICE_RUNNING;
108     status.dwControlsAccepted        = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
109     status.dwWin32ExitCode           = 0;
110     status.dwServiceSpecificExitCode = 0;
111     status.dwCheckPoint              = 0;
112     status.dwWaitHint                = 10000;
113     res = SetServiceStatus(service_handle, &status);
114     service_ok(res, "SetServiceStatus(SERVICE_RUNNING) failed: %u", GetLastError());
115
116     service_event("RUNNING");
117
118     WaitForSingleObject(service_stop_event, INFINITE);
119
120     status.dwCurrentState     = SERVICE_STOPPED;
121     status.dwControlsAccepted = 0;
122     res = SetServiceStatus(service_handle, &status);
123     service_ok(res, "SetServiceStatus(SERVICE_STOPPED) failed: %u", GetLastError());
124 }
125
126 static void service_process(void)
127 {
128     BOOL res;
129
130     static const SERVICE_TABLE_ENTRYA servtbl[] = {
131         {service_name, service_main},
132         {NULL, NULL}
133     };
134
135     res = WaitNamedPipeA(named_pipe_name, NMPWAIT_USE_DEFAULT_WAIT);
136     if(!res)
137         return;
138
139     pipe_handle = CreateFileA(named_pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
140     if(pipe_handle == INVALID_HANDLE_VALUE)
141         return;
142
143     service_trace("Starting...");
144
145     service_stop_event = CreateEventA(NULL, TRUE, FALSE, NULL);
146     service_ok(service_stop_event != NULL, "Could not create event: %u\n", GetLastError());
147     if(!service_stop_event)
148         return;
149
150     res = StartServiceCtrlDispatcherA(servtbl);
151     service_ok(res, "StartServiceCtrlDispatcher failed: %u\n", GetLastError());
152
153     CloseHandle(service_stop_event);
154 }
155
156 /* Test process global variables */
157 static SC_HANDLE scm_handle;
158
159 static char current_event[32];
160 static HANDLE event_handle = INVALID_HANDLE_VALUE;
161 static CRITICAL_SECTION event_cs;
162
163 static SC_HANDLE register_service(void)
164 {
165     char service_cmd[MAX_PATH+150], *ptr;
166     SC_HANDLE service;
167
168     ptr = service_cmd + GetModuleFileNameA(NULL, service_cmd, MAX_PATH);
169
170     /* If the file doesn't exist, assume we're using built-in exe and append .so to the path */
171     if(GetFileAttributesA(service_cmd) == INVALID_FILE_ATTRIBUTES) {
172         strcpy(ptr, ".so");
173         ptr += 3;
174     }
175
176     strcpy(ptr, " service ");
177     ptr += strlen(ptr);
178     strcpy(ptr, service_name);
179
180     trace("service_cmd \"%s\"\n", service_cmd);
181
182     service = CreateServiceA(scm_handle, service_name, service_name, GENERIC_ALL,
183                              SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE,
184                              service_cmd, NULL, NULL, NULL, NULL, NULL);
185     if(!service && GetLastError() == ERROR_ACCESS_DENIED) {
186         skip("Not enough access right to create service\n");
187         return NULL;
188     }
189
190     ok(service != NULL, "CreateService failed: %u\n", GetLastError());
191     return service;
192 }
193
194 static void expect_event(const char *event_name)
195 {
196     char evt[32];
197     DWORD res;
198
199     trace("waiting for %s\n", event_name);
200
201     res = WaitForSingleObject(event_handle, 30000);
202     ok(res == WAIT_OBJECT_0, "WaitForSingleObject failed: %u\n", res);
203     if(res != WAIT_OBJECT_0)
204         return;
205
206     EnterCriticalSection(&event_cs);
207     strcpy(evt, current_event);
208     *current_event = 0;
209     LeaveCriticalSection(&event_cs);
210
211     ok(!strcmp(evt, event_name), "Unexpected event: %s, expected %s\n", evt, event_name);
212 }
213
214 static DWORD WINAPI pipe_thread(void *arg)
215 {
216     char buf[257], *ptr;
217     DWORD read;
218     BOOL res;
219
220     res = ConnectNamedPipe(pipe_handle, NULL);
221     ok(res || GetLastError() == ERROR_PIPE_CONNECTED, "ConnectNamedPipe failed: %u\n", GetLastError());
222
223     while(1) {
224         res = ReadFile(pipe_handle, buf, sizeof(buf), &read, NULL);
225         if(!res) {
226             ok(GetLastError() == ERROR_BROKEN_PIPE || GetLastError() == ERROR_INVALID_HANDLE,
227                "ReadFile failed: %u\n", GetLastError());
228             break;
229         }
230
231         for(ptr = buf; ptr < buf+read; ptr += strlen(ptr)+1) {
232             if(!strncmp(ptr, "TRACE:", 6)) {
233                 trace("service trace: %s\n", ptr+6);
234             }else if(!strncmp(ptr, "OK:", 3)) {
235                 ok(1, "service: %s\n", ptr+3);
236             }else if(!strncmp(ptr, "FAIL:", 3)) {
237                 ok(0, "service: %s\n", ptr+5);
238             }else if(!strncmp(ptr, "EVENT:", 6)) {
239                 trace("service event: %s\n", ptr+6);
240                 EnterCriticalSection(&event_cs);
241                 ok(!current_event[0], "event %s still queued\n", current_event);
242                 strcpy(current_event, ptr+6);
243                 LeaveCriticalSection(&event_cs);
244                 SetEvent(event_handle);
245             }else {
246                 ok(0, "malformed service message: %s\n", ptr);
247             }
248         }
249     }
250
251     DisconnectNamedPipe(pipe_handle);
252     trace("pipe disconnected\n");
253     return 0;
254 }
255
256 static void test_process(void)
257 {
258     SC_HANDLE service_handle;
259     SERVICE_STATUS status;
260     HANDLE thread;
261     BOOL res;
262
263     sprintf(service_name, "WineTestService%d", GetTickCount());
264     trace("service_name: %s\n", service_name);
265     sprintf(named_pipe_name, "\\\\.\\pipe\\%s_pipe", service_name);
266
267     pipe_handle = CreateNamedPipeA(named_pipe_name, PIPE_ACCESS_INBOUND,
268                                    PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_WAIT, 10, 2048, 2048, 10000, NULL);
269     ok(pipe_handle != INVALID_HANDLE_VALUE, "CreateNamedPipe failed: %u\n", GetLastError());
270     if(pipe_handle == INVALID_HANDLE_VALUE)
271         return;
272
273     InitializeCriticalSection(&event_cs);
274     event_handle = CreateEventA(NULL, FALSE, FALSE, NULL);
275     ok(event_handle != INVALID_HANDLE_VALUE, "CreateEvent failed: %u\n", GetLastError());
276     if(event_handle == INVALID_HANDLE_VALUE)
277         return;
278
279     thread = CreateThread(NULL, 0, pipe_thread, NULL, 0, NULL);
280     ok(thread != NULL, "CreateThread failed: %u\n", GetLastError());
281     if(!thread)
282         return;
283
284     service_handle = register_service();
285     if(!service_handle)
286         return;
287
288     trace("starting...\n");
289     res = StartServiceA(service_handle, 0, NULL);
290     ok(res, "StartService failed: %u\n", GetLastError());
291     if(!res)
292         return;
293     expect_event("RUNNING");
294
295     res = ControlService(service_handle, SERVICE_CONTROL_STOP, &status);
296     ok(res, "ControlService failed: %u\n", GetLastError());
297     expect_event("STOP");
298
299     res = DeleteService(service_handle);
300     ok(res, "DeleteService failed: %u\n", GetLastError());
301
302     CloseServiceHandle(service_handle);
303 }
304
305 START_TEST(service)
306 {
307     char **argv;
308     int argc;
309
310     pRegisterServiceCtrlHandlerExA = (void*)GetProcAddress(GetModuleHandleA("advapi32.dll"), "RegisterServiceCtrlHandlerExA");
311     if(!pRegisterServiceCtrlHandlerExA) {
312         win_skip("RegisterServiceCtrlHandlerExA not available, skipping tests\n");
313         return;
314     }
315
316     argc = winetest_get_mainargs(&argv);
317
318     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
319     ok(scm_handle != NULL, "OpenSCManager failed: %u\n", GetLastError());
320
321     if(argc < 3) {
322         test_process();
323     }else {
324         strcpy(service_name, argv[2]);
325         sprintf(named_pipe_name, "\\\\.\\pipe\\%s_pipe", service_name);
326
327         service_process();
328     }
329
330     CloseHandle(event_handle);
331     CloseHandle(pipe_handle);
332     CloseServiceHandle(scm_handle);
333 }