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