| 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/edk/system/core.h" | 5 #include "mojo/edk/system/core.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 // Note: This may return either |MOJO_RESULT_INVALID_ARGUMENT| or | 320 // Note: This may return either |MOJO_RESULT_INVALID_ARGUMENT| or |
| 321 // |MOJO_RESULT_RESOURCE_EXHAUSTED|, depending on whether it's plausible or | 321 // |MOJO_RESULT_RESOURCE_EXHAUSTED|, depending on whether it's plausible or |
| 322 // not. | 322 // not. |
| 323 ASSERT_NE( | 323 ASSERT_NE( |
| 324 MOJO_RESULT_OK, | 324 MOJO_RESULT_OK, |
| 325 core()->WriteMessage(h, nullptr, 0, handles, | 325 core()->WriteMessage(h, nullptr, 0, handles, |
| 326 std::numeric_limits<uint32_t>::max(), | 326 std::numeric_limits<uint32_t>::max(), |
| 327 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 327 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 328 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); | 328 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); |
| 329 | 329 |
| 330 // Null |bytes| with non-zero message size. |
| 331 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 332 core()->WriteMessage(h, nullptr, 1, nullptr, 0, |
| 333 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 334 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); |
| 335 |
| 336 // Null |handles| with non-zero handle count. |
| 337 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 338 core()->WriteMessage(h, nullptr, 0, nullptr, 1, |
| 339 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 340 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); |
| 341 |
| 330 // Huge handle count (plausibly big). | 342 // Huge handle count (plausibly big). |
| 331 ASSERT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, | 343 ASSERT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, |
| 332 core()->WriteMessage( | 344 core()->WriteMessage( |
| 333 h, nullptr, 0, handles, | 345 h, nullptr, 0, handles, |
| 334 std::numeric_limits<uint32_t>::max() / sizeof(handles[0]), | 346 std::numeric_limits<uint32_t>::max() / sizeof(handles[0]), |
| 335 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 347 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 336 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); | 348 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); |
| 337 | 349 |
| 338 // Invalid handle in |handles|. | 350 // Invalid handle in |handles|. |
| 339 ASSERT_EQ( | 351 ASSERT_EQ( |
| 340 MOJO_RESULT_INVALID_ARGUMENT, | 352 MOJO_RESULT_INVALID_ARGUMENT, |
| 341 core()->WriteMessage(h, nullptr, 0, handles, 1, | 353 core()->WriteMessage(h, nullptr, 0, handles, 1, |
| 342 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 354 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 343 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); | 355 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); |
| 344 | 356 |
| 345 // Two invalid handles in |handles|. | 357 // Two invalid handles in |handles|. |
| 346 ASSERT_EQ( | 358 ASSERT_EQ( |
| 347 MOJO_RESULT_INVALID_ARGUMENT, | 359 MOJO_RESULT_INVALID_ARGUMENT, |
| 348 core()->WriteMessage(h, nullptr, 0, handles, 2, | 360 core()->WriteMessage(h, nullptr, 0, handles, 2, |
| 349 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 361 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 350 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); | 362 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); |
| 351 | 363 |
| 352 // Can't send a handle over itself. | 364 // Can't send a handle over itself. Note that this will also cause |h| to be |
| 365 // closed. |
| 353 handles[0] = h; | 366 handles[0] = h; |
| 354 ASSERT_EQ( | 367 ASSERT_EQ( |
| 355 MOJO_RESULT_BUSY, | 368 MOJO_RESULT_INVALID_ARGUMENT, |
| 356 core()->WriteMessage(h, nullptr, 0, handles, 1, | 369 core()->WriteMessage(h, nullptr, 0, handles, 1, |
| 357 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 370 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 358 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); | 371 ASSERT_EQ(0u, info.GetWriteMessageCallCount()); |
| 359 | 372 |
| 373 h = CreateMockHandle(&info); |
| 374 |
| 360 MockHandleInfo info2; | 375 MockHandleInfo info2; |
| 361 MojoHandle h2 = CreateMockHandle(&info2); | |
| 362 | 376 |
| 363 // This is "okay", but |MockDispatcher| doesn't implement it. | 377 // This is "okay", but |MockDispatcher| doesn't implement it. |
| 364 handles[0] = h2; | 378 handles[0] = CreateMockHandle(&info2); |
| 365 ASSERT_EQ( | 379 ASSERT_EQ( |
| 366 MOJO_RESULT_UNIMPLEMENTED, | 380 MOJO_RESULT_UNIMPLEMENTED, |
| 367 core()->WriteMessage(h, nullptr, 0, handles, 1, | 381 core()->WriteMessage(h, nullptr, 0, handles, 1, |
| 368 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 382 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 369 ASSERT_EQ(1u, info.GetWriteMessageCallCount()); | 383 ASSERT_EQ(1u, info.GetWriteMessageCallCount()); |
| 370 | 384 |
| 371 // One of the |handles| is still invalid. | 385 // One of the |handles| is still invalid. |
| 386 handles[0] = CreateMockHandle(&info2); |
| 372 ASSERT_EQ( | 387 ASSERT_EQ( |
| 373 MOJO_RESULT_INVALID_ARGUMENT, | 388 MOJO_RESULT_INVALID_ARGUMENT, |
| 374 core()->WriteMessage(h, nullptr, 0, handles, 2, | 389 core()->WriteMessage(h, nullptr, 0, handles, 2, |
| 375 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 390 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 376 ASSERT_EQ(1u, info.GetWriteMessageCallCount()); | 391 ASSERT_EQ(1u, info.GetWriteMessageCallCount()); |
| 377 | 392 |
| 378 // One of the |handles| is the same as |handle|. | 393 // One of the |handles| is the same as |h|. Both handles are closed. |
| 394 handles[0] = CreateMockHandle(&info2); |
| 379 handles[1] = h; | 395 handles[1] = h; |
| 380 ASSERT_EQ( | 396 ASSERT_EQ( |
| 397 MOJO_RESULT_INVALID_ARGUMENT, |
| 398 core()->WriteMessage(h, nullptr, 0, handles, 2, |
| 399 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 400 ASSERT_EQ(1u, info.GetWriteMessageCallCount()); |
| 401 |
| 402 h = CreateMockHandle(&info); |
| 403 |
| 404 // Can't send a handle twice in the same message. |
| 405 handles[0] = CreateMockHandle(&info2); |
| 406 handles[1] = handles[0]; |
| 407 ASSERT_EQ( |
| 381 MOJO_RESULT_BUSY, | 408 MOJO_RESULT_BUSY, |
| 382 core()->WriteMessage(h, nullptr, 0, handles, 2, | 409 core()->WriteMessage(h, nullptr, 0, handles, 2, |
| 383 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 410 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 384 ASSERT_EQ(1u, info.GetWriteMessageCallCount()); | 411 ASSERT_EQ(1u, info.GetWriteMessageCallCount()); |
| 385 | 412 |
| 386 // Can't send a handle twice in the same message. | |
| 387 handles[1] = h2; | |
| 388 ASSERT_EQ( | |
| 389 MOJO_RESULT_BUSY, | |
| 390 core()->WriteMessage(h, nullptr, 0, handles, 2, | |
| 391 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
| 392 ASSERT_EQ(1u, info.GetWriteMessageCallCount()); | |
| 393 | |
| 394 // Note: Since we never successfully sent anything with it, |h2| should | |
| 395 // still be valid. | |
| 396 ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h2)); | |
| 397 | |
| 398 ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h)); | 413 ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h)); |
| 399 } | 414 } |
| 400 | 415 |
| 401 // |ReadMessage()|: | 416 // |ReadMessage()|: |
| 402 // Only check arguments checked by |Core|, namely |handle|, |handles|, and | 417 // Only check arguments checked by |Core|, namely |handle|, |handles|, and |
| 403 // |num_handles|. | 418 // |num_handles|. |
| 404 { | 419 { |
| 405 ASSERT_EQ( | 420 ASSERT_EQ( |
| 406 MOJO_RESULT_INVALID_ARGUMENT, | 421 MOJO_RESULT_INVALID_ARGUMENT, |
| 407 core()->ReadMessage(MOJO_HANDLE_INVALID, nullptr, nullptr, nullptr, | 422 core()->ReadMessage(MOJO_HANDLE_INVALID, nullptr, nullptr, nullptr, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 core()->CreateMessagePipe(nullptr, nullptr, nullptr), | 470 core()->CreateMessagePipe(nullptr, nullptr, nullptr), |
| 456 kMemoryCheckFailedRegex); | 471 kMemoryCheckFailedRegex); |
| 457 ASSERT_DEATH_IF_SUPPORTED( | 472 ASSERT_DEATH_IF_SUPPORTED( |
| 458 core()->CreateMessagePipe(nullptr, &h, nullptr), | 473 core()->CreateMessagePipe(nullptr, &h, nullptr), |
| 459 kMemoryCheckFailedRegex); | 474 kMemoryCheckFailedRegex); |
| 460 ASSERT_DEATH_IF_SUPPORTED( | 475 ASSERT_DEATH_IF_SUPPORTED( |
| 461 core()->CreateMessagePipe(nullptr, nullptr, &h), | 476 core()->CreateMessagePipe(nullptr, nullptr, &h), |
| 462 kMemoryCheckFailedRegex); | 477 kMemoryCheckFailedRegex); |
| 463 } | 478 } |
| 464 | 479 |
| 465 // |WriteMessage()|: | |
| 466 // Only check arguments checked by |Core|, namely |handle|, |handles|, and | |
| 467 // |num_handles|. | |
| 468 { | |
| 469 MockHandleInfo info; | |
| 470 MojoHandle h = CreateMockHandle(&info); | |
| 471 | |
| 472 // Null |handles| with nonzero |num_handles|. | |
| 473 ASSERT_DEATH_IF_SUPPORTED( | |
| 474 core()->WriteMessage(h, nullptr, 0, nullptr, 1, | |
| 475 MOJO_WRITE_MESSAGE_FLAG_NONE), | |
| 476 kMemoryCheckFailedRegex); | |
| 477 | |
| 478 ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h)); | |
| 479 } | |
| 480 | |
| 481 // |ReadMessage()|: | 480 // |ReadMessage()|: |
| 482 // Only check arguments checked by |Core|, namely |handle|, |handles|, and | 481 // Only check arguments checked by |Core|, namely |handle|, |handles|, and |
| 483 // |num_handles|. | 482 // |num_handles|. |
| 484 { | 483 { |
| 485 MockHandleInfo info; | 484 MockHandleInfo info; |
| 486 MojoHandle h = CreateMockHandle(&info); | 485 MojoHandle h = CreateMockHandle(&info); |
| 487 | 486 |
| 488 uint32_t handle_count = 1; | 487 uint32_t handle_count = 1; |
| 489 ASSERT_DEATH_IF_SUPPORTED( | 488 ASSERT_DEATH_IF_SUPPORTED( |
| 490 core()->ReadMessage(h, nullptr, nullptr, nullptr, &handle_count, | 489 core()->ReadMessage(h, nullptr, nullptr, nullptr, &handle_count, |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 704 ASSERT_EQ(MOJO_RESULT_OK, | 703 ASSERT_EQ(MOJO_RESULT_OK, |
| 705 core()->ReadMessage( | 704 core()->ReadMessage( |
| 706 h_passing[1], buffer, &num_bytes, handles, &num_handles, | 705 h_passing[1], buffer, &num_bytes, handles, &num_handles, |
| 707 MOJO_READ_MESSAGE_FLAG_NONE)); | 706 MOJO_READ_MESSAGE_FLAG_NONE)); |
| 708 ASSERT_EQ(kHelloSize, num_bytes); | 707 ASSERT_EQ(kHelloSize, num_bytes); |
| 709 ASSERT_STREQ(kHello, buffer); | 708 ASSERT_STREQ(kHello, buffer); |
| 710 ASSERT_EQ(0u, num_handles); | 709 ASSERT_EQ(0u, num_handles); |
| 711 | 710 |
| 712 // Make sure that you can't pass either of the message pipe's handles over | 711 // Make sure that you can't pass either of the message pipe's handles over |
| 713 // itself. | 712 // itself. |
| 714 ASSERT_EQ(MOJO_RESULT_BUSY, | 713 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 715 core()->WriteMessage(h_passing[0], kHello, kHelloSize, | 714 core()->WriteMessage(h_passing[0], kHello, kHelloSize, |
| 716 &h_passing[0], 1, | 715 &h_passing[0], 1, |
| 717 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 716 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 717 ASSERT_EQ(MOJO_RESULT_OK, |
| 718 core()->CreateMessagePipe(nullptr, &h_passing[0], &h_passing[1])); |
| 719 |
| 718 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 720 ASSERT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 719 core()->WriteMessage(h_passing[0], kHello, kHelloSize, | 721 core()->WriteMessage(h_passing[0], kHello, kHelloSize, |
| 720 &h_passing[1], 1, | 722 &h_passing[1], 1, |
| 721 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 723 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 724 ASSERT_EQ(MOJO_RESULT_OK, |
| 725 core()->CreateMessagePipe(nullptr, &h_passing[0], &h_passing[1])); |
| 722 | 726 |
| 723 MojoHandle h_passed[2]; | 727 MojoHandle h_passed[2]; |
| 724 MojoCreateMessagePipeOptions options; | 728 MojoCreateMessagePipeOptions options; |
| 725 options.struct_size = sizeof(MojoCreateMessagePipeOptions); | 729 options.struct_size = sizeof(MojoCreateMessagePipeOptions); |
| 726 options.flags = MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_TRANSFERABLE; | 730 options.flags = MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_TRANSFERABLE; |
| 727 ASSERT_EQ(MOJO_RESULT_OK, | 731 ASSERT_EQ(MOJO_RESULT_OK, |
| 728 core()->CreateMessagePipe(&options, &h_passed[0], &h_passed[1])); | 732 core()->CreateMessagePipe(&options, &h_passed[0], &h_passed[1])); |
| 729 | 733 |
| 730 // Make sure that |h_passed[]| work properly. | 734 // Make sure that |h_passed[]| work properly. |
| 731 ASSERT_EQ(MOJO_RESULT_OK, | 735 ASSERT_EQ(MOJO_RESULT_OK, |
| (...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1260 ASSERT_EQ(MOJO_RESULT_BUSY, waiter.result); | 1264 ASSERT_EQ(MOJO_RESULT_BUSY, waiter.result); |
| 1261 | 1265 |
| 1262 ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h)); | 1266 ASSERT_EQ(MOJO_RESULT_OK, core()->Close(h)); |
| 1263 } | 1267 } |
| 1264 | 1268 |
| 1265 // TODO(vtl): Test |DuplicateBufferHandle()| and |MapBuffer()|. | 1269 // TODO(vtl): Test |DuplicateBufferHandle()| and |MapBuffer()|. |
| 1266 | 1270 |
| 1267 } // namespace | 1271 } // namespace |
| 1268 } // namespace edk | 1272 } // namespace edk |
| 1269 } // namespace mojo | 1273 } // namespace mojo |
| OLD | NEW |