Author: tfaber
Date: Mon Jul 4 19:47:49 2011
New Revision: 52541
URL:
http://svn.reactos.org/svn/reactos?rev=52541&view=rev
Log:
[KMTESTS]
- prepare the device object for being opened multiple times (required for communication
with special-purpose drivers)
- more verbose DPRINTs
- implement utility functions for app<->driver communication
- misc fixes
Modified:
branches/GSoC_2011/KMTestSuite/kmtests/include/kmt_test.h
branches/GSoC_2011/KMTestSuite/kmtests/kmtest/kmtest.c
branches/GSoC_2011/KMTestSuite/kmtests/kmtest/support.c
branches/GSoC_2011/KMTestSuite/kmtests/kmtest_drv/kmtest_drv.c
Modified: branches/GSoC_2011/KMTestSuite/kmtests/include/kmt_test.h
URL:
http://svn.reactos.org/svn/reactos/branches/GSoC_2011/KMTestSuite/kmtests/i…
==============================================================================
--- branches/GSoC_2011/KMTestSuite/kmtests/include/kmt_test.h [iso-8859-1] (original)
+++ branches/GSoC_2011/KMTestSuite/kmtests/include/kmt_test.h [iso-8859-1] Mon Jul 4
19:47:49 2011
@@ -38,7 +38,14 @@
CHAR LogBuffer[ANYSIZE_ARRAY];
} KMT_RESULTBUFFER, *PKMT_RESULTBUFFER;
-#if defined KMT_USER_MODE
+#ifdef KMT_KERNEL_MODE
+/* Device Extension layout */
+typedef struct
+{
+ PKMT_RESULTBUFFER ResultBuffer;
+ PMDL Mdl;
+} KMT_DEVICE_EXTENSION, *PKMT_DEVICE_EXTENSION;
+#elif defined KMT_USER_MODE
VOID KmtLoadDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning);
VOID KmtUnloadDriver(VOID);
VOID KmtOpenDriver(VOID);
@@ -46,7 +53,7 @@
DWORD KmtSendToDriver(IN DWORD ControlCode);
DWORD KmtSendStringToDriver(IN DWORD ControlCode, IN PCSTR String);
-DWORD KmtSendBufferToDriver(IN DWORD ControlCode, IN OUT PVOID Buffer, IN DWORD Length);
+DWORD KmtSendBufferToDriver(IN DWORD ControlCode, IN OUT PVOID Buffer, IN OUT PDWORD
Length);
#endif /* defined KMT_USER_MODE */
extern PKMT_RESULTBUFFER ResultBuffer;
@@ -95,6 +102,11 @@
#define ok_eq_str(value, expected) ok(!strcmp(value, expected), #value " =
\"%s\", expected \"%s\"\n", value, expected)
#define ok_eq_wstr(value, expected) ok(!wcscmp(value, expected), #value " =
\"%ls\", expected \"%ls\"\n", value, expected)
+#define KMT_MAKE_CODE(ControlCode) CTL_CODE(FILE_DEVICE_UNKNOWN, \
+ 0xA00 + (ControlCode), \
+ METHOD_BUFFERED, \
+ FILE_ANY_ACCESS)
+
#if defined KMT_DEFINE_TEST_FUNCTIONS
PKMT_RESULTBUFFER ResultBuffer = NULL;
@@ -122,6 +134,9 @@
{
LONG OldLength;
LONG NewLength;
+
+ if (!Buffer)
+ return;
do
{
@@ -199,6 +214,9 @@
CHAR MessageBuffer[512];
SIZE_T MessageLength;
+ if (!ResultBuffer)
+ return;
+
MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, NULL, NULL,
"%s: %ld tests executed (0 marked as todo, %ld
failures), %ld skipped.\n",
TestName,
@@ -213,6 +231,9 @@
CHAR MessageBuffer[512];
SIZE_T MessageLength;
+ if (!ResultBuffer)
+ return;
+
if (Condition)
{
InterlockedIncrement(&ResultBuffer->Successes);
@@ -260,6 +281,9 @@
{
CHAR MessageBuffer[512];
SIZE_T MessageLength;
+
+ if (!ResultBuffer)
+ return !Condition;
if (!Condition)
{
Modified: branches/GSoC_2011/KMTestSuite/kmtests/kmtest/kmtest.c
URL:
http://svn.reactos.org/svn/reactos/branches/GSoC_2011/KMTestSuite/kmtests/k…
==============================================================================
--- branches/GSoC_2011/KMTestSuite/kmtests/kmtest/kmtest.c [iso-8859-1] (original)
+++ branches/GSoC_2011/KMTestSuite/kmtests/kmtest/kmtest.c [iso-8859-1] Mon Jul 4
19:47:49 2011
@@ -9,13 +9,13 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <strsafe.h>
+#include <winioctl.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "kmtest.h"
-#include <winioctl.h>
#include <kmt_public.h>
#define KMT_DEFINE_TEST_FUNCTIONS
#include <kmt_test.h>
@@ -187,7 +187,7 @@
KmtFinishTest(TestName);
if (!WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), ResultBuffer->LogBuffer,
ResultBuffer->LogBufferLength, &BytesWritten, NULL))
- Error = GetLastError();
+ error(Error);
return Error;
}
@@ -229,7 +229,8 @@
error_goto(Error, cleanup);
cleanup:
- OutputResult(TestName);
+ if (!Error)
+ OutputResult(TestName);
KmtFreeResultBuffer(ResultBuffer);
Modified: branches/GSoC_2011/KMTestSuite/kmtests/kmtest/support.c
URL:
http://svn.reactos.org/svn/reactos/branches/GSoC_2011/KMTestSuite/kmtests/k…
==============================================================================
--- branches/GSoC_2011/KMTestSuite/kmtests/kmtest/support.c [iso-8859-1] (original)
+++ branches/GSoC_2011/KMTestSuite/kmtests/kmtest/support.c [iso-8859-1] Mon Jul 4
19:47:49 2011
@@ -9,20 +9,26 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <strsafe.h>
+#include <winioctl.h>
+
+#include <assert.h>
#include "kmtest.h"
+#include <kmt_public.h>
#include <kmt_test.h>
-#include <kmt_public.h>
/* pseudo-tests */
START_TEST(Create)
{
- // nothing to do here. All tests start the service if needed
+ // nothing to do here. All tests create the service if needed
}
START_TEST(Delete)
{
- // TODO: delete kmtest service
+ SC_HANDLE Handle = NULL;
+ DWORD Error = KmtDeleteService(L"Kmtest", &Handle);
+
+ ok_eq_hex(Error, (DWORD)ERROR_SUCCESS);
}
START_TEST(Start)
@@ -32,7 +38,14 @@
START_TEST(Stop)
{
- // TODO: stop kmtest service
+ // TODO: requiring the service to be started for this is... bad,
+ // especially when it's marked for deletion and won't start ;)
+ SC_HANDLE Handle = NULL;
+ DWORD Error = KmtStopService(L"Kmtest", &Handle);
+
+ ok_eq_hex(Error, (DWORD)ERROR_SUCCESS);
+ Error = KmtCloseService(&Handle);
+ ok_eq_hex(Error, (DWORD)ERROR_SUCCESS);
}
/* test support functions for special-purpose drivers */
@@ -107,7 +120,7 @@
StringCbCopy(DevicePath, sizeof DevicePath,
L"\\\\.\\Global\\GLOBALROOT\\Device\\");
StringCbCat(DevicePath, sizeof DevicePath, TestServiceName);
- TestDeviceHandle = CreateFile(KMTEST_DEVICE_PATH, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, NULL);
+ TestDeviceHandle = CreateFile(DevicePath, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (TestDeviceHandle == INVALID_HANDLE_VALUE)
error(Error);
@@ -138,8 +151,6 @@
__debugbreak();
}
}
-
-/* TODO: check if these will be useful */
/**
* @name KmtSendToDriver
@@ -154,8 +165,12 @@
KmtSendToDriver(
IN DWORD ControlCode)
{
- // TODO
- return ERROR_CALL_NOT_IMPLEMENTED;
+ DWORD BytesRead;
+
+ if (!DeviceIoControl(TestDeviceHandle, KMT_MAKE_CODE(ControlCode), NULL, 0, NULL, 0,
&BytesRead, NULL))
+ return GetLastError();
+
+ return ERROR_SUCCESS;
}
/**
@@ -173,8 +188,12 @@
IN DWORD ControlCode,
IN PCSTR String)
{
- // TODO
- return ERROR_CALL_NOT_IMPLEMENTED;
+ DWORD BytesRead;
+
+ if (!DeviceIoControl(TestDeviceHandle, KMT_MAKE_CODE(ControlCode), (PVOID)String,
strlen(String), NULL, 0, &BytesRead, NULL))
+ return GetLastError();
+
+ return ERROR_SUCCESS;
}
/**
@@ -190,8 +209,12 @@
KmtSendBufferToDriver(
IN DWORD ControlCode,
IN OUT PVOID Buffer,
- IN DWORD Length)
-{
- // TODO
- return ERROR_CALL_NOT_IMPLEMENTED;
+ IN OUT PDWORD Length)
+{
+ assert(Length);
+
+ if (!DeviceIoControl(TestDeviceHandle, KMT_MAKE_CODE(ControlCode), Buffer, *Length,
NULL, 0, Length, NULL))
+ return GetLastError();
+
+ return ERROR_SUCCESS;
}
Modified: branches/GSoC_2011/KMTestSuite/kmtests/kmtest_drv/kmtest_drv.c
URL:
http://svn.reactos.org/svn/reactos/branches/GSoC_2011/KMTestSuite/kmtests/k…
==============================================================================
--- branches/GSoC_2011/KMTestSuite/kmtests/kmtest_drv/kmtest_drv.c [iso-8859-1]
(original)
+++ branches/GSoC_2011/KMTestSuite/kmtests/kmtest_drv/kmtest_drv.c [iso-8859-1] Mon Jul 4
19:47:49 2011
@@ -24,13 +24,6 @@
static DRIVER_DISPATCH DriverClose;
static DRIVER_DISPATCH DriverIoControl;
-/* Device Extension layout */
-typedef struct
-{
- PKMT_RESULTBUFFER ResultBuffer;
- PMDL Mdl;
-} KMT_DEVICE_EXTENSION, *PKMT_DEVICE_EXTENSION;
-
/* Globals */
static PDEVICE_OBJECT MainDeviceObject;
@@ -68,13 +61,13 @@
&DeviceName,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN | FILE_READ_ONLY_DEVICE,
- TRUE, &MainDeviceObject);
+ FALSE, &MainDeviceObject);
if (!NT_SUCCESS(Status))
goto cleanup;
- DPRINT("DriverEntry. Created DeviceObject %p\n",
- MainDeviceObject);
+ DPRINT("DriverEntry. Created DeviceObject %p. DeviceExtension %p\n",
+ MainDeviceObject, MainDeviceObject->DeviceExtension);
DeviceExtension = MainDeviceObject->DeviceExtension;
DeviceExtension->ResultBuffer = NULL;
DeviceExtension->Mdl = NULL;
@@ -146,19 +139,14 @@
{
NTSTATUS Status = STATUS_SUCCESS;
PIO_STACK_LOCATION IoStackLocation;
- PKMT_DEVICE_EXTENSION DeviceExtension;
PAGED_CODE();
IoStackLocation = IoGetCurrentIrpStackLocation(Irp);
- DPRINT("DriverCreate. DeviceObject=%p\n",
- DeviceObject);
-
- DeviceExtension = DeviceObject->DeviceExtension;
- ASSERT(!DeviceExtension->Mdl);
- ASSERT(!DeviceExtension->ResultBuffer);
- ASSERT(!ResultBuffer);
+ DPRINT("DriverCreate. DeviceObject=%p, RequestorMode=%d, FileObject=%p,
FsContext=%p, FsContext2=%p\n",
+ DeviceObject, Irp->RequestorMode, IoStackLocation->FileObject,
+ IoStackLocation->FileObject->FsContext,
IoStackLocation->FileObject->FsContext2);
Irp->IoStatus.Status = Status;
Irp->IoStatus.Information = 0;
@@ -195,17 +183,23 @@
IoStackLocation = IoGetCurrentIrpStackLocation(Irp);
- DPRINT("DriverClose. DeviceObject=%p\n",
- DeviceObject);
-
+ DPRINT("DriverClose. DeviceObject=%p, RequestorMode=%d, FileObject=%p,
FsContext=%p, FsContext2=%p\n",
+ DeviceObject, Irp->RequestorMode, IoStackLocation->FileObject,
+ IoStackLocation->FileObject->FsContext,
IoStackLocation->FileObject->FsContext2);
+
+ ASSERT(IoStackLocation->FileObject->FsContext2 == NULL);
DeviceExtension = DeviceObject->DeviceExtension;
- if (DeviceExtension->Mdl)
+ if (DeviceExtension->Mdl && IoStackLocation->FileObject->FsContext
== DeviceExtension->Mdl)
{
MmUnlockPages(DeviceExtension->Mdl);
IoFreeMdl(DeviceExtension->Mdl);
DeviceExtension->Mdl = NULL;
ResultBuffer = DeviceExtension->ResultBuffer = NULL;
}
+ else
+ {
+ ASSERT(IoStackLocation->FileObject->FsContext == NULL);
+ }
Irp->IoStatus.Status = Status;
Irp->IoStatus.Information = 0;
@@ -242,9 +236,10 @@
IoStackLocation = IoGetCurrentIrpStackLocation(Irp);
- DPRINT("DriverIoControl. Code=0x%08X, DeviceObject=%p\n",
+ DPRINT("DriverIoControl. Code=0x%08X, DeviceObject=%p, FileObject=%p,
FsContext=%p, FsContext2=%p\n",
IoStackLocation->Parameters.DeviceIoControl.IoControlCode,
- DeviceObject);
+ DeviceObject, IoStackLocation->FileObject,
+ IoStackLocation->FileObject->FsContext,
IoStackLocation->FileObject->FsContext2);
switch (IoStackLocation->Parameters.DeviceIoControl.IoControlCode)
{
@@ -315,8 +310,15 @@
if (DeviceExtension->Mdl)
{
+ if (IoStackLocation->FileObject->FsContext !=
DeviceExtension->Mdl)
+ {
+ Status = STATUS_ACCESS_DENIED;
+ break;
+ }
MmUnlockPages(DeviceExtension->Mdl);
IoFreeMdl(DeviceExtension->Mdl);
+ IoStackLocation->FileObject->FsContext = NULL;
+ ResultBuffer = DeviceExtension->ResultBuffer = NULL;
}
DeviceExtension->Mdl =
IoAllocateMdl(IoStackLocation->Parameters.DeviceIoControl.Type3InputBuffer,
@@ -341,6 +343,7 @@
} _SEH2_END;
ResultBuffer = DeviceExtension->ResultBuffer =
MmGetSystemAddressForMdlSafe(DeviceExtension->Mdl, NormalPagePriority);
+ IoStackLocation->FileObject->FsContext = DeviceExtension->Mdl;
DPRINT("DriverIoControl. ResultBuffer: %ld %ld %ld %ld\n",
ResultBuffer->Successes, ResultBuffer->Failures,