OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/edk/embedder/platform_channel_pair.h" | 5 #include "mojo/edk/embedder/platform_channel_pair.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <poll.h> | 8 #include <poll.h> |
9 #include <signal.h> | 9 #include <signal.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 105 |
106 TEST_F(PlatformChannelPairPosixTest, SendReceiveData) { | 106 TEST_F(PlatformChannelPairPosixTest, SendReceiveData) { |
107 PlatformChannelPair channel_pair; | 107 PlatformChannelPair channel_pair; |
108 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); | 108 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle().Pass(); |
109 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); | 109 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle().Pass(); |
110 | 110 |
111 for (size_t i = 0; i < 10; i++) { | 111 for (size_t i = 0; i < 10; i++) { |
112 std::string send_string(1 << i, 'A' + i); | 112 std::string send_string(1 << i, 'A' + i); |
113 | 113 |
114 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), | 114 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), |
115 PlatformChannelWrite(server_handle.get(), send_string.data(), | 115 PlatformChannelWrite( |
116 send_string.size())); | 116 server_handle.get(), send_string.data(), send_string.size())); |
117 | 117 |
118 WaitReadable(client_handle.get()); | 118 WaitReadable(client_handle.get()); |
119 | 119 |
120 char buf[10000] = {}; | 120 char buf[10000] = {}; |
121 std::deque<PlatformHandle> received_handles; | 121 std::deque<PlatformHandle> received_handles; |
122 ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf, | 122 ssize_t result = PlatformChannelRecvmsg( |
123 sizeof(buf), &received_handles); | 123 client_handle.get(), buf, sizeof(buf), &received_handles); |
124 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result); | 124 EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result); |
125 EXPECT_EQ(send_string, std::string(buf, static_cast<size_t>(result))); | 125 EXPECT_EQ(send_string, std::string(buf, static_cast<size_t>(result))); |
126 EXPECT_TRUE(received_handles.empty()); | 126 EXPECT_TRUE(received_handles.empty()); |
127 } | 127 } |
128 } | 128 } |
129 | 129 |
130 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) { | 130 TEST_F(PlatformChannelPairPosixTest, SendReceiveFDs) { |
131 base::ScopedTempDir temp_dir; | 131 base::ScopedTempDir temp_dir; |
132 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 132 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
133 | 133 |
(...skipping 14 matching lines...) Expand all Loading... |
148 ASSERT_EQ(j, fwrite(std::string(j, '0' + i).data(), 1, j, fp.get())); | 148 ASSERT_EQ(j, fwrite(std::string(j, '0' + i).data(), 1, j, fp.get())); |
149 platform_handles.push_back( | 149 platform_handles.push_back( |
150 test::PlatformHandleFromFILE(fp.Pass()).release()); | 150 test::PlatformHandleFromFILE(fp.Pass()).release()); |
151 ASSERT_TRUE(platform_handles.back().is_valid()); | 151 ASSERT_TRUE(platform_handles.back().is_valid()); |
152 } | 152 } |
153 | 153 |
154 // Send the FDs (+ "hello"). | 154 // Send the FDs (+ "hello"). |
155 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; | 155 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; |
156 // We assume that the |sendmsg()| actually sends all the data. | 156 // We assume that the |sendmsg()| actually sends all the data. |
157 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 157 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
158 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, | 158 PlatformChannelSendmsgWithHandles(server_handle.get(), |
| 159 &iov, |
| 160 1, |
159 &platform_handles[0], | 161 &platform_handles[0], |
160 platform_handles.size())); | 162 platform_handles.size())); |
161 | 163 |
162 WaitReadable(client_handle.get()); | 164 WaitReadable(client_handle.get()); |
163 | 165 |
164 char buf[100] = {}; | 166 char buf[100] = {}; |
165 std::deque<PlatformHandle> received_handles; | 167 std::deque<PlatformHandle> received_handles; |
166 // We assume that the |recvmsg()| actually reads all the data. | 168 // We assume that the |recvmsg()| actually reads all the data. |
167 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 169 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
168 PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), | 170 PlatformChannelRecvmsg( |
169 &received_handles)); | 171 client_handle.get(), buf, sizeof(buf), &received_handles)); |
170 EXPECT_STREQ(kHello, buf); | 172 EXPECT_STREQ(kHello, buf); |
171 EXPECT_EQ(i, received_handles.size()); | 173 EXPECT_EQ(i, received_handles.size()); |
172 | 174 |
173 for (size_t j = 0; !received_handles.empty(); j++) { | 175 for (size_t j = 0; !received_handles.empty(); j++) { |
174 base::ScopedFILE fp(test::FILEFromPlatformHandle( | 176 base::ScopedFILE fp(test::FILEFromPlatformHandle( |
175 ScopedPlatformHandle(received_handles.front()), "rb")); | 177 ScopedPlatformHandle(received_handles.front()), "rb")); |
176 received_handles.pop_front(); | 178 received_handles.pop_front(); |
177 ASSERT_TRUE(fp); | 179 ASSERT_TRUE(fp); |
178 rewind(fp.get()); | 180 rewind(fp.get()); |
179 char read_buf[100]; | 181 char read_buf[100]; |
(...skipping 25 matching lines...) Expand all Loading... |
205 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); | 207 fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); |
206 PlatformHandleVector platform_handles; | 208 PlatformHandleVector platform_handles; |
207 platform_handles.push_back( | 209 platform_handles.push_back( |
208 test::PlatformHandleFromFILE(fp.Pass()).release()); | 210 test::PlatformHandleFromFILE(fp.Pass()).release()); |
209 ASSERT_TRUE(platform_handles.back().is_valid()); | 211 ASSERT_TRUE(platform_handles.back().is_valid()); |
210 | 212 |
211 // Send the FD (+ "hello"). | 213 // Send the FD (+ "hello"). |
212 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; | 214 struct iovec iov = {const_cast<char*>(kHello), sizeof(kHello)}; |
213 // We assume that the |sendmsg()| actually sends all the data. | 215 // We assume that the |sendmsg()| actually sends all the data. |
214 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 216 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
215 PlatformChannelSendmsgWithHandles(server_handle.get(), &iov, 1, | 217 PlatformChannelSendmsgWithHandles(server_handle.get(), |
| 218 &iov, |
| 219 1, |
216 &platform_handles[0], | 220 &platform_handles[0], |
217 platform_handles.size())); | 221 platform_handles.size())); |
218 } | 222 } |
219 | 223 |
220 WaitReadable(client_handle.get()); | 224 WaitReadable(client_handle.get()); |
221 | 225 |
222 // Start with an invalid handle in the deque. | 226 // Start with an invalid handle in the deque. |
223 std::deque<PlatformHandle> received_handles; | 227 std::deque<PlatformHandle> received_handles; |
224 received_handles.push_back(PlatformHandle()); | 228 received_handles.push_back(PlatformHandle()); |
225 | 229 |
226 char buf[100] = {}; | 230 char buf[100] = {}; |
227 // We assume that the |recvmsg()| actually reads all the data. | 231 // We assume that the |recvmsg()| actually reads all the data. |
228 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), | 232 EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)), |
229 PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf), | 233 PlatformChannelRecvmsg( |
230 &received_handles)); | 234 client_handle.get(), buf, sizeof(buf), &received_handles)); |
231 EXPECT_STREQ(kHello, buf); | 235 EXPECT_STREQ(kHello, buf); |
232 ASSERT_EQ(2u, received_handles.size()); | 236 ASSERT_EQ(2u, received_handles.size()); |
233 EXPECT_FALSE(received_handles[0].is_valid()); | 237 EXPECT_FALSE(received_handles[0].is_valid()); |
234 EXPECT_TRUE(received_handles[1].is_valid()); | 238 EXPECT_TRUE(received_handles[1].is_valid()); |
235 | 239 |
236 { | 240 { |
237 base::ScopedFILE fp(test::FILEFromPlatformHandle( | 241 base::ScopedFILE fp(test::FILEFromPlatformHandle( |
238 ScopedPlatformHandle(received_handles[1]), "rb")); | 242 ScopedPlatformHandle(received_handles[1]), "rb")); |
239 received_handles[1] = PlatformHandle(); | 243 received_handles[1] = PlatformHandle(); |
240 ASSERT_TRUE(fp); | 244 ASSERT_TRUE(fp); |
241 rewind(fp.get()); | 245 rewind(fp.get()); |
242 char read_buf[100]; | 246 char read_buf[100]; |
243 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); | 247 size_t bytes_read = fread(read_buf, 1, sizeof(read_buf), fp.get()); |
244 EXPECT_EQ(file_contents.size(), bytes_read); | 248 EXPECT_EQ(file_contents.size(), bytes_read); |
245 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); | 249 EXPECT_EQ(file_contents, std::string(read_buf, bytes_read)); |
246 } | 250 } |
247 } | 251 } |
248 | 252 |
249 } // namespace | 253 } // namespace |
250 } // namespace embedder | 254 } // namespace embedder |
251 } // namespace mojo | 255 } // namespace mojo |
OLD | NEW |