[PATCH 2/3] ntoskrnl.exe/tests: Add a test for IoGetCurrentProcess().

Zebediah Figura z.figura12 at gmail.com
Tue May 1 18:38:03 CDT 2018


For https://bugs.winehq.org/show_bug.cgi?id=29460

Signed-off-by: Zebediah Figura <z.figura12 at gmail.com>
---
 dlls/ntoskrnl.exe/ntoskrnl.exe.spec |   8 +-
 dlls/ntoskrnl.exe/tests/driver.c    | 157 ++++++++++++++++++++++++++++++++++++
 dlls/ntoskrnl.exe/tests/driver.h    |   9 +++
 dlls/ntoskrnl.exe/tests/ntoskrnl.c  |  50 ++++++++++++
 include/wine/test.h                 |   5 +-
 5 files changed, 224 insertions(+), 5 deletions(-)

diff --git a/dlls/ntoskrnl.exe/ntoskrnl.exe.spec b/dlls/ntoskrnl.exe/ntoskrnl.exe.spec
index 0bece17..46c03ec 100644
--- a/dlls/ntoskrnl.exe/ntoskrnl.exe.spec
+++ b/dlls/ntoskrnl.exe/ntoskrnl.exe.spec
@@ -1299,7 +1299,7 @@
 @ stdcall -private ZwCancelIoFile(long ptr) NtCancelIoFile
 @ stdcall -private ZwCancelTimer(long ptr) NtCancelTimer
 @ stdcall -private ZwClearEvent(long) NtClearEvent
-@ stdcall -private ZwClose(long) NtClose
+@ stdcall ZwClose(long) NtClose
 @ stub ZwCloseObjectAuditAlarm
 @ stdcall -private ZwConnectPort(ptr ptr ptr ptr ptr ptr ptr ptr) NtConnectPort
 @ stdcall -private ZwCreateDirectoryObject(ptr long ptr) NtCreateDirectoryObject
@@ -1335,7 +1335,7 @@
 @ stdcall -private ZwNotifyChangeKey(long long ptr ptr ptr long long ptr long long) NtNotifyChangeKey
 @ stdcall -private ZwOpenDirectoryObject(ptr long ptr) NtOpenDirectoryObject
 @ stdcall -private ZwOpenEvent(ptr long ptr) NtOpenEvent
-@ stdcall -private ZwOpenFile(ptr long ptr ptr long long) NtOpenFile
+@ stdcall ZwOpenFile(ptr long ptr ptr long long) NtOpenFile
 @ stdcall -private ZwOpenJobObject(ptr long ptr) NtOpenJobObject
 @ stdcall -private ZwOpenKey(ptr long ptr) NtOpenKey
 @ stdcall -private ZwOpenProcess(ptr long ptr ptr) NtOpenProcess
@@ -1403,7 +1403,7 @@
 @ stdcall -private ZwUnmapViewOfSection(long ptr) NtUnmapViewOfSection
 @ stdcall -private ZwWaitForMultipleObjects(long ptr long long ptr) NtWaitForMultipleObjects
 @ stdcall -private ZwWaitForSingleObject(long long ptr) NtWaitForSingleObject
-@ stdcall -private ZwWriteFile(long long ptr ptr ptr ptr long ptr ptr) NtWriteFile
+@ stdcall ZwWriteFile(long long ptr ptr ptr ptr long ptr ptr) NtWriteFile
 @ stdcall -private ZwYieldExecution() NtYieldExecution
 @ stdcall -private -arch=arm,x86_64 -norelay __chkstk()
 @ cdecl -private -arch=i386 _CIcos() msvcrt._CIcos
@@ -1439,7 +1439,7 @@
 @ cdecl -private _strrev(str) msvcrt._strrev
 @ cdecl -private _strset(str long) msvcrt._strset
 @ cdecl -private _strupr(str) msvcrt._strupr
-@ cdecl -private _vsnprintf(ptr long str ptr) msvcrt._vsnprintf
+@ cdecl _vsnprintf(ptr long str ptr) msvcrt._vsnprintf
 @ cdecl -private _vsnwprintf(ptr long wstr ptr) msvcrt._vsnwprintf
 @ cdecl -private _wcsicmp(wstr wstr) msvcrt._wcsicmp
 @ cdecl -private _wcslwr(wstr) msvcrt._wcslwr
diff --git a/dlls/ntoskrnl.exe/tests/driver.c b/dlls/ntoskrnl.exe/tests/driver.c
index c8afeb6..59d6411 100644
--- a/dlls/ntoskrnl.exe/tests/driver.c
+++ b/dlls/ntoskrnl.exe/tests/driver.c
@@ -37,6 +37,160 @@ static const WCHAR driver_device[] = {'\\','D','e','v','i','c','e',
 static const WCHAR driver_link[] = {'\\','D','o','s','D','e','v','i','c','e','s',
                                     '\\','W','i','n','e','T','e','s','t','D','r','i','v','e','r',0};
 
+static HANDLE okfile;
+static LONG successes;
+static LONG failures;
+static LONG skipped;
+static LONG todo_successes;
+static LONG todo_failures;
+static int todo_level, todo_do_loop;
+static int running_under_wine;
+static int winetest_debug;
+static int report_success;
+
+extern int CDECL _vsnprintf(char *str, size_t len, const char *format, __ms_va_list argptr);
+
+static void kvprintf(const char *format, __ms_va_list ap)
+{
+    static char buffer[512];
+    LARGE_INTEGER offset;
+    IO_STATUS_BLOCK io;
+
+    _vsnprintf(buffer, sizeof(buffer), format, ap);
+    offset.QuadPart = -1;
+    ZwWriteFile(okfile, NULL, NULL, NULL, &io, buffer, strlen(buffer), &offset, NULL);
+}
+
+static void kprintf(const char *format, ...)
+{
+    __ms_va_list valist;
+
+    __ms_va_start(valist, format);
+    kvprintf(format, valist);
+    __ms_va_end(valist);
+}
+
+static void ok_(const char *file, int line, int condition, const char *msg, ...)
+{
+    const char *current_file;
+    __ms_va_list args;
+
+    if (!(current_file = strrchr(file, '/')) &&
+        !(current_file = strrchr(file, '\\')))
+        current_file = file;
+    else
+        current_file++;
+
+    __ms_va_start(args, msg);
+    if (todo_level)
+    {
+        if (condition)
+        {
+            kprintf("%s:%d: Test succeeded inside todo block: ", current_file, line);
+            kvprintf(msg, args);
+            InterlockedIncrement(&todo_failures);
+        }
+        else
+        {
+            if (winetest_debug > 0)
+            {
+                kprintf("%s:%d: Test marked todo: ", current_file, line);
+                kvprintf(msg, args);
+            }
+            InterlockedIncrement(&todo_successes);
+        }
+    }
+    else
+    {
+        if (!condition)
+        {
+            kprintf("%s:%d: Test failed: ", current_file, line);
+            kvprintf(msg, args);
+            InterlockedIncrement(&failures);
+        }
+        else
+        {
+            if (report_success)
+                kprintf("%s:%d: Test succeeded\n", current_file, line);
+            InterlockedIncrement(&successes);
+        }
+    }
+    __ms_va_end(args);
+}
+
+static void winetest_start_todo( int is_todo )
+{
+    todo_level = (todo_level << 1) | (is_todo != 0);
+    todo_do_loop=1;
+}
+
+static int winetest_loop_todo(void)
+{
+    int do_loop=todo_do_loop;
+    todo_do_loop=0;
+    return do_loop;
+}
+
+static void winetest_end_todo(void)
+{
+    todo_level >>= 1;
+}
+
+#define ok(condition, ...)  ok_(__FILE__, __LINE__, condition, __VA_ARGS__)
+#define todo_if(is_todo) for (winetest_start_todo(is_todo); \
+                              winetest_loop_todo(); \
+                              winetest_end_todo())
+#define todo_wine               todo_if(running_under_wine)
+#define todo_wine_if(is_todo)   todo_if((is_todo) && running_under_wine)
+
+static void test_currentprocess(void)
+{
+    PEPROCESS current;
+
+    current = IoGetCurrentProcess();
+todo_wine
+    ok(current != NULL, "Expected current process to be non-NULL\n");
+}
+
+static NTSTATUS main_test(IRP *irp, IO_STACK_LOCATION *stack, ULONG_PTR *info)
+{
+    ULONG length = stack->Parameters.DeviceIoControl.OutputBufferLength;
+    void *buffer = irp->AssociatedIrp.SystemBuffer;
+    struct test_input *test_input = (struct test_input *)buffer;
+    OBJECT_ATTRIBUTES attr = {0};
+    UNICODE_STRING pathU;
+    IO_STATUS_BLOCK io;
+
+    if (!buffer)
+        return STATUS_ACCESS_VIOLATION;
+
+    if (length < sizeof(failures))
+        return STATUS_BUFFER_TOO_SMALL;
+
+    attr.Length = sizeof(attr);
+    RtlInitUnicodeString(&pathU, test_input->path);
+    running_under_wine = test_input->running_under_wine;
+    winetest_debug = test_input->winetest_debug;
+    report_success = test_input->report_success;
+    attr.ObjectName = &pathU;
+    ZwOpenFile(&okfile, FILE_APPEND_DATA, &attr, &io, 0, 0);
+
+    test_currentprocess();
+
+    /* print process report */
+    if (test_input->winetest_debug)
+    {
+        kprintf("%04x:ntoskrnl: %d tests executed (%d marked as todo, %d %s), %d skipped.\n",
+            PsGetCurrentProcessId(), successes + failures + todo_successes + todo_failures,
+            todo_successes, failures + todo_failures,
+            (failures + todo_failures != 1) ? "failures" : "failure", skipped );
+    }
+    ZwClose(okfile);
+    *((LONG *)buffer) = failures;
+    *info = sizeof(failures);
+    return STATUS_SUCCESS;
+}
+
 static NTSTATUS test_basic_ioctl(IRP *irp, IO_STACK_LOCATION *stack, ULONG_PTR *info)
 {
     ULONG length = stack->Parameters.DeviceIoControl.OutputBufferLength;
@@ -71,6 +225,9 @@ static NTSTATUS WINAPI driver_IoControl(DEVICE_OBJECT *device, IRP *irp)
         case IOCTL_WINETEST_BASIC_IOCTL:
             status = test_basic_ioctl(irp, stack, &irp->IoStatus.Information);
             break;
+        case IOCTL_WINETEST_MAIN_TEST:
+            status = main_test(irp, stack, &irp->IoStatus.Information);
+            break;
         default:
             break;
     }
diff --git a/dlls/ntoskrnl.exe/tests/driver.h b/dlls/ntoskrnl.exe/tests/driver.h
index 8c32bf2..da6d129 100644
--- a/dlls/ntoskrnl.exe/tests/driver.h
+++ b/dlls/ntoskrnl.exe/tests/driver.h
@@ -23,5 +23,14 @@
 
 /* All custom IOCTLs need to have a function value >= 0x800. */
 #define IOCTL_WINETEST_BASIC_IOCTL      CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
+#define IOCTL_WINETEST_MAIN_TEST        CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
 
 static const char teststr[] = "Wine is not an emulator";
+
+struct test_input
+{
+    int running_under_wine;
+    int report_success;
+    int winetest_debug;
+    WCHAR path[1];
+};
diff --git a/dlls/ntoskrnl.exe/tests/ntoskrnl.c b/dlls/ntoskrnl.exe/tests/ntoskrnl.c
index bd0498a..4a6dcc7 100644
--- a/dlls/ntoskrnl.exe/tests/ntoskrnl.c
+++ b/dlls/ntoskrnl.exe/tests/ntoskrnl.c
@@ -20,10 +20,13 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+#include <stdio.h>
 #include "windows.h"
 #include "winsvc.h"
 #include "winioctl.h"
+#include "winternl.h"
 #include "wine/test.h"
+#include "wine/heap.h"
 
 #include "driver.h"
 
@@ -32,6 +35,8 @@ static const char device_path[] = "\\\\.\\WineTestDriver";
 
 static HANDLE device;
 
+static BOOL     (WINAPI *pRtlDosPathNameToNtPathName_U)( LPCWSTR, PUNICODE_STRING, PWSTR*, CURDIR* );
+
 static void load_resource(const char *name, char *filename)
 {
     static char path[MAX_PATH];
@@ -131,6 +136,47 @@ static SC_HANDLE load_driver(char *filename)
     return service;
 }
 
+static void main_test(void)
+{
+    static const WCHAR dokW[] = {'d','o','k',0};
+    WCHAR temppathW[MAX_PATH], pathW[MAX_PATH];
+    struct test_input *test_input;
+    UNICODE_STRING pathU;
+    DWORD written, read;
+    LONG new_failures;
+    char buffer[512];
+    HANDLE okfile;
+    BOOL res;
+
+    /* Create a temporary file that the driver will write ok/trace output to. */
+    GetTempPathW(MAX_PATH, temppathW);
+    GetTempFileNameW(temppathW, dokW, 0, pathW);
+    pRtlDosPathNameToNtPathName_U( pathW, &pathU, NULL, NULL );
+
+    test_input = heap_alloc(sizeof(*test_input) + pathU.Length);
+    test_input->running_under_wine = !strcmp(winetest_platform, "wine");
+    test_input->report_success = report_success;
+    test_input->winetest_debug = winetest_debug;
+    lstrcpynW(test_input->path, pathU.Buffer, pathU.Length);
+    res = DeviceIoControl(device, IOCTL_WINETEST_MAIN_TEST, test_input, sizeof(*test_input) + pathU.Length,
+                          &new_failures, sizeof(new_failures), &written, NULL);
+    ok(res, "DeviceIoControl failed: %u\n", GetLastError());
+    ok(written == sizeof(new_failures), "got size %x\n", written);
+
+    okfile = CreateFileW(pathW, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
+    ok(okfile != INVALID_HANDLE_VALUE, "failed to create %s: %u\n", wine_dbgstr_w(pathW), GetLastError());
+
+    /* Print the ok/trace output and then add to our failure count. */
+    do {
+        ReadFile(okfile, buffer, sizeof(buffer), &read, NULL);
+        printf("%.*s", read, buffer);
+    } while (read == sizeof(buffer));
+    winetest_add_failures(new_failures);
+
+    CloseHandle(okfile);
+    DeleteFileW(pathW);
+}
+
 static void test_basic_ioctl(void)
 {
     DWORD written;
@@ -149,10 +195,14 @@ START_TEST(ntoskrnl)
     char filename[MAX_PATH];
     SC_HANDLE service;
 
+    HMODULE hntdll = GetModuleHandleA("ntdll.dll");
+    pRtlDosPathNameToNtPathName_U = (void *)GetProcAddress(hntdll, "RtlDosPathNameToNtPathName_U");
+
     if (!(service = load_driver(filename)))
         return;
 
     test_basic_ioctl();
+    main_test();
 
     unload_driver(service);
     ok(DeleteFileA(filename), "DeleteFile failed: %u\n", GetLastError());
diff --git a/include/wine/test.h b/include/wine/test.h
index 4ec66d5..5dd0861 100644
--- a/include/wine/test.h
+++ b/include/wine/test.h
@@ -52,6 +52,9 @@ extern int winetest_debug;
 /* running in interactive mode? */
 extern int winetest_interactive;
 
+/* report successful tests (BOOL) */
+extern int report_success;
+
 /* current platform */
 extern const char *winetest_platform;
 
@@ -204,7 +207,7 @@ int winetest_interactive = 0;
 const char *winetest_platform = "windows";
 
 /* report successful tests (BOOL) */
-static int report_success = 0;
+int report_success = 0;
 
 /* passing arguments around */
 static int winetest_argc;
-- 
2.7.4




More information about the wine-devel mailing list