OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #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...
| |
5 #include <stddef.h> | 6 #include <stddef.h> |
6 #include <stdint.h> | 7 #include <stdint.h> |
7 | 8 |
8 #include <algorithm> | 9 #include <algorithm> |
9 #include <fstream> | 10 #include <fstream> |
11 #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. :-)
| |
10 #include <set> | 12 #include <set> |
11 #include <utility> | 13 #include <utility> |
12 #include <vector> | 14 #include <vector> |
13 | 15 |
14 #include "base/base_paths.h" | 16 #include "base/base_paths.h" |
15 #include "base/environment.h" | 17 #include "base/environment.h" |
16 #include "base/files/file_enumerator.h" | 18 #include "base/files/file_enumerator.h" |
17 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
18 #include "base/files/file_util.h" | 20 #include "base/files/file_util.h" |
19 #include "base/files/scoped_file.h" | 21 #include "base/files/scoped_file.h" |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 std::wstring ReadTextFile(const FilePath& filename) { | 241 std::wstring ReadTextFile(const FilePath& filename) { |
240 wchar_t contents[64]; | 242 wchar_t contents[64]; |
241 std::wifstream file; | 243 std::wifstream file; |
242 file.open(filename.value().c_str()); | 244 file.open(filename.value().c_str()); |
243 EXPECT_TRUE(file.is_open()); | 245 EXPECT_TRUE(file.is_open()); |
244 file.getline(contents, arraysize(contents)); | 246 file.getline(contents, arraysize(contents)); |
245 file.close(); | 247 file.close(); |
246 return std::wstring(contents); | 248 return std::wstring(contents); |
247 } | 249 } |
248 | 250 |
251 // 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.
| |
252 // child processes (i.e., HANDLE_FLAG_INHERIT is set on the underlying handle on | |
253 // Windows, or FD_CLOEXEC is not set on the underlying file descriptor on | |
254 // POSIX). | |
255 void GetIsInheritable(FILE* stream, bool* is_inheritable) { | |
256 #if defined(OS_WIN) | |
257 HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(stream))); | |
258 ASSERT_NE(INVALID_HANDLE_VALUE, handle); | |
259 | |
260 DWORD info = 0; | |
261 ASSERT_EQ(TRUE, ::GetHandleInformation(handle, &info)); | |
262 *is_inheritable = ((info & HANDLE_FLAG_INHERIT) != 0); | |
263 #elif defined(OS_POSIX) | |
264 int fd = fileno(stream); | |
265 ASSERT_NE(-1, fd); | |
266 int flags = fcntl(fd, F_GETFD, 0); | |
267 ASSERT_NE(-1, flags); | |
268 *is_inheritable = ((flags & FD_CLOEXEC) == 0); | |
269 #endif | |
270 } | |
271 | |
249 TEST_F(FileUtilTest, FileAndDirectorySize) { | 272 TEST_F(FileUtilTest, FileAndDirectorySize) { |
250 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize | 273 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize |
251 // should return 53 bytes. | 274 // should return 53 bytes. |
252 FilePath file_01 = temp_dir_.GetPath().Append(FPL("The file 01.txt")); | 275 FilePath file_01 = temp_dir_.GetPath().Append(FPL("The file 01.txt")); |
253 CreateTextFile(file_01, L"12345678901234567890"); | 276 CreateTextFile(file_01, L"12345678901234567890"); |
254 int64_t size_f1 = 0; | 277 int64_t size_f1 = 0; |
255 ASSERT_TRUE(GetFileSize(file_01, &size_f1)); | 278 ASSERT_TRUE(GetFileSize(file_01, &size_f1)); |
256 EXPECT_EQ(20ll, size_f1); | 279 EXPECT_EQ(20ll, size_f1); |
257 | 280 |
258 FilePath subdir_path = temp_dir_.GetPath().Append(FPL("Level2")); | 281 FilePath subdir_path = temp_dir_.GetPath().Append(FPL("Level2")); |
(...skipping 1443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1702 } | 1725 } |
1703 | 1726 |
1704 // Restore the original $TMP. | 1727 // Restore the original $TMP. |
1705 if (original_tmp) { | 1728 if (original_tmp) { |
1706 ::_tputenv_s(kTmpKey, original_tmp); | 1729 ::_tputenv_s(kTmpKey, original_tmp); |
1707 free(original_tmp); | 1730 free(original_tmp); |
1708 } else { | 1731 } else { |
1709 ::_tputenv_s(kTmpKey, _T("")); | 1732 ::_tputenv_s(kTmpKey, _T("")); |
1710 } | 1733 } |
1711 } | 1734 } |
1735 | |
1712 #endif // OS_WIN | 1736 #endif // OS_WIN |
1713 | 1737 |
1738 // Test that files opened by OpenFile are not set up for inheritance into child | |
1739 // procs. | |
1740 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
| |
1741 FilePath file_path(temp_dir_.GetPath().Append(FPL("a_file"))); | |
1742 | |
1743 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.
| |
1744 ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_path, L"Geepers")); | |
1745 FILE* file = OpenFile(file_path, mode); | |
1746 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
| |
1747 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.
| |
1748 ASSERT_NO_FATAL_FAILURE(GetIsInheritable(file, &is_inheritable)); | |
1749 EXPECT_FALSE(is_inheritable); | |
1750 ASSERT_TRUE(CloseFile(file)); | |
1751 ASSERT_TRUE(DeleteFile(file_path, false)); | |
1752 } | |
1753 } | |
1754 | |
1714 TEST_F(FileUtilTest, CreateTemporaryFileTest) { | 1755 TEST_F(FileUtilTest, CreateTemporaryFileTest) { |
1715 FilePath temp_files[3]; | 1756 FilePath temp_files[3]; |
1716 for (int i = 0; i < 3; i++) { | 1757 for (int i = 0; i < 3; i++) { |
1717 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i]))); | 1758 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i]))); |
1718 EXPECT_TRUE(PathExists(temp_files[i])); | 1759 EXPECT_TRUE(PathExists(temp_files[i])); |
1719 EXPECT_FALSE(DirectoryExists(temp_files[i])); | 1760 EXPECT_FALSE(DirectoryExists(temp_files[i])); |
1720 } | 1761 } |
1721 for (int i = 0; i < 3; i++) | 1762 for (int i = 0; i < 3; i++) |
1722 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); | 1763 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); |
1723 for (int i = 0; i < 3; i++) | 1764 for (int i = 0; i < 3; i++) |
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2568 // Trying to close it should crash. This is important for security. | 2609 // Trying to close it should crash. This is important for security. |
2569 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); | 2610 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); |
2570 #endif | 2611 #endif |
2571 } | 2612 } |
2572 | 2613 |
2573 #endif // defined(OS_POSIX) | 2614 #endif // defined(OS_POSIX) |
2574 | 2615 |
2575 } // namespace | 2616 } // namespace |
2576 | 2617 |
2577 } // namespace base | 2618 } // namespace base |
OLD | NEW |