]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/fmt/test/posix-mock-test.cc
update download target update for octopus release
[ceph.git] / ceph / src / seastar / fmt / test / posix-mock-test.cc
1 // Tests of the C++ interface to POSIX functions that require mocks
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7
8 // Disable bogus MSVC warnings.
9 #ifdef _MSC_VER
10 # define _CRT_SECURE_NO_WARNINGS
11 #endif
12
13 #include "posix-mock.h"
14 #include "../src/posix.cc"
15
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <climits>
19
20 #ifdef _WIN32
21 # include <io.h>
22 # undef max
23 # undef ERROR
24 #endif
25
26 #include "gmock.h"
27 #include "gtest-extra.h"
28 #include "util.h"
29
30 using fmt::buffered_file;
31 using fmt::error_code;
32 using fmt::file;
33
34 using testing::internal::scoped_ptr;
35 using testing::_;
36 using testing::StrEq;
37 using testing::Return;
38
39 namespace {
40 int open_count;
41 int close_count;
42 int dup_count;
43 int dup2_count;
44 int fdopen_count;
45 int read_count;
46 int write_count;
47 int pipe_count;
48 int fopen_count;
49 int fclose_count;
50 int fileno_count;
51 std::size_t read_nbyte;
52 std::size_t write_nbyte;
53 bool sysconf_error;
54
55 enum FStatSimulation { NONE, MAX_SIZE, ERROR } fstat_sim;
56 }
57
58 #define EMULATE_EINTR(func, error_result) \
59 if (func##_count != 0) { \
60 if (func##_count++ != 3) { \
61 errno = EINTR; \
62 return error_result; \
63 } \
64 }
65
66 #ifndef _MSC_VER
67 int test::open(const char *path, int oflag, int mode) {
68 EMULATE_EINTR(open, -1);
69 return ::open(path, oflag, mode);
70 }
71 #else
72 errno_t test::sopen_s(
73 int* pfh, const char *filename, int oflag, int shflag, int pmode) {
74 EMULATE_EINTR(open, EINTR);
75 return _sopen_s(pfh, filename, oflag, shflag, pmode);
76 }
77 #endif
78
79 #ifndef _WIN32
80
81 long test::sysconf(int name) {
82 long result = ::sysconf(name);
83 if (!sysconf_error)
84 return result;
85 // Simulate an error.
86 errno = EINVAL;
87 return -1;
88 }
89
90 static off_t max_file_size() { return std::numeric_limits<off_t>::max(); }
91
92 int test::fstat(int fd, struct stat *buf) {
93 int result = ::fstat(fd, buf);
94 if (fstat_sim == MAX_SIZE)
95 buf->st_size = max_file_size();
96 return result;
97 }
98
99 #else
100
101 static LONGLONG max_file_size() { return std::numeric_limits<LONGLONG>::max(); }
102
103 DWORD test::GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh) {
104 if (fstat_sim == ERROR) {
105 SetLastError(ERROR_ACCESS_DENIED);
106 return INVALID_FILE_SIZE;
107 }
108 if (fstat_sim == MAX_SIZE) {
109 DWORD max = std::numeric_limits<DWORD>::max();
110 *lpFileSizeHigh = max >> 1;
111 return max;
112 }
113 return ::GetFileSize(hFile, lpFileSizeHigh);
114 }
115
116 #endif
117
118 int test::close(int fildes) {
119 // Close the file first because close shouldn't be retried.
120 int result = ::FMT_POSIX(close(fildes));
121 EMULATE_EINTR(close, -1);
122 return result;
123 }
124
125 int test::dup(int fildes) {
126 EMULATE_EINTR(dup, -1);
127 return ::FMT_POSIX(dup(fildes));
128 }
129
130 int test::dup2(int fildes, int fildes2) {
131 EMULATE_EINTR(dup2, -1);
132 return ::FMT_POSIX(dup2(fildes, fildes2));
133 }
134
135 FILE *test::fdopen(int fildes, const char *mode) {
136 EMULATE_EINTR(fdopen, FMT_NULL);
137 return ::FMT_POSIX(fdopen(fildes, mode));
138 }
139
140 test::ssize_t test::read(int fildes, void *buf, test::size_t nbyte) {
141 read_nbyte = nbyte;
142 EMULATE_EINTR(read, -1);
143 return ::FMT_POSIX(read(fildes, buf, nbyte));
144 }
145
146 test::ssize_t test::write(int fildes, const void *buf, test::size_t nbyte) {
147 write_nbyte = nbyte;
148 EMULATE_EINTR(write, -1);
149 return ::FMT_POSIX(write(fildes, buf, nbyte));
150 }
151
152 #ifndef _WIN32
153 int test::pipe(int fildes[2]) {
154 EMULATE_EINTR(pipe, -1);
155 return ::pipe(fildes);
156 }
157 #else
158 int test::pipe(int *pfds, unsigned psize, int textmode) {
159 EMULATE_EINTR(pipe, -1);
160 return _pipe(pfds, psize, textmode);
161 }
162 #endif
163
164 FILE *test::fopen(const char *filename, const char *mode) {
165 EMULATE_EINTR(fopen, FMT_NULL);
166 return ::fopen(filename, mode);
167 }
168
169 int test::fclose(FILE *stream) {
170 EMULATE_EINTR(fclose, EOF);
171 return ::fclose(stream);
172 }
173
174 int (test::fileno)(FILE *stream) {
175 EMULATE_EINTR(fileno, -1);
176 #ifdef fileno
177 return FMT_POSIX(fileno(stream));
178 #else
179 return ::FMT_POSIX(fileno(stream));
180 #endif
181 }
182
183 #ifndef _WIN32
184 # define EXPECT_RETRY(statement, func, message) \
185 func##_count = 1; \
186 statement; \
187 EXPECT_EQ(4, func##_count); \
188 func##_count = 0;
189 # define EXPECT_EQ_POSIX(expected, actual) EXPECT_EQ(expected, actual)
190 #else
191 # define EXPECT_RETRY(statement, func, message) \
192 func##_count = 1; \
193 EXPECT_SYSTEM_ERROR(statement, EINTR, message); \
194 func##_count = 0;
195 # define EXPECT_EQ_POSIX(expected, actual)
196 #endif
197
198 static void write_file(fmt::cstring_view filename, fmt::string_view content) {
199 fmt::buffered_file f(filename, "w");
200 f.print("{}", content);
201 }
202
203 TEST(UtilTest, GetPageSize) {
204 #ifdef _WIN32
205 SYSTEM_INFO si = {};
206 GetSystemInfo(&si);
207 EXPECT_EQ(si.dwPageSize, fmt::getpagesize());
208 #else
209 EXPECT_EQ(sysconf(_SC_PAGESIZE), fmt::getpagesize());
210 sysconf_error = true;
211 EXPECT_SYSTEM_ERROR(
212 fmt::getpagesize(), EINVAL, "cannot get memory page size");
213 sysconf_error = false;
214 #endif
215 }
216
217 TEST(FileTest, OpenRetry) {
218 write_file("test", "there must be something here");
219 scoped_ptr<file> f{FMT_NULL};
220 EXPECT_RETRY(f.reset(new file("test", file::RDONLY)),
221 open, "cannot open file test");
222 #ifndef _WIN32
223 char c = 0;
224 f->read(&c, 1);
225 #endif
226 }
227
228 TEST(FileTest, CloseNoRetryInDtor) {
229 file read_end, write_end;
230 file::pipe(read_end, write_end);
231 scoped_ptr<file> f(new file(std::move(read_end)));
232 int saved_close_count = 0;
233 EXPECT_WRITE(stderr, {
234 close_count = 1;
235 f.reset(FMT_NULL);
236 saved_close_count = close_count;
237 close_count = 0;
238 }, format_system_error(EINTR, "cannot close file") + "\n");
239 EXPECT_EQ(2, saved_close_count);
240 }
241
242 TEST(FileTest, CloseNoRetry) {
243 file read_end, write_end;
244 file::pipe(read_end, write_end);
245 close_count = 1;
246 EXPECT_SYSTEM_ERROR(read_end.close(), EINTR, "cannot close file");
247 EXPECT_EQ(2, close_count);
248 close_count = 0;
249 }
250
251 TEST(FileTest, Size) {
252 std::string content = "top secret, destroy before reading";
253 write_file("test", content);
254 file f("test", file::RDONLY);
255 EXPECT_GE(f.size(), 0);
256 EXPECT_EQ(content.size(), static_cast<unsigned long long>(f.size()));
257 #ifdef _WIN32
258 fmt::memory_buffer message;
259 fmt::internal::format_windows_error(
260 message, ERROR_ACCESS_DENIED, "cannot get file size");
261 fstat_sim = ERROR;
262 EXPECT_THROW_MSG(f.size(), fmt::windows_error, fmt::to_string(message));
263 fstat_sim = NONE;
264 #else
265 f.close();
266 EXPECT_SYSTEM_ERROR(f.size(), EBADF, "cannot get file attributes");
267 #endif
268 }
269
270 TEST(FileTest, MaxSize) {
271 write_file("test", "");
272 file f("test", file::RDONLY);
273 fstat_sim = MAX_SIZE;
274 EXPECT_GE(f.size(), 0);
275 EXPECT_EQ(max_file_size(), f.size());
276 fstat_sim = NONE;
277 }
278
279 TEST(FileTest, ReadRetry) {
280 file read_end, write_end;
281 file::pipe(read_end, write_end);
282 enum { SIZE = 4 };
283 write_end.write("test", SIZE);
284 write_end.close();
285 char buffer[SIZE];
286 std::size_t count = 0;
287 EXPECT_RETRY(count = read_end.read(buffer, SIZE),
288 read, "cannot read from file");
289 EXPECT_EQ_POSIX(static_cast<std::streamsize>(SIZE), count);
290 }
291
292 TEST(FileTest, WriteRetry) {
293 file read_end, write_end;
294 file::pipe(read_end, write_end);
295 enum { SIZE = 4 };
296 std::size_t count = 0;
297 EXPECT_RETRY(count = write_end.write("test", SIZE),
298 write, "cannot write to file");
299 write_end.close();
300 #ifndef _WIN32
301 EXPECT_EQ(static_cast<std::streamsize>(SIZE), count);
302 char buffer[SIZE + 1];
303 read_end.read(buffer, SIZE);
304 buffer[SIZE] = '\0';
305 EXPECT_STREQ("test", buffer);
306 #endif
307 }
308
309 #ifdef _WIN32
310 TEST(FileTest, ConvertReadCount) {
311 file read_end, write_end;
312 file::pipe(read_end, write_end);
313 char c;
314 std::size_t size = UINT_MAX;
315 if (sizeof(unsigned) != sizeof(std::size_t))
316 ++size;
317 read_count = 1;
318 read_nbyte = 0;
319 EXPECT_THROW(read_end.read(&c, size), fmt::system_error);
320 read_count = 0;
321 EXPECT_EQ(UINT_MAX, read_nbyte);
322 }
323
324 TEST(FileTest, ConvertWriteCount) {
325 file read_end, write_end;
326 file::pipe(read_end, write_end);
327 char c;
328 std::size_t size = UINT_MAX;
329 if (sizeof(unsigned) != sizeof(std::size_t))
330 ++size;
331 write_count = 1;
332 write_nbyte = 0;
333 EXPECT_THROW(write_end.write(&c, size), fmt::system_error);
334 write_count = 0;
335 EXPECT_EQ(UINT_MAX, write_nbyte);
336 }
337 #endif
338
339 TEST(FileTest, DupNoRetry) {
340 int stdout_fd = FMT_POSIX(fileno(stdout));
341 dup_count = 1;
342 EXPECT_SYSTEM_ERROR(file::dup(stdout_fd), EINTR,
343 fmt::format("cannot duplicate file descriptor {}", stdout_fd));
344 dup_count = 0;
345 }
346
347 TEST(FileTest, Dup2Retry) {
348 int stdout_fd = FMT_POSIX(fileno(stdout));
349 file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd);
350 EXPECT_RETRY(f1.dup2(f2.descriptor()), dup2,
351 fmt::format("cannot duplicate file descriptor {} to {}",
352 f1.descriptor(), f2.descriptor()));
353 }
354
355 TEST(FileTest, Dup2NoExceptRetry) {
356 int stdout_fd = FMT_POSIX(fileno(stdout));
357 file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd);
358 error_code ec;
359 dup2_count = 1;
360 f1.dup2(f2.descriptor(), ec);
361 #ifndef _WIN32
362 EXPECT_EQ(4, dup2_count);
363 #else
364 EXPECT_EQ(EINTR, ec.get());
365 #endif
366 dup2_count = 0;
367 }
368
369 TEST(FileTest, PipeNoRetry) {
370 file read_end, write_end;
371 pipe_count = 1;
372 EXPECT_SYSTEM_ERROR(
373 file::pipe(read_end, write_end), EINTR, "cannot create pipe");
374 pipe_count = 0;
375 }
376
377 TEST(FileTest, FdopenNoRetry) {
378 file read_end, write_end;
379 file::pipe(read_end, write_end);
380 fdopen_count = 1;
381 EXPECT_SYSTEM_ERROR(read_end.fdopen("r"),
382 EINTR, "cannot associate stream with file descriptor");
383 fdopen_count = 0;
384 }
385
386 TEST(BufferedFileTest, OpenRetry) {
387 write_file("test", "there must be something here");
388 scoped_ptr<buffered_file> f{FMT_NULL};
389 EXPECT_RETRY(f.reset(new buffered_file("test", "r")),
390 fopen, "cannot open file test");
391 #ifndef _WIN32
392 char c = 0;
393 if (fread(&c, 1, 1, f->get()) < 1)
394 throw fmt::system_error(errno, "fread failed");
395 #endif
396 }
397
398 TEST(BufferedFileTest, CloseNoRetryInDtor) {
399 file read_end, write_end;
400 file::pipe(read_end, write_end);
401 scoped_ptr<buffered_file> f(new buffered_file(read_end.fdopen("r")));
402 int saved_fclose_count = 0;
403 EXPECT_WRITE(stderr, {
404 fclose_count = 1;
405 f.reset(FMT_NULL);
406 saved_fclose_count = fclose_count;
407 fclose_count = 0;
408 }, format_system_error(EINTR, "cannot close file") + "\n");
409 EXPECT_EQ(2, saved_fclose_count);
410 }
411
412 TEST(BufferedFileTest, CloseNoRetry) {
413 file read_end, write_end;
414 file::pipe(read_end, write_end);
415 buffered_file f = read_end.fdopen("r");
416 fclose_count = 1;
417 EXPECT_SYSTEM_ERROR(f.close(), EINTR, "cannot close file");
418 EXPECT_EQ(2, fclose_count);
419 fclose_count = 0;
420 }
421
422 TEST(BufferedFileTest, FilenoNoRetry) {
423 file read_end, write_end;
424 file::pipe(read_end, write_end);
425 buffered_file f = read_end.fdopen("r");
426 fileno_count = 1;
427 EXPECT_SYSTEM_ERROR((f.fileno)(), EINTR, "cannot get file descriptor");
428 EXPECT_EQ(2, fileno_count);
429 fileno_count = 0;
430 }
431
432 struct TestMock {
433 static TestMock *instance;
434 } *TestMock::instance;
435
436 TEST(ScopedMock, Scope) {
437 {
438 ScopedMock<TestMock> mock;
439 EXPECT_EQ(&mock, TestMock::instance);
440 TestMock &copy = mock;
441 static_cast<void>(copy);
442 }
443 EXPECT_EQ(FMT_NULL, TestMock::instance);
444 }
445
446 #ifdef FMT_LOCALE
447
448 typedef fmt::Locale::Type LocaleType;
449
450 struct LocaleMock {
451 static LocaleMock *instance;
452 MOCK_METHOD3(newlocale, LocaleType (int category_mask, const char *locale,
453 LocaleType base));
454 MOCK_METHOD1(freelocale, void (LocaleType locale));
455
456 MOCK_METHOD3(strtod_l, double (const char *nptr, char **endptr,
457 LocaleType locale));
458 } *LocaleMock::instance;
459
460 #ifdef _MSC_VER
461 # pragma warning(push)
462 # pragma warning(disable: 4273)
463
464 _locale_t _create_locale(int category, const char *locale) {
465 return LocaleMock::instance->newlocale(category, locale, 0);
466 }
467
468 void _free_locale(_locale_t locale) {
469 LocaleMock::instance->freelocale(locale);
470 }
471
472 double _strtod_l(const char *nptr, char **endptr, _locale_t locale) {
473 return LocaleMock::instance->strtod_l(nptr, endptr, locale);
474 }
475 # pragma warning(pop)
476 #endif
477
478 #if defined(__THROW) && FMT_GCC_VERSION > 0 && FMT_GCC_VERSION <= 408
479 #define FMT_LOCALE_THROW __THROW
480 #else
481 #define FMT_LOCALE_THROW
482 #endif
483
484 LocaleType newlocale(int category_mask, const char *locale, LocaleType base) FMT_LOCALE_THROW {
485 return LocaleMock::instance->newlocale(category_mask, locale, base);
486 }
487
488 #if defined(__APPLE__) || (defined(__FreeBSD__) && __FreeBSD_version < 1200002)
489 typedef int FreeLocaleResult;
490 #else
491 typedef void FreeLocaleResult;
492 #endif
493
494 FreeLocaleResult freelocale(LocaleType locale) FMT_LOCALE_THROW {
495 LocaleMock::instance->freelocale(locale);
496 return FreeLocaleResult();
497 }
498
499 double strtod_l(const char *nptr, char **endptr, LocaleType locale) FMT_LOCALE_THROW {
500 return LocaleMock::instance->strtod_l(nptr, endptr, locale);
501 }
502
503 #undef FMT_LOCALE_THROW
504
505 TEST(LocaleTest, LocaleMock) {
506 ScopedMock<LocaleMock> mock;
507 LocaleType locale = reinterpret_cast<LocaleType>(11);
508 EXPECT_CALL(mock, newlocale(222, StrEq("foo"), locale));
509 newlocale(222, "foo", locale);
510 }
511
512 TEST(LocaleTest, Locale) {
513 #ifndef LC_NUMERIC_MASK
514 enum { LC_NUMERIC_MASK = LC_NUMERIC };
515 #endif
516 ScopedMock<LocaleMock> mock;
517 LocaleType impl = reinterpret_cast<LocaleType>(42);
518 EXPECT_CALL(mock, newlocale(LC_NUMERIC_MASK, StrEq("C"), FMT_NULL))
519 .WillOnce(Return(impl));
520 EXPECT_CALL(mock, freelocale(impl));
521 fmt::Locale locale;
522 EXPECT_EQ(impl, locale.get());
523 }
524
525 TEST(LocaleTest, Strtod) {
526 ScopedMock<LocaleMock> mock;
527 EXPECT_CALL(mock, newlocale(_, _, _))
528 .WillOnce(Return(reinterpret_cast<LocaleType>(42)));
529 EXPECT_CALL(mock, freelocale(_));
530 fmt::Locale locale;
531 const char *str = "4.2";
532 char end = 'x';
533 EXPECT_CALL(mock, strtod_l(str, _, locale.get()))
534 .WillOnce(testing::DoAll(testing::SetArgPointee<1>(&end), Return(777)));
535 EXPECT_EQ(777, locale.strtod(str));
536 EXPECT_EQ(&end, str);
537 }
538
539 #endif // FMT_LOCALE