aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/unit_tests/process_worker.cpp
blob: 1ddf1d52627877c85fb52220fef0db03fd72f3e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "winapi-common" project.
// For details, see https://github.com/egor-tensin/winapi-common.
// Distributed under the MIT License.

#include "fixtures.hpp"
#include "shared/command.hpp"
#include "shared/console.hpp"
#include "shared/test_data.hpp"
#include "shared/worker.hpp"

#include <winapi/buffer.hpp>
#include <winapi/cmd_line.hpp>
#include <winapi/handle.hpp>
#include <winapi/pipe.hpp>
#include <winapi/process.hpp>
#include <winapi/process_io.hpp>

#include <boost/test/unit_test.hpp>

#include <windows.h>

#include <cstddef>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using winapi::Buffer;
using winapi::CommandLine;
using winapi::Handle;
using winapi::Pipe;
using winapi::Process;
using winapi::ProcessParameters;
namespace process = winapi::process;

using worker::StdHandles;
using worker::Worker;

namespace {

void check_window_same(Worker& worker) {
    BOOST_TEST(::GetConsoleWindow());
    BOOST_TEST(worker.get_console_window() == ::GetConsoleWindow());
    BOOST_TEST(worker.is_window_visible());
}

void check_window_none(Worker& worker) {
    BOOST_TEST(::GetConsoleWindow());
    BOOST_TEST(!worker.get_console_window());
    BOOST_TEST(!worker.is_window_visible());
}

void check_window_new(Worker& worker) {
    BOOST_TEST(::GetConsoleWindow());
    BOOST_TEST(worker.get_console_window());
    BOOST_TEST(worker.get_console_window() != ::GetConsoleWindow());
    BOOST_TEST(worker.is_window_visible());
}

void check_std_handles(Worker& worker, const StdHandles& expected) {
    const auto actual = worker.get_std_handles();
    BOOST_TEST(actual.in == expected.in);
    BOOST_TEST(actual.out == expected.out);
    BOOST_TEST(actual.err == expected.err);
}

void check_std_handles_same(Worker& worker) {
    check_std_handles(worker,
                      {::GetStdHandle(STD_INPUT_HANDLE),
                       ::GetStdHandle(STD_OUTPUT_HANDLE),
                       ::GetStdHandle(STD_ERROR_HANDLE)});
}

void check_std_handles_different(Worker& worker) {
    const auto actual = worker.get_std_handles();
    BOOST_TEST(actual.in);
    BOOST_TEST(actual.out);
    BOOST_TEST(actual.err);
    BOOST_TEST(actual.in != ::GetStdHandle(STD_INPUT_HANDLE));
    BOOST_TEST(actual.out != ::GetStdHandle(STD_OUTPUT_HANDLE));
    BOOST_TEST(actual.err != ::GetStdHandle(STD_ERROR_HANDLE));
}

void check_write(Worker& worker) {
    const auto handles = worker.test_write();
    BOOST_TEST(Handle::is_valid(handles.in));
    BOOST_TEST(Handle::is_valid(handles.out));
    BOOST_TEST(Handle::is_valid(handles.err));
}

void check_redirected_output(const Buffer& buffer, const std::vector<std::string>& expected_lines) {
    const auto actual = buffer.as_utf8();
    std::ostringstream oss;
    for (const auto& line : expected_lines) {
        oss << line << "\r\n";
    }
    const std::string expected{oss.str()};
    BOOST_TEST(actual == expected);
}

const std::string PLACEHOLDER{"This is a placeholder line."};

void check_console_buffer_inherit(Worker& worker, const std::vector<std::string>& expected) {
    // Check that
    // 1) the worker process writes the expected lines to its console window,
    // 2) the lines appear in this process's window, since they're the same.

    const auto numof_lines = expected.size();
    // Write some lines in this process's window, they should be overwritten
    // by worker's writes.
    for (std::size_t i = 0; i < numof_lines; ++i) {
        std::cout << PLACEHOLDER << std::endl;
    }
    // Order worker to write the lines:
    const auto handles = worker.test_write();
    // Query worker process's window:
    const auto worker_actual = worker.read_last_lines(numof_lines);
    // Query this process's window:
    const auto this_actual = console::Buffer{}.read_last_lines(numof_lines);

    // They should look the same:
    const auto& worker_expected = expected;
    const auto& this_expected = worker_expected;

    BOOST_TEST(worker_actual == worker_expected);
    BOOST_TEST(this_actual == this_expected);
}

void check_console_buffer_new(Worker& worker, const std::vector<std::string>& expected) {
    // Check that
    // 1) the worker process writes the expected lines in its console window,
    // 2) the lines _do not_ appear in this process's window, since they're
    // different windows.

    const auto numof_lines = expected.size();
    // Write some lines in this process's window, they _should not_ be
    // overwritten by worker's writes.
    for (std::size_t i = 0; i < numof_lines; ++i) {
        std::cout << PLACEHOLDER << std::endl;
    }
    // Order worker to write the lines:
    const auto handles = worker.test_write();
    // Query worker process's window:
    const auto worker_actual = worker.read_last_lines(numof_lines);
    // Query this process's window:
    const auto this_actual = console::Buffer{}.read_last_lines(numof_lines);

    // Placeholder lines are still last in this process's window:
    const auto& worker_expected = expected;
    const std::vector<std::string> this_expected(numof_lines, PLACEHOLDER);

    BOOST_TEST(worker_actual == worker_expected);
    BOOST_TEST(this_actual == this_expected);
}

// CREATE_NO_WINDOW actually does create a new buffer, it's just invisible.
void check_console_buffer_none(Worker& worker, const std::vector<std::string>& expected) {
    check_console_buffer_new(worker, expected);
}

} // namespace

BOOST_AUTO_TEST_SUITE(process_console_tests)

BOOST_FIXTURE_TEST_CASE(create_inherit, WithWorkerExe) {
    const CommandLine cmd_line{get_worker_exe()};
    ProcessParameters params{cmd_line};
    params.console_mode = ProcessParameters::ConsoleInherit;

    Worker worker{Process::create(std::move(params))};
    check_window_same(worker);
    check_std_handles_same(worker);
    check_write(worker);
    check_console_buffer_inherit(worker, {worker::test_data::out(), worker::test_data::err()});
    BOOST_TEST(worker.exit() == 0);
}

BOOST_FIXTURE_TEST_CASE(create_inherit_override, WithWorkerExe) {
    const CommandLine cmd_line{get_worker_exe()};
    ProcessParameters params{cmd_line};
    params.console_mode = ProcessParameters::ConsoleInherit;

    Pipe stdin_pipe, stderr_pipe;
    const StdHandles expected_handles{
        stdin_pipe.read_end().get(), Handle::std_out().get(), stderr_pipe.write_end().get()};

    process::IO io;
    io.std_in = process::Stdin{stdin_pipe};
    io.std_err = process::Stderr{stderr_pipe};
    params.io = std::move(io);

    Worker worker{Process::create(std::move(params))};
    check_window_same(worker);
    check_std_handles(worker, expected_handles);
    check_write(worker);
    check_console_buffer_inherit(worker, {worker::test_data::out()});
    BOOST_TEST(worker.exit() == 0);
    check_redirected_output(stderr_pipe.read_end().read(),
                            {worker::test_data::err(), worker::test_data::err()});
}

BOOST_FIXTURE_TEST_CASE(create_none, WithWorkerExe) {
    const CommandLine cmd_line{get_worker_exe()};
    ProcessParameters params{cmd_line};
    params.console_mode = ProcessParameters::ConsoleNone;

    Worker worker{Process::create(std::move(params))};
    check_window_none(worker);
    check_std_handles_different(worker);
    check_write(worker);
    check_console_buffer_none(worker, {worker::test_data::out(), worker::test_data::err()});
    BOOST_TEST(worker.exit() == 0);
}

BOOST_FIXTURE_TEST_CASE(create_none_override, WithWorkerExe) {
    const CommandLine cmd_line{get_worker_exe()};
    ProcessParameters params{cmd_line};
    params.console_mode = ProcessParameters::ConsoleNone;

    Pipe stdin_pipe, stderr_pipe;
    const StdHandles expected_handles{
        stdin_pipe.read_end().get(), Handle::std_out().get(), stderr_pipe.write_end().get()};

    process::IO io;
    io.std_in = process::Stdin{stdin_pipe};
    io.std_err = process::Stderr{stderr_pipe};
    params.io = std::move(io);

    Worker worker{Process::create(std::move(params))};
    check_window_none(worker);
    check_std_handles(worker, expected_handles);
    check_write(worker);
    check_console_buffer_none(worker, {worker::test_data::out()});
    BOOST_TEST(worker.exit() == 0);
    check_redirected_output(stderr_pipe.read_end().read(),
                            {worker::test_data::err(), worker::test_data::err()});
}

BOOST_FIXTURE_TEST_CASE(create_new, WithWorkerExe) {
    const CommandLine cmd_line{get_worker_exe()};
    ProcessParameters params{cmd_line};
    params.console_mode = ProcessParameters::ConsoleNew;

    Worker worker{Process::create(std::move(params))};
    check_window_new(worker);
    check_std_handles_different(worker);
    check_write(worker);
    check_console_buffer_new(worker, {worker::test_data::out(), worker::test_data::err()});
    BOOST_TEST(worker.exit() == 0);
}

BOOST_FIXTURE_TEST_CASE(create_new_override, WithWorkerExe) {
    const CommandLine cmd_line{get_worker_exe()};
    ProcessParameters params{cmd_line};
    params.console_mode = ProcessParameters::ConsoleNew;

    Pipe stdin_pipe, stderr_pipe;
    const StdHandles expected_handles{
        stdin_pipe.read_end().get(), Handle::std_out().get(), stderr_pipe.write_end().get()};

    process::IO io;
    io.std_in = process::Stdin{stdin_pipe};
    io.std_err = process::Stderr{stderr_pipe};
    params.io = std::move(io);

    Worker worker{Process::create(std::move(params))};
    check_window_new(worker);
    check_std_handles(worker, expected_handles);
    check_write(worker);
    check_console_buffer_new(worker, {worker::test_data::out()});
    BOOST_TEST(worker.exit() == 0);
    check_redirected_output(stderr_pipe.read_end().read(),
                            {worker::test_data::err(), worker::test_data::err()});
}

BOOST_AUTO_TEST_SUITE_END()