EditlineTest.cpp
8.85 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//===-- EditlineTest.cpp --------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Host/Config.h"
#if LLDB_ENABLE_LIBEDIT
#define EDITLINE_TEST_DUMP_OUTPUT 0
#include <stdio.h>
#include <unistd.h>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <memory>
#include <thread>
#include "TestingSupport/SubsystemRAII.h"
#include "lldb/Host/Editline.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/Pipe.h"
#include "lldb/Host/PseudoTerminal.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/StringList.h"
using namespace lldb_private;
namespace {
const size_t TIMEOUT_MILLIS = 5000;
}
class FilePointer {
public:
FilePointer() = delete;
FilePointer(const FilePointer &) = delete;
FilePointer(FILE *file_p) : _file_p(file_p) {}
~FilePointer() {
if (_file_p != nullptr) {
const int close_result = fclose(_file_p);
EXPECT_EQ(0, close_result);
}
}
operator FILE *() { return _file_p; }
private:
FILE *_file_p;
};
/**
Wraps an Editline class, providing a simple way to feed
input (as if from the keyboard) and receive output from Editline.
*/
class EditlineAdapter {
public:
EditlineAdapter();
void CloseInput();
bool IsValid() const { return _editline_sp != nullptr; }
lldb_private::Editline &GetEditline() { return *_editline_sp; }
bool SendLine(const std::string &line);
bool SendLines(const std::vector<std::string> &lines);
bool GetLine(std::string &line, bool &interrupted, size_t timeout_millis);
bool GetLines(lldb_private::StringList &lines, bool &interrupted,
size_t timeout_millis);
void ConsumeAllOutput();
private:
static bool IsInputComplete(lldb_private::Editline *editline,
lldb_private::StringList &lines, void *baton);
std::unique_ptr<lldb_private::Editline> _editline_sp;
PseudoTerminal _pty;
int _pty_master_fd;
int _pty_secondary_fd;
std::unique_ptr<FilePointer> _el_secondary_file;
};
EditlineAdapter::EditlineAdapter()
: _editline_sp(), _pty(), _pty_master_fd(-1), _pty_secondary_fd(-1),
_el_secondary_file() {
lldb_private::Status error;
// Open the first master pty available.
char error_string[256];
error_string[0] = '\0';
if (!_pty.OpenFirstAvailablePrimary(O_RDWR, error_string,
sizeof(error_string))) {
fprintf(stderr, "failed to open first available master pty: '%s'\n",
error_string);
return;
}
// Grab the master fd. This is a file descriptor we will:
// (1) write to when we want to send input to editline.
// (2) read from when we want to see what editline sends back.
_pty_master_fd = _pty.GetPrimaryFileDescriptor();
// Open the corresponding secondary pty.
if (!_pty.OpenSecondary(O_RDWR, error_string, sizeof(error_string))) {
fprintf(stderr, "failed to open secondary pty: '%s'\n", error_string);
return;
}
_pty_secondary_fd = _pty.GetSecondaryFileDescriptor();
_el_secondary_file.reset(new FilePointer(fdopen(_pty_secondary_fd, "rw")));
EXPECT_FALSE(nullptr == *_el_secondary_file);
if (*_el_secondary_file == nullptr)
return;
// Create an Editline instance.
_editline_sp.reset(new lldb_private::Editline(
"gtest editor", *_el_secondary_file, *_el_secondary_file,
*_el_secondary_file, false));
_editline_sp->SetPrompt("> ");
// Hookup our input complete callback.
_editline_sp->SetIsInputCompleteCallback(IsInputComplete, this);
}
void EditlineAdapter::CloseInput() {
if (_el_secondary_file != nullptr)
_el_secondary_file.reset(nullptr);
}
bool EditlineAdapter::SendLine(const std::string &line) {
// Ensure we're valid before proceeding.
if (!IsValid())
return false;
// Write the line out to the pipe connected to editline's input.
ssize_t input_bytes_written =
::write(_pty_master_fd, line.c_str(),
line.length() * sizeof(std::string::value_type));
const char *eoln = "\n";
const size_t eoln_length = strlen(eoln);
input_bytes_written =
::write(_pty_master_fd, eoln, eoln_length * sizeof(char));
EXPECT_NE(-1, input_bytes_written) << strerror(errno);
EXPECT_EQ(eoln_length * sizeof(char), size_t(input_bytes_written));
return eoln_length * sizeof(char) == size_t(input_bytes_written);
}
bool EditlineAdapter::SendLines(const std::vector<std::string> &lines) {
for (auto &line : lines) {
#if EDITLINE_TEST_DUMP_OUTPUT
printf("<stdin> sending line \"%s\"\n", line.c_str());
#endif
if (!SendLine(line))
return false;
}
return true;
}
// We ignore the timeout for now.
bool EditlineAdapter::GetLine(std::string &line, bool &interrupted,
size_t /* timeout_millis */) {
// Ensure we're valid before proceeding.
if (!IsValid())
return false;
_editline_sp->GetLine(line, interrupted);
return true;
}
bool EditlineAdapter::GetLines(lldb_private::StringList &lines,
bool &interrupted, size_t /* timeout_millis */) {
// Ensure we're valid before proceeding.
if (!IsValid())
return false;
_editline_sp->GetLines(1, lines, interrupted);
return true;
}
bool EditlineAdapter::IsInputComplete(lldb_private::Editline *editline,
lldb_private::StringList &lines,
void *baton) {
// We'll call ourselves complete if we've received a balanced set of braces.
int start_block_count = 0;
int brace_balance = 0;
for (const std::string &line : lines) {
for (auto ch : line) {
if (ch == '{') {
++start_block_count;
++brace_balance;
} else if (ch == '}')
--brace_balance;
}
}
return (start_block_count > 0) && (brace_balance == 0);
}
void EditlineAdapter::ConsumeAllOutput() {
FilePointer output_file(fdopen(_pty_master_fd, "r"));
int ch;
while ((ch = fgetc(output_file)) != EOF) {
#if EDITLINE_TEST_DUMP_OUTPUT
char display_str[] = {0, 0, 0};
switch (ch) {
case '\t':
display_str[0] = '\\';
display_str[1] = 't';
break;
case '\n':
display_str[0] = '\\';
display_str[1] = 'n';
break;
case '\r':
display_str[0] = '\\';
display_str[1] = 'r';
break;
default:
display_str[0] = ch;
break;
}
printf("<stdout> 0x%02x (%03d) (%s)\n", ch, ch, display_str);
// putc(ch, stdout);
#endif
}
}
class EditlineTestFixture : public ::testing::Test {
SubsystemRAII<FileSystem> subsystems;
EditlineAdapter _el_adapter;
std::shared_ptr<std::thread> _sp_output_thread;
public:
static void SetUpTestCase() {
// We need a TERM set properly for editline to work as expected.
setenv("TERM", "vt100", 1);
}
void SetUp() override {
// Validate the editline adapter.
EXPECT_TRUE(_el_adapter.IsValid());
if (!_el_adapter.IsValid())
return;
// Dump output.
_sp_output_thread =
std::make_shared<std::thread>([&] { _el_adapter.ConsumeAllOutput(); });
}
void TearDown() override {
_el_adapter.CloseInput();
if (_sp_output_thread)
_sp_output_thread->join();
}
EditlineAdapter &GetEditlineAdapter() { return _el_adapter; }
};
TEST_F(EditlineTestFixture, EditlineReceivesSingleLineText) {
// Send it some text via our virtual keyboard.
const std::string input_text("Hello, world");
EXPECT_TRUE(GetEditlineAdapter().SendLine(input_text));
// Verify editline sees what we put in.
std::string el_reported_line;
bool input_interrupted = false;
const bool received_line = GetEditlineAdapter().GetLine(
el_reported_line, input_interrupted, TIMEOUT_MILLIS);
EXPECT_TRUE(received_line);
EXPECT_FALSE(input_interrupted);
EXPECT_EQ(input_text, el_reported_line);
}
TEST_F(EditlineTestFixture, EditlineReceivesMultiLineText) {
// Send it some text via our virtual keyboard.
std::vector<std::string> input_lines;
input_lines.push_back("int foo()");
input_lines.push_back("{");
input_lines.push_back("printf(\"Hello, world\");");
input_lines.push_back("}");
input_lines.push_back("");
EXPECT_TRUE(GetEditlineAdapter().SendLines(input_lines));
// Verify editline sees what we put in.
lldb_private::StringList el_reported_lines;
bool input_interrupted = false;
EXPECT_TRUE(GetEditlineAdapter().GetLines(el_reported_lines,
input_interrupted, TIMEOUT_MILLIS));
EXPECT_FALSE(input_interrupted);
// Without any auto indentation support, our output should directly match our
// input.
std::vector<std::string> reported_lines;
for (const std::string &line : el_reported_lines)
reported_lines.push_back(line);
EXPECT_THAT(reported_lines, testing::ContainerEq(input_lines));
}
#endif