OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stdint.h> | 5 #include <stdint.h> |
6 #include <stdio.h> | 6 #include <stdio.h> |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 CHECK_EQ(hss.satisfied_signals, 0u); | 65 CHECK_EQ(hss.satisfied_signals, 0u); |
66 CHECK_EQ(hss.satisfiable_signals, 0u); | 66 CHECK_EQ(hss.satisfiable_signals, 0u); |
67 break; | 67 break; |
68 } else { | 68 } else { |
69 CHECK((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 69 CHECK((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
70 CHECK((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 70 CHECK((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
71 } | 71 } |
72 | 72 |
73 std::string read_buffer(1000, '\0'); | 73 std::string read_buffer(1000, '\0'); |
74 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); | 74 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); |
75 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 75 CHECK_EQ(mp->ReadMessage(0, |
76 MakeUserPointer(&read_buffer_size), nullptr, | 76 UserPointer<void>(&read_buffer[0]), |
77 nullptr, MOJO_READ_MESSAGE_FLAG_NONE), | 77 MakeUserPointer(&read_buffer_size), |
| 78 nullptr, |
| 79 nullptr, |
| 80 MOJO_READ_MESSAGE_FLAG_NONE), |
78 MOJO_RESULT_OK); | 81 MOJO_RESULT_OK); |
79 read_buffer.resize(read_buffer_size); | 82 read_buffer.resize(read_buffer_size); |
80 VLOG(2) << "Child got: " << read_buffer; | 83 VLOG(2) << "Child got: " << read_buffer; |
81 | 84 |
82 if (read_buffer == quitquitquit) { | 85 if (read_buffer == quitquitquit) { |
83 VLOG(2) << "Child quitting."; | 86 VLOG(2) << "Child quitting."; |
84 break; | 87 break; |
85 } | 88 } |
86 | 89 |
87 std::string write_buffer = read_buffer + read_buffer; | 90 std::string write_buffer = read_buffer + read_buffer; |
88 CHECK_EQ(mp->WriteMessage(0, UserPointer<const void>(write_buffer.data()), | 91 CHECK_EQ(mp->WriteMessage(0, |
| 92 UserPointer<const void>(write_buffer.data()), |
89 static_cast<uint32_t>(write_buffer.size()), | 93 static_cast<uint32_t>(write_buffer.size()), |
90 nullptr, MOJO_WRITE_MESSAGE_FLAG_NONE), | 94 nullptr, |
| 95 MOJO_WRITE_MESSAGE_FLAG_NONE), |
91 MOJO_RESULT_OK); | 96 MOJO_RESULT_OK); |
92 } | 97 } |
93 | 98 |
94 mp->Close(0); | 99 mp->Close(0); |
95 return rv; | 100 return rv; |
96 } | 101 } |
97 | 102 |
98 // Sends "hello" to child, and expects "hellohello" back. | 103 // Sends "hello" to child, and expects "hellohello" back. |
99 TEST_F(MultiprocessMessagePipeTest, Basic) { | 104 TEST_F(MultiprocessMessagePipeTest, Basic) { |
100 helper()->StartChild("EchoEcho"); | 105 helper()->StartChild("EchoEcho"); |
101 | 106 |
102 scoped_refptr<ChannelEndpoint> ep; | 107 scoped_refptr<ChannelEndpoint> ep; |
103 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | 108 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); |
104 Init(ep); | 109 Init(ep); |
105 | 110 |
106 std::string hello("hello"); | 111 std::string hello("hello"); |
107 EXPECT_EQ(MOJO_RESULT_OK, | 112 EXPECT_EQ(MOJO_RESULT_OK, |
108 mp->WriteMessage(0, UserPointer<const void>(hello.data()), | 113 mp->WriteMessage(0, |
109 static_cast<uint32_t>(hello.size()), nullptr, | 114 UserPointer<const void>(hello.data()), |
| 115 static_cast<uint32_t>(hello.size()), |
| 116 nullptr, |
110 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 117 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
111 | 118 |
112 HandleSignalsState hss; | 119 HandleSignalsState hss; |
113 EXPECT_EQ(MOJO_RESULT_OK, | 120 EXPECT_EQ(MOJO_RESULT_OK, |
114 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | 121 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); |
115 // The child may or may not have closed its end of the message pipe and died | 122 // The child may or may not have closed its end of the message pipe and died |
116 // (and we may or may not know it yet), so our end may or may not appear as | 123 // (and we may or may not know it yet), so our end may or may not appear as |
117 // writable. | 124 // writable. |
118 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 125 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
119 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 126 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
120 | 127 |
121 std::string read_buffer(1000, '\0'); | 128 std::string read_buffer(1000, '\0'); |
122 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); | 129 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); |
123 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 130 CHECK_EQ(mp->ReadMessage(0, |
124 MakeUserPointer(&read_buffer_size), nullptr, nullptr, | 131 UserPointer<void>(&read_buffer[0]), |
| 132 MakeUserPointer(&read_buffer_size), |
| 133 nullptr, |
| 134 nullptr, |
125 MOJO_READ_MESSAGE_FLAG_NONE), | 135 MOJO_READ_MESSAGE_FLAG_NONE), |
126 MOJO_RESULT_OK); | 136 MOJO_RESULT_OK); |
127 read_buffer.resize(read_buffer_size); | 137 read_buffer.resize(read_buffer_size); |
128 VLOG(2) << "Parent got: " << read_buffer; | 138 VLOG(2) << "Parent got: " << read_buffer; |
129 EXPECT_EQ(hello + hello, read_buffer); | 139 EXPECT_EQ(hello + hello, read_buffer); |
130 | 140 |
131 mp->Close(0); | 141 mp->Close(0); |
132 | 142 |
133 // We sent one message. | 143 // We sent one message. |
134 EXPECT_EQ(1 % 100, helper()->WaitForChildShutdown()); | 144 EXPECT_EQ(1 % 100, helper()->WaitForChildShutdown()); |
135 } | 145 } |
136 | 146 |
137 // Sends a bunch of messages to the child. Expects them "repeated" back. Waits | 147 // Sends a bunch of messages to the child. Expects them "repeated" back. Waits |
138 // for the child to close its end before quitting. | 148 // for the child to close its end before quitting. |
139 TEST_F(MultiprocessMessagePipeTest, QueueMessages) { | 149 TEST_F(MultiprocessMessagePipeTest, QueueMessages) { |
140 helper()->StartChild("EchoEcho"); | 150 helper()->StartChild("EchoEcho"); |
141 | 151 |
142 scoped_refptr<ChannelEndpoint> ep; | 152 scoped_refptr<ChannelEndpoint> ep; |
143 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | 153 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); |
144 Init(ep); | 154 Init(ep); |
145 | 155 |
146 static const size_t kNumMessages = 1001; | 156 static const size_t kNumMessages = 1001; |
147 for (size_t i = 0; i < kNumMessages; i++) { | 157 for (size_t i = 0; i < kNumMessages; i++) { |
148 std::string write_buffer(i, 'A' + (i % 26)); | 158 std::string write_buffer(i, 'A' + (i % 26)); |
149 EXPECT_EQ(MOJO_RESULT_OK, | 159 EXPECT_EQ(MOJO_RESULT_OK, |
150 mp->WriteMessage(0, UserPointer<const void>(write_buffer.data()), | 160 mp->WriteMessage(0, |
| 161 UserPointer<const void>(write_buffer.data()), |
151 static_cast<uint32_t>(write_buffer.size()), | 162 static_cast<uint32_t>(write_buffer.size()), |
152 nullptr, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 163 nullptr, |
| 164 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
153 } | 165 } |
154 | 166 |
155 const std::string quitquitquit("quitquitquit"); | 167 const std::string quitquitquit("quitquitquit"); |
156 EXPECT_EQ(MOJO_RESULT_OK, | 168 EXPECT_EQ(MOJO_RESULT_OK, |
157 mp->WriteMessage(0, UserPointer<const void>(quitquitquit.data()), | 169 mp->WriteMessage(0, |
| 170 UserPointer<const void>(quitquitquit.data()), |
158 static_cast<uint32_t>(quitquitquit.size()), | 171 static_cast<uint32_t>(quitquitquit.size()), |
159 nullptr, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 172 nullptr, |
| 173 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
160 | 174 |
161 for (size_t i = 0; i < kNumMessages; i++) { | 175 for (size_t i = 0; i < kNumMessages; i++) { |
162 HandleSignalsState hss; | 176 HandleSignalsState hss; |
163 EXPECT_EQ(MOJO_RESULT_OK, | 177 EXPECT_EQ(MOJO_RESULT_OK, |
164 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | 178 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); |
165 // The child may or may not have closed its end of the message pipe and died | 179 // The child may or may not have closed its end of the message pipe and died |
166 // (and we may or may not know it yet), so our end may or may not appear as | 180 // (and we may or may not know it yet), so our end may or may not appear as |
167 // writable. | 181 // writable. |
168 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 182 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
169 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 183 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
170 | 184 |
171 std::string read_buffer(kNumMessages * 2, '\0'); | 185 std::string read_buffer(kNumMessages * 2, '\0'); |
172 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); | 186 uint32_t read_buffer_size = static_cast<uint32_t>(read_buffer.size()); |
173 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 187 CHECK_EQ(mp->ReadMessage(0, |
174 MakeUserPointer(&read_buffer_size), nullptr, | 188 UserPointer<void>(&read_buffer[0]), |
175 nullptr, MOJO_READ_MESSAGE_FLAG_NONE), | 189 MakeUserPointer(&read_buffer_size), |
| 190 nullptr, |
| 191 nullptr, |
| 192 MOJO_READ_MESSAGE_FLAG_NONE), |
176 MOJO_RESULT_OK); | 193 MOJO_RESULT_OK); |
177 read_buffer.resize(read_buffer_size); | 194 read_buffer.resize(read_buffer_size); |
178 | 195 |
179 EXPECT_EQ(std::string(i * 2, 'A' + (i % 26)), read_buffer); | 196 EXPECT_EQ(std::string(i * 2, 'A' + (i % 26)), read_buffer); |
180 } | 197 } |
181 | 198 |
182 // Wait for it to become readable, which should fail (since we sent | 199 // Wait for it to become readable, which should fail (since we sent |
183 // "quitquitquit"). | 200 // "quitquitquit"). |
184 HandleSignalsState hss; | 201 HandleSignalsState hss; |
185 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 202 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
(...skipping 26 matching lines...) Expand all Loading... |
212 CHECK_EQ(hss.satisfied_signals, | 229 CHECK_EQ(hss.satisfied_signals, |
213 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); | 230 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); |
214 CHECK_EQ(hss.satisfiable_signals, | 231 CHECK_EQ(hss.satisfiable_signals, |
215 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); | 232 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); |
216 | 233 |
217 // It should have a shared buffer. | 234 // It should have a shared buffer. |
218 std::string read_buffer(100, '\0'); | 235 std::string read_buffer(100, '\0'); |
219 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); | 236 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); |
220 DispatcherVector dispatchers; | 237 DispatcherVector dispatchers; |
221 uint32_t num_dispatchers = 10; // Maximum number to receive. | 238 uint32_t num_dispatchers = 10; // Maximum number to receive. |
222 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 239 CHECK_EQ(mp->ReadMessage(0, |
223 MakeUserPointer(&num_bytes), &dispatchers, | 240 UserPointer<void>(&read_buffer[0]), |
224 &num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE), | 241 MakeUserPointer(&num_bytes), |
| 242 &dispatchers, |
| 243 &num_dispatchers, |
| 244 MOJO_READ_MESSAGE_FLAG_NONE), |
225 MOJO_RESULT_OK); | 245 MOJO_RESULT_OK); |
226 read_buffer.resize(num_bytes); | 246 read_buffer.resize(num_bytes); |
227 CHECK_EQ(read_buffer, std::string("go 1")); | 247 CHECK_EQ(read_buffer, std::string("go 1")); |
228 CHECK_EQ(num_dispatchers, 1u); | 248 CHECK_EQ(num_dispatchers, 1u); |
229 | 249 |
230 CHECK_EQ(dispatchers[0]->GetType(), Dispatcher::kTypeSharedBuffer); | 250 CHECK_EQ(dispatchers[0]->GetType(), Dispatcher::kTypeSharedBuffer); |
231 | 251 |
232 scoped_refptr<SharedBufferDispatcher> dispatcher( | 252 scoped_refptr<SharedBufferDispatcher> dispatcher( |
233 static_cast<SharedBufferDispatcher*>(dispatchers[0].get())); | 253 static_cast<SharedBufferDispatcher*>(dispatchers[0].get())); |
234 | 254 |
235 // Make a mapping. | 255 // Make a mapping. |
236 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; | 256 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; |
237 CHECK_EQ(dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping), | 257 CHECK_EQ(dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping), |
238 MOJO_RESULT_OK); | 258 MOJO_RESULT_OK); |
239 CHECK(mapping); | 259 CHECK(mapping); |
240 CHECK(mapping->GetBase()); | 260 CHECK(mapping->GetBase()); |
241 CHECK_EQ(mapping->GetLength(), 100u); | 261 CHECK_EQ(mapping->GetLength(), 100u); |
242 | 262 |
243 // Write some stuff to the shared buffer. | 263 // Write some stuff to the shared buffer. |
244 static const char kHello[] = "hello"; | 264 static const char kHello[] = "hello"; |
245 memcpy(mapping->GetBase(), kHello, sizeof(kHello)); | 265 memcpy(mapping->GetBase(), kHello, sizeof(kHello)); |
246 | 266 |
247 // We should be able to close the dispatcher now. | 267 // We should be able to close the dispatcher now. |
248 dispatcher->Close(); | 268 dispatcher->Close(); |
249 | 269 |
250 // And send a message to signal that we've written stuff. | 270 // And send a message to signal that we've written stuff. |
251 const std::string go2("go 2"); | 271 const std::string go2("go 2"); |
252 CHECK_EQ(mp->WriteMessage(0, UserPointer<const void>(&go2[0]), | 272 CHECK_EQ(mp->WriteMessage(0, |
253 static_cast<uint32_t>(go2.size()), nullptr, | 273 UserPointer<const void>(&go2[0]), |
| 274 static_cast<uint32_t>(go2.size()), |
| 275 nullptr, |
254 MOJO_WRITE_MESSAGE_FLAG_NONE), | 276 MOJO_WRITE_MESSAGE_FLAG_NONE), |
255 MOJO_RESULT_OK); | 277 MOJO_RESULT_OK); |
256 | 278 |
257 // Now wait for our parent to send us a message. | 279 // Now wait for our parent to send us a message. |
258 hss = HandleSignalsState(); | 280 hss = HandleSignalsState(); |
259 CHECK_EQ(test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss), | 281 CHECK_EQ(test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss), |
260 MOJO_RESULT_OK); | 282 MOJO_RESULT_OK); |
261 CHECK_EQ(hss.satisfied_signals, | 283 CHECK_EQ(hss.satisfied_signals, |
262 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); | 284 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); |
263 CHECK_EQ(hss.satisfiable_signals, | 285 CHECK_EQ(hss.satisfiable_signals, |
264 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); | 286 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); |
265 | 287 |
266 read_buffer = std::string(100, '\0'); | 288 read_buffer = std::string(100, '\0'); |
267 num_bytes = static_cast<uint32_t>(read_buffer.size()); | 289 num_bytes = static_cast<uint32_t>(read_buffer.size()); |
268 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 290 CHECK_EQ(mp->ReadMessage(0, |
269 MakeUserPointer(&num_bytes), nullptr, nullptr, | 291 UserPointer<void>(&read_buffer[0]), |
| 292 MakeUserPointer(&num_bytes), |
| 293 nullptr, |
| 294 nullptr, |
270 MOJO_READ_MESSAGE_FLAG_NONE), | 295 MOJO_READ_MESSAGE_FLAG_NONE), |
271 MOJO_RESULT_OK); | 296 MOJO_RESULT_OK); |
272 read_buffer.resize(num_bytes); | 297 read_buffer.resize(num_bytes); |
273 CHECK_EQ(read_buffer, std::string("go 3")); | 298 CHECK_EQ(read_buffer, std::string("go 3")); |
274 | 299 |
275 // It should have written something to the shared buffer. | 300 // It should have written something to the shared buffer. |
276 static const char kWorld[] = "world!!!"; | 301 static const char kWorld[] = "world!!!"; |
277 CHECK_EQ(memcmp(mapping->GetBase(), kWorld, sizeof(kWorld)), 0); | 302 CHECK_EQ(memcmp(mapping->GetBase(), kWorld, sizeof(kWorld)), 0); |
278 | 303 |
279 // And we're done. | 304 // And we're done. |
(...skipping 10 matching lines...) Expand all Loading... |
290 #endif | 315 #endif |
291 TEST_F(MultiprocessMessagePipeTest, MAYBE_SharedBufferPassing) { | 316 TEST_F(MultiprocessMessagePipeTest, MAYBE_SharedBufferPassing) { |
292 helper()->StartChild("CheckSharedBuffer"); | 317 helper()->StartChild("CheckSharedBuffer"); |
293 | 318 |
294 scoped_refptr<ChannelEndpoint> ep; | 319 scoped_refptr<ChannelEndpoint> ep; |
295 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); | 320 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalProxy(&ep)); |
296 Init(ep); | 321 Init(ep); |
297 | 322 |
298 // Make a shared buffer. | 323 // Make a shared buffer. |
299 scoped_refptr<SharedBufferDispatcher> dispatcher; | 324 scoped_refptr<SharedBufferDispatcher> dispatcher; |
300 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( | 325 EXPECT_EQ(MOJO_RESULT_OK, |
301 platform_support(), | 326 SharedBufferDispatcher::Create( |
302 SharedBufferDispatcher::kDefaultCreateOptions, | 327 platform_support(), |
303 100, &dispatcher)); | 328 SharedBufferDispatcher::kDefaultCreateOptions, |
| 329 100, |
| 330 &dispatcher)); |
304 ASSERT_TRUE(dispatcher.get()); | 331 ASSERT_TRUE(dispatcher.get()); |
305 | 332 |
306 // Make a mapping. | 333 // Make a mapping. |
307 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; | 334 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; |
308 EXPECT_EQ(MOJO_RESULT_OK, | 335 EXPECT_EQ(MOJO_RESULT_OK, |
309 dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | 336 dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |
310 ASSERT_TRUE(mapping); | 337 ASSERT_TRUE(mapping); |
311 ASSERT_TRUE(mapping->GetBase()); | 338 ASSERT_TRUE(mapping->GetBase()); |
312 ASSERT_EQ(100u, mapping->GetLength()); | 339 ASSERT_EQ(100u, mapping->GetLength()); |
313 | 340 |
314 // Send the shared buffer. | 341 // Send the shared buffer. |
315 const std::string go1("go 1"); | 342 const std::string go1("go 1"); |
316 DispatcherTransport transport( | 343 DispatcherTransport transport( |
317 test::DispatcherTryStartTransport(dispatcher.get())); | 344 test::DispatcherTryStartTransport(dispatcher.get())); |
318 ASSERT_TRUE(transport.is_valid()); | 345 ASSERT_TRUE(transport.is_valid()); |
319 | 346 |
320 std::vector<DispatcherTransport> transports; | 347 std::vector<DispatcherTransport> transports; |
321 transports.push_back(transport); | 348 transports.push_back(transport); |
322 EXPECT_EQ(MOJO_RESULT_OK, | 349 EXPECT_EQ(MOJO_RESULT_OK, |
323 mp->WriteMessage(0, UserPointer<const void>(&go1[0]), | 350 mp->WriteMessage(0, |
324 static_cast<uint32_t>(go1.size()), &transports, | 351 UserPointer<const void>(&go1[0]), |
| 352 static_cast<uint32_t>(go1.size()), |
| 353 &transports, |
325 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 354 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
326 transport.End(); | 355 transport.End(); |
327 | 356 |
328 EXPECT_TRUE(dispatcher->HasOneRef()); | 357 EXPECT_TRUE(dispatcher->HasOneRef()); |
329 dispatcher = nullptr; | 358 dispatcher = nullptr; |
330 | 359 |
331 // Wait for a message from the child. | 360 // Wait for a message from the child. |
332 HandleSignalsState hss; | 361 HandleSignalsState hss; |
333 EXPECT_EQ(MOJO_RESULT_OK, | 362 EXPECT_EQ(MOJO_RESULT_OK, |
334 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | 363 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); |
335 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 364 EXPECT_TRUE((hss.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
336 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); | 365 EXPECT_TRUE((hss.satisfiable_signals & MOJO_HANDLE_SIGNAL_READABLE)); |
337 | 366 |
338 std::string read_buffer(100, '\0'); | 367 std::string read_buffer(100, '\0'); |
339 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); | 368 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); |
340 EXPECT_EQ(MOJO_RESULT_OK, | 369 EXPECT_EQ(MOJO_RESULT_OK, |
341 mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 370 mp->ReadMessage(0, |
342 MakeUserPointer(&num_bytes), nullptr, nullptr, | 371 UserPointer<void>(&read_buffer[0]), |
| 372 MakeUserPointer(&num_bytes), |
| 373 nullptr, |
| 374 nullptr, |
343 MOJO_READ_MESSAGE_FLAG_NONE)); | 375 MOJO_READ_MESSAGE_FLAG_NONE)); |
344 read_buffer.resize(num_bytes); | 376 read_buffer.resize(num_bytes); |
345 EXPECT_EQ(std::string("go 2"), read_buffer); | 377 EXPECT_EQ(std::string("go 2"), read_buffer); |
346 | 378 |
347 // After we get it, the child should have written something to the shared | 379 // After we get it, the child should have written something to the shared |
348 // buffer. | 380 // buffer. |
349 static const char kHello[] = "hello"; | 381 static const char kHello[] = "hello"; |
350 EXPECT_EQ(0, memcmp(mapping->GetBase(), kHello, sizeof(kHello))); | 382 EXPECT_EQ(0, memcmp(mapping->GetBase(), kHello, sizeof(kHello))); |
351 | 383 |
352 // Now we'll write some stuff to the shared buffer. | 384 // Now we'll write some stuff to the shared buffer. |
353 static const char kWorld[] = "world!!!"; | 385 static const char kWorld[] = "world!!!"; |
354 memcpy(mapping->GetBase(), kWorld, sizeof(kWorld)); | 386 memcpy(mapping->GetBase(), kWorld, sizeof(kWorld)); |
355 | 387 |
356 // And send a message to signal that we've written stuff. | 388 // And send a message to signal that we've written stuff. |
357 const std::string go3("go 3"); | 389 const std::string go3("go 3"); |
358 EXPECT_EQ(MOJO_RESULT_OK, | 390 EXPECT_EQ(MOJO_RESULT_OK, |
359 mp->WriteMessage(0, UserPointer<const void>(&go3[0]), | 391 mp->WriteMessage(0, |
360 static_cast<uint32_t>(go3.size()), nullptr, | 392 UserPointer<const void>(&go3[0]), |
| 393 static_cast<uint32_t>(go3.size()), |
| 394 nullptr, |
361 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 395 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
362 | 396 |
363 // Wait for |mp| to become readable, which should fail. | 397 // Wait for |mp| to become readable, which should fail. |
364 hss = HandleSignalsState(); | 398 hss = HandleSignalsState(); |
365 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 399 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
366 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | 400 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); |
367 EXPECT_EQ(0u, hss.satisfied_signals); | 401 EXPECT_EQ(0u, hss.satisfied_signals); |
368 EXPECT_EQ(0u, hss.satisfiable_signals); | 402 EXPECT_EQ(0u, hss.satisfiable_signals); |
369 | 403 |
370 mp->Close(0); | 404 mp->Close(0); |
(...skipping 16 matching lines...) Expand all Loading... |
387 MOJO_RESULT_OK); | 421 MOJO_RESULT_OK); |
388 CHECK_EQ(hss.satisfied_signals, | 422 CHECK_EQ(hss.satisfied_signals, |
389 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); | 423 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); |
390 CHECK_EQ(hss.satisfiable_signals, | 424 CHECK_EQ(hss.satisfiable_signals, |
391 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); | 425 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE); |
392 | 426 |
393 std::string read_buffer(100, '\0'); | 427 std::string read_buffer(100, '\0'); |
394 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); | 428 uint32_t num_bytes = static_cast<uint32_t>(read_buffer.size()); |
395 DispatcherVector dispatchers; | 429 DispatcherVector dispatchers; |
396 uint32_t num_dispatchers = 10; // Maximum number to receive. | 430 uint32_t num_dispatchers = 10; // Maximum number to receive. |
397 CHECK_EQ(mp->ReadMessage(0, UserPointer<void>(&read_buffer[0]), | 431 CHECK_EQ(mp->ReadMessage(0, |
398 MakeUserPointer(&num_bytes), &dispatchers, | 432 UserPointer<void>(&read_buffer[0]), |
399 &num_dispatchers, MOJO_READ_MESSAGE_FLAG_NONE), | 433 MakeUserPointer(&num_bytes), |
| 434 &dispatchers, |
| 435 &num_dispatchers, |
| 436 MOJO_READ_MESSAGE_FLAG_NONE), |
400 MOJO_RESULT_OK); | 437 MOJO_RESULT_OK); |
401 mp->Close(0); | 438 mp->Close(0); |
402 | 439 |
403 read_buffer.resize(num_bytes); | 440 read_buffer.resize(num_bytes); |
404 CHECK_EQ(read_buffer, std::string("hello")); | 441 CHECK_EQ(read_buffer, std::string("hello")); |
405 CHECK_EQ(num_dispatchers, 1u); | 442 CHECK_EQ(num_dispatchers, 1u); |
406 | 443 |
407 CHECK_EQ(dispatchers[0]->GetType(), Dispatcher::kTypePlatformHandle); | 444 CHECK_EQ(dispatchers[0]->GetType(), Dispatcher::kTypePlatformHandle); |
408 | 445 |
409 scoped_refptr<PlatformHandleDispatcher> dispatcher( | 446 scoped_refptr<PlatformHandleDispatcher> dispatcher( |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 new PlatformHandleDispatcher(h.Pass())); | 489 new PlatformHandleDispatcher(h.Pass())); |
453 | 490 |
454 const std::string hello("hello"); | 491 const std::string hello("hello"); |
455 DispatcherTransport transport( | 492 DispatcherTransport transport( |
456 test::DispatcherTryStartTransport(dispatcher.get())); | 493 test::DispatcherTryStartTransport(dispatcher.get())); |
457 ASSERT_TRUE(transport.is_valid()); | 494 ASSERT_TRUE(transport.is_valid()); |
458 | 495 |
459 std::vector<DispatcherTransport> transports; | 496 std::vector<DispatcherTransport> transports; |
460 transports.push_back(transport); | 497 transports.push_back(transport); |
461 EXPECT_EQ(MOJO_RESULT_OK, | 498 EXPECT_EQ(MOJO_RESULT_OK, |
462 mp->WriteMessage(0, UserPointer<const void>(&hello[0]), | 499 mp->WriteMessage(0, |
463 static_cast<uint32_t>(hello.size()), &transports, | 500 UserPointer<const void>(&hello[0]), |
| 501 static_cast<uint32_t>(hello.size()), |
| 502 &transports, |
464 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 503 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
465 transport.End(); | 504 transport.End(); |
466 | 505 |
467 EXPECT_TRUE(dispatcher->HasOneRef()); | 506 EXPECT_TRUE(dispatcher->HasOneRef()); |
468 dispatcher = nullptr; | 507 dispatcher = nullptr; |
469 | 508 |
470 // Wait for it to become readable, which should fail. | 509 // Wait for it to become readable, which should fail. |
471 HandleSignalsState hss; | 510 HandleSignalsState hss; |
472 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 511 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
473 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); | 512 test::WaitIfNecessary(mp, MOJO_HANDLE_SIGNAL_READABLE, &hss)); |
474 EXPECT_EQ(0u, hss.satisfied_signals); | 513 EXPECT_EQ(0u, hss.satisfied_signals); |
475 EXPECT_EQ(0u, hss.satisfiable_signals); | 514 EXPECT_EQ(0u, hss.satisfiable_signals); |
476 | 515 |
477 mp->Close(0); | 516 mp->Close(0); |
478 | 517 |
479 EXPECT_EQ(0, helper()->WaitForChildShutdown()); | 518 EXPECT_EQ(0, helper()->WaitForChildShutdown()); |
480 } | 519 } |
481 | 520 |
482 } // namespace | 521 } // namespace |
483 } // namespace system | 522 } // namespace system |
484 } // namespace mojo | 523 } // namespace mojo |
OLD | NEW |