[SHELL32_APITEST] -Add some tests for SHParseDisplayName for CORE-12882.
[reactos.git] / rostests / apitests / apphelp / layerapi.c
1 /*
2 * Copyright 2015,2016 Mark Jansen
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 <ntstatus.h>
20 #define WIN32_NO_STATUS
21 #include <windows.h>
22 #include <shlwapi.h>
23 #include <winnt.h>
24 #ifdef __REACTOS__
25 #include <ntndk.h>
26 #else
27 #include <winternl.h>
28 #endif
29 #include <winerror.h>
30 #include <stdio.h>
31
32 #include "wine/test.h"
33
34 #include "apphelp_apitest.h"
35
36 #define GPLK_USER 1
37 #define GPLK_MACHINE 2
38 #define MAX_LAYER_LENGTH 256
39 #define LAYER_APPLY_TO_SYSTEM_EXES 1
40
41
42 static HMODULE hdll;
43 static BOOL(WINAPI *pAllowPermLayer)(PCWSTR path);
44 static BOOL(WINAPI *pSdbSetPermLayerKeys)(PCWSTR wszPath, PCWSTR wszLayers, BOOL bMachine);
45 static BOOL(WINAPI *pSdbGetPermLayerKeys)(PCWSTR wszPath, PWSTR pwszLayers, PDWORD pdwBytes, DWORD dwFlags);
46 static BOOL(WINAPI *pSetPermLayerState)(PCWSTR wszPath, PCWSTR wszLayer, DWORD dwFlags, BOOL bMachine, BOOL bEnable);
47
48
49 /* Helper function to disable Wow64 redirection on an os that reports it being enabled. */
50 static DWORD g_QueryFlag = 0xffffffff;
51 static DWORD QueryFlag(void)
52 {
53 if (g_QueryFlag == 0xffffffff)
54 {
55 ULONG_PTR wow64_ptr = 0;
56 NTSTATUS status = NtQueryInformationProcess(NtCurrentProcess(), ProcessWow64Information, &wow64_ptr, sizeof(wow64_ptr), NULL);
57 g_QueryFlag = (NT_SUCCESS(status) && wow64_ptr != 0) ? KEY_WOW64_64KEY : 0;
58 }
59 return g_QueryFlag;
60 }
61
62 /* Helper function to prepare the registry key with a value. */
63 static BOOL setLayerValue(BOOL bMachine, const char* valueName, const char* value)
64 {
65 HKEY key = NULL;
66 LSTATUS lstatus = RegCreateKeyExA(bMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
67 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers", 0, NULL, 0, QueryFlag() | KEY_SET_VALUE, NULL, &key, NULL);
68 if (lstatus == ERROR_SUCCESS)
69 {
70 if (value)
71 lstatus = RegSetValueExA(key, valueName, 0, REG_SZ, (const BYTE*)value, strlen(value)+1);
72 else
73 {
74 lstatus = RegDeleteValueA(key, valueName);
75 lstatus = (lstatus == ERROR_FILE_NOT_FOUND ? ERROR_SUCCESS : lstatus);
76 }
77 RegCloseKey(key);
78 }
79 return lstatus == ERROR_SUCCESS;
80 }
81
82
83 static void expect_LayerValue_imp(BOOL bMachine, const char* valueName, const char* value)
84 {
85 HKEY key = NULL;
86 LSTATUS lstatus = RegCreateKeyExA(bMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
87 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers", 0, NULL, 0, QueryFlag() | KEY_QUERY_VALUE, NULL, &key, NULL);
88 winetest_ok(lstatus == ERROR_SUCCESS, "Expected to be able to open a registry key\n");
89 if (lstatus == ERROR_SUCCESS)
90 {
91 char data[512] = { 0 };
92 DWORD dwType = 0;
93 DWORD dwDataLen = sizeof(data);
94 lstatus = RegQueryValueExA(key, valueName, NULL, &dwType, (LPBYTE)data, &dwDataLen);
95 if (value)
96 {
97 winetest_ok(lstatus == ERROR_SUCCESS, "Expected to get a valid value, err: %u\n", lstatus);
98 if (lstatus == ERROR_SUCCESS)
99 {
100 winetest_ok(dwType == REG_SZ, "Expected the type to be REG_SZ, was: %u\n", dwType);
101 winetest_ok(!strcmp(data, value), "Expected the data to be: '%s', was: '%s'\n", value, data);
102 }
103 }
104 else
105 {
106 winetest_ok(lstatus == ERROR_FILE_NOT_FOUND, "Expected not to find the value %s\n", valueName);
107 }
108 RegCloseKey(key);
109 }
110 }
111
112 static void expect_LayerValue_imp2(BOOL bMachine, const char* valueName, const char* value, int use_alt, const char* alt_value)
113 {
114 expect_LayerValue_imp(bMachine, valueName, use_alt ? alt_value : value);
115 }
116
117
118 void expect_Sdb_imp(PCSTR path, DWORD type, BOOL result, DWORD lenResult, PCSTR stringResult)
119 {
120 WCHAR pathW[MAX_PATH], buffer[MAX_LAYER_LENGTH] = { 0 };
121 char resultBuffer[MAX_LAYER_LENGTH] = { 0 };
122 DWORD dwBufSize = sizeof(buffer);
123
124 /* In case of a failure, the buffer size is sometimes set to 0, and sometimes not touched,
125 depending on the version. Either case is fine, since the function returns FALSE anyway. */
126
127 MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, MAX_PATH);
128
129 winetest_ok(pSdbGetPermLayerKeys(pathW, buffer, &dwBufSize, type) == result, "Expected pSdbGetPermLayerKeys to %s\n", (result ? "succeed" : "fail"));
130 if (!result && lenResult == 0xffffffff)
131 winetest_ok(dwBufSize == 0 || dwBufSize == sizeof(buffer), "Expected dwBufSize to be 0 or %u, was %u\n", sizeof(buffer), dwBufSize);
132 else
133 winetest_ok(dwBufSize == lenResult ||
134 /* W2k3 is off by 2 when concatenating user / machine */
135 broken(g_WinVersion < WINVER_VISTA && type == (GPLK_MACHINE|GPLK_USER) && (lenResult + 2) == dwBufSize),
136 "Expected dwBufSize to be %u, was %u\n", lenResult, dwBufSize);
137 if (result)
138 {
139 winetest_ok(lstrlenW(buffer) * sizeof(WCHAR) + sizeof(WCHAR) == lenResult, "Expected lstrlenW(buffer)*2+2 to be %u, was %u\n",
140 lenResult, lstrlenW(buffer) * sizeof(WCHAR) + sizeof(WCHAR));
141 }
142 WideCharToMultiByte(CP_ACP, 0, buffer, -1, resultBuffer, sizeof(resultBuffer), NULL, NULL);
143 winetest_ok(!strcmp(stringResult, resultBuffer), "Expected the result to be '%s', was '%s'\n", stringResult, resultBuffer);
144
145 if (result)
146 {
147 UNICODE_STRING pathNT;
148
149 if (RtlDosPathNameToNtPathName_U(pathW, &pathNT, NULL, NULL))
150 {
151 memset(buffer, 0, sizeof(buffer));
152 dwBufSize = sizeof(buffer);
153 winetest_ok(pSdbGetPermLayerKeys(pathNT.Buffer, buffer, &dwBufSize, type) == FALSE, "Expected pSdbGetPermLayerKeys to fail for NT path\n");
154
155 RtlFreeUnicodeString(&pathNT);
156 }
157 }
158
159 }
160
161
162 /* In case of a failure, let the location be from where the function was invoked, not inside the function itself. */
163 #define expect_Sdb (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : expect_Sdb_imp
164 #define expect_LayerValue (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : expect_LayerValue_imp
165 #define expect_LayerValue2 (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : expect_LayerValue_imp2
166
167
168 BOOL wrapAllowPermLayer(const char* str)
169 {
170 WCHAR buf[100];
171 MultiByteToWideChar(CP_ACP, 0, str, -1, buf, 100);
172 return pAllowPermLayer(buf);
173 }
174
175 /* Brute forcing all ascii chars in the first 2 places seems to indicate that all it cares for is:
176 - Second char has to be a ':'
177 if it's not a ':', display a diagnostic message (and a different one for '\\').
178 - First char does not really matter, as long as it's not on a DRIVE_REMOTE (but, according to the logging this is meant to check for a CDROM drive...)
179 */
180 static void test_AllowPermLayer(void)
181 {
182 char buf[20];
183 char drive_letter;
184 UINT drivetype = 0;
185 ok(pAllowPermLayer(NULL) == FALSE, "Expected AllowPermLayer to fail for NULL\n");
186 if (g_WinVersion < WINVER_WIN8)
187 {
188 ok(wrapAllowPermLayer("-:"), "Expected AllowPermLayer to succeed\n");
189 ok(wrapAllowPermLayer("@:"), "Expected AllowPermLayer to succeed\n");
190 ok(wrapAllowPermLayer("4:"), "Expected AllowPermLayer to succeed\n");
191 ok(wrapAllowPermLayer("*:"), "Expected AllowPermLayer to succeed\n");
192 }
193 ok(wrapAllowPermLayer("*a") == FALSE, "Expected AllowPermLayer to fail\n");
194 ok(wrapAllowPermLayer("*\\") == FALSE, "Expected AllowPermLayer to fail\n");
195 for (drive_letter = 'a'; drive_letter <= 'z'; ++drive_letter)
196 {
197 sprintf(buf, "%c:\\", drive_letter);
198 drivetype = GetDriveTypeA(buf);
199 ok(wrapAllowPermLayer(buf) == (drivetype != DRIVE_REMOTE), "Expected AllowPermLayer to be %d for %c:\\\n", (drivetype != DRIVE_REMOTE), drive_letter);
200 }
201 }
202
203 static BOOL wrapSdbSetPermLayerKeys(PCWSTR wszPath, PCSTR szLayers, BOOL bMachine)
204 {
205 WCHAR wszLayers[MAX_LAYER_LENGTH];
206 MultiByteToWideChar(CP_ACP, 0, szLayers, -1, wszLayers, MAX_LAYER_LENGTH);
207 return pSdbSetPermLayerKeys(wszPath, wszLayers, bMachine);
208 }
209
210 static void test_SdbSetPermLayerKeysLevel(BOOL bMachine, const char* file)
211 {
212 WCHAR fileW[MAX_PATH+20];
213 WCHAR emptyString[1] = { 0 };
214
215 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, MAX_PATH+20);
216
217 /* Test some parameter validation. */
218 ok(pSdbSetPermLayerKeys(NULL, NULL, bMachine) == FALSE, "Expected SdbSetPermLayerKeys to fail\n");
219 ok(pSdbSetPermLayerKeys(NULL, emptyString, bMachine) == FALSE, "Expected SdbSetPermLayerKeys to fail\n");
220 ok(pSdbSetPermLayerKeys(emptyString, emptyString, bMachine) == FALSE, "Expected SdbSetPermLayerKeys to fail\n");
221 ok(pSdbSetPermLayerKeys(fileW, NULL, bMachine) == TRUE, "Expected SdbSetPermLayerKeys to succeed\n");
222 ok(pSdbSetPermLayerKeys(fileW, emptyString, bMachine) == TRUE, "Expected SdbSetPermLayerKeys to fail\n");
223
224 /* Basic tests */
225 ok(wrapSdbSetPermLayerKeys(fileW, "TEST1", bMachine), "Expected SdbSetPermLayerKeys to succeed\n");
226 expect_LayerValue(bMachine, file, "TEST1");
227
228 ok(wrapSdbSetPermLayerKeys(fileW, "TEST1 TEST2", bMachine), "Expected SdbSetPermLayerKeys to succeed\n");
229 expect_LayerValue(bMachine, file, "TEST1 TEST2");
230
231 /* SdbSetPermLayerKeys does not do any validation of the value passed in. */
232 ok(wrapSdbSetPermLayerKeys(fileW, "!#$% TEST1 TEST2", bMachine), "Expected SdbSetPermLayerKeys to succeed\n");
233 expect_LayerValue(bMachine, file, "!#$% TEST1 TEST2");
234
235 ok(wrapSdbSetPermLayerKeys(fileW, "!#$% TEST1 TEST2", bMachine), "Expected SdbSetPermLayerKeys to succeed\n");
236 expect_LayerValue(bMachine, file, "!#$% TEST1 TEST2");
237
238 ok(pSdbSetPermLayerKeys(fileW, NULL, bMachine) == TRUE, "Expected SdbSetPermLayerKeys to succeed\n");
239 expect_LayerValue(bMachine, file, NULL);
240
241 ok(wrapSdbSetPermLayerKeys(fileW, " ", bMachine), "Expected SdbSetPermLayerKeys to succeed\n");
242 expect_LayerValue(bMachine, file, " ");
243
244 ok(pSdbSetPermLayerKeys(fileW, NULL, bMachine) == TRUE, "Expected SdbSetPermLayerKeys to fail\n");
245 expect_LayerValue(bMachine, file, NULL);
246 }
247
248 static void test_SdbGetPermLayerKeys(void)
249 {
250 WCHAR pathW[MAX_PATH], buffer[MAX_LAYER_LENGTH] = { 0 };
251 char file[MAX_PATH + 20], tmp[MAX_PATH + 20];
252 BOOL bUser, bMachine;
253 HANDLE hfile;
254 DWORD dwBufSize = sizeof(buffer);
255
256 GetTempPathA(MAX_PATH, tmp);
257 GetLongPathNameA(tmp, file, sizeof(file));
258 PathCombineA(tmp, file, "notexist.exe");
259 PathAppendA(file, "test_file.exe");
260
261 /* Check that we can access the keys */
262 bUser = setLayerValue(FALSE, file, "RUNASADMIN WINXPSP3");
263 expect_LayerValue(FALSE, file, "RUNASADMIN WINXPSP3");
264 ok(bUser, "Expected to be able to set atleast the flags for the user\n");
265 if (!bUser)
266 {
267 skip("Cannot do any tests if I cannot set some values\n");
268 return;
269 }
270 bMachine = setLayerValue(TRUE, file, "WINXPSP3 WINXPSP2");
271 if (bMachine)
272 {
273 expect_LayerValue(TRUE, file, "WINXPSP3 WINXPSP2");
274 }
275
276
277 hfile = CreateFileA(file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
278 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile failed on '%s'..\n", file);
279 if (hfile == INVALID_HANDLE_VALUE)
280 {
281 skip("Running these tests is useless without a file present\n");
282 return;
283 }
284 CloseHandle(hfile);
285
286 MultiByteToWideChar(CP_ACP, 0, file, -1, pathW, MAX_PATH);
287
288 /* Parameter validation */
289 ok(pSdbGetPermLayerKeys(NULL, NULL, NULL, 0) == FALSE, "Expected pSdbGetPermLayerKeys to fail\n");
290 ok(pSdbGetPermLayerKeys(pathW, NULL, NULL, 0) == FALSE, "Expected pSdbGetPermLayerKeys to fail\n");
291 ok(pSdbGetPermLayerKeys(pathW, buffer, NULL, 0) == FALSE, "Expected pSdbGetPermLayerKeys to fail\n");
292 ok(pSdbGetPermLayerKeys(pathW, buffer, &dwBufSize, 0) == FALSE, "Expected pSdbGetPermLayerKeys to fail\n");
293 ok(dwBufSize == 0, "Expected dwBufSize to be %u, was %u\n", 0, dwBufSize);
294
295 /* It fails on a nonexisting file */
296 expect_Sdb(tmp, GPLK_USER | GPLK_MACHINE, FALSE, 0xffffffff, "");
297 expect_Sdb(file, GPLK_USER, TRUE, 40, "RUNASADMIN WINXPSP3");
298 GetShortPathNameA(file, tmp, sizeof(tmp));
299 expect_Sdb(tmp, GPLK_USER, TRUE, 40, "RUNASADMIN WINXPSP3");
300
301 if (bMachine)
302 {
303 /* Query from HKLM */
304 expect_Sdb(file, GPLK_MACHINE, TRUE, 36, "WINXPSP3 WINXPSP2");
305 /* Query from both, showing that duplicates are not removed */
306 expect_Sdb(file, GPLK_USER | GPLK_MACHINE, TRUE, 76, "WINXPSP3 WINXPSP2 RUNASADMIN WINXPSP3");
307
308 /* Showing that no validation is done on the value read. */
309 ok(setLayerValue(TRUE, file, "!#!# WINXPSP3 WINXPSP3 !# WINXPSP2 "), "Expected setLayerValue not to fail\n");
310 expect_Sdb(file, GPLK_MACHINE, TRUE, 82, "!#!# WINXPSP3 WINXPSP3 !# WINXPSP2 ");
311 /* Showing that a space is inserted, even if the last char was already a space. */
312 expect_Sdb(file, GPLK_USER | GPLK_MACHINE, TRUE, 122, "!#!# WINXPSP3 WINXPSP3 !# WINXPSP2 RUNASADMIN WINXPSP3");
313 /* Now clear the user key */
314 setLayerValue(FALSE, file, NULL);
315 /* Request both, to show that the last space (from the key) is not cut off. */
316 expect_Sdb(file, GPLK_USER | GPLK_MACHINE, TRUE, 82, "!#!# WINXPSP3 WINXPSP3 !# WINXPSP2 ");
317 setLayerValue(FALSE, file, "RUNASADMIN WINXPSP3");
318 }
319 else
320 {
321 skip("Skipping tests for HKLM, cannot alter the registry\n");
322 }
323 /* Fail from these paths */
324 sprintf(tmp, "\\?\\%s", file);
325 expect_Sdb(tmp, GPLK_USER, FALSE, 0xffffffff, "");
326 sprintf(tmp, "\\??\\%s", file);
327 expect_Sdb(tmp, GPLK_USER, FALSE, 0xffffffff, "");
328
329 ok(setLayerValue(FALSE, file, "!#!# RUNASADMIN RUNASADMIN !# WINXPSP3 "), "Expected setLayerValue not to fail\n");
330 /* There is no validation on information read back. */
331 expect_Sdb(file, GPLK_USER, TRUE, 90, "!#!# RUNASADMIN RUNASADMIN !# WINXPSP3 ");
332
333
334 /* Cleanup */
335 ok(DeleteFileA(file), "DeleteFile failed....\n");
336 setLayerValue(FALSE, file, NULL);
337 setLayerValue(TRUE, file, NULL);
338 }
339
340
341 static BOOL wrapSetPermLayerState(PCWSTR wszPath, PCSTR szLayer, DWORD dwFlags, BOOL bMachine, BOOL bEnable)
342 {
343 WCHAR wszLayer[MAX_LAYER_LENGTH];
344 MultiByteToWideChar(CP_ACP, 0, szLayer, -1, wszLayer, MAX_LAYER_LENGTH);
345 return pSetPermLayerState(wszPath, wszLayer, dwFlags, bMachine, bEnable);
346 }
347
348 static void test_SetPermLayerStateLevel(BOOL bMachine, const char* file)
349 {
350 WCHAR fileW[MAX_PATH+20];
351 WCHAR emptyString[1] = { 0 };
352 DWORD dwFlag;
353
354 MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, MAX_PATH+20);
355
356 /* Test some parameter validation. */
357 ok(pSetPermLayerState(fileW, NULL, 0, bMachine, 0) == FALSE, "Expected SetPermLayerState to fail\n");
358 expect_LayerValue(bMachine, file, NULL);
359
360 ok(pSetPermLayerState(fileW, NULL, 0, bMachine, 1) == FALSE, "Expected SetPermLayerState to fail\n");
361 expect_LayerValue(bMachine, file, NULL);
362
363 ok(wrapSetPermLayerState(fileW, "", 0, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
364 expect_LayerValue(bMachine, file, NULL);
365
366 ok(wrapSetPermLayerState(fileW, "", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
367 expect_LayerValue(bMachine, file, NULL);
368
369 ok(wrapSetPermLayerState(NULL, NULL, 0, bMachine, 0) == FALSE, "Expected SetPermLayerState to fail\n");
370 expect_LayerValue(bMachine, NULL, NULL);
371
372 ok(wrapSetPermLayerState(NULL, NULL, 0, bMachine, 1) == FALSE, "Expected SetPermLayerState to fail\n");
373 expect_LayerValue(bMachine, NULL, NULL);
374
375 ok(wrapSetPermLayerState(emptyString, "", 0, bMachine, 0) == FALSE, "Expected SetPermLayerState to fail\n");
376 expect_LayerValue(bMachine, NULL, NULL);
377
378 ok(wrapSetPermLayerState(emptyString, "", 0, bMachine, 1) == FALSE, "Expected SetPermLayerState to fail\n");
379 expect_LayerValue(bMachine, NULL, NULL);
380
381 ok(wrapSetPermLayerState(emptyString, "TEST", 0, bMachine, 0) == FALSE, "Expected SetPermLayerState to fail\n");
382 expect_LayerValue(bMachine, NULL, NULL);
383
384 if (g_WinVersion <= WINVER_WIN8)
385 {
386 ok(wrapSetPermLayerState(emptyString, "TEST", 0, bMachine, 1) == FALSE, "Expected SetPermLayerState to fail\n");
387 expect_LayerValue(bMachine, NULL, NULL);
388 }
389
390
391 /* Now, on to the actual tests. */
392 expect_LayerValue(bMachine, file, NULL);
393 ok(wrapSetPermLayerState(fileW, "TEST", 0, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
394 expect_LayerValue(bMachine, file, NULL);
395
396 ok(wrapSetPermLayerState(fileW, "TEST", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
397 expect_LayerValue(bMachine, file, "TEST");
398
399 ok(wrapSetPermLayerState(fileW, "", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
400 expect_LayerValue(bMachine, file, "TEST");
401
402 ok(wrapSetPermLayerState(fileW, "test", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
403 expect_LayerValue(bMachine, file, "test");
404
405 ok(wrapSetPermLayerState(fileW, "TEST", 0, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
406 expect_LayerValue(bMachine, file, NULL);
407
408 ok(wrapSetPermLayerState(fileW, "TEST", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
409 expect_LayerValue(bMachine, file, "TEST");
410
411 ok(wrapSetPermLayerState(fileW, "TEST1", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
412 expect_LayerValue2(bMachine, file, "TEST TEST1", g_WinVersion >= WINVER_WIN8, "TEST1 TEST");
413
414 ok(wrapSetPermLayerState(fileW, "TEST2", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
415 expect_LayerValue2(bMachine, file, "TEST TEST1 TEST2", g_WinVersion >= WINVER_WIN8, "TEST2 TEST1 TEST");
416
417 ok(wrapSetPermLayerState(fileW, "TEST1", 0, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
418 expect_LayerValue2(bMachine, file, "TEST TEST2", g_WinVersion >= WINVER_WIN8, "TEST2 TEST");
419
420 ok(wrapSetPermLayerState(fileW, "TEST", 0, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
421 expect_LayerValue(bMachine, file, "TEST2");
422
423 ok(wrapSetPermLayerState(fileW, "TEST2", 0, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
424 expect_LayerValue(bMachine, file, NULL);
425
426 /* Valid flags until win8: !# */
427 /* Key is empty, now play around with the flags. */
428 for (dwFlag = ((g_WinVersion >= WINVER_WIN8) ? 6 : 2); dwFlag < 32; ++dwFlag)
429 {
430 ok(wrapSetPermLayerState(fileW, "TEST", (1<<dwFlag), bMachine, 1) == FALSE, "Expected SetPermLayerState to fail on 0x%x\n", (1<<dwFlag));
431 }
432 expect_LayerValue(bMachine, file, NULL);
433
434 /* Add layer flags */
435 ok(wrapSetPermLayerState(fileW, "TEST", LAYER_APPLY_TO_SYSTEM_EXES, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
436 expect_LayerValue(bMachine, file, "# TEST");
437
438 ok(wrapSetPermLayerState(fileW, "TEST2", 2, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
439 expect_LayerValue2(bMachine, file, "!# TEST TEST2", g_WinVersion >= WINVER_WIN8, "!# TEST2 TEST");
440
441 ok(wrapSetPermLayerState(fileW, "TEST", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
442 expect_LayerValue2(bMachine, file, "!# TEST2 TEST", g_WinVersion >= WINVER_WIN8, "!# TEST TEST2");
443
444 ok(wrapSetPermLayerState(fileW, "TEST3", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
445 expect_LayerValue2(bMachine, file, "!# TEST2 TEST TEST3", g_WinVersion >= WINVER_WIN8, "!# TEST3 TEST TEST2");
446
447 /* Remove on a flag removes that flag from the start. */
448 ok(wrapSetPermLayerState(fileW, "TEST2", 2, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
449 expect_LayerValue2(bMachine, file, "# TEST TEST3", g_WinVersion >= WINVER_WIN8, "# TEST3 TEST");
450
451 ok(wrapSetPermLayerState(fileW, "", LAYER_APPLY_TO_SYSTEM_EXES, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
452 expect_LayerValue2(bMachine, file, "TEST TEST3", g_WinVersion >= WINVER_WIN8, "TEST3 TEST");
453
454 ok(wrapSetPermLayerState(fileW, "", LAYER_APPLY_TO_SYSTEM_EXES | 2, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
455 expect_LayerValue2(bMachine, file, "!# TEST TEST3", g_WinVersion >= WINVER_WIN8, "!# TEST3 TEST");
456
457 ok(wrapSetPermLayerState(fileW, "TEST3", LAYER_APPLY_TO_SYSTEM_EXES, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
458 expect_LayerValue(bMachine, file, "! TEST");
459
460 ok(wrapSetPermLayerState(fileW, "TEST", 2, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
461 expect_LayerValue(bMachine, file, NULL);
462
463 /* Try adding multiple layers: */
464 ok(wrapSetPermLayerState(fileW, "TEST TEST2", LAYER_APPLY_TO_SYSTEM_EXES | 2, bMachine, 1) == FALSE, "Expected SetPermLayerState to fail\n");
465 expect_LayerValue(bMachine, file, NULL);
466
467 ok(wrapSetPermLayerState(fileW, "TEST2", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
468 expect_LayerValue(bMachine, file, "TEST2");
469
470 /* Try adding flags in via layer string */
471 ok(wrapSetPermLayerState(fileW, "#", 0, bMachine, 1) == FALSE, "Expected SetPermLayerState to fail\n");
472 expect_LayerValue(bMachine, file, "TEST2");
473
474 ok(wrapSetPermLayerState(fileW, "!", 0, bMachine, 1) == FALSE, "Expected SetPermLayerState to fail\n");
475 expect_LayerValue(bMachine, file, "TEST2");
476
477 /* Now we prepare the registry with some crap to see how data is validated. */
478 setLayerValue(bMachine, file, "!#!# TEST2 TEST2 !# TEST ");
479
480 ok(wrapSetPermLayerState(fileW, "TEST1", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
481 expect_LayerValue2(bMachine, file, "!# TEST2 TEST2 !# TEST TEST1", g_WinVersion >= WINVER_WIN8, "!# TEST1 TEST2 TEST2 !# TEST");
482
483 /* Removing a duplicate entry will remove all instances of it */
484 ok(wrapSetPermLayerState(fileW, "TEST2", 0, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
485 expect_LayerValue2(bMachine, file, "!# !# TEST TEST1", g_WinVersion >= WINVER_WIN8, "!# TEST1 !# TEST");
486
487 /* Adding a flag cleans other flags (from the start) */
488 ok(wrapSetPermLayerState(fileW, "", LAYER_APPLY_TO_SYSTEM_EXES, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
489 expect_LayerValue2(bMachine, file, "!# TEST TEST1", g_WinVersion >= WINVER_WIN8, "!# TEST1 !# TEST");
490
491 if(g_WinVersion < WINVER_WIN8)
492 {
493 ok(wrapSetPermLayerState(fileW, "$%$%^^", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
494 expect_LayerValue(bMachine, file, "!# TEST TEST1 $%$%^^");
495 }
496
497 setLayerValue(bMachine, file, "!#!# TEST2 !# TEST ");
498 ok(wrapSetPermLayerState(fileW, "", LAYER_APPLY_TO_SYSTEM_EXES, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
499 expect_LayerValue(bMachine, file, "! TEST2 !# TEST");
500
501 /* Tabs are treated as spaces */
502 setLayerValue(bMachine, file, "!#!# TEST2 \t TEST2 !# \t TEST ");
503 ok(wrapSetPermLayerState(fileW, "TEST2", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
504 expect_LayerValue2(bMachine, file, "!# !# TEST TEST2", g_WinVersion >= WINVER_WIN8, "!# TEST2 !# TEST");
505
506 /* Newlines are left as-is */
507 setLayerValue(bMachine, file, "!#!# TEST2 \n TEST2 !# \r\n TEST ");
508 ok(wrapSetPermLayerState(fileW, "TEST2", 0, bMachine, 1) == TRUE, "Expected SetPermLayerState to succeed\n");
509 expect_LayerValue2(bMachine, file, "!# \n !# \r\n TEST TEST2", g_WinVersion >= WINVER_WIN8, "!# TEST2 \n !# \r\n TEST");
510
511 /* Whitespace and duplicate flags are eaten from the start */
512 setLayerValue(bMachine, file, " !#!# TEST2 \t TEST2 !# \t TEST ");
513 ok(wrapSetPermLayerState(fileW, "", LAYER_APPLY_TO_SYSTEM_EXES, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
514 expect_LayerValue(bMachine, file, "! TEST2 TEST2 !# TEST");
515
516 setLayerValue(bMachine, file, "!# !# TEST2 !# TEST ");
517 ok(wrapSetPermLayerState(fileW, "", LAYER_APPLY_TO_SYSTEM_EXES, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
518 expect_LayerValue(bMachine, file, "! TEST2 !# TEST");
519
520 ok(wrapSetPermLayerState(fileW, "", LAYER_APPLY_TO_SYSTEM_EXES, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
521 expect_LayerValue(bMachine, file, "! TEST2 !# TEST");
522
523 ok(wrapSetPermLayerState(fileW, "", 2, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
524 expect_LayerValue(bMachine, file, "TEST2 !# TEST");
525
526 /* First flags are cleaned, then a layer is removed. */
527 ok(wrapSetPermLayerState(fileW, "TEST2", 2, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
528 expect_LayerValue(bMachine, file, "!# TEST");
529
530 /* Nothing is changed, still it succeeds. */
531 ok(wrapSetPermLayerState(fileW, "TEST2", 2, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
532 expect_LayerValue(bMachine, file, "# TEST");
533
534 /* And remove the last bits. */
535 ok(wrapSetPermLayerState(fileW, "TEST", LAYER_APPLY_TO_SYSTEM_EXES, bMachine, 0) == TRUE, "Expected SetPermLayerState to succeed\n");
536 expect_LayerValue(bMachine, file, NULL);
537 }
538
539 static void test_SetPermLayer(void)
540 {
541 char file[MAX_PATH + 20], tmp[MAX_PATH + 20];
542 HANDLE hfile;
543
544 GetTempPathA(MAX_PATH, tmp);
545 GetLongPathNameA(tmp, file, sizeof(file));
546 PathAppendA(file, "test_file.exe");
547
548 hfile = CreateFileA(file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
549 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile failed for '%s'\n", file);
550 if (hfile == INVALID_HANDLE_VALUE)
551 {
552 skip("Running these tests is useless without a file present\n");
553 return;
554 }
555 CloseHandle(hfile);
556
557 if (setLayerValue(FALSE, file, NULL))
558 {
559 test_SdbSetPermLayerKeysLevel(FALSE, file);
560 test_SetPermLayerStateLevel(FALSE, file);
561 }
562 else
563 {
564 skip("Skipping SetPermLayerStateLevel tests for User, because I cannot prepare the environment\n");
565 }
566 if (setLayerValue(TRUE, file, NULL))
567 {
568 test_SdbSetPermLayerKeysLevel(TRUE, file);
569 test_SetPermLayerStateLevel(TRUE, file);
570 }
571 else
572 {
573 skip("Skipping SetPermLayerStateLevel tests for Machine (HKLM), because I cannot prepare the environment\n");
574 }
575 ok(DeleteFileA(file), "DeleteFile failed....\n");
576 }
577
578 static BOOL create_file(LPCSTR dir, LPCSTR name, int filler, size_t size)
579 {
580 char target[MAX_PATH], *tmp;
581 HANDLE file;
582 PathCombineA(target, dir, name);
583
584 tmp = malloc(size);
585 memset(tmp, filler, size);
586
587 file = CreateFileA(target, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
588 if(file == INVALID_HANDLE_VALUE)
589 return FALSE;
590
591 WriteFile(file, tmp, size, &size, NULL);
592 CloseHandle(file);
593 free(tmp);
594 return TRUE;
595 }
596
597 static BOOL delete_file(LPCSTR dir, LPCSTR name)
598 {
599 char target[MAX_PATH];
600 PathCombineA(target, dir, name);
601 return DeleteFileA(target);
602 }
603
604 static char g_FakeDrive = 0;
605
606 UINT (WINAPI *pGetDriveTypeW)(LPCWSTR target) = NULL;
607 UINT WINAPI mGetDriveTypeW(LPCWSTR target)
608 {
609 UINT uRet = pGetDriveTypeW(target);
610 if(g_FakeDrive && target && (char)*target == g_FakeDrive)
611 return DRIVE_CDROM;
612 return uRet;
613 }
614
615
616 static PIMAGE_IMPORT_DESCRIPTOR FindImportDescriptor(PBYTE DllBase, PCSTR DllName)
617 {
618 ULONG Size;
619 PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = RtlImageDirectoryEntryToData((HMODULE)DllBase, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &Size);
620 while (ImportDescriptor->Name && ImportDescriptor->OriginalFirstThunk)
621 {
622 PCHAR Name = (PCHAR)(DllBase + ImportDescriptor->Name);
623 if (!lstrcmpiA(Name, DllName))
624 {
625 return ImportDescriptor;
626 }
627 ImportDescriptor++;
628 }
629 return NULL;
630 }
631
632 static BOOL RedirectIat(PCSTR TargetDllName, PCSTR DllName, PCSTR FunctionName, ULONG_PTR NewFunction, ULONG_PTR* OriginalFunction)
633 {
634 PBYTE DllBase = (PBYTE)GetModuleHandleA(TargetDllName);
635 if (DllBase)
636 {
637 PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = FindImportDescriptor(DllBase, DllName);
638 if (ImportDescriptor)
639 {
640 // On loaded images, OriginalFirstThunk points to the name / ordinal of the function
641 PIMAGE_THUNK_DATA OriginalThunk = (PIMAGE_THUNK_DATA)(DllBase + ImportDescriptor->OriginalFirstThunk);
642 // FirstThunk points to the resolved address.
643 PIMAGE_THUNK_DATA FirstThunk = (PIMAGE_THUNK_DATA)(DllBase + ImportDescriptor->FirstThunk);
644 while (OriginalThunk->u1.AddressOfData && FirstThunk->u1.Function)
645 {
646 if (!IMAGE_SNAP_BY_ORDINAL32(OriginalThunk->u1.AddressOfData))
647 {
648 PIMAGE_IMPORT_BY_NAME ImportName = (PIMAGE_IMPORT_BY_NAME)(DllBase + OriginalThunk->u1.AddressOfData);
649 if (!lstrcmpiA((PCSTR)ImportName->Name, FunctionName))
650 {
651 DWORD dwOld;
652 VirtualProtect(&FirstThunk->u1.Function, sizeof(ULONG_PTR), PAGE_EXECUTE_READWRITE, &dwOld);
653 *OriginalFunction = FirstThunk->u1.Function;
654 FirstThunk->u1.Function = NewFunction;
655 VirtualProtect(&FirstThunk->u1.Function, sizeof(ULONG_PTR), dwOld, &dwOld);
656 return TRUE;
657 }
658 }
659 OriginalThunk++;
660 FirstThunk++;
661 }
662 skip("Unable to find the Import '%s' from '%s' in %s'\n", FunctionName, DllName, TargetDllName);
663 }
664 else
665 {
666 skip("Unable to find the ImportDescriptor for '%s' in '%s'\n", DllName, TargetDllName);
667 }
668 }
669 else
670 {
671 skip("Unable to find the loaded module '%s'\n", TargetDllName);
672 }
673 return FALSE;
674 }
675
676 static BOOL RestoreIat(PCSTR target, PCSTR DllName, PCSTR FunctionName, ULONG_PTR OriginalFunction)
677 {
678 ULONG_PTR old = 0;
679 return RedirectIat(target, DllName, FunctionName, OriginalFunction, &old);
680 }
681
682 static BOOL wrapSdbSetPermLayerKeys2(LPCSTR dir, LPCSTR name, PCSTR szLayers, BOOL bMachine)
683 {
684 char szPath[MAX_PATH];
685 WCHAR wszPath[MAX_PATH], wszLayers[MAX_LAYER_LENGTH];
686 PathCombineA(szPath, dir, name);
687 MultiByteToWideChar(CP_ACP, 0, szLayers, -1, wszLayers, MAX_LAYER_LENGTH);
688 MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);
689 return pSdbSetPermLayerKeys(wszPath, wszLayers, bMachine);
690 }
691
692
693 BOOL expect_files(const char* dir, int num, ...)
694 {
695 char finddir[MAX_PATH + 20];
696 va_list args;
697 WIN32_FIND_DATAA find = { 0 };
698 HANDLE hFind;
699 int cmp = 0;
700
701 va_start(args, num);
702
703 PathCombineA(finddir, dir, "*");
704 hFind = FindFirstFileA(finddir, &find);
705 if (hFind != INVALID_HANDLE_VALUE)
706 {
707 const char* file;
708 do
709 {
710 if (!(find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
711 {
712 if (--num < 0)
713 break;
714 file = va_arg(args, const char*);
715 cmp = strcmp(file, find.cFileName);
716 }
717 } while (cmp == 0 && FindNextFileA(hFind, &find));
718 FindClose(hFind);
719 }
720 va_end(args);
721 return cmp == 0 && num == 0;
722 }
723
724
725 static void test_Sign_Media(void)
726 {
727 char workdir[MAX_PATH], subdir[MAX_PATH], drive[5] = "Z:";
728 BOOL ret;
729
730 DWORD logical_drives = GetLogicalDrives();
731 g_FakeDrive = 0;
732 for (drive[0] = 'D'; drive[0] <= 'Z'; drive[0]++)
733 {
734 DWORD idx = 1 << (drive[0] - 'D' + 3);
735 if (!(logical_drives & idx))
736 {
737 g_FakeDrive = drive[0];
738 break;
739 }
740 }
741 if (!g_FakeDrive)
742 {
743 skip("Unable to find a free drive\n");
744 return;
745 }
746
747 ret = GetTempPathA(MAX_PATH, workdir);
748 ok(ret, "GetTempPathA error: %d\n", GetLastError());
749 PathAppendA(workdir, "apphelp_test");
750
751 ret = CreateDirectoryA(workdir, NULL);
752 ok(ret, "CreateDirectoryA error: %d\n", GetLastError());
753
754 PathCombineA(subdir, workdir, "sub");
755 ret = CreateDirectoryA(subdir, NULL);
756 ok(ret, "CreateDirectoryA error: %d\n", GetLastError());
757
758 ret = DefineDosDeviceA(DDD_NO_BROADCAST_SYSTEM, drive, workdir);
759 ok(ret, "DefineDosDeviceA error: %d\n", GetLastError());
760 if(ret)
761 {
762 ret = RedirectIat("apphelp.dll", "kernel32.dll", "GetDriveTypeW", (ULONG_PTR)mGetDriveTypeW, (ULONG_PTR*)&pGetDriveTypeW);
763 if (g_WinVersion < WINVER_WIN8)
764 ok(ret, "Expected redirect_iat to succeed\n");
765 if(ret)
766 {
767 ok(create_file(workdir, "test.exe", 'a', 4), "create_file error: %d\n", GetLastError());
768
769 ok(wrapSdbSetPermLayerKeys2(drive, "test.exe", "TEST", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
770 /* 4 */
771 /* test.exe */
772 expect_LayerValue(0, "SIGN.MEDIA=4 test.exe", "TEST");
773 ok(wrapSdbSetPermLayerKeys2(drive, "test.exe", "", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
774
775 ok(create_file(workdir, "test.txt", 'a', 1), "create_file error: %d\n", GetLastError());
776
777 if (!expect_files(workdir, 2, "test.exe", "test.txt"))
778 {
779 skip("Skipping test, files are not returned in the expected order by the FS\n");
780 }
781 else
782 {
783 ok(wrapSdbSetPermLayerKeys2(drive, "test.exe", "TEST", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
784 /* (4 << 1) ^ 1 */
785 /* test.exe test.txt */
786 expect_LayerValue(0, "SIGN.MEDIA=9 test.exe", "TEST");
787 ok(wrapSdbSetPermLayerKeys2(drive, "test.exe", "", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
788 }
789
790 ok(create_file(workdir, "test.zz", 'a', 0x1000), "create_file error: %d\n", GetLastError());
791
792 if (!expect_files(workdir, 3, "test.exe", "test.txt", "test.zz"))
793 {
794 skip("Skipping test, files are not returned in the expected order by the FS\n");
795 }
796 else
797 {
798 ok(wrapSdbSetPermLayerKeys2(drive, "test.exe", "TEST", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
799 /* (((4 << 1) ^ 1) << 1) ^ 0x1000 */
800 /* test.exe test.txt test.zz */
801 expect_LayerValue(0, "SIGN.MEDIA=1012 test.exe", "TEST");
802 ok(wrapSdbSetPermLayerKeys2(drive, "test.exe", "", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
803 }
804
805 ok(create_file(subdir, "test.exe", 'a', 0x10203), "create_file error: %d\n", GetLastError());
806
807 if (!expect_files(subdir, 1, "test.exe"))
808 {
809 skip("Skipping test, files are not returned in the expected order by the FS\n");
810 }
811 else
812 {
813 ok(wrapSdbSetPermLayerKeys2(drive, "sub\\test.exe", "TEST", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
814 /* 0x10203 */
815 /* test.exe */
816 expect_LayerValue(0, "SIGN.MEDIA=10203 sub\\test.exe", "TEST");
817 ok(wrapSdbSetPermLayerKeys2(drive, "sub\\test.exe", "", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
818 }
819
820 ok(create_file(subdir, "test.bbb", 'a', 0), "create_file error: %d\n", GetLastError());
821
822 if (!expect_files(subdir, 2, "test.bbb", "test.exe"))
823 {
824 skip("Skipping test, files are not returned in the expected order by the FS\n");
825 }
826 else
827 {
828 ok(wrapSdbSetPermLayerKeys2(drive, "sub\\test.exe", "TEST", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
829 /* 0x10203 */
830 /* test.exe */
831 expect_LayerValue(0, "SIGN.MEDIA=10203 sub\\test.exe", "TEST");
832 ok(wrapSdbSetPermLayerKeys2(drive, "sub\\test.exe", "", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
833 }
834
835 ok(create_file(subdir, "TEST.txt", 'a', 0x30201), "create_file error: %d\n", GetLastError());
836
837 if (!expect_files(subdir, 3, "test.bbb", "test.exe", "TEST.txt"))
838 {
839 skip("Skipping test, files are not returned in the expected order by the FS\n");
840 }
841 else
842 {
843 ok(wrapSdbSetPermLayerKeys2(drive, "sub\\test.exe", "TEST", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
844 /* (0x10203 << 1) ^ 0x30201 */
845 /* test.exe TEST.txt */
846 expect_LayerValue(0, "SIGN.MEDIA=10607 sub\\test.exe", "TEST");
847 ok(wrapSdbSetPermLayerKeys2(drive, "sub\\test.exe", "", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
848 }
849
850 ok(create_file(subdir, "TEST.aaa", 'a', 0x3a2a1), "create_file error: %d\n", GetLastError());
851
852 if (!expect_files(subdir, 4, "TEST.aaa", "test.bbb", "test.exe", "TEST.txt"))
853 {
854 skip("Skipping test, files are not returned in the expected order by the FS\n");
855 }
856 else
857 {
858 ok(wrapSdbSetPermLayerKeys2(drive, "sub\\test.exe", "TEST", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
859 /* (((0x3a2a1 << 1) ^ 0x10203) << 1) ^ 0x30201 */
860 /* TEST.aaa test.exe TEST.txt */
861 expect_LayerValue(0, "SIGN.MEDIA=F8C83 sub\\test.exe", "TEST");
862 ok(wrapSdbSetPermLayerKeys2(drive, "sub\\test.exe", "", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n");
863 }
864
865 ret = RestoreIat("apphelp.dll", "kernel32.dll", "GetDriveTypeW", (ULONG_PTR)pGetDriveTypeW);
866 ok(ret, "Expected restore_iat to succeed\n");
867
868 ok(delete_file(subdir, "test.bbb"), "delete_file error: %d\n", GetLastError());
869 ok(delete_file(subdir, "TEST.aaa"), "delete_file error: %d\n", GetLastError());
870 ok(delete_file(subdir, "TEST.txt"), "delete_file error: %d\n", GetLastError());
871 ok(delete_file(subdir, "test.exe"), "delete_file error: %d\n", GetLastError());
872 ok(delete_file(workdir, "test.zz"), "delete_file error: %d\n", GetLastError());
873 ok(delete_file(workdir, "test.txt"), "delete_file error: %d\n", GetLastError());
874 ok(delete_file(workdir, "test.exe"), "delete_file error: %d\n", GetLastError());
875 }
876 ret = DefineDosDeviceA(DDD_REMOVE_DEFINITION | DDD_NO_BROADCAST_SYSTEM, drive, NULL);
877 ok(ret, "DefineDosDeviceA error: %d\n", GetLastError());
878 }
879 ret = RemoveDirectoryA(subdir);
880 ok(ret, "RemoveDirectoryA error: %d\n", GetLastError());
881 ret = RemoveDirectoryA(workdir);
882 ok(ret, "RemoveDirectoryA error: %d\n", GetLastError());
883 }
884
885
886 START_TEST(layerapi)
887 {
888 silence_debug_output();
889 /*SetEnvironmentVariable("SHIM_DEBUG_LEVEL", "4");*/
890 hdll = LoadLibraryA("apphelp.dll");
891 pAllowPermLayer = (void *)GetProcAddress(hdll, "AllowPermLayer");
892 pSdbSetPermLayerKeys = (void *)GetProcAddress(hdll, "SdbSetPermLayerKeys");
893 pSdbGetPermLayerKeys = (void *)GetProcAddress(hdll, "SdbGetPermLayerKeys");
894 pSetPermLayerState = (void *)GetProcAddress(hdll, "SetPermLayerState");
895 g_WinVersion = get_host_winver();
896
897 if (!pAllowPermLayer)
898 {
899 skip("Skipping tests with AllowPermLayer, function not found\n");
900 }
901 else
902 {
903 test_AllowPermLayer();
904 }
905
906 if (!pSdbSetPermLayerKeys)
907 {
908 skip("Skipping tests with SdbSetPermLayerKeys, function not found\n");
909 }
910 else
911 {
912 if (!pSdbGetPermLayerKeys)
913 {
914 skip("Skipping tests with SdbGetPermLayerKeys, function not found\n");
915 }
916 else
917 {
918 test_SdbGetPermLayerKeys();
919 }
920
921 if (!pSetPermLayerState)
922 {
923 skip("Skipping tests with SetPermLayerState, function not found\n");
924 }
925 else
926 {
927 test_SetPermLayer();
928 test_Sign_Media();
929 }
930 }
931 }