1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2004-2007 Jonathan Turkanis
3 // (C) Copyright 2012 Boris Schaeling
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 
7 // See http://www.boost.org/libs/iostreams for documentation.
8 
9 #include <windows.h>
10 #include <boost/iostreams/device/file_descriptor.hpp>
11 #include <boost/iostreams/filtering_stream.hpp>
12 #include <boost/iostreams/get.hpp>
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
15 
16 using namespace boost::iostreams;
17 using boost::unit_test::test_suite;
18 
read_from_file_descriptor_source_test()19 void read_from_file_descriptor_source_test()
20 {
21     HANDLE handles[2];
22     ::CreatePipe(&handles[0], &handles[1], NULL, 0);
23 
24     char buffer[2] = { 'a', 'b' };
25     DWORD written;
26     ::WriteFile(handles[1], buffer, 2, &written, NULL);
27     ::CloseHandle(handles[1]);
28 
29     file_descriptor_source is(handles[0], close_handle);
30 
31     BOOST_CHECK_EQUAL('a', get(is));
32     BOOST_CHECK_EQUAL('b', get(is));
33     BOOST_CHECK_EQUAL(-1, get(is));
34     BOOST_CHECK_EQUAL(-1, get(is));
35 }
36 
read_from_filtering_istream_test()37 void read_from_filtering_istream_test()
38 {
39     HANDLE handles[2];
40     ::CreatePipe(&handles[0], &handles[1], NULL, 0);
41 
42     char buffer[2] = { 'a', 'b' };
43     DWORD written;
44     ::WriteFile(handles[1], buffer, 2, &written, NULL);
45     ::CloseHandle(handles[1]);
46 
47     file_descriptor_source source(handles[0], close_handle);
48     filtering_istream is;
49     is.push(source);
50 
51     BOOST_CHECK_EQUAL('a', get(is));
52     BOOST_CHECK_EQUAL('b', get(is));
53     BOOST_CHECK_EQUAL(-1, get(is));
54     BOOST_CHECK_EQUAL(-1, get(is));
55 }
56 
init_unit_test_suite(int,char * [])57 test_suite* init_unit_test_suite(int, char* [])
58 {
59     test_suite* test = BOOST_TEST_SUITE("Windows pipe test");
60     test->add(BOOST_TEST_CASE(&read_from_file_descriptor_source_test));
61     test->add(BOOST_TEST_CASE(&read_from_filtering_istream_test));
62     return test;
63 }
64