os-test.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // Formatting library for C++ - tests of the OS-specific functionality
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #include "fmt/os.h"
  8. #include <cstdlib> // std::exit
  9. #include <cstring>
  10. #include <memory>
  11. #include <thread>
  12. #include "gtest-extra.h"
  13. #include "util.h"
  14. using fmt::buffered_file;
  15. using testing::HasSubstr;
  16. using wstring_view = fmt::basic_string_view<wchar_t>;
  17. static std::string uniq_file_name(unsigned line_number) {
  18. return "test-file" + std::to_string(line_number);
  19. }
  20. #ifdef _WIN32
  21. # include <windows.h>
  22. TEST(os_test, format_windows_error) {
  23. LPWSTR message = nullptr;
  24. auto result = FormatMessageW(
  25. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  26. FORMAT_MESSAGE_IGNORE_INSERTS,
  27. nullptr, ERROR_FILE_EXISTS, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  28. reinterpret_cast<LPWSTR>(&message), 0, nullptr);
  29. auto utf8_message =
  30. fmt::detail::to_utf8<wchar_t>(wstring_view(message, result - 2));
  31. LocalFree(message);
  32. fmt::memory_buffer actual_message;
  33. fmt::detail::format_windows_error(actual_message, ERROR_FILE_EXISTS, "test");
  34. EXPECT_EQ(fmt::format("test: {}", utf8_message.str()),
  35. fmt::to_string(actual_message));
  36. actual_message.resize(0);
  37. }
  38. TEST(os_test, format_long_windows_error) {
  39. LPWSTR message = nullptr;
  40. // this error code is not available on all Windows platforms and
  41. // Windows SDKs, so do not fail the test if the error string cannot
  42. // be retrieved.
  43. int provisioning_not_allowed = 0x80284013L; // TBS_E_PROVISIONING_NOT_ALLOWED
  44. auto result = FormatMessageW(
  45. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  46. FORMAT_MESSAGE_IGNORE_INSERTS,
  47. nullptr, static_cast<DWORD>(provisioning_not_allowed),
  48. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  49. reinterpret_cast<LPWSTR>(&message), 0, nullptr);
  50. if (result == 0) {
  51. LocalFree(message);
  52. return;
  53. }
  54. auto utf8_message =
  55. fmt::detail::to_utf8<wchar_t>(wstring_view(message, result - 2));
  56. LocalFree(message);
  57. fmt::memory_buffer actual_message;
  58. fmt::detail::format_windows_error(actual_message, provisioning_not_allowed,
  59. "test");
  60. EXPECT_EQ(fmt::format("test: {}", utf8_message.str()),
  61. fmt::to_string(actual_message));
  62. }
  63. TEST(os_test, windows_error) {
  64. auto error = std::system_error(std::error_code());
  65. try {
  66. throw fmt::windows_error(ERROR_FILE_EXISTS, "test {}", "error");
  67. } catch (const std::system_error& e) {
  68. error = e;
  69. }
  70. fmt::memory_buffer message;
  71. fmt::detail::format_windows_error(message, ERROR_FILE_EXISTS, "test error");
  72. EXPECT_THAT(error.what(), HasSubstr(to_string(message)));
  73. EXPECT_EQ(ERROR_FILE_EXISTS, error.code().value());
  74. }
  75. TEST(os_test, report_windows_error) {
  76. fmt::memory_buffer out;
  77. fmt::detail::format_windows_error(out, ERROR_FILE_EXISTS, "test error");
  78. out.push_back('\n');
  79. EXPECT_WRITE(stderr,
  80. fmt::report_windows_error(ERROR_FILE_EXISTS, "test error"),
  81. fmt::to_string(out));
  82. }
  83. # if FMT_USE_FCNTL && !defined(__MINGW32__)
  84. TEST(file_test, open_windows_file) {
  85. using fmt::file;
  86. file out = file::open_windows_file(L"test-file",
  87. file::WRONLY | file::CREATE | file::TRUNC);
  88. out.write("x", 1);
  89. file in = file::open_windows_file(L"test-file", file::RDONLY);
  90. EXPECT_READ(in, "x");
  91. }
  92. # endif // FMT_USE_FCNTL && !defined(__MINGW32__)
  93. #endif // _WIN32
  94. #if FMT_USE_FCNTL
  95. using fmt::file;
  96. auto isclosed(int fd) -> bool {
  97. char buffer;
  98. auto result = std::streamsize();
  99. SUPPRESS_ASSERT(result = FMT_POSIX(read(fd, &buffer, 1)));
  100. return result == -1 && errno == EBADF;
  101. }
  102. // Opens a file for reading.
  103. auto open_file() -> file {
  104. auto pipe = fmt::pipe();
  105. pipe.write_end.write(file_content, std::strlen(file_content));
  106. pipe.write_end.close();
  107. return std::move(pipe.read_end);
  108. }
  109. // Attempts to write a string to a file.
  110. void write(file& f, fmt::string_view s) {
  111. size_t num_chars_left = s.size();
  112. const char* ptr = s.data();
  113. do {
  114. size_t count = f.write(ptr, num_chars_left);
  115. ptr += count;
  116. // We can't write more than size_t bytes since num_chars_left
  117. // has type size_t.
  118. num_chars_left -= count;
  119. } while (num_chars_left != 0);
  120. }
  121. TEST(buffered_file_test, default_ctor) {
  122. auto f = buffered_file();
  123. EXPECT_TRUE(f.get() == nullptr);
  124. }
  125. TEST(buffered_file_test, move_ctor) {
  126. buffered_file bf = open_buffered_file();
  127. FILE* fp = bf.get();
  128. EXPECT_TRUE(fp != nullptr);
  129. buffered_file bf2(std::move(bf));
  130. EXPECT_EQ(fp, bf2.get());
  131. EXPECT_TRUE(bf.get() == nullptr);
  132. }
  133. TEST(buffered_file_test, move_assignment) {
  134. buffered_file bf = open_buffered_file();
  135. FILE* fp = bf.get();
  136. EXPECT_TRUE(fp != nullptr);
  137. buffered_file bf2;
  138. bf2 = std::move(bf);
  139. EXPECT_EQ(fp, bf2.get());
  140. EXPECT_TRUE(bf.get() == nullptr);
  141. }
  142. TEST(buffered_file_test, move_assignment_closes_file) {
  143. buffered_file bf = open_buffered_file();
  144. buffered_file bf2 = open_buffered_file();
  145. int old_fd = bf2.descriptor();
  146. bf2 = std::move(bf);
  147. EXPECT_TRUE(isclosed(old_fd));
  148. }
  149. TEST(buffered_file_test, move_from_temporary_in_ctor) {
  150. FILE* fp = nullptr;
  151. buffered_file f = open_buffered_file(&fp);
  152. EXPECT_EQ(fp, f.get());
  153. }
  154. TEST(buffered_file_test, move_from_temporary_in_assignment) {
  155. FILE* fp = nullptr;
  156. auto f = buffered_file();
  157. f = open_buffered_file(&fp);
  158. EXPECT_EQ(fp, f.get());
  159. }
  160. TEST(buffered_file_test, move_from_temporary_in_assignment_closes_file) {
  161. buffered_file f = open_buffered_file();
  162. int old_fd = f.descriptor();
  163. f = open_buffered_file();
  164. EXPECT_TRUE(isclosed(old_fd));
  165. }
  166. TEST(buffered_file_test, close_file_in_dtor) {
  167. int fd = 0;
  168. {
  169. buffered_file f = open_buffered_file();
  170. fd = f.descriptor();
  171. }
  172. EXPECT_TRUE(isclosed(fd));
  173. }
  174. TEST(buffered_file_test, close_error_in_dtor) {
  175. auto f =
  176. std::unique_ptr<buffered_file>(new buffered_file(open_buffered_file()));
  177. EXPECT_WRITE(
  178. stderr,
  179. {
  180. // The close function must be called inside EXPECT_WRITE,
  181. // otherwise the system may recycle closed file descriptor when
  182. // redirecting the output in EXPECT_STDERR and the second close
  183. // will break output redirection.
  184. FMT_POSIX(close(f->descriptor()));
  185. SUPPRESS_ASSERT(f.reset(nullptr));
  186. },
  187. system_error_message(EBADF, "cannot close file") + "\n");
  188. }
  189. TEST(buffered_file_test, close) {
  190. buffered_file f = open_buffered_file();
  191. int fd = f.descriptor();
  192. f.close();
  193. EXPECT_TRUE(f.get() == nullptr);
  194. EXPECT_TRUE(isclosed(fd));
  195. }
  196. TEST(buffered_file_test, close_error) {
  197. buffered_file f = open_buffered_file();
  198. FMT_POSIX(close(f.descriptor()));
  199. EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file");
  200. EXPECT_TRUE(f.get() == nullptr);
  201. }
  202. TEST(buffered_file_test, descriptor) {
  203. auto f = open_buffered_file();
  204. EXPECT_TRUE(f.descriptor() != -1);
  205. file copy = file::dup(f.descriptor());
  206. EXPECT_READ(copy, file_content);
  207. }
  208. TEST(ostream_test, move) {
  209. auto test_file = uniq_file_name(__LINE__);
  210. fmt::ostream out = fmt::output_file(test_file);
  211. fmt::ostream moved(std::move(out));
  212. moved.print("hello");
  213. }
  214. TEST(ostream_test, move_while_holding_data) {
  215. auto test_file = uniq_file_name(__LINE__);
  216. {
  217. fmt::ostream out = fmt::output_file(test_file);
  218. out.print("Hello, ");
  219. fmt::ostream moved(std::move(out));
  220. moved.print("world!\n");
  221. }
  222. {
  223. file in(test_file, file::RDONLY);
  224. EXPECT_READ(in, "Hello, world!\n");
  225. }
  226. }
  227. TEST(ostream_test, print) {
  228. auto test_file = uniq_file_name(__LINE__);
  229. fmt::ostream out = fmt::output_file(test_file);
  230. out.print("The answer is {}.\n", 42);
  231. out.close();
  232. file in(test_file, file::RDONLY);
  233. EXPECT_READ(in, "The answer is 42.\n");
  234. }
  235. TEST(ostream_test, buffer_boundary) {
  236. auto str = std::string(4096, 'x');
  237. auto test_file = uniq_file_name(__LINE__);
  238. fmt::ostream out = fmt::output_file(test_file);
  239. out.print("{}", str);
  240. out.print("{}", str);
  241. out.close();
  242. file in(test_file, file::RDONLY);
  243. EXPECT_READ(in, str + str);
  244. }
  245. TEST(ostream_test, buffer_size) {
  246. auto test_file = uniq_file_name(__LINE__);
  247. fmt::ostream out = fmt::output_file(test_file, fmt::buffer_size = 1);
  248. out.print("{}", "foo");
  249. out.close();
  250. file in(test_file, file::RDONLY);
  251. EXPECT_READ(in, "foo");
  252. }
  253. TEST(ostream_test, truncate) {
  254. auto test_file = uniq_file_name(__LINE__);
  255. {
  256. fmt::ostream out = fmt::output_file(test_file);
  257. out.print("0123456789");
  258. }
  259. {
  260. fmt::ostream out = fmt::output_file(test_file);
  261. out.print("foo");
  262. }
  263. file in(test_file, file::RDONLY);
  264. EXPECT_EQ("foo", read(in, 4));
  265. }
  266. TEST(ostream_test, flush) {
  267. auto test_file = uniq_file_name(__LINE__);
  268. auto out = fmt::output_file(test_file);
  269. out.print("x");
  270. out.flush();
  271. auto in = fmt::file(test_file, file::RDONLY);
  272. EXPECT_READ(in, "x");
  273. }
  274. TEST(file_test, default_ctor) {
  275. file f;
  276. EXPECT_EQ(-1, f.descriptor());
  277. }
  278. TEST(file_test, open_buffered_file_in_ctor) {
  279. auto test_file = uniq_file_name(__LINE__);
  280. FILE* fp = safe_fopen(test_file.c_str(), "w");
  281. std::fputs(file_content, fp);
  282. std::fclose(fp);
  283. file f(test_file.c_str(), file::RDONLY);
  284. // Check if the file is open by reading one character from it.
  285. char buffer;
  286. bool isopen = FMT_POSIX(read(f.descriptor(), &buffer, 1)) == 1;
  287. ASSERT_TRUE(isopen);
  288. }
  289. TEST(file_test, open_buffered_file_error) {
  290. EXPECT_SYSTEM_ERROR(file("nonexistent", file::RDONLY), ENOENT,
  291. "cannot open file nonexistent");
  292. }
  293. TEST(file_test, move_ctor) {
  294. file f = open_file();
  295. int fd = f.descriptor();
  296. EXPECT_NE(-1, fd);
  297. file f2(std::move(f));
  298. EXPECT_EQ(fd, f2.descriptor());
  299. EXPECT_EQ(-1, f.descriptor());
  300. }
  301. TEST(file_test, move_assignment) {
  302. file f = open_file();
  303. int fd = f.descriptor();
  304. EXPECT_NE(-1, fd);
  305. file f2;
  306. f2 = std::move(f);
  307. EXPECT_EQ(fd, f2.descriptor());
  308. EXPECT_EQ(-1, f.descriptor());
  309. }
  310. TEST(file_test, move_assignment_closes_file) {
  311. file f = open_file();
  312. file f2 = open_file();
  313. int old_fd = f2.descriptor();
  314. f2 = std::move(f);
  315. EXPECT_TRUE(isclosed(old_fd));
  316. }
  317. file open_buffered_file(int& fd) {
  318. file f = open_file();
  319. fd = f.descriptor();
  320. return f;
  321. }
  322. TEST(file_test, move_from_temporary_in_ctor) {
  323. int fd = 0xdead;
  324. file f(open_buffered_file(fd));
  325. EXPECT_EQ(fd, f.descriptor());
  326. }
  327. TEST(file_test, move_from_temporary_in_assignment) {
  328. int fd = 0xdead;
  329. file f;
  330. f = open_buffered_file(fd);
  331. EXPECT_EQ(fd, f.descriptor());
  332. }
  333. TEST(file_test, move_from_temporary_in_assignment_closes_file) {
  334. int fd = 0xdead;
  335. file f = open_file();
  336. int old_fd = f.descriptor();
  337. f = open_buffered_file(fd);
  338. EXPECT_TRUE(isclosed(old_fd));
  339. }
  340. TEST(file_test, close_file_in_dtor) {
  341. int fd = 0;
  342. {
  343. file f = open_file();
  344. fd = f.descriptor();
  345. }
  346. EXPECT_TRUE(isclosed(fd));
  347. }
  348. TEST(file_test, close_error_in_dtor) {
  349. std::unique_ptr<file> f(new file(open_file()));
  350. EXPECT_WRITE(
  351. stderr,
  352. {
  353. // The close function must be called inside EXPECT_WRITE,
  354. // otherwise the system may recycle closed file descriptor when
  355. // redirecting the output in EXPECT_STDERR and the second close
  356. // will break output redirection.
  357. FMT_POSIX(close(f->descriptor()));
  358. SUPPRESS_ASSERT(f.reset(nullptr));
  359. },
  360. system_error_message(EBADF, "cannot close file") + "\n");
  361. }
  362. TEST(file_test, close) {
  363. file f = open_file();
  364. int fd = f.descriptor();
  365. f.close();
  366. EXPECT_EQ(-1, f.descriptor());
  367. EXPECT_TRUE(isclosed(fd));
  368. }
  369. TEST(file_test, close_error) {
  370. file f = open_file();
  371. FMT_POSIX(close(f.descriptor()));
  372. EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file");
  373. EXPECT_EQ(-1, f.descriptor());
  374. }
  375. TEST(file_test, read) {
  376. file f = open_file();
  377. EXPECT_READ(f, file_content);
  378. }
  379. TEST(file_test, read_error) {
  380. auto test_file = uniq_file_name(__LINE__);
  381. file f(test_file, file::WRONLY | file::CREATE);
  382. char buf;
  383. // We intentionally read from a file opened in the write-only mode to
  384. // cause error.
  385. EXPECT_SYSTEM_ERROR(f.read(&buf, 1), EBADF, "cannot read from file");
  386. }
  387. TEST(file_test, write) {
  388. auto pipe = fmt::pipe();
  389. auto test_file = uniq_file_name(__LINE__);
  390. write(pipe.write_end, test_file);
  391. pipe.write_end.close();
  392. EXPECT_READ(pipe.read_end, test_file);
  393. }
  394. TEST(file_test, write_error) {
  395. auto test_file = uniq_file_name(__LINE__);
  396. file f(test_file, file::RDONLY | file::CREATE);
  397. // We intentionally write to a file opened in the read-only mode to
  398. // cause error.
  399. EXPECT_SYSTEM_ERROR(f.write(" ", 1), EBADF, "cannot write to file");
  400. }
  401. TEST(file_test, dup) {
  402. file f = open_file();
  403. file copy = file::dup(f.descriptor());
  404. EXPECT_NE(f.descriptor(), copy.descriptor());
  405. EXPECT_EQ(file_content, read(copy, std::strlen(file_content)));
  406. }
  407. # ifndef __COVERITY__
  408. TEST(file_test, dup_error) {
  409. int value = -1;
  410. EXPECT_SYSTEM_ERROR_NOASSERT(file::dup(value), EBADF,
  411. "cannot duplicate file descriptor -1");
  412. }
  413. # endif
  414. TEST(file_test, dup2) {
  415. file f = open_file();
  416. file copy = open_file();
  417. f.dup2(copy.descriptor());
  418. EXPECT_NE(f.descriptor(), copy.descriptor());
  419. EXPECT_READ(copy, file_content);
  420. }
  421. TEST(file_test, dup2_error) {
  422. file f = open_file();
  423. EXPECT_SYSTEM_ERROR_NOASSERT(
  424. f.dup2(-1), EBADF,
  425. fmt::format("cannot duplicate file descriptor {} to -1", f.descriptor()));
  426. }
  427. TEST(file_test, dup2_noexcept) {
  428. file f = open_file();
  429. file copy = open_file();
  430. std::error_code ec;
  431. f.dup2(copy.descriptor(), ec);
  432. EXPECT_EQ(ec.value(), 0);
  433. EXPECT_NE(f.descriptor(), copy.descriptor());
  434. EXPECT_READ(copy, file_content);
  435. }
  436. TEST(file_test, dup2_noexcept_error) {
  437. file f = open_file();
  438. std::error_code ec;
  439. SUPPRESS_ASSERT(f.dup2(-1, ec));
  440. EXPECT_EQ(EBADF, ec.value());
  441. }
  442. TEST(file_test, pipe) {
  443. auto pipe = fmt::pipe();
  444. EXPECT_NE(-1, pipe.read_end.descriptor());
  445. EXPECT_NE(-1, pipe.write_end.descriptor());
  446. write(pipe.write_end, "test");
  447. EXPECT_READ(pipe.read_end, "test");
  448. }
  449. TEST(file_test, fdopen) {
  450. auto pipe = fmt::pipe();
  451. int read_fd = pipe.read_end.descriptor();
  452. EXPECT_EQ(read_fd, FMT_POSIX(fileno(pipe.read_end.fdopen("r").get())));
  453. }
  454. // Windows CRT implements _IOLBF incorrectly (full buffering).
  455. # ifndef _WIN32
  456. TEST(file_test, line_buffering) {
  457. auto pipe = fmt::pipe();
  458. int write_fd = pipe.write_end.descriptor();
  459. auto write_end = pipe.write_end.fdopen("w");
  460. setvbuf(write_end.get(), nullptr, _IOLBF, 4096);
  461. write_end.print("42\n");
  462. close(write_fd);
  463. try {
  464. write_end.close();
  465. } catch (const std::system_error&) {
  466. }
  467. auto read_end = pipe.read_end.fdopen("r");
  468. std::thread reader([&]() {
  469. int n = 0;
  470. int result = fscanf(read_end.get(), "%d", &n);
  471. (void)result;
  472. EXPECT_EQ(n, 42);
  473. });
  474. reader.join();
  475. }
  476. # endif // _WIN32
  477. TEST(file_test, buffer_boundary) {
  478. auto pipe = fmt::pipe();
  479. auto write_end = pipe.write_end.fdopen("w");
  480. setvbuf(write_end.get(), nullptr, _IOFBF, 4096);
  481. for (int i = 3; i < 4094; i++)
  482. write_end.print("{}", (i % 73) != 0 ? 'x' : '\n');
  483. write_end.print("{} {}", 1234, 567);
  484. write_end.close();
  485. auto read_end = pipe.read_end.fdopen("r");
  486. char buf[4091] = {};
  487. size_t n = fread(buf, 1, sizeof(buf), read_end.get());
  488. EXPECT_EQ(n, sizeof(buf));
  489. EXPECT_STREQ(fgets(buf, sizeof(buf), read_end.get()), "1234 567");
  490. }
  491. TEST(file_test, io_putting) {
  492. auto pipe = fmt::pipe();
  493. auto read_end = pipe.read_end.fdopen("r");
  494. auto write_end = pipe.write_end.fdopen("w");
  495. size_t read_size = 0;
  496. auto reader = std::thread([&]() {
  497. size_t n = 0;
  498. do {
  499. char buf[4096] = {};
  500. n = fread(buf, 1, sizeof(buf), read_end.get());
  501. read_size += n;
  502. } while (n != 0);
  503. });
  504. // This initialize buffers but doesn't set _IO_CURRENTLY_PUTTING.
  505. fseek(write_end.get(), 0, SEEK_SET);
  506. size_t write_size = 0;
  507. for (int i = 0; i <= 20000; ++i) {
  508. auto s = fmt::format("{}\n", i);
  509. fmt::print(write_end.get(), "{}", s);
  510. write_size += s.size();
  511. }
  512. write_end.close();
  513. reader.join();
  514. EXPECT_EQ(read_size, write_size);
  515. }
  516. #endif // FMT_USE_FCNTL