ReactOS  0.4.14-dev-49-gfb4591c
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 #include <strsafe.h>
29 
30 static const char inffile[] = "test.inf";
31 static char CURR_DIR[MAX_PATH];
32 
33 /* Helpers */
34 
35 static void create_inf_file(LPCSTR filename, const char *data)
36 {
37  DWORD res;
38  BOOL ret;
43  assert(ret != 0);
45 }
46 
47 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
48 static HHOOK hhook;
50 {
51  return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
52 }
53 
55 {
56  char inf[2048];
57  char path[MAX_PATH];
58  HINF infhandle;
59  BOOL ret;
60  SC_HANDLE scm_handle, svc_handle;
61 
62  /* Bail out if we are on win98 */
63  SetLastError(0xdeadbeef);
65 
67  {
68  win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
69  return;
70  }
72 
73  /* Basic inf file to satisfy SetupOpenInfFileA */
74  strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
76  StringCbPrintfA(path, sizeof(path), "%s\\%s", CURR_DIR, inffile);
78 
79  /* Nothing but the Version section */
80  SetLastError(0xdeadbeef);
81  ret = SetupInstallServicesFromInfSectionExA(infhandle, "trolololo", 0, NULL, NULL, NULL,NULL);
82  ok(!ret, "Expected failure\n");
84  "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
85  SetupCloseInfFile(infhandle);
87 
88 
89  /* Add the section */
90  strcat(inf, "[Winetest.Services]\n");
93  SetLastError(0xdeadbeef);
94  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
95  ok(!ret, "Expected failure\n");
97  "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
98  SetupCloseInfFile(infhandle);
100 
101  /* Add a reference */
102  strcat(inf, "AddService=Winetest,,Winetest.Service\n");
103  create_inf_file(inffile, inf);
105  SetLastError(0xdeadbeef);
106  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
107  ok(!ret, "Expected failure\n");
109  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
110  SetupCloseInfFile(infhandle);
112 
113  /* Add the section */
114  strcat(inf, "[Winetest.Service]\n");
115  create_inf_file(inffile, inf);
117  SetLastError(0xdeadbeef);
118  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
119  ok(!ret, "Expected failure\n");
121  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
122  SetupCloseInfFile(infhandle);
124 
125  /* Just the ServiceBinary */
126  strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
127  create_inf_file(inffile, inf);
129  SetLastError(0xdeadbeef);
130  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
131  ok(!ret, "Expected failure\n");
133  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
134  SetupCloseInfFile(infhandle);
136 
137  /* Add the ServiceType */
138  strcat(inf, "ServiceType=1\n");
139  create_inf_file(inffile, inf);
141  SetLastError(0xdeadbeef);
142  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
143  ok(!ret, "Expected failure\n");
145  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
146  SetupCloseInfFile(infhandle);
148 
149  /* Add the StartType */
150  strcat(inf, "StartType=4\n");
151  create_inf_file(inffile, inf);
153  SetLastError(0xdeadbeef);
154  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
155  ok(!ret, "Expected failure\n");
157  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
158  SetupCloseInfFile(infhandle);
160 
161  /* This should be it, the minimal entries to install a service */
162  strcat(inf, "ErrorControl=1");
163  create_inf_file(inffile, inf);
165  SetLastError(0xdeadbeef);
166  ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
167  if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
168  {
169  skip("Not enough rights to install the service\n");
170  SetupCloseInfFile(infhandle);
172  return;
173  }
174  ok(ret, "Expected success\n");
176  "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
177  SetupCloseInfFile(infhandle);
179 
181 
182  /* Open the service to see if it's really there */
183  svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
184  ok(svc_handle != NULL, "Service was not created\n");
185 
186  SetLastError(0xdeadbeef);
187  ret = DeleteService(svc_handle);
188  ok(ret, "Service could not be deleted : %d\n", (int)GetLastError());
189 
190  CloseServiceHandle(svc_handle);
192 
193  strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
194  strcat(inf, "[XSP.InstallPerVer]\n");
195  strcat(inf, "AddReg=AspEventlogMsg.Reg,Perf.Reg,AspVersions.Reg,FreeADO.Reg,IndexServer.Reg\n");
196  create_inf_file(inffile, inf);
197  StringCbPrintfA(path, sizeof(path), "%s\\%s", CURR_DIR, inffile);
199 
200  SetLastError(0xdeadbeef);
201  ret = SetupInstallServicesFromInfSectionExA(infhandle, "XSP.InstallPerVer", 0, NULL, NULL, NULL,NULL);
202  ok(ret, "Expected success\n");
203  ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
204  SetupCloseInfFile(infhandle);
206 
207  /* TODO: Test the Flags */
208 }
210 {
211  char inf[2048];
212  char path[MAX_PATH];
213  HINF infhandle;
214  BOOL ret;
215  SC_HANDLE scm_handle, svc_handle;
216 
217  /* Bail out if we are on win98 */
218  SetLastError(0xdeadbeef);
220 
222  {
223  win_skip("OpenSCManagerW is not implemented, we are most likely on win9x\n");
224  return;
225  }
227 
228  /* Basic inf file to satisfy SetupOpenInfFileA */
229  strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
230  create_inf_file(inffile, inf);
231  StringCbPrintfA(path, sizeof(path), "%s\\%s", CURR_DIR, inffile);
233 
234  /* Nothing but the Version section */
235  SetLastError(0xdeadbeef);
236  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"trolololo", 0, NULL, NULL, NULL,NULL);
237  ok(!ret, "Expected failure\n");
239  "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
240  SetupCloseInfFile(infhandle);
242 
243 
244  /* Add the section */
245  strcat(inf, "[Winetest.Services]\n");
246  create_inf_file(inffile, inf);
248  SetLastError(0xdeadbeef);
249  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
250  ok(!ret, "Expected failure\n");
252  "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
253  SetupCloseInfFile(infhandle);
255 
256  /* Add a reference */
257  strcat(inf, "AddService=Winetest,,Winetest.Service\n");
258  create_inf_file(inffile, inf);
260  SetLastError(0xdeadbeef);
261  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
262  ok(!ret, "Expected failure\n");
264  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
265  SetupCloseInfFile(infhandle);
267 
268  /* Add the section */
269  strcat(inf, "[Winetest.Service]\n");
270  create_inf_file(inffile, inf);
272  SetLastError(0xdeadbeef);
273  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
274  ok(!ret, "Expected failure\n");
276  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
277  SetupCloseInfFile(infhandle);
279 
280  /* Just the ServiceBinary */
281  strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
282  create_inf_file(inffile, inf);
284  SetLastError(0xdeadbeef);
285  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
286  ok(!ret, "Expected failure\n");
288  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
289  SetupCloseInfFile(infhandle);
291 
292  /* Add the ServiceType */
293  strcat(inf, "ServiceType=1\n");
294  create_inf_file(inffile, inf);
296  SetLastError(0xdeadbeef);
297  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
298  ok(!ret, "Expected failure\n");
300  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
301  SetupCloseInfFile(infhandle);
303 
304  /* Add the StartType */
305  strcat(inf, "StartType=4\n");
306  create_inf_file(inffile, inf);
308  SetLastError(0xdeadbeef);
309  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
310  ok(!ret, "Expected failure\n");
312  "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
313  SetupCloseInfFile(infhandle);
315 
316  /* This should be it, the minimal entries to install a service */
317  strcat(inf, "ErrorControl=1");
318  create_inf_file(inffile, inf);
320  SetLastError(0xdeadbeef);
321  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
322  if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
323  {
324  skip("Not enough rights to install the service\n");
325  SetupCloseInfFile(infhandle);
327  return;
328  }
329  ok(ret, "Expected success\n");
331  "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
332  SetupCloseInfFile(infhandle);
334 
336 
337  /* Open the service to see if it's really there */
338  svc_handle = OpenServiceW(scm_handle, L"Winetest", DELETE);
339  ok(svc_handle != NULL, "Service was not created\n");
340 
341  SetLastError(0xdeadbeef);
342  ret = DeleteService(svc_handle);
343  ok(ret, "Service could not be deleted : %d\n", (int)GetLastError());
344 
345  CloseServiceHandle(svc_handle);
347 
348  strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
349  strcat(inf, "[XSP.InstallPerVer]\n");
350  strcat(inf, "AddReg=AspEventlogMsg.Reg,Perf.Reg,AspVersions.Reg,FreeADO.Reg,IndexServer.Reg\n");
351  create_inf_file(inffile, inf);
352  StringCbPrintfA(path, sizeof(path), "%s\\%s", CURR_DIR, inffile);
354 
355  SetLastError(0xdeadbeef);
356  ret = SetupInstallServicesFromInfSectionExW(infhandle, L"XSP.InstallPerVer", 0, NULL, NULL, NULL, NULL);
357  ok(ret, "Expected success\n");
358  ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
359  SetupCloseInfFile(infhandle);
361 
362  /* TODO: Test the Flags */
363 }
364 
365 
367 {
368  char temp_path[MAX_PATH], prev_path[MAX_PATH];
369 
370  GetCurrentDirectoryA(MAX_PATH, prev_path);
373 
375 
376  /* Set CBT hook to disallow MessageBox creation in current thread */
378  assert(hhook != 0);
379 
382 
384  SetCurrentDirectoryA(prev_path);
385 }
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
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
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:458
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
STRSAFEAPI StringCbPrintfA(STRSAFE_LPSTR pszDest, size_t cbDest, STRSAFE_LPCSTR pszFormat,...)
Definition: strsafe.h:547
#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
#define ok(value,...)
Definition: atltest.h:57
DWORD WINAPI GetCurrentDirectoryA(IN DWORD nBufferLength, OUT LPSTR lpBuffer)
Definition: path.c:2145
Definition: services.c:325
SC_HANDLE WINAPI OpenServiceA(SC_HANDLE hSCManager, LPCSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2055
#define skip(...)
Definition: atltest.h:64
GLuint res
Definition: glext.h:9613
#define SetupInstallServicesFromInfSectionEx
Definition: setupapi.h:2642
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)