mscoree/tests: Fix failures when .NET 4 but not 1 or 2 is installed.
[wine/multimedia.git] / dlls / mscoree / tests / mscoree.c
blob852944f8f85e56eb1fe3d8de801f20276f3239ce
1 /*
2 * Copyright 2010 Louis Lenders
3 * Copyright 2011 André Hentschel
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
22 #include "corerror.h"
23 #include "mscoree.h"
24 #include "metahost.h"
25 #include "shlwapi.h"
26 #include "wine/test.h"
28 static HMODULE hmscoree;
30 static HRESULT (WINAPI *pGetCORVersion)(LPWSTR, DWORD, DWORD*);
31 static HRESULT (WINAPI *pGetCORSystemDirectory)(LPWSTR, DWORD, DWORD*);
32 static HRESULT (WINAPI *pGetRequestedRuntimeInfo)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, DWORD, LPWSTR, DWORD, DWORD*, LPWSTR, DWORD, DWORD*);
33 static HRESULT (WINAPI *pLoadLibraryShim)(LPCWSTR, LPCWSTR, LPVOID, HMODULE*);
34 static HRESULT (WINAPI *pCreateConfigStream)(LPCWSTR, IStream**);
35 static HRESULT (WINAPI *pCreateInterface)(REFCLSID, REFIID, VOID**);
37 static int no_legacy_runtimes;
39 static BOOL init_functionpointers(void)
41 hmscoree = LoadLibraryA("mscoree.dll");
43 if (!hmscoree)
45 win_skip("mscoree.dll not available\n");
46 return FALSE;
49 pGetCORVersion = (void *)GetProcAddress(hmscoree, "GetCORVersion");
50 pGetCORSystemDirectory = (void *)GetProcAddress(hmscoree, "GetCORSystemDirectory");
51 pGetRequestedRuntimeInfo = (void *)GetProcAddress(hmscoree, "GetRequestedRuntimeInfo");
52 pLoadLibraryShim = (void *)GetProcAddress(hmscoree, "LoadLibraryShim");
53 pCreateConfigStream = (void *)GetProcAddress(hmscoree, "CreateConfigStream");
54 pCreateInterface = (void *)GetProcAddress(hmscoree, "CreateInterface");
56 if (!pGetCORVersion || !pGetCORSystemDirectory || !pGetRequestedRuntimeInfo || !pLoadLibraryShim ||
57 !pCreateInterface)
59 win_skip("functions not available\n");
60 FreeLibrary(hmscoree);
61 return FALSE;
64 return TRUE;
67 static void test_versioninfo(void)
69 const WCHAR v9_0[] = {'v','9','.','0','.','3','0','3','1','9',0};
70 const WCHAR v2_0cap[] = {'V','2','.','0','.','5','0','7','2','7',0};
71 const WCHAR v2_0[] = {'v','2','.','0','.','5','0','7','2','7',0};
72 const WCHAR v2_0_0[] = {'v','2','.','0','.','0',0};
73 const WCHAR v1_1[] = {'v','1','.','1','.','4','3','2','2',0};
74 const WCHAR v1_1_0[] = {'v','1','.','1','.','0',0};
76 WCHAR version[MAX_PATH];
77 WCHAR path[MAX_PATH];
78 DWORD size, path_len;
79 HRESULT hr;
81 if (0) /* crashes on <= w2k3 */
83 hr = pGetCORVersion(NULL, MAX_PATH, &size);
84 ok(hr == E_POINTER,"GetCORVersion returned %08x\n", hr);
87 hr = pGetCORVersion(version, 1, &size);
88 if (hr == CLR_E_SHIM_RUNTIME)
90 no_legacy_runtimes = 1;
91 win_skip("No legacy .NET runtimes are installed\n");
92 return;
95 ok(hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER),"GetCORVersion returned %08x\n", hr);
97 hr = pGetCORVersion(version, MAX_PATH, &size);
98 ok(hr == S_OK,"GetCORVersion returned %08x\n", hr);
100 trace("latest installed .net runtime: %s\n", wine_dbgstr_w(version));
102 hr = pGetCORSystemDirectory(path, MAX_PATH , &size);
103 ok(hr == S_OK, "GetCORSystemDirectory returned %08x\n", hr);
104 /* size includes terminating null-character */
105 ok(size == (lstrlenW(path) + 1),"size is %d instead of %d\n", size, (lstrlenW(path) + 1));
107 path_len = size;
109 hr = pGetCORSystemDirectory(path, path_len-1 , &size);
110 ok(hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "GetCORSystemDirectory returned %08x\n", hr);
112 if (0) /* crashes on <= w2k3 */
114 hr = pGetCORSystemDirectory(NULL, MAX_PATH , &size);
115 ok(hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "GetCORSystemDirectory returned %08x\n", hr);
118 hr = pGetCORSystemDirectory(path, MAX_PATH , NULL);
119 ok(hr == E_POINTER,"GetCORSystemDirectory returned %08x\n", hr);
121 trace("latest installed .net installed in directory: %s\n", wine_dbgstr_w(path));
123 /* test GetRequestedRuntimeInfo, first get info about different versions of runtime */
124 hr = pGetRequestedRuntimeInfo( NULL, v2_0, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, &size);
126 if(hr == CLR_E_SHIM_RUNTIME) return; /* skipping rest of tests on win2k as .net 2.0 not installed */
128 ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
129 trace(" installed in directory %s is .net version %s\n", wine_dbgstr_w(path), wine_dbgstr_w(version));
131 hr = pGetRequestedRuntimeInfo( NULL, v1_1, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, &size);
132 ok(hr == S_OK || hr == CLR_E_SHIM_RUNTIME /*v1_1 not installed*/, "GetRequestedRuntimeInfo returned %08x\n", hr);
133 if(hr == S_OK)
134 trace(" installed in directory %s is .net version %s\n", wine_dbgstr_w(path), wine_dbgstr_w(version));
135 /* version number NULL not allowed without RUNTIME_INFO_UPGRADE_VERSION flag */
136 hr = pGetRequestedRuntimeInfo( NULL, NULL, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, &size);
137 ok(hr == CLR_E_SHIM_RUNTIME, "GetRequestedRuntimeInfo returned %08x\n", hr);
138 /* with RUNTIME_INFO_UPGRADE_VERSION flag and version number NULL, latest installed version is returned */
139 hr = pGetRequestedRuntimeInfo( NULL, NULL, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, path, MAX_PATH, &path_len, version, MAX_PATH, &size);
140 ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
142 hr = pGetRequestedRuntimeInfo( NULL, v2_0, NULL, 0, 0, path, 1, &path_len, version, MAX_PATH, &size);
143 ok(hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "GetRequestedRuntimeInfo returned %08x\n", hr);
145 /* if one of the buffers is NULL, the other one is still happily filled */
146 memset(version, 0, sizeof(version));
147 hr = pGetRequestedRuntimeInfo( NULL, v2_0, NULL, 0, 0, NULL, MAX_PATH, &path_len, version, MAX_PATH, &size);
148 ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
149 ok(!winetest_strcmpW(version, v2_0), "version is %s , expected %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v2_0));
150 /* With NULL-pointer for bufferlength, the buffer itself still gets filled with correct string */
151 memset(version, 0, sizeof(version));
152 hr = pGetRequestedRuntimeInfo( NULL, v2_0, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
153 ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
154 ok(!winetest_strcmpW(version, v2_0), "version is %s , expected %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v2_0));
156 memset(version, 0, sizeof(version));
157 hr = pGetRequestedRuntimeInfo( NULL, v2_0cap, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
158 ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
159 ok(!winetest_strcmpW(version, v2_0cap), "version is %s , expected %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v2_0cap));
161 /* Invalid Version and RUNTIME_INFO_UPGRADE_VERSION flag*/
162 memset(version, 0, sizeof(version));
163 hr = pGetRequestedRuntimeInfo( NULL, v1_1, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
164 ok(hr == S_OK || hr == CLR_E_SHIM_RUNTIME , "GetRequestedRuntimeInfo returned %08x\n", hr);
165 if(hr == S_OK)
167 /* .NET 1.1 may not be installed. */
168 ok(!winetest_strcmpW(version, v1_1) || !winetest_strcmpW(version, v2_0),
169 "version is %s , expected %s or %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v1_1), wine_dbgstr_w(v2_0));
173 memset(version, 0, sizeof(version));
174 hr = pGetRequestedRuntimeInfo( NULL, v9_0, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
175 ok(hr == CLR_E_SHIM_RUNTIME, "GetRequestedRuntimeInfo returned %08x\n", hr);
177 memset(version, 0, sizeof(version));
178 hr = pGetRequestedRuntimeInfo( NULL, v1_1_0, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
179 ok(hr == CLR_E_SHIM_RUNTIME, "GetRequestedRuntimeInfo returned %08x\n", hr);
181 memset(version, 0, sizeof(version));
182 hr = pGetRequestedRuntimeInfo( NULL, v1_1_0, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
183 ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
184 ok(!winetest_strcmpW(version, v2_0), "version is %s , expected %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v2_0));
186 memset(version, 0, sizeof(version));
187 hr = pGetRequestedRuntimeInfo( NULL, v2_0_0, NULL, 0, 0, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
188 ok(hr == CLR_E_SHIM_RUNTIME, "GetRequestedRuntimeInfo returned %08x\n", hr);
190 memset(version, 0, sizeof(version));
191 hr = pGetRequestedRuntimeInfo( NULL, v2_0_0, NULL, 0, RUNTIME_INFO_UPGRADE_VERSION, path, MAX_PATH, &path_len, version, MAX_PATH, NULL);
192 ok(hr == S_OK, "GetRequestedRuntimeInfo returned %08x\n", hr);
193 ok(!winetest_strcmpW(version, v2_0), "version is %s , expected %s\n", wine_dbgstr_w(version), wine_dbgstr_w(v2_0));
196 static void test_loadlibraryshim(void)
198 const WCHAR v4_0[] = {'v','4','.','0','.','3','0','3','1','9',0};
199 const WCHAR v2_0[] = {'v','2','.','0','.','5','0','7','2','7',0};
200 const WCHAR v1_1[] = {'v','1','.','1','.','4','3','2','2',0};
201 const WCHAR vbogus[] = {'v','b','o','g','u','s',0};
202 const WCHAR fusion[] = {'f','u','s','i','o','n',0};
203 const WCHAR fusiondll[] = {'f','u','s','i','o','n','.','d','l','l',0};
204 const WCHAR nosuchdll[] = {'j','n','v','n','l','.','d','l','l',0};
205 const WCHAR gdidll[] = {'g','d','i','3','2','.','d','l','l',0};
206 HRESULT hr;
207 const WCHAR *latest = NULL;
208 CHAR latestA[MAX_PATH];
209 HMODULE hdll;
210 CHAR dllpath[MAX_PATH];
212 if (no_legacy_runtimes)
214 win_skip("No legacy .NET runtimes are installed\n");
215 return;
218 hr = pLoadLibraryShim(fusion, v1_1, NULL, &hdll);
219 ok(hr == S_OK || hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
220 if (SUCCEEDED(hr))
222 latest = v1_1;
224 GetModuleFileNameA(hdll, dllpath, MAX_PATH);
226 todo_wine ok(StrStrIA(dllpath, "v1.1.4322") != 0, "incorrect fusion.dll path %s\n", dllpath);
227 ok(StrStrIA(dllpath, "fusion.dll") != 0, "incorrect fusion.dll path %s\n", dllpath);
229 FreeLibrary(hdll);
232 hr = pLoadLibraryShim(fusion, v2_0, NULL, &hdll);
233 ok(hr == S_OK || hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
234 if (SUCCEEDED(hr))
236 latest = v2_0;
238 GetModuleFileNameA(hdll, dllpath, MAX_PATH);
240 todo_wine ok(StrStrIA(dllpath, "v2.0.50727") != 0, "incorrect fusion.dll path %s\n", dllpath);
241 ok(StrStrIA(dllpath, "fusion.dll") != 0, "incorrect fusion.dll path %s\n", dllpath);
243 FreeLibrary(hdll);
246 hr = pLoadLibraryShim(fusion, v4_0, NULL, &hdll);
247 ok(hr == S_OK || hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
248 if (SUCCEEDED(hr))
250 /* LoadLibraryShim with a NULL version prefers 2.0 and earlier */
251 if (!latest)
252 latest = v4_0;
254 GetModuleFileNameA(hdll, dllpath, MAX_PATH);
256 todo_wine ok(StrStrIA(dllpath, "v4.0.30319") != 0, "incorrect fusion.dll path %s\n", dllpath);
257 ok(StrStrIA(dllpath, "fusion.dll") != 0, "incorrect fusion.dll path %s\n", dllpath);
259 FreeLibrary(hdll);
262 hr = pLoadLibraryShim(fusion, vbogus, NULL, &hdll);
263 todo_wine ok(hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
264 if (SUCCEEDED(hr))
265 FreeLibrary(hdll);
267 WideCharToMultiByte(CP_ACP, 0, latest, -1, latestA, MAX_PATH, NULL, NULL);
269 hr = pLoadLibraryShim(fusion, NULL, NULL, &hdll);
270 ok(hr == S_OK, "LoadLibraryShim failed, hr=%x\n", hr);
271 if (SUCCEEDED(hr))
273 GetModuleFileNameA(hdll, dllpath, MAX_PATH);
275 if (latest)
276 todo_wine ok(StrStrIA(dllpath, latestA) != 0, "incorrect fusion.dll path %s\n", dllpath);
277 ok(StrStrIA(dllpath, "fusion.dll") != 0, "incorrect fusion.dll path %s\n", dllpath);
279 FreeLibrary(hdll);
282 hr = pLoadLibraryShim(fusiondll, NULL, NULL, &hdll);
283 ok(hr == S_OK, "LoadLibraryShim failed, hr=%x\n", hr);
284 if (SUCCEEDED(hr))
286 GetModuleFileNameA(hdll, dllpath, MAX_PATH);
288 if (latest)
289 todo_wine ok(StrStrIA(dllpath, latestA) != 0, "incorrect fusion.dll path %s\n", dllpath);
290 ok(StrStrIA(dllpath, "fusion.dll") != 0, "incorrect fusion.dll path %s\n", dllpath);
292 FreeLibrary(hdll);
295 hr = pLoadLibraryShim(nosuchdll, latest, NULL, &hdll);
296 ok(hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
297 if (SUCCEEDED(hr))
298 FreeLibrary(hdll);
300 hr = pLoadLibraryShim(gdidll, latest, NULL, &hdll);
301 todo_wine ok(hr == E_HANDLE, "LoadLibraryShim failed, hr=%x\n", hr);
302 if (SUCCEEDED(hr))
303 FreeLibrary(hdll);
306 static const char xmldata[] =
307 "<?xml version=\"1.0\" ?>\n"
308 "<!DOCTYPE Config>\n"
309 "<Configuration>\n"
310 " <Name>Test</Name>\n"
311 " <Value>1234</Value>\n"
312 "</Configuration>";
314 static void create_xml_file(LPCWSTR filename)
316 DWORD dwNumberOfBytesWritten;
317 HANDLE hfile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
318 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
319 ok(hfile != INVALID_HANDLE_VALUE, "File creation failed\n");
320 WriteFile(hfile, xmldata, sizeof(xmldata) - 1, &dwNumberOfBytesWritten, NULL);
321 CloseHandle(hfile);
324 static void test_createconfigstream(void)
326 IStream *stream = NULL;
327 WCHAR file[] = {'c', 'o', 'n', 'f', '.', 'x', 'm', 'l', 0};
328 WCHAR nonexistent[] = {'n', 'o', 'n', 'e', 'x', 'i', 's', 't', '.', 'x', 'm', 'l', 0};
329 WCHAR path[MAX_PATH];
330 HRESULT hr;
331 char buffer[256] = {0};
333 if (!pCreateConfigStream)
335 win_skip("CreateConfigStream not available\n");
336 return;
339 create_xml_file(file);
340 GetFullPathNameW(file, MAX_PATH, path, NULL);
342 hr = pCreateConfigStream(NULL, &stream);
343 todo_wine ok(hr == E_FAIL ||
344 broken(hr == HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND)) || /* some WinXP, Win2K3 and Win7 */
345 broken(hr == S_OK && !stream), /* some Win2K3 */
346 "CreateConfigStream returned %x\n", hr);
348 hr = pCreateConfigStream(path, NULL);
349 todo_wine ok(hr == COR_E_NULLREFERENCE, "CreateConfigStream returned %x\n", hr);
351 hr = pCreateConfigStream(NULL, NULL);
352 todo_wine ok(hr == COR_E_NULLREFERENCE, "CreateConfigStream returned %x\n", hr);
354 hr = pCreateConfigStream(nonexistent, &stream);
355 todo_wine ok(hr == COR_E_FILENOTFOUND, "CreateConfigStream returned %x\n", hr);
356 ok(stream == NULL, "Expected stream to be NULL\n");
358 hr = pCreateConfigStream(path, &stream);
359 todo_wine ok(hr == S_OK, "CreateConfigStream failed, hr=%x\n", hr);
360 todo_wine ok(stream != NULL, "Expected non-NULL stream\n");
362 if (stream)
364 DWORD count;
365 LARGE_INTEGER pos;
366 ULARGE_INTEGER size;
367 IStream *stream2 = NULL;
369 hr = IStream_Read(stream, buffer, strlen(xmldata), &count);
370 ok(hr == S_OK, "IStream_Read failed, hr=%x\n", hr);
371 ok(count == strlen(xmldata), "wrong count: %u\n", count);
372 ok(!strcmp(buffer, xmldata), "Strings do not match\n");
374 hr = IStream_Write(stream, xmldata, strlen(xmldata), &count);
375 ok(hr == E_FAIL, "IStream_Write returned hr=%x\n", hr);
377 pos.QuadPart = strlen(xmldata);
378 hr = IStream_Seek(stream, pos, STREAM_SEEK_SET, NULL);
379 ok(hr == E_NOTIMPL, "IStream_Seek returned hr=%x\n", hr);
381 size.QuadPart = strlen(xmldata);
382 hr = IStream_SetSize(stream, size);
383 ok(hr == E_NOTIMPL, "IStream_SetSize returned hr=%x\n", hr);
385 hr = IStream_Clone(stream, &stream2);
386 ok(hr == E_NOTIMPL, "IStream_Clone returned hr=%x\n", hr);
388 hr = IStream_Commit(stream, STGC_DEFAULT);
389 ok(hr == E_NOTIMPL, "IStream_Commit returned hr=%x\n", hr);
391 hr = IStream_Revert(stream);
392 ok(hr == E_NOTIMPL, "IStream_Revert returned hr=%x\n", hr);
394 hr = IStream_Release(stream);
395 ok(hr == S_OK, "IStream_Release returned hr=%x\n", hr);
397 DeleteFileW(file);
400 static void test_createinstance(void)
402 HRESULT hr;
403 ICLRMetaHost *host;
405 if (no_legacy_runtimes)
407 /* If we don't have 1.x or 2.0 runtimes, we should at least have .NET 4. */
408 ok(pCreateInterface != NULL, "no legacy runtimes or .NET 4 interfaces available\n");
411 if(!pCreateInterface)
413 win_skip("Function CreateInterface not found.\n");
414 return;
417 hr = pCreateInterface(&CLSID_CLRMetaHost, &IID_ICLRMetaHost, (void**)&host);
418 if(SUCCEEDED(hr))
420 ICLRMetaHost_Release(host);
422 else
424 win_skip(".NET 4 not installed.\n");
428 START_TEST(mscoree)
430 if (!init_functionpointers())
431 return;
433 test_versioninfo();
434 test_loadlibraryshim();
435 test_createconfigstream();
436 test_createinstance();
438 FreeLibrary(hmscoree);