Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: base/files/file_util_unittest.cc

Issue 2687713003: Stop base::OpenFile from leaking fds/handles into child procs. (Closed)
Patch Set: gab comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/file_util_posix.cc ('k') | base/files/file_util_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
16 #include "base/bind.h"
17 #include "base/bind_helpers.h"
18 #include "base/callback_helpers.h"
15 #include "base/environment.h" 19 #include "base/environment.h"
16 #include "base/files/file_enumerator.h" 20 #include "base/files/file_enumerator.h"
17 #include "base/files/file_path.h" 21 #include "base/files/file_path.h"
18 #include "base/files/file_util.h" 22 #include "base/files/file_util.h"
19 #include "base/files/scoped_file.h" 23 #include "base/files/scoped_file.h"
20 #include "base/files/scoped_temp_dir.h" 24 #include "base/files/scoped_temp_dir.h"
21 #include "base/macros.h" 25 #include "base/macros.h"
22 #include "base/path_service.h" 26 #include "base/path_service.h"
23 #include "base/strings/string_util.h" 27 #include "base/strings/string_util.h"
24 #include "base/strings/utf_string_conversions.h" 28 #include "base/strings/utf_string_conversions.h"
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 std::wstring ReadTextFile(const FilePath& filename) { 243 std::wstring ReadTextFile(const FilePath& filename) {
240 wchar_t contents[64]; 244 wchar_t contents[64];
241 std::wifstream file; 245 std::wifstream file;
242 file.open(filename.value().c_str()); 246 file.open(filename.value().c_str());
243 EXPECT_TRUE(file.is_open()); 247 EXPECT_TRUE(file.is_open());
244 file.getline(contents, arraysize(contents)); 248 file.getline(contents, arraysize(contents));
245 file.close(); 249 file.close();
246 return std::wstring(contents); 250 return std::wstring(contents);
247 } 251 }
248 252
253 // Sets |is_inheritable| to indicate whether or not |stream| is set up to be
254 // inerhited into child processes (i.e., HANDLE_FLAG_INHERIT is set on the
255 // underlying handle on Windows, or FD_CLOEXEC is not set on the underlying file
256 // descriptor on POSIX). Calls to this function must be wrapped with
257 // ASSERT_NO_FATAL_FAILURE to properly abort tests in case of fatal failure.
258 void GetIsInheritable(FILE* stream, bool* is_inheritable) {
259 #if defined(OS_WIN)
260 HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(stream)));
261 ASSERT_NE(INVALID_HANDLE_VALUE, handle);
262
263 DWORD info = 0;
264 ASSERT_EQ(TRUE, ::GetHandleInformation(handle, &info));
265 *is_inheritable = ((info & HANDLE_FLAG_INHERIT) != 0);
266 #elif defined(OS_POSIX)
267 int fd = fileno(stream);
268 ASSERT_NE(-1, fd);
269 int flags = fcntl(fd, F_GETFD, 0);
270 ASSERT_NE(-1, flags);
271 *is_inheritable = ((flags & FD_CLOEXEC) == 0);
272 #else
273 #error Not implemented
274 #endif
275 }
276
249 TEST_F(FileUtilTest, FileAndDirectorySize) { 277 TEST_F(FileUtilTest, FileAndDirectorySize) {
250 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize 278 // Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize
251 // should return 53 bytes. 279 // should return 53 bytes.
252 FilePath file_01 = temp_dir_.GetPath().Append(FPL("The file 01.txt")); 280 FilePath file_01 = temp_dir_.GetPath().Append(FPL("The file 01.txt"));
253 CreateTextFile(file_01, L"12345678901234567890"); 281 CreateTextFile(file_01, L"12345678901234567890");
254 int64_t size_f1 = 0; 282 int64_t size_f1 = 0;
255 ASSERT_TRUE(GetFileSize(file_01, &size_f1)); 283 ASSERT_TRUE(GetFileSize(file_01, &size_f1));
256 EXPECT_EQ(20ll, size_f1); 284 EXPECT_EQ(20ll, size_f1);
257 285
258 FilePath subdir_path = temp_dir_.GetPath().Append(FPL("Level2")); 286 FilePath subdir_path = temp_dir_.GetPath().Append(FPL("Level2"));
(...skipping 1445 matching lines...) Expand 10 before | Expand all | Expand 10 after
1704 // Restore the original $TMP. 1732 // Restore the original $TMP.
1705 if (original_tmp) { 1733 if (original_tmp) {
1706 ::_tputenv_s(kTmpKey, original_tmp); 1734 ::_tputenv_s(kTmpKey, original_tmp);
1707 free(original_tmp); 1735 free(original_tmp);
1708 } else { 1736 } else {
1709 ::_tputenv_s(kTmpKey, _T("")); 1737 ::_tputenv_s(kTmpKey, _T(""));
1710 } 1738 }
1711 } 1739 }
1712 #endif // OS_WIN 1740 #endif // OS_WIN
1713 1741
1742 // Test that files opened by OpenFile are not set up for inheritance into child
1743 // procs.
1744 TEST_F(FileUtilTest, OpenFileNoInheritance) {
1745 FilePath file_path(temp_dir_.GetPath().Append(FPL("a_file")));
1746
1747 for (const char* mode : {"wb", "r,ccs=UNICODE"}) {
1748 SCOPED_TRACE(mode);
1749 ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_path, L"Geepers"));
1750 FILE* file = OpenFile(file_path, mode);
1751 ASSERT_NE(nullptr, file);
1752 {
1753 ScopedClosureRunner file_closer(Bind(IgnoreResult(&CloseFile), file));
1754 bool is_inheritable = true;
1755 ASSERT_NO_FATAL_FAILURE(GetIsInheritable(file, &is_inheritable));
1756 EXPECT_FALSE(is_inheritable);
1757 }
1758 ASSERT_TRUE(DeleteFile(file_path, false));
1759 }
1760 }
1761
1714 TEST_F(FileUtilTest, CreateTemporaryFileTest) { 1762 TEST_F(FileUtilTest, CreateTemporaryFileTest) {
1715 FilePath temp_files[3]; 1763 FilePath temp_files[3];
1716 for (int i = 0; i < 3; i++) { 1764 for (int i = 0; i < 3; i++) {
1717 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i]))); 1765 ASSERT_TRUE(CreateTemporaryFile(&(temp_files[i])));
1718 EXPECT_TRUE(PathExists(temp_files[i])); 1766 EXPECT_TRUE(PathExists(temp_files[i]));
1719 EXPECT_FALSE(DirectoryExists(temp_files[i])); 1767 EXPECT_FALSE(DirectoryExists(temp_files[i]));
1720 } 1768 }
1721 for (int i = 0; i < 3; i++) 1769 for (int i = 0; i < 3; i++)
1722 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]); 1770 EXPECT_FALSE(temp_files[i] == temp_files[(i+1)%3]);
1723 for (int i = 0; i < 3; i++) 1771 for (int i = 0; i < 3; i++)
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after
2568 // Trying to close it should crash. This is important for security. 2616 // Trying to close it should crash. This is important for security.
2569 EXPECT_DEATH(CloseWithScopedFD(fds[1]), ""); 2617 EXPECT_DEATH(CloseWithScopedFD(fds[1]), "");
2570 #endif 2618 #endif
2571 } 2619 }
2572 2620
2573 #endif // defined(OS_POSIX) 2621 #endif // defined(OS_POSIX)
2574 2622
2575 } // namespace 2623 } // namespace
2576 2624
2577 } // namespace base 2625 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_util_posix.cc ('k') | base/files/file_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698