1 /*
2  * Copyright 2009 Andrew Eikum 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 
24 #include "mshtml.h"
25 #include "wininet.h"
26 
27 struct location_test {
28     const char *name;
29     const char *url;
30 
31     const char *href;
32     const char *protocol;
33     const char *host;
34     const char *hostname;
35     const char *port;
36     const char *pathname;
37     const char *search;
38     const char *hash;
39 };
40 
41 static const struct location_test location_tests[] = {
42     {
43         "HTTP",
44         "http://www.winehq.org?search#hash",
45         "http://www.winehq.org/?search#hash",
46         "http:",
47         "www.winehq.org:80",
48         "www.winehq.org",
49         "80",
50         "",
51         "?search",
52         "#hash"
53     },
54     {
55         "HTTP with file",
56         "http://www.winehq.org/file?search#hash",
57         "http://www.winehq.org/file?search#hash",
58         "http:",
59         "www.winehq.org:80",
60         "www.winehq.org",
61         "80",
62         "file",
63         "?search",
64         "#hash"
65     },
66     {
67         "FTP",
68         "ftp://ftp.winehq.org/",
69         "ftp://ftp.winehq.org/",
70         "ftp:",
71         "ftp.winehq.org:21",
72         "ftp.winehq.org",
73         "21",
74         "",
75         NULL,
76         NULL
77     },
78     {
79         "FTP with file",
80         "ftp://ftp.winehq.org/file",
81         "ftp://ftp.winehq.org/file",
82         "ftp:",
83         "ftp.winehq.org:21",
84         "ftp.winehq.org",
85         "21",
86         "file",
87         NULL,
88         NULL
89     },
90     {
91         "FILE",
92         "file://C:\\windows\\win.ini",
93         "file:///C:/windows/win.ini",
94         "file:",
95         NULL,
96         NULL,
97         "",
98         "C:\\windows\\win.ini",
99         NULL,
100         NULL
101     }
102 };
103 
104 static int str_eq_wa(LPCWSTR strw, const char *stra)
105 {
106     CHAR buf[512];
107 
108     if(!strw || !stra)
109         return (void*)strw == (void*)stra;
110 
111     WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
112     return !lstrcmpA(stra, buf);
113 }
114 
115 static void test_href(IHTMLLocation *loc, const struct location_test *test)
116 {
117     HRESULT hres;
118     BSTR str;
119 
120     hres = IHTMLLocation_get_href(loc, NULL);
121     ok(hres == E_POINTER,
122             "%s: get_href should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
123             test->name, E_POINTER, hres);
124 
125     hres = IHTMLLocation_get_href(loc, &str);
126     ok(hres == S_OK, "%s: get_href failed: 0x%08x\n", test->name, hres);
127     if(hres == S_OK)
128         ok(str_eq_wa(str, test->href),
129                 "%s: expected retrieved href to be L\"%s\", was: %s\n",
130                 test->name, test->href, wine_dbgstr_w(str));
131     SysFreeString(str);
132 
133     hres = IHTMLLocation_toString(loc, &str);
134     ok(hres == S_OK, "%s: toString failed: 0x%08x\n", test->name, hres);
135     ok(str_eq_wa(str, test->href), "%s: toString returned %s, expected %s\n",
136        test->name, wine_dbgstr_w(str), test->href);
137     SysFreeString(str);
138 }
139 
140 static void test_protocol(IHTMLLocation *loc, const struct location_test *test)
141 {
142     HRESULT hres;
143     BSTR str;
144 
145     hres = IHTMLLocation_get_protocol(loc, NULL);
146     ok(hres == E_POINTER,
147             "%s: get_protocol should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
148             test->name, E_POINTER, hres);
149 
150     hres = IHTMLLocation_get_protocol(loc, &str);
151     ok(hres == S_OK, "%s: get_protocol failed: 0x%08x\n", test->name, hres);
152     if(hres == S_OK)
153         ok(str_eq_wa(str, test->protocol),
154                 "%s: expected retrieved protocol to be L\"%s\", was: %s\n",
155                 test->name, test->protocol, wine_dbgstr_w(str));
156     SysFreeString(str);
157 }
158 
159 static void test_host(IHTMLLocation *loc, const struct location_test *test)
160 {
161     HRESULT hres;
162     BSTR str;
163 
164     hres = IHTMLLocation_get_host(loc, NULL);
165     ok(hres == E_POINTER,
166             "%s: get_host should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
167             test->name, E_POINTER, hres);
168 
169     hres = IHTMLLocation_get_host(loc, &str);
170     ok(hres == S_OK, "%s: get_host failed: 0x%08x\n", test->name, hres);
171     if(hres == S_OK)
172         ok(str_eq_wa(str, test->host),
173                 "%s: expected retrieved host to be L\"%s\", was: %s\n",
174                 test->name, test->host, wine_dbgstr_w(str));
175     SysFreeString(str);
176 }
177 
178 static void test_hostname(IHTMLLocation *loc, IHTMLDocument2 *doc, const struct location_test *test)
179 {
180     HRESULT hres;
181     BSTR str;
182 
183     hres = IHTMLLocation_get_hostname(loc, NULL);
184     ok(hres == E_POINTER,
185             "%s: get_hostname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
186             test->name, E_POINTER, hres);
187 
188     hres = IHTMLLocation_get_hostname(loc, &str);
189     ok(hres == S_OK, "%s: get_hostname failed: 0x%08x\n", test->name, hres);
190     if(hres == S_OK)
191         ok(str_eq_wa(str, test->hostname),
192                 "%s: expected retrieved hostname to be L\"%s\", was: %s\n",
193                 test->name, test->hostname, wine_dbgstr_w(str));
194     SysFreeString(str);
195 
196     hres = IHTMLDocument2_get_domain(doc, &str);
197     ok(hres == S_OK, "%s: get_domain failed: 0x%08x\n", test->name, hres);
198     if(hres == S_OK)
199         ok(str_eq_wa(str, test->hostname ? test->hostname : ""),
200                 "%s: expected retrieved domain to be L\"%s\", was: %s\n",
201                 test->name, test->hostname, wine_dbgstr_w(str));
202     SysFreeString(str);
203 }
204 
205 static void test_port(IHTMLLocation *loc, const struct location_test *test)
206 {
207     HRESULT hres;
208     BSTR str;
209 
210     hres = IHTMLLocation_get_port(loc, NULL);
211     ok(hres == E_POINTER,
212             "%s: get_port should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
213             test->name, E_POINTER, hres);
214 
215     hres = IHTMLLocation_get_port(loc, &str);
216     ok(hres == S_OK, "%s: get_port failed: 0x%08x\n", test->name, hres);
217     if(hres == S_OK)
218         ok(str_eq_wa(str, test->port),
219                 "%s: expected retrieved port to be L\"%s\", was: %s\n",
220                 test->name, test->port, wine_dbgstr_w(str));
221     SysFreeString(str);
222 }
223 
224 static void test_pathname(IHTMLLocation *loc, const struct location_test *test)
225 {
226     HRESULT hres;
227     BSTR str;
228 
229     hres = IHTMLLocation_get_pathname(loc, NULL);
230     ok(hres == E_POINTER,
231             "%s: get_pathname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
232             test->name, E_POINTER, hres);
233 
234     hres = IHTMLLocation_get_pathname(loc, &str);
235     ok(hres == S_OK, "%s: get_pathname failed: 0x%08x\n", test->name, hres);
236     if(hres == S_OK)
237         ok(str_eq_wa(str, test->pathname),
238                 "%s: expected retrieved pathname to be L\"%s\", was: %s\n",
239                 test->name, test->pathname, wine_dbgstr_w(str));
240     SysFreeString(str);
241 }
242 
243 static void test_search(IHTMLLocation *loc, const struct location_test *test)
244 {
245     HRESULT hres;
246     BSTR str;
247 
248     hres = IHTMLLocation_get_search(loc, NULL);
249     ok(hres == E_POINTER,
250             "%s: get_search should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
251             test->name, E_POINTER, hres);
252 
253     hres = IHTMLLocation_get_search(loc, &str);
254     ok(hres == S_OK, "%s: get_search failed: 0x%08x\n", test->name, hres);
255     if(hres == S_OK)
256         ok(str_eq_wa(str, test->search),
257                 "%s: expected retrieved search to be L\"%s\", was: %s\n",
258                 test->name, test->search, wine_dbgstr_w(str));
259     SysFreeString(str);
260 }
261 
262 static void test_hash(IHTMLLocation *loc, const struct location_test *test)
263 {
264     HRESULT hres;
265     BSTR str;
266 
267     hres = IHTMLLocation_get_hash(loc, NULL);
268     ok(hres == E_POINTER,
269             "%s: get_hash should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
270             test->name, E_POINTER, hres);
271 
272     hres = IHTMLLocation_get_hash(loc, &str);
273     ok(hres == S_OK, "%s: get_hash failed: 0x%08x\n", test->name, hres);
274     if(hres == S_OK)
275         ok(str_eq_wa(str, test->hash),
276                 "%s: expected retrieved hash to be L\"%s\", was: %s\n",
277                 test->name, test->hash, wine_dbgstr_w(str));
278     SysFreeString(str);
279 }
280 
281 static void perform_test(const struct location_test* test)
282 {
283     WCHAR url[INTERNET_MAX_URL_LENGTH];
284     HRESULT hres;
285     IBindCtx *bc;
286     IMoniker *url_mon;
287     IPersistMoniker *persist_mon;
288     IHTMLDocument2 *doc;
289     IHTMLDocument6 *doc6;
290     IHTMLLocation *location;
291 
292     hres = CreateBindCtx(0, &bc);
293     ok(hres == S_OK, "%s: CreateBindCtx failed: 0x%08x\n", test->name, hres);
294     if(FAILED(hres))
295         return;
296 
297     MultiByteToWideChar(CP_ACP, 0, test->url, -1, url, sizeof(url)/sizeof(WCHAR));
298     hres = CreateURLMoniker(NULL, url, &url_mon);
299     ok(hres == S_OK, "%s: CreateURLMoniker failed: 0x%08x\n", test->name, hres);
300     if(FAILED(hres)){
301         IBindCtx_Release(bc);
302         return;
303     }
304 
305     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL,
306             CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER, &IID_IHTMLDocument2,
307             (void**)&doc);
308     ok(hres == S_OK, "%s: CoCreateInstance failed: 0x%08x\n", test->name, hres);
309     if(FAILED(hres)){
310         IMoniker_Release(url_mon);
311         IBindCtx_Release(bc);
312         return;
313     }
314 
315     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument6, (void**)&doc6);
316     if(hres == S_OK){
317         IHTMLDocument6_Release(doc6);
318     }else{
319         win_skip("%s: Could not get IHTMLDocument6, probably too old IE. Requires IE 8+\n", test->name);
320         IMoniker_Release(url_mon);
321         IBindCtx_Release(bc);
322         return;
323     }
324 
325     hres = IHTMLDocument2_QueryInterface(doc, &IID_IPersistMoniker,
326             (void**)&persist_mon);
327     ok(hres == S_OK, "%s: IHTMlDocument2_QueryInterface failed: 0x%08x\n", test->name, hres);
328     if(FAILED(hres)){
329         IHTMLDocument2_Release(doc);
330         IMoniker_Release(url_mon);
331         IBindCtx_Release(bc);
332         return;
333     }
334 
335     hres = IPersistMoniker_Load(persist_mon, FALSE, url_mon, bc,
336             STGM_SHARE_EXCLUSIVE | STGM_READWRITE);
337     ok(hres == S_OK, "%s: IPersistMoniker_Load failed: 0x%08x\n", test->name, hres);
338     if(FAILED(hres)){
339         IPersistMoniker_Release(persist_mon);
340         IHTMLDocument2_Release(doc);
341         IMoniker_Release(url_mon);
342         IBindCtx_Release(bc);
343         return;
344     }
345 
346     hres = IHTMLDocument2_get_location(doc, &location);
347     ok(hres == S_OK, "%s: IHTMLDocument2_get_location failed: 0x%08x\n", test->name, hres);
348     if(FAILED(hres)){
349         IPersistMoniker_Release(persist_mon);
350         IHTMLDocument2_Release(doc);
351         IMoniker_Release(url_mon);
352         IBindCtx_Release(bc);
353         return;
354     }
355 
356     test_href(location, test);
357     test_protocol(location, test);
358     test_host(location, test);
359     test_hostname(location, doc, test);
360     test_port(location, test);
361     test_pathname(location, test);
362     test_search(location, test);
363     test_hash(location, test);
364 
365     IHTMLLocation_Release(location);
366     IPersistMoniker_Release(persist_mon);
367     IHTMLDocument2_Release(doc);
368     IMoniker_Release(url_mon);
369     IBindCtx_Release(bc);
370 }
371 
372 START_TEST(htmllocation)
373 {
374     int i;
375 
376     CoInitialize(NULL);
377 
378     for(i=0; i < sizeof(location_tests)/sizeof(*location_tests); i++)
379         perform_test(location_tests+i);
380 
381     CoUninitialize();
382 }
383