posix-mock-test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. file read_end, write_end;
  191. file::pipe(read_end, write_end);
  192. std::unique_ptr<file> f(new file(std::move(read_end)));
  193. int saved_close_count = 0;
  194. EXPECT_WRITE(
  195. stderr,
  196. {
  197. close_count = 1;
  198. f.reset(nullptr);
  199. saved_close_count = close_count;
  200. close_count = 0;
  201. },
  202. system_error_message(EINTR, "cannot close file") + "\n");
  203. EXPECT_EQ(2, saved_close_count);
  204. }
  205. TEST(file_test, close_no_retry) {
  206. file read_end, write_end;
  207. file::pipe(read_end, write_end);
  208. close_count = 1;
  209. EXPECT_SYSTEM_ERROR(read_end.close(), EINTR, "cannot close file");
  210. EXPECT_EQ(2, close_count);
  211. close_count = 0;
  212. }
  213. TEST(file_test, size) {
  214. std::string content = "top secret, destroy before reading";
  215. write_file("temp", content);
  216. file f("temp", file::RDONLY);
  217. EXPECT_GE(f.size(), 0);
  218. EXPECT_EQ(content.size(), static_cast<unsigned long long>(f.size()));
  219. # ifdef _WIN32
  220. auto error_code = std::error_code();
  221. fstat_sim = error;
  222. try {
  223. f.size();
  224. } catch (const std::system_error& e) {
  225. error_code = e.code();
  226. }
  227. fstat_sim = none;
  228. EXPECT_EQ(error_code,
  229. std::error_code(ERROR_ACCESS_DENIED, fmt::system_category()));
  230. # else
  231. f.close();
  232. EXPECT_SYSTEM_ERROR(f.size(), EBADF, "cannot get file attributes");
  233. # endif
  234. }
  235. TEST(file_test, max_size) {
  236. write_file("temp", "");
  237. file f("temp", file::RDONLY);
  238. fstat_sim = max_size;
  239. EXPECT_GE(f.size(), 0);
  240. EXPECT_EQ(max_file_size(), f.size());
  241. fstat_sim = none;
  242. }
  243. TEST(file_test, read_retry) {
  244. file read_end, write_end;
  245. file::pipe(read_end, write_end);
  246. enum { SIZE = 4 };
  247. write_end.write("test", SIZE);
  248. write_end.close();
  249. char buffer[SIZE];
  250. size_t count = 0;
  251. EXPECT_RETRY(count = read_end.read(buffer, SIZE), read,
  252. "cannot read from file");
  253. EXPECT_EQ_POSIX(static_cast<std::streamsize>(SIZE), count);
  254. }
  255. TEST(file_test, write_retry) {
  256. file read_end, write_end;
  257. file::pipe(read_end, write_end);
  258. enum { SIZE = 4 };
  259. size_t count = 0;
  260. EXPECT_RETRY(count = write_end.write("test", SIZE), write,
  261. "cannot write to file");
  262. write_end.close();
  263. # ifndef _WIN32
  264. EXPECT_EQ(static_cast<std::streamsize>(SIZE), count);
  265. char buffer[SIZE + 1];
  266. read_end.read(buffer, SIZE);
  267. buffer[SIZE] = '\0';
  268. EXPECT_STREQ("test", buffer);
  269. # endif
  270. }
  271. # ifdef _WIN32
  272. TEST(file_test, convert_read_count) {
  273. file read_end, write_end;
  274. file::pipe(read_end, write_end);
  275. char c;
  276. size_t size = UINT_MAX;
  277. if (sizeof(unsigned) != sizeof(size_t)) ++size;
  278. read_count = 1;
  279. read_nbyte = 0;
  280. EXPECT_THROW(read_end.read(&c, size), std::system_error);
  281. read_count = 0;
  282. EXPECT_EQ(UINT_MAX, read_nbyte);
  283. }
  284. TEST(file_test, convert_write_count) {
  285. file read_end, write_end;
  286. file::pipe(read_end, write_end);
  287. char c;
  288. size_t size = UINT_MAX;
  289. if (sizeof(unsigned) != sizeof(size_t)) ++size;
  290. write_count = 1;
  291. write_nbyte = 0;
  292. EXPECT_THROW(write_end.write(&c, size), std::system_error);
  293. write_count = 0;
  294. EXPECT_EQ(UINT_MAX, write_nbyte);
  295. }
  296. # endif
  297. TEST(file_test, dup_no_retry) {
  298. int stdout_fd = FMT_POSIX(fileno(stdout));
  299. dup_count = 1;
  300. EXPECT_SYSTEM_ERROR(
  301. file::dup(stdout_fd), EINTR,
  302. fmt::format("cannot duplicate file descriptor {}", stdout_fd));
  303. dup_count = 0;
  304. }
  305. TEST(file_test, dup2_retry) {
  306. int stdout_fd = FMT_POSIX(fileno(stdout));
  307. file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd);
  308. EXPECT_RETRY(f1.dup2(f2.descriptor()), dup2,
  309. fmt::format("cannot duplicate file descriptor {} to {}",
  310. f1.descriptor(), f2.descriptor()));
  311. }
  312. TEST(file_test, dup2_no_except_retry) {
  313. int stdout_fd = FMT_POSIX(fileno(stdout));
  314. file f1 = file::dup(stdout_fd), f2 = file::dup(stdout_fd);
  315. std::error_code ec;
  316. dup2_count = 1;
  317. f1.dup2(f2.descriptor(), ec);
  318. # ifndef _WIN32
  319. EXPECT_EQ(4, dup2_count);
  320. # else
  321. EXPECT_EQ(EINTR, ec.value());
  322. # endif
  323. dup2_count = 0;
  324. }
  325. TEST(file_test, pipe_no_retry) {
  326. file read_end, write_end;
  327. pipe_count = 1;
  328. EXPECT_SYSTEM_ERROR(file::pipe(read_end, write_end), EINTR,
  329. "cannot create pipe");
  330. pipe_count = 0;
  331. }
  332. TEST(file_test, fdopen_no_retry) {
  333. file read_end, write_end;
  334. file::pipe(read_end, write_end);
  335. fdopen_count = 1;
  336. EXPECT_SYSTEM_ERROR(read_end.fdopen("r"), EINTR,
  337. "cannot associate stream with file descriptor");
  338. fdopen_count = 0;
  339. }
  340. TEST(buffered_file_test, open_retry) {
  341. write_file("temp", "there must be something here");
  342. std::unique_ptr<buffered_file> f{nullptr};
  343. EXPECT_RETRY(f.reset(new buffered_file("temp", "r")), fopen,
  344. "cannot open file temp");
  345. # ifndef _WIN32
  346. char c = 0;
  347. if (fread(&c, 1, 1, f->get()) < 1)
  348. throw fmt::system_error(errno, "fread failed");
  349. # endif
  350. }
  351. TEST(buffered_file_test, close_no_retry_in_dtor) {
  352. file read_end, write_end;
  353. file::pipe(read_end, write_end);
  354. std::unique_ptr<buffered_file> f(new buffered_file(read_end.fdopen("r")));
  355. int saved_fclose_count = 0;
  356. EXPECT_WRITE(
  357. stderr,
  358. {
  359. fclose_count = 1;
  360. f.reset(nullptr);
  361. saved_fclose_count = fclose_count;
  362. fclose_count = 0;
  363. },
  364. system_error_message(EINTR, "cannot close file") + "\n");
  365. EXPECT_EQ(2, saved_fclose_count);
  366. }
  367. TEST(buffered_file_test, close_no_retry) {
  368. file read_end, write_end;
  369. file::pipe(read_end, write_end);
  370. buffered_file f = read_end.fdopen("r");
  371. fclose_count = 1;
  372. EXPECT_SYSTEM_ERROR(f.close(), EINTR, "cannot close file");
  373. EXPECT_EQ(2, fclose_count);
  374. fclose_count = 0;
  375. }
  376. TEST(buffered_file_test, fileno_no_retry) {
  377. file read_end, write_end;
  378. file::pipe(read_end, write_end);
  379. buffered_file f = read_end.fdopen("r");
  380. fileno_count = 1;
  381. EXPECT_SYSTEM_ERROR((f.descriptor)(), EINTR, "cannot get file descriptor");
  382. EXPECT_EQ(2, fileno_count);
  383. fileno_count = 0;
  384. }
  385. #endif // FMT_USE_FCNTL
  386. struct test_mock {
  387. static test_mock* instance;
  388. } * test_mock::instance;
  389. TEST(scoped_mock, scope) {
  390. {
  391. scoped_mock<test_mock> mock;
  392. EXPECT_EQ(&mock, test_mock::instance);
  393. test_mock& copy = mock;
  394. static_cast<void>(copy);
  395. }
  396. EXPECT_EQ(nullptr, test_mock::instance);
  397. }