comctl32/listview: Free ID array when removing all items.
[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 void test_SecurityManager(void)
95 {
96     int i;
97     IInternetSecurityManager *secmgr = NULL;
98     BYTE buf[512];
99     DWORD zone, size, policy;
100     HRESULT hres;
101
102     hres = CoInternetCreateSecurityManager(NULL, &secmgr, 0);
103     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
104     if(FAILED(hres))
105         return;
106
107     for(i=0; i < sizeof(secmgr_tests)/sizeof(secmgr_tests[0]); i++) {
108         zone = 100;
109         hres = IInternetSecurityManager_MapUrlToZone(secmgr, secmgr_tests[i].url,
110                                                      &zone, 0);
111         ok(hres == secmgr_tests[i].zone_hres /* IE <=6 */
112            || (FAILED(secmgr_tests[i].zone_hres) && hres == E_INVALIDARG), /* IE7 */
113            "[%d] MapUrlToZone failed: %08x, expected %08x\n",
114                 i, hres, secmgr_tests[i].zone_hres);
115         if(SUCCEEDED(hres))
116             ok(zone == secmgr_tests[i].zone, "[%d] zone=%d, expected %d\n", i, zone,
117                secmgr_tests[i].zone);
118         else
119             ok(zone == secmgr_tests[i].zone || zone == -1, "[%d] zone=%d\n", i, zone);
120
121         size = sizeof(buf);
122         memset(buf, 0xf0, sizeof(buf));
123         hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[i].url,
124                 buf, &size, 0);
125         ok(hres == secmgr_tests[i].secid_hres,
126            "[%d] GetSecurityId failed: %08x, expected %08x\n",
127            i, hres, secmgr_tests[i].secid_hres);
128         if(secmgr_tests[i].secid) {
129             ok(size == secmgr_tests[i].secid_size, "[%d] size=%d, expected %d\n",
130                     i, size, secmgr_tests[i].secid_size);
131             ok(!memcmp(buf, secmgr_tests[i].secid, size), "[%d] wrong secid\n", i);
132         }
133     }
134
135     zone = 100;
136     hres = IInternetSecurityManager_MapUrlToZone(secmgr, url10, &zone, 0);
137     ok(hres == S_OK, "MapUrlToZone failed: %08x, expected S_OK\n", hres);
138     ok(zone == 3, "zone=%d, expected 3\n", zone);
139
140     /* win2k3 translates %20 into a space */
141     size = sizeof(buf);
142     memset(buf, 0xf0, sizeof(buf));
143     hres = IInternetSecurityManager_GetSecurityId(secmgr, url10, buf, &size, 0);
144     ok(hres == S_OK, "GetSecurityId failed: %08x, expected S_OK\n", hres);
145     ok(size == sizeof(secid10) ||
146        size == sizeof(secid10_2), /* win2k3 */
147        "size=%d\n", size);
148     ok(!memcmp(buf, secid10, size) ||
149        !memcmp(buf, secid10_2, size), /* win2k3 */
150        "wrong secid\n");
151
152     zone = 100;
153     hres = IInternetSecurityManager_MapUrlToZone(secmgr, NULL, &zone, 0);
154     ok(hres == E_INVALIDARG, "MapUrlToZone failed: %08x, expected E_INVALIDARG\n", hres);
155     ok(zone == 100 || zone == -1, "zone=%d\n", zone);
156
157     size = sizeof(buf);
158     hres = IInternetSecurityManager_GetSecurityId(secmgr, NULL, buf, &size, 0);
159     ok(hres == E_INVALIDARG,
160        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
161     hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[1].url,
162                                                   NULL, &size, 0);
163     ok(hres == E_INVALIDARG,
164        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
165     hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[1].url,
166                                                   buf, NULL, 0);
167     ok(hres == E_INVALIDARG,
168        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
169
170     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, NULL, URLACTION_SCRIPT_RUN, (BYTE*)&policy,
171             sizeof(WCHAR), NULL, 0, 0, 0);
172     ok(hres == E_INVALIDARG, "ProcessUrlAction failed: %08x, expected E_INVALIDARG\n", hres);
173
174     IInternetSecurityManager_Release(secmgr);
175 }
176
177 /* Check if Internet Explorer is configured to run in "Enhanced Security Configuration" (aka hardened mode) */
178 /* Note: this code is duplicated in dlls/mshtml/tests/mshtml_test.h and dlls/urlmon/tests/sec_mgr.c */
179 static BOOL is_ie_hardened(void)
180 {
181     HKEY zone_map;
182     DWORD ie_harden, type, size;
183
184     ie_harden = 0;
185     if(RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap",
186                     0, KEY_QUERY_VALUE, &zone_map) == ERROR_SUCCESS) {
187         size = sizeof(DWORD);
188         if (RegQueryValueExA(zone_map, "IEHarden", NULL, &type, (LPBYTE) &ie_harden, &size) != ERROR_SUCCESS ||
189             type != REG_DWORD) {
190             ie_harden = 0;
191         }
192         RegCloseKey(zone_map);
193     }
194
195     return ie_harden != 0;
196 }
197
198 static void test_url_action(IInternetSecurityManager *secmgr, IInternetZoneManager *zonemgr, DWORD action)
199 {
200     DWORD res, size, policy, reg_policy;
201     char buf[10];
202     HKEY hkey;
203     HRESULT hres;
204
205     /* FIXME: HKEY_CURRENT_USER is most of the time the default but this can be changed on a system.
206      * The test should be changed to cope with that, if need be.
207      */
208     res = RegOpenKeyA(HKEY_CURRENT_USER,
209             "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3", &hkey);
210     if(res != ERROR_SUCCESS) {
211         ok(0, "Could not open zone key\n");
212         return;
213     }
214
215     wsprintf(buf, "%X", action);
216     size = sizeof(DWORD);
217     res = RegQueryValueExA(hkey, buf, NULL, NULL, (BYTE*)&reg_policy, &size);
218     RegCloseKey(hkey);
219     if(res != ERROR_SUCCESS || size != sizeof(DWORD)) {
220         policy = 0xdeadbeef;
221         hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
222                 sizeof(WCHAR), NULL, 0, 0, 0);
223         ok(hres == E_FAIL || broken(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)),
224             "(0x%x) got 0x%x (expected E_FAIL)\n", action, hres);
225         ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
226
227         policy = 0xdeadbeef;
228         hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, (BYTE*)&policy,
229                 sizeof(DWORD), URLZONEREG_DEFAULT);
230         ok(hres == E_FAIL || broken(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)),
231             "(0x%x) got 0x%x (expected E_FAIL)\n", action, hres);
232         ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
233         return;
234     }
235
236     policy = 0xdeadbeef;
237     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, (BYTE*)&policy,
238             sizeof(DWORD), URLZONEREG_DEFAULT);
239     ok(hres == S_OK, "GetZoneActionPolicy failed: %08x\n", hres);
240     ok(policy == reg_policy, "(%x) policy=%x, expected %x\n", action, policy, reg_policy);
241
242     if(policy != URLPOLICY_QUERY) {
243         if(winetest_interactive || ! is_ie_hardened()) {
244             policy = 0xdeadbeef;
245             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
246                     sizeof(WCHAR), NULL, 0, 0, 0);
247             if(reg_policy == URLPOLICY_DISALLOW)
248                 ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
249             else
250                 ok(hres == S_OK, "ProcessUrlAction(%x) failed: %08x\n", action, hres);
251             ok(policy == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
252
253             policy = 0xdeadbeef;
254             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
255                     2, 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                     sizeof(DWORD), 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 == reg_policy, "(%x) policy=%x\n", action, policy);
270
271             policy = 0xdeadbeef;
272             hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url9, action, (BYTE*)&policy,
273                     sizeof(WCHAR), (BYTE*)0xdeadbeef, 16, 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 == 0xdeadbeef, "(%x) policy=%x\n", action, policy);
279         }else {
280             skip("IE running in Enhanced Security Configuration\n");
281         }
282     }
283 }
284
285 static void test_special_url_action(IInternetSecurityManager *secmgr, IInternetZoneManager *zonemgr, DWORD action)
286 {
287     DWORD policy;
288     HRESULT hres;
289
290     policy = 0xdeadbeef;
291     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, (BYTE*)&policy,
292             sizeof(DWORD), URLZONEREG_DEFAULT);
293     ok(hres == S_OK, "GetZoneActionPolicy failed: %08x\n", hres);
294     ok(policy == URLPOLICY_DISALLOW, "(%x) policy=%x, expected URLPOLICY_DISALLOW\n", action, policy);
295
296     policy = 0xdeadbeef;
297     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url1, action, (BYTE*)&policy,
298             sizeof(WCHAR), NULL, 0, 0, 0);
299     ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
300
301     policy = 0xdeadbeef;
302     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url1, action, (BYTE*)&policy,
303             sizeof(DWORD), NULL, 0, 0, 0);
304     ok(hres == S_FALSE, "ProcessUrlAction(%x) failed: %08x, expected S_FALSE\n", action, hres);
305     ok(policy == URLPOLICY_DISALLOW, "policy = %x\n", policy);
306 }
307
308 static void test_activex(IInternetSecurityManager *secmgr)
309 {
310     DWORD policy, policy_size;
311     struct CONFIRMSAFETY cs;
312     BYTE *ppolicy;
313     HRESULT hres;
314
315     policy = 0xdeadbeef;
316     hres = IInternetSecurityManager_ProcessUrlAction(secmgr, url1, URLACTION_ACTIVEX_RUN, (BYTE*)&policy,
317             sizeof(DWORD), (BYTE*)&CLSID_TestActiveX, sizeof(CLSID), 0, 0);
318     ok(hres == S_OK, "ProcessUrlAction(URLACTION_ACTIVEX_RUN) failed: %08x\n", hres);
319     ok(policy == URLPOLICY_ALLOW || policy == URLPOLICY_DISALLOW, "policy = %x\n", policy);
320
321     cs.clsid = CLSID_TestActiveX;
322     cs.pUnk = (IUnknown*)0xdeadbeef;
323     cs.dwFlags = 0;
324     hres = IInternetSecurityManager_QueryCustomPolicy(secmgr, url1, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
325             &ppolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
326     ok(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND), "QueryCusromPolicy failed: %08x\n", hres);
327 }
328
329 static void test_polices(void)
330 {
331     IInternetZoneManager *zonemgr = NULL;
332     IInternetSecurityManager *secmgr = NULL;
333     HRESULT hres;
334
335     hres = CoInternetCreateSecurityManager(NULL, &secmgr, 0);
336     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
337     hres = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
338     ok(hres == S_OK, "CoInternetCreateZoneManager failed: %08x\n", hres);
339
340     test_url_action(secmgr, zonemgr, URLACTION_SCRIPT_RUN);
341     test_url_action(secmgr, zonemgr, URLACTION_ACTIVEX_RUN);
342     test_url_action(secmgr, zonemgr, URLACTION_ACTIVEX_OVERRIDE_OBJECT_SAFETY);
343     test_url_action(secmgr, zonemgr, URLACTION_CHANNEL_SOFTDIST_PERMISSIONS);
344     test_url_action(secmgr, zonemgr, 0xdeadbeef);
345
346     test_special_url_action(secmgr, zonemgr, URLACTION_SCRIPT_OVERRIDE_SAFETY);
347     test_special_url_action(secmgr, zonemgr, URLACTION_ACTIVEX_OVERRIDE_SCRIPT_SAFETY);
348
349     test_activex(secmgr);
350
351     IInternetSecurityManager_Release(secmgr);
352     IInternetZoneManager_Release(zonemgr);
353 }
354
355 static void test_CoInternetCreateZoneManager(void)
356 {
357     IInternetZoneManager *zonemgr = NULL;
358     IUnknown *punk = NULL;
359     HRESULT hr;
360
361     hr = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
362     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
363     if (FAILED(hr))
364         return;
365
366     hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IUnknown, (void **) &punk);
367     ok(SUCCEEDED(hr), "got 0x%x with %p (expected Success)\n", hr, punk);
368     if (punk)
369         IUnknown_Release(punk);
370
371     hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IInternetZoneManager, (void **) &punk);
372     ok(SUCCEEDED(hr), "got 0x%x with %p (expected Success)\n", hr, punk);
373     if (punk)
374         IUnknown_Release(punk);
375
376
377     hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IInternetZoneManagerEx, (void **) &punk);
378     if (SUCCEEDED(hr)) {
379         IUnknown_Release(punk);
380
381         hr = IInternetZoneManager_QueryInterface(zonemgr, &IID_IInternetZoneManagerEx2, (void **) &punk);
382         if (punk)
383             IUnknown_Release(punk);
384         else
385             win_skip("InternetZoneManagerEx2 not supported\n");
386
387     }
388     else
389         win_skip("InternetZoneManagerEx not supported\n");
390
391     hr = IInternetZoneManager_Release(zonemgr);
392     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
393
394 }
395
396 static void test_CreateZoneEnumerator(void)
397 {
398     IInternetZoneManager *zonemgr = NULL;
399     HRESULT hr;
400     DWORD dwEnum;
401     DWORD dwEnum2;
402     DWORD dwCount;
403     DWORD dwCount2;
404
405     hr = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
406     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
407     if (FAILED(hr))
408         return;
409
410     dwEnum=0xdeadbeef;
411     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, NULL, 0);
412     ok((hr == E_INVALIDARG) && (dwEnum == 0xdeadbeef),
413         "got 0x%x with 0x%x (expected E_INVALIDARG with 0xdeadbeef)\n", hr, dwEnum);
414
415     dwCount=0xdeadbeef;
416     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, NULL, &dwCount, 0);
417     ok((hr == E_INVALIDARG) && (dwCount == 0xdeadbeef),
418         "got 0x%x and 0x%x (expected E_INVALIDARG and 0xdeadbeef)\n", hr, dwCount);
419
420     dwEnum=0xdeadbeef;
421     dwCount=0xdeadbeef;
422     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 0xffffffff);
423     ok((hr == E_INVALIDARG) && (dwEnum == 0xdeadbeef) && (dwCount == 0xdeadbeef),
424         "got 0x%x with 0x%x and 0x%x (expected E_INVALIDARG with 0xdeadbeef and 0xdeadbeef)\n",
425         hr, dwEnum, dwCount);
426
427     dwEnum=0xdeadbeef;
428     dwCount=0xdeadbeef;
429     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 1);
430     ok((hr == E_INVALIDARG) && (dwEnum == 0xdeadbeef) && (dwCount == 0xdeadbeef),
431         "got 0x%x with 0x%x and 0x%x (expected E_INVALIDARG with 0xdeadbeef and 0xdeadbeef)\n",
432         hr, dwEnum, dwCount);
433
434     dwEnum=0xdeadbeef;
435     dwCount=0xdeadbeef;
436     /* Normal use */
437     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 0);
438     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
439
440     if (SUCCEEDED(hr)) {
441         dwEnum2=0xdeadbeef;
442         dwCount2=0xdeadbeef;
443         hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum2, &dwCount2, 0);
444         ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
445         if (SUCCEEDED(hr)) {
446             /* native urlmon has an incrementing counter for dwEnum */
447             hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum2);
448             ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
449         }
450
451         hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum);
452         ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
453
454         /* Destroy the Enumerator twice is detected and handled in native urlmon */
455         hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum);
456         ok((hr == E_INVALIDARG), "got 0x%x (expected E_INVALIDARG)\n", hr);
457     }
458
459     /* ::Release succeed also, when a ::DestroyZoneEnumerator is missing */
460     hr = IInternetZoneManager_Release(zonemgr);
461     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
462 }
463
464 static void test_GetZoneActionPolicy(void)
465 {
466     IInternetZoneManager *zonemgr = NULL;
467     BYTE buf[32];
468     HRESULT hres;
469     DWORD action = URLACTION_CREDENTIALS_USE; /* Implemented on all IE versions */
470
471     hres = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
472     ok(hres == S_OK, "CoInternetCreateZoneManager failed: %08x\n", hres);
473     if(FAILED(hres))
474         return;
475
476     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, buf,
477             sizeof(DWORD), URLZONEREG_DEFAULT);
478     ok(hres == S_OK, "GetZoneActionPolicy failed: %08x\n", hres);
479     ok(*(DWORD*)buf == URLPOLICY_CREDENTIALS_SILENT_LOGON_OK ||
480             *(DWORD*)buf == URLPOLICY_CREDENTIALS_MUST_PROMPT_USER ||
481             *(DWORD*)buf == URLPOLICY_CREDENTIALS_CONDITIONAL_PROMPT ||
482             *(DWORD*)buf == URLPOLICY_CREDENTIALS_ANONYMOUS_ONLY,
483             "unexpected policy=%d\n", *(DWORD*)buf);
484
485     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, NULL,
486             sizeof(DWORD), URLZONEREG_DEFAULT);
487     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
488
489     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, action, buf,
490             2, URLZONEREG_DEFAULT);
491     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
492
493     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, 0x1fff, buf,
494             sizeof(DWORD), URLZONEREG_DEFAULT);
495     ok(hres == E_FAIL || broken(hres == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)),
496             "(0x%x) got 0x%x (expected E_FAIL)\n", action, hres);
497
498     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 13, action, buf,
499             sizeof(DWORD), URLZONEREG_DEFAULT);
500     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
501
502     IInternetZoneManager_Release(zonemgr);
503 }
504
505 static void test_GetZoneAt(void)
506 {
507     IInternetZoneManager *zonemgr = NULL;
508     HRESULT hr;
509     DWORD dwEnum;
510     DWORD dwCount;
511     DWORD dwZone;
512     DWORD i;
513
514     hr = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
515     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
516     if (FAILED(hr))
517         return;
518
519     hr = IInternetZoneManager_CreateZoneEnumerator(zonemgr, &dwEnum, &dwCount, 0);
520     if (FAILED(hr))
521         goto cleanup;
522
523     if (0) {
524         /* this crashes with native urlmon */
525         hr = IInternetZoneManager_GetZoneAt(zonemgr, dwEnum, 0, NULL);
526     }
527
528     dwZone = 0xdeadbeef;
529     hr = IInternetZoneManager_GetZoneAt(zonemgr, 0xdeadbeef, 0, &dwZone);
530     ok(hr == E_INVALIDARG,
531         "got 0x%x with 0x%x (expected E_INVALIDARG)\n", hr, dwZone);
532
533     for (i = 0; i < dwCount; i++)
534     {
535         dwZone = 0xdeadbeef;
536         hr = IInternetZoneManager_GetZoneAt(zonemgr, dwEnum, i, &dwZone);
537         ok(hr == S_OK, "#%d: got x%x with %d (expected S_OK)\n", i, hr, dwZone);
538     }
539
540     dwZone = 0xdeadbeef;
541     /* MSDN (index .. must be .. less than or equal to) is wrong */
542     hr = IInternetZoneManager_GetZoneAt(zonemgr, dwEnum, dwCount, &dwZone);
543     ok(hr == E_INVALIDARG,
544         "got 0x%x with 0x%x (expected E_INVALIDARG)\n", hr, dwZone);
545
546     hr = IInternetZoneManager_DestroyZoneEnumerator(zonemgr, dwEnum);
547     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
548
549 cleanup:
550     hr = IInternetZoneManager_Release(zonemgr);
551     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
552 }
553
554 static void test_GetZoneAttributes(void)
555 {
556     IInternetZoneManager *zonemgr = NULL;
557     CHAR buffer [sizeof(ZONEATTRIBUTES) + 32];
558     ZONEATTRIBUTES* pZA = (ZONEATTRIBUTES*) buffer;
559     HRESULT hr;
560     DWORD i;
561
562     hr = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
563     ok(hr == S_OK, "CoInternetCreateZoneManager result: 0x%x\n", hr);
564     if (FAILED(hr))
565         return;
566
567     /* native urlmon has Zone "0" up to Zone "4" since IE4 */
568     for (i = 0; i < 5; i++) {
569         memset(buffer, -1, sizeof(buffer));
570         hr = IInternetZoneManager_GetZoneAttributes(zonemgr, i, pZA);
571         ok(hr == S_OK, "#%d: got 0x%x (expected S_OK)\n", i, hr);
572     }
573
574     /* IE8 no longer set cbSize */
575     memset(buffer, -1, sizeof(buffer));
576     pZA->cbSize = 0;
577     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, pZA);
578     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
579     ok((pZA->cbSize == 0) || (pZA->cbSize == sizeof(ZONEATTRIBUTES)),
580         "got cbSize = %d (expected 0)\n", pZA->cbSize);
581
582     memset(buffer, -1, sizeof(buffer));
583     pZA->cbSize = 64;
584     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, pZA);
585     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
586     ok((pZA->cbSize == 64) || (pZA->cbSize == sizeof(ZONEATTRIBUTES)),
587         "got cbSize = %d (expected 64)\n", pZA->cbSize);
588
589     memset(buffer, -1, sizeof(buffer));
590     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, pZA);
591     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
592     ok((pZA->cbSize == 0xffffffff) || (pZA->cbSize == sizeof(ZONEATTRIBUTES)),
593         "got cbSize = 0x%x (expected 0xffffffff)\n", pZA->cbSize);
594
595     /* IE8 no longer fail on invalid zones */
596     memset(buffer, -1, sizeof(buffer));
597     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0xdeadbeef, pZA);
598     ok(hr == S_OK || (hr == E_FAIL),
599         "got 0x%x (expected S_OK or E_FAIL)\n", hr);
600
601     hr = IInternetZoneManager_GetZoneAttributes(zonemgr, 0, NULL);
602     ok(hr == E_INVALIDARG, "got 0x%x (expected E_INVALIDARG)\n", hr);
603
604     hr = IInternetZoneManager_Release(zonemgr);
605     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
606 }
607
608
609 START_TEST(sec_mgr)
610 {
611     OleInitialize(NULL);
612
613     test_SecurityManager();
614     test_polices();
615     test_CoInternetCreateZoneManager();
616     test_CreateZoneEnumerator();
617     test_GetZoneActionPolicy();
618     test_GetZoneAt();
619     test_GetZoneAttributes();
620
621     OleUninitialize();
622 }