Do filename postprocessing in GetDisplayNameOf (Hide filename
[wine] / dlls / kernel / tests / module.c
1 /*
2  * Unit tests for module/DLL/library API
3  *
4  * Copyright (c) 2004 Eric Pouech
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "wine/test.h"
22 #include <windows.h>
23
24 static BOOL cmpStrAW(const char* a, const WCHAR* b, DWORD lenA, DWORD lenB)
25 {
26     WCHAR       aw[1024];
27
28     DWORD len = MultiByteToWideChar( AreFileApisANSI() ? CP_ACP : CP_OEMCP, 0,
29                                      a, lenA, aw, sizeof(aw) / sizeof(aw[0]) );
30     if (len != lenB) return FALSE;
31     return memcmp(aw, b, len * sizeof(WCHAR)) == 0;
32 }
33
34 static void testGetModuleFileName(const char* name)
35 {
36     HMODULE     hMod;
37     char        bufA[MAX_PATH];
38     WCHAR       bufW[MAX_PATH];
39     DWORD       len1A, len1W, len2A, len2W;
40
41     hMod = (name) ? GetModuleHandle(name) : NULL;
42
43     /* first test, with enough space in buffer */
44     memset(bufA, '-', sizeof(bufA));
45     len1A = GetModuleFileNameA(hMod, bufA, sizeof(bufA));
46     ok(len1A > 0, "Getting module filename for handle %p\n", hMod);
47     memset(bufW, '-', sizeof(bufW));
48     len1W = GetModuleFileNameW(hMod, bufW, sizeof(bufW) / sizeof(WCHAR));
49     ok(len1W > 0, "Getting module filename for handle %p\n", hMod);
50     ok(len1A == strlen(bufA), "Unexpected length of GetModuleFilenameA (%ld/%d)\n", len1A, strlen(bufA));
51     ok(len1W == lstrlenW(bufW), "Unexpected length of GetModuleFilenameW (%ld/%d)\n", len1W, lstrlenW(bufW));
52     ok(cmpStrAW(bufA, bufW, len1A, len1W), "Comparing GetModuleFilenameAW results\n");
53
54     /* second test with a buffer too small */
55     memset(bufA, '-', sizeof(bufA));
56     len2A = GetModuleFileNameA(hMod, bufA, len1A / 2);
57     ok(len2A > 0, "Getting module filename for handle %p\n", hMod);
58     memset(bufW, '-', sizeof(bufW));
59     len2W = GetModuleFileNameW(hMod, bufW, len1W / 2);
60     ok(len2W > 0, "Getting module filename for handle %p\n", hMod);
61     ok(cmpStrAW(bufA, bufW, len2A, len2W), "Comparing GetModuleFilenameAW results with buffer too small\n" );
62     ok(len1A / 2 == len2A, "Correct length in GetModuleFilenameA with buffer too small (%ld/%ld)\n", len1A / 2, len2A);
63     ok(len1W / 2 == len2W, "Correct length in GetModuleFilenameW with buffer too small (%ld/%ld)\n", len1W / 2, len2W);
64 }
65
66 static void testGetModuleFileName_Wrong(void)
67 {
68     char        bufA[MAX_PATH];
69     WCHAR       bufW[MAX_PATH];
70
71     /* test wrong handle */
72     bufW[0] = '*';
73     ok(GetModuleFileNameW((void*)0xffffffff, bufW, sizeof(bufW) / sizeof(WCHAR)) == 0, "Unexpected success in module handle\n");
74     ok(bufW[0] == '*', "When failing, buffer shouldn't be written to\n");
75
76     bufA[0] = '*';
77     ok(GetModuleFileNameA((void*)0xffffffff, bufA, sizeof(bufA)) == 0, "Unexpected success in module handle\n");
78     ok(bufA[0] == '*', "When failing, buffer shouldn't be written to\n");
79 }
80
81 static void testLoadLibraryA(void)
82 {
83     HMODULE hModule;
84     FARPROC fp;
85
86     SetLastError(0xdeadbeef);
87     hModule = LoadLibraryA("ntdll.dll");
88     ok( hModule != NULL, "ntdll.dll should be loadable\n");
89     ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %08lx\n", GetLastError());
90
91     fp = GetProcAddress(hModule, "LdrLoadDll"); 
92     ok( fp != NULL, "Call should be there\n");
93     ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %08lx\n", GetLastError());
94
95     FreeLibrary(hModule);
96 }
97
98 static void testLoadLibraryA_Wrong(void)
99 {
100     HMODULE hModule;
101
102     /* Try to load a non-existing dll */
103     SetLastError(0xdeadbeef);
104     hModule = LoadLibraryA("non_ex_pv.dll");
105     ok( !hModule, "non_ex_pv.dll should be not loadable\n");
106     ok( GetLastError() == ERROR_MOD_NOT_FOUND, "Expected ERROR_MOD_NOT_FOUND, got %08lx\n", GetLastError());
107
108     /* Just in case */
109     FreeLibrary(hModule);
110 }
111
112 static void testGetProcAddress_Wrong(void)
113 {
114     FARPROC fp;
115
116     SetLastError(0xdeadbeef);
117     fp = GetProcAddress(NULL, "non_ex_call");
118     ok( !fp, "non_ex_call should not be found\n");
119     ok( GetLastError() == ERROR_PROC_NOT_FOUND, "Expected ERROR_PROC_NOT_FOUND, got %08lx\n", GetLastError());
120     fp = GetProcAddress((HMODULE)0xdeadbeef, "non_ex_call");
121     ok( !fp, "non_ex_call should not be found\n");
122     ok( GetLastError() == ERROR_MOD_NOT_FOUND, "Expected ERROR_MOD_NOT_FOUND, got %08lx\n", GetLastError());
123 }
124
125 START_TEST(module)
126 {
127     testGetModuleFileName(NULL);
128     testGetModuleFileName("kernel32.dll");
129     testGetModuleFileName_Wrong();
130
131     testLoadLibraryA();
132     testLoadLibraryA_Wrong();
133     testGetProcAddress_Wrong();
134 }