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