]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/iostreams/test/file_descriptor_test.cpp
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2004-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 // See http://www.boost.org/libs/iostreams for documentation.
10 #include <boost/iostreams/device/file_descriptor.hpp>
11 #include <boost/iostreams/stream.hpp>
12 #include <boost/test/test_tools.hpp>
13 #include <boost/test/unit_test.hpp>
14 #include "detail/temp_file.hpp"
15 #include "detail/verification.hpp"
16 #include "detail/file_handle.hpp"
18 using namespace boost
;
19 using namespace boost::iostreams
;
20 using namespace boost::iostreams::test
;
21 namespace boost_ios
= boost::iostreams
;
23 using boost::unit_test::test_suite
;
25 void file_descriptor_test()
28 typedef stream
<file_descriptor_source
> fdistream
;
29 typedef stream
<file_descriptor_sink
> fdostream
;
30 typedef stream
<file_descriptor
> fdstream
;
35 //--------------Test file_descriptor_source-------------------------------//
38 fdistream
first(file_descriptor_source(test1
.name()), 0);
39 ifstream
second(test2
.name().c_str());
40 BOOST_CHECK(first
->is_open());
42 compare_streams_in_chars(first
, second
),
43 "failed reading from file_descriptor_source in chars with no buffer"
46 BOOST_CHECK(!first
->is_open());
50 fdistream
first(file_descriptor_source(test1
.name()), 0);
51 ifstream
second(test2
.name().c_str());
52 BOOST_CHECK(first
->is_open());
54 compare_streams_in_chunks(first
, second
),
55 "failed reading from file_descriptor_source in chunks with no buffer"
58 BOOST_CHECK(!first
->is_open());
62 file_descriptor_source
file(test1
.name());
63 fdistream
first(file
);
64 ifstream
second(test2
.name().c_str());
65 BOOST_CHECK(first
->is_open());
67 compare_streams_in_chars(first
, second
),
68 "failed reading from file_descriptor_source in chars with buffer"
71 BOOST_CHECK(!first
->is_open());
75 file_descriptor_source
file(test1
.name());
76 fdistream
first(file
);
77 ifstream
second(test2
.name().c_str());
78 BOOST_CHECK(first
->is_open());
80 compare_streams_in_chunks(first
, second
),
81 "failed reading from file_descriptor_source in chunks with buffer"
84 BOOST_CHECK(!first
->is_open());
87 // test illegal flag combinations
90 file_descriptor_source(test1
.name(),
92 BOOST_IOSTREAMS_FAILURE
);
94 file_descriptor_source(test1
.name(),
95 BOOST_IOS::app
| BOOST_IOS::trunc
),
96 BOOST_IOSTREAMS_FAILURE
);
98 file_descriptor_source(test1
.name(),
100 BOOST_IOSTREAMS_FAILURE
);
102 file_descriptor_source(test1
.name(),
103 BOOST_IOS::out
| BOOST_IOS::app
),
104 BOOST_IOSTREAMS_FAILURE
);
106 file_descriptor_source(test1
.name(),
107 BOOST_IOS::out
| BOOST_IOS::trunc
),
108 BOOST_IOSTREAMS_FAILURE
);
110 file_descriptor_source(test1
.name(),
111 BOOST_IOS::out
| BOOST_IOS::app
| BOOST_IOS::trunc
),
112 BOOST_IOSTREAMS_FAILURE
);
115 //--------------Test file_descriptor_sink---------------------------------//
119 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
120 fdostream
out(file
, 0);
121 BOOST_CHECK(out
->is_open());
122 write_data_in_chars(out
);
125 compare_files(test1
.name(), temp
.name()),
126 "failed writing to file_descriptor_sink in chars with no buffer"
129 BOOST_CHECK(!file
.is_open());
134 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
135 fdostream
out(file
, 0);
136 BOOST_CHECK(out
->is_open());
137 write_data_in_chunks(out
);
140 compare_files(test1
.name(), temp
.name()),
141 "failed writing to file_descriptor_sink in chunks with no buffer"
144 BOOST_CHECK(!file
.is_open());
149 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
151 BOOST_CHECK(out
->is_open());
152 write_data_in_chars(out
);
155 compare_files(test1
.name(), temp
.name()),
156 "failed writing to file_descriptor_sink in chars with buffer"
159 BOOST_CHECK(!file
.is_open());
164 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
166 BOOST_CHECK(out
->is_open());
167 write_data_in_chunks(out
);
170 compare_files(test1
.name(), temp
.name()),
171 "failed writing to file_descriptor_sink in chunks with buffer"
174 BOOST_CHECK(!file
.is_open());
181 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
183 write_data_in_chunks(out
);
187 // test std::ios_base::app
189 file_descriptor_sink
file(temp
.name(), BOOST_IOS::app
);
191 BOOST_CHECK(out
->is_open());
192 write_data_in_chars(out
);
194 std::string
expected(narrow_data());
195 expected
+= narrow_data();
197 compare_container_and_file(expected
, temp
.name()),
198 "failed writing to file_descriptor_sink in append mode"
201 BOOST_CHECK(!file
.is_open());
203 // test std::ios_base::trunc
205 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
207 BOOST_CHECK(out
->is_open());
208 write_data_in_chars(out
);
211 compare_files(test1
.name(), temp
.name()),
212 "failed writing to file_descriptor_sink in trunc mode"
215 BOOST_CHECK(!file
.is_open());
218 // test illegal flag combinations
221 file_descriptor_sink(temp
.name(),
222 BOOST_IOS::trunc
| BOOST_IOS::app
),
223 BOOST_IOSTREAMS_FAILURE
);
225 file_descriptor_sink(temp
.name(),
227 BOOST_IOSTREAMS_FAILURE
);
229 file_descriptor_sink(temp
.name(),
230 BOOST_IOS::in
| BOOST_IOS::app
),
231 BOOST_IOSTREAMS_FAILURE
);
233 file_descriptor_sink(temp
.name(),
234 BOOST_IOS::in
| BOOST_IOS::trunc
),
235 BOOST_IOSTREAMS_FAILURE
);
237 file_descriptor_sink(temp
.name(),
238 BOOST_IOS::in
| BOOST_IOS::trunc
| BOOST_IOS::app
),
239 BOOST_IOSTREAMS_FAILURE
);
243 //--Test seeking with file_descriptor_source and file_descriptor_sink-----//
247 file_descriptor_sink
sink(test3
.name());
249 BOOST_CHECK(out
->is_open());
251 test_output_seekable(out
),
252 "failed seeking within a file_descriptor_sink"
255 BOOST_CHECK(!out
->is_open());
257 file_descriptor_source
source(test3
.name());
258 fdistream
in(source
);
259 BOOST_CHECK(in
->is_open());
261 test_input_seekable(in
),
262 "failed seeking within a file_descriptor_source"
265 BOOST_CHECK(!in
->is_open());
268 //--------------Test file_descriptor--------------------------------------//
272 file_descriptor
file( temp
.name(),
277 fdstream
io(file
, BUFSIZ
);
279 test_seekable_in_chars(io
),
280 "failed seeking within a file_descriptor, in chars"
286 file_descriptor
file( temp
.name(),
291 fdstream
io(file
, BUFSIZ
);
293 test_seekable_in_chunks(io
),
294 "failed seeking within a file_descriptor, in chunks"
298 //--------------Test read-only file_descriptor----------------------------//
301 fdstream
first(file_descriptor(test1
.name(), BOOST_IOS::in
), 0);
302 ifstream
second(test2
.name().c_str());
303 BOOST_CHECK(first
->is_open());
304 write_data_in_chars(first
);
305 BOOST_CHECK(first
.fail());
308 compare_streams_in_chars(first
, second
),
309 "failed reading from file_descriptor in chars with no buffer"
312 BOOST_CHECK(!first
->is_open());
316 fdstream
first(file_descriptor(test1
.name(), BOOST_IOS::in
), 0);
317 ifstream
second(test2
.name().c_str());
318 BOOST_CHECK(first
->is_open());
319 write_data_in_chunks(first
);
320 BOOST_CHECK(first
.fail());
323 compare_streams_in_chunks(first
, second
),
324 "failed reading from file_descriptor in chunks with no buffer"
327 BOOST_CHECK(!first
->is_open());
331 file_descriptor
file(test1
.name(), BOOST_IOS::in
);
332 fdstream
first(file
);
333 ifstream
second(test2
.name().c_str());
334 BOOST_CHECK(first
->is_open());
335 write_data_in_chars(first
);
336 BOOST_CHECK(first
.fail());
338 first
.seekg(0, BOOST_IOS::beg
);
340 compare_streams_in_chars(first
, second
),
341 "failed reading from file_descriptor in chars with buffer"
344 BOOST_CHECK(!first
->is_open());
348 file_descriptor
file(test1
.name(), BOOST_IOS::in
);
349 fdstream
first(file
);
350 ifstream
second(test2
.name().c_str());
351 BOOST_CHECK(first
->is_open());
352 write_data_in_chunks(first
);
353 BOOST_CHECK(first
.fail());
355 first
.seekg(0, BOOST_IOS::beg
);
357 compare_streams_in_chunks(first
, second
),
358 "failed reading from file_descriptor in chunks with buffer"
361 BOOST_CHECK(!first
->is_open());
364 //--------------Test write-only file_descriptor---------------------------//
367 file_descriptor
file( temp
.name(),
370 fdstream
out(file
, 0);
371 BOOST_CHECK(out
->is_open());
373 BOOST_CHECK(out
.fail());
375 write_data_in_chars(out
);
376 out
.seekg(0, BOOST_IOS::beg
);
378 BOOST_CHECK(out
.fail());
382 compare_files(test1
.name(), temp
.name()),
383 "failed writing to file_descriptor in chars with no buffer"
386 BOOST_CHECK(!file
.is_open());
391 file_descriptor
file( temp
.name(),
394 fdstream
out(file
, 0);
395 BOOST_CHECK(out
->is_open());
397 BOOST_CHECK(out
.fail());
399 write_data_in_chunks(out
);
400 out
.seekg(0, BOOST_IOS::beg
);
402 BOOST_CHECK(out
.fail());
406 compare_files(test1
.name(), temp
.name()),
407 "failed writing to file_descriptor_sink in chunks with no buffer"
410 BOOST_CHECK(!file
.is_open());
415 file_descriptor
file( temp
.name(),
419 BOOST_CHECK(out
->is_open());
421 BOOST_CHECK(out
.fail());
423 write_data_in_chars(out
);
424 out
.seekg(0, BOOST_IOS::beg
);
426 BOOST_CHECK(out
.fail());
430 compare_files(test1
.name(), temp
.name()),
431 "failed writing to file_descriptor_sink in chars with buffer"
434 BOOST_CHECK(!file
.is_open());
439 file_descriptor
file( temp
.name(),
443 BOOST_CHECK(out
->is_open());
445 BOOST_CHECK(out
.fail());
447 write_data_in_chunks(out
);
448 out
.seekg(0, BOOST_IOS::beg
);
450 BOOST_CHECK(out
.fail());
454 compare_files(test1
.name(), temp
.name()),
455 "failed writing to file_descriptor_sink in chunks with buffer"
458 BOOST_CHECK(!file
.is_open());
461 // test illegal flag combinations
464 file_descriptor(test1
.name(),
465 BOOST_IOS::openmode(0)),
466 BOOST_IOSTREAMS_FAILURE
);
468 file_descriptor(test1
.name(),
470 BOOST_IOSTREAMS_FAILURE
);
472 file_descriptor(test1
.name(),
473 BOOST_IOS::app
| BOOST_IOS::trunc
),
474 BOOST_IOSTREAMS_FAILURE
);
476 file_descriptor(test1
.name(),
477 BOOST_IOS::in
| BOOST_IOS::trunc
),
478 BOOST_IOSTREAMS_FAILURE
);
480 file_descriptor(test1
.name(),
481 BOOST_IOS::in
| BOOST_IOS::app
| BOOST_IOS::trunc
),
482 BOOST_IOSTREAMS_FAILURE
);
484 file_descriptor(test1
.name(),
485 BOOST_IOS::out
| BOOST_IOS::app
| BOOST_IOS::trunc
),
486 BOOST_IOSTREAMS_FAILURE
);
488 file_descriptor(test1
.name(),
493 BOOST_IOSTREAMS_FAILURE
);
497 template <class FileDescriptor
>
498 void file_handle_test_impl(FileDescriptor
*)
504 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
506 FileDescriptor
device1(handle
, boost_ios::never_close_handle
);
507 BOOST_CHECK(device1
.handle() == handle
);
509 BOOST_CHECK_HANDLE_OPEN(handle
);
510 close_file_handle(handle
);
514 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
516 FileDescriptor
device1(handle
, boost_ios::close_handle
);
517 BOOST_CHECK(device1
.handle() == handle
);
519 BOOST_CHECK_HANDLE_CLOSED(handle
);
523 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
524 FileDescriptor
device1(handle
, boost_ios::never_close_handle
);
525 BOOST_CHECK(device1
.handle() == handle
);
527 BOOST_CHECK(!device1
.is_open());
528 BOOST_CHECK_HANDLE_OPEN(handle
);
529 close_file_handle(handle
);
533 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
534 FileDescriptor
device1(handle
, boost_ios::close_handle
);
535 BOOST_CHECK(device1
.handle() == handle
);
537 BOOST_CHECK(!device1
.is_open());
538 BOOST_CHECK_HANDLE_CLOSED(handle
);
542 boost_ios::detail::file_handle handle1
= open_file_handle(test1
.name());
543 boost_ios::detail::file_handle handle2
= open_file_handle(test2
.name());
545 FileDescriptor
device1(handle1
, boost_ios::never_close_handle
);
546 BOOST_CHECK(device1
.handle() == handle1
);
547 device1
.open(handle2
, boost_ios::never_close_handle
);
548 BOOST_CHECK(device1
.handle() == handle2
);
550 BOOST_CHECK_HANDLE_OPEN(handle1
);
551 BOOST_CHECK_HANDLE_OPEN(handle2
);
552 close_file_handle(handle1
);
553 close_file_handle(handle2
);
557 boost_ios::detail::file_handle handle1
= open_file_handle(test1
.name());
558 boost_ios::detail::file_handle handle2
= open_file_handle(test2
.name());
560 FileDescriptor
device1(handle1
, boost_ios::close_handle
);
561 BOOST_CHECK(device1
.handle() == handle1
);
562 device1
.open(handle2
, boost_ios::close_handle
);
563 BOOST_CHECK(device1
.handle() == handle2
);
564 BOOST_CHECK_HANDLE_CLOSED(handle1
);
565 BOOST_CHECK_HANDLE_OPEN(handle2
);
567 BOOST_CHECK_HANDLE_CLOSED(handle1
);
568 BOOST_CHECK_HANDLE_CLOSED(handle2
);
572 boost_ios::detail::file_handle handle1
= open_file_handle(test1
.name());
573 boost_ios::detail::file_handle handle2
= open_file_handle(test2
.name());
575 FileDescriptor
device1(handle1
, boost_ios::close_handle
);
576 BOOST_CHECK(device1
.handle() == handle1
);
577 device1
.open(handle2
, boost_ios::never_close_handle
);
578 BOOST_CHECK(device1
.handle() == handle2
);
579 BOOST_CHECK_HANDLE_CLOSED(handle1
);
580 BOOST_CHECK_HANDLE_OPEN(handle2
);
582 BOOST_CHECK_HANDLE_CLOSED(handle1
);
583 BOOST_CHECK_HANDLE_OPEN(handle2
);
584 close_file_handle(handle2
);
588 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
590 FileDescriptor device1
;
591 BOOST_CHECK(!device1
.is_open());
592 device1
.open(handle
, boost_ios::never_close_handle
);
593 BOOST_CHECK(device1
.handle() == handle
);
594 BOOST_CHECK_HANDLE_OPEN(handle
);
596 BOOST_CHECK_HANDLE_OPEN(handle
);
597 close_file_handle(handle
);
601 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
603 FileDescriptor device1
;
604 BOOST_CHECK(!device1
.is_open());
605 device1
.open(handle
, boost_ios::close_handle
);
606 BOOST_CHECK(device1
.handle() == handle
);
607 BOOST_CHECK_HANDLE_OPEN(handle
);
609 BOOST_CHECK_HANDLE_CLOSED(handle
);
613 void file_handle_test()
615 file_handle_test_impl((boost_ios::file_descriptor
*) 0);
616 file_handle_test_impl((boost_ios::file_descriptor_source
*) 0);
617 file_handle_test_impl((boost_ios::file_descriptor_sink
*) 0);
620 test_suite
* init_unit_test_suite(int, char* [])
622 test_suite
* test
= BOOST_TEST_SUITE("file_descriptor test");
623 test
->add(BOOST_TEST_CASE(&file_descriptor_test
));
624 test
->add(BOOST_TEST_CASE(&file_handle_test
));