Chromium Code Reviews| Index: base/files/file_util_unittest.cc |
| diff --git a/base/files/file_util_unittest.cc b/base/files/file_util_unittest.cc |
| index 7ef60a4b29881d0016adf5564956f5d1c749bd0a..29f10f1afa197940708c10e856043b0b69f4b279 100644 |
| --- a/base/files/file_util_unittest.cc |
| +++ b/base/files/file_util_unittest.cc |
| @@ -2,11 +2,13 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include <fcntl.h> |
|
gab
2017/02/09 15:18:09
#if POSIX
grt (UTC plus 2)
2017/02/09 15:57:01
Is this conventional? For some reason I'd thought
gab
2017/02/09 17:01:24
Actually, fcntl.h is already included in the OS_PO
grt (UTC plus 2)
2017/02/10 09:37:21
Well, then...
|
| #include <stddef.h> |
| #include <stdint.h> |
| #include <algorithm> |
| #include <fstream> |
| +#include <initializer_list> |
|
gab
2017/02/09 15:18:09
Is this implicitly required by the for-each below
grt (UTC plus 2)
2017/02/09 15:57:02
It is explicitly required for the for loop. :-)
|
| #include <set> |
| #include <utility> |
| #include <vector> |
| @@ -246,6 +248,27 @@ std::wstring ReadTextFile(const FilePath& filename) { |
| return std::wstring(contents); |
| } |
| +// Sets |is_inheritable| to true if |stream| is set up to be inerhited into |
|
gab
2017/02/09 15:18:09
This reads to me as though it "sets it to true iff
grt (UTC plus 2)
2017/02/10 09:37:21
Comment updated.
|
| +// child processes (i.e., HANDLE_FLAG_INHERIT is set on the underlying handle on |
| +// Windows, or FD_CLOEXEC is not set on the underlying file descriptor on |
| +// POSIX). |
| +void GetIsInheritable(FILE* stream, bool* is_inheritable) { |
| +#if defined(OS_WIN) |
| + HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(stream))); |
| + ASSERT_NE(INVALID_HANDLE_VALUE, handle); |
| + |
| + DWORD info = 0; |
| + ASSERT_EQ(TRUE, ::GetHandleInformation(handle, &info)); |
| + *is_inheritable = ((info & HANDLE_FLAG_INHERIT) != 0); |
| +#elif defined(OS_POSIX) |
| + int fd = fileno(stream); |
| + ASSERT_NE(-1, fd); |
| + int flags = fcntl(fd, F_GETFD, 0); |
| + ASSERT_NE(-1, flags); |
| + *is_inheritable = ((flags & FD_CLOEXEC) == 0); |
| +#endif |
| +} |
| + |
| TEST_F(FileUtilTest, FileAndDirectorySize) { |
| // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize |
| // should return 53 bytes. |
| @@ -1709,8 +1732,26 @@ TEST_F(FileUtilTest, GetTempDirTest) { |
| ::_tputenv_s(kTmpKey, _T("")); |
| } |
| } |
| + |
| #endif // OS_WIN |
| +// Test that files opened by OpenFile are not set up for inheritance into child |
| +// procs. |
| +TEST_F(FileUtilTest, OpenFileNoInheritance) { |
|
gab
2017/02/09 15:18:09
Assuming you verified that this test fails with th
grt (UTC plus 2)
2017/02/09 15:57:01
Yes, indeed.
gab
2017/02/09 17:01:24
Not quite, i.e. wanted to explicitly state that it
|
| + FilePath file_path(temp_dir_.GetPath().Append(FPL("a_file"))); |
| + |
| + for (const char* mode : {"wb", "r,ccs=UNICODE"}) { |
|
gab
2017/02/09 15:18:09
SCOPED_TRACE(mode);
grt (UTC plus 2)
2017/02/10 09:37:21
Done.
|
| + ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_path, L"Geepers")); |
| + FILE* file = OpenFile(file_path, mode); |
| + ASSERT_NE(nullptr, file); |
|
gab
2017/02/09 15:18:09
ASSERT_TRUE(file);
grt (UTC plus 2)
2017/02/09 15:57:01
I don't get this. |file| is a pointer. Why do a bo
gab
2017/02/09 17:01:24
I don't care strongly but the way I see it: if thi
grt (UTC plus 2)
2017/02/10 09:37:21
I see where you're going, but looking at this fail
|
| + bool is_inheritable = false; |
|
gab
2017/02/09 15:18:09
Initialize to |true| to ensure GetIsInheritable()
grt (UTC plus 2)
2017/02/10 09:37:21
Done.
|
| + ASSERT_NO_FATAL_FAILURE(GetIsInheritable(file, &is_inheritable)); |
| + EXPECT_FALSE(is_inheritable); |
| + ASSERT_TRUE(CloseFile(file)); |
| + ASSERT_TRUE(DeleteFile(file_path, false)); |
| + } |
| +} |
| + |
| TEST_F(FileUtilTest, CreateTemporaryFileTest) { |
| FilePath temp_files[3]; |
| for (int i = 0; i < 3; i++) { |