user32/tests: Restructure the DDE end-to-end tests to make it easier to add new cases.
[wine] / dlls / shlwapi / tests / url.c
1 /* Unit test suite for Path functions
2  *
3  * Copyright 2002 Matthew Mastracci
4  * Copyright 2007-2010 Detlef Riekenberg
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "wine/test.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "shlwapi.h"
29 #include "wininet.h"
30 #include "intshcut.h"
31
32 /* ################ */
33 static HMODULE hShlwapi;
34 static HRESULT (WINAPI *pUrlUnescapeA)(LPSTR,LPSTR,LPDWORD,DWORD);
35 static HRESULT (WINAPI *pUrlUnescapeW)(LPWSTR,LPWSTR,LPDWORD,DWORD);
36 static BOOL    (WINAPI *pUrlIsA)(LPCSTR,URLIS);
37 static BOOL    (WINAPI *pUrlIsW)(LPCWSTR,URLIS);
38 static HRESULT (WINAPI *pUrlHashA)(LPCSTR,LPBYTE,DWORD);
39 static HRESULT (WINAPI *pUrlHashW)(LPCWSTR,LPBYTE,DWORD);
40 static HRESULT (WINAPI *pUrlGetPartA)(LPCSTR,LPSTR,LPDWORD,DWORD,DWORD);
41 static HRESULT (WINAPI *pUrlGetPartW)(LPCWSTR,LPWSTR,LPDWORD,DWORD,DWORD);
42 static HRESULT (WINAPI *pUrlEscapeA)(LPCSTR,LPSTR,LPDWORD,DWORD);
43 static HRESULT (WINAPI *pUrlEscapeW)(LPCWSTR,LPWSTR,LPDWORD,DWORD);
44 static HRESULT (WINAPI *pUrlCreateFromPathA)(LPCSTR,LPSTR,LPDWORD,DWORD);
45 static HRESULT (WINAPI *pUrlCreateFromPathW)(LPCWSTR,LPWSTR,LPDWORD,DWORD);
46 static HRESULT (WINAPI *pUrlCombineA)(LPCSTR,LPCSTR,LPSTR,LPDWORD,DWORD);
47 static HRESULT (WINAPI *pUrlCombineW)(LPCWSTR,LPCWSTR,LPWSTR,LPDWORD,DWORD);
48 static HRESULT (WINAPI *pUrlCanonicalizeA)(LPCSTR, LPSTR, LPDWORD, DWORD);
49 static HRESULT (WINAPI *pUrlCanonicalizeW)(LPCWSTR, LPWSTR, LPDWORD, DWORD);
50 static HRESULT (WINAPI *pUrlApplySchemeA)(LPCSTR,LPSTR,LPDWORD,DWORD);
51 static HRESULT (WINAPI *pUrlApplySchemeW)(LPCWSTR,LPWSTR,LPDWORD,DWORD);
52 static HRESULT (WINAPI *pParseURLA)(LPCSTR,PARSEDURLA*);
53 static HRESULT (WINAPI *pParseURLW)(LPCWSTR,PARSEDURLW*);
54 static HRESULT (WINAPI *pHashData)(LPBYTE, DWORD, LPBYTE, DWORD);
55
56 static const char* TEST_URL_1 = "http://www.winehq.org/tests?date=10/10/1923";
57 static const char* TEST_URL_2 = "http://localhost:8080/tests%2e.html?date=Mon%2010/10/1923";
58 static const char* TEST_URL_3 = "http://foo:bar@localhost:21/internal.php?query=x&return=y";
59 static const WCHAR winehqW[] = {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.','o','r','g','/',0};
60 static const  CHAR winehqA[] = {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.','o','r','g','/',0};
61
62 /* ################ */
63
64 static const CHAR untouchedA[] = "untouched";
65
66 #define TEST_APPLY_MAX_LENGTH INTERNET_MAX_URL_LENGTH
67
68 typedef struct _TEST_URL_APPLY {
69     const char * url;
70     DWORD flags;
71     HRESULT res;
72     DWORD newlen;
73     const char * newurl;
74 } TEST_URL_APPLY;
75
76 static const TEST_URL_APPLY TEST_APPLY[] = {
77     {"www.winehq.org", URL_APPLY_GUESSSCHEME | URL_APPLY_DEFAULT, S_OK, 21, "http://www.winehq.org"},
78     {"www.winehq.org", URL_APPLY_GUESSSCHEME, S_OK, 21, "http://www.winehq.org"},
79     {"www.winehq.org", URL_APPLY_DEFAULT, S_OK, 21, "http://www.winehq.org"},
80     {"ftp.winehq.org", URL_APPLY_GUESSSCHEME | URL_APPLY_DEFAULT, S_OK, 20, "ftp://ftp.winehq.org"},
81     {"ftp.winehq.org", URL_APPLY_GUESSSCHEME, S_OK, 20, "ftp://ftp.winehq.org"},
82     {"ftp.winehq.org", URL_APPLY_DEFAULT, S_OK, 21, "http://ftp.winehq.org"},
83     {"winehq.org", URL_APPLY_GUESSSCHEME | URL_APPLY_DEFAULT, S_OK, 17, "http://winehq.org"},
84     {"winehq.org", URL_APPLY_GUESSSCHEME, S_FALSE, TEST_APPLY_MAX_LENGTH, untouchedA},
85     {"winehq.org", URL_APPLY_DEFAULT, S_OK, 17, "http://winehq.org"},
86     {"", URL_APPLY_GUESSSCHEME | URL_APPLY_DEFAULT, S_OK, 7, "http://"},
87     {"", URL_APPLY_GUESSSCHEME, S_FALSE, TEST_APPLY_MAX_LENGTH, untouchedA},
88     {"", URL_APPLY_DEFAULT, S_OK, 7, "http://"}
89 };
90
91 /* ################ */
92
93 typedef struct _TEST_URL_CANONICALIZE {
94     const char *url;
95     DWORD flags;
96     HRESULT expectret;
97     const char *expecturl;
98     BOOL todo;
99 } TEST_URL_CANONICALIZE;
100
101 static const TEST_URL_CANONICALIZE TEST_CANONICALIZE[] = {
102     {"http://www.winehq.org/tests/../tests/../..", 0, S_OK, "http://www.winehq.org/", TRUE},
103     {"http://www.winehq.org/tests/../tests", 0, S_OK, "http://www.winehq.org/tests", FALSE},
104     {"http://www.winehq.org/tests\n", URL_WININET_COMPATIBILITY|URL_ESCAPE_SPACES_ONLY|URL_ESCAPE_UNSAFE, S_OK, "http://www.winehq.org/tests", FALSE},
105     {"http://www.winehq.org/tests\r", URL_WININET_COMPATIBILITY|URL_ESCAPE_SPACES_ONLY|URL_ESCAPE_UNSAFE, S_OK, "http://www.winehq.org/tests", FALSE},
106     {"http://www.winehq.org/tests\r", 0, S_OK, "http://www.winehq.org/tests", FALSE},
107     {"http://www.winehq.org/tests\r", URL_DONT_SIMPLIFY, S_OK, "http://www.winehq.org/tests", FALSE},
108     {"http://www.winehq.org/tests/../tests/", 0, S_OK, "http://www.winehq.org/tests/", FALSE},
109     {"http://www.winehq.org/tests/../tests/..", 0, S_OK, "http://www.winehq.org/", FALSE},
110     {"http://www.winehq.org/tests/../tests/../", 0, S_OK, "http://www.winehq.org/", FALSE},
111     {"http://www.winehq.org/tests/..", 0, S_OK, "http://www.winehq.org/", FALSE},
112     {"http://www.winehq.org/tests/../", 0, S_OK, "http://www.winehq.org/", FALSE},
113     {"http://www.winehq.org/tests/..?query=x&return=y", 0, S_OK, "http://www.winehq.org/?query=x&return=y", FALSE},
114     {"http://www.winehq.org/tests/../?query=x&return=y", 0, S_OK, "http://www.winehq.org/?query=x&return=y", FALSE},
115     {"http://www.winehq.org/tests/..#example", 0, S_OK, "http://www.winehq.org/#example", FALSE},
116     {"http://www.winehq.org/tests/../#example", 0, S_OK, "http://www.winehq.org/#example", FALSE},
117     {"http://www.winehq.org/tests\\../#example", 0, S_OK, "http://www.winehq.org/#example", FALSE},
118     {"http://www.winehq.org/tests/..\\#example", 0, S_OK, "http://www.winehq.org/#example", FALSE},
119     {"http://www.winehq.org\\tests/../#example", 0, S_OK, "http://www.winehq.org/#example", FALSE},
120     {"http://www.winehq.org/tests/../#example", URL_DONT_SIMPLIFY, S_OK, "http://www.winehq.org/tests/../#example", FALSE},
121     {"http://www.winehq.org/tests/foo bar", URL_ESCAPE_SPACES_ONLY| URL_DONT_ESCAPE_EXTRA_INFO , S_OK, "http://www.winehq.org/tests/foo%20bar", FALSE},
122     {"http://www.winehq.org/tests/foo%20bar", URL_UNESCAPE , S_OK, "http://www.winehq.org/tests/foo bar", FALSE},
123     {"file:///c:/tests/foo%20bar", URL_UNESCAPE , S_OK, "file:///c:/tests/foo bar", FALSE},
124     {"file:///c:/tests\\foo%20bar", URL_UNESCAPE , S_OK, "file:///c:/tests/foo bar", FALSE},
125     {"file:///c:/tests/foo%20bar", 0, S_OK, "file:///c:/tests/foo%20bar", FALSE},
126     {"file:///c:/tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "file://c:\\tests\\foo bar", FALSE},
127     {"file://localhost/c:/tests/../tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "file://c:\\tests\\foo bar", FALSE},
128     {"file://localhost\\c:/tests/../tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "file://c:\\tests\\foo bar", FALSE},
129     {"file://localhost\\\\c:/tests/../tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "file://c:\\tests\\foo bar", FALSE},
130     {"file://localhost\\c:\\tests/../tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "file://c:\\tests\\foo bar", FALSE},
131     {"file://c:/tests/../tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "file://c:\\tests\\foo bar", FALSE},
132     {"file://c:/tests\\../tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "file://c:\\tests\\foo bar", FALSE},
133     {"file://c:/tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "file://c:\\tests\\foo bar", FALSE},
134     {"file:///c://tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "file://c:\\\\tests\\foo bar", FALSE},
135     {"file:///c:\\tests\\foo bar", 0, S_OK, "file:///c:/tests/foo bar", FALSE},
136     {"file:///c:\\tests\\foo bar", URL_DONT_SIMPLIFY, S_OK, "file:///c:/tests/foo bar", FALSE},
137     {"http://www.winehq.org/site/about", URL_FILE_USE_PATHURL, S_OK, "http://www.winehq.org/site/about", FALSE},
138     {"file_://www.winehq.org/site/about", URL_FILE_USE_PATHURL, S_OK, "file_://www.winehq.org/site/about", FALSE},
139     {"c:\\dir\\file", 0, S_OK, "file:///c:/dir/file", FALSE},
140     {"file:///c:\\dir\\file", 0, S_OK, "file:///c:/dir/file", FALSE},
141     {"c:dir\\file", 0, S_OK, "file:///c:dir/file", FALSE},
142     {"c:\\tests\\foo bar", URL_FILE_USE_PATHURL, S_OK, "file://c:\\tests\\foo bar", FALSE},
143     {"c:\\tests\\foo bar", 0, S_OK, "file:///c:/tests/foo%20bar", FALSE},
144     {"res://file", 0, S_OK, "res://file/", FALSE},
145     {"res://file", URL_FILE_USE_PATHURL, S_OK, "res://file/", FALSE},
146     {"res:///c:/tests/foo%20bar", URL_UNESCAPE , S_OK, "res:///c:/tests/foo bar", FALSE},
147     {"res:///c:/tests\\foo%20bar", URL_UNESCAPE , S_OK, "res:///c:/tests\\foo bar", FALSE},
148     {"res:///c:/tests/foo%20bar", 0, S_OK, "res:///c:/tests/foo%20bar", FALSE},
149     {"res:///c:/tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "res:///c:/tests/foo%20bar", FALSE},
150     {"res://c:/tests/../tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "res://c:/tests/foo%20bar", FALSE},
151     {"res://c:/tests\\../tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "res://c:/tests/foo%20bar", FALSE},
152     {"res://c:/tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "res://c:/tests/foo%20bar", FALSE},
153     {"res:///c://tests/foo%20bar", URL_FILE_USE_PATHURL, S_OK, "res:///c://tests/foo%20bar", FALSE},
154     {"res:///c:\\tests\\foo bar", 0, S_OK, "res:///c:\\tests\\foo bar", FALSE},
155     {"res:///c:\\tests\\foo bar", URL_DONT_SIMPLIFY, S_OK, "res:///c:\\tests\\foo bar", FALSE},
156     {"res://c:\\tests\\foo bar/res", URL_FILE_USE_PATHURL, S_OK, "res://c:\\tests\\foo bar/res", FALSE},
157     {"res://c:\\tests/res\\foo%20bar/strange\\sth", 0, S_OK, "res://c:\\tests/res\\foo%20bar/strange\\sth", FALSE},
158     {"res://c:\\tests/res\\foo%20bar/strange\\sth", URL_FILE_USE_PATHURL, S_OK, "res://c:\\tests/res\\foo%20bar/strange\\sth", FALSE},
159     {"res://c:\\tests/res\\foo%20bar/strange\\sth", URL_UNESCAPE, S_OK, "res://c:\\tests/res\\foo bar/strange\\sth", FALSE},
160     {"A", 0, S_OK, "A", FALSE},
161     {"/uri-res/N2R?urn:sha1:B3K", URL_DONT_ESCAPE_EXTRA_INFO | URL_WININET_COMPATIBILITY /*0x82000000*/, S_OK, "/uri-res/N2R?urn:sha1:B3K", FALSE} /*LimeWire online installer calls this*/,
162     {"http:www.winehq.org/dir/../index.html", 0, S_OK, "http:www.winehq.org/index.html"},
163     {"http://localhost/test.html", URL_FILE_USE_PATHURL, S_OK, "http://localhost/test.html"},
164     {"http://localhost/te%20st.html", URL_FILE_USE_PATHURL, S_OK, "http://localhost/te%20st.html"},
165     {"http://www.winehq.org/%E6%A1%9C.html", URL_FILE_USE_PATHURL, S_OK, "http://www.winehq.org/%E6%A1%9C.html"}
166 };
167
168 /* ################ */
169
170 typedef struct _TEST_URL_ESCAPE {
171     const char *url;
172     DWORD flags;
173     DWORD expectescaped;
174     HRESULT expectret;
175     const char *expecturl;
176 } TEST_URL_ESCAPE;
177
178 static const TEST_URL_ESCAPE TEST_ESCAPE[] = {
179     {"http://www.winehq.org/tests0", 0, 0, S_OK, "http://www.winehq.org/tests0"},
180     {"http://www.winehq.org/tests1\n", 0, 0, S_OK, "http://www.winehq.org/tests1%0A"},
181     {"http://www.winehq.org/tests2\r", 0, 0, S_OK, "http://www.winehq.org/tests2%0D"},
182     {"http://www.winehq.org/tests3\r", URL_ESCAPE_SPACES_ONLY|URL_ESCAPE_UNSAFE, 0, S_OK, "http://www.winehq.org/tests3\r"},
183     {"http://www.winehq.org/tests4\r", URL_ESCAPE_SPACES_ONLY, 0, S_OK, "http://www.winehq.org/tests4\r"},
184     {"http://www.winehq.org/tests5\r", URL_WININET_COMPATIBILITY|URL_ESCAPE_SPACES_ONLY, 0, S_OK, "http://www.winehq.org/tests5\r"},
185     {"/direct/swhelp/series6/6.2i_latestservicepack.dat\r", URL_ESCAPE_SPACES_ONLY, 0, S_OK, "/direct/swhelp/series6/6.2i_latestservicepack.dat\r"},
186
187     {"file://////foo/bar\\baz", 0, 0, S_OK, "file://foo/bar/baz"},
188     {"file://///foo/bar\\baz", 0, 0, S_OK, "file://foo/bar/baz"},
189     {"file:////foo/bar\\baz", 0, 0, S_OK, "file://foo/bar/baz"},
190     {"file:///localhost/foo/bar\\baz", 0, 0, S_OK, "file:///localhost/foo/bar/baz"},
191     {"file:///foo/bar\\baz", 0, 0, S_OK, "file:///foo/bar/baz"},
192     {"file://loCalHost/foo/bar\\baz", 0, 0, S_OK, "file:///foo/bar/baz"},
193     {"file://foo/bar\\baz", 0, 0, S_OK, "file://foo/bar/baz"},
194     {"file:/localhost/foo/bar\\baz", 0, 0, S_OK, "file:///localhost/foo/bar/baz"},
195     {"file:/foo/bar\\baz", 0, 0, S_OK, "file:///foo/bar/baz"},
196     {"file:foo/bar\\baz", 0, 0, S_OK, "file:foo/bar/baz"},
197     {"file:\\foo/bar\\baz", 0, 0, S_OK, "file:///foo/bar/baz"},
198     {"file:\\\\foo/bar\\baz", 0, 0, S_OK, "file://foo/bar/baz"},
199     {"file:\\\\\\foo/bar\\baz", 0, 0, S_OK, "file:///foo/bar/baz"},
200     {"file:\\\\localhost\\foo/bar\\baz", 0, 0, S_OK, "file:///foo/bar/baz"},
201     {"file:///f oo/b?a r\\baz", 0, 0, S_OK, "file:///f%20oo/b?a r\\baz"},
202     {"file:///foo/b#a r\\baz", 0, 0, S_OK, "file:///foo/b%23a%20r/baz"},
203     {"file:///f o^&`{}|][\"<>\\%o/b#a r\\baz", 0, 0, S_OK, "file:///f%20o%5E%26%60%7B%7D%7C%5D%5B%22%3C%3E/%o/b%23a%20r/baz"},
204     {"file:///f o%o/b?a r\\b%az", URL_ESCAPE_PERCENT, 0, S_OK, "file:///f%20o%25o/b?a r\\b%az"},
205     {"file:/foo/bar\\baz", URL_ESCAPE_SEGMENT_ONLY, 0, S_OK, "file:%2Ffoo%2Fbar%5Cbaz"},
206
207     {"foo/b%ar\\ba?z\\", URL_ESCAPE_SEGMENT_ONLY, 0, S_OK, "foo%2Fb%ar%5Cba%3Fz%5C"},
208     {"foo/b%ar\\ba?z\\", URL_ESCAPE_PERCENT | URL_ESCAPE_SEGMENT_ONLY, 0, S_OK, "foo%2Fb%25ar%5Cba%3Fz%5C"},
209     {"foo/bar\\ba?z\\", 0, 0, S_OK, "foo/bar%5Cba?z\\"},
210     {"/foo/bar\\ba?z\\", 0, 0, S_OK, "/foo/bar%5Cba?z\\"},
211     {"/foo/bar\\ba#z\\", 0, 0, S_OK, "/foo/bar%5Cba#z\\"},
212     {"/foo/%5C", 0, 0, S_OK, "/foo/%5C"},
213     {"/foo/%5C", URL_ESCAPE_PERCENT, 0, S_OK, "/foo/%255C"},
214
215     {"http://////foo/bar\\baz", 0, 0, S_OK, "http://////foo/bar/baz"},
216     {"http://///foo/bar\\baz", 0, 0, S_OK, "http://///foo/bar/baz"},
217     {"http:////foo/bar\\baz", 0, 0, S_OK, "http:////foo/bar/baz"},
218     {"http:///foo/bar\\baz", 0, 0, S_OK, "http:///foo/bar/baz"},
219     {"http://localhost/foo/bar\\baz", 0, 0, S_OK, "http://localhost/foo/bar/baz"},
220     {"http://foo/bar\\baz", 0, 0, S_OK, "http://foo/bar/baz"},
221     {"http:/foo/bar\\baz", 0, 0, S_OK, "http:/foo/bar/baz"},
222     {"http:foo/bar\\ba?z\\", 0, 0, S_OK, "http:foo%2Fbar%2Fba?z\\"},
223     {"http:foo/bar\\ba#z\\", 0, 0, S_OK, "http:foo%2Fbar%2Fba#z\\"},
224     {"http:\\foo/bar\\baz", 0, 0, S_OK, "http:/foo/bar/baz"},
225     {"http:\\\\foo/bar\\baz", 0, 0, S_OK, "http://foo/bar/baz"},
226     {"http:\\\\\\foo/bar\\baz", 0, 0, S_OK, "http:///foo/bar/baz"},
227     {"http:\\\\\\\\foo/bar\\baz", 0, 0, S_OK, "http:////foo/bar/baz"},
228     {"http:/fo ?o/b ar\\baz", 0, 0, S_OK, "http:/fo%20?o/b ar\\baz"},
229     {"http:fo ?o/b ar\\baz", 0, 0, S_OK, "http:fo%20?o/b ar\\baz"},
230     {"http:/foo/bar\\baz", URL_ESCAPE_SEGMENT_ONLY, 0, S_OK, "http:%2Ffoo%2Fbar%5Cbaz"},
231
232     {"https://foo/bar\\baz", 0, 0, S_OK, "https://foo/bar/baz"},
233     {"https:/foo/bar\\baz", 0, 0, S_OK, "https:/foo/bar/baz"},
234     {"https:\\foo/bar\\baz", 0, 0, S_OK, "https:/foo/bar/baz"},
235
236     {"foo:////foo/bar\\baz", 0, 0, S_OK, "foo:////foo/bar%5Cbaz"},
237     {"foo:///foo/bar\\baz", 0, 0, S_OK, "foo:///foo/bar%5Cbaz"},
238     {"foo://localhost/foo/bar\\baz", 0, 0, S_OK, "foo://localhost/foo/bar%5Cbaz"},
239     {"foo://foo/bar\\baz", 0, 0, S_OK, "foo://foo/bar%5Cbaz"},
240     {"foo:/foo/bar\\baz", 0, 0, S_OK, "foo:/foo/bar%5Cbaz"},
241     {"foo:foo/bar\\baz", 0, 0, S_OK, "foo:foo%2Fbar%5Cbaz"},
242     {"foo:\\foo/bar\\baz", 0, 0, S_OK, "foo:%5Cfoo%2Fbar%5Cbaz"},
243     {"foo:/foo/bar\\ba?\\z", 0, 0, S_OK, "foo:/foo/bar%5Cba?\\z"},
244     {"foo:/foo/bar\\ba#\\z", 0, 0, S_OK, "foo:/foo/bar%5Cba#\\z"},
245
246     {"mailto:/fo/o@b\\%a?\\r.b#\\az", 0, 0, S_OK, "mailto:%2Ffo%2Fo@b%5C%a%3F%5Cr.b%23%5Caz"},
247     {"mailto:fo/o@b\\%a?\\r.b#\\az", 0, 0, S_OK, "mailto:fo%2Fo@b%5C%a%3F%5Cr.b%23%5Caz"},
248     {"mailto:fo/o@b\\%a?\\r.b#\\az", URL_ESCAPE_PERCENT, 0, S_OK, "mailto:fo%2Fo@b%5C%25a%3F%5Cr.b%23%5Caz"},
249
250     {"ftp:fo/o@bar.baz/foo/bar", 0, 0, S_OK, "ftp:fo%2Fo@bar.baz%2Ffoo%2Fbar"},
251     {"ftp:/fo/o@bar.baz/foo/bar", 0, 0, S_OK, "ftp:/fo/o@bar.baz/foo/bar"},
252     {"ftp://fo/o@bar.baz/fo?o\\bar", 0, 0, S_OK, "ftp://fo/o@bar.baz/fo?o\\bar"},
253     {"ftp://fo/o@bar.baz/fo#o\\bar", 0, 0, S_OK, "ftp://fo/o@bar.baz/fo#o\\bar"},
254     {"ftp://localhost/o@bar.baz/fo#o\\bar", 0, 0, S_OK, "ftp://localhost/o@bar.baz/fo#o\\bar"},
255     {"ftp:///fo/o@bar.baz/foo/bar", 0, 0, S_OK, "ftp:///fo/o@bar.baz/foo/bar"},
256     {"ftp:////fo/o@bar.baz/foo/bar", 0, 0, S_OK, "ftp:////fo/o@bar.baz/foo/bar"}
257 };
258
259 /* ################ */
260
261 typedef struct _TEST_URL_COMBINE {
262     const char *url1;
263     const char *url2;
264     DWORD flags;
265     HRESULT expectret;
266     const char *expecturl;
267 } TEST_URL_COMBINE;
268
269 static const TEST_URL_COMBINE TEST_COMBINE[] = {
270     {"http://www.winehq.org/tests", "tests1", 0, S_OK, "http://www.winehq.org/tests1"},
271     {"http://www.%77inehq.org/tests", "tests1", 0, S_OK, "http://www.%77inehq.org/tests1"},
272     /*FIXME {"http://www.winehq.org/tests", "../tests2", 0, S_OK, "http://www.winehq.org/tests2"},*/
273     {"http://www.winehq.org/tests/", "../tests3", 0, S_OK, "http://www.winehq.org/tests3"},
274     {"http://www.winehq.org/tests/test1", "test2", 0, S_OK, "http://www.winehq.org/tests/test2"},
275     {"http://www.winehq.org/tests/../tests", "tests4", 0, S_OK, "http://www.winehq.org/tests4"},
276     {"http://www.winehq.org/tests/../tests/", "tests5", 0, S_OK, "http://www.winehq.org/tests/tests5"},
277     {"http://www.winehq.org/tests/../tests/", "/tests6/..", 0, S_OK, "http://www.winehq.org/"},
278     {"http://www.winehq.org/tests/../tests/..", "tests7/..", 0, S_OK, "http://www.winehq.org/"},
279     {"http://www.winehq.org/tests/?query=x&return=y", "tests8", 0, S_OK, "http://www.winehq.org/tests/tests8"},
280     {"http://www.winehq.org/tests/#example", "tests9", 0, S_OK, "http://www.winehq.org/tests/tests9"},
281     {"http://www.winehq.org/tests/../tests/", "/tests10/..", URL_DONT_SIMPLIFY, S_OK, "http://www.winehq.org/tests10/.."},
282     {"http://www.winehq.org/tests/../", "tests11", URL_DONT_SIMPLIFY, S_OK, "http://www.winehq.org/tests/../tests11"},
283     {"file:///C:\\dir\\file.txt", "test.txt", 0, S_OK, "file:///C:/dir/test.txt"},
284     {"file:///C:\\dir\\file.txt#hash\\hash", "test.txt", 0, S_OK, "file:///C:/dir/file.txt#hash/test.txt"},
285     {"file:///C:\\dir\\file.html#hash\\hash", "test.html", 0, S_OK, "file:///C:/dir/test.html"},
286     {"file:///C:\\dir\\file.htm#hash\\hash", "test.htm", 0, S_OK, "file:///C:/dir/test.htm"},
287     {"file:///C:\\dir\\file.hTmL#hash\\hash", "test.hTmL", 0, S_OK, "file:///C:/dir/test.hTmL"},
288     {"file:///C:\\dir.html\\file.txt#hash\\hash", "test.txt", 0, S_OK, "file:///C:/dir.html/file.txt#hash/test.txt"},
289     {"C:\\winehq\\winehq.txt", "C:\\Test\\test.txt", 0, S_OK, "file:///C:/Test/test.txt"},
290     {"http://www.winehq.org/test/", "test%20file.txt", 0, S_OK, "http://www.winehq.org/test/test%20file.txt"},
291     {"http://www.winehq.org/test/", "test%20file.txt", URL_FILE_USE_PATHURL, S_OK, "http://www.winehq.org/test/test%20file.txt"},
292     {"http://www.winehq.org%2ftest/", "test%20file.txt", URL_FILE_USE_PATHURL, S_OK, "http://www.winehq.org%2ftest/test%20file.txt"},
293     {"xxx:@MSITStore:file.chm/file.html", "dir/file", 0, S_OK, "xxx:dir/file"},
294     {"mk:@MSITStore:file.chm::/file.html", "/dir/file", 0, S_OK, "mk:@MSITStore:file.chm::/dir/file"},
295     {"mk:@MSITStore:file.chm::/file.html", "mk:@MSITStore:file.chm::/dir/file", 0, S_OK, "mk:@MSITStore:file.chm::/dir/file"},
296     {"foo:today", "foo:calendar", 0, S_OK, "foo:calendar"},
297     {"foo:today", "bar:calendar", 0, S_OK, "bar:calendar"},
298     {"foo:/today", "foo:calendar", 0, S_OK, "foo:/calendar"},
299     {"foo:/today/", "foo:calendar", 0, S_OK, "foo:/today/calendar"},
300     {"mk:@MSITStore:dir/test.chm::dir/index.html", "image.jpg", 0, S_OK, "mk:@MSITStore:dir/test.chm::dir/image.jpg"},
301     {"mk:@MSITStore:dir/test.chm::dir/dir2/index.html", "../image.jpg", 0, S_OK, "mk:@MSITStore:dir/test.chm::dir/image.jpg"},
302     /* UrlCombine case 2 tests.  Schemes do not match */
303     {"outbind://xxxxxxxxx","http://wine1/dir",0, S_OK,"http://wine1/dir"},
304     {"xxxx://xxxxxxxxx","http://wine2/dir",0, S_OK,"http://wine2/dir"},
305     {"ftp://xxxxxxxxx/","http://wine3/dir",0, S_OK,"http://wine3/dir"},
306     {"outbind://xxxxxxxxx","http://wine4/dir",URL_PLUGGABLE_PROTOCOL, S_OK,"http://wine4/dir"},
307     {"xxx://xxxxxxxxx","http://wine5/dir",URL_PLUGGABLE_PROTOCOL, S_OK,"http://wine5/dir"},
308     {"ftp://xxxxxxxxx/","http://wine6/dir",URL_PLUGGABLE_PROTOCOL, S_OK,"http://wine6/dir"},
309     {"http://xxxxxxxxx","outbind://wine7/dir",0, S_OK,"outbind://wine7/dir"},
310     {"xxx://xxxxxxxxx","ftp://wine8/dir",0, S_OK,"ftp://wine8/dir"},
311     {"ftp://xxxxxxxxx/","xxx://wine9/dir",0, S_OK,"xxx://wine9/dir"},
312     {"http://xxxxxxxxx","outbind://wine10/dir",URL_PLUGGABLE_PROTOCOL, S_OK,"outbind://wine10/dir"},
313     {"xxx://xxxxxxxxx","ftp://wine11/dir",URL_PLUGGABLE_PROTOCOL, S_OK,"ftp://wine11/dir"},
314     {"ftp://xxxxxxxxx/","xxx://wine12/dir",URL_PLUGGABLE_PROTOCOL, S_OK,"xxx://wine12/dir"},
315     {"http://xxxxxxxxx","outbind:wine13/dir",0, S_OK,"outbind:wine13/dir"},
316     {"xxx://xxxxxxxxx","ftp:wine14/dir",0, S_OK,"ftp:wine14/dir"},
317     {"ftp://xxxxxxxxx/","xxx:wine15/dir",0, S_OK,"xxx:wine15/dir"},
318     {"outbind://xxxxxxxxx/","http:wine16/dir",0, S_OK,"http:wine16/dir"},
319     {"http://xxxxxxxxx","outbind:wine17/dir",URL_PLUGGABLE_PROTOCOL, S_OK,"outbind:wine17/dir"},
320     {"xxx://xxxxxxxxx","ftp:wine18/dir",URL_PLUGGABLE_PROTOCOL, S_OK,"ftp:wine18/dir"},
321     {"ftp://xxxxxxxxx/","xxx:wine19/dir",URL_PLUGGABLE_PROTOCOL, S_OK,"xxx:wine19/dir"},
322     {"outbind://xxxxxxxxx/","http:wine20/dir",URL_PLUGGABLE_PROTOCOL, S_OK,"http:wine20/dir"},
323     {"file:///c:/dir/file.txt","index.html?test=c:/abc",URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO,S_OK,"file:///c:/dir/index.html?test=c:/abc"}
324 };
325
326 /* ################ */
327
328 static const struct {
329     const char *path;
330     const char *url;
331     DWORD ret;
332 } TEST_URLFROMPATH [] = {
333     {"foo", "file:foo", S_OK},
334     {"foo\\bar", "file:foo/bar", S_OK},
335     {"\\foo\\bar", "file:///foo/bar", S_OK},
336     {"c:\\foo\\bar", "file:///c:/foo/bar", S_OK},
337     {"c:foo\\bar", "file:///c:foo/bar", S_OK},
338     {"c:\\foo/b a%r", "file:///c:/foo/b%20a%25r", S_OK},
339     {"c:\\foo\\foo bar", "file:///c:/foo/foo%20bar", S_OK},
340 #if 0
341     /* The following test fails on native shlwapi as distributed with Win95/98.
342      * Wine matches the behaviour of later versions.
343      */
344     {"xx:c:\\foo\\bar", "xx:c:\\foo\\bar", S_FALSE}
345 #endif
346 };
347
348 /* ################ */
349
350 static struct {
351     char url[30];
352     const char *expect;
353 } TEST_URL_UNESCAPE[] = {
354     {"file://foo/bar", "file://foo/bar"},
355     {"file://fo%20o%5Ca/bar", "file://fo o\\a/bar"},
356     {"file://%24%25foobar", "file://$%foobar"}
357 };
358
359 /* ################ */
360
361 static const  struct {
362     const char *path;
363     BOOL expect;
364 } TEST_PATH_IS_URL[] = {
365     {"http://foo/bar", TRUE},
366     {"c:\\foo\\bar", FALSE},
367     {"foo://foo/bar", TRUE},
368     {"foo\\bar", FALSE},
369     {"foo.bar", FALSE},
370     {"bogusscheme:", TRUE},
371     {"http:partial", TRUE}
372 };
373
374 /* ################ */
375
376 static const struct {
377     const char *url;
378     BOOL expectOpaque;
379     BOOL expectFile;
380 } TEST_URLIS_ATTRIBS[] = {
381     {   "ftp:",                                         FALSE,  FALSE   },
382     {   "http:",                                        FALSE,  FALSE   },
383     {   "gopher:",                                      FALSE,  FALSE   },
384     {   "mailto:",                                      TRUE,   FALSE   },
385     {   "news:",                                        FALSE,  FALSE   },
386     {   "nntp:",                                        FALSE,  FALSE   },
387     {   "telnet:",                                      FALSE,  FALSE   },
388     {   "wais:",                                        FALSE,  FALSE   },
389     {   "file:",                                        FALSE,  TRUE    },
390     {   "mk:",                                          FALSE,  FALSE   },
391     {   "https:",                                       FALSE,  FALSE   },
392     {   "shell:",                                       TRUE,   FALSE   },
393     {   "https:",                                       FALSE,  FALSE   },
394     {   "snews:",                                       FALSE,  FALSE   },
395     {   "local:",                                       FALSE,  FALSE   },
396     {   "javascript:",                                  TRUE,   FALSE   },
397     {   "vbscript:",                                    TRUE,   FALSE   },
398     {   "about:",                                       TRUE,   FALSE   },
399     {   "res:",                                         FALSE,  FALSE   },
400     {   "bogusscheme:",                                 FALSE,  FALSE   },
401     {   "file:\\\\e:\\b\\c",                            FALSE,  TRUE    },
402     {   "file://e:/b/c",                                FALSE,  TRUE    },
403     {   "http:partial",                                 FALSE,  FALSE   },
404     {   "mailto://www.winehq.org/test.html",            TRUE,   FALSE   },
405     {   "file:partial",                                 FALSE,  TRUE    }
406 };
407
408 /* ########################### */
409
410 static LPWSTR GetWideString(const char* szString)
411 {
412   LPWSTR wszString = HeapAlloc(GetProcessHeap(), 0, (2*INTERNET_MAX_URL_LENGTH) * sizeof(WCHAR));
413
414   MultiByteToWideChar(0, 0, szString, -1, wszString, INTERNET_MAX_URL_LENGTH);
415
416   return wszString;
417 }
418
419
420 static void FreeWideString(LPWSTR wszString)
421 {
422    HeapFree(GetProcessHeap(), 0, wszString);
423 }
424
425 /* ########################### */
426
427 static void test_UrlApplyScheme(void)
428 {
429     CHAR newurl[TEST_APPLY_MAX_LENGTH];
430     WCHAR urlW[TEST_APPLY_MAX_LENGTH];
431     WCHAR newurlW[TEST_APPLY_MAX_LENGTH];
432     HRESULT res;
433     DWORD len;
434     DWORD i;
435
436     if (!pUrlApplySchemeA) {
437         win_skip("UrlApplySchemeA not found\n");
438         return;
439     }
440
441     for(i = 0; i < sizeof(TEST_APPLY)/sizeof(TEST_APPLY[0]); i++) {
442         len = TEST_APPLY_MAX_LENGTH;
443         lstrcpyA(newurl, untouchedA);
444         res = pUrlApplySchemeA(TEST_APPLY[i].url, newurl, &len, TEST_APPLY[i].flags);
445         ok( res == TEST_APPLY[i].res,
446             "#%dA: got HRESULT 0x%x (expected 0x%x)\n", i, res, TEST_APPLY[i].res);
447
448         ok( len == TEST_APPLY[i].newlen,
449             "#%dA: got len %d (expected %d)\n", i, len, TEST_APPLY[i].newlen);
450
451         ok( !lstrcmpA(newurl, TEST_APPLY[i].newurl),
452             "#%dA: got '%s' (expected '%s')\n", i, newurl, TEST_APPLY[i].newurl);
453
454         /* returned length is in character */
455         len = TEST_APPLY_MAX_LENGTH;
456         lstrcpyA(newurl, untouchedA);
457         MultiByteToWideChar(CP_ACP, 0, newurl, -1, newurlW, len);
458         MultiByteToWideChar(CP_ACP, 0, TEST_APPLY[i].url, -1, urlW, len);
459
460         res = pUrlApplySchemeW(urlW, newurlW, &len, TEST_APPLY[i].flags);
461         WideCharToMultiByte(CP_ACP, 0, newurlW, -1, newurl, TEST_APPLY_MAX_LENGTH, NULL, NULL);
462         ok( res == TEST_APPLY[i].res,
463             "#%dW: got HRESULT 0x%x (expected 0x%x)\n", i, res, TEST_APPLY[i].res);
464
465         ok( len == TEST_APPLY[i].newlen,
466             "#%dW: got len %d (expected %d)\n", i, len, TEST_APPLY[i].newlen);
467
468         ok( !lstrcmpA(newurl, TEST_APPLY[i].newurl),
469             "#%dW: got '%s' (expected '%s')\n", i, newurl, TEST_APPLY[i].newurl);
470
471     }
472
473     /* buffer too small */
474     lstrcpyA(newurl, untouchedA);
475     len = lstrlenA(TEST_APPLY[0].newurl);
476     res = pUrlApplySchemeA(TEST_APPLY[0].url, newurl, &len, TEST_APPLY[0].flags);
477     ok(res == E_POINTER, "got HRESULT 0x%x (expected E_POINTER)\n", res);
478     /* The returned length include the space for the terminating 0 */
479     i = lstrlenA(TEST_APPLY[0].newurl)+1;
480     ok(len == i, "got len %d (expected %d)\n", len, i);
481     ok(!lstrcmpA(newurl, untouchedA), "got '%s' (expected '%s')\n", newurl, untouchedA);
482
483     /* NULL as parameter. The length and the buffer are not modified */
484     lstrcpyA(newurl, untouchedA);
485     len = TEST_APPLY_MAX_LENGTH;
486     res = pUrlApplySchemeA(NULL, newurl, &len, TEST_APPLY[0].flags);
487     ok(res == E_INVALIDARG, "got HRESULT 0x%x (expected E_INVALIDARG)\n", res);
488     ok(len == TEST_APPLY_MAX_LENGTH, "got len %d\n", len);
489     ok(!lstrcmpA(newurl, untouchedA), "got '%s' (expected '%s')\n", newurl, untouchedA);
490
491     len = TEST_APPLY_MAX_LENGTH;
492     res = pUrlApplySchemeA(TEST_APPLY[0].url, NULL, &len, TEST_APPLY[0].flags);
493     ok(res == E_INVALIDARG, "got HRESULT 0x%x (expected E_INVALIDARG)\n", res);
494     ok(len == TEST_APPLY_MAX_LENGTH, "got len %d\n", len);
495
496     lstrcpyA(newurl, untouchedA);
497     res = pUrlApplySchemeA(TEST_APPLY[0].url, newurl, NULL, TEST_APPLY[0].flags);
498     ok(res == E_INVALIDARG, "got HRESULT 0x%x (expected E_INVALIDARG)\n", res);
499     ok(!lstrcmpA(newurl, untouchedA), "got '%s' (expected '%s')\n", newurl, untouchedA);
500
501 }
502
503 /* ########################### */
504
505 static void hash_url(const char* szUrl)
506 {
507   LPCSTR szTestUrl = szUrl;
508   LPWSTR wszTestUrl = GetWideString(szTestUrl);
509   HRESULT res;
510
511   DWORD cbSize = sizeof(DWORD);
512   DWORD dwHash1, dwHash2;
513   res = pUrlHashA(szTestUrl, (LPBYTE)&dwHash1, cbSize);
514   ok(res == S_OK, "UrlHashA returned 0x%x (expected S_OK) for %s\n", res, szUrl);
515   if (pUrlHashW) {
516     res = pUrlHashW(wszTestUrl, (LPBYTE)&dwHash2, cbSize);
517     ok(res == S_OK, "UrlHashW returned 0x%x (expected S_OK) for %s\n", res, szUrl);
518     ok(dwHash1 == dwHash2,
519         "Hashes didn't match (A: 0x%x, W: 0x%x) for %s\n", dwHash1, dwHash2, szUrl);
520   }
521   FreeWideString(wszTestUrl);
522
523 }
524
525 static void test_UrlHash(void)
526 {
527   if (!pUrlHashA) {
528     win_skip("UrlHashA not found\n");
529     return;
530   }
531
532   hash_url(TEST_URL_1);
533   hash_url(TEST_URL_2);
534   hash_url(TEST_URL_3);
535 }
536
537 /* ########################### */
538
539 static void test_url_part(const char* szUrl, DWORD dwPart, DWORD dwFlags, const char* szExpected)
540 {
541   CHAR szPart[INTERNET_MAX_URL_LENGTH];
542   WCHAR wszPart[INTERNET_MAX_URL_LENGTH];
543   LPWSTR wszUrl = GetWideString(szUrl);
544   LPWSTR wszConvertedPart;
545   HRESULT res;
546   DWORD dwSize;
547
548   dwSize = 1;
549   res = pUrlGetPartA(szUrl, szPart, &dwSize, dwPart, dwFlags);
550   ok(res == E_POINTER, "UrlGetPart for \"%s\" gave: 0x%08x\n", szUrl, res);
551   ok(dwSize == strlen(szExpected)+1 ||
552           (*szExpected == '?' && dwSize == strlen(szExpected)),
553           "UrlGetPart for \"%s\" gave size: %u\n", szUrl, dwSize);
554
555   dwSize = INTERNET_MAX_URL_LENGTH;
556   res = pUrlGetPartA(szUrl, szPart, &dwSize, dwPart, dwFlags);
557   ok(res == S_OK,
558     "UrlGetPartA for \"%s\" part 0x%08x returned 0x%x and \"%s\"\n",
559     szUrl, dwPart, res, szPart);
560   if (pUrlGetPartW) {
561     dwSize = INTERNET_MAX_URL_LENGTH;
562     res = pUrlGetPartW(wszUrl, wszPart, &dwSize, dwPart, dwFlags);
563     ok(res == S_OK,
564       "UrlGetPartW for \"%s\" part 0x%08x returned 0x%x\n",
565       szUrl, dwPart, res);
566
567     wszConvertedPart = GetWideString(szPart);
568
569     ok(lstrcmpW(wszPart,wszConvertedPart)==0,
570         "Strings didn't match between ascii and unicode UrlGetPart!\n");
571
572     FreeWideString(wszConvertedPart);
573   }
574   FreeWideString(wszUrl);
575
576   /* Note that v6.0 and later don't return '?' with the query */
577   ok(strcmp(szPart,szExpected)==0 ||
578      (*szExpected=='?' && !strcmp(szPart,szExpected+1)),
579          "Expected %s, but got %s\n", szExpected, szPart);
580 }
581
582 /* ########################### */
583
584 static void test_UrlGetPart(void)
585 {
586   const char* file_url = "file://h o s t/c:/windows/file";
587   const char* http_url = "http://user:pass 123@www.wine hq.org";
588   const char* res_url = "res://some.dll/find.dlg";
589   const char* about_url = "about:blank";
590
591   CHAR szPart[INTERNET_MAX_URL_LENGTH];
592   DWORD dwSize;
593   HRESULT res;
594
595   if (!pUrlGetPartA) {
596     win_skip("UrlGetPartA not found\n");
597     return;
598   }
599
600   res = pUrlGetPartA(NULL, NULL, NULL, URL_PART_SCHEME, 0);
601   ok(res == E_INVALIDARG, "null params gave: 0x%08x\n", res);
602
603   res = pUrlGetPartA(NULL, szPart, &dwSize, URL_PART_SCHEME, 0);
604   ok(res == E_INVALIDARG, "null URL gave: 0x%08x\n", res);
605
606   res = pUrlGetPartA(res_url, NULL, &dwSize, URL_PART_SCHEME, 0);
607   ok(res == E_INVALIDARG, "null szPart gave: 0x%08x\n", res);
608
609   res = pUrlGetPartA(res_url, szPart, NULL, URL_PART_SCHEME, 0);
610   ok(res == E_INVALIDARG, "null URL gave: 0x%08x\n", res);
611
612   dwSize = 0;
613   szPart[0]='x'; szPart[1]=0;
614   res = pUrlGetPartA("hi", szPart, &dwSize, URL_PART_SCHEME, 0);
615   ok(res == E_INVALIDARG, "UrlGetPartA(*pcchOut = 0) returned %08X\n", res);
616   ok(szPart[0] == 'x' && szPart[1] == 0, "UrlGetPartA(*pcchOut = 0) modified szPart: \"%s\"\n", szPart);
617   ok(dwSize == 0, "dwSize = %d\n", dwSize);
618
619   dwSize = sizeof szPart;
620   szPart[0]='x'; szPart[1]=0;
621   res = pUrlGetPartA("hi", szPart, &dwSize, URL_PART_SCHEME, 0);
622   ok (res==S_FALSE, "UrlGetPartA(\"hi\") returned %08X\n", res);
623   ok(szPart[0]==0, "UrlGetPartA(\"hi\") return \"%s\" instead of \"\"\n", szPart);
624   ok(dwSize == 0, "dwSize = %d\n", dwSize);
625
626   dwSize = sizeof szPart;
627   szPart[0]='x'; szPart[1]=0;
628   res = pUrlGetPartA("hi", szPart, &dwSize, URL_PART_QUERY, 0);
629   ok (res==S_FALSE, "UrlGetPartA(\"hi\") returned %08X\n", res);
630   ok(szPart[0]==0, "UrlGetPartA(\"hi\") return \"%s\" instead of \"\"\n", szPart);
631   ok(dwSize == 0, "dwSize = %d\n", dwSize);
632
633   test_url_part(TEST_URL_3, URL_PART_HOSTNAME, 0, "localhost");
634   test_url_part(TEST_URL_3, URL_PART_PORT, 0, "21");
635   test_url_part(TEST_URL_3, URL_PART_USERNAME, 0, "foo");
636   test_url_part(TEST_URL_3, URL_PART_PASSWORD, 0, "bar");
637   test_url_part(TEST_URL_3, URL_PART_SCHEME, 0, "http");
638   test_url_part(TEST_URL_3, URL_PART_QUERY, 0, "?query=x&return=y");
639
640   test_url_part(file_url, URL_PART_HOSTNAME, 0, "h o s t");
641
642   test_url_part(http_url, URL_PART_HOSTNAME, 0, "www.wine hq.org");
643   test_url_part(http_url, URL_PART_PASSWORD, 0, "pass 123");
644
645   test_url_part(about_url, URL_PART_SCHEME, 0, "about");
646
647   dwSize = sizeof(szPart);
648   res = pUrlGetPartA(about_url, szPart, &dwSize, URL_PART_HOSTNAME, 0);
649   ok(res==E_FAIL, "returned %08x\n", res);
650
651   test_url_part(res_url, URL_PART_SCHEME, 0, "res");
652   test_url_part("http://www.winehq.org", URL_PART_HOSTNAME, URL_PARTFLAG_KEEPSCHEME, "http:www.winehq.org");
653
654   dwSize = sizeof szPart;
655   szPart[0]='x'; szPart[1]=0;
656   res = pUrlGetPartA(res_url, szPart, &dwSize, URL_PART_QUERY, 0);
657   ok(res==S_FALSE, "UrlGetPartA returned %08X\n", res);
658   ok(szPart[0]==0, "UrlGetPartA gave \"%s\" instead of \"\"\n", szPart);
659   ok(dwSize == 0, "dwSize = %d\n", dwSize);
660
661   dwSize = sizeof(szPart);
662   res = pUrlGetPartA("file://c:\\index.htm", szPart, &dwSize, URL_PART_HOSTNAME, 0);
663   ok(res==S_FALSE, "returned %08x\n", res);
664   ok(dwSize == 0, "dwSize = %d\n", dwSize);
665
666   dwSize = sizeof(szPart);
667   szPart[0] = 'x'; szPart[1] = '\0';
668   res = pUrlGetPartA("file:some text", szPart, &dwSize, URL_PART_HOSTNAME, 0);
669   ok(res==S_FALSE, "returned %08x\n", res);
670   ok(szPart[0] == '\0', "szPart[0] = %c\n", szPart[0]);
671   ok(dwSize == 0, "dwSize = %d\n", dwSize);
672
673   dwSize = sizeof(szPart);
674   szPart[0] = 'x'; szPart[1] = '\0';
675   res = pUrlGetPartA("index.htm", szPart, &dwSize, URL_PART_HOSTNAME, 0);
676   ok(res==E_FAIL, "returned %08x\n", res);
677 }
678
679 /* ########################### */
680
681 static void test_url_escape(const char *szUrl, DWORD dwFlags, HRESULT dwExpectReturn, const char *szExpectUrl)
682 {
683     CHAR szReturnUrl[INTERNET_MAX_URL_LENGTH];
684     DWORD dwEscaped;
685     WCHAR ret_urlW[INTERNET_MAX_URL_LENGTH];
686     WCHAR *urlW, *expected_urlW;
687     dwEscaped=INTERNET_MAX_URL_LENGTH;
688
689     ok(pUrlEscapeA(szUrl, szReturnUrl, &dwEscaped, dwFlags) == dwExpectReturn,
690         "UrlEscapeA didn't return 0x%08x from \"%s\"\n", dwExpectReturn, szUrl);
691     ok(strcmp(szReturnUrl,szExpectUrl)==0, "Expected \"%s\", but got \"%s\" from \"%s\"\n", szExpectUrl, szReturnUrl, szUrl);
692
693     if (pUrlEscapeW) {
694         dwEscaped = INTERNET_MAX_URL_LENGTH;
695         urlW = GetWideString(szUrl);
696         expected_urlW = GetWideString(szExpectUrl);
697         ok(pUrlEscapeW(urlW, ret_urlW, &dwEscaped, dwFlags) == dwExpectReturn,
698             "UrlEscapeW didn't return 0x%08x from \"%s\"\n", dwExpectReturn, szUrl);
699         WideCharToMultiByte(CP_ACP,0,ret_urlW,-1,szReturnUrl,INTERNET_MAX_URL_LENGTH,0,0);
700         ok(lstrcmpW(ret_urlW, expected_urlW)==0,
701             "Expected \"%s\", but got \"%s\" from \"%s\" flags %08x\n",
702             szExpectUrl, szReturnUrl, szUrl, dwFlags);
703         FreeWideString(urlW);
704         FreeWideString(expected_urlW);
705     }
706 }
707
708 static void test_url_canonicalize(int index, const char *szUrl, DWORD dwFlags, HRESULT dwExpectReturn, HRESULT dwExpectReturnAlt, const char *szExpectUrl, BOOL todo)
709 {
710     CHAR szReturnUrl[INTERNET_MAX_URL_LENGTH];
711     WCHAR wszReturnUrl[INTERNET_MAX_URL_LENGTH];
712     LPWSTR wszUrl = GetWideString(szUrl);
713     LPWSTR wszExpectUrl = GetWideString(szExpectUrl);
714     LPWSTR wszConvertedUrl;
715     HRESULT ret;
716
717     DWORD dwSize;
718
719     dwSize = INTERNET_MAX_URL_LENGTH;
720     ret = pUrlCanonicalizeA(szUrl, NULL, &dwSize, dwFlags);
721     ok(ret != dwExpectReturn, "got 0s%x: Unexpected return for NULL buffer, index %d\n", ret, index);
722     ret = pUrlCanonicalizeA(szUrl, szReturnUrl, &dwSize, dwFlags);
723     ok(ret == dwExpectReturn || ret == dwExpectReturnAlt,
724        "UrlCanonicalizeA failed: expected=0x%08x or 0x%08x, got=0x%08x, index %d\n",
725        dwExpectReturn, dwExpectReturnAlt, ret, index);
726     if (todo)
727         todo_wine
728         ok(strcmp(szReturnUrl,szExpectUrl)==0, "UrlCanonicalizeA dwFlags 0x%08x url '%s' Expected \"%s\", but got \"%s\", index %d\n", dwFlags, szUrl, szExpectUrl, szReturnUrl, index);
729     else
730         ok(strcmp(szReturnUrl,szExpectUrl)==0, "UrlCanonicalizeA dwFlags 0x%08x url '%s' Expected \"%s\", but got \"%s\", index %d\n", dwFlags, szUrl, szExpectUrl, szReturnUrl, index);
731
732     if (pUrlCanonicalizeW) {
733         dwSize = INTERNET_MAX_URL_LENGTH;
734         ret = pUrlCanonicalizeW(wszUrl, NULL, &dwSize, dwFlags);
735         ok(ret != dwExpectReturn, "got 0x%x: Unexpected return for NULL buffer, index %d\n", ret, index);
736         ret = pUrlCanonicalizeW(wszUrl, wszReturnUrl, &dwSize, dwFlags);
737         ok(ret == dwExpectReturn, "UrlCanonicalizeW failed: expected 0x%08x, got 0x%x, index %d\n",
738             dwExpectReturn, ret, index);
739
740         wszConvertedUrl = GetWideString(szReturnUrl);
741         ok(lstrcmpW(wszReturnUrl, wszConvertedUrl)==0,
742             "Strings didn't match between ascii and unicode UrlCanonicalize, index %d!\n", index);
743         FreeWideString(wszConvertedUrl);
744     }
745
746     FreeWideString(wszUrl);
747     FreeWideString(wszExpectUrl);
748 }
749
750
751 static void test_UrlEscape(void)
752 {
753     DWORD size = 0;
754     HRESULT ret;
755     unsigned int i;
756     char empty_string[] = "";
757
758     if (!pUrlEscapeA) {
759         win_skip("UrlEscapeA noz found\n");
760         return;
761     }
762
763     ret = pUrlEscapeA("/woningplan/woonkamer basis.swf", NULL, &size, URL_ESCAPE_SPACES_ONLY);
764     ok(ret == E_INVALIDARG, "got %x, expected %x\n", ret, E_INVALIDARG);
765     ok(size == 0, "got %d, expected %d\n", size, 0);
766
767     size = 0;
768     ret = pUrlEscapeA("/woningplan/woonkamer basis.swf", empty_string, &size, URL_ESCAPE_SPACES_ONLY);
769     ok(ret == E_INVALIDARG, "got %x, expected %x\n", ret, E_INVALIDARG);
770     ok(size == 0, "got %d, expected %d\n", size, 0);
771
772     size = 1;
773     ret = pUrlEscapeA("/woningplan/woonkamer basis.swf", NULL, &size, URL_ESCAPE_SPACES_ONLY);
774     ok(ret == E_INVALIDARG, "got %x, expected %x\n", ret, E_INVALIDARG);
775     ok(size == 1, "got %d, expected %d\n", size, 1);
776
777     size = 1;
778     ret = pUrlEscapeA("/woningplan/woonkamer basis.swf", empty_string, NULL, URL_ESCAPE_SPACES_ONLY);
779     ok(ret == E_INVALIDARG, "got %x, expected %x\n", ret, E_INVALIDARG);
780     ok(size == 1, "got %d, expected %d\n", size, 1);
781
782     size = 1;
783     ret = pUrlEscapeA("/woningplan/woonkamer basis.swf", empty_string, &size, URL_ESCAPE_SPACES_ONLY);
784     ok(ret == E_POINTER, "got %x, expected %x\n", ret, E_POINTER);
785     ok(size == 34, "got %d, expected %d\n", size, 34);
786
787     for(i=0; i<sizeof(TEST_ESCAPE)/sizeof(TEST_ESCAPE[0]); i++) {
788         test_url_escape(TEST_ESCAPE[i].url, TEST_ESCAPE[i].flags,
789                               TEST_ESCAPE[i].expectret, TEST_ESCAPE[i].expecturl);
790     }
791 }
792
793 /* ########################### */
794
795 static void test_UrlCanonicalizeA(void)
796 {
797     unsigned int i;
798     CHAR szReturnUrl[4*INTERNET_MAX_URL_LENGTH];
799     CHAR longurl[4*INTERNET_MAX_URL_LENGTH];
800     DWORD dwSize;
801     DWORD urllen;
802     HRESULT hr;
803
804     if (!pUrlCanonicalizeA) {
805         win_skip("UrlCanonicalizeA not found\n");
806         return;
807     }
808
809     urllen = lstrlenA(winehqA);
810
811     /* buffer has no space for the result */
812     dwSize=urllen-1;
813     memset(szReturnUrl, '#', urllen+4);
814     szReturnUrl[urllen+4] = '\0';
815     SetLastError(0xdeadbeef);
816     hr = pUrlCanonicalizeA(winehqA, szReturnUrl, &dwSize, URL_WININET_COMPATIBILITY  | URL_ESCAPE_UNSAFE);
817     ok( (hr == E_POINTER) && (dwSize == (urllen + 1)),
818         "got 0x%x with %u and size %u for '%s' and %u (expected 'E_POINTER' and size %u)\n",
819         hr, GetLastError(), dwSize, szReturnUrl, lstrlenA(szReturnUrl), urllen+1);
820
821     /* buffer has no space for the terminating '\0' */
822     dwSize=urllen;
823     memset(szReturnUrl, '#', urllen+4);
824     szReturnUrl[urllen+4] = '\0';
825     SetLastError(0xdeadbeef);
826     hr = pUrlCanonicalizeA(winehqA, szReturnUrl, &dwSize, URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE);
827     ok( (hr == E_POINTER) && (dwSize == (urllen + 1)),
828         "got 0x%x with %u and size %u for '%s' and %u (expected 'E_POINTER' and size %u)\n",
829         hr, GetLastError(), dwSize, szReturnUrl, lstrlenA(szReturnUrl), urllen+1);
830
831     /* buffer has the required size */
832     dwSize=urllen+1;
833     memset(szReturnUrl, '#', urllen+4);
834     szReturnUrl[urllen+4] = '\0';
835     SetLastError(0xdeadbeef);
836     hr = pUrlCanonicalizeA(winehqA, szReturnUrl, &dwSize, URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE);
837     ok( (hr == S_OK) && (dwSize == urllen),
838         "got 0x%x with %u and size %u for '%s' and %u (expected 'S_OK' and size %u)\n",
839         hr, GetLastError(), dwSize, szReturnUrl, lstrlenA(szReturnUrl), urllen);
840
841     /* buffer is larger as the required size */
842     dwSize=urllen+2;
843     memset(szReturnUrl, '#', urllen+4);
844     szReturnUrl[urllen+4] = '\0';
845     SetLastError(0xdeadbeef);
846     hr = pUrlCanonicalizeA(winehqA, szReturnUrl, &dwSize, URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE);
847     ok( (hr == S_OK) && (dwSize == urllen),
848         "got 0x%x with %u and size %u for '%s' and %u (expected 'S_OK' and size %u)\n",
849         hr, GetLastError(), dwSize, szReturnUrl, lstrlenA(szReturnUrl), urllen);
850
851     /* length is set to 0 */
852     dwSize=0;
853     memset(szReturnUrl, '#', urllen+4);
854     szReturnUrl[urllen+4] = '\0';
855     SetLastError(0xdeadbeef);
856     hr = pUrlCanonicalizeA(winehqA, szReturnUrl, &dwSize, URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE);
857     ok( (hr == E_INVALIDARG) && (dwSize == 0),
858             "got 0x%x with %u and size %u for '%s' and %u (expected 'E_INVALIDARG' and size %u)\n",
859             hr, GetLastError(), dwSize, szReturnUrl, lstrlenA(szReturnUrl), 0);
860
861     /* url length > INTERNET_MAX_URL_SIZE */
862     dwSize=sizeof(szReturnUrl);
863     memset(longurl, 'a', sizeof(longurl));
864     memcpy(longurl, winehqA, sizeof(winehqA)-1);
865     longurl[sizeof(longurl)-1] = '\0';
866     hr = pUrlCanonicalizeA(longurl, szReturnUrl, &dwSize, URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE);
867     ok(hr == S_OK, "hr = %x\n", hr);
868
869     test_url_canonicalize(-1, "", 0, S_OK, S_FALSE /* Vista/win2k8 */, "", FALSE);
870
871     /* test url-modification */
872     for(i=0; i<sizeof(TEST_CANONICALIZE)/sizeof(TEST_CANONICALIZE[0]); i++) {
873         test_url_canonicalize(i, TEST_CANONICALIZE[i].url, TEST_CANONICALIZE[i].flags,
874                               TEST_CANONICALIZE[i].expectret, TEST_CANONICALIZE[i].expectret, TEST_CANONICALIZE[i].expecturl,
875                               TEST_CANONICALIZE[i].todo);
876     }
877 }
878
879 /* ########################### */
880
881 static void test_UrlCanonicalizeW(void)
882 {
883     WCHAR szReturnUrl[INTERNET_MAX_URL_LENGTH];
884     DWORD dwSize;
885     DWORD urllen;
886     HRESULT hr;
887     int i;
888
889
890     if (!pUrlCanonicalizeW) {
891         win_skip("UrlCanonicalizeW not found\n");
892         return;
893     }
894     urllen = lstrlenW(winehqW);
895
896     /* buffer has no space for the result */
897     dwSize = (urllen-1);
898     memset(szReturnUrl, '#', (urllen+4) * sizeof(WCHAR));
899     szReturnUrl[urllen+4] = '\0';
900     SetLastError(0xdeadbeef);
901     hr = pUrlCanonicalizeW(winehqW, szReturnUrl, &dwSize, URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE);
902     ok( (hr == E_POINTER) && (dwSize == (urllen + 1)),
903         "got 0x%x with %u and size %u for %u (expected 'E_POINTER' and size %u)\n",
904         hr, GetLastError(), dwSize, lstrlenW(szReturnUrl), urllen+1);
905
906
907     /* buffer has no space for the terminating '\0' */
908     dwSize = urllen;
909     memset(szReturnUrl, '#', (urllen+4) * sizeof(WCHAR));
910     szReturnUrl[urllen+4] = '\0';
911     SetLastError(0xdeadbeef);
912     hr = pUrlCanonicalizeW(winehqW, szReturnUrl, &dwSize, URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE);
913     ok( (hr == E_POINTER) && (dwSize == (urllen + 1)),
914         "got 0x%x with %u and size %u for %u (expected 'E_POINTER' and size %u)\n",
915         hr, GetLastError(), dwSize, lstrlenW(szReturnUrl), urllen+1);
916
917     /* buffer has the required size */
918     dwSize = urllen +1;
919     memset(szReturnUrl, '#', (urllen+4) * sizeof(WCHAR));
920     szReturnUrl[urllen+4] = '\0';
921     SetLastError(0xdeadbeef);
922     hr = pUrlCanonicalizeW(winehqW, szReturnUrl, &dwSize, URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE);
923     ok( (hr == S_OK) && (dwSize == urllen),
924         "got 0x%x with %u and size %u for %u (expected 'S_OK' and size %u)\n",
925         hr, GetLastError(), dwSize, lstrlenW(szReturnUrl), urllen);
926
927     /* buffer is larger as the required size */
928     dwSize = (urllen+2);
929     memset(szReturnUrl, '#', (urllen+4) * sizeof(WCHAR));
930     szReturnUrl[urllen+4] = '\0';
931     SetLastError(0xdeadbeef);
932     hr = pUrlCanonicalizeW(winehqW, szReturnUrl, &dwSize, URL_WININET_COMPATIBILITY | URL_ESCAPE_UNSAFE);
933     ok( (hr == S_OK) && (dwSize == urllen),
934         "got 0x%x with %u and size %u for %u (expected 'S_OK' and size %u)\n",
935         hr, GetLastError(), dwSize, lstrlenW(szReturnUrl), urllen);
936
937     /* check that the characters 1..32 are chopped from the end of the string */
938     for (i = 1; i < 65536; i++)
939     {
940         WCHAR szUrl[128];
941         BOOL choped;
942         int pos;
943
944         MultiByteToWideChar(CP_ACP, 0, "http://www.winehq.org/X", -1, szUrl, 128);
945         pos = lstrlenW(szUrl) - 1;
946         szUrl[pos] = i;
947         urllen = INTERNET_MAX_URL_LENGTH;
948         pUrlCanonicalizeW(szUrl, szReturnUrl, &urllen, 0);
949         choped = lstrlenW(szReturnUrl) < lstrlenW(szUrl);
950         ok(choped == (i <= 32), "Incorrect char chopping for char %d\n", i);
951     }
952 }
953
954 /* ########################### */
955
956 static void test_url_combine(const char *szUrl1, const char *szUrl2, DWORD dwFlags, HRESULT dwExpectReturn, const char *szExpectUrl)
957 {
958     HRESULT hr;
959     CHAR szReturnUrl[INTERNET_MAX_URL_LENGTH];
960     WCHAR wszReturnUrl[INTERNET_MAX_URL_LENGTH];
961     LPWSTR wszUrl1 = GetWideString(szUrl1);
962     LPWSTR wszUrl2 = GetWideString(szUrl2);
963     LPWSTR wszExpectUrl = GetWideString(szExpectUrl);
964     LPWSTR wszConvertedUrl;
965
966     DWORD dwSize;
967     DWORD dwExpectLen = lstrlen(szExpectUrl);
968
969     if (!pUrlCombineA) {
970         win_skip("UrlCombineA not found\n");
971         return;
972     }
973
974     hr = pUrlCombineA(szUrl1, szUrl2, NULL, NULL, dwFlags);
975     ok(hr == E_INVALIDARG, "UrlCombineA returned 0x%08x, expected 0x%08x\n", hr, E_INVALIDARG);
976
977     dwSize = 0;
978     hr = pUrlCombineA(szUrl1, szUrl2, NULL, &dwSize, dwFlags);
979     ok(hr == E_POINTER, "Checking length of string, return was 0x%08x, expected 0x%08x\n", hr, E_POINTER);
980     ok(dwSize == dwExpectLen+1, "Got length %d, expected %d\n", dwSize, dwExpectLen+1);
981
982     dwSize--;
983     hr = pUrlCombineA(szUrl1, szUrl2, szReturnUrl, &dwSize, dwFlags);
984     ok(hr == E_POINTER, "UrlCombineA returned 0x%08x, expected 0x%08x\n", hr, E_POINTER);
985     ok(dwSize == dwExpectLen+1, "Got length %d, expected %d\n", dwSize, dwExpectLen+1);
986
987     hr = pUrlCombineA(szUrl1, szUrl2, szReturnUrl, &dwSize, dwFlags);
988     ok(hr == dwExpectReturn, "UrlCombineA returned 0x%08x, expected 0x%08x\n", hr, dwExpectReturn);
989     ok(dwSize == dwExpectLen, "Got length %d, expected %d\n", dwSize, dwExpectLen);
990     if(SUCCEEDED(hr)) {
991         ok(strcmp(szReturnUrl,szExpectUrl)==0, "Expected %s, but got %s\n", szExpectUrl, szReturnUrl);
992     }
993
994     if (pUrlCombineW) {
995         dwSize = 0;
996         hr = pUrlCombineW(wszUrl1, wszUrl2, NULL, &dwSize, dwFlags);
997         ok(hr == E_POINTER, "Checking length of string, return was 0x%08x, expected 0x%08x\n", hr, E_POINTER);
998         ok(dwSize == dwExpectLen+1, "Got length %d, expected %d\n", dwSize, dwExpectLen+1);
999
1000         dwSize--;
1001         hr = pUrlCombineW(wszUrl1, wszUrl2, wszReturnUrl, &dwSize, dwFlags);
1002         ok(hr == E_POINTER, "UrlCombineW returned 0x%08x, expected 0x%08x\n", hr, E_POINTER);
1003         ok(dwSize == dwExpectLen+1, "Got length %d, expected %d\n", dwSize, dwExpectLen+1);
1004
1005         hr = pUrlCombineW(wszUrl1, wszUrl2, wszReturnUrl, &dwSize, dwFlags);
1006         ok(hr == dwExpectReturn, "UrlCombineW returned 0x%08x, expected 0x%08x\n", hr, dwExpectReturn);
1007         ok(dwSize == dwExpectLen, "Got length %d, expected %d\n", dwSize, dwExpectLen);
1008         if(SUCCEEDED(hr)) {
1009             wszConvertedUrl = GetWideString(szReturnUrl);
1010             ok(lstrcmpW(wszReturnUrl, wszConvertedUrl)==0, "Strings didn't match between ascii and unicode UrlCombine!\n");
1011             FreeWideString(wszConvertedUrl);
1012         }
1013     }
1014
1015     FreeWideString(wszUrl1);
1016     FreeWideString(wszUrl2);
1017     FreeWideString(wszExpectUrl);
1018 }
1019
1020 /* ########################### */
1021
1022 static void test_UrlCombine(void)
1023 {
1024     unsigned int i;
1025     for(i=0; i<sizeof(TEST_COMBINE)/sizeof(TEST_COMBINE[0]); i++) {
1026         test_url_combine(TEST_COMBINE[i].url1, TEST_COMBINE[i].url2, TEST_COMBINE[i].flags,
1027                          TEST_COMBINE[i].expectret, TEST_COMBINE[i].expecturl);
1028     }
1029 }
1030
1031 /* ########################### */
1032
1033 static void test_UrlCreateFromPath(void)
1034 {
1035     size_t i;
1036     char ret_url[INTERNET_MAX_URL_LENGTH];
1037     DWORD len, ret;
1038     WCHAR ret_urlW[INTERNET_MAX_URL_LENGTH];
1039     WCHAR *pathW, *urlW;
1040
1041     if (!pUrlCreateFromPathA) {
1042         win_skip("UrlCreateFromPathA not found\n");
1043         return;
1044     }
1045
1046     for(i = 0; i < sizeof(TEST_URLFROMPATH) / sizeof(TEST_URLFROMPATH[0]); i++) {
1047         len = INTERNET_MAX_URL_LENGTH;
1048         ret = pUrlCreateFromPathA(TEST_URLFROMPATH[i].path, ret_url, &len, 0);
1049         ok(ret == TEST_URLFROMPATH[i].ret, "ret %08x from path %s\n", ret, TEST_URLFROMPATH[i].path);
1050         ok(!lstrcmpi(ret_url, TEST_URLFROMPATH[i].url), "url %s from path %s\n", ret_url, TEST_URLFROMPATH[i].path);
1051         ok(len == strlen(ret_url), "ret len %d from path %s\n", len, TEST_URLFROMPATH[i].path);
1052
1053         if (pUrlCreateFromPathW) {
1054             len = INTERNET_MAX_URL_LENGTH;
1055             pathW = GetWideString(TEST_URLFROMPATH[i].path);
1056             urlW = GetWideString(TEST_URLFROMPATH[i].url);
1057             ret = pUrlCreateFromPathW(pathW, ret_urlW, &len, 0);
1058             WideCharToMultiByte(CP_ACP, 0, ret_urlW, -1, ret_url, sizeof(ret_url),0,0);
1059             ok(ret == TEST_URLFROMPATH[i].ret, "ret %08x from path L\"%s\", expected %08x\n",
1060                 ret, TEST_URLFROMPATH[i].path, TEST_URLFROMPATH[i].ret);
1061             ok(!lstrcmpiW(ret_urlW, urlW), "got %s expected %s from path L\"%s\"\n",
1062                 ret_url, TEST_URLFROMPATH[i].url, TEST_URLFROMPATH[i].path);
1063             ok(len == lstrlenW(ret_urlW), "ret len %d from path L\"%s\"\n", len, TEST_URLFROMPATH[i].path);
1064             FreeWideString(urlW);
1065             FreeWideString(pathW);
1066         }
1067     }
1068 }
1069
1070 /* ########################### */
1071
1072 static void test_UrlIs_null(DWORD flag)
1073 {
1074     BOOL ret;
1075     ret = pUrlIsA(NULL, flag);
1076     ok(ret == FALSE, "pUrlIsA(NULL, %d) failed\n", flag);
1077     ret = pUrlIsW(NULL, flag);
1078     ok(ret == FALSE, "pUrlIsW(NULL, %d) failed\n", flag);
1079 }
1080
1081 static void test_UrlIs(void)
1082 {
1083     BOOL ret;
1084     size_t i;
1085     WCHAR wurl[80];
1086
1087     if (!pUrlIsA) {
1088         win_skip("UrlIsA not found\n");
1089         return;
1090     }
1091
1092     test_UrlIs_null(URLIS_APPLIABLE);
1093     test_UrlIs_null(URLIS_DIRECTORY);
1094     test_UrlIs_null(URLIS_FILEURL);
1095     test_UrlIs_null(URLIS_HASQUERY);
1096     test_UrlIs_null(URLIS_NOHISTORY);
1097     test_UrlIs_null(URLIS_OPAQUE);
1098     test_UrlIs_null(URLIS_URL);
1099
1100     for(i = 0; i < sizeof(TEST_PATH_IS_URL) / sizeof(TEST_PATH_IS_URL[0]); i++) {
1101         MultiByteToWideChar(CP_ACP, 0, TEST_PATH_IS_URL[i].path, -1, wurl, 80);
1102
1103         ret = pUrlIsA( TEST_PATH_IS_URL[i].path, URLIS_URL );
1104         ok( ret == TEST_PATH_IS_URL[i].expect,
1105             "returned %d from path %s, expected %d\n", ret, TEST_PATH_IS_URL[i].path,
1106             TEST_PATH_IS_URL[i].expect );
1107
1108         if (pUrlIsW) {
1109             ret = pUrlIsW( wurl, URLIS_URL );
1110             ok( ret == TEST_PATH_IS_URL[i].expect,
1111                 "returned %d from path (UrlIsW) %s, expected %d\n", ret,
1112                 TEST_PATH_IS_URL[i].path, TEST_PATH_IS_URL[i].expect );
1113         }
1114     }
1115     for(i = 0; i < sizeof(TEST_URLIS_ATTRIBS) / sizeof(TEST_URLIS_ATTRIBS[0]); i++) {
1116         MultiByteToWideChar(CP_ACP, 0, TEST_URLIS_ATTRIBS[i].url, -1, wurl, 80);
1117
1118         ret = pUrlIsA( TEST_URLIS_ATTRIBS[i].url, URLIS_OPAQUE);
1119         ok( ret == TEST_URLIS_ATTRIBS[i].expectOpaque,
1120             "returned %d for URLIS_OPAQUE, url \"%s\", expected %d\n", ret, TEST_URLIS_ATTRIBS[i].url,
1121             TEST_URLIS_ATTRIBS[i].expectOpaque );
1122         ret = pUrlIsA( TEST_URLIS_ATTRIBS[i].url, URLIS_FILEURL);
1123         ok( ret == TEST_URLIS_ATTRIBS[i].expectFile,
1124             "returned %d for URLIS_FILEURL, url \"%s\", expected %d\n", ret, TEST_URLIS_ATTRIBS[i].url,
1125             TEST_URLIS_ATTRIBS[i].expectFile );
1126
1127         if (pUrlIsW) {
1128             ret = pUrlIsW( wurl, URLIS_OPAQUE);
1129             ok( ret == TEST_URLIS_ATTRIBS[i].expectOpaque,
1130                 "returned %d for URLIS_OPAQUE (UrlIsW), url \"%s\", expected %d\n",
1131                 ret, TEST_URLIS_ATTRIBS[i].url, TEST_URLIS_ATTRIBS[i].expectOpaque );
1132             ret = pUrlIsW( wurl, URLIS_FILEURL);
1133             ok( ret == TEST_URLIS_ATTRIBS[i].expectFile,
1134                 "returned %d for URLIS_FILEURL (UrlIsW), url \"%s\", expected %d\n",
1135                 ret, TEST_URLIS_ATTRIBS[i].url, TEST_URLIS_ATTRIBS[i].expectFile );
1136         }
1137     }
1138 }
1139
1140 /* ########################### */
1141
1142 static void test_UrlUnescape(void)
1143 {
1144     CHAR szReturnUrl[INTERNET_MAX_URL_LENGTH];
1145     WCHAR ret_urlW[INTERNET_MAX_URL_LENGTH];
1146     WCHAR *urlW, *expected_urlW;
1147     DWORD dwEscaped;
1148     size_t i;
1149     static char inplace[] = "file:///C:/Program%20Files";
1150     static char another_inplace[] = "file:///C:/Program%20Files";
1151     static const char expected[] = "file:///C:/Program Files";
1152     static WCHAR inplaceW[] = {'f','i','l','e',':','/','/','/','C',':','/','P','r','o','g','r','a','m',' ','F','i','l','e','s',0};
1153     static WCHAR another_inplaceW[] ={'f','i','l','e',':','/','/','/',
1154                 'C',':','/','P','r','o','g','r','a','m','%','2','0','F','i','l','e','s',0};
1155     HRESULT res;
1156
1157     if (!pUrlUnescapeA) {
1158         win_skip("UrlUnescapeA not found\n");
1159         return;
1160     }
1161     for(i=0; i<sizeof(TEST_URL_UNESCAPE)/sizeof(TEST_URL_UNESCAPE[0]); i++) {
1162         dwEscaped=INTERNET_MAX_URL_LENGTH;
1163         res = pUrlUnescapeA(TEST_URL_UNESCAPE[i].url, szReturnUrl, &dwEscaped, 0);
1164         ok(res == S_OK,
1165             "UrlUnescapeA returned 0x%x (expected S_OK) for \"%s\"\n",
1166             res, TEST_URL_UNESCAPE[i].url);
1167         ok(strcmp(szReturnUrl,TEST_URL_UNESCAPE[i].expect)==0, "Expected \"%s\", but got \"%s\" from \"%s\"\n", TEST_URL_UNESCAPE[i].expect, szReturnUrl, TEST_URL_UNESCAPE[i].url);
1168
1169         ZeroMemory(szReturnUrl, sizeof(szReturnUrl));
1170         /* if we set the bufferpointer to NULL here UrlUnescape  fails and string gets not converted */
1171         res = pUrlUnescapeA(TEST_URL_UNESCAPE[i].url, szReturnUrl, NULL, 0);
1172         ok(res == E_INVALIDARG,
1173             "UrlUnescapeA returned 0x%x (expected E_INVALIDARG) for \"%s\"\n",
1174             res, TEST_URL_UNESCAPE[i].url);
1175         ok(strcmp(szReturnUrl,"")==0, "Expected empty string\n");
1176
1177         if (pUrlUnescapeW) {
1178             dwEscaped = INTERNET_MAX_URL_LENGTH;
1179             urlW = GetWideString(TEST_URL_UNESCAPE[i].url);
1180             expected_urlW = GetWideString(TEST_URL_UNESCAPE[i].expect);
1181             res = pUrlUnescapeW(urlW, ret_urlW, &dwEscaped, 0);
1182             ok(res == S_OK,
1183                 "UrlUnescapeW returned 0x%x (expected S_OK) for \"%s\"\n",
1184                 res, TEST_URL_UNESCAPE[i].url);
1185
1186             WideCharToMultiByte(CP_ACP,0,ret_urlW,-1,szReturnUrl,INTERNET_MAX_URL_LENGTH,0,0);
1187             ok(lstrcmpW(ret_urlW, expected_urlW)==0,
1188                 "Expected \"%s\", but got \"%s\" from \"%s\" flags %08lx\n",
1189                 TEST_URL_UNESCAPE[i].expect, szReturnUrl, TEST_URL_UNESCAPE[i].url, 0L);
1190             FreeWideString(urlW);
1191             FreeWideString(expected_urlW);
1192         }
1193     }
1194
1195     dwEscaped = sizeof(inplace);
1196     res = pUrlUnescapeA(inplace, NULL, &dwEscaped, URL_UNESCAPE_INPLACE);
1197     ok(res == S_OK, "UrlUnescapeA returned 0x%x (expected S_OK)\n", res);
1198     ok(!strcmp(inplace, expected), "got %s expected %s\n", inplace, expected);
1199     ok(dwEscaped == 27, "got %d expected 27\n", dwEscaped);
1200
1201     /* if we set the bufferpointer to NULL, the string apparently still gets converted (Google Lively does this)) */
1202     res = pUrlUnescapeA(another_inplace, NULL, NULL, URL_UNESCAPE_INPLACE);
1203     ok(res == S_OK, "UrlUnescapeA returned 0x%x (expected S_OK)\n", res);
1204     ok(!strcmp(another_inplace, expected), "got %s expected %s\n", another_inplace, expected);
1205
1206     if (pUrlUnescapeW) {
1207         dwEscaped = sizeof(inplaceW);
1208         res = pUrlUnescapeW(inplaceW, NULL, &dwEscaped, URL_UNESCAPE_INPLACE);
1209         ok(res == S_OK, "UrlUnescapeW returned 0x%x (expected S_OK)\n", res);
1210         ok(dwEscaped == 50, "got %d expected 50\n", dwEscaped);
1211
1212         /* if we set the bufferpointer to NULL, the string apparently still gets converted (Google Lively does this)) */
1213         res = pUrlUnescapeW(another_inplaceW, NULL, NULL, URL_UNESCAPE_INPLACE);
1214         ok(res == S_OK, "UrlUnescapeW returned 0x%x (expected S_OK)\n", res);
1215
1216         ok(lstrlenW(another_inplaceW) == 24, "got %d expected 24\n", lstrlenW(another_inplaceW));
1217     }
1218 }
1219
1220 static const struct parse_url_test_t {
1221     const char *url;
1222     HRESULT hres;
1223     UINT protocol_len;
1224     UINT scheme;
1225 } parse_url_tests[] = {
1226     {"http://www.winehq.org/",S_OK,4,URL_SCHEME_HTTP},
1227     {"https://www.winehq.org/",S_OK,5,URL_SCHEME_HTTPS},
1228     {"ftp://www.winehq.org/",S_OK,3,URL_SCHEME_FTP},
1229     {"test.txt?test=c:/dir",URL_E_INVALID_SYNTAX},
1230     {"test.txt",URL_E_INVALID_SYNTAX},
1231     {"xxx://www.winehq.org/",S_OK,3,URL_SCHEME_UNKNOWN},
1232     {"1xx://www.winehq.org/",S_OK,3,URL_SCHEME_UNKNOWN},
1233     {"-xx://www.winehq.org/",S_OK,3,URL_SCHEME_UNKNOWN},
1234     {"xx0://www.winehq.org/",S_OK,3,URL_SCHEME_UNKNOWN},
1235     {"x://www.winehq.org/",URL_E_INVALID_SYNTAX},
1236     {"xx$://www.winehq.org/",URL_E_INVALID_SYNTAX},
1237     {"htt?p://www.winehq.org/",URL_E_INVALID_SYNTAX},
1238     {"ab-://www.winehq.org/",S_OK,3,URL_SCHEME_UNKNOWN},
1239     {" http://www.winehq.org/",URL_E_INVALID_SYNTAX},
1240 };
1241
1242 static void test_ParseURL(void)
1243 {
1244     const struct parse_url_test_t *test;
1245     WCHAR url[INTERNET_MAX_URL_LENGTH];
1246     PARSEDURLA parseda;
1247     PARSEDURLW parsedw;
1248     HRESULT hres;
1249
1250     for(test = parse_url_tests; test < parse_url_tests + sizeof(parse_url_tests)/sizeof(*parse_url_tests); test++) {
1251         memset(&parseda, 0xd0, sizeof(parseda));
1252         parseda.cbSize = sizeof(parseda);
1253         hres = pParseURLA(test->url, &parseda);
1254         ok(hres == test->hres, "ParseURL failed: %08x, expected %08x\n", hres, test->hres);
1255         if(hres == S_OK) {
1256             ok(parseda.pszProtocol == test->url, "parseda.pszProtocol = %s, expected %s\n",
1257                parseda.pszProtocol, test->url);
1258             ok(parseda.cchProtocol == test->protocol_len, "parseda.cchProtocol = %d, expected %d\n",
1259                parseda.cchProtocol, test->protocol_len);
1260             ok(parseda.pszSuffix == test->url+test->protocol_len+1, "parseda.pszSuffix = %s, expected %s\n",
1261                parseda.pszSuffix, test->url+test->protocol_len+1);
1262             ok(parseda.cchSuffix == strlen(test->url+test->protocol_len+1),
1263                "parseda.pszSuffix = %d, expected %d\n",
1264                parseda.cchSuffix, lstrlenA(test->url+test->protocol_len+1));
1265             ok(parseda.nScheme == test->scheme, "parseda.nScheme = %d, expected %d\n",
1266                parseda.nScheme, test->scheme);
1267         }else {
1268             ok(!parseda.pszProtocol, "parseda.pszProtocol = %p\n", parseda.pszProtocol);
1269             ok(parseda.nScheme == 0xd0d0d0d0, "nScheme = %d\n", parseda.nScheme);
1270         }
1271
1272         MultiByteToWideChar(CP_ACP, 0, test->url, -1, url, sizeof(url)/sizeof(WCHAR));
1273
1274         memset(&parsedw, 0xd0, sizeof(parsedw));
1275         parsedw.cbSize = sizeof(parsedw);
1276         hres = pParseURLW(url, &parsedw);
1277         ok(hres == test->hres, "ParseURL failed: %08x, expected %08x\n", hres, test->hres);
1278         if(hres == S_OK) {
1279             ok(parsedw.pszProtocol == url, "parsedw.pszProtocol = %s, expected %s\n",
1280                wine_dbgstr_w(parsedw.pszProtocol), wine_dbgstr_w(url));
1281             ok(parsedw.cchProtocol == test->protocol_len, "parsedw.cchProtocol = %d, expected %d\n",
1282                parsedw.cchProtocol, test->protocol_len);
1283             ok(parsedw.pszSuffix == url+test->protocol_len+1, "parsedw.pszSuffix = %s, expected %s\n",
1284                wine_dbgstr_w(parsedw.pszSuffix), wine_dbgstr_w(url+test->protocol_len+1));
1285             ok(parsedw.cchSuffix == strlen(test->url+test->protocol_len+1),
1286                "parsedw.pszSuffix = %d, expected %d\n",
1287                parsedw.cchSuffix, lstrlenA(test->url+test->protocol_len+1));
1288             ok(parsedw.nScheme == test->scheme, "parsedw.nScheme = %d, expected %d\n",
1289                parsedw.nScheme, test->scheme);
1290         }else {
1291             ok(!parsedw.pszProtocol, "parsedw.pszProtocol = %p\n", parseda.pszProtocol);
1292             ok(parsedw.nScheme == 0xd0d0d0d0, "nScheme = %d\n", parsedw.nScheme);
1293         }
1294     }
1295 }
1296
1297 static void test_HashData(void)
1298 {
1299     HRESULT res;
1300     BYTE input[16] = {0x51, 0x33, 0x4F, 0xA7, 0x45, 0x15, 0xF0, 0x52, 0x90,
1301                       0x2B, 0xE7, 0xF5, 0xFD, 0xE1, 0xA6, 0xA7};
1302     BYTE output[32];
1303     static const BYTE expected[] = {0x54, 0x9C, 0x92, 0x55, 0xCD, 0x82, 0xFF,
1304                                     0xA1, 0x8E, 0x0F, 0xCF, 0x93, 0x14, 0xAA,
1305                                     0xE3, 0x2D};
1306     static const BYTE expected2[] = {0x54, 0x9C, 0x92, 0x55, 0xCD, 0x82, 0xFF,
1307                                      0xA1, 0x8E, 0x0F, 0xCF, 0x93, 0x14, 0xAA,
1308                                      0xE3, 0x2D, 0x47, 0xFC, 0x80, 0xB8, 0xD0,
1309                                      0x49, 0xE6, 0x13, 0x2A, 0x30, 0x51, 0x8D,
1310                                      0xF9, 0x4B, 0x07, 0xA6};
1311     static const BYTE expected3[] = {0x2B, 0xDC, 0x9A, 0x1B, 0xF0, 0x5A, 0xF9,
1312                                      0xC6, 0xBE, 0x94, 0x6D, 0xF3, 0x33, 0xC1,
1313                                      0x36, 0x07};
1314     int i;
1315
1316     /* Test hashing with identically sized input/output buffers. */
1317     res = pHashData(input, 16, output, 16);
1318     ok(res == S_OK, "Expected HashData to return S_OK, got 0x%08x\n", res);
1319     if(res == S_OK)
1320        ok(!memcmp(output, expected, sizeof(expected)),
1321           "Output buffer did not match expected contents\n");
1322
1323     /* Test hashing with larger output buffer. */
1324     res = pHashData(input, 16, output, 32);
1325     ok(res == S_OK, "Expected HashData to return S_OK, got 0x%08x\n", res);
1326     if(res == S_OK)
1327        ok(!memcmp(output, expected2, sizeof(expected2)),
1328           "Output buffer did not match expected contents\n");
1329
1330     /* Test hashing with smaller input buffer. */
1331     res = pHashData(input, 8, output, 16);
1332     ok(res == S_OK, "Expected HashData to return S_OK, got 0x%08x\n", res);
1333     if(res == S_OK)
1334        ok(!memcmp(output, expected3, sizeof(expected3)),
1335           "Output buffer did not match expected contents\n");
1336
1337     /* Test passing NULL pointers for input/output parameters. */
1338     res = pHashData(NULL, 0, NULL, 0);
1339     ok(res == E_INVALIDARG || broken(res == S_OK), /* Windows 2000 */
1340        "Expected HashData to return E_INVALIDARG, got 0x%08x\n", res);
1341
1342     res = pHashData(input, 0, NULL, 0);
1343     ok(res == E_INVALIDARG || broken(res == S_OK), /* Windows 2000 */
1344        "Expected HashData to return E_INVALIDARG, got 0x%08x\n", res);
1345
1346     res = pHashData(NULL, 0, output, 0);
1347     ok(res == E_INVALIDARG || broken(res == S_OK), /* Windows 2000 */
1348        "Expected HashData to return E_INVALIDARG, got 0x%08x\n", res);
1349
1350     /* Test passing valid pointers with sizes of zero. */
1351     for (i = 0; i < sizeof(input)/sizeof(BYTE); i++)
1352         input[i] = 0x00;
1353
1354     for (i = 0; i < sizeof(output)/sizeof(BYTE); i++)
1355         output[i] = 0xFF;
1356
1357     res = pHashData(input, 0, output, 0);
1358     ok(res == S_OK, "Expected HashData to return S_OK, got 0x%08x\n", res);
1359
1360     /* The buffers should be unchanged. */
1361     for (i = 0; i < sizeof(input)/sizeof(BYTE); i++)
1362     {
1363         ok(input[i] == 0x00, "Expected the input buffer to be unchanged\n");
1364         if(input[i] != 0x00) break;
1365     }
1366
1367     for (i = 0; i < sizeof(output)/sizeof(BYTE); i++)
1368     {
1369         ok(output[i] == 0xFF, "Expected the output buffer to be unchanged\n");
1370         if(output[i] != 0xFF) break;
1371     }
1372
1373     /* Input/output parameters are not validated. */
1374     res = pHashData((BYTE *)0xdeadbeef, 0, (BYTE *)0xdeadbeef, 0);
1375     ok(res == S_OK, "Expected HashData to return S_OK, got 0x%08x\n", res);
1376
1377     if (0)
1378     {
1379         res = pHashData((BYTE *)0xdeadbeef, 1, (BYTE *)0xdeadbeef, 1);
1380         trace("HashData returned 0x%08x\n", res);
1381     }
1382 }
1383
1384 /* ########################### */
1385
1386 START_TEST(url)
1387 {
1388
1389   hShlwapi = GetModuleHandleA("shlwapi.dll");
1390   pUrlUnescapeA = (void *) GetProcAddress(hShlwapi, "UrlUnescapeA");
1391   pUrlUnescapeW = (void *) GetProcAddress(hShlwapi, "UrlUnescapeW");
1392   pUrlIsA = (void *) GetProcAddress(hShlwapi, "UrlIsA");
1393   pUrlIsW = (void *) GetProcAddress(hShlwapi, "UrlIsW");
1394   pUrlHashA = (void *) GetProcAddress(hShlwapi, "UrlHashA");
1395   pUrlHashW = (void *) GetProcAddress(hShlwapi, "UrlHashW");
1396   pUrlGetPartA = (void *) GetProcAddress(hShlwapi, "UrlGetPartA");
1397   pUrlGetPartW = (void *) GetProcAddress(hShlwapi, "UrlGetPartW");
1398   pUrlEscapeA = (void *) GetProcAddress(hShlwapi, "UrlEscapeA");
1399   pUrlEscapeW = (void *) GetProcAddress(hShlwapi, "UrlEscapeW");
1400   pUrlCreateFromPathA = (void *) GetProcAddress(hShlwapi, "UrlCreateFromPathA");
1401   pUrlCreateFromPathW = (void *) GetProcAddress(hShlwapi, "UrlCreateFromPathW");
1402   pUrlCombineA = (void *) GetProcAddress(hShlwapi, "UrlCombineA");
1403   pUrlCombineW = (void *) GetProcAddress(hShlwapi, "UrlCombineW");
1404   pUrlCanonicalizeA = (void *) GetProcAddress(hShlwapi, "UrlCanonicalizeA");
1405   pUrlCanonicalizeW = (void *) GetProcAddress(hShlwapi, "UrlCanonicalizeW");
1406   pUrlApplySchemeA = (void *) GetProcAddress(hShlwapi, "UrlApplySchemeA");
1407   pUrlApplySchemeW = (void *) GetProcAddress(hShlwapi, "UrlApplySchemeW");
1408   pParseURLA = (void*)GetProcAddress(hShlwapi, (LPCSTR)1);
1409   pParseURLW = (void*)GetProcAddress(hShlwapi, (LPCSTR)2);
1410   pHashData = (void*)GetProcAddress(hShlwapi, "HashData");
1411
1412   test_UrlApplyScheme();
1413   test_UrlHash();
1414   test_UrlGetPart();
1415   test_UrlCanonicalizeA();
1416   test_UrlCanonicalizeW();
1417   test_UrlEscape();
1418   test_UrlCombine();
1419   test_UrlCreateFromPath();
1420   test_UrlIs();
1421   test_UrlUnescape();
1422   test_ParseURL();
1423   test_HashData();
1424 }