blob: 4eaa93bac24a8d7d141fbbe794562cace111e494 [file] [log] [blame]
Ed Tanous7a6f0032024-01-28 09:49:03 -08001#include "file_test_utilities.hpp"
2#include "http_file_body.hpp"
3
4#include <boost/system/error_code.hpp>
5
6#include <array>
7#include <span>
8#include <string>
9
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13using ::testing::ElementsAre;
14
15namespace bmcweb
16{
17namespace
18{
19
20TEST(HttpFileBodyValueType, MoveString)
21{
22 FileBody::value_type value("teststring");
23 // Move constructor
24 FileBody::value_type value2(std::move(value));
25 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
26 EXPECT_EQ(value2.str(), "teststring");
27 EXPECT_EQ(value2.payloadSize(), 10);
28}
29
30TEST(HttpFileBodyValueType, MoveOperatorString)
31{
32 FileBody::value_type value;
33 value.str() = "teststring";
34 // Move constructor
35 FileBody::value_type value2 = std::move(value);
36 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
37 EXPECT_EQ(value2.str(), "teststring");
38 EXPECT_EQ(value2.payloadSize(), 10);
39}
40
41TEST(HttpFileBodyValueType, copysignl)
42{
43 FileBody::value_type value;
44 value.str() = "teststring";
45 // Move constructor
46 FileBody::value_type value2(value);
47 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
48 EXPECT_EQ(value2.str(), "teststring");
49 EXPECT_EQ(value2.payloadSize(), 10);
50}
51
52TEST(HttpFileBodyValueType, CopyOperatorString)
53{
54 FileBody::value_type value;
55 value.str() = "teststring";
56 // Move constructor
57 FileBody::value_type value2 = value;
58 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
59 EXPECT_EQ(value2.str(), "teststring");
60 EXPECT_EQ(value2.payloadSize(), 10);
61}
62
63TEST(HttpFileBodyValueType, MoveFile)
64{
65 FileBody::value_type value(EncodingType::Base64);
66 std::string filepath = makeFile("teststring");
67 boost::system::error_code ec;
68 value.open(filepath.c_str(), boost::beast::file_mode::read, ec);
69 ASSERT_FALSE(ec);
70 // Move constructor
71 FileBody::value_type value2(std::move(value));
72 std::array<char, 11> buffer{};
73 size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
74 ASSERT_FALSE(ec);
75 EXPECT_EQ(value2.encodingType, EncodingType::Base64);
76
77 EXPECT_THAT(std::span(buffer.data(), out),
78 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
79
80 EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
81 'g', '\0'));
82
83 EXPECT_EQ(value2.payloadSize(), 16);
84}
85
86TEST(HttpFileBodyValueType, MoveOperatorFile)
87{
88 FileBody::value_type value(EncodingType::Base64);
89 std::string filepath = makeFile("teststring");
90 boost::system::error_code ec;
91 value.open(filepath.c_str(), boost::beast::file_mode::read, ec);
92 ASSERT_FALSE(ec);
93 // Move constructor
94 FileBody::value_type value2 = std::move(value);
95 std::array<char, 11> buffer{};
96 size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
97 ASSERT_FALSE(ec);
98 EXPECT_EQ(value2.encodingType, EncodingType::Base64);
99
100 EXPECT_THAT(std::span(buffer.data(), out),
101 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
102 EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
103 'g', '\0'));
104
105 EXPECT_EQ(value2.payloadSize(), 16);
106}
107
108TEST(HttpFileBodyValueType, SetFd)
109{
110 FileBody::value_type value(EncodingType::Base64);
111 std::string filepath = makeFile("teststring");
112
113 boost::system::error_code ec;
114 value.setFd(fileno(fopen(filepath.c_str(), "r")), ec);
115 ASSERT_FALSE(ec);
116
117 std::array<char, 4096> buffer{};
118
119 size_t out = value.file().read(buffer.data(), buffer.size(), ec);
120 ASSERT_FALSE(ec);
121
122 EXPECT_THAT(std::span(buffer.data(), out),
123 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
124 EXPECT_EQ(value.payloadSize(), 16);
125}
126
127} // namespace
128} // namespace bmcweb