EnumThemeColors() and EnumThemeSizes() actually do not return a single
[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 is_unicode_enabled = TRUE;
25
26 static BOOL cmpStrAW(const char* a, const WCHAR* b, DWORD lenA, DWORD lenB)
27 {
28     WCHAR       aw[1024];
29
30     DWORD len = MultiByteToWideChar( AreFileApisANSI() ? CP_ACP : CP_OEMCP, 0,
31                                      a, lenA, aw, sizeof(aw) / sizeof(aw[0]) );
32     if (len != lenB) return FALSE;
33     return memcmp(aw, b, len * sizeof(WCHAR)) == 0;
34 }
35
36 static void testGetModuleFileName(const char* name)
37 {
38     HMODULE     hMod;
39     char        bufA[MAX_PATH];
40     WCHAR       bufW[MAX_PATH];
41     DWORD       len1A, len1W = 0, len2A, len2W = 0;
42
43     hMod = (name) ? GetModuleHandle(name) : NULL;
44
45     /* first test, with enough space in buffer */
46     memset(bufA, '-', sizeof(bufA));
47     len1A = GetModuleFileNameA(hMod, bufA, sizeof(bufA));
48     ok(len1A > 0, "Getting module filename for handle %p\n", hMod);
49
50     if (is_unicode_enabled)
51     {
52         memset(bufW, '-', sizeof(bufW));
53         len1W = GetModuleFileNameW(hMod, bufW, sizeof(bufW) / sizeof(WCHAR));
54         ok(len1W > 0, "Getting module filename for handle %p\n", hMod);
55     }
56
57     ok(len1A == strlen(bufA), "Unexpected length of GetModuleFilenameA (%ld/%d)\n", len1A, strlen(bufA));
58
59     if (is_unicode_enabled)
60     {
61         ok(len1W == lstrlenW(bufW), "Unexpected length of GetModuleFilenameW (%ld/%d)\n", len1W, lstrlenW(bufW));
62         ok(cmpStrAW(bufA, bufW, len1A, len1W), "Comparing GetModuleFilenameAW results\n");
63     }
64
65     /* second test with a buffer too small */
66     memset(bufA, '-', sizeof(bufA));
67     len2A = GetModuleFileNameA(hMod, bufA, len1A / 2);
68     ok(len2A > 0, "Getting module filename for handle %p\n", hMod);
69
70     if (is_unicode_enabled)
71     {
72         memset(bufW, '-', sizeof(bufW));
73         len2W = GetModuleFileNameW(hMod, bufW, len1W / 2);
74         ok(len2W > 0, "Getting module filename for handle %p\n", hMod);
75         ok(cmpStrAW(bufA, bufW, len2A, len2W), "Comparing GetModuleFilenameAW results with buffer too small\n" );
76         ok(len1W / 2 == len2W, "Correct length in GetModuleFilenameW with buffer too small (%ld/%ld)\n", len1W / 2, len2W);
77     }
78
79     ok(len1A / 2 == len2A || 
80        len1A / 2 == len2A + 1, /* Win9x */
81        "Correct length in GetModuleFilenameA with buffer too small (%ld/%ld)\n", len1A / 2, len2A);
82 }
83
84 static void testGetModuleFileName_Wrong(void)
85 {
86     char        bufA[MAX_PATH];
87     WCHAR       bufW[MAX_PATH];
88
89     /* test wrong handle */
90     if (is_unicode_enabled)
91     {
92         bufW[0] = '*';
93         ok(GetModuleFileNameW((void*)0xffffffff, bufW, sizeof(bufW) / sizeof(WCHAR)) == 0, "Unexpected success in module handle\n");
94         ok(bufW[0] == '*', "When failing, buffer shouldn't be written to\n");
95     }
96
97     bufA[0] = '*';
98     ok(GetModuleFileNameA((void*)0xffffffff, bufA, sizeof(bufA)) == 0, "Unexpected success in module handle\n");
99     ok(bufA[0] == '*' ||
100        bufA[0] == 0 /* Win9x */,
101        "When failing, buffer shouldn't be written to\n");
102 }
103
104 static void testLoadLibraryA(void)
105 {
106     HMODULE hModule;
107     FARPROC fp;
108
109     SetLastError(0xdeadbeef);
110     hModule = LoadLibraryA("ntdll.dll");
111     ok( hModule != NULL, "ntdll.dll should be loadable\n");
112     ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %08lx\n", GetLastError());
113
114     fp = GetProcAddress(hModule, "NtCreateFile"); 
115     ok( fp != NULL, "Call should be there\n");
116     ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %08lx\n", GetLastError());
117
118     FreeLibrary(hModule);
119 }
120
121 static void testLoadLibraryA_Wrong(void)
122 {
123     HMODULE hModule;
124
125     /* Try to load a nonexistent dll */
126     SetLastError(0xdeadbeef);
127     hModule = LoadLibraryA("non_ex_pv.dll");
128     ok( !hModule, "non_ex_pv.dll should be not loadable\n");
129     ok( GetLastError() == ERROR_MOD_NOT_FOUND || GetLastError() == ERROR_DLL_NOT_FOUND, 
130         "Expected ERROR_MOD_NOT_FOUND or ERROR_DLL_NOT_FOUND (win9x), got %08lx\n", GetLastError());
131
132     /* Just in case */
133     FreeLibrary(hModule);
134 }
135
136 static void testGetProcAddress_Wrong(void)
137 {
138     FARPROC fp;
139
140     SetLastError(0xdeadbeef);
141     fp = GetProcAddress(NULL, "non_ex_call");
142     ok( !fp, "non_ex_call should not be found\n");
143     ok( GetLastError() == ERROR_PROC_NOT_FOUND || GetLastError() == ERROR_INVALID_HANDLE,
144         "Expected ERROR_PROC_NOT_FOUND or ERROR_INVALID_HANDLE(win9x), got %08lx\n", GetLastError());
145
146     SetLastError(0xdeadbeef);
147     fp = GetProcAddress((HMODULE)0xdeadbeef, "non_ex_call");
148     ok( !fp, "non_ex_call should not be found\n");
149     ok( GetLastError() == ERROR_MOD_NOT_FOUND || GetLastError() == ERROR_INVALID_HANDLE,
150         "Expected ERROR_MOD_NOT_FOUND or ERROR_INVALID_HANDLE(win9x), got %08lx\n", GetLastError());
151 }
152
153 START_TEST(module)
154 {
155     WCHAR filenameW[MAX_PATH];
156
157     /* Test if we can use GetModuleFileNameW */
158
159     SetLastError(0xdeadbeef);
160     GetModuleFileNameW(NULL, filenameW, MAX_PATH);
161     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
162     {
163         trace("GetModuleFileNameW not existing on this platform, skipping W-calls\n");
164         is_unicode_enabled = FALSE;
165     }
166
167     testGetModuleFileName(NULL);
168     testGetModuleFileName("kernel32.dll");
169     testGetModuleFileName_Wrong();
170
171     testLoadLibraryA();
172     testLoadLibraryA_Wrong();
173     testGetProcAddress_Wrong();
174 }