2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_FILE_TEST_HPP
11 #define BOOST_BEAST_FILE_TEST_HPP
13 #include <boost/beast/_experimental/unit_test/suite.hpp>
14 #include <boost/beast/core/string.hpp>
15 #include <boost/beast/core/file_base.hpp>
16 #include <boost/filesystem/path.hpp>
17 #include <boost/filesystem/operations.hpp>
20 #include <type_traits>
30 is_file<File>::value);
32 ! std::is_copy_constructible<File>::value);
34 ! std::is_copy_assignable<File>::value);
36 namespace fs = boost::filesystem;
45 : path_(fs::unique_path())
46 , str_(path_.string<std::string>())
50 operator fs::path const&()
55 operator char const*()
62 [](fs::path const& path)
65 path.string<std::string>();
66 BEAST_EXPECT(! fs::exists(path));
67 FILE* f = ::fopen(s.c_str(), "w");
68 if( BEAST_EXPECT(f != nullptr))
73 [](fs::path const& path)
80 // bad file descriptor
84 BEAST_EXPECT(! f.is_open());
85 BEAST_EXPECT(! fs::exists(path));
89 BEAST_EXPECT(ec == errc::bad_file_descriptor);
94 BEAST_EXPECT(ec == errc::bad_file_descriptor);
99 BEAST_EXPECT(ec == errc::bad_file_descriptor);
104 BEAST_EXPECT(ec == errc::bad_file_descriptor);
109 BEAST_EXPECT(ec == errc::bad_file_descriptor);
119 f.open(path, file_mode::read, ec);
131 f.open(path, file_mode::scan, ec);
142 BEAST_EXPECT(! fs::exists(path));
143 f.open(path, file_mode::write, ec);
145 BEAST_EXPECT(fs::exists(path));
150 BEAST_EXPECT(fs::exists(path));
151 f.open(path, file_mode::write, ec);
153 BEAST_EXPECT(fs::exists(path));
158 // file_mode::write_new
163 BEAST_EXPECT(! fs::exists(path));
164 f.open(path, file_mode::write_new, ec);
165 BEAST_EXPECTS(! ec, ec.message());
166 BEAST_EXPECT(fs::exists(path));
171 BEAST_EXPECT(fs::exists(path));
172 f.open(path, file_mode::write_new, ec);
178 // file_mode::write_existing
183 BEAST_EXPECT(! fs::exists(path));
184 f.open(path, file_mode::write_existing, ec);
186 BEAST_EXPECT(! fs::exists(path));
192 BEAST_EXPECT(fs::exists(path));
193 f.open(path, file_mode::write_existing, ec);
204 BEAST_EXPECT(! fs::exists(path));
205 f.open(path, file_mode::append, ec);
207 BEAST_EXPECT(fs::exists(path));
212 BEAST_EXPECT(fs::exists(path));
213 f.open(path, file_mode::append, ec);
215 BEAST_EXPECT(fs::exists(path));
220 // file_mode::append_existing
225 BEAST_EXPECT(! fs::exists(path));
226 f.open(path, file_mode::append_existing, ec);
228 BEAST_EXPECT(! fs::exists(path));
235 BEAST_EXPECT(fs::exists(path));
236 f.open(path, file_mode::append_existing, ec);
247 f1.open(path, file_mode::write, ec);
249 BEAST_EXPECT(f1.is_open());
252 File f2(std::move(f1));
253 BEAST_EXPECT(! f1.is_open());
254 BEAST_EXPECT(f2.is_open());
259 BEAST_EXPECT(! f2.is_open());
260 BEAST_EXPECT(f3.is_open());
270 f.open(path, file_mode::write, ec);
272 f.open(path, file_mode::write, ec);
285 f1.open(path, file_mode::write, ec);
289 f2.open(path2, file_mode::write, ec);
293 BEAST_EXPECT(! f1.is_open());
294 BEAST_EXPECT(f2.is_open());
305 f.open(path, file_mode::write, ec);
308 BEAST_EXPECT(f.is_open());
317 auto none = f.native_handle();
319 f.open(path, file_mode::write, ec);
321 auto fd = f.native_handle();
322 BEAST_EXPECT(fd != none);
323 f.native_handle(none);
324 BEAST_EXPECT(! f.is_open());
331 string_view const s = "Hello, world!";
337 f.open(path, file_mode::write, ec);
340 f.write(s.data(), s.size(), ec);
343 auto size = f.size(ec);
345 BEAST_EXPECT(size == s.size());
347 auto pos = f.pos(ec);
349 BEAST_EXPECT(pos == size);
359 f.open(path, file_mode::read, ec);
363 buf.resize(s.size());
364 f.read(&buf[0], buf.size(), ec);
366 BEAST_EXPECT(buf == s);
371 f.read(&buf[0], buf.size(), ec);
373 BEAST_EXPECT(buf == "ell");
375 auto pos = f.pos(ec);
377 BEAST_EXPECT(pos == 4);
382 BEAST_EXPECT(! fs::exists(path));