blob: 0983c1d8158fdc053c12e14dda76c21dc6691e63 [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 Tanous7a6f0032024-01-28 09:49:03 -080070 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
Ed Tanousb2896142024-01-31 15:25:47 -080075 HttpBody::value_type value2(std::move(value));
Ed Tanous7a6f0032024-01-28 09:49:03 -080076 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
Ed Tanousb2896142024-01-31 15:25:47 -080090TEST(HttpHttpBodyValueType, MoveOperatorFile)
Ed Tanous7a6f0032024-01-28 09:49:03 -080091{
Ed Tanousb2896142024-01-31 15:25:47 -080092 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous7a6f0032024-01-28 09:49:03 -080093 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
Ed Tanousb2896142024-01-31 15:25:47 -080098 HttpBody::value_type value2 = std::move(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -080099 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
Ed Tanousb2896142024-01-31 15:25:47 -0800112TEST(HttpBodyValueType, SetFd)
Ed Tanous7a6f0032024-01-28 09:49:03 -0800113{
Ed Tanousb2896142024-01-31 15:25:47 -0800114 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800115 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