From 96476044387e98ee1ee7a6eb992b521bd447813c Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 3 Mar 2023 16:55:33 +0100 Subject: Renamed whiteboard to webchat --- tests/Makefile | 14 +-- tests/test-diff.cpp | 199 ------------------------------------ tests/test-qrcode.cpp | 140 ------------------------- tests/test-webchat.cpp | 255 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test-whiteboard.cpp | 255 ---------------------------------------------- 5 files changed, 258 insertions(+), 605 deletions(-) delete mode 100644 tests/test-diff.cpp delete mode 100644 tests/test-qrcode.cpp create mode 100644 tests/test-webchat.cpp delete mode 100644 tests/test-whiteboard.cpp (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile index f3ec6c8..44c884a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -11,15 +11,13 @@ CXXFLAGS+=--coverage LDFLAGS+=--coverage endif -UNITS=storage.cpp config.cpp compiledsql.cpp qrcode.cpp whiteboard.cpp connectionregistry.cpp diff.cpp +UNITS=storage.cpp config.cpp compiledsql.cpp webchat.cpp connectionregistry.cpp UNITTESTS=test-config.cpp \ test-storage.cpp \ test-connectionregistry.cpp \ test-compiledsql.cpp \ - test-qrcode.cpp \ - test-whiteboard.cpp \ - test-diff.cpp + test-webchat.cpp CXXFLAGS+=\ -I/usr/src/googletest/googletest/include \ @@ -54,19 +52,13 @@ config.o: ../config.cpp connectionregistry.o: ../connectionregistry.cpp $(CXX) $(CXXFLAGS) -o $@ -c $< -diff.o: ../diff.cpp - $(CXX) $(CXXFLAGS) -o $@ -c $< - storage.o: ../storage.cpp $(CXX) $(CXXFLAGS) -o $@ -c $< compiledsql.o: ../compiledsql.cpp $(CXX) $(CXXFLAGS) -o $@ -c $< -whiteboard.o: ../whiteboard.cpp - $(CXX) $(CXXFLAGS) -o $@ -c $< - -qrcode.o: ../qrcode.cpp +webchat.o: ../webchat.cpp $(CXX) $(CXXFLAGS) -o $@ -c $< libgmock.a: diff --git a/tests/test-diff.cpp b/tests/test-diff.cpp deleted file mode 100644 index d09c58e..0000000 --- a/tests/test-diff.cpp +++ /dev/null @@ -1,199 +0,0 @@ -#include - -#include -#include -#include - -#include - -#include - -#include "libreichwein/file.h" - -#include "diff.h" - -namespace fs = std::filesystem; -namespace pt = boost::property_tree; -using namespace Reichwein; - -class DiffTest: public ::testing::Test -{ -protected: - DiffTest(){ - } - - ~DiffTest(){ - } -}; - -TEST_F(DiffTest, constructor) -{ - // empty constructor - { - Diff d{}; - EXPECT_EQ(d.get_xml(), "00"); - } - - // constructor via xml diff - { - EXPECT_THROW(Diff d{""}, std::exception); - } - { - EXPECT_THROW(Diff d{""}, std::exception); - EXPECT_THROW(Diff d{"0abc"}, std::exception); - EXPECT_THROW(Diff d{"0abc"}, std::exception); - EXPECT_THROW(Diff d{"00"}, std::exception); - EXPECT_THROW(Diff d{"50abc"}, std::exception); - EXPECT_THROW(Diff d{"0abc"}, std::exception); - } - - { - Diff d{"00abc"}; - EXPECT_EQ(d.get_xml(), "00abc"); - } - - { - Diff d{"55abc"}; - EXPECT_EQ(d.get_xml(), "55abc"); - } - - { - Diff d{"550abc"}; - EXPECT_EQ(d.get_xml(), "550abc"); - } - - // constructor via ptree - { - pt::ptree ptree; - EXPECT_THROW(Diff d{ptree}, std::exception); - - ptree.put("diff.start", 0); - ptree.put("diff.end", 0); - ptree.put("diff.data", "abc"); - Diff d{ptree}; - EXPECT_EQ(d.get_xml(), "00abc"); - } - - // constructor via versions - { - Diff d{"", ""}; - EXPECT_EQ(d.get_xml(), "00"); - } - { - Diff d{"a", "a"}; - EXPECT_EQ(d.get_xml(), "00"); - } - { - Diff d{"a", "b"}; - EXPECT_EQ(d.get_xml(), "01b"); - } - { - Diff d{"0a1", "0b1"}; - EXPECT_EQ(d.get_xml(), "12b"); - } - { - Diff d{"0abc1", "00b01"}; - EXPECT_EQ(d.get_xml(), "140b0"); - } - { - Diff d{"0ab1", "00b01"}; - EXPECT_EQ(d.get_xml(), "130b0"); - } - { - Diff d{"0abc1", "00b1"}; - EXPECT_EQ(d.get_xml(), "140b"); - } - { - Diff d{"0abc1", ""}; - EXPECT_EQ(d.get_xml(), "05"); - } - { - Diff d{"bc1", "0abc1"}; - EXPECT_EQ(d.get_xml(), "000a"); - } - { - Diff d{"0abc1", "0ab"}; - EXPECT_EQ(d.get_xml(), "35"); - } - { - Diff d{"0abc1", "0abc123"}; - EXPECT_EQ(d.get_xml(), "5523"); - } - { - Diff d{"0abc1", "010abc1"}; - EXPECT_EQ(d.get_xml(), "0001"); - } - { - Diff d{"0abc1", "0ac1"}; - EXPECT_EQ(d.get_xml(), "23"); - } - { - Diff d{"0abc1", "0abxc1"}; - EXPECT_EQ(d.get_xml(), "33x"); - } - { - Diff d{"abc", "c"}; - EXPECT_EQ(d.get_xml(), "02"); - } - { - Diff d{"aaaa", "aa"}; - EXPECT_EQ(d.get_xml(), "24"); - } - { - Diff d{"baaaa", "baa"}; - EXPECT_EQ(d.get_xml(), "35"); - } - { - Diff d{"baaaab", "baab"}; - EXPECT_EQ(d.get_xml(), "13"); - } - { - Diff d{"baaaab", "baaaaaaab"}; - EXPECT_EQ(d.get_xml(), "11aaa"); - } -} - -TEST_F(DiffTest, empty) -{ - { - Diff d; - EXPECT_TRUE(d.empty()); - } - - { - Diff d{"13"}; - EXPECT_FALSE(d.empty()); - } - - { - Diff d{"11"}; - EXPECT_TRUE(d.empty()); - } - - { - Diff d{"11abc"}; - EXPECT_FALSE(d.empty()); - } - - { - Diff d{"00"}; - EXPECT_TRUE(d.empty()); - } -} - -TEST_F(DiffTest, diff_create) -{ - const char* result {diff_create("0abc1", "0ab")}; - - EXPECT_EQ(std::string(result), "35"); - free((void*)result); // this will be done by javascript side in real scenario -} - -TEST_F(DiffTest, diff_apply) -{ - const char* result {diff_apply("0abc1", "35")}; - - EXPECT_EQ(std::string(result), "0ab"); - free((void*)result); // this will be done by javascript side in real scenario -} - diff --git a/tests/test-qrcode.cpp b/tests/test-qrcode.cpp deleted file mode 100644 index 96cfdb0..0000000 --- a/tests/test-qrcode.cpp +++ /dev/null @@ -1,140 +0,0 @@ -#include - -#include -#include -#include -#include - -#include "libreichwein/file.h" - -#include "qrcode.h" - -namespace fs = std::filesystem; - -class QRCodeTest: public ::testing::Test -{ -protected: - QRCodeTest(){ - } - - ~QRCodeTest() override{ - } - - void SetUp() override - { - QRCode::init(); - } - - void TearDown() override - { - } - -}; - -TEST_F(QRCodeTest, empty) -{ - unsigned char empty_png[] = { - 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, - 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x15, - 0x10, 0x00, 0x00, 0x00, 0x00, 0xdc, 0xec, 0x26, 0x09, 0x00, 0x00, 0x00, - 0xa7, 0x49, 0x44, 0x41, 0x54, 0x28, 0xcf, 0x7d, 0x92, 0x4b, 0x12, 0xc3, - 0x30, 0x08, 0x43, 0x79, 0x99, 0xde, 0xff, 0xca, 0xea, 0x82, 0x51, 0xf9, - 0xc4, 0xae, 0x17, 0x89, 0x01, 0x03, 0x02, 0x89, 0x18, 0x47, 0x8a, 0x00, - 0x29, 0x2d, 0xe8, 0xb1, 0x4f, 0x86, 0x7b, 0x48, 0x02, 0x7b, 0x57, 0xcc, - 0xa6, 0xd4, 0x43, 0xd3, 0x96, 0x22, 0x9e, 0x38, 0x9e, 0x9e, 0xe2, 0xf3, - 0x9c, 0x1f, 0x15, 0xde, 0x81, 0xf5, 0x5d, 0x43, 0x02, 0x3f, 0x3e, 0xd5, - 0x1f, 0xf8, 0x36, 0xee, 0x88, 0x08, 0xae, 0x59, 0x37, 0xac, 0x00, 0xfe, - 0xee, 0x9d, 0x76, 0x6f, 0x48, 0x52, 0xb5, 0xcc, 0x7b, 0xfd, 0xed, 0x1f, - 0x1b, 0xa8, 0xec, 0x64, 0x6d, 0x51, 0x31, 0xab, 0x15, 0xf2, 0xb2, 0xdc, - 0x91, 0x4e, 0x24, 0x48, 0x7d, 0xa3, 0x53, 0x03, 0xaf, 0x0d, 0x54, 0xd3, - 0x9b, 0x6c, 0x16, 0xa1, 0x7b, 0x34, 0x29, 0x82, 0xa9, 0x9e, 0x84, 0x61, - 0xc6, 0x66, 0xfd, 0xc7, 0x9b, 0x73, 0x93, 0x2d, 0xc0, 0xf4, 0xc3, 0x1f, - 0x65, 0xf1, 0x1b, 0xd8, 0x30, 0xae, 0xca, 0x9a, 0x83, 0x02, 0x1c, 0x94, - 0xe5, 0x4d, 0x27, 0x42, 0x57, 0x3e, 0x34, 0xde, 0x74, 0x16, 0xf2, 0x2f, - 0x2f, 0xd5, 0xa5, 0xd0, 0x5a, 0x61, 0xde, 0x49, 0x00, 0x00, 0x00, 0x00, - 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82 - }; - // Test prepared with: - //File::setFile("empty.png", QRCode::getQRCode("")); - //then: xxd -i tests/empty.png >> tests/test-qrcode.cpp - - EXPECT_EQ(QRCode::getQRCode(""), std::string(reinterpret_cast(&empty_png[0]), sizeof(empty_png))); -} - -TEST_F(QRCodeTest, simple) -{ - unsigned char simple_png[] = { - 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, - 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x15, - 0x10, 0x00, 0x00, 0x00, 0x00, 0xdc, 0xec, 0x26, 0x09, 0x00, 0x00, 0x00, - 0xa5, 0x49, 0x44, 0x41, 0x54, 0x28, 0xcf, 0x75, 0x52, 0xd1, 0x0e, 0xc0, - 0x40, 0x04, 0x6b, 0x17, 0xff, 0xff, 0xcb, 0xf6, 0xe0, 0x4c, 0x39, 0x93, - 0x6c, 0x09, 0x47, 0x55, 0x01, 0x9a, 0xb9, 0xbb, 0xbb, 0x03, 0xf9, 0xa9, - 0x99, 0x86, 0x48, 0x80, 0x8c, 0xe4, 0x2c, 0xd5, 0xb7, 0xcf, 0x2d, 0xa4, - 0xfa, 0xeb, 0x9b, 0x61, 0xb5, 0xd9, 0x1c, 0x00, 0x9e, 0x2d, 0x2d, 0x88, - 0xcc, 0xb8, 0xdd, 0x18, 0xa4, 0x3b, 0xa9, 0xad, 0x7f, 0x1b, 0xff, 0x29, - 0xc0, 0xbd, 0x8a, 0x5c, 0xf0, 0x52, 0xc7, 0x2e, 0x90, 0x2a, 0x91, 0x51, - 0x4b, 0xfa, 0xc9, 0x2e, 0xfc, 0x40, 0x55, 0x6c, 0xb2, 0xe1, 0xf5, 0x4e, - 0xd3, 0x7f, 0xaa, 0xf2, 0xa6, 0x52, 0x24, 0x48, 0x77, 0xd9, 0xfa, 0x9c, - 0x7c, 0xae, 0xf7, 0x52, 0xa0, 0xab, 0xaa, 0x71, 0xeb, 0x5b, 0xa9, 0x21, - 0xfa, 0x50, 0x67, 0x5b, 0xe3, 0x7a, 0xd0, 0x67, 0x8f, 0x0e, 0xe4, 0x59, - 0x6c, 0xa4, 0x94, 0x96, 0xda, 0x29, 0xca, 0x80, 0xf5, 0x5c, 0x52, 0x53, - 0x3d, 0x9a, 0xa3, 0xcb, 0x7d, 0xaf, 0x93, 0xf9, 0xa1, 0xd2, 0xb9, 0xea, - 0xc6, 0x92, 0xf1, 0x7a, 0x0f, 0xaa, 0xee, 0x8c, 0x03, 0x2f, 0x88, 0x71, - 0xb5, 0xba, 0xf0, 0x88, 0xba, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, - 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82 - }; - // Test prepared with: - //File::setFile("simple.png", QRCode::getQRCode("abc")); - //then: xxd -i tests/simple.png >> tests/test-qrcode.cpp - EXPECT_EQ(QRCode::getQRCode("abc"), std::string(reinterpret_cast(&simple_png[0]), sizeof(simple_png))); -} - -TEST_F(QRCodeTest, url) -{ - unsigned char url_png[] = { - 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, - 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x1d, - 0x10, 0x00, 0x00, 0x00, 0x00, 0x23, 0x68, 0xe4, 0x90, 0x00, 0x00, 0x01, - 0x1f, 0x49, 0x44, 0x41, 0x54, 0x38, 0xcb, 0x75, 0x54, 0xdb, 0x0e, 0xc5, - 0x30, 0x08, 0x92, 0xa5, 0xff, 0xff, 0xcb, 0x9c, 0x07, 0xe7, 0x41, 0xb4, - 0x6b, 0xb2, 0x64, 0x36, 0x5e, 0xa8, 0xa0, 0x11, 0x76, 0xc8, 0x08, 0x92, - 0x24, 0xcb, 0xde, 0x7f, 0x75, 0x4e, 0xba, 0xe7, 0x01, 0x22, 0x48, 0x20, - 0xbf, 0xb4, 0x2b, 0xe1, 0xfc, 0x03, 0x4e, 0x85, 0xd4, 0x75, 0x06, 0xe5, - 0xbf, 0xc2, 0xf3, 0xd6, 0x7d, 0x9f, 0x58, 0x07, 0x50, 0xbd, 0xc2, 0x11, - 0x97, 0xb3, 0x42, 0x3b, 0xd0, 0xb4, 0x33, 0x7c, 0x87, 0x1e, 0x7f, 0x81, - 0xd7, 0x51, 0x0a, 0x01, 0xbe, 0x25, 0x69, 0x1d, 0xfe, 0xfe, 0xac, 0xea, - 0x2d, 0x8b, 0x57, 0x50, 0xed, 0xe1, 0xbb, 0x73, 0x3a, 0xaf, 0x65, 0x7b, - 0xba, 0xd7, 0xea, 0x64, 0x94, 0x93, 0x1c, 0x2a, 0xd8, 0xd3, 0x92, 0x7f, - 0x95, 0xec, 0x90, 0xa9, 0x9f, 0xb2, 0x94, 0xee, 0x88, 0xf4, 0xba, 0x72, - 0x4e, 0xf5, 0x5a, 0x69, 0x6b, 0xf0, 0xdc, 0x6b, 0xf8, 0x4b, 0x85, 0xac, - 0x3f, 0x8a, 0x7c, 0xba, 0x0c, 0x66, 0x78, 0xe7, 0x55, 0xdc, 0x96, 0xda, - 0x5e, 0x99, 0x2b, 0x38, 0xc5, 0x3f, 0x61, 0x6d, 0x0a, 0x81, 0xf0, 0x56, - 0x4c, 0x4e, 0x7d, 0xe4, 0x7c, 0x24, 0x9f, 0x39, 0x74, 0xe5, 0x86, 0x86, - 0x87, 0x04, 0x2e, 0x23, 0xd0, 0xdb, 0x20, 0x8a, 0xe6, 0x60, 0x4f, 0x91, - 0x7c, 0x32, 0xe6, 0x2f, 0xdc, 0x12, 0x79, 0x31, 0x0a, 0x56, 0xe7, 0xb2, - 0xef, 0x07, 0x98, 0x8f, 0x8d, 0xe0, 0x6c, 0xd4, 0x04, 0xb6, 0xe1, 0xbf, - 0xc2, 0x98, 0x97, 0x5d, 0x43, 0xaa, 0x5f, 0xdb, 0xca, 0x6f, 0x57, 0xd3, - 0xee, 0xe4, 0xec, 0x3e, 0x9c, 0x4d, 0x8e, 0x81, 0xc2, 0x2d, 0x5d, 0x56, - 0x7e, 0x32, 0x44, 0xbc, 0xed, 0x6a, 0x1a, 0x87, 0xf4, 0x2a, 0xe8, 0x6b, - 0xad, 0x75, 0x51, 0xfa, 0x66, 0xf4, 0x07, 0x01, 0x97, 0x8d, 0x08, 0x90, - 0x09, 0xb5, 0xef, 0x64, 0xe7, 0xbf, 0x49, 0x62, 0x8e, 0xfa, 0xec, 0xfb, - 0x26, 0xad, 0x8d, 0xba, 0x3b, 0xf5, 0xf6, 0xf8, 0x22, 0x95, 0xf7, 0x0f, - 0x4c, 0x20, 0xaf, 0x2d, 0xca, 0xaf, 0x0d, 0xc5, 0x00, 0x00, 0x00, 0x00, - 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82 - }; - // Test prepared with: - //File::setFile("url.png", QRCode::getQRCode("https://reichwein.it/whiteboard")); - //then: xxd -i tests/url.png >> tests/test-qrcode.cpp - EXPECT_EQ(QRCode::getQRCode("https://reichwein.it/whiteboard"), std::string(reinterpret_cast(&url_png[0]), sizeof(url_png))); -} - -TEST_F(QRCodeTest, deterministic) -{ - EXPECT_EQ(QRCode::getQRCode("data1"), QRCode::getQRCode("data1")); -} - -TEST_F(QRCodeTest, different_data) -{ - EXPECT_NE(QRCode::getQRCode("data1"), QRCode::getQRCode("data2")); -} diff --git a/tests/test-webchat.cpp b/tests/test-webchat.cpp new file mode 100644 index 0000000..3f70bcf --- /dev/null +++ b/tests/test-webchat.cpp @@ -0,0 +1,255 @@ +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "libreichwein/file.h" +#include "libreichwein/process.h" + +#include "config.h" +#include "storage.h" +#include "whiteboard.h" + +namespace bp = boost::process; +namespace fs = std::filesystem; +using namespace Reichwein; +using namespace std::string_literals; + +namespace { + const fs::path webserverConfigFilename{"./webserver.conf"}; + + const fs::path testConfigFilename{"./whiteboard.conf"}; + const fs::path testDbFilename{"./whiteboard.db3"}; +} + +class WhiteboardTest: public ::testing::Test +{ +protected: + WhiteboardTest(){ + } + + ~WhiteboardTest() override{ + } + + void SetUp() override + { + File::setFile(testConfigFilename, R"CONFIG( + + ::1:9876 + . + 2592000 + 4 + 3 + +)CONFIG"); + std::error_code ec; + fs::remove(testDbFilename, ec); + + m_config = std::make_shared(testConfigFilename); + + m_pid = fork(); + if (m_pid == -1) { + throw std::runtime_error("Error on fork(): "s + strerror(errno)); + } else if (m_pid == 0) { // child + Whiteboard whiteboard; + std::vector argvv{{"whiteboard", "-c", testConfigFilename.generic_string()}}; + char* argv[] = {argvv[0].data(), argvv[1].data(), argvv[2].data(), nullptr}; + whiteboard.run(argvv.size(), argv); + exit(0); + } + Process::wait_for_pid_listening_on(m_pid, 9876); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + } + + void TearDown() override + { + if (m_pid == 0) + throw std::runtime_error("Whiteboard not running on requesting SIGTERM"); + + if (kill(m_pid, SIGTERM) != 0) + throw std::runtime_error("Unable to SIGTERM Whiteboard"); + + if (int result = waitpid(m_pid, NULL, 0); result != m_pid) + throw std::runtime_error("waitpid returned "s + std::to_string(result)); + + std::error_code ec; + fs::remove(testDbFilename, ec); + fs::remove(testConfigFilename, ec); + } + + std::shared_ptr m_config; + pid_t m_pid{}; +}; + +class WebsocketClient +{ +public: + WebsocketClient() + { + std::string host = "::1"; + auto const port = "9876" ; + + // These objects perform our I/O + boost::asio::ip::tcp::resolver resolver{ioc_}; + ws_ = std::make_unique>(ioc_); + + // Look up the domain name + resolver_results_ = resolver.resolve(host, port); + + connect(); + handshake(); + } + + void connect() + { + // Make the connection on the IP address we get from a lookup + ep_ = boost::asio::connect(boost::beast::get_lowest_layer(*ws_), resolver_results_); + } + + void handshake() + { + // Update the host_ string. This will provide the value of the + // Host HTTP header during the WebSocket handshake. + // See https://tools.ietf.org/html/rfc7230#section-5.4 + std::string host{"[::1]:9876"}; + + // Set a decorator to change the User-Agent of the handshake + ws_->set_option(boost::beast::websocket::stream_base::decorator( + [](boost::beast::websocket::request_type& req) + { + req.set(boost::beast::http::field::user_agent, + std::string("Reichwein.IT Test Websocket Client")); + })); + + // Perform the websocket handshake + ws_->handshake(host, "/"); + + } + + void write(const std::string& data) + { + ws_->write(boost::asio::buffer(data)); + } + + std::string read() + { + boost::beast::flat_buffer buffer; + ws_->read(buffer); + return {boost::asio::buffers_begin(buffer.data()), boost::asio::buffers_end(buffer.data())}; + } + + ~WebsocketClient() + { + } + + bool is_open() + { + return ws_->is_open(); + } + +private: + boost::asio::io_context ioc_; + boost::asio::ip::tcp::resolver::results_type resolver_results_; + std::unique_ptr> ws_; + boost::asio::ip::tcp::endpoint ep_; +}; + +// +// tests via websocket server in separate process (hides coverage) +// + +TEST_F(WhiteboardTest, websocket_server_connection) +{ + WebsocketClient wc; +} + +TEST_F(WhiteboardTest, websocket_server_generate_id) +{ + WebsocketClient wc; + + wc.write("newid"); + std::string result0 {wc.read()}; + ASSERT_TRUE(boost::algorithm::starts_with(result0, "newid")); + ASSERT_TRUE(boost::algorithm::ends_with(result0, "")); + ASSERT_EQ(result0.size(), 58); + + wc.write("newid"); + std::string result1 {wc.read()}; + ASSERT_TRUE(boost::algorithm::starts_with(result1, "newid")); + ASSERT_TRUE(boost::algorithm::ends_with(result1, "")); + ASSERT_EQ(result1.size(), 58); + + ASSERT_NE(result0, result1); +} + +// check number of threads as configured +TEST_F(WhiteboardTest, threads) +{ + ASSERT_GE(Process::number_of_threads(m_pid), 4); +} + +TEST_F(WhiteboardTest, max_connections) +{ + WebsocketClient wc1; + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + ASSERT_TRUE(wc1.is_open()); + + WebsocketClient wc2; + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + ASSERT_TRUE(wc2.is_open()); + + WebsocketClient wc3; + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + ASSERT_TRUE(wc3.is_open()); + + ASSERT_THROW(WebsocketClient wc4, std::exception); +} + +TEST_F(WhiteboardTest, id) +{ + WebsocketClient wc; + + wc.write("getfile1"); + std::string result {wc.read()}; + + EXPECT_EQ(result, "getfile00"); + + wc.write("getfile"); + result = wc.read(); + EXPECT_EQ(result, "errorMessage handling error: Invalid id (empty)"); + + wc.write("getfile01234567890123456789"); + result = wc.read(); + EXPECT_EQ(result, "errorMessage handling error: Invalid id (size > 16)"); + + wc.write("getfileX"); + result = wc.read(); + EXPECT_EQ(result, "errorMessage handling error: Invalid id char: X"); + + wc.write("getfilea."); + result = wc.read(); + EXPECT_EQ(result, "errorMessage handling error: Invalid id char: ."); + + wc.write("getfilea$b"); + result = wc.read(); + EXPECT_EQ(result, "errorMessage handling error: Invalid id char: $"); +} + diff --git a/tests/test-whiteboard.cpp b/tests/test-whiteboard.cpp deleted file mode 100644 index 3f70bcf..0000000 --- a/tests/test-whiteboard.cpp +++ /dev/null @@ -1,255 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "libreichwein/file.h" -#include "libreichwein/process.h" - -#include "config.h" -#include "storage.h" -#include "whiteboard.h" - -namespace bp = boost::process; -namespace fs = std::filesystem; -using namespace Reichwein; -using namespace std::string_literals; - -namespace { - const fs::path webserverConfigFilename{"./webserver.conf"}; - - const fs::path testConfigFilename{"./whiteboard.conf"}; - const fs::path testDbFilename{"./whiteboard.db3"}; -} - -class WhiteboardTest: public ::testing::Test -{ -protected: - WhiteboardTest(){ - } - - ~WhiteboardTest() override{ - } - - void SetUp() override - { - File::setFile(testConfigFilename, R"CONFIG( - - ::1:9876 - . - 2592000 - 4 - 3 - -)CONFIG"); - std::error_code ec; - fs::remove(testDbFilename, ec); - - m_config = std::make_shared(testConfigFilename); - - m_pid = fork(); - if (m_pid == -1) { - throw std::runtime_error("Error on fork(): "s + strerror(errno)); - } else if (m_pid == 0) { // child - Whiteboard whiteboard; - std::vector argvv{{"whiteboard", "-c", testConfigFilename.generic_string()}}; - char* argv[] = {argvv[0].data(), argvv[1].data(), argvv[2].data(), nullptr}; - whiteboard.run(argvv.size(), argv); - exit(0); - } - Process::wait_for_pid_listening_on(m_pid, 9876); - std::this_thread::sleep_for(std::chrono::milliseconds(20)); - } - - void TearDown() override - { - if (m_pid == 0) - throw std::runtime_error("Whiteboard not running on requesting SIGTERM"); - - if (kill(m_pid, SIGTERM) != 0) - throw std::runtime_error("Unable to SIGTERM Whiteboard"); - - if (int result = waitpid(m_pid, NULL, 0); result != m_pid) - throw std::runtime_error("waitpid returned "s + std::to_string(result)); - - std::error_code ec; - fs::remove(testDbFilename, ec); - fs::remove(testConfigFilename, ec); - } - - std::shared_ptr m_config; - pid_t m_pid{}; -}; - -class WebsocketClient -{ -public: - WebsocketClient() - { - std::string host = "::1"; - auto const port = "9876" ; - - // These objects perform our I/O - boost::asio::ip::tcp::resolver resolver{ioc_}; - ws_ = std::make_unique>(ioc_); - - // Look up the domain name - resolver_results_ = resolver.resolve(host, port); - - connect(); - handshake(); - } - - void connect() - { - // Make the connection on the IP address we get from a lookup - ep_ = boost::asio::connect(boost::beast::get_lowest_layer(*ws_), resolver_results_); - } - - void handshake() - { - // Update the host_ string. This will provide the value of the - // Host HTTP header during the WebSocket handshake. - // See https://tools.ietf.org/html/rfc7230#section-5.4 - std::string host{"[::1]:9876"}; - - // Set a decorator to change the User-Agent of the handshake - ws_->set_option(boost::beast::websocket::stream_base::decorator( - [](boost::beast::websocket::request_type& req) - { - req.set(boost::beast::http::field::user_agent, - std::string("Reichwein.IT Test Websocket Client")); - })); - - // Perform the websocket handshake - ws_->handshake(host, "/"); - - } - - void write(const std::string& data) - { - ws_->write(boost::asio::buffer(data)); - } - - std::string read() - { - boost::beast::flat_buffer buffer; - ws_->read(buffer); - return {boost::asio::buffers_begin(buffer.data()), boost::asio::buffers_end(buffer.data())}; - } - - ~WebsocketClient() - { - } - - bool is_open() - { - return ws_->is_open(); - } - -private: - boost::asio::io_context ioc_; - boost::asio::ip::tcp::resolver::results_type resolver_results_; - std::unique_ptr> ws_; - boost::asio::ip::tcp::endpoint ep_; -}; - -// -// tests via websocket server in separate process (hides coverage) -// - -TEST_F(WhiteboardTest, websocket_server_connection) -{ - WebsocketClient wc; -} - -TEST_F(WhiteboardTest, websocket_server_generate_id) -{ - WebsocketClient wc; - - wc.write("newid"); - std::string result0 {wc.read()}; - ASSERT_TRUE(boost::algorithm::starts_with(result0, "newid")); - ASSERT_TRUE(boost::algorithm::ends_with(result0, "")); - ASSERT_EQ(result0.size(), 58); - - wc.write("newid"); - std::string result1 {wc.read()}; - ASSERT_TRUE(boost::algorithm::starts_with(result1, "newid")); - ASSERT_TRUE(boost::algorithm::ends_with(result1, "")); - ASSERT_EQ(result1.size(), 58); - - ASSERT_NE(result0, result1); -} - -// check number of threads as configured -TEST_F(WhiteboardTest, threads) -{ - ASSERT_GE(Process::number_of_threads(m_pid), 4); -} - -TEST_F(WhiteboardTest, max_connections) -{ - WebsocketClient wc1; - std::this_thread::sleep_for(std::chrono::milliseconds(20)); - ASSERT_TRUE(wc1.is_open()); - - WebsocketClient wc2; - std::this_thread::sleep_for(std::chrono::milliseconds(20)); - ASSERT_TRUE(wc2.is_open()); - - WebsocketClient wc3; - std::this_thread::sleep_for(std::chrono::milliseconds(20)); - ASSERT_TRUE(wc3.is_open()); - - ASSERT_THROW(WebsocketClient wc4, std::exception); -} - -TEST_F(WhiteboardTest, id) -{ - WebsocketClient wc; - - wc.write("getfile1"); - std::string result {wc.read()}; - - EXPECT_EQ(result, "getfile00"); - - wc.write("getfile"); - result = wc.read(); - EXPECT_EQ(result, "errorMessage handling error: Invalid id (empty)"); - - wc.write("getfile01234567890123456789"); - result = wc.read(); - EXPECT_EQ(result, "errorMessage handling error: Invalid id (size > 16)"); - - wc.write("getfileX"); - result = wc.read(); - EXPECT_EQ(result, "errorMessage handling error: Invalid id char: X"); - - wc.write("getfilea."); - result = wc.read(); - EXPECT_EQ(result, "errorMessage handling error: Invalid id char: ."); - - wc.write("getfilea$b"); - result = wc.read(); - EXPECT_EQ(result, "errorMessage handling error: Invalid id char: $"); -} - -- cgit v1.2.3