ReactOS  0.4.13-dev-52-g0efcfec
SetupInstallServicesFromInfSectionEx.c
Go to the documentation of this file.
1 /*
2  * SetupAPI device class-related functions tests
3  *
4  * Copyright 2015 Víctor Martínez (victor.martinez@reactos.org)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include <apitest.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include <winuser.h>
25 #include <winreg.h>
26 #include <winsvc.h>
27 #include <setupapi.h>
28 
29 static const char inffile[] = "test.inf";
30 static char CURR_DIR[MAX_PATH];
31 
32 /* Helpers */
33 
34 static void create_inf_file(LPCSTR filename, const char *data)
35 {
36  DWORD res;
37  BOOL ret;
42  assert(ret != 0);
44 }
45 
46 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
47 static HHOOK hhook;
49 {
50  return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
51 }
52 
54 {
55  char inf[2048];
56  char path[MAX_PATH];
57  HINF infhandle;
58  BOOL ret;
59  SC_HANDLE scm_handle, svc_handle;
60 
61  /* Bail out if we are on win98 */
62  SetLastError(0xdeadbeef);
64 
66  {
67  win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
68  return;
69  }
71 
72  /* Basic inf file to satisfy SetupOpenInfFileA */
73  strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
75  sprintf(path, "%s\\%s", CURR_DIR, inffile);
77 
78  /* Nothing but the Version section */
79  SetLastError(0xdeadbeef);
80  ret = SetupInstallServicesFromInfSectionExA(infhandle, "trolololo", 0, NULL, NULL, NULL,NULL);
81  ok(!ret, "Expected failure\n");
83  "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
84  SetupCloseInfFile(infhandle);
86 
87 
88  /* Add the section */
89  strcat(inf, "[Winetest.Services]\n");
92  SetLastError(0xdeadbeef);
93  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
94  ok(!ret, "Expected failure\n");
96  "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
97  SetupCloseInfFile(infhandle);
99 
100  /* Add a reference */
101  strcat(inf, "AddService=Winetest,,Winetest.Service\n");
102  create_inf_file(inffile, inf);
104  SetLastError(0xdeadbeef);
105  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
106  ok(!ret, "Expected failure\n");
108  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
109  SetupCloseInfFile(infhandle);
111 
112  /* Add the section */
113  strcat(inf, "[Winetest.Service]\n");
114  create_inf_file(inffile, inf);
116  SetLastError(0xdeadbeef);
117  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
118  ok(!ret, "Expected failure\n");
120  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
121  SetupCloseInfFile(infhandle);
123 
124  /* Just the ServiceBinary */
125  strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
126  create_inf_file(inffile, inf);
128  SetLastError(0xdeadbeef);
129  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
130  ok(!ret, "Expected failure\n");
132  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
133  SetupCloseInfFile(infhandle);
135 
136  /* Add the ServiceType */
137  strcat(inf, "ServiceType=1\n");
138  create_inf_file(inffile, inf);
140  SetLastError(0xdeadbeef);
141  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
142  ok(!ret, "Expected failure\n");
144  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
145  SetupCloseInfFile(infhandle);
147 
148  /* Add the StartType */
149  strcat(inf, "StartType=4\n");
150  create_inf_file(inffile, inf);
152  SetLastError(0xdeadbeef);
153  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
154  ok(!ret, "Expected failure\n");
156  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
157  SetupCloseInfFile(infhandle);
159 
160  /* This should be it, the minimal entries to install a service */
161  strcat(inf, "ErrorControl=1");
162  create_inf_file(inffile, inf);
164  SetLastError(0xdeadbeef);
165  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
166  if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
167  {
168  skip("Not enough rights to install the service\n");
169  SetupCloseInfFile(infhandle);
171  return;
172  }
173  ok(ret, "Expected success\n");
175  "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
176  SetupCloseInfFile(infhandle);
178 
180 
181  /* Open the service to see if it's really there */
182  svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
183  ok(svc_handle != NULL, "Service was not created\n");
184 
185  SetLastError(0xdeadbeef);
186  ret = DeleteService(svc_handle);
187  ok(ret, "Service could not be deleted : %d\n", (int)GetLastError());
188 
189  CloseServiceHandle(svc_handle);
191 
192  strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
193  strcat(inf, "[XSP.InstallPerVer]\n");
194  strcat(inf, "AddReg=AspEventlogMsg.Reg,Perf.Reg,AspVersions.Reg,FreeADO.Reg,IndexServer.Reg\n");
195  create_inf_file(inffile, inf);
196  sprintf(path, "%s\\%s", CURR_DIR, inffile);
198 
199  SetLastError(0xdeadbeef);
200  ret = SetupInstallServicesFromInfSectionExA(infhandle, "XSP.InstallPerVer", 0, NULL, NULL, NULL,NULL);
201  ok(ret, "Expected success\n");
202  ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
203  SetupCloseInfFile(infhandle);
205 
206  /* TODO: Test the Flags */
207 }
209 {
210  char inf[2048];
211  char path[MAX_PATH];
212  HINF infhandle;
213  BOOL ret;
214  SC_HANDLE scm_handle, svc_handle;
215 
216  /* Bail out if we are on win98 */
217  SetLastError(0xdeadbeef);
219 
221  {
222  win_skip("OpenSCManagerW is not implemented, we are most likely on win9x\n");
223  return;
224  }
226 
227  /* Basic inf file to satisfy SetupOpenInfFileA */
228  strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
229  create_inf_file(inffile, inf);
230  sprintf(path, "%s\\%s", CURR_DIR, inffile);
232 
233  /* Nothing but the Version section */
234  SetLastError(0xdeadbeef);
235  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"trolololo", 0, NULL, NULL, NULL,NULL);
236  ok(!ret, "Expected failure\n");
238  "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
239  SetupCloseInfFile(infhandle);
241 
242 
243  /* Add the section */
244  strcat(inf, "[Winetest.Services]\n");
245  create_inf_file(inffile, inf);
247  SetLastError(0xdeadbeef);
248  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
249  ok(!ret, "Expected failure\n");
251  "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
252  SetupCloseInfFile(infhandle);
254 
255  /* Add a reference */
256  strcat(inf, "AddService=Winetest,,Winetest.Service\n");
257  create_inf_file(inffile, inf);
259  SetLastError(0xdeadbeef);
260  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
261  ok(!ret, "Expected failure\n");
263  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
264  SetupCloseInfFile(infhandle);
266 
267  /* Add the section */
268  strcat(inf, "[Winetest.Service]\n");
269  create_inf_file(inffile, inf);
271  SetLastError(0xdeadbeef);
272  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
273  ok(!ret, "Expected failure\n");
275  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
276  SetupCloseInfFile(infhandle);
278 
279  /* Just the ServiceBinary */
280  strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
281  create_inf_file(inffile, inf);
283  SetLastError(0xdeadbeef);
284  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
285  ok(!ret, "Expected failure\n");
287  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
288  SetupCloseInfFile(infhandle);
290 
291  /* Add the ServiceType */
292  strcat(inf, "ServiceType=1\n");
293  create_inf_file(inffile, inf);
295  SetLastError(0xdeadbeef);
296  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
297  ok(!ret, "Expected failure\n");
299  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
300  SetupCloseInfFile(infhandle);
302 
303  /* Add the StartType */
304  strcat(inf, "StartType=4\n");
305  create_inf_file(inffile, inf);
307  SetLastError(0xdeadbeef);
308  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
309  ok(!ret, "Expected failure\n");
311  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
312  SetupCloseInfFile(infhandle);
314 
315  /* This should be it, the minimal entries to install a service */
316  strcat(inf, "ErrorControl=1");
317  create_inf_file(inffile, inf);
319  SetLastError(0xdeadbeef);
320  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
321  if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
322  {
323  skip("Not enough rights to install the service\n");
324  SetupCloseInfFile(infhandle);
326  return;
327  }
328  ok(ret, "Expected success\n");
330  "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
331  SetupCloseInfFile(infhandle);
333 
335 
336  /* Open the service to see if it's really there */
337  svc_handle = OpenServiceW(scm_handle, L"Winetest", DELETE);
338  ok(svc_handle != NULL, "Service was not created\n");
339 
340  SetLastError(0xdeadbeef);
341  ret = DeleteService(svc_handle);
342  ok(ret, "Service could not be deleted : %d\n", (int)GetLastError());
343 
344  CloseServiceHandle(svc_handle);
346 
347  strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
348  strcat(inf, "[XSP.InstallPerVer]\n");
349  strcat(inf, "AddReg=AspEventlogMsg.Reg,Perf.Reg,AspVersions.Reg,FreeADO.Reg,IndexServer.Reg\n");
350  create_inf_file(inffile, inf);
351  sprintf(path, "%s\\%s", CURR_DIR, inffile);
353 
354  SetLastError(0xdeadbeef);
355  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"XSP.InstallPerVer", 0, NULL, NULL, NULL, NULL);
356  ok(ret, "Expected success\n");
357  ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
358  SetupCloseInfFile(infhandle);
360 
361  /* TODO: Test the Flags */
362 }
363 
364 
366 {
367  char temp_path[MAX_PATH], prev_path[MAX_PATH];
368 
369  GetCurrentDirectoryA(MAX_PATH, prev_path);
372 
374 
375  /* Set CBT hook to disallow MessageBox creation in current thread */
377  assert(hhook != 0);
378 
381 
383  SetCurrentDirectoryA(prev_path);
384 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#define GENERIC_ALL
Definition: nt_native.h:92
#define ERROR_SECTION_NOT_FOUND
Definition: setupapi.h:288
#define CloseHandle
Definition: compat.h:398
#define HCBT_CREATEWND
Definition: winuser.h:58
#define INF_STYLE_WIN4
Definition: infsupp.h:41
#define ERROR_SUCCESS
Definition: deptool.c:10
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
BOOL WINAPI SetupInstallServicesFromInfSectionExW(HINF hinf, PCWSTR sectionname, DWORD flags, HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData, PVOID reserved1, PVOID reserved2)
Definition: install.c:2069
static void test_SetupInstallServicesFromInfSectionExW(void)
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
static SC_HANDLE scm_handle
Definition: ServiceArgs.c:20
#define CALLBACK
Definition: compat.h:27
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
#define assert(x)
Definition: debug.h:53
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
UINT_PTR WPARAM
Definition: windef.h:207
BOOL WINAPI DeleteService(SC_HANDLE hService)
Definition: scm.c:916
const char * filename
Definition: ioapi.h:135
WPARAM wParam
Definition: combotst.c:138
VOID WINAPI SetupCloseInfFile(IN HINF InfHandle)
Definition: infsupp.c:43
#define sprintf(buf, format,...)
Definition: sprintf.c:55
HHOOK WINAPI SetWindowsHookExA(_In_ int, _In_ HOOKPROC, _In_opt_ HINSTANCE, _In_ DWORD)
SC_HANDLE WINAPI OpenSCManagerA(LPCSTR lpMachineName, LPCSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:1967
#define ERROR_ACCESS_DENIED
Definition: compat.h:87
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GENERIC_WRITE
Definition: nt_native.h:90
#define ok(value,...)
SC_HANDLE WINAPI OpenServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2103
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:577
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
BOOL WINAPI DeleteFileA(IN LPCSTR lpFileName)
Definition: delete.c:24
static char CURR_DIR[MAX_PATH]
#define WH_CBT
Definition: winuser.h:35
const char * LPCSTR
Definition: xmlstorage.h:183
static const char inffile[]
DWORD WINAPI GetCurrentThreadId(VOID)
Definition: thread.c:421
static void test_SetupInstallServicesFromInfSectionExA(void)
static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
#define ERROR_BAD_SERVICE_INSTALLSECT
Definition: setupapi.h:314
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
int ret
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
static const WCHAR L[]
Definition: oid.c:1250
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2011
BOOL WINAPI SetCurrentDirectoryA(IN LPCSTR lpPathName)
Definition: path.c:2205
BOOL WINAPI UnhookWindowsHookEx(_In_ HHOOK)
HINF WINAPI SetupOpenInfFileA(PCSTR name, PCSTR class, DWORD style, UINT *error)
Definition: parser.c:1139
START_TEST(SetupInstallServicesFromInfSectionEx)
#define CREATE_ALWAYS
Definition: disk.h:72
LRESULT WINAPI CallNextHookEx(_In_opt_ HHOOK, _In_ int, _In_ WPARAM, _In_ LPARAM)
DWORD WINAPI GetTempPathA(IN DWORD nBufferLength, OUT LPSTR lpBuffer)
Definition: path.c:2053
DWORD WINAPI GetCurrentDirectoryA(IN DWORD nBufferLength, OUT LPSTR lpBuffer)
Definition: path.c:2145
Definition: services.c:325
#define skip(...)
SC_HANDLE WINAPI OpenServiceA(SC_HANDLE hSCManager, LPCSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2055
GLuint res
Definition: glext.h:9613
#define SetupInstallServicesFromInfSectionEx
Definition: setupapi.h:2641
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:92
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:399
LONG_PTR LRESULT
Definition: windef.h:209
BOOL WINAPI SetupInstallServicesFromInfSectionExA(HINF hinf, PCSTR sectionname, DWORD flags, HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data, PVOID reserved1, PVOID reserved2)
Definition: install.c:1626
#define win_skip
Definition: test.h:141
LPARAM lParam
Definition: combotst.c:139
#define DELETE
Definition: nt_native.h:57
char temp_path[MAX_PATH]
Definition: mspatcha.c:123
static void create_inf_file(LPCSTR filename, const char *data)