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

Side by Side Diff: chrome/common/ipc_send_fds_test.cc

Issue 21238: Fix error checking for mmap() for POSIX shared memory. (Closed)
Patch Set: Created 11 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/shared_memory_posix.cc ('k') | no next file » | 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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 #include "chrome/common/ipc_tests.h" 7 #include "chrome/common/ipc_tests.h"
8 8
9 #if defined(OS_MACOSX) 9 #if defined(OS_MACOSX)
10 extern "C" { 10 extern "C" {
11 #include <sandbox.h> 11 #include <sandbox.h>
12 } 12 }
13 #endif 13 #endif
14 #include <sys/stat.h> 14 #include <sys/stat.h>
15 15
16 #include "base/message_loop.h" 16 #include "base/message_loop.h"
17 #include "chrome/common/ipc_channel.h" 17 #include "chrome/common/ipc_channel.h"
18 #include "chrome/common/ipc_message_utils.h" 18 #include "chrome/common/ipc_message_utils.h"
19 19
20 #if defined(OS_POSIX) 20 #if defined(OS_POSIX)
21 21
22 namespace { 22 namespace {
23 23
24 const int kNumFDsToSend = 200;
24 const char* kDevRandomPath = "/dev/random"; 25 const char* kDevRandomPath = "/dev/random";
25 26
26 static void VerifyAndCloseDescriptor(int fd, ino_t inode_num) { 27 static void VerifyAndCloseDescriptor(int fd, ino_t inode_num) {
27 // Check that we can read from the FD. 28 // Check that we can read from the FD.
28 char buf; 29 char buf;
29 ssize_t amt_read = read(fd, &buf, 1); 30 ssize_t amt_read = read(fd, &buf, 1);
30 ASSERT_EQ(amt_read, 1); 31 ASSERT_EQ(amt_read, 1);
31 32
32 struct stat st; 33 struct stat st;
33 ASSERT_EQ(fstat(fd, &st), 0); 34 ASSERT_EQ(fstat(fd, &st), 0);
34 35
35 ASSERT_EQ(close(fd), 0); 36 ASSERT_EQ(close(fd), 0);
36 37
37 // We compare iNode numbers to check that the file sent over the wire 38 // We compare iNode numbers to check that the file sent over the wire
38 // was actually the same physical file as the one we were expecting. 39 // was actually the same physical file as the one we were expecting.
39 ASSERT_EQ(inode_num, st.st_ino); 40 ASSERT_EQ(inode_num, st.st_ino);
40 } 41 }
41 42
42 class MyChannelDescriptorListener : public IPC::Channel::Listener { 43 class MyChannelDescriptorListener : public IPC::Channel::Listener {
43 public: 44 public:
44 MyChannelDescriptorListener(ino_t expected_inode_num) 45 MyChannelDescriptorListener(ino_t expected_inode_num)
45 : expected_inode_num_(expected_inode_num) {} 46 : expected_inode_num_(expected_inode_num),
47 num_fds_received_(0) {}
46 48
47 virtual void OnMessageReceived(const IPC::Message& message) { 49 virtual void OnMessageReceived(const IPC::Message& message) {
48 void* iter = NULL; 50 void* iter = NULL;
49 51
52 ++num_fds_received_;
50 FileDescriptor descriptor; 53 FileDescriptor descriptor;
51
52 ASSERT_TRUE( 54 ASSERT_TRUE(
53 IPC::ParamTraits<FileDescriptor>::Read(&message, &iter, &descriptor)); 55 IPC::ParamTraits<FileDescriptor>::Read(&message, &iter, &descriptor));
54 56
55 VerifyAndCloseDescriptor(descriptor.fd, expected_inode_num_); 57 VerifyAndCloseDescriptor(descriptor.fd, expected_inode_num_);
56 MessageLoop::current()->Quit(); 58
59 if (num_fds_received_ == kNumFDsToSend) {
60 MessageLoop::current()->Quit();
61 }
57 } 62 }
58 63
59 virtual void OnChannelError() { 64 virtual void OnChannelError() {
60 MessageLoop::current()->Quit(); 65 MessageLoop::current()->Quit();
61 } 66 }
62 private: 67 private:
63 ino_t expected_inode_num_; 68 ino_t expected_inode_num_;
69 int num_fds_received_;
64 }; 70 };
65 71
66 void TestDescriptorServer(IPC::Channel &chan, 72 void TestDescriptorServer(IPC::Channel &chan,
67 base::ProcessHandle process_handle) { 73 base::ProcessHandle process_handle) {
68 ASSERT_TRUE(process_handle); 74 ASSERT_TRUE(process_handle);
69 75
70 FileDescriptor descriptor; 76 for (int i = 0; i < kNumFDsToSend; ++i) {
71 const int fd = open(kDevRandomPath, O_RDONLY); 77 FileDescriptor descriptor;
72 ASSERT_GE(fd, 0); 78 const int fd = open(kDevRandomPath, O_RDONLY);
73 descriptor.auto_close = true; 79 ASSERT_GE(fd, 0);
74 descriptor.fd = fd; 80 descriptor.auto_close = true;
81 descriptor.fd = fd;
75 82
76 IPC::Message* message = new IPC::Message(0, // routing_id 83 IPC::Message* message = new IPC::Message(0, // routing_id
77 3, // message type 84 3, // message type
78 IPC::Message::PRIORITY_NORMAL); 85 IPC::Message::PRIORITY_NORMAL);
79 IPC::ParamTraits<FileDescriptor>::Write(message, descriptor); 86 IPC::ParamTraits<FileDescriptor>::Write(message, descriptor);
80 chan.Send(message); 87 chan.Send(message);
88 }
81 89
82 // Run message loop. 90 // Run message loop.
83 MessageLoop::current()->Run(); 91 MessageLoop::current()->Run();
84 92
85 // Close Channel so client gets its OnChannelError() callback fired. 93 // Close Channel so client gets its OnChannelError() callback fired.
86 chan.Close(); 94 chan.Close();
87 95
88 // Cleanup child process. 96 // Cleanup child process.
89 EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000)); 97 EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000));
90 } 98 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_SERVER, 174 IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_SERVER,
167 &listener); 175 &listener);
168 chan.Connect(); 176 chan.Connect();
169 177
170 base::ProcessHandle process_handle = SpawnChild(TEST_DESCRIPTOR_CLIENT, 178 base::ProcessHandle process_handle = SpawnChild(TEST_DESCRIPTOR_CLIENT,
171 &chan); 179 &chan);
172 TestDescriptorServer(chan, process_handle); 180 TestDescriptorServer(chan, process_handle);
173 } 181 }
174 182
175 #endif // defined(OS_POSIX) 183 #endif // defined(OS_POSIX)
OLDNEW
« no previous file with comments | « base/shared_memory_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698