]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/test/beast/core/file_test.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / beast / test / beast / core / file_test.hpp
1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
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 // Official repository: https://github.com/boostorg/beast
8 //
9
10 #ifndef BOOST_BEAST_FILE_TEST_HPP
11 #define BOOST_BEAST_FILE_TEST_HPP
12
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>
18 #include <cstdio>
19 #include <string>
20 #include <type_traits>
21
22 namespace boost {
23 namespace beast {
24
25 template<class File>
26 void
27 test_file()
28 {
29 BOOST_STATIC_ASSERT(
30 is_file<File>::value);
31 BOOST_STATIC_ASSERT(
32 ! std::is_copy_constructible<File>::value);
33 BOOST_STATIC_ASSERT(
34 ! std::is_copy_assignable<File>::value);
35
36 namespace fs = boost::filesystem;
37
38 class temp_path
39 {
40 fs::path path_;
41 std::string str_;
42
43 public:
44 temp_path()
45 : path_(fs::unique_path())
46 , str_(path_.string<std::string>())
47 {
48 }
49
50 operator fs::path const&()
51 {
52 return path_;
53 }
54
55 operator char const*()
56 {
57 return str_.c_str();
58 }
59 };
60
61 auto const create =
62 [](fs::path const& path)
63 {
64 auto const s =
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))
69 ::fclose(f);
70 };
71
72 auto const remove =
73 [](fs::path const& path)
74 {
75 fs::remove(path);
76 };
77
78 temp_path path;
79
80 // bad file descriptor
81 {
82 File f;
83 char buf[1];
84 BEAST_EXPECT(! f.is_open());
85 BEAST_EXPECT(! fs::exists(path));
86 {
87 error_code ec;
88 f.size(ec);
89 BEAST_EXPECT(ec == errc::bad_file_descriptor);
90 }
91 {
92 error_code ec;
93 f.pos(ec);
94 BEAST_EXPECT(ec == errc::bad_file_descriptor);
95 }
96 {
97 error_code ec;
98 f.seek(0, ec);
99 BEAST_EXPECT(ec == errc::bad_file_descriptor);
100 }
101 {
102 error_code ec;
103 f.read(buf, 0, ec);
104 BEAST_EXPECT(ec == errc::bad_file_descriptor);
105 }
106 {
107 error_code ec;
108 f.write(buf, 0, ec);
109 BEAST_EXPECT(ec == errc::bad_file_descriptor);
110 }
111 }
112
113 // file_mode::read
114 {
115 {
116 File f;
117 error_code ec;
118 create(path);
119 f.open(path, file_mode::read, ec);
120 BEAST_EXPECT(! ec);
121 }
122 remove(path);
123 }
124
125 // file_mode::scan
126 {
127 {
128 File f;
129 error_code ec;
130 create(path);
131 f.open(path, file_mode::scan, ec);
132 BEAST_EXPECT(! ec);
133 }
134 remove(path);
135 }
136
137 // file_mode::write
138 {
139 {
140 File f;
141 error_code ec;
142 BEAST_EXPECT(! fs::exists(path));
143 f.open(path, file_mode::write, ec);
144 BEAST_EXPECT(! ec);
145 BEAST_EXPECT(fs::exists(path));
146 }
147 {
148 File f;
149 error_code ec;
150 BEAST_EXPECT(fs::exists(path));
151 f.open(path, file_mode::write, ec);
152 BEAST_EXPECT(! ec);
153 BEAST_EXPECT(fs::exists(path));
154 }
155 remove(path);
156 }
157
158 // file_mode::write_new
159 {
160 {
161 File f;
162 error_code ec;
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));
167 }
168 {
169 File f;
170 error_code ec;
171 BEAST_EXPECT(fs::exists(path));
172 f.open(path, file_mode::write_new, ec);
173 BEAST_EXPECT(ec);
174 }
175 remove(path);
176 }
177
178 // file_mode::write_existing
179 {
180 {
181 File f;
182 error_code ec;
183 BEAST_EXPECT(! fs::exists(path));
184 f.open(path, file_mode::write_existing, ec);
185 BEAST_EXPECT(ec);
186 BEAST_EXPECT(! fs::exists(path));
187 }
188 {
189 File f;
190 error_code ec;
191 create(path);
192 BEAST_EXPECT(fs::exists(path));
193 f.open(path, file_mode::write_existing, ec);
194 BEAST_EXPECT(! ec);
195 }
196 remove(path);
197 }
198
199 // file_mode::append
200 {
201 {
202 File f;
203 error_code ec;
204 BEAST_EXPECT(! fs::exists(path));
205 f.open(path, file_mode::append, ec);
206 BEAST_EXPECT(! ec);
207 BEAST_EXPECT(fs::exists(path));
208 }
209 {
210 File f;
211 error_code ec;
212 BEAST_EXPECT(fs::exists(path));
213 f.open(path, file_mode::append, ec);
214 BEAST_EXPECT(! ec);
215 BEAST_EXPECT(fs::exists(path));
216 }
217 remove(path);
218 }
219
220 // file_mode::append_existing
221 {
222 {
223 File f;
224 error_code ec;
225 BEAST_EXPECT(! fs::exists(path));
226 f.open(path, file_mode::append_existing, ec);
227 BEAST_EXPECT(ec);
228 BEAST_EXPECT(! fs::exists(path));
229 }
230 remove(path);
231 {
232 File f;
233 error_code ec;
234 create(path);
235 BEAST_EXPECT(fs::exists(path));
236 f.open(path, file_mode::append_existing, ec);
237 BEAST_EXPECT(! ec);
238 }
239 remove(path);
240 }
241
242 // special members
243 {
244 {
245 File f1;
246 error_code ec;
247 f1.open(path, file_mode::write, ec);
248 BEAST_EXPECT(! ec);
249 BEAST_EXPECT(f1.is_open());
250
251 // move constructor
252 File f2(std::move(f1));
253 BEAST_EXPECT(! f1.is_open());
254 BEAST_EXPECT(f2.is_open());
255
256 // move assignment
257 File f3;
258 f3 = std::move(f2);
259 BEAST_EXPECT(! f2.is_open());
260 BEAST_EXPECT(f3.is_open());
261 }
262 remove(path);
263 }
264
265 // re-open
266 {
267 {
268 File f;
269 error_code ec;
270 f.open(path, file_mode::write, ec);
271 BEAST_EXPECT(! ec);
272 f.open(path, file_mode::write, ec);
273 BEAST_EXPECT(! ec);
274 }
275 remove(path);
276 }
277
278 // re-assign
279 {
280 temp_path path2;
281 {
282 error_code ec;
283
284 File f1;
285 f1.open(path, file_mode::write, ec);
286 BEAST_EXPECT(! ec);
287
288 File f2;
289 f2.open(path2, file_mode::write, ec);
290 BEAST_EXPECT(! ec);
291
292 f2 = std::move(f1);
293 BEAST_EXPECT(! f1.is_open());
294 BEAST_EXPECT(f2.is_open());
295 }
296 remove(path);
297 remove(path2);
298 }
299
300 // self-move
301 {
302 {
303 File f;
304 error_code ec;
305 f.open(path, file_mode::write, ec);
306 BEAST_EXPECT(! ec);
307 f = std::move(f);
308 BEAST_EXPECT(f.is_open());
309 }
310 remove(path);
311 }
312
313 // native_handle
314 {
315 {
316 File f;
317 auto none = f.native_handle();
318 error_code ec;
319 f.open(path, file_mode::write, ec);
320 BEAST_EXPECT(! ec);
321 auto fd = f.native_handle();
322 BEAST_EXPECT(fd != none);
323 f.native_handle(none);
324 BEAST_EXPECT(! f.is_open());
325 }
326 remove(path);
327 }
328
329 // read and write
330 {
331 string_view const s = "Hello, world!";
332
333 // write
334 {
335 File f;
336 error_code ec;
337 f.open(path, file_mode::write, ec);
338 BEAST_EXPECT(! ec);
339
340 f.write(s.data(), s.size(), ec);
341 BEAST_EXPECT(! ec);
342
343 auto size = f.size(ec);
344 BEAST_EXPECT(! ec);
345 BEAST_EXPECT(size == s.size());
346
347 auto pos = f.pos(ec);
348 BEAST_EXPECT(! ec);
349 BEAST_EXPECT(pos == size);
350
351 f.close(ec);
352 BEAST_EXPECT(! ec);
353 }
354
355 // read
356 {
357 File f;
358 error_code ec;
359 f.open(path, file_mode::read, ec);
360 BEAST_EXPECT(! ec);
361
362 std::string buf;
363 buf.resize(s.size());
364 f.read(&buf[0], buf.size(), ec);
365 BEAST_EXPECT(! ec);
366 BEAST_EXPECT(buf == s);
367
368 f.seek(1, ec);
369 BEAST_EXPECT(! ec);
370 buf.resize(3);
371 f.read(&buf[0], buf.size(), ec);
372 BEAST_EXPECT(! ec);
373 BEAST_EXPECT(buf == "ell");
374
375 auto pos = f.pos(ec);
376 BEAST_EXPECT(! ec);
377 BEAST_EXPECT(pos == 4);
378 }
379 remove(path);
380 }
381
382 BEAST_EXPECT(! fs::exists(path));
383 }
384
385 } // beast
386 } // boost
387
388 #endif