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