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