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> |
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> |
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 |
| 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 #else |
| 270 FAIL() << "Unimplemented"; |
| 271 #endif |
| 272 } |
| 273 |
249 TEST_F(FileUtilTest, FileAndDirectorySize) { | 274 TEST_F(FileUtilTest, FileAndDirectorySize) { |
250 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize | 275 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize |
251 // should return 53 bytes. | 276 // should return 53 bytes. |
252 FilePath file_01 = temp_dir_.GetPath().Append(FPL("The file 01.txt")); | 277 FilePath file_01 = temp_dir_.GetPath().Append(FPL("The file 01.txt")); |
253 CreateTextFile(file_01, L"12345678901234567890"); | 278 CreateTextFile(file_01, L"12345678901234567890"); |
254 int64_t size_f1 = 0; | 279 int64_t size_f1 = 0; |
255 ASSERT_TRUE(GetFileSize(file_01, &size_f1)); | 280 ASSERT_TRUE(GetFileSize(file_01, &size_f1)); |
256 EXPECT_EQ(20ll, size_f1); | 281 EXPECT_EQ(20ll, size_f1); |
257 | 282 |
258 FilePath subdir_path = temp_dir_.GetPath().Append(FPL("Level2")); | 283 FilePath subdir_path = temp_dir_.GetPath().Append(FPL("Level2")); |
(...skipping 1443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1702 } | 1727 } |
1703 | 1728 |
1704 // Restore the original $TMP. | 1729 // Restore the original $TMP. |
1705 if (original_tmp) { | 1730 if (original_tmp) { |
1706 ::_tputenv_s(kTmpKey, original_tmp); | 1731 ::_tputenv_s(kTmpKey, original_tmp); |
1707 free(original_tmp); | 1732 free(original_tmp); |
1708 } else { | 1733 } else { |
1709 ::_tputenv_s(kTmpKey, _T("")); | 1734 ::_tputenv_s(kTmpKey, _T("")); |
1710 } | 1735 } |
1711 } | 1736 } |
| 1737 |
1712 #endif // OS_WIN | 1738 #endif // OS_WIN |
1713 | 1739 |
| 1740 // Test that files opened by OpenFile are not set up for inheritance into child |
| 1741 // procs. |
| 1742 TEST_F(FileUtilTest, OpenFileNoInheritance) { |
| 1743 FilePath file_path(temp_dir_.GetPath().Append(FPL("a_file"))); |
| 1744 |
| 1745 for (const char* mode : {"wb", "r,ccs=UNICODE"}) { |
| 1746 ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_path, L"Geepers")); |
| 1747 FILE* file = OpenFile(file_path, mode); |
| 1748 ASSERT_NE(nullptr, file); |
| 1749 bool is_inheritable = false; |
| 1750 ASSERT_NO_FATAL_FAILURE(GetIsInheritable(file, &is_inheritable)); |
| 1751 EXPECT_FALSE(is_inheritable); |
| 1752 ASSERT_TRUE(CloseFile(file)); |
| 1753 ASSERT_TRUE(DeleteFile(file_path, false)); |
| 1754 } |
| 1755 } |
| 1756 |
1714 TEST_F(FileUtilTest, CreateTemporaryFileTest) { | 1757 TEST_F(FileUtilTest, CreateTemporaryFileTest) { |
1715 FilePath temp_files[3]; | 1758 FilePath temp_files[3]; |
1716 for (int i = 0; i < 3; i++) { | 1759 for (int i = 0; i < 3; i++) { |
1717 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i]))); | 1760 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i]))); |
1718 EXPECT_TRUE(PathExists(temp_files[i])); | 1761 EXPECT_TRUE(PathExists(temp_files[i])); |
1719 EXPECT_FALSE(DirectoryExists(temp_files[i])); | 1762 EXPECT_FALSE(DirectoryExists(temp_files[i])); |
1720 } | 1763 } |
1721 for (int i = 0; i < 3; i++) | 1764 for (int i = 0; i < 3; i++) |
1722 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); | 1765 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); |
1723 for (int i = 0; i < 3; i++) | 1766 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. | 2611 // Trying to close it should crash. This is important for security. |
2569 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); | 2612 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); |
2570 #endif | 2613 #endif |
2571 } | 2614 } |
2572 | 2615 |
2573 #endif // defined(OS_POSIX) | 2616 #endif // defined(OS_POSIX) |
2574 | 2617 |
2575 } // namespace | 2618 } // namespace |
2576 | 2619 |
2577 } // namespace base | 2620 } // namespace base |
OLD | NEW |