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 --- config.cpp | 12 +-- config.h | 13 ++- https.cpp | 4 +- tests/test-auth.cpp | 12 ++- tests/test-config.cpp | 230 +++++++++++++++++++++++++++++++++++++++++++++++++- webserver.cpp | 3 +- 6 files changed, 257 insertions(+), 17 deletions(-) diff --git a/config.cpp b/config.cpp index b892317..342d973 100644 --- a/config.cpp +++ b/config.cpp @@ -9,6 +9,7 @@ #include #include +namespace fs = std::filesystem; namespace pt = boost::property_tree; using namespace std::string_literals; @@ -26,15 +27,16 @@ namespace { } // anonymous namespace -void Config::readConfigfile(std::string filename) +void Config::readConfigfile(const std::filesystem::path& filename) { - if (filename == "") { - filename = default_filename; + fs::path used_filename{filename}; + if (used_filename.empty()) { + used_filename = default_filename; } pt::ptree tree; - pt::read_xml(filename, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); + pt::read_xml(used_filename, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); // mandatory m_user = tree.get("webserver.user"); @@ -203,7 +205,7 @@ void Config::create_look_up_table() } } -Config::Config(const std::string& filename) +Config::Config(const std::filesystem::path& filename) { readConfigfile(filename); dump(); diff --git a/config.h b/config.h index a021c80..d0e78ba 100644 --- a/config.h +++ b/config.h @@ -6,8 +6,7 @@ #include #include -namespace fs = std::filesystem; - +// URL path, not FS struct Path { std::string requested; // the requested path @@ -21,8 +20,8 @@ struct Site // std::string name; is the index in the m_sites map std::unordered_set hosts; std::vector paths; - fs::path cert_path; - fs::path key_path; + std::filesystem::path cert_path; + std::filesystem::path key_path; }; enum class SocketProtocol @@ -43,9 +42,9 @@ struct Socket class Config { - const std::string default_filename{"/etc/webserver.conf"}; + const std::filesystem::path default_filename{"/etc/webserver.conf"}; - void readConfigfile(std::string filename); + void readConfigfile(const std::filesystem::path& filename); void expand_socket_sites(); void validate(); void create_look_up_table(); @@ -58,7 +57,7 @@ class Config std::vector m_sockets; public: - Config(const std::string& filename); + Config(const std::filesystem::path& filename); // Data getters std::string User() const; diff --git a/https.cpp b/https.cpp index 1e8bec1..f096863 100644 --- a/https.cpp +++ b/https.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -35,6 +36,7 @@ #include #include +namespace fs = std::filesystem; namespace beast = boost::beast; // from namespace http = beast::http; // from namespace net = boost::asio; // from @@ -468,7 +470,7 @@ private: /* Load a signed certificate into the ssl context, and configure the context for use with a server. */ -void load_server_certificate(boost::asio::ssl::context& ctx, fs::path cert_path, fs::path key_path) +void load_server_certificate(boost::asio::ssl::context& ctx, const fs::path& cert_path, const fs::path& key_path) { /* The certificate was generated from CMD.EXE on Windows 10 using: 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(); } diff --git a/webserver.cpp b/webserver.cpp index 4591fc9..bfa3b8c 100644 --- a/webserver.cpp +++ b/webserver.cpp @@ -3,6 +3,7 @@ #include "server.h" #include "plugin.h" +#include #include #include #include @@ -29,7 +30,7 @@ int webserver(int argc, char* argv[]) try { //initlocale(); // TODO: breaks plugins - std::string config_filename; + std::filesystem::path config_filename; if (!(argc == 1 || argc == 3)) { usage(); -- cgit v1.2.3