blob: 75c81fac57ac55bbfeb747cbfb3b32cc8fac63db [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous7a6f0032024-01-28 09:49:03 -08003#include "file_test_utilities.hpp"
Ed Tanousb2896142024-01-31 15:25:47 -08004#include "http_body.hpp"
Ed Tanous7a6f0032024-01-28 09:49:03 -08005
Ed Tanousf0b59af2024-03-20 13:38:04 -07006#include <boost/beast/core/file_base.hpp>
Ed Tanous7a6f0032024-01-28 09:49:03 -08007#include <boost/system/error_code.hpp>
8
9#include <array>
Ed Tanousf0b59af2024-03-20 13:38:04 -070010#include <cstddef>
11#include <cstdio>
Ed Tanous7a6f0032024-01-28 09:49:03 -080012#include <span>
13#include <string>
Ed Tanousf0b59af2024-03-20 13:38:04 -070014#include <utility>
Ed Tanous7a6f0032024-01-28 09:49:03 -080015
16#include <gmock/gmock.h>
17#include <gtest/gtest.h>
18
19using ::testing::ElementsAre;
20
21namespace bmcweb
22{
23namespace
24{
25
Ed Tanousb2896142024-01-31 15:25:47 -080026TEST(HttpHttpBodyValueType, MoveString)
Ed Tanous7a6f0032024-01-28 09:49:03 -080027{
Ed Tanousb2896142024-01-31 15:25:47 -080028 HttpBody::value_type value("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -080029 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080030 HttpBody::value_type value2(std::move(value));
Ed Tanous7a6f0032024-01-28 09:49:03 -080031 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
32 EXPECT_EQ(value2.str(), "teststring");
33 EXPECT_EQ(value2.payloadSize(), 10);
34}
35
Ed Tanousb2896142024-01-31 15:25:47 -080036TEST(HttpHttpBodyValueType, MoveOperatorString)
Ed Tanous7a6f0032024-01-28 09:49:03 -080037{
Ed Tanousb2896142024-01-31 15:25:47 -080038 HttpBody::value_type value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080039 value.str() = "teststring";
40 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080041 HttpBody::value_type value2 = std::move(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -080042 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
43 EXPECT_EQ(value2.str(), "teststring");
44 EXPECT_EQ(value2.payloadSize(), 10);
45}
46
Ed Tanousb2896142024-01-31 15:25:47 -080047TEST(HttpHttpBodyValueType, copysignl)
Ed Tanous7a6f0032024-01-28 09:49:03 -080048{
Ed Tanousb2896142024-01-31 15:25:47 -080049 HttpBody::value_type value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080050 value.str() = "teststring";
51 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080052 HttpBody::value_type value2(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -080053 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
54 EXPECT_EQ(value2.str(), "teststring");
55 EXPECT_EQ(value2.payloadSize(), 10);
56}
57
Ed Tanousb2896142024-01-31 15:25:47 -080058TEST(HttpHttpBodyValueType, CopyOperatorString)
Ed Tanous7a6f0032024-01-28 09:49:03 -080059{
Ed Tanousb2896142024-01-31 15:25:47 -080060 HttpBody::value_type value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080061 value.str() = "teststring";
62 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080063 HttpBody::value_type value2 = value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080064 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
65 EXPECT_EQ(value2.str(), "teststring");
66 EXPECT_EQ(value2.payloadSize(), 10);
67}
68
Ed Tanousb2896142024-01-31 15:25:47 -080069TEST(HttpHttpBodyValueType, MoveFile)
Ed Tanous7a6f0032024-01-28 09:49:03 -080070{
Ed Tanousb2896142024-01-31 15:25:47 -080071 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous5575efb2024-01-30 09:25:03 -080072 TemporaryFileHandle temporaryFile("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -080073 boost::system::error_code ec;
Ed Tanous5575efb2024-01-30 09:25:03 -080074 value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
75 ec);
Ed Tanous7a6f0032024-01-28 09:49:03 -080076 ASSERT_FALSE(ec);
77 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080078 HttpBody::value_type value2(std::move(value));
Ed Tanous7a6f0032024-01-28 09:49:03 -080079 std::array<char, 11> buffer{};
80 size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
81 ASSERT_FALSE(ec);
82 EXPECT_EQ(value2.encodingType, EncodingType::Base64);
83
84 EXPECT_THAT(std::span(buffer.data(), out),
85 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
86
87 EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
88 'g', '\0'));
89
90 EXPECT_EQ(value2.payloadSize(), 16);
91}
92
Ed Tanousb2896142024-01-31 15:25:47 -080093TEST(HttpHttpBodyValueType, MoveOperatorFile)
Ed Tanous7a6f0032024-01-28 09:49:03 -080094{
Ed Tanousb2896142024-01-31 15:25:47 -080095 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous5575efb2024-01-30 09:25:03 -080096 TemporaryFileHandle temporaryFile("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -080097 boost::system::error_code ec;
Ed Tanous5575efb2024-01-30 09:25:03 -080098 value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
99 ec);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800100 ASSERT_FALSE(ec);
101 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -0800102 HttpBody::value_type value2 = std::move(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800103 std::array<char, 11> buffer{};
104 size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
105 ASSERT_FALSE(ec);
106 EXPECT_EQ(value2.encodingType, EncodingType::Base64);
107
108 EXPECT_THAT(std::span(buffer.data(), out),
109 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
110 EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
111 'g', '\0'));
112
113 EXPECT_EQ(value2.payloadSize(), 16);
114}
115
Ed Tanous5575efb2024-01-30 09:25:03 -0800116TEST(HttpFileBodyValueType, SetFd)
Ed Tanous7a6f0032024-01-28 09:49:03 -0800117{
Ed Tanousb2896142024-01-31 15:25:47 -0800118 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous5575efb2024-01-30 09:25:03 -0800119 TemporaryFileHandle temporaryFile("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -0800120 boost::system::error_code ec;
Ed Tanousdaadfb22024-12-20 09:25:54 -0800121 FILE* r = fopen(temporaryFile.stringPath.c_str(), "r");
122 ASSERT_NE(r, nullptr);
123 value.setFd(fileno(r), ec);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800124 ASSERT_FALSE(ec);
125
126 std::array<char, 4096> buffer{};
127
128 size_t out = value.file().read(buffer.data(), buffer.size(), ec);
129 ASSERT_FALSE(ec);
130
131 EXPECT_THAT(std::span(buffer.data(), out),
132 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
133 EXPECT_EQ(value.payloadSize(), 16);
134}
135
136} // namespace
137} // namespace bmcweb