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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
8 #if defined(OS_MACOSX) | 8 #if defined(OS_MACOSX) |
9 extern "C" { | 9 extern "C" { |
10 #include <sandbox.h> | 10 #include <sandbox.h> |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "ipc/ipc_message_utils.h" | 26 #include "ipc/ipc_message_utils.h" |
27 #include "ipc/ipc_test_base.h" | 27 #include "ipc/ipc_test_base.h" |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 const unsigned kNumFDsToSend = 20; | 31 const unsigned kNumFDsToSend = 20; |
32 const char* kDevZeroPath = "/dev/zero"; | 32 const char* kDevZeroPath = "/dev/zero"; |
33 | 33 |
34 class MyChannelDescriptorListenerBase : public IPC::Listener { | 34 class MyChannelDescriptorListenerBase : public IPC::Listener { |
35 public: | 35 public: |
36 virtual bool OnMessageReceived(const IPC::Message& message) override { | 36 bool OnMessageReceived(const IPC::Message& message) override { |
37 PickleIterator iter(message); | 37 PickleIterator iter(message); |
38 | 38 |
39 base::FileDescriptor descriptor; | 39 base::FileDescriptor descriptor; |
40 | 40 |
41 IPC::ParamTraits<base::FileDescriptor>::Read(&message, &iter, &descriptor); | 41 IPC::ParamTraits<base::FileDescriptor>::Read(&message, &iter, &descriptor); |
42 | 42 |
43 HandleFD(descriptor.fd); | 43 HandleFD(descriptor.fd); |
44 return true; | 44 return true; |
45 } | 45 } |
46 | 46 |
47 protected: | 47 protected: |
48 virtual void HandleFD(int fd) = 0; | 48 virtual void HandleFD(int fd) = 0; |
49 }; | 49 }; |
50 | 50 |
51 class MyChannelDescriptorListener : public MyChannelDescriptorListenerBase { | 51 class MyChannelDescriptorListener : public MyChannelDescriptorListenerBase { |
52 public: | 52 public: |
53 explicit MyChannelDescriptorListener(ino_t expected_inode_num) | 53 explicit MyChannelDescriptorListener(ino_t expected_inode_num) |
54 : MyChannelDescriptorListenerBase(), | 54 : MyChannelDescriptorListenerBase(), |
55 expected_inode_num_(expected_inode_num), | 55 expected_inode_num_(expected_inode_num), |
56 num_fds_received_(0) { | 56 num_fds_received_(0) { |
57 } | 57 } |
58 | 58 |
59 bool GotExpectedNumberOfDescriptors() const { | 59 bool GotExpectedNumberOfDescriptors() const { |
60 return num_fds_received_ == kNumFDsToSend; | 60 return num_fds_received_ == kNumFDsToSend; |
61 } | 61 } |
62 | 62 |
63 virtual void OnChannelError() override { | 63 void OnChannelError() override { |
64 base::MessageLoop::current()->Quit(); | 64 base::MessageLoop::current()->Quit(); |
65 } | 65 } |
66 | 66 |
67 protected: | 67 protected: |
68 virtual void HandleFD(int fd) override { | 68 void HandleFD(int fd) override { |
69 // Check that we can read from the FD. | 69 // Check that we can read from the FD. |
70 char buf; | 70 char buf; |
71 ssize_t amt_read = read(fd, &buf, 1); | 71 ssize_t amt_read = read(fd, &buf, 1); |
72 ASSERT_EQ(amt_read, 1); | 72 ASSERT_EQ(amt_read, 1); |
73 ASSERT_EQ(buf, 0); // /dev/zero always reads 0 bytes. | 73 ASSERT_EQ(buf, 0); // /dev/zero always reads 0 bytes. |
74 | 74 |
75 struct stat st; | 75 struct stat st; |
76 ASSERT_EQ(fstat(fd, &st), 0); | 76 ASSERT_EQ(fstat(fd, &st), 0); |
77 | 77 |
78 ASSERT_EQ(close(fd), 0); | 78 ASSERT_EQ(close(fd), 0); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 | 195 |
196 | 196 |
197 class MyCBListener : public MyChannelDescriptorListenerBase { | 197 class MyCBListener : public MyChannelDescriptorListenerBase { |
198 public: | 198 public: |
199 MyCBListener(base::Callback<void(int)> cb, int fds_to_send) | 199 MyCBListener(base::Callback<void(int)> cb, int fds_to_send) |
200 : MyChannelDescriptorListenerBase(), | 200 : MyChannelDescriptorListenerBase(), |
201 cb_(cb) { | 201 cb_(cb) { |
202 } | 202 } |
203 | 203 |
204 protected: | 204 protected: |
205 virtual void HandleFD(int fd) override { | 205 void HandleFD(int fd) override { cb_.Run(fd); } |
206 cb_.Run(fd); | |
207 } | |
208 private: | 206 private: |
209 base::Callback<void(int)> cb_; | 207 base::Callback<void(int)> cb_; |
210 }; | 208 }; |
211 | 209 |
212 std::pair<int, int> make_socket_pair() { | 210 std::pair<int, int> make_socket_pair() { |
213 int pipe_fds[2]; | 211 int pipe_fds[2]; |
214 CHECK_EQ(0, HANDLE_EINTR(socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds))); | 212 CHECK_EQ(0, HANDLE_EINTR(socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds))); |
215 return std::pair<int, int>(pipe_fds[0], pipe_fds[1]); | 213 return std::pair<int, int>(pipe_fds[0], pipe_fds[1]); |
216 } | 214 } |
217 | 215 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 base::WaitableEvent received_; | 365 base::WaitableEvent received_; |
368 }; | 366 }; |
369 | 367 |
370 TEST_F(IPCMultiSendingFdsTest, StressTest) { | 368 TEST_F(IPCMultiSendingFdsTest, StressTest) { |
371 Run(); | 369 Run(); |
372 } | 370 } |
373 | 371 |
374 } // namespace | 372 } // namespace |
375 | 373 |
376 #endif // defined(OS_POSIX) | 374 #endif // defined(OS_POSIX) |
OLD | NEW |