1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-10-27 05:23:39 -04:00
polybar/tests/unit_tests/ipc/decoder.cpp
Patrick Ziegler 3356188056
Use sockets for IPC (#2539)
Deprecates not using `polybar-msg` for IPC.

Fixes #2532
Closes #2465
Fixes #2504

* Create FIFO specific NamedPipeHandle subclass to PipeHandle

* Prototype SocketHandle

* Move mainloop up to main.cpp

* Pass eventloop to ipc class

* Deprecate sending ipc over the named pipe

Unfortunately, we can only show the warning in the polybar log and not
give the user any feedback because the pipe is one-way

* Move eventloop into its own namespace

* Prototype ipc socket handling

* Remove handles from ipc_client

Should be independent from eventloop logic

* Remove ipc clients when finished

* Add tests for ipc_client decoding

* Add callback for complete ipc messages

* Remove template param from mixins

* Move signal handler to new callback system

* Move poll handle to new callback system

* Move FSEventHandle to new callback system

* Move TimerHandle and AsyncHandle to new callback system

* Move PipeHandle to new callback system

* Implement socket functionality in new callback system

* Correctly reset ipc named pipe handle

* Let client close handles in error callback

* Wrap client pipe and ipc::client in connection class

* Better decoder log messages

* Socket path logic

* Fix CI warnings

* Remove UVHandleGeneric

* Fix error when socket folder already exists

* Proof of concept message writeback

* Restructure ipc files

* polybar-msg: Use sockets

* polybar-msg: Better syntax for actions

* Fix memory leak with fifo

After EOF, the pipe wasn't closed and EOF was called all the time, each
time allocating a new pipe.

* Make polybar-msg compile on its own

* Rudimentary writeback for polybar-msg

* Fix payload reference going out of scope.

* Add IPC documentation

* Cleanup polybar-msg code

* Specify the v0 ipc message format

* Close ipc connection after message

* Fix ipc tests

* Properly close ipc connections

* Fix polybar-msg not working with action string

* Write polybar-msg manpage

* polybar-msg: Stop using exit()

* ipc: Print log message with PID

* Add tests for ipc util

* polybar-msg: Print PID with success message

* ipc: Propagate message errors

* Rename ipc::client to ipc::decoder

* Rename ipc.cpp to polybar-msg.cpp

* ipc: Write encoder function and fix decoder bugs

* ipc: Use message format for responses

* ipc: Handle wrong message types

* ipc: Write back error message if ipc message cannot be processed

This only happens for commands and empty actions.
Non-empty actions are not immediately executed, but deferred until the
next loop iteration.

* Remove TODO about deleting runtime directory

The socket file is not deleted after socket.close() is called, only
after libuv executes the close callback.
So we can't just call rmdir because it will probably always fail.

* CLeanup WriteRequest

* Update manpage authors

* Cleanup
2022-01-22 20:35:37 +01:00

96 lines
3.1 KiB
C++

#include "ipc/decoder.hpp"
#include <cstring>
#include <string>
#include <vector>
#include "common/test.hpp"
#include "components/logger.hpp"
#include "gmock/gmock.h"
#include "ipc/msg.hpp"
using namespace polybar;
using namespace ipc;
using ::testing::InSequence;
static std::vector<uint8_t> get_msg(decltype(MAGIC) magic, uint32_t version, type_t type, const std::string& msg) {
std::vector<uint8_t> data(HEADER_SIZE);
header* header = reinterpret_cast<ipc::header*>(data.data());
std::copy(magic.begin(), magic.end(), header->s.magic);
header->s.version = version;
header->s.size = msg.size();
header->s.type = type;
data.insert(data.end(), msg.begin(), msg.end());
return data;
}
static decltype(MAGIC) MAGIC_WRONG = {'0', '0', '0', '0', '0', '0', '0'};
static type_t TYPE_ACTION = to_integral(v0::ipc_type::ACTION);
static auto MSG1 = get_msg(MAGIC, VERSION, TYPE_ACTION, "foobar");
static auto MSG2 = get_msg(MAGIC, VERSION, TYPE_ACTION, "");
static auto MSG_WRONG1 = get_msg(MAGIC_WRONG, VERSION, TYPE_ACTION, "");
static auto MSG_WRONG2 = get_msg(MAGIC, 120, TYPE_ACTION, "");
static auto MSG_WRONG3 = get_msg(MAGIC_WRONG, 120, TYPE_ACTION, "");
class MockCallback {
public:
MOCK_METHOD(void, cb, (uint8_t version, type_t, const vector<uint8_t>&));
};
static logger null_logger(loglevel::NONE);
class DecoderTest : public ::testing::Test {
protected:
MockCallback cb;
decoder dec{null_logger, [this](uint8_t version, auto type, const auto& data) { cb.cb(version, type, data); }};
};
TEST_F(DecoderTest, single_msg1) {
EXPECT_CALL(cb, cb(0, TYPE_ACTION, vector<uint8_t>(MSG1.begin() + HEADER_SIZE, MSG1.end()))).Times(1);
EXPECT_NO_THROW(dec.on_read(MSG1.data(), MSG1.size()));
}
TEST_F(DecoderTest, single_msg2) {
EXPECT_CALL(cb, cb(0, TYPE_ACTION, vector<uint8_t>(MSG2.begin() + HEADER_SIZE, MSG2.end()))).Times(1);
EXPECT_NO_THROW(dec.on_read(MSG2.data(), MSG2.size()));
}
TEST_F(DecoderTest, single_msg_wrong1) {
EXPECT_THROW(dec.on_read(MSG_WRONG1.data(), MSG_WRONG1.size()), decoder::error);
// After an error, any further read fails
EXPECT_THROW(dec.on_read(MSG1.data(), MSG1.size()), decoder::error);
}
TEST_F(DecoderTest, single_msg_wrong2) {
EXPECT_THROW(dec.on_read(MSG_WRONG2.data(), MSG_WRONG2.size()), decoder::error);
// After an error, any further read fails
EXPECT_THROW(dec.on_read(MSG1.data(), MSG1.size()), decoder::error);
}
TEST_F(DecoderTest, single_msg_wrong3) {
EXPECT_THROW(dec.on_read(MSG_WRONG3.data(), MSG_WRONG3.size()), decoder::error);
// After an error, any further read fails
EXPECT_THROW(dec.on_read(MSG1.data(), MSG1.size()), decoder::error);
}
TEST_F(DecoderTest, byte_by_byte) {
EXPECT_CALL(cb, cb(0, TYPE_ACTION, vector<uint8_t>(MSG1.begin() + HEADER_SIZE, MSG1.end()))).Times(1);
for (const uint8_t c : MSG1) {
EXPECT_NO_THROW(dec.on_read(&c, 1));
}
}
TEST_F(DecoderTest, multiple) {
static constexpr int NUM_ITER = 10;
{
InSequence seq;
EXPECT_CALL(cb, cb(0, TYPE_ACTION, vector<uint8_t>(MSG1.begin() + HEADER_SIZE, MSG1.end()))).Times(NUM_ITER);
}
for (int i = 0; i < NUM_ITER; i++) {
EXPECT_NO_THROW(dec.on_read(MSG1.data(), MSG1.size()));
}
}