msvcrt: Mark function that are only called from assembly as hidden.
[wine.git] / dlls / wininet / tests / ftp.c
blob567545d2b9fe692532c869ac9690640f53a4fdd9
1 /*
2 * Wininet - ftp tests
4 * Copyright 2007 Paul Vriens
5 * Copyright 2007 Hans Leidekker
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * FIXME:
24 * Use InternetGetLastResponseInfo when the last error is set to ERROR_INTERNET_EXTENDED_ERROR.
25 * TODO:
26 * Add W-function tests.
27 * Add missing function tests:
28 * FtpGetFileSize
29 * FtpSetCurrentDirectory
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wininet.h"
39 #include "winsock2.h"
41 #include "wine/test.h"
44 static BOOL (WINAPI *pFtpCommandA)(HINTERNET,BOOL,DWORD,LPCSTR,DWORD_PTR,HINTERNET*);
45 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET,INTERNET_STATUS_CALLBACK);
48 static void test_getfile_no_open(void)
50 BOOL bRet;
52 /* Invalid internet handle, the others are valid parameters */
53 SetLastError(0xdeadbeef);
54 bRet = FtpGetFileA(NULL, "welcome.msg", "should_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
55 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
56 ok ( GetLastError() == ERROR_INTERNET_NOT_INITIALIZED ||
57 GetLastError() == ERROR_INVALID_HANDLE,
58 "Expected ERROR_INTERNET_NOT_INITIALIZED or ERROR_INVALID_HANDLE (win98), got %d\n", GetLastError());
61 static void test_connect(HINTERNET hInternet)
63 HINTERNET hFtp;
65 /* Try a few username/password combinations:
66 * anonymous : NULL
67 * NULL : IEUser@
68 * NULL : NULL
69 * "" : IEUser@
70 * "" : NULL
73 SetLastError(0xdeadbeef);
74 hFtp = InternetConnectA(hInternet, "ftp.winehq.org", INTERNET_DEFAULT_FTP_PORT, "anonymous", NULL, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
75 if (hFtp) /* some servers accept an empty password */
77 ok ( GetLastError() == ERROR_SUCCESS, "ERROR_SUCCESS, got %d\n", GetLastError());
78 InternetCloseHandle(hFtp);
80 else
81 ok ( GetLastError() == ERROR_INTERNET_LOGIN_FAILURE,
82 "Expected ERROR_INTERNET_LOGIN_FAILURE, got %d\n", GetLastError());
84 SetLastError(0xdeadbeef);
85 hFtp = InternetConnectA(hInternet, "ftp.winehq.org", INTERNET_DEFAULT_FTP_PORT, NULL, "IEUser@", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
86 ok ( hFtp == NULL, "Expected InternetConnect to fail\n");
87 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
88 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
90 SetLastError(0xdeadbeef);
91 hFtp = InternetConnectA(hInternet, "ftp.winehq.org", INTERNET_DEFAULT_FTP_PORT, "", "IEUser@",
92 INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
93 ok(!hFtp, "Expected InternetConnect to fail\n");
94 ok(GetLastError() == ERROR_INVALID_PARAMETER,
95 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
97 /* Using a NULL username and password will be interpreted as anonymous ftp. The username will be 'anonymous' the password
98 * is created via some simple heuristics (see dlls/wininet/ftp.c).
99 * On Wine this registry key is not set by default so (NULL, NULL) will result in anonymous ftp with an (most likely) not
100 * accepted password (the username).
101 * If the first call fails because we get an ERROR_INTERNET_LOGIN_FAILURE, we try again with a (more) correct password.
104 SetLastError(0xdeadbeef);
105 hFtp = InternetConnectA(hInternet, "ftp.winehq.org", INTERNET_DEFAULT_FTP_PORT, NULL, NULL, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
106 if (!hFtp && (GetLastError() == ERROR_INTERNET_LOGIN_FAILURE))
108 /* We are most likely running on a clean Wine install or a Windows install where the registry key is removed */
109 SetLastError(0xdeadbeef);
110 hFtp = InternetConnectA(hInternet, "ftp.winehq.org", INTERNET_DEFAULT_FTP_PORT, "anonymous", "IEUser@", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
112 ok ( hFtp != NULL, "InternetConnect failed : %d\n", GetLastError());
113 ok ( GetLastError() == ERROR_SUCCESS,
114 "ERROR_SUCCESS, got %d\n", GetLastError());
116 SetLastError(0xdeadbeef);
117 hFtp = InternetConnectA(hInternet, "ftp.winehq.org", INTERNET_DEFAULT_FTP_PORT, "", NULL,
118 INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
119 if (!hFtp)
121 ok(GetLastError() == ERROR_INTERNET_LOGIN_FAILURE,
122 "Expected ERROR_INTERNET_LOGIN_FAILURE, got %d\n", GetLastError());
124 else
126 ok(GetLastError() == ERROR_SUCCESS,
127 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
131 static void test_createdir(HINTERNET hFtp, HINTERNET hConnect)
133 BOOL bRet;
135 /* Invalid internet handle, the other is a valid parameter */
136 SetLastError(0xdeadbeef);
137 bRet = FtpCreateDirectoryA(NULL, "new_directory_deadbeef");
138 ok ( bRet == FALSE, "Expected FtpCreateDirectoryA to fail\n");
139 ok ( GetLastError() == ERROR_INVALID_HANDLE,
140 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
142 /* No directory-name */
143 SetLastError(0xdeadbeef);
144 bRet = FtpCreateDirectoryA(hFtp, NULL);
145 ok ( bRet == FALSE, "Expected FtpCreateDirectoryA to fail\n");
146 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
147 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
149 /* Parameters are OK, but we shouldn't be allowed to create the directory */
150 SetLastError(0xdeadbeef);
151 bRet = FtpCreateDirectoryA(hFtp, "new_directory_deadbeef");
152 ok ( bRet == FALSE, "Expected FtpCreateDirectoryA to fail\n");
153 ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR,
154 "Expected ERROR_INTERNET_EXTENDED_ERROR, got %d\n", GetLastError());
156 /* One small test to show that handle type is checked before parameters */
157 SetLastError(0xdeadbeef);
158 bRet = FtpCreateDirectoryA(hConnect, NULL);
159 ok ( bRet == FALSE, "Expected FtpCreateDirectoryA to fail\n");
160 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
161 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
163 SetLastError(0xdeadbeef);
164 bRet = FtpCreateDirectoryA(hConnect, "new_directory_deadbeef");
165 ok ( bRet == FALSE, "Expected FtpCreateDirectoryA to fail\n");
166 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
167 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
170 static void test_deletefile(HINTERNET hFtp, HINTERNET hConnect)
172 BOOL bRet;
174 /* Invalid internet handle, the other is a valid parameter */
175 SetLastError(0xdeadbeef);
176 bRet = FtpDeleteFileA(NULL, "non_existent_file_deadbeef");
177 ok ( bRet == FALSE, "Expected FtpDeleteFileA to fail\n");
178 ok ( GetLastError() == ERROR_INVALID_HANDLE,
179 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
181 /* No filename */
182 SetLastError(0xdeadbeef);
183 bRet = FtpDeleteFileA(hFtp, NULL);
184 ok ( bRet == FALSE, "Expected FtpDeleteFileA to fail\n");
185 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
186 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
188 /* Parameters are OK but remote file should not be there */
189 SetLastError(0xdeadbeef);
190 bRet = FtpDeleteFileA(hFtp, "non_existent_file_deadbeef");
191 ok ( bRet == FALSE, "Expected FtpDeleteFileA to fail\n");
192 ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR,
193 "Expected ERROR_INTERNET_EXTENDED_ERROR, got %d\n", GetLastError());
195 /* One small test to show that handle type is checked before parameters */
196 SetLastError(0xdeadbeef);
197 bRet = FtpDeleteFileA(hConnect, NULL);
198 ok ( bRet == FALSE, "Expected FtpDeleteFileA to fail\n");
199 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
200 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
202 SetLastError(0xdeadbeef);
203 bRet = FtpDeleteFileA(hConnect, "non_existent_file_deadbeef");
204 ok ( bRet == FALSE, "Expected FtpCreateDirectoryA to fail\n");
205 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
206 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
209 static void test_getfile(HINTERNET hFtp, HINTERNET hConnect)
211 BOOL bRet;
212 HANDLE hFile;
214 /* The order of checking is:
216 * All parameters except 'session handle' and 'condition flags'
217 * Session handle
218 * Session handle type
219 * Condition flags
222 /* Test to show the parameter checking order depends on the Windows version */
223 SetLastError(0xdeadbeef);
224 bRet = FtpGetFileA(NULL, NULL, "should_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
225 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
226 ok ( GetLastError() == ERROR_INVALID_HANDLE ||
227 GetLastError() == ERROR_INVALID_PARAMETER,
228 "Expected ERROR_INVALID_HANDLE (win98) or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
230 /* Test to show session handle is checked before 'condition flags' */
231 SetLastError(0xdeadbeef);
232 bRet = FtpGetFileA(NULL, "welcome.msg", "should_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, 5, 0);
233 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
234 ok ( GetLastError() == ERROR_INVALID_HANDLE,
235 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
237 /* Make sure we start clean */
239 DeleteFileA("should_be_non_existing_deadbeef");
240 DeleteFileA("should_also_be_non_existing_deadbeef");
242 /* No remote file */
243 SetLastError(0xdeadbeef);
244 bRet = FtpGetFileA(hFtp, NULL, "should_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
245 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
246 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
247 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
248 ok (GetFileAttributesA("should_be_non_existing_deadbeef") == INVALID_FILE_ATTRIBUTES,
249 "Local file should not have been created\n");
250 DeleteFileA("should_be_non_existing_deadbeef");
252 /* No local file */
253 SetLastError(0xdeadbeef);
254 bRet = FtpGetFileA(hFtp, "welcome.msg", NULL, FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
255 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
256 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
257 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
259 /* Zero attributes */
260 bRet = FtpGetFileA(hFtp, "welcome.msg", "should_be_existing_non_deadbeef", FALSE, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0);
261 ok ( bRet == TRUE, "Expected FtpGetFileA to succeed\n");
262 ok (GetFileAttributesA("should_be_existing_non_deadbeef") != INVALID_FILE_ATTRIBUTES,
263 "Local file should have been created\n");
264 DeleteFileA("should_be_existing_non_deadbeef");
266 /* Illegal condition flags */
267 SetLastError(0xdeadbeef);
268 bRet = FtpGetFileA(hFtp, "welcome.msg", "should_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, 0xffffffff, 0);
269 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
270 ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR || GetLastError() == ERROR_INVALID_PARAMETER,
271 "Expected ERROR_INTERNET_EXTENDED_ERROR or ERROR_INVALID_PARAMETER (win98), got %d\n", GetLastError());
272 ok (GetFileAttributesA("should_be_non_existing_deadbeef") == INVALID_FILE_ATTRIBUTES,
273 "Local file should not have been created\n");
274 DeleteFileA("should_be_non_existing_deadbeef");
276 /* Remote file doesn't exist (and local doesn't exist as well) */
277 SetLastError(0xdeadbeef);
278 bRet = FtpGetFileA(hFtp, "should_be_non_existing_deadbeef", "should_also_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
279 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
280 ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR,
281 "Expected ERROR_INTERNET_EXTENDED_ERROR, got %d\n", GetLastError());
282 /* Currently Wine always creates the local file (even on failure) which is not correct, hence the test */
283 ok (GetFileAttributesA("should_also_be_non_existing_deadbeef") == INVALID_FILE_ATTRIBUTES,
284 "Local file should not have been created\n");
286 DeleteFileA("should_also_be_non_existing_deadbeef");
288 /* Same call as the previous but now the local file does exists. Windows just removes the file if the call fails
289 * even if the local existed before!
292 /* Create a temporary local file */
293 SetLastError(0xdeadbeef);
294 hFile = CreateFileA("should_also_be_non_existing_deadbeef", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
295 ok ( hFile != NULL, "Error creating a local file : %d\n", GetLastError());
296 CloseHandle(hFile);
297 SetLastError(0xdeadbeef);
298 bRet = FtpGetFileA(hFtp, "should_be_non_existing_deadbeef", "should_also_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
299 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
300 ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR,
301 "Expected ERROR_INTERNET_EXTENDED_ERROR, got %d\n", GetLastError());
302 /* Currently Wine always creates the local file (even on failure) which is not correct, hence the test */
303 ok (GetFileAttributesA("should_also_be_non_existing_deadbeef") == INVALID_FILE_ATTRIBUTES,
304 "Local file should not have been created\n");
306 DeleteFileA("should_also_be_non_existing_deadbeef");
308 /* This one should succeed */
309 SetLastError(0xdeadbeef);
310 bRet = FtpGetFileA(hFtp, "welcome.msg", "should_be_existing_non_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
311 ok ( bRet == TRUE, "Expected FtpGetFileA to fail\n");
312 ok ( GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
314 if (GetFileAttributesA("should_be_existing_non_deadbeef") != INVALID_FILE_ATTRIBUTES)
316 /* Should succeed as fFailIfExists is set to FALSE (meaning don't fail if local file exists) */
317 SetLastError(0xdeadbeef);
318 bRet = FtpGetFileA(hFtp, "welcome.msg", "should_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
319 ok ( bRet == TRUE, "Expected FtpGetFileA to succeed\n");
320 ok ( GetLastError() == ERROR_SUCCESS,
321 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
323 /* Should fail as fFailIfExists is set to TRUE */
324 SetLastError(0xdeadbeef);
325 bRet = FtpGetFileA(hFtp, "welcome.msg", "should_be_non_existing_deadbeef", TRUE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
326 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
327 ok ( GetLastError() == ERROR_FILE_EXISTS,
328 "Expected ERROR_FILE_EXISTS, got %d\n", GetLastError());
330 /* Prove that the existence of the local file is checked first (or at least reported last) */
331 SetLastError(0xdeadbeef);
332 bRet = FtpGetFileA(hFtp, "should_be_non_existing_deadbeef", "should_be_non_existing_deadbeef", TRUE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
333 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
334 ok ( GetLastError() == ERROR_FILE_EXISTS,
335 "Expected ERROR_FILE_EXISTS, got %d\n", GetLastError());
337 DeleteFileA("should_be_existing_non_deadbeef");
340 /* Test to show the parameter checking order depends on the Windows version */
341 SetLastError(0xdeadbeef);
342 bRet = FtpGetFileA(hConnect, NULL, "should_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
343 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
344 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE ||
345 GetLastError() == ERROR_INVALID_PARAMETER,
346 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE (win98) or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
348 /* Test to show that 'session handle type' is checked before 'condition flags' */
349 SetLastError(0xdeadbeef);
350 bRet = FtpGetFileA(hConnect, "welcome.msg", "should_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, 5, 0);
351 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
352 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
353 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
355 SetLastError(0xdeadbeef);
356 bRet = FtpGetFileA(hConnect, "should_be_non_existing_deadbeef", "should_be_non_existing_deadbeef", TRUE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
357 ok ( bRet == FALSE, "Expected FtpGetFileA to fail\n");
358 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
359 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
362 static void trace_extended_error(DWORD error)
364 DWORD code, buflen = 0;
366 if (error != ERROR_INTERNET_EXTENDED_ERROR) return;
367 if (!InternetGetLastResponseInfoA(&code, NULL, &buflen) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
369 char *text = HeapAlloc(GetProcessHeap(), 0, ++buflen);
370 InternetGetLastResponseInfoA(&code, text, &buflen);
371 trace("%u %s\n", code, text);
372 HeapFree(GetProcessHeap(), 0, text);
376 static void test_openfile(HINTERNET hFtp, HINTERNET hConnect)
378 HINTERNET hOpenFile;
380 /* Invalid internet handle, the rest are valid parameters */
381 SetLastError(0xdeadbeef);
382 hOpenFile = FtpOpenFileA(NULL, "welcome.msg", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
383 ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
384 ok ( GetLastError() == ERROR_INVALID_HANDLE,
385 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
386 InternetCloseHandle(hOpenFile); /* Just in case */
388 /* No filename */
389 SetLastError(0xdeadbeef);
390 hOpenFile = FtpOpenFileA(hFtp, NULL, GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
391 ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
392 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
393 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
394 InternetCloseHandle(hOpenFile); /* Just in case */
396 /* Illegal access flags */
397 SetLastError(0xdeadbeef);
398 hOpenFile = FtpOpenFileA(hFtp, "welcome.msg", 0, FTP_TRANSFER_TYPE_ASCII, 0);
399 ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
400 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
401 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
402 InternetCloseHandle(hOpenFile); /* Just in case */
404 /* Illegal combination of access flags */
405 SetLastError(0xdeadbeef);
406 hOpenFile = FtpOpenFileA(hFtp, "welcome.msg", GENERIC_READ|GENERIC_WRITE, FTP_TRANSFER_TYPE_ASCII, 0);
407 ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
408 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
409 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
410 InternetCloseHandle(hOpenFile); /* Just in case */
412 /* Illegal condition flags */
413 SetLastError(0xdeadbeef);
414 hOpenFile = FtpOpenFileA(hFtp, "welcome.msg", GENERIC_READ, 0xffffffff, 0);
415 ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
416 ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR || GetLastError() == ERROR_INVALID_PARAMETER,
417 "Expected ERROR_INTERNET_EXTENDED_ERROR or ERROR_INVALID_PARAMETER (win98), got %d\n", GetLastError());
418 InternetCloseHandle(hOpenFile); /* Just in case */
420 SetLastError(0xdeadbeef);
421 hOpenFile = FtpOpenFileA(hFtp, "welcome.msg", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
422 ok ( hOpenFile != NULL, "Expected FtpOpenFileA to succeed\n");
423 ok ( GetLastError() == ERROR_SUCCESS ||
424 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* Win98 */
425 "Expected ERROR_SUCCESS, got %u\n", GetLastError());
427 if (hOpenFile)
429 BOOL bRet;
430 DWORD error;
431 HINTERNET hOpenFile2;
432 HANDLE hFile;
434 /* We have a handle so all ftp calls should fail (TODO: Put all ftp-calls in here) */
435 SetLastError(0xdeadbeef);
436 bRet = FtpCreateDirectoryA(hFtp, "new_directory_deadbeef");
437 error = GetLastError();
438 ok ( bRet == FALSE, "Expected FtpCreateDirectoryA to fail\n");
439 ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
440 "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
441 trace_extended_error(error);
443 SetLastError(0xdeadbeef);
444 bRet = FtpDeleteFileA(hFtp, "non_existent_file_deadbeef");
445 error = GetLastError();
446 ok ( bRet == FALSE, "Expected FtpDeleteFileA to fail\n");
447 ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
448 "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
449 trace_extended_error(error);
451 SetLastError(0xdeadbeef);
452 bRet = FtpGetFileA(hFtp, "welcome.msg", "should_be_non_existing_deadbeef", FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
453 error = GetLastError();
454 ok ( bRet == FALSE || broken(bRet == TRUE), "Expected FtpGetFileA to fail\n");
455 ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_SUCCESS),
456 "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
457 DeleteFileA("should_be_non_existing_deadbeef"); /* Just in case */
459 SetLastError(0xdeadbeef);
460 hOpenFile2 = FtpOpenFileA(hFtp, "welcome.msg", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
461 error = GetLastError();
462 ok ( bRet == FALSE || broken(bRet == TRUE), "Expected FtpOpenFileA to fail\n");
463 ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_SUCCESS),
464 "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
465 InternetCloseHandle(hOpenFile2); /* Just in case */
467 /* Create a temporary local file */
468 SetLastError(0xdeadbeef);
469 hFile = CreateFileA("now_existing_local", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
470 ok ( hFile != NULL, "Error creating a local file : %d\n", GetLastError());
471 CloseHandle(hFile);
472 SetLastError(0xdeadbeef);
473 bRet = FtpPutFileA(hFtp, "now_existing_local", "non_existing_remote", FTP_TRANSFER_TYPE_UNKNOWN, 0);
474 error = GetLastError();
475 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
476 ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
477 "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
478 DeleteFileA("now_existing_local");
480 SetLastError(0xdeadbeef);
481 bRet = FtpRemoveDirectoryA(hFtp, "should_be_non_existing_deadbeef_dir");
482 error = GetLastError();
483 ok ( bRet == FALSE, "Expected FtpRemoveDirectoryA to fail\n");
484 ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
485 "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
487 SetLastError(0xdeadbeef);
488 bRet = FtpRenameFileA(hFtp , "should_be_non_existing_deadbeef", "new");
489 error = GetLastError();
490 ok ( bRet == FALSE, "Expected FtpRenameFileA to fail\n");
491 ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
492 "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error);
495 InternetCloseHandle(hOpenFile);
497 /* One small test to show that handle type is checked before parameters */
498 SetLastError(0xdeadbeef);
499 hOpenFile = FtpOpenFileA(hConnect, "welcome.msg", GENERIC_READ, 5, 0);
500 ok ( !hOpenFile, "Expected FtpOpenFileA to fail\n");
501 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
502 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
503 InternetCloseHandle(hOpenFile); /* Just in case */
505 SetLastError(0xdeadbeef);
506 hOpenFile = FtpOpenFileA(hConnect, "welcome.msg", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
507 ok ( hOpenFile == NULL, "Expected FtpOpenFileA to fail\n");
508 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
509 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
511 InternetCloseHandle(hOpenFile); /* Just in case */
514 static void test_putfile(HINTERNET hFtp, HINTERNET hConnect)
516 BOOL bRet;
517 HANDLE hFile;
519 /* The order of checking is:
521 * All parameters except 'session handle' and 'condition flags'
522 * Session handle
523 * Session handle type
524 * Condition flags
527 /* Test to show the parameter checking order depends on the Windows version */
528 SetLastError(0xdeadbeef);
529 bRet = FtpPutFileA(NULL, NULL, "non_existing_remote", FTP_TRANSFER_TYPE_UNKNOWN, 0);
530 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
531 ok ( GetLastError() == ERROR_INVALID_HANDLE ||
532 GetLastError() == ERROR_INVALID_PARAMETER,
533 "Expected ERROR_INVALID_HANDLE (win98) or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
535 /* Test to show session handle is checked before 'condition flags' */
536 SetLastError(0xdeadbeef);
537 bRet = FtpPutFileA(NULL, "non_existing_local", "non_existing_remote", 5, 0);
538 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
539 ok ( GetLastError() == ERROR_INVALID_HANDLE,
540 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
542 /* Start clean */
543 DeleteFileA("non_existing_local");
545 /* No local file given */
546 SetLastError(0xdeadbeef);
547 bRet = FtpPutFileA(hFtp, NULL, "non_existing_remote", FTP_TRANSFER_TYPE_UNKNOWN, 0);
548 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
549 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
550 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
552 /* No remote file given */
553 SetLastError(0xdeadbeef);
554 bRet = FtpPutFileA(hFtp, "non_existing_local", NULL, FTP_TRANSFER_TYPE_UNKNOWN, 0);
555 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
556 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
557 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
559 /* Illegal condition flags */
560 SetLastError(0xdeadbeef);
561 bRet = FtpPutFileA(hFtp, "non_existing_local", "non_existing_remote", 5, 0);
562 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
563 ok ( GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_PARAMETER,
564 "Expected ERROR_FILE_NOT_FOUND or ERROR_INVALID_PARAMETER (win98), got %d\n", GetLastError());
566 /* Parameters are OK but local file doesn't exist */
567 SetLastError(0xdeadbeef);
568 bRet = FtpPutFileA(hFtp, "non_existing_local", "non_existing_remote", FTP_TRANSFER_TYPE_UNKNOWN, 0);
569 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
570 ok ( GetLastError() == ERROR_FILE_NOT_FOUND,
571 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
573 /* Create a temporary local file */
574 SetLastError(0xdeadbeef);
575 hFile = CreateFileA("now_existing_local", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
576 ok ( hFile != NULL, "Error creating a local file : %d\n", GetLastError());
577 CloseHandle(hFile);
579 /* Local file exists but we shouldn't be allowed to 'put' the file */
580 SetLastError(0xdeadbeef);
581 bRet = FtpPutFileA(hFtp, "now_existing_local", "non_existing_remote", FTP_TRANSFER_TYPE_UNKNOWN, 0);
582 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
583 ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR,
584 "Expected ERROR_INTERNET_EXTENDED_ERROR, got %d\n", GetLastError());
586 DeleteFileA("now_existing_local");
588 /* Test to show the parameter checking order depends on the Windows version */
589 SetLastError(0xdeadbeef);
590 bRet = FtpPutFileA(hConnect, NULL, "non_existing_remote", FTP_TRANSFER_TYPE_UNKNOWN, 0);
591 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
592 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE ||
593 GetLastError() == ERROR_INVALID_PARAMETER,
594 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE (win98) or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
596 /* Test to show that 'session handle type' is checked before 'condition flags' */
597 SetLastError(0xdeadbeef);
598 bRet = FtpPutFileA(hConnect, "non_existing_local", "non_existing_remote", 5, 0);
599 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
600 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
601 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
603 SetLastError(0xdeadbeef);
604 bRet = FtpPutFileA(hConnect, "non_existing_local", "non_existing_remote", FTP_TRANSFER_TYPE_UNKNOWN, 0);
605 ok ( bRet == FALSE, "Expected FtpPutFileA to fail\n");
606 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
607 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
610 static void test_removedir(HINTERNET hFtp, HINTERNET hConnect)
612 BOOL bRet;
614 /* Invalid internet handle, the other is a valid parameter */
615 SetLastError(0xdeadbeef);
616 bRet = FtpRemoveDirectoryA(NULL, "should_be_non_existing_deadbeef_dir");
617 ok ( bRet == FALSE, "Expected FtpRemoveDirectoryA to fail\n");
618 ok ( GetLastError() == ERROR_INVALID_HANDLE,
619 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
621 /* No remote directory given */
622 SetLastError(0xdeadbeef);
623 bRet = FtpRemoveDirectoryA(hFtp, NULL);
624 ok ( bRet == FALSE, "Expected FtpRemoveDirectoryA to fail\n");
625 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
626 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
628 /* Remote directory doesn't exist */
629 SetLastError(0xdeadbeef);
630 bRet = FtpRemoveDirectoryA(hFtp, "should_be_non_existing_deadbeef_dir");
631 ok ( bRet == FALSE, "Expected FtpRemoveDirectoryA to fail\n");
632 ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR,
633 "Expected ERROR_INTERNET_EXTENDED_ERROR, got %d\n", GetLastError());
635 /* We shouldn't be allowed to remove that directory */
636 SetLastError(0xdeadbeef);
637 bRet = FtpRemoveDirectoryA(hFtp, "pub");
638 ok ( bRet == FALSE, "Expected FtpRemoveDirectoryA to fail\n");
639 ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR,
640 "Expected ERROR_INTERNET_EXTENDED_ERROR, got %d\n", GetLastError());
642 /* One small test to show that handle type is checked before parameters */
643 SetLastError(0xdeadbeef);
644 bRet = FtpRemoveDirectoryA(hConnect, NULL);
645 ok ( bRet == FALSE, "Expected FtpRemoveDirectoryA to fail\n");
646 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
647 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
649 SetLastError(0xdeadbeef);
650 bRet = FtpRemoveDirectoryA(hConnect, "should_be_non_existing_deadbeef_dir");
651 ok ( bRet == FALSE, "Expected FtpRemoveDirectoryA to fail\n");
652 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
653 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
656 static void test_renamefile(HINTERNET hFtp, HINTERNET hConnect)
658 BOOL bRet;
660 /* Invalid internet handle, the rest are valid parameters */
661 SetLastError(0xdeadbeef);
662 bRet = FtpRenameFileA(NULL , "should_be_non_existing_deadbeef", "new");
663 ok ( bRet == FALSE, "Expected FtpRenameFileA to fail\n");
664 ok ( GetLastError() == ERROR_INVALID_HANDLE,
665 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
667 /* No 'existing' file */
668 SetLastError(0xdeadbeef);
669 bRet = FtpRenameFileA(hFtp , NULL, "new");
670 ok ( bRet == FALSE, "Expected FtpRenameFileA to fail\n");
671 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
672 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
674 /* No new file */
675 SetLastError(0xdeadbeef);
676 bRet = FtpRenameFileA(hFtp , "should_be_non_existing_deadbeef", NULL);
677 ok ( bRet == FALSE, "Expected FtpRenameFileA to fail\n");
678 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
679 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
681 /* Existing file shouldn't be there */
682 SetLastError(0xdeadbeef);
683 bRet = FtpRenameFileA(hFtp , "should_be_non_existing_deadbeef", "new");
684 ok ( bRet == FALSE, "Expected FtpRenameFileA to fail\n");
685 ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR,
686 "Expected ERROR_INTERNET_EXTENDED_ERROR, got %d\n", GetLastError());
688 /* One small test to show that handle type is checked before parameters */
689 SetLastError(0xdeadbeef);
690 bRet = FtpRenameFileA(hConnect , "should_be_non_existing_deadbeef", NULL);
691 ok ( bRet == FALSE, "Expected FtpRenameFileA to fail\n");
692 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
693 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
695 SetLastError(0xdeadbeef);
696 bRet = FtpRenameFileA(hConnect , "should_be_non_existing_deadbeef", "new");
697 ok ( bRet == FALSE, "Expected FtpRenameFileA to fail\n");
698 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
699 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError());
702 static void test_command(HINTERNET hFtp, HINTERNET hConnect)
704 BOOL ret;
705 DWORD error;
706 unsigned int i;
707 static const struct
709 BOOL ret;
710 DWORD error;
711 const char *cmd;
713 command_test[] =
715 { FALSE, ERROR_INVALID_PARAMETER, NULL },
716 { FALSE, ERROR_INVALID_PARAMETER, "" },
717 { FALSE, ERROR_INTERNET_EXTENDED_ERROR, "HELO" },
718 { FALSE, ERROR_INTERNET_EXTENDED_ERROR, "SIZE " },
719 { FALSE, ERROR_INTERNET_EXTENDED_ERROR, " SIZE" },
720 { FALSE, ERROR_INTERNET_EXTENDED_ERROR, "SIZE " },
721 { FALSE, ERROR_INTERNET_EXTENDED_ERROR, "SIZE /welcome.msg /welcome.msg" },
722 { FALSE, ERROR_INTERNET_EXTENDED_ERROR, "SIZE /welcome.msg" },
723 { FALSE, ERROR_INTERNET_EXTENDED_ERROR, "SIZE /welcome.msg " },
724 { TRUE, ERROR_SUCCESS, "SIZE\t/welcome.msg" },
725 { TRUE, ERROR_SUCCESS, "SIZE /welcome.msg" },
726 { FALSE, ERROR_INTERNET_EXTENDED_ERROR, "PWD /welcome.msg" },
727 { TRUE, ERROR_SUCCESS, "PWD" }
730 if (!pFtpCommandA)
732 win_skip("FtpCommandA() is not available. Skipping the Ftp command tests\n");
733 return;
736 for (i = 0; i < sizeof(command_test) / sizeof(command_test[0]); i++)
738 SetLastError(0xdeadbeef);
739 ret = pFtpCommandA(hFtp, FALSE, FTP_TRANSFER_TYPE_ASCII, command_test[i].cmd, 0, NULL);
740 error = GetLastError();
742 ok(ret == command_test[i].ret, "%d: expected FtpCommandA to %s\n", i, command_test[i].ret ? "succeed" : "fail");
743 ok(error == command_test[i].error, "%d: expected error %u, got %u\n", i, command_test[i].error, error);
747 static void test_find_first_file(HINTERNET hFtp, HINTERNET hConnect)
749 WIN32_FIND_DATAA findData;
750 HINTERNET hSearch;
751 HINTERNET hSearch2;
752 HINTERNET hOpenFile;
753 DWORD error;
754 BOOL success;
756 /* NULL as the search file ought to return the first file in the directory */
757 SetLastError(0xdeadbeef);
758 hSearch = FtpFindFirstFileA(hFtp, NULL, &findData, 0, 0);
759 ok ( hSearch != NULL, "Expected FtpFindFirstFileA to pass\n" );
761 /* This should fail as the previous handle wasn't closed */
762 SetLastError(0xdeadbeef);
763 hSearch2 = FtpFindFirstFileA(hFtp, "welcome.msg", &findData, 0, 0);
764 todo_wine ok ( hSearch2 == NULL, "Expected FtpFindFirstFileA to fail\n" );
765 todo_wine ok ( GetLastError() == ERROR_FTP_TRANSFER_IN_PROGRESS,
766 "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", GetLastError() );
767 InternetCloseHandle(hSearch2); /* Just in case */
769 InternetCloseHandle(hSearch);
771 /* Try a valid filename in a subdirectory search */
772 SetLastError(0xdeadbeef);
773 hSearch = FtpFindFirstFileA(hFtp, "pub/wine", &findData, 0, 0);
774 ok( hSearch != NULL, "Expected FtpFindFirstFileA to pass\n" );
775 InternetCloseHandle(hSearch);
777 /* Try a valid filename in a subdirectory wildcard search */
778 SetLastError(0xdeadbeef);
779 hSearch = FtpFindFirstFileA(hFtp, "pub/w*", &findData, 0, 0);
780 ok( hSearch != NULL, "Expected FtpFindFirstFileA to pass\n" );
781 InternetCloseHandle(hSearch);
783 /* Try an invalid wildcard search */
784 SetLastError(0xdeadbeef);
785 hSearch = FtpFindFirstFileA(hFtp, "*/w*", &findData, 0, 0);
786 ok ( hSearch == NULL, "Expected FtpFindFirstFileA to fail\n" );
787 InternetCloseHandle(hSearch); /* Just in case */
789 /* change current directory, and repeat those tests - this shows
790 * that the search string is interpreted as relative directory. */
791 success = FtpSetCurrentDirectoryA(hFtp, "pub");
792 ok( success, "Expected FtpSetCurrentDirectory to succeed\n" );
794 SetLastError(0xdeadbeef);
795 hSearch = FtpFindFirstFileA(hFtp, "wine", &findData, 0, 0);
796 ok( hSearch != NULL, "Expected FtpFindFirstFileA to pass\n" );
797 InternetCloseHandle(hSearch);
799 SetLastError(0xdeadbeef);
800 hSearch = FtpFindFirstFileA(hFtp, "w*", &findData, 0, 0);
801 ok( hSearch != NULL, "Expected FtpFindFirstFileA to pass\n" );
802 InternetCloseHandle(hSearch);
804 success = FtpSetCurrentDirectoryA(hFtp, "..");
805 ok( success, "Expected FtpSetCurrentDirectory to succeed\n" );
807 /* Try FindFirstFile between FtpOpenFile and InternetCloseHandle */
808 SetLastError(0xdeadbeef);
809 hOpenFile = FtpOpenFileA(hFtp, "welcome.msg", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, 0);
810 ok ( hOpenFile != NULL, "Expected FtpOpenFileA to succeed\n" );
811 ok ( GetLastError() == ERROR_SUCCESS ||
812 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* Win98 */
813 "Expected ERROR_SUCCESS, got %u\n", GetLastError() );
815 /* This should fail as the OpenFile handle wasn't closed */
816 SetLastError(0xdeadbeef);
817 hSearch = FtpFindFirstFileA(hFtp, "welcome.msg", &findData, 0, 0);
818 error = GetLastError();
819 ok ( hSearch == NULL || broken(hSearch != NULL), /* win2k */
820 "Expected FtpFindFirstFileA to fail\n" );
821 if (!hSearch)
822 ok ( error == ERROR_FTP_TRANSFER_IN_PROGRESS || broken(error == ERROR_INTERNET_EXTENDED_ERROR),
823 "Expected ERROR_FTP_TRANSFER_IN_PROGRESS, got %d\n", error );
824 else
826 ok( error == ERROR_SUCCESS, "wrong error %u on success\n", GetLastError() );
827 InternetCloseHandle(hSearch);
830 InternetCloseHandle(hOpenFile);
832 /* Test using a nonexistent filename */
833 SetLastError(0xdeadbeef);
834 hSearch = FtpFindFirstFileA(hFtp, "this_file_should_not_exist", &findData, 0, 0);
835 ok ( hSearch == NULL, "Expected FtpFindFirstFileA to fail\n" );
836 todo_wine ok ( GetLastError() == ERROR_INTERNET_EXTENDED_ERROR,
837 "Expected ERROR_INTERNET_EXTENDED_ERROR, got %d\n", GetLastError() );
838 InternetCloseHandle(hSearch); /* Just in case */
840 /* Test using a nonexistent filename and a wildcard */
841 SetLastError(0xdeadbeef);
842 hSearch = FtpFindFirstFileA(hFtp, "this_file_should_not_exist*", &findData, 0, 0);
843 ok ( hSearch == NULL, "Expected FtpFindFirstFileA to fail\n" );
844 todo_wine ok ( GetLastError() == ERROR_NO_MORE_FILES,
845 "Expected ERROR_NO_MORE_FILES, got %d\n", GetLastError() );
846 InternetCloseHandle(hSearch); /* Just in case */
848 /* Test using an invalid handle type */
849 SetLastError(0xdeadbeef);
850 hSearch = FtpFindFirstFileA(hConnect, "welcome.msg", &findData, 0, 0);
851 ok ( hSearch == NULL, "Expected FtpFindFirstFileA to fail\n" );
852 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
853 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %d\n", GetLastError() );
854 InternetCloseHandle(hSearch); /* Just in case */
857 static void test_get_current_dir(HINTERNET hFtp, HINTERNET hConnect)
859 BOOL bRet;
860 DWORD dwCurrentDirectoryLen = MAX_PATH;
861 CHAR lpszCurrentDirectory[MAX_PATH];
863 if (!pFtpCommandA)
865 win_skip("FtpCommandA() is not available. Skipping the Ftp get_current_dir tests\n");
866 return;
869 /* change directories to get a more interesting pwd */
870 bRet = pFtpCommandA(hFtp, FALSE, FTP_TRANSFER_TYPE_ASCII, "CWD pub/", 0, NULL);
871 if(bRet == FALSE)
873 skip("Failed to change directories in test_get_current_dir(HINTERNET hFtp).\n");
874 return;
877 /* test with all NULL arguments */
878 SetLastError(0xdeadbeef);
879 bRet = FtpGetCurrentDirectoryA( NULL, NULL, 0 );
880 ok ( bRet == FALSE, "Expected FtpGetCurrentDirectoryA to fail\n" );
881 ok ( GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got: %d\n", GetLastError());
883 /* test with NULL parameters instead of expected LPSTR/LPDWORD */
884 SetLastError(0xdeadbeef);
885 bRet = FtpGetCurrentDirectoryA( hFtp, NULL, 0 );
886 ok ( bRet == FALSE, "Expected FtpGetCurrentDirectoryA to fail\n" );
887 ok ( GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got: %d\n", GetLastError());
889 /* test with no valid handle and valid parameters */
890 SetLastError(0xdeadbeef);
891 bRet = FtpGetCurrentDirectoryA( NULL, lpszCurrentDirectory, &dwCurrentDirectoryLen );
892 ok ( bRet == FALSE, "Expected FtpGetCurrentDirectoryA to fail\n" );
893 ok ( GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got: %d\n", GetLastError());
895 /* test with invalid dwCurrentDirectory and all other parameters correct */
896 SetLastError(0xdeadbeef);
897 bRet = FtpGetCurrentDirectoryA( hFtp, lpszCurrentDirectory, 0 );
898 ok ( bRet == FALSE, "Expected FtpGetCurrentDirectoryA to fail\n" );
899 ok ( GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got: %d\n", GetLastError());
901 /* test with invalid lpszCurrentDirectory and all other parameters correct */
902 SetLastError(0xdeadbeef);
903 bRet = FtpGetCurrentDirectoryA( hFtp, NULL, &dwCurrentDirectoryLen );
904 ok ( bRet == FALSE, "Expected FtpGetCurrentDirectoryA to fail\n" );
905 ok ( GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got: %d\n", GetLastError());
907 /* test to show it checks the handle type */
908 SetLastError(0xdeadbeef);
909 bRet = FtpGetCurrentDirectoryA( hConnect, lpszCurrentDirectory, &dwCurrentDirectoryLen );
910 ok ( bRet == FALSE, "Expected FtpGetCurrentDirectoryA to fail\n" );
911 ok ( GetLastError() == ERROR_INTERNET_INCORRECT_HANDLE_TYPE,
912 "Expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got: %d\n", GetLastError());
914 /* test for the current directory with legitimate values */
915 SetLastError(0xdeadbeef);
916 bRet = FtpGetCurrentDirectoryA( hFtp, lpszCurrentDirectory, &dwCurrentDirectoryLen );
917 ok ( bRet == TRUE, "Expected FtpGetCurrentDirectoryA to pass\n" );
918 ok ( !strcmp(lpszCurrentDirectory, "/pub"), "Expected returned value \"%s\" to match \"/pub\"\n", lpszCurrentDirectory);
919 ok ( GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got: %d\n", GetLastError());
921 /* test for the current directory with a size only large enough to
922 * fit the string and not the null terminating character */
923 SetLastError(0xdeadbeef);
924 dwCurrentDirectoryLen = 4;
925 lpszCurrentDirectory[4] = 'a'; /* set position 4 of the array to something else to make sure a leftover \0 isn't fooling the test */
926 bRet = FtpGetCurrentDirectoryA( hFtp, lpszCurrentDirectory, &dwCurrentDirectoryLen );
927 ok ( bRet == FALSE, "Expected FtpGetCurrentDirectoryA to fail\n");
928 ok ( strcmp(lpszCurrentDirectory, "/pub"), "Expected returned value \"%s\" to not match \"/pub\"\n", lpszCurrentDirectory);
929 ok ( GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got: %d\n", GetLastError());
931 /* test for the current directory with a size large enough to store
932 * the expected string as well as the null terminating character */
933 SetLastError(0xdeadbeef);
934 dwCurrentDirectoryLen = 5;
935 bRet = FtpGetCurrentDirectoryA( hFtp, lpszCurrentDirectory, &dwCurrentDirectoryLen );
936 ok ( bRet == TRUE, "Expected FtpGetCurrentDirectoryA to pass\n");
937 ok ( !strcmp(lpszCurrentDirectory, "/pub"), "Expected returned value \"%s\" to match \"/pub\"\n", lpszCurrentDirectory);
938 ok ( GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got: %d\n", GetLastError());
941 static void WINAPI status_callback(HINTERNET handle, DWORD_PTR ctx, DWORD status, LPVOID info, DWORD info_len)
943 switch (status)
945 case INTERNET_STATUS_RESOLVING_NAME:
946 case INTERNET_STATUS_NAME_RESOLVED:
947 case INTERNET_STATUS_CONNECTING_TO_SERVER:
948 case INTERNET_STATUS_CONNECTED_TO_SERVER:
949 trace("%p %lx %u %s %u\n", handle, ctx, status, (char *)info, info_len);
950 break;
951 default:
952 break;
956 static void test_status_callbacks(HINTERNET hInternet)
958 INTERNET_STATUS_CALLBACK cb;
959 HINTERNET hFtp;
960 BOOL ret;
962 cb = pInternetSetStatusCallbackA(hInternet, status_callback);
963 ok(cb == NULL, "expected NULL got %p\n", cb);
965 hFtp = InternetConnectA(hInternet, "ftp.winehq.org", INTERNET_DEFAULT_FTP_PORT, "anonymous", NULL,
966 INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 1);
967 if (!hFtp)
969 skip("No ftp connection could be made to ftp.winehq.org %u\n", GetLastError());
970 return;
973 ret = InternetCloseHandle(hFtp);
974 ok(ret, "InternetCloseHandle failed %u\n", GetLastError());
976 cb = pInternetSetStatusCallbackA(hInternet, NULL);
977 ok(cb == status_callback, "expected check_status got %p\n", cb);
980 START_TEST(ftp)
982 HMODULE hWininet;
983 HANDLE hInternet, hFtp, hHttp;
985 hWininet = GetModuleHandleA("wininet.dll");
987 if(!GetProcAddress(hWininet, "InternetGetCookieExW")) {
988 win_skip("Too old IE (older than 6.0)\n");
989 return;
992 pFtpCommandA = (void*)GetProcAddress(hWininet, "FtpCommandA");
993 pInternetSetStatusCallbackA = (void*)GetProcAddress(hWininet, "InternetSetStatusCallbackA");
995 SetLastError(0xdeadbeef);
996 hInternet = InternetOpenA("winetest", 0, NULL, NULL, 0);
997 ok(hInternet != NULL, "InternetOpen failed: %u\n", GetLastError());
999 hFtp = InternetConnectA(hInternet, "ftp.winehq.org", INTERNET_DEFAULT_FTP_PORT, "anonymous", NULL, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
1000 if (!hFtp)
1002 InternetCloseHandle(hInternet);
1003 skip("No ftp connection could be made to ftp.winehq.org\n");
1004 return;
1006 hHttp = InternetConnectA(hInternet, "www.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1007 if (!hHttp)
1009 InternetCloseHandle(hFtp);
1010 InternetCloseHandle(hInternet);
1011 skip("No http connection could be made to www.winehq.org\n");
1012 return;
1015 /* The first call should always be a proper InternetOpen, if not
1016 * several calls will return ERROR_INTERNET_NOT_INITIALIZED when
1017 * all parameters are correct but no session handle is given. Whereas
1018 * the same call will return ERROR_INVALID_HANDLE if an InternetOpen
1019 * is done before.
1020 * The following test will show that behaviour, where the tests inside
1021 * the other sub-tests will show the other situation.
1023 test_getfile_no_open();
1024 test_connect(hInternet);
1025 test_createdir(hFtp, hHttp);
1026 test_deletefile(hFtp, hHttp);
1027 test_getfile(hFtp, hHttp);
1028 test_openfile(hFtp, hHttp);
1029 test_putfile(hFtp, hHttp);
1030 test_removedir(hFtp, hHttp);
1031 test_renamefile(hFtp, hHttp);
1032 test_command(hFtp, hHttp);
1033 test_find_first_file(hFtp, hHttp);
1034 test_get_current_dir(hFtp, hHttp);
1035 test_status_callbacks(hInternet);
1037 InternetCloseHandle(hHttp);
1038 InternetCloseHandle(hFtp);
1039 InternetCloseHandle(hInternet);