]>
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(),
96 BOOST_IOSTREAMS_FAILURE
);
98 file_descriptor_source(test1
.name(),
99 BOOST_IOS::app
| BOOST_IOS::trunc
),
100 BOOST_IOSTREAMS_FAILURE
);
102 file_descriptor_source(test1
.name(),
104 BOOST_IOSTREAMS_FAILURE
);
106 file_descriptor_source(test1
.name(),
107 BOOST_IOS::out
| BOOST_IOS::app
),
108 BOOST_IOSTREAMS_FAILURE
);
110 file_descriptor_source(test1
.name(),
111 BOOST_IOS::out
| BOOST_IOS::trunc
),
112 BOOST_IOSTREAMS_FAILURE
);
114 file_descriptor_source(test1
.name(),
115 BOOST_IOS::out
| BOOST_IOS::app
| BOOST_IOS::trunc
),
116 BOOST_IOSTREAMS_FAILURE
);
119 //--------------Test file_descriptor_sink---------------------------------//
123 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
124 fdostream
out(file
, 0);
125 BOOST_CHECK(out
->is_open());
126 write_data_in_chars(out
);
129 compare_files(test1
.name(), temp
.name()),
130 "failed writing to file_descriptor_sink in chars with no buffer"
133 BOOST_CHECK(!file
.is_open());
138 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
139 fdostream
out(file
, 0);
140 BOOST_CHECK(out
->is_open());
141 write_data_in_chunks(out
);
144 compare_files(test1
.name(), temp
.name()),
145 "failed writing to file_descriptor_sink in chunks with no buffer"
148 BOOST_CHECK(!file
.is_open());
153 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
155 BOOST_CHECK(out
->is_open());
156 write_data_in_chars(out
);
159 compare_files(test1
.name(), temp
.name()),
160 "failed writing to file_descriptor_sink in chars with buffer"
163 BOOST_CHECK(!file
.is_open());
168 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
170 BOOST_CHECK(out
->is_open());
171 write_data_in_chunks(out
);
174 compare_files(test1
.name(), temp
.name()),
175 "failed writing to file_descriptor_sink in chunks with buffer"
178 BOOST_CHECK(!file
.is_open());
185 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
187 write_data_in_chunks(out
);
191 // test std::ios_base::app
193 file_descriptor_sink
file(temp
.name(), BOOST_IOS::app
);
195 BOOST_CHECK(out
->is_open());
196 write_data_in_chars(out
);
198 std::string
expected(narrow_data());
199 expected
+= narrow_data();
201 compare_container_and_file(expected
, temp
.name()),
202 "failed writing to file_descriptor_sink in append mode"
205 BOOST_CHECK(!file
.is_open());
207 // test std::ios_base::trunc
209 file_descriptor_sink
file(temp
.name(), BOOST_IOS::trunc
);
211 BOOST_CHECK(out
->is_open());
212 write_data_in_chars(out
);
215 compare_files(test1
.name(), temp
.name()),
216 "failed writing to file_descriptor_sink in trunc mode"
219 BOOST_CHECK(!file
.is_open());
222 // test illegal flag combinations
225 file_descriptor_sink(temp
.name(),
226 BOOST_IOS::trunc
| BOOST_IOS::app
),
227 BOOST_IOSTREAMS_FAILURE
);
229 file_descriptor_sink(temp
.name(),
231 BOOST_IOSTREAMS_FAILURE
);
233 file_descriptor_sink(temp
.name(),
234 BOOST_IOS::in
| BOOST_IOS::app
),
235 BOOST_IOSTREAMS_FAILURE
);
237 file_descriptor_sink(temp
.name(),
238 BOOST_IOS::in
| BOOST_IOS::trunc
),
239 BOOST_IOSTREAMS_FAILURE
);
241 file_descriptor_sink(temp
.name(),
242 BOOST_IOS::in
| BOOST_IOS::trunc
| BOOST_IOS::app
),
243 BOOST_IOSTREAMS_FAILURE
);
247 //--Test seeking with file_descriptor_source and file_descriptor_sink-----//
251 file_descriptor_sink
sink(test3
.name());
253 BOOST_CHECK(out
->is_open());
255 test_output_seekable(out
),
256 "failed seeking within a file_descriptor_sink"
259 BOOST_CHECK(!out
->is_open());
261 file_descriptor_source
source(test3
.name());
262 fdistream
in(source
);
263 BOOST_CHECK(in
->is_open());
265 test_input_seekable(in
),
266 "failed seeking within a file_descriptor_source"
269 BOOST_CHECK(!in
->is_open());
272 //--------------Test file_descriptor--------------------------------------//
276 file_descriptor
file( temp
.name(),
281 fdstream
io(file
, BUFSIZ
);
283 test_seekable_in_chars(io
),
284 "failed seeking within a file_descriptor, in chars"
290 file_descriptor
file( temp
.name(),
295 fdstream
io(file
, BUFSIZ
);
297 test_seekable_in_chunks(io
),
298 "failed seeking within a file_descriptor, in chunks"
302 //--------------Test read-only file_descriptor----------------------------//
305 fdstream
first(file_descriptor(test1
.name(), BOOST_IOS::in
), 0);
306 ifstream
second(test2
.name().c_str());
307 BOOST_CHECK(first
->is_open());
308 write_data_in_chars(first
);
309 BOOST_CHECK(first
.fail());
312 compare_streams_in_chars(first
, second
),
313 "failed reading from file_descriptor in chars with no buffer"
316 BOOST_CHECK(!first
->is_open());
320 fdstream
first(file_descriptor(test1
.name(), BOOST_IOS::in
), 0);
321 ifstream
second(test2
.name().c_str());
322 BOOST_CHECK(first
->is_open());
323 write_data_in_chunks(first
);
324 BOOST_CHECK(first
.fail());
327 compare_streams_in_chunks(first
, second
),
328 "failed reading from file_descriptor in chunks with no buffer"
331 BOOST_CHECK(!first
->is_open());
335 file_descriptor
file(test1
.name(), BOOST_IOS::in
);
336 fdstream
first(file
);
337 ifstream
second(test2
.name().c_str());
338 BOOST_CHECK(first
->is_open());
339 write_data_in_chars(first
);
340 BOOST_CHECK(first
.fail());
342 first
.seekg(0, BOOST_IOS::beg
);
344 compare_streams_in_chars(first
, second
),
345 "failed reading from file_descriptor in chars with buffer"
348 BOOST_CHECK(!first
->is_open());
352 file_descriptor
file(test1
.name(), BOOST_IOS::in
);
353 fdstream
first(file
);
354 ifstream
second(test2
.name().c_str());
355 BOOST_CHECK(first
->is_open());
356 write_data_in_chunks(first
);
357 BOOST_CHECK(first
.fail());
359 first
.seekg(0, BOOST_IOS::beg
);
361 compare_streams_in_chunks(first
, second
),
362 "failed reading from file_descriptor in chunks with buffer"
365 BOOST_CHECK(!first
->is_open());
368 //--------------Test write-only file_descriptor---------------------------//
371 file_descriptor
file( temp
.name(),
374 fdstream
out(file
, 0);
375 BOOST_CHECK(out
->is_open());
377 BOOST_CHECK(out
.fail());
379 write_data_in_chars(out
);
380 out
.seekg(0, BOOST_IOS::beg
);
382 BOOST_CHECK(out
.fail());
386 compare_files(test1
.name(), temp
.name()),
387 "failed writing to file_descriptor in chars with no buffer"
390 BOOST_CHECK(!file
.is_open());
395 file_descriptor
file( temp
.name(),
398 fdstream
out(file
, 0);
399 BOOST_CHECK(out
->is_open());
401 BOOST_CHECK(out
.fail());
403 write_data_in_chunks(out
);
404 out
.seekg(0, BOOST_IOS::beg
);
406 BOOST_CHECK(out
.fail());
410 compare_files(test1
.name(), temp
.name()),
411 "failed writing to file_descriptor_sink in chunks with no buffer"
414 BOOST_CHECK(!file
.is_open());
419 file_descriptor
file( temp
.name(),
423 BOOST_CHECK(out
->is_open());
425 BOOST_CHECK(out
.fail());
427 write_data_in_chars(out
);
428 out
.seekg(0, BOOST_IOS::beg
);
430 BOOST_CHECK(out
.fail());
434 compare_files(test1
.name(), temp
.name()),
435 "failed writing to file_descriptor_sink in chars with buffer"
438 BOOST_CHECK(!file
.is_open());
443 file_descriptor
file( temp
.name(),
447 BOOST_CHECK(out
->is_open());
449 BOOST_CHECK(out
.fail());
451 write_data_in_chunks(out
);
452 out
.seekg(0, BOOST_IOS::beg
);
454 BOOST_CHECK(out
.fail());
458 compare_files(test1
.name(), temp
.name()),
459 "failed writing to file_descriptor_sink in chunks with buffer"
462 BOOST_CHECK(!file
.is_open());
465 // test illegal flag combinations
468 file_descriptor(test1
.name(),
469 BOOST_IOS::openmode(0)),
470 BOOST_IOSTREAMS_FAILURE
);
472 file_descriptor(test1
.name(),
474 BOOST_IOSTREAMS_FAILURE
);
476 file_descriptor(test1
.name(),
478 BOOST_IOSTREAMS_FAILURE
);
480 file_descriptor(test1
.name(),
481 BOOST_IOS::app
| BOOST_IOS::trunc
),
482 BOOST_IOSTREAMS_FAILURE
);
484 file_descriptor(test1
.name(),
485 BOOST_IOS::in
| BOOST_IOS::app
),
486 BOOST_IOSTREAMS_FAILURE
);
488 file_descriptor(test1
.name(),
489 BOOST_IOS::in
| BOOST_IOS::trunc
),
490 BOOST_IOSTREAMS_FAILURE
);
492 file_descriptor(test1
.name(),
493 BOOST_IOS::in
| BOOST_IOS::app
| BOOST_IOS::trunc
),
494 BOOST_IOSTREAMS_FAILURE
);
496 file_descriptor(test1
.name(),
497 BOOST_IOS::out
| BOOST_IOS::app
| BOOST_IOS::trunc
),
498 BOOST_IOSTREAMS_FAILURE
);
500 file_descriptor(test1
.name(),
501 BOOST_IOS::in
| BOOST_IOS::out
| BOOST_IOS::app
),
502 BOOST_IOSTREAMS_FAILURE
);
504 file_descriptor(test1
.name(),
509 BOOST_IOSTREAMS_FAILURE
);
513 template <class FileDescriptor
>
514 void file_handle_test_impl(FileDescriptor
*)
520 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
522 FileDescriptor
device1(handle
, boost_ios::never_close_handle
);
523 BOOST_CHECK(device1
.handle() == handle
);
525 BOOST_CHECK_HANDLE_OPEN(handle
);
526 close_file_handle(handle
);
530 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
532 FileDescriptor
device1(handle
, boost_ios::close_handle
);
533 BOOST_CHECK(device1
.handle() == handle
);
535 BOOST_CHECK_HANDLE_CLOSED(handle
);
539 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
540 FileDescriptor
device1(handle
, boost_ios::never_close_handle
);
541 BOOST_CHECK(device1
.handle() == handle
);
543 BOOST_CHECK(!device1
.is_open());
544 BOOST_CHECK_HANDLE_OPEN(handle
);
545 close_file_handle(handle
);
549 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
550 FileDescriptor
device1(handle
, boost_ios::close_handle
);
551 BOOST_CHECK(device1
.handle() == handle
);
553 BOOST_CHECK(!device1
.is_open());
554 BOOST_CHECK_HANDLE_CLOSED(handle
);
558 boost_ios::detail::file_handle handle1
= open_file_handle(test1
.name());
559 boost_ios::detail::file_handle handle2
= open_file_handle(test2
.name());
561 FileDescriptor
device1(handle1
, boost_ios::never_close_handle
);
562 BOOST_CHECK(device1
.handle() == handle1
);
563 device1
.open(handle2
, boost_ios::never_close_handle
);
564 BOOST_CHECK(device1
.handle() == handle2
);
566 BOOST_CHECK_HANDLE_OPEN(handle1
);
567 BOOST_CHECK_HANDLE_OPEN(handle2
);
568 close_file_handle(handle1
);
569 close_file_handle(handle2
);
573 boost_ios::detail::file_handle handle1
= open_file_handle(test1
.name());
574 boost_ios::detail::file_handle handle2
= open_file_handle(test2
.name());
576 FileDescriptor
device1(handle1
, boost_ios::close_handle
);
577 BOOST_CHECK(device1
.handle() == handle1
);
578 device1
.open(handle2
, boost_ios::close_handle
);
579 BOOST_CHECK(device1
.handle() == handle2
);
580 BOOST_CHECK_HANDLE_CLOSED(handle1
);
581 BOOST_CHECK_HANDLE_OPEN(handle2
);
583 BOOST_CHECK_HANDLE_CLOSED(handle1
);
584 BOOST_CHECK_HANDLE_CLOSED(handle2
);
588 boost_ios::detail::file_handle handle1
= open_file_handle(test1
.name());
589 boost_ios::detail::file_handle handle2
= open_file_handle(test2
.name());
591 FileDescriptor
device1(handle1
, boost_ios::close_handle
);
592 BOOST_CHECK(device1
.handle() == handle1
);
593 device1
.open(handle2
, boost_ios::never_close_handle
);
594 BOOST_CHECK(device1
.handle() == handle2
);
595 BOOST_CHECK_HANDLE_CLOSED(handle1
);
596 BOOST_CHECK_HANDLE_OPEN(handle2
);
598 BOOST_CHECK_HANDLE_CLOSED(handle1
);
599 BOOST_CHECK_HANDLE_OPEN(handle2
);
600 close_file_handle(handle2
);
604 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
606 FileDescriptor device1
;
607 BOOST_CHECK(!device1
.is_open());
608 device1
.open(handle
, boost_ios::never_close_handle
);
609 BOOST_CHECK(device1
.handle() == handle
);
610 BOOST_CHECK_HANDLE_OPEN(handle
);
612 BOOST_CHECK_HANDLE_OPEN(handle
);
613 close_file_handle(handle
);
617 boost_ios::detail::file_handle handle
= open_file_handle(test1
.name());
619 FileDescriptor device1
;
620 BOOST_CHECK(!device1
.is_open());
621 device1
.open(handle
, boost_ios::close_handle
);
622 BOOST_CHECK(device1
.handle() == handle
);
623 BOOST_CHECK_HANDLE_OPEN(handle
);
625 BOOST_CHECK_HANDLE_CLOSED(handle
);
629 void file_handle_test()
631 file_handle_test_impl((boost_ios::file_descriptor
*) 0);
632 file_handle_test_impl((boost_ios::file_descriptor_source
*) 0);
633 file_handle_test_impl((boost_ios::file_descriptor_sink
*) 0);
636 test_suite
* init_unit_test_suite(int, char* [])
638 test_suite
* test
= BOOST_TEST_SUITE("file_descriptor test");
639 test
->add(BOOST_TEST_CASE(&file_descriptor_test
));
640 test
->add(BOOST_TEST_CASE(&file_handle_test
));