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