posix-mock-test.cc 11 KB

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