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" |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 | 263 |
264 // Add a waiter. | 264 // Add a waiter. |
265 waiter.Init(); | 265 waiter.Init(); |
266 ASSERT_EQ(MOJO_RESULT_OK, | 266 ASSERT_EQ(MOJO_RESULT_OK, |
267 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90)); | 267 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90)); |
268 | 268 |
269 // Read one element, using a two-phase read. | 269 // Read one element, using a two-phase read. |
270 const void* read_buffer = NULL; | 270 const void* read_buffer = NULL; |
271 num_bytes = 0u; | 271 num_bytes = 0u; |
272 EXPECT_EQ(MOJO_RESULT_OK, | 272 EXPECT_EQ(MOJO_RESULT_OK, |
273 dp->ConsumerBeginReadData(&read_buffer, &num_bytes, false)); | 273 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), |
| 274 MakeUserPointer(&num_bytes), false)); |
274 EXPECT_TRUE(read_buffer != NULL); | 275 EXPECT_TRUE(read_buffer != NULL); |
275 // Since we only read one element (after having written three in all), the | 276 // Since we only read one element (after having written three in all), the |
276 // two-phase read should only allow us to read one. This checks an | 277 // two-phase read should only allow us to read one. This checks an |
277 // implementation detail! | 278 // implementation detail! |
278 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 279 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
279 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]); | 280 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]); |
280 EXPECT_EQ(MOJO_RESULT_OK, | 281 EXPECT_EQ(MOJO_RESULT_OK, |
281 dp->ConsumerEndReadData( | 282 dp->ConsumerEndReadData( |
282 static_cast<uint32_t>(1u * sizeof(elements[0])))); | 283 static_cast<uint32_t>(1u * sizeof(elements[0])))); |
283 | 284 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 // Should already be readable. | 436 // Should already be readable. |
436 waiter.Init(); | 437 waiter.Init(); |
437 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 438 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
438 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12)); | 439 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12)); |
439 | 440 |
440 // Read one element. | 441 // Read one element. |
441 // Request two in all-or-none mode, but only read one. | 442 // Request two in all-or-none mode, but only read one. |
442 const void* read_buffer = NULL; | 443 const void* read_buffer = NULL; |
443 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); | 444 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); |
444 EXPECT_EQ(MOJO_RESULT_OK, | 445 EXPECT_EQ(MOJO_RESULT_OK, |
445 dp->ConsumerBeginReadData(&read_buffer, &num_bytes, true)); | 446 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), |
| 447 MakeUserPointer(&num_bytes), true)); |
446 EXPECT_TRUE(read_buffer != NULL); | 448 EXPECT_TRUE(read_buffer != NULL); |
447 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); | 449 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); |
448 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer); | 450 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer); |
449 EXPECT_EQ(123, read_elements[0]); | 451 EXPECT_EQ(123, read_elements[0]); |
450 EXPECT_EQ(MOJO_RESULT_OK, | 452 EXPECT_EQ(MOJO_RESULT_OK, |
451 dp->ConsumerEndReadData( | 453 dp->ConsumerEndReadData( |
452 static_cast<uint32_t>(1u * sizeof(elements[0])))); | 454 static_cast<uint32_t>(1u * sizeof(elements[0])))); |
453 | 455 |
454 // Should still be readable. | 456 // Should still be readable. |
455 waiter.Init(); | 457 waiter.Init(); |
456 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 458 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
457 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34)); | 459 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34)); |
458 | 460 |
459 // Read one element. | 461 // Read one element. |
460 // Request three, but not in all-or-none mode. | 462 // Request three, but not in all-or-none mode. |
461 read_buffer = NULL; | 463 read_buffer = NULL; |
462 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); | 464 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); |
463 EXPECT_EQ(MOJO_RESULT_OK, | 465 EXPECT_EQ(MOJO_RESULT_OK, |
464 dp->ConsumerBeginReadData(&read_buffer, &num_bytes, false)); | 466 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer), |
| 467 MakeUserPointer(&num_bytes), false)); |
465 EXPECT_TRUE(read_buffer != NULL); | 468 EXPECT_TRUE(read_buffer != NULL); |
466 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); | 469 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); |
467 read_elements = static_cast<const int32_t*>(read_buffer); | 470 read_elements = static_cast<const int32_t*>(read_buffer); |
468 EXPECT_EQ(456, read_elements[0]); | 471 EXPECT_EQ(456, read_elements[0]); |
469 EXPECT_EQ(MOJO_RESULT_OK, | 472 EXPECT_EQ(MOJO_RESULT_OK, |
470 dp->ConsumerEndReadData( | 473 dp->ConsumerEndReadData( |
471 static_cast<uint32_t>(1u * sizeof(elements[0])))); | 474 static_cast<uint32_t>(1u * sizeof(elements[0])))); |
472 | 475 |
473 // Adding a waiter should now succeed. | 476 // Adding a waiter should now succeed. |
474 waiter.Init(); | 477 waiter.Init(); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 562 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
560 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5)); | 563 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5)); |
561 | 564 |
562 // End the two-phase write without writing anything. | 565 // End the two-phase write without writing anything. |
563 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); | 566 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); |
564 | 567 |
565 // Start a two-phase read. | 568 // Start a two-phase read. |
566 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); | 569 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); |
567 const void* read_ptr = NULL; | 570 const void* read_ptr = NULL; |
568 EXPECT_EQ(MOJO_RESULT_OK, | 571 EXPECT_EQ(MOJO_RESULT_OK, |
569 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false)); | 572 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 573 MakeUserPointer(&num_bytes), false)); |
570 EXPECT_TRUE(read_ptr != NULL); | 574 EXPECT_TRUE(read_ptr != NULL); |
571 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes); | 575 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes); |
572 | 576 |
573 // At this point, it should still be writable. | 577 // At this point, it should still be writable. |
574 waiter.Init(); | 578 waiter.Init(); |
575 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 579 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
576 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6)); | 580 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6)); |
577 | 581 |
578 // But not readable. | 582 // But not readable. |
579 waiter.Init(); | 583 waiter.Init(); |
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1115 num_bytes = 1u; | 1119 num_bytes = 1u; |
1116 write_ptr = NULL; | 1120 write_ptr = NULL; |
1117 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 1121 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
1118 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1122 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
1119 MakeUserPointer(&num_bytes), true)); | 1123 MakeUserPointer(&num_bytes), true)); |
1120 | 1124 |
1121 // Try reading way too much (two-phase). | 1125 // Try reading way too much (two-phase). |
1122 num_bytes = 20u * sizeof(int32_t); | 1126 num_bytes = 20u * sizeof(int32_t); |
1123 const void* read_ptr = NULL; | 1127 const void* read_ptr = NULL; |
1124 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1128 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1125 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); | 1129 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 1130 MakeUserPointer(&num_bytes), true)); |
1126 | 1131 |
1127 // Write half (two-phase). | 1132 // Write half (two-phase). |
1128 num_bytes = 5u * sizeof(int32_t); | 1133 num_bytes = 5u * sizeof(int32_t); |
1129 write_ptr = NULL; | 1134 write_ptr = NULL; |
1130 EXPECT_EQ(MOJO_RESULT_OK, | 1135 EXPECT_EQ(MOJO_RESULT_OK, |
1131 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 1136 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
1132 MakeUserPointer(&num_bytes), true)); | 1137 MakeUserPointer(&num_bytes), true)); |
1133 // May provide more space than requested. | 1138 // May provide more space than requested. |
1134 EXPECT_GE(num_bytes, 5u * sizeof(int32_t)); | 1139 EXPECT_GE(num_bytes, 5u * sizeof(int32_t)); |
1135 EXPECT_TRUE(write_ptr != NULL); | 1140 EXPECT_TRUE(write_ptr != NULL); |
1136 Seq(0, 5, static_cast<int32_t*>(write_ptr)); | 1141 Seq(0, 5, static_cast<int32_t*>(write_ptr)); |
1137 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(5u * sizeof(int32_t))); | 1142 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(5u * sizeof(int32_t))); |
1138 | 1143 |
1139 // Try reading an amount which isn't a multiple of the element size | 1144 // Try reading an amount which isn't a multiple of the element size |
1140 // (two-phase). | 1145 // (two-phase). |
1141 num_bytes = 1u; | 1146 num_bytes = 1u; |
1142 read_ptr = NULL; | 1147 read_ptr = NULL; |
1143 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 1148 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
1144 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); | 1149 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 1150 MakeUserPointer(&num_bytes), true)); |
1145 | 1151 |
1146 // Read one (two-phase). | 1152 // Read one (two-phase). |
1147 num_bytes = 1u * sizeof(int32_t); | 1153 num_bytes = 1u * sizeof(int32_t); |
1148 read_ptr = NULL; | 1154 read_ptr = NULL; |
1149 EXPECT_EQ(MOJO_RESULT_OK, | 1155 EXPECT_EQ(MOJO_RESULT_OK, |
1150 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); | 1156 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 1157 MakeUserPointer(&num_bytes), true)); |
1151 EXPECT_GE(num_bytes, 1u * sizeof(int32_t)); | 1158 EXPECT_GE(num_bytes, 1u * sizeof(int32_t)); |
1152 EXPECT_EQ(0, static_cast<const int32_t*>(read_ptr)[0]); | 1159 EXPECT_EQ(0, static_cast<const int32_t*>(read_ptr)[0]); |
1153 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(1u * sizeof(int32_t))); | 1160 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(1u * sizeof(int32_t))); |
1154 | 1161 |
1155 // We should have four left, leaving room for six. | 1162 // We should have four left, leaving room for six. |
1156 num_bytes = 0u; | 1163 num_bytes = 0u; |
1157 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); | 1164 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
1158 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); | 1165 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); |
1159 | 1166 |
1160 // Assuming a tight circular buffer of the specified capacity, we can't do a | 1167 // Assuming a tight circular buffer of the specified capacity, we can't do a |
(...skipping 13 matching lines...) Expand all Loading... |
1174 | 1181 |
1175 // We have ten. | 1182 // We have ten. |
1176 num_bytes = 0u; | 1183 num_bytes = 0u; |
1177 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); | 1184 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
1178 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); | 1185 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); |
1179 | 1186 |
1180 // But a two-phase read of ten should fail. | 1187 // But a two-phase read of ten should fail. |
1181 num_bytes = 10u * sizeof(int32_t); | 1188 num_bytes = 10u * sizeof(int32_t); |
1182 read_ptr = NULL; | 1189 read_ptr = NULL; |
1183 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, | 1190 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, |
1184 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); | 1191 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 1192 MakeUserPointer(&num_bytes), true)); |
1185 | 1193 |
1186 // Close the producer. | 1194 // Close the producer. |
1187 dp->ProducerClose(); | 1195 dp->ProducerClose(); |
1188 | 1196 |
1189 // A two-phase read of nine should work. | 1197 // A two-phase read of nine should work. |
1190 num_bytes = 9u * sizeof(int32_t); | 1198 num_bytes = 9u * sizeof(int32_t); |
1191 read_ptr = NULL; | 1199 read_ptr = NULL; |
1192 EXPECT_EQ(MOJO_RESULT_OK, | 1200 EXPECT_EQ(MOJO_RESULT_OK, |
1193 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); | 1201 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 1202 MakeUserPointer(&num_bytes), true)); |
1194 EXPECT_GE(num_bytes, 9u * sizeof(int32_t)); | 1203 EXPECT_GE(num_bytes, 9u * sizeof(int32_t)); |
1195 EXPECT_EQ(1, static_cast<const int32_t*>(read_ptr)[0]); | 1204 EXPECT_EQ(1, static_cast<const int32_t*>(read_ptr)[0]); |
1196 EXPECT_EQ(2, static_cast<const int32_t*>(read_ptr)[1]); | 1205 EXPECT_EQ(2, static_cast<const int32_t*>(read_ptr)[1]); |
1197 EXPECT_EQ(3, static_cast<const int32_t*>(read_ptr)[2]); | 1206 EXPECT_EQ(3, static_cast<const int32_t*>(read_ptr)[2]); |
1198 EXPECT_EQ(4, static_cast<const int32_t*>(read_ptr)[3]); | 1207 EXPECT_EQ(4, static_cast<const int32_t*>(read_ptr)[3]); |
1199 EXPECT_EQ(100, static_cast<const int32_t*>(read_ptr)[4]); | 1208 EXPECT_EQ(100, static_cast<const int32_t*>(read_ptr)[4]); |
1200 EXPECT_EQ(101, static_cast<const int32_t*>(read_ptr)[5]); | 1209 EXPECT_EQ(101, static_cast<const int32_t*>(read_ptr)[5]); |
1201 EXPECT_EQ(102, static_cast<const int32_t*>(read_ptr)[6]); | 1210 EXPECT_EQ(102, static_cast<const int32_t*>(read_ptr)[6]); |
1202 EXPECT_EQ(103, static_cast<const int32_t*>(read_ptr)[7]); | 1211 EXPECT_EQ(103, static_cast<const int32_t*>(read_ptr)[7]); |
1203 EXPECT_EQ(104, static_cast<const int32_t*>(read_ptr)[8]); | 1212 EXPECT_EQ(104, static_cast<const int32_t*>(read_ptr)[8]); |
1204 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(9u * sizeof(int32_t))); | 1213 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(9u * sizeof(int32_t))); |
1205 | 1214 |
1206 // A two-phase read of two should fail, with "failed precondition". | 1215 // A two-phase read of two should fail, with "failed precondition". |
1207 num_bytes = 2u * sizeof(int32_t); | 1216 num_bytes = 2u * sizeof(int32_t); |
1208 read_ptr = NULL; | 1217 read_ptr = NULL; |
1209 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1218 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1210 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); | 1219 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 1220 MakeUserPointer(&num_bytes), true)); |
1211 | 1221 |
1212 dp->ConsumerClose(); | 1222 dp->ConsumerClose(); |
1213 } | 1223 } |
1214 | 1224 |
1215 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads, | 1225 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads, |
1216 // respectively, as much as possible, even if it has to "wrap around" the | 1226 // respectively, as much as possible, even if it has to "wrap around" the |
1217 // internal circular buffer. (Note that the two-phase write and read do not do | 1227 // internal circular buffer. (Note that the two-phase write and read do not do |
1218 // this.) | 1228 // this.) |
1219 TEST(LocalDataPipeTest, WrapAround) { | 1229 TEST(LocalDataPipeTest, WrapAround) { |
1220 unsigned char test_data[1000]; | 1230 unsigned char test_data[1000]; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1268 EXPECT_EQ(MOJO_RESULT_OK, | 1278 EXPECT_EQ(MOJO_RESULT_OK, |
1269 dp->ProducerWriteData(&test_data[20], &num_bytes, false)); | 1279 dp->ProducerWriteData(&test_data[20], &num_bytes, false)); |
1270 EXPECT_EQ(90u, num_bytes); | 1280 EXPECT_EQ(90u, num_bytes); |
1271 | 1281 |
1272 // Check that a two-phase read can now only read (at most) 90 bytes. (This | 1282 // Check that a two-phase read can now only read (at most) 90 bytes. (This |
1273 // checks an implementation detail; this behavior is not guaranteed, but we | 1283 // checks an implementation detail; this behavior is not guaranteed, but we |
1274 // need it for this test.) | 1284 // need it for this test.) |
1275 const void* read_buffer_ptr = NULL; | 1285 const void* read_buffer_ptr = NULL; |
1276 num_bytes = 0u; | 1286 num_bytes = 0u; |
1277 EXPECT_EQ(MOJO_RESULT_OK, | 1287 EXPECT_EQ(MOJO_RESULT_OK, |
1278 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); | 1288 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), |
| 1289 MakeUserPointer(&num_bytes), false)); |
1279 EXPECT_TRUE(read_buffer_ptr != NULL); | 1290 EXPECT_TRUE(read_buffer_ptr != NULL); |
1280 EXPECT_EQ(90u, num_bytes); | 1291 EXPECT_EQ(90u, num_bytes); |
1281 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u)); | 1292 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u)); |
1282 | 1293 |
1283 // Read as much as possible (using |ConsumerReadData()|). We should read 100 | 1294 // Read as much as possible (using |ConsumerReadData()|). We should read 100 |
1284 // bytes. | 1295 // bytes. |
1285 num_bytes = | 1296 num_bytes = |
1286 static_cast<uint32_t>(arraysize(read_buffer) * sizeof(read_buffer[0])); | 1297 static_cast<uint32_t>(arraysize(read_buffer) * sizeof(read_buffer[0])); |
1287 memset(read_buffer, 0, num_bytes); | 1298 memset(read_buffer, 0, num_bytes); |
1288 EXPECT_EQ(MOJO_RESULT_OK, | 1299 EXPECT_EQ(MOJO_RESULT_OK, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1332 EXPECT_EQ(MOJO_RESULT_OK, | 1343 EXPECT_EQ(MOJO_RESULT_OK, |
1333 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), | 1344 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
1334 MakeUserPointer(&num_bytes), false)); | 1345 MakeUserPointer(&num_bytes), false)); |
1335 EXPECT_TRUE(write_buffer_ptr != NULL); | 1346 EXPECT_TRUE(write_buffer_ptr != NULL); |
1336 EXPECT_GT(num_bytes, 0u); | 1347 EXPECT_GT(num_bytes, 0u); |
1337 | 1348 |
1338 // Start two-phase read. | 1349 // Start two-phase read. |
1339 const void* read_buffer_ptr = NULL; | 1350 const void* read_buffer_ptr = NULL; |
1340 num_bytes = 0u; | 1351 num_bytes = 0u; |
1341 EXPECT_EQ(MOJO_RESULT_OK, | 1352 EXPECT_EQ(MOJO_RESULT_OK, |
1342 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); | 1353 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), |
| 1354 MakeUserPointer(&num_bytes), false)); |
1343 EXPECT_TRUE(read_buffer_ptr != NULL); | 1355 EXPECT_TRUE(read_buffer_ptr != NULL); |
1344 EXPECT_EQ(2u * kTestDataSize, num_bytes); | 1356 EXPECT_EQ(2u * kTestDataSize, num_bytes); |
1345 | 1357 |
1346 // Close the producer. | 1358 // Close the producer. |
1347 dp->ProducerClose(); | 1359 dp->ProducerClose(); |
1348 | 1360 |
1349 // The consumer can finish its two-phase read. | 1361 // The consumer can finish its two-phase read. |
1350 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize)); | 1362 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize)); |
1351 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(kTestDataSize)); | 1363 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(kTestDataSize)); |
1352 | 1364 |
1353 // And start another. | 1365 // And start another. |
1354 read_buffer_ptr = NULL; | 1366 read_buffer_ptr = NULL; |
1355 num_bytes = 0u; | 1367 num_bytes = 0u; |
1356 EXPECT_EQ(MOJO_RESULT_OK, | 1368 EXPECT_EQ(MOJO_RESULT_OK, |
1357 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); | 1369 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), |
| 1370 MakeUserPointer(&num_bytes), false)); |
1358 EXPECT_TRUE(read_buffer_ptr != NULL); | 1371 EXPECT_TRUE(read_buffer_ptr != NULL); |
1359 EXPECT_EQ(kTestDataSize, num_bytes); | 1372 EXPECT_EQ(kTestDataSize, num_bytes); |
1360 | 1373 |
1361 // Close the consumer, which cancels the two-phase read. | 1374 // Close the consumer, which cancels the two-phase read. |
1362 dp->ConsumerClose(); | 1375 dp->ConsumerClose(); |
1363 } | 1376 } |
1364 | 1377 |
1365 // Close consumer first, then producer. | 1378 // Close consumer first, then producer. |
1366 { | 1379 { |
1367 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); | 1380 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); |
(...skipping 10 matching lines...) Expand all Loading... |
1378 EXPECT_EQ(MOJO_RESULT_OK, | 1391 EXPECT_EQ(MOJO_RESULT_OK, |
1379 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), | 1392 dp->ProducerBeginWriteData(MakeUserPointer(&write_buffer_ptr), |
1380 MakeUserPointer(&num_bytes), false)); | 1393 MakeUserPointer(&num_bytes), false)); |
1381 EXPECT_TRUE(write_buffer_ptr != NULL); | 1394 EXPECT_TRUE(write_buffer_ptr != NULL); |
1382 ASSERT_GT(num_bytes, kTestDataSize); | 1395 ASSERT_GT(num_bytes, kTestDataSize); |
1383 | 1396 |
1384 // Start two-phase read. | 1397 // Start two-phase read. |
1385 const void* read_buffer_ptr = NULL; | 1398 const void* read_buffer_ptr = NULL; |
1386 num_bytes = 0u; | 1399 num_bytes = 0u; |
1387 EXPECT_EQ(MOJO_RESULT_OK, | 1400 EXPECT_EQ(MOJO_RESULT_OK, |
1388 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); | 1401 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), |
| 1402 MakeUserPointer(&num_bytes), false)); |
1389 EXPECT_TRUE(read_buffer_ptr != NULL); | 1403 EXPECT_TRUE(read_buffer_ptr != NULL); |
1390 EXPECT_EQ(kTestDataSize, num_bytes); | 1404 EXPECT_EQ(kTestDataSize, num_bytes); |
1391 | 1405 |
1392 // Close the consumer. | 1406 // Close the consumer. |
1393 dp->ConsumerClose(); | 1407 dp->ConsumerClose(); |
1394 | 1408 |
1395 // Actually write some data. (Note: Premature freeing of the buffer would | 1409 // Actually write some data. (Note: Premature freeing of the buffer would |
1396 // probably only be detected under ASAN or similar.) | 1410 // probably only be detected under ASAN or similar.) |
1397 memcpy(write_buffer_ptr, kTestData, kTestDataSize); | 1411 memcpy(write_buffer_ptr, kTestData, kTestDataSize); |
1398 // Note: Even though the consumer has been closed, ending the two-phase | 1412 // Note: Even though the consumer has been closed, ending the two-phase |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1455 | 1469 |
1456 // A second read should fail. | 1470 // A second read should fail. |
1457 num_bytes = static_cast<uint32_t>(sizeof(buffer)); | 1471 num_bytes = static_cast<uint32_t>(sizeof(buffer)); |
1458 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1472 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1459 dp->ConsumerReadData(buffer, &num_bytes, false)); | 1473 dp->ConsumerReadData(buffer, &num_bytes, false)); |
1460 | 1474 |
1461 // A two-phase read should also fail. | 1475 // A two-phase read should also fail. |
1462 const void* read_buffer_ptr = NULL; | 1476 const void* read_buffer_ptr = NULL; |
1463 num_bytes = 0u; | 1477 num_bytes = 0u; |
1464 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1478 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1465 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); | 1479 dp->ConsumerBeginReadData(MakeUserPointer(&read_buffer_ptr), |
| 1480 MakeUserPointer(&num_bytes), false)); |
1466 | 1481 |
1467 // Ditto for discard. | 1482 // Ditto for discard. |
1468 num_bytes = 10u; | 1483 num_bytes = 10u; |
1469 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | 1484 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
1470 dp->ConsumerDiscardData(&num_bytes, false)); | 1485 dp->ConsumerDiscardData(&num_bytes, false)); |
1471 | 1486 |
1472 dp->ConsumerClose(); | 1487 dp->ConsumerClose(); |
1473 } | 1488 } |
1474 } | 1489 } |
1475 | 1490 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1552 | 1567 |
1553 // Still one element available. | 1568 // Still one element available. |
1554 num_bytes = 0u; | 1569 num_bytes = 0u; |
1555 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); | 1570 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
1556 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 1571 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1557 | 1572 |
1558 // Try ending a two-phase read with an invalid amount (too much). | 1573 // Try ending a two-phase read with an invalid amount (too much). |
1559 num_bytes = 0u; | 1574 num_bytes = 0u; |
1560 const void* read_ptr = NULL; | 1575 const void* read_ptr = NULL; |
1561 EXPECT_EQ(MOJO_RESULT_OK, | 1576 EXPECT_EQ(MOJO_RESULT_OK, |
1562 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false)); | 1577 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 1578 MakeUserPointer(&num_bytes), false)); |
1563 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 1579 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
1564 dp->ConsumerEndReadData( | 1580 dp->ConsumerEndReadData( |
1565 num_bytes + static_cast<uint32_t>(sizeof(int32_t)))); | 1581 num_bytes + static_cast<uint32_t>(sizeof(int32_t)))); |
1566 | 1582 |
1567 // Still one element available. | 1583 // Still one element available. |
1568 num_bytes = 0u; | 1584 num_bytes = 0u; |
1569 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); | 1585 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
1570 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 1586 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1571 | 1587 |
1572 // Try ending a two-phase read with an invalid amount (not a multiple of the | 1588 // Try ending a two-phase read with an invalid amount (not a multiple of the |
1573 // element size). | 1589 // element size). |
1574 num_bytes = 0u; | 1590 num_bytes = 0u; |
1575 read_ptr = NULL; | 1591 read_ptr = NULL; |
1576 EXPECT_EQ(MOJO_RESULT_OK, | 1592 EXPECT_EQ(MOJO_RESULT_OK, |
1577 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false)); | 1593 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 1594 MakeUserPointer(&num_bytes), false)); |
1578 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 1595 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1579 EXPECT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]); | 1596 EXPECT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]); |
1580 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ConsumerEndReadData(1u)); | 1597 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ConsumerEndReadData(1u)); |
1581 | 1598 |
1582 // Still one element available. | 1599 // Still one element available. |
1583 num_bytes = 0u; | 1600 num_bytes = 0u; |
1584 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); | 1601 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); |
1585 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); | 1602 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); |
1586 | 1603 |
1587 dp->ProducerClose(); | 1604 dp->ProducerClose(); |
(...skipping 23 matching lines...) Expand all Loading... |
1611 // Write some elements. | 1628 // Write some elements. |
1612 char elements[2] = { 'a', 'b' }; | 1629 char elements[2] = { 'a', 'b' }; |
1613 uint32_t num_bytes = 2u; | 1630 uint32_t num_bytes = 2u; |
1614 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false)); | 1631 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false)); |
1615 EXPECT_EQ(2u, num_bytes); | 1632 EXPECT_EQ(2u, num_bytes); |
1616 | 1633 |
1617 // Begin reading. | 1634 // Begin reading. |
1618 const void* read_ptr = NULL; | 1635 const void* read_ptr = NULL; |
1619 num_bytes = 2u; | 1636 num_bytes = 2u; |
1620 EXPECT_EQ(MOJO_RESULT_OK, | 1637 EXPECT_EQ(MOJO_RESULT_OK, |
1621 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false)); | 1638 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 1639 MakeUserPointer(&num_bytes), false)); |
1622 EXPECT_EQ(2u, num_bytes); | 1640 EXPECT_EQ(2u, num_bytes); |
1623 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]); | 1641 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]); |
1624 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]); | 1642 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]); |
1625 | 1643 |
1626 // Try to write some more. But nothing should be discardable right now. | 1644 // Try to write some more. But nothing should be discardable right now. |
1627 elements[0] = 'x'; | 1645 elements[0] = 'x'; |
1628 elements[1] = 'y'; | 1646 elements[1] = 'y'; |
1629 num_bytes = 2u; | 1647 num_bytes = 2u; |
1630 // TODO(vtl): This should be: | 1648 // TODO(vtl): This should be: |
1631 // EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, | 1649 // EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, |
1632 // dp->ProducerWriteData(elements, &num_bytes, false)); | 1650 // dp->ProducerWriteData(elements, &num_bytes, false)); |
1633 // but we incorrectly think that the bytes being read are discardable. Letting | 1651 // but we incorrectly think that the bytes being read are discardable. Letting |
1634 // this through reveals the significant consequence. | 1652 // this through reveals the significant consequence. |
1635 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false)); | 1653 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false)); |
1636 | 1654 |
1637 // Check that our read buffer hasn't changed underneath us. | 1655 // Check that our read buffer hasn't changed underneath us. |
1638 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]); | 1656 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]); |
1639 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]); | 1657 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]); |
1640 | 1658 |
1641 // End reading. | 1659 // End reading. |
1642 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); | 1660 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); |
1643 | 1661 |
1644 // Now writing should succeed. | 1662 // Now writing should succeed. |
1645 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false)); | 1663 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false)); |
1646 | 1664 |
1647 // And if we read, we should get the new values. | 1665 // And if we read, we should get the new values. |
1648 read_ptr = NULL; | 1666 read_ptr = NULL; |
1649 num_bytes = 2u; | 1667 num_bytes = 2u; |
1650 EXPECT_EQ(MOJO_RESULT_OK, | 1668 EXPECT_EQ(MOJO_RESULT_OK, |
1651 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false)); | 1669 dp->ConsumerBeginReadData(MakeUserPointer(&read_ptr), |
| 1670 MakeUserPointer(&num_bytes), false)); |
1652 EXPECT_EQ(2u, num_bytes); | 1671 EXPECT_EQ(2u, num_bytes); |
1653 EXPECT_EQ('x', static_cast<const char*>(read_ptr)[0]); | 1672 EXPECT_EQ('x', static_cast<const char*>(read_ptr)[0]); |
1654 EXPECT_EQ('y', static_cast<const char*>(read_ptr)[1]); | 1673 EXPECT_EQ('y', static_cast<const char*>(read_ptr)[1]); |
1655 | 1674 |
1656 // End reading. | 1675 // End reading. |
1657 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); | 1676 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); |
1658 | 1677 |
1659 dp->ProducerClose(); | 1678 dp->ProducerClose(); |
1660 dp->ConsumerClose(); | 1679 dp->ConsumerClose(); |
1661 } | 1680 } |
1662 | 1681 |
1663 } // namespace | 1682 } // namespace |
1664 } // namespace system | 1683 } // namespace system |
1665 } // namespace mojo | 1684 } // namespace mojo |
OLD | NEW |