Release 1.5.29.
[wine] / dlls / scrrun / tests / filesystem.c
1 /*
2  *
3  * Copyright 2012 Alistair Leslie-Hughes
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #define COBJMACROS
21 #include <stdio.h>
22
23 #include "windows.h"
24 #include "ole2.h"
25 #include "olectl.h"
26 #include "oleauto.h"
27 #include "dispex.h"
28
29 #include "wine/test.h"
30
31 #include "initguid.h"
32 #include "scrrun.h"
33
34 static IFileSystem3 *fs3;
35
36 static void test_interfaces(void)
37 {
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];
51
52     IFileSystem3_QueryInterface(fs3, &IID_IDispatch, (void**)&disp);
53
54     GetSystemDirectoryW(windows_path, MAX_PATH);
55     lstrcpyW(file_path, windows_path);
56     lstrcatW(file_path, file_kernel32W);
57
58     hr = IDispatch_QueryInterface(disp, &IID_IObjectWithSite, (void**)&site);
59     ok(hr == E_NOINTERFACE, "got 0x%08x, expected 0x%08x\n", hr, E_NOINTERFACE);
60
61     hr = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
62     ok(hr == E_NOINTERFACE, "got 0x%08x, expected 0x%08x\n", hr, E_NOINTERFACE);
63
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);
68
69     hr = IFileSystem3_FileExists(fs3, NULL, NULL);
70     ok(hr == E_POINTER, "got 0x%08x, expected 0x%08x\n", hr, E_POINTER);
71
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);
78
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);
85
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);
92
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);
96
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);
102
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);
108
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);
114
115     IDispatch_Release(disp);
116 }
117
118 static void test_createfolder(void)
119 {
120     HRESULT hr;
121     WCHAR pathW[MAX_PATH];
122     BSTR path;
123     IFolder *folder;
124
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);
133 }
134
135 static void test_textstream(void)
136 {
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;
143
144     file = CreateFileW(testfileW, GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
145     CloseHandle(file);
146
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);
152
153     hr = IFileSystem3_OpenTextFile(fs3, name, ForReading, VARIANT_FALSE, TristateFalse, &stream);
154     ok(hr == S_OK, "got 0x%08x\n", hr);
155
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);
161 }
162     ITextStream_Release(stream);
163
164     hr = IFileSystem3_OpenTextFile(fs3, name, ForWriting, VARIANT_FALSE, TristateFalse, &stream);
165     ok(hr == S_OK, "got 0x%08x\n", hr);
166
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);
172 }
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);
178 }
179     hr = ITextStream_Read(stream, 1, &data);
180     ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
181
182     hr = ITextStream_ReadLine(stream, &data);
183     ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
184
185     hr = ITextStream_ReadAll(stream, &data);
186     ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
187
188     ITextStream_Release(stream);
189
190     hr = IFileSystem3_OpenTextFile(fs3, name, ForAppending, VARIANT_FALSE, TristateFalse, &stream);
191     ok(hr == S_OK, "got 0x%08x\n", hr);
192     SysFreeString(name);
193
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);
199 }
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);
205 }
206     hr = ITextStream_Read(stream, 1, &data);
207     ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
208
209     hr = ITextStream_ReadLine(stream, &data);
210     ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
211
212     hr = ITextStream_ReadAll(stream, &data);
213     ok(hr == CTL_E_BADFILEMODE, "got 0x%08x\n", hr);
214
215     ITextStream_Release(stream);
216
217     DeleteFileW(testfileW);
218 }
219
220 static void test_GetFileVersion(void)
221 {
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;
227
228     GetSystemDirectoryW(pathW, sizeof(pathW)/sizeof(WCHAR));
229
230     lstrcpyW(filenameW, pathW);
231     lstrcatW(filenameW, k32W);
232
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);
239
240     lstrcpyW(filenameW, pathW);
241     lstrcatW(filenameW, k33W);
242
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)
248     {
249         ok(*version == 0, "got %s\n", wine_dbgstr_w(version));
250         SysFreeString(version);
251     }
252     else
253         ok(version == (void*)0xdeadbeef, "got %p\n", version);
254     SysFreeString(path);
255 }
256
257 START_TEST(filesystem)
258 {
259     HRESULT hr;
260
261     CoInitialize(NULL);
262
263     hr = CoCreateInstance(&CLSID_FileSystemObject, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
264             &IID_IFileSystem3, (void**)&fs3);
265     if(FAILED(hr)) {
266         win_skip("Could not create FileSystem object: %08x\n", hr);
267         return;
268     }
269
270     test_interfaces();
271     test_createfolder();
272     test_textstream();
273     test_GetFileVersion();
274
275     IFileSystem3_Release(fs3);
276
277     CoUninitialize();
278 }