1 #include "file_test_utilities.hpp"
2 #include "http_body.hpp"
3
4 #include <boost/beast/core/file_base.hpp>
5 #include <boost/system/error_code.hpp>
6
7 #include <array>
8 #include <cstddef>
9 #include <cstdio>
10 #include <span>
11 #include <string>
12 #include <utility>
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
TEST(HttpHttpBodyValueType,MoveString)24 TEST(HttpHttpBodyValueType, MoveString)
25 {
26 HttpBody::value_type value("teststring");
27 // Move constructor
28 HttpBody::value_type value2(std::move(value));
29 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
30 EXPECT_EQ(value2.str(), "teststring");
31 EXPECT_EQ(value2.payloadSize(), 10);
32 }
33
TEST(HttpHttpBodyValueType,MoveOperatorString)34 TEST(HttpHttpBodyValueType, MoveOperatorString)
35 {
36 HttpBody::value_type value;
37 value.str() = "teststring";
38 // Move constructor
39 HttpBody::value_type value2 = std::move(value);
40 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
41 EXPECT_EQ(value2.str(), "teststring");
42 EXPECT_EQ(value2.payloadSize(), 10);
43 }
44
TEST(HttpHttpBodyValueType,copysignl)45 TEST(HttpHttpBodyValueType, copysignl)
46 {
47 HttpBody::value_type value;
48 value.str() = "teststring";
49 // Move constructor
50 HttpBody::value_type value2(value);
51 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
52 EXPECT_EQ(value2.str(), "teststring");
53 EXPECT_EQ(value2.payloadSize(), 10);
54 }
55
TEST(HttpHttpBodyValueType,CopyOperatorString)56 TEST(HttpHttpBodyValueType, CopyOperatorString)
57 {
58 HttpBody::value_type value;
59 value.str() = "teststring";
60 // Move constructor
61 HttpBody::value_type value2 = value;
62 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
63 EXPECT_EQ(value2.str(), "teststring");
64 EXPECT_EQ(value2.payloadSize(), 10);
65 }
66
TEST(HttpHttpBodyValueType,MoveFile)67 TEST(HttpHttpBodyValueType, MoveFile)
68 {
69 HttpBody::value_type value(EncodingType::Base64);
70 TemporaryFileHandle temporaryFile("teststring");
71 boost::system::error_code ec;
72 value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
73 ec);
74 ASSERT_FALSE(ec);
75 // Move constructor
76 HttpBody::value_type value2(std::move(value));
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
TEST(HttpHttpBodyValueType,MoveOperatorFile)91 TEST(HttpHttpBodyValueType, MoveOperatorFile)
92 {
93 HttpBody::value_type value(EncodingType::Base64);
94 TemporaryFileHandle temporaryFile("teststring");
95 boost::system::error_code ec;
96 value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
97 ec);
98 ASSERT_FALSE(ec);
99 // Move constructor
100 HttpBody::value_type value2 = std::move(value);
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
TEST(HttpFileBodyValueType,SetFd)114 TEST(HttpFileBodyValueType, SetFd)
115 {
116 HttpBody::value_type value(EncodingType::Base64);
117 TemporaryFileHandle temporaryFile("teststring");
118 boost::system::error_code ec;
119 value.setFd(fileno(fopen(temporaryFile.stringPath.c_str(), "r")), ec);
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
134