From 7973293c311e27ff08a1488c9759c1b5b0fda30e Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 5 Jan 2023 13:55:20 +0100 Subject: Added tests --- debian/changelog | 1 + mime.cpp | 55 +++++++++++++++++++++------------------------ stringhelper.cpp | 2 +- stringhelper.h | 2 +- tempfile.cpp | 3 ++- tempfile.h | 2 +- tests/test-base64.cpp | 25 ++++++++++++++++++++- tests/test-file.cpp | 45 +++++++++++++++++++++++++++++++++++++ tests/test-mime.cpp | 31 ++++++++++++++++++++++++- tests/test-os.cpp | 16 +++++++++---- tests/test-stringhelper.cpp | 45 ++++++++++++++++++++++++++++++++++++- tests/test-tempfile.cpp | 15 ++++++++++++- tests/test-url.cpp | 10 ++++++++- 13 files changed, 210 insertions(+), 42 deletions(-) diff --git a/debian/changelog b/debian/changelog index 9d20da5..1752e59 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,7 @@ libreichwein (1.1) unstable; urgency=medium * Shared and static lib + * Added tests -- Roland Reichwein Tue, 03 Jan 2023 17:21:51 +0100 diff --git a/mime.cpp b/mime.cpp index cb3e323..b28cb9d 100644 --- a/mime.cpp +++ b/mime.cpp @@ -8,34 +8,31 @@ namespace beast = boost::beast; // Return a reasonable mime type based on the extension of a file. std::string Reichwein::Mime::mime_type(const std::string& path) { - auto const ext = [&path] - { - auto const pos = path.rfind("."); - if (pos == std::string::npos) - return std::string{}; - return path.substr(pos); - }(); - if(boost::algorithm::istarts_with(ext, ".htm")) return "text/html"; - if(boost::algorithm::istarts_with(ext, ".html")) return "text/html"; - if(boost::algorithm::istarts_with(ext, ".php")) return "text/html"; - if(boost::algorithm::istarts_with(ext, ".css")) return "text/css"; - if(boost::algorithm::istarts_with(ext, ".txt")) return "text/plain"; - if(boost::algorithm::istarts_with(ext, ".js")) return "application/javascript"; - if(boost::algorithm::istarts_with(ext, ".json")) return "application/json"; - if(boost::algorithm::istarts_with(ext, ".xml")) return "application/xml"; - if(boost::algorithm::istarts_with(ext, ".swf")) return "application/x-shockwave-flash"; - if(boost::algorithm::istarts_with(ext, ".flv")) return "video/x-flv"; - if(boost::algorithm::istarts_with(ext, ".png")) return "image/png"; - if(boost::algorithm::istarts_with(ext, ".jpe")) return "image/jpeg"; - if(boost::algorithm::istarts_with(ext, ".jpeg")) return "image/jpeg"; - if(boost::algorithm::istarts_with(ext, ".jpg")) return "image/jpeg"; - if(boost::algorithm::istarts_with(ext, ".gif")) return "image/gif"; - if(boost::algorithm::istarts_with(ext, ".bmp")) return "image/bmp"; - if(boost::algorithm::istarts_with(ext, ".ico")) return "image/vnd.microsoft.icon"; - if(boost::algorithm::istarts_with(ext, ".tiff")) return "image/tiff"; - if(boost::algorithm::istarts_with(ext, ".tif")) return "image/tiff"; - if(boost::algorithm::istarts_with(ext, ".svg")) return "image/svg+xml"; - if(boost::algorithm::istarts_with(ext, ".svgz")) return "image/svg+xml"; - return "application/text"; + auto const pos = path.rfind("."); + if (pos == std::string::npos) + return {}; + auto const ext = path.substr(pos); + if(boost::algorithm::iequals(ext, ".htm")) return "text/html"; + if(boost::algorithm::iequals(ext, ".html")) return "text/html"; + if(boost::algorithm::iequals(ext, ".php")) return "text/html"; + if(boost::algorithm::iequals(ext, ".css")) return "text/css"; + if(boost::algorithm::iequals(ext, ".txt")) return "text/plain"; + if(boost::algorithm::iequals(ext, ".js")) return "application/javascript"; + if(boost::algorithm::iequals(ext, ".json")) return "application/json"; + if(boost::algorithm::iequals(ext, ".xml")) return "application/xml"; + if(boost::algorithm::iequals(ext, ".swf")) return "application/x-shockwave-flash"; + if(boost::algorithm::iequals(ext, ".flv")) return "video/x-flv"; + if(boost::algorithm::iequals(ext, ".png")) return "image/png"; + if(boost::algorithm::iequals(ext, ".jpe")) return "image/jpeg"; + if(boost::algorithm::iequals(ext, ".jpeg")) return "image/jpeg"; + if(boost::algorithm::iequals(ext, ".jpg")) return "image/jpeg"; + if(boost::algorithm::iequals(ext, ".gif")) return "image/gif"; + if(boost::algorithm::iequals(ext, ".bmp")) return "image/bmp"; + if(boost::algorithm::iequals(ext, ".ico")) return "image/vnd.microsoft.icon"; + if(boost::algorithm::iequals(ext, ".tiff")) return "image/tiff"; + if(boost::algorithm::iequals(ext, ".tif")) return "image/tiff"; + if(boost::algorithm::iequals(ext, ".svg")) return "image/svg+xml"; + if(boost::algorithm::iequals(ext, ".svgz")) return "image/svg+xml"; + return "application/text"; } diff --git a/stringhelper.cpp b/stringhelper.cpp index af37499..8ce2068 100644 --- a/stringhelper.cpp +++ b/stringhelper.cpp @@ -56,7 +56,7 @@ std::string Reichwein::Stringhelper::join(std::vector vs, std::stri return s; } -bool Reichwein::Stringhelper::startsWithAnyOfLower(const std::string &s, const std::vector &list) { +bool Reichwein::Stringhelper::istartsWithAnyOf(const std::string &s, const std::vector &list) { for (const std::string& element : list) { if (boost::algorithm::starts_with(boost::algorithm::to_lower_copy(s), boost::algorithm::to_lower_copy(element))) return true; diff --git a/stringhelper.h b/stringhelper.h index 7b27a02..0ee6ab7 100644 --- a/stringhelper.h +++ b/stringhelper.h @@ -12,6 +12,6 @@ EXPORT std::string strfmt(const char* fmt, ...); EXPORT std::vector split(std::string value, const std::string separators = "\r\n "); EXPORT std::string join(std::vector vs, std::string separator = "\n"); -EXPORT bool startsWithAnyOfLower(const std::string &s, const std::vector &list); +EXPORT bool istartsWithAnyOf(const std::string &s, const std::vector &list); } // namespace diff --git a/tempfile.cpp b/tempfile.cpp index ca87d9b..4a02ad6 100644 --- a/tempfile.cpp +++ b/tempfile.cpp @@ -9,7 +9,7 @@ namespace fs = std::filesystem; using namespace std::string_literals; -fs::path Reichwein::Tempfile::GetPath() const +fs::path Reichwein::Tempfile::getPath() const { return m_path; } @@ -40,3 +40,4 @@ Reichwein::Tempfile::~Tempfile() std::cerr << "Warning: Couldn't remove temporary file " << m_path << std::endl; } } + diff --git a/tempfile.h b/tempfile.h index 7fe86ea..4f0bbc8 100644 --- a/tempfile.h +++ b/tempfile.h @@ -11,7 +11,7 @@ class EXPORT Tempfile std::filesystem::path m_path; public: - std::filesystem::path GetPath() const; + std::filesystem::path getPath() const; // extension: e.g. ".zip" Tempfile(const std::filesystem::path& extension = std::filesystem::path{}); diff --git a/tests/test-base64.cpp b/tests/test-base64.cpp index 03648d4..5b7644e 100644 --- a/tests/test-base64.cpp +++ b/tests/test-base64.cpp @@ -1,6 +1,6 @@ #include -#include "file.h" +#include "base64.h" class Base64Test: public ::testing::Test { @@ -21,3 +21,26 @@ protected: }; +TEST_F(Base64Test, encode) +{ + EXPECT_EQ(Reichwein::Base64::encode64(""), ""); + EXPECT_EQ(Reichwein::Base64::encode64("a"), "YQ=="); + EXPECT_EQ(Reichwein::Base64::encode64("ab"), "YWI="); + EXPECT_EQ(Reichwein::Base64::encode64("abc"), "YWJj"); + EXPECT_EQ(Reichwein::Base64::encode64("abcd"), "YWJjZA=="); +} + +TEST_F(Base64Test, decode) +{ + EXPECT_EQ(Reichwein::Base64::decode64(""), ""); + EXPECT_EQ(Reichwein::Base64::decode64("YQ=="), "a"); + EXPECT_EQ(Reichwein::Base64::decode64("YQ"), "a"); // unpadded is accepted here + EXPECT_EQ(Reichwein::Base64::decode64("YWI="), "ab"); + EXPECT_EQ(Reichwein::Base64::decode64("YWI"), "ab"); + EXPECT_EQ(Reichwein::Base64::decode64("YWJj"), "abc"); + EXPECT_EQ(Reichwein::Base64::decode64("YWJjZA=="), "abcd"); + EXPECT_EQ(Reichwein::Base64::decode64("YWJjZA="), "abcd"); + EXPECT_EQ(Reichwein::Base64::decode64("YWJjZA"), "abcd"); + EXPECT_ANY_THROW({Reichwein::Base64::decode64("_");}); + EXPECT_ANY_THROW({Reichwein::Base64::decode64("abc_");}); +} diff --git a/tests/test-file.cpp b/tests/test-file.cpp index 7c3b752..a175858 100644 --- a/tests/test-file.cpp +++ b/tests/test-file.cpp @@ -53,3 +53,48 @@ TEST_F(FileTest, getFile_proc) EXPECT_GT(s.size(), 0); } + +TEST_F(FileTest, getFile_not_found) +{ + EXPECT_THROW({Reichwein::File::getFile(testFilename);}, std::runtime_error); +} + +TEST_F(FileTest, setFile) +{ + Reichwein::File::setFile(testFilename, std::string("abc")); + + std::ifstream f(testFilename, std::ios::binary); + std::string s{" "}; + EXPECT_EQ(f.readsome(s.data(), s.size()), static_cast(3)); + EXPECT_EQ(s, "abc "); +} + +TEST_F(FileTest, setFile_ptr) +{ + std::string s{"abc"}; + Reichwein::File::setFile(testFilename, s.data(), s.size()); + + EXPECT_EQ(Reichwein::File::getFile(testFilename), "abc"); +} + +TEST_F(FileTest, setFile_vector) +{ + std::vector v{1, 2, 3, 4}; + Reichwein::File::setFile(testFilename, v); + + EXPECT_EQ(Reichwein::File::getFile(testFilename), "\x01\x02\x03\x04"); +} + +TEST_F(FileTest, setFile_overwrite) +{ + Reichwein::File::setFile(testFilename, std::string("abc")); + Reichwein::File::setFile(testFilename, std::string("def")); + + EXPECT_EQ(Reichwein::File::getFile(testFilename), "def"); +} + +TEST_F(FileTest, setFile_nonexisting_dir) +{ + EXPECT_THROW({Reichwein::File::setFile(testFilename/testFilename, "abc");}, std::runtime_error); +} + diff --git a/tests/test-mime.cpp b/tests/test-mime.cpp index 2d28a36..f3ca8ee 100644 --- a/tests/test-mime.cpp +++ b/tests/test-mime.cpp @@ -1,6 +1,6 @@ #include -#include "file.h" +#include "mime.h" class MimeTest: public ::testing::Test { @@ -21,3 +21,32 @@ protected: }; +TEST_F(MimeTest, mime_type) +{ + EXPECT_EQ(Reichwein::Mime::mime_type(""), ""); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.html"), "text/html"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.htm"), "text/html"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.html"), "text/html"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.php"), "text/html"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.css"), "text/css"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.txt"), "text/plain"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.js"), "application/javascript"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.json"), "application/json"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.xml"), "application/xml"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.swf"), "application/x-shockwave-flash"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.flv"), "video/x-flv"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.png"), "image/png"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.jpe"), "image/jpeg"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.jpeg"), "image/jpeg"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.jpg"), "image/jpeg"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.gif"), "image/gif"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.bmp"), "image/bmp"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.ico"), "image/vnd.microsoft.icon"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.tiff"), "image/tiff"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.tif"), "image/tiff"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.svg"), "image/svg+xml"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.svgz"), "image/svg+xml"); + EXPECT_EQ(Reichwein::Mime::mime_type("a/bcd.cpp"), "application/text"); + EXPECT_EQ(Reichwein::Mime::mime_type("bla.HTML"), "text/html"); +} + diff --git a/tests/test-os.cpp b/tests/test-os.cpp index 6177bfa..06eebc8 100644 --- a/tests/test-os.cpp +++ b/tests/test-os.cpp @@ -1,14 +1,14 @@ #include -#include "file.h" +#include "os.h" -class OsTest: public ::testing::Test +class OSTest: public ::testing::Test { protected: - OsTest(){ + OSTest(){ } - ~OsTest() override{ + ~OSTest() override{ } void SetUp() override @@ -20,3 +20,11 @@ protected: } }; + +TEST_F(OSTest,uptime_host) { + EXPECT_GT(Reichwein::OS::uptime_host().size(), 0); +} + +TEST_F(OSTest,uptime_process) { + EXPECT_GT(Reichwein::OS::uptime_process().size(), 0); +} diff --git a/tests/test-stringhelper.cpp b/tests/test-stringhelper.cpp index 3ece784..7f326af 100644 --- a/tests/test-stringhelper.cpp +++ b/tests/test-stringhelper.cpp @@ -1,6 +1,9 @@ #include -#include "file.h" +#include "stringhelper.h" + +#include +#include class StringhelperTest: public ::testing::Test { @@ -21,3 +24,43 @@ protected: }; +TEST_F(StringhelperTest, strfmt) +{ + EXPECT_EQ(Reichwein::Stringhelper::strfmt(""), ""); + EXPECT_EQ(Reichwein::Stringhelper::strfmt("abc"), "abc"); + EXPECT_EQ(Reichwein::Stringhelper::strfmt("abc%ddef", 123), "abc123def"); + EXPECT_EQ(Reichwein::Stringhelper::strfmt("abc%sdef", "XYZ"), "abcXYZdef"); +} + +TEST_F(StringhelperTest, split) +{ + EXPECT_EQ(Reichwein::Stringhelper::split(""), std::vector{}); + EXPECT_EQ(Reichwein::Stringhelper::split("abc"), std::vector{"abc"}); + std::vector v{"abc", "def", "ghi"}; + EXPECT_EQ(Reichwein::Stringhelper::split("abc def ghi"), v); + EXPECT_EQ(Reichwein::Stringhelper::split("abcxdefxghi", "x"), v); +} + +TEST_F(StringhelperTest, join) +{ + EXPECT_EQ(Reichwein::Stringhelper::join({}), ""); + EXPECT_EQ(Reichwein::Stringhelper::join({"abc"}), "abc"); + EXPECT_EQ(Reichwein::Stringhelper::join({"abc", "def"}), "abc\ndef"); + EXPECT_EQ(Reichwein::Stringhelper::join({"abc", "def"}, " "), "abc def"); + EXPECT_EQ(Reichwein::Stringhelper::join({"abc", "def", "ghi"}, "12"), "abc12def12ghi"); +} + +TEST_F(StringhelperTest, startsWithAnyOfLower) +{ + EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("", {""})); + EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("a", {""})); + EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("abc", {"a"})); + EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("abc", {"A"})); + EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("Abc", {"a"})); + EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("Abc", {"A"})); + EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("Abc", {"AB"})); + EXPECT_TRUE(Reichwein::Stringhelper::istartsWithAnyOf("Abc", {"B", "aB"})); + + EXPECT_FALSE(Reichwein::Stringhelper::istartsWithAnyOf("", {"a"})); + EXPECT_FALSE(Reichwein::Stringhelper::istartsWithAnyOf("Abc", {"B"})); +} diff --git a/tests/test-tempfile.cpp b/tests/test-tempfile.cpp index b9bef05..782c3de 100644 --- a/tests/test-tempfile.cpp +++ b/tests/test-tempfile.cpp @@ -1,6 +1,6 @@ #include -#include "file.h" +#include "tempfile.h" class TempfileTest: public ::testing::Test { @@ -21,3 +21,16 @@ protected: }; +TEST_F(TempfileTest, getPath) +{ + Reichwein::Tempfile t0; + Reichwein::Tempfile t1{".cpp"}; + Reichwein::Tempfile t2{".cpp"}; + + EXPECT_NE(t0.getPath(), t1.getPath()); + EXPECT_NE(t1.getPath(), t2.getPath()); + + EXPECT_GT(t0.getPath().generic_u8string().size(), 0); + EXPECT_GT(t1.getPath().generic_u8string().size(), 4); + EXPECT_GT(t2.getPath().generic_u8string().size(), 4); +} diff --git a/tests/test-url.cpp b/tests/test-url.cpp index 2f5075a..2e56d2a 100644 --- a/tests/test-url.cpp +++ b/tests/test-url.cpp @@ -1,6 +1,6 @@ #include -#include "file.h" +#include "url.h" class URLTest: public ::testing::Test { @@ -20,3 +20,11 @@ protected: } }; + +TEST_F(URLTest, urlDecode) +{ + EXPECT_EQ(Reichwein::URL::urlDecode(""), ""); + EXPECT_EQ(Reichwein::URL::urlDecode("abc"), "abc"); + EXPECT_EQ(Reichwein::URL::urlDecode("a+b+c"), "a b c"); + EXPECT_EQ(Reichwein::URL::urlDecode("a+b%41c"), "a bAc"); +} -- cgit v1.2.3