diff options
author | Ed Tanous <ed@tanous.net> | 2024-01-28 20:49:03 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2024-02-16 20:34:04 +0300 |
commit | 7a6f003180968fc578af8eee88b453a906efeed7 (patch) | |
tree | f1e871c02729317bdfb50896b4b253134f6db5cb | |
parent | 52e31629978bbcefc0e14c96272ef77c2dad6a9c (diff) | |
download | bmcweb-7a6f003180968fc578af8eee88b453a906efeed7.tar.xz |
Add unit tests for file body
File body needs some unit tests for managing the move constructors.
Tested: Unit tests pass.
Change-Id: Ia640aec75a6f3f85640a50f5dd492638871f9eca
Signed-off-by: Ed Tanous <ed@tanous.net>
-rw-r--r-- | meson.build | 3 | ||||
-rw-r--r-- | test/http/file_test_utilities.hpp | 19 | ||||
-rw-r--r-- | test/http/http_file_body_test.cpp | 128 | ||||
-rw-r--r-- | test/http/http_response_test.cpp | 42 |
4 files changed, 168 insertions, 24 deletions
diff --git a/meson.build b/meson.build index d47bdb7e2d..f260f86c28 100644 --- a/meson.build +++ b/meson.build @@ -357,7 +357,7 @@ else endif if get_option('tests').allowed() - gtest = dependency('gtest', main: true, disabler: true, required : false) + gtest = dependency('gtest', main: true, version: '>=1.14.0', disabler: true, required : false) gmock = dependency('gmock', required : false) if not gtest.found() and get_option('tests').allowed() gtest_proj = subproject('gtest', required: true) @@ -429,6 +429,7 @@ srcfiles_unittest = files( 'test/http/utility_test.cpp', 'test/http/verb_test.cpp', 'test/http/mutual_tls_meta.cpp', + 'test/http/http_file_body_test.cpp', 'test/include/dbus_utility_test.cpp', 'test/include/google/google_service_root_test.cpp', 'test/include/json_html_serializer.cpp', diff --git a/test/http/file_test_utilities.hpp b/test/http/file_test_utilities.hpp new file mode 100644 index 0000000000..073a0747a4 --- /dev/null +++ b/test/http/file_test_utilities.hpp @@ -0,0 +1,19 @@ +#pragma once +#include <filesystem> +#include <string> +#include <string_view> + +#include <gtest/gtest.h> + +inline std::string makeFile(std::string_view sampleData) +{ + std::filesystem::path path = std::filesystem::temp_directory_path(); + path /= "bmcweb_http_response_test_XXXXXXXXXXX"; + std::string stringPath = path.string(); + int fd = mkstemp(stringPath.data()); + EXPECT_GT(fd, 0); + EXPECT_EQ(write(fd, sampleData.data(), sampleData.size()), + sampleData.size()); + close(fd); + return stringPath; +} diff --git a/test/http/http_file_body_test.cpp b/test/http/http_file_body_test.cpp new file mode 100644 index 0000000000..4eaa93bac2 --- /dev/null +++ b/test/http/http_file_body_test.cpp @@ -0,0 +1,128 @@ +#include "file_test_utilities.hpp" +#include "http_file_body.hpp" + +#include <boost/system/error_code.hpp> + +#include <array> +#include <span> +#include <string> + +#include <gmock/gmock.h> +#include <gtest/gtest.h> + +using ::testing::ElementsAre; + +namespace bmcweb +{ +namespace +{ + +TEST(HttpFileBodyValueType, MoveString) +{ + FileBody::value_type value("teststring"); + // Move constructor + FileBody::value_type value2(std::move(value)); + EXPECT_EQ(value2.encodingType, EncodingType::Raw); + EXPECT_EQ(value2.str(), "teststring"); + EXPECT_EQ(value2.payloadSize(), 10); +} + +TEST(HttpFileBodyValueType, MoveOperatorString) +{ + FileBody::value_type value; + value.str() = "teststring"; + // Move constructor + FileBody::value_type value2 = std::move(value); + EXPECT_EQ(value2.encodingType, EncodingType::Raw); + EXPECT_EQ(value2.str(), "teststring"); + EXPECT_EQ(value2.payloadSize(), 10); +} + +TEST(HttpFileBodyValueType, copysignl) +{ + FileBody::value_type value; + value.str() = "teststring"; + // Move constructor + FileBody::value_type value2(value); + EXPECT_EQ(value2.encodingType, EncodingType::Raw); + EXPECT_EQ(value2.str(), "teststring"); + EXPECT_EQ(value2.payloadSize(), 10); +} + +TEST(HttpFileBodyValueType, CopyOperatorString) +{ + FileBody::value_type value; + value.str() = "teststring"; + // Move constructor + FileBody::value_type value2 = value; + EXPECT_EQ(value2.encodingType, EncodingType::Raw); + EXPECT_EQ(value2.str(), "teststring"); + EXPECT_EQ(value2.payloadSize(), 10); +} + +TEST(HttpFileBodyValueType, MoveFile) +{ + FileBody::value_type value(EncodingType::Base64); + std::string filepath = makeFile("teststring"); + boost::system::error_code ec; + value.open(filepath.c_str(), boost::beast::file_mode::read, ec); + ASSERT_FALSE(ec); + // Move constructor + FileBody::value_type value2(std::move(value)); + std::array<char, 11> buffer{}; + size_t out = value2.file().read(buffer.data(), buffer.size(), ec); + ASSERT_FALSE(ec); + EXPECT_EQ(value2.encodingType, EncodingType::Base64); + + EXPECT_THAT(std::span(buffer.data(), out), + ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g')); + + EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', + 'g', '\0')); + + EXPECT_EQ(value2.payloadSize(), 16); +} + +TEST(HttpFileBodyValueType, MoveOperatorFile) +{ + FileBody::value_type value(EncodingType::Base64); + std::string filepath = makeFile("teststring"); + boost::system::error_code ec; + value.open(filepath.c_str(), boost::beast::file_mode::read, ec); + ASSERT_FALSE(ec); + // Move constructor + FileBody::value_type value2 = std::move(value); + std::array<char, 11> buffer{}; + size_t out = value2.file().read(buffer.data(), buffer.size(), ec); + ASSERT_FALSE(ec); + EXPECT_EQ(value2.encodingType, EncodingType::Base64); + + EXPECT_THAT(std::span(buffer.data(), out), + ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g')); + EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', + 'g', '\0')); + + EXPECT_EQ(value2.payloadSize(), 16); +} + +TEST(HttpFileBodyValueType, SetFd) +{ + FileBody::value_type value(EncodingType::Base64); + std::string filepath = makeFile("teststring"); + + boost::system::error_code ec; + value.setFd(fileno(fopen(filepath.c_str(), "r")), ec); + ASSERT_FALSE(ec); + + std::array<char, 4096> buffer{}; + + size_t out = value.file().read(buffer.data(), buffer.size(), ec); + ASSERT_FALSE(ec); + + EXPECT_THAT(std::span(buffer.data(), out), + ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g')); + EXPECT_EQ(value.payloadSize(), 16); +} + +} // namespace +} // namespace bmcweb diff --git a/test/http/http_response_test.cpp b/test/http/http_response_test.cpp index f61d6dea9d..c5bfbdca99 100644 --- a/test/http/http_response_test.cpp +++ b/test/http/http_response_test.cpp @@ -1,6 +1,7 @@ #include "boost/beast/core/buffers_to_string.hpp" #include "boost/beast/core/flat_buffer.hpp" #include "boost/beast/http/serializer.hpp" +#include "file_test_utilities.hpp" #include "http/http_response.hpp" #include <filesystem> @@ -23,19 +24,6 @@ void verifyHeaders(crow::Response& res) EXPECT_EQ(res.result(), boost::beast::http::status::ok); } -std::string makeFile(std::string_view sampleData) -{ - std::filesystem::path path = std::filesystem::temp_directory_path(); - path /= "bmcweb_http_response_test_XXXXXXXXXXX"; - std::string stringPath = path.string(); - int fd = mkstemp(stringPath.data()); - EXPECT_GT(fd, 0); - EXPECT_EQ(write(fd, sampleData.data(), sampleData.size()), - sampleData.size()); - close(fd); - return stringPath; -} - std::string getData(boost::beast::http::response<bmcweb::FileBody>& m) { std::string ret; @@ -139,6 +127,24 @@ void testFileData(crow::Response& res, const std::string& data) EXPECT_EQ(getData(fb), data); } +std::string generateBigdata() +{ + std::string result; + while (result.size() < 10000) + { + result += "sample text"; + } + return result; +} + +TEST(HttpResponse, StringBodyWriterLarge) +{ + crow::Response res; + std::string data = generateBigdata(); + res.write(std::string(data)); + testFileData(res, data); +} + TEST(HttpResponse, Base64FileBodyWriter) { crow::Response res; @@ -151,16 +157,6 @@ TEST(HttpResponse, Base64FileBodyWriter) std::filesystem::remove(path); } -std::string generateBigdata() -{ - std::string result; - while (result.size() < 10000) - { - result += "sample text"; - } - return result; -} - TEST(HttpResponse, Base64FileBodyWriterLarge) { crow::Response res; |