1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
// (C) Copyright Frank Birbacher 2007
// 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.)
// See http://www.boost.org/libs/iostreams for documentation.
#include <boost/config.hpp>
#include <boost/iostreams/categories.hpp> // tags.
#include <boost/iostreams/detail/ios.hpp> // openmode, seekdir, int types.
#include <boost/iostreams/detail/error.hpp>
#include <boost/iostreams/positioning.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
using boost::iostreams::detail::bad_read;
using boost::iostreams::detail::bad_seek;
using boost::iostreams::detail::bad_write;
using boost::iostreams::seekable_device_tag;
using boost::iostreams::stream;
using boost::iostreams::stream_offset;
using boost::unit_test::test_suite;
/*
* This test unit uses a custom device to trigger errors. The device supports
* input, output, and seek according to the SeekableDevice concept. And each
* of the required functions throw a special detail::bad_xxx exception. This
* should trigger the iostreams::stream to set the badbit status flag.
* Additionally the exception can be propagated to the caller if the exception
* mask of the stream allows exceptions.
*
* The stream offers four different functions: read, write, seekg, and seekp.
* Each of them is tested with three different error reporting concepts:
* test by reading status flags, test by propagated exception, and test by
* calling std::ios_base::exceptions when badbit is already set.
*
* In each case all of the status checking functions of a stream are checked.
*
* MSVCPRT (Visual Studio 2017, at least) does not perform exception
* handling in the seek methods (confirmed by inspecting sources).
*
* CYGWIN (with gcc-7.3.0) does not behave properly on the throw_delayed cases.
*/
//------------------Definition of error_device--------------------------------//
// Device whose member functions throw
struct error_device {
typedef char char_type;
typedef seekable_device_tag category;
error_device(char const*) {}
std::streamsize read(char_type*, std::streamsize)
{
throw bad_read();
}
std::streamsize write(const char_type*, std::streamsize)
{
throw bad_write();
}
std::streampos seek(stream_offset, BOOST_IOS::seekdir)
{
throw bad_seek();
}
};
typedef stream<error_device> test_stream;
//------------------Stream state tester---------------------------------------//
void check_stream_for_badbit(const std::iostream& str)
{
BOOST_CHECK_MESSAGE(!str.good(), "stream still good");
BOOST_CHECK_MESSAGE(!str.eof(), "eofbit set but not expected");
BOOST_CHECK_MESSAGE(str.bad(), "stream did not set badbit");
BOOST_CHECK_MESSAGE(str.fail(), "stream did not fail");
BOOST_CHECK_MESSAGE(str.operator ! (),
"stream does not report failure by operator !");
BOOST_CHECK_MESSAGE(false == static_cast<bool>(str),
"stream does not report failure by operator void* or bool");
}
//------------------Test case generators--------------------------------------//
template<void (*const function)(std::iostream&)>
struct wrap_nothrow {
static void execute()
{
test_stream stream("foo");
BOOST_CHECK_NO_THROW( function(stream) );
check_stream_for_badbit(stream);
}
};
template<void (*const function)(std::iostream&)>
struct wrap_throw {
static void execute()
{
typedef std::ios_base ios;
test_stream stream("foo");
stream.exceptions(ios::failbit | ios::badbit);
BOOST_CHECK_THROW( function(stream), std::exception );
check_stream_for_badbit(stream);
}
};
template<void (*const function)(std::iostream&)>
struct wrap_throw_delayed {
static void execute()
{
typedef std::ios_base ios;
test_stream stream("foo");
function(stream);
BOOST_CHECK_THROW(
stream.exceptions(ios::failbit | ios::badbit),
ios::failure
);
check_stream_for_badbit(stream);
}
};
//------------------Stream operations that throw------------------------------//
void test_read(std::iostream& str)
{
char data[10];
str.read(data, 10);
}
void test_write(std::iostream& str)
{
char data[10] = {0};
str.write(data, 10);
//force use of streambuf
str.flush();
}
void test_seekg(std::iostream& str)
{
str.seekg(10);
}
void test_seekp(std::iostream& str)
{
str.seekp(10);
}
test_suite* init_unit_test_suite(int, char* [])
{
test_suite* test = BOOST_TEST_SUITE("stream state test");
test->add(BOOST_TEST_CASE(&wrap_nothrow <&test_read>::execute));
test->add(BOOST_TEST_CASE(&wrap_throw <&test_read>::execute));
#ifndef __CYGWIN__
test->add(BOOST_TEST_CASE(&wrap_throw_delayed<&test_read>::execute));
#endif
test->add(BOOST_TEST_CASE(&wrap_nothrow <&test_write>::execute));
test->add(BOOST_TEST_CASE(&wrap_throw <&test_write>::execute));
#ifndef __CYGWIN__
test->add(BOOST_TEST_CASE(&wrap_throw_delayed<&test_write>::execute));
#endif
// MSSTL and libc++ don't handle exceptions from seekg correctly
#if !defined(_CPPLIB_VER) && !defined(_LIBCPP_VERSION)
test->add(BOOST_TEST_CASE(&wrap_nothrow <&test_seekg>::execute));
test->add(BOOST_TEST_CASE(&wrap_throw <&test_seekg>::execute));
#ifndef __CYGWIN__
test->add(BOOST_TEST_CASE(&wrap_throw_delayed<&test_seekg>::execute));
#endif
#endif // !defined(_CPPLIB_VER) && !defined(_LIBCPP_VERSION)
// Since C++11, seekp does not catch exceptions
#if 0
test->add(BOOST_TEST_CASE(&wrap_nothrow <&test_seekp>::execute));
test->add(BOOST_TEST_CASE(&wrap_throw <&test_seekp>::execute));
#ifndef __CYGWIN__
test->add(BOOST_TEST_CASE(&wrap_throw_delayed<&test_seekp>::execute));
#endif
#endif
return test;
}
|