init commit
This commit is contained in:
commit
1497f114e6
47 files changed
+8358
No files matched your search
@@ -0,0 +1,345 @@
|
||||
#include "TerminalInput.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <Windows.h>
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
using Bilicraft::TerminalInput;
|
||||
|
||||
void CloseDescriptor(int fd)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
_close(fd);
|
||||
#else
|
||||
close(fd);
|
||||
#endif
|
||||
}
|
||||
|
||||
class InputPipe
|
||||
{
|
||||
public:
|
||||
InputPipe()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (_pipe(descriptors, 4096, _O_BINARY) != 0)
|
||||
#else
|
||||
if (pipe(descriptors) != 0)
|
||||
#endif
|
||||
throw std::runtime_error("Cannot create test pipe");
|
||||
}
|
||||
|
||||
~InputPipe()
|
||||
{
|
||||
for (const int fd : descriptors)
|
||||
if (fd >= 0)
|
||||
CloseDescriptor(fd);
|
||||
}
|
||||
|
||||
InputPipe(const InputPipe &) = delete;
|
||||
InputPipe &operator=(const InputPipe &) = delete;
|
||||
|
||||
int Reader() const
|
||||
{
|
||||
return descriptors[0];
|
||||
}
|
||||
|
||||
void CloseWriter()
|
||||
{
|
||||
CloseDescriptor(descriptors[1]);
|
||||
descriptors[1] = -1;
|
||||
}
|
||||
|
||||
void Write(std::string_view text)
|
||||
{
|
||||
// These tests only write small messages that fit into the empty pipe.
|
||||
while (!text.empty())
|
||||
{
|
||||
#ifdef _WIN32
|
||||
const auto count = _write(descriptors[1], text.data(), static_cast<unsigned>(text.size()));
|
||||
#else
|
||||
const auto count = write(descriptors[1], text.data(), text.size());
|
||||
#endif
|
||||
if (count <= 0)
|
||||
throw std::runtime_error("Cannot write test pipe");
|
||||
text.remove_prefix(static_cast<std::size_t>(count));
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t BytesAvailable() const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
DWORD bytes = 0;
|
||||
if (!PeekNamedPipe(reinterpret_cast<HANDLE>(_get_osfhandle(Reader())), nullptr, 0, nullptr, &bytes, nullptr))
|
||||
#else
|
||||
int bytes = 0;
|
||||
if (ioctl(Reader(), FIONREAD, &bytes) != 0)
|
||||
#endif
|
||||
throw std::runtime_error("Cannot inspect test pipe");
|
||||
return static_cast<std::size_t>(bytes);
|
||||
}
|
||||
|
||||
private:
|
||||
int descriptors[2]{-1, -1};
|
||||
};
|
||||
|
||||
class InputFile
|
||||
{
|
||||
public:
|
||||
explicit InputFile(std::string_view text) : file(std::tmpfile())
|
||||
{
|
||||
if (!file || std::fwrite(text.data(), 1, text.size(), file.get()) != text.size() ||
|
||||
std::fflush(file.get()) != 0 || std::fseek(file.get(), 0, SEEK_SET) != 0)
|
||||
throw std::runtime_error("Cannot create test input file");
|
||||
}
|
||||
|
||||
int Descriptor() const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return _fileno(file.get());
|
||||
#else
|
||||
return fileno(file.get());
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
struct CloseFile
|
||||
{
|
||||
void operator()(std::FILE *value) const { std::fclose(value); }
|
||||
};
|
||||
std::unique_ptr<std::FILE, CloseFile> file;
|
||||
};
|
||||
|
||||
std::optional<std::string> PollAvailableLine(TerminalInput &input)
|
||||
{
|
||||
// A call has a bounded byte budget. These files contain at most 64 KiB+1.
|
||||
for (int attempt = 0; attempt < 64 && !input.IsEof(); ++attempt)
|
||||
if (auto line = input.PollLine())
|
||||
return line;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, EmptyOpenPipeReturnsImmediatelyWithoutEof)
|
||||
{
|
||||
InputPipe pipe;
|
||||
TerminalInput input(pipe.Reader());
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
EXPECT_LT(std::chrono::steady_clock::now() - start, std::chrono::milliseconds(500));
|
||||
EXPECT_FALSE(input.IsEof());
|
||||
EXPECT_FALSE(input.IsInteractive());
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, ReturnsCompleteLineWithoutWaitingForWriterToClose)
|
||||
{
|
||||
InputPipe pipe;
|
||||
TerminalInput input(pipe.Reader());
|
||||
pipe.Write("hello\n");
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("hello"));
|
||||
EXPECT_FALSE(input.IsEof());
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, DoesNotReadAnyByteOfNextLine)
|
||||
{
|
||||
InputPipe pipe;
|
||||
TerminalInput input(pipe.Reader());
|
||||
pipe.Write("first\nsecond\n");
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("first"));
|
||||
// Check the OS pipe, not just a private buffer: no read-ahead is allowed.
|
||||
EXPECT_EQ(pipe.BytesAvailable(), std::string_view("second\n").size());
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("second"));
|
||||
EXPECT_EQ(pipe.BytesAvailable(), 0U);
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, RetainsPartialLineWithoutWaitingForMoreBytes)
|
||||
{
|
||||
InputPipe pipe;
|
||||
TerminalInput input(pipe.Reader());
|
||||
pipe.Write("par");
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
EXPECT_LT(std::chrono::steady_clock::now() - start, std::chrono::milliseconds(500));
|
||||
EXPECT_FALSE(input.IsEof());
|
||||
pipe.Write("tial\nnext\n");
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("partial"));
|
||||
EXPECT_EQ(pipe.BytesAvailable(), std::string_view("next\n").size());
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, ReturnsFinalUnterminatedLineOnceAndMarksEof)
|
||||
{
|
||||
InputPipe pipe;
|
||||
TerminalInput input(pipe.Reader());
|
||||
pipe.Write("last");
|
||||
pipe.CloseWriter();
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("last"));
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, EmptyClosedPipeMarksEofWithoutAnEmptyLine)
|
||||
{
|
||||
InputPipe pipe;
|
||||
TerminalInput input(pipe.Reader());
|
||||
pipe.CloseWriter();
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, DrainsLinesBeforeReportingHangupAndAddsNoPhantomLine)
|
||||
{
|
||||
InputPipe pipe;
|
||||
TerminalInput input(pipe.Reader());
|
||||
pipe.Write("first\n\n");
|
||||
pipe.CloseWriter();
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("first"));
|
||||
EXPECT_FALSE(input.IsEof());
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>(""));
|
||||
EXPECT_FALSE(input.IsEof());
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, RemovesOnlyCrImmediatelyBeforeLfIncludingAcrossCalls)
|
||||
{
|
||||
InputPipe pipe;
|
||||
TerminalInput input(pipe.Reader());
|
||||
pipe.Write("one\r");
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
pipe.Write("\n\r\nx\ry\nfinal\r");
|
||||
pipe.CloseWriter();
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("one"));
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>(""));
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("x\ry"));
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("final\r"));
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, ReadsRegularFileAndFinalPartialLine)
|
||||
{
|
||||
InputFile file("first\r\nlast");
|
||||
TerminalInput input(file.Descriptor());
|
||||
EXPECT_FALSE(input.IsInteractive());
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("first"));
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("last"));
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, AcceptsExactLengthLimitWithNewline)
|
||||
{
|
||||
const std::string line(TerminalInput::MaxLineBytes, 'x');
|
||||
InputFile file(line + "\n");
|
||||
TerminalInput input(file.Descriptor());
|
||||
EXPECT_EQ(PollAvailableLine(input), std::optional<std::string>(line));
|
||||
EXPECT_FALSE(input.IsEof());
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, AcceptsExactLengthLimitWithoutNewline)
|
||||
{
|
||||
const std::string line(TerminalInput::MaxLineBytes, 'x');
|
||||
InputFile file(line);
|
||||
TerminalInput input(file.Descriptor());
|
||||
EXPECT_EQ(PollAvailableLine(input), std::optional<std::string>(line));
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, RejectsOverlongLineOnceRatherThanExecutingTruncatedCommand)
|
||||
{
|
||||
InputFile file(std::string(TerminalInput::MaxLineBytes + 1, 'x') + "\nnext\n");
|
||||
TerminalInput input(file.Descriptor());
|
||||
EXPECT_THROW(PollAvailableLine(input), std::length_error);
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
EXPECT_NO_THROW(EXPECT_FALSE(input.PollLine()));
|
||||
EXPECT_NO_THROW(EXPECT_FALSE(input.PollLine()));
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, InvalidDescriptorReportsErrorOnlyOnce)
|
||||
{
|
||||
TerminalInput input(-1);
|
||||
EXPECT_THROW(input.PollLine(), std::runtime_error);
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
EXPECT_NO_THROW(EXPECT_FALSE(input.PollLine()));
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, DestructionDoesNotCloseBorrowedDescriptor)
|
||||
{
|
||||
InputPipe pipe;
|
||||
{
|
||||
TerminalInput input(pipe.Reader());
|
||||
EXPECT_FALSE(input.IsEof());
|
||||
}
|
||||
pipe.Write("still open\n");
|
||||
TerminalInput next(pipe.Reader());
|
||||
EXPECT_EQ(next.PollLine(), std::optional<std::string>("still open"));
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
TEST(TerminalInputTest, PreservesBlockingAndNonblockingDescriptorFlags)
|
||||
{
|
||||
InputPipe pipe;
|
||||
const int original = fcntl(pipe.Reader(), F_GETFL);
|
||||
ASSERT_NE(original, -1);
|
||||
{
|
||||
TerminalInput input(pipe.Reader());
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
EXPECT_EQ(fcntl(pipe.Reader(), F_GETFL), original);
|
||||
}
|
||||
ASSERT_NE(fcntl(pipe.Reader(), F_SETFL, original | O_NONBLOCK), -1);
|
||||
{
|
||||
TerminalInput input(pipe.Reader());
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
pipe.Write("ready\n");
|
||||
EXPECT_EQ(input.PollLine(), std::optional<std::string>("ready"));
|
||||
}
|
||||
EXPECT_EQ(fcntl(pipe.Reader(), F_GETFL), original | O_NONBLOCK);
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, ClosedDescriptorReportsPollingErrorOnlyOnce)
|
||||
{
|
||||
InputPipe pipe;
|
||||
const int duplicate = dup(pipe.Reader());
|
||||
ASSERT_NE(duplicate, -1);
|
||||
TerminalInput input(duplicate);
|
||||
ASSERT_EQ(close(duplicate), 0);
|
||||
EXPECT_THROW(input.PollLine(), std::runtime_error);
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
EXPECT_NO_THROW(EXPECT_FALSE(input.PollLine()));
|
||||
}
|
||||
|
||||
TEST(TerminalInputTest, ReadErrorDiscardsPartialLineAndLatchesEof)
|
||||
{
|
||||
InputPipe pipe;
|
||||
const int duplicate = dup(pipe.Reader());
|
||||
ASSERT_NE(duplicate, -1);
|
||||
TerminalInput input(duplicate);
|
||||
pipe.Write("do not execute");
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
ASSERT_EQ(close(duplicate), 0);
|
||||
EXPECT_THROW(input.PollLine(), std::runtime_error);
|
||||
EXPECT_TRUE(input.IsEof());
|
||||
EXPECT_FALSE(input.PollLine());
|
||||
}
|
||||
#endif
|
||||
} // namespace
|
||||
Reference in new issue
Block a user