From e8a7c88b6cedbd05b183e85dff74c513bfcd774e Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 5 Jan 2023 19:47:05 +0100 Subject: Test config.cpp --- tests/test-auth.cpp | 12 ++- tests/test-config.cpp | 230 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 239 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/test-auth.cpp b/tests/test-auth.cpp index 8397b35..37bc02f 100644 --- a/tests/test-auth.cpp +++ b/tests/test-auth.cpp @@ -18,7 +18,16 @@ class AuthFixture public: AuthFixture(){} ~AuthFixture(){} - void setup(){} + void setup() + { + int filedes[2]; + if (pipe(filedes) == -1) + throw std::runtime_error("Pipe error"); + if (close(2) == -1) + throw std::runtime_error("Can't close stderr"); + if (dup(filedes[1]) == -1) + throw std::runtime_error("Replace stdout w/ pipe input"); + } void teardown(){} }; @@ -36,6 +45,7 @@ BOOST_FIXTURE_TEST_CASE(validate, AuthFixture) { BOOST_CHECK(Auth::validate("t5MMkLQXzYkdw", "abc")); + BOOST_CHECK(!Auth::validate("", "")); BOOST_CHECK(!Auth::validate("abc", "abc")); BOOST_CHECK(!Auth::validate("t5MNkLQXzYkdw", "abc")); } diff --git a/tests/test-config.cpp b/tests/test-config.cpp index ddba9c9..ddae1c8 100644 --- a/tests/test-config.cpp +++ b/tests/test-config.cpp @@ -6,23 +6,249 @@ #include #include +#include #include #include #include "config.h" +#include "libreichwein/file.h" + using namespace std::string_literals; +namespace fs = std::filesystem; + +const fs::path testConfigFilename{"test-webserver.conf"}; class ConfigFixture { public: ConfigFixture(){} ~ConfigFixture(){} - void setup(){} - void teardown(){} + void setup() + { + int filedes[2]; + if (pipe(filedes) == -1) + throw std::runtime_error("Pipe error"); + if (close(1) == -1) + throw std::runtime_error("Can't close stdout"); + if (dup(filedes[1]) == -1) + throw std::runtime_error("Replace stdout w/ pipe input"); + } + void teardown() + { + std::error_code ec; + fs::remove(testConfigFilename); + } }; BOOST_FIXTURE_TEST_CASE(config, ConfigFixture) { + Reichwein::File::setFile(testConfigFilename, R"CONFIG( + + user1 + www-data + 10 + + plugins + + + antcom.de + lists.antcom.de + antcom.de + www.antcom.de + ip6-localhost + 127.0.0.1 + [::1] + reichwein.mooo.com + [2001:a61:410:c001:5e51:4fff:fea2:ec7f] + + + static-files + /home/ernie/homepage/test + + + + webbox + /home/ernie/testbox + Testbox1 + 0 + /home/ernie/code/webserver/plugins/webbox/html + + + + webbox + /home/ernie/testbox + Testbox1 + 1 + /home/ernie/code/webserver/plugins/webbox/html + + + weblog + /home/ernie/testblog + Roland Reichweins Blog + Roland Reichwein, Blog + + + statistics + + + + cgi + /home/ernie/code/webserver/cgi-bin + + + + fcgi + 127.0.0.1:9000 + + + fcgi + /home/ernie/code/webserver/fastcgi/socket + + + fcgi + /run/php/php-fpm.sock + + + + fcgi + 127.0.0.1:9001 + + + fcgi + 127.0.0.1:9002 + + + fcgi + 127.0.0.1:9003 + + + static-files + /home/ernie/code/webshop/html + + + + static-files + /home/ernie/code/downtube/html + + + fcgi + 127.0.0.1:9004 + + + + static-files + /home/ernie/code/whiteboard/html + + + fcgi + 127.0.0.1:9014 + + + + redirect + https://www.antcom.de/ + 301 + Redirecting to antcom.de ... + + /home/ernie/code/webserver/fullchain.pem + /home/ernie/code/webserver/privkey.pem + + + marx + marx.antcom.de + marx1.antcom.de + localhost + + static-files + /home/ernie/homepage/test1 + + + cgi + /home/ernie/code/webserver/cgi-bin + + + cgi + /usr/lib/cgit/cgit.cgi + + + static-files + /usr/share/cgit + + /home/ernie/code/webserver/cert.pem + /home/ernie/code/webserver/key.pem + + + + +
127.0.0.1
+ 8080 + http + + antcom.de + marx + +
+ +
2001:a61:410:c001:5e51:4fff:fea2:ec7f
+ 80 + http + antcom.de +
+ +
::1
+ 8080 + http + +
+ +
127.0.0.1
+ 8081 + https +
+ +
::1
+ 8081 + https +
+ +
127.0.0.1
+ 443 + https +
+
+
+ +)CONFIG"); + + Config config{testConfigFilename}; + + BOOST_CHECK_EQUAL(config.User(), "user1"); + BOOST_CHECK_EQUAL(config.Group(), "www-data"); + BOOST_CHECK_EQUAL(config.Threads(), 10); + + auto pd{config.PluginDirectories()}; + BOOST_CHECK_EQUAL(pd.size(), 1); + BOOST_CHECK_EQUAL(pd[0], "plugins"); + + auto sites{config.Sites()}; + BOOST_CHECK_EQUAL(sites.size(), 2); + + auto sockets{config.Sockets()}; + BOOST_CHECK_EQUAL(sockets.size(), 4); // 2 privileged ports skipped + + const Path& p {config.GetPath(sockets[0], "antcom.de", "/webbox1/path1/abc.txt")}; + BOOST_CHECK_EQUAL(p.requested, "/webbox1"); + + BOOST_CHECK_EQUAL(config.PluginIsConfigured("webbox"), true); + BOOST_CHECK_EQUAL(config.PluginIsConfigured("webmux"), false); + + (void)config.dump(); } -- cgit v1.2.3