summaryrefslogtreecommitdiff
path: root/test/http/http_file_body_test.cpp
blob: 4eaa93bac24a8d7d141fbbe794562cace111e494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "file_test_utilities.hpp"
#include "http_file_body.hpp"

#include <boost/system/error_code.hpp>

#include <array>
#include <span>
#include <string>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

using ::testing::ElementsAre;

namespace bmcweb
{
namespace
{

TEST(HttpFileBodyValueType, MoveString)
{
    FileBody::value_type value("teststring");
    // Move constructor
    FileBody::value_type value2(std::move(value));
    EXPECT_EQ(value2.encodingType, EncodingType::Raw);
    EXPECT_EQ(value2.str(), "teststring");
    EXPECT_EQ(value2.payloadSize(), 10);
}

TEST(HttpFileBodyValueType, MoveOperatorString)
{
    FileBody::value_type value;
    value.str() = "teststring";
    // Move constructor
    FileBody::value_type value2 = std::move(value);
    EXPECT_EQ(value2.encodingType, EncodingType::Raw);
    EXPECT_EQ(value2.str(), "teststring");
    EXPECT_EQ(value2.payloadSize(), 10);
}

TEST(HttpFileBodyValueType, copysignl)
{
    FileBody::value_type value;
    value.str() = "teststring";
    // Move constructor
    FileBody::value_type value2(value);
    EXPECT_EQ(value2.encodingType, EncodingType::Raw);
    EXPECT_EQ(value2.str(), "teststring");
    EXPECT_EQ(value2.payloadSize(), 10);
}

TEST(HttpFileBodyValueType, CopyOperatorString)
{
    FileBody::value_type value;
    value.str() = "teststring";
    // Move constructor
    FileBody::value_type value2 = value;
    EXPECT_EQ(value2.encodingType, EncodingType::Raw);
    EXPECT_EQ(value2.str(), "teststring");
    EXPECT_EQ(value2.payloadSize(), 10);
}

TEST(HttpFileBodyValueType, MoveFile)
{
    FileBody::value_type value(EncodingType::Base64);
    std::string filepath = makeFile("teststring");
    boost::system::error_code ec;
    value.open(filepath.c_str(), boost::beast::file_mode::read, ec);
    ASSERT_FALSE(ec);
    // Move constructor
    FileBody::value_type value2(std::move(value));
    std::array<char, 11> buffer{};
    size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
    ASSERT_FALSE(ec);
    EXPECT_EQ(value2.encodingType, EncodingType::Base64);

    EXPECT_THAT(std::span(buffer.data(), out),
                ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));

    EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
                                    'g', '\0'));

    EXPECT_EQ(value2.payloadSize(), 16);
}

TEST(HttpFileBodyValueType, MoveOperatorFile)
{
    FileBody::value_type value(EncodingType::Base64);
    std::string filepath = makeFile("teststring");
    boost::system::error_code ec;
    value.open(filepath.c_str(), boost::beast::file_mode::read, ec);
    ASSERT_FALSE(ec);
    // Move constructor
    FileBody::value_type value2 = std::move(value);
    std::array<char, 11> buffer{};
    size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
    ASSERT_FALSE(ec);
    EXPECT_EQ(value2.encodingType, EncodingType::Base64);

    EXPECT_THAT(std::span(buffer.data(), out),
                ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
    EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
                                    'g', '\0'));

    EXPECT_EQ(value2.payloadSize(), 16);
}

TEST(HttpFileBodyValueType, SetFd)
{
    FileBody::value_type value(EncodingType::Base64);
    std::string filepath = makeFile("teststring");

    boost::system::error_code ec;
    value.setFd(fileno(fopen(filepath.c_str(), "r")), ec);
    ASSERT_FALSE(ec);

    std::array<char, 4096> buffer{};

    size_t out = value.file().read(buffer.data(), buffer.size(), ec);
    ASSERT_FALSE(ec);

    EXPECT_THAT(std::span(buffer.data(), out),
                ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
    EXPECT_EQ(value.payloadSize(), 16);
}

} // namespace
} // namespace bmcweb