OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #define _CRT_SECURE_NO_WARNINGS | 5 #define _CRT_SECURE_NO_WARNINGS |
6 | 6 |
7 #include "base/multiprocess_test.h" | 7 #include "base/multiprocess_test.h" |
8 #include "base/platform_thread.h" | 8 #include "base/platform_thread.h" |
9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
| 12 #if defined(OS_LINUX) |
| 13 #include <dlfcn.h> |
| 14 #endif |
| 15 #if defined(OS_POSIX) |
| 16 #include <fcntl.h> |
| 17 #include <sys/socket.h> |
| 18 #endif |
12 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
13 #include <windows.h> | 20 #include <windows.h> |
14 #elif defined(OS_LINUX) | |
15 #include <dlfcn.h> | |
16 #endif | 21 #endif |
17 | 22 |
18 namespace base { | 23 namespace base { |
19 | 24 |
20 class ProcessUtilTest : public MultiProcessTest { | 25 class ProcessUtilTest : public MultiProcessTest { |
21 }; | 26 }; |
22 | 27 |
23 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) { | 28 MULTIPROCESS_TEST_MAIN(SimpleChildProcess) { |
24 return 0; | 29 return 0; |
25 } | 30 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 EXPECT_GE(free_mem2.total, free_mem2.largest); | 135 EXPECT_GE(free_mem2.total, free_mem2.largest); |
131 EXPECT_GE(expected_total, free_mem2.total); | 136 EXPECT_GE(expected_total, free_mem2.total); |
132 EXPECT_GE(expected_largest, free_mem2.largest); | 137 EXPECT_GE(expected_largest, free_mem2.largest); |
133 EXPECT_TRUE(NULL != free_mem2.largest_ptr); | 138 EXPECT_TRUE(NULL != free_mem2.largest_ptr); |
134 | 139 |
135 delete[] alloc; | 140 delete[] alloc; |
136 delete metrics; | 141 delete metrics; |
137 } | 142 } |
138 #endif // defined(OS_WIN) | 143 #endif // defined(OS_WIN) |
139 | 144 |
| 145 #if defined(OS_POSIX) |
| 146 const int kChildPipe = 20; // FD # for write end of pipe in child process. |
| 147 MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess) { |
| 148 // This child process counts the number of open FDs, it then writes that |
| 149 // number out to a pipe connected to the parent. |
| 150 int num_open_files = 0; |
| 151 int write_pipe = kChildPipe; |
| 152 int max_files = GetMaxFilesOpenInProcess(); |
| 153 for (int i = STDERR_FILENO + 1; i < max_files; i++) { |
| 154 if (i != kChildPipe) { |
| 155 if (close(i) != -1) { |
| 156 LOG(WARNING) << "Leaked FD " << i; |
| 157 num_open_files += 1; |
| 158 } |
| 159 } |
| 160 } |
| 161 |
| 162 #if defined(OS_LINUX) |
| 163 // On Linux, '/etc/localtime' is opened before the test's main() enters. |
| 164 const int expected_num_open_fds = 1; |
| 165 num_open_files -= expected_num_open_fds; |
| 166 #endif // defined(OS_LINUX) |
| 167 |
| 168 write(write_pipe, &num_open_files, sizeof(num_open_files)); |
| 169 close(write_pipe); |
| 170 |
| 171 return 0; |
| 172 } |
| 173 |
| 174 TEST_F(ProcessUtilTest, FDRemapping) { |
| 175 // Open some files to check they don't get leaked to the child process. |
| 176 int fds[2]; |
| 177 pipe(fds); |
| 178 int pipe_read_fd = fds[0]; |
| 179 int pipe_write_fd = fds[1]; |
| 180 |
| 181 // open some dummy fds to make sure they don't propogate over to the |
| 182 // child process. |
| 183 int dev_null = open("/dev/null", O_RDONLY); |
| 184 int sockets[2]; |
| 185 socketpair(AF_UNIX, SOCK_STREAM, 0, sockets); |
| 186 |
| 187 file_handle_mapping_vector fd_mapping_vec; |
| 188 fd_mapping_vec.push_back(std::pair<int,int>(pipe_write_fd, kChildPipe)); |
| 189 ProcessHandle handle = this->SpawnChild(L"ProcessUtilsLeakFDChildProcess", |
| 190 fd_mapping_vec, |
| 191 false); |
| 192 ASSERT_NE(static_cast<ProcessHandle>(NULL), handle); |
| 193 close(pipe_write_fd); |
| 194 |
| 195 // Read number of open files in client process from pipe; |
| 196 int num_open_files = -1; |
| 197 ssize_t bytes_read = read(pipe_read_fd, &num_open_files, |
| 198 sizeof(num_open_files)); |
| 199 ASSERT_EQ(bytes_read, static_cast<ssize_t>(sizeof(num_open_files))); |
| 200 |
| 201 // Make sure 0 fds are leaked to the client. |
| 202 ASSERT_EQ(0, num_open_files); |
| 203 |
| 204 EXPECT_TRUE(WaitForSingleProcess(handle, 1000)); |
| 205 close(fds[0]); |
| 206 close(sockets[0]); |
| 207 close(sockets[1]); |
| 208 close(dev_null); |
| 209 } |
| 210 |
| 211 #endif // defined(OS_POSIX) |
| 212 |
140 } // namespace base | 213 } // namespace base |
OLD | NEW |