Ticket #5115: test.cpp

File test.cpp, 1.6 KB (added by msuvajac@…, 11 years ago)

Buffer overwrite test case.

Line 
1#include <boost/iostreams/detail/adapter/non_blocking_adapter.hpp>
2#include <boost/iterator/counting_iterator.hpp>
3#include <boost/test/unit_test.hpp>
4#include <boost/iostreams/categories.hpp>
5
6#include <algorithm>
7
8// a toy source implementation that reads only one byte every time read is called
9class read_one_source_t
10{
11public:
12 typedef char char_type;
13
14 typedef boost::iostreams::source_tag category;
15
16 read_one_source_t() : pos_m(0)
17 {
18 // generate data that we can inspect later
19 std::copy(boost::counting_iterator<char>(0), boost::counting_iterator<char>(100), data_m);
20 }
21
22 std::streamsize read(char* s, std::streamsize n)
23 {
24 // bounds checking
25 if (pos_m >= 100 || n <= 0)
26 return -1;
27
28 // write a single char
29 *s = data_m[pos_m++];
30
31 // always return 1
32 return 1;
33 }
34
35private:
36 std::size_t pos_m;
37 char data_m[100];
38};
39
40void nonblocking_read_test()
41{
42 read_one_source_t src;
43 boost::iostreams::non_blocking_adapter<read_one_source_t> nb(src);
44
45 char read_data[100];
46
47 std::streamsize amt = boost::iostreams::read(nb, read_data, 100);
48
49 BOOST_CHECK_EQUAL(amt, 100);
50
51 for (int i = 0; i < 100; ++i)
52 {
53 BOOST_CHECK_EQUAL(std::char_traits<char>::to_int_type(read_data[i]), i);
54 }
55}
56
57boost::unit_test::test_suite* init_unit_test_suite(int, char* [])
58{
59 boost::unit_test::test_suite* test = BOOST_TEST_SUITE("non-blocking read test");
60 test->add(BOOST_TEST_CASE(&nonblocking_read_test));
61 return test;
62}