Files
boost_beast/test/beast/http/read.cpp
T

479 lines
13 KiB
C++
Raw Normal View History

2017-07-20 08:01:46 -07:00
//
2017-07-24 09:42:36 -07:00
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-07-20 08:01:46 -07:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
2017-07-20 08:01:46 -07:00
// Test that header file is self-contained.
2017-07-20 13:40:34 -07:00
#include <boost/beast/http/read.hpp>
2016-05-04 17:27:50 -04:00
2016-11-20 07:32:41 -05:00
#include "test_parser.hpp"
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/ostream.hpp>
#include <boost/beast/core/flat_static_buffer.hpp>
#include <boost/beast/http/fields.hpp>
#include <boost/beast/http/dynamic_body.hpp>
#include <boost/beast/http/parser.hpp>
#include <boost/beast/http/string_body.hpp>
2017-07-29 17:47:04 -07:00
#include <boost/beast/test/stream.hpp>
2017-07-20 13:40:34 -07:00
#include <boost/beast/test/yield_to.hpp>
#include <boost/beast/unit_test/suite.hpp>
2016-05-04 17:27:50 -04:00
#include <boost/asio/spawn.hpp>
2017-03-31 10:12:58 -04:00
#include <atomic>
2016-05-04 17:27:50 -04:00
2017-07-20 13:40:34 -07:00
namespace boost {
2016-05-04 17:27:50 -04:00
namespace beast {
namespace http {
class read_test
2016-05-06 19:14:17 -04:00
: public beast::unit_test::suite
2016-05-04 17:27:50 -04:00
, public test::enable_yield_to
{
public:
2016-04-30 10:29:39 -04:00
template<bool isRequest>
2016-11-20 07:32:41 -05:00
void
failMatrix(char const* s, yield_context do_yield)
2016-04-30 10:29:39 -04:00
{
using boost::asio::buffer;
using boost::asio::buffer_copy;
static std::size_t constexpr limit = 100;
std::size_t n;
auto const len = strlen(s);
for(n = 0; n < limit; ++n)
{
multi_buffer b;
b.commit(buffer_copy(
b.prepare(len), buffer(s, len)));
2016-04-30 10:29:39 -04:00
test::fail_counter fc(n);
2017-08-15 22:20:13 -07:00
test::stream ts{ios_, fc};
2016-11-20 07:32:41 -05:00
test_parser<isRequest> p(fc);
2017-06-13 07:23:58 -07:00
error_code ec = test::error::fail_error;
2017-08-21 14:45:58 -07:00
ts.close_remote();
2017-08-15 22:20:13 -07:00
read(ts, b, p, ec);
2016-04-30 10:29:39 -04:00
if(! ec)
break;
}
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(n < limit);
2016-04-30 10:29:39 -04:00
for(n = 0; n < limit; ++n)
{
static std::size_t constexpr pre = 10;
multi_buffer b;
b.commit(buffer_copy(
b.prepare(pre), buffer(s, pre)));
2016-04-30 10:29:39 -04:00
test::fail_counter fc(n);
2017-08-15 22:20:13 -07:00
test::stream ts{ios_, fc,
std::string(s + pre, len - pre)};
2016-11-20 07:32:41 -05:00
test_parser<isRequest> p(fc);
2017-06-13 07:23:58 -07:00
error_code ec = test::error::fail_error;
2017-08-21 14:45:58 -07:00
ts.close_remote();
2017-08-15 22:20:13 -07:00
read(ts, b, p, ec);
2016-04-30 10:29:39 -04:00
if(! ec)
break;
}
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(n < limit);
2016-04-30 10:29:39 -04:00
for(n = 0; n < limit; ++n)
{
multi_buffer b;
b.commit(buffer_copy(
b.prepare(len), buffer(s, len)));
2016-04-30 10:29:39 -04:00
test::fail_counter fc(n);
2017-08-15 22:20:13 -07:00
test::stream ts{ios_, fc};
2016-11-20 07:32:41 -05:00
test_parser<isRequest> p(fc);
2017-06-13 07:23:58 -07:00
error_code ec = test::error::fail_error;
2017-08-21 14:45:58 -07:00
ts.close_remote();
2017-08-15 22:20:13 -07:00
async_read(ts, b, p, do_yield[ec]);
2016-04-30 10:29:39 -04:00
if(! ec)
break;
}
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(n < limit);
2016-04-30 10:29:39 -04:00
for(n = 0; n < limit; ++n)
{
static std::size_t constexpr pre = 10;
multi_buffer b;
b.commit(buffer_copy(
b.prepare(pre), buffer(s, pre)));
2016-04-30 10:29:39 -04:00
test::fail_counter fc(n);
2017-08-15 22:20:13 -07:00
test::stream ts(ios_, fc,
std::string{s + pre, len - pre});
2016-11-20 07:32:41 -05:00
test_parser<isRequest> p(fc);
2017-06-13 07:23:58 -07:00
error_code ec = test::error::fail_error;
2017-08-21 14:45:58 -07:00
ts.close_remote();
2017-08-15 22:20:13 -07:00
async_read(ts, b, p, do_yield[ec]);
2016-10-15 09:29:14 -04:00
if(! ec)
break;
}
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(n < limit);
2016-04-30 10:29:39 -04:00
}
void testThrow()
{
try
{
multi_buffer b;
2017-07-29 17:47:04 -07:00
test::stream c{ios_, "GET / X"};
2017-08-21 14:45:58 -07:00
c.close_remote();
request_parser<dynamic_body> p;
2017-07-29 17:47:04 -07:00
read(c, b, p);
2016-04-30 10:29:39 -04:00
fail();
}
catch(std::exception const&)
{
pass();
}
}
void
testBufferOverflow()
{
{
2017-07-29 17:47:04 -07:00
test::stream c{ios_};
ostream(c.buffer()) <<
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"User-Agent: test\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"10\r\n"
"****************\r\n"
"0\r\n\r\n";
flat_static_buffer<1024> b;
request<string_body> req;
try
{
2017-07-29 17:47:04 -07:00
read(c, b, req);
pass();
}
catch(std::exception const& e)
{
fail(e.what(), __FILE__, __LINE__);
}
}
{
2017-07-29 17:47:04 -07:00
test::stream c{ios_};
ostream(c.buffer()) <<
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"User-Agent: test\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"10\r\n"
"****************\r\n"
"0\r\n\r\n";
2017-06-13 07:23:58 -07:00
error_code ec = test::error::fail_error;
flat_static_buffer<10> b;
request<string_body> req;
2017-07-29 17:47:04 -07:00
read(c, b, req, ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(ec == error::buffer_overflow,
ec.message());
}
}
2016-04-30 10:29:39 -04:00
void testFailures(yield_context do_yield)
{
char const* req[] = {
"GET / HTTP/1.0\r\n"
"Host: localhost\r\n"
"User-Agent: test\r\n"
"Empty:\r\n"
"\r\n"
,
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"User-Agent: test\r\n"
"Content-Length: 2\r\n"
"\r\n"
"**"
,
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"User-Agent: test\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"10\r\n"
"****************\r\n"
"0\r\n\r\n"
,
nullptr
};
char const* res[] = {
"HTTP/1.0 200 OK\r\n"
"Server: test\r\n"
"\r\n"
,
"HTTP/1.0 200 OK\r\n"
"Server: test\r\n"
"\r\n"
"***"
,
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"Content-Length: 3\r\n"
"\r\n"
"***"
,
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"10\r\n"
"****************\r\n"
"0\r\n\r\n"
,
nullptr
};
for(std::size_t i = 0; req[i]; ++i)
failMatrix<true>(req[i], do_yield);
for(std::size_t i = 0; res[i]; ++i)
failMatrix<false>(res[i], do_yield);
}
2016-05-04 17:27:50 -04:00
void testRead(yield_context do_yield)
{
static std::size_t constexpr limit = 100;
std::size_t n;
2016-05-07 17:06:46 -04:00
for(n = 0; n < limit; ++n)
2016-05-04 17:27:50 -04:00
{
2017-07-29 17:47:04 -07:00
test::fail_counter fc{n};
test::stream c{ios_, fc,
2016-05-04 17:27:50 -04:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"User-Agent: test\r\n"
"Content-Length: 0\r\n"
"\r\n"
2017-07-29 17:47:04 -07:00
};
request<dynamic_body> m;
2016-05-04 17:27:50 -04:00
try
{
multi_buffer b;
2017-07-29 17:47:04 -07:00
read(c, b, m);
2016-05-04 17:27:50 -04:00
break;
}
catch(std::exception const&)
{
}
}
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(n < limit);
2016-05-07 17:06:46 -04:00
for(n = 0; n < limit; ++n)
2016-05-04 17:27:50 -04:00
{
2017-08-15 22:20:13 -07:00
test::fail_counter fc{n};
test::stream ts{ios_, fc,
2016-05-04 17:27:50 -04:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"User-Agent: test\r\n"
"Content-Length: 0\r\n"
"\r\n"
2017-08-15 22:20:13 -07:00
};
request<dynamic_body> m;
2017-06-13 07:23:58 -07:00
error_code ec = test::error::fail_error;
multi_buffer b;
2017-08-15 22:20:13 -07:00
read(ts, b, m, ec);
2016-05-04 17:27:50 -04:00
if(! ec)
break;
}
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(n < limit);
2016-05-07 17:06:46 -04:00
for(n = 0; n < limit; ++n)
2016-05-04 17:27:50 -04:00
{
2017-07-29 17:47:04 -07:00
test::fail_counter fc{n};
test::stream c{ios_, fc,
2016-05-04 17:27:50 -04:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"User-Agent: test\r\n"
"Content-Length: 0\r\n"
"\r\n"
2017-07-29 17:47:04 -07:00
};
request<dynamic_body> m;
2017-06-13 07:23:58 -07:00
error_code ec = test::error::fail_error;
multi_buffer b;
2017-07-29 17:47:04 -07:00
async_read(c, b, m, do_yield[ec]);
2016-05-04 17:27:50 -04:00
if(! ec)
break;
}
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(n < limit);
2016-05-04 17:27:50 -04:00
}
2016-11-20 07:32:41 -05:00
void
testEof(yield_context do_yield)
2016-04-30 10:29:39 -04:00
{
{
multi_buffer b;
2017-08-15 22:20:13 -07:00
test::stream ts{ios_};
2017-06-03 08:56:21 -07:00
request_parser<dynamic_body> p;
2016-04-30 10:29:39 -04:00
error_code ec;
2017-08-21 14:45:58 -07:00
ts.close_remote();
2017-08-15 22:20:13 -07:00
read(ts, b, p, ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(ec == http::error::end_of_stream);
2016-04-30 10:29:39 -04:00
}
{
multi_buffer b;
2017-08-15 22:20:13 -07:00
test::stream ts{ios_};
2017-06-03 08:56:21 -07:00
request_parser<dynamic_body> p;
2016-04-30 10:29:39 -04:00
error_code ec;
2017-08-21 14:45:58 -07:00
ts.close_remote();
2017-08-15 22:20:13 -07:00
async_read(ts, b, p, do_yield[ec]);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(ec == http::error::end_of_stream);
2016-04-30 10:29:39 -04:00
}
}
2017-03-31 10:12:58 -04:00
// Ensure completion handlers are not leaked
struct handler
{
static std::atomic<std::size_t>&
count() { static std::atomic<std::size_t> n; return n; }
handler() { ++count(); }
~handler() { --count(); }
handler(handler const&) { ++count(); }
void operator()(error_code const&) const {}
};
void
testIoService()
{
{
// Make sure handlers are not destroyed
// after calling io_service::stop
boost::asio::io_service ios;
2017-08-15 22:20:13 -07:00
test::stream ts{ios,
2017-03-31 10:12:58 -04:00
"GET / HTTP/1.1\r\n\r\n"};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(handler::count() == 0);
multi_buffer b;
2017-06-07 16:30:49 -07:00
request<dynamic_body> m;
2017-08-15 22:20:13 -07:00
async_read(ts, b, m, handler{});
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(handler::count() > 0);
2017-03-31 10:12:58 -04:00
ios.stop();
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(handler::count() > 0);
2017-03-31 10:12:58 -04:00
ios.reset();
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(handler::count() > 0);
2017-03-31 10:12:58 -04:00
ios.run_one();
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(handler::count() == 0);
2017-03-31 10:12:58 -04:00
}
{
// Make sure uninvoked handlers are
// destroyed when calling ~io_service
{
boost::asio::io_service ios;
2017-08-15 22:20:13 -07:00
test::stream ts{ios,
2017-03-31 10:12:58 -04:00
"GET / HTTP/1.1\r\n\r\n"};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(handler::count() == 0);
multi_buffer b;
2017-06-07 16:30:49 -07:00
request<dynamic_body> m;
2017-08-15 22:20:13 -07:00
async_read(ts, b, m, handler{});
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(handler::count() > 0);
2017-03-31 10:12:58 -04:00
}
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(handler::count() == 0);
2017-03-31 10:12:58 -04:00
}
}
2017-07-29 05:03:05 -07:00
// https://github.com/boostorg/beast/issues/430
2017-06-07 10:48:35 -07:00
void
testRegression430()
{
2017-08-15 22:20:13 -07:00
test::stream ts{ios_};
ts.read_size(1);
ostream(ts.buffer()) <<
2017-06-07 10:48:35 -07:00
"HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked\r\n"
"Content-Type: application/octet-stream\r\n"
"\r\n"
"4\r\nabcd\r\n"
"0\r\n\r\n";
error_code ec;
flat_buffer fb;
2017-08-15 06:58:33 -07:00
response_parser<dynamic_body> p;
2017-08-15 22:20:13 -07:00
read(ts, fb, p, ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
2017-06-07 10:48:35 -07:00
}
//--------------------------------------------------------------------------
template<class Parser, class Pred>
void
readgrind(string_view s, Pred&& pred)
{
using boost::asio::buffer;
for(std::size_t n = 1; n < s.size() - 1; ++n)
{
Parser p;
2017-06-13 07:23:58 -07:00
error_code ec = test::error::fail_error;
2017-06-07 10:48:35 -07:00
flat_buffer b;
2017-08-15 22:20:13 -07:00
test::stream ts{ios_};
ostream(ts.buffer()) << s;
ts.read_size(n);
read(ts, b, p, ec);
2017-07-25 12:35:54 -07:00
if(! BEAST_EXPECTS(! ec, ec.message()))
2017-06-07 10:48:35 -07:00
continue;
pred(p);
}
}
void
testReadGrind()
{
readgrind<test_parser<false>>(
"HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked\r\n"
"Content-Type: application/octet-stream\r\n"
"\r\n"
"4\r\nabcd\r\n"
"0\r\n\r\n"
,[&](test_parser<false> const& p)
{
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(p.body == "abcd");
2017-06-07 10:48:35 -07:00
});
readgrind<test_parser<false>>(
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"Expect: Expires, MD5-Fingerprint\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"5\r\n"
"*****\r\n"
"2;a;b=1;c=\"2\"\r\n"
"--\r\n"
"0;d;e=3;f=\"4\"\r\n"
"Expires: never\r\n"
"MD5-Fingerprint: -\r\n"
"\r\n"
,[&](test_parser<false> const& p)
{
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(p.body == "*****--");
2017-06-07 10:48:35 -07:00
});
}
2016-11-20 07:32:41 -05:00
void
run() override
2016-05-04 17:27:50 -04:00
{
2016-04-30 10:29:39 -04:00
testThrow();
testBufferOverflow();
2016-04-30 10:29:39 -04:00
2017-08-15 22:20:13 -07:00
yield_to([&](yield_context yield)
{
testFailures(yield);
});
yield_to([&](yield_context yield)
{
testRead(yield);
});
yield_to([&](yield_context yield)
{
testEof(yield);
});
2017-03-31 10:12:58 -04:00
testIoService();
2017-06-07 10:48:35 -07:00
testRegression430();
testReadGrind();
2016-05-04 17:27:50 -04:00
}
};
2017-08-01 17:01:57 -07:00
BEAST_DEFINE_TESTSUITE(beast,http,read);
2016-05-04 17:27:50 -04:00
} // http
} // beast
2017-07-20 13:40:34 -07:00
} // boost