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 { base::MessageLoop::current()->Quit(); } |
agl
2014/10/21 17:49:50
ditto (and elsewhere).
dcheng
2014/10/21 17:57:44
See previous comment.
| |
64 base::MessageLoop::current()->Quit(); | |
65 } | |
66 | 64 |
67 protected: | 65 protected: |
68 virtual void HandleFD(int fd) override { | 66 void HandleFD(int fd) override { |
69 // Check that we can read from the FD. | 67 // Check that we can read from the FD. |
70 char buf; | 68 char buf; |
71 ssize_t amt_read = read(fd, &buf, 1); | 69 ssize_t amt_read = read(fd, &buf, 1); |
72 ASSERT_EQ(amt_read, 1); | 70 ASSERT_EQ(amt_read, 1); |
73 ASSERT_EQ(buf, 0); // /dev/zero always reads 0 bytes. | 71 ASSERT_EQ(buf, 0); // /dev/zero always reads 0 bytes. |
74 | 72 |
75 struct stat st; | 73 struct stat st; |
76 ASSERT_EQ(fstat(fd, &st), 0); | 74 ASSERT_EQ(fstat(fd, &st), 0); |
77 | 75 |
78 ASSERT_EQ(close(fd), 0); | 76 ASSERT_EQ(close(fd), 0); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 | 193 |
196 | 194 |
197 class MyCBListener : public MyChannelDescriptorListenerBase { | 195 class MyCBListener : public MyChannelDescriptorListenerBase { |
198 public: | 196 public: |
199 MyCBListener(base::Callback<void(int)> cb, int fds_to_send) | 197 MyCBListener(base::Callback<void(int)> cb, int fds_to_send) |
200 : MyChannelDescriptorListenerBase(), | 198 : MyChannelDescriptorListenerBase(), |
201 cb_(cb) { | 199 cb_(cb) { |
202 } | 200 } |
203 | 201 |
204 protected: | 202 protected: |
205 virtual void HandleFD(int fd) override { | 203 void HandleFD(int fd) override { cb_.Run(fd); } |
206 cb_.Run(fd); | |
207 } | |
208 private: | 204 private: |
209 base::Callback<void(int)> cb_; | 205 base::Callback<void(int)> cb_; |
210 }; | 206 }; |
211 | 207 |
212 std::pair<int, int> make_socket_pair() { | 208 std::pair<int, int> make_socket_pair() { |
213 int pipe_fds[2]; | 209 int pipe_fds[2]; |
214 CHECK_EQ(0, HANDLE_EINTR(socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds))); | 210 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]); | 211 return std::pair<int, int>(pipe_fds[0], pipe_fds[1]); |
216 } | 212 } |
217 | 213 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 base::WaitableEvent received_; | 363 base::WaitableEvent received_; |
368 }; | 364 }; |
369 | 365 |
370 TEST_F(IPCMultiSendingFdsTest, StressTest) { | 366 TEST_F(IPCMultiSendingFdsTest, StressTest) { |
371 Run(); | 367 Run(); |
372 } | 368 } |
373 | 369 |
374 } // namespace | 370 } // namespace |
375 | 371 |
376 #endif // defined(OS_POSIX) | 372 #endif // defined(OS_POSIX) |
OLD | NEW |