posix-mock-test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. // Disable bogus MSVC warnings.
  8. #if !defined(_CRT_SECURE_NO_WARNINGS) && defined(_MSC_VER)
  9. # define _CRT_SECURE_NO_WARNINGS
  10. #endif
  11. #include "posix-mock.h"
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <climits>
  15. #include <memory>
  16. #include "../src/os.cc"
  17. #ifdef _WIN32
  18. # include <io.h>
  19. # undef max
  20. #endif
  21. #include "gmock/gmock.h"
  22. #include "gtest-extra.h"
  23. #include "util.h"
  24. using fmt::buffered_file;
  25. using testing::_;
  26. using testing::Return;
  27. using testing::StrEq;
  28. template <typename Mock> struct scoped_mock : testing::StrictMock<Mock> {
  29. scoped_mock() { Mock::instance = this; }
  30. ~scoped_mock() { Mock::instance = nullptr; }
  31. };
  32. namespace {
  33. int open_count;
  34. int close_count;
  35. int dup_count;
  36. int dup2_count;
  37. int fdopen_count;
  38. int read_count;
  39. int write_count;
  40. int pipe_count;
  41. int fopen_count;
  42. int fclose_count;
  43. int fileno_count;
  44. size_t read_nbyte;
  45. size_t write_nbyte;
  46. bool sysconf_error;
  47. enum { none, max_size, error } fstat_sim;
  48. } // namespace
  49. #define EMULATE_EINTR(func, error_result) \
  50. if (func##_count != 0) { \
  51. if (func##_count++ != 3) { \
  52. errno = EINTR; \
  53. return error_result; \
  54. } \
  55. }
  56. #ifndef _MSC_VER
  57. int test::open(const char* path, int oflag, int mode) {
  58. EMULATE_EINTR(open, -1);
  59. return ::open(path, oflag, mode);
  60. }
  61. #else
  62. errno_t test::sopen_s(int* pfh, const char* filename, int oflag, int shflag,
  63. int pmode) {
  64. EMULATE_EINTR(open, EINTR);
  65. return _sopen_s(pfh, filename, oflag, shflag, pmode);
  66. }
  67. #endif
  68. #ifndef _WIN32
  69. long test::sysconf(int name) {
  70. long result = ::sysconf(name);
  71. if (!sysconf_error) return result;
  72. // Simulate an error.
  73. errno = EINVAL;
  74. return -1;
  75. }
  76. static off_t max_file_size() { return std::numeric_limits<off_t>::max(); }
  77. int test::fstat(int fd, struct stat* buf) {
  78. int result = ::fstat(fd, buf);
  79. if (fstat_sim == max_size) buf->st_size = max_file_size();
  80. return result;
  81. }
  82. #else
  83. static LONGLONG max_file_size() { return std::numeric_limits<LONGLONG>::max(); }
  84. DWORD test::GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh) {
  85. if (fstat_sim == error) {
  86. SetLastError(ERROR_ACCESS_DENIED);
  87. return INVALID_FILE_SIZE;
  88. }
  89. if (fstat_sim == max_size) {
  90. DWORD max = std::numeric_limits<DWORD>::max();
  91. *lpFileSizeHigh = max >> 1;
  92. return max;
  93. }
  94. return ::GetFileSize(hFile, lpFileSizeHigh);
  95. }
  96. #endif
  97. int test::close(int fildes) {
  98. // Close the file first because close shouldn't be retried.
  99. int result = ::FMT_POSIX(close(fildes));
  100. EMULATE_EINTR(close, -1);
  101. return result;
  102. }
  103. int test::dup(int fildes) {
  104. EMULATE_EINTR(dup, -1);
  105. return ::FMT_POSIX(dup(fildes));
  106. }
  107. int test::dup2(int fildes, int fildes2) {
  108. EMULATE_EINTR(dup2, -1);
  109. return ::FMT_POSIX(dup2(fildes, fildes2));
  110. }
  111. FILE* test::fdopen(int fildes, const char* mode) {
  112. EMULATE_EINTR(fdopen, nullptr);
  113. return ::FMT_POSIX(fdopen(fildes, mode));
  114. }
  115. test::ssize_t test::read(int fildes, void* buf, test::size_t nbyte) {
  116. read_nbyte = nbyte;
  117. EMULATE_EINTR(read, -1);
  118. return ::FMT_POSIX(read(fildes, buf, nbyte));
  119. }
  120. test::ssize_t test::write(int fildes, const void* buf, test::size_t nbyte) {
  121. write_nbyte = nbyte;
  122. EMULATE_EINTR(write, -1);
  123. return ::FMT_POSIX(write(fildes, buf, nbyte));
  124. }
  125. #ifndef _WIN32
  126. int test::pipe(int fildes[2]) {
  127. EMULATE_EINTR(pipe, -1);
  128. return ::pipe(fildes);
  129. }
  130. #else
  131. int test::pipe(int* pfds, unsigned psize, int textmode) {
  132. EMULATE_EINTR(pipe, -1);
  133. return _pipe(pfds, psize, textmode);
  134. }
  135. #endif
  136. FILE* test::fopen(const char* filename, const char* mode) {
  137. EMULATE_EINTR(fopen, nullptr);
  138. return ::fopen(filename, mode);
  139. }
  140. int test::fclose(FILE* stream) {
  141. EMULATE_EINTR(fclose, EOF);
  142. return ::fclose(stream);
  143. }
  144. int(test::fileno)(FILE* stream) {
  145. EMULATE_EINTR(fileno, -1);
  146. #ifdef fileno
  147. return FMT_POSIX(fileno(stream));
  148. #else
  149. return ::FMT_POSIX(fileno(stream));
  150. #endif
  151. }
  152. #ifndef _WIN32
  153. # define EXPECT_RETRY(statement, func, message) \
  154. func##_count = 1; \
  155. statement; \
  156. EXPECT_EQ(4, func##_count); \
  157. func##_count = 0;
  158. # define EXPECT_EQ_POSIX(expected, actual) EXPECT_EQ(expected, actual)
  159. #else
  160. # define EXPECT_RETRY(statement, func, message) \
  161. func##_count = 1; \
  162. EXPECT_SYSTEM_ERROR(statement, EINTR, message); \
  163. func##_count = 0;
  164. # define EXPECT_EQ_POSIX(expected, actual)
  165. #endif
  166. #if FMT_USE_FCNTL
  167. void write_file(fmt::cstring_view filename, fmt::string_view content) {
  168. fmt::buffered_file f(filename, "w");
  169. f.print("{}", content);
  170. }
  171. using fmt::file;
  172. TEST(os_test, getpagesize) {
  173. # ifdef _WIN32
  174. SYSTEM_INFO si = {};
  175. GetSystemInfo(&si);
  176. EXPECT_EQ(si.dwPageSize, fmt::getpagesize());
  177. # else
  178. EXPECT_EQ(sysconf(_SC_PAGESIZE), fmt::getpagesize());
  179. sysconf_error = true;
  180. EXPECT_SYSTEM_ERROR(fmt::getpagesize(), EINVAL,
  181. "cannot get memory page size");
  182. sysconf_error = false;
  183. # endif
  184. }
  185. TEST(file_test, open_retry) {
  186. write_file("temp", "there must be something here");
  187. std::unique_ptr<file> f{nullptr};
  188. EXPECT_RETRY(f.reset(new file("temp", file::RDONLY)), open,
  189. "cannot open file temp");
  190. # ifndef _WIN32
  191. char c = 0;
  192. f->read(&c, 1);
  193. # endif
  194. }
  195. TEST(file_test, close_no_retry_in_dtor) {
  196. file read_end, write_end;
  197. file::pipe(read_end, write_end);
  198. std::unique_ptr<file> f(new file(std::move(read_end)));
  199. int saved_close_count = 0;
  200. EXPECT_WRITE(
  201. stderr,
  202. {
  203. close_count = 1;
  204. f.reset(nullptr);
  205. saved_close_count = close_count;
  206. close_count = 0;
  207. },
  208. system_error_message(EINTR, "cannot close file") + "\n");
  209. EXPECT_EQ(2, saved_close_count);
  210. }
  211. TEST(file_test, close_no_retry) {
  212. file read_end, write_end;
  213. file::pipe(read_end, write_end);
  214. close_count = 1;
  215. EXPECT_SYSTEM_ERROR(read_end.close(), EINTR, "cannot close file");
  216. EXPECT_EQ(2, close_count);
  217. close_count = 0;
  218. }
  219. TEST(file_test, size) {
  220. std::string content = "top secret, destroy before reading";
  221. write_file("temp", content);
  222. file f("temp", file::RDONLY);
  223. EXPECT_GE(f.size(), 0);
  224. EXPECT_EQ(content.size(), static_cast<unsigned long long>(f.size()));
  225. # ifdef _WIN32
  226. auto error_code = std::error_code();
  227. fstat_sim = error;
  228. try {
  229. f.size();
  230. } catch (const std::system_error& e) {
  231. error_code = e.code();
  232. }
  233. fstat_sim = none;
  234. EXPECT_EQ(error_code,
  235. std::error_code(ERROR_ACCESS_DENIED, fmt::system_category()));
  236. # else
  237. f.close();
  238. EXPECT_SYSTEM_ERROR(f.size(), EBADF, "cannot get file attributes");
  239. # endif
  240. }
  241. TEST(file_test, max_size) {
  242. write_file("temp", "");
  243. file f("temp", file::RDONLY);
  244. fstat_sim = max_size;
  245. EXPECT_GE(f.size(), 0);
  246. EXPECT_EQ(max_file_size(), f.size());
  247. fstat_sim = none;
  248. }
  249. TEST(file_test, read_retry) {
  250. file read_end, write_end;
  251. file::pipe(read_end, write_end);
  252. enum { SIZE = 4 };
  253. write_end.write("test", SIZE);
  254. write_end.close();
  255. char buffer[SIZE];
  256. size_t count = 0;
  257. EXPECT_RETRY(count = read_end.read(buffer, SIZE), read,
  258. "cannot read from file");
  259. EXPECT_EQ_POSIX(static_cast<std::streamsize>(SIZE), count);
  260. }
  261. TEST(file_test, write_retry) {
  262. file read_end, write_end;
  263. file::pipe(read_end, write_end);
  264. enum { SIZE = 4 };
  265. size_t count = 0;
  266. EXPECT_RETRY(count = write_end.write("test", SIZE), write,
  267. "cannot write to file");
  268. write_end.close();
  269. # ifndef _WIN32
  270. EXPECT_EQ(static_cast<std::streamsize>(SIZE), count);
  271. char buffer[SIZE + 1];
  272. read_end.read(buffer, SIZE);
  273. buffer[SIZE] = '\0';
  274. EXPECT_STREQ("test", buffer);
  275. # endif
  276. }
  277. # ifdef _WIN32
  278. TEST(file_test, convert_read_count) {
  279. file read_end, write_end;
  280. file::pipe(read_end, write_end);
  281. char c;
  282. size_t size = UINT_MAX;
  283. if (sizeof(unsigned) != sizeof(size_t)) ++size;
  284. read_count = 1;
  285. read_nbyte = 0;
  286. EXPECT_THROW(read_end.read(&c, size), std::system_error);
  287. read_count = 0;
  288. EXPECT_EQ(UINT_MAX, read_nbyte);
  289. }
  290. TEST(file_test, convert_write_count) {
  291. file read_end, write_end;
  292. file::pipe(read_end, write_end);
  293. char c;
  294. size_t size = UINT_MAX;
  295. if (sizeof(unsigned) != sizeof(size_t)) ++size;
  296. write_count = 1;
  297. write_nbyte = 0;
  298. EXPECT_THROW(write_end.write(&c, size), std::system_error);
  299. write_count = 0;
  300. EXPECT_EQ(UINT_MAX, write_nbyte);
  301. }
  302. # endif
  303. TEST(file_test, dup_no_retry) {
  304. int stdout_fd = FMT_POSIX(fileno(stdout));
  305. dup_count = 1;
  306. EXPECT_SYSTEM_ERROR(
  307. file::dup(stdout_fd), EINTR,
  308. fmt::format("cannot duplicate file descriptor {}", stdout_fd));
  309. dup_count = 0;
  310. }
  311. TEST(file_test, dup2_retry) {
  312. int stdout_fd = FMT_POSIX(fileno(stdout));
  313. file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd);
  314. EXPECT_RETRY(f1.dup2(f2.descriptor()), dup2,
  315. fmt::format("cannot duplicate file descriptor {} to {}",
  316. f1.descriptor(), f2.descriptor()));
  317. }
  318. TEST(file_test, dup2_no_except_retry) {
  319. int stdout_fd = FMT_POSIX(fileno(stdout));
  320. file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd);
  321. std::error_code ec;
  322. dup2_count = 1;
  323. f1.dup2(f2.descriptor(), ec);
  324. # ifndef _WIN32
  325. EXPECT_EQ(4, dup2_count);
  326. # else
  327. EXPECT_EQ(EINTR, ec.value());
  328. # endif
  329. dup2_count = 0;
  330. }
  331. TEST(file_test, pipe_no_retry) {
  332. file read_end, write_end;
  333. pipe_count = 1;
  334. EXPECT_SYSTEM_ERROR(file::pipe(read_end, write_end), EINTR,
  335. "cannot create pipe");
  336. pipe_count = 0;
  337. }
  338. TEST(file_test, fdopen_no_retry) {
  339. file read_end, write_end;
  340. file::pipe(read_end, write_end);
  341. fdopen_count = 1;
  342. EXPECT_SYSTEM_ERROR(read_end.fdopen("r"), EINTR,
  343. "cannot associate stream with file descriptor");
  344. fdopen_count = 0;
  345. }
  346. TEST(buffered_file_test, open_retry) {
  347. write_file("temp", "there must be something here");
  348. std::unique_ptr<buffered_file> f{nullptr};
  349. EXPECT_RETRY(f.reset(new buffered_file("temp", "r")), fopen,
  350. "cannot open file temp");
  351. # ifndef _WIN32
  352. char c = 0;
  353. if (fread(&c, 1, 1, f->get()) < 1)
  354. throw fmt::system_error(errno, "fread failed");
  355. # endif
  356. }
  357. TEST(buffered_file_test, close_no_retry_in_dtor) {
  358. file read_end, write_end;
  359. file::pipe(read_end, write_end);
  360. std::unique_ptr<buffered_file> f(new buffered_file(read_end.fdopen("r")));
  361. int saved_fclose_count = 0;
  362. EXPECT_WRITE(
  363. stderr,
  364. {
  365. fclose_count = 1;
  366. f.reset(nullptr);
  367. saved_fclose_count = fclose_count;
  368. fclose_count = 0;
  369. },
  370. system_error_message(EINTR, "cannot close file") + "\n");
  371. EXPECT_EQ(2, saved_fclose_count);
  372. }
  373. TEST(buffered_file_test, close_no_retry) {
  374. file read_end, write_end;
  375. file::pipe(read_end, write_end);
  376. buffered_file f = read_end.fdopen("r");
  377. fclose_count = 1;
  378. EXPECT_SYSTEM_ERROR(f.close(), EINTR, "cannot close file");
  379. EXPECT_EQ(2, fclose_count);
  380. fclose_count = 0;
  381. }
  382. TEST(buffered_file_test, fileno_no_retry) {
  383. file read_end, write_end;
  384. file::pipe(read_end, write_end);
  385. buffered_file f = read_end.fdopen("r");
  386. fileno_count = 1;
  387. EXPECT_SYSTEM_ERROR((f.descriptor)(), EINTR, "cannot get file descriptor");
  388. EXPECT_EQ(2, fileno_count);
  389. fileno_count = 0;
  390. }
  391. #endif // FMT_USE_FCNTL
  392. struct test_mock {
  393. static test_mock* instance;
  394. } * test_mock::instance;
  395. TEST(scoped_mock, scope) {
  396. {
  397. scoped_mock<test_mock> mock;
  398. EXPECT_EQ(&mock, test_mock::instance);
  399. test_mock& copy = mock;
  400. static_cast<void>(copy);
  401. }
  402. EXPECT_EQ(nullptr, test_mock::instance);
  403. }