mshtml: Added IHTMLStyleSheet::get_rules implementation.
[wine] / dlls / urlmon / tests / misc.c
1 /*
2  * Copyright 2005-2006 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20 #define CONST_VTABLE
21
22 #include <wine/test.h>
23 #include <stdarg.h>
24 #include <stddef.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "ole2.h"
29 #include "urlmon.h"
30
31 #include "initguid.h"
32
33 DEFINE_GUID(CLSID_AboutProtocol, 0x3050F406, 0x98B5, 0x11CF, 0xBB,0x82, 0x00,0xAA,0x00,0xBD,0xCE,0x0B);
34
35 #define DEFINE_EXPECT(func) \
36     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
37
38 #define SET_EXPECT(func) \
39     expect_ ## func = TRUE
40
41 #define CHECK_EXPECT(func) \
42     do { \
43         ok(expect_ ##func, "unexpected call " #func "\n"); \
44         expect_ ## func = FALSE; \
45         called_ ## func = TRUE; \
46     }while(0)
47
48 #define CHECK_EXPECT2(func) \
49     do { \
50         ok(expect_ ##func, "unexpected call " #func "\n"); \
51         called_ ## func = TRUE; \
52     }while(0)
53
54 #define CHECK_CALLED(func) \
55     do { \
56         ok(called_ ## func, "expected " #func "\n"); \
57         expect_ ## func = called_ ## func = FALSE; \
58     }while(0)
59
60 DEFINE_EXPECT(ParseUrl);
61 DEFINE_EXPECT(QI_IInternetProtocolInfo);
62 DEFINE_EXPECT(CreateInstance);
63 DEFINE_EXPECT(unk_Release);
64
65 static const char *debugstr_w(LPCWSTR str)
66 {
67     static char buf[1024];
68     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
69     return buf;
70 }
71
72 static void test_CreateFormatEnum(void)
73 {
74     IEnumFORMATETC *fenum = NULL, *fenum2 = NULL;
75     FORMATETC fetc[5];
76     ULONG ul;
77     HRESULT hres;
78
79     static DVTARGETDEVICE dev = {sizeof(dev),0,0,0,0,{0}};
80     static FORMATETC formatetc[] = {
81         {0,&dev,0,0,0},
82         {0,&dev,0,1,0},
83         {0,NULL,0,2,0},
84         {0,NULL,0,3,0},
85         {0,NULL,0,4,0}
86     };
87
88     hres = CreateFormatEnumerator(0, formatetc, &fenum);
89     ok(hres == E_FAIL, "CreateFormatEnumerator failed: %08x, expected E_FAIL\n", hres);
90     hres = CreateFormatEnumerator(0, formatetc, NULL);
91     ok(hres == E_INVALIDARG, "CreateFormatEnumerator failed: %08x, expected E_INVALIDARG\n", hres);
92     hres = CreateFormatEnumerator(5, formatetc, NULL);
93     ok(hres == E_INVALIDARG, "CreateFormatEnumerator failed: %08x, expected E_INVALIDARG\n", hres);
94
95
96     hres = CreateFormatEnumerator(5, formatetc, &fenum);
97     ok(hres == S_OK, "CreateFormatEnumerator failed: %08x\n", hres);
98     if(FAILED(hres))
99         return;
100
101     hres = IEnumFORMATETC_Next(fenum, 2, NULL, &ul);
102     ok(hres == E_INVALIDARG, "Next failed: %08x, expected E_INVALIDARG\n", hres);
103     ul = 100;
104     hres = IEnumFORMATETC_Next(fenum, 0, fetc, &ul);
105     ok(hres == S_OK, "Next failed: %08x\n", hres);
106     ok(ul == 0, "ul=%d, expected 0\n", ul);
107
108     hres = IEnumFORMATETC_Next(fenum, 2, fetc, &ul);
109     ok(hres == S_OK, "Next failed: %08x\n", hres);
110     ok(fetc[0].lindex == 0, "fetc[0].lindex=%d, expected 0\n", fetc[0].lindex);
111     ok(fetc[1].lindex == 1, "fetc[1].lindex=%d, expected 1\n", fetc[1].lindex);
112     ok(fetc[0].ptd == &dev, "fetc[0].ptd=%p, expected %p\n", fetc[0].ptd, &dev);
113     ok(ul == 2, "ul=%d, expected 2\n", ul);
114
115     hres = IEnumFORMATETC_Skip(fenum, 1);
116     ok(hres == S_OK, "Skip failed: %08x\n", hres);
117
118     hres = IEnumFORMATETC_Next(fenum, 4, fetc, &ul);
119     ok(hres == S_FALSE, "Next failed: %08x, expected S_FALSE\n", hres);
120     ok(fetc[0].lindex == 3, "fetc[0].lindex=%d, expected 3\n", fetc[0].lindex);
121     ok(fetc[1].lindex == 4, "fetc[1].lindex=%d, expected 4\n", fetc[1].lindex);
122     ok(fetc[0].ptd == NULL, "fetc[0].ptd=%p, expected NULL\n", fetc[0].ptd);
123     ok(ul == 2, "ul=%d, expected 2\n", ul);
124
125     hres = IEnumFORMATETC_Next(fenum, 4, fetc, &ul);
126     ok(hres == S_FALSE, "Next failed: %08x, expected S_FALSE\n", hres);
127     ok(ul == 0, "ul=%d, expected 0\n", ul);
128     ul = 100;
129     hres = IEnumFORMATETC_Next(fenum, 0, fetc, &ul);
130     ok(hres == S_OK, "Next failed: %08x\n", hres);
131     ok(ul == 0, "ul=%d, expected 0\n", ul);
132
133     hres = IEnumFORMATETC_Skip(fenum, 3);
134     ok(hres == S_FALSE, "Skip failed: %08x, expected S_FALSE\n", hres);
135
136     hres = IEnumFORMATETC_Reset(fenum);
137     ok(hres == S_OK, "Reset failed: %08x\n", hres);
138
139     hres = IEnumFORMATETC_Next(fenum, 5, fetc, NULL);
140     ok(hres == S_OK, "Next failed: %08x\n", hres);
141     ok(fetc[0].lindex == 0, "fetc[0].lindex=%d, expected 0\n", fetc[0].lindex);
142
143     hres = IEnumFORMATETC_Reset(fenum);
144     ok(hres == S_OK, "Reset failed: %08x\n", hres);
145
146     hres = IEnumFORMATETC_Skip(fenum, 2);
147     ok(hres == S_OK, "Skip failed: %08x\n", hres);
148
149     hres = IEnumFORMATETC_Clone(fenum, NULL);
150     ok(hres == E_INVALIDARG, "Clone failed: %08x, expected E_INVALIDARG\n", hres);
151
152     hres = IEnumFORMATETC_Clone(fenum, &fenum2);
153     ok(hres == S_OK, "Clone failed: %08x\n", hres);
154
155     if(SUCCEEDED(hres)) {
156         ok(fenum != fenum2, "fenum == fenum2\n");
157
158         hres = IEnumFORMATETC_Next(fenum2, 2, fetc, &ul);
159         ok(hres == S_OK, "Next failed: %08x\n", hres);
160         ok(fetc[0].lindex == 2, "fetc[0].lindex=%d, expected 2\n", fetc[0].lindex);
161
162         IEnumFORMATETC_Release(fenum2);
163     }
164
165     hres = IEnumFORMATETC_Next(fenum, 2, fetc, &ul);
166     ok(hres == S_OK, "Next failed: %08x\n", hres);
167     ok(fetc[0].lindex == 2, "fetc[0].lindex=%d, expected 2\n", fetc[0].lindex);
168
169     hres = IEnumFORMATETC_Skip(fenum, 1);
170     ok(hres == S_OK, "Skip failed: %08x\n", hres);
171     
172     IEnumFORMATETC_Release(fenum);
173 }
174
175 static void test_RegisterFormatEnumerator(void)
176 {
177     IBindCtx *bctx = NULL;
178     IEnumFORMATETC *format = NULL, *format2 = NULL;
179     IUnknown *unk = NULL;
180     HRESULT hres;
181
182     static FORMATETC formatetc = {0,NULL,0,0,0};
183     static WCHAR wszEnumFORMATETC[] =
184         {'_','E','n','u','m','F','O','R','M','A','T','E','T','C','_',0};
185
186     CreateBindCtx(0, &bctx);
187
188     hres = CreateFormatEnumerator(1, &formatetc, &format);
189     ok(hres == S_OK, "CreateFormatEnumerator failed: %08x\n", hres);
190     if(FAILED(hres))
191         return;
192
193     hres = RegisterFormatEnumerator(NULL, format, 0);
194     ok(hres == E_INVALIDARG,
195             "RegisterFormatEnumerator failed: %08x, expected E_INVALIDARG\n", hres);
196     hres = RegisterFormatEnumerator(bctx, NULL, 0);
197     ok(hres == E_INVALIDARG,
198             "RegisterFormatEnumerator failed: %08x, expected E_INVALIDARG\n", hres);
199
200     hres = RegisterFormatEnumerator(bctx, format, 0);
201     ok(hres == S_OK, "RegisterFormatEnumerator failed: %08x\n", hres);
202
203     hres = IBindCtx_GetObjectParam(bctx, wszEnumFORMATETC, &unk);
204     ok(hres == S_OK, "GetObjectParam failed: %08x\n", hres);
205     ok(unk == (IUnknown*)format, "unk != format\n");
206
207     hres = RevokeFormatEnumerator(NULL, format);
208     ok(hres == E_INVALIDARG,
209             "RevokeFormatEnumerator failed: %08x, expected E_INVALIDARG\n", hres);
210
211     hres = RevokeFormatEnumerator(bctx, format);
212     ok(hres == S_OK, "RevokeFormatEnumerator failed: %08x\n", hres);
213
214     hres = RevokeFormatEnumerator(bctx, format);
215     ok(hres == E_FAIL, "RevokeFormatEnumerator failed: %08x, expected E_FAIL\n", hres);
216
217     hres = IBindCtx_GetObjectParam(bctx, wszEnumFORMATETC, &unk);
218     ok(hres == E_FAIL, "GetObjectParam failed: %08x, expected E_FAIL\n", hres);
219
220     hres = RegisterFormatEnumerator(bctx, format, 0);
221     ok(hres == S_OK, "RegisterFormatEnumerator failed: %08x\n", hres);
222
223     hres = CreateFormatEnumerator(1, &formatetc, &format2);
224     ok(hres == S_OK, "CreateFormatEnumerator failed: %08x\n", hres);
225
226     if(SUCCEEDED(hres)) {
227         hres = RevokeFormatEnumerator(bctx, format);
228         ok(hres == S_OK, "RevokeFormatEnumerator failed: %08x\n", hres);
229
230         IEnumFORMATETC_Release(format2);
231     }
232
233     hres = IBindCtx_GetObjectParam(bctx, wszEnumFORMATETC, &unk);
234     ok(hres == E_FAIL, "GetObjectParam failed: %08x, expected E_FAIL\n", hres);
235
236     IEnumFORMATETC_Release(format);
237
238     hres = RegisterFormatEnumerator(bctx, format, 0);
239     ok(hres == S_OK, "RegisterFormatEnumerator failed: %08x\n", hres);
240     hres = RevokeFormatEnumerator(bctx, NULL);
241     ok(hres == S_OK, "RevokeFormatEnumerator failed: %08x\n", hres);
242     hres = IBindCtx_GetObjectParam(bctx, wszEnumFORMATETC, &unk);
243     ok(hres == E_FAIL, "GetObjectParam failed: %08x, expected E_FAIL\n", hres);
244
245     IEnumFORMATETC_Release(format);
246     IBindCtx_Release(bctx);
247 }
248
249 static const WCHAR url1[] = {'r','e','s',':','/','/','m','s','h','t','m','l','.','d','l','l',
250         '/','b','l','a','n','k','.','h','t','m',0};
251 static const WCHAR url2[] = {'i','n','d','e','x','.','h','t','m',0};
252 static const WCHAR url3[] = {'f','i','l','e',':','c',':','\\','I','n','d','e','x','.','h','t','m',0};
253 static const WCHAR url4[] = {'f','i','l','e',':','s','o','m','e','%','2','0','f','i','l','e',
254         '%','2','e','j','p','g',0};
255 static const WCHAR url5[] = {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q',
256         '.','o','r','g',0};
257 static const WCHAR url6[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
258 static const WCHAR url7[] = {'f','t','p',':','/','/','w','i','n','e','h','q','.','o','r','g','/',
259         'f','i','l','e','.','t','e','s','t',0};
260 static const WCHAR url8[] = {'t','e','s','t',':','1','2','3','a','b','c',0};
261
262
263 static const WCHAR url4e[] = {'f','i','l','e',':','s','o','m','e',' ','f','i','l','e',
264         '.','j','p','g',0};
265
266 static const WCHAR path3[] = {'c',':','\\','I','n','d','e','x','.','h','t','m',0};
267 static const WCHAR path4[] = {'s','o','m','e',' ','f','i','l','e','.','j','p','g',0};
268
269 static const WCHAR wszRes[] = {'r','e','s',0};
270 static const WCHAR wszFile[] = {'f','i','l','e',0};
271 static const WCHAR wszHttp[] = {'h','t','t','p',0};
272 static const WCHAR wszAbout[] = {'a','b','o','u','t',0};
273 static const WCHAR wszEmpty[] = {0};
274
275 struct parse_test {
276     LPCWSTR url;
277     HRESULT secur_hres;
278     LPCWSTR encoded_url;
279     HRESULT path_hres;
280     LPCWSTR path;
281     LPCWSTR schema;
282 };
283
284 static const struct parse_test parse_tests[] = {
285     {url1, S_OK,   url1,  E_INVALIDARG, NULL, wszRes},
286     {url2, E_FAIL, url2,  E_INVALIDARG, NULL, wszEmpty},
287     {url3, E_FAIL, url3,  S_OK, path3,        wszFile},
288     {url4, E_FAIL, url4e, S_OK, path4,        wszFile},
289     {url5, E_FAIL, url5,  E_INVALIDARG, NULL, wszHttp},
290     {url6, S_OK,   url6,  E_INVALIDARG, NULL, wszAbout}
291 };
292
293 static void test_CoInternetParseUrl(void)
294 {
295     HRESULT hres;
296     DWORD size;
297     int i;
298
299     static WCHAR buf[4096];
300
301     memset(buf, 0xf0, sizeof(buf));
302     hres = CoInternetParseUrl(parse_tests[0].url, PARSE_SCHEMA, 0, buf,
303             3, &size, 0);
304     ok(hres == E_POINTER, "schema failed: %08x, expected E_POINTER\n", hres);
305
306     for(i=0; i < sizeof(parse_tests)/sizeof(parse_tests[0]); i++) {
307         memset(buf, 0xf0, sizeof(buf));
308         hres = CoInternetParseUrl(parse_tests[i].url, PARSE_SECURITY_URL, 0, buf,
309                 sizeof(buf)/sizeof(WCHAR), &size, 0);
310         ok(hres == parse_tests[i].secur_hres, "[%d] security url failed: %08x, expected %08x\n",
311                 i, hres, parse_tests[i].secur_hres);
312
313         memset(buf, 0xf0, sizeof(buf));
314         hres = CoInternetParseUrl(parse_tests[i].url, PARSE_ENCODE, 0, buf,
315                 sizeof(buf)/sizeof(WCHAR), &size, 0);
316         ok(hres == S_OK, "[%d] encoding failed: %08x\n", i, hres);
317         ok(size == lstrlenW(parse_tests[i].encoded_url), "[%d] wrong size\n", i);
318         ok(!lstrcmpW(parse_tests[i].encoded_url, buf), "[%d] wrong encoded url\n", i);
319
320         memset(buf, 0xf0, sizeof(buf));
321         hres = CoInternetParseUrl(parse_tests[i].url, PARSE_PATH_FROM_URL, 0, buf,
322                 sizeof(buf)/sizeof(WCHAR), &size, 0);
323         ok(hres == parse_tests[i].path_hres, "[%d] path failed: %08x, expected %08x\n",
324                 i, hres, parse_tests[i].path_hres);
325         if(parse_tests[i].path) {
326             ok(size == lstrlenW(parse_tests[i].path), "[%d] wrong size\n", i);
327             ok(!lstrcmpW(parse_tests[i].path, buf), "[%d] wrong path\n", i);
328         }
329
330         memset(buf, 0xf0, sizeof(buf));
331         hres = CoInternetParseUrl(parse_tests[i].url, PARSE_SCHEMA, 0, buf,
332                 sizeof(buf)/sizeof(WCHAR), &size, 0);
333         ok(hres == S_OK, "[%d] schema failed: %08x\n", i, hres);
334         ok(size == lstrlenW(parse_tests[i].schema), "[%d] wrong size\n", i);
335         ok(!lstrcmpW(parse_tests[i].schema, buf), "[%d] wrong schema\n", i);
336     }
337 }
338
339 static void test_CoInternetCompareUrl(void)
340 {
341     HRESULT hres;
342
343     hres = CoInternetCompareUrl(url1, url1, 0);
344     ok(hres == S_OK, "CoInternetParseUrl failed: %08x\n", hres);
345
346     hres = CoInternetCompareUrl(url1, url3, 0);
347     ok(hres == S_FALSE, "CoInternetParseUrl failed: %08x\n", hres);
348
349     hres = CoInternetCompareUrl(url3, url1, 0);
350     ok(hres == S_FALSE, "CoInternetParseUrl failed: %08x\n", hres);
351 }
352
353 static const WCHAR mimeTextHtml[] = {'t','e','x','t','/','h','t','m','l',0};
354 static const WCHAR mimeTextPlain[] = {'t','e','x','t','/','p','l','a','i','n',0};
355 static const WCHAR mimeTextRichtext[] = {'t','e','x','t','/','r','i','c','h','t','e','x','t',0};
356 static const WCHAR mimeAppOctetStream[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
357     'o','c','t','e','t','-','s','t','r','e','a','m',0};
358 static const WCHAR mimeImagePjpeg[] = {'i','m','a','g','e','/','p','j','p','e','g',0};
359 static const WCHAR mimeImageGif[] = {'i','m','a','g','e','/','g','i','f',0};
360 static const WCHAR mimeImageBmp[] = {'i','m','a','g','e','/','b','m','p',0};
361 static const WCHAR mimeImageXPng[] = {'i','m','a','g','e','/','x','-','p','n','g',0};
362 static const WCHAR mimeImageTiff[] = {'i','m','a','g','e','/','t','i','f','f',0};
363 static const WCHAR mimeVideoAvi[] = {'v','i','d','e','o','/','a','v','i',0};
364 static const WCHAR mimeVideoMpeg[] = {'v','i','d','e','o','/','m','p','e','g',0};
365 static const WCHAR mimeAppPostscript[] =
366     {'a','p','p','l','i','c','a','t','i','o','n','/','p','o','s','t','s','c','r','i','p','t',0};
367 static const WCHAR mimeAppXCompressed[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
368                                     'x','-','c','o','m','p','r','e','s','s','e','d',0};
369 static const WCHAR mimeAppXZip[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
370                                     'x','-','z','i','p','-','c','o','m','p','r','e','s','s','e','d',0};
371 static const WCHAR mimeAppXGzip[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
372                                     'x','-','g','z','i','p','-','c','o','m','p','r','e','s','s','e','d',0};
373 static const WCHAR mimeAppJava[] = {'a','p','p','l','i','c','a','t','i','o','n','/','j','a','v','a',0};
374 static const WCHAR mimeAppPdf[] = {'a','p','p','l','i','c','a','t','i','o','n','/','p','d','f',0};
375 static const WCHAR mimeAppXMSDownload[] =
376     {'a','p','p','l','i','c','a','t','i','o','n','/','x','-','m','s','d','o','w','n','l','o','a','d',0};
377 static const WCHAR mimeAudioWav[] = {'a','u','d','i','o','/','w','a','v',0};
378
379 static const struct {
380     LPCWSTR url;
381     LPCWSTR mime;
382     HRESULT hres;
383 } mime_tests[] = {
384     {url1, mimeTextHtml, S_OK},
385     {url2, mimeTextHtml, S_OK},
386     {url3, mimeTextHtml, S_OK},
387     {url4, NULL, E_FAIL},
388     {url5, NULL, __HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)},
389     {url6, NULL, E_FAIL},
390     {url7, NULL, __HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)}
391 };
392
393 static BYTE data1[] = "test data\n";
394 static BYTE data2[] = {31,'t','e','s',0xfa,'t',' ','d','a','t','a','\n',0};
395 static BYTE data3[] = {0,0,0};
396 static BYTE data4[] = {'t','e','s',0xfa,'t',' ','d','a','t','a','\n',0,0};
397 static BYTE data5[] = {0xa,0xa,0xa,'x',32,'x',0};
398 static BYTE data6[] = {0xfa,0xfa,0xfa,0xfa,'\n','\r','\t','x','x','x',1};
399 static BYTE data7[] = "<html>blahblah";
400 static BYTE data8[] = {'t','e','s',0xfa,'t',' ','<','h','t','m','l','>','d','a','t','a','\n',0,0};
401 static BYTE data9[] = {'t','e',0,'s',0xfa,'t',' ','<','h','t','m','l','>','d','a','t','a','\n',0,0};
402 static BYTE data10[] = "<HtmL>blahblah";
403 static BYTE data11[] = "blah<HTML>blahblah";
404 static BYTE data12[] = "blah<HTMLblahblah";
405 static BYTE data13[] = "blahHTML>blahblah";
406 static BYTE data14[] = "blah<HTMblahblah";
407 static BYTE data15[] = {0xff,0xd8};
408 static BYTE data16[] = {0xff,0xd8,'h'};
409 static BYTE data17[] = {0,0xff,0xd8};
410 static BYTE data18[] = {0xff,0xd8,'<','h','t','m','l','>'};
411 static BYTE data19[] = {'G','I','F','8','7','a'};
412 static BYTE data20[] = {'G','I','F','8','9','a'};
413 static BYTE data21[] = {'G','I','F','8','7'};
414 static BYTE data22[] = {'G','i','F','8','7','a'};
415 static BYTE data23[] = {'G','i','F','8','8','a'};
416 static BYTE data24[] = {'g','i','f','8','7','a'};
417 static BYTE data25[] = {'G','i','F','8','7','A'};
418 static BYTE data26[] = {'G','i','F','8','7','a','<','h','t','m','l','>'};
419 static BYTE data27[] = {0x30,'G','i','F','8','7','A'};
420 static BYTE data28[] = {0x42,0x4d,0x6e,0x42,0x1c,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00};
421 static BYTE data29[] = {0x42,0x4d,'x','x','x','x',0x00,0x00,0x00,0x00,'x','x','x','x'};
422 static BYTE data30[] = {0x42,0x4d,'x','x','x','x',0x00,0x01,0x00,0x00,'x','x','x','x'};
423 static BYTE data31[] = {0x42,0x4d,'x','x','x','x',0x00,0x00,0x00,0x00,'<','h','t','m','l','>'};
424 static BYTE data32[] = {0x42,0x4d,'x','x','x','x',0x00,0x00,0x00,0x00,'x','x','x'};
425 static BYTE data33[] = {0x00,0x42,0x4d,'x','x','x','x',0x00,0x00,0x00,0x00,'x','x','x'};
426 static BYTE data34[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a,'x'};
427 static BYTE data35[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a,'x','x','x','x',0};
428 static BYTE data36[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,'x','x'};
429 static BYTE data37[] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a,'<','h','t','m','l','>'};
430 static BYTE data38[] = {0x00,0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a,'x'};
431 static BYTE data39[] = {0x4d,0x4d,0x00,0x2a};
432 static BYTE data40[] = {0x4d,0x4d,0x00,0x2a,'<','h','t','m','l','>',0};
433 static BYTE data41[] = {0x4d,0x4d,0xff};
434 static BYTE data42[] = {0x4d,0x4d};
435 static BYTE data43[] = {0x00,0x4d,0x4d,0x00};
436 static BYTE data44[] = {'R','I','F','F',0xff,0xff,0xff,0xff,'A','V','I',0x20,0xff};
437 static BYTE data45[] = {'R','I','F','f',0xff,0xff,0xff,0xff,'A','V','I',0x20,0xff};
438 static BYTE data46[] = {'R','I','F','F',0xff,0xff,0xff,0xff,'A','V','I',0x20};
439 static BYTE data47[] = {'R','I','F','F',0xff,0xff,0xff,0xff,'A','V','I',0x21,0xff};
440 static BYTE data48[] = {'R','I','F','F',0xff,0xff,0xff,0xff,'A','V','I',0x20,'<','h','t','m','l','>'};
441 static BYTE data49[] = {'R','I','F','F',0x0f,0x0f,0xf0,0xf0,'A','V','I',0x20,0xf0,0x00};
442 static BYTE data50[] = {0x00,0x00,0x01,0xb3,0xff};
443 static BYTE data51[] = {0x00,0x00,0x01,0xba,0xff};
444 static BYTE data52[] = {0x00,0x00,0x01,0xb8,0xff};
445 static BYTE data53[] = {0x00,0x00,0x01,0xba};
446 static BYTE data54[] = {0x00,0x00,0x01,0xba,'<','h','t','m','l','>'};
447 static BYTE data55[] = {0x1f,0x8b,'x'};
448 static BYTE data56[] = {0x1f};
449 static BYTE data57[] = {0x1f,0x8b,'<','h','t','m','l','>','t','e','s','t',0};
450 static BYTE data58[] = {0x1f,0x8b};
451 static BYTE data59[] = {0x50,0x4b,'x'};
452 static BYTE data60[] = {0x50,0x4b};
453 static BYTE data61[] = {0x50,0x4b,'<','h','t','m','l','>',0};
454 static BYTE data62[] = {0xca,0xfe,0xba,0xbe,'x'};
455 static BYTE data63[] = {0xca,0xfe,0xba,0xbe};
456 static BYTE data64[] = {0xca,0xfe,0xba,0xbe,'<','h','t','m','l','>',0};
457 static BYTE data65[] = {0x25,0x50,0x44,0x46,'x'};
458 static BYTE data66[] = {0x25,0x50,0x44,0x46};
459 static BYTE data67[] = {0x25,0x50,0x44,0x46,'x','<','h','t','m','l','>'};
460 static BYTE data68[] = {'M','Z','x'};
461 static BYTE data69[] = {'M','Z'};
462 static BYTE data70[] = {'M','Z','<','h','t','m','l','>',0xff};
463 static BYTE data71[] = {'{','\\','r','t','f',0};
464 static BYTE data72[] = {'{','\\','r','t','f'};
465 static BYTE data73[] = {' ','{','\\','r','t','f',' '};
466 static BYTE data74[] = {'{','\\','r','t','f','<','h','t','m','l','>',' '};
467 static BYTE data75[] = {'R','I','F','F',0xff,0xff,0xff,0xff,'W','A','V','E',0xff};
468 static BYTE data76[] = {'R','I','F','F',0xff,0xff,0xff,0xff,'W','A','V','E'};
469 static BYTE data77[] = {'R','I','F','F',0xff,0xff,0xff,0xff,'W','A','V',0xff,0xff};
470 static BYTE data78[] = {'R','I','F','F',0xff,0xff,0xff,0xff,'<','h','t','m','l','>',0xff};
471 static BYTE data79[] = {'%','!',0xff};
472 static BYTE data80[] = {'%','!'};
473 static BYTE data81[] = {'%','!','P','S','<','h','t','m','l','>'};
474
475 static const struct {
476     BYTE *data;
477     DWORD size;
478     LPCWSTR mime;
479 } mime_tests2[] = {
480     {data1, sizeof(data1), mimeTextPlain},
481     {data2, sizeof(data2), mimeAppOctetStream},
482     {data3, sizeof(data3), mimeAppOctetStream},
483     {data4, sizeof(data4), mimeAppOctetStream},
484     {data5, sizeof(data5), mimeTextPlain},
485     {data6, sizeof(data6), mimeTextPlain},
486     {data7, sizeof(data7), mimeTextHtml},
487     {data8, sizeof(data8), mimeTextHtml},
488     {data9, sizeof(data9), mimeTextHtml},
489     {data10, sizeof(data10), mimeTextHtml},
490     {data11, sizeof(data11), mimeTextHtml},
491     {data12, sizeof(data12), mimeTextHtml},
492     {data13, sizeof(data13), mimeTextPlain},
493     {data14, sizeof(data14), mimeTextPlain},
494     {data15, sizeof(data15), mimeTextPlain},
495     {data16, sizeof(data16), mimeImagePjpeg},
496     {data17, sizeof(data17), mimeAppOctetStream},
497     {data18, sizeof(data18), mimeTextHtml},
498     {data19, sizeof(data19), mimeImageGif},
499     {data20, sizeof(data20), mimeImageGif},
500     {data21, sizeof(data21), mimeTextPlain},
501     {data22, sizeof(data22), mimeImageGif},
502     {data23, sizeof(data23), mimeTextPlain},
503     {data24, sizeof(data24), mimeImageGif},
504     {data25, sizeof(data25), mimeImageGif},
505     {data26, sizeof(data26), mimeTextHtml},
506     {data27, sizeof(data27), mimeTextPlain},
507     {data28, sizeof(data28), mimeImageBmp},
508     {data29, sizeof(data29), mimeImageBmp},
509     {data30, sizeof(data30), mimeAppOctetStream},
510     {data31, sizeof(data31), mimeTextHtml},
511     {data32, sizeof(data32), mimeAppOctetStream},
512     {data33, sizeof(data33), mimeAppOctetStream},
513     {data34, sizeof(data34), mimeImageXPng},
514     {data35, sizeof(data35), mimeImageXPng},
515     {data36, sizeof(data36), mimeAppOctetStream},
516     {data37, sizeof(data37), mimeTextHtml},
517     {data38, sizeof(data38), mimeAppOctetStream},
518     {data39, sizeof(data39), mimeImageTiff},
519     {data40, sizeof(data40), mimeTextHtml},
520     {data41, sizeof(data41), mimeImageTiff},
521     {data42, sizeof(data42), mimeTextPlain},
522     {data43, sizeof(data43), mimeAppOctetStream},
523     {data44, sizeof(data44), mimeVideoAvi},
524     {data45, sizeof(data45), mimeTextPlain},
525     {data46, sizeof(data46), mimeTextPlain},
526     {data47, sizeof(data47), mimeTextPlain},
527     {data48, sizeof(data48), mimeTextHtml},
528     {data49, sizeof(data49), mimeVideoAvi},
529     {data50, sizeof(data50), mimeVideoMpeg},
530     {data51, sizeof(data51), mimeVideoMpeg},
531     {data52, sizeof(data52), mimeAppOctetStream},
532     {data53, sizeof(data53), mimeAppOctetStream},
533     {data54, sizeof(data54), mimeTextHtml},
534     {data55, sizeof(data55), mimeAppXGzip},
535     {data56, sizeof(data56), mimeTextPlain},
536     {data57, sizeof(data57), mimeTextHtml},
537     {data58, sizeof(data58), mimeAppOctetStream},
538     {data59, sizeof(data59), mimeAppXZip},
539     {data60, sizeof(data60), mimeTextPlain},
540     {data61, sizeof(data61), mimeTextHtml},
541     {data62, sizeof(data62), mimeAppJava},
542     {data63, sizeof(data63), mimeTextPlain},
543     {data64, sizeof(data64), mimeTextHtml},
544     {data65, sizeof(data65), mimeAppPdf},
545     {data66, sizeof(data66), mimeTextPlain},
546     {data67, sizeof(data67), mimeTextHtml},
547     {data68, sizeof(data68), mimeAppXMSDownload},
548     {data69, sizeof(data69), mimeTextPlain},
549     {data70, sizeof(data70), mimeTextHtml},
550     {data71, sizeof(data71), mimeTextRichtext},
551     {data72, sizeof(data72), mimeTextPlain},
552     {data73, sizeof(data73), mimeTextPlain},
553     {data74, sizeof(data74), mimeTextHtml},
554     {data75, sizeof(data75), mimeAudioWav},
555     {data76, sizeof(data76), mimeTextPlain},
556     {data77, sizeof(data77), mimeTextPlain},
557     {data78, sizeof(data78), mimeTextHtml},
558     {data79, sizeof(data79), mimeAppPostscript},
559     {data80, sizeof(data80), mimeTextPlain},
560     {data81, sizeof(data81), mimeTextHtml}
561 };
562
563 static void test_FindMimeFromData(void)
564 {
565     HRESULT hres;
566     LPWSTR mime;
567     int i;
568
569     for(i=0; i<sizeof(mime_tests)/sizeof(mime_tests[0]); i++) {
570         mime = (LPWSTR)0xf0f0f0f0;
571         hres = FindMimeFromData(NULL, mime_tests[i].url, NULL, 0, NULL, 0, &mime, 0);
572         if(mime_tests[i].mime) {
573             ok(hres == S_OK, "[%d] FindMimeFromData failed: %08x\n", i, hres);
574             ok(!lstrcmpW(mime, mime_tests[i].mime), "[%d] wrong mime\n", i);
575             CoTaskMemFree(mime);
576         }else {
577             ok(hres == E_FAIL || hres == mime_tests[i].hres,
578                "[%d] FindMimeFromData failed: %08x, expected %08x\n",
579                i, hres, mime_tests[i].hres);
580             ok(mime == (LPWSTR)0xf0f0f0f0, "[%d] mime != 0xf0f0f0f0\n", i);
581         }
582
583         mime = (LPWSTR)0xf0f0f0f0;
584         hres = FindMimeFromData(NULL, mime_tests[i].url, NULL, 0, mimeTextPlain, 0, &mime, 0);
585         ok(hres == S_OK, "[%d] FindMimeFromData failed: %08x\n", i, hres);
586         ok(!lstrcmpW(mime, mimeTextPlain), "[%d] wrong mime\n", i);
587         CoTaskMemFree(mime);
588
589         mime = (LPWSTR)0xf0f0f0f0;
590         hres = FindMimeFromData(NULL, mime_tests[i].url, NULL, 0, mimeAppOctetStream, 0, &mime, 0);
591         ok(hres == S_OK, "[%d] FindMimeFromData failed: %08x\n", i, hres);
592         ok(!lstrcmpW(mime, mimeAppOctetStream), "[%d] wrong mime\n", i);
593         CoTaskMemFree(mime);
594     }
595
596     for(i=0; i < sizeof(mime_tests2)/sizeof(mime_tests2[0]); i++) {
597         hres = FindMimeFromData(NULL, NULL, mime_tests2[i].data, mime_tests2[i].size,
598                 NULL, 0, &mime, 0);
599         ok(hres == S_OK, "[%d] FindMimeFromData failed: %08x\n", i, hres);
600         ok(!lstrcmpW(mime, mime_tests2[i].mime), "[%d] wrong mime: %s\n", i, debugstr_w(mime));
601         CoTaskMemFree(mime);
602
603         hres = FindMimeFromData(NULL, NULL, mime_tests2[i].data, mime_tests2[i].size,
604                 mimeTextHtml, 0, &mime, 0);
605         ok(hres == S_OK, "[%d] FindMimeFromData failed: %08x\n", i, hres);
606         if(!lstrcmpW(mimeAppOctetStream, mime_tests2[i].mime)
607            || !lstrcmpW(mimeTextPlain, mime_tests2[i].mime))
608             ok(!lstrcmpW(mime, mimeTextHtml), "[%d] wrong mime\n", i);
609         else
610             ok(!lstrcmpW(mime, mime_tests2[i].mime), "[%d] wrong mime\n", i);
611
612         hres = FindMimeFromData(NULL, NULL, mime_tests2[i].data, mime_tests2[i].size,
613                 mimeImagePjpeg, 0, &mime, 0);
614         ok(hres == S_OK, "[%d] FindMimeFromData failed: %08x\n", i, hres);
615         if(!lstrcmpW(mimeAppOctetStream, mime_tests2[i].mime) || i == 17)
616             ok(!lstrcmpW(mime, mimeImagePjpeg), "[%d] wrong mime\n", i);
617         else
618             ok(!lstrcmpW(mime, mime_tests2[i].mime), "[%d] wrong mime\n", i);
619
620         CoTaskMemFree(mime);
621     }
622
623     hres = FindMimeFromData(NULL, url1, data1, sizeof(data1), NULL, 0, &mime, 0);
624     ok(hres == S_OK, "FindMimeFromData failed: %08x\n", hres);
625     ok(!lstrcmpW(mime, mimeTextPlain), "wrong mime\n");
626     CoTaskMemFree(mime);
627
628     hres = FindMimeFromData(NULL, url1, data1, sizeof(data1), mimeAppOctetStream, 0, &mime, 0);
629     ok(hres == S_OK, "FindMimeFromData failed: %08x\n", hres);
630     ok(!lstrcmpW(mime, mimeTextPlain), "wrong mime\n");
631     CoTaskMemFree(mime);
632
633     hres = FindMimeFromData(NULL, url4, data1, sizeof(data1), mimeAppOctetStream, 0, &mime, 0);
634     ok(hres == S_OK, "FindMimeFromData failed: %08x\n", hres);
635     ok(!lstrcmpW(mime, mimeTextPlain), "wrong mime\n");
636     CoTaskMemFree(mime);
637
638     hres = FindMimeFromData(NULL, NULL, NULL, 0, NULL, 0, &mime, 0);
639     ok(hres == E_INVALIDARG, "FindMimeFromData failed: %08x, excepted E_INVALIDARG\n", hres);
640
641     hres = FindMimeFromData(NULL, NULL, NULL, 0, mimeTextPlain, 0, &mime, 0);
642     ok(hres == E_INVALIDARG, "FindMimeFromData failed: %08x, expected E_INVALIDARG\n", hres);
643
644     hres = FindMimeFromData(NULL, NULL, data1, 0, NULL, 0, &mime, 0);
645     ok(hres == E_FAIL, "FindMimeFromData failed: %08x, expected E_FAIL\n", hres);
646
647     hres = FindMimeFromData(NULL, url1, data1, 0, NULL, 0, &mime, 0);
648     ok(hres == E_FAIL, "FindMimeFromData failed: %08x, expected E_FAIL\n", hres);
649
650     hres = FindMimeFromData(NULL, NULL, data1, 0, mimeTextPlain, 0, &mime, 0);
651     ok(hres == S_OK, "FindMimeFromData failed: %08x\n", hres);
652     ok(!lstrcmpW(mime, mimeTextPlain), "wrong mime\n");
653     CoTaskMemFree(mime);
654
655     hres = FindMimeFromData(NULL, NULL, data1, 0, mimeTextPlain, 0, NULL, 0);
656     ok(hres == E_INVALIDARG, "FindMimeFromData failed: %08x, expected E_INVALIDARG\n", hres);
657 }
658
659 static const BYTE secid1[] = {'f','i','l','e',':',0,0,0,0};
660 static const BYTE secid4[] ={'f','i','l','e',':',3,0,0,0};
661 static const BYTE secid5[] = {'h','t','t','p',':','w','w','w','.','w','i','n','e','h','q',
662     '.','o','r','g',3,0,0,0};
663 static const BYTE secid6[] = {'a','b','o','u','t',':','b','l','a','n','k',3,0,0,0};
664 static const BYTE secid7[] = {'f','t','p',':','w','i','n','e','h','q','.','o','r','g',
665                               3,0,0,0};
666
667 static struct secmgr_test {
668     LPCWSTR url;
669     DWORD zone;
670     HRESULT zone_hres;
671     DWORD secid_size;
672     const BYTE *secid;
673     HRESULT secid_hres;
674 } secmgr_tests[] = {
675     {url1, 0,   S_OK, sizeof(secid1), secid1, S_OK},
676     {url2, 100, 0x80041001, 0, NULL, E_INVALIDARG},
677     {url3, 0,   S_OK, sizeof(secid1), secid1, S_OK},
678     {url4, 3,   S_OK, sizeof(secid4), secid4, S_OK},
679     {url5, 3,   S_OK, sizeof(secid5), secid5, S_OK},
680     {url6, 3,   S_OK, sizeof(secid6), secid6, S_OK},
681     {url7, 3,   S_OK, sizeof(secid7), secid7, S_OK}
682 };
683
684 static void test_SecurityManager(void)
685 {
686     int i;
687     IInternetSecurityManager *secmgr = NULL;
688     BYTE buf[512];
689     DWORD zone, size;
690     HRESULT hres;
691
692     hres = CoInternetCreateSecurityManager(NULL, &secmgr, 0);
693     ok(hres == S_OK, "CoInternetCreateSecurityManager failed: %08x\n", hres);
694     if(FAILED(hres))
695         return;
696
697     for(i=0; i < sizeof(secmgr_tests)/sizeof(secmgr_tests[0]); i++) {
698         zone = 100;
699         hres = IInternetSecurityManager_MapUrlToZone(secmgr, secmgr_tests[i].url,
700                                                      &zone, 0);
701         ok(hres == secmgr_tests[i].zone_hres,
702            "[%d] MapUrlToZone failed: %08x, expected %08x\n",
703                 i, hres, secmgr_tests[i].zone_hres);
704         if(SUCCEEDED(hres))
705             ok(zone == secmgr_tests[i].zone, "[%d] zone=%d, expected %d\n", i, zone,
706                secmgr_tests[i].zone);
707         else
708             ok(zone == secmgr_tests[i].zone || zone == -1, "[%d] zone=%d\n", i, zone);
709
710         size = sizeof(buf);
711         memset(buf, 0xf0, sizeof(buf));
712         hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[i].url,
713                 buf, &size, 0);
714         ok(hres == secmgr_tests[i].secid_hres,
715            "[%d] GetSecurityId failed: %08x, expected %08x\n",
716            i, hres, secmgr_tests[i].secid_hres);
717         if(secmgr_tests[i].secid) {
718             ok(size == secmgr_tests[i].secid_size, "[%d] size=%d, expected %d\n",
719                     i, size, secmgr_tests[i].secid_size);
720             ok(!memcmp(buf, secmgr_tests[i].secid, size), "[%d] wrong secid\n", i);
721         }
722     }
723
724     zone = 100;
725     hres = IInternetSecurityManager_MapUrlToZone(secmgr, NULL, &zone, 0);
726     ok(hres == E_INVALIDARG, "MapUrlToZone failed: %08x, expected E_INVALIDARG\n", hres);
727     ok(zone == 100 || zone == -1, "zone=%d\n", zone);
728
729     size = sizeof(buf);
730     hres = IInternetSecurityManager_GetSecurityId(secmgr, NULL, buf, &size, 0);
731     ok(hres == E_INVALIDARG,
732        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
733     hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[1].url,
734                                                   NULL, &size, 0);
735     ok(hres == E_INVALIDARG,
736        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
737     hres = IInternetSecurityManager_GetSecurityId(secmgr, secmgr_tests[1].url,
738                                                   buf, NULL, 0);
739     ok(hres == E_INVALIDARG,
740        "GetSecurityId failed: %08x, expected E_INVALIDARG\n", hres);
741
742     IInternetSecurityManager_Release(secmgr);
743 }
744
745 static void test_ZoneManager(void)
746 {
747     IInternetZoneManager *zonemgr = NULL;
748     BYTE buf[32];
749     HRESULT hres;
750
751     hres = CoInternetCreateZoneManager(NULL, &zonemgr, 0);
752     ok(hres == S_OK, "CoInternetCreateZoneManager failed: %08x\n", hres);
753     if(FAILED(hres))
754         return;
755
756     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, 0x1a10, buf,
757             sizeof(DWORD), URLZONEREG_DEFAULT);
758     ok(hres == S_OK, "GetZoneActionPolicy failed: %08x\n", hres);
759     ok(*(DWORD*)buf == 1, "policy=%d, expected 1\n", *(DWORD*)buf);
760
761     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, 0x1a10, NULL,
762             sizeof(DWORD), URLZONEREG_DEFAULT);
763     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
764
765     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, 0x1a10, buf,
766             2, URLZONEREG_DEFAULT);
767     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
768
769     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 3, 0x1fff, buf,
770             sizeof(DWORD), URLZONEREG_DEFAULT);
771     ok(hres == E_FAIL, "GetZoneActionPolicy failed: %08x, expected E_FAIL\n", hres);
772
773     hres = IInternetZoneManager_GetZoneActionPolicy(zonemgr, 13, 0x1a10, buf,
774             sizeof(DWORD), URLZONEREG_DEFAULT);
775     ok(hres == E_INVALIDARG, "GetZoneActionPolicy failed: %08x, expected E_INVALIDARG\n", hres);
776
777     IInternetZoneManager_Release(zonemgr);
778 }
779
780 static void register_protocols(void)
781 {
782     IInternetSession *session;
783     IClassFactory *factory;
784     HRESULT hres;
785
786     static const WCHAR wszAbout[] = {'a','b','o','u','t',0};
787
788     hres = CoInternetGetSession(0, &session, 0);
789     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
790     if(FAILED(hres))
791         return;
792
793     hres = CoGetClassObject(&CLSID_AboutProtocol, CLSCTX_INPROC_SERVER, NULL,
794             &IID_IClassFactory, (void**)&factory);
795     ok(hres == S_OK, "Coud not get AboutProtocol factory: %08x\n", hres);
796     if(FAILED(hres))
797         return;
798
799     IInternetSession_RegisterNameSpace(session, factory, &CLSID_AboutProtocol,
800                                        wszAbout, 0, NULL, 0);
801     IClassFactory_Release(factory);
802
803     IInternetSession_Release(session);
804 }
805
806 static HRESULT WINAPI InternetProtocolInfo_QueryInterface(IInternetProtocolInfo *iface,
807                                                           REFIID riid, void **ppv)
808 {
809     ok(0, "unexpected call\n");
810     return E_NOINTERFACE;
811 }
812
813 static ULONG WINAPI InternetProtocolInfo_AddRef(IInternetProtocolInfo *iface)
814 {
815     return 2;
816 }
817
818 static ULONG WINAPI InternetProtocolInfo_Release(IInternetProtocolInfo *iface)
819 {
820     return 1;
821 }
822
823 static HRESULT WINAPI InternetProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
824         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
825         DWORD *pcchResult, DWORD dwReserved)
826 {
827     CHECK_EXPECT(ParseUrl);
828     return E_NOTIMPL;
829 }
830
831 static HRESULT WINAPI InternetProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
832         LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
833         LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult, DWORD dwReserved)
834 {
835     ok(0, "unexpected call\n");
836     return E_NOTIMPL;
837 }
838
839 static HRESULT WINAPI InternetProtocolInfo_CompareUrl(IInternetProtocolInfo *iface,
840         LPCWSTR pwzUrl1, LPCWSTR pwzUrl2, DWORD dwCompareFlags)
841 {
842     ok(0, "unexpected call\n");
843     return E_NOTIMPL;
844 }
845
846 static HRESULT WINAPI InternetProtocolInfo_QueryInfo(IInternetProtocolInfo *iface,
847         LPCWSTR pwzUrl, QUERYOPTION OueryOption, DWORD dwQueryFlags, LPVOID pBuffer,
848         DWORD cbBuffer, DWORD *pcbBuf, DWORD dwReserved)
849 {
850     ok(0, "unexpected call\n");
851     return E_NOTIMPL;
852 }
853
854 static const IInternetProtocolInfoVtbl InternetProtocolInfoVtbl = {
855     InternetProtocolInfo_QueryInterface,
856     InternetProtocolInfo_AddRef,
857     InternetProtocolInfo_Release,
858     InternetProtocolInfo_ParseUrl,
859     InternetProtocolInfo_CombineUrl,
860     InternetProtocolInfo_CompareUrl,
861     InternetProtocolInfo_QueryInfo
862 };
863
864 static IInternetProtocolInfo protocol_info = { &InternetProtocolInfoVtbl };
865
866 static HRESULT qiret;
867 static IClassFactory *expect_cf;
868
869 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
870 {
871     if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
872         CHECK_EXPECT(QI_IInternetProtocolInfo);
873         ok(iface == expect_cf, "unexpected iface\n");
874         *ppv = &protocol_info;
875         return qiret;
876     }
877
878     ok(0, "unexpected call\n");
879     return E_NOINTERFACE;
880 }
881
882 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
883 {
884     return 2;
885 }
886
887 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
888 {
889     return 1;
890 }
891
892 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
893                                         REFIID riid, void **ppv)
894 {
895     CHECK_EXPECT(CreateInstance);
896
897     ok(iface == expect_cf, "unexpected iface\n");
898     ok(pOuter == NULL, "pOuter = %p\n", pOuter);
899     ok(IsEqualGUID(&IID_IInternetProtocolInfo, riid), "unexpected riid\n");
900     ok(ppv != NULL, "ppv == NULL\n");
901
902     *ppv = &protocol_info;
903     return S_OK;
904 }
905
906 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
907 {
908     ok(0, "unexpected call\n");
909     return S_OK;
910 }
911
912 static const IClassFactoryVtbl ClassFactoryVtbl = {
913     ClassFactory_QueryInterface,
914     ClassFactory_AddRef,
915     ClassFactory_Release,
916     ClassFactory_CreateInstance,
917     ClassFactory_LockServer
918 };
919
920 static IClassFactory test_protocol_cf = { &ClassFactoryVtbl };
921 static IClassFactory test_protocol_cf2 = { &ClassFactoryVtbl };
922
923 static void test_NameSpace(void)
924 {
925     IInternetSession *session;
926     WCHAR buf[200];
927     DWORD size;
928     HRESULT hres;
929
930     static const WCHAR wszTest[] = {'t','e','s','t',0};
931
932     hres = CoInternetGetSession(0, &session, 0);
933     ok(hres == S_OK, "CoInternetGetSession failed: %08x\n", hres);
934     if(FAILED(hres))
935         return;
936
937     hres = IInternetSession_RegisterNameSpace(session, NULL, &IID_NULL,
938                                               wszTest, 0, NULL, 0);
939     ok(hres == E_INVALIDARG, "RegisterNameSpace failed: %08x\n", hres);
940
941     hres = IInternetSession_RegisterNameSpace(session, &test_protocol_cf, &IID_NULL,
942                                               NULL, 0, NULL, 0);
943     ok(hres == E_INVALIDARG, "RegisterNameSpace failed: %08x\n", hres);
944
945     hres = IInternetSession_RegisterNameSpace(session, &test_protocol_cf, &IID_NULL,
946                                               wszTest, 0, NULL, 0);
947     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
948
949     qiret = E_NOINTERFACE;
950     expect_cf = &test_protocol_cf;
951     SET_EXPECT(QI_IInternetProtocolInfo);
952     SET_EXPECT(CreateInstance);
953     SET_EXPECT(ParseUrl);
954
955     hres = CoInternetParseUrl(url8, PARSE_ENCODE, 0, buf, sizeof(buf)/sizeof(WCHAR),
956                               &size, 0);
957     ok(hres == S_OK, "CoInternetParseUrl failed: %08x\n", hres);
958
959     CHECK_CALLED(QI_IInternetProtocolInfo);
960     CHECK_CALLED(CreateInstance);
961     CHECK_CALLED(ParseUrl);
962
963     qiret = S_OK;
964     SET_EXPECT(QI_IInternetProtocolInfo);
965     SET_EXPECT(ParseUrl);
966
967     hres = CoInternetParseUrl(url8, PARSE_ENCODE, 0, buf, sizeof(buf)/sizeof(WCHAR),
968                               &size, 0);
969     ok(hres == S_OK, "CoInternetParseUrl failed: %08x\n", hres);
970
971     CHECK_CALLED(QI_IInternetProtocolInfo);
972     CHECK_CALLED(ParseUrl);
973
974     hres = IInternetSession_UnregisterNameSpace(session, &test_protocol_cf, wszTest);
975     ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
976
977     hres = CoInternetParseUrl(url8, PARSE_ENCODE, 0, buf, sizeof(buf)/sizeof(WCHAR),
978                               &size, 0);
979     ok(hres == S_OK, "CoInternetParseUrl failed: %08x\n", hres);
980
981     hres = IInternetSession_RegisterNameSpace(session, &test_protocol_cf2, &IID_NULL,
982                                               wszTest, 0, NULL, 0);
983     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
984
985     hres = IInternetSession_RegisterNameSpace(session, &test_protocol_cf, &IID_NULL,
986                                               wszTest, 0, NULL, 0);
987     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
988
989     hres = IInternetSession_RegisterNameSpace(session, &test_protocol_cf, &IID_NULL,
990                                               wszTest, 0, NULL, 0);
991     ok(hres == S_OK, "RegisterNameSpace failed: %08x\n", hres);
992
993     SET_EXPECT(QI_IInternetProtocolInfo);
994     SET_EXPECT(ParseUrl);
995
996     hres = CoInternetParseUrl(url8, PARSE_ENCODE, 0, buf, sizeof(buf)/sizeof(WCHAR),
997                               &size, 0);
998     ok(hres == S_OK, "CoInternetParseUrl failed: %08x\n", hres);
999
1000     CHECK_CALLED(QI_IInternetProtocolInfo);
1001     CHECK_CALLED(ParseUrl);
1002
1003     hres = IInternetSession_UnregisterNameSpace(session, &test_protocol_cf, wszTest);
1004     ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
1005
1006     SET_EXPECT(QI_IInternetProtocolInfo);
1007     SET_EXPECT(ParseUrl);
1008
1009     hres = CoInternetParseUrl(url8, PARSE_ENCODE, 0, buf, sizeof(buf)/sizeof(WCHAR),
1010                               &size, 0);
1011     ok(hres == S_OK, "CoInternetParseUrl failed: %08x\n", hres);
1012
1013     CHECK_CALLED(QI_IInternetProtocolInfo);
1014     CHECK_CALLED(ParseUrl);
1015
1016     hres = IInternetSession_UnregisterNameSpace(session, &test_protocol_cf, wszTest);
1017     ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
1018
1019     expect_cf = &test_protocol_cf2;
1020     SET_EXPECT(QI_IInternetProtocolInfo);
1021     SET_EXPECT(ParseUrl);
1022
1023     hres = CoInternetParseUrl(url8, PARSE_ENCODE, 0, buf, sizeof(buf)/sizeof(WCHAR),
1024                               &size, 0);
1025     ok(hres == S_OK, "CoInternetParseUrl failed: %08x\n", hres);
1026
1027     CHECK_CALLED(QI_IInternetProtocolInfo);
1028     CHECK_CALLED(ParseUrl);
1029
1030     hres = IInternetSession_UnregisterNameSpace(session, &test_protocol_cf, wszTest);
1031     ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
1032     hres = IInternetSession_UnregisterNameSpace(session, &test_protocol_cf, wszTest);
1033     ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
1034     hres = IInternetSession_UnregisterNameSpace(session, &test_protocol_cf, NULL);
1035     ok(hres == E_INVALIDARG, "UnregisterNameSpace failed: %08x\n", hres);
1036     hres = IInternetSession_UnregisterNameSpace(session, NULL, wszTest);
1037     ok(hres == E_INVALIDARG, "UnregisterNameSpace failed: %08x\n", hres);
1038
1039     hres = IInternetSession_UnregisterNameSpace(session, &test_protocol_cf2, wszTest);
1040     ok(hres == S_OK, "UnregisterNameSpace failed: %08x\n", hres);
1041
1042     hres = CoInternetParseUrl(url8, PARSE_ENCODE, 0, buf, sizeof(buf)/sizeof(WCHAR),
1043                               &size, 0);
1044     ok(hres == S_OK, "CoInternetParseUrl failed: %08x\n", hres);
1045
1046     IInternetSession_Release(session);
1047 }
1048
1049 static ULONG WINAPI unk_Release(IUnknown *iface)
1050 {
1051     CHECK_EXPECT(unk_Release);
1052     return 0;
1053 }
1054
1055 static const IUnknownVtbl unk_vtbl = {
1056     (void*)0xdeadbeef,
1057     (void*)0xdeadbeef,
1058     unk_Release
1059 };
1060
1061 static void test_ReleaseBindInfo(void)
1062 {
1063     BINDINFO bi;
1064     IUnknown unk = { &unk_vtbl };
1065
1066     ReleaseBindInfo(NULL); /* shouldn't crash */
1067
1068     memset(&bi, 0, sizeof(bi));
1069     bi.cbSize = sizeof(BINDINFO);
1070     bi.pUnk = &unk;
1071     SET_EXPECT(unk_Release);
1072     ReleaseBindInfo(&bi);
1073     ok(bi.cbSize == sizeof(BINDINFO), "bi.cbSize=%d\n", bi.cbSize);
1074     ok(bi.pUnk == NULL, "bi.pUnk=%p, expected NULL\n", bi.pUnk);
1075     CHECK_CALLED(unk_Release);
1076
1077     memset(&bi, 0, sizeof(bi));
1078     bi.cbSize = offsetof(BINDINFO, pUnk);
1079     bi.pUnk = &unk;
1080     ReleaseBindInfo(&bi);
1081     ok(bi.cbSize == offsetof(BINDINFO, pUnk), "bi.cbSize=%d\n", bi.cbSize);
1082     ok(bi.pUnk == &unk, "bi.pUnk=%p, expected %p\n", bi.pUnk, &unk);
1083
1084     memset(&bi, 0, sizeof(bi));
1085     bi.pUnk = &unk;
1086     ReleaseBindInfo(&bi);
1087     ok(!bi.cbSize, "bi.cbSize=%d, expected 0\n", bi.cbSize);
1088     ok(bi.pUnk == &unk, "bi.pUnk=%p, expected %p\n", bi.pUnk, &unk);
1089 }
1090
1091 static void test_UrlMkGetSessionOption(void)
1092 {
1093     DWORD encoding, size;
1094     HRESULT hres;
1095
1096     size = encoding = 0xdeadbeef;
1097     hres = UrlMkGetSessionOption(URLMON_OPTION_URL_ENCODING, &encoding,
1098                                  sizeof(encoding), &size, 0);
1099     ok(hres == S_OK, "UrlMkGetSessionOption failed: %08x\n", hres);
1100     ok(encoding != 0xdeadbeef, "encoding not changed\n");
1101     ok(size == sizeof(encoding), "size=%d\n", size);
1102
1103     size = encoding = 0xdeadbeef;
1104     hres = UrlMkGetSessionOption(URLMON_OPTION_URL_ENCODING, &encoding,
1105                                  sizeof(encoding)+1, &size, 0);
1106     ok(hres == S_OK, "UrlMkGetSessionOption failed: %08x\n", hres);
1107     ok(encoding != 0xdeadbeef, "encoding not changed\n");
1108     ok(size == sizeof(encoding), "size=%d\n", size);
1109
1110     size = encoding = 0xdeadbeef;
1111     hres = UrlMkGetSessionOption(URLMON_OPTION_URL_ENCODING, &encoding,
1112                                  sizeof(encoding)-1, &size, 0);
1113     ok(hres == E_INVALIDARG, "UrlMkGetSessionOption failed: %08x\n", hres);
1114     ok(encoding == 0xdeadbeef, "encoding = %08x, exepcted 0xdeadbeef\n", encoding);
1115     ok(size == 0xdeadbeef, "size=%d\n", size);
1116
1117     size = encoding = 0xdeadbeef;
1118     hres = UrlMkGetSessionOption(URLMON_OPTION_URL_ENCODING, NULL,
1119                                  sizeof(encoding)-1, &size, 0);
1120     ok(hres == E_INVALIDARG, "UrlMkGetSessionOption failed: %08x\n", hres);
1121     ok(encoding == 0xdeadbeef, "encoding = %08x, exepcted 0xdeadbeef\n", encoding);
1122     ok(size == 0xdeadbeef, "size=%d\n", size);
1123
1124     encoding = 0xdeadbeef;
1125     hres = UrlMkGetSessionOption(URLMON_OPTION_URL_ENCODING, &encoding,
1126                                  sizeof(encoding)-1, NULL, 0);
1127     ok(hres == E_INVALIDARG, "UrlMkGetSessionOption failed: %08x\n", hres);
1128     ok(encoding == 0xdeadbeef, "encoding = %08x, exepcted 0xdeadbeef\n", encoding);
1129 }
1130
1131 static void test_ObtainUserAgentString(void)
1132 {
1133     static const CHAR expected[] = "Mozilla/4.0 (compatible; MSIE ";
1134     static CHAR str[3];
1135     LPSTR str2 = NULL;
1136     HRESULT hres;
1137     DWORD size, saved;
1138
1139     hres = ObtainUserAgentString(0, NULL, NULL);
1140     ok(hres == E_INVALIDARG, "ObtainUserAgentString failed: %08x\n", hres);
1141
1142     size = 100;
1143     hres = ObtainUserAgentString(0, NULL, &size);
1144     ok(hres == E_INVALIDARG, "ObtainUserAgentString failed: %08x\n", hres);
1145     ok(size == 100, "size=%d, expected %d\n", size, 100);
1146
1147     size = 0;
1148     hres = ObtainUserAgentString(0, str, &size);
1149     ok(hres == E_OUTOFMEMORY, "ObtainUserAgentString failed: %08x\n", hres);
1150     ok(size > 0, "size=%d, expected non-zero\n", size);
1151
1152     size = 2;
1153     str[0] = 'a';
1154     hres = ObtainUserAgentString(0, str, &size);
1155     ok(hres == E_OUTOFMEMORY, "ObtainUserAgentString failed: %08x\n", hres);
1156     ok(size > 0, "size=%d, expected non-zero\n", size);
1157     ok(str[0] == 'a', "str[0]=%c, expected 'a'\n", str[0]);
1158
1159     size = 0;
1160     hres = ObtainUserAgentString(1, str, &size);
1161     ok(hres == E_OUTOFMEMORY, "ObtainUserAgentString failed: %08x\n", hres);
1162     ok(size > 0, "size=%d, expected non-zero\n", size);
1163
1164     str2 = HeapAlloc(GetProcessHeap(), 0, (size+20)*sizeof(CHAR));
1165     if (!str2)
1166     {
1167         skip("skipping rest of ObtainUserAgent tests, out of memory\n");
1168     }
1169     else
1170     {
1171         saved = size;
1172         hres = ObtainUserAgentString(0, str2, &size);
1173         ok(hres == S_OK, "ObtainUserAgentString failed: %08x\n", hres);
1174         ok(size == saved, "size=%d, expected %d\n", size, saved);
1175         ok(strlen(expected) <= strlen(str2) &&
1176            !memcmp(expected, str2, strlen(expected)*sizeof(CHAR)),
1177            "user agent was \"%s\", expected to start with \"%s\"\n",
1178            str2, expected);
1179
1180         size = saved+10;
1181         hres = ObtainUserAgentString(0, str2, &size);
1182         ok(hres == S_OK, "ObtainUserAgentString failed: %08x\n", hres);
1183         ok(size == saved, "size=%d, expected %d\n", size, saved);
1184     }
1185     HeapFree(GetProcessHeap(), 0, str2);
1186 }
1187
1188 START_TEST(misc)
1189 {
1190     OleInitialize(NULL);
1191
1192     register_protocols();
1193
1194     test_CreateFormatEnum();
1195     test_RegisterFormatEnumerator();
1196     test_CoInternetParseUrl();
1197     test_CoInternetCompareUrl();
1198     test_FindMimeFromData();
1199     test_SecurityManager();
1200     test_ZoneManager();
1201     test_NameSpace();
1202     test_ReleaseBindInfo();
1203     test_UrlMkGetSessionOption();
1204     test_ObtainUserAgentString();
1205
1206     OleUninitialize();
1207 }