Assorted spelling fixes.
[wine] / dlls / shdocvw / tests / shdocvw.c
1 /*
2  * Unit tests for misc shdocvw functions
3  *
4  * Copyright 2008 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
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "wininet.h"
28 #include "winnls.h"
29
30 #include "wine/test.h"
31
32 /* ################ */
33
34 static HMODULE hshdocvw;
35 static HRESULT (WINAPI *pURLSubRegQueryA)(LPCSTR, LPCSTR, DWORD, LPVOID, DWORD, DWORD);
36 static DWORD (WINAPI *pParseURLFromOutsideSourceA)(LPCSTR, LPSTR, LPDWORD, LPDWORD);
37 static DWORD (WINAPI *pParseURLFromOutsideSourceW)(LPCWSTR, LPWSTR, LPDWORD, LPDWORD);
38
39 static CHAR appdata[] = "AppData";
40 static CHAR common_appdata[] = "Common AppData";
41 static CHAR default_page_url[] = "Default_Page_URL";
42 static CHAR does_not_exist[] = "does_not_exist";
43 static CHAR regpath_iemain[] = "Software\\Microsoft\\Internet Explorer\\Main";
44 static CHAR regpath_shellfolders[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
45 static CHAR start_page[] = "Start Page";
46
47 /* ################ */
48
49 static const  struct {
50     const char *url;
51     const char *newurl;
52     DWORD len;
53 } ParseURL_table[] = {
54     {"http://www.winehq.org", "http://www.winehq.org/", 22},
55     {"www.winehq.org", "http://www.winehq.org/", 22},
56     {"winehq.org", "http://winehq.org/", 18},
57     {"ftp.winehq.org", "ftp://ftp.winehq.org/", 21},
58     {"http://winehq.org", "http://winehq.org/", 18},
59     {"https://winehq.org", "https://winehq.org/", 19},
60     {"https://www.winehq.org", "https://www.winehq.org/", 23},
61     {"ftp://winehq.org", "ftp://winehq.org/", 17},
62     {"ftp://ftp.winehq.org", "ftp://ftp.winehq.org/", 21},
63     {"about:blank", "about:blank", 11},
64     {"about:home", "about:home", 10},
65     {"about:mozilla", "about:mozilla", 13},
66     /* a space at the start is not allowed */
67     {" http://www.winehq.org", "http://%20http://www.winehq.org", 31}
68
69 };
70
71 /* ################ */
72
73 static void init_functions(void)
74 {
75     hshdocvw = LoadLibraryA("shdocvw.dll");
76     pURLSubRegQueryA = (void *) GetProcAddress(hshdocvw, (LPSTR) 151);
77     pParseURLFromOutsideSourceA = (void *) GetProcAddress(hshdocvw, (LPSTR) 169);
78     pParseURLFromOutsideSourceW = (void *) GetProcAddress(hshdocvw, (LPSTR) 170);
79 }
80
81 /* ################ */
82
83 static void test_URLSubRegQueryA(void)
84 {
85     CHAR buffer[INTERNET_MAX_URL_LENGTH];
86     HRESULT hr;
87     DWORD used;
88     DWORD len;
89
90     if (!pURLSubRegQueryA) {
91         skip("URLSubRegQueryA not found\n");
92         return;
93     }
94
95     memset(buffer, '#', sizeof(buffer)-1);
96     buffer[sizeof(buffer)-1] = '\0';
97     /* called by inetcpl.cpl */
98     hr = pURLSubRegQueryA(regpath_iemain, default_page_url, REG_SZ, buffer, INTERNET_MAX_URL_LENGTH, -1);
99     ok(hr == E_FAIL || hr == S_OK, "got 0x%x (expected E_FAIL or S_OK)\n", hr);
100
101     memset(buffer, '#', sizeof(buffer)-1);
102     buffer[sizeof(buffer)-1] = '\0';
103     /* called by inetcpl.cpl */
104     hr = pURLSubRegQueryA(regpath_iemain, start_page, REG_SZ, buffer, INTERNET_MAX_URL_LENGTH, -1);
105     len = lstrlenA(buffer);
106     /* respect privacy: do not dump the url */
107     ok(hr == S_OK, "got 0x%x and %d (expected S_OK)\n", hr, len);
108
109     /* test buffer length: just large enough */
110     memset(buffer, '#', sizeof(buffer)-1);
111     buffer[sizeof(buffer)-1] = '\0';
112     hr = pURLSubRegQueryA(regpath_iemain, start_page, REG_SZ, buffer, len+1, -1);
113     used = lstrlenA(buffer);
114     /* respect privacy: do not dump the url */
115     ok((hr == S_OK) && (used == len),
116         "got 0x%x and %d (expected S_OK and %d)\n", hr, used, len);
117
118     /* no space for terminating 0: result is truncated */
119     memset(buffer, '#', sizeof(buffer)-1);
120     buffer[sizeof(buffer)-1] = '\0';
121     hr = pURLSubRegQueryA(regpath_iemain, start_page, REG_SZ, buffer, len, -1);
122     used = lstrlenA(buffer);
123     ok((hr == S_OK) && (used == len - 1),
124         "got 0x%x and %d (expected S_OK and %d)\n", hr, used, len - 1);
125
126     /* no space for the complete result: truncate another char */
127     if (len > 1) {
128         memset(buffer, '#', sizeof(buffer)-1);
129         buffer[sizeof(buffer)-1] = '\0';
130         hr = pURLSubRegQueryA(regpath_iemain, start_page, REG_SZ, buffer, len-1, -1);
131         used = lstrlenA(buffer);
132         ok((hr == S_OK) && (used == (len - 2)),
133             "got 0x%x and %d (expected S_OK and %d)\n", hr, used, len - 2);
134     }
135
136     /* only space for the terminating 0: function still succeded */
137     memset(buffer, '#', sizeof(buffer)-1);
138     buffer[sizeof(buffer)-1] = '\0';
139     hr = pURLSubRegQueryA(regpath_iemain, start_page, REG_SZ, buffer, 1, -1);
140     used = lstrlenA(buffer);
141     ok((hr == S_OK) && !used,
142         "got 0x%x and %d (expected S_OK and 0)\n", hr, used);
143
144     /* size of buffer is 0, but the function still succeed.
145        buffer[0] is cleared in IE 5.01 and IE 5.5 (Buffer Overflow) */
146     memset(buffer, '#', sizeof(buffer)-1);
147     buffer[sizeof(buffer)-1] = '\0';
148     hr = pURLSubRegQueryA(regpath_iemain, start_page, REG_SZ, buffer, 0, -1);
149     used = lstrlenA(buffer);
150     ok( (hr == S_OK) &&
151         ((used == INTERNET_MAX_URL_LENGTH - 1) || broken(used == 0)) ,
152         "got 0x%x and %d (expected S_OK and INTERNET_MAX_URL_LENGTH - 1)\n",
153         hr, used);
154
155     /* still succeed without a buffer for the result */
156     hr = pURLSubRegQueryA(regpath_iemain, start_page, REG_SZ, NULL, 0, -1);
157     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
158
159     /* still succeed, when a length is given without a buffer */
160     hr = pURLSubRegQueryA(regpath_iemain, start_page, REG_SZ, NULL, INTERNET_MAX_URL_LENGTH, -1);
161     ok(hr == S_OK, "got 0x%x (expected S_OK)\n", hr);
162
163     /* this value does not exist */
164     memset(buffer, '#', sizeof(buffer)-1);
165     buffer[sizeof(buffer)-1] = '\0';
166     hr = pURLSubRegQueryA(regpath_iemain, does_not_exist, REG_SZ, buffer, INTERNET_MAX_URL_LENGTH, -1);
167     /* random bytes are copied to the buffer */
168     ok((hr == E_FAIL), "got 0x%x (expected E_FAIL)\n", hr);
169
170     /* the third parameter is ignored. Is it really a type? (data is REG_SZ) */
171     memset(buffer, '#', sizeof(buffer)-1);
172     buffer[sizeof(buffer)-1] = '\0';
173     hr = pURLSubRegQueryA(regpath_iemain, start_page, REG_DWORD, buffer, INTERNET_MAX_URL_LENGTH, -1);
174     used = lstrlenA(buffer);
175     ok((hr == S_OK) && (used == len),
176         "got 0x%x and %d (expected S_OK and %d)\n", hr, used, len);
177
178     /* the function works for HKCU and HKLM */
179     memset(buffer, '#', sizeof(buffer)-1);
180     buffer[sizeof(buffer)-1] = '\0';
181     hr = pURLSubRegQueryA(regpath_shellfolders, appdata, REG_SZ, buffer, INTERNET_MAX_URL_LENGTH, -1);
182     used = lstrlenA(buffer);
183     ok(hr == S_OK, "got 0x%x and %d (expected S_OK)\n", hr, used);
184
185     memset(buffer, '#', sizeof(buffer)-1);
186     buffer[sizeof(buffer)-1] = '\0';
187     hr = pURLSubRegQueryA(regpath_shellfolders, common_appdata, REG_SZ, buffer, INTERNET_MAX_URL_LENGTH, -1);
188     used = lstrlenA(buffer);
189     ok(hr == S_OK, "got 0x%x and %d (expected S_OK)\n", hr, used);
190
191     /* todo: what does the last parameter mean? */
192 }
193
194 /* ################ */
195
196 static void test_ParseURLFromOutsideSourceA(void)
197 {
198     CHAR buffer[INTERNET_MAX_URL_LENGTH];
199     DWORD dummy;
200     DWORD maxlen;
201     DWORD len;
202     DWORD res;
203     int i;
204
205     if (!pParseURLFromOutsideSourceA) {
206         skip("ParseURLFromOutsideSourceA not found\n");
207         return;
208     }
209
210     for(i = 0; i < sizeof(ParseURL_table)/sizeof(ParseURL_table[0]); i++) {
211         memset(buffer, '#', sizeof(buffer)-1);
212         buffer[sizeof(buffer)-1] = '\0';
213         len = sizeof(buffer);
214         dummy = 0;
215         /* on success, len+1 is returned. No idea, if someone depend on this */
216         res = pParseURLFromOutsideSourceA(ParseURL_table[i].url, buffer, &len, &dummy);
217         /* len does not include the terminating 0, when buffer is large enough */
218         ok( res != 0 && len == ParseURL_table[i].len &&
219             !lstrcmpA(buffer, ParseURL_table[i].newurl),
220             "#%d: got %d and %d with '%s' (expected '!=0' and %d with '%s')\n",
221             i, res, len, buffer, ParseURL_table[i].len, ParseURL_table[i].newurl);
222
223
224         /* use the size test only for the first examples */
225         if (i > 4) continue;
226
227         maxlen = len;
228
229         memset(buffer, '#', sizeof(buffer)-1);
230         buffer[sizeof(buffer)-1] = '\0';
231         len = maxlen + 1;
232         dummy = 0;
233         res = pParseURLFromOutsideSourceA(ParseURL_table[i].url, buffer, &len, &dummy);
234         ok( res != 0 && len == ParseURL_table[i].len &&
235             !lstrcmpA(buffer, ParseURL_table[i].newurl),
236             "#%d (+1): got %d and %d with '%s' (expected '!=0' and %d with '%s')\n",
237             i, res, len, buffer, ParseURL_table[i].len, ParseURL_table[i].newurl);
238
239         memset(buffer, '#', sizeof(buffer)-1);
240         buffer[sizeof(buffer)-1] = '\0';
241         len = maxlen;
242         dummy = 0;
243         res = pParseURLFromOutsideSourceA(ParseURL_table[i].url, buffer, &len, &dummy);
244         /* len includes the terminating 0, when the buffer is too small */
245         ok( res == 0 && len == ParseURL_table[i].len + 1,
246             "#%d (==): got %d and %d (expected '0' and %d)\n",
247             i, res, len, ParseURL_table[i].len + 1);
248
249         memset(buffer, '#', sizeof(buffer)-1);
250         buffer[sizeof(buffer)-1] = '\0';
251         len = maxlen-1;
252         dummy = 0;
253         res = pParseURLFromOutsideSourceA(ParseURL_table[i].url, buffer, &len, &dummy);
254         ok( res == 0 && len == ParseURL_table[i].len + 1,
255             "#%d (-1): got %d and %d (expected '0' and %d)\n",
256             i, res, len, ParseURL_table[i].len + 1);
257
258         memset(buffer, '#', sizeof(buffer)-1);
259         buffer[sizeof(buffer)-1] = '\0';
260         len = maxlen+1;
261         dummy = 0;
262         res = pParseURLFromOutsideSourceA(ParseURL_table[i].url, NULL, &len, &dummy);
263         /* len does not include the terminating 0, when buffer is NULL */
264         ok( res == 0 && len == ParseURL_table[i].len,
265             "#%d (buffer): got %d and %d (expected '0' and %d)\n",
266             i, res, len, ParseURL_table[i].len);
267
268         if (0) {
269             /* that test crash on native shdocvw */
270             res = pParseURLFromOutsideSourceA(ParseURL_table[i].url, buffer, NULL, &dummy);
271         }
272
273         memset(buffer, '#', sizeof(buffer)-1);
274         buffer[sizeof(buffer)-1] = '\0';
275         len = maxlen+1;
276         dummy = 0;
277         res = pParseURLFromOutsideSourceA(ParseURL_table[i].url, buffer, &len, NULL);
278         ok( res != 0 && len == ParseURL_table[i].len &&
279             !lstrcmpA(buffer, ParseURL_table[i].newurl),
280             "#%d (unknown): got %d and %d with '%s' (expected '!=0' and %d with '%s')\n",
281             i, res, len, buffer, ParseURL_table[i].len, ParseURL_table[i].newurl);
282     }
283 }
284
285 /* ################ */
286
287 static void test_ParseURLFromOutsideSourceW(void)
288 {
289     WCHAR urlW[INTERNET_MAX_URL_LENGTH];
290     WCHAR bufferW[INTERNET_MAX_URL_LENGTH];
291     CHAR  bufferA[INTERNET_MAX_URL_LENGTH];
292     DWORD maxlen;
293     DWORD dummy;
294     DWORD len;
295     DWORD res;
296
297     if (!pParseURLFromOutsideSourceW) {
298         skip("ParseURLFromOutsideSourceW not found\n");
299         return;
300     }
301     MultiByteToWideChar(CP_ACP, 0, ParseURL_table[0].url, -1, urlW, INTERNET_MAX_URL_LENGTH);
302
303     memset(bufferA, '#', sizeof(bufferA)-1);
304     bufferA[sizeof(bufferA) - 1] = '\0';
305     MultiByteToWideChar(CP_ACP, 0, bufferA, -1, bufferW, INTERNET_MAX_URL_LENGTH);
306
307     /* len is in characters */
308     len = sizeof(bufferW)/sizeof(bufferW[0]);
309     dummy = 0;
310     res = pParseURLFromOutsideSourceW(urlW, bufferW, &len, &dummy);
311     WideCharToMultiByte(CP_ACP, 0, bufferW, -1, bufferA, sizeof(bufferA), NULL, NULL);
312     ok( res != 0 && len == ParseURL_table[0].len &&
313         !lstrcmpA(bufferA, ParseURL_table[0].newurl),
314         "got %d and %d with '%s' (expected '!=0' and %d with '%s')\n",
315         res, len, bufferA, ParseURL_table[0].len, ParseURL_table[0].newurl);
316
317
318     maxlen = len;
319
320     memset(bufferA, '#', sizeof(bufferA)-1);
321     bufferA[sizeof(bufferA) - 1] = '\0';
322     MultiByteToWideChar(CP_ACP, 0, bufferA, -1, bufferW, INTERNET_MAX_URL_LENGTH);
323     len = maxlen+1;
324     dummy = 0;
325     res = pParseURLFromOutsideSourceW(urlW, bufferW, &len, &dummy);
326     WideCharToMultiByte(CP_ACP, 0, bufferW, -1, bufferA, sizeof(bufferA), NULL, NULL);
327     /* len does not include the terminating 0, when buffer is large enough */
328     ok( res != 0 && len == ParseURL_table[0].len &&
329         !lstrcmpA(bufferA, ParseURL_table[0].newurl),
330         "+1: got %d and %d with '%s' (expected '!=0' and %d with '%s')\n",
331         res, len, bufferA, ParseURL_table[0].len, ParseURL_table[0].newurl);
332
333     len = maxlen;
334     dummy = 0;
335     res = pParseURLFromOutsideSourceW(urlW, bufferW, &len, &dummy);
336     /* len includes the terminating 0, when the buffer is too small */
337     ok( res == 0 && len == ParseURL_table[0].len + 1,
338         "==: got %d and %d (expected '0' and %d)\n",
339         res, len, ParseURL_table[0].len + 1);
340
341     len = maxlen - 1;
342     dummy = 0;
343     res = pParseURLFromOutsideSourceW(urlW, bufferW, &len, &dummy);
344     ok( res == 0 && len == ParseURL_table[0].len + 1,
345         "-1: got %d and %d (expected '0' and %d)\n",
346         res, len, ParseURL_table[0].len + 1);
347
348 }
349
350 /* ################ */
351
352 START_TEST(shdocvw)
353 {
354     init_functions();
355     test_URLSubRegQueryA();
356     test_ParseURLFromOutsideSourceA();
357     test_ParseURLFromOutsideSourceW();
358 }