Reorganize source files

This commit is contained in:
Vinnie Falco
2016-04-19 20:29:16 -04:00
parent 54f6f0ceba
commit f07cd8ceb4
239 changed files with 2692 additions and 19127 deletions

View File

@@ -7,24 +7,27 @@
import os ;
path-constant test_main : ../beast/unit_test/src/main.cpp ;
unit-test all :
append_buffers.cpp
asio.cpp
../src/beast_http_nodejs_parser.cpp
async_completion.cpp
base64.cpp
basic_streambuf.cpp
basic_headers.cpp
bind_handler.cpp
buffer_cat.cpp
buffers_adapter.cpp
buffers_debug.cpp
chunk_encode.cpp
consuming_buffers.cpp
empty_base_optimization.cpp
handler_alloc.cpp
message.cpp
placeholders.cpp
prepare_buffers.cpp
rfc2616.cpp
static_streambuf.cpp
streambuf.cpp
streambuf_readstream.cpp
type_check.cpp
$(test_main)
main.cpp
;

View File

@@ -1,9 +0,0 @@
//
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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)
//
// Test that header file is self-contained.
#include <beast/asio.h>

View File

@@ -6,4 +6,4 @@
//
// Test that header file is self-contained.
#include <beast/asio/async_completion.h>
#include <beast/async_completion.hpp>

View File

@@ -17,27 +17,38 @@
*/
//==============================================================================
#include <beast/asio/ssl_error.h>
#include <beast/unit_test/suite.h>
#include <string>
#include <beast/detail/base64.hpp>
#include <beast/detail/unit_test/suite.hpp>
namespace beast {
namespace test {
class ssl_error_test : public unit_test::suite
class base64_test : public detail::unit_test::suite
{
public:
void run()
void
check (std::string const& in, std::string const& out)
{
{
boost::system::error_code ec =
boost::system::error_code (335544539,
boost::asio::error::get_ssl_category ());
std::string const s = beast::error_message_with_ssl(ec);
expect(s == " (20,0,219) error:140000DB:SSL routines:SSL routines:short read");
}
auto const encoded = detail::base64_encode (in);
expect (encoded == out);
expect (detail::base64_decode (encoded) == in);
}
void
run()
{
check ("", "");
check ("f", "Zg==");
check ("fo", "Zm8=");
check ("foo", "Zm9v");
check ("foob", "Zm9vYg==");
check ("fooba", "Zm9vYmE=");
check ("foobar", "Zm9vYmFy");
}
};
BEAST_DEFINE_TESTSUITE(ssl_error,asio,beast);
BEAST_DEFINE_TESTSUITE(base64,crypto,beast);
} // test
} // beast

View File

@@ -6,15 +6,15 @@
//
// Test that header file is self-contained.
#include <beast/http/basic_headers.h>
#include <beast/http/basic_headers.hpp>
#include <beast/unit_test/suite.h>
#include <beast/detail/unit_test/suite.hpp>
namespace beast {
namespace http {
namespace test {
class basic_headers_test : public unit_test::suite
class basic_headers_test : public beast::detail::unit_test::suite
{
public:
template<class Allocator>

View File

@@ -6,4 +6,4 @@
//
// Test that header file is self-contained.
#include <beast/asio/basic_streambuf.h>
#include <beast/basic_streambuf.hpp>

View File

@@ -1,358 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <beast/wsproto/src/test/async_echo_peer.h>
#include <beast/wsproto/src/test/sync_echo_peer.h>
#include <beast/unit_test/suite.h>
#include <beast/unit_test/thread.h>
#include <beast/http.h>
#include <boost/asio/spawn.hpp>
namespace beast {
namespace wsproto {
class ws_test : public unit_test::suite
{
public:
using error_code = boost::system::error_code;
using endpoint_type = boost::asio::ip::tcp::endpoint;
using address_type = boost::asio::ip::address;
using socket_type = boost::asio::ip::tcp::socket;
using yield_context = boost::asio::yield_context;
endpoint_type ep_;
//--------------------------------------------------------------------------
// opcodes for creating the test plans
// concurrent read and write
struct case_1{};
// write a bad frame and shut down
struct case_2{};
//--------------------------------------------------------------------------
class coro_peer
{
error_code ec_;
boost::asio::io_service ios_;
boost::asio::ip::tcp::acceptor acceptor_;
socket_type sock_;
socket<socket_type&> ws_;
opcode::value op_;
beast::streambuf rb_;
beast::streambuf wb_;
yield_context* yield_;
int state_ = 0;
//unit_test::suite& test_;
public:
coro_peer(coro_peer&&) = default;
coro_peer(coro_peer const&) = delete;
coro_peer& operator=(coro_peer&&) = delete;
coro_peer& operator=(coro_peer const&) = delete;
template<class... Ops>
coro_peer(bool server, endpoint_type ep,
unit_test::suite& test, Ops const&... ops)
: acceptor_(ios_)
, sock_(ios_)
, ws_(sock_)
//, test_(test)
{
if(server)
{
acceptor_.open(ep.protocol());
acceptor_.bind(ep);
acceptor_.listen(
boost::asio::socket_base::max_connections);
boost::asio::spawn(ios_,
[=](auto yield)
{
yield_ = &yield;
state_ = 10;
acceptor_.async_accept(sock_, (*yield_)[ec_]);
if(ec_)
return this->fail("accept");
state_ = 20;
ws_.async_accept((*yield_)[ec_]);
if(ec_)
return this->fail("ws.accept");
this->invoke(ops...);
state_ = -1;
});
}
else
{
boost::asio::spawn(ios_,
[=](auto yield)
{
yield_ = &yield;
state_ = 30;
sock_.async_connect(ep, (*yield_)[ec_]);
if(ec_)
return this->fail("connect");
state_ = 40;
ws_.async_handshake(ep.address().to_string() +
std::to_string(ep.port()), "/", (*yield_)[ec_]);
if(ec_)
return this->fail("handshake");
this->invoke(ops...);
state_ = -1;
});
}
}
~coro_peer()
{
}
int
state() const
{
return state_;
}
void
run_one()
{
ios_.run_one();
}
void
step_to(int to = 0)
{
while(state_ != to)
ios_.run_one();
}
private:
template<class String>
void fail(String const& s)
{
}
void invoke_1(case_1)
{
async_read(ws_, op_, rb_,
[&](auto ec)
{
if(ec)
return this->fail(ec);
rb_.consume(rb_.size());
});
state_ = 100;
async_write(ws_, opcode::text,
boost::asio::null_buffers{}, (*yield_)[ec_]);
if(ec_)
return fail("write");
}
void invoke_1(case_2)
{
detail::frame_header fh;
fh.op = opcode::rsv5; // bad opcode
fh.fin = true;
fh.mask = true;
fh.rsv1 = false;
fh.rsv2 = false;
fh.rsv3 = false;
fh.len = 0;
fh.key = 0;
detail::write(wb_, fh);
state_ = 200;
boost::asio::async_write(
ws_.next_layer(), wb_.data(),
(*yield_)[ec_]);
if(ec_)
return fail("write");
ws_.next_layer().shutdown(
socket_type::shutdown_both, ec_);
if(ec_)
return fail("shutdown");
}
inline
void
invoke()
{
}
template<class Op, class... Ops>
inline
void
invoke(Op op, Ops const&... ops)
{
invoke_1(op);
invoke(ops...);
}
};
void
testInvokable()
{
endpoint_type const ep{
address_type::from_string(
"127.0.0.1"), 6000};
coro_peer server(true, ep, *this, case_1{});
coro_peer client(false, ep, *this, case_2{});
server.step_to(10); // async_accept
client.step_to(30); // async_connect
server.step_to(20); // async_accept(ws)
client.step_to(40); // async_handshake
server.step_to(100); // case_1
client.step_to(200); // case_2
client.step_to(-1);
server.step_to(-1);
}
//--------------------------------------------------------------------------
void
maybe_fail(error_code const& ec, std::string const& what)
{
expect(! ec, what + ": " + ec.message());
}
void
maybe_throw(error_code ec, std::string what)
{
if(ec)
{
maybe_fail(ec, what);
throw ec;
}
}
template<class Buffers>
static
std::string
buffers_to_string(Buffers const& bs)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(auto const& b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
for(auto i = s.size(); i-- > 0;)
if(s[i] == '\r')
s.replace(i, 1, "\\r");
else if(s[i] == '\n')
s.replace(i, 1, "\\n\n");
return s;
}
int
makeRequest(endpoint_type ep, std::string const& s)
{
using boost::asio::buffer;
boost::asio::io_service ios;
boost::asio::ip::tcp::socket sock(ios);
sock.connect(ep);
write(sock, append_buffers(
buffer(s), buffer("\r\n")));
using namespace http;
parsed_response<string_body> m;
streambuf sb;
read(sock, sb, m);
return m.status;
}
void
expectStatus(endpoint_type ep,
int status, std::string const& s)
{
expect(makeRequest(ep, s) == status);
}
void
testHandshake(endpoint_type ep)
{
expectStatus(ep, 400, "GET / HTTP/1.0\r\n");
}
void
syncEchoClient(endpoint_type ep)
{
using boost::asio::buffer;
error_code ec;
boost::asio::io_service ios;
wsproto::socket<socket_type> ws(ios);
ws.next_layer().connect(ep, ec);
maybe_fail(ec, "connect");
ws.handshake(ep.address().to_string(), "/", ec);
maybe_fail(ec, "upgrade");
std::string const s = "Hello, world!";
ws.write(wsproto::opcode::text, true, buffer(s), ec);
maybe_fail(ec, "write");
boost::asio::streambuf sb;
wsproto::opcode::value op;
read(ws, op, sb, ec);
maybe_fail(ec, "read");
if(! ec)
expect(op == wsproto::opcode::text);
expect(buffers_to_string(sb.data()) == s);
sb.consume(sb.size());
ws.close({}, ec);
maybe_fail(ec, "close");
while(! ec)
{
read(ws, op, sb, ec);
if(! ec)
sb.consume(sb.size());
}
if(ec != error::closed)
maybe_fail(ec, "teardown");
}
void
run() override
{
//testInvokable();
#if 0
{
endpoint_type ep{
address_type::from_string("127.0.0.1"), 6000};
testcase("Echo Server");
test::sync_echo_peer s(true, ep, *this);
testHandshake(ep);
syncEchoClient(ep);
}
#endif
{
endpoint_type ep{
address_type::from_string("127.0.0.1"), 6001};
testcase("Async Echo Server");
test::async_echo_peer s(true, ep, *this);
//testHandshake(ep);
syncEchoClient(ep);
}
}
};
BEAST_DEFINE_TESTSUITE(ws,asio,beast);
} // wsproto
} // beast

View File

@@ -6,6 +6,32 @@
//
// Test that header file is self-contained.
#include <beast/asio/bind_handler.h>
#include <beast/bind_handler.hpp>
#include <beast/asio/src/test/beast_asio_bind_handler_test.cpp>
#include <beast/detail/unit_test/suite.hpp>
#include <functional>
namespace beast {
namespace test {
class bind_handler_test : public detail::unit_test::suite
{
public:
static void foo (int)
{
}
void run()
{
auto f (bind_handler (
std::bind (&foo, std::placeholders::_1),
42));
f();
pass();
}
};
BEAST_DEFINE_TESTSUITE(bind_handler,asio,beast);
} // test
} // beast

View File

@@ -6,9 +6,9 @@
//
// Test that header file is self-contained.
#include <beast/asio/append_buffers.h>
#include <beast/buffer_cat.hpp>
#include <beast/unit_test/suite.h>
#include <beast/detail/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/streambuf.hpp>
#include <iterator>
@@ -16,14 +16,13 @@
#include <vector>
namespace beast {
namespace asio {
namespace test {
class append_buffers_test : public unit_test::suite
class buffer_cat_test : public detail::unit_test::suite
{
public:
void testAppendBuffers()
void testBufferCat()
{
using boost::asio::buffer_size;
using boost::asio::const_buffer;
@@ -40,7 +39,7 @@ public:
std::list<const_buffer> b5{
const_buffer{buf+9, 1}};
std::list<const_buffer> b6;
auto bs = append_buffers(
auto bs = buffer_cat(
b1, b2, b3, b4, b5, b6);
expect(buffer_size(bs) == 10);
std::vector<const_buffer> v;
@@ -54,23 +53,22 @@ public:
bs3 = std::move(bs2);
{
boost::asio::streambuf sb1, sb2;
expect(buffer_size(append_buffers(
expect(buffer_size(buffer_cat(
sb1.prepare(5), sb2.prepare(7))) == 12);
sb1.commit(5);
sb2.commit(7);
expect(buffer_size(append_buffers(
expect(buffer_size(buffer_cat(
sb1.data(), sb2.data())) == 12);
}
}
void run() override
{
testAppendBuffers();
testBufferCat();
}
};
BEAST_DEFINE_TESTSUITE(append_buffers,asio,beast);
BEAST_DEFINE_TESTSUITE(buffer_cat,asio,beast);
} // test
} // asio
} // beast

View File

@@ -6,18 +6,17 @@
//
// Test that header file is self-contained.
#include <beast/asio/buffers_adapter.h>
#include <beast/buffers_adapter.hpp>
#include <beast/unit_test/suite.h>
#include <beast/detail/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/streambuf.hpp>
#include <iterator>
namespace beast {
namespace asio {
namespace test {
class buffers_adapter_test : public unit_test::suite
class buffers_adapter_test : public detail::unit_test::suite
{
public:
template<class ConstBufferSequence>
@@ -120,5 +119,4 @@ public:
BEAST_DEFINE_TESTSUITE(buffers_adapter,asio,beast);
} // test
} // asio
} // beast

View File

@@ -6,4 +6,4 @@
//
// Test that header file is self-contained.
#include <beast/asio/buffers_debug.h>
#include <beast/buffers_debug.hpp>

154
test/chunk_encode.cpp Normal file
View File

@@ -0,0 +1,154 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <beast/streambuf.hpp>
#include <beast/http/chunk_encode.hpp>
#include <beast/detail/unit_test/suite.hpp>
namespace beast {
namespace http {
namespace test {
class chunk_encode_test : public beast::detail::unit_test::suite
{
public:
// Convert CR LF to printables for display
static
std::string
encode (std::string const& s)
{
std::string result;
for(auto const c : s)
{
if (c == '\r')
result += "\\r";
else if (c== '\n')
result += "\\n";
else
result += c;
}
return result;
}
// Print the contents of a ConstBufferSequence to the log
template <class ConstBufferSequence, class Log>
static
void
print (ConstBufferSequence const& buffers, Log log)
{
for(auto const& buf : buffers)
log << encode (std::string(
boost::asio::buffer_cast<char const*>(buf),
boost::asio::buffer_size(buf)));
}
// Convert a ConstBufferSequence to a string
template <class ConstBufferSequence>
static
std::string
buffer_to_string (ConstBufferSequence const& b)
{
std::string s;
auto const n = boost::asio::buffer_size(b);
s.resize(n);
boost::asio::buffer_copy(
boost::asio::buffer(&s[0], n), b);
return s;
}
// Append a ConstBufferSequence to an existing string
template <class ConstBufferSequence>
static
void
buffer_append (std::string& s, ConstBufferSequence const& b)
{
s += buffer_to_string(b);
}
// Convert the input sequence of the stream to a
// chunked-encoded string. The input sequence is consumed.
template <class Streambuf>
static
std::string
streambuf_to_string (Streambuf& sb,
bool final_chunk = false)
{
std::string s;
buffer_append(s, chunk_encode(sb.data(), final_chunk));
return s;
}
// Check an input against the correct chunk encoded version
void
check (std::string const& in, std::string const& answer,
bool final_chunk = true)
{
streambuf sb(3);
sb << in;
auto const out = streambuf_to_string (sb, final_chunk);
if (! expect (out == answer))
log << "expected\n" << encode(answer) <<
"\ngot\n" << encode(out);
}
void testStreambuf()
{
streambuf sb(3);
std::string const s =
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789";
sb << s;
expect(buffer_to_string(sb.data()) == s);
}
void
testEncoder()
{
check("", "0\r\n\r\n");
check("x", "1\r\nx\r\n0\r\n\r\n");
check("abcd", "4\r\nabcd\r\n0\r\n\r\n");
check("x", "1\r\nx\r\n", false);
check(
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
,
"d2\r\n"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"0123456789012345678901234567890123456789012345678901234567890123456789"
"\r\n"
"0\r\n\r\n");
}
void
run()
{
testStreambuf();
testEncoder();
}
};
BEAST_DEFINE_TESTSUITE(chunk_encode,http,beast);
} // test
} // http
} // beast

View File

@@ -6,17 +6,16 @@
//
// Test that header file is self-contained.
#include <beast/asio/consuming_buffers.h>
#include <beast/consuming_buffers.hpp>
#include <beast/unit_test/suite.h>
#include <beast/detail/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <string>
namespace beast {
namespace asio {
namespace test {
class consuming_buffers_test : public unit_test::suite
class consuming_buffers_test : public beast::detail::unit_test::suite
{
public:
template<class ConstBufferSequence>
@@ -93,5 +92,4 @@ public:
BEAST_DEFINE_TESTSUITE(consuming_buffers,asio,beast);
} // test
} // asio
} // beast

View File

@@ -0,0 +1,109 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2014, Howard Hinnant <howard.hinnant@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#if BEAST_INCLUDE_BEASTCONFIG
#include <BeastConfig.h>
#endif
#include <beast/detail/empty_base_optimization.hpp>
#include <beast/detail/unit_test/suite.hpp>
namespace beast {
namespace test {
class empty_base_optimization_test
: public beast::detail::unit_test::suite
{
public:
template <class T>
class test1
: private detail::empty_base_optimization<T>
{
using Base = detail::empty_base_optimization<T>;
void* m_p;
public:
explicit test1 (T const& t)
: Base (t)
{}
T& member() {return Base::member();}
T const& member() const {return Base::member();}
};
template <class T>
class test2
{
void* m_p;
T m_t;
public:
explicit test2 (T const& t)
: m_t (t)
{}
T& member() {return m_t;}
T const& member() const {return m_t;}
};
struct Empty
{
operator bool() {return true;}
};
static
bool
test_one ()
{
test1<int> t1(1);
test2<int> t2(2);
static_assert(sizeof(t1) == sizeof(t2), "don't optimize for int");
if (t1.member() != 1)
return false;
if (t2.member() != 2)
return false;
return true;
}
static
bool
test_two ()
{
test1<Empty> t1((Empty()));
test2<Empty> t2((Empty()));
static_assert(sizeof(t1) < sizeof(t2), "do optimize for Empty");
if (t1.member() != true)
return false;
if (t2.member() != true)
return false;
return true;
}
void
run ()
{
expect (test_one());
expect (test_two());
pass ();
}
};
BEAST_DEFINE_TESTSUITE(empty_base_optimization,utility,beast);
} // test
} // beast

View File

@@ -6,4 +6,4 @@
//
// Test that header file is self-contained.
#include <beast/asio/handler_alloc.h>
#include <beast/handler_alloc.hpp>

58
test/main.cpp Normal file
View File

@@ -0,0 +1,58 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <beast/detail/unit_test/reporter.hpp>
#include <beast/detail/unit_test/suite.hpp>
#include <beast/detail/stream/debug_ostream.hpp>
#ifdef _MSC_VER
# ifndef WIN32_LEAN_AND_MEAN // VC_EXTRALEAN
# define WIN32_LEAN_AND_MEAN
#include <windows.h>
# undef WIN32_LEAN_AND_MEAN
# else
#include <windows.h>
# endif
#endif
#include <cstdlib>
// Simple main used to produce stand
// alone executables that run unit tests.
int main()
{
using namespace beast::detail::unit_test;
#ifdef _MSC_VER
{
int flags = _CrtSetDbgFlag (_CRTDBG_REPORT_FLAG);
flags |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag (flags);
}
#endif
{
beast::detail::debug_ostream s;
reporter r (s);
bool failed (r.run_each (global_suites()));
if (failed)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
}

188
test/message.cpp Normal file
View File

@@ -0,0 +1,188 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
// Test that header file is self-contained.
#include <beast/http/message.hpp>
#include <beast/detail/unit_test/suite.hpp>
#include <beast/detail/unit_test/thread.hpp>
#include <beast/buffers_debug.hpp>
#include <beast/placeholders.hpp>
#include <beast/streambuf.hpp>
#include <beast/http.hpp>
#include <boost/asio.hpp>
namespace beast {
namespace http {
namespace test {
class sync_echo_http_server
{
public:
using error_code = boost::system::error_code;
using endpoint_type = boost::asio::ip::tcp::endpoint;
using address_type = boost::asio::ip::address;
using socket_type = boost::asio::ip::tcp::socket;
private:
beast::detail::unit_test::suite& suite_;
boost::asio::io_service ios_;
socket_type sock_;
boost::asio::ip::tcp::acceptor acceptor_;
beast::detail::unit_test::thread thread_;
public:
sync_echo_http_server(
endpoint_type ep, beast::detail::unit_test::suite& suite)
: suite_(suite)
, sock_(ios_)
, acceptor_(ios_)
{
error_code ec;
acceptor_.open(ep.protocol(), ec);
maybe_throw(ec, "open");
acceptor_.bind(ep, ec);
maybe_throw(ec, "bind");
acceptor_.listen(
boost::asio::socket_base::max_connections, ec);
maybe_throw(ec, "listen");
acceptor_.async_accept(sock_,
std::bind(&sync_echo_http_server::on_accept, this,
beast::asio::placeholders::error));
thread_ = beast::detail::unit_test::thread(suite_,
[&]
{
ios_.run();
});
}
~sync_echo_http_server()
{
error_code ec;
ios_.dispatch(
[&]{ acceptor_.close(ec); });
thread_.join();
}
private:
void
fail(error_code ec, std::string what)
{
suite_.log <<
what << ": " << ec.message();
}
void
maybe_throw(error_code ec, std::string what)
{
if(ec &&
ec != boost::asio::error::operation_aborted)
{
fail(ec, what);
throw ec;
}
}
void
on_accept(error_code ec)
{
if(ec == boost::asio::error::operation_aborted)
return;
maybe_throw(ec, "accept");
std::thread{&sync_echo_http_server::do_client, this,
std::move(sock_), boost::asio::io_service::work{
sock_.get_io_service()}}.detach();
acceptor_.async_accept(sock_,
std::bind(&sync_echo_http_server::on_accept, this,
beast::asio::placeholders::error));
}
void
do_client(socket_type sock, boost::asio::io_service::work)
{
error_code ec;
streambuf rb;
for(;;)
{
request<string_body> req;
read(sock, rb, req, ec);
if(ec)
break;
response<string_body> resp(
{100, "OK", req.version});
resp.body = "Completed successfully.";
write(sock, resp, ec);
if(ec)
break;
}
}
};
class message_test : public beast::detail::unit_test::suite
{
public:
using error_code = boost::system::error_code;
using endpoint_type = boost::asio::ip::tcp::endpoint;
using address_type = boost::asio::ip::address;
using socket_type = boost::asio::ip::tcp::socket;
void
syncEcho(endpoint_type ep)
{
boost::asio::io_service ios;
socket_type sock(ios);
sock.connect(ep);
streambuf rb;
{
request<string_body> req(
{beast::http::method_t::http_get, "/", 11});
req.body = "Beast.HTTP";
req.headers.replace("Host",
ep.address().to_string() + ":" +
std::to_string(ep.port()));
write(sock, req);
}
{
response<string_body> m;
read(sock, rb, m);
}
}
void
testAsio()
{
endpoint_type ep{
address_type::from_string("127.0.0.1"), 6000};
sync_echo_http_server s(ep, *this);
syncEcho(ep);
}
void run() override
{
//testAsio();
pass();
}
};
BEAST_DEFINE_TESTSUITE(message,http,beast);
} // test
} // http
} // beast

View File

@@ -6,4 +6,4 @@
//
// Test that header file is self-contained.
#include <beast/asio/placeholders.h>
#include <beast/placeholders.hpp>

View File

@@ -6,18 +6,17 @@
//
// Test that header file is self-contained.
#include <beast/asio/prepare_buffers.h>
#include <beast/prepare_buffers.hpp>
#include <beast/asio/consuming_buffers.h>
#include <beast/unit_test/suite.h>
#include <beast/consuming_buffers.hpp>
#include <beast/detail/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <string>
namespace beast {
namespace asio {
namespace test {
class prepare_buffers_test : public unit_test::suite
class prepare_buffers_test : public beast::detail::unit_test::suite
{
public:
template<class ConstBufferSequence>
@@ -83,7 +82,7 @@ public:
cb.consume(1);
expect(buffer_size(cb) == 0);
expect(buffer_copy(cb, pb1) == 0);
auto pbc = prepare_buffers(2, cb);
expect(buffer_size(pbc) == 0);
expect(buffer_copy(pbc, cb) == 0);
@@ -99,5 +98,4 @@ public:
BEAST_DEFINE_TESTSUITE(prepare_buffers,asio,beast);
} // test
} // asio
} // beast

125
test/rfc2616.cpp Normal file
View File

@@ -0,0 +1,125 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <beast/http/rfc2616.hpp>
#include <beast/detail/unit_test/suite.hpp>
#include <string>
#include <vector>
namespace beast {
namespace rfc2616 {
namespace test {
class rfc2616_test : public beast::detail::unit_test::suite
{
public:
void
checkSplit(std::string const& s,
std::vector <std::string> const& expected)
{
auto const parsed = split_commas(s.begin(), s.end());
expect (parsed == expected);
}
void testSplit()
{
checkSplit("", {});
checkSplit(" ", {});
checkSplit(" ", {});
checkSplit("\t", {});
checkSplit(" \t ", {});
checkSplit(",", {});
checkSplit(",,", {});
checkSplit(" ,", {});
checkSplit(" , ,", {});
checkSplit("x", {"x"});
checkSplit(" x", {"x"});
checkSplit(" \t x", {"x"});
checkSplit("x ", {"x"});
checkSplit("x \t", {"x"});
checkSplit(" \t x \t ", {"x"});
checkSplit("\"\"", {});
checkSplit(" \"\"", {});
checkSplit("\"\" ", {});
checkSplit("\"x\"", {"x"});
checkSplit("\" \"", {" "});
checkSplit("\" x\"", {" x"});
checkSplit("\"x \"", {"x "});
checkSplit("\" x \"", {" x "});
checkSplit("\"\tx \"", {"\tx "});
checkSplit("x,y", {"x", "y"});
checkSplit("x ,\ty ", {"x", "y"});
checkSplit("x, y, z", {"x","y","z"});
checkSplit("x, \"y\", z", {"x","y","z"});
checkSplit(",,x,,\"y\",,", {"x","y"});
}
void
checkIter(std::string const& s,
std::vector<std::string> const& expected)
{
std::vector<std::string> got;
for(auto const& v : make_list(s))
got.emplace_back(v);
expect(got == expected);
}
void
testIter()
{
checkIter("x", {"x"});
checkIter(" x", {"x"});
checkIter("x\t", {"x"});
checkIter("\tx ", {"x"});
checkIter(",x", {"x"});
checkIter("x,", {"x"});
checkIter(",x,", {"x"});
checkIter(" , x\t,\t", {"x"});
checkIter("x,y", {"x", "y"});
checkIter("x, ,y ", {"x", "y"});
checkIter("\"x\"", {"x"});
}
void
testList()
{
expect(token_in_list("x", "x"));
expect(token_in_list("x,y", "x"));
expect(token_in_list("x,y", "y"));
expect(token_in_list("x, y ", "y"));
expect(token_in_list("x", "X"));
expect(token_in_list("Y", "y"));
expect(token_in_list("close, keepalive", "close"));
expect(token_in_list("close, keepalive", "keepalive"));
}
void
run()
{
testSplit();
testIter();
testList();
}
};
BEAST_DEFINE_TESTSUITE(rfc2616,http,beast);
} // test
} // rfc2616
} // beast

View File

@@ -6,17 +6,16 @@
//
// Test that header file is self-contained.
#include <beast/asio/static_streambuf.h>
#include <beast/static_streambuf.hpp>
#include <beast/unit_test/suite.h>
#include <beast/detail/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <string>
namespace beast {
namespace asio {
namespace test {
class static_streambuf_test : public unit_test::suite
class static_streambuf_test : public beast::detail::unit_test::suite
{
public:
template<class ConstBufferSequence>
@@ -109,5 +108,4 @@ public:
BEAST_DEFINE_TESTSUITE(static_streambuf,asio,beast);
} // test
} // asio
} // beast
} // beastp

View File

@@ -6,16 +6,15 @@
//
// Test that header file is self-contained.
#include <beast/asio/streambuf.h>
#include <beast/streambuf.hpp>
#include <beast/unit_test/suite.h>
#include <beast/detail/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <atomic>
#include <memory>
#include <string>
namespace beast {
namespace asio {
namespace test {
struct test_allocator_info
@@ -135,7 +134,7 @@ public:
}
};
class streambuf_test : public unit_test::suite
class streambuf_test : public beast::detail::unit_test::suite
{
public:
template<class ConstBufferSequence>
@@ -287,5 +286,4 @@ public:
BEAST_DEFINE_TESTSUITE(streambuf,asio,beast);
} // test
} // asio
} // beast

View File

@@ -6,4 +6,4 @@
//
// Test that header file is self-contained.
#include <beast/asio/streambuf_readstream.h>
#include <beast/streambuf_readstream.hpp>

View File

@@ -1,9 +0,0 @@
//
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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)
//
// Test that header file is self-contained.
#include <beast/asio/temp_buffer.h>

View File

@@ -6,4 +6,4 @@
//
// Test that header file is self-contained.
#include <beast/asio/type_check.h>
#include <beast/type_check.hpp>