| 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/system/local_data_pipe.h" | 5 #include "mojo/system/local_data_pipe.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "mojo/system/data_pipe.h" | 11 #include "mojo/system/data_pipe.h" |
| 12 #include "mojo/system/memory.h" |
| 12 #include "mojo/system/waiter.h" | 13 #include "mojo/system/waiter.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace mojo { | 16 namespace mojo { |
| 16 namespace system { | 17 namespace system { |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const uint32_t kSizeOfOptions = | 20 const uint32_t kSizeOfOptions = |
| 20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); | 21 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); |
| 21 | 22 |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 244 |
| 244 // Waiting should now succeed. | 245 // Waiting should now succeed. |
| 245 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); | 246 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); |
| 246 EXPECT_EQ(78u, context); | 247 EXPECT_EQ(78u, context); |
| 247 dp->ProducerRemoveWaiter(&waiter); | 248 dp->ProducerRemoveWaiter(&waiter); |
| 248 | 249 |
| 249 // Try writing, using a two-phase write. | 250 // Try writing, using a two-phase write. |
| 250 void* buffer = NULL; | 251 void* buffer = NULL; |
| 251 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); | 252 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); |
| 252 EXPECT_EQ(MOJO_RESULT_OK, | 253 EXPECT_EQ(MOJO_RESULT_OK, |
| 253 dp->ProducerBeginWriteData(&buffer, &num_bytes, false)); | 254 dp->ProducerBeginWriteData(MakeUserPointer(&buffer), |
| 255 MakeUserPointer(&num_bytes), false)); |
| 254 EXPECT_TRUE(buffer != NULL); | 256 EXPECT_TRUE(buffer != NULL); |
| 255 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 257 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
| 256 | 258 |
| 257 static_cast<int32_t*>(buffer)[0] = 789; | 259 static_cast<int32_t*>(buffer)[0] = 789; |
| 258 EXPECT_EQ(MOJO_RESULT_OK, | 260 EXPECT_EQ(MOJO_RESULT_OK, |
| 259 dp->ProducerEndWriteData( | 261 dp->ProducerEndWriteData( |
| 260 static_cast<uint32_t>(1u * sizeof(elements[0])))); | 262 static_cast<uint32_t>(1u * sizeof(elements[0])))); |
| 261 | 263 |
| 262 // Add a waiter. | 264 // Add a waiter. |
| 263 waiter.Init(); | 265 waiter.Init(); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 414 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
| 413 Waiter waiter; | 415 Waiter waiter; |
| 414 uint32_t context = 0; | 416 uint32_t context = 0; |
| 415 | 417 |
| 416 // Write two elements. | 418 // Write two elements. |
| 417 int32_t* elements = NULL; | 419 int32_t* elements = NULL; |
| 418 void* buffer = NULL; | 420 void* buffer = NULL; |
| 419 // Request room for three (but we'll only write two). | 421 // Request room for three (but we'll only write two). |
| 420 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); | 422 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); |
| 421 EXPECT_EQ(MOJO_RESULT_OK, | 423 EXPECT_EQ(MOJO_RESULT_OK, |
| 422 dp->ProducerBeginWriteData(&buffer, &num_bytes, true)); | 424 dp->ProducerBeginWriteData(MakeUserPointer(&buffer), |
| 425 MakeUserPointer(&num_bytes), true)); |
| 423 EXPECT_TRUE(buffer != NULL); | 426 EXPECT_TRUE(buffer != NULL); |
| 424 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0]))); | 427 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0]))); |
| 425 elements = static_cast<int32_t*>(buffer); | 428 elements = static_cast<int32_t*>(buffer); |
| 426 elements[0] = 123; | 429 elements[0] = 123; |
| 427 elements[1] = 456; | 430 elements[1] = 456; |
| 428 EXPECT_EQ(MOJO_RESULT_OK, | 431 EXPECT_EQ(MOJO_RESULT_OK, |
| 429 dp->ProducerEndWriteData( | 432 dp->ProducerEndWriteData( |
| 430 static_cast<uint32_t>(2u * sizeof(elements[0])))); | 433 static_cast<uint32_t>(2u * sizeof(elements[0])))); |
| 431 | 434 |
| 432 // Should already be readable. | 435 // Should already be readable. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 Waiter waiter; | 503 Waiter waiter; |
| 501 | 504 |
| 502 // It should be writable. | 505 // It should be writable. |
| 503 waiter.Init(); | 506 waiter.Init(); |
| 504 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 507 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 505 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0)); | 508 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0)); |
| 506 | 509 |
| 507 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); | 510 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); |
| 508 void* write_ptr = NULL; | 511 void* write_ptr = NULL; |
| 509 EXPECT_EQ(MOJO_RESULT_OK, | 512 EXPECT_EQ(MOJO_RESULT_OK, |
| 510 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); | 513 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 514 MakeUserPointer(&num_bytes), false)); |
| 511 EXPECT_TRUE(write_ptr != NULL); | 515 EXPECT_TRUE(write_ptr != NULL); |
| 512 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); | 516 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); |
| 513 | 517 |
| 514 // At this point, it shouldn't be writable. | 518 // At this point, it shouldn't be writable. |
| 515 waiter.Init(); | 519 waiter.Init(); |
| 516 ASSERT_EQ(MOJO_RESULT_OK, | 520 ASSERT_EQ(MOJO_RESULT_OK, |
| 517 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 1)); | 521 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 1)); |
| 518 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, NULL)); | 522 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, NULL)); |
| 519 dp->ProducerRemoveWaiter(&waiter); | 523 dp->ProducerRemoveWaiter(&waiter); |
| 520 | 524 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 538 // And readable. | 542 // And readable. |
| 539 waiter.Init(); | 543 waiter.Init(); |
| 540 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 544 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 541 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 4)); | 545 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 4)); |
| 542 | 546 |
| 543 // Start another two-phase write and check that it's readable even in the | 547 // Start another two-phase write and check that it's readable even in the |
| 544 // middle of it. | 548 // middle of it. |
| 545 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); | 549 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); |
| 546 write_ptr = NULL; | 550 write_ptr = NULL; |
| 547 EXPECT_EQ(MOJO_RESULT_OK, | 551 EXPECT_EQ(MOJO_RESULT_OK, |
| 548 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); | 552 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 553 MakeUserPointer(&num_bytes), false)); |
| 549 EXPECT_TRUE(write_ptr != NULL); | 554 EXPECT_TRUE(write_ptr != NULL); |
| 550 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); | 555 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); |
| 551 | 556 |
| 552 // It should be readable. | 557 // It should be readable. |
| 553 waiter.Init(); | 558 waiter.Init(); |
| 554 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 559 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| 555 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5)); | 560 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5)); |
| 556 | 561 |
| 557 // End the two-phase write without writing anything. | 562 // End the two-phase write without writing anything. |
| 558 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); | 563 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 771 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); | 776 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); |
| 772 | 777 |
| 773 // Test two-phase writes, including in all-or-none mode. | 778 // Test two-phase writes, including in all-or-none mode. |
| 774 // Note: Again, the following depends on an implementation detail -- namely | 779 // Note: Again, the following depends on an implementation detail -- namely |
| 775 // that the write pointer will point at the 5th element of the buffer (and the | 780 // that the write pointer will point at the 5th element of the buffer (and the |
| 776 // buffer has exactly the capacity requested). | 781 // buffer has exactly the capacity requested). |
| 777 | 782 |
| 778 num_bytes = 0u; | 783 num_bytes = 0u; |
| 779 void* write_ptr = NULL; | 784 void* write_ptr = NULL; |
| 780 EXPECT_EQ(MOJO_RESULT_OK, | 785 EXPECT_EQ(MOJO_RESULT_OK, |
| 781 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); | 786 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 787 MakeUserPointer(&num_bytes), false)); |
| 782 EXPECT_TRUE(write_ptr != NULL); | 788 EXPECT_TRUE(write_ptr != NULL); |
| 783 EXPECT_EQ(6u * sizeof(int32_t), num_bytes); | 789 EXPECT_EQ(6u * sizeof(int32_t), num_bytes); |
| 784 Seq(400, 6, static_cast<int32_t*>(write_ptr)); | 790 Seq(400, 6, static_cast<int32_t*>(write_ptr)); |
| 785 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(6u * sizeof(int32_t))); | 791 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(6u * sizeof(int32_t))); |
| 786 // Internally, a circular buffer would now look like: | 792 // Internally, a circular buffer would now look like: |
| 787 // -, -, -, -, 400, 401, 402, 403, 404, 405 | 793 // -, -, -, -, 400, 401, 402, 403, 404, 405 |
| 788 | 794 |
| 789 // |ProducerBeginWriteData()| ignores |*num_bytes| except in "all-or-none" | 795 // |ProducerBeginWriteData()| ignores |*num_bytes| except in "all-or-none" |
| 790 // mode. | 796 // mode. |
| 791 num_bytes = 6u * sizeof(int32_t); | 797 num_bytes = 6u * sizeof(int32_t); |
| 792 write_ptr = NULL; | 798 write_ptr = NULL; |
| 793 EXPECT_EQ(MOJO_RESULT_OK, | 799 EXPECT_EQ(MOJO_RESULT_OK, |
| 794 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); | 800 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 801 MakeUserPointer(&num_bytes), false)); |
| 795 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); | 802 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); |
| 796 static_cast<int32_t*>(write_ptr)[0] = 500; | 803 static_cast<int32_t*>(write_ptr)[0] = 500; |
| 797 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(1u * sizeof(int32_t))); | 804 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(1u * sizeof(int32_t))); |
| 798 // Internally, a circular buffer would now look like: | 805 // Internally, a circular buffer would now look like: |
| 799 // 500, -, -, -, 400, 401, 402, 403, 404, 405 | 806 // 500, -, -, -, 400, 401, 402, 403, 404, 405 |
| 800 | 807 |
| 801 // Requesting a 10-element buffer in all-or-none mode fails at this point. | 808 // Requesting a 10-element buffer in all-or-none mode fails at this point. |
| 802 num_bytes = 10u * sizeof(int32_t); | 809 num_bytes = 10u * sizeof(int32_t); |
| 803 write_ptr = NULL; | 810 write_ptr = NULL; |
| 804 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 811 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
| 805 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); | 812 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 813 MakeUserPointer(&num_bytes), true)); |
| 806 | 814 |
| 807 // But requesting, say, a 5-element (up to 9, really) buffer should be okay. | 815 // But requesting, say, a 5-element (up to 9, really) buffer should be okay. |
| 808 // It will discard two elements. | 816 // It will discard two elements. |
| 809 num_bytes = 5u * sizeof(int32_t); | 817 num_bytes = 5u * sizeof(int32_t); |
| 810 write_ptr = NULL; | 818 write_ptr = NULL; |
| 811 EXPECT_EQ(MOJO_RESULT_OK, | 819 EXPECT_EQ(MOJO_RESULT_OK, |
| 812 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); | 820 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 821 MakeUserPointer(&num_bytes), true)); |
| 813 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 822 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
| 814 // Only write 4 elements though. | 823 // Only write 4 elements though. |
| 815 Seq(600, 4, static_cast<int32_t*>(write_ptr)); | 824 Seq(600, 4, static_cast<int32_t*>(write_ptr)); |
| 816 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(4u * sizeof(int32_t))); | 825 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(4u * sizeof(int32_t))); |
| 817 // Internally, a circular buffer would now look like: | 826 // Internally, a circular buffer would now look like: |
| 818 // 500, 600, 601, 602, 603, -, 402, 403, 404, 405 | 827 // 500, 600, 601, 602, 603, -, 402, 403, 404, 405 |
| 819 | 828 |
| 820 // Do this again. Make sure we can get a buffer all the way out to the end of | 829 // Do this again. Make sure we can get a buffer all the way out to the end of |
| 821 // the internal buffer. | 830 // the internal buffer. |
| 822 num_bytes = 5u * sizeof(int32_t); | 831 num_bytes = 5u * sizeof(int32_t); |
| 823 write_ptr = NULL; | 832 write_ptr = NULL; |
| 824 EXPECT_EQ(MOJO_RESULT_OK, | 833 EXPECT_EQ(MOJO_RESULT_OK, |
| 825 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); | 834 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 835 MakeUserPointer(&num_bytes), true)); |
| 826 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); | 836 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); |
| 827 // Only write 3 elements though. | 837 // Only write 3 elements though. |
| 828 Seq(700, 3, static_cast<int32_t*>(write_ptr)); | 838 Seq(700, 3, static_cast<int32_t*>(write_ptr)); |
| 829 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(3u * sizeof(int32_t))); | 839 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(3u * sizeof(int32_t))); |
| 830 // Internally, a circular buffer would now look like: | 840 // Internally, a circular buffer would now look like: |
| 831 // 500, 600, 601, 602, 603, 700, 701, 702, -, - | 841 // 500, 600, 601, 602, 603, 700, 701, 702, -, - |
| 832 | 842 |
| 833 // Read everything. | 843 // Read everything. |
| 834 num_bytes = sizeof(buffer); | 844 num_bytes = sizeof(buffer); |
| 835 memset(buffer, 0xab, sizeof(buffer)); | 845 memset(buffer, 0xab, sizeof(buffer)); |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1089 MojoCreateDataPipeOptions validated_options = { 0 }; | 1099 MojoCreateDataPipeOptions validated_options = { 0 }; |
| 1090 EXPECT_EQ(MOJO_RESULT_OK, | 1100 EXPECT_EQ(MOJO_RESULT_OK, |
| 1091 DataPipe::ValidateCreateOptions(&options, &validated_options)); | 1101 DataPipe::ValidateCreateOptions(&options, &validated_options)); |
| 1092 | 1102 |
| 1093 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1103 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
| 1094 | 1104 |
| 1095 // Try writing way too much (two-phase). | 1105 // Try writing way too much (two-phase). |
| 1096 uint32_t num_bytes = 20u * sizeof(int32_t); | 1106 uint32_t num_bytes = 20u * sizeof(int32_t); |
| 1097 void* write_ptr = NULL; | 1107 void* write_ptr = NULL; |
| 1098 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1108 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
| 1099 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); | 1109 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 1110 MakeUserPointer(&num_bytes), true)); |
| 1100 | 1111 |
| 1101 // Try writing an amount which isn't a multiple of the element size | 1112 // Try writing an amount which isn't a multiple of the element size |
| 1102 // (two-phase). | 1113 // (two-phase). |
| 1103 COMPILE_ASSERT(sizeof(int32_t) > 1u, wow_int32_ts_have_size_1); | 1114 COMPILE_ASSERT(sizeof(int32_t) > 1u, wow_int32_ts_have_size_1); |
| 1104 num_bytes = 1u; | 1115 num_bytes = 1u; |
| 1105 write_ptr = NULL; | 1116 write_ptr = NULL; |
| 1106 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 1117 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 1107 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); | 1118 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 1119 MakeUserPointer(&num_bytes), true)); |
| 1108 | 1120 |
| 1109 // Try reading way too much (two-phase). | 1121 // Try reading way too much (two-phase). |
| 1110 num_bytes = 20u * sizeof(int32_t); | 1122 num_bytes = 20u * sizeof(int32_t); |
| 1111 const void* read_ptr = NULL; | 1123 const void* read_ptr = NULL; |
| 1112 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1124 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
| 1113 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); | 1125 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); |
| 1114 | 1126 |
| 1115 // Write half (two-phase). | 1127 // Write half (two-phase). |
| 1116 num_bytes = 5u * sizeof(int32_t); | 1128 num_bytes = 5u * sizeof(int32_t); |
| 1117 write_ptr = NULL; | 1129 write_ptr = NULL; |
| 1118 EXPECT_EQ(MOJO_RESULT_OK, | 1130 EXPECT_EQ(MOJO_RESULT_OK, |
| 1119 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); | 1131 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 1132 MakeUserPointer(&num_bytes), true)); |
| 1120 // May provide more space than requested. | 1133 // May provide more space than requested. |
| 1121 EXPECT_GE(num_bytes, 5u * sizeof(int32_t)); | 1134 EXPECT_GE(num_bytes, 5u * sizeof(int32_t)); |
| 1122 EXPECT_TRUE(write_ptr != NULL); | 1135 EXPECT_TRUE(write_ptr != NULL); |
| 1123 Seq(0, 5, static_cast<int32_t*>(write_ptr)); | 1136 Seq(0, 5, static_cast<int32_t*>(write_ptr)); |
| 1124 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(5u * sizeof(int32_t))); | 1137 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(5u * sizeof(int32_t))); |
| 1125 | 1138 |
| 1126 // Try reading an amount which isn't a multiple of the element size | 1139 // Try reading an amount which isn't a multiple of the element size |
| 1127 // (two-phase). | 1140 // (two-phase). |
| 1128 num_bytes = 1u; | 1141 num_bytes = 1u; |
| 1129 read_ptr = NULL; | 1142 read_ptr = NULL; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1142 // We should have four left, leaving room for six. | 1155 // We should have four left, leaving room for six. |
| 1143 num_bytes = 0u; | 1156 num_bytes = 0u; |
| 1144 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); | 1157 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
| 1145 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); | 1158 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); |
| 1146 | 1159 |
| 1147 // Assuming a tight circular buffer of the specified capacity, we can't do a | 1160 // Assuming a tight circular buffer of the specified capacity, we can't do a |
| 1148 // two-phase write of six now. | 1161 // two-phase write of six now. |
| 1149 num_bytes = 6u * sizeof(int32_t); | 1162 num_bytes = 6u * sizeof(int32_t); |
| 1150 write_ptr = NULL; | 1163 write_ptr = NULL; |
| 1151 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1164 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
| 1152 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); | 1165 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 1166 MakeUserPointer(&num_bytes), true)); |
| 1153 | 1167 |
| 1154 // Write six elements (simple), filling the buffer. | 1168 // Write six elements (simple), filling the buffer. |
| 1155 num_bytes = 6u * sizeof(int32_t); | 1169 num_bytes = 6u * sizeof(int32_t); |
| 1156 int32_t buffer[100]; | 1170 int32_t buffer[100]; |
| 1157 Seq(100, 6, buffer); | 1171 Seq(100, 6, buffer); |
| 1158 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true)); | 1172 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true)); |
| 1159 EXPECT_EQ(6u * sizeof(int32_t), num_bytes); | 1173 EXPECT_EQ(6u * sizeof(int32_t), num_bytes); |
| 1160 | 1174 |
| 1161 // We have ten. | 1175 // We have ten. |
| 1162 num_bytes = 0u; | 1176 num_bytes = 0u; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1235 dp->ConsumerReadData(read_buffer, &num_bytes, false)); | 1249 dp->ConsumerReadData(read_buffer, &num_bytes, false)); |
| 1236 EXPECT_EQ(10u, num_bytes); | 1250 EXPECT_EQ(10u, num_bytes); |
| 1237 EXPECT_EQ(0, memcmp(read_buffer, &test_data[0], 10u)); | 1251 EXPECT_EQ(0, memcmp(read_buffer, &test_data[0], 10u)); |
| 1238 | 1252 |
| 1239 // Check that a two-phase write can now only write (at most) 80 bytes. (This | 1253 // Check that a two-phase write can now only write (at most) 80 bytes. (This |
| 1240 // checks an implementation detail; this behavior is not guaranteed, but we | 1254 // checks an implementation detail; this behavior is not guaranteed, but we |
| 1241 // need it for this test.) | 1255 // need it for this test.) |
| 1242 void* write_buffer_ptr = NULL; | 1256 void* write_buffer_ptr = NULL; |
| 1243 num_bytes = 0u; | 1257 num_bytes = 0u; |
| 1244 EXPECT_EQ(MOJO_RESULT_OK, | 1258 EXPECT_EQ(MOJO_RESULT_OK, |
| 1245 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false)); | 1259 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
| 1260 MakeUserPointer(&num_bytes), false)); |
| 1246 EXPECT_TRUE(write_buffer_ptr != NULL); | 1261 EXPECT_TRUE(write_buffer_ptr != NULL); |
| 1247 EXPECT_EQ(80u, num_bytes); | 1262 EXPECT_EQ(80u, num_bytes); |
| 1248 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); | 1263 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); |
| 1249 | 1264 |
| 1250 // Write as much data as we can (using |ProducerWriteData()|). We should write | 1265 // Write as much data as we can (using |ProducerWriteData()|). We should write |
| 1251 // 90 bytes. | 1266 // 90 bytes. |
| 1252 num_bytes = 200u; | 1267 num_bytes = 200u; |
| 1253 EXPECT_EQ(MOJO_RESULT_OK, | 1268 EXPECT_EQ(MOJO_RESULT_OK, |
| 1254 dp->ProducerWriteData(&test_data[20], &num_bytes, false)); | 1269 dp->ProducerWriteData(&test_data[20], &num_bytes, false)); |
| 1255 EXPECT_EQ(90u, num_bytes); | 1270 EXPECT_EQ(90u, num_bytes); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1308 // Write it again, so we'll have something left over. | 1323 // Write it again, so we'll have something left over. |
| 1309 num_bytes = kTestDataSize; | 1324 num_bytes = kTestDataSize; |
| 1310 EXPECT_EQ(MOJO_RESULT_OK, | 1325 EXPECT_EQ(MOJO_RESULT_OK, |
| 1311 dp->ProducerWriteData(kTestData, &num_bytes, false)); | 1326 dp->ProducerWriteData(kTestData, &num_bytes, false)); |
| 1312 EXPECT_EQ(kTestDataSize, num_bytes); | 1327 EXPECT_EQ(kTestDataSize, num_bytes); |
| 1313 | 1328 |
| 1314 // Start two-phase write. | 1329 // Start two-phase write. |
| 1315 void* write_buffer_ptr = NULL; | 1330 void* write_buffer_ptr = NULL; |
| 1316 num_bytes = 0u; | 1331 num_bytes = 0u; |
| 1317 EXPECT_EQ(MOJO_RESULT_OK, | 1332 EXPECT_EQ(MOJO_RESULT_OK, |
| 1318 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false)); | 1333 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
| 1334 MakeUserPointer(&num_bytes), false)); |
| 1319 EXPECT_TRUE(write_buffer_ptr != NULL); | 1335 EXPECT_TRUE(write_buffer_ptr != NULL); |
| 1320 EXPECT_GT(num_bytes, 0u); | 1336 EXPECT_GT(num_bytes, 0u); |
| 1321 | 1337 |
| 1322 // Start two-phase read. | 1338 // Start two-phase read. |
| 1323 const void* read_buffer_ptr = NULL; | 1339 const void* read_buffer_ptr = NULL; |
| 1324 num_bytes = 0u; | 1340 num_bytes = 0u; |
| 1325 EXPECT_EQ(MOJO_RESULT_OK, | 1341 EXPECT_EQ(MOJO_RESULT_OK, |
| 1326 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); | 1342 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); |
| 1327 EXPECT_TRUE(read_buffer_ptr != NULL); | 1343 EXPECT_TRUE(read_buffer_ptr != NULL); |
| 1328 EXPECT_EQ(2u * kTestDataSize, num_bytes); | 1344 EXPECT_EQ(2u * kTestDataSize, num_bytes); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1353 // Write some data, so we'll have something to read. | 1369 // Write some data, so we'll have something to read. |
| 1354 uint32_t num_bytes = kTestDataSize; | 1370 uint32_t num_bytes = kTestDataSize; |
| 1355 EXPECT_EQ(MOJO_RESULT_OK, | 1371 EXPECT_EQ(MOJO_RESULT_OK, |
| 1356 dp->ProducerWriteData(kTestData, &num_bytes, false)); | 1372 dp->ProducerWriteData(kTestData, &num_bytes, false)); |
| 1357 EXPECT_EQ(kTestDataSize, num_bytes); | 1373 EXPECT_EQ(kTestDataSize, num_bytes); |
| 1358 | 1374 |
| 1359 // Start two-phase write. | 1375 // Start two-phase write. |
| 1360 void* write_buffer_ptr = NULL; | 1376 void* write_buffer_ptr = NULL; |
| 1361 num_bytes = 0u; | 1377 num_bytes = 0u; |
| 1362 EXPECT_EQ(MOJO_RESULT_OK, | 1378 EXPECT_EQ(MOJO_RESULT_OK, |
| 1363 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false)); | 1379 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
| 1380 MakeUserPointer(&num_bytes), false)); |
| 1364 EXPECT_TRUE(write_buffer_ptr != NULL); | 1381 EXPECT_TRUE(write_buffer_ptr != NULL); |
| 1365 ASSERT_GT(num_bytes, kTestDataSize); | 1382 ASSERT_GT(num_bytes, kTestDataSize); |
| 1366 | 1383 |
| 1367 // Start two-phase read. | 1384 // Start two-phase read. |
| 1368 const void* read_buffer_ptr = NULL; | 1385 const void* read_buffer_ptr = NULL; |
| 1369 num_bytes = 0u; | 1386 num_bytes = 0u; |
| 1370 EXPECT_EQ(MOJO_RESULT_OK, | 1387 EXPECT_EQ(MOJO_RESULT_OK, |
| 1371 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); | 1388 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); |
| 1372 EXPECT_TRUE(read_buffer_ptr != NULL); | 1389 EXPECT_TRUE(read_buffer_ptr != NULL); |
| 1373 EXPECT_EQ(kTestDataSize, num_bytes); | 1390 EXPECT_EQ(kTestDataSize, num_bytes); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1384 | 1401 |
| 1385 // But trying to write should result in failure. | 1402 // But trying to write should result in failure. |
| 1386 num_bytes = kTestDataSize; | 1403 num_bytes = kTestDataSize; |
| 1387 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1404 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 1388 dp->ProducerWriteData(kTestData, &num_bytes, false)); | 1405 dp->ProducerWriteData(kTestData, &num_bytes, false)); |
| 1389 | 1406 |
| 1390 // As will trying to start another two-phase write. | 1407 // As will trying to start another two-phase write. |
| 1391 write_buffer_ptr = NULL; | 1408 write_buffer_ptr = NULL; |
| 1392 num_bytes = 0u; | 1409 num_bytes = 0u; |
| 1393 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1410 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 1394 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false)); | 1411 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
| 1412 MakeUserPointer(&num_bytes), false)); |
| 1395 | 1413 |
| 1396 dp->ProducerClose(); | 1414 dp->ProducerClose(); |
| 1397 } | 1415 } |
| 1398 | 1416 |
| 1399 // Test closing the consumer first, then the producer, with an active | 1417 // Test closing the consumer first, then the producer, with an active |
| 1400 // two-phase write. | 1418 // two-phase write. |
| 1401 { | 1419 { |
| 1402 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1420 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
| 1403 | 1421 |
| 1404 // Start two-phase write. | 1422 // Start two-phase write. |
| 1405 void* write_buffer_ptr = NULL; | 1423 void* write_buffer_ptr = NULL; |
| 1406 uint32_t num_bytes = 0u; | 1424 uint32_t num_bytes = 0u; |
| 1407 EXPECT_EQ(MOJO_RESULT_OK, | 1425 EXPECT_EQ(MOJO_RESULT_OK, |
| 1408 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false)); | 1426 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
| 1427 MakeUserPointer(&num_bytes), false)); |
| 1409 EXPECT_TRUE(write_buffer_ptr != NULL); | 1428 EXPECT_TRUE(write_buffer_ptr != NULL); |
| 1410 ASSERT_GT(num_bytes, kTestDataSize); | 1429 ASSERT_GT(num_bytes, kTestDataSize); |
| 1411 | 1430 |
| 1412 dp->ConsumerClose(); | 1431 dp->ConsumerClose(); |
| 1413 dp->ProducerClose(); | 1432 dp->ProducerClose(); |
| 1414 } | 1433 } |
| 1415 | 1434 |
| 1416 // Test closing the producer and then trying to read (with no data). | 1435 // Test closing the producer and then trying to read (with no data). |
| 1417 { | 1436 { |
| 1418 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1437 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1478 | 1497 |
| 1479 // Still no data. | 1498 // Still no data. |
| 1480 num_bytes = 1000u; | 1499 num_bytes = 1000u; |
| 1481 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); | 1500 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
| 1482 EXPECT_EQ(0u, num_bytes); | 1501 EXPECT_EQ(0u, num_bytes); |
| 1483 | 1502 |
| 1484 // Try ending a two-phase write with an invalid amount (too much). | 1503 // Try ending a two-phase write with an invalid amount (too much). |
| 1485 num_bytes = 0u; | 1504 num_bytes = 0u; |
| 1486 void* write_ptr = NULL; | 1505 void* write_ptr = NULL; |
| 1487 EXPECT_EQ(MOJO_RESULT_OK, | 1506 EXPECT_EQ(MOJO_RESULT_OK, |
| 1488 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); | 1507 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 1508 MakeUserPointer(&num_bytes), false)); |
| 1489 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 1509 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 1490 dp->ProducerEndWriteData( | 1510 dp->ProducerEndWriteData( |
| 1491 num_bytes + static_cast<uint32_t>(sizeof(int32_t)))); | 1511 num_bytes + static_cast<uint32_t>(sizeof(int32_t)))); |
| 1492 | 1512 |
| 1493 // But the two-phase write still ended. | 1513 // But the two-phase write still ended. |
| 1494 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u)); | 1514 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u)); |
| 1495 | 1515 |
| 1496 // Still no data. | 1516 // Still no data. |
| 1497 num_bytes = 1000u; | 1517 num_bytes = 1000u; |
| 1498 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); | 1518 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
| 1499 EXPECT_EQ(0u, num_bytes); | 1519 EXPECT_EQ(0u, num_bytes); |
| 1500 | 1520 |
| 1501 // Try ending a two-phase write with an invalid amount (not a multiple of the | 1521 // Try ending a two-phase write with an invalid amount (not a multiple of the |
| 1502 // element size). | 1522 // element size). |
| 1503 num_bytes = 0u; | 1523 num_bytes = 0u; |
| 1504 write_ptr = NULL; | 1524 write_ptr = NULL; |
| 1505 EXPECT_EQ(MOJO_RESULT_OK, | 1525 EXPECT_EQ(MOJO_RESULT_OK, |
| 1506 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); | 1526 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 1527 MakeUserPointer(&num_bytes), false)); |
| 1507 EXPECT_GE(num_bytes, 1u); | 1528 EXPECT_GE(num_bytes, 1u); |
| 1508 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ProducerEndWriteData(1u)); | 1529 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ProducerEndWriteData(1u)); |
| 1509 | 1530 |
| 1510 // But the two-phase write still ended. | 1531 // But the two-phase write still ended. |
| 1511 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u)); | 1532 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u)); |
| 1512 | 1533 |
| 1513 // Still no data. | 1534 // Still no data. |
| 1514 num_bytes = 1000u; | 1535 num_bytes = 1000u; |
| 1515 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); | 1536 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
| 1516 EXPECT_EQ(0u, num_bytes); | 1537 EXPECT_EQ(0u, num_bytes); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1635 // End reading. | 1656 // End reading. |
| 1636 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); | 1657 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); |
| 1637 | 1658 |
| 1638 dp->ProducerClose(); | 1659 dp->ProducerClose(); |
| 1639 dp->ConsumerClose(); | 1660 dp->ConsumerClose(); |
| 1640 } | 1661 } |
| 1641 | 1662 |
| 1642 } // namespace | 1663 } // namespace |
| 1643 } // namespace system | 1664 } // namespace system |
| 1644 } // namespace mojo | 1665 } // namespace mojo |
| OLD | NEW |