Time to commit some Work-In-Progress stuff before my diff gets too large..
[reactos.git] / rostests / apitests / setupapi / SetupDiInstallClassExA.c
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;
38 HANDLE handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
39 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
40 assert(handle != INVALID_HANDLE_VALUE);
41 ret = WriteFile(handle, data, strlen(data), &res, NULL);
42 assert(ret != 0);
43 CloseHandle(handle);
44 }
45
46 static void test_SetupDiInstallClassExA(void)
47 {
48 char inf[2048];
49 char path[MAX_PATH];
50 BOOL ret;
51 ULONG del;
52 HKEY RegHandle;
53 HINF infhandle;
54 SC_HANDLE scm_handle, svc_handle;
55
56 /* [Version]:Signature */
57 strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
58 create_inf_file(inffile, inf);
59 sprintf(path, "%s\\%s", CURR_DIR, inffile);
60
61 SetLastError(0xdeadbeef);
62 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
63 ok(!ret, "Expected failure\n");
64 ok(GetLastError() == ERROR_INVALID_CLASS,
65 "Expected ERROR_INVALID_CLASS, got %08x\n", (int)GetLastError());
66 DeleteFileA(inffile);
67
68 /* [Version]:Signature+Class */
69 strcat(inf, "Class=MySampleClass\n");
70 create_inf_file(inffile, inf);
71 sprintf(path, "%s\\%s", CURR_DIR, inffile);
72
73 SetLastError(0xdeadbeef);
74 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
75 ok(!ret, "Expected failure\n");
76 ok(GetLastError() == ERROR_INVALID_CLASS,
77 "Expected ERROR_INVALID_CLASS, got %08x\n", (int)GetLastError());
78 DeleteFileA(inffile);
79
80 /* [Version]:Signature+Class+ClassGUID */
81 strcat(inf, "ClassGuid={3b409830-5f9d-432a-abf5-7d2e4e102467}\n");
82 create_inf_file(inffile, inf);
83 sprintf(path, "%s\\%s", CURR_DIR, inffile);
84
85 SetLastError(0xdeadbeef);
86 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
87 ok(!ret, "Expected failure\n");
88 ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
89 "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", (int)GetLastError());
90 DeleteFileA(inffile);
91
92 /* [Version]Signature+Class+ClassGUID;[ClassInstall32.NT]Empty */
93 strcat(inf, "[ClassInstall32.NT]\n");
94 create_inf_file(inffile, inf);
95 sprintf(path, "%s\\%s", CURR_DIR, inffile);
96
97 SetLastError(0xdeadbeef);
98 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
99 ok(ret, "Expected success\n");
100 ok(!GetLastError(),
101 "Expected no error, got %08x\n", (int)GetLastError());
102 DeleteFileA(inffile);
103 if (ret == TRUE)
104 {
105 RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM", 0, KEY_ALL_ACCESS, &RegHandle);
106 del = RegDeleteKeyW(RegHandle, L"CurrentControlSet\\Control\\Class\\{3B409830-5F9D-432A-ABF5-7D2E4E102467}");
107 ok(del == ERROR_SUCCESS, "Expected success \n");
108 }
109
110 /* [Version]Signature+Class+ClassGUID;[ClassInstall32.NT]AddReg */
111 strcat(inf, "AddReg=SampleClassAddReg\n");
112 create_inf_file(inffile, inf);
113 sprintf(path, "%s\\%s", CURR_DIR, inffile);
114
115 SetLastError(0xdeadbeef);
116 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
117 ok(ret, "Expected success\n");
118 ok(!GetLastError(),
119 "Expected no error, got %08x\n", (int)GetLastError());
120 DeleteFileA(inffile);
121 if (ret == TRUE)
122 {
123 RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM", 0, KEY_ALL_ACCESS, &RegHandle);
124 del = RegDeleteKeyW(RegHandle, L"CurrentControlSet\\Control\\Class\\{3B409830-5F9D-432A-ABF5-7D2E4E102467}");
125 ok(del == ERROR_SUCCESS, "Expected success \n");
126 }
127
128 /* [Version]Signature+Class+ClassGUID;[ClassInstall32.NT]AddReg; [SampleClassAddReg];*/
129 strcat(inf, "[SampleClassAddReg]\n");
130 create_inf_file(inffile, inf);
131 sprintf(path, "%s\\%s", CURR_DIR, inffile);
132
133 SetLastError(0xdeadbeef);
134 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
135 ok(ret, "Expected success\n");
136 ok(!GetLastError(),
137 "Expected no error, got %08x\n", (int)GetLastError());
138 DeleteFileA(inffile);
139 if (ret == TRUE)
140 {
141 RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM", 0, KEY_ALL_ACCESS, &RegHandle);
142 del = RegDeleteKeyW(RegHandle, L"CurrentControlSet\\Control\\Class\\{3B409830-5F9D-432A-ABF5-7D2E4E102467}");
143 ok(del == ERROR_SUCCESS, "Expected success \n");
144 }
145
146 /* [Version]Signature+Class+ClassGUID;[ClassInstall32.NT]AddReg; [SampleClassAddReg]HKR;*/
147 strcat(inf, "HKR,,,,\"ReactOS Test SetupDiInstallClassExA\"\n");
148 create_inf_file(inffile, inf);
149 sprintf(path, "%s\\%s", CURR_DIR, inffile);
150
151 SetLastError(0xdeadbeef);
152 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
153 ok(ret, "Expected success\n");
154 ok(!GetLastError(),
155 "Expected no error, got %08x\n", (int)GetLastError());
156 DeleteFileA(inffile);
157 if (ret == TRUE)
158 {
159 RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM", 0, KEY_ALL_ACCESS, &RegHandle);
160 del = RegDeleteKeyW(RegHandle, L"CurrentControlSet\\Control\\Class\\{3B409830-5F9D-432A-ABF5-7D2E4E102467}");
161 ok(del == ERROR_SUCCESS, "Expected success \n");
162 }
163
164 /*[Version]Signature+Class+ClassGUID;[ClassInstall32.NT]AddReg;[SampleClassAddReg]HKR;[ClassInstall32.NT.Services]*/
165 strcat(inf, "[ClassInstall32.NT.Services]\n");
166 create_inf_file(inffile, inf);
167 sprintf(path, "%s\\%s", CURR_DIR, inffile);
168
169 SetLastError(0xdeadbeef);
170 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
171 ok(ret, "Expected success\n");
172 ok(!GetLastError(),
173 "Expected no error, got %08x\n", (int)GetLastError());
174 DeleteFileA(inffile);
175 if (ret == TRUE)
176 {
177 RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM", 0, KEY_ALL_ACCESS, &RegHandle);
178 del = RegDeleteKeyW(RegHandle, L"CurrentControlSet\\Control\\Class\\{3B409830-5F9D-432A-ABF5-7D2E4E102467}");
179 ok(del == ERROR_SUCCESS, "Expected success\n");
180 }
181
182 /* Add a reference */
183 strcat(inf, "AddService=Reactostest,,Reactostest.Service\n");
184 create_inf_file(inffile, inf);
185 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
186 SetLastError(0xdeadbeef);
187 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
188 ok(!ret, "Expected failure\n");
189 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
190 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", (int)GetLastError());
191 SetupCloseInfFile(infhandle);
192 DeleteFileA(inffile);
193
194 /* Add the section */
195 strcat(inf, "[Reactostest.Service]\n");
196 create_inf_file(inffile, inf);
197 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
198 SetLastError(0xdeadbeef);
199 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
200 ok(!ret, "Expected failure\n");
201 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
202 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", (int)GetLastError());
203 SetupCloseInfFile(infhandle);
204 DeleteFileA(inffile);
205
206 /* Just the ServiceBinary */
207 strcat(inf, "ServiceBinary=%12%\\reactostest.sys\n");
208 create_inf_file(inffile, inf);
209 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
210 SetLastError(0xdeadbeef);
211 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
212 ok(!ret, "Expected failure\n");
213 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
214 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", (int)GetLastError());
215 SetupCloseInfFile(infhandle);
216 DeleteFileA(inffile);
217
218 /* Add the ServiceType */
219 strcat(inf, "ServiceType=1\n");
220 create_inf_file(inffile, inf);
221 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
222 SetLastError(0xdeadbeef);
223 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
224 ok(!ret, "Expected failure\n");
225 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
226 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", (int)GetLastError());
227 SetupCloseInfFile(infhandle);
228 DeleteFileA(inffile);
229
230 /* Add the StartType */
231 strcat(inf, "StartType=4\n");
232 create_inf_file(inffile, inf);
233 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
234 SetLastError(0xdeadbeef);
235 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
236 ok(!ret, "Expected failure\n");
237 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
238 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", (int)GetLastError());
239 SetupCloseInfFile(infhandle);
240 DeleteFileA(inffile);
241
242 /* This should be it, the minimal entries to install a service */
243 strcat(inf, "ErrorControl=1");
244 create_inf_file(inffile, inf);
245 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
246 SetLastError(0xdeadbeef);
247 ret = SetupDiInstallClassExA(NULL, path, DI_QUIETINSTALL, NULL, NULL, NULL,NULL);
248 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
249 {
250 skip("Not enough rights to install the service\n");
251 SetupCloseInfFile(infhandle);
252 DeleteFileA(inffile);
253 return;
254 }
255 ok(ret, "Expected success\n");
256 ok(GetLastError() == ERROR_SUCCESS,
257 "Expected ERROR_SUCCESS, got %08x\n", (int)GetLastError());
258 SetupCloseInfFile(infhandle);
259 DeleteFileA(inffile);
260
261 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
262
263 /* Open the service to see if it's really there */
264 svc_handle = OpenServiceA(scm_handle, "Reactostest", DELETE);
265 ok(svc_handle != NULL, "Service was not created\n");
266
267 SetLastError(0xdeadbeef);
268 ret = DeleteService(svc_handle);
269 ok(ret, "Service could not be deleted : %d\n", (int)GetLastError());
270
271 CloseServiceHandle(svc_handle);
272 CloseServiceHandle(scm_handle);
273 }
274
275
276 START_TEST(SetupDiInstallClassExA)
277 {
278 char temp_path[MAX_PATH], prev_path[MAX_PATH];
279 DWORD len;
280
281 GetCurrentDirectoryA(MAX_PATH, prev_path);
282 GetTempPathA(MAX_PATH, temp_path);
283 SetCurrentDirectoryA(temp_path);
284
285 strcpy(CURR_DIR, temp_path);
286 len = strlen(CURR_DIR);
287 if (len && (CURR_DIR[len - 1] == '\\'))
288 CURR_DIR[len - 1] = 0;
289
290 test_SetupDiInstallClassExA();
291 SetCurrentDirectoryA(prev_path);
292
293 }