os-test.cc 15 KB

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