Improvements to code coverage:

More tests and test cases are added to bring up
the percentage of library code covered by tests.
This commit is contained in:
Vinnie Falco
2017-07-20 08:12:06 -07:00
parent 00d8ed71fd
commit a59329ff8e
17 changed files with 649 additions and 122 deletions
+107 -1
View File
@@ -10,7 +10,6 @@
#include "message_fuzz.hpp"
#include <beast/error.hpp>
#include <beast/streambuf.hpp>
#include <beast/write_streambuf.hpp>
#include <beast/http/rfc2616.hpp>
@@ -102,6 +101,68 @@ public:
}
};
template<bool isRequest>
struct cb_fail
: public basic_parser_v1<isRequest, cb_fail<isRequest>>
{
std::size_t n_;
void fail(error_code& ec)
{
if(n_ > 0)
--n_;
if(! n_)
ec = boost::system::errc::make_error_code(
boost::system::errc::invalid_argument);
}
private:
friend class basic_parser_v1<isRequest, cb_checker<isRequest>>;
void on_method(boost::string_ref const&, error_code& ec)
{
fail(ec);
}
void on_uri(boost::string_ref const&, error_code& ec)
{
fail(ec);
}
void on_reason(boost::string_ref const&, error_code& ec)
{
fail(ec);
}
void on_request(error_code& ec)
{
fail(ec);
}
void on_response(error_code& ec)
{
fail(ec);
}
void on_field(boost::string_ref const&, error_code& ec)
{
fail(ec);
}
void on_value(boost::string_ref const&, error_code& ec)
{
fail(ec);
}
int on_headers(error_code& ec)
{
fail(ec);
return 0;
}
void on_body(boost::string_ref const&, error_code& ec)
{
fail(ec);
}
void on_complete(error_code& ec)
{
fail(ec);
}
};
//--------------------------------------------------------------------------
static
@@ -177,6 +238,50 @@ public:
}
};
void
testFail()
{
using boost::asio::buffer;
{
std::string const s =
"GET / HTTP/1.1\r\n"
"User-Agent: test\r\n"
"Content-Length: 1\r\n"
"\r\n"
"*";
static std::size_t constexpr limit = 100;
std::size_t n = 1;
for(; n < limit; ++n)
{
error_code ec;
basic_parser_v1<true, cb_fail<true>> p;
p.write(buffer(s), ec);
if(! ec)
break;
}
expect(n < limit);
}
{
std::string const s =
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"Content-Length: 1\r\n"
"\r\n"
"*";
static std::size_t constexpr limit = 100;
std::size_t n = 1;
for(; n < limit; ++n)
{
error_code ec;
basic_parser_v1<false, cb_fail<false>> p;
p.write(buffer(s), ec);
if(! ec)
break;
}
expect(n < limit);
}
}
void
testCallbacks()
{
@@ -647,6 +752,7 @@ public:
void run() override
{
testFail();
testCallbacks();
testVersion();
testFlags();
+47
View File
@@ -7,3 +7,50 @@
// Test that header file is self-contained.
#include <beast/http/parse_error.hpp>
#include <beast/unit_test/suite.hpp>
#include <memory>
namespace beast {
namespace http {
class parse_error_test : public unit_test::suite
{
public:
void check(char const* name, parse_error ev)
{
auto const ec = make_error_code(ev);
expect(std::string{ec.category().name()} == name);
expect(! ec.message().empty());
expect(std::addressof(ec.category()) ==
std::addressof(get_parse_error_category()));
expect(get_parse_error_category().equivalent(static_cast<int>(ev),
ec.category().default_error_condition(static_cast<int>(ev))));
expect(get_parse_error_category().equivalent(
ec, static_cast<int>(ev)));
}
void run() override
{
check("http", parse_error::connection_closed);
check("http", parse_error::bad_method);
check("http", parse_error::bad_uri);
check("http", parse_error::bad_version);
check("http", parse_error::bad_crlf);
check("http", parse_error::bad_request);
check("http", parse_error::bad_status_code);
check("http", parse_error::bad_status);
check("http", parse_error::bad_field);
check("http", parse_error::bad_value);
check("http", parse_error::bad_content_length);
check("http", parse_error::illegal_content_length);
check("http", parse_error::bad_on_headers_rv);
check("http", parse_error::invalid_chunk_size);
check("http", parse_error::short_read);
}
};
BEAST_DEFINE_TESTSUITE(parse_error,http,beast);
} // http
} // beast
+20
View File
@@ -7,3 +7,23 @@
// Test that header file is self-contained.
#include <beast/http/reason.hpp>
#include <beast/unit_test/suite.hpp>
namespace beast {
namespace http {
class reason_test : public unit_test::suite
{
public:
void run() override
{
for(int i = 1; i <= 999; ++i)
expect(reason_string(i) != nullptr);
}
};
BEAST_DEFINE_TESTSUITE(reason,http,beast);
} // http
} // beast
+38 -4
View File
@@ -56,7 +56,7 @@ public:
}
};
struct test_Body
struct fail_body
{
using value_type = std::string;
@@ -67,7 +67,41 @@ public:
public:
template<bool isRequest, class Allocator>
explicit
writer(message<isRequest, test_Body, Allocator> const& msg)
writer(message<isRequest, fail_body, Allocator> const& msg)
: body_(msg.body)
{
}
void
init(error_code& ec)
{
ec = boost::system::errc::make_error_code(
boost::system::errc::errc_t::invalid_argument);
}
template<class Write>
boost::tribool
operator()(resume_context&&, error_code&, Write&& write)
{
write(boost::asio::buffer(body_));
return true;
}
};
};
struct test_body
{
using value_type = std::string;
class writer
{
std::size_t pos_ = 0;
value_type const& body_;
public:
template<bool isRequest, class Allocator>
explicit
writer(message<isRequest, test_body, Allocator> const& msg)
: body_(msg.body)
{
}
@@ -148,7 +182,7 @@ public:
}
// no content-length HTTP/1.0
{
message_v1<true, test_Body, headers> m{{
message_v1<true, test_body, headers> m{{
"GET", "/", 10}};
m.headers.insert("User-Agent", "test");
m.body = "*";
@@ -214,7 +248,7 @@ public:
}
// no content-length HTTP/1.1
{
message_v1<true, test_Body, headers> m{{
message_v1<true, test_body, headers> m{{
"GET", "/", 11}};
m.headers.insert("User-Agent", "test");
m.body = "*";