- It actually wasn't a very good idea to authenticate against the MD5 password hash...
[reactos.git] / rostests / rosautotest / main.c
1 /*
2 * PROJECT: ReactOS Automatic Testing Utility
3 * LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
4 * PURPOSE: Main implementation file
5 * COPYRIGHT: Copyright 2008-2009 Colin Finck <colin@reactos.org>
6 */
7
8 #include "precomp.h"
9
10 typedef void (WINAPI *GETSYSINFO)(LPSYSTEM_INFO);
11
12 APP_OPTIONS AppOptions = {0};
13 HANDLE hProcessHeap;
14 PCHAR AuthenticationRequestString = NULL;
15 PCHAR SystemInfoRequestString = NULL;
16
17 /**
18 * Gets a value from a specified INI file and returns it converted to ASCII.
19 *
20 * @param AppName
21 * The INI section to look in (lpAppName parameter passed to GetPrivateProfileStringW)
22 *
23 * @param KeyName
24 * The key to look for in the specified section (lpKeyName parameter passed to GetPrivateProfileStringW)
25 *
26 * @param FileName
27 * The path to the INI file
28 *
29 * @param ReturnedValue
30 * Pointer to a CHAR pointer, which will receive the read and converted value.
31 * The caller needs to HeapFree that value manually.
32 *
33 * @return
34 * Returns the string length of the read value (in characters) or zero if we didn't get any value.
35 */
36 static DWORD
37 IntGetINIValueA(PCWCH AppName, PCWCH KeyName, PCWCH FileName, char** ReturnedValue)
38 {
39 DWORD Length;
40 WCHAR Buffer[2048];
41
42 /* Load the value into a temporary Unicode buffer */
43 Length = GetPrivateProfileStringW(AppName, KeyName, NULL, Buffer, sizeof(Buffer) / sizeof(WCHAR), FileName);
44
45 if(!Length)
46 return 0;
47
48 /* Convert the string to ANSI charset */
49 *ReturnedValue = HeapAlloc(hProcessHeap, 0, Length + 1);
50 WideCharToMultiByte(CP_ACP, 0, Buffer, Length + 1, *ReturnedValue, Length + 1, NULL, NULL);
51
52 return Length;
53 }
54
55 /**
56 * Gets the username and password from the "rosautotest.ini" file if the user enabled submitting the results to the web service.
57 * The "rosautotest.ini" file should look like this:
58 *
59 * [Login]
60 * UserName=TestMan
61 * Password=TestPassword
62 */
63 static BOOL
64 IntGetConfigurationValues()
65 {
66 const CHAR PasswordProp[] = "&password=";
67 const CHAR UserNameProp[] = "&username=";
68
69 DWORD DataLength;
70 DWORD Length;
71 PCHAR Password;
72 PCHAR UserName;
73 WCHAR ConfigFile[MAX_PATH];
74
75 /* We only need this if the results are going to be submitted */
76 if(!AppOptions.Submit)
77 return TRUE;
78
79 /* Build the path to the configuration file from the application's path */
80 GetModuleFileNameW(NULL, ConfigFile, MAX_PATH);
81 Length = wcsrchr(ConfigFile, '\\') - ConfigFile;
82 wcscpy(&ConfigFile[Length], L"\\rosautotest.ini");
83
84 /* Check if it exists */
85 if(GetFileAttributesW(ConfigFile) == INVALID_FILE_ATTRIBUTES)
86 {
87 StringOut("Missing \"rosautotest.ini\" configuration file!\n");
88 return FALSE;
89 }
90
91 /* Get the required length of the authentication request string */
92 DataLength = sizeof(UserNameProp) - 1;
93 Length = IntGetINIValueA(L"Login", L"UserName", ConfigFile, &UserName);
94
95 if(!Length)
96 {
97 StringOut("UserName is missing in the configuration file\n");
98 return FALSE;
99 }
100
101 /* Some characters might need to be escaped and an escaped character takes 3 bytes */
102 DataLength += 3 * Length;
103
104 DataLength += sizeof(PasswordProp) - 1;
105 Length = IntGetINIValueA(L"Login", L"Password", ConfigFile, &Password);
106
107 if(!Length)
108 {
109 StringOut("Password is missing in the configuration file\n");
110 return FALSE;
111 }
112
113 DataLength += 3 * Length;
114
115 /* Build the string */
116 AuthenticationRequestString = HeapAlloc(hProcessHeap, 0, DataLength + 1);
117
118 strcpy(AuthenticationRequestString, UserNameProp);
119 EscapeString(&AuthenticationRequestString[strlen(AuthenticationRequestString)], UserName);
120
121 strcat(AuthenticationRequestString, PasswordProp);
122 EscapeString(&AuthenticationRequestString[strlen(AuthenticationRequestString)], Password);
123
124 return TRUE;
125 }
126
127 /**
128 * Determines on which platform we're running on.
129 * Prepares the appropriate request strings needed if we want to submit test results to the web service.
130 */
131 static BOOL
132 IntGetBuildAndPlatform()
133 {
134 const CHAR PlatformProp[] = "&platform=";
135 const CHAR RevisionProp[] = "&revision=";
136
137 CHAR BuildNo[BUILDNO_LENGTH];
138 CHAR Platform[PLATFORM_LENGTH];
139 CHAR PlatformArchitecture[3];
140 CHAR ProductType;
141 DWORD DataLength;
142 GETSYSINFO GetSysInfo;
143 HANDLE hKernel32;
144 OSVERSIONINFOEXW os;
145 SYSTEM_INFO si;
146 WCHAR WindowsDirectory[MAX_PATH];
147
148 /* Get the build from the define */
149 _ultoa(KERNEL_VERSION_BUILD_HEX, BuildNo, 10);
150
151 /* Check if we are running under ReactOS from the SystemRoot directory */
152 GetWindowsDirectoryW(WindowsDirectory, MAX_PATH);
153
154 if(!_wcsnicmp(&WindowsDirectory[3], L"reactos", 7))
155 {
156 /* Yes, we are most-probably under ReactOS */
157 strcpy(Platform, "reactos");
158 }
159 else
160 {
161 /* No, then use the info from GetVersionExW */
162 os.dwOSVersionInfoSize = sizeof(os);
163
164 if(!GetVersionExW((LPOSVERSIONINFOW)&os))
165 {
166 StringOut("GetVersionExW failed\n");
167 return FALSE;
168 }
169
170 if(os.dwMajorVersion < 5)
171 {
172 StringOut("Application requires at least Windows 2000!\n");
173 return FALSE;
174 }
175
176 if(os.wProductType == VER_NT_WORKSTATION)
177 ProductType = 'w';
178 else
179 ProductType = 's';
180
181 /* Print all necessary identification information into the Platform string */
182 sprintf(Platform, "%lu.%lu.%lu.%u.%u.%c", os.dwMajorVersion, os.dwMinorVersion, os.dwBuildNumber, os.wServicePackMajor, os.wServicePackMinor, ProductType);
183 }
184
185 /* We also need to know about the processor architecture.
186 To retrieve this information accurately, check whether "GetNativeSystemInfo" is exported and use it then, otherwise fall back to "GetSystemInfo". */
187 hKernel32 = GetModuleHandleW(L"KERNEL32.DLL");
188 GetSysInfo = (GETSYSINFO)GetProcAddress(hKernel32, "GetNativeSystemInfo");
189
190 if(!GetSysInfo)
191 GetSysInfo = (GETSYSINFO)GetProcAddress(hKernel32, "GetSystemInfo");
192
193 GetSysInfo(&si);
194
195 PlatformArchitecture[0] = '.';
196 _ultoa(si.wProcessorArchitecture, &PlatformArchitecture[1], 10);
197 PlatformArchitecture[2] = 0;
198 strcat(Platform, PlatformArchitecture);
199
200 /* Get the required length of the system info request string */
201 DataLength = sizeof(RevisionProp) - 1;
202 DataLength += strlen(BuildNo);
203 DataLength += sizeof(PlatformProp) - 1;
204 DataLength += strlen(Platform);
205
206 /* Now build the string */
207 SystemInfoRequestString = HeapAlloc(hProcessHeap, 0, DataLength + 1);
208 strcpy(SystemInfoRequestString, RevisionProp);
209 strcat(SystemInfoRequestString, BuildNo);
210 strcat(SystemInfoRequestString, PlatformProp);
211 strcat(SystemInfoRequestString, Platform);
212
213 return TRUE;
214 }
215
216 /**
217 * Prints the application usage.
218 */
219 static VOID
220 IntPrintUsage()
221 {
222 printf("rosautotest - ReactOS Automatic Testing Utility\n");
223 printf("Usage: rosautotest [options] [module] [test]\n");
224 printf(" options:\n");
225 printf(" /? - Shows this help\n");
226 printf(" /s - Shut down the system after finishing the tests\n");
227 printf(" /w - Submit the results to the webservice\n");
228 printf(" Requires a \"rosautotest.ini\" with valid login data.\n");
229 printf("\n");
230 printf(" module:\n");
231 printf(" The module to be tested (i.e. \"advapi32\")\n");
232 printf(" If this parameter is specified without any test parameter,\n");
233 printf(" all tests of the specified module are run.\n");
234 printf("\n");
235 printf(" test:\n");
236 printf(" The test to be run. Needs to be a test of the specified module.\n");
237 }
238
239 /**
240 * Main entry point
241 */
242 int
243 wmain(int argc, wchar_t* argv[])
244 {
245 int Result = 0;
246 UINT i;
247
248 hProcessHeap = GetProcessHeap();
249
250 /* Parse the command line arguments */
251 for(i = 1; i < (UINT)argc; i++)
252 {
253 if(argv[i][0] == '-' || argv[i][0] == '/')
254 {
255 switch(argv[i][1])
256 {
257 case 's':
258 AppOptions.Shutdown = TRUE;
259 break;
260
261 case 'w':
262 AppOptions.Submit = TRUE;
263 break;
264
265 default:
266 Result = 1;
267 /* Fall through */
268
269 case '?':
270 IntPrintUsage();
271 goto End;
272 }
273 }
274 else
275 {
276 size_t Length;
277
278 /* Which parameter is this? */
279 if(!AppOptions.Module)
280 {
281 /* Copy the parameter */
282 Length = (wcslen(argv[i]) + 1) * sizeof(WCHAR);
283 AppOptions.Module = HeapAlloc(hProcessHeap, 0, Length);
284 memcpy(AppOptions.Module, argv[i], Length);
285 }
286 else if(!AppOptions.Test)
287 {
288 /* Copy the parameter converted to ASCII */
289 Length = WideCharToMultiByte(CP_ACP, 0, argv[i], -1, NULL, 0, NULL, NULL);
290 AppOptions.Test = HeapAlloc(hProcessHeap, 0, Length);
291 WideCharToMultiByte(CP_ACP, 0, argv[i], -1, AppOptions.Test, Length, NULL, NULL);
292 }
293 else
294 {
295 Result = 1;
296 IntPrintUsage();
297 goto End;
298 }
299 }
300 }
301
302 if(!IntGetConfigurationValues() || !IntGetBuildAndPlatform() || !RunWineTests())
303 {
304 Result = 1;
305 goto End;
306 }
307
308 /* For sysreg */
309 OutputDebugStringA("SYSREG_CHECKPOINT:THIRDBOOT_COMPLETE\n");
310
311 End:
312 /* Cleanup */
313 if(AppOptions.Module)
314 HeapFree(hProcessHeap, 0, AppOptions.Module);
315
316 if(AppOptions.Test)
317 HeapFree(hProcessHeap, 0, AppOptions.Test);
318
319 if(AuthenticationRequestString)
320 HeapFree(hProcessHeap, 0, AuthenticationRequestString);
321
322 if(SystemInfoRequestString)
323 HeapFree(hProcessHeap, 0, SystemInfoRequestString);
324
325 /* Shut down the system if requested */
326 if(AppOptions.Shutdown && !ShutdownSystem())
327 Result = 1;
328
329 return Result;
330 }