1 // (C) Copyright 2010 Daniel James
2 // Distributed under the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
4 
5 #include <boost/iostreams/device/file_descriptor.hpp>
6 #include <boost/iostreams/stream.hpp>
7 #include <boost/test/unit_test.hpp>
8 #include "detail/temp_file.hpp"
9 #include "detail/verification.hpp"
10 #include "detail/file_handle.hpp"
11 
12 // Test file_descriptor with the depreacted constructors.
13 
14 namespace boost_ios = boost::iostreams;
15 namespace ios_test = boost::iostreams::test;
16 
17 template <class FileDescriptor>
file_handle_test_impl(FileDescriptor *)18 void file_handle_test_impl(FileDescriptor*)
19 {
20     ios_test::test_file  test1;
21     ios_test::test_file  test2;
22 
23     //--------------Deprecated file descriptor constructor--------------------//
24 
25     {
26         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
27         {
28             FileDescriptor device1(handle);
29             BOOST_CHECK(device1.handle() == handle);
30         }
31         BOOST_CHECK_HANDLE_OPEN(handle);
32         ios_test::close_file_handle(handle);
33     }
34 
35     {
36         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
37         {
38             FileDescriptor device1(handle, false);
39             BOOST_CHECK(device1.handle() == handle);
40         }
41         BOOST_CHECK_HANDLE_OPEN(handle);
42         ios_test::close_file_handle(handle);
43     }
44 
45     {
46         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
47         {
48             FileDescriptor device1(handle, true);
49             BOOST_CHECK(device1.handle() == handle);
50         }
51         BOOST_CHECK_HANDLE_CLOSED(handle);
52     }
53 
54     {
55         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
56         FileDescriptor device1(handle);
57         BOOST_CHECK(device1.handle() == handle);
58         device1.close();
59         BOOST_CHECK(!device1.is_open());
60         BOOST_CHECK_HANDLE_CLOSED(handle);
61     }
62 
63     {
64         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
65         FileDescriptor device1(handle, false);
66         BOOST_CHECK(device1.handle() == handle);
67         device1.close();
68         BOOST_CHECK(!device1.is_open());
69         BOOST_CHECK_HANDLE_CLOSED(handle);
70     }
71 
72     {
73         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
74         FileDescriptor device1(handle, true);
75         BOOST_CHECK(device1.handle() == handle);
76         device1.close();
77         BOOST_CHECK(!device1.is_open());
78         BOOST_CHECK_HANDLE_CLOSED(handle);
79     }
80 
81     //--------------Deprecated file descriptor open---------------------------//
82 
83     {
84         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
85         {
86             FileDescriptor device1;
87             device1.open(handle);
88             BOOST_CHECK(device1.handle() == handle);
89         }
90         BOOST_CHECK_HANDLE_OPEN(handle);
91         ios_test::close_file_handle(handle);
92     }
93 
94     {
95         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
96         {
97             FileDescriptor device1;
98             device1.open(handle, false);
99             BOOST_CHECK(device1.handle() == handle);
100         }
101         BOOST_CHECK_HANDLE_OPEN(handle);
102         ios_test::close_file_handle(handle);
103     }
104 
105     {
106         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
107         {
108             FileDescriptor device1;
109             device1.open(handle, true);
110             BOOST_CHECK(device1.handle() == handle);
111         }
112         BOOST_CHECK_HANDLE_CLOSED(handle);
113     }
114 
115     {
116         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
117         FileDescriptor device1;
118         device1.open(handle);
119         BOOST_CHECK(device1.handle() == handle);
120         device1.close();
121         BOOST_CHECK(!device1.is_open());
122         BOOST_CHECK_HANDLE_CLOSED(handle);
123     }
124 
125     {
126         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
127         FileDescriptor device1;
128         device1.open(handle, false);
129         BOOST_CHECK(device1.handle() == handle);
130         device1.close();
131         BOOST_CHECK(!device1.is_open());
132         BOOST_CHECK_HANDLE_CLOSED(handle);
133     }
134 
135     {
136         boost_ios::detail::file_handle handle = ios_test::open_file_handle(test1.name());
137         FileDescriptor device1;
138         device1.open(handle, true);
139         BOOST_CHECK(device1.handle() == handle);
140         device1.close();
141         BOOST_CHECK(!device1.is_open());
142         BOOST_CHECK_HANDLE_CLOSED(handle);
143     }
144 
145     //--------------Deprecated open with existing descriptor------------------//
146 
147     {
148         boost_ios::detail::file_handle handle1 = ios_test::open_file_handle(test1.name());
149         boost_ios::detail::file_handle handle2 = ios_test::open_file_handle(test1.name());
150 
151         {
152             FileDescriptor device1(handle1);
153             BOOST_CHECK(device1.handle() == handle1);
154             BOOST_CHECK_HANDLE_OPEN(handle1);
155             BOOST_CHECK_HANDLE_OPEN(handle2);
156             device1.open(handle2);
157             BOOST_CHECK(device1.handle() == handle2);
158             BOOST_CHECK_HANDLE_OPEN(handle1);
159             BOOST_CHECK_HANDLE_OPEN(handle2);
160             device1.close();
161             BOOST_CHECK_HANDLE_OPEN(handle1);
162             BOOST_CHECK_HANDLE_CLOSED(handle2);
163         }
164         BOOST_CHECK_HANDLE_OPEN(handle1);
165         BOOST_CHECK_HANDLE_CLOSED(handle2);
166 
167         ios_test::close_file_handle(handle1);
168     }
169 
170     {
171         boost_ios::detail::file_handle handle1 = ios_test::open_file_handle(test1.name());
172         boost_ios::detail::file_handle handle2 = ios_test::open_file_handle(test1.name());
173 
174         {
175             FileDescriptor device1(handle1, true);
176             BOOST_CHECK(device1.handle() == handle1);
177             BOOST_CHECK_HANDLE_OPEN(handle1);
178             BOOST_CHECK_HANDLE_OPEN(handle2);
179             device1.open(handle2);
180             BOOST_CHECK(device1.handle() == handle2);
181             BOOST_CHECK_HANDLE_CLOSED(handle1);
182             BOOST_CHECK_HANDLE_OPEN(handle2);
183             device1.close();
184             BOOST_CHECK_HANDLE_CLOSED(handle1);
185             BOOST_CHECK_HANDLE_CLOSED(handle2);
186         }
187         BOOST_CHECK_HANDLE_CLOSED(handle1);
188         BOOST_CHECK_HANDLE_CLOSED(handle2);
189     }
190 
191     {
192         boost_ios::detail::file_handle handle1 = ios_test::open_file_handle(test1.name());
193         boost_ios::detail::file_handle handle2 = ios_test::open_file_handle(test1.name());
194 
195         {
196             FileDescriptor device1(handle1, false);
197             BOOST_CHECK(device1.handle() == handle1);
198             BOOST_CHECK_HANDLE_OPEN(handle1);
199             BOOST_CHECK_HANDLE_OPEN(handle2);
200             device1.open(handle2, false);
201             BOOST_CHECK(device1.handle() == handle2);
202             BOOST_CHECK_HANDLE_OPEN(handle1);
203             BOOST_CHECK_HANDLE_OPEN(handle2);
204         }
205         BOOST_CHECK_HANDLE_OPEN(handle1);
206         BOOST_CHECK_HANDLE_OPEN(handle2);
207 
208         ios_test::close_file_handle(handle1);
209         ios_test::close_file_handle(handle2);
210     }
211 
212     {
213         boost_ios::detail::file_handle handle1 = ios_test::open_file_handle(test1.name());
214         boost_ios::detail::file_handle handle2 = ios_test::open_file_handle(test1.name());
215 
216         {
217             FileDescriptor device1(handle1, true);
218             BOOST_CHECK(device1.handle() == handle1);
219             BOOST_CHECK_HANDLE_OPEN(handle1);
220             BOOST_CHECK_HANDLE_OPEN(handle2);
221             device1.open(handle2, true);
222             BOOST_CHECK(device1.handle() == handle2);
223             BOOST_CHECK_HANDLE_CLOSED(handle1);
224             BOOST_CHECK_HANDLE_OPEN(handle2);
225         }
226         BOOST_CHECK_HANDLE_CLOSED(handle1);
227         BOOST_CHECK_HANDLE_CLOSED(handle2);
228     }
229 }
230 
deprecated_file_descriptor_test()231 void deprecated_file_descriptor_test()
232 {
233     file_handle_test_impl((boost_ios::file_descriptor*) 0);
234     file_handle_test_impl((boost_ios::file_descriptor_sink*) 0);
235     file_handle_test_impl((boost_ios::file_descriptor_source*) 0);
236 }
237 
init_unit_test_suite(int,char * [])238 boost::unit_test::test_suite* init_unit_test_suite(int, char* [])
239 {
240     boost::unit_test::test_suite* test = BOOST_TEST_SUITE("deprecated file_descriptor test");
241     test->add(BOOST_TEST_CASE(&deprecated_file_descriptor_test));
242     return test;
243 }
244