dmloader: Don't claim partial success when loading fails.
[wine] / dlls / mscoree / tests / mscoree.c
1 /*
2  * Copyright 2010 Louis Lenders
3  * Copyright 2011 AndrĂ© Hentschel
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
22 #include "corerror.h"
23 #include "mscoree.h"
24 #include "shlwapi.h"
25 #include "wine/test.h"
26
27 static HMODULE hmscoree;
28
29 static HRESULT (WINAPI *pGetCORVersion)(LPWSTR, DWORD, DWORD*);
30 static HRESULT (WINAPI *pGetCORSystemDirectory)(LPWSTR, DWORD, DWORD*);
31 static HRESULT (WINAPI *pGetRequestedRuntimeInfo)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, DWORD, LPWSTR, DWORD, DWORD*, LPWSTR, DWORD, DWORD*);
32 static HRESULT (WINAPI *pLoadLibraryShim)(LPCWSTR, LPCWSTR, LPVOID, HMODULE*);
33 static HRESULT (WINAPI *pCreateConfigStream)(LPCWSTR, IStream**);
34
35 static BOOL init_functionpointers(void)
36 {
37     hmscoree = LoadLibraryA("mscoree.dll");
38
39     if (!hmscoree)
40     {
41         win_skip("mscoree.dll not available\n");
42         return FALSE;
43     }
44
45     pGetCORVersion = (void *)GetProcAddress(hmscoree, "GetCORVersion");
46     pGetCORSystemDirectory = (void *)GetProcAddress(hmscoree, "GetCORSystemDirectory");
47     pGetRequestedRuntimeInfo = (void *)GetProcAddress(hmscoree, "GetRequestedRuntimeInfo");
48     pLoadLibraryShim = (void *)GetProcAddress(hmscoree, "LoadLibraryShim");
49     pCreateConfigStream = (void *)GetProcAddress(hmscoree, "CreateConfigStream");
50
51     if (!pGetCORVersion || !pGetCORSystemDirectory || !pGetRequestedRuntimeInfo || !pLoadLibraryShim)
52     {
53         win_skip("functions not available\n");
54         FreeLibrary(hmscoree);
55         return FALSE;
56     }
57
58     return TRUE;
59 }
60
61 static void test_versioninfo(void)
62 {
63     const WCHAR v9_0[] = {'v','9','.','0','.','3','0','3','1','9',0};
64     const WCHAR v2_0[] = {'v','2','.','0','.','5','0','7','2','7',0};
65     const WCHAR v2_0_0[] = {'v','2','.','0','.','0',0};
66     const WCHAR v1_1[] = {'v','1','.','1','.','4','3','2','2',0};
67     const WCHAR v1_1_0[] = {'v','1','.','1','.','0',0};
68
69     WCHAR version[MAX_PATH];
70     WCHAR path[MAX_PATH];
71     DWORD size, path_len;
72     HRESULT hr;
73
74     if (0)  /* crashes on <= w2k3 */
75     {
76         hr = pGetCORVersion(NULL, MAX_PATH, &size);
77         ok(hr == E_POINTER,"GetCORVersion returned %08x\n", hr);
78     }
79
80     hr =  pGetCORVersion(version, 1, &size);
81     if (hr == CLR_E_SHIM_RUNTIME)
82     {
83         /* FIXME: Get Mono packaged properly so we can fail here. */
84         todo_wine ok(hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER),"GetCORVersion returned %08x\n", hr);
85         skip("No .NET runtimes are installed\n");
86         return;
87     }
88
89     ok(hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER),"GetCORVersion returned %08x\n", hr);
90
91     hr =  pGetCORVersion(version, MAX_PATH, &size);
92     ok(hr == S_OK,"GetCORVersion returned %08x\n", hr);
93
94     trace("latest installed .net runtime: %s\n", wine_dbgstr_w(version));
95
96     hr = pGetCORSystemDirectory(path, MAX_PATH , &size);
97     ok(hr == S_OK, "GetCORSystemDirectory returned %08x\n", hr);
98     /* size includes terminating null-character */
99     ok(size == (lstrlenW(path) + 1),"size is %d instead of %d\n", size, (lstrlenW(path) + 1));
100
101     path_len = size;
102
103     hr = pGetCORSystemDirectory(path, path_len-1 , &size);
104     ok(hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "GetCORSystemDirectory returned %08x\n", hr);
105
106     if (0)  /* crashes on <= w2k3 */
107     {
108         hr = pGetCORSystemDirectory(NULL, MAX_PATH , &size);
109         ok(hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "GetCORSystemDirectory returned %08x\n", hr);
110     }
111
112     hr = pGetCORSystemDirectory(path, MAX_PATH , NULL);
113     ok(hr == E_POINTER,"GetCORSystemDirectory returned %08x\n", hr);
114
115     trace("latest installed .net installed in directory: %s\n", wine_dbgstr_w(path));
116
117     /* test GetRequestedRuntimeInfo, first get info about different versions of runtime */
118     hr = pGetRequestedRuntimeInfo( NULL, v2_0, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, &size);
119
120     if(hr == CLR_E_SHIM_RUNTIME) return; /* skipping rest of tests on win2k as .net 2.0 not installed */
121
122     ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
123     trace(" installed in directory %s is .net version %s\n", wine_dbgstr_w(path), wine_dbgstr_w(version));
124
125     hr = pGetRequestedRuntimeInfo( NULL, v1_1, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, &size);
126     ok(hr == S_OK || hr == CLR_E_SHIM_RUNTIME /*v1_1 not installed*/, "GetRequestedRuntimeInfo returned %08x\n", hr);
127     if(hr == S_OK)
128         trace(" installed in directory %s is .net version %s\n", wine_dbgstr_w(path), wine_dbgstr_w(version));
129     /* version number NULL not allowed without RUNTIME_INFO_UPGRADE_VERSION flag */
130     hr = pGetRequestedRuntimeInfo( NULL, NULL, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, &size);
131     ok(hr == CLR_E_SHIM_RUNTIME, "GetRequestedRuntimeInfo returned %08x\n", hr);
132     /* with RUNTIME_INFO_UPGRADE_VERSION flag and version number NULL, latest installed version is returned */
133     hr = pGetRequestedRuntimeInfo( NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, path, MAX_PATH, &path_len, version, MAX_PATH, &size);
134     ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
135
136     hr = pGetRequestedRuntimeInfo( NULL, v2_0, NULL, 0, 0, path, 1, &path_len, version, MAX_PATH, &size);
137     ok(hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "GetRequestedRuntimeInfo returned %08x\n", hr);
138
139     /* if one of the buffers is NULL, the other one is still happily filled */
140     memset(version, 0, sizeof(version));
141     hr = pGetRequestedRuntimeInfo( NULL, v2_0, NULL, 0, 0, NULL, MAX_PATH, &path_len, version, MAX_PATH, &size);
142     ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
143     ok(!winetest_strcmpW(version, v2_0), "version is %s , expected %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v2_0));
144     /* With NULL-pointer for bufferlength, the buffer itself still gets filled with correct string */
145     memset(version, 0, sizeof(version));
146     hr = pGetRequestedRuntimeInfo( NULL, v2_0, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
147     ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
148     ok(!winetest_strcmpW(version, v2_0), "version is %s , expected %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v2_0));
149
150     /* Invalid Version and RUNTIME_INFO_UPGRADE_VERSION flag*/
151     memset(version, 0, sizeof(version));
152     hr = pGetRequestedRuntimeInfo( NULL, v1_1, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
153     ok(hr == S_OK  || hr == CLR_E_SHIM_RUNTIME , "GetRequestedRuntimeInfo returned %08x\n", hr);
154     if(hr == S_OK)
155     {
156         /* .NET 1.1 may not be installed. */
157         ok(!winetest_strcmpW(version, v1_1) || !winetest_strcmpW(version, v2_0),
158            "version is %s , expected %s or %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v1_1), wine_dbgstr_w(v2_0));
159
160     }
161
162     memset(version, 0, sizeof(version));
163     hr = pGetRequestedRuntimeInfo( NULL, v9_0, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
164     ok(hr == CLR_E_SHIM_RUNTIME, "GetRequestedRuntimeInfo returned %08x\n", hr);
165
166     memset(version, 0, sizeof(version));
167     hr = pGetRequestedRuntimeInfo( NULL, v1_1_0, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
168     ok(hr == CLR_E_SHIM_RUNTIME, "GetRequestedRuntimeInfo returned %08x\n", hr);
169
170     memset(version, 0, sizeof(version));
171     hr = pGetRequestedRuntimeInfo( NULL, v1_1_0, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
172     ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
173     ok(!winetest_strcmpW(version, v2_0), "version is %s , expected %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v2_0));
174
175     memset(version, 0, sizeof(version));
176     hr = pGetRequestedRuntimeInfo( NULL, v2_0_0, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
177     ok(hr == CLR_E_SHIM_RUNTIME, "GetRequestedRuntimeInfo returned %08x\n", hr);
178
179     memset(version, 0, sizeof(version));
180     hr = pGetRequestedRuntimeInfo( NULL, v2_0_0, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
181     ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
182     ok(!winetest_strcmpW(version, v2_0), "version is %s , expected %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v2_0));
183 }
184
185 static void test_loadlibraryshim(void)
186 {
187     const WCHAR v4_0[] = {'v','4','.','0','.','3','0','3','1','9',0};
188     const WCHAR v2_0[] = {'v','2','.','0','.','5','0','7','2','7',0};
189     const WCHAR v1_1[] = {'v','1','.','1','.','4','3','2','2',0};
190     const WCHAR vbogus[] = {'v','b','o','g','u','s',0};
191     const WCHAR fusion[] = {'f','u','s','i','o','n',0};
192     const WCHAR fusiondll[] = {'f','u','s','i','o','n','.','d','l','l',0};
193     const WCHAR nosuchdll[] = {'j','n','v','n','l','.','d','l','l',0};
194     const WCHAR gdidll[] = {'g','d','i','3','2','.','d','l','l',0};
195     HRESULT hr;
196     const WCHAR *latest = NULL;
197     CHAR latestA[MAX_PATH];
198     HMODULE hdll;
199     CHAR dllpath[MAX_PATH];
200
201     hr = pLoadLibraryShim(fusion, v1_1, NULL, &hdll);
202     ok(hr == S_OK || hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
203     if (SUCCEEDED(hr))
204     {
205         latest = v1_1;
206
207         GetModuleFileNameA(hdll, dllpath, MAX_PATH);
208
209         todo_wine ok(StrStrIA(dllpath, "v1.1.4322") != 0, "incorrect fusion.dll path %s\n", dllpath);
210         ok(StrStrIA(dllpath, "fusion.dll") != 0, "incorrect fusion.dll path %s\n", dllpath);
211
212         FreeLibrary(hdll);
213     }
214
215     hr = pLoadLibraryShim(fusion, v2_0, NULL, &hdll);
216     ok(hr == S_OK || hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
217     if (SUCCEEDED(hr))
218     {
219         latest = v2_0;
220
221         GetModuleFileNameA(hdll, dllpath, MAX_PATH);
222
223         todo_wine ok(StrStrIA(dllpath, "v2.0.50727") != 0, "incorrect fusion.dll path %s\n", dllpath);
224         ok(StrStrIA(dllpath, "fusion.dll") != 0, "incorrect fusion.dll path %s\n", dllpath);
225
226         FreeLibrary(hdll);
227     }
228
229     hr = pLoadLibraryShim(fusion, v4_0, NULL, &hdll);
230     ok(hr == S_OK || hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
231     if (SUCCEEDED(hr))
232     {
233         /* LoadLibraryShim with a NULL version prefers 2.0 and earlier */
234         if (!latest)
235             latest = v4_0;
236
237         GetModuleFileNameA(hdll, dllpath, MAX_PATH);
238
239         todo_wine ok(StrStrIA(dllpath, "v4.0.30319") != 0, "incorrect fusion.dll path %s\n", dllpath);
240         ok(StrStrIA(dllpath, "fusion.dll") != 0, "incorrect fusion.dll path %s\n", dllpath);
241
242         FreeLibrary(hdll);
243     }
244
245     hr = pLoadLibraryShim(fusion, vbogus, NULL, &hdll);
246     todo_wine ok(hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
247     if (SUCCEEDED(hr))
248         FreeLibrary(hdll);
249
250     WideCharToMultiByte(CP_ACP, 0, latest, -1, latestA, MAX_PATH, NULL, NULL);
251
252     hr = pLoadLibraryShim(fusion, NULL, NULL, &hdll);
253     ok(hr == S_OK, "LoadLibraryShim failed, hr=%x\n", hr);
254     if (SUCCEEDED(hr))
255     {
256         GetModuleFileNameA(hdll, dllpath, MAX_PATH);
257
258         if (latest)
259             todo_wine ok(StrStrIA(dllpath, latestA) != 0, "incorrect fusion.dll path %s\n", dllpath);
260         ok(StrStrIA(dllpath, "fusion.dll") != 0, "incorrect fusion.dll path %s\n", dllpath);
261
262         FreeLibrary(hdll);
263     }
264
265     hr = pLoadLibraryShim(fusiondll, NULL, NULL, &hdll);
266     ok(hr == S_OK, "LoadLibraryShim failed, hr=%x\n", hr);
267     if (SUCCEEDED(hr))
268     {
269         GetModuleFileNameA(hdll, dllpath, MAX_PATH);
270
271         if (latest)
272             todo_wine ok(StrStrIA(dllpath, latestA) != 0, "incorrect fusion.dll path %s\n", dllpath);
273         ok(StrStrIA(dllpath, "fusion.dll") != 0, "incorrect fusion.dll path %s\n", dllpath);
274
275         FreeLibrary(hdll);
276     }
277
278     hr = pLoadLibraryShim(nosuchdll, latest, NULL, &hdll);
279     ok(hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
280     if (SUCCEEDED(hr))
281         FreeLibrary(hdll);
282
283     hr = pLoadLibraryShim(gdidll, latest, NULL, &hdll);
284     todo_wine ok(hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
285     if (SUCCEEDED(hr))
286         FreeLibrary(hdll);
287 }
288
289 static const char xmldata[] =
290     "<?xml version=\"1.0\" ?>\n"
291     "<!DOCTYPE Config>\n"
292     "<Configuration>\n"
293     "  <Name>Test</Name>\n"
294     "  <Value>1234</Value>\n"
295     "</Configuration>";
296
297 static void create_xml_file(LPCWSTR filename)
298 {
299     DWORD dwNumberOfBytesWritten;
300     HANDLE hfile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
301                               CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
302     ok(hfile != INVALID_HANDLE_VALUE, "File creation failed\n");
303     WriteFile(hfile, xmldata, sizeof(xmldata) - 1, &dwNumberOfBytesWritten, NULL);
304     CloseHandle(hfile);
305 }
306
307 static void test_createconfigstream(void)
308 {
309     IStream *stream = NULL;
310     WCHAR file[] = {'c', 'o', 'n', 'f', '.', 'x', 'm', 'l', 0};
311     WCHAR nonexistent[] = {'n', 'o', 'n', 'e', 'x', 'i', 's', 't', '.', 'x', 'm', 'l', 0};
312     WCHAR path[MAX_PATH];
313     HRESULT hr;
314     char buffer[256] = {0};
315
316     if (!pCreateConfigStream)
317     {
318         win_skip("CreateConfigStream not available\n");
319         return;
320     }
321
322     create_xml_file(file);
323     GetFullPathNameW(file, MAX_PATH, path, NULL);
324
325     hr = pCreateConfigStream(NULL, &stream);
326     todo_wine ok(hr == E_FAIL ||
327                  broken(hr == HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND)) || /* some WinXP, Win2K3 and Win7 */
328                  broken(hr == S_OK && !stream), /* some Win2K3 */
329                  "CreateConfigStream returned %x\n", hr);
330
331     hr = pCreateConfigStream(path, NULL);
332     todo_wine ok(hr == COR_E_NULLREFERENCE, "CreateConfigStream returned %x\n", hr);
333
334     hr = pCreateConfigStream(NULL, NULL);
335     todo_wine ok(hr == COR_E_NULLREFERENCE, "CreateConfigStream returned %x\n", hr);
336
337     hr = pCreateConfigStream(nonexistent, &stream);
338     todo_wine ok(hr == COR_E_FILENOTFOUND, "CreateConfigStream returned %x\n", hr);
339     ok(stream == NULL, "Expected stream to be NULL\n");
340
341     hr = pCreateConfigStream(path, &stream);
342     todo_wine ok(hr == S_OK, "CreateConfigStream failed, hr=%x\n", hr);
343     todo_wine ok(stream != NULL, "Expected non-NULL stream\n");
344
345     if (stream)
346     {
347         DWORD count;
348         LARGE_INTEGER pos;
349         ULARGE_INTEGER size;
350         IStream *stream2 = NULL;
351
352         hr = IStream_Read(stream, buffer, strlen(xmldata), &count);
353         ok(hr == S_OK, "IStream_Read failed, hr=%x\n", hr);
354         ok(count == strlen(xmldata), "wrong count: %u\n", count);
355         ok(!strcmp(buffer, xmldata), "Strings do not match\n");
356
357         hr = IStream_Write(stream, xmldata, strlen(xmldata), &count);
358         ok(hr == E_FAIL, "IStream_Write returned hr=%x\n", hr);
359
360         pos.QuadPart = strlen(xmldata);
361         hr = IStream_Seek(stream, pos, STREAM_SEEK_SET, NULL);
362         ok(hr == E_NOTIMPL, "IStream_Seek returned hr=%x\n", hr);
363
364         size.QuadPart = strlen(xmldata);
365         hr = IStream_SetSize(stream, size);
366         ok(hr == E_NOTIMPL, "IStream_SetSize returned hr=%x\n", hr);
367
368         hr = IStream_Clone(stream, &stream2);
369         ok(hr == E_NOTIMPL, "IStream_Clone returned hr=%x\n", hr);
370
371         hr = IStream_Commit(stream, STGC_DEFAULT);
372         ok(hr == E_NOTIMPL, "IStream_Commit returned hr=%x\n", hr);
373
374         hr = IStream_Revert(stream);
375         ok(hr == E_NOTIMPL, "IStream_Revert returned hr=%x\n", hr);
376
377         hr = IStream_Release(stream);
378         ok(hr == S_OK, "IStream_Release returned hr=%x\n", hr);
379     }
380     DeleteFileW(file);
381 }
382
383 START_TEST(mscoree)
384 {
385     if (!init_functionpointers())
386         return;
387
388     test_versioninfo();
389     test_loadlibraryshim();
390     test_createconfigstream();
391
392     FreeLibrary(hmscoree);
393 }