blob: 889161c53403e2aeed8caf2d72dbbf812b4f7de8 [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);
Ed Tanousb2539062024-03-12 16:58:35 -070065 EXPECT_EQ(value2.compressionType, CompressionType::Raw);
Ed Tanous7a6f0032024-01-28 09:49:03 -080066 EXPECT_EQ(value2.str(), "teststring");
67 EXPECT_EQ(value2.payloadSize(), 10);
68}
69
Ed Tanousb2896142024-01-31 15:25:47 -080070TEST(HttpHttpBodyValueType, MoveFile)
Ed Tanous7a6f0032024-01-28 09:49:03 -080071{
Ed Tanousb2539062024-03-12 16:58:35 -070072 HttpBody::value_type value(EncodingType::Base64, CompressionType::Raw);
Ed Tanous5575efb2024-01-30 09:25:03 -080073 TemporaryFileHandle temporaryFile("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -080074 boost::system::error_code ec;
Ed Tanous5575efb2024-01-30 09:25:03 -080075 value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
76 ec);
Ed Tanous7a6f0032024-01-28 09:49:03 -080077 ASSERT_FALSE(ec);
78 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080079 HttpBody::value_type value2(std::move(value));
Ed Tanous7a6f0032024-01-28 09:49:03 -080080 std::array<char, 11> buffer{};
81 size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
82 ASSERT_FALSE(ec);
83 EXPECT_EQ(value2.encodingType, EncodingType::Base64);
Ed Tanousb2539062024-03-12 16:58:35 -070084 EXPECT_EQ(value2.compressionType, CompressionType::Raw);
Ed Tanous7a6f0032024-01-28 09:49:03 -080085
86 EXPECT_THAT(std::span(buffer.data(), out),
87 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
88
89 EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
90 'g', '\0'));
91
92 EXPECT_EQ(value2.payloadSize(), 16);
93}
94
Ed Tanousb2896142024-01-31 15:25:47 -080095TEST(HttpHttpBodyValueType, MoveOperatorFile)
Ed Tanous7a6f0032024-01-28 09:49:03 -080096{
Ed Tanousb2539062024-03-12 16:58:35 -070097 HttpBody::value_type value(EncodingType::Base64, CompressionType::Raw);
Ed Tanous5575efb2024-01-30 09:25:03 -080098 TemporaryFileHandle temporaryFile("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -080099 boost::system::error_code ec;
Ed Tanous5575efb2024-01-30 09:25:03 -0800100 value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
101 ec);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800102 ASSERT_FALSE(ec);
103 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -0800104 HttpBody::value_type value2 = std::move(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800105 std::array<char, 11> buffer{};
106 size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
107 ASSERT_FALSE(ec);
108 EXPECT_EQ(value2.encodingType, EncodingType::Base64);
Ed Tanousb2539062024-03-12 16:58:35 -0700109 EXPECT_EQ(value2.compressionType, CompressionType::Raw);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800110
111 EXPECT_THAT(std::span(buffer.data(), out),
112 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
113 EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
114 'g', '\0'));
115
116 EXPECT_EQ(value2.payloadSize(), 16);
117}
118
Ed Tanous5575efb2024-01-30 09:25:03 -0800119TEST(HttpFileBodyValueType, SetFd)
Ed Tanous7a6f0032024-01-28 09:49:03 -0800120{
Ed Tanousb2539062024-03-12 16:58:35 -0700121 HttpBody::value_type value(EncodingType::Base64, CompressionType::Raw);
Ed Tanous5575efb2024-01-30 09:25:03 -0800122 TemporaryFileHandle temporaryFile("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -0800123 boost::system::error_code ec;
Ed Tanousdaadfb22024-12-20 09:25:54 -0800124 FILE* r = fopen(temporaryFile.stringPath.c_str(), "r");
125 ASSERT_NE(r, nullptr);
126 value.setFd(fileno(r), ec);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800127 ASSERT_FALSE(ec);
128
129 std::array<char, 4096> buffer{};
130
131 size_t out = value.file().read(buffer.data(), buffer.size(), ec);
132 ASSERT_FALSE(ec);
133
134 EXPECT_THAT(std::span(buffer.data(), out),
135 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
136 EXPECT_EQ(value.payloadSize(), 16);
137}
138
139} // namespace
140} // namespace bmcweb