scrrun: Implement Reset() for folder collection.
[wine/multimedia.git] / dlls / scrrun / tests / filesystem.c
blobbe5131c79c342439a5f7c20c8ec9e0756db863ed
1 /*
3 * Copyright 2012 Alistair Leslie-Hughes
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define COBJMACROS
21 #include <stdio.h>
23 #include "windows.h"
24 #include "ole2.h"
25 #include "olectl.h"
26 #include "oleauto.h"
27 #include "dispex.h"
29 #include "wine/test.h"
31 #include "initguid.h"
32 #include "scrrun.h"
34 static IFileSystem3 *fs3;
36 static inline ULONG get_refcount(IUnknown *iface)
38 IUnknown_AddRef(iface);
39 return IUnknown_Release(iface);
42 #define GET_REFCOUNT(iface) \
43 get_refcount((IUnknown*)iface)
45 static void test_interfaces(void)
47 static const WCHAR nonexistent_dirW[] = {
48 'c', ':', '\\', 'N', 'o', 'n', 'e', 'x', 'i', 's', 't', 'e', 'n', 't', 0};
49 static const WCHAR pathW[] = {'p','a','t','h',0};
50 static const WCHAR file_kernel32W[] = {
51 '\\', 'k', 'e', 'r', 'n', 'e', 'l', '3', '2', '.', 'd', 'l', 'l', 0};
52 HRESULT hr;
53 IDispatch *disp;
54 IDispatchEx *dispex;
55 IObjectWithSite *site;
56 VARIANT_BOOL b;
57 BSTR path;
58 WCHAR windows_path[MAX_PATH];
59 WCHAR file_path[MAX_PATH];
61 IFileSystem3_QueryInterface(fs3, &IID_IDispatch, (void**)&disp);
63 GetSystemDirectoryW(windows_path, MAX_PATH);
64 lstrcpyW(file_path, windows_path);
65 lstrcatW(file_path, file_kernel32W);
67 hr = IDispatch_QueryInterface(disp, &IID_IObjectWithSite, (void**)&site);
68 ok(hr == E_NOINTERFACE, "got 0x%08x, expected 0x%08x\n", hr, E_NOINTERFACE);
70 hr = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
71 ok(hr == E_NOINTERFACE, "got 0x%08x, expected 0x%08x\n", hr, E_NOINTERFACE);
73 b = VARIANT_TRUE;
74 hr = IFileSystem3_FileExists(fs3, NULL, &b);
75 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
76 ok(b == VARIANT_FALSE, "got %x\n", b);
78 hr = IFileSystem3_FileExists(fs3, NULL, NULL);
79 ok(hr == E_POINTER, "got 0x%08x, expected 0x%08x\n", hr, E_POINTER);
81 path = SysAllocString(pathW);
82 b = VARIANT_TRUE;
83 hr = IFileSystem3_FileExists(fs3, path, &b);
84 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
85 ok(b == VARIANT_FALSE, "got %x\n", b);
86 SysFreeString(path);
88 path = SysAllocString(file_path);
89 b = VARIANT_FALSE;
90 hr = IFileSystem3_FileExists(fs3, path, &b);
91 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
92 ok(b == VARIANT_TRUE, "got %x\n", b);
93 SysFreeString(path);
95 path = SysAllocString(windows_path);
96 b = VARIANT_TRUE;
97 hr = IFileSystem3_FileExists(fs3, path, &b);
98 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
99 ok(b == VARIANT_FALSE, "got %x\n", b);
100 SysFreeString(path);
102 /* Folder Exists */
103 hr = IFileSystem3_FolderExists(fs3, NULL, NULL);
104 ok(hr == E_POINTER, "got 0x%08x, expected 0x%08x\n", hr, E_POINTER);
106 path = SysAllocString(windows_path);
107 hr = IFileSystem3_FolderExists(fs3, path, &b);
108 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
109 ok(b == VARIANT_TRUE, "Folder doesn't exists\n");
110 SysFreeString(path);
112 path = SysAllocString(nonexistent_dirW);
113 hr = IFileSystem3_FolderExists(fs3, path, &b);
114 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
115 ok(b == VARIANT_FALSE, "Folder exists\n");
116 SysFreeString(path);
118 path = SysAllocString(file_path);
119 hr = IFileSystem3_FolderExists(fs3, path, &b);
120 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
121 ok(b == VARIANT_FALSE, "Folder exists\n");
122 SysFreeString(path);
124 IDispatch_Release(disp);
127 static void test_createfolder(void)
129 HRESULT hr;
130 WCHAR pathW[MAX_PATH];
131 BSTR path;
132 IFolder *folder;
134 /* create existing directory */
135 GetCurrentDirectoryW(sizeof(pathW)/sizeof(WCHAR), pathW);
136 path = SysAllocString(pathW);
137 folder = (void*)0xdeabeef;
138 hr = IFileSystem3_CreateFolder(fs3, path, &folder);
139 ok(hr == CTL_E_FILEALREADYEXISTS, "got 0x%08x\n", hr);
140 ok(folder == NULL, "got %p\n", folder);
141 SysFreeString(path);
144 static void test_textstream(void)
146 static const WCHAR testfileW[] = {'t','e','s','t','f','i','l','e','.','t','x','t',0};
147 ITextStream *stream;
148 VARIANT_BOOL b;
149 HANDLE file;
150 HRESULT hr;
151 BSTR name, data;
153 file = CreateFileW(testfileW, GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
154 CloseHandle(file);
156 name = SysAllocString(testfileW);
157 b = VARIANT_FALSE;
158 hr = IFileSystem3_FileExists(fs3, name, &b);
159 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
160 ok(b == VARIANT_TRUE, "got %x\n", b);
162 hr = IFileSystem3_OpenTextFile(fs3, name, ForReading, VARIANT_FALSE, TristateFalse, &stream);
163 ok(hr == S_OK, "got 0x%08x\n", hr);
165 b = 10;
166 hr = ITextStream_get_AtEndOfStream(stream, &b);
167 todo_wine {
168 ok(hr == S_FALSE || broken(hr == S_OK), "got 0x%08x\n", hr);
169 ok(b == VARIANT_TRUE, "got 0x%x\n", b);
171 ITextStream_Release(stream);
173 hr = IFileSystem3_OpenTextFile(fs3, name, ForWriting, VARIANT_FALSE, TristateFalse, &stream);
174 ok(hr == S_OK, "got 0x%08x\n", hr);
176 b = 10;
177 hr = ITextStream_get_AtEndOfStream(stream, &b);
178 todo_wine {
179 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
180 ok(b == VARIANT_TRUE || broken(b == 10), "got 0x%x\n", b);
182 b = 10;
183 hr = ITextStream_get_AtEndOfLine(stream, &b);
184 todo_wine {
185 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
186 ok(b == VARIANT_FALSE || broken(b == 10), "got 0x%x\n", b);
188 hr = ITextStream_Read(stream, 1, &data);
189 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
191 hr = ITextStream_ReadLine(stream, &data);
192 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
194 hr = ITextStream_ReadAll(stream, &data);
195 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
197 ITextStream_Release(stream);
199 hr = IFileSystem3_OpenTextFile(fs3, name, ForAppending, VARIANT_FALSE, TristateFalse, &stream);
200 ok(hr == S_OK, "got 0x%08x\n", hr);
201 SysFreeString(name);
203 b = 10;
204 hr = ITextStream_get_AtEndOfStream(stream, &b);
205 todo_wine {
206 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
207 ok(b == VARIANT_TRUE || broken(b == 10), "got 0x%x\n", b);
209 b = 10;
210 hr = ITextStream_get_AtEndOfLine(stream, &b);
211 todo_wine {
212 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
213 ok(b == VARIANT_FALSE || broken(b == 10), "got 0x%x\n", b);
215 hr = ITextStream_Read(stream, 1, &data);
216 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
218 hr = ITextStream_ReadLine(stream, &data);
219 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
221 hr = ITextStream_ReadAll(stream, &data);
222 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
224 ITextStream_Release(stream);
226 DeleteFileW(testfileW);
229 static void test_GetFileVersion(void)
231 static const WCHAR k32W[] = {'\\','k','e','r','n','e','l','3','2','.','d','l','l',0};
232 static const WCHAR k33W[] = {'\\','k','e','r','n','e','l','3','3','.','d','l','l',0};
233 WCHAR pathW[MAX_PATH], filenameW[MAX_PATH];
234 BSTR path, version;
235 HRESULT hr;
237 GetSystemDirectoryW(pathW, sizeof(pathW)/sizeof(WCHAR));
239 lstrcpyW(filenameW, pathW);
240 lstrcatW(filenameW, k32W);
242 path = SysAllocString(filenameW);
243 hr = IFileSystem3_GetFileVersion(fs3, path, &version);
244 ok(hr == S_OK, "got 0x%08x\n", hr);
245 ok(*version != 0, "got %s\n", wine_dbgstr_w(version));
246 SysFreeString(version);
247 SysFreeString(path);
249 lstrcpyW(filenameW, pathW);
250 lstrcatW(filenameW, k33W);
252 path = SysAllocString(filenameW);
253 version = (void*)0xdeadbeef;
254 hr = IFileSystem3_GetFileVersion(fs3, path, &version);
255 ok(broken(hr == S_OK) || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "got 0x%08x\n", hr);
256 if (hr == S_OK)
258 ok(*version == 0, "got %s\n", wine_dbgstr_w(version));
259 SysFreeString(version);
261 else
262 ok(version == (void*)0xdeadbeef, "got %p\n", version);
263 SysFreeString(path);
266 static void test_GetParentFolderName(void)
268 static const WCHAR path1[] = {'a',0};
269 static const WCHAR path2[] = {'a','/','a','/','a',0};
270 static const WCHAR path3[] = {'a','\\','a','\\','a',0};
271 static const WCHAR path4[] = {'a','/','a','/','/','\\','\\',0};
272 static const WCHAR path5[] = {'c',':','\\','\\','a',0};
273 static const WCHAR path6[] = {'a','c',':','\\','a',0};
274 static const WCHAR result2[] = {'a','/','a',0};
275 static const WCHAR result3[] = {'a','\\','a',0};
276 static const WCHAR result4[] = {'a',0};
277 static const WCHAR result5[] = {'c',':','\\',0};
278 static const WCHAR result6[] = {'a','c',':',0};
280 static const struct {
281 const WCHAR *path;
282 const WCHAR *result;
283 } tests[] = {
284 {NULL, NULL},
285 {path1, NULL},
286 {path2, result2},
287 {path3, result3},
288 {path4, result4},
289 {path5, result5},
290 {path6, result6}
293 BSTR path, result;
294 HRESULT hr;
295 int i;
297 hr = IFileSystem3_GetParentFolderName(fs3, NULL, NULL);
298 ok(hr == E_POINTER, "GetParentFolderName returned %x, expected E_POINTER\n", hr);
300 for(i=0; i<sizeof(tests)/sizeof(tests[0]); i++) {
301 result = (BSTR)0xdeadbeef;
302 path = tests[i].path ? SysAllocString(tests[i].path) : NULL;
303 hr = IFileSystem3_GetParentFolderName(fs3, path, &result);
304 ok(hr == S_OK, "%d) GetParentFolderName returned %x, expected S_OK\n", i, hr);
305 if(!tests[i].result)
306 ok(!result, "%d) result = %s\n", i, wine_dbgstr_w(result));
307 else
308 ok(!lstrcmpW(result, tests[i].result), "%d) result = %s\n", i, wine_dbgstr_w(result));
309 SysFreeString(path);
310 SysFreeString(result);
314 static void test_GetFileName(void)
316 static const WCHAR path1[] = {'a',0};
317 static const WCHAR path2[] = {'a','/','a','.','b',0};
318 static const WCHAR path3[] = {'a','\\',0};
319 static const WCHAR path4[] = {'c',':',0};
320 static const WCHAR path5[] = {'/','\\',0};
321 static const WCHAR result2[] = {'a','.','b',0};
322 static const WCHAR result3[] = {'a',0};
324 static const struct {
325 const WCHAR *path;
326 const WCHAR *result;
327 } tests[] = {
328 {NULL, NULL},
329 {path1, path1},
330 {path2, result2},
331 {path3, result3},
332 {path4, NULL},
333 {path5, NULL}
336 BSTR path, result;
337 HRESULT hr;
338 int i;
340 hr = IFileSystem3_GetFileName(fs3, NULL, NULL);
341 ok(hr == E_POINTER, "GetFileName returned %x, expected E_POINTER\n", hr);
343 for(i=0; i<sizeof(tests)/sizeof(tests[0]); i++) {
344 result = (BSTR)0xdeadbeef;
345 path = tests[i].path ? SysAllocString(tests[i].path) : NULL;
346 hr = IFileSystem3_GetFileName(fs3, path, &result);
347 ok(hr == S_OK, "%d) GetFileName returned %x, expected S_OK\n", i, hr);
348 if(!tests[i].result)
349 ok(!result, "%d) result = %s\n", i, wine_dbgstr_w(result));
350 else
351 ok(!lstrcmpW(result, tests[i].result), "%d) result = %s\n", i, wine_dbgstr_w(result));
352 SysFreeString(path);
353 SysFreeString(result);
357 static void test_GetBaseName(void)
359 static const WCHAR path1[] = {'a',0};
360 static const WCHAR path2[] = {'a','/','a','.','b','.','c',0};
361 static const WCHAR path3[] = {'a','.','b','\\',0};
362 static const WCHAR path4[] = {'c',':',0};
363 static const WCHAR path5[] = {'/','\\',0};
364 static const WCHAR path6[] = {'.','a',0};
365 static const WCHAR result1[] = {'a',0};
366 static const WCHAR result2[] = {'a','.','b',0};
367 static const WCHAR result6[] = {0};
369 static const struct {
370 const WCHAR *path;
371 const WCHAR *result;
372 } tests[] = {
373 {NULL, NULL},
374 {path1, result1},
375 {path2, result2},
376 {path3, result1},
377 {path4, NULL},
378 {path5, NULL},
379 {path6, result6}
382 BSTR path, result;
383 HRESULT hr;
384 int i;
386 hr = IFileSystem3_GetBaseName(fs3, NULL, NULL);
387 ok(hr == E_POINTER, "GetBaseName returned %x, expected E_POINTER\n", hr);
389 for(i=0; i<sizeof(tests)/sizeof(tests[0]); i++) {
390 result = (BSTR)0xdeadbeef;
391 path = tests[i].path ? SysAllocString(tests[i].path) : NULL;
392 hr = IFileSystem3_GetBaseName(fs3, path, &result);
393 ok(hr == S_OK, "%d) GetBaseName returned %x, expected S_OK\n", i, hr);
394 if(!tests[i].result)
395 ok(!result, "%d) result = %s\n", i, wine_dbgstr_w(result));
396 else
397 ok(!lstrcmpW(result, tests[i].result), "%d) result = %s\n", i, wine_dbgstr_w(result));
398 SysFreeString(path);
399 SysFreeString(result);
403 static void test_GetAbsolutePathName(void)
405 static const WCHAR dir1[] = {'t','e','s','t','_','d','i','r','1',0};
406 static const WCHAR dir2[] = {'t','e','s','t','_','d','i','r','2',0};
407 static const WCHAR dir_match1[] = {'t','e','s','t','_','d','i','r','*',0};
408 static const WCHAR dir_match2[] = {'t','e','s','t','_','d','i','*',0};
409 static const WCHAR cur_dir[] = {'.',0};
411 WIN32_FIND_DATAW fdata;
412 HANDLE find;
413 WCHAR buf[MAX_PATH], buf2[MAX_PATH];
414 BSTR path, result;
415 HRESULT hr;
417 hr = IFileSystem3_GetAbsolutePathName(fs3, NULL, NULL);
418 ok(hr == E_POINTER, "GetAbsolutePathName returned %x, expected E_POINTER\n", hr);
420 hr = IFileSystem3_GetAbsolutePathName(fs3, NULL, &result);
421 ok(hr == S_OK, "GetAbsolutePathName returned %x, expected S_OK\n", hr);
422 GetFullPathNameW(cur_dir, MAX_PATH, buf, NULL);
423 ok(!lstrcmpiW(buf, result), "result = %s, expected %s\n", wine_dbgstr_w(result), wine_dbgstr_w(buf));
424 SysFreeString(result);
426 find = FindFirstFileW(dir_match2, &fdata);
427 if(find != INVALID_HANDLE_VALUE) {
428 skip("GetAbsolutePathName tests\n");
429 FindClose(find);
430 return;
433 path = SysAllocString(dir_match1);
434 hr = IFileSystem3_GetAbsolutePathName(fs3, path, &result);
435 ok(hr == S_OK, "GetAbsolutePathName returned %x, expected S_OK\n", hr);
436 GetFullPathNameW(dir_match1, MAX_PATH, buf2, NULL);
437 ok(!lstrcmpiW(buf2, result), "result = %s, expected %s\n", wine_dbgstr_w(result), wine_dbgstr_w(buf2));
438 SysFreeString(result);
440 ok(CreateDirectoryW(dir1, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(dir1));
441 hr = IFileSystem3_GetAbsolutePathName(fs3, path, &result);
442 ok(hr == S_OK, "GetAbsolutePathName returned %x, expected S_OK\n", hr);
443 GetFullPathNameW(dir1, MAX_PATH, buf, NULL);
444 ok(!lstrcmpiW(buf, result) || broken(!lstrcmpiW(buf2, result)), "result = %s, expected %s\n",
445 wine_dbgstr_w(result), wine_dbgstr_w(buf));
446 SysFreeString(result);
448 ok(CreateDirectoryW(dir2, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(dir2));
449 hr = IFileSystem3_GetAbsolutePathName(fs3, path, &result);
450 ok(hr == S_OK, "GetAbsolutePathName returned %x, expected S_OK\n", hr);
451 if(!lstrcmpiW(buf, result) || !lstrcmpiW(buf2, result)) {
452 ok(!lstrcmpiW(buf, result) || broken(!lstrcmpiW(buf2, result)), "result = %s, expected %s\n",
453 wine_dbgstr_w(result), wine_dbgstr_w(buf));
454 }else {
455 GetFullPathNameW(dir2, MAX_PATH, buf, NULL);
456 ok(!lstrcmpiW(buf, result), "result = %s, expected %s\n",
457 wine_dbgstr_w(result), wine_dbgstr_w(buf));
459 SysFreeString(result);
461 SysFreeString(path);
462 path = SysAllocString(dir_match2);
463 hr = IFileSystem3_GetAbsolutePathName(fs3, path, &result);
464 ok(hr == S_OK, "GetAbsolutePathName returned %x, expected S_OK\n", hr);
465 GetFullPathNameW(dir_match2, MAX_PATH, buf, NULL);
466 ok(!lstrcmpiW(buf, result), "result = %s, expected %s\n", wine_dbgstr_w(result), wine_dbgstr_w(buf));
467 SysFreeString(result);
468 SysFreeString(path);
470 RemoveDirectoryW(dir1);
471 RemoveDirectoryW(dir2);
474 static void test_GetFile(void)
476 static const WCHAR get_file[] = {'g','e','t','_','f','i','l','e','.','t','s','t',0};
478 BSTR path = SysAllocString(get_file);
479 FileAttribute fa;
480 VARIANT size;
481 DWORD gfa;
482 IFile *file;
483 HRESULT hr;
484 HANDLE hf;
486 hr = IFileSystem3_GetFile(fs3, path, NULL);
487 ok(hr == E_POINTER, "GetFile returned %x, expected E_POINTER\n", hr);
488 hr = IFileSystem3_GetFile(fs3, NULL, &file);
489 ok(hr == E_INVALIDARG, "GetFile returned %x, expected E_INVALIDARG\n", hr);
491 if(GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES) {
492 skip("File already exists, skipping GetFile tests\n");
493 SysFreeString(path);
494 return;
497 file = (IFile*)0xdeadbeef;
498 hr = IFileSystem3_GetFile(fs3, path, &file);
499 ok(!file, "file != NULL\n");
500 ok(hr == CTL_E_FILENOTFOUND, "GetFile returned %x, expected CTL_E_FILENOTFOUND\n", hr);
502 hf = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_READONLY, NULL);
503 if(hf == INVALID_HANDLE_VALUE) {
504 skip("Can't create temporary file\n");
505 SysFreeString(path);
506 return;
508 CloseHandle(hf);
510 hr = IFileSystem3_GetFile(fs3, path, &file);
511 ok(hr == S_OK, "GetFile returned %x, expected S_OK\n", hr);
513 hr = IFile_get_Attributes(file, &fa);
514 gfa = GetFileAttributesW(get_file) & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
515 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ARCHIVE |
516 FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_COMPRESSED);
517 ok(hr == S_OK, "get_Attributes returned %x, expected S_OK\n", hr);
518 ok(fa == gfa, "fa = %x, expected %x\n", fa, gfa);
520 hr = IFile_get_Size(file, &size);
521 ok(hr == S_OK, "get_Size returned %x, expected S_OK\n", hr);
522 ok(V_VT(&size) == VT_I4, "V_VT(&size) = %d, expected VT_I4\n", V_VT(&size));
523 ok(V_I4(&size) == 0, "V_I4(&size) = %d, expected 0\n", V_I4(&size));
524 IFile_Release(file);
526 hr = IFileSystem3_DeleteFile(fs3, path, FALSE);
527 ok(hr==CTL_E_PERMISSIONDENIED || broken(hr==S_OK),
528 "DeleteFile returned %x, expected CTL_E_PERMISSIONDENIED\n", hr);
529 if(hr != S_OK) {
530 hr = IFileSystem3_DeleteFile(fs3, path, TRUE);
531 ok(hr == S_OK, "DeleteFile returned %x, expected S_OK\n", hr);
533 hr = IFileSystem3_DeleteFile(fs3, path, TRUE);
534 ok(hr == CTL_E_FILENOTFOUND, "DeleteFile returned %x, expected CTL_E_FILENOTFOUND\n", hr);
536 SysFreeString(path);
539 static inline BOOL create_file(const WCHAR *name)
541 HANDLE f = CreateFileW(name, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
542 CloseHandle(f);
543 return f != INVALID_HANDLE_VALUE;
546 static inline void create_path(const WCHAR *folder, const WCHAR *name, WCHAR *ret)
548 DWORD len = lstrlenW(folder);
549 memmove(ret, folder, len*sizeof(WCHAR));
550 ret[len] = '\\';
551 memmove(ret+len+1, name, (lstrlenW(name)+1)*sizeof(WCHAR));
554 static void test_CopyFolder(void)
556 static const WCHAR filesystem3_dir[] = {'f','i','l','e','s','y','s','t','e','m','3','_','t','e','s','t',0};
557 static const WCHAR s1[] = {'s','r','c','1',0};
558 static const WCHAR s[] = {'s','r','c','*',0};
559 static const WCHAR d[] = {'d','s','t',0};
560 static const WCHAR empty[] = {0};
562 WCHAR tmp[MAX_PATH];
563 BSTR bsrc, bdst;
564 HRESULT hr;
566 if(!CreateDirectoryW(filesystem3_dir, NULL)) {
567 skip("can't create temporary directory\n");
568 return;
571 create_path(filesystem3_dir, s1, tmp);
572 bsrc = SysAllocString(tmp);
573 create_path(filesystem3_dir, d, tmp);
574 bdst = SysAllocString(tmp);
575 hr = IFileSystem3_CopyFile(fs3, bsrc, bdst, VARIANT_TRUE);
576 ok(hr == CTL_E_FILENOTFOUND, "CopyFile returned %x, expected CTL_E_FILENOTFOUND\n", hr);
578 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_TRUE);
579 ok(hr == CTL_E_PATHNOTFOUND, "CopyFolder returned %x, expected CTL_E_PATHNOTFOUND\n", hr);
581 ok(create_file(bsrc), "can't create %s file\n", wine_dbgstr_w(bsrc));
582 hr = IFileSystem3_CopyFile(fs3, bsrc, bdst, VARIANT_TRUE);
583 ok(hr == S_OK, "CopyFile returned %x, expected S_OK\n", hr);
585 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_TRUE);
586 ok(hr == CTL_E_PATHNOTFOUND, "CopyFolder returned %x, expected CTL_E_PATHNOTFOUND\n", hr);
588 hr = IFileSystem3_DeleteFile(fs3, bsrc, VARIANT_FALSE);
589 ok(hr == S_OK, "DeleteFile returned %x, expected S_OK\n", hr);
591 ok(CreateDirectoryW(bsrc, NULL), "can't create %s\n", wine_dbgstr_w(bsrc));
592 hr = IFileSystem3_CopyFile(fs3, bsrc, bdst, VARIANT_TRUE);
593 ok(hr == CTL_E_FILENOTFOUND, "CopyFile returned %x, expected CTL_E_FILENOTFOUND\n", hr);
595 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_TRUE);
596 ok(hr == CTL_E_FILEALREADYEXISTS, "CopyFolder returned %x, expected CTL_E_FILEALREADYEXISTS\n", hr);
598 hr = IFileSystem3_DeleteFile(fs3, bdst, VARIANT_TRUE);
599 ok(hr == S_OK, "DeleteFile returned %x, expected S_OK\n", hr);
601 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_TRUE);
602 ok(hr == S_OK, "CopyFolder returned %x, expected S_OK\n", hr);
604 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_TRUE);
605 ok(hr == S_OK, "CopyFolder returned %x, expected S_OK\n", hr);
606 create_path(tmp, s1, tmp);
607 ok(GetFileAttributesW(tmp) == INVALID_FILE_ATTRIBUTES,
608 "%s file exists\n", wine_dbgstr_w(tmp));
610 create_path(filesystem3_dir, d, tmp);
611 create_path(tmp, empty, tmp);
612 SysFreeString(bdst);
613 bdst = SysAllocString(tmp);
614 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_TRUE);
615 ok(hr == S_OK, "CopyFolder returned %x, expected S_OK\n", hr);
616 create_path(tmp, s1, tmp);
617 ok(GetFileAttributesW(tmp) != INVALID_FILE_ATTRIBUTES,
618 "%s directory doesn't exist\n", wine_dbgstr_w(tmp));
619 ok(RemoveDirectoryW(tmp), "can't remove %s directory\n", wine_dbgstr_w(tmp));
620 create_path(filesystem3_dir, d, tmp);
621 SysFreeString(bdst);
622 bdst = SysAllocString(tmp);
625 create_path(filesystem3_dir, s, tmp);
626 SysFreeString(bsrc);
627 bsrc = SysAllocString(tmp);
628 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_TRUE);
629 ok(hr == S_OK, "CopyFolder returned %x, expected S_OK\n", hr);
630 create_path(filesystem3_dir, d, tmp);
631 create_path(tmp, s1, tmp);
632 ok(GetFileAttributesW(tmp) != INVALID_FILE_ATTRIBUTES,
633 "%s directory doesn't exist\n", wine_dbgstr_w(tmp));
635 hr = IFileSystem3_DeleteFolder(fs3, bdst, VARIANT_FALSE);
636 ok(hr == S_OK, "DeleteFolder returned %x, expected S_OK\n", hr);
638 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_TRUE);
639 ok(hr == CTL_E_PATHNOTFOUND, "CopyFolder returned %x, expected CTL_E_PATHNOTFOUND\n", hr);
641 create_path(filesystem3_dir, s1, tmp);
642 SysFreeString(bsrc);
643 bsrc = SysAllocString(tmp);
644 create_path(tmp, s1, tmp);
645 ok(create_file(tmp), "can't create %s file\n", wine_dbgstr_w(tmp));
646 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_FALSE);
647 ok(hr == S_OK, "CopyFolder returned %x, expected S_OK\n", hr);
649 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_FALSE);
650 ok(hr == CTL_E_FILEALREADYEXISTS, "CopyFolder returned %x, expected CTL_E_FILEALREADYEXISTS\n", hr);
652 hr = IFileSystem3_CopyFolder(fs3, bsrc, bdst, VARIANT_TRUE);
653 ok(hr == S_OK, "CopyFolder returned %x, expected S_OK\n", hr);
654 SysFreeString(bsrc);
655 SysFreeString(bdst);
657 bsrc = SysAllocString(filesystem3_dir);
658 hr = IFileSystem3_DeleteFolder(fs3, bsrc, VARIANT_FALSE);
659 ok(hr == S_OK, "DeleteFolder returned %x, expected S_OK\n", hr);
660 SysFreeString(bsrc);
663 static BSTR bstr_from_str(const char *str)
665 int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
666 BSTR ret = SysAllocStringLen(NULL, len - 1); /* NUL character added automatically */
667 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
668 return ret;
671 struct buildpath_test
673 const char *path;
674 const char *name;
675 const char *result;
678 static struct buildpath_test buildpath_data[] =
680 { "C:\\path", "..\\name.tmp", "C:\\path\\..\\name.tmp" },
681 { "C:\\path", "\\name.tmp", "C:\\path\\name.tmp" },
682 { "C:\\path", "name.tmp", "C:\\path\\name.tmp" },
683 { "C:\\path\\", "name.tmp", "C:\\path\\name.tmp" },
684 { "C:\\path", "\\\\name.tmp", "C:\\path\\\\name.tmp" },
685 { "C:\\path\\", "\\name.tmp", "C:\\path\\name.tmp" },
686 { "C:\\path\\", "\\\\name.tmp", "C:\\path\\\\name.tmp" },
687 { "C:\\path\\\\", "\\\\name.tmp", "C:\\path\\\\\\name.tmp" },
688 { "C:\\\\", "\\name.tmp", "C:\\\\name.tmp" },
689 { "C:", "name.tmp", "C:name.tmp" },
690 { "C:", "\\\\name.tmp", "C:\\\\name.tmp" },
691 { NULL }
694 static void test_BuildPath(void)
696 struct buildpath_test *ptr = buildpath_data;
697 BSTR ret, path;
698 HRESULT hr;
699 int i = 0;
701 hr = IFileSystem3_BuildPath(fs3, NULL, NULL, NULL);
702 ok(hr == E_POINTER, "got 0x%08x\n", hr);
704 ret = (BSTR)0xdeadbeef;
705 hr = IFileSystem3_BuildPath(fs3, NULL, NULL, &ret);
706 ok(hr == S_OK, "got 0x%08x\n", hr);
707 ok(*ret == 0, "got %p\n", ret);
708 SysFreeString(ret);
710 ret = (BSTR)0xdeadbeef;
711 path = bstr_from_str("path");
712 hr = IFileSystem3_BuildPath(fs3, path, NULL, &ret);
713 ok(hr == S_OK, "got 0x%08x\n", hr);
714 ok(!lstrcmpW(ret, path), "got %s\n", wine_dbgstr_w(ret));
715 SysFreeString(ret);
716 SysFreeString(path);
718 ret = (BSTR)0xdeadbeef;
719 path = bstr_from_str("path");
720 hr = IFileSystem3_BuildPath(fs3, NULL, path, &ret);
721 ok(hr == S_OK, "got 0x%08x\n", hr);
722 ok(!lstrcmpW(ret, path), "got %s\n", wine_dbgstr_w(ret));
723 SysFreeString(ret);
724 SysFreeString(path);
726 while (ptr->path)
728 BSTR name, result;
730 ret = NULL;
731 path = bstr_from_str(ptr->path);
732 name = bstr_from_str(ptr->name);
733 result = bstr_from_str(ptr->result);
734 hr = IFileSystem3_BuildPath(fs3, path, name, &ret);
735 ok(hr == S_OK, "%d: got 0x%08x\n", i, hr);
736 if (hr == S_OK)
738 ok(!lstrcmpW(ret, result), "%d: got wrong path %s, expected %s\n", i, wine_dbgstr_w(ret),
739 wine_dbgstr_w(result));
740 SysFreeString(ret);
742 SysFreeString(path);
743 SysFreeString(name);
744 SysFreeString(result);
746 i++;
747 ptr++;
751 static void test_GetFolder(void)
753 static const WCHAR dummyW[] = {'d','u','m','m','y',0};
754 WCHAR buffW[MAX_PATH];
755 IFolder *folder;
756 HRESULT hr;
757 BSTR str;
759 folder = (void*)0xdeadbeef;
760 hr = IFileSystem3_GetFolder(fs3, NULL, &folder);
761 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
762 ok(folder == NULL, "got %p\n", folder);
764 hr = IFileSystem3_GetFolder(fs3, NULL, NULL);
765 ok(hr == E_POINTER, "got 0x%08x\n", hr);
767 /* something that doesn't exist */
768 str = SysAllocString(dummyW);
770 hr = IFileSystem3_GetFolder(fs3, str, NULL);
771 ok(hr == E_POINTER, "got 0x%08x\n", hr);
773 folder = (void*)0xdeadbeef;
774 hr = IFileSystem3_GetFolder(fs3, str, &folder);
775 ok(hr == CTL_E_PATHNOTFOUND, "got 0x%08x\n", hr);
776 ok(folder == NULL, "got %p\n", folder);
777 SysFreeString(str);
779 GetWindowsDirectoryW(buffW, MAX_PATH);
780 str = SysAllocString(buffW);
781 hr = IFileSystem3_GetFolder(fs3, str, &folder);
782 ok(hr == S_OK, "got 0x%08x\n", hr);
783 SysFreeString(str);
784 IFolder_Release(folder);
787 static void test_FolderCollection(void)
789 static const WCHAR aW[] = {'\\','a',0};
790 static const WCHAR bW[] = {'\\','b',0};
791 IFolderCollection *folders;
792 WCHAR buffW[MAX_PATH], pathW[MAX_PATH], path2W[MAX_PATH];
793 IEnumVARIANT *enumvar, *clone;
794 LONG count, count2, ref, ref2;
795 IUnknown *unk, *unk2;
796 IFolder *folder;
797 HRESULT hr;
798 BSTR str;
800 GetTempPathW(MAX_PATH, buffW);
802 str = SysAllocString(buffW);
803 hr = IFileSystem3_GetFolder(fs3, str, &folder);
804 ok(hr == S_OK, "got 0x%08x\n", hr);
805 SysFreeString(str);
807 hr = IFolder_get_SubFolders(folder, NULL);
808 ok(hr == E_POINTER, "got 0x%08x\n", hr);
810 lstrcpyW(pathW, buffW);
811 lstrcatW(pathW, aW);
812 CreateDirectoryW(pathW, NULL);
814 hr = IFolder_get_SubFolders(folder, &folders);
815 ok(hr == S_OK, "got 0x%08x\n", hr);
817 count = 0;
818 hr = IFolderCollection_get_Count(folders, &count);
819 ok(hr == S_OK, "got 0x%08x\n", hr);
820 ok(count > 0, "got %d\n", count);
822 lstrcpyW(path2W, buffW);
823 lstrcatW(path2W, bW);
824 CreateDirectoryW(path2W, NULL);
826 /* every time property is requested it scans directory */
827 count2 = 0;
828 hr = IFolderCollection_get_Count(folders, &count2);
829 ok(hr == S_OK, "got 0x%08x\n", hr);
830 ok(count2 > count, "got %d, %d\n", count, count2);
832 hr = IFolderCollection_get__NewEnum(folders, NULL);
833 ok(hr == E_POINTER, "got 0x%08x\n", hr);
835 hr = IFolderCollection_QueryInterface(folders, &IID_IEnumVARIANT, (void**)&unk);
836 ok(hr == E_NOINTERFACE, "got 0x%08x\n", hr);
838 /* NewEnum creates new instance each time it's called */
839 ref = GET_REFCOUNT(folders);
841 unk = NULL;
842 hr = IFolderCollection_get__NewEnum(folders, &unk);
843 ok(hr == S_OK, "got 0x%08x\n", hr);
845 ref2 = GET_REFCOUNT(folders);
846 ok(ref2 == ref + 1, "got %d, %d\n", ref2, ref);
848 unk2 = NULL;
849 hr = IFolderCollection_get__NewEnum(folders, &unk2);
850 ok(hr == S_OK, "got 0x%08x\n", hr);
851 ok(unk != unk2, "got %p, %p\n", unk2, unk);
852 IUnknown_Release(unk2);
854 /* now get IEnumVARIANT */
855 ref = GET_REFCOUNT(folders);
856 hr = IUnknown_QueryInterface(unk, &IID_IEnumVARIANT, (void**)&enumvar);
857 ok(hr == S_OK, "got 0x%08x\n", hr);
858 ref2 = GET_REFCOUNT(folders);
859 ok(ref2 == ref, "got %d, %d\n", ref2, ref);
861 /* clone enumerator */
862 hr = IEnumVARIANT_Clone(enumvar, &clone);
863 todo_wine
864 ok(hr == S_OK, "got 0x%08x\n", hr);
865 if (hr == S_OK) {
866 ok(clone != enumvar, "got %p, %p\n", enumvar, clone);
867 IEnumVARIANT_Release(clone);
870 hr = IEnumVARIANT_Reset(enumvar);
871 ok(hr == S_OK, "got 0x%08x\n", hr);
873 IEnumVARIANT_Release(enumvar);
874 IUnknown_Release(unk);
876 RemoveDirectoryW(pathW);
877 RemoveDirectoryW(path2W);
879 IFolderCollection_Release(folders);
880 IFolder_Release(folder);
883 START_TEST(filesystem)
885 HRESULT hr;
887 CoInitialize(NULL);
889 hr = CoCreateInstance(&CLSID_FileSystemObject, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
890 &IID_IFileSystem3, (void**)&fs3);
891 if(FAILED(hr)) {
892 win_skip("Could not create FileSystem object: %08x\n", hr);
893 return;
896 test_interfaces();
897 test_createfolder();
898 test_textstream();
899 test_GetFileVersion();
900 test_GetParentFolderName();
901 test_GetFileName();
902 test_GetBaseName();
903 test_GetAbsolutePathName();
904 test_GetFile();
905 test_CopyFolder();
906 test_BuildPath();
907 test_GetFolder();
908 test_FolderCollection();
910 IFileSystem3_Release(fs3);
912 CoUninitialize();