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