- Add some checks to prevent crashes in unexpected situations and add useful error...
[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 BOOL ReturnValue = FALSE;
70 DWORD DataLength;
71 DWORD Length;
72 PCHAR Password = NULL;
73 PCHAR UserName = NULL;
74 WCHAR ConfigFile[MAX_PATH];
75
76 /* We only need this if the results are going to be submitted */
77 if(!AppOptions.Submit)
78 {
79 ReturnValue = TRUE;
80 goto Cleanup;
81 }
82
83 /* Build the path to the configuration file from the application's path */
84 GetModuleFileNameW(NULL, ConfigFile, MAX_PATH);
85 Length = wcsrchr(ConfigFile, '\\') - ConfigFile;
86 wcscpy(&ConfigFile[Length], L"\\rosautotest.ini");
87
88 /* Check if it exists */
89 if(GetFileAttributesW(ConfigFile) == INVALID_FILE_ATTRIBUTES)
90 {
91 StringOut("Missing \"rosautotest.ini\" configuration file!\n");
92 goto Cleanup;
93 }
94
95 /* Get the required length of the authentication request string */
96 DataLength = sizeof(UserNameProp) - 1;
97 Length = IntGetINIValueA(L"Login", L"UserName", ConfigFile, &UserName);
98
99 if(!Length)
100 {
101 StringOut("UserName is missing in the configuration file\n");
102 goto Cleanup;
103 }
104
105 /* Some characters might need to be escaped and an escaped character takes 3 bytes */
106 DataLength += 3 * Length;
107
108 DataLength += sizeof(PasswordProp) - 1;
109 Length = IntGetINIValueA(L"Login", L"Password", ConfigFile, &Password);
110
111 if(!Length)
112 {
113 StringOut("Password is missing in the configuration file\n");
114 goto Cleanup;
115 }
116
117 DataLength += 3 * Length;
118
119 /* Build the string */
120 AuthenticationRequestString = HeapAlloc(hProcessHeap, 0, DataLength + 1);
121
122 strcpy(AuthenticationRequestString, UserNameProp);
123 EscapeString(&AuthenticationRequestString[strlen(AuthenticationRequestString)], UserName);
124
125 strcat(AuthenticationRequestString, PasswordProp);
126 EscapeString(&AuthenticationRequestString[strlen(AuthenticationRequestString)], Password);
127
128 ReturnValue = TRUE;
129
130 Cleanup:
131 if(UserName)
132 HeapFree(hProcessHeap, 0, UserName);
133
134 if(Password)
135 HeapFree(hProcessHeap, 0, Password);
136
137 return ReturnValue;
138 }
139
140 /**
141 * Determines on which platform we're running on.
142 * Prepares the appropriate request strings needed if we want to submit test results to the web service.
143 */
144 static BOOL
145 IntGetBuildAndPlatform()
146 {
147 const CHAR PlatformProp[] = "&platform=";
148 const CHAR RevisionProp[] = "&revision=";
149
150 CHAR BuildNo[BUILDNO_LENGTH];
151 CHAR Platform[PLATFORM_LENGTH];
152 CHAR PlatformArchitecture[3];
153 CHAR ProductType;
154 DWORD DataLength;
155 GETSYSINFO GetSysInfo;
156 HANDLE hKernel32;
157 OSVERSIONINFOEXW os;
158 SYSTEM_INFO si;
159 WCHAR WindowsDirectory[MAX_PATH];
160
161 /* Get the build from the define */
162 _ultoa(KERNEL_VERSION_BUILD_HEX, BuildNo, 10);
163
164 /* Check if we are running under ReactOS from the SystemRoot directory */
165 GetWindowsDirectoryW(WindowsDirectory, MAX_PATH);
166
167 if(!_wcsnicmp(&WindowsDirectory[3], L"reactos", 7))
168 {
169 /* Yes, we are most-probably under ReactOS */
170 strcpy(Platform, "reactos");
171 }
172 else
173 {
174 /* No, then use the info from GetVersionExW */
175 os.dwOSVersionInfoSize = sizeof(os);
176
177 if(!GetVersionExW((LPOSVERSIONINFOW)&os))
178 {
179 StringOut("GetVersionExW failed\n");
180 return FALSE;
181 }
182
183 if(os.dwMajorVersion < 5)
184 {
185 StringOut("Application requires at least Windows 2000!\n");
186 return FALSE;
187 }
188
189 if(os.wProductType == VER_NT_WORKSTATION)
190 ProductType = 'w';
191 else
192 ProductType = 's';
193
194 /* Print all necessary identification information into the Platform string */
195 sprintf(Platform, "%lu.%lu.%lu.%u.%u.%c", os.dwMajorVersion, os.dwMinorVersion, os.dwBuildNumber, os.wServicePackMajor, os.wServicePackMinor, ProductType);
196 }
197
198 /* We also need to know about the processor architecture.
199 To retrieve this information accurately, check whether "GetNativeSystemInfo" is exported and use it then, otherwise fall back to "GetSystemInfo". */
200 hKernel32 = GetModuleHandleW(L"KERNEL32.DLL");
201 GetSysInfo = (GETSYSINFO)GetProcAddress(hKernel32, "GetNativeSystemInfo");
202
203 if(!GetSysInfo)
204 GetSysInfo = (GETSYSINFO)GetProcAddress(hKernel32, "GetSystemInfo");
205
206 GetSysInfo(&si);
207
208 PlatformArchitecture[0] = '.';
209 _ultoa(si.wProcessorArchitecture, &PlatformArchitecture[1], 10);
210 PlatformArchitecture[2] = 0;
211 strcat(Platform, PlatformArchitecture);
212
213 /* Get the required length of the system info request string */
214 DataLength = sizeof(RevisionProp) - 1;
215 DataLength += strlen(BuildNo);
216 DataLength += sizeof(PlatformProp) - 1;
217 DataLength += strlen(Platform);
218
219 /* Now build the string */
220 SystemInfoRequestString = HeapAlloc(hProcessHeap, 0, DataLength + 1);
221 strcpy(SystemInfoRequestString, RevisionProp);
222 strcat(SystemInfoRequestString, BuildNo);
223 strcat(SystemInfoRequestString, PlatformProp);
224 strcat(SystemInfoRequestString, Platform);
225
226 return TRUE;
227 }
228
229 /**
230 * Prints the application usage.
231 */
232 static VOID
233 IntPrintUsage()
234 {
235 printf("rosautotest - ReactOS Automatic Testing Utility\n");
236 printf("Usage: rosautotest [options] [module] [test]\n");
237 printf(" options:\n");
238 printf(" /? - Shows this help\n");
239 printf(" /s - Shut down the system after finishing the tests\n");
240 printf(" /w - Submit the results to the webservice\n");
241 printf(" Requires a \"rosautotest.ini\" with valid login data.\n");
242 printf("\n");
243 printf(" module:\n");
244 printf(" The module to be tested (i.e. \"advapi32\")\n");
245 printf(" If this parameter is specified without any test parameter,\n");
246 printf(" all tests of the specified module are run.\n");
247 printf("\n");
248 printf(" test:\n");
249 printf(" The test to be run. Needs to be a test of the specified module.\n");
250 }
251
252 /**
253 * Main entry point
254 */
255 int
256 wmain(int argc, wchar_t* argv[])
257 {
258 int ReturnValue = 0;
259 UINT i;
260
261 hProcessHeap = GetProcessHeap();
262
263 /* Parse the command line arguments */
264 for(i = 1; i < (UINT)argc; i++)
265 {
266 if(argv[i][0] == '-' || argv[i][0] == '/')
267 {
268 switch(argv[i][1])
269 {
270 case 's':
271 AppOptions.Shutdown = TRUE;
272 break;
273
274 case 'w':
275 AppOptions.Submit = TRUE;
276 break;
277
278 default:
279 ReturnValue = 1;
280 /* Fall through */
281
282 case '?':
283 IntPrintUsage();
284 goto Cleanup;
285 }
286 }
287 else
288 {
289 size_t Length;
290
291 /* Which parameter is this? */
292 if(!AppOptions.Module)
293 {
294 /* Copy the parameter */
295 Length = (wcslen(argv[i]) + 1) * sizeof(WCHAR);
296 AppOptions.Module = HeapAlloc(hProcessHeap, 0, Length);
297 memcpy(AppOptions.Module, argv[i], Length);
298 }
299 else if(!AppOptions.Test)
300 {
301 /* Copy the parameter converted to ASCII */
302 Length = WideCharToMultiByte(CP_ACP, 0, argv[i], -1, NULL, 0, NULL, NULL);
303 AppOptions.Test = HeapAlloc(hProcessHeap, 0, Length);
304 WideCharToMultiByte(CP_ACP, 0, argv[i], -1, AppOptions.Test, Length, NULL, NULL);
305 }
306 else
307 {
308 ReturnValue = 1;
309 IntPrintUsage();
310 goto Cleanup;
311 }
312 }
313 }
314
315 if(!IntGetConfigurationValues() || !IntGetBuildAndPlatform() || !RunWineTests())
316 {
317 ReturnValue = 1;
318 goto Cleanup;
319 }
320
321 /* For sysreg */
322 OutputDebugStringA("SYSREG_CHECKPOINT:THIRDBOOT_COMPLETE\n");
323
324 Cleanup:
325 if(AppOptions.Module)
326 HeapFree(hProcessHeap, 0, AppOptions.Module);
327
328 if(AppOptions.Test)
329 HeapFree(hProcessHeap, 0, AppOptions.Test);
330
331 if(AuthenticationRequestString)
332 HeapFree(hProcessHeap, 0, AuthenticationRequestString);
333
334 if(SystemInfoRequestString)
335 HeapFree(hProcessHeap, 0, SystemInfoRequestString);
336
337 /* Shut down the system if requested */
338 if(AppOptions.Shutdown && !ShutdownSystem())
339 ReturnValue = 1;
340
341 return ReturnValue;
342 }