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.compressionType, CompressionType::Raw); 66 EXPECT_EQ(value2.str(), "teststring"); 67 EXPECT_EQ(value2.payloadSize(), 10); 68 } 69 70 TEST(HttpHttpBodyValueType, MoveFile) 71 { 72 HttpBody::value_type value(EncodingType::Base64, CompressionType::Raw); 73 TemporaryFileHandle temporaryFile("teststring"); 74 boost::system::error_code ec; 75 value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read, 76 ec); 77 ASSERT_FALSE(ec); 78 // Move constructor 79 HttpBody::value_type value2(std::move(value)); 80 std::array<char, 11> buffer{}; 81 size_t out = value2.file().read(buffer.data(), buffer.size(), ec); 82 ASSERT_FALSE(ec); 83 EXPECT_EQ(value2.encodingType, EncodingType::Base64); 84 EXPECT_EQ(value2.compressionType, CompressionType::Raw); 85 86 EXPECT_THAT(std::span(buffer.data(), out), 87 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g')); 88 89 EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 90 'g', '\0')); 91 92 EXPECT_EQ(value2.payloadSize(), 16); 93 } 94 95 TEST(HttpHttpBodyValueType, MoveOperatorFile) 96 { 97 HttpBody::value_type value(EncodingType::Base64, CompressionType::Raw); 98 TemporaryFileHandle temporaryFile("teststring"); 99 boost::system::error_code ec; 100 value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read, 101 ec); 102 ASSERT_FALSE(ec); 103 // Move constructor 104 HttpBody::value_type value2 = std::move(value); 105 std::array<char, 11> buffer{}; 106 size_t out = value2.file().read(buffer.data(), buffer.size(), ec); 107 ASSERT_FALSE(ec); 108 EXPECT_EQ(value2.encodingType, EncodingType::Base64); 109 EXPECT_EQ(value2.compressionType, CompressionType::Raw); 110 111 EXPECT_THAT(std::span(buffer.data(), out), 112 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g')); 113 EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 114 'g', '\0')); 115 116 EXPECT_EQ(value2.payloadSize(), 16); 117 } 118 119 TEST(HttpFileBodyValueType, SetFd) 120 { 121 HttpBody::value_type value(EncodingType::Base64, CompressionType::Raw); 122 TemporaryFileHandle temporaryFile("teststring"); 123 boost::system::error_code ec; 124 FILE* r = fopen(temporaryFile.stringPath.c_str(), "r"); 125 ASSERT_NE(r, nullptr); 126 value.setFd(fileno(r), ec); 127 ASSERT_FALSE(ec); 128 129 std::array<char, 4096> buffer{}; 130 131 size_t out = value.file().read(buffer.data(), buffer.size(), ec); 132 ASSERT_FALSE(ec); 133 134 EXPECT_THAT(std::span(buffer.data(), out), 135 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g')); 136 EXPECT_EQ(value.payloadSize(), 16); 137 } 138 139 } // namespace 140 } // namespace bmcweb 141