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