cmd/tests: Add tests for console InsertMode

Hugh McMaster hugh.mcmaster at outlook.com
Thu Apr 30 04:36:06 CDT 2015


---
 programs/cmd/tests/Makefile.in  |   4 +-
 programs/cmd/tests/insertmode.c | 136 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 139 insertions(+), 1 deletion(-)
 create mode 100644 programs/cmd/tests/insertmode.c

diff --git a/programs/cmd/tests/Makefile.in b/programs/cmd/tests/Makefile.in
index 87942fe..4777ac0 100644
--- a/programs/cmd/tests/Makefile.in
+++ b/programs/cmd/tests/Makefile.in
@@ -1,6 +1,8 @@
 TESTDLL   = cmd.exe
+IMPORTS   = advapi32
 
 C_SRCS = \
-	batch.c
+	batch.c \
+	insertmode.c
 
 RC_SRCS   = rsrc.rc
diff --git a/programs/cmd/tests/insertmode.c b/programs/cmd/tests/insertmode.c
new file mode 100644
index 0000000..89fb6ed
--- /dev/null
+++ b/programs/cmd/tests/insertmode.c
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2015 Hugh McMaster
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include <windows.h>
+#include <string.h>
+
+#include "wine/test.h"
+
+/* AttachConsole is not present on Windows 2000, although the PSDK says it should be */
+static BOOL (__cdecl *p_attachconsole)(DWORD process_id);
+
+static BOOL have_attachconsole(void)
+{
+    HMODULE hmod = GetModuleHandleA("kernel32.dll");
+
+    p_attachconsole = (void *)GetProcAddress(hmod, "AttachConsole");
+    if (!p_attachconsole)
+        return FALSE;
+    return TRUE;
+}
+
+static LONG reg_get_insertmode(DWORD *value)
+{
+    HKEY hkey;
+    DWORD size = sizeof(DWORD);
+    LONG ret;
+
+    if (!(ret = RegOpenKeyExA(HKEY_CURRENT_USER, "Console", 0, KEY_READ, &hkey)))
+    {
+        ret = RegQueryValueExA(hkey, "InsertMode", NULL, NULL, (BYTE *)value, &size);
+        RegCloseKey(hkey);
+    }
+    return ret;
+}
+
+static LONG reg_set_insertmode(DWORD value)
+{
+    HKEY hkey;
+    LONG ret;
+
+    if (!(ret = RegOpenKeyExA(HKEY_CURRENT_USER, "Console", 0, KEY_SET_VALUE, &hkey)))
+    {
+        ret = RegSetValueExA(hkey, "InsertMode", 0, REG_DWORD, (const BYTE *)&value, sizeof(value));
+        RegCloseKey(hkey);
+    }
+    return ret;
+}
+
+#define INSERT_MODE_FLAGS (ENABLE_INSERT_MODE | ENABLE_EXTENDED_FLAGS)
+static DWORD is_insertmode(DWORD mode)
+{
+    if ((mode & INSERT_MODE_FLAGS) == INSERT_MODE_FLAGS)
+        return 1;
+    else
+        return 0;
+}
+
+static BOOL get_child_console_mode(DWORD *mode)
+{
+    STARTUPINFOA si;
+    PROCESS_INFORMATION pi;
+    char cmd[32];
+    DWORD res;
+
+    ZeroMemory(&si, sizeof(si));
+    si.cb = sizeof(si);
+    ZeroMemory(&pi, sizeof(pi));
+
+    strcpy(cmd, "cmd.exe /c ping 127.0.0.1 -n 4");
+    if (!CreateProcessA(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
+        return FALSE;
+
+    Sleep(1000); /* Wait for console to fully initialise */
+
+    FreeConsole();
+    p_attachconsole(pi.dwProcessId);
+
+    GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode);
+
+    FreeConsole();
+    p_attachconsole(ATTACH_PARENT_PROCESS);
+
+    res = WaitForSingleObject(pi.hProcess, 5000);
+    if (res == WAIT_TIMEOUT)
+        TerminateProcess(pi.hProcess, 1);
+
+    CloseHandle(pi.hThread);
+    CloseHandle(pi.hProcess);
+    return TRUE;
+}
+
+START_TEST(insertmode)
+{
+    DWORD orig_mode, orig_reg_value, status, child_mode;
+
+    GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &orig_mode);
+    status = is_insertmode(orig_mode);
+    reg_get_insertmode(&orig_reg_value);
+    ok(status == orig_reg_value, "Console InsertMode (%x) does not match registry value (%x)\n",
+       status, orig_reg_value);
+
+    if (!have_attachconsole()) {
+        win_skip("AttachConsole not present in kernel32.dll, skipping InsertMode tests\n");
+        return;
+    }
+
+    reg_set_insertmode(0);
+    if (!get_child_console_mode(&child_mode)) {
+        win_skip("Error creating cmd process (%d), skipping InsertMode tests\n", GetLastError());
+        return;
+    }
+    status = is_insertmode(child_mode);
+    ok(status == 0, "Incorrect console status, got %x, expected 0\n", status);
+
+    reg_set_insertmode(1);
+    get_child_console_mode(&child_mode);
+    status = is_insertmode(child_mode);
+    ok(status == 1, "Incorrect console status, got %x, expected 1\n", status);
+
+    reg_set_insertmode(orig_reg_value); /* Restore original InsertMode value */
+}
-- 
1.9.1




More information about the wine-patches mailing list