From 01e9e84887955a9e3efd5f70cebcaa45c4333564 Mon Sep 17 00:00:00 2001 From: Doralitze <doralitze@chaotikum.org> Date: Tue, 5 Jan 2021 19:44:15 +0100 Subject: [PATCH] add: Loopback connection client for testing purposes `get_send_data()` doesn't work yet as I haven't found a C++ style way to safely cast const std::string& to std::string in line 24 of loopback_connection.cpp --- src/test/loopback_connection_client.cpp | 42 +++++++++++++++++++ src/test/loopback_connection_client.hpp | 55 +++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/test/loopback_connection_client.cpp create mode 100644 src/test/loopback_connection_client.hpp diff --git a/src/test/loopback_connection_client.cpp b/src/test/loopback_connection_client.cpp new file mode 100644 index 0000000..13b7f17 --- /dev/null +++ b/src/test/loopback_connection_client.cpp @@ -0,0 +1,42 @@ +/* + * loopback_connection_client.cpp + * + * Created on: 05.01.2021 + * Author: doralitze + */ + +#include "test/loopback_connection_client.hpp" + +#include <string.h> + +namespace rmrf::test { + loopback_connection_client::loopback_connection_client(rmrf::net::connection_client::incomming_data_cb mut_send_data_cb_) : + rmrf::net::connection_client{}, mut_send_data_cb(mut_send_data_cb_), send_data_archive{} { + // Does nothing special + } + + loopback_connection_client::~loopback_connection_client() { + // Also doesn't do anything fancy. + } + + void loopback_connection_client::write_data(const std::string& data) { + // TODO fixme + //this->send_data_archive.push_back(data_ptr); + if(this->mut_send_data_cb != nullptr) { + this->mut_send_data_cb(data); + } + } + + void loopback_connection_client::send_data_to_incomming_data_cb(const std::string& data) { + if(this->in_data_cb != nullptr) { + this->in_data_cb(data); + } + } + + std::vector<std::string> loopback_connection_client::get_send_data() { + return this->send_data_archive; + } + +} + + diff --git a/src/test/loopback_connection_client.hpp b/src/test/loopback_connection_client.hpp new file mode 100644 index 0000000..ae5d779 --- /dev/null +++ b/src/test/loopback_connection_client.hpp @@ -0,0 +1,55 @@ +/* + * loopback_connection_client.hpp + * + * Created on: 05.01.2021 + * Author: doralitze + */ + +#pragma once + +#include <vector> + +#include "net/connection_client.hpp" + +namespace rmrf::test { + +/** + * Use this cass to mock a connection client. + */ +class loopback_connection_client : public rmrf::net::connection_client { +private: + const rmrf::net::connection_client::incomming_data_cb mut_send_data_cb; + const std::vector<std::string> send_data_archive; +public: + /** + * This constructor uses the given callback to notify the test suite that the module under test + * send data. + */ + loopback_connection_client(rmrf::net::connection_client::incomming_data_cb mut_send_data_cb_); + + /** + * Just the mandatory virtual destructor. + */ + virtual ~loopback_connection_client(); + + /** + * This method gets called by the module under test as it simulates the behavior of a normal connection client. + * @param data The data the module wants to send. + */ + virtual void write_data(const std::string& data); + + /** + * This method sends data to the connections incoming data callback. + * Use it to mimic a remote client sending data. + * + * @param data The data to send. + */ + void send_data_to_incomming_data_cb(const std::string& data); + + /** + * Use this method in order to get all data the module under test has send. + */ + std::vector<std::string> get_send_data(); +}; + +} -- GitLab