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 "mojo/public/system/core_cpp.h" | 5 #include "mojo/public/system/core_cpp.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "mojo/public/system/macros.h" | 9 #include "mojo/public/system/macros.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 char buffer[10] = { 0 }; | 195 char buffer[10] = { 0 }; |
196 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 196 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
197 EXPECT_EQ(MOJO_RESULT_OK, | 197 EXPECT_EQ(MOJO_RESULT_OK, |
198 ReadMessageRaw(h1.get(), | 198 ReadMessageRaw(h1.get(), |
199 buffer, &buffer_size, | 199 buffer, &buffer_size, |
200 NULL, NULL, | 200 NULL, NULL, |
201 MOJO_READ_MESSAGE_FLAG_NONE)); | 201 MOJO_READ_MESSAGE_FLAG_NONE)); |
202 EXPECT_EQ(kHelloSize, buffer_size); | 202 EXPECT_EQ(kHelloSize, buffer_size); |
203 EXPECT_STREQ(kHello, buffer); | 203 EXPECT_STREQ(kHello, buffer); |
204 | 204 |
205 // Send a handle over the previously-establish |MessagePipe|. | 205 // Send a handle over the previously-establish message pipe. Use the |
206 ScopedMessagePipeHandle h2; | 206 // |MessagePipe| wrapper (to test it), which automatically creates a |
207 ScopedMessagePipeHandle h3; | 207 // message pipe. |
208 CreateMessagePipe(&h2, &h3); | 208 MessagePipe mp; |
209 | 209 |
210 // Write a message to |h2|, before we send |h3|. | 210 // Write a message to |mp.handle0|, before we send |mp.handle1|. |
211 const char kWorld[] = "world!"; | 211 const char kWorld[] = "world!"; |
212 const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); | 212 const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); |
213 EXPECT_EQ(MOJO_RESULT_OK, | 213 EXPECT_EQ(MOJO_RESULT_OK, |
214 WriteMessageRaw(h2.get(), | 214 WriteMessageRaw(mp.handle0.get(), |
215 kWorld, kWorldSize, | 215 kWorld, kWorldSize, |
216 NULL, 0, | 216 NULL, 0, |
217 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 217 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
218 | 218 |
219 // Send |h3| over |h1| to |h0|. | 219 // Send |mp.handle1| over |h1| to |h0|. |
220 MojoHandle handles[5]; | 220 MojoHandle handles[5]; |
221 handles[0] = h3.release().value(); | 221 handles[0] = mp.handle1.release().value(); |
222 EXPECT_NE(kInvalidHandleValue, handles[0]); | 222 EXPECT_NE(kInvalidHandleValue, handles[0]); |
223 EXPECT_FALSE(h3.get().is_valid()); | 223 EXPECT_FALSE(mp.handle1.get().is_valid()); |
224 uint32_t handles_count = 1; | 224 uint32_t handles_count = 1; |
225 EXPECT_EQ(MOJO_RESULT_OK, | 225 EXPECT_EQ(MOJO_RESULT_OK, |
226 WriteMessageRaw(h1.get(), | 226 WriteMessageRaw(h1.get(), |
227 kHello, kHelloSize, | 227 kHello, kHelloSize, |
228 handles, handles_count, | 228 handles, handles_count, |
229 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 229 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
230 // |handles[0]| should actually be invalid now. | 230 // |handles[0]| should actually be invalid now. |
231 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handles[0])); | 231 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handles[0])); |
232 | 232 |
233 // Read "hello" and the sent handle. | 233 // Read "hello" and the sent handle. |
234 EXPECT_EQ(MOJO_RESULT_OK, | 234 EXPECT_EQ(MOJO_RESULT_OK, |
235 Wait(h0.get(), MOJO_WAIT_FLAG_READABLE, | 235 Wait(h0.get(), MOJO_WAIT_FLAG_READABLE, |
236 MOJO_DEADLINE_INDEFINITE)); | 236 MOJO_DEADLINE_INDEFINITE)); |
237 memset(buffer, 0, sizeof(buffer)); | 237 memset(buffer, 0, sizeof(buffer)); |
238 buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 238 buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
239 for (size_t i = 0; i < MOJO_ARRAYSIZE(handles); i++) | 239 for (size_t i = 0; i < MOJO_ARRAYSIZE(handles); i++) |
240 handles[i] = kInvalidHandleValue; | 240 handles[i] = kInvalidHandleValue; |
241 handles_count = static_cast<uint32_t>(MOJO_ARRAYSIZE(handles)); | 241 handles_count = static_cast<uint32_t>(MOJO_ARRAYSIZE(handles)); |
242 EXPECT_EQ(MOJO_RESULT_OK, | 242 EXPECT_EQ(MOJO_RESULT_OK, |
243 ReadMessageRaw(h0.get(), | 243 ReadMessageRaw(h0.get(), |
244 buffer, &buffer_size, | 244 buffer, &buffer_size, |
245 handles, &handles_count, | 245 handles, &handles_count, |
246 MOJO_READ_MESSAGE_FLAG_NONE)); | 246 MOJO_READ_MESSAGE_FLAG_NONE)); |
247 EXPECT_EQ(kHelloSize, buffer_size); | 247 EXPECT_EQ(kHelloSize, buffer_size); |
248 EXPECT_STREQ(kHello, buffer); | 248 EXPECT_STREQ(kHello, buffer); |
249 EXPECT_EQ(1u, handles_count); | 249 EXPECT_EQ(1u, handles_count); |
250 EXPECT_NE(kInvalidHandleValue, handles[0]); | 250 EXPECT_NE(kInvalidHandleValue, handles[0]); |
251 | 251 |
252 // Read from the sent/received handle. | 252 // Read from the sent/received handle. |
253 h3.reset(MessagePipeHandle(handles[0])); | 253 mp.handle1.reset(MessagePipeHandle(handles[0])); |
254 // Save |handles[0]| to check that it gets properly closed. | 254 // Save |handles[0]| to check that it gets properly closed. |
255 hv0 = handles[0]; | 255 hv0 = handles[0]; |
256 EXPECT_EQ(MOJO_RESULT_OK, | 256 EXPECT_EQ(MOJO_RESULT_OK, |
257 Wait(h3.get(), MOJO_WAIT_FLAG_READABLE, | 257 Wait(mp.handle1.get(), MOJO_WAIT_FLAG_READABLE, |
258 MOJO_DEADLINE_INDEFINITE)); | 258 MOJO_DEADLINE_INDEFINITE)); |
259 memset(buffer, 0, sizeof(buffer)); | 259 memset(buffer, 0, sizeof(buffer)); |
260 buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 260 buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
261 for (size_t i = 0; i < MOJO_ARRAYSIZE(handles); i++) | 261 for (size_t i = 0; i < MOJO_ARRAYSIZE(handles); i++) |
262 handles[i] = kInvalidHandleValue; | 262 handles[i] = kInvalidHandleValue; |
263 handles_count = static_cast<uint32_t>(MOJO_ARRAYSIZE(handles)); | 263 handles_count = static_cast<uint32_t>(MOJO_ARRAYSIZE(handles)); |
264 EXPECT_EQ(MOJO_RESULT_OK, | 264 EXPECT_EQ(MOJO_RESULT_OK, |
265 ReadMessageRaw(h3.get(), | 265 ReadMessageRaw(mp.handle1.get(), |
266 buffer, &buffer_size, | 266 buffer, &buffer_size, |
267 handles, &handles_count, | 267 handles, &handles_count, |
268 MOJO_READ_MESSAGE_FLAG_NONE)); | 268 MOJO_READ_MESSAGE_FLAG_NONE)); |
269 EXPECT_EQ(kWorldSize, buffer_size); | 269 EXPECT_EQ(kWorldSize, buffer_size); |
270 EXPECT_STREQ(kWorld, buffer); | 270 EXPECT_STREQ(kWorld, buffer); |
271 EXPECT_EQ(0u, handles_count); | 271 EXPECT_EQ(0u, handles_count); |
272 } | 272 } |
273 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(hv0)); | 273 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(hv0)); |
274 } | 274 } |
275 | 275 |
276 // TODO(vtl): Test |CloseRaw()|. | 276 // TODO(vtl): Test |CloseRaw()|. |
277 // TODO(vtl): Test |reset()| more thoroughly? | 277 // TODO(vtl): Test |reset()| more thoroughly? |
278 } | 278 } |
279 | 279 |
280 TEST(CoreCppTest, TearDownWithMessagesEnqueued) { | 280 TEST(CoreCppTest, TearDownWithMessagesEnqueued) { |
281 // Tear down a |MessagePipe| which still has a message enqueued, with the | 281 // Tear down a message pipe which still has a message enqueued, with the |
282 // message also having a valid |MessagePipe| handle. | 282 // message also having a valid message pipe handle. |
283 { | 283 { |
284 ScopedMessagePipeHandle h0; | 284 ScopedMessagePipeHandle h0; |
285 ScopedMessagePipeHandle h1; | 285 ScopedMessagePipeHandle h1; |
286 CreateMessagePipe(&h0, &h1); | 286 CreateMessagePipe(&h0, &h1); |
287 | 287 |
288 // Send a handle over the previously-establish |MessagePipe|. | 288 // Send a handle over the previously-establish message pipe. |
289 ScopedMessagePipeHandle h2; | 289 ScopedMessagePipeHandle h2; |
290 ScopedMessagePipeHandle h3; | 290 ScopedMessagePipeHandle h3; |
291 CreateMessagePipe(&h2, &h3); | 291 CreateMessagePipe(&h2, &h3); |
292 | 292 |
293 // Write a message to |h2|, before we send |h3|. | 293 // Write a message to |h2|, before we send |h3|. |
294 const char kWorld[] = "world!"; | 294 const char kWorld[] = "world!"; |
295 const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); | 295 const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); |
296 EXPECT_EQ(MOJO_RESULT_OK, | 296 EXPECT_EQ(MOJO_RESULT_OK, |
297 WriteMessageRaw(h2.get(), | 297 WriteMessageRaw(h2.get(), |
298 kWorld, kWorldSize, | 298 kWorld, kWorldSize, |
(...skipping 19 matching lines...) Expand all Loading... |
318 &h3_value, 1, | 318 &h3_value, 1, |
319 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 319 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
320 // |h3_value| should actually be invalid now. | 320 // |h3_value| should actually be invalid now. |
321 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(h3_value)); | 321 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(h3_value)); |
322 | 322 |
323 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0.release().value())); | 323 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0.release().value())); |
324 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1.release().value())); | 324 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1.release().value())); |
325 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h2.release().value())); | 325 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h2.release().value())); |
326 } | 326 } |
327 | 327 |
328 // Do this in a different order: make the enqueued |MessagePipe| handle only | 328 // Do this in a different order: make the enqueued message pipe handle only |
329 // half-alive. | 329 // half-alive. |
330 { | 330 { |
331 ScopedMessagePipeHandle h0; | 331 ScopedMessagePipeHandle h0; |
332 ScopedMessagePipeHandle h1; | 332 ScopedMessagePipeHandle h1; |
333 CreateMessagePipe(&h0, &h1); | 333 CreateMessagePipe(&h0, &h1); |
334 | 334 |
335 // Send a handle over the previously-establish |MessagePipe|. | 335 // Send a handle over the previously-establish message pipe. |
336 ScopedMessagePipeHandle h2; | 336 ScopedMessagePipeHandle h2; |
337 ScopedMessagePipeHandle h3; | 337 ScopedMessagePipeHandle h3; |
338 CreateMessagePipe(&h2, &h3); | 338 CreateMessagePipe(&h2, &h3); |
339 | 339 |
340 // Write a message to |h2|, before we send |h3|. | 340 // Write a message to |h2|, before we send |h3|. |
341 const char kWorld[] = "world!"; | 341 const char kWorld[] = "world!"; |
342 const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); | 342 const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); |
343 EXPECT_EQ(MOJO_RESULT_OK, | 343 EXPECT_EQ(MOJO_RESULT_OK, |
344 WriteMessageRaw(h2.get(), | 344 WriteMessageRaw(h2.get(), |
345 kWorld, kWorldSize, | 345 kWorld, kWorldSize, |
(...skipping 24 matching lines...) Expand all Loading... |
370 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h2.release().value())); | 370 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h2.release().value())); |
371 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0.release().value())); | 371 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0.release().value())); |
372 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1.release().value())); | 372 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1.release().value())); |
373 } | 373 } |
374 } | 374 } |
375 | 375 |
376 // TODO(vtl): Write data pipe tests. | 376 // TODO(vtl): Write data pipe tests. |
377 | 377 |
378 } // namespace | 378 } // namespace |
379 } // namespace mojo | 379 } // namespace mojo |
OLD | NEW |