1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Test for GetDeviceDriverFileName & GetDeviceDriverBaseName
5  * PROGRAMMER:      Pierre Schweitzer
6  */
7 
8 #include <apitest.h>
9 #include <psapi.h>
10 #include <tlhelp32.h>
11 #include <stdio.h>
12 
13 typedef struct
14 {
15     LPVOID ImageBase;
16     CHAR Path[255];
17     DWORD Len;
18 } TEST_MODULE_INFO;
19 
20 static LPVOID IntGetImageBase(LPCSTR Image)
21 {
22     HANDLE Snap;
23     MODULEENTRY32 Module;
24 
25     Snap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0);
26     if (Snap == INVALID_HANDLE_VALUE)
27     {
28         return NULL;
29     }
30 
31     Module.dwSize = sizeof(MODULEENTRY32);
32     if(!Module32First(Snap, &Module))
33     {
34         CloseHandle(Snap);
35         return NULL;
36     }
37 
38     do
39     {
40         if (lstrcmpiA(Module.szExePath, Image) == 0)
41         {
42             CloseHandle(Snap);
43             return (LPVOID)Module.modBaseAddr;
44         }
45     } while(Module32Next(Snap, &Module));
46 
47     CloseHandle(Snap);
48     return NULL;
49 }
50 
51 static BOOLEAN IntGetModuleInformation(LPCSTR Module, BOOLEAN IsDriver, BOOLEAN IsProcMod, BOOLEAN BaseName, TEST_MODULE_INFO * Info)
52 {
53     CHAR System[255];
54     UINT Len;
55 
56     memset(Info, 0, sizeof(TEST_MODULE_INFO));
57 
58     /* Get system path */
59     Len = GetSystemWindowsDirectory(System, 255);
60     if (Len > 255 || Len == 0)
61     {
62         printf("GetSystemWindowsDirectory failed\n");
63         return FALSE;
64     }
65 
66     /* Make path to module */
67     strcat(System, "\\system32\\");
68     if (IsDriver) strcat(System, "drivers\\");
69     strcat(System, Module);
70 
71     /* Get base address */
72     if (IsProcMod)
73     {
74         Info->ImageBase = IntGetImageBase(System);
75         if (!Info->ImageBase)
76         {
77             printf("IntGetImageBase failed\n");
78             return FALSE;
79         }
80     }
81     else
82     {
83         /* FIXME */
84         printf("Not supported yet!\n");
85         return FALSE;
86     }
87 
88     if (BaseName)
89     {
90         strcpy(Info->Path, Module);
91         Info->Len = lstrlenA(Info->Path);
92     }
93     else
94     {
95         /* Skip disk */
96         strcpy(Info->Path, System + 2);
97         Info->Len = lstrlenA(Info->Path);
98     }
99 
100     return TRUE;
101 }
102 
103 START_TEST(GetDeviceDriverFileName)
104 {
105     DWORD Len;
106     CHAR FileName[255];
107     TEST_MODULE_INFO ModInfo;
108 
109     SetLastError(0xDEADBEEF);
110     Len = GetDeviceDriverFileNameA(0, FileName, 255);
111     ok(Len == 0, "Len: %lu\n", Len);
112     ok(GetLastError() == ERROR_INVALID_HANDLE, "Error: %lx\n", GetLastError());
113 
114     if (IntGetModuleInformation("ntdll.dll", FALSE, TRUE, FALSE, &ModInfo))
115     {
116         SetLastError(0xDEADBEEF);
117         Len = GetDeviceDriverFileNameA(ModInfo.ImageBase, FileName, 255);
118         ok(Len == ModInfo.Len, "Len: %lu\n", Len);
119         ok(GetLastError() == 0xDEADBEEF, "Error: %lx\n", GetLastError());
120         ok(lstrcmpiA(ModInfo.Path, FileName) == 0, "File name: %s\n", FileName);
121 
122         /* Test with too small buffer */
123         SetLastError(0xDEADBEEF);
124         ModInfo.Len--;
125         ModInfo.Path[ModInfo.Len] = 0;
126         FileName[ModInfo.Len] = 0;
127         Len = GetDeviceDriverFileNameA(ModInfo.ImageBase, FileName, ModInfo.Len);
128         ok(Len == ModInfo.Len, "Len: %lu\n", Len);
129         ok(GetLastError() == 0xDEADBEEF, "Error: %lx\n", GetLastError());
130         ok(lstrcmpiA(ModInfo.Path, FileName) == 0, "File name: %s\n", FileName);
131     }
132     else
133     {
134         skip("Couldn't find info about ntdll.dll\n");
135     }
136 
137     if (IntGetModuleInformation("msvcrt.dll", FALSE, TRUE, FALSE, &ModInfo))
138     {
139         SetLastError(0xDEADBEEF);
140         Len = GetDeviceDriverFileNameA(ModInfo.ImageBase, FileName, 255);
141         ok(Len == 0, "Len: %lu\n", Len);
142         ok(GetLastError() == ERROR_INVALID_HANDLE, "Error: %lx\n", GetLastError());
143     }
144     else
145     {
146         skip("Couldn't find info about msvcrt.dll\n");
147     }
148 
149     if (IntGetModuleInformation("psapi.dll", FALSE, TRUE, FALSE, &ModInfo))
150     {
151         SetLastError(0xDEADBEEF);
152         Len = GetDeviceDriverFileNameA(ModInfo.ImageBase, FileName, 255);
153         ok(Len == 0, "Len: %lu\n", Len);
154         ok(GetLastError() == ERROR_INVALID_HANDLE, "Error: %lx\n", GetLastError());
155     }
156     else
157     {
158         skip("Couldn't find info about psapi.dll\n");
159     }
160 }
161 
162 START_TEST(GetDeviceDriverBaseName)
163 {
164     DWORD Len;
165     CHAR FileName[255];
166     TEST_MODULE_INFO ModInfo;
167 
168     SetLastError(0xDEADBEEF);
169     Len = GetDeviceDriverBaseNameA(0, FileName, 255);
170     ok(Len == 0, "Len: %lu\n", Len);
171     ok(GetLastError() == ERROR_INVALID_HANDLE, "Error: %lx\n", GetLastError());
172 
173     if (IntGetModuleInformation("ntdll.dll", FALSE, TRUE, TRUE, &ModInfo))
174     {
175         SetLastError(0xDEADBEEF);
176         Len = GetDeviceDriverBaseNameA(ModInfo.ImageBase, FileName, 255);
177         ok(Len == ModInfo.Len, "Len: %lu\n", Len);
178         ok(GetLastError() == 0xDEADBEEF, "Error: %lx\n", GetLastError());
179         ok(lstrcmpiA(ModInfo.Path, FileName) == 0, "File name: %s\n", FileName);
180 
181         /* Test with too small buffer */
182         SetLastError(0xDEADBEEF);
183         ModInfo.Len--;
184         ModInfo.Path[ModInfo.Len] = 0;
185         FileName[ModInfo.Len] = 0;
186         Len = GetDeviceDriverBaseNameA(ModInfo.ImageBase, FileName, ModInfo.Len);
187         ok(Len == ModInfo.Len, "Len: %lu\n", Len);
188         ok(GetLastError() == 0xDEADBEEF, "Error: %lx\n", GetLastError());
189         ok(lstrcmpiA(ModInfo.Path, FileName) == 0, "File name: %s\n", FileName);
190     }
191     else
192     {
193         skip("Couldn't find info about ntdll.dll\n");
194     }
195 
196     if (IntGetModuleInformation("msvcrt.dll", FALSE, TRUE, TRUE, &ModInfo))
197     {
198         SetLastError(0xDEADBEEF);
199         Len = GetDeviceDriverBaseNameA(ModInfo.ImageBase, FileName, 255);
200         ok(Len == 0, "Len: %lu\n", Len);
201         ok(GetLastError() == ERROR_INVALID_HANDLE, "Error: %lx\n", GetLastError());
202     }
203     else
204     {
205         skip("Couldn't find info about msvcrt.dll\n");
206     }
207 
208     if (IntGetModuleInformation("psapi.dll", FALSE, TRUE, TRUE, &ModInfo))
209     {
210         SetLastError(0xDEADBEEF);
211         Len = GetDeviceDriverBaseNameA(ModInfo.ImageBase, FileName, 255);
212         ok(Len == 0, "Len: %lu\n", Len);
213         ok(GetLastError() == ERROR_INVALID_HANDLE, "Error: %lx\n", GetLastError());
214     }
215     else
216     {
217         skip("Couldn't find info about psapi.dll\n");
218     }
219 }
220