Chromium Code Reviews| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <fstream> | 9 #include <fstream> |
| 10 #include <initializer_list> | |
| 10 #include <set> | 11 #include <set> |
| 11 #include <utility> | 12 #include <utility> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/base_paths.h" | 15 #include "base/base_paths.h" |
| 15 #include "base/environment.h" | 16 #include "base/environment.h" |
| 16 #include "base/files/file_enumerator.h" | 17 #include "base/files/file_enumerator.h" |
| 17 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 18 #include "base/files/file_util.h" | 19 #include "base/files/file_util.h" |
| 19 #include "base/files/scoped_file.h" | 20 #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) { | 240 std::wstring ReadTextFile(const FilePath& filename) { |
| 240 wchar_t contents[64]; | 241 wchar_t contents[64]; |
| 241 std::wifstream file; | 242 std::wifstream file; |
| 242 file.open(filename.value().c_str()); | 243 file.open(filename.value().c_str()); |
| 243 EXPECT_TRUE(file.is_open()); | 244 EXPECT_TRUE(file.is_open()); |
| 244 file.getline(contents, arraysize(contents)); | 245 file.getline(contents, arraysize(contents)); |
| 245 file.close(); | 246 file.close(); |
| 246 return std::wstring(contents); | 247 return std::wstring(contents); |
| 247 } | 248 } |
| 248 | 249 |
| 250 // Sets |is_inheritable| to indicate whether or not |stream| is set up to be | |
| 251 // inerhited into child processes (i.e., HANDLE_FLAG_INHERIT is set on the | |
| 252 // underlying handle on Windows, or FD_CLOEXEC is not set on the underlying file | |
| 253 // descriptor on POSIX). Calls to this function must be wrapped with | |
| 254 // ASSERT_NO_FATAL_FAILURE to properly abort tests in case of fatal failure. | |
| 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 1445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 } |
| 1712 #endif // OS_WIN | 1737 #endif // OS_WIN |
| 1713 | 1738 |
| 1739 // Test that files opened by OpenFile are not set up for inheritance into child | |
| 1740 // procs. | |
| 1741 TEST_F(FileUtilTest, OpenFileNoInheritance) { | |
| 1742 FilePath file_path(temp_dir_.GetPath().Append(FPL("a_file"))); | |
| 1743 | |
| 1744 for (const char* mode : {"wb", "r,ccs=UNICODE"}) { | |
| 1745 SCOPED_TRACE(mode); | |
| 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 = true; | |
| 1750 ASSERT_NO_FATAL_FAILURE(GetIsInheritable(file, &is_inheritable)); | |
|
Mark Mentovai
2017/02/11 00:52:29
You do want to reach CloseFile() even if this fail
grt (UTC plus 2)
2017/02/13 07:53:28
Good point. Moved CloseFile into a ScopedClosureRu
| |
| 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 |