//
// read_at.cpp
// ~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff 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)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include "asio/read_at.hpp"
#include <cstring>
#include "archetypes/async_result.hpp"
#include "asio/io_context.hpp"
#include "asio/post.hpp"
#include "asio/streambuf.hpp"
#include "unit_test.hpp"
#if defined(ASIO_HAS_BOOST_BIND)
# include <boost/bind/bind.hpp>
#else // defined(ASIO_HAS_BOOST_BIND)
# include <functional>
#endif // defined(ASIO_HAS_BOOST_BIND)
#if defined(ASIO_HAS_BOOST_ARRAY)
#include <boost/array.hpp>
#endif // defined(ASIO_HAS_BOOST_ARRAY)
#if defined(ASIO_HAS_STD_ARRAY)
# include <array>
#endif // defined(ASIO_HAS_STD_ARRAY)
using namespace std; // For memcmp, memcpy and memset.
class test_random_access_device
{
public:
typedef asio::io_context::executor_type executor_type;
test_random_access_device(asio::io_context& io_context)
: io_context_(io_context),
length_(0),
next_read_length_(0)
{
}
executor_type get_executor() ASIO_NOEXCEPT
{
return io_context_.get_executor();
}
void reset(const void* data, size_t length)
{
ASIO_CHECK(length <= max_length);
length_ = 0;
while (length_ + length < max_length)
{
memcpy(data_ + length_, data, length);
length_ += length;
}
next_read_length_ = length;
}
void next_read_length(size_t length)
{
next_read_length_ = length;
}
template <typename Iterator>
bool check_buffers(asio::uint64_t offset,
Iterator begin, Iterator end, size_t length)
{
if (offset + length > max_length)
return false;
Iterator iter = begin;
size_t checked_length = 0;
for (; iter != end && checked_length < length; ++iter)
{
size_t buffer_length = asio::buffer_size(*iter);
if (buffer_length > length - checked_length)
buffer_length = length - checked_length;
if (memcmp(data_ + offset + checked_length,
iter->data(), buffer_length) != 0)
return false;
checked_length += buffer_length;
}
return true;
}
template <typename Const_Buffers>
bool check_buffers(asio::uint64_t offset,
const Const_Buffers& buffers, size_t length)
{
return check_buffers(offset, asio::buffer_sequence_begin(buffers),
asio::buffer_sequence_end(buffers), length);
}
template <typename Mutable_Buffers>
size_t read_some_at(asio::uint64_t offset,
const Mutable_Buffers& buffers)
{
return asio::buffer_copy(buffers,
asio::buffer(data_, length_) + offset,
next_read_length_);
}
template <typename Mutable_Buffers>
size_t read_some_at(asio::uint64_t offset,
const Mutable_Buffers& buffers, asio::error_code& ec)
{
ec = asio::error_code();
return read_some_at(offset, buffers);
}
template <typename Mutable_Buffers, typename Handler>
void async_read_some_at(asio::uint64_t offset,
const Mutable_Buffers& buffers, ASIO_MOVE_ARG(Handler) handler)
{
size_t bytes_transferred = read_some_at(offset, buffers);
asio::post(get_executor(),
asio::detail::bind_handler(
ASIO_MOVE_CAST(Handler)(handler),
asio::error_code(), bytes_transferred));
}
private:
asio::io_context& io_context_;
enum { max_length = 8192 };
char data_[max_length];
size_t length_;
size_t next_read_length_;
};
static const char read_data[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
void test_3_arg_mutable_buffer_read_at()
{
asio::io_context ioc;
test_random_access_device s(ioc);
char read_buf[sizeof(read_data)];
asio::mutable_buffer buffers
= asio::buffer(read_buf, sizeof(read_buf));
s.reset(read_data, sizeof(read_data));
memset(read_buf, 0, sizeof(read_buf));
size_t bytes_transferred = asio::read_at(s, 0, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(0, buffers, sizeof(read_data)));
s.reset(read_data, sizeof(read_data));
memset(read_buf, 0, sizeof(read_buf));
bytes_transferred = asio::read_at(s, 1234, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(1234, buffers, sizeof(read_data)));
s.reset(read_data, sizeof(read_data));
s.next_read_length(1);
memset(read_buf, 0, sizeof(read_buf));
bytes_transferred = asio::read_at(s, 0, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(0, buffers, sizeof(read_data)));
s.reset(read_data, sizeof(read_data));
s.next_read_length(1);
memset(read_buf, 0, sizeof(read_buf));
bytes_transferred = asio::read_at(s, 1234, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(1234, buffers, sizeof(read_data)));
s.reset(read_data, sizeof(read_data));
s.next_read_length(10);
memset(read_buf, 0, sizeof(read_buf));
bytes_transferred = asio::read_at(s, 0, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(0, buffers, sizeof(read_data)));
s.reset(read_data, sizeof(read_data));
s.next_read_length(10);
memset(read_buf, 0, sizeof(read_buf));
bytes_transferred = asio::read_at(s, 1234, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(1234, buffers, sizeof(read_data)));
}
void test_3_arg_vector_buffers_read_at()
{
asio::io_context ioc;
test_random_access_device s(ioc);
char read_buf[sizeof(read_data)];
std::vector<asio::mutable_buffer> buffers;
buffers.push_back(asio::buffer(read_buf, 32));
buffers.push_back(asio::buffer(read_buf) + 32);
s.reset(read_data, sizeof(read_data));
memset(read_buf, 0, sizeof(read_buf));
size_t bytes_transferred = asio::read_at(s, 0, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(0, buffers, sizeof(read_data)));
s.reset(read_data, sizeof(read_data));
memset(read_buf, 0, sizeof(read_buf));
bytes_transferred = asio::read_at(s, 1234, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(1234, buffers, sizeof(read_data)));
s.reset(read_data, sizeof(read_data));
s.next_read_length(1);
memset(read_buf, 0, sizeof(read_buf));
bytes_transferred = asio::read_at(s, 0, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(0, buffers, sizeof(read_data)));
s.reset(read_data, sizeof(read_data));
s.next_read_length(1);
memset(read_buf, 0, sizeof(read_buf));
bytes_transferred = asio::read_at(s, 1234, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(1234, buffers, sizeof(read_data)));
s.reset(read_data, sizeof(read_data));
s.next_read_length(10);
memset(read_buf, 0, sizeof(read_buf));
bytes_transferred = asio::read_at(s, 0, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(0, buffers, sizeof(read_data)));
s.reset(read_data, sizeof(read_data));
s.next_read_length(10);
memset(read_buf, 0, sizeof(read_buf));
bytes_transferred = asio::read_at(s, 1234, buffers);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(s.check_buffers(1234, buffers, sizeof(read_data)));
}
void test_3_arg_streambuf_read_at()
{
asio::io_context ioc;
test_random_access_device s(ioc);
asio::streambuf sb(sizeof(read_data));
s.reset(read_data, sizeof(read_data));
sb.consume(sb.size());
size_t bytes_transferred = asio::read_at(s, 0, sb);
ASIO_CHECK(bytes_transferred == sizeof(read_data));
ASIO_CHECK(sb.size() == sizeof(read_data));
ASIO_CHECK(s.check_buffers(0, sb.data(), sizeof(read_data)));
s.reset(read_data, s
评论0