scrrun: Add IFile::get_Size implementation.
[wine.git] / dlls / scrrun / tests / filesystem.c
blob0a55736129f3d1592f22019a37b1365a9e7a5c31
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 void test_interfaces(void)
38 static const WCHAR nonexistent_dirW[] = {
39 'c', ':', '\\', 'N', 'o', 'n', 'e', 'x', 'i', 's', 't', 'e', 'n', 't', 0};
40 static const WCHAR pathW[] = {'p','a','t','h',0};
41 static const WCHAR file_kernel32W[] = {
42 '\\', 'k', 'e', 'r', 'n', 'e', 'l', '3', '2', '.', 'd', 'l', 'l', 0};
43 HRESULT hr;
44 IDispatch *disp;
45 IDispatchEx *dispex;
46 IObjectWithSite *site;
47 VARIANT_BOOL b;
48 BSTR path;
49 WCHAR windows_path[MAX_PATH];
50 WCHAR file_path[MAX_PATH];
52 IFileSystem3_QueryInterface(fs3, &IID_IDispatch, (void**)&disp);
54 GetSystemDirectoryW(windows_path, MAX_PATH);
55 lstrcpyW(file_path, windows_path);
56 lstrcatW(file_path, file_kernel32W);
58 hr = IDispatch_QueryInterface(disp, &IID_IObjectWithSite, (void**)&site);
59 ok(hr == E_NOINTERFACE, "got 0x%08x, expected 0x%08x\n", hr, E_NOINTERFACE);
61 hr = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
62 ok(hr == E_NOINTERFACE, "got 0x%08x, expected 0x%08x\n", hr, E_NOINTERFACE);
64 b = VARIANT_TRUE;
65 hr = IFileSystem3_FileExists(fs3, NULL, &b);
66 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
67 ok(b == VARIANT_FALSE, "got %x\n", b);
69 hr = IFileSystem3_FileExists(fs3, NULL, NULL);
70 ok(hr == E_POINTER, "got 0x%08x, expected 0x%08x\n", hr, E_POINTER);
72 path = SysAllocString(pathW);
73 b = VARIANT_TRUE;
74 hr = IFileSystem3_FileExists(fs3, path, &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);
77 SysFreeString(path);
79 path = SysAllocString(file_path);
80 b = VARIANT_FALSE;
81 hr = IFileSystem3_FileExists(fs3, path, &b);
82 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
83 ok(b == VARIANT_TRUE, "got %x\n", b);
84 SysFreeString(path);
86 path = SysAllocString(windows_path);
87 b = VARIANT_TRUE;
88 hr = IFileSystem3_FileExists(fs3, path, &b);
89 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
90 ok(b == VARIANT_FALSE, "got %x\n", b);
91 SysFreeString(path);
93 /* Folder Exists */
94 hr = IFileSystem3_FolderExists(fs3, NULL, NULL);
95 ok(hr == E_POINTER, "got 0x%08x, expected 0x%08x\n", hr, E_POINTER);
97 path = SysAllocString(windows_path);
98 hr = IFileSystem3_FolderExists(fs3, path, &b);
99 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
100 ok(b == VARIANT_TRUE, "Folder doesn't exists\n");
101 SysFreeString(path);
103 path = SysAllocString(nonexistent_dirW);
104 hr = IFileSystem3_FolderExists(fs3, path, &b);
105 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
106 ok(b == VARIANT_FALSE, "Folder exists\n");
107 SysFreeString(path);
109 path = SysAllocString(file_path);
110 hr = IFileSystem3_FolderExists(fs3, path, &b);
111 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
112 ok(b == VARIANT_FALSE, "Folder exists\n");
113 SysFreeString(path);
115 IDispatch_Release(disp);
118 static void test_createfolder(void)
120 HRESULT hr;
121 WCHAR pathW[MAX_PATH];
122 BSTR path;
123 IFolder *folder;
125 /* create existing directory */
126 GetCurrentDirectoryW(sizeof(pathW)/sizeof(WCHAR), pathW);
127 path = SysAllocString(pathW);
128 folder = (void*)0xdeabeef;
129 hr = IFileSystem3_CreateFolder(fs3, path, &folder);
130 ok(hr == CTL_E_FILEALREADYEXISTS, "got 0x%08x\n", hr);
131 ok(folder == NULL, "got %p\n", folder);
132 SysFreeString(path);
135 static void test_textstream(void)
137 static WCHAR testfileW[] = {'t','e','s','t','f','i','l','e','.','t','x','t',0};
138 ITextStream *stream;
139 VARIANT_BOOL b;
140 HANDLE file;
141 HRESULT hr;
142 BSTR name, data;
144 file = CreateFileW(testfileW, GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
145 CloseHandle(file);
147 name = SysAllocString(testfileW);
148 b = VARIANT_FALSE;
149 hr = IFileSystem3_FileExists(fs3, name, &b);
150 ok(hr == S_OK, "got 0x%08x, expected 0x%08x\n", hr, S_OK);
151 ok(b == VARIANT_TRUE, "got %x\n", b);
153 hr = IFileSystem3_OpenTextFile(fs3, name, ForReading, VARIANT_FALSE, TristateFalse, &stream);
154 ok(hr == S_OK, "got 0x%08x\n", hr);
156 b = 10;
157 hr = ITextStream_get_AtEndOfStream(stream, &b);
158 todo_wine {
159 ok(hr == S_FALSE || broken(hr == S_OK), "got 0x%08x\n", hr);
160 ok(b == VARIANT_TRUE, "got 0x%x\n", b);
162 ITextStream_Release(stream);
164 hr = IFileSystem3_OpenTextFile(fs3, name, ForWriting, VARIANT_FALSE, TristateFalse, &stream);
165 ok(hr == S_OK, "got 0x%08x\n", hr);
167 b = 10;
168 hr = ITextStream_get_AtEndOfStream(stream, &b);
169 todo_wine {
170 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
171 ok(b == VARIANT_TRUE || broken(b == 10), "got 0x%x\n", b);
173 b = 10;
174 hr = ITextStream_get_AtEndOfLine(stream, &b);
175 todo_wine {
176 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
177 ok(b == VARIANT_FALSE || broken(b == 10), "got 0x%x\n", b);
179 hr = ITextStream_Read(stream, 1, &data);
180 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
182 hr = ITextStream_ReadLine(stream, &data);
183 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
185 hr = ITextStream_ReadAll(stream, &data);
186 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
188 ITextStream_Release(stream);
190 hr = IFileSystem3_OpenTextFile(fs3, name, ForAppending, VARIANT_FALSE, TristateFalse, &stream);
191 ok(hr == S_OK, "got 0x%08x\n", hr);
192 SysFreeString(name);
194 b = 10;
195 hr = ITextStream_get_AtEndOfStream(stream, &b);
196 todo_wine {
197 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
198 ok(b == VARIANT_TRUE || broken(b == 10), "got 0x%x\n", b);
200 b = 10;
201 hr = ITextStream_get_AtEndOfLine(stream, &b);
202 todo_wine {
203 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
204 ok(b == VARIANT_FALSE || broken(b == 10), "got 0x%x\n", b);
206 hr = ITextStream_Read(stream, 1, &data);
207 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
209 hr = ITextStream_ReadLine(stream, &data);
210 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
212 hr = ITextStream_ReadAll(stream, &data);
213 ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
215 ITextStream_Release(stream);
217 DeleteFileW(testfileW);
220 static void test_GetFileVersion(void)
222 static const WCHAR k32W[] = {'\\','k','e','r','n','e','l','3','2','.','d','l','l',0};
223 static const WCHAR k33W[] = {'\\','k','e','r','n','e','l','3','3','.','d','l','l',0};
224 WCHAR pathW[MAX_PATH], filenameW[MAX_PATH];
225 BSTR path, version;
226 HRESULT hr;
228 GetSystemDirectoryW(pathW, sizeof(pathW)/sizeof(WCHAR));
230 lstrcpyW(filenameW, pathW);
231 lstrcatW(filenameW, k32W);
233 path = SysAllocString(filenameW);
234 hr = IFileSystem3_GetFileVersion(fs3, path, &version);
235 ok(hr == S_OK, "got 0x%08x\n", hr);
236 ok(*version != 0, "got %s\n", wine_dbgstr_w(version));
237 SysFreeString(version);
238 SysFreeString(path);
240 lstrcpyW(filenameW, pathW);
241 lstrcatW(filenameW, k33W);
243 path = SysAllocString(filenameW);
244 version = (void*)0xdeadbeef;
245 hr = IFileSystem3_GetFileVersion(fs3, path, &version);
246 ok(broken(hr == S_OK) || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "got 0x%08x\n", hr);
247 if (hr == S_OK)
249 ok(*version == 0, "got %s\n", wine_dbgstr_w(version));
250 SysFreeString(version);
252 else
253 ok(version == (void*)0xdeadbeef, "got %p\n", version);
254 SysFreeString(path);
257 static void test_GetParentFolderName(void)
259 static const WCHAR path1[] = {'a',0};
260 static const WCHAR path2[] = {'a','/','a','/','a',0};
261 static const WCHAR path3[] = {'a','\\','a','\\','a',0};
262 static const WCHAR path4[] = {'a','/','a','/','/','\\','\\',0};
263 static const WCHAR path5[] = {'c',':','\\','\\','a',0};
264 static const WCHAR path6[] = {'a','c',':','\\','a',0};
265 static const WCHAR result2[] = {'a','/','a',0};
266 static const WCHAR result3[] = {'a','\\','a',0};
267 static const WCHAR result4[] = {'a',0};
268 static const WCHAR result5[] = {'c',':','\\',0};
269 static const WCHAR result6[] = {'a','c',':',0};
271 static const struct {
272 const WCHAR *path;
273 const WCHAR *result;
274 } tests[] = {
275 {NULL, NULL},
276 {path1, NULL},
277 {path2, result2},
278 {path3, result3},
279 {path4, result4},
280 {path5, result5},
281 {path6, result6}
284 BSTR path, result;
285 HRESULT hr;
286 int i;
288 hr = IFileSystem3_GetParentFolderName(fs3, NULL, NULL);
289 ok(hr == E_POINTER, "GetParentFolderName returned %x, expected E_POINTER\n", hr);
291 for(i=0; i<sizeof(tests)/sizeof(tests[0]); i++) {
292 result = (BSTR)0xdeadbeef;
293 path = tests[i].path ? SysAllocString(tests[i].path) : NULL;
294 hr = IFileSystem3_GetParentFolderName(fs3, path, &result);
295 ok(hr == S_OK, "%d) GetParentFolderName returned %x, expected S_OK\n", i, hr);
296 if(!tests[i].result)
297 ok(!result, "%d) result = %s\n", i, wine_dbgstr_w(result));
298 else
299 ok(!lstrcmpW(result, tests[i].result), "%d) result = %s\n", i, wine_dbgstr_w(result));
300 SysFreeString(path);
301 SysFreeString(result);
305 static void test_GetFileName(void)
307 static const WCHAR path1[] = {'a',0};
308 static const WCHAR path2[] = {'a','/','a','.','b',0};
309 static const WCHAR path3[] = {'a','\\',0};
310 static const WCHAR path4[] = {'c',':',0};
311 static const WCHAR path5[] = {'/','\\',0};
312 static const WCHAR result2[] = {'a','.','b',0};
313 static const WCHAR result3[] = {'a',0};
315 static const struct {
316 const WCHAR *path;
317 const WCHAR *result;
318 } tests[] = {
319 {NULL, NULL},
320 {path1, path1},
321 {path2, result2},
322 {path3, result3},
323 {path4, NULL},
324 {path5, NULL}
327 BSTR path, result;
328 HRESULT hr;
329 int i;
331 hr = IFileSystem3_GetFileName(fs3, NULL, NULL);
332 ok(hr == E_POINTER, "GetFileName returned %x, expected E_POINTER\n", hr);
334 for(i=0; i<sizeof(tests)/sizeof(tests[0]); i++) {
335 result = (BSTR)0xdeadbeef;
336 path = tests[i].path ? SysAllocString(tests[i].path) : NULL;
337 hr = IFileSystem3_GetFileName(fs3, path, &result);
338 ok(hr == S_OK, "%d) GetFileName returned %x, expected S_OK\n", i, hr);
339 if(!tests[i].result)
340 ok(!result, "%d) result = %s\n", i, wine_dbgstr_w(result));
341 else
342 ok(!lstrcmpW(result, tests[i].result), "%d) result = %s\n", i, wine_dbgstr_w(result));
343 SysFreeString(path);
344 SysFreeString(result);
348 static void test_GetBaseName(void)
350 static const WCHAR path1[] = {'a',0};
351 static const WCHAR path2[] = {'a','/','a','.','b','.','c',0};
352 static const WCHAR path3[] = {'a','.','b','\\',0};
353 static const WCHAR path4[] = {'c',':',0};
354 static const WCHAR path5[] = {'/','\\',0};
355 static const WCHAR path6[] = {'.','a',0};
356 static const WCHAR result1[] = {'a',0};
357 static const WCHAR result2[] = {'a','.','b',0};
358 static const WCHAR result6[] = {0};
360 static const struct {
361 const WCHAR *path;
362 const WCHAR *result;
363 } tests[] = {
364 {NULL, NULL},
365 {path1, result1},
366 {path2, result2},
367 {path3, result1},
368 {path4, NULL},
369 {path5, NULL},
370 {path6, result6}
373 BSTR path, result;
374 HRESULT hr;
375 int i;
377 hr = IFileSystem3_GetBaseName(fs3, NULL, NULL);
378 ok(hr == E_POINTER, "GetBaseName returned %x, expected E_POINTER\n", hr);
380 for(i=0; i<sizeof(tests)/sizeof(tests[0]); i++) {
381 result = (BSTR)0xdeadbeef;
382 path = tests[i].path ? SysAllocString(tests[i].path) : NULL;
383 hr = IFileSystem3_GetBaseName(fs3, path, &result);
384 ok(hr == S_OK, "%d) GetBaseName returned %x, expected S_OK\n", i, hr);
385 if(!tests[i].result)
386 ok(!result, "%d) result = %s\n", i, wine_dbgstr_w(result));
387 else
388 ok(!lstrcmpW(result, tests[i].result), "%d) result = %s\n", i, wine_dbgstr_w(result));
389 SysFreeString(path);
390 SysFreeString(result);
394 static void test_GetAbsolutePathName(void)
396 static const WCHAR dir1[] = {'t','e','s','t','_','d','i','r','1',0};
397 static const WCHAR dir2[] = {'t','e','s','t','_','d','i','r','2',0};
398 static const WCHAR dir_match1[] = {'t','e','s','t','_','d','i','r','*',0};
399 static const WCHAR dir_match2[] = {'t','e','s','t','_','d','i','*',0};
400 static const WCHAR cur_dir[] = {'.',0};
402 WIN32_FIND_DATAW fdata;
403 HANDLE find;
404 WCHAR buf[MAX_PATH];
405 BSTR path, result;
406 HRESULT hr;
408 hr = IFileSystem3_GetAbsolutePathName(fs3, NULL, NULL);
409 ok(hr == E_POINTER, "GetAbsolutePathName returned %x, expected E_POINTER\n", hr);
411 hr = IFileSystem3_GetAbsolutePathName(fs3, NULL, &result);
412 ok(hr == S_OK, "GetAbsolutePathName returned %x, expected S_OK\n", hr);
413 GetFullPathNameW(cur_dir, MAX_PATH, buf, NULL);
414 ok(!lstrcmpW(buf, result), "result = %s, expected %s\n", wine_dbgstr_w(result), wine_dbgstr_w(buf));
415 SysFreeString(result);
417 find = FindFirstFileW(dir_match2, &fdata);
418 if(find != INVALID_HANDLE_VALUE) {
419 skip("GetAbsolutePathName tests\n");
420 FindClose(find);
421 return;
424 path = SysAllocString(dir_match1);
425 hr = IFileSystem3_GetAbsolutePathName(fs3, path, &result);
426 ok(hr == S_OK, "GetAbsolutePathName returned %x, expected S_OK\n", hr);
427 GetFullPathNameW(dir_match1, MAX_PATH, buf, NULL);
428 ok(!lstrcmpW(buf, result), "result = %s, expected %s\n", wine_dbgstr_w(result), wine_dbgstr_w(buf));
429 SysFreeString(result);
431 ok(CreateDirectoryW(dir1, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(dir1));
432 hr = IFileSystem3_GetAbsolutePathName(fs3, path, &result);
433 ok(hr == S_OK, "GetAbsolutePathName returned %x, expected S_OK\n", hr);
434 GetFullPathNameW(dir1, MAX_PATH, buf, NULL);
435 ok(!lstrcmpW(buf, result), "result = %s, expected %s\n", wine_dbgstr_w(result), wine_dbgstr_w(buf));
436 SysFreeString(result);
438 ok(CreateDirectoryW(dir2, NULL), "CreateDirectory(%s) failed\n", wine_dbgstr_w(dir2));
439 hr = IFileSystem3_GetAbsolutePathName(fs3, path, &result);
440 ok(hr == S_OK, "GetAbsolutePathName returned %x, expected S_OK\n", hr);
441 if(!lstrcmpW(buf, result)) {
442 ok(!lstrcmpW(buf, result), "result = %s, expected %s\n",
443 wine_dbgstr_w(result), wine_dbgstr_w(buf));
444 }else {
445 GetFullPathNameW(dir2, MAX_PATH, buf, NULL);
446 ok(!lstrcmpW(buf, result), "result = %s, expected %s\n",
447 wine_dbgstr_w(result), wine_dbgstr_w(buf));
449 SysFreeString(result);
451 SysFreeString(path);
452 path = SysAllocString(dir_match2);
453 hr = IFileSystem3_GetAbsolutePathName(fs3, path, &result);
454 ok(hr == S_OK, "GetAbsolutePathName returned %x, expected S_OK\n", hr);
455 GetFullPathNameW(dir_match2, MAX_PATH, buf, NULL);
456 ok(!lstrcmpW(buf, result), "result = %s, expected %s\n", wine_dbgstr_w(result), wine_dbgstr_w(buf));
457 SysFreeString(result);
458 SysFreeString(path);
460 RemoveDirectoryW(dir1);
461 RemoveDirectoryW(dir2);
464 static void test_GetFile(void)
466 static const WCHAR get_file[] = {'g','e','t','_','f','i','l','e','.','t','s','t',0};
468 BSTR path = SysAllocString(get_file);
469 FileAttribute fa;
470 VARIANT size;
471 DWORD gfa;
472 IFile *file;
473 HRESULT hr;
474 HANDLE hf;
476 hr = IFileSystem3_GetFile(fs3, path, NULL);
477 ok(hr == E_POINTER, "GetFile returned %x, expected E_POINTER\n", hr);
478 hr = IFileSystem3_GetFile(fs3, NULL, &file);
479 ok(hr == E_INVALIDARG, "GetFile returned %x, expected E_INVALIDARG\n", hr);
481 hf = CreateFileW(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
482 if(hf != INVALID_HANDLE_VALUE) {
483 CloseHandle(hf);
484 skip("File already exists, skipping GetFile tests\n");
485 SysFreeString(path);
486 return;
489 file = (IFile*)0xdeadbeef;
490 hr = IFileSystem3_GetFile(fs3, path, &file);
491 ok(!file, "file != NULL\n");
492 ok(hr == CTL_E_FILENOTFOUND, "GetFile returned %x, expected CTL_E_FILENOTFOUND\n", hr);
494 hf = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
495 if(hf == INVALID_HANDLE_VALUE) {
496 skip("Can't create temporary file\n");
497 SysFreeString(path);
498 return;
500 CloseHandle(hf);
502 hr = IFileSystem3_GetFile(fs3, path, &file);
503 ok(hr == S_OK, "GetFile returned %x, expected S_OK\n", hr);
505 hr = IFile_get_Attributes(file, &fa);
506 gfa = GetFileAttributesW(get_file) & ~FILE_ATTRIBUTE_NORMAL;
507 ok(hr == S_OK, "get_Attributes returned %x, expected S_OK\n", hr);
508 ok(fa == gfa, "fa = %x, expected %x\n", fa, gfa);
510 hr = IFile_get_Size(file, &size);
511 ok(hr == S_OK, "get_Size returned %x, expected S_OK\n", hr);
512 ok(V_VT(&size) == VT_I4, "V_VT(&size) = %d, expected VT_I4\n", V_VT(&size));
513 ok(V_I4(&size) == 0, "V_I4(&size) = %d, expected 0\n", V_I4(&size));
514 IFile_Release(file);
516 DeleteFileW(path);
517 SysFreeString(path);
520 START_TEST(filesystem)
522 HRESULT hr;
524 CoInitialize(NULL);
526 hr = CoCreateInstance(&CLSID_FileSystemObject, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
527 &IID_IFileSystem3, (void**)&fs3);
528 if(FAILED(hr)) {
529 win_skip("Could not create FileSystem object: %08x\n", hr);
530 return;
533 test_interfaces();
534 test_createfolder();
535 test_textstream();
536 test_GetFileVersion();
537 test_GetParentFolderName();
538 test_GetFileName();
539 test_GetBaseName();
540 test_GetAbsolutePathName();
541 test_GetFile();
543 IFileSystem3_Release(fs3);
545 CoUninitialize();