2011-06-09 15:14:02 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS kernel-mode tests
|
|
|
|
* LICENSE: GPLv2+ - See COPYING in the top level directory
|
|
|
|
* PURPOSE: Kernel-Mode Test Suite Loader Application
|
|
|
|
* PROGRAMMER: Thomas Faber <thfabba@gmx.de>
|
|
|
|
*/
|
|
|
|
|
2011-08-01 21:53:52 +00:00
|
|
|
#define KMT_DEFINE_TEST_FUNCTIONS
|
|
|
|
#include <kmt_test.h>
|
|
|
|
|
|
|
|
#include "kmtest.h"
|
|
|
|
#include <kmt_public.h>
|
2011-06-09 15:14:02 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
#define SERVICE_NAME L"Kmtest"
|
|
|
|
#define SERVICE_PATH L"kmtest_drv.sys"
|
2011-07-10 08:03:15 +00:00
|
|
|
#define SERVICE_DESCRIPTION L"ReactOS Kernel-Mode Test Suite Driver"
|
2011-07-03 18:53:26 +00:00
|
|
|
|
2011-07-28 09:00:56 +00:00
|
|
|
#define RESULTBUFFER_SIZE (1024 * 1024)
|
2011-07-03 18:53:26 +00:00
|
|
|
|
2011-07-10 08:03:15 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
KMT_DO_NOTHING,
|
|
|
|
KMT_LIST_TESTS,
|
2011-07-11 18:03:19 +00:00
|
|
|
KMT_LIST_ALL_TESTS,
|
2011-07-10 08:03:15 +00:00
|
|
|
KMT_RUN_TEST,
|
|
|
|
} KMT_OPERATION;
|
|
|
|
|
2011-07-06 18:55:21 +00:00
|
|
|
HANDLE KmtestHandle;
|
2011-07-10 08:03:15 +00:00
|
|
|
SC_HANDLE KmtestServiceHandle;
|
2011-07-03 18:53:26 +00:00
|
|
|
PCSTR ErrorFileAndLine = "No error";
|
|
|
|
|
2011-07-11 18:03:19 +00:00
|
|
|
static void OutputError(IN DWORD Error);
|
|
|
|
static DWORD ListTests(IN BOOLEAN IncludeHidden);
|
|
|
|
static PKMT_TESTFUNC FindTest(IN PCSTR TestName);
|
|
|
|
static DWORD OutputResult(IN PCSTR TestName);
|
|
|
|
static DWORD RunTest(IN PCSTR TestName);
|
2011-07-03 18:53:26 +00:00
|
|
|
int __cdecl main(int ArgCount, char **Arguments);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name OutputError
|
|
|
|
*
|
|
|
|
* Output an error message to the console.
|
|
|
|
*
|
|
|
|
* @param Error
|
|
|
|
* Win32 error code
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
void
|
|
|
|
OutputError(
|
2011-07-11 18:03:19 +00:00
|
|
|
IN DWORD Error)
|
2011-06-09 15:14:02 +00:00
|
|
|
{
|
2011-07-03 18:53:26 +00:00
|
|
|
PSTR Message;
|
2011-06-09 15:14:02 +00:00
|
|
|
if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
2011-07-03 18:53:26 +00:00
|
|
|
NULL, Error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&Message, 0, NULL))
|
2011-06-09 15:14:02 +00:00
|
|
|
{
|
2011-07-03 18:53:26 +00:00
|
|
|
fprintf(stderr, "%s: Could not retrieve error message (error 0x%08lx). Original error: 0x%08lx\n",
|
|
|
|
ErrorFileAndLine, GetLastError(), Error);
|
|
|
|
return;
|
2011-06-09 15:14:02 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
fprintf(stderr, "%s: error 0x%08lx: %s\n", ErrorFileAndLine, Error, Message);
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
LocalFree(Message);
|
2011-06-09 15:14:02 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 11:33:17 +00:00
|
|
|
/**
|
|
|
|
* @name CompareTestNames
|
|
|
|
*
|
|
|
|
* strcmp that skips a leading '-' on either string if present
|
|
|
|
*
|
|
|
|
* @param Str1
|
|
|
|
* @param Str2
|
|
|
|
* @return see strcmp
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
INT
|
|
|
|
CompareTestNames(
|
|
|
|
IN PCSTR Str1,
|
|
|
|
IN PCSTR Str2)
|
|
|
|
{
|
|
|
|
if (*Str1 == '-')
|
|
|
|
++Str1;
|
|
|
|
if (*Str2 == '-')
|
|
|
|
++Str2;
|
|
|
|
while (*Str1 && *Str1 == *Str2)
|
|
|
|
{
|
|
|
|
++Str1;
|
|
|
|
++Str2;
|
|
|
|
}
|
|
|
|
return *Str1 - *Str2;
|
|
|
|
}
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
/**
|
|
|
|
* @name ListTests
|
|
|
|
*
|
|
|
|
* Output the list of tests to the console.
|
|
|
|
* The list will comprise tests as listed by the driver
|
|
|
|
* in addition to user-mode tests in TestList.
|
|
|
|
*
|
2011-07-11 18:03:19 +00:00
|
|
|
* @param IncludeHidden
|
|
|
|
* TRUE to include "hidden" tests prefixed with a '-'
|
|
|
|
*
|
2011-07-03 18:53:26 +00:00
|
|
|
* @return Win32 error code
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
DWORD
|
2011-07-11 18:03:19 +00:00
|
|
|
ListTests(
|
|
|
|
IN BOOLEAN IncludeHidden)
|
2011-06-09 15:14:02 +00:00
|
|
|
{
|
2011-07-03 18:53:26 +00:00
|
|
|
DWORD Error = ERROR_SUCCESS;
|
|
|
|
CHAR Buffer[1024];
|
|
|
|
DWORD BytesRead;
|
|
|
|
PCSTR TestName = Buffer;
|
|
|
|
PCKMT_TEST TestEntry = TestList;
|
|
|
|
PCSTR NextTestName;
|
2011-06-13 17:50:07 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
puts("Valid test names:");
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
// get test list from driver
|
|
|
|
if (!DeviceIoControl(KmtestHandle, IOCTL_KMTEST_GET_TESTS, NULL, 0, Buffer, sizeof Buffer, &BytesRead, NULL))
|
|
|
|
error_goto(Error, cleanup);
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
// output test list plus user-mode tests
|
|
|
|
while (TestEntry->TestName || *TestName)
|
2011-06-09 15:14:02 +00:00
|
|
|
{
|
2011-07-03 18:53:26 +00:00
|
|
|
if (!TestEntry->TestName)
|
|
|
|
{
|
|
|
|
NextTestName = TestName;
|
|
|
|
TestName += strlen(TestName) + 1;
|
|
|
|
}
|
|
|
|
else if (!*TestName)
|
|
|
|
{
|
|
|
|
NextTestName = TestEntry->TestName;
|
|
|
|
++TestEntry;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-22 11:33:17 +00:00
|
|
|
INT Result = CompareTestNames(TestEntry->TestName, TestName);
|
2011-07-03 18:53:26 +00:00
|
|
|
|
|
|
|
if (Result == 0)
|
|
|
|
{
|
|
|
|
NextTestName = TestEntry->TestName;
|
|
|
|
TestName += strlen(TestName) + 1;
|
|
|
|
++TestEntry;
|
|
|
|
}
|
|
|
|
else if (Result < 0)
|
|
|
|
{
|
|
|
|
NextTestName = TestEntry->TestName;
|
|
|
|
++TestEntry;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NextTestName = TestName;
|
|
|
|
TestName += strlen(TestName) + 1;
|
|
|
|
}
|
|
|
|
}
|
2011-07-11 18:03:19 +00:00
|
|
|
|
|
|
|
if (IncludeHidden && NextTestName[0] == '-')
|
|
|
|
++NextTestName;
|
|
|
|
|
|
|
|
if (NextTestName[0] != '-')
|
|
|
|
printf(" %s\n", NextTestName);
|
2011-06-09 15:14:02 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
cleanup:
|
|
|
|
return Error;
|
|
|
|
}
|
2011-06-13 17:50:07 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
/**
|
|
|
|
* @name FindTest
|
|
|
|
*
|
|
|
|
* Find a test in TestList by name.
|
|
|
|
*
|
|
|
|
* @param TestName
|
|
|
|
* Name of the test to look for. Case sensitive
|
|
|
|
*
|
|
|
|
* @return pointer to test function, or NULL if not found
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
PKMT_TESTFUNC
|
|
|
|
FindTest(
|
2011-07-11 18:03:19 +00:00
|
|
|
IN PCSTR TestName)
|
2011-07-03 18:53:26 +00:00
|
|
|
{
|
|
|
|
PCKMT_TEST TestEntry = TestList;
|
2011-06-19 09:23:03 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
for (TestEntry = TestList; TestEntry->TestName; ++TestEntry)
|
2011-06-09 15:14:02 +00:00
|
|
|
{
|
2011-07-03 18:53:26 +00:00
|
|
|
PCSTR TestEntryName = TestEntry->TestName;
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
// skip leading '-' if present
|
|
|
|
if (*TestEntryName == '-')
|
|
|
|
++TestEntryName;
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
if (!lstrcmpA(TestEntryName, TestName))
|
|
|
|
break;
|
|
|
|
}
|
2011-06-13 17:50:07 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
return TestEntry->TestFunction;
|
2011-06-09 15:14:02 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
/**
|
|
|
|
* @name OutputResult
|
|
|
|
*
|
|
|
|
* Output the test results in ResultBuffer to the console.
|
|
|
|
*
|
|
|
|
* @param TestName
|
|
|
|
* Name of the test whose result is to be printed
|
|
|
|
*
|
|
|
|
* @return Win32 error code
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
DWORD
|
|
|
|
OutputResult(
|
2011-07-11 18:03:19 +00:00
|
|
|
IN PCSTR TestName)
|
2011-06-09 15:14:02 +00:00
|
|
|
{
|
2011-07-03 18:53:26 +00:00
|
|
|
DWORD Error = ERROR_SUCCESS;
|
|
|
|
DWORD BytesWritten;
|
2011-06-13 17:50:07 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
KmtFinishTest(TestName);
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
if (!WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), ResultBuffer->LogBuffer, ResultBuffer->LogBufferLength, &BytesWritten, NULL))
|
2011-07-04 19:47:49 +00:00
|
|
|
error(Error);
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
return Error;
|
|
|
|
}
|
2011-06-13 17:50:07 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
/**
|
|
|
|
* @name RunTest
|
|
|
|
*
|
|
|
|
* Run the named test and output its results.
|
|
|
|
*
|
|
|
|
* @param TestName
|
|
|
|
* Name of the test to run. Case sensitive
|
|
|
|
*
|
|
|
|
* @return Win32 error code
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
DWORD
|
|
|
|
RunTest(
|
2011-07-11 18:03:19 +00:00
|
|
|
IN PCSTR TestName)
|
2011-07-03 18:53:26 +00:00
|
|
|
{
|
|
|
|
DWORD Error = ERROR_SUCCESS;
|
|
|
|
PKMT_TESTFUNC TestFunction;
|
|
|
|
DWORD BytesRead;
|
|
|
|
|
2011-07-10 08:03:15 +00:00
|
|
|
assert(TestName != NULL);
|
|
|
|
|
2011-07-28 09:00:56 +00:00
|
|
|
if (!ResultBuffer)
|
|
|
|
{
|
|
|
|
ResultBuffer = KmtAllocateResultBuffer(RESULTBUFFER_SIZE);
|
|
|
|
if (!ResultBuffer)
|
|
|
|
error_goto(Error, cleanup);
|
|
|
|
if (!DeviceIoControl(KmtestHandle, IOCTL_KMTEST_SET_RESULTBUFFER, ResultBuffer, RESULTBUFFER_SIZE, NULL, 0, &BytesRead, NULL))
|
|
|
|
error_goto(Error, cleanup);
|
|
|
|
}
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
// check test list
|
|
|
|
TestFunction = FindTest(TestName);
|
|
|
|
|
|
|
|
if (TestFunction)
|
2011-06-09 15:14:02 +00:00
|
|
|
{
|
2011-07-03 18:53:26 +00:00
|
|
|
TestFunction();
|
2011-06-09 15:14:02 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-07-06 18:55:21 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
// not found in user-mode test list, call driver
|
2011-07-06 18:55:21 +00:00
|
|
|
Error = KmtRunKernelTest(TestName);
|
2011-06-09 15:14:02 +00:00
|
|
|
|
|
|
|
cleanup:
|
2011-07-04 19:47:49 +00:00
|
|
|
if (!Error)
|
2011-07-06 18:55:21 +00:00
|
|
|
Error = OutputResult(TestName);
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
return Error;
|
2011-06-09 15:14:02 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
/**
|
|
|
|
* @name main
|
|
|
|
*
|
|
|
|
* Program entry point
|
|
|
|
*
|
|
|
|
* @param ArgCount
|
|
|
|
* @param Arguments
|
|
|
|
*
|
|
|
|
* @return EXIT_SUCCESS on success, EXIT_FAILURE on failure
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(
|
|
|
|
int ArgCount,
|
|
|
|
char **Arguments)
|
2011-06-09 15:14:02 +00:00
|
|
|
{
|
2011-07-03 18:53:26 +00:00
|
|
|
INT Status = EXIT_SUCCESS;
|
|
|
|
DWORD Error = ERROR_SUCCESS;
|
|
|
|
PCSTR AppName = "kmtest.exe";
|
2011-07-10 08:03:15 +00:00
|
|
|
PCSTR TestName = NULL;
|
|
|
|
KMT_OPERATION Operation = KMT_DO_NOTHING;
|
2011-07-11 18:03:19 +00:00
|
|
|
BOOLEAN ShowHidden = FALSE;
|
2011-07-03 18:53:26 +00:00
|
|
|
|
|
|
|
Error = KmtServiceInit();
|
|
|
|
if (Error)
|
|
|
|
goto cleanup;
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
if (ArgCount >= 1)
|
|
|
|
AppName = Arguments[0];
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
if (ArgCount <= 1)
|
|
|
|
{
|
2011-07-10 08:03:15 +00:00
|
|
|
printf("Usage: %s <test_name> - run the specified test (creates/starts the driver(s) as appropriate)\n", AppName);
|
2011-07-03 18:53:26 +00:00
|
|
|
printf(" %s --list - list available tests\n", AppName);
|
2011-07-11 18:03:19 +00:00
|
|
|
printf(" %s --list-all - list available tests, including hidden\n", AppName);
|
2011-07-03 18:53:26 +00:00
|
|
|
printf(" %s <create|delete|start|stop> - manage the kmtest driver\n\n", AppName);
|
2011-07-10 08:03:15 +00:00
|
|
|
Operation = KMT_LIST_TESTS;
|
2011-06-09 15:14:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-03 18:53:26 +00:00
|
|
|
TestName = Arguments[1];
|
2011-07-10 08:03:15 +00:00
|
|
|
if (!lstrcmpA(TestName, "create"))
|
|
|
|
Error = KmtCreateService(SERVICE_NAME, SERVICE_PATH, SERVICE_DESCRIPTION, &KmtestServiceHandle);
|
|
|
|
else if (!lstrcmpA(TestName, "delete"))
|
|
|
|
Error = KmtDeleteService(SERVICE_NAME, &KmtestServiceHandle);
|
|
|
|
else if (!lstrcmpA(TestName, "start"))
|
|
|
|
Error = KmtStartService(SERVICE_NAME, &KmtestServiceHandle);
|
|
|
|
else if (!lstrcmpA(TestName, "stop"))
|
|
|
|
Error = KmtStopService(SERVICE_NAME, &KmtestServiceHandle);
|
|
|
|
|
|
|
|
else if (!lstrcmpA(TestName, "--list"))
|
|
|
|
Operation = KMT_LIST_TESTS;
|
2011-07-11 18:03:19 +00:00
|
|
|
else if (!lstrcmpA(TestName, "--list-all"))
|
|
|
|
Operation = KMT_LIST_ALL_TESTS;
|
2011-06-09 15:14:02 +00:00
|
|
|
else
|
2011-07-10 08:03:15 +00:00
|
|
|
Operation = KMT_RUN_TEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Operation)
|
|
|
|
{
|
|
|
|
Error = KmtCreateAndStartService(SERVICE_NAME, SERVICE_PATH, SERVICE_DESCRIPTION, &KmtestServiceHandle, FALSE);
|
|
|
|
if (Error)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
KmtestHandle = CreateFile(KMTEST_DEVICE_PATH, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
if (KmtestHandle == INVALID_HANDLE_VALUE)
|
|
|
|
error_goto(Error, cleanup);
|
|
|
|
|
|
|
|
switch (Operation)
|
|
|
|
{
|
2011-07-11 18:03:19 +00:00
|
|
|
case KMT_LIST_ALL_TESTS:
|
|
|
|
ShowHidden = TRUE;
|
|
|
|
/* fall through */
|
2011-07-10 08:03:15 +00:00
|
|
|
case KMT_LIST_TESTS:
|
2011-07-11 18:03:19 +00:00
|
|
|
Error = ListTests(ShowHidden);
|
2011-07-10 08:03:15 +00:00
|
|
|
break;
|
|
|
|
case KMT_RUN_TEST:
|
|
|
|
Error = RunTest(TestName);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(FALSE);
|
|
|
|
}
|
2011-06-09 15:14:02 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
cleanup:
|
|
|
|
if (KmtestHandle)
|
|
|
|
CloseHandle(KmtestHandle);
|
|
|
|
|
2011-07-28 09:00:56 +00:00
|
|
|
if (ResultBuffer)
|
|
|
|
KmtFreeResultBuffer(ResultBuffer);
|
|
|
|
|
2011-07-10 08:03:15 +00:00
|
|
|
KmtCloseService(&KmtestServiceHandle);
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
if (Error)
|
|
|
|
KmtServiceCleanup(TRUE);
|
|
|
|
else
|
|
|
|
Error = KmtServiceCleanup(FALSE);
|
|
|
|
|
|
|
|
if (Error)
|
2011-07-10 08:03:15 +00:00
|
|
|
{
|
2011-07-03 18:53:26 +00:00
|
|
|
OutputError(Error);
|
|
|
|
|
|
|
|
Status = EXIT_FAILURE;
|
2011-07-10 08:03:15 +00:00
|
|
|
}
|
2011-06-09 15:14:02 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
return Status;
|
2011-06-09 15:14:02 +00:00
|
|
|
}
|