kernel32: Remove superfluous heap reallocation calls in FormatMessageA/W.
[wine] / dlls / urlmon / tests / sec_mgr.c
1 /*
2  * Copyright 2005-2006 Jacek Caban for CodeWeavers
3  * Copyright 2009-2010 Detlef Riekenberg
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #define COBJMACROS
21 #define CONST_VTABLE
22 #define NONAMELESSUNION
23
24 /* needed for IInternetZoneManagerEx2 */
25 #define _WIN32_IE 0x0700
26
27 #include <wine/test.h>
28 #include <stdarg.h>
29 #include <stddef.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "ole2.h"
34 #include "urlmon.h"
35
36 #include "initguid.h"
37
38 static HRESULT (WINAPI *pCoInternetGetSecurityUrl)(LPCWSTR, LPWSTR*, PSUACTION, DWORD);
39
40 static const WCHAR url1[] = {'r','e','s',':','/','/','m','s','h','t','m','l','.','d','l','l',
41         '/','b','l','a','n','k','.','h','t','m',0};
42 static const WCHAR url2[] = {'i','n','d','e','x','.','h','t','m',0};
43 static const WCHAR url3[] = {'f','i','l','e',':','/','/','c',':','\\','I','n','d','e','x','.','h','t','m',0};
44 static const WCHAR url4[] = {'f','i','l','e',':','s','o','m','e','%','2','0','f','i','l','e',
45         '%','2','e','j','p','g',0};
46 static const WCHAR url5[] = {'h','t','t','p',':','/','/','w','w','w','.','z','o','n','e','3',
47         '.','w','i','n','e','t','e','s','t',0};
48 static const WCHAR url6[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
49 static const WCHAR url7[] = {'f','t','p',':','/','/','z','o','n','e','3',
50         '.','w','i','n','e','t','e','s','t','/','f','i','l','e','.','t','e','s','t',0};
51 static const WCHAR url8[] = {'t','e','s','t',':','1','2','3','a','b','c',0};
52 static const WCHAR url9[] = {'h','t','t','p',':','/','/','w','w','w','.','z','o','n','e','3',
53         '.','w','i','n','e','t','e','s','t', '/','s','i','t','e','/','a','b','o','u','t',0};
54 static const WCHAR url10[] = {'f','i','l','e',':','/','/','s','o','m','e','%','2','0','f','i','l','e',
55         '.','j','p','g',0};
56
57 static const WCHAR url4e[] = {'f','i','l','e',':','s','o','m','e',' ','f','i','l','e',
58         '.','j','p','g',0};
59
60
61 static const BYTE secid1[] = {'f','i','l','e',':',0,0,0,0};
62 static const BYTE secid5[] = {'h','t','t','p',':','w','w','w','.','z','o','n','e','3',
63         '.','w','i','n','e','t','e','s','t',3,0,0,0};
64 static const BYTE secid6[] = {'a','b','o','u','t',':','b','l','a','n','k',3,0,0,0};
65 static const BYTE secid7[] = {'f','t','p',':','z','o','n','e','3',
66         '.','w','i','n','e','t','e','s','t',3,0,0,0};
67 static const BYTE secid10[] =
68     {'f','i','l','e',':','s','o','m','e','%','2','0','f','i','l','e','.','j','p','g',3,0,0,0};
69 static const BYTE secid10_2[] =
70     {'f','i','l','e',':','s','o','m','e',' ','f','i','l','e','.','j','p','g',3,0,0,0};
71
72 static const GUID CLSID_TestActiveX =
73     {0x178fc163,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x06,0x46}};
74
75 /* Defined as extern in urlmon.idl, but not exported by uuid.lib */
76 const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
77     {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};
78
79 static struct secmgr_test {
80     LPCWSTR url;
81     DWORD zone;
82     HRESULT zone_hres;
83     DWORD secid_size;
84     const BYTE *secid;
85     HRESULT secid_hres;
86 } secmgr_tests[] = {
87     {url1, 0,   S_OK, sizeof(secid1), secid1, S_OK},
88     {url2, 100, 0x80041001, 0, NULL, E_INVALIDARG},
89     {url3, 0,   S_OK, sizeof(secid1), secid1, S_OK},
90     {url5, 3,   S_OK, sizeof(secid5), secid5, S_OK},
91     {url6, 3,   S_OK, sizeof(secid6), secid6, S_OK},
92     {url7, 3,   S_OK, sizeof(secid7), secid7, S_OK}
93 };
94
95 static int strcmp_w(const WCHAR *str1, const WCHAR *str2)
96 {
97     DWORD len1 = lstrlenW(str1);
98     DWORD len2 = lstrlenW(str2);
99
100     if(len1!=len2) return 1;
101     return memcmp(str1, str2, len1*sizeof(WCHAR));
102 }
103
104 static void test_SecurityManager(void)
105 {
106     int i;
107     IInternetSecurityManager *secmgr = NULL;
108     BYTE buf[512];
109     DWORD zone, size, policy;
110     HRESULT hres;
111
112     hres = CoInternetCreateSecurityManager(NULL, &secmgr, 0);
113     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
114     if(FAILED(hres))
115         return;
116
117     for(i=0; i < sizeof(secmgr_tests)/sizeof(secmgr_tests[0]); i++) {
118         zone = 100;
119         hres = IInternetSecurityManager_MapUrlToZone(secmgr, secmgr_tests[i].url,
120                                                      &zone, 0);
121         ok(hres == secmgr_tests[i].zone_hres /* IE <=6 */
122            || (FAILED(secmgr_tests[i].zone_hres) && hres == E_INVALIDARG), /* IE7 */
123            "[%d] MapUrlToZone failed: %08x, expected %08x\n",
124                 i, hres, secmgr_tests[i].zone_hres);
125         if(SUCCEEDED(hres))
126             ok(zone == secmgr_tests[i].zone, "[%d] zone=%d, expected %d\n", i, zone,
127                secmgr_tests[i].zone);
128         else
129             ok(zone == secmgr_tests[i].zone || zone == -1, "[%d] zone=%d\n", i, zone);
130
131         size = sizeof(buf);
132         memset(buf, 0xf0, sizeof(buf));
133         hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[i].url,
134                 buf, &size, 0);
135         ok(hres == secmgr_tests[i].secid_hres,
136            "[%d] GetSecurityId failed: %08x, expected %08x\n",
137            i, hres, secmgr_tests[i].secid_hres);
138         if(secmgr_tests[i].secid) {
139             ok(size == secmgr_tests[i].secid_size, "[%d] size=%d, expected %d\n",
140                     i, size, secmgr_tests[i].secid_size);
141             ok(!memcmp(buf, secmgr_tests[i].secid, size), "[%d] wrong secid\n", i);
142         }
143     }
144
145     zone = 100;
146     hres = IInternetSecurityManager_MapUrlToZone(secmgr, url10, &zone, 0);
147     ok(hres == S_OK, "MapUrlToZone failed: %08x, expected S_OK\n", hres);
148     ok(zone == 3, "zone=%d, expected 3\n", zone);
149
150     /* win2k3 translates %20 into a space */
151     size = sizeof(buf);
152     memset(buf, 0xf0, sizeof(buf));
153     hres = IInternetSecurityManager_GetSecurityId(secmgr, url10, buf, &size, 0);
154     ok(hres == S_OK, "GetSecurityId failed: %08x, expected S_OK\n", hres);
155     ok(size == sizeof(secid10) ||
156        size == sizeof(secid10_2), /* win2k3 */
157        "size=%d\n", size);
158     ok(!memcmp(buf, secid10, size) ||
159        !memcmp(buf, secid10_2, size), /* win2k3 */
160        "wrong secid\n");
161
162     zone = 100;
163     hres = IInternetSecurityManager_MapUrlToZone(secmgr, NULL, &zone, 0);
164     ok(hres == E_INVALIDARG, "MapUrlToZone failed: %08x, expected E_INVALIDARG\n", hres);
165     ok(zone == 100 || zone == -1, "zone=%d\n", zone);
166
167     size = sizeof(buf);
168     hres = IInternetSecurityManager_GetSecurityId(secmgr, NULL, buf, &size, 0);
169     ok(hres == E_INVALIDARG,
170        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
171     hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[1].url,
172                                                   NULL, &size, 0);
173     ok(hres == E_INVALIDARG,
174        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
175     hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[1].url,
176                                                   buf, NULL, 0);
177     ok(hres == E_INVALIDARG,
178        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
179
180     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, NULL, URLACTION_SCRIPT_RUN, (BYTE*)&policy,
181             sizeof(WCHAR), NULL, 0, 0, 0);
182     ok(hres == E_INVALIDARG, "ProcessUrlAction failed: %08x, expected E_INVALIDARG\n", hres);
183
184     IInternetSecurityManager_Release(secmgr);
185 }
186
187 /* Check if Internet Explorer is configured to run in "Enhanced Security Configuration" (aka hardened mode) */
188 /* Note: this code is duplicated in dlls/mshtml/tests/mshtml_test.h and dlls/urlmon/tests/sec_mgr.c */
189 static BOOL is_ie_hardened(void)
190 {
191     HKEY zone_map;
192     DWORD ie_harden, type, size;
193
194     ie_harden = 0;
195     if(RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap",
196                     0, KEY_QUERY_VALUE, &zone_map) == ERROR_SUCCESS) {
197         size = sizeof(DWORD);
198         if (RegQueryValueExA(zone_map, "IEHarden", NULL, &type, (LPBYTE) &ie_harden, &size) != ERROR_SUCCESS ||
199             type != REG_DWORD) {
200             ie_harden = 0;
201         }
202         RegCloseKey(zone_map);
203     }
204
205     return ie_harden != 0;
206 }
207
208 static void test_url_action(IInternetSecurityManager *secmgr, IInternetZoneManager *zonemgr, DWORD action)
209 {
210     DWORD res, size, policy, reg_policy;
211     char buf[10];
212     HKEY hkey;
213     HRESULT hres;
214
215     /* FIXME: HKEY_CURRENT_USER is most of the time the default but this can be changed on a system.
216      * The test should be changed to cope with that, if need be.
217      */
218     res = RegOpenKeyA(HKEY_CURRENT_USER,
219             "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3", &hkey);
220     if(res != ERROR_SUCCESS) {
221         ok(0, "Could not open zone key\n");
222         return;
223     }
224
225     wsprintf(buf, "%X", action);
226     size = sizeof(DWORD);
227     res = RegQueryValueExA(hkey, buf, NULL, NULL, (BYTE*)&reg_policy, &size);
228     RegCloseKey(hkey);
229     if(res != ERROR_SUCCESS || size != sizeof(DWORD)) {
230         policy = 0xdeadbeef;
231         hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
232                 sizeof(WCHAR), NULL, 0, 0, 0);
233         ok(hres == E_FAIL || broken(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)),
234             "(0x%x) got 0x%x (expected E_FAIL)\n", action, hres);
235         ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
236
237         policy = 0xdeadbeef;
238         hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, (BYTE*)&policy,
239                 sizeof(DWORD), URLZONEREG_DEFAULT);
240         ok(hres == E_FAIL || broken(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)),
241             "(0x%x) got 0x%x (expected E_FAIL)\n", action, hres);
242         ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
243         return;
244     }
245
246     policy = 0xdeadbeef;
247     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, (BYTE*)&policy,
248             sizeof(DWORD), URLZONEREG_DEFAULT);
249     ok(hres == S_OK, "GetZoneActionPolicy failed: %08x\n", hres);
250     ok(policy == reg_policy, "(%x) policy=%x, expected %x\n", action, policy, reg_policy);
251
252     if(policy != URLPOLICY_QUERY) {
253         if(winetest_interactive || ! is_ie_hardened()) {
254             policy = 0xdeadbeef;
255             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
256                     sizeof(WCHAR), NULL, 0, 0, 0);
257             if(reg_policy == URLPOLICY_DISALLOW)
258                 ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
259             else
260                 ok(hres == S_OK, "ProcessUrlAction(%x) failed: %08x\n", action, hres);
261             ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
262
263             policy = 0xdeadbeef;
264             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
265                     2, NULL, 0, 0, 0);
266             if(reg_policy == URLPOLICY_DISALLOW)
267                 ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
268             else
269                 ok(hres == S_OK, "ProcessUrlAction(%x) failed: %08x\n", action, hres);
270             ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
271
272             policy = 0xdeadbeef;
273             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
274                     sizeof(DWORD), NULL, 0, 0, 0);
275             if(reg_policy == URLPOLICY_DISALLOW)
276                 ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
277             else
278                 ok(hres == S_OK, "ProcessUrlAction(%x) failed: %08x\n", action, hres);
279             ok(policy == reg_policy, "(%x) policy=%x\n", action, policy);
280
281             policy = 0xdeadbeef;
282             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
283                     sizeof(WCHAR), (BYTE*)0xdeadbeef, 16, 0, 0);
284             if(reg_policy == URLPOLICY_DISALLOW)
285                 ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
286             else
287                 ok(hres == S_OK, "ProcessUrlAction(%x) failed: %08x\n", action, hres);
288             ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
289         }else {
290             skip("IE running in Enhanced Security Configuration\n");
291         }
292     }
293 }
294
295 static void test_special_url_action(IInternetSecurityManager *secmgr, IInternetZoneManager *zonemgr, DWORD action)
296 {
297     DWORD policy;
298     HRESULT hres;
299
300     policy = 0xdeadbeef;
301     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, (BYTE*)&policy,
302             sizeof(DWORD), URLZONEREG_DEFAULT);
303     ok(hres == S_OK, "GetZoneActionPolicy failed: %08x\n", hres);
304     ok(policy == URLPOLICY_DISALLOW, "(%x) policy=%x, expected URLPOLICY_DISALLOW\n", action, policy);
305
306     policy = 0xdeadbeef;
307     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url1, action, (BYTE*)&policy,
308             sizeof(WCHAR), NULL, 0, 0, 0);
309     ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
310
311     policy = 0xdeadbeef;
312     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url1, action, (BYTE*)&policy,
313             sizeof(DWORD), NULL, 0, 0, 0);
314     ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
315     ok(policy == URLPOLICY_DISALLOW, "policy = %x\n", policy);
316 }
317
318 static void test_activex(IInternetSecurityManager *secmgr)
319 {
320     DWORD policy, policy_size;
321     struct CONFIRMSAFETY cs;
322     BYTE *ppolicy;
323     HRESULT hres;
324
325     policy = 0xdeadbeef;
326     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url1, URLACTION_ACTIVEX_RUN, (BYTE*)&policy,
327             sizeof(DWORD), (BYTE*)&CLSID_TestActiveX, sizeof(CLSID), 0, 0);
328     ok(hres == S_OK, "ProcessUrlAction(URLACTION_ACTIVEX_RUN) failed: %08x\n", hres);
329     ok(policy == URLPOLICY_ALLOW || policy == URLPOLICY_DISALLOW, "policy = %x\n", policy);
330
331     cs.clsid = CLSID_TestActiveX;
332     cs.pUnk = (IUnknown*)0xdeadbeef;
333     cs.dwFlags = 0;
334     hres = IInternetSecurityManager_QueryCustomPolicy(secmgr, url1, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
335             &ppolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
336     ok(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), "QueryCusromPolicy failed: %08x\n", hres);
337 }
338
339 static void test_polices(void)
340 {
341     IInternetZoneManager *zonemgr = NULL;
342     IInternetSecurityManager *secmgr = NULL;
343     HRESULT hres;
344
345     hres = CoInternetCreateSecurityManager(NULL, &secmgr, 0);
346     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
347     hres = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
348     ok(hres == S_OK, "CoInternetCreateZoneManager failed: %08x\n", hres);
349
350     test_url_action(secmgr, zonemgr, URLACTION_SCRIPT_RUN);
351     test_url_action(secmgr, zonemgr, URLACTION_ACTIVEX_RUN);
352     test_url_action(secmgr, zonemgr, URLACTION_ACTIVEX_OVERRIDE_OBJECT_SAFETY);
353     test_url_action(secmgr, zonemgr, URLACTION_CHANNEL_SOFTDIST_PERMISSIONS);
354     test_url_action(secmgr, zonemgr, 0xdeadbeef);
355
356     test_special_url_action(secmgr, zonemgr, URLACTION_SCRIPT_OVERRIDE_SAFETY);
357     test_special_url_action(secmgr, zonemgr, URLACTION_ACTIVEX_OVERRIDE_SCRIPT_SAFETY);
358
359     test_activex(secmgr);
360
361     IInternetSecurityManager_Release(secmgr);
362     IInternetZoneManager_Release(zonemgr);
363 }
364
365 static void test_CoInternetCreateZoneManager(void)
366 {
367     IInternetZoneManager *zonemgr = NULL;
368     IUnknown *punk = NULL;
369     HRESULT hr;
370
371     hr = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
372     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
373     if (FAILED(hr))
374         return;
375
376     hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IUnknown, (void **) &punk);
377     ok(SUCCEEDED(hr), "got 0x%x with %p (expected Success)\n", hr, punk);
378     if (punk)
379         IUnknown_Release(punk);
380
381     hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IInternetZoneManager, (void **) &punk);
382     ok(SUCCEEDED(hr), "got 0x%x with %p (expected Success)\n", hr, punk);
383     if (punk)
384         IUnknown_Release(punk);
385
386
387     hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IInternetZoneManagerEx, (void **) &punk);
388     if (SUCCEEDED(hr)) {
389         IUnknown_Release(punk);
390
391         hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IInternetZoneManagerEx2, (void **) &punk);
392         if (punk)
393             IUnknown_Release(punk);
394         else
395             win_skip("InternetZoneManagerEx2 not supported\n");
396
397     }
398     else
399         win_skip("InternetZoneManagerEx not supported\n");
400
401     hr = IInternetZoneManager_Release(zonemgr);
402     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
403
404 }
405
406 static void test_CreateZoneEnumerator(void)
407 {
408     IInternetZoneManager *zonemgr = NULL;
409     HRESULT hr;
410     DWORD dwEnum;
411     DWORD dwEnum2;
412     DWORD dwCount;
413     DWORD dwCount2;
414
415     hr = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
416     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
417     if (FAILED(hr))
418         return;
419
420     dwEnum=0xdeadbeef;
421     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, NULL, 0);
422     ok((hr == E_INVALIDARG) && (dwEnum == 0xdeadbeef),
423         "got 0x%x with 0x%x (expected E_INVALIDARG with 0xdeadbeef)\n", hr, dwEnum);
424
425     dwCount=0xdeadbeef;
426     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, NULL, &dwCount, 0);
427     ok((hr == E_INVALIDARG) && (dwCount == 0xdeadbeef),
428         "got 0x%x and 0x%x (expected E_INVALIDARG and 0xdeadbeef)\n", hr, dwCount);
429
430     dwEnum=0xdeadbeef;
431     dwCount=0xdeadbeef;
432     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 0xffffffff);
433     ok((hr == E_INVALIDARG) && (dwEnum == 0xdeadbeef) && (dwCount == 0xdeadbeef),
434         "got 0x%x with 0x%x and 0x%x (expected E_INVALIDARG with 0xdeadbeef and 0xdeadbeef)\n",
435         hr, dwEnum, dwCount);
436
437     dwEnum=0xdeadbeef;
438     dwCount=0xdeadbeef;
439     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 1);
440     ok((hr == E_INVALIDARG) && (dwEnum == 0xdeadbeef) && (dwCount == 0xdeadbeef),
441         "got 0x%x with 0x%x and 0x%x (expected E_INVALIDARG with 0xdeadbeef and 0xdeadbeef)\n",
442         hr, dwEnum, dwCount);
443
444     dwEnum=0xdeadbeef;
445     dwCount=0xdeadbeef;
446     /* Normal use */
447     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 0);
448     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
449
450     if (SUCCEEDED(hr)) {
451         dwEnum2=0xdeadbeef;
452         dwCount2=0xdeadbeef;
453         hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum2, &dwCount2, 0);
454         ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
455         if (SUCCEEDED(hr)) {
456             /* native urlmon has an incrementing counter for dwEnum */
457             hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum2);
458             ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
459         }
460
461         hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum);
462         ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
463
464         /* Destroy the Enumerator twice is detected and handled in native urlmon */
465         hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum);
466         ok((hr == E_INVALIDARG), "got 0x%x (expected E_INVALIDARG)\n", hr);
467     }
468
469     /* ::Release succeed also, when a ::DestroyZoneEnumerator is missing */
470     hr = IInternetZoneManager_Release(zonemgr);
471     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
472 }
473
474 static void test_GetZoneActionPolicy(void)
475 {
476     IInternetZoneManager *zonemgr = NULL;
477     BYTE buf[32];
478     HRESULT hres;
479     DWORD action = URLACTION_CREDENTIALS_USE; /* Implemented on all IE versions */
480
481     hres = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
482     ok(hres == S_OK, "CoInternetCreateZoneManager failed: %08x\n", hres);
483     if(FAILED(hres))
484         return;
485
486     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, buf,
487             sizeof(DWORD), URLZONEREG_DEFAULT);
488     ok(hres == S_OK, "GetZoneActionPolicy failed: %08x\n", hres);
489     ok(*(DWORD*)buf == URLPOLICY_CREDENTIALS_SILENT_LOGON_OK ||
490             *(DWORD*)buf == URLPOLICY_CREDENTIALS_MUST_PROMPT_USER ||
491             *(DWORD*)buf == URLPOLICY_CREDENTIALS_CONDITIONAL_PROMPT ||
492             *(DWORD*)buf == URLPOLICY_CREDENTIALS_ANONYMOUS_ONLY,
493             "unexpected policy=%d\n", *(DWORD*)buf);
494
495     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, NULL,
496             sizeof(DWORD), URLZONEREG_DEFAULT);
497     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
498
499     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, buf,
500             2, URLZONEREG_DEFAULT);
501     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
502
503     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, 0x1fff, buf,
504             sizeof(DWORD), URLZONEREG_DEFAULT);
505     ok(hres == E_FAIL || broken(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)),
506             "(0x%x) got 0x%x (expected E_FAIL)\n", action, hres);
507
508     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 13, action, buf,
509             sizeof(DWORD), URLZONEREG_DEFAULT);
510     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
511
512     IInternetZoneManager_Release(zonemgr);
513 }
514
515 static void test_GetZoneAt(void)
516 {
517     IInternetZoneManager *zonemgr = NULL;
518     HRESULT hr;
519     DWORD dwEnum;
520     DWORD dwCount;
521     DWORD dwZone;
522     DWORD i;
523
524     hr = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
525     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
526     if (FAILED(hr))
527         return;
528
529     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 0);
530     if (FAILED(hr))
531         goto cleanup;
532
533     if (0) {
534         /* this crashes with native urlmon */
535         hr = IInternetZoneManager_GetZoneAt(zonemgr, dwEnum, 0, NULL);
536     }
537
538     dwZone = 0xdeadbeef;
539     hr = IInternetZoneManager_GetZoneAt(zonemgr, 0xdeadbeef, 0, &dwZone);
540     ok(hr == E_INVALIDARG,
541         "got 0x%x with 0x%x (expected E_INVALIDARG)\n", hr, dwZone);
542
543     for (i = 0; i < dwCount; i++)
544     {
545         dwZone = 0xdeadbeef;
546         hr = IInternetZoneManager_GetZoneAt(zonemgr, dwEnum, i, &dwZone);
547         ok(hr == S_OK, "#%d: got x%x with %d (expected S_OK)\n", i, hr, dwZone);
548     }
549
550     dwZone = 0xdeadbeef;
551     /* MSDN (index .. must be .. less than or equal to) is wrong */
552     hr = IInternetZoneManager_GetZoneAt(zonemgr, dwEnum, dwCount, &dwZone);
553     ok(hr == E_INVALIDARG,
554         "got 0x%x with 0x%x (expected E_INVALIDARG)\n", hr, dwZone);
555
556     hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum);
557     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
558
559 cleanup:
560     hr = IInternetZoneManager_Release(zonemgr);
561     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
562 }
563
564 static void test_GetZoneAttributes(void)
565 {
566     IInternetZoneManager *zonemgr = NULL;
567     CHAR buffer [sizeof(ZONEATTRIBUTES) + 32];
568     ZONEATTRIBUTES* pZA = (ZONEATTRIBUTES*) buffer;
569     HRESULT hr;
570     DWORD i;
571
572     hr = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
573     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
574     if (FAILED(hr))
575         return;
576
577     /* native urlmon has Zone "0" up to Zone "4" since IE4 */
578     for (i = 0; i < 5; i++) {
579         memset(buffer, -1, sizeof(buffer));
580         hr = IInternetZoneManager_GetZoneAttributes(zonemgr, i, pZA);
581         ok(hr == S_OK, "#%d: got 0x%x (expected S_OK)\n", i, hr);
582     }
583
584     /* IE8 no longer set cbSize */
585     memset(buffer, -1, sizeof(buffer));
586     pZA->cbSize = 0;
587     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, pZA);
588     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
589     ok((pZA->cbSize == 0) || (pZA->cbSize == sizeof(ZONEATTRIBUTES)),
590         "got cbSize = %d (expected 0)\n", pZA->cbSize);
591
592     memset(buffer, -1, sizeof(buffer));
593     pZA->cbSize = 64;
594     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, pZA);
595     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
596     ok((pZA->cbSize == 64) || (pZA->cbSize == sizeof(ZONEATTRIBUTES)),
597         "got cbSize = %d (expected 64)\n", pZA->cbSize);
598
599     memset(buffer, -1, sizeof(buffer));
600     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, pZA);
601     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
602     ok((pZA->cbSize == 0xffffffff) || (pZA->cbSize == sizeof(ZONEATTRIBUTES)),
603         "got cbSize = 0x%x (expected 0xffffffff)\n", pZA->cbSize);
604
605     /* IE8 no longer fail on invalid zones */
606     memset(buffer, -1, sizeof(buffer));
607     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0xdeadbeef, pZA);
608     ok(hr == S_OK || (hr == E_FAIL),
609         "got 0x%x (expected S_OK or E_FAIL)\n", hr);
610
611     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, NULL);
612     ok(hr == E_INVALIDARG, "got 0x%x (expected E_INVALIDARG)\n", hr);
613
614     hr = IInternetZoneManager_Release(zonemgr);
615     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
616 }
617
618 static void test_InternetSecurityMarshalling(void)
619 {
620     IInternetSecurityManager *secmgr = NULL;
621     IUnknown *unk;
622     IStream *stream;
623     HRESULT hres;
624
625     hres = CoInternetCreateSecurityManager(NULL, &secmgr, 0);
626     if(FAILED(hres))
627         return;
628
629     hres = IInternetSecurityManager_QueryInterface(secmgr, &IID_IUnknown, (void**)&unk);
630     ok(hres == S_OK, "QueryInterface returned: %08x\n", hres);
631
632     hres = CreateStreamOnHGlobal(NULL, TRUE, &stream);
633     ok(hres == S_OK, "CreateStreamOnHGlobal returned: %08x\n", hres);
634
635     hres = CoMarshalInterface(stream, &IID_IInternetSecurityManager, unk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
636     /* Not supported in W98 */
637     ok(hres == S_OK || broken(hres == REGDB_E_IIDNOTREG),
638         "CoMarshalInterface returned: %08x\n", hres);
639
640     IStream_Release(stream);
641     IUnknown_Release(unk);
642     IInternetSecurityManager_Release(secmgr);
643 }
644
645 static void test_InternetGetSecurityUrl(void)
646 {
647     const WCHAR url5_out[] = {'h','t','t','p',':','w','w','w','.','z','o','n','e','3',
648                               '.','w','i','n','e','t','e','s','t',0};
649     const WCHAR url7_out[] = {'f','t','p',':','z','o','n','e','3','.','w','i','n','e','t','e','s','t',0};
650
651     const WCHAR *in[] = {url2, url3, url4, url5, url7, url8, url9, url10};
652     const WCHAR *out_default[] = {url2, url3, url4, url5_out, url7_out, url8, url5_out, url10};
653     const WCHAR *out_securl[] = {url2, url3, url4, url5, url7, url8, url9, url10};
654
655     WCHAR *sec;
656     DWORD i;
657     HRESULT hres;
658
659     if (!pCoInternetGetSecurityUrl) {
660         win_skip("CoInternetGetSecurityUrl not found\n");
661         return;
662     }
663
664     for(i=0; i<sizeof(in)/sizeof(WCHAR*); i++) {
665         hres = pCoInternetGetSecurityUrl(in[i], &sec, PSU_DEFAULT, 0);
666         ok(hres == S_OK, "(%d) CoInternetGetSecurityUrl returned: %08x\n", i, hres);
667         if(hres == S_OK) {
668             ok(!strcmp_w(sec, out_default[i]), "(%d) Got %s, expected %s\n",
669                     i, wine_dbgstr_w(sec), wine_dbgstr_w(out_default[i]));
670             CoTaskMemFree(sec);
671         }
672
673         hres = pCoInternetGetSecurityUrl(in[i], &sec, PSU_SECURITY_URL_ONLY, 0);
674         ok(hres == S_OK, "(%d) CoInternetGetSecurityUrl returned: %08x\n", i, hres);
675         if(hres == S_OK) {
676             ok(!strcmp_w(sec, out_securl[i]), "(%d) Got %s, expected %s\n",
677                     i, wine_dbgstr_w(sec), wine_dbgstr_w(out_securl[i]));
678             CoTaskMemFree(sec);
679         }
680     }
681 }
682
683
684 START_TEST(sec_mgr)
685 {
686     HMODULE hurlmon;
687
688     OleInitialize(NULL);
689
690     hurlmon = GetModuleHandle("urlmon.dll");
691     pCoInternetGetSecurityUrl = (void*) GetProcAddress(hurlmon, "CoInternetGetSecurityUrl");
692
693     test_InternetGetSecurityUrl();
694     test_SecurityManager();
695     test_polices();
696     test_CoInternetCreateZoneManager();
697     test_CreateZoneEnumerator();
698     test_GetZoneActionPolicy();
699     test_GetZoneAt();
700     test_GetZoneAttributes();
701     test_InternetSecurityMarshalling();
702
703     OleUninitialize();
704 }