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