]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/beast/test/beast/http/file_body.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / beast / test / beast / http / file_body.cpp
CommitLineData
b32b8144 1//
92f5a8d4 2// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
b32b8144
FG
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// Official repository: https://github.com/boostorg/beast
8//
9
10// Test that header file is self-contained.
11#include <boost/beast/http/file_body.hpp>
12
92f5a8d4 13#include <boost/beast/core/buffer_traits.hpp>
b32b8144
FG
14#include <boost/beast/core/buffers_prefix.hpp>
15#include <boost/beast/core/file_stdio.hpp>
16#include <boost/beast/core/flat_buffer.hpp>
20effc67 17#include <boost/beast/core/static_string.hpp>
b32b8144
FG
18#include <boost/beast/http/parser.hpp>
19#include <boost/beast/http/serializer.hpp>
92f5a8d4 20#include <boost/beast/_experimental/unit_test/suite.hpp>
b32b8144 21#include <boost/filesystem.hpp>
f67539c2 22#include <boost/asio/error.hpp>
b32b8144
FG
23
24namespace boost {
25namespace beast {
26namespace http {
27
28class file_body_test : public beast::unit_test::suite
29{
30public:
31 struct lambda
32 {
33 flat_buffer buffer;
34
35 template<class ConstBufferSequence>
36 void
37 operator()(error_code&, ConstBufferSequence const& buffers)
38 {
92f5a8d4
TL
39 buffer.commit(net::buffer_copy(
40 buffer.prepare(buffer_bytes(buffers)),
b32b8144
FG
41 buffers));
42 }
43 };
44
f67539c2
TL
45 struct temp_file
46 {
47 temp_file(std::ostream& logger)
48 : path_(boost::filesystem::unique_path())
49 , log_(logger)
50 {}
51
52 ~temp_file()
53 {
54 if (!path_.empty())
55 {
56 auto ec = error_code();
57 boost::filesystem::remove(path_, ec);
58 if (ec)
59 {
60 log_ << "warning: failed to remove temporary file: " << path_ << "\n";
61 }
62 }
63
64 }
65
66 temp_file(temp_file&&) = default;
67 temp_file(temp_file const&) = delete;
68 temp_file& operator=(temp_file&&) = delete;
69 temp_file& operator=(temp_file const&) = delete;
70
71 boost::filesystem::path const& path() const
72 {
73 return path_;
74 }
75
76 private:
77
78 boost::filesystem::path path_;
79 std::ostream& log_;
80 };
81
b32b8144
FG
82 template<class File>
83 void
84 doTestFileBody()
85 {
86 error_code ec;
87 string_view const s =
88 "HTTP/1.1 200 OK\r\n"
89 "Server: test\r\n"
90 "Content-Length: 3\r\n"
91 "\r\n"
92 "xyz";
93 auto const temp = boost::filesystem::unique_path();
94 {
95 response_parser<basic_file_body<File>> p;
96 p.eager(true);
97
98 p.get().body().open(
99 temp.string<std::string>().c_str(), file_mode::write, ec);
100 BEAST_EXPECTS(! ec, ec.message());
101
92f5a8d4 102 p.put(net::buffer(s.data(), s.size()), ec);
b32b8144
FG
103 BEAST_EXPECTS(! ec, ec.message());
104 }
105 {
106 File f;
107 f.open(temp.string<std::string>().c_str(), file_mode::read, ec);
108 auto size = f.size(ec);
109 BEAST_EXPECTS(! ec, ec.message());
110 BEAST_EXPECT(size == 3);
111 std::string s1;
112 s1.resize(3);
113 f.read(&s1[0], s1.size(), ec);
114 BEAST_EXPECTS(! ec, ec.message());
115 BEAST_EXPECTS(s1 == "xyz", s);
116 }
117 {
118 lambda visit;
119 {
120 response<basic_file_body<File>> res{status::ok, 11};
121 res.set(field::server, "test");
122 res.body().open(temp.string<std::string>().c_str(),
123 file_mode::scan, ec);
124 BEAST_EXPECTS(! ec, ec.message());
125 res.prepare_payload();
126
127 serializer<false, basic_file_body<File>, fields> sr{res};
128 sr.next(ec, visit);
129 BEAST_EXPECTS(! ec, ec.message());
130 auto const b = buffers_front(visit.buffer.data());
131 string_view const s1{
132 reinterpret_cast<char const*>(b.data()),
133 b.size()};
134 BEAST_EXPECTS(s1 == s, s1);
135 }
136 }
137 boost::filesystem::remove(temp, ec);
138 BEAST_EXPECTS(! ec, ec.message());
139 }
f67539c2
TL
140
141 template<class File>
142 void
143 fileBodyUnexpectedEofOnGet()
144 {
145 auto temp = temp_file(log);
146
147 error_code ec;
148 string_view const ten =
149 "0123456789"; // 40
150 // create the temporary file
151 {
152 std::ofstream fstemp(temp.path().native());
153 std::size_t written = 0;
154 std::size_t to_write = 4097;
155 while (written < to_write)
156 {
157 auto size = std::min(ten.size(), to_write - written);
158 fstemp << ten.substr(0, size);
159 BEAST_EXPECT(fstemp.good());
160 written += size;
161 }
162 fstemp.close();
163 }
164
165 // open the file and read the header
166 {
167 using file_body_type = basic_file_body<File>;
168
169 typename file_body_type::value_type value;
170 // opened in write mode so we can truncate later
171 value.open(temp.path().string<std::string>().c_str(), file_mode::read, ec);
172 BEAST_EXPECTS(! ec, ec.message());
173
174 response_header<> header;
175 header.version(11);
176 header.result(status::accepted);
177 header.set(field::server, "test");
20effc67 178 header.set(field::content_length, to_static_string(4097));
f67539c2
TL
179
180 typename file_body_type::writer w(header, value);
181 auto maybe_range = w.get(ec);
182 BEAST_EXPECTS(!ec, ec.message());
183 BEAST_EXPECTS(maybe_range.has_value(), "no range returned");
184 BEAST_EXPECT(maybe_range.value().second);
185
186 value.file().seek(4097, ec);
187 BEAST_EXPECTS(!ec, ec.message());
188
189 maybe_range = w.get(ec);
190 BEAST_EXPECTS(ec == error::short_read, ec.message());
191 BEAST_EXPECTS(!maybe_range.has_value(), "range returned on error");
192 }
193 }
194
b32b8144
FG
195 void
196 run() override
197 {
198 doTestFileBody<file_stdio>();
f67539c2 199#if BOOST_BEAST_USE_WIN32_FILE
b32b8144 200 doTestFileBody<file_win32>();
f67539c2
TL
201#endif
202#if BOOST_BEAST_USE_POSIX_FILE
b32b8144 203 doTestFileBody<file_posix>();
f67539c2
TL
204#endif
205
206 fileBodyUnexpectedEofOnGet<file_stdio>();
207 #if BOOST_BEAST_USE_POSIX_FILE
208 fileBodyUnexpectedEofOnGet<file_posix>();
b32b8144 209 #endif
f67539c2
TL
210 #if BOOST_BEAST_USE_WIN32_FILE
211 fileBodyUnexpectedEofOnGet<file_win32>();
212 #endif
213
b32b8144
FG
214 }
215};
216
217BEAST_DEFINE_TESTSUITE(beast,http,file_body);
218
219} // http
220} // beast
221} // boost