blob: d8c9dd19191dc9bc22f52aa5617d667426683169 [file] [log] [blame]
Ed Tanous7a6f0032024-01-28 09:49:03 -08001#include "file_test_utilities.hpp"
Ed Tanousb2896142024-01-31 15:25:47 -08002#include "http_body.hpp"
Ed Tanous7a6f0032024-01-28 09:49:03 -08003
Ed Tanousf0b59af2024-03-20 13:38:04 -07004#include <boost/beast/core/file_base.hpp>
Ed Tanous7a6f0032024-01-28 09:49:03 -08005#include <boost/system/error_code.hpp>
6
7#include <array>
Ed Tanousf0b59af2024-03-20 13:38:04 -07008#include <cstddef>
9#include <cstdio>
Ed Tanous7a6f0032024-01-28 09:49:03 -080010#include <span>
11#include <string>
Ed Tanousf0b59af2024-03-20 13:38:04 -070012#include <utility>
Ed Tanous7a6f0032024-01-28 09:49:03 -080013
14#include <gmock/gmock.h>
15#include <gtest/gtest.h>
16
17using ::testing::ElementsAre;
18
19namespace bmcweb
20{
21namespace
22{
23
Ed Tanousb2896142024-01-31 15:25:47 -080024TEST(HttpHttpBodyValueType, MoveString)
Ed Tanous7a6f0032024-01-28 09:49:03 -080025{
Ed Tanousb2896142024-01-31 15:25:47 -080026 HttpBody::value_type value("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -080027 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080028 HttpBody::value_type value2(std::move(value));
Ed Tanous7a6f0032024-01-28 09:49:03 -080029 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
30 EXPECT_EQ(value2.str(), "teststring");
31 EXPECT_EQ(value2.payloadSize(), 10);
32}
33
Ed Tanousb2896142024-01-31 15:25:47 -080034TEST(HttpHttpBodyValueType, MoveOperatorString)
Ed Tanous7a6f0032024-01-28 09:49:03 -080035{
Ed Tanousb2896142024-01-31 15:25:47 -080036 HttpBody::value_type value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080037 value.str() = "teststring";
38 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080039 HttpBody::value_type value2 = std::move(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -080040 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
41 EXPECT_EQ(value2.str(), "teststring");
42 EXPECT_EQ(value2.payloadSize(), 10);
43}
44
Ed Tanousb2896142024-01-31 15:25:47 -080045TEST(HttpHttpBodyValueType, copysignl)
Ed Tanous7a6f0032024-01-28 09:49:03 -080046{
Ed Tanousb2896142024-01-31 15:25:47 -080047 HttpBody::value_type value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080048 value.str() = "teststring";
49 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080050 HttpBody::value_type value2(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -080051 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
52 EXPECT_EQ(value2.str(), "teststring");
53 EXPECT_EQ(value2.payloadSize(), 10);
54}
55
Ed Tanousb2896142024-01-31 15:25:47 -080056TEST(HttpHttpBodyValueType, CopyOperatorString)
Ed Tanous7a6f0032024-01-28 09:49:03 -080057{
Ed Tanousb2896142024-01-31 15:25:47 -080058 HttpBody::value_type value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080059 value.str() = "teststring";
60 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080061 HttpBody::value_type value2 = value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080062 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
63 EXPECT_EQ(value2.str(), "teststring");
64 EXPECT_EQ(value2.payloadSize(), 10);
65}
66
Ed Tanousb2896142024-01-31 15:25:47 -080067TEST(HttpHttpBodyValueType, MoveFile)
Ed Tanous7a6f0032024-01-28 09:49:03 -080068{
Ed Tanousb2896142024-01-31 15:25:47 -080069 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous5575efb2024-01-30 09:25:03 -080070 TemporaryFileHandle temporaryFile("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -080071 boost::system::error_code ec;
Ed Tanous5575efb2024-01-30 09:25:03 -080072 value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
73 ec);
Ed Tanous7a6f0032024-01-28 09:49:03 -080074 ASSERT_FALSE(ec);
75 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080076 HttpBody::value_type value2(std::move(value));
Ed Tanous7a6f0032024-01-28 09:49:03 -080077 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 Tanousb2896142024-01-31 15:25:47 -080091TEST(HttpHttpBodyValueType, MoveOperatorFile)
Ed Tanous7a6f0032024-01-28 09:49:03 -080092{
Ed Tanousb2896142024-01-31 15:25:47 -080093 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous5575efb2024-01-30 09:25:03 -080094 TemporaryFileHandle temporaryFile("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -080095 boost::system::error_code ec;
Ed Tanous5575efb2024-01-30 09:25:03 -080096 value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
97 ec);
Ed Tanous7a6f0032024-01-28 09:49:03 -080098 ASSERT_FALSE(ec);
99 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -0800100 HttpBody::value_type value2 = std::move(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800101 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 Tanous5575efb2024-01-30 09:25:03 -0800114TEST(HttpFileBodyValueType, SetFd)
Ed Tanous7a6f0032024-01-28 09:49:03 -0800115{
Ed Tanousb2896142024-01-31 15:25:47 -0800116 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous5575efb2024-01-30 09:25:03 -0800117 TemporaryFileHandle temporaryFile("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -0800118 boost::system::error_code ec;
Ed Tanous5575efb2024-01-30 09:25:03 -0800119 value.setFd(fileno(fopen(temporaryFile.stringPath.c_str(), "r")), ec);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800120 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