1b2896149SEd Tanous #include "file_test_utilities.hpp" 2b2896149SEd Tanous #include "http_body.hpp" 3b2896149SEd Tanous 4f0b59af4SEd Tanous #include <boost/beast/core/file_base.hpp> 5b2896149SEd Tanous #include <boost/system/error_code.hpp> 6b2896149SEd Tanous 7b2896149SEd Tanous #include <array> 8f0b59af4SEd Tanous #include <cstddef> 9f0b59af4SEd Tanous #include <cstdio> 10b2896149SEd Tanous #include <span> 11b2896149SEd Tanous #include <string> 12f0b59af4SEd Tanous #include <utility> 13b2896149SEd Tanous 14b2896149SEd Tanous #include <gmock/gmock.h> 15b2896149SEd Tanous #include <gtest/gtest.h> 16b2896149SEd Tanous 17b2896149SEd Tanous using ::testing::ElementsAre; 18b2896149SEd Tanous 19b2896149SEd Tanous namespace bmcweb 20b2896149SEd Tanous { 21b2896149SEd Tanous namespace 22b2896149SEd Tanous { 23b2896149SEd Tanous 24b2896149SEd Tanous TEST(HttpHttpBodyValueType, MoveString) 25b2896149SEd Tanous { 26b2896149SEd Tanous HttpBody::value_type value("teststring"); 27b2896149SEd Tanous // Move constructor 28b2896149SEd Tanous HttpBody::value_type value2(std::move(value)); 29b2896149SEd Tanous EXPECT_EQ(value2.encodingType, EncodingType::Raw); 30b2896149SEd Tanous EXPECT_EQ(value2.str(), "teststring"); 31b2896149SEd Tanous EXPECT_EQ(value2.payloadSize(), 10); 32b2896149SEd Tanous } 33b2896149SEd Tanous 34b2896149SEd Tanous TEST(HttpHttpBodyValueType, MoveOperatorString) 35b2896149SEd Tanous { 36b2896149SEd Tanous HttpBody::value_type value; 37b2896149SEd Tanous value.str() = "teststring"; 38b2896149SEd Tanous // Move constructor 39b2896149SEd Tanous HttpBody::value_type value2 = std::move(value); 40b2896149SEd Tanous EXPECT_EQ(value2.encodingType, EncodingType::Raw); 41b2896149SEd Tanous EXPECT_EQ(value2.str(), "teststring"); 42b2896149SEd Tanous EXPECT_EQ(value2.payloadSize(), 10); 43b2896149SEd Tanous } 44b2896149SEd Tanous 45b2896149SEd Tanous TEST(HttpHttpBodyValueType, copysignl) 46b2896149SEd Tanous { 47b2896149SEd Tanous HttpBody::value_type value; 48b2896149SEd Tanous value.str() = "teststring"; 49b2896149SEd Tanous // Move constructor 50b2896149SEd Tanous HttpBody::value_type value2(value); 51b2896149SEd Tanous EXPECT_EQ(value2.encodingType, EncodingType::Raw); 52b2896149SEd Tanous EXPECT_EQ(value2.str(), "teststring"); 53b2896149SEd Tanous EXPECT_EQ(value2.payloadSize(), 10); 54b2896149SEd Tanous } 55b2896149SEd Tanous 56b2896149SEd Tanous TEST(HttpHttpBodyValueType, CopyOperatorString) 57b2896149SEd Tanous { 58b2896149SEd Tanous HttpBody::value_type value; 59b2896149SEd Tanous value.str() = "teststring"; 60b2896149SEd Tanous // Move constructor 61b2896149SEd Tanous HttpBody::value_type value2 = value; 62b2896149SEd Tanous EXPECT_EQ(value2.encodingType, EncodingType::Raw); 63b2896149SEd Tanous EXPECT_EQ(value2.str(), "teststring"); 64b2896149SEd Tanous EXPECT_EQ(value2.payloadSize(), 10); 65b2896149SEd Tanous } 66b2896149SEd Tanous 67b2896149SEd Tanous TEST(HttpHttpBodyValueType, MoveFile) 68b2896149SEd Tanous { 69b2896149SEd Tanous HttpBody::value_type value(EncodingType::Base64); 705575efb6SEd Tanous TemporaryFileHandle temporaryFile("teststring"); 71b2896149SEd Tanous boost::system::error_code ec; 725575efb6SEd Tanous value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read, 735575efb6SEd Tanous ec); 74b2896149SEd Tanous ASSERT_FALSE(ec); 75b2896149SEd Tanous // Move constructor 76b2896149SEd Tanous HttpBody::value_type value2(std::move(value)); 77b2896149SEd Tanous std::array<char, 11> buffer{}; 78b2896149SEd Tanous size_t out = value2.file().read(buffer.data(), buffer.size(), ec); 79b2896149SEd Tanous ASSERT_FALSE(ec); 80b2896149SEd Tanous EXPECT_EQ(value2.encodingType, EncodingType::Base64); 81b2896149SEd Tanous 82b2896149SEd Tanous EXPECT_THAT(std::span(buffer.data(), out), 83b2896149SEd Tanous ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g')); 84b2896149SEd Tanous 85b2896149SEd Tanous EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 86b2896149SEd Tanous 'g', '\0')); 87b2896149SEd Tanous 88b2896149SEd Tanous EXPECT_EQ(value2.payloadSize(), 16); 89b2896149SEd Tanous } 90b2896149SEd Tanous 91b2896149SEd Tanous TEST(HttpHttpBodyValueType, MoveOperatorFile) 92b2896149SEd Tanous { 93b2896149SEd Tanous HttpBody::value_type value(EncodingType::Base64); 945575efb6SEd Tanous TemporaryFileHandle temporaryFile("teststring"); 95b2896149SEd Tanous boost::system::error_code ec; 965575efb6SEd Tanous value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read, 975575efb6SEd Tanous ec); 98b2896149SEd Tanous ASSERT_FALSE(ec); 99b2896149SEd Tanous // Move constructor 100b2896149SEd Tanous HttpBody::value_type value2 = std::move(value); 101b2896149SEd Tanous std::array<char, 11> buffer{}; 102b2896149SEd Tanous size_t out = value2.file().read(buffer.data(), buffer.size(), ec); 103b2896149SEd Tanous ASSERT_FALSE(ec); 104b2896149SEd Tanous EXPECT_EQ(value2.encodingType, EncodingType::Base64); 105b2896149SEd Tanous 106b2896149SEd Tanous EXPECT_THAT(std::span(buffer.data(), out), 107b2896149SEd Tanous ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g')); 108b2896149SEd Tanous EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 109b2896149SEd Tanous 'g', '\0')); 110b2896149SEd Tanous 111b2896149SEd Tanous EXPECT_EQ(value2.payloadSize(), 16); 112b2896149SEd Tanous } 113b2896149SEd Tanous 1145575efb6SEd Tanous TEST(HttpFileBodyValueType, SetFd) 115b2896149SEd Tanous { 116b2896149SEd Tanous HttpBody::value_type value(EncodingType::Base64); 1175575efb6SEd Tanous TemporaryFileHandle temporaryFile("teststring"); 118b2896149SEd Tanous boost::system::error_code ec; 119*daadfb2eSEd Tanous FILE* r = fopen(temporaryFile.stringPath.c_str(), "r"); 120*daadfb2eSEd Tanous ASSERT_NE(r, nullptr); 121*daadfb2eSEd Tanous value.setFd(fileno(r), ec); 122b2896149SEd Tanous ASSERT_FALSE(ec); 123b2896149SEd Tanous 124b2896149SEd Tanous std::array<char, 4096> buffer{}; 125b2896149SEd Tanous 126b2896149SEd Tanous size_t out = value.file().read(buffer.data(), buffer.size(), ec); 127b2896149SEd Tanous ASSERT_FALSE(ec); 128b2896149SEd Tanous 129b2896149SEd Tanous EXPECT_THAT(std::span(buffer.data(), out), 130b2896149SEd Tanous ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g')); 131b2896149SEd Tanous EXPECT_EQ(value.payloadSize(), 16); 132b2896149SEd Tanous } 133b2896149SEd Tanous 134b2896149SEd Tanous } // namespace 135b2896149SEd Tanous } // namespace bmcweb 136