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