mshtml: Implement IHTMLDOMNode replaceChild.
[wine] / dlls / urlmon / tests / sec_mgr.c
1 /*
2  * Copyright 2005-2006 Jacek Caban for CodeWeavers
3  * Copyright 2009-2011 Detlef Riekenberg
4  * Copyright 2011 Thomas Mullaly for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #define CONST_VTABLE
23 #define NONAMELESSUNION
24
25 /* needed for IInternetZoneManagerEx2 */
26 #define _WIN32_IE 0x0700
27
28 #include <wine/test.h>
29 #include <stdarg.h>
30 #include <stddef.h>
31 #include <stdio.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "ole2.h"
36 #include "urlmon.h"
37
38 #include "initguid.h"
39
40 #define URLZONE_CUSTOM  URLZONE_USER_MIN+1
41 #define URLZONE_CUSTOM2 URLZONE_CUSTOM+1
42
43 #define DEFINE_EXPECT(func) \
44     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
45
46 #define SET_EXPECT(func) \
47     expect_ ## func = TRUE
48
49 #define CHECK_EXPECT(func) \
50     do { \
51         ok(expect_ ##func, "unexpected call " #func "\n"); \
52         expect_ ## func = FALSE; \
53         called_ ## func = TRUE; \
54     }while(0)
55
56 #define CHECK_EXPECT2(func) \
57     do { \
58         ok(expect_ ##func, "unexpected call " #func "\n"); \
59         called_ ## func = TRUE; \
60     }while(0)
61
62 #define CHECK_CALLED(func) \
63     do { \
64         ok(called_ ## func, "expected " #func "\n"); \
65         expect_ ## func = called_ ## func = FALSE; \
66     }while(0)
67
68 DEFINE_EXPECT(ParseUrl_SECURITY_URL_input);
69 DEFINE_EXPECT(ParseUrl_SECURITY_URL_input2);
70 DEFINE_EXPECT(ParseUrl_SECURITY_URL_expected);
71 DEFINE_EXPECT(ParseUrl_SECURITY_URL_http);
72 DEFINE_EXPECT(ParseUrl_SECURITY_DOMAIN_expected);
73 DEFINE_EXPECT(ProcessUrlAction);
74
75 static HRESULT (WINAPI *pCoInternetCreateSecurityManager)(IServiceProvider *, IInternetSecurityManager**, DWORD);
76 static HRESULT (WINAPI *pCoInternetCreateZoneManager)(IServiceProvider *, IInternetZoneManager**, DWORD);
77 static HRESULT (WINAPI *pCoInternetGetSecurityUrl)(LPCWSTR, LPWSTR*, PSUACTION, DWORD);
78 static HRESULT (WINAPI *pCoInternetGetSecurityUrlEx)(IUri*, IUri**, PSUACTION, DWORD_PTR);
79 static HRESULT (WINAPI *pCreateUri)(LPCWSTR, DWORD, DWORD_PTR, IUri**);
80 static HRESULT (WINAPI *pCoInternetGetSession)(DWORD, IInternetSession**, DWORD);
81 static HRESULT (WINAPI *pCoInternetIsFeatureEnabled)(INTERNETFEATURELIST, DWORD);
82 static HRESULT (WINAPI *pCoInternetIsFeatureEnabledForUrl)(INTERNETFEATURELIST, DWORD, LPCWSTR, IInternetSecurityManager*);
83 static HRESULT (WINAPI *pCoInternetIsFeatureZoneElevationEnabled)(LPCWSTR, LPCWSTR, IInternetSecurityManager*, DWORD);
84
85 static const WCHAR url1[] = {'r','e','s',':','/','/','m','s','h','t','m','l','.','d','l','l',
86         '/','b','l','a','n','k','.','h','t','m',0};
87 static const WCHAR url2[] = {'i','n','d','e','x','.','h','t','m',0};
88 static const WCHAR url3[] = {'f','i','l','e',':','/','/','c',':','\\','I','n','d','e','x','.','h','t','m',0};
89 static const WCHAR url4[] = {'f','i','l','e',':','s','o','m','e','%','2','0','f','i','l','e',
90         '%','2','e','j','p','g',0};
91 static const WCHAR url5[] = {'h','t','t','p',':','/','/','w','w','w','.','z','o','n','e','3',
92         '.','w','i','n','e','t','e','s','t',0};
93 static const WCHAR url6[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
94 static const WCHAR url7[] = {'f','t','p',':','/','/','z','o','n','e','3',
95         '.','w','i','n','e','t','e','s','t','/','f','i','l','e','.','t','e','s','t',0};
96 static const WCHAR url8[] = {'t','e','s','t',':','1','2','3','a','b','c',0};
97 static const WCHAR url9[] = {'h','t','t','p',':','/','/','w','w','w','.','z','o','n','e','3',
98         '.','w','i','n','e','t','e','s','t', '/','s','i','t','e','/','a','b','o','u','t',0};
99 static const WCHAR url10[] = {'f','i','l','e',':','/','/','s','o','m','e','%','2','0','f','i','l','e',
100         '.','j','p','g',0};
101 static const WCHAR url11[] = {'f','i','l','e',':','/','/','c',':','/','I','n','d','e','x','.','h','t','m',0};
102 static const WCHAR url12[] = {'f','i','l','e',':','/','/','/','c',':','/','I','n','d','e','x','.','h','t','m',0};
103 static const WCHAR url13[] = {'h','t','t','p',':','g','o','o','g','l','e','.','c','o','m',0};
104 static const WCHAR url14[] = {'z','i','p',':','t','e','s','t','i','n','g','.','c','o','m','/','t','e','s','t','i','n','g',0};
105 static const WCHAR url15[] = {'h','t','t','p',':','/','/','g','o','o','g','l','e','.','c','o','m','.','u','k',0};
106
107 static const WCHAR url4e[] = {'f','i','l','e',':','s','o','m','e',' ','f','i','l','e',
108         '.','j','p','g',0};
109
110 static const WCHAR winetestW[] = {'w','i','n','e','t','e','s','t',0};
111 static const WCHAR security_urlW[] = {'w','i','n','e','t','e','s','t',':','t','e','s','t','i','n','g',0};
112 static const WCHAR security_url2W[] = {'w','i','n','e','t','e','s','t',':','t','e','s','t','i','n','g','2',0};
113 static const WCHAR security_expectedW[] = {'w','i','n','e','t','e','s','t',':','z','i','p',0};
114 static const WCHAR winetest_to_httpW[] = {'w','i','n','e','t','e','s','t',':','h',0};
115
116 static const char *szZoneMapDomainsKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains";
117 static const char *szInternetSettingsKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
118
119 static const BYTE secid1[] = {'f','i','l','e',':',0,0,0,0};
120 static const BYTE secid2[] = {'*',':','i','n','d','e','x','.','h','t','m',3,0,0,0};
121 static const BYTE secid5[] = {'h','t','t','p',':','w','w','w','.','z','o','n','e','3',
122         '.','w','i','n','e','t','e','s','t',3,0,0,0};
123 static const BYTE secid6[] = {'a','b','o','u','t',':','b','l','a','n','k',3,0,0,0};
124 static const BYTE secid7[] = {'f','t','p',':','z','o','n','e','3',
125         '.','w','i','n','e','t','e','s','t',3,0,0,0};
126 static const BYTE secid10[] =
127     {'f','i','l','e',':','s','o','m','e','%','2','0','f','i','l','e','.','j','p','g',3,0,0,0};
128 static const BYTE secid14[] =
129     {'z','i','p',':','t','e','s','t','i','n','g','.','c','o','m','/','t','e','s','t','i','n','g',3,0,0,0};
130 static const BYTE secid10_2[] =
131     {'f','i','l','e',':','s','o','m','e',' ','f','i','l','e','.','j','p','g',3,0,0,0};
132 static const BYTE secid13[] = {'h','t','t','p',':','c','o','m','.','u','k',3,0,0,0};
133 static const BYTE secid13_2[] = {'h','t','t','p',':','g','o','o','g','l','e','.','c','o','m','.','u','k',3,0,0,0};
134
135 static const GUID CLSID_TestActiveX =
136     {0x178fc163,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x06,0x46}};
137
138 /* Defined as extern in urlmon.idl, but not exported by uuid.lib */
139 const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
140     {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};
141
142 static int called_securl_http;
143 static DWORD ProcessUrlAction_policy;
144
145 static struct secmgr_test {
146     LPCWSTR url;
147     DWORD zone;
148     HRESULT zone_hres;
149     DWORD secid_size;
150     const BYTE *secid;
151     HRESULT secid_hres;
152 } secmgr_tests[] = {
153     {url1, 0,   S_OK, sizeof(secid1), secid1, S_OK},
154     {url2, 100, 0x80041001, 0, NULL, E_INVALIDARG},
155     {url3, 0,   S_OK, sizeof(secid1), secid1, S_OK},
156     {url5, 3,   S_OK, sizeof(secid5), secid5, S_OK},
157     {url6, 3,   S_OK, sizeof(secid6), secid6, S_OK},
158     {url7, 3,   S_OK, sizeof(secid7), secid7, S_OK},
159     {url11,0,   S_OK, sizeof(secid1), secid1, S_OK},
160     {url12,0,   S_OK, sizeof(secid1), secid1, S_OK}
161 };
162
163 static int strcmp_w(const WCHAR *str1, const WCHAR *str2)
164 {
165     DWORD len1 = lstrlenW(str1);
166     DWORD len2 = lstrlenW(str2);
167
168     if(len1!=len2) return 1;
169     return memcmp(str1, str2, len1*sizeof(WCHAR));
170 }
171
172 static inline void heap_free(void *mem)
173 {
174     HeapFree(GetProcessHeap(), 0, mem);
175 }
176
177 static inline LPWSTR a2w(LPCSTR str)
178 {
179     LPWSTR ret = NULL;
180
181     if(str) {
182         DWORD len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
183         ret = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
184         MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
185     }
186
187     return ret;
188 }
189
190 static inline DWORD strcmp_aw(LPCSTR strA, LPCWSTR strB) {
191     LPWSTR strAW = a2w(strA);
192     DWORD ret = lstrcmpW(strAW, strB);
193     heap_free(strAW);
194     return ret;
195 }
196
197
198 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
199 static LONG myRegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
200 {
201     LONG ret;
202     DWORD dwMaxSubkeyLen, dwMaxValueLen;
203     DWORD dwMaxLen, dwSize;
204     CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
205     HKEY hSubKey = hKey;
206
207     if(lpszSubKey)
208     {
209         ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
210         if (ret) return ret;
211     }
212
213     /* Get highest length for keys, values */
214     ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
215             &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
216     if (ret) goto cleanup;
217
218     dwMaxSubkeyLen++;
219     dwMaxValueLen++;
220     dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
221     if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
222     {
223         /* Name too big: alloc a buffer for it */
224         if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
225         {
226             ret = ERROR_NOT_ENOUGH_MEMORY;
227             goto cleanup;
228         }
229     }
230
231     /* Recursively delete all the subkeys */
232     while (TRUE)
233     {
234         dwSize = dwMaxLen;
235         if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
236                           NULL, NULL, NULL)) break;
237
238         ret = myRegDeleteTreeA(hSubKey, lpszName);
239         if (ret) goto cleanup;
240     }
241
242     if (lpszSubKey)
243         ret = RegDeleteKeyA(hKey, lpszSubKey);
244     else
245         while (TRUE)
246         {
247             dwSize = dwMaxLen;
248             if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
249                   NULL, NULL, NULL, NULL)) break;
250
251             ret = RegDeleteValueA(hKey, lpszName);
252             if (ret) goto cleanup;
253         }
254
255 cleanup:
256     /* Free buffer if allocated */
257     if (lpszName != szNameBuf)
258         heap_free(lpszName);
259     if(lpszSubKey)
260         RegCloseKey(hSubKey);
261     return ret;
262 }
263
264 static HRESULT WINAPI SecurityManager_QueryInterface(IInternetSecurityManager* This,
265         REFIID riid, void **ppvObject)
266 {
267     ok(0, "unexpected call\n");
268     return E_NOTIMPL;
269 }
270
271 static ULONG WINAPI SecurityManager_AddRef(IInternetSecurityManager* This)
272 {
273     return 2;
274 }
275
276 static ULONG WINAPI SecurityManager_Release(IInternetSecurityManager* This)
277 {
278     return 1;
279 }
280
281 static HRESULT WINAPI SecurityManager_SetSecuritySite(IInternetSecurityManager* This,
282         IInternetSecurityMgrSite *pSite)
283 {
284     ok(0, "unexpected call\n");
285     return E_NOTIMPL;
286 }
287
288 static HRESULT WINAPI SecurityManager_GetSecuritySite(IInternetSecurityManager* This,
289         IInternetSecurityMgrSite **ppSite)
290 {
291     ok(0, "unexpected call\n");
292     return E_NOTIMPL;
293 }
294
295 static HRESULT WINAPI SecurityManager_MapUrlToZone(IInternetSecurityManager* This,
296         LPCWSTR pwszUrl, DWORD *pdwZone, DWORD dwFlags)
297 {
298     ok(0, "unexpected call\n");
299     return E_NOTIMPL;
300 }
301
302 static HRESULT WINAPI SecurityManager_GetSecurityId(IInternetSecurityManager* This,
303         LPCWSTR pwszUrl, BYTE *pbSecurityId, DWORD *pcbSecurityId, DWORD_PTR dwReserved)
304 {
305     ok(0, "unexpected call\n");
306     return E_NOTIMPL;
307 }
308
309 static HRESULT WINAPI SecurityManager_ProcessUrlAction(IInternetSecurityManager* This,
310         LPCWSTR pwszUrl, DWORD dwAction, BYTE *pPolicy, DWORD cbPolicy,
311         BYTE *pContext, DWORD cbContext, DWORD dwFlags, DWORD dwReserved)
312 {
313     CHECK_EXPECT(ProcessUrlAction);
314     ok(dwAction == URLACTION_FEATURE_ZONE_ELEVATION, "dwAction = %x\n", dwAction);
315     ok(cbPolicy == sizeof(DWORD), "cbPolicy = %d\n", cbPolicy);
316     ok(!pContext, "pContext != NULL\n");
317     ok(dwFlags == PUAF_NOUI, "dwFlags = %x\n", dwFlags);
318     ok(dwReserved == 0, "dwReserved = %x\n", dwReserved);
319
320     *pPolicy = ProcessUrlAction_policy;
321     return ProcessUrlAction_policy==URLPOLICY_ALLOW ? S_OK : S_FALSE;
322 }
323
324 static HRESULT WINAPI SecurityManager_QueryCustomPolicy(IInternetSecurityManager* This,
325         LPCWSTR pwszUrl, REFGUID guidKey, BYTE **ppPolicy, DWORD *pcbPolicy,
326         BYTE *pContext, DWORD cbContext, DWORD dwReserved)
327 {
328     ok(0, "unexpected call\n");
329     return E_NOTIMPL;
330 }
331
332 static HRESULT WINAPI SecurityManager_SetZoneMapping(IInternetSecurityManager* This,
333         DWORD dwZone, LPCWSTR lpszPattern, DWORD dwFlags)
334 {
335     ok(0, "unexpected call\n");
336     return E_NOTIMPL;
337 }
338
339 static HRESULT WINAPI SecurityManager_GetZoneMappings(IInternetSecurityManager* This,
340         DWORD dwZone, IEnumString **ppenumString, DWORD dwFlags)
341 {
342     ok(0, "unexpected call\n");
343     return E_NOTIMPL;
344 }
345
346 static const IInternetSecurityManagerVtbl SecurityManagerVtbl = {
347     SecurityManager_QueryInterface,
348     SecurityManager_AddRef,
349     SecurityManager_Release,
350     SecurityManager_SetSecuritySite,
351     SecurityManager_GetSecuritySite,
352     SecurityManager_MapUrlToZone,
353     SecurityManager_GetSecurityId,
354     SecurityManager_ProcessUrlAction,
355     SecurityManager_QueryCustomPolicy,
356     SecurityManager_SetZoneMapping,
357     SecurityManager_GetZoneMappings
358 };
359
360 static IInternetSecurityManager security_manager = { &SecurityManagerVtbl };
361
362 static void test_SecurityManager(void)
363 {
364     int i;
365     IInternetSecurityManager *secmgr = NULL;
366     BYTE buf[512];
367     DWORD zone, size, policy;
368     HRESULT hres;
369
370     if(!pCoInternetCreateSecurityManager) {
371         return;
372     }
373
374     trace("Testing security manager...\n");
375
376     hres = pCoInternetCreateSecurityManager(NULL, &secmgr, 0);
377     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
378     if(FAILED(hres))
379         return;
380
381     for(i=0; i < sizeof(secmgr_tests)/sizeof(secmgr_tests[0]); i++) {
382         zone = 100;
383         hres = IInternetSecurityManager_MapUrlToZone(secmgr, secmgr_tests[i].url,
384                                                      &zone, 0);
385         ok(hres == secmgr_tests[i].zone_hres /* IE <=6 */
386            || (FAILED(secmgr_tests[i].zone_hres) && hres == E_INVALIDARG), /* IE7 */
387            "[%d] MapUrlToZone failed: %08x, expected %08x\n",
388                 i, hres, secmgr_tests[i].zone_hres);
389         if(SUCCEEDED(hres))
390             ok(zone == secmgr_tests[i].zone, "[%d] zone=%d, expected %d\n", i, zone,
391                secmgr_tests[i].zone);
392         else
393             ok(zone == secmgr_tests[i].zone || zone == -1, "[%d] zone=%d\n", i, zone);
394
395         size = sizeof(buf);
396         memset(buf, 0xf0, sizeof(buf));
397         hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[i].url,
398                 buf, &size, 0);
399         ok(hres == secmgr_tests[i].secid_hres,
400            "[%d] GetSecurityId failed: %08x, expected %08x\n",
401            i, hres, secmgr_tests[i].secid_hres);
402         if(secmgr_tests[i].secid) {
403             ok(size == secmgr_tests[i].secid_size, "[%d] size=%d, expected %d\n",
404                     i, size, secmgr_tests[i].secid_size);
405             ok(!memcmp(buf, secmgr_tests[i].secid, size), "[%d] wrong secid\n", i);
406         }
407     }
408
409     zone = 100;
410     hres = IInternetSecurityManager_MapUrlToZone(secmgr, url10, &zone, 0);
411     ok(hres == S_OK, "MapUrlToZone failed: %08x, expected S_OK\n", hres);
412     ok(zone == 3, "zone=%d, expected 3\n", zone);
413
414     /* win2k3 translates %20 into a space */
415     size = sizeof(buf);
416     memset(buf, 0xf0, sizeof(buf));
417     hres = IInternetSecurityManager_GetSecurityId(secmgr, url10, buf, &size, 0);
418     ok(hres == S_OK, "GetSecurityId failed: %08x, expected S_OK\n", hres);
419     ok(size == sizeof(secid10) ||
420        size == sizeof(secid10_2), /* win2k3 */
421        "size=%d\n", size);
422     ok(!memcmp(buf, secid10, size) ||
423        !memcmp(buf, secid10_2, size), /* win2k3 */
424        "wrong secid\n");
425
426     zone = 100;
427     hres = IInternetSecurityManager_MapUrlToZone(secmgr, url13, &zone, 0);
428     ok(hres == S_OK, "MapUrlToZone failed: %08x\n", hres);
429     ok(zone == URLZONE_INVALID || broken(zone == URLZONE_INTERNET), "zone=%d\n", zone);
430
431     size = sizeof(buf);
432     memset(buf, 0xf0, sizeof(buf));
433     hres = IInternetSecurityManager_GetSecurityId(secmgr, url13, buf, &size, 0);
434     ok(hres == E_INVALIDARG || broken(hres == S_OK), "GetSecurityId failed: %08x\n", hres);
435
436     zone = 100;
437     hres = IInternetSecurityManager_MapUrlToZone(secmgr, url14, &zone, 0);
438     ok(hres == S_OK, "MapUrlToZone failed: %08x, expected S_OK\n", hres);
439     ok(zone == URLZONE_INTERNET, "zone=%d\n", zone);
440
441     size = sizeof(buf);
442     memset(buf, 0xf0, sizeof(buf));
443     hres = IInternetSecurityManager_GetSecurityId(secmgr, url14, buf, &size, 0);
444     ok(hres == S_OK, "GetSecurityId failed: %08x, expected S_OK\n", hres);
445     ok(size == sizeof(secid14), "size=%d\n", size);
446     ok(!memcmp(buf, secid14, size), "wrong secid\n");
447
448     zone = 100;
449     hres = IInternetSecurityManager_MapUrlToZone(secmgr, NULL, &zone, 0);
450     ok(hres == E_INVALIDARG, "MapUrlToZone failed: %08x, expected E_INVALIDARG\n", hres);
451     ok(zone == 100 || zone == -1, "zone=%d\n", zone);
452
453     size = sizeof(buf);
454     hres = IInternetSecurityManager_GetSecurityId(secmgr, NULL, buf, &size, 0);
455     ok(hres == E_INVALIDARG,
456        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
457     hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[1].url,
458                                                   NULL, &size, 0);
459     ok(hres == E_INVALIDARG,
460        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
461     hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[1].url,
462                                                   buf, NULL, 0);
463     ok(hres == E_INVALIDARG,
464        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
465
466     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, NULL, URLACTION_SCRIPT_RUN, (BYTE*)&policy,
467             sizeof(WCHAR), NULL, 0, 0, 0);
468     ok(hres == E_INVALIDARG, "ProcessUrlAction failed: %08x, expected E_INVALIDARG\n", hres);
469
470     IInternetSecurityManager_Release(secmgr);
471 }
472
473 /* Check if Internet Explorer is configured to run in "Enhanced Security Configuration" (aka hardened mode) */
474 /* Note: this code is duplicated in dlls/mshtml/tests/mshtml_test.h and dlls/urlmon/tests/sec_mgr.c */
475 static BOOL is_ie_hardened(void)
476 {
477     HKEY zone_map;
478     DWORD ie_harden, type, size;
479
480     ie_harden = 0;
481     if(RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap",
482                     0, KEY_QUERY_VALUE, &zone_map) == ERROR_SUCCESS) {
483         size = sizeof(DWORD);
484         if (RegQueryValueExA(zone_map, "IEHarden", NULL, &type, (LPBYTE) &ie_harden, &size) != ERROR_SUCCESS ||
485             type != REG_DWORD) {
486             ie_harden = 0;
487         }
488         RegCloseKey(zone_map);
489     }
490
491     return ie_harden != 0;
492 }
493
494 static void test_url_action(IInternetSecurityManager *secmgr, IInternetZoneManager *zonemgr, DWORD action)
495 {
496     DWORD res, size, policy, reg_policy;
497     char buf[10];
498     HKEY hkey;
499     HRESULT hres;
500
501     /* FIXME: HKEY_CURRENT_USER is most of the time the default but this can be changed on a system.
502      * The test should be changed to cope with that, if need be.
503      */
504     res = RegOpenKeyA(HKEY_CURRENT_USER,
505             "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3", &hkey);
506     if(res != ERROR_SUCCESS) {
507         ok(0, "Could not open zone key\n");
508         return;
509     }
510
511     wsprintf(buf, "%X", action);
512     size = sizeof(DWORD);
513     res = RegQueryValueExA(hkey, buf, NULL, NULL, (BYTE*)&reg_policy, &size);
514     RegCloseKey(hkey);
515     if(res != ERROR_SUCCESS || size != sizeof(DWORD)) {
516         policy = 0xdeadbeef;
517         hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
518                 sizeof(WCHAR), NULL, 0, 0, 0);
519         ok(hres == E_FAIL || broken(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)),
520             "(0x%x) got 0x%x (expected E_FAIL)\n", action, hres);
521         ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
522
523         policy = 0xdeadbeef;
524         hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, (BYTE*)&policy,
525                 sizeof(DWORD), URLZONEREG_DEFAULT);
526         ok(hres == E_FAIL || broken(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)),
527             "(0x%x) got 0x%x (expected E_FAIL)\n", action, hres);
528         ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
529         return;
530     }
531
532     policy = 0xdeadbeef;
533     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, (BYTE*)&policy,
534             sizeof(DWORD), URLZONEREG_DEFAULT);
535     ok(hres == S_OK, "GetZoneActionPolicy failed: %08x\n", hres);
536     ok(policy == reg_policy, "(%x) policy=%x, expected %x\n", action, policy, reg_policy);
537
538     if(policy != URLPOLICY_QUERY) {
539         if(winetest_interactive || ! is_ie_hardened()) {
540             BOOL expect_parse_call = !called_securl_http;
541
542             policy = 0xdeadbeef;
543             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
544                     sizeof(WCHAR), NULL, 0, 0, 0);
545             if(reg_policy == URLPOLICY_DISALLOW)
546                 ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
547             else
548                 ok(hres == S_OK, "ProcessUrlAction(%x) failed: %08x\n", action, hres);
549             ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
550
551             policy = 0xdeadbeef;
552             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
553                     2, NULL, 0, 0, 0);
554             if(reg_policy == URLPOLICY_DISALLOW)
555                 ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
556             else
557                 ok(hres == S_OK, "ProcessUrlAction(%x) failed: %08x\n", action, hres);
558             ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
559
560             policy = 0xdeadbeef;
561             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
562                     sizeof(DWORD), NULL, 0, 0, 0);
563             if(reg_policy == URLPOLICY_DISALLOW)
564                 ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
565             else
566                 ok(hres == S_OK, "ProcessUrlAction(%x) failed: %08x\n", action, hres);
567             ok(policy == reg_policy, "(%x) policy=%x\n", action, policy);
568
569             policy = 0xdeadbeef;
570             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
571                     sizeof(WCHAR), (BYTE*)0xdeadbeef, 16, 0, 0);
572             if(reg_policy == URLPOLICY_DISALLOW)
573                 ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
574             else
575                 ok(hres == S_OK, "ProcessUrlAction(%x) failed: %08x\n", action, hres);
576             ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
577
578             policy = 0xdeadbeef;
579             if(expect_parse_call)
580                 SET_EXPECT(ParseUrl_SECURITY_URL_http);
581             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, winetest_to_httpW, action, (BYTE*)&policy,
582                     sizeof(DWORD), NULL, 0, 0, 0);
583             if(expect_parse_call)
584                 CHECK_CALLED(ParseUrl_SECURITY_URL_http);
585             if(reg_policy == URLPOLICY_DISALLOW)
586                 ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
587             else
588                 ok(hres == S_OK, "ProcessUrlAction(%x) failed: %08x\n", action, hres);
589             ok(policy == reg_policy, "(%x) policy=%x\n", action, policy);
590         }else {
591             skip("IE running in Enhanced Security Configuration\n");
592         }
593     }
594 }
595
596 static void test_special_url_action(IInternetSecurityManager *secmgr, IInternetZoneManager *zonemgr, DWORD action)
597 {
598     DWORD policy;
599     HRESULT hres;
600
601     policy = 0xdeadbeef;
602     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, (BYTE*)&policy,
603             sizeof(DWORD), URLZONEREG_DEFAULT);
604     ok(hres == S_OK, "GetZoneActionPolicy failed: %08x\n", hres);
605     ok(policy == URLPOLICY_DISALLOW, "(%x) policy=%x, expected URLPOLICY_DISALLOW\n", action, policy);
606
607     policy = 0xdeadbeef;
608     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url1, action, (BYTE*)&policy,
609             sizeof(WCHAR), NULL, 0, 0, 0);
610     ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
611
612     policy = 0xdeadbeef;
613     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url1, action, (BYTE*)&policy,
614             sizeof(DWORD), NULL, 0, 0, 0);
615     ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
616     ok(policy == URLPOLICY_DISALLOW, "policy = %x\n", policy);
617 }
618
619 static void test_activex(IInternetSecurityManager *secmgr)
620 {
621     DWORD policy, policy_size;
622     struct CONFIRMSAFETY cs;
623     BYTE *ppolicy;
624     HRESULT hres;
625
626     policy = 0xdeadbeef;
627     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url1, URLACTION_ACTIVEX_RUN, (BYTE*)&policy,
628             sizeof(DWORD), (BYTE*)&CLSID_TestActiveX, sizeof(CLSID), 0, 0);
629     ok(hres == S_OK, "ProcessUrlAction(URLACTION_ACTIVEX_RUN) failed: %08x\n", hres);
630     ok(policy == URLPOLICY_ALLOW || policy == URLPOLICY_DISALLOW, "policy = %x\n", policy);
631
632     cs.clsid = CLSID_TestActiveX;
633     cs.pUnk = (IUnknown*)0xdeadbeef;
634     cs.dwFlags = 0;
635     hres = IInternetSecurityManager_QueryCustomPolicy(secmgr, url1, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
636             &ppolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
637     ok(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), "QueryCusromPolicy failed: %08x\n", hres);
638 }
639
640 static void test_polices(void)
641 {
642     IInternetZoneManager *zonemgr = NULL;
643     IInternetSecurityManager *secmgr = NULL;
644     HRESULT hres;
645
646     trace("testing polices...\n");
647
648     hres = pCoInternetCreateSecurityManager(NULL, &secmgr, 0);
649     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
650     hres = pCoInternetCreateZoneManager(NULL, &zonemgr, 0);
651     ok(hres == S_OK, "CoInternetCreateZoneManager failed: %08x\n", hres);
652
653     test_url_action(secmgr, zonemgr, URLACTION_SCRIPT_RUN);
654     test_url_action(secmgr, zonemgr, URLACTION_ACTIVEX_RUN);
655     test_url_action(secmgr, zonemgr, URLACTION_ACTIVEX_OVERRIDE_OBJECT_SAFETY);
656     test_url_action(secmgr, zonemgr, URLACTION_CHANNEL_SOFTDIST_PERMISSIONS);
657     test_url_action(secmgr, zonemgr, 0xdeadbeef);
658
659     test_special_url_action(secmgr, zonemgr, URLACTION_SCRIPT_OVERRIDE_SAFETY);
660     test_special_url_action(secmgr, zonemgr, URLACTION_ACTIVEX_OVERRIDE_SCRIPT_SAFETY);
661
662     test_activex(secmgr);
663
664     IInternetSecurityManager_Release(secmgr);
665     IInternetZoneManager_Release(zonemgr);
666 }
667
668 /* IE (or at least newer versions of it) seem to cache the keys in ZoneMap
669  * when urlmon.dll is loaded and it doesn't seem to update its cache, unless
670  * SetZoneMapping is used.
671  */
672 static void test_zone_domain_cache(void)
673 {
674     HRESULT hres;
675     DWORD res, zone;
676     IInternetSecurityManager *secmgr = NULL;
677     HKEY domains, domain;
678
679     static const WCHAR testing_domain_urlW[] = {'h','t','t','p',':','/','/','t','e','s','t','i','n','g','.',
680             'd','o','m','a','i','n','/',0};
681
682     res = RegOpenKeyA(HKEY_CURRENT_USER, szZoneMapDomainsKey, &domains);
683     ok(res == ERROR_SUCCESS, "RegOpenKey failed: %d\n", res);
684     if(res != ERROR_SUCCESS)
685         return;
686
687     res = RegCreateKeyA(domains, "testing.domain", &domain);
688     ok(res == ERROR_SUCCESS, "RegCreateKey failed: %d\n", res);
689     if(res != ERROR_SUCCESS) {
690         RegCloseKey(domains);
691         return;
692     }
693
694     zone = URLZONE_CUSTOM;
695     res = RegSetValueExA(domain, "http", 0, REG_DWORD, (BYTE*)&zone, sizeof(DWORD));
696     ok(res == ERROR_SUCCESS, "RegSetValueEx failed: %d\n", res);
697
698     RegCloseKey(domain);
699
700     hres = pCoInternetCreateSecurityManager(NULL, &secmgr, 0);
701     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
702
703     zone = URLZONE_INVALID;
704     hres = IInternetSecurityManager_MapUrlToZone(secmgr, testing_domain_urlW, &zone, 0);
705     ok(hres == S_OK, "MapUrlToZone failed: %08x\n", hres);
706     todo_wine ok(zone == URLZONE_INTERNET, "Got %d, expected URLZONE_INTERNET\n", zone);
707
708     /* FIXME: Play nice with ZoneMaps that existed before the test is run. */
709     res = RegDeleteKeyA(domains, "testing.domain");
710     ok(res == ERROR_SUCCESS, "RegDeleteKey failed: %d\n", res);
711
712     RegCloseKey(domains);
713     IInternetSecurityManager_Release(secmgr);
714 }
715
716 typedef struct {
717     const char  *domain;
718     const char  *subdomain;
719     const char  *scheme;
720     DWORD       zone;
721 } zone_domain_mapping;
722
723 /* FIXME: Move these into SetZoneMapping tests when the day comes... */
724 static const zone_domain_mapping zone_domain_mappings[] = {
725     /* Implicitly means "*.yabadaba.do". */
726     {"yabadaba.do",NULL,"http",URLZONE_CUSTOM},
727     /* The '*' doesn't count as a wildcard, since its not the first component of the subdomain. */
728     {"super.cool","testing.*","ftp",URLZONE_CUSTOM2},
729     /* The '*' counts since it's the first component of the subdomain. */
730     {"super.cool","*.testing","ftp",URLZONE_CUSTOM2},
731     /* All known scheme types apply to wildcard schemes. */
732     {"tests.test",NULL,"*",URLZONE_CUSTOM},
733     /* Due to a defect with how windows checks the mappings, unknown scheme types
734      * never seem to get mapped properly. */
735     {"tests.test",NULL,"zip",URLZONE_CUSTOM},
736     {"www.testing.com",NULL,"http",URLZONE_CUSTOM},
737     {"www.testing.com","testing","http",URLZONE_CUSTOM2},
738     {"org",NULL,"http",URLZONE_CUSTOM},
739     {"org","testing","http",URLZONE_CUSTOM2},
740     {"wine.testing",NULL,"*",URLZONE_CUSTOM2}
741 };
742
743 static void register_zone_domains(void)
744 {
745     HKEY domains;
746     DWORD res, i;
747
748     /* Some Windows versions don't seem to have a "Domains" key in their HKLM. */
749     res = RegOpenKeyA(HKEY_LOCAL_MACHINE, szZoneMapDomainsKey, &domains);
750     ok(res == ERROR_SUCCESS || broken(res == ERROR_FILE_NOT_FOUND), "RegOpenKey failed: %d\n", res);
751     if(res == ERROR_SUCCESS) {
752         HKEY domain;
753         DWORD zone = URLZONE_CUSTOM;
754
755         res = RegCreateKeyA(domains, "local.machine", &domain);
756         ok(res == ERROR_SUCCESS, "RegCreateKey failed: %d\n", res);
757
758         res = RegSetValueExA(domain, "http", 0, REG_DWORD, (BYTE*)&zone, sizeof(DWORD));
759         ok(res == ERROR_SUCCESS, "RegSetValueEx failed: %d\n", res);
760
761         RegCloseKey(domain);
762         RegCloseKey(domains);
763     }
764
765     res = RegOpenKeyA(HKEY_CURRENT_USER, szZoneMapDomainsKey, &domains);
766     ok(res == ERROR_SUCCESS, "RegOpenKey failed: %d\n", res);
767
768     for(i = 0; i < sizeof(zone_domain_mappings)/sizeof(zone_domain_mappings[0]); ++i) {
769         const zone_domain_mapping *test = zone_domain_mappings+i;
770         HKEY domain;
771
772         res = RegCreateKeyA(domains, test->domain, &domain);
773         ok(res == ERROR_SUCCESS, "RegCreateKey failed with %d on test %d\n", res, i);
774
775         /* Only set the value if there's no subdomain. */
776         if(!test->subdomain) {
777             res = RegSetValueExA(domain, test->scheme, 0, REG_DWORD, (BYTE*)&test->zone, sizeof(DWORD));
778             ok(res == ERROR_SUCCESS, "RegSetValueEx failed with %d on test %d\n", res, i);
779         } else {
780             HKEY subdomain;
781
782             res = RegCreateKeyA(domain, test->subdomain, &subdomain);
783             ok(res == ERROR_SUCCESS, "RegCreateKey failed with %d on test %d\n", res, i);
784
785             res = RegSetValueExA(subdomain, test->scheme, 0, REG_DWORD, (BYTE*)&test->zone, sizeof(DWORD));
786             ok(res == ERROR_SUCCESS, "RegSetValueEx failed with %d on test %d\n", res, i);
787
788             RegCloseKey(subdomain);
789         }
790
791         RegCloseKey(domain);
792     }
793
794     RegCloseKey(domains);
795 }
796
797 static void unregister_zone_domains(void)
798 {
799     HKEY domains;
800     DWORD res, i;
801
802     res = RegOpenKeyA(HKEY_LOCAL_MACHINE, szZoneMapDomainsKey, &domains);
803     ok(res == ERROR_SUCCESS || broken(res == ERROR_FILE_NOT_FOUND), "RegOpenKey failed: %d\n", res);
804     if(res == ERROR_SUCCESS) {
805         RegDeleteKeyA(domains, "local.machine");
806         RegCloseKey(domains);
807     }
808
809     res = RegOpenKeyA(HKEY_CURRENT_USER, szZoneMapDomainsKey, &domains);
810     ok(res == ERROR_SUCCESS, "RegOpenKey failed: %d\n", res);
811
812     for(i = 0; i < sizeof(zone_domain_mappings)/sizeof(zone_domain_mappings[0]); ++i) {
813         const zone_domain_mapping *test = zone_domain_mappings+i;
814
815         /* FIXME: Uses the "cludge" approach to remove the test data from the registry!
816          *        Although, if domain names are... unique, this shouldn't cause any harm
817          *        to keys (if any) that existed before the tests.
818          */
819         if(test->subdomain) {
820             HKEY domain;
821
822             res = RegOpenKeyA(domains, test->domain, &domain);
823             if(res == ERROR_SUCCESS) {
824                 RegDeleteKeyA(domain, test->subdomain);
825                 RegCloseKey(domain);
826             }
827         }
828         RegDeleteKeyA(domains, test->domain);
829     }
830
831     RegCloseKey(domains);
832 }
833
834 static void run_child_process(void)
835 {
836     char cmdline[MAX_PATH];
837     char path[MAX_PATH];
838     char **argv;
839     PROCESS_INFORMATION pi;
840     STARTUPINFO si = { 0 };
841     BOOL ret;
842
843     GetModuleFileNameA(NULL, path, MAX_PATH);
844
845     si.cb = sizeof(si);
846     winetest_get_mainargs(&argv);
847     sprintf(cmdline, "\"%s\" %s domain_tests", argv[0], argv[1]);
848     ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
849     ok(ret, "Failed to spawn child process: %u\n", GetLastError());
850     winetest_wait_child_process(pi.hProcess);
851     CloseHandle(pi.hThread);
852     CloseHandle(pi.hProcess);
853 }
854
855 typedef struct {
856     const char  *url;
857     DWORD       zone;
858     BOOL        todo;
859     DWORD       broken_zone;
860 } zone_mapping_test;
861
862 static const zone_mapping_test zone_mapping_tests[] = {
863     /* Tests for "yabadaba.do" zone mappings. */
864     {"http://yabadaba.do/",URLZONE_CUSTOM},
865     {"http://google.yabadaba.do/",URLZONE_CUSTOM},
866     {"zip://yabadaba.do/",URLZONE_INTERNET},
867     /* Tests for "super.cool" zone mappings. */
868     {"ftp://testing.google.super.cool/",URLZONE_INTERNET},
869     {"ftp://testing.*.super.cool/",URLZONE_CUSTOM2},
870     {"ftp://google.testing.super.cool/",URLZONE_CUSTOM2},
871     /* Tests for "tests.test" zone mappings. */
872     {"http://tests.test/",URLZONE_CUSTOM},
873     {"http://www.tests.test/",URLZONE_CUSTOM},
874     {"ftp://tests.test/",URLZONE_CUSTOM},
875     {"ftp://www.tests.test/",URLZONE_CUSTOM},
876     {"test://www.tests.test/",URLZONE_INTERNET},
877     {"test://tests.test/",URLZONE_INTERNET},
878     {"zip://www.tests.test/",URLZONE_INTERNET},
879     {"zip://tests.test/",URLZONE_INTERNET},
880     /* Tests for "www.testing.com" zone mappings. */
881     {"http://google.www.testing.com/",URLZONE_INTERNET},
882     {"http://www.testing.com/",URLZONE_CUSTOM,FALSE,URLZONE_INTERNET},
883     {"http://testing.www.testing.com/",URLZONE_CUSTOM2,FALSE,URLZONE_INTERNET},
884     /* Tests for "org" zone mappings. */
885     {"http://google.org/",URLZONE_INTERNET,FALSE,URLZONE_CUSTOM},
886     {"http://org/",URLZONE_CUSTOM},
887     {"http://testing.org/",URLZONE_CUSTOM2},
888     /* Tests for "wine.testing" mapping */
889     {"*:wine.testing/test",URLZONE_CUSTOM2},
890     {"http://wine.testing/testing",URLZONE_CUSTOM2}
891 };
892
893 static void test_zone_domain_mappings(void)
894 {
895     HRESULT hres;
896     DWORD i, res;
897     IInternetSecurityManager *secmgr = NULL;
898     HKEY domains;
899     DWORD zone = URLZONE_INVALID;
900
901     trace("testing zone domain mappings...\n");
902
903     hres = pCoInternetCreateSecurityManager(NULL, &secmgr, 0);
904     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
905
906     res = RegOpenKeyA(HKEY_LOCAL_MACHINE, szZoneMapDomainsKey, &domains);
907     if(res == ERROR_SUCCESS) {
908         static const WCHAR local_machineW[] = {'h','t','t','p',':','/','/','t','e','s','t','.','l','o','c','a','l',
909                 '.','m','a','c','h','i','n','e','/',0};
910
911         hres = IInternetSecurityManager_MapUrlToZone(secmgr, local_machineW, &zone, 0);
912         ok(hres == S_OK, "MapUrlToZone failed: %08x\n", hres);
913         ok(zone == URLZONE_CUSTOM, "Expected URLZONE_CUSTOM, but got %d\n", zone);
914
915         RegCloseKey(domains);
916     }
917
918     for(i = 0; i < sizeof(zone_mapping_tests)/sizeof(zone_mapping_tests[0]); ++i) {
919         const zone_mapping_test *test = zone_mapping_tests+i;
920         LPWSTR urlW = a2w(test->url);
921         zone = URLZONE_INVALID;
922
923         hres = IInternetSecurityManager_MapUrlToZone(secmgr, urlW, &zone, 0);
924         ok(hres == S_OK, "MapUrlToZone failed: %08x\n", hres);
925         if(test->todo)
926             todo_wine
927                 ok(zone == test->zone || broken(test->broken_zone == zone),
928                     "Expected %d, but got %d on test %d\n", test->zone, zone, i);
929         else
930             ok(zone == test->zone || broken(test->broken_zone == zone),
931                 "Expected %d, but got %d on test %d\n", test->zone, zone, i);
932
933         heap_free(urlW);
934     }
935
936     IInternetSecurityManager_Release(secmgr);
937 }
938
939 static void test_zone_domains(void)
940 {
941     if(is_ie_hardened()) {
942         skip("IE running in Enhanced Security Configuration\n");
943         return;
944     } else if(!pCreateUri) {
945         win_skip("Skipping zone domain tests, IE too old\n");
946         return;
947     }
948
949     trace("testing zone domains...\n");
950
951     test_zone_domain_cache();
952
953     register_zone_domains();
954     run_child_process();
955     unregister_zone_domains();
956 }
957
958 static void test_CoInternetCreateZoneManager(void)
959 {
960     IInternetZoneManager *zonemgr = NULL;
961     IUnknown *punk = NULL;
962     HRESULT hr;
963
964     trace("simple zone manager tests...\n");
965
966     hr = pCoInternetCreateZoneManager(NULL, &zonemgr, 0);
967     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
968     if (FAILED(hr))
969         return;
970
971     hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IUnknown, (void **) &punk);
972     ok(SUCCEEDED(hr), "got 0x%x with %p (expected Success)\n", hr, punk);
973     if (punk)
974         IUnknown_Release(punk);
975
976     hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IInternetZoneManager, (void **) &punk);
977     ok(SUCCEEDED(hr), "got 0x%x with %p (expected Success)\n", hr, punk);
978     if (punk)
979         IUnknown_Release(punk);
980
981
982     hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IInternetZoneManagerEx, (void **) &punk);
983     if (SUCCEEDED(hr)) {
984         IUnknown_Release(punk);
985
986         hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IInternetZoneManagerEx2, (void **) &punk);
987         ok(hr == S_OK || broken(hr == E_NOINTERFACE /* some W2K3 */),
988            "got 0x%x (expected S_OK)\n", hr);
989         if (punk)
990             IUnknown_Release(punk);
991         else
992             win_skip("InternetZoneManagerEx2 not supported\n");
993
994     }
995     else
996         win_skip("InternetZoneManagerEx not supported\n");
997
998     hr = IInternetZoneManager_Release(zonemgr);
999     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1000
1001 }
1002
1003 static void test_CreateZoneEnumerator(void)
1004 {
1005     IInternetZoneManager *zonemgr = NULL;
1006     HRESULT hr;
1007     DWORD dwEnum;
1008     DWORD dwEnum2;
1009     DWORD dwCount;
1010     DWORD dwCount2;
1011
1012     trace("testing zone enumerator...\n");
1013
1014     hr = pCoInternetCreateZoneManager(NULL, &zonemgr, 0);
1015     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
1016     if (FAILED(hr))
1017         return;
1018
1019     dwEnum=0xdeadbeef;
1020     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, NULL, 0);
1021     ok((hr == E_INVALIDARG) && (dwEnum == 0xdeadbeef),
1022         "got 0x%x with 0x%x (expected E_INVALIDARG with 0xdeadbeef)\n", hr, dwEnum);
1023
1024     dwCount=0xdeadbeef;
1025     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, NULL, &dwCount, 0);
1026     ok((hr == E_INVALIDARG) && (dwCount == 0xdeadbeef),
1027         "got 0x%x and 0x%x (expected E_INVALIDARG and 0xdeadbeef)\n", hr, dwCount);
1028
1029     dwEnum=0xdeadbeef;
1030     dwCount=0xdeadbeef;
1031     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 0xffffffff);
1032     ok((hr == E_INVALIDARG) && (dwEnum == 0xdeadbeef) && (dwCount == 0xdeadbeef),
1033         "got 0x%x with 0x%x and 0x%x (expected E_INVALIDARG with 0xdeadbeef and 0xdeadbeef)\n",
1034         hr, dwEnum, dwCount);
1035
1036     dwEnum=0xdeadbeef;
1037     dwCount=0xdeadbeef;
1038     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 1);
1039     ok((hr == E_INVALIDARG) && (dwEnum == 0xdeadbeef) && (dwCount == 0xdeadbeef),
1040         "got 0x%x with 0x%x and 0x%x (expected E_INVALIDARG with 0xdeadbeef and 0xdeadbeef)\n",
1041         hr, dwEnum, dwCount);
1042
1043     dwEnum=0xdeadbeef;
1044     dwCount=0xdeadbeef;
1045     /* Normal use */
1046     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 0);
1047     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1048
1049     if (SUCCEEDED(hr)) {
1050         dwEnum2=0xdeadbeef;
1051         dwCount2=0xdeadbeef;
1052         hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum2, &dwCount2, 0);
1053         ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1054         if (SUCCEEDED(hr)) {
1055             /* native urlmon has an incrementing counter for dwEnum */
1056             hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum2);
1057             ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1058         }
1059
1060         hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum);
1061         ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1062
1063         /* Destroy the Enumerator twice is detected and handled in native urlmon */
1064         hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum);
1065         ok((hr == E_INVALIDARG), "got 0x%x (expected E_INVALIDARG)\n", hr);
1066     }
1067
1068     /* ::Release succeed also, when a ::DestroyZoneEnumerator is missing */
1069     hr = IInternetZoneManager_Release(zonemgr);
1070     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1071 }
1072
1073 static void test_GetZoneActionPolicy(void)
1074 {
1075     IInternetZoneManager *zonemgr = NULL;
1076     BYTE buf[32];
1077     HRESULT hres;
1078     DWORD action = URLACTION_CREDENTIALS_USE; /* Implemented on all IE versions */
1079
1080     trace("testing GetZoneActionPolixy...\n");
1081
1082     hres = pCoInternetCreateZoneManager(NULL, &zonemgr, 0);
1083     ok(hres == S_OK, "CoInternetCreateZoneManager failed: %08x\n", hres);
1084     if(FAILED(hres))
1085         return;
1086
1087     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, buf,
1088             sizeof(DWORD), URLZONEREG_DEFAULT);
1089     ok(hres == S_OK, "GetZoneActionPolicy failed: %08x\n", hres);
1090     ok(*(DWORD*)buf == URLPOLICY_CREDENTIALS_SILENT_LOGON_OK ||
1091             *(DWORD*)buf == URLPOLICY_CREDENTIALS_MUST_PROMPT_USER ||
1092             *(DWORD*)buf == URLPOLICY_CREDENTIALS_CONDITIONAL_PROMPT ||
1093             *(DWORD*)buf == URLPOLICY_CREDENTIALS_ANONYMOUS_ONLY,
1094             "unexpected policy=%d\n", *(DWORD*)buf);
1095
1096     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, NULL,
1097             sizeof(DWORD), URLZONEREG_DEFAULT);
1098     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
1099
1100     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, buf,
1101             2, URLZONEREG_DEFAULT);
1102     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
1103
1104     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, 0x1fff, buf,
1105             sizeof(DWORD), URLZONEREG_DEFAULT);
1106     ok(hres == E_FAIL || broken(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)),
1107             "(0x%x) got 0x%x (expected E_FAIL)\n", action, hres);
1108
1109     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 13, action, buf,
1110             sizeof(DWORD), URLZONEREG_DEFAULT);
1111     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
1112
1113     IInternetZoneManager_Release(zonemgr);
1114 }
1115
1116 static void test_GetZoneAt(void)
1117 {
1118     IInternetZoneManager *zonemgr = NULL;
1119     HRESULT hr;
1120     DWORD dwEnum;
1121     DWORD dwCount;
1122     DWORD dwZone;
1123     DWORD i;
1124
1125     trace("testing GetZoneAt...\n");
1126
1127     hr = pCoInternetCreateZoneManager(NULL, &zonemgr, 0);
1128     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
1129     if (FAILED(hr))
1130         return;
1131
1132     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 0);
1133     if (FAILED(hr))
1134         goto cleanup;
1135
1136     if (0) {
1137         /* this crashes with native urlmon */
1138         IInternetZoneManager_GetZoneAt(zonemgr, dwEnum, 0, NULL);
1139     }
1140
1141     dwZone = 0xdeadbeef;
1142     hr = IInternetZoneManager_GetZoneAt(zonemgr, 0xdeadbeef, 0, &dwZone);
1143     ok(hr == E_INVALIDARG,
1144         "got 0x%x with 0x%x (expected E_INVALIDARG)\n", hr, dwZone);
1145
1146     for (i = 0; i < dwCount; i++)
1147     {
1148         dwZone = 0xdeadbeef;
1149         hr = IInternetZoneManager_GetZoneAt(zonemgr, dwEnum, i, &dwZone);
1150         ok(hr == S_OK, "#%d: got x%x with %d (expected S_OK)\n", i, hr, dwZone);
1151     }
1152
1153     dwZone = 0xdeadbeef;
1154     /* MSDN (index .. must be .. less than or equal to) is wrong */
1155     hr = IInternetZoneManager_GetZoneAt(zonemgr, dwEnum, dwCount, &dwZone);
1156     ok(hr == E_INVALIDARG,
1157         "got 0x%x with 0x%x (expected E_INVALIDARG)\n", hr, dwZone);
1158
1159     hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum);
1160     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1161
1162 cleanup:
1163     hr = IInternetZoneManager_Release(zonemgr);
1164     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1165 }
1166
1167 static void test_GetZoneAttributes(void)
1168 {
1169     IInternetZoneManager *zonemgr = NULL;
1170     CHAR buffer [sizeof(ZONEATTRIBUTES) + 32];
1171     ZONEATTRIBUTES* pZA = (ZONEATTRIBUTES*) buffer;
1172     HRESULT hr;
1173     DWORD i;
1174
1175     trace("testing GetZoneAttributes...\n");
1176
1177     hr = pCoInternetCreateZoneManager(NULL, &zonemgr, 0);
1178     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
1179     if (FAILED(hr))
1180         return;
1181
1182     /* native urlmon has Zone "0" up to Zone "4" since IE4 */
1183     for (i = 0; i < 5; i++) {
1184         memset(buffer, -1, sizeof(buffer));
1185         hr = IInternetZoneManager_GetZoneAttributes(zonemgr, i, pZA);
1186         ok(hr == S_OK, "#%d: got 0x%x (expected S_OK)\n", i, hr);
1187     }
1188
1189     /* IE8 no longer set cbSize */
1190     memset(buffer, -1, sizeof(buffer));
1191     pZA->cbSize = 0;
1192     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, pZA);
1193     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1194     ok((pZA->cbSize == 0) || (pZA->cbSize == sizeof(ZONEATTRIBUTES)),
1195         "got cbSize = %d (expected 0)\n", pZA->cbSize);
1196
1197     memset(buffer, -1, sizeof(buffer));
1198     pZA->cbSize = 64;
1199     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, pZA);
1200     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1201     ok((pZA->cbSize == 64) || (pZA->cbSize == sizeof(ZONEATTRIBUTES)),
1202         "got cbSize = %d (expected 64)\n", pZA->cbSize);
1203
1204     memset(buffer, -1, sizeof(buffer));
1205     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, pZA);
1206     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1207     ok((pZA->cbSize == 0xffffffff) || (pZA->cbSize == sizeof(ZONEATTRIBUTES)),
1208         "got cbSize = 0x%x (expected 0xffffffff)\n", pZA->cbSize);
1209
1210     /* IE8 no longer fail on invalid zones */
1211     memset(buffer, -1, sizeof(buffer));
1212     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0xdeadbeef, pZA);
1213     ok(hr == S_OK || (hr == E_FAIL),
1214         "got 0x%x (expected S_OK or E_FAIL)\n", hr);
1215
1216     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, NULL);
1217     ok(hr == E_INVALIDARG, "got 0x%x (expected E_INVALIDARG)\n", hr);
1218
1219     hr = IInternetZoneManager_Release(zonemgr);
1220     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1221 }
1222
1223 static void test_SetZoneAttributes(void)
1224 {
1225     IInternetZoneManager *zonemgr = NULL;
1226     CHAR buffer [sizeof(ZONEATTRIBUTES) + 16];
1227     ZONEATTRIBUTES* pZA = (ZONEATTRIBUTES*) buffer;
1228     CHAR regpath[MAX_PATH];
1229     HKEY hkey;
1230     HRESULT hr;
1231     DWORD res;
1232
1233     trace("testing SetZoneAttributes...\n");
1234     hr = pCoInternetCreateZoneManager(NULL, &zonemgr, 0);
1235     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
1236     if (FAILED(hr))
1237         return;
1238
1239     memset(buffer, -1, sizeof(buffer));
1240     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, URLZONE_LOCAL_MACHINE, pZA);
1241     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1242
1243     sprintf(regpath, "%s\\Zones\\%d", szInternetSettingsKey, URLZONE_CUSTOM);
1244     res = RegCreateKeyA(HKEY_CURRENT_USER, regpath, &hkey);
1245     RegCloseKey(hkey);
1246
1247     ok(res == ERROR_SUCCESS, "got %d (expected ERROR_SUCCESS)\n", res);
1248     if (res != ERROR_SUCCESS)
1249         goto cleanup;
1250
1251     pZA->cbSize = sizeof(ZONEATTRIBUTES);
1252     hr = IInternetZoneManager_SetZoneAttributes(zonemgr, URLZONE_CUSTOM, NULL);
1253     ok(hr == E_INVALIDARG, "got 0x%x (expected E_INVALIDARG)\n", hr);
1254
1255     /* normal use */
1256     hr = IInternetZoneManager_SetZoneAttributes(zonemgr, URLZONE_CUSTOM, pZA);
1257     if (hr == E_FAIL) {
1258         win_skip("SetZoneAttributes not supported: IE too old\n");
1259         goto cleanup;
1260     }
1261     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1262
1263     /* native urlmon ignores cbSize */
1264     pZA->cbSize = sizeof(ZONEATTRIBUTES) + sizeof(DWORD);
1265     hr = IInternetZoneManager_SetZoneAttributes(zonemgr, URLZONE_CUSTOM, pZA);
1266     ok(hr == S_OK, "got 0x%x for sizeof(ZONEATTRIBUTES) + sizeof(DWORD) (expected S_OK)\n", hr);
1267
1268     pZA->cbSize = sizeof(ZONEATTRIBUTES) - sizeof(DWORD);
1269     hr = IInternetZoneManager_SetZoneAttributes(zonemgr, URLZONE_CUSTOM, pZA);
1270     ok(hr == S_OK, "got 0x%x for sizeof(ZONEATTRIBUTES) - sizeof(DWORD) (expected S_OK)\n", hr);
1271
1272     pZA->cbSize = 0;
1273     hr = IInternetZoneManager_SetZoneAttributes(zonemgr, URLZONE_CUSTOM, pZA);
1274     ok(hr == S_OK, "got 0x%x for size 0 (expected S_OK)\n", hr);
1275
1276     /* The key for the zone must be present, when calling SetZoneAttributes */
1277     myRegDeleteTreeA(HKEY_CURRENT_USER, regpath);
1278     /* E_FAIL is returned from IE6 here, which is reasonable.
1279        All newer IE return S_OK without saving the zone attributes to the registry.
1280        This is a Windows bug, but we have to accept that as standard */
1281     hr = IInternetZoneManager_SetZoneAttributes(zonemgr, URLZONE_CUSTOM, pZA);
1282     ok((hr == S_OK) || broken(hr == E_FAIL), "got 0x%x (expected S_OK)\n", hr);
1283
1284     /* SetZoneAttributes did not create the directory */
1285     res = RegOpenKeyA(HKEY_CURRENT_USER, regpath, &hkey);
1286     ok((res == ERROR_FILE_NOT_FOUND) && (hkey == NULL),
1287         "got %u with %p (expected ERROR_FILE_NOT_FOUND with NULL)\n", res, hkey);
1288
1289     if (hkey) RegCloseKey(hkey);
1290
1291 cleanup:
1292     /* delete zone settings in the registry */
1293     myRegDeleteTreeA(HKEY_CURRENT_USER, regpath);
1294
1295     hr = IInternetZoneManager_Release(zonemgr);
1296     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
1297 }
1298
1299
1300 static void test_InternetSecurityMarshalling(void)
1301 {
1302     IInternetSecurityManager *secmgr = NULL;
1303     IUnknown *unk;
1304     IStream *stream;
1305     HRESULT hres;
1306
1307     trace("testing marshalling...\n");
1308
1309     hres = pCoInternetCreateSecurityManager(NULL, &secmgr, 0);
1310     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
1311     if(FAILED(hres))
1312         return;
1313
1314     hres = IInternetSecurityManager_QueryInterface(secmgr, &IID_IUnknown, (void**)&unk);
1315     ok(hres == S_OK, "QueryInterface returned: %08x\n", hres);
1316
1317     hres = CreateStreamOnHGlobal(NULL, TRUE, &stream);
1318     ok(hres == S_OK, "CreateStreamOnHGlobal returned: %08x\n", hres);
1319
1320     hres = CoMarshalInterface(stream, &IID_IInternetSecurityManager, unk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1321     /* Not supported in W98 */
1322     ok(hres == S_OK || broken(hres == REGDB_E_IIDNOTREG),
1323         "CoMarshalInterface returned: %08x\n", hres);
1324
1325     IStream_Release(stream);
1326     IUnknown_Release(unk);
1327     IInternetSecurityManager_Release(secmgr);
1328 }
1329
1330 static void test_InternetGetSecurityUrl(void)
1331 {
1332     const WCHAR url5_out[] = {'h','t','t','p',':','w','w','w','.','z','o','n','e','3',
1333                               '.','w','i','n','e','t','e','s','t',0};
1334     const WCHAR url7_out[] = {'f','t','p',':','z','o','n','e','3','.','w','i','n','e','t','e','s','t',0};
1335
1336     const WCHAR *in[] = {url2, url3, url4, url5, url7, url8, url9, url10};
1337     const WCHAR *out_default[] = {url2, url3, url4, url5_out, url7_out, url8, url5_out, url10};
1338     const WCHAR *out_securl[] = {url2, url3, url4, url5, url7, url8, url9, url10};
1339
1340     WCHAR *sec;
1341     DWORD i;
1342     HRESULT hres;
1343
1344     trace("testing CoInternetGetSecurityUrl...\n");
1345
1346     for(i=0; i<sizeof(in)/sizeof(WCHAR*); i++) {
1347         hres = pCoInternetGetSecurityUrl(in[i], &sec, PSU_DEFAULT, 0);
1348         ok(hres == S_OK, "(%d) CoInternetGetSecurityUrl returned: %08x\n", i, hres);
1349         if(hres == S_OK) {
1350             ok(!strcmp_w(sec, out_default[i]), "(%d) Got %s, expected %s\n",
1351                     i, wine_dbgstr_w(sec), wine_dbgstr_w(out_default[i]));
1352             CoTaskMemFree(sec);
1353         }
1354
1355         hres = pCoInternetGetSecurityUrl(in[i], &sec, PSU_SECURITY_URL_ONLY, 0);
1356         ok(hres == S_OK, "(%d) CoInternetGetSecurityUrl returned: %08x\n", i, hres);
1357         if(hres == S_OK) {
1358             ok(!strcmp_w(sec, out_securl[i]), "(%d) Got %s, expected %s\n",
1359                     i, wine_dbgstr_w(sec), wine_dbgstr_w(out_securl[i]));
1360             CoTaskMemFree(sec);
1361         }
1362     }
1363
1364     SET_EXPECT(ParseUrl_SECURITY_URL_input2);
1365     SET_EXPECT(ParseUrl_SECURITY_URL_expected);
1366     SET_EXPECT(ParseUrl_SECURITY_DOMAIN_expected);
1367
1368     hres = pCoInternetGetSecurityUrl(security_url2W, &sec, PSU_DEFAULT, 0);
1369     ok(hres == S_OK, "CoInternetGetSecurityUrl returned 0x%08x, expected S_OK\n", hres);
1370
1371     CHECK_CALLED(ParseUrl_SECURITY_URL_input2);
1372     CHECK_CALLED(ParseUrl_SECURITY_URL_expected);
1373     CHECK_CALLED(ParseUrl_SECURITY_DOMAIN_expected);
1374
1375     ok(!lstrcmpW(security_expectedW, sec), "Expected %s but got %s\n",
1376        wine_dbgstr_w(security_expectedW), wine_dbgstr_w(sec));
1377     CoTaskMemFree(sec);
1378 }
1379
1380 static HRESULT WINAPI InternetProtocolInfo_QueryInterface(IInternetProtocolInfo *iface,
1381                                                           REFIID riid, void **ppv)
1382 {
1383     ok(0, "unexpected call\n");
1384     return E_NOINTERFACE;
1385 }
1386
1387 static ULONG WINAPI InternetProtocolInfo_AddRef(IInternetProtocolInfo *iface)
1388 {
1389     return 2;
1390 }
1391
1392 static ULONG WINAPI InternetProtocolInfo_Release(IInternetProtocolInfo *iface)
1393 {
1394     return 1;
1395 }
1396
1397 static HRESULT WINAPI InternetProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
1398         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
1399         DWORD *pcchResult, DWORD dwReserved)
1400 {
1401     const WCHAR *ret = NULL;
1402
1403     ok(pwzResult != NULL, "pwzResult == NULL\n");
1404     ok(pcchResult != NULL, "pcchResult == NULL\n");
1405     ok(!dwParseFlags, "Expected 0, but got 0x%08x\n", dwParseFlags);
1406
1407     switch(ParseAction) {
1408     case PARSE_SECURITY_URL:
1409         if(!strcmp_w(pwzUrl, security_urlW)) {
1410             CHECK_EXPECT(ParseUrl_SECURITY_URL_input);
1411             ok(cchResult == lstrlenW(pwzUrl)+1, "Got %d\n", cchResult);
1412             ret = security_expectedW;
1413         } else if(!strcmp_w(pwzUrl, security_url2W)) {
1414             CHECK_EXPECT(ParseUrl_SECURITY_URL_input2);
1415             ok(cchResult == lstrlenW(pwzUrl)+1, "Got %d\n", cchResult);
1416             ret = security_expectedW;
1417         } else if(!strcmp_w(pwzUrl, security_expectedW)) {
1418             CHECK_EXPECT(ParseUrl_SECURITY_URL_expected);
1419             ok(cchResult == lstrlenW(pwzUrl)+1, "Got %d\n", cchResult);
1420             ret = security_expectedW;
1421         } else if(!strcmp_w(pwzUrl, winetest_to_httpW)) {
1422             switch(++called_securl_http) {
1423             case 1:
1424                 ok(cchResult == lstrlenW(pwzUrl)+1, "Got %d\n", cchResult);
1425                 break;
1426             case 2:
1427                 CHECK_EXPECT(ParseUrl_SECURITY_URL_http);
1428                 ok(cchResult == lstrlenW(url9)+1, "Got %d\n", cchResult);
1429                 break;
1430             default:
1431                 todo_wine CHECK_EXPECT(ParseUrl_SECURITY_URL_http);
1432             }
1433             ret = url9;
1434         } else
1435             ok(0, "Unexpected call, pwzUrl=%s\n", wine_dbgstr_w(pwzUrl));
1436
1437         break;
1438     case PARSE_SECURITY_DOMAIN:
1439
1440         CHECK_EXPECT(ParseUrl_SECURITY_DOMAIN_expected);
1441
1442         ok(!strcmp_w(pwzUrl, security_expectedW), "Expected %s but got %s\n",
1443             wine_dbgstr_w(security_expectedW), wine_dbgstr_w(pwzUrl));
1444         ok(cchResult == lstrlenW(pwzUrl)+1, "Got %d\n", cchResult);
1445         ret = security_expectedW;
1446         break;
1447     default:
1448         ok(0, "Unexpected call, ParseAction=%d pwzUrl=%s\n", ParseAction,
1449             wine_dbgstr_w(pwzUrl));
1450     }
1451
1452     if(!ret)
1453         return E_FAIL;
1454
1455     *pcchResult = lstrlenW(ret)+1;
1456     if(*pcchResult > cchResult)
1457         return S_FALSE;
1458     memcpy(pwzResult, ret, (*pcchResult)*sizeof(WCHAR));
1459     return S_OK;
1460 }
1461
1462 static HRESULT WINAPI InternetProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
1463         LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
1464         LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult, DWORD dwReserved)
1465 {
1466     ok(0, "unexpected call\n");
1467     return E_NOTIMPL;
1468 }
1469
1470 static HRESULT WINAPI InternetProtocolInfo_CompareUrl(IInternetProtocolInfo *iface,
1471         LPCWSTR pwzUrl1, LPCWSTR pwzUrl2, DWORD dwCompareFlags)
1472 {
1473     ok(0, "unexpected call\n");
1474     return E_NOTIMPL;
1475 }
1476
1477 static HRESULT WINAPI InternetProtocolInfo_QueryInfo(IInternetProtocolInfo *iface,
1478         LPCWSTR pwzUrl, QUERYOPTION OueryOption, DWORD dwQueryFlags, LPVOID pBuffer,
1479         DWORD cbBuffer, DWORD *pcbBuf, DWORD dwReserved)
1480 {
1481     ok(0, "unexpected call\n");
1482     return E_NOTIMPL;
1483 }
1484
1485 static const IInternetProtocolInfoVtbl InternetProtocolInfoVtbl = {
1486     InternetProtocolInfo_QueryInterface,
1487     InternetProtocolInfo_AddRef,
1488     InternetProtocolInfo_Release,
1489     InternetProtocolInfo_ParseUrl,
1490     InternetProtocolInfo_CombineUrl,
1491     InternetProtocolInfo_CompareUrl,
1492     InternetProtocolInfo_QueryInfo
1493 };
1494
1495 static IInternetProtocolInfo protocol_info = { &InternetProtocolInfoVtbl };
1496
1497 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
1498 {
1499     if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
1500         *ppv = &protocol_info;
1501         return S_OK;
1502     }
1503
1504     ok(0, "unexpected call\n");
1505     return E_NOINTERFACE;
1506 }
1507
1508 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
1509 {
1510     return 2;
1511 }
1512
1513 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
1514 {
1515     return 1;
1516 }
1517
1518 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
1519                                         REFIID riid, void **ppv)
1520 {
1521     ok(0, "unexpected call\n");
1522     return E_NOTIMPL;
1523 }
1524
1525 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
1526 {
1527     ok(0, "unexpected call\n");
1528     return S_OK;
1529 }
1530
1531 static const IClassFactoryVtbl ClassFactoryVtbl = {
1532     ClassFactory_QueryInterface,
1533     ClassFactory_AddRef,
1534     ClassFactory_Release,
1535     ClassFactory_CreateInstance,
1536     ClassFactory_LockServer
1537 };
1538
1539 static IClassFactory protocol_cf = { &ClassFactoryVtbl };
1540
1541 static void register_protocols(void)
1542 {
1543     IInternetSession *session;
1544     HRESULT hres;
1545
1546     hres = pCoInternetGetSession(0, &session, 0);
1547     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
1548     if(FAILED(hres))
1549         return;
1550
1551     hres = IInternetSession_RegisterNameSpace(session, &protocol_cf, &IID_NULL,
1552             winetestW, 0, NULL, 0);
1553     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
1554
1555     IInternetSession_Release(session);
1556 }
1557
1558 static void unregister_protocols(void) {
1559     IInternetSession *session;
1560     HRESULT hr;
1561
1562     hr = pCoInternetGetSession(0, &session, 0);
1563     ok(hr == S_OK, "CoInternetGetSession failed: 0x%08x\n", hr);
1564     if(FAILED(hr))
1565         return;
1566
1567     hr = IInternetSession_UnregisterNameSpace(session, &protocol_cf, winetestW);
1568     ok(hr == S_OK, "UnregisterNameSpace failed: 0x%08x\n", hr);
1569
1570     IInternetSession_Release(session);
1571 }
1572
1573 static const struct {
1574     const char  *uri;
1575     DWORD       create_flags;
1576     const char  *security_uri;
1577     HRESULT     security_hres;
1578     const char  *default_uri;
1579     HRESULT     default_hres;
1580     BOOL        todo;
1581 } sec_url_ex_tests[] = {
1582     {"index.htm",Uri_CREATE_ALLOW_RELATIVE,"*:index.html",S_OK,"*:index.htm",S_OK},
1583     {"file://c:\\Index.htm",Uri_CREATE_FILE_USE_DOS_PATH,"file:///c:/Index.htm",S_OK,"file:///c:/Index.htm",S_OK},
1584     {"file:some%20file%2ejpg",0,NULL,E_INVALIDARG,NULL,E_INVALIDARG},
1585     {"file:some file.jpg",0,NULL,E_INVALIDARG,NULL,E_INVALIDARG},
1586     {"http://www.zone3.winetest/",0,"http://www.zone3.winetest/",S_OK,"http://www.zone3.winetest/",S_OK},
1587     {"about:blank",0,"about:blank",S_OK,"about:blank",S_OK},
1588     {"ftp://zone3.winetest/file.test",0,"ftp://zone3.winetest/file.test",S_OK,"ftp://zone3.winetest/file.test",S_OK},
1589     {"test:123abc",0,"test:123abc",S_OK,"test:123abc",S_OK},
1590     {"http:google.com/test.file",0,"http:google.com/test.file",S_OK,"http:google.com/test.file",S_OK},
1591     {"ftp://test@ftp.winehq.org/",0,"ftp://ftp.winehq.org/",S_OK,"ftp://ftp.winehq.org/",S_OK},
1592     {"test://google@ftp.winehq.org/",0,"test://google@ftp.winehq.org/",S_OK,"test://google@ftp.winehq.org/",S_OK}
1593 };
1594
1595 static void test_InternetGetSecurityUrlEx(void)
1596 {
1597     HRESULT hr;
1598     DWORD i;
1599     IUri *uri = NULL, *result = NULL;
1600
1601     trace("testing CoInternetGetSecurityUrlEx...\n");
1602
1603     hr = pCoInternetGetSecurityUrlEx(NULL, NULL, PSU_DEFAULT, 0);
1604     ok(hr == E_INVALIDARG, "CoInternetGetSecurityUrlEx returned 0x%08x, expected E_INVALIDARG\n", hr);
1605
1606     result = (void*) 0xdeadbeef;
1607     hr = pCoInternetGetSecurityUrlEx(NULL, &result, PSU_DEFAULT, 0);
1608     ok(hr == E_INVALIDARG, "CoInternetGetSecurityUrlEx returned 0x%08x, expected E_INVALIDARG\n", hr);
1609     ok(result == (void*) 0xdeadbeef, "'result' was %p\n", result);
1610
1611     for(i = 0; i < sizeof(sec_url_ex_tests)/sizeof(sec_url_ex_tests[0]); ++i) {
1612         LPWSTR uriW = a2w(sec_url_ex_tests[i].uri);
1613         uri = NULL;
1614
1615         hr = pCreateUri(uriW, sec_url_ex_tests[i].create_flags, 0, &uri);
1616         ok(hr == S_OK, "CreateUri returned 0x%08x on test %d\n", hr, i);
1617         if(hr == S_OK) {
1618             result = NULL;
1619
1620             hr = pCoInternetGetSecurityUrlEx(uri, &result, PSU_DEFAULT, 0);
1621             if(sec_url_ex_tests[i].todo) {
1622                 todo_wine
1623                     ok(hr == sec_url_ex_tests[i].default_hres,
1624                         "CoInternetGetSecurityUrlEx returned 0x%08x, expected 0x%08x on test %d\n",
1625                         hr, sec_url_ex_tests[i].default_hres, i);
1626             } else {
1627                 ok(hr == sec_url_ex_tests[i].default_hres,
1628                     "CoInternetGetSecurityUrlEx returned 0x%08x, expected 0x%08x on test %d\n",
1629                     hr, sec_url_ex_tests[i].default_hres, i);
1630             }
1631             if(SUCCEEDED(hr)) {
1632                 BSTR received;
1633
1634                 hr = IUri_GetDisplayUri(result, &received);
1635                 ok(hr == S_OK, "GetDisplayUri returned 0x%08x on test %d\n", hr, i);
1636                 if(hr == S_OK) {
1637                     if(sec_url_ex_tests[i].todo) {
1638                         todo_wine
1639                             ok(!strcmp_aw(sec_url_ex_tests[i].default_uri, received),
1640                                 "Expected %s but got %s on test %d\n", sec_url_ex_tests[i].default_uri,
1641                                 wine_dbgstr_w(received), i);
1642                     } else {
1643                         ok(!strcmp_aw(sec_url_ex_tests[i].default_uri, received),
1644                             "Expected %s but got %s on test %d\n", sec_url_ex_tests[i].default_uri,
1645                             wine_dbgstr_w(received), i);
1646                     }
1647                 }
1648                 SysFreeString(received);
1649             }
1650             if(result) IUri_Release(result);
1651
1652             result = NULL;
1653             hr = pCoInternetGetSecurityUrlEx(uri, &result, PSU_SECURITY_URL_ONLY, 0);
1654             if(sec_url_ex_tests[i].todo) {
1655                 todo_wine
1656                     ok(hr == sec_url_ex_tests[i].default_hres,
1657                         "CoInternetGetSecurityUrlEx returned 0x%08x, expected 0x%08x on test %d\n",
1658                         hr, sec_url_ex_tests[i].default_hres, i);
1659             } else {
1660                 ok(hr == sec_url_ex_tests[i].default_hres,
1661                     "CoInternetGetSecurityUrlEx returned 0x%08x, expected 0x%08x on test %d\n",
1662                     hr, sec_url_ex_tests[i].default_hres, i);
1663             }
1664             if(SUCCEEDED(hr)) {
1665                 BSTR received;
1666
1667                 hr = IUri_GetDisplayUri(result, &received);
1668                 ok(hr == S_OK, "GetDisplayUri returned 0x%08x on test %d\n", hr, i);
1669                 if(hr == S_OK) {
1670                     if(sec_url_ex_tests[i].todo) {
1671                         todo_wine
1672                             ok(!strcmp_aw(sec_url_ex_tests[i].default_uri, received),
1673                                 "Expected %s but got %s on test %d\n", sec_url_ex_tests[i].default_uri,
1674                                 wine_dbgstr_w(received), i);
1675                     } else {
1676                         ok(!strcmp_aw(sec_url_ex_tests[i].default_uri, received),
1677                             "Expected %s but got %s on test %d\n", sec_url_ex_tests[i].default_uri,
1678                             wine_dbgstr_w(received), i);
1679                     }
1680                 }
1681                 SysFreeString(received);
1682             }
1683             if(result) IUri_Release(result);
1684         }
1685
1686         if(uri) IUri_Release(uri);
1687         heap_free(uriW);
1688     }
1689 }
1690
1691 static void test_InternetGetSecurityUrlEx_Pluggable(void)
1692 {
1693     HRESULT hr;
1694     IUri *uri = NULL, *result;
1695
1696     trace("testing CoInternetGetSecurityUrlEx for pluggable protocols...\n");
1697
1698     hr = pCreateUri(security_urlW, 0, 0, &uri);
1699     ok(hr == S_OK, "CreateUri returned 0x%08x\n", hr);
1700     if(hr == S_OK) {
1701         SET_EXPECT(ParseUrl_SECURITY_URL_input);
1702         SET_EXPECT(ParseUrl_SECURITY_URL_expected);
1703         SET_EXPECT(ParseUrl_SECURITY_DOMAIN_expected);
1704
1705         hr = pCoInternetGetSecurityUrlEx(uri, &result, PSU_DEFAULT, 0);
1706         ok(hr == S_OK, "CoInternetGetSecurityUrlEx returned 0x%08x, expected S_OK\n", hr);
1707
1708         CHECK_CALLED(ParseUrl_SECURITY_URL_input);
1709         CHECK_CALLED(ParseUrl_SECURITY_URL_expected);
1710         CHECK_CALLED(ParseUrl_SECURITY_DOMAIN_expected);
1711
1712         if(hr == S_OK) {
1713             BSTR received = NULL;
1714
1715             hr = IUri_GetAbsoluteUri(result, &received);
1716             ok(hr == S_OK, "GetAbsoluteUri returned 0x%08x\n", hr);
1717             if(hr == S_OK) {
1718                 ok(!strcmp_w(security_expectedW, received), "Expected %s but got %s\n",
1719                     wine_dbgstr_w(security_expectedW), wine_dbgstr_w(received));
1720             }
1721             SysFreeString(received);
1722         }
1723         if(result) IUri_Release(result);
1724
1725         result = NULL;
1726
1727         SET_EXPECT(ParseUrl_SECURITY_URL_input);
1728         SET_EXPECT(ParseUrl_SECURITY_URL_expected);
1729
1730         hr = pCoInternetGetSecurityUrlEx(uri, &result, PSU_SECURITY_URL_ONLY, 0);
1731         ok(hr == S_OK, "CoInternetGetSecurityUrlEx returned 0x%08x, expected S_OK\n", hr);
1732
1733         CHECK_CALLED(ParseUrl_SECURITY_URL_input);
1734         CHECK_CALLED(ParseUrl_SECURITY_URL_expected);
1735
1736         if(hr == S_OK) {
1737             BSTR received = NULL;
1738
1739             hr = IUri_GetAbsoluteUri(result, &received);
1740             ok(hr == S_OK, "GetAbsoluteUri returned 0x%08x\n", hr);
1741             if(hr == S_OK) {
1742                 ok(!strcmp_w(security_expectedW, received), "Expected %s but got %s\n",
1743                     wine_dbgstr_w(security_expectedW), wine_dbgstr_w(received));
1744             }
1745             SysFreeString(received);
1746         }
1747         if(result) IUri_Release(result);
1748     }
1749     if(uri) IUri_Release(uri);
1750 }
1751
1752 static const BYTE secidex2_1[] = {'z','i','p',':','/','/','t','e','s','t','i','n','g','.','c','o','m','/',3,0,0,0};
1753 static const BYTE secidex2_2[] = {'z','i','p',':','t','e','s','t','i','n','g','.','c','o','m',3,0,0,0};
1754 static const BYTE secidex2_3[] = {'*',':','t','e','s','t','i','n','g','.','c','o','m',3,0,0,0};
1755
1756 static const struct {
1757     const char  *uri;
1758     DWORD       create_flags;
1759     HRESULT     map_hres;
1760     DWORD       zone;
1761     BOOL        map_todo;
1762     const BYTE  *secid;
1763     DWORD       secid_size;
1764     HRESULT     secid_hres;
1765     BOOL        secid_todo;
1766 } sec_mgr_ex2_tests[] = {
1767     {"res://mshtml.dll/blank.htm",0,S_OK,URLZONE_LOCAL_MACHINE,FALSE,secid1,sizeof(secid1),S_OK},
1768     {"index.htm",Uri_CREATE_ALLOW_RELATIVE,0,URLZONE_INTERNET,FALSE,secid2,sizeof(secid2),S_OK},
1769     {"file://c:\\Index.html",0,0,URLZONE_LOCAL_MACHINE,FALSE,secid1,sizeof(secid1),S_OK},
1770     {"http://www.zone3.winetest/",0,0,URLZONE_INTERNET,FALSE,secid5,sizeof(secid5),S_OK},
1771     {"about:blank",0,0,URLZONE_INTERNET,FALSE,secid6,sizeof(secid6),S_OK},
1772     {"ftp://zone3.winetest/file.test",0,0,URLZONE_INTERNET,FALSE,secid7,sizeof(secid7),S_OK},
1773     {"/file/testing/test.test",Uri_CREATE_ALLOW_RELATIVE,0,URLZONE_INTERNET,FALSE,NULL,0,E_INVALIDARG},
1774     {"zip://testing.com/",0,0,URLZONE_INTERNET,FALSE,secidex2_1,sizeof(secidex2_1),S_OK},
1775     {"zip:testing.com",0,0,URLZONE_INTERNET,FALSE,secidex2_2,sizeof(secidex2_2),S_OK},
1776     {"http:google.com",0,S_OK,URLZONE_INVALID,FALSE,NULL,0,E_INVALIDARG},
1777     {"http:/google.com",0,S_OK,URLZONE_INVALID,FALSE,NULL,0,E_INVALIDARG},
1778     {"*:/testing",0,S_OK,URLZONE_INTERNET,FALSE,NULL,0,E_INVALIDARG},
1779     {"*://testing.com",0,S_OK,URLZONE_INTERNET,FALSE,secidex2_3,sizeof(secidex2_3),S_OK}
1780 };
1781
1782 static void test_SecurityManagerEx2(void)
1783 {
1784     HRESULT hres;
1785     DWORD i, zone;
1786     BYTE buf[512];
1787     DWORD buf_size = sizeof(buf);
1788     IInternetSecurityManager *sec_mgr;
1789     IInternetSecurityManagerEx2 *sec_mgr2;
1790     IUri *uri = NULL;
1791
1792     static const WCHAR domainW[] = {'c','o','m','.','u','k',0};
1793
1794     if(!pCreateUri) {
1795         win_skip("Skipping SecurityManagerEx2, IE is too old\n");
1796         return;
1797     }
1798
1799     trace("Testing SecurityManagerEx2...\n");
1800
1801     hres = pCoInternetCreateSecurityManager(NULL, &sec_mgr, 0);
1802     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
1803
1804     hres = IInternetSecurityManager_QueryInterface(sec_mgr, &IID_IInternetSecurityManagerEx2, (void**)&sec_mgr2);
1805     ok(hres == S_OK, "QueryInterface(IID_IInternetSecurityManagerEx2) failed: %08x\n", hres);
1806
1807     zone = 0xdeadbeef;
1808
1809     hres = IInternetSecurityManagerEx2_MapUrlToZoneEx2(sec_mgr2, NULL, &zone, 0, NULL, NULL);
1810     ok(hres == E_INVALIDARG, "MapUrlToZoneEx2 returned %08x, expected E_INVALIDARG\n", hres);
1811     ok(zone == URLZONE_INVALID, "zone was %d\n", zone);
1812
1813     hres = IInternetSecurityManagerEx2_GetSecurityIdEx2(sec_mgr2, NULL, buf, &buf_size, 0);
1814     ok(hres == E_INVALIDARG, "GetSecurityIdEx2 returned %08x, expected E_INVALIDARG\n", hres);
1815     ok(buf_size == sizeof(buf), "buf_size was %d\n", buf_size);
1816
1817     hres = pCreateUri(url5, 0, 0, &uri);
1818     ok(hres == S_OK, "CreateUri failed: %08x\n", hres);
1819
1820     hres = IInternetSecurityManagerEx2_MapUrlToZoneEx2(sec_mgr2, uri, NULL, 0, NULL, NULL);
1821     ok(hres == E_INVALIDARG, "MapToUrlZoneEx2 returned %08x, expected E_INVALIDARG\n", hres);
1822
1823     buf_size = sizeof(buf);
1824     hres = IInternetSecurityManagerEx2_GetSecurityIdEx2(sec_mgr2, uri, NULL, &buf_size, 0);
1825     ok(hres == E_INVALIDARG || broken(hres == S_OK), "GetSecurityIdEx2 failed: %08x\n", hres);
1826     ok(buf_size == sizeof(buf), "bug_size was %d\n", buf_size);
1827
1828     hres = IInternetSecurityManagerEx2_GetSecurityIdEx2(sec_mgr2, uri, buf, NULL, 0);
1829     ok(hres == E_INVALIDARG, "GetSecurityIdEx2 returned %08x, expected E_INVALIDARG\n", hres);
1830
1831     IUri_Release(uri);
1832
1833     for(i = 0; i < sizeof(sec_mgr_ex2_tests)/sizeof(sec_mgr_ex2_tests[0]); ++i) {
1834         LPWSTR uriW = a2w(sec_mgr_ex2_tests[i].uri);
1835
1836         uri = NULL;
1837         zone = URLZONE_INVALID;
1838
1839         hres = pCreateUri(uriW, sec_mgr_ex2_tests[i].create_flags, 0, &uri);
1840         ok(hres == S_OK, "CreateUri returned %08x for '%s'\n", hres, sec_mgr_ex2_tests[i].uri);
1841
1842         hres = IInternetSecurityManagerEx2_MapUrlToZoneEx2(sec_mgr2, uri, &zone, 0, NULL, NULL);
1843         if(sec_mgr_ex2_tests[i].map_todo) {
1844             todo_wine
1845                 ok(hres == sec_mgr_ex2_tests[i].map_hres, "MapUrlZoneToEx2 returned %08x, expected %08x for '%s'\n",
1846                     hres, sec_mgr_ex2_tests[i].map_hres, sec_mgr_ex2_tests[i].uri);
1847             todo_wine
1848                 ok(zone == sec_mgr_ex2_tests[i].zone, "Expected zone %d, but got %d for '%s'\n", sec_mgr_ex2_tests[i].zone,
1849                     zone, sec_mgr_ex2_tests[i].uri);
1850         } else {
1851             ok(hres == sec_mgr_ex2_tests[i].map_hres, "MapUrlToZoneEx2 returned %08x, expected %08x for '%s'\n",
1852                 hres, sec_mgr_ex2_tests[i].map_hres, sec_mgr_ex2_tests[i].uri);
1853             ok(zone == sec_mgr_ex2_tests[i].zone, "Expected zone %d, but got %d for '%s'\n", sec_mgr_ex2_tests[i].zone,
1854                 zone, sec_mgr_ex2_tests[i].uri);
1855         }
1856
1857         buf_size = sizeof(buf);
1858         memset(buf, 0xf0, buf_size);
1859
1860         hres = IInternetSecurityManagerEx2_GetSecurityIdEx2(sec_mgr2, uri, buf, &buf_size, 0);
1861         if(sec_mgr_ex2_tests[i].secid_todo) {
1862             todo_wine
1863                 ok(hres == sec_mgr_ex2_tests[i].secid_hres, "GetSecurityIdEx2 returned %08x, expected %08x on test '%s'\n",
1864                     hres, sec_mgr_ex2_tests[i].secid_hres, sec_mgr_ex2_tests[i].uri);
1865             if(sec_mgr_ex2_tests[i].secid) {
1866                 todo_wine {
1867                     ok(buf_size == sec_mgr_ex2_tests[i].secid_size, "Got wrong security id size=%d, expected %d on test '%s'\n",
1868                         buf_size, sec_mgr_ex2_tests[i].secid_size, sec_mgr_ex2_tests[i].uri);
1869                     ok(!memcmp(buf, sec_mgr_ex2_tests[i].secid, sec_mgr_ex2_tests[i].secid_size), "Got wrong security id on test '%s'\n",
1870                         sec_mgr_ex2_tests[i].uri);
1871                 }
1872             }
1873         } else {
1874             ok(hres == sec_mgr_ex2_tests[i].secid_hres, "GetSecurityIdEx2 returned %08x, expected %08x on test '%s'\n",
1875                 hres, sec_mgr_ex2_tests[i].secid_hres, sec_mgr_ex2_tests[i].uri);
1876             if(sec_mgr_ex2_tests[i].secid) {
1877                 ok(buf_size == sec_mgr_ex2_tests[i].secid_size, "Got wrong security id size=%d, expected %d on test '%s'\n",
1878                     buf_size, sec_mgr_ex2_tests[i].secid_size, sec_mgr_ex2_tests[i].uri);
1879                 ok(!memcmp(buf, sec_mgr_ex2_tests[i].secid, sec_mgr_ex2_tests[i].secid_size), "Got wrong security id on test '%s'\n",
1880                     sec_mgr_ex2_tests[i].uri);
1881             }
1882         }
1883
1884         heap_free(uriW);
1885         IUri_Release(uri);
1886     }
1887
1888     hres = pCreateUri(url15, 0, 0, &uri);
1889     ok(hres == S_OK, "CreateUri failed: %08x\n", hres);
1890
1891     buf_size = sizeof(buf);
1892     memset(buf, 0xf0, buf_size);
1893
1894     hres = IInternetSecurityManagerEx2_GetSecurityIdEx2(sec_mgr2, uri, buf, &buf_size, (DWORD_PTR)domainW);
1895     ok(hres == S_OK, "GetSecurityIdEx2 failed: %08x\n", hres);
1896     todo_wine ok(buf_size == sizeof(secid13), "buf_size was %d\n", buf_size);
1897     todo_wine ok(!memcmp(buf, secid13, sizeof(secid13)), "Got wrong secid\n");
1898
1899     buf_size = sizeof(buf);
1900     memset(buf, 0xf0, buf_size);
1901
1902     hres = IInternetSecurityManagerEx2_GetSecurityIdEx2(sec_mgr2, uri, buf, &buf_size, 0);
1903     ok(hres == S_OK, "GetSecurityIdEx2 failed: %08x\n", hres);
1904     ok(buf_size == sizeof(secid13_2), "buf_size was %d\n", buf_size);
1905     ok(!memcmp(buf, secid13_2, sizeof(secid13_2)), "Got wrong secid\n");
1906
1907     IUri_Release(uri);
1908
1909     IInternetSecurityManagerEx2_Release(sec_mgr2);
1910     IInternetSecurityManager_Release(sec_mgr);
1911 }
1912
1913 static void test_CoInternetIsFeatureZoneElevationEnabled(void)
1914 {
1915     struct {
1916         const char *url_from;
1917         const char *url_to;
1918         DWORD flags;
1919         HRESULT hres;
1920         DWORD policy_flags;
1921     } testcases[] = {
1922         /*  0 */ { "http://www.winehq.org", "http://www.winehq.org", 0, S_FALSE, URLPOLICY_ALLOW },
1923         /*  1 */ { "http://www.winehq.org", "http://www.winehq.org", 0, S_OK, URLPOLICY_DISALLOW },
1924         /*  2 */ { "http://www.winehq.org", "http://www.codeweavers.com", 0, S_FALSE, URLPOLICY_ALLOW },
1925         /*  3 */ { "http://www.winehq.org", "http://www.codeweavers.com", 0, S_OK, URLPOLICY_DISALLOW },
1926         /*  4 */ { "http://www.winehq.org", "http://www.winehq.org", GET_FEATURE_FROM_PROCESS, S_FALSE, -1 },
1927         /*  5 */ { "http://www.winehq.org", "http://www.winehq.org/dir", GET_FEATURE_FROM_PROCESS, S_FALSE, -1 },
1928         /*  6 */ { "http://www.winehq.org", "http://www.codeweavers.com", GET_FEATURE_FROM_PROCESS, S_FALSE, -1 },
1929         /*  7 */ { "http://www.winehq.org", "ftp://winehq.org", GET_FEATURE_FROM_PROCESS, S_FALSE, -1 },
1930         /*  8 */ { "http://www.winehq.org", "ftp://winehq.org", GET_FEATURE_FROM_PROCESS|0x100, S_FALSE, URLPOLICY_ALLOW },
1931         /*  9 */ { "http://www.winehq.org", "ftp://winehq.org", GET_FEATURE_FROM_REGISTRY, S_FALSE, URLPOLICY_ALLOW },
1932     };
1933
1934     WCHAR *url_from, *url_to;
1935     int i;
1936     HRESULT hres;
1937
1938     if(!pCoInternetIsFeatureZoneElevationEnabled || !pCoInternetIsFeatureEnabled
1939             || !pCoInternetIsFeatureEnabledForUrl) {
1940         win_skip("Skipping CoInternetIsFeatureZoneElevationEnabled tests\n");
1941         return;
1942     }
1943
1944
1945     hres = pCoInternetIsFeatureEnabled(FEATURE_ZONE_ELEVATION, GET_FEATURE_FROM_PROCESS);
1946     ok(SUCCEEDED(hres), "CoInternetIsFeatureEnabled returned %x\n", hres);
1947
1948     trace("Testing CoInternetIsFeatureZoneElevationEnabled... (%x)\n", hres);
1949
1950     for(i=0; i<sizeof(testcases)/sizeof(testcases[0]); i++) {
1951         if(hres==S_OK && testcases[i].flags == GET_FEATURE_FROM_PROCESS)
1952             testcases[i].policy_flags = URLPOLICY_ALLOW;
1953     }
1954
1955     for(i=0; i<sizeof(testcases)/sizeof(testcases[0]); i++) {
1956         url_from = a2w(testcases[i].url_from);
1957         url_to = a2w(testcases[i].url_to);
1958
1959         if(testcases[i].policy_flags != -1) {
1960             ProcessUrlAction_policy = testcases[i].policy_flags;
1961             SET_EXPECT(ProcessUrlAction);
1962         }
1963         hres = pCoInternetIsFeatureZoneElevationEnabled(url_from, url_to,
1964                 &security_manager, testcases[i].flags);
1965         ok(hres == testcases[i].hres, "%d) CoInternetIsFeatureZoneElevationEnabled returned %x\n", i, hres);
1966         if(testcases[i].policy_flags != -1)
1967             CHECK_CALLED(ProcessUrlAction);
1968
1969         if(testcases[i].policy_flags != -1)
1970             SET_EXPECT(ProcessUrlAction);
1971         hres = pCoInternetIsFeatureEnabledForUrl(FEATURE_ZONE_ELEVATION,
1972                 testcases[i].flags, url_to, &security_manager);
1973         ok(hres == testcases[i].hres, "%d) CoInternetIsFeatureEnabledForUrl returned %x\n", i, hres);
1974         if(testcases[i].policy_flags != -1)
1975             CHECK_CALLED(ProcessUrlAction);
1976
1977         heap_free(url_from);
1978         heap_free(url_to);
1979     }
1980 }
1981
1982 START_TEST(sec_mgr)
1983 {
1984     HMODULE hurlmon;
1985     int argc;
1986     char **argv;
1987
1988     hurlmon = GetModuleHandle("urlmon.dll");
1989     pCoInternetCreateSecurityManager = (void*) GetProcAddress(hurlmon, "CoInternetCreateSecurityManager");
1990     pCoInternetCreateZoneManager = (void*) GetProcAddress(hurlmon, "CoInternetCreateZoneManager");
1991     pCoInternetGetSecurityUrl = (void*) GetProcAddress(hurlmon, "CoInternetGetSecurityUrl");
1992     pCoInternetGetSecurityUrlEx = (void*) GetProcAddress(hurlmon, "CoInternetGetSecurityUrlEx");
1993     pCreateUri = (void*) GetProcAddress(hurlmon, "CreateUri");
1994     pCoInternetGetSession = (void*) GetProcAddress(hurlmon, "CoInternetGetSession");
1995     pCoInternetIsFeatureEnabled = (void*) GetProcAddress(hurlmon, "CoInternetIsFeatureEnabled");
1996     pCoInternetIsFeatureEnabledForUrl = (void*) GetProcAddress(hurlmon, "CoInternetIsFeatureEnabledForUrl");
1997     pCoInternetIsFeatureZoneElevationEnabled = (void*) GetProcAddress(hurlmon, "CoInternetIsFeatureZoneElevationEnabled");
1998
1999     if (!pCoInternetCreateSecurityManager || !pCoInternetCreateZoneManager ||
2000         !pCoInternetGetSecurityUrl) {
2001         win_skip("Various CoInternet* functions not present in IE 4.0\n");
2002         return;
2003     }
2004
2005     argc = winetest_get_mainargs(&argv);
2006     if(argc > 2 && !strcmp(argv[2], "domain_tests")) {
2007         test_zone_domain_mappings();
2008         return;
2009     }
2010
2011     OleInitialize(NULL);
2012     register_protocols();
2013
2014     test_InternetGetSecurityUrl();
2015
2016     if(!pCoInternetGetSecurityUrlEx || !pCreateUri)
2017         win_skip("Skipping CoInternetGetSecurityUrlEx tests, IE too old\n");
2018     else {
2019         test_InternetGetSecurityUrlEx();
2020         test_InternetGetSecurityUrlEx_Pluggable();
2021     }
2022
2023     test_SecurityManager();
2024     test_SecurityManagerEx2();
2025     test_polices();
2026     test_zone_domains();
2027     test_CoInternetCreateZoneManager();
2028     test_CreateZoneEnumerator();
2029     test_GetZoneActionPolicy();
2030     test_GetZoneAt();
2031     test_GetZoneAttributes();
2032     test_SetZoneAttributes();
2033     test_InternetSecurityMarshalling();
2034     test_CoInternetIsFeatureZoneElevationEnabled();
2035
2036     unregister_protocols();
2037     OleUninitialize();
2038 }