kernel32/tests: Fix a test for real hardware.
[wine] / dlls / kernel32 / tests / actctx.c
1 /*
2  * Copyright 2007 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 "wine/test.h"
20 #include <winbase.h>
21 #include <windef.h>
22 #include <winnt.h>
23 #include <winternl.h>
24 #include <winnls.h>
25 #include <stdio.h>
26
27 static BOOL   (WINAPI *pActivateActCtx)(HANDLE,ULONG_PTR*);
28 static HANDLE (WINAPI *pCreateActCtxW)(PCACTCTXW);
29 static BOOL   (WINAPI *pDeactivateActCtx)(DWORD,ULONG_PTR);
30 static BOOL   (WINAPI *pFindActCtxSectionStringW)(DWORD,const GUID *,ULONG,LPCWSTR,PACTCTX_SECTION_KEYED_DATA);
31 static BOOL   (WINAPI *pGetCurrentActCtx)(HANDLE *);
32 static BOOL   (WINAPI *pIsDebuggerPresent)(void);
33 static BOOL   (WINAPI *pQueryActCtxW)(DWORD,HANDLE,PVOID,ULONG,PVOID,SIZE_T,SIZE_T*);
34 static VOID   (WINAPI *pReleaseActCtx)(HANDLE);
35
36 static const char* strw(LPCWSTR x)
37 {
38     static char buffer[1024];
39     char*       p = buffer;
40
41     if (!x) return "(nil)";
42     else while ((*p++ = *x++));
43     return buffer;
44 }
45
46 #ifdef __i386__
47 #define ARCH "x86"
48 #elif defined __x86_64__
49 #define ARCH "amd64"
50 #else
51 #define ARCH "none"
52 #endif
53
54 static const char manifest1[] =
55 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
56 "<assemblyIdentity version=\"1.0.0.0\"  name=\"Wine.Test\" type=\"win32\"></assemblyIdentity>"
57 "</assembly>";
58
59 static const char manifest2[] =
60 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
61 "<assemblyIdentity version=\"1.2.3.4\" name=\"Wine.Test\" type=\"win32\">"
62 "</assemblyIdentity>"
63 "<dependency>"
64 "<dependentAssembly>"
65 "<assemblyIdentity type=\"win32\" name=\"testdep\" version=\"6.5.4.3\" processorArchitecture=\"" ARCH "\">"
66 "</assemblyIdentity>"
67 "</dependentAssembly>"
68 "</dependency>"
69 "</assembly>";
70
71 static const char manifest3[] =
72 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
73 "<assemblyIdentity version=\"1.2.3.4\"  name=\"Wine.Test\" type=\"win32\""
74 " publicKeyToken=\"6595b6414666f1df\" />"
75 "<file name=\"testlib.dll\">"
76 "<windowClass>wndClass</windowClass>"
77 "</file>"
78 "</assembly>";
79
80 static const char manifest4[] =
81 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
82 "<assemblyIdentity version=\"1.2.3.4\" name=\"Wine.Test\" type=\"win32\">"
83 "</assemblyIdentity>"
84 "<dependency>"
85 "<dependentAssembly>"
86 "<assemblyIdentity type=\"win32\" name=\"Microsoft.Windows.Common-Controls\" "
87     "version=\"6.0.1.0\" processorArchitecture=\"" ARCH "\" publicKeyToken=\"6595b64144ccf1df\">"
88 "</assemblyIdentity>"
89 "</dependentAssembly>"
90 "</dependency>"
91 "</assembly>";
92
93 static const char testdep_manifest1[] =
94 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
95 "<assemblyIdentity type=\"win32\" name=\"testdep\" version=\"6.5.4.3\" processorArchitecture=\"" ARCH "\"/>"
96 "</assembly>";
97
98 static const char testdep_manifest2[] =
99 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
100 "<assemblyIdentity type=\"win32\" name=\"testdep\" version=\"6.5.4.3\" processorArchitecture=\"" ARCH "\" />"
101 "<file name=\"testlib.dll\"></file>"
102 "<file name=\"testlib2.dll\" hash=\"63c978c2b53d6cf72b42fb7308f9af12ab19ec53\" hashalg=\"SHA1\" />"
103 "</assembly>";
104
105 static const char testdep_manifest3[] =
106 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\"> "
107 "<assemblyIdentity type=\"win32\" name=\"testdep\" version=\"6.5.4.3\" processorArchitecture=\"" ARCH "\"/>"
108 "<file name=\"testlib.dll\"/>"
109 "<file name=\"testlib2.dll\" hash=\"63c978c2b53d6cf72b42fb7308f9af12ab19ec53\" hashalg=\"SHA1\">"
110 "<windowClass>wndClass</windowClass>"
111 "<windowClass>wndClass2</windowClass>"
112 "</file>"
113 "</assembly>";
114
115 static const char wrong_manifest1[] =
116 "<assembly manifestVersion=\"1.0\">"
117 "<assemblyIdentity version=\"1.0.0.0\"  name=\"Wine.Test\" type=\"win32\"></assemblyIdentity>"
118 "</assembly>";
119
120 static const char wrong_manifest2[] =
121 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\">"
122 "<assemblyIdentity version=\"1.0.0.0\"  name=\"Wine.Test\" type=\"win32\"></assemblyIdentity>"
123 "</assembly>";
124
125 static const char wrong_manifest3[] =
126 "<assembly test=\"test\" xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
127 "<assemblyIdentity version=\"1.0.0.0\"  name=\"Wine.Test\" type=\"win32\"></assemblyIdentity>"
128 "</assembly>";
129
130 static const char wrong_manifest4[] =
131 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
132 "<assemblyIdentity version=\"1.0.0.0\"  name=\"Wine.Test\" type=\"win32\"></assemblyIdentity>"
133 "<test></test>"
134 "</assembly>";
135
136 static const char wrong_manifest5[] =
137 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
138 "<assemblyIdentity version=\"1.0.0.0\"  name=\"Wine.Test\" type=\"win32\"></assemblyIdentity>"
139 "</assembly>"
140 "<test></test>";
141
142 static const char wrong_manifest6[] =
143 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v5\" manifestVersion=\"1.0\">"
144 "<assemblyIdentity version=\"1.0.0.0\"  name=\"Wine.Test\" type=\"win32\"></assemblyIdentity>"
145 "</assembly>";
146
147 static const char wrong_manifest7[] =
148 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
149 "<assemblyIdentity type=\"win32\" name=\"testdep\" version=\"6.5.4.3\" processorArchitecture=\"" ARCH "\" />"
150 "<file name=\"testlib.dll\" hash=\"63c978c2b53d6cf72b42fb7308f9af12ab19ec5\" hashalg=\"SHA1\" />"
151 "</assembly>";
152
153 static const char wrong_manifest8[] =
154 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
155 "<assemblyIdentity version=\"1.2.3.4\"  name=\"Wine.Test\" type=\"win32\"></assemblyIdentity>"
156 "<file></file>"
157 "</assembly>";
158
159 static const char wrong_depmanifest1[] =
160 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
161 "<assemblyIdentity type=\"win32\" name=\"testdep\" version=\"6.5.4.4\" processorArchitecture=\"" ARCH "\" />"
162 "</assembly>";
163
164 static const WCHAR testlib_dll[] =
165     {'t','e','s','t','l','i','b','.','d','l','l',0};
166 static const WCHAR testlib2_dll[] =
167     {'t','e','s','t','l','i','b','2','.','d','l','l',0};
168 static const WCHAR wndClassW[] =
169     {'w','n','d','C','l','a','s','s',0};
170 static const WCHAR wndClass2W[] =
171     {'w','n','d','C','l','a','s','s','2',0};
172 static const WCHAR acr_manifest[] =
173     {'a','c','r','.','m','a','n','i','f','e','s','t',0};
174
175 static WCHAR app_dir[MAX_PATH], exe_path[MAX_PATH], work_dir[MAX_PATH], work_dir_subdir[MAX_PATH];
176 static WCHAR app_manifest_path[MAX_PATH], manifest_path[MAX_PATH], depmanifest_path[MAX_PATH];
177
178 static int strcmp_aw(LPCWSTR strw, const char *stra)
179 {
180     WCHAR buf[1024];
181
182     if (!stra) return 1;
183     MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
184     return lstrcmpW(strw, buf);
185 }
186
187 static DWORD strlen_aw(const char *str)
188 {
189     return MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0) - 1;
190 }
191
192 static BOOL create_manifest_file(const char *filename, const char *manifest, int manifest_len,
193                                  const char *depfile, const char *depmanifest)
194 {
195     DWORD size;
196     HANDLE file;
197     WCHAR path[MAX_PATH];
198
199     MultiByteToWideChar( CP_ACP, 0, filename, -1, path, MAX_PATH );
200     GetFullPathNameW(path, sizeof(manifest_path)/sizeof(WCHAR), manifest_path, NULL);
201
202     if (manifest_len == -1)
203         manifest_len = strlen(manifest);
204
205     file = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
206                        FILE_ATTRIBUTE_NORMAL, NULL);
207     ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %u\n", GetLastError());
208     if(file == INVALID_HANDLE_VALUE)
209         return FALSE;
210     WriteFile(file, manifest, manifest_len, &size, NULL);
211     CloseHandle(file);
212
213     if (depmanifest)
214     {
215         MultiByteToWideChar( CP_ACP, 0, depfile, -1, path, MAX_PATH );
216         GetFullPathNameW(path, sizeof(depmanifest_path)/sizeof(WCHAR), depmanifest_path, NULL);
217         file = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
218                            FILE_ATTRIBUTE_NORMAL, NULL);
219         ok(file != INVALID_HANDLE_VALUE, "CreateFile failed: %u\n", GetLastError());
220         if(file == INVALID_HANDLE_VALUE)
221             return FALSE;
222         WriteFile(file, depmanifest, strlen(depmanifest), &size, NULL);
223         CloseHandle(file);
224     }
225     return TRUE;
226 }
227
228 static BOOL create_wide_manifest(const char *filename, const char *manifest, BOOL fBOM, BOOL fReverse)
229 {
230     WCHAR *wmanifest = HeapAlloc(GetProcessHeap(), 0, (strlen(manifest)+2) * sizeof(WCHAR));
231     BOOL ret;
232     int offset = (fBOM ? 0 : 1);
233
234     MultiByteToWideChar(CP_ACP, 0, manifest, -1, &wmanifest[1], (strlen(manifest)+1));
235     wmanifest[0] = 0xfeff;
236     if (fReverse)
237     {
238         size_t i;
239         for (i = 0; i < strlen(manifest)+1; i++)
240             wmanifest[i] = (wmanifest[i] << 8) | ((wmanifest[i] >> 8) & 0xff);
241     }
242     ret = create_manifest_file(filename, (char *)&wmanifest[offset], (strlen(manifest)+1-offset) * sizeof(WCHAR), NULL, NULL);
243     HeapFree(GetProcessHeap(), 0, wmanifest);
244     return ret;
245 }
246
247 typedef struct {
248     ULONG format_version;
249     ULONG assembly_cnt_min;
250     ULONG assembly_cnt_max;
251     ULONG root_manifest_type;
252     LPWSTR root_manifest_path;
253     ULONG root_config_type;
254     ULONG app_dir_type;
255     LPCWSTR app_dir;
256 } detailed_info_t;
257
258 static const detailed_info_t detailed_info0 = {
259     0, 0, 0, 0, NULL, 0, 0, NULL
260 };
261
262 static const detailed_info_t detailed_info1 = {
263     1, 1, 1, ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE, manifest_path,
264     ACTIVATION_CONTEXT_PATH_TYPE_NONE, ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE,
265     work_dir,
266 };
267
268 static const detailed_info_t detailed_info1_child = {
269     1, 1, 1, ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE, app_manifest_path,
270     ACTIVATION_CONTEXT_PATH_TYPE_NONE, ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE,
271     app_dir,
272 };
273
274 /* On Vista+, there's an extra assembly for Microsoft.Windows.Common-Controls.Resources */
275 static const detailed_info_t detailed_info2 = {
276     1, 2, 3, ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE, manifest_path,
277     ACTIVATION_CONTEXT_PATH_TYPE_NONE, ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE,
278     work_dir,
279 };
280
281 static void test_detailed_info(HANDLE handle, const detailed_info_t *exinfo, int line)
282 {
283     ACTIVATION_CONTEXT_DETAILED_INFORMATION detailed_info_tmp, *detailed_info;
284     SIZE_T size, exsize, retsize;
285     BOOL b;
286
287     exsize = sizeof(ACTIVATION_CONTEXT_DETAILED_INFORMATION)
288         + (exinfo->root_manifest_path ? (lstrlenW(exinfo->root_manifest_path)+1)*sizeof(WCHAR):0)
289         + (exinfo->app_dir ? (lstrlenW(exinfo->app_dir)+1)*sizeof(WCHAR) : 0);
290
291     if(exsize != sizeof(ACTIVATION_CONTEXT_DETAILED_INFORMATION)) {
292         size = 0xdeadbeef;
293         b = pQueryActCtxW(0, handle, NULL,
294                           ActivationContextDetailedInformation, &detailed_info_tmp,
295                           sizeof(detailed_info_tmp), &size);
296         ok_(__FILE__, line)(!b, "QueryActCtx succeeded\n");
297         ok_(__FILE__, line)(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() = %u\n", GetLastError());
298         ok_(__FILE__, line)(size == exsize, "size=%ld, expected %ld\n", size, exsize);
299     }else {
300         size = sizeof(ACTIVATION_CONTEXT_DETAILED_INFORMATION);
301     }
302
303     detailed_info = HeapAlloc(GetProcessHeap(), 0, size);
304     memset(detailed_info, 0xfe, size);
305     b = pQueryActCtxW(0, handle, NULL,
306                       ActivationContextDetailedInformation, detailed_info,
307                       size, &retsize);
308     ok_(__FILE__, line)(b, "QueryActCtx failed: %u\n", GetLastError());
309     ok_(__FILE__, line)(retsize == exsize, "size=%ld, expected %ld\n", retsize, exsize);
310
311     ok_(__FILE__, line)(detailed_info->dwFlags == 0, "detailed_info->dwFlags=%x\n", detailed_info->dwFlags);
312     ok_(__FILE__, line)(detailed_info->ulFormatVersion == exinfo->format_version,
313        "detailed_info->ulFormatVersion=%u, expected %u\n", detailed_info->ulFormatVersion,
314        exinfo->format_version);
315     ok_(__FILE__, line)(exinfo->assembly_cnt_min <= detailed_info->ulAssemblyCount &&
316        detailed_info->ulAssemblyCount <= exinfo->assembly_cnt_max,
317        "detailed_info->ulAssemblyCount=%u, expected between %u and %u\n", detailed_info->ulAssemblyCount,
318        exinfo->assembly_cnt_min, exinfo->assembly_cnt_max);
319     ok_(__FILE__, line)(detailed_info->ulRootManifestPathType == exinfo->root_manifest_type,
320        "detailed_info->ulRootManifestPathType=%u, expected %u\n",
321        detailed_info->ulRootManifestPathType, exinfo->root_manifest_type);
322     ok_(__FILE__, line)(detailed_info->ulRootManifestPathChars ==
323        (exinfo->root_manifest_path ? lstrlenW(exinfo->root_manifest_path) : 0),
324        "detailed_info->ulRootManifestPathChars=%u, expected %u\n",
325        detailed_info->ulRootManifestPathChars,
326        exinfo->root_manifest_path ?lstrlenW(exinfo->root_manifest_path) : 0);
327     ok_(__FILE__, line)(detailed_info->ulRootConfigurationPathType == exinfo->root_config_type,
328        "detailed_info->ulRootConfigurationPathType=%u, expected %u\n",
329        detailed_info->ulRootConfigurationPathType, exinfo->root_config_type);
330     ok_(__FILE__, line)(detailed_info->ulRootConfigurationPathChars == 0,
331        "detailed_info->ulRootConfigurationPathChars=%d\n", detailed_info->ulRootConfigurationPathChars);
332     ok_(__FILE__, line)(detailed_info->ulAppDirPathType == exinfo->app_dir_type,
333        "detailed_info->ulAppDirPathType=%u, expected %u\n", detailed_info->ulAppDirPathType,
334        exinfo->app_dir_type);
335     ok_(__FILE__, line)(detailed_info->ulAppDirPathChars == (exinfo->app_dir ? lstrlenW(exinfo->app_dir) : 0),
336        "detailed_info->ulAppDirPathChars=%u, expected %u\n",
337        detailed_info->ulAppDirPathChars, exinfo->app_dir ? lstrlenW(exinfo->app_dir) : 0);
338     if(exinfo->root_manifest_path) {
339         ok_(__FILE__, line)(detailed_info->lpRootManifestPath != NULL, "detailed_info->lpRootManifestPath == NULL\n");
340         if(detailed_info->lpRootManifestPath)
341             ok_(__FILE__, line)(!lstrcmpiW(detailed_info->lpRootManifestPath, exinfo->root_manifest_path),
342                "unexpected detailed_info->lpRootManifestPath\n");
343     }else {
344         ok_(__FILE__, line)(detailed_info->lpRootManifestPath == NULL, "detailed_info->lpRootManifestPath != NULL\n");
345     }
346     ok_(__FILE__, line)(detailed_info->lpRootConfigurationPath == NULL,
347        "detailed_info->lpRootConfigurationPath=%p\n", detailed_info->lpRootConfigurationPath);
348     if(exinfo->app_dir) {
349         ok_(__FILE__, line)(detailed_info->lpAppDirPath != NULL, "detailed_info->lpAppDirPath == NULL\n");
350         if(detailed_info->lpAppDirPath)
351             ok_(__FILE__, line)(!lstrcmpiW(exinfo->app_dir, detailed_info->lpAppDirPath),
352                "unexpected detailed_info->lpAppDirPath\n%s\n",strw(detailed_info->lpAppDirPath));
353     }else {
354         ok_(__FILE__, line)(detailed_info->lpAppDirPath == NULL, "detailed_info->lpAppDirPath != NULL\n");
355     }
356
357     HeapFree(GetProcessHeap(), 0, detailed_info);
358 }
359
360 typedef struct {
361     ULONG flags;
362 /*    ULONG manifest_path_type; FIXME */
363     LPCWSTR manifest_path;
364     LPCSTR encoded_assembly_id;
365     BOOL has_assembly_dir;
366 } info_in_assembly;
367
368 static const info_in_assembly manifest1_info = {
369     1, manifest_path,
370     "Wine.Test,type=\"win32\",version=\"1.0.0.0\"",
371     FALSE
372 };
373
374 static const info_in_assembly manifest1_child_info = {
375     1, app_manifest_path,
376     "Wine.Test,type=\"win32\",version=\"1.0.0.0\"",
377     FALSE
378 };
379
380 static const info_in_assembly manifest2_info = {
381     1, manifest_path,
382     "Wine.Test,type=\"win32\",version=\"1.2.3.4\"",
383     FALSE
384 };
385
386 static const info_in_assembly manifest3_info = {
387     1, manifest_path,
388     "Wine.Test,publicKeyToken=\"6595b6414666f1df\",type=\"win32\",version=\"1.2.3.4\"",
389     FALSE
390 };
391
392 static const info_in_assembly manifest4_info = {
393     1, manifest_path,
394     "Wine.Test,type=\"win32\",version=\"1.2.3.4\"",
395     FALSE
396 };
397
398 static const info_in_assembly depmanifest1_info = {
399     0x10, depmanifest_path,
400     "testdep,processorArchitecture=\"" ARCH "\","
401     "type=\"win32\",version=\"6.5.4.3\"",
402     TRUE
403 };
404
405 static const info_in_assembly depmanifest2_info = {
406     0x10, depmanifest_path,
407     "testdep,processorArchitecture=\"" ARCH "\","
408     "type=\"win32\",version=\"6.5.4.3\"",
409     TRUE
410 };
411
412 static const info_in_assembly depmanifest3_info = {
413     0x10, depmanifest_path,
414     "testdep,processorArchitecture=\"" ARCH "\",type=\"win32\",version=\"6.5.4.3\"",
415     TRUE
416 };
417
418 static const info_in_assembly manifest_comctrl_info = {
419     0, NULL, NULL, TRUE /* These values may differ between Windows installations */
420 };
421
422 static void test_info_in_assembly(HANDLE handle, DWORD id, const info_in_assembly *exinfo, int line)
423 {
424     ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION *info, info_tmp;
425     SIZE_T size, exsize;
426     ULONG len;
427     BOOL b;
428
429     exsize = sizeof(ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION);
430     if (exinfo->manifest_path) exsize += (lstrlenW(exinfo->manifest_path)+1) * sizeof(WCHAR);
431     if (exinfo->encoded_assembly_id) exsize += (strlen_aw(exinfo->encoded_assembly_id) + 1) * sizeof(WCHAR);
432
433     size = 0xdeadbeef;
434     b = pQueryActCtxW(0, handle, &id,
435                       AssemblyDetailedInformationInActivationContext, &info_tmp,
436                       sizeof(info_tmp), &size);
437     ok_(__FILE__, line)(!b, "QueryActCtx succeeded\n");
438     ok_(__FILE__, line)(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() = %u\n", GetLastError());
439
440     ok_(__FILE__, line)(size >= exsize, "size=%lu, expected %lu\n", size, exsize);
441
442     if (size == 0xdeadbeef)
443     {
444         skip("bad size\n");
445         return;
446     }
447
448     info = HeapAlloc(GetProcessHeap(), 0, size);
449     memset(info, 0xfe, size);
450
451     size = 0xdeadbeef;
452     b = pQueryActCtxW(0, handle, &id,
453                       AssemblyDetailedInformationInActivationContext, info, size, &size);
454     ok_(__FILE__, line)(b, "QueryActCtx failed: %u\n", GetLastError());
455     if (!exinfo->manifest_path)
456         exsize += info->ulManifestPathLength + sizeof(WCHAR);
457     if (!exinfo->encoded_assembly_id)
458         exsize += info->ulEncodedAssemblyIdentityLength + sizeof(WCHAR);
459     if (exinfo->has_assembly_dir)
460         exsize += info->ulAssemblyDirectoryNameLength + sizeof(WCHAR);
461     ok_(__FILE__, line)(size == exsize, "size=%lu, expected %lu\n", size, exsize);
462
463     if (0)  /* FIXME: flags meaning unknown */
464     {
465         ok_(__FILE__, line)((info->ulFlags) == exinfo->flags, "info->ulFlags = %x, expected %x\n",
466            info->ulFlags, exinfo->flags);
467     }
468     if(exinfo->encoded_assembly_id) {
469         len = strlen_aw(exinfo->encoded_assembly_id)*sizeof(WCHAR);
470         ok_(__FILE__, line)(info->ulEncodedAssemblyIdentityLength == len,
471            "info->ulEncodedAssemblyIdentityLength = %u, expected %u\n",
472            info->ulEncodedAssemblyIdentityLength, len);
473     } else {
474         ok_(__FILE__, line)(info->ulEncodedAssemblyIdentityLength != 0,
475            "info->ulEncodedAssemblyIdentityLength == 0\n");
476     }
477     ok_(__FILE__, line)(info->ulManifestPathType == ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE,
478        "info->ulManifestPathType = %x\n", info->ulManifestPathType);
479     if(exinfo->manifest_path) {
480         len = lstrlenW(exinfo->manifest_path)*sizeof(WCHAR);
481         ok_(__FILE__, line)(info->ulManifestPathLength == len, "info->ulManifestPathLength = %u, expected %u\n",
482            info->ulManifestPathLength, len);
483     } else {
484         ok_(__FILE__, line)(info->ulManifestPathLength != 0, "info->ulManifestPathLength == 0\n");
485     }
486
487     ok_(__FILE__, line)(info->ulPolicyPathType == ACTIVATION_CONTEXT_PATH_TYPE_NONE,
488        "info->ulPolicyPathType = %x\n", info->ulPolicyPathType);
489     ok_(__FILE__, line)(info->ulPolicyPathLength == 0,
490        "info->ulPolicyPathLength = %u, expected 0\n", info->ulPolicyPathLength);
491     ok_(__FILE__, line)(info->ulMetadataSatelliteRosterIndex == 0, "info->ulMetadataSatelliteRosterIndex = %x\n",
492        info->ulMetadataSatelliteRosterIndex);
493     ok_(__FILE__, line)(info->ulManifestVersionMajor == 1,"info->ulManifestVersionMajor = %x\n",
494        info->ulManifestVersionMajor);
495     ok_(__FILE__, line)(info->ulManifestVersionMinor == 0, "info->ulManifestVersionMinor = %x\n",
496        info->ulManifestVersionMinor);
497     ok_(__FILE__, line)(info->ulPolicyVersionMajor == 0, "info->ulPolicyVersionMajor = %x\n",
498        info->ulPolicyVersionMajor);
499     ok_(__FILE__, line)(info->ulPolicyVersionMinor == 0, "info->ulPolicyVersionMinor = %x\n",
500        info->ulPolicyVersionMinor);
501     if(exinfo->has_assembly_dir)
502         ok_(__FILE__, line)(info->ulAssemblyDirectoryNameLength != 0,
503            "info->ulAssemblyDirectoryNameLength == 0\n");
504     else
505         ok_(__FILE__, line)(info->ulAssemblyDirectoryNameLength == 0,
506            "info->ulAssemblyDirectoryNameLength != 0\n");
507
508     ok_(__FILE__, line)(info->lpAssemblyEncodedAssemblyIdentity != NULL,
509        "info->lpAssemblyEncodedAssemblyIdentity == NULL\n");
510     if(info->lpAssemblyEncodedAssemblyIdentity && exinfo->encoded_assembly_id) {
511         ok_(__FILE__, line)(!strcmp_aw(info->lpAssemblyEncodedAssemblyIdentity, exinfo->encoded_assembly_id),
512            "unexpected info->lpAssemblyEncodedAssemblyIdentity %s / %s\n",
513            strw(info->lpAssemblyEncodedAssemblyIdentity), exinfo->encoded_assembly_id);
514     }
515     if(exinfo->manifest_path) {
516         ok_(__FILE__, line)(info->lpAssemblyManifestPath != NULL, "info->lpAssemblyManifestPath == NULL\n");
517         if(info->lpAssemblyManifestPath)
518             ok_(__FILE__, line)(!lstrcmpiW(info->lpAssemblyManifestPath, exinfo->manifest_path),
519                "unexpected info->lpAssemblyManifestPath\n");
520     }else {
521         ok_(__FILE__, line)(info->lpAssemblyManifestPath != NULL, "info->lpAssemblyManifestPath == NULL\n");
522     }
523
524     ok_(__FILE__, line)(info->lpAssemblyPolicyPath == NULL, "info->lpAssemblyPolicyPath != NULL\n");
525     if(info->lpAssemblyPolicyPath)
526         ok_(__FILE__, line)(*(WORD*)info->lpAssemblyPolicyPath == 0, "info->lpAssemblyPolicyPath is not empty\n");
527     if(exinfo->has_assembly_dir)
528         ok_(__FILE__, line)(info->lpAssemblyDirectoryName != NULL, "info->lpAssemblyDirectoryName == NULL\n");
529     else
530         ok_(__FILE__, line)(info->lpAssemblyDirectoryName == NULL, "info->lpAssemblyDirectoryName = %s\n",
531            strw(info->lpAssemblyDirectoryName));
532     HeapFree(GetProcessHeap(), 0, info);
533 }
534
535 static void test_file_info(HANDLE handle, ULONG assid, ULONG fileid, LPCWSTR filename, int line)
536 {
537     ASSEMBLY_FILE_DETAILED_INFORMATION *info, info_tmp;
538     ACTIVATION_CONTEXT_QUERY_INDEX index = {assid, fileid};
539     SIZE_T size, exsize;
540     BOOL b;
541
542     exsize = sizeof(ASSEMBLY_FILE_DETAILED_INFORMATION)
543         +(lstrlenW(filename)+1)*sizeof(WCHAR);
544
545     size = 0xdeadbeef;
546     b = pQueryActCtxW(0, handle, &index,
547                       FileInformationInAssemblyOfAssemblyInActivationContext, &info_tmp,
548                       sizeof(info_tmp), &size);
549     ok_(__FILE__, line)(!b, "QueryActCtx succeeded\n");
550     ok_(__FILE__, line)(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() = %u\n", GetLastError());
551     ok_(__FILE__, line)(size == exsize, "size=%lu, expected %lu\n", size, exsize);
552
553     if(size == 0xdeadbeef)
554     {
555         skip("bad size\n");
556         return;
557     }
558
559     info = HeapAlloc(GetProcessHeap(), 0, size);
560     memset(info, 0xfe, size);
561
562     b = pQueryActCtxW(0, handle, &index,
563                       FileInformationInAssemblyOfAssemblyInActivationContext, info, size, &size);
564     ok_(__FILE__, line)(b, "QueryActCtx failed: %u\n", GetLastError());
565     ok_(__FILE__, line)(!size, "size=%lu, expected 0\n", size);
566
567     ok_(__FILE__, line)(info->ulFlags == 2, "info->ulFlags=%x, expected 2\n", info->ulFlags);
568     ok_(__FILE__, line)(info->ulFilenameLength == lstrlenW(filename)*sizeof(WCHAR),
569        "info->ulFilenameLength=%u, expected %u*sizeof(WCHAR)\n",
570        info->ulFilenameLength, lstrlenW(filename));
571     ok_(__FILE__, line)(info->ulPathLength == 0, "info->ulPathLength=%u\n", info->ulPathLength);
572     ok_(__FILE__, line)(info->lpFileName != NULL, "info->lpFileName == NULL\n");
573     if(info->lpFileName)
574         ok_(__FILE__, line)(!lstrcmpiW(info->lpFileName, filename), "unexpected info->lpFileName\n");
575     ok_(__FILE__, line)(info->lpFilePath == NULL, "info->lpFilePath != NULL\n");
576     HeapFree(GetProcessHeap(), 0, info);
577 }
578
579 static HANDLE test_create(const char *file, const char *manifest)
580 {
581     ACTCTXW actctx;
582     HANDLE handle;
583     WCHAR path[MAX_PATH];
584
585     MultiByteToWideChar( CP_ACP, 0, file, -1, path, MAX_PATH );
586     memset(&actctx, 0, sizeof(ACTCTXW));
587     actctx.cbSize = sizeof(ACTCTXW);
588     actctx.lpSource = path;
589
590     handle = pCreateActCtxW(&actctx);
591     ok(handle != INVALID_HANDLE_VALUE, "handle == INVALID_HANDLE_VALUE, error %u\n", GetLastError());
592
593     ok(actctx.cbSize == sizeof(actctx), "actctx.cbSize=%d\n", actctx.cbSize);
594     ok(actctx.dwFlags == 0, "actctx.=%d\n", actctx.dwFlags);
595     ok(actctx.lpSource == path, "actctx.lpSource=%p\n", actctx.lpSource);
596     ok(actctx.wProcessorArchitecture == 0,
597        "actctx.wProcessorArchitecture=%d\n", actctx.wProcessorArchitecture);
598     ok(actctx.wLangId == 0, "actctx.wLangId=%d\n", actctx.wLangId);
599     ok(actctx.lpAssemblyDirectory == NULL,
600        "actctx.lpAssemblyDirectory=%p\n", actctx.lpAssemblyDirectory);
601     ok(actctx.lpResourceName == NULL, "actctx.lpResourceName=%p\n", actctx.lpResourceName);
602     ok(actctx.lpApplicationName == NULL, "actctx.lpApplocationName=%p\n",
603        actctx.lpApplicationName);
604     ok(actctx.hModule == NULL, "actctx.hModule=%p\n", actctx.hModule);
605
606     return handle;
607 }
608
609 static void test_create_and_fail(const char *manifest, const char *depmanifest, int todo)
610 {
611     ACTCTXW actctx;
612     HANDLE handle;
613     WCHAR path[MAX_PATH];
614
615     MultiByteToWideChar( CP_ACP, 0, "bad.manifest", -1, path, MAX_PATH );
616     memset(&actctx, 0, sizeof(ACTCTXW));
617     actctx.cbSize = sizeof(ACTCTXW);
618     actctx.lpSource = path;
619
620     create_manifest_file("bad.manifest", manifest, -1, "testdep.manifest", depmanifest);
621     handle = pCreateActCtxW(&actctx);
622     if (todo) todo_wine
623     {
624         ok(handle == INVALID_HANDLE_VALUE, "handle != INVALID_HANDLE_VALUE\n");
625         ok(GetLastError() == ERROR_SXS_CANT_GEN_ACTCTX, "GetLastError == %u\n", GetLastError());
626     }
627     else
628     {
629         ok(handle == INVALID_HANDLE_VALUE, "handle != INVALID_HANDLE_VALUE\n");
630         ok(GetLastError() == ERROR_SXS_CANT_GEN_ACTCTX, "GetLastError == %u\n", GetLastError());
631     }
632     if (handle != INVALID_HANDLE_VALUE) pReleaseActCtx( handle );
633     DeleteFileA("bad.manifest");
634     DeleteFileA("testdep.manifest");
635 }
636
637 static void test_create_wide_and_fail(const char *manifest, BOOL fBOM)
638 {
639     ACTCTXW actctx;
640     HANDLE handle;
641     WCHAR path[MAX_PATH];
642
643     MultiByteToWideChar( CP_ACP, 0, "bad.manifest", -1, path, MAX_PATH );
644     memset(&actctx, 0, sizeof(ACTCTXW));
645     actctx.cbSize = sizeof(ACTCTXW);
646     actctx.lpSource = path;
647
648     create_wide_manifest("bad.manifest", manifest, fBOM, FALSE);
649     handle = pCreateActCtxW(&actctx);
650     ok(handle == INVALID_HANDLE_VALUE, "handle != INVALID_HANDLE_VALUE\n");
651     ok(GetLastError() == ERROR_SXS_CANT_GEN_ACTCTX, "GetLastError == %u\n", GetLastError());
652
653     if (handle != INVALID_HANDLE_VALUE) pReleaseActCtx( handle );
654     DeleteFileA("bad.manifest");
655 }
656
657 static void test_create_fail(void)
658 {
659     ACTCTXW actctx;
660     HANDLE handle;
661     WCHAR path[MAX_PATH];
662
663     MultiByteToWideChar( CP_ACP, 0, "nonexistent.manifest", -1, path, MAX_PATH );
664     memset(&actctx, 0, sizeof(ACTCTXW));
665     actctx.cbSize = sizeof(ACTCTXW);
666     actctx.lpSource = path;
667
668     handle = pCreateActCtxW(&actctx);
669     ok(handle == INVALID_HANDLE_VALUE, "handle != INVALID_HANDLE_VALUE\n");
670     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError == %u\n", GetLastError());
671
672     trace("wrong_manifest1\n");
673     test_create_and_fail(wrong_manifest1, NULL, 0 );
674     trace("wrong_manifest2\n");
675     test_create_and_fail(wrong_manifest2, NULL, 0 );
676     trace("wrong_manifest3\n");
677     test_create_and_fail(wrong_manifest3, NULL, 1 );
678     trace("wrong_manifest4\n");
679     test_create_and_fail(wrong_manifest4, NULL, 1 );
680     trace("wrong_manifest5\n");
681     test_create_and_fail(wrong_manifest5, NULL, 0 );
682     trace("wrong_manifest6\n");
683     test_create_and_fail(wrong_manifest6, NULL, 0 );
684     trace("wrong_manifest7\n");
685     test_create_and_fail(wrong_manifest7, NULL, 1 );
686     trace("wrong_manifest8\n");
687     test_create_and_fail(wrong_manifest8, NULL, 0 );
688     trace("UTF-16 manifest1 without BOM\n");
689     test_create_wide_and_fail(manifest1, FALSE );
690     trace("manifest2\n");
691     test_create_and_fail(manifest2, NULL, 0 );
692     trace("manifest2+depmanifest1\n");
693     test_create_and_fail(manifest2, wrong_depmanifest1, 0 );
694 }
695
696 static void test_find_dll_redirection(HANDLE handle, LPCWSTR libname, ULONG exid, int line)
697 {
698     ACTCTX_SECTION_KEYED_DATA data;
699     DWORD *p;
700     BOOL ret;
701
702     memset(&data, 0xfe, sizeof(data));
703     data.cbSize = sizeof(data);
704
705     ret = pFindActCtxSectionStringW(0, NULL,
706                                     ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION,
707                                     libname, &data);
708     ok_(__FILE__, line)(ret, "FindActCtxSectionStringW failed: %u\n", GetLastError());
709     if(!ret)
710     {
711         skip("couldn't find %s\n",strw(libname));
712         return;
713     }
714
715     ok_(__FILE__, line)(data.cbSize == sizeof(data), "data.cbSize=%u\n", data.cbSize);
716     ok_(__FILE__, line)(data.ulDataFormatVersion == 1, "data.ulDataFormatVersion=%u\n", data.ulDataFormatVersion);
717     ok_(__FILE__, line)(data.lpData != NULL, "data.lpData == NULL\n");
718     ok_(__FILE__, line)(data.ulLength == 20, "data.ulLength=%u\n", data.ulLength);
719
720     p = data.lpData;
721     if(ret && p) todo_wine {
722         ok_(__FILE__, line)(p[0] == 20 && p[1] == 2 && p[2] == 0 && p[3] == 0 && p[4] == 0,
723            "wrong data %u,%u,%u,%u,%u\n",p[0], p[1], p[2], p[3], p[4]);
724     }
725
726     ok_(__FILE__, line)(data.lpSectionGlobalData == NULL, "data.lpSectionGlobalData != NULL\n");
727     ok_(__FILE__, line)(data.ulSectionGlobalDataLength == 0, "data.ulSectionGlobalDataLength=%u\n",
728        data.ulSectionGlobalDataLength);
729     ok_(__FILE__, line)(data.lpSectionBase != NULL, "data.lpSectionBase == NULL\n");
730     /* ok_(__FILE__, line)(data.ulSectionTotalLength == ??, "data.ulSectionTotalLength=%u\n",
731        data.ulSectionTotalLength); */
732     ok_(__FILE__, line)(data.hActCtx == NULL, "data.hActCtx=%p\n", data.hActCtx);
733     ok_(__FILE__, line)(data.ulAssemblyRosterIndex == exid, "data.ulAssemblyRosterIndex=%u, expected %u\n",
734        data.ulAssemblyRosterIndex, exid);
735
736     memset(&data, 0xfe, sizeof(data));
737     data.cbSize = sizeof(data);
738
739     ret = pFindActCtxSectionStringW(FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX, NULL,
740                                     ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION,
741                                     libname, &data);
742     ok_(__FILE__, line)(ret, "FindActCtxSectionStringW failed: %u\n", GetLastError());
743     if(!ret)
744     {
745         skip("couldn't find\n");
746         return;
747     }
748
749     ok_(__FILE__, line)(data.cbSize == sizeof(data), "data.cbSize=%u\n", data.cbSize);
750     ok_(__FILE__, line)(data.ulDataFormatVersion == 1, "data.ulDataFormatVersion=%u\n", data.ulDataFormatVersion);
751     ok_(__FILE__, line)(data.lpData != NULL, "data.lpData == NULL\n");
752     ok_(__FILE__, line)(data.ulLength == 20, "data.ulLength=%u\n", data.ulLength);
753     ok_(__FILE__, line)(data.lpSectionGlobalData == NULL, "data.lpSectionGlobalData != NULL\n");
754     ok_(__FILE__, line)(data.ulSectionGlobalDataLength == 0, "data.ulSectionGlobalDataLength=%u\n",
755        data.ulSectionGlobalDataLength);
756     ok_(__FILE__, line)(data.lpSectionBase != NULL, "data.lpSectionBase == NULL\n");
757     /* ok_(__FILE__, line)(data.ulSectionTotalLength == ?? , "data.ulSectionTotalLength=%u\n",
758        data.ulSectionTotalLength); */
759     ok_(__FILE__, line)(data.hActCtx == handle, "data.hActCtx=%p\n", data.hActCtx);
760     ok_(__FILE__, line)(data.ulAssemblyRosterIndex == exid, "data.ulAssemblyRosterIndex=%u, expected %u\n",
761        data.ulAssemblyRosterIndex, exid);
762
763     pReleaseActCtx(handle);
764 }
765
766 static void test_find_window_class(HANDLE handle, LPCWSTR clsname, ULONG exid, int line)
767 {
768     ACTCTX_SECTION_KEYED_DATA data;
769     BOOL ret;
770
771     memset(&data, 0xfe, sizeof(data));
772     data.cbSize = sizeof(data);
773
774     ret = pFindActCtxSectionStringW(0, NULL,
775                                     ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION,
776                                     clsname, &data);
777     ok_(__FILE__, line)(ret, "FindActCtxSectionStringW failed: %u\n", GetLastError());
778     if(!ret)
779     {
780         skip("couldn't find\n");
781         return;
782     }
783
784     ok_(__FILE__, line)(data.cbSize == sizeof(data), "data.cbSize=%u\n", data.cbSize);
785     ok_(__FILE__, line)(data.ulDataFormatVersion == 1, "data.ulDataFormatVersion=%u\n", data.ulDataFormatVersion);
786     ok_(__FILE__, line)(data.lpData != NULL, "data.lpData == NULL\n");
787     /* ok_(__FILE__, line)(data.ulLength == ??, "data.ulLength=%u\n", data.ulLength); */
788     ok_(__FILE__, line)(data.lpSectionGlobalData == NULL, "data.lpSectionGlobalData != NULL\n");
789     ok_(__FILE__, line)(data.ulSectionGlobalDataLength == 0, "data.ulSectionGlobalDataLength=%u\n",
790        data.ulSectionGlobalDataLength);
791     ok_(__FILE__, line)(data.lpSectionBase != NULL, "data.lpSectionBase == NULL\n");
792     /* ok_(__FILE__, line)(data.ulSectionTotalLength == 0, "data.ulSectionTotalLength=%u\n",
793        data.ulSectionTotalLength); FIXME */
794     ok_(__FILE__, line)(data.hActCtx == NULL, "data.hActCtx=%p\n", data.hActCtx);
795     ok_(__FILE__, line)(data.ulAssemblyRosterIndex == exid, "data.ulAssemblyRosterIndex=%u, expected %u\n",
796        data.ulAssemblyRosterIndex, exid);
797
798     memset(&data, 0xfe, sizeof(data));
799     data.cbSize = sizeof(data);
800
801     ret = pFindActCtxSectionStringW(FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX, NULL,
802                                     ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION,
803                                     clsname, &data);
804     ok_(__FILE__, line)(ret, "FindActCtxSectionStringW failed: %u\n", GetLastError());
805     if(!ret)
806     {
807         skip("couldn't find\n");
808         return;
809     }
810
811     ok_(__FILE__, line)(data.cbSize == sizeof(data), "data.cbSize=%u\n", data.cbSize);
812     ok_(__FILE__, line)(data.ulDataFormatVersion == 1, "data.ulDataFormatVersion=%u\n", data.ulDataFormatVersion);
813     ok_(__FILE__, line)(data.lpData != NULL, "data.lpData == NULL\n");
814     /* ok_(__FILE__, line)(data.ulLength == ??, "data.ulLength=%u\n", data.ulLength); FIXME */
815     ok_(__FILE__, line)(data.lpSectionGlobalData == NULL, "data.lpSectionGlobalData != NULL\n");
816     ok_(__FILE__, line)(data.ulSectionGlobalDataLength == 0, "data.ulSectionGlobalDataLength=%u\n",
817        data.ulSectionGlobalDataLength);
818     ok_(__FILE__, line)(data.lpSectionBase != NULL, "data.lpSectionBase == NULL\n");
819     /* ok_(__FILE__, line)(data.ulSectionTotalLength == 0, "data.ulSectionTotalLength=%u\n",
820        data.ulSectionTotalLength); FIXME */
821     ok_(__FILE__, line)(data.hActCtx == handle, "data.hActCtx=%p\n", data.hActCtx);
822     ok_(__FILE__, line)(data.ulAssemblyRosterIndex == exid, "data.ulAssemblyRosterIndex=%u, expected %u\n",
823        data.ulAssemblyRosterIndex, exid);
824
825     pReleaseActCtx(handle);
826 }
827
828 static void test_find_string_fail(void)
829 {
830     ACTCTX_SECTION_KEYED_DATA data = {sizeof(data)};
831     BOOL ret;
832
833     ret = pFindActCtxSectionStringW(0, NULL, 100, testlib_dll, &data);
834     ok(!ret, "FindActCtxSectionStringW succeeded\n");
835     ok(GetLastError() == ERROR_SXS_SECTION_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
836
837     ret = pFindActCtxSectionStringW(0, NULL, ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION,
838                                     testlib2_dll, &data);
839     ok(!ret, "FindActCtxSectionStringW succeeded\n");
840     ok(GetLastError() == ERROR_SXS_KEY_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
841
842     ret = pFindActCtxSectionStringW(0, NULL, ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION,
843                                     testlib_dll, NULL);
844     ok(!ret, "FindActCtxSectionStringW succeeded\n");
845     ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError()=%u\n", GetLastError());
846
847     ret = pFindActCtxSectionStringW(0, NULL, ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION,
848                                     NULL, &data);
849     ok(!ret, "FindActCtxSectionStringW succeeded\n");
850     ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError()=%u\n", GetLastError());
851
852     data.cbSize = 0;
853     ret = pFindActCtxSectionStringW(0, NULL, ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION,
854                                     testlib_dll, &data);
855     ok(!ret, "FindActCtxSectionStringW succeeded\n");
856     ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError()=%u\n", GetLastError());
857
858     data.cbSize = 35;
859     ret = pFindActCtxSectionStringW(0, NULL, ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION,
860                                     testlib_dll, &data);
861     ok(!ret, "FindActCtxSectionStringW succeeded\n");
862     ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError()=%u\n", GetLastError());
863 }
864
865
866 static void test_basic_info(HANDLE handle, int line)
867 {
868     ACTIVATION_CONTEXT_BASIC_INFORMATION basic;
869     SIZE_T size;
870     BOOL b;
871
872     b = pQueryActCtxW(QUERY_ACTCTX_FLAG_NO_ADDREF, handle, NULL,
873                           ActivationContextBasicInformation, &basic,
874                           sizeof(basic), &size);
875
876     ok_(__FILE__, line) (b,"ActivationContextBasicInformation failed\n");
877     ok_(__FILE__, line) (size == sizeof(ACTIVATION_CONTEXT_BASIC_INFORMATION),"size mismatch\n");
878     ok_(__FILE__, line) (basic.dwFlags == 0, "unexpected flags %x\n",basic.dwFlags);
879     ok_(__FILE__, line) (basic.hActCtx == handle, "unexpected handle\n");
880
881     b = pQueryActCtxW(QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX |
882                       QUERY_ACTCTX_FLAG_NO_ADDREF, handle, NULL,
883                           ActivationContextBasicInformation, &basic,
884                           sizeof(basic), &size);
885     if (handle)
886     {
887         ok_(__FILE__, line) (!b,"ActivationContextBasicInformation succeeded\n");
888         ok_(__FILE__, line) (size == 0,"size mismatch\n");
889         ok_(__FILE__, line) (GetLastError() == ERROR_INVALID_PARAMETER, "Wrong last error\n");
890         ok_(__FILE__, line) (basic.dwFlags == 0, "unexpected flags %x\n",basic.dwFlags);
891         ok_(__FILE__, line) (basic.hActCtx == handle, "unexpected handle\n");
892     }
893     else
894     {
895         ok_(__FILE__, line) (b,"ActivationContextBasicInformation failed\n");
896         ok_(__FILE__, line) (size == sizeof(ACTIVATION_CONTEXT_BASIC_INFORMATION),"size mismatch\n");
897         ok_(__FILE__, line) (basic.dwFlags == 0, "unexpected flags %x\n",basic.dwFlags);
898         ok_(__FILE__, line) (basic.hActCtx == handle, "unexpected handle\n");
899     }
900 }
901
902 static void test_actctx(void)
903 {
904     ULONG_PTR cookie;
905     HANDLE handle;
906     BOOL b;
907
908     test_create_fail();
909
910     trace("default actctx\n");
911
912     b = pGetCurrentActCtx(&handle);
913     ok(handle == NULL, "handle = %p, expected NULL\n", handle);
914     ok(b, "GetCurrentActCtx failed: %u\n", GetLastError());
915     if(b) {
916         test_basic_info(handle, __LINE__);
917         test_detailed_info(handle, &detailed_info0, __LINE__);
918         pReleaseActCtx(handle);
919     }
920
921     if(!create_manifest_file("test1.manifest", manifest1, -1, NULL, NULL)) {
922         skip("Could not create manifest file\n");
923         return;
924     }
925
926     trace("manifest1\n");
927
928     handle = test_create("test1.manifest", manifest1);
929     DeleteFileA("test1.manifest");
930     if(handle != INVALID_HANDLE_VALUE) {
931         test_basic_info(handle, __LINE__);
932         test_detailed_info(handle, &detailed_info1, __LINE__);
933         test_info_in_assembly(handle, 1, &manifest1_info, __LINE__);
934
935         if (pIsDebuggerPresent && !pIsDebuggerPresent())
936         {
937             /* CloseHandle will generate an exception if a debugger is present */
938             b = CloseHandle(handle);
939             ok(!b, "CloseHandle succeeded\n");
940             ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError() == %u\n", GetLastError());
941         }
942
943         pReleaseActCtx(handle);
944     }
945
946     if(!create_manifest_file("test2.manifest", manifest2, -1, "testdep.manifest", testdep_manifest1)) {
947         skip("Could not create manifest file\n");
948         return;
949     }
950
951     trace("manifest2 depmanifest1\n");
952
953     handle = test_create("test2.manifest", manifest2);
954     DeleteFileA("test2.manifest");
955     DeleteFileA("testdep.manifest");
956     if(handle != INVALID_HANDLE_VALUE) {
957         test_basic_info(handle, __LINE__);
958         test_detailed_info(handle, &detailed_info2, __LINE__);
959         test_info_in_assembly(handle, 1, &manifest2_info, __LINE__);
960         test_info_in_assembly(handle, 2, &depmanifest1_info, __LINE__);
961         pReleaseActCtx(handle);
962     }
963
964     if(!create_manifest_file("test2-2.manifest", manifest2, -1, "testdep.manifest", testdep_manifest2)) {
965         skip("Could not create manifest file\n");
966         return;
967     }
968
969     trace("manifest2 depmanifest2\n");
970
971     handle = test_create("test2-2.manifest", manifest2);
972     DeleteFileA("test2-2.manifest");
973     DeleteFileA("testdep.manifest");
974     if(handle != INVALID_HANDLE_VALUE) {
975         test_basic_info(handle, __LINE__);
976         test_detailed_info(handle, &detailed_info2, __LINE__);
977         test_info_in_assembly(handle, 1, &manifest2_info, __LINE__);
978         test_info_in_assembly(handle, 2, &depmanifest2_info, __LINE__);
979         test_file_info(handle, 1, 0, testlib_dll, __LINE__);
980         test_file_info(handle, 1, 1, testlib2_dll, __LINE__);
981
982         b = pActivateActCtx(handle, &cookie);
983         ok(b, "ActivateActCtx failed: %u\n", GetLastError());
984         test_find_dll_redirection(handle, testlib_dll, 2, __LINE__);
985         test_find_dll_redirection(handle, testlib2_dll, 2, __LINE__);
986         b = pDeactivateActCtx(0, cookie);
987         ok(b, "DeactivateActCtx failed: %u\n", GetLastError());
988
989         pReleaseActCtx(handle);
990     }
991
992     trace("manifest2 depmanifest3\n");
993
994     if(!create_manifest_file("test2-3.manifest", manifest2, -1, "testdep.manifest", testdep_manifest3)) {
995         skip("Could not create manifest file\n");
996         return;
997     }
998
999     handle = test_create("test2-3.manifest", manifest2);
1000     DeleteFileA("test2-3.manifest");
1001     DeleteFileA("testdep.manifest");
1002     if(handle != INVALID_HANDLE_VALUE) {
1003         test_basic_info(handle, __LINE__);
1004         test_detailed_info(handle, &detailed_info2, __LINE__);
1005         test_info_in_assembly(handle, 1, &manifest2_info, __LINE__);
1006         test_info_in_assembly(handle, 2, &depmanifest3_info, __LINE__);
1007         test_file_info(handle, 1, 0, testlib_dll, __LINE__);
1008         test_file_info(handle, 1, 1, testlib2_dll, __LINE__);
1009
1010         b = pActivateActCtx(handle, &cookie);
1011         ok(b, "ActivateActCtx failed: %u\n", GetLastError());
1012         test_find_dll_redirection(handle, testlib_dll, 2, __LINE__);
1013         test_find_dll_redirection(handle, testlib2_dll, 2, __LINE__);
1014         test_find_window_class(handle, wndClassW, 2, __LINE__);
1015         test_find_window_class(handle, wndClass2W, 2, __LINE__);
1016         b = pDeactivateActCtx(0, cookie);
1017         ok(b, "DeactivateActCtx failed: %u\n", GetLastError());
1018
1019         pReleaseActCtx(handle);
1020     }
1021
1022     trace("manifest3\n");
1023
1024     if(!create_manifest_file("test3.manifest", manifest3, -1, NULL, NULL)) {
1025         skip("Could not create manifest file\n");
1026         return;
1027     }
1028
1029     handle = test_create("test3.manifest", manifest3);
1030     DeleteFileA("test3.manifest");
1031     if(handle != INVALID_HANDLE_VALUE) {
1032         test_basic_info(handle, __LINE__);
1033         test_detailed_info(handle, &detailed_info1, __LINE__);
1034         test_info_in_assembly(handle, 1, &manifest3_info, __LINE__);
1035         test_file_info(handle, 0, 0, testlib_dll, __LINE__);
1036
1037         b = pActivateActCtx(handle, &cookie);
1038         ok(b, "ActivateActCtx failed: %u\n", GetLastError());
1039         test_find_dll_redirection(handle, testlib_dll, 1, __LINE__);
1040         test_find_dll_redirection(handle, testlib_dll, 1, __LINE__);
1041         test_find_string_fail();
1042         b = pDeactivateActCtx(0, cookie);
1043         ok(b, "DeactivateActCtx failed: %u\n", GetLastError());
1044
1045         pReleaseActCtx(handle);
1046     }
1047
1048     trace("manifest4\n");
1049
1050     if(!create_manifest_file("test4.manifest", manifest4, -1, NULL, NULL)) {
1051         skip("Could not create manifest file\n");
1052         return;
1053     }
1054
1055     handle = test_create("test4.manifest", manifest4);
1056     DeleteFileA("test4.manifest");
1057     DeleteFileA("testdep.manifest");
1058     if(handle != INVALID_HANDLE_VALUE) {
1059         test_basic_info(handle, __LINE__);
1060         test_detailed_info(handle, &detailed_info2, __LINE__);
1061         test_info_in_assembly(handle, 1, &manifest4_info, __LINE__);
1062         test_info_in_assembly(handle, 2, &manifest_comctrl_info, __LINE__);
1063         pReleaseActCtx(handle);
1064     }
1065
1066     trace("manifest1 in subdir\n");
1067
1068     CreateDirectoryW(work_dir_subdir, NULL);
1069     if (SetCurrentDirectoryW(work_dir_subdir))
1070     {
1071         if(!create_manifest_file("..\\test1.manifest", manifest1, -1, NULL, NULL)) {
1072             skip("Could not create manifest file\n");
1073             return;
1074         }
1075         handle = test_create("..\\test1.manifest", manifest1);
1076         DeleteFileA("..\\test1.manifest");
1077         if(handle != INVALID_HANDLE_VALUE) {
1078             test_basic_info(handle, __LINE__);
1079             test_detailed_info(handle, &detailed_info1, __LINE__);
1080             test_info_in_assembly(handle, 1, &manifest1_info, __LINE__);
1081             pReleaseActCtx(handle);
1082         }
1083         SetCurrentDirectoryW(work_dir);
1084     }
1085     else
1086         skip("Couldn't change directory\n");
1087     RemoveDirectoryW(work_dir_subdir);
1088
1089     trace("UTF-16 manifest1, with BOM\n");
1090     if(!create_wide_manifest("test1.manifest", manifest1, TRUE, FALSE)) {
1091         skip("Could not create manifest file\n");
1092         return;
1093     }
1094
1095     handle = test_create("test1.manifest", manifest1);
1096     DeleteFileA("test1.manifest");
1097     if (handle != INVALID_HANDLE_VALUE) {
1098         test_basic_info(handle, __LINE__);
1099         test_detailed_info(handle, &detailed_info1, __LINE__);
1100         test_info_in_assembly(handle, 1, &manifest1_info, __LINE__);
1101         pReleaseActCtx(handle);
1102     }
1103
1104     trace("UTF-16 manifest1, reverse endian, with BOM\n");
1105     if(!create_wide_manifest("test1.manifest", manifest1, TRUE, TRUE)) {
1106         skip("Could not create manifest file\n");
1107         return;
1108     }
1109
1110     handle = test_create("test1.manifest", manifest1);
1111     DeleteFileA("test1.manifest");
1112     if (handle != INVALID_HANDLE_VALUE) {
1113         test_basic_info(handle, __LINE__);
1114         test_detailed_info(handle, &detailed_info1, __LINE__);
1115         test_info_in_assembly(handle, 1, &manifest1_info, __LINE__);
1116         pReleaseActCtx(handle);
1117     }
1118
1119 }
1120
1121 static void test_app_manifest(void)
1122 {
1123     HANDLE handle;
1124     BOOL b;
1125
1126     trace("child process manifest1\n");
1127
1128     b = pGetCurrentActCtx(&handle);
1129     ok(handle == NULL, "handle != NULL\n");
1130     ok(b, "GetCurrentActCtx failed: %u\n", GetLastError());
1131     if(b) {
1132         test_basic_info(handle, __LINE__);
1133         test_detailed_info(handle, &detailed_info1_child, __LINE__);
1134         test_info_in_assembly(handle, 1, &manifest1_child_info, __LINE__);
1135         pReleaseActCtx(handle);
1136     }
1137 }
1138
1139 static void run_child_process(void)
1140 {
1141     char cmdline[MAX_PATH];
1142     char path[MAX_PATH];
1143     char **argv;
1144     PROCESS_INFORMATION pi;
1145     STARTUPINFO si = { 0 };
1146     HANDLE file;
1147     FILETIME now;
1148     BOOL ret;
1149
1150     GetModuleFileNameA(NULL, path, MAX_PATH);
1151     strcat(path, ".manifest");
1152     if(!create_manifest_file(path, manifest1, -1, NULL, NULL)) {
1153         skip("Could not create manifest file\n");
1154         return;
1155     }
1156
1157     si.cb = sizeof(si);
1158     winetest_get_mainargs( &argv );
1159     /* Vista+ seems to cache presence of .manifest files. Change last modified
1160        date to defeat the cache */
1161     file = CreateFileA(argv[0], FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE,
1162                        NULL, OPEN_EXISTING, 0, NULL);
1163     if (file != INVALID_HANDLE_VALUE) {
1164         GetSystemTimeAsFileTime(&now);
1165         SetFileTime(file, NULL, NULL, &now);
1166         CloseHandle(file);
1167     }
1168     sprintf(cmdline, "\"%s\" %s manifest1", argv[0], argv[1]);
1169     ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1170     ok(ret, "Could not create process: %u\n", GetLastError());
1171     winetest_wait_child_process( pi.hProcess );
1172     CloseHandle(pi.hThread);
1173     CloseHandle(pi.hProcess);
1174     DeleteFileA(path);
1175 }
1176
1177 static void init_paths(void)
1178 {
1179     LPWSTR ptr;
1180     WCHAR last;
1181
1182     static const WCHAR dot_manifest[] = {'.','M','a','n','i','f','e','s','t',0};
1183     static const WCHAR backslash[] = {'\\',0};
1184     static const WCHAR subdir[] = {'T','e','s','t','S','u','b','d','i','r','\\',0};
1185
1186     GetModuleFileNameW(NULL, exe_path, sizeof(exe_path)/sizeof(WCHAR));
1187     lstrcpyW(app_dir, exe_path);
1188     for(ptr=app_dir+lstrlenW(app_dir); *ptr != '\\' && *ptr != '/'; ptr--);
1189     ptr[1] = 0;
1190
1191     GetCurrentDirectoryW(MAX_PATH, work_dir);
1192     last = work_dir[lstrlenW(work_dir) - 1];
1193     if (last != '\\' && last != '/')
1194         lstrcatW(work_dir, backslash);
1195     lstrcpyW(work_dir_subdir, work_dir);
1196     lstrcatW(work_dir_subdir, subdir);
1197
1198     GetModuleFileNameW(NULL, app_manifest_path, sizeof(app_manifest_path)/sizeof(WCHAR));
1199     lstrcpyW(app_manifest_path+lstrlenW(app_manifest_path), dot_manifest);
1200 }
1201
1202 static BOOL init_funcs(void)
1203 {
1204     HMODULE hKernel32 = GetModuleHandle("kernel32");
1205
1206 #define X(f) if (!(p##f = (void*)GetProcAddress(hKernel32, #f))) return FALSE;
1207     X(ActivateActCtx);
1208     X(CreateActCtxW);
1209     X(DeactivateActCtx);
1210     X(FindActCtxSectionStringW);
1211     X(GetCurrentActCtx);
1212     X(IsDebuggerPresent);
1213     X(QueryActCtxW);
1214     X(ReleaseActCtx);
1215 #undef X
1216
1217     return TRUE;
1218 }
1219
1220 START_TEST(actctx)
1221 {
1222     int argc;
1223     char **argv;
1224
1225     argc = winetest_get_mainargs(&argv);
1226
1227     if (!init_funcs())
1228     {
1229         win_skip("Needed functions are not available\n");
1230         return;
1231     }
1232     init_paths();
1233
1234     if(argc > 2 && !strcmp(argv[2], "manifest1")) {
1235         test_app_manifest();
1236         return;
1237     }
1238
1239     test_actctx();
1240     run_child_process();
1241 }