Repository: microsoft/terminal
File: src/host/ft_host/API_MultipleInflightMessageTests.cpp (C++)

Code before:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "precomp.h"

class MultipleInflightMessageTests
{
    BEGIN_TEST_CLASS(MultipleInflightMessageTests)
    END_TEST_CLASS()

    TEST_METHOD(WriteWhileReadingInputCrash)
    {
        BEGIN_TEST_METHOD_PROPERTIES()
            TEST_METHOD_PROPERTY(L"IsolationLevel", L"Method") // Don't pollute other tests by isolating our codepage change to this test.
        END_TEST_METHOD_PROPERTIES()

        using namespace std::string_view_literals;

        const auto inputHandle = GetStdInputHandle();
        const auto outputHandle = GetStdOutputHandle();

        DWORD originalConsoleMode{};
        VERIFY_WIN32_BOOL_SUCCEEDED(GetConsoleMode(inputHandle, &originalConsoleMode));
        auto restoreMode{ wil::scope_exit([=]() {
            SetConsoleMode(inputHandle, originalConsoleMode);
        }) };
        VERIFY_WIN32_BOOL_SUCCEEDED(SetConsoleMode(inputHandle, ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));

        // Prime the console with some fake input records.
        const std::array<INPUT_RECORD, 4> inputRecords{
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ TRUE, 1, 'h', 0, L'h', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ FALSE, 1, 'h', 0, L'h', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ TRUE, 1, 'i', 0, L'i', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ FALSE, 1, 'i', 0, L'i', 0 } },
        };
        DWORD written{};
        VERIFY_WIN32_BOOL_SUCCEEDED(WriteConsoleInput(inputHandle, inputRecords.data(), static_cast<DWORD>(inputRecords.size()), &written));
        VERIFY_ARE_EQUAL(inputRecords.size(), written);


Review comment (bug):
```suggestion
            TEST_METHOD_PROPERTY(L"IsolationLevel", L"Method") // Don't pollute other tests by isolating our potential crash and buffer resizing to this test.
```

Code after:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "precomp.h"

class MultipleInflightMessageTests
{
    BEGIN_TEST_CLASS(MultipleInflightMessageTests)
    END_TEST_CLASS()

    // This test is intended to make sure that we do not regress after the _handlePostCharInputLoop fix in OpenConsole:c0ab9cb5b
    TEST_METHOD(WriteWhileReadingInputCrash)
    {
        BEGIN_TEST_METHOD_PROPERTIES()
            TEST_METHOD_PROPERTY(L"IsolationLevel", L"Method") // Don't pollute other tests by isolating our potential crash and buffer resizing to this test.
        END_TEST_METHOD_PROPERTIES()

        using namespace std::string_view_literals;

        const auto inputHandle = GetStdInputHandle();
        const auto outputHandle = GetStdOutputHandle();

        DWORD originalConsoleMode{};
        VERIFY_WIN32_BOOL_SUCCEEDED(GetConsoleMode(inputHandle, &originalConsoleMode));
        auto restoreMode{ wil::scope_exit([=]() {
            SetConsoleMode(inputHandle, originalConsoleMode);
        }) };
        VERIFY_WIN32_BOOL_SUCCEEDED(SetConsoleMode(inputHandle, ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));

        // Prime the console with some fake input records.
        const std::array<INPUT_RECORD, 4> inputRecords{
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ TRUE, 1, 'h', 0, L'h', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ FALSE, 1, 'h', 0, L'h', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ TRUE, 1, 'i', 0, L'i', 0 } },
            INPUT_RECORD{ KEY_EVENT, KEY_EVENT_RECORD{ FALSE, 1, 'i', 0, L'i', 0 } },
        };
        DWORD written{};
        VERIFY_WIN32_BOOL_SUCCEEDED(WriteConsoleInput(inputHandle, inputRecords.data(), static_cast<DWORD>(inputRecords.size()), &written));
        VERIFY_ARE_EQUAL(inputRecords.size(), written);