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 3 // found in the LICENSE file. 4 5 #include "mojo/system/local_data_pipe.h" 6 7 #include <string.h> 8 9 #include "base/basictypes.h" 10 #include "base/memory/ref_counted.h" 11 #include "mojo/system/data_pipe.h" 12 #include "mojo/system/waiter.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace mojo { 16 namespace system { 17 namespace { 18 19 const uint32_t kSizeOfOptions = 20 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)); 21 22 // Validate options. 23 TEST(LocalDataPipeTest, Creation) { 24 // Create using default options. 25 { 26 // Get default options. 27 MojoCreateDataPipeOptions default_options = { 0 }; 28 EXPECT_EQ(MOJO_RESULT_OK, 29 DataPipe::ValidateCreateOptions(NULL, &default_options)); 30 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(default_options)); 31 dp->ProducerClose(); 32 dp->ConsumerClose(); 33 } 34 35 // Create using non-default options. 36 { 37 const MojoCreateDataPipeOptions options = { 38 kSizeOfOptions, // |struct_size|. 39 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 40 1, // |element_num_bytes|. 41 1000 // |capacity_num_bytes|. 42 }; 43 MojoCreateDataPipeOptions validated_options = { 0 }; 44 EXPECT_EQ(MOJO_RESULT_OK, 45 DataPipe::ValidateCreateOptions(&options, &validated_options)); 46 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 47 dp->ProducerClose(); 48 dp->ConsumerClose(); 49 } 50 { 51 const MojoCreateDataPipeOptions options = { 52 kSizeOfOptions, // |struct_size|. 53 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 54 4, // |element_num_bytes|. 55 4000 // |capacity_num_bytes|. 56 }; 57 MojoCreateDataPipeOptions validated_options = { 0 }; 58 EXPECT_EQ(MOJO_RESULT_OK, 59 DataPipe::ValidateCreateOptions(&options, &validated_options)); 60 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 61 dp->ProducerClose(); 62 dp->ConsumerClose(); 63 } 64 { 65 const MojoCreateDataPipeOptions options = { 66 kSizeOfOptions, // |struct_size|. 67 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. 68 7, // |element_num_bytes|. 69 7000000 // |capacity_num_bytes|. 70 }; 71 MojoCreateDataPipeOptions validated_options = { 0 }; 72 EXPECT_EQ(MOJO_RESULT_OK, 73 DataPipe::ValidateCreateOptions(&options, &validated_options)); 74 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 75 dp->ProducerClose(); 76 dp->ConsumerClose(); 77 } 78 // Default capacity. 79 { 80 const MojoCreateDataPipeOptions options = { 81 kSizeOfOptions, // |struct_size|. 82 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. 83 100, // |element_num_bytes|. 84 0 // |capacity_num_bytes|. 85 }; 86 MojoCreateDataPipeOptions validated_options = { 0 }; 87 EXPECT_EQ(MOJO_RESULT_OK, 88 DataPipe::ValidateCreateOptions(&options, &validated_options)); 89 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 90 dp->ProducerClose(); 91 dp->ConsumerClose(); 92 } 93 } 94 95 TEST(LocalDataPipeTest, SimpleReadWrite) { 96 const MojoCreateDataPipeOptions options = { 97 kSizeOfOptions, // |struct_size|. 98 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 99 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. 100 1000 * sizeof(int32_t) // |capacity_num_bytes|. 101 }; 102 MojoCreateDataPipeOptions validated_options = { 0 }; 103 EXPECT_EQ(MOJO_RESULT_OK, 104 DataPipe::ValidateCreateOptions(&options, &validated_options)); 105 106 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 107 108 int32_t elements[10] = { 0 }; 109 uint32_t num_bytes = 0; 110 111 // Try reading; nothing there yet. 112 num_bytes = static_cast<uint32_t>(arraysize(elements) * sizeof(elements[0])); 113 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 114 dp->ConsumerReadData(elements, &num_bytes, false)); 115 116 // Query; nothing there yet. 117 num_bytes = 0; 118 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 119 EXPECT_EQ(0u, num_bytes); 120 121 // Discard; nothing there yet. 122 num_bytes = static_cast<uint32_t>(5u * sizeof(elements[0])); 123 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 124 dp->ConsumerDiscardData(&num_bytes, false)); 125 126 // Read with invalid |num_bytes|. 127 num_bytes = sizeof(elements[0]) + 1; 128 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 129 dp->ConsumerReadData(elements, &num_bytes, false)); 130 131 // Write two elements. 132 elements[0] = 123; 133 elements[1] = 456; 134 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); 135 EXPECT_EQ(MOJO_RESULT_OK, 136 dp->ProducerWriteData(elements, &num_bytes, false)); 137 // It should have written everything (even without "all or none"). 138 EXPECT_EQ(2u * sizeof(elements[0]), num_bytes); 139 140 // Query. 141 num_bytes = 0; 142 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 143 EXPECT_EQ(2 * sizeof(elements[0]), num_bytes); 144 145 // Read one element. 146 elements[0] = -1; 147 elements[1] = -1; 148 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); 149 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, false)); 150 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); 151 EXPECT_EQ(123, elements[0]); 152 EXPECT_EQ(-1, elements[1]); 153 154 // Query. 155 num_bytes = 0; 156 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 157 EXPECT_EQ(1 * sizeof(elements[0]), num_bytes); 158 159 // Try to read two elements, with "all or none". 160 elements[0] = -1; 161 elements[1] = -1; 162 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); 163 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 164 dp->ConsumerReadData(elements, &num_bytes, true)); 165 EXPECT_EQ(-1, elements[0]); 166 EXPECT_EQ(-1, elements[1]); 167 168 // Try to read two elements, without "all or none". 169 elements[0] = -1; 170 elements[1] = -1; 171 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); 172 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, false)); 173 EXPECT_EQ(456, elements[0]); 174 EXPECT_EQ(-1, elements[1]); 175 176 // Query. 177 num_bytes = 0; 178 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 179 EXPECT_EQ(0u, num_bytes); 180 181 dp->ProducerClose(); 182 dp->ConsumerClose(); 183 } 184 185 // Note: The "basic" waiting tests test that the "wait states" are correct in 186 // various situations; they don't test that waiters are properly awoken on state 187 // changes. (For that, we need to use multiple threads.) 188 TEST(LocalDataPipeTest, BasicProducerWaiting) { 189 // Note: We take advantage of the fact that for |LocalDataPipe|, capacities 190 // are strict maximums. This is not guaranteed by the API. 191 192 const MojoCreateDataPipeOptions options = { 193 kSizeOfOptions, // |struct_size|. 194 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 195 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. 196 2 * sizeof(int32_t) // |capacity_num_bytes|. 197 }; 198 MojoCreateDataPipeOptions validated_options = { 0 }; 199 EXPECT_EQ(MOJO_RESULT_OK, 200 DataPipe::ValidateCreateOptions(&options, &validated_options)); 201 202 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 203 Waiter waiter; 204 uint32_t context = 0; 205 206 // Never readable. 207 waiter.Init(); 208 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 209 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12)); 210 211 // Already writable. 212 waiter.Init(); 213 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 214 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 34)); 215 216 // Write two elements. 217 int32_t elements[2] = { 123, 456 }; 218 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); 219 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, true)); 220 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); 221 222 // Adding a waiter should now succeed. 223 waiter.Init(); 224 EXPECT_EQ(MOJO_RESULT_OK, 225 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 56)); 226 // And it shouldn't be writable yet. 227 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, NULL)); 228 dp->ProducerRemoveWaiter(&waiter); 229 230 // Do it again. 231 waiter.Init(); 232 EXPECT_EQ(MOJO_RESULT_OK, 233 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 78)); 234 235 // Read one element. 236 elements[0] = -1; 237 elements[1] = -1; 238 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); 239 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, true)); 240 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 241 EXPECT_EQ(123, elements[0]); 242 EXPECT_EQ(-1, elements[1]); 243 244 // Waiting should now succeed. 245 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); 246 EXPECT_EQ(78u, context); 247 dp->ProducerRemoveWaiter(&waiter); 248 249 // Try writing, using a two-phase write. 250 void* buffer = NULL; 251 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); 252 EXPECT_EQ(MOJO_RESULT_OK, 253 dp->ProducerBeginWriteData(&buffer, &num_bytes, false)); 254 EXPECT_TRUE(buffer != NULL); 255 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 256 257 static_cast<int32_t*>(buffer)[0] = 789; 258 EXPECT_EQ(MOJO_RESULT_OK, 259 dp->ProducerEndWriteData( 260 static_cast<uint32_t>(1u * sizeof(elements[0])))); 261 262 // Add a waiter. 263 waiter.Init(); 264 EXPECT_EQ(MOJO_RESULT_OK, 265 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 90)); 266 267 // Read one element, using a two-phase read. 268 const void* read_buffer = NULL; 269 num_bytes = 0u; 270 EXPECT_EQ(MOJO_RESULT_OK, 271 dp->ConsumerBeginReadData(&read_buffer, &num_bytes, false)); 272 EXPECT_TRUE(read_buffer != NULL); 273 // Since we only read one element (after having written three in all), the 274 // two-phase read should only allow us to read one. This checks an 275 // implementation detail! 276 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 277 EXPECT_EQ(456, static_cast<const int32_t*>(read_buffer)[0]); 278 EXPECT_EQ(MOJO_RESULT_OK, 279 dp->ConsumerEndReadData( 280 static_cast<uint32_t>(1u * sizeof(elements[0])))); 281 282 // Waiting should succeed. 283 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); 284 EXPECT_EQ(90u, context); 285 dp->ProducerRemoveWaiter(&waiter); 286 287 // Write one element. 288 elements[0] = 123; 289 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); 290 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false)); 291 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 292 293 // Add a waiter. 294 waiter.Init(); 295 EXPECT_EQ(MOJO_RESULT_OK, 296 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12)); 297 298 // Close the consumer. 299 dp->ConsumerClose(); 300 301 // It should now be never-writable. 302 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context)); 303 EXPECT_EQ(12u, context); 304 dp->ProducerRemoveWaiter(&waiter); 305 306 dp->ProducerClose(); 307 } 308 309 TEST(LocalDataPipeTest, BasicConsumerWaiting) { 310 const MojoCreateDataPipeOptions options = { 311 kSizeOfOptions, // |struct_size|. 312 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 313 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. 314 1000 * sizeof(int32_t) // |capacity_num_bytes|. 315 }; 316 MojoCreateDataPipeOptions validated_options = { 0 }; 317 EXPECT_EQ(MOJO_RESULT_OK, 318 DataPipe::ValidateCreateOptions(&options, &validated_options)); 319 320 { 321 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 322 Waiter waiter; 323 uint32_t context = 0; 324 325 // Never writable. 326 waiter.Init(); 327 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 328 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 12)); 329 330 // Not yet readable. 331 waiter.Init(); 332 EXPECT_EQ(MOJO_RESULT_OK, 333 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34)); 334 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, NULL)); 335 dp->ConsumerRemoveWaiter(&waiter); 336 337 // Write two elements. 338 int32_t elements[2] = { 123, 456 }; 339 uint32_t num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); 340 EXPECT_EQ(MOJO_RESULT_OK, 341 dp->ProducerWriteData(elements, &num_bytes, true)); 342 343 // Should already be readable. 344 waiter.Init(); 345 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 346 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 56)); 347 348 // Discard one element. 349 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); 350 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerDiscardData(&num_bytes, true)); 351 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 352 353 // Should still be readable. 354 waiter.Init(); 355 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 356 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 78)); 357 358 // Read one element. 359 elements[0] = -1; 360 elements[1] = -1; 361 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); 362 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, true)); 363 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 364 EXPECT_EQ(456, elements[0]); 365 EXPECT_EQ(-1, elements[1]); 366 367 // Adding a waiter should now succeed. 368 waiter.Init(); 369 EXPECT_EQ(MOJO_RESULT_OK, 370 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 90)); 371 372 // Write one element. 373 elements[0] = 789; 374 elements[1] = -1; 375 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); 376 EXPECT_EQ(MOJO_RESULT_OK, 377 dp->ProducerWriteData(elements, &num_bytes, true)); 378 379 // Waiting should now succeed. 380 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(1000, &context)); 381 EXPECT_EQ(90u, context); 382 dp->ConsumerRemoveWaiter(&waiter); 383 384 // Close the producer. 385 dp->ProducerClose(); 386 387 // Should still be readable. 388 waiter.Init(); 389 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 390 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12)); 391 392 // Read one element. 393 elements[0] = -1; 394 elements[1] = -1; 395 num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); 396 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(elements, &num_bytes, true)); 397 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 398 EXPECT_EQ(789, elements[0]); 399 EXPECT_EQ(-1, elements[1]); 400 401 // Should be never-readable. 402 waiter.Init(); 403 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 404 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34)); 405 406 dp->ConsumerClose(); 407 } 408 409 // Test with two-phase APIs and closing the producer with an active consumer 410 // waiter. 411 { 412 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 413 Waiter waiter; 414 uint32_t context = 0; 415 416 // Write two elements. 417 int32_t* elements = NULL; 418 void* buffer = NULL; 419 // Request room for three (but we'll only write two). 420 uint32_t num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); 421 EXPECT_EQ(MOJO_RESULT_OK, 422 dp->ProducerBeginWriteData(&buffer, &num_bytes, true)); 423 EXPECT_TRUE(buffer != NULL); 424 EXPECT_GE(num_bytes, static_cast<uint32_t>(3u * sizeof(elements[0]))); 425 elements = static_cast<int32_t*>(buffer); 426 elements[0] = 123; 427 elements[1] = 456; 428 EXPECT_EQ(MOJO_RESULT_OK, 429 dp->ProducerEndWriteData( 430 static_cast<uint32_t>(2u * sizeof(elements[0])))); 431 432 // Should already be readable. 433 waiter.Init(); 434 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 435 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 12)); 436 437 // Read one element. 438 // Request two in all-or-none mode, but only read one. 439 const void* read_buffer = NULL; 440 num_bytes = static_cast<uint32_t>(2u * sizeof(elements[0])); 441 EXPECT_EQ(MOJO_RESULT_OK, 442 dp->ConsumerBeginReadData(&read_buffer, &num_bytes, true)); 443 EXPECT_TRUE(read_buffer != NULL); 444 EXPECT_EQ(static_cast<uint32_t>(2u * sizeof(elements[0])), num_bytes); 445 const int32_t* read_elements = static_cast<const int32_t*>(read_buffer); 446 EXPECT_EQ(123, read_elements[0]); 447 EXPECT_EQ(MOJO_RESULT_OK, 448 dp->ConsumerEndReadData( 449 static_cast<uint32_t>(1u * sizeof(elements[0])))); 450 451 // Should still be readable. 452 waiter.Init(); 453 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 454 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 34)); 455 456 // Read one element. 457 // Request three, but not in all-or-none mode. 458 read_buffer = NULL; 459 num_bytes = static_cast<uint32_t>(3u * sizeof(elements[0])); 460 EXPECT_EQ(MOJO_RESULT_OK, 461 dp->ConsumerBeginReadData(&read_buffer, &num_bytes, false)); 462 EXPECT_TRUE(read_buffer != NULL); 463 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(elements[0])), num_bytes); 464 read_elements = static_cast<const int32_t*>(read_buffer); 465 EXPECT_EQ(456, read_elements[0]); 466 EXPECT_EQ(MOJO_RESULT_OK, 467 dp->ConsumerEndReadData( 468 static_cast<uint32_t>(1u * sizeof(elements[0])))); 469 470 // Adding a waiter should now succeed. 471 waiter.Init(); 472 EXPECT_EQ(MOJO_RESULT_OK, 473 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 56)); 474 475 // Close the producer. 476 dp->ProducerClose(); 477 478 // Should be never-readable. 479 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, waiter.Wait(1000, &context)); 480 EXPECT_EQ(56u, context); 481 dp->ConsumerRemoveWaiter(&waiter); 482 483 dp->ConsumerClose(); 484 } 485 } 486 487 // Tests that data pipes aren't writable/readable during two-phase writes/reads. 488 TEST(LocalDataPipeTest, BasicTwoPhaseWaiting) { 489 const MojoCreateDataPipeOptions options = { 490 kSizeOfOptions, // |struct_size|. 491 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 492 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. 493 1000 * sizeof(int32_t) // |capacity_num_bytes|. 494 }; 495 MojoCreateDataPipeOptions validated_options = { 0 }; 496 EXPECT_EQ(MOJO_RESULT_OK, 497 DataPipe::ValidateCreateOptions(&options, &validated_options)); 498 499 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 500 Waiter waiter; 501 502 // It should be writable. 503 waiter.Init(); 504 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 505 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0)); 506 507 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); 508 void* write_ptr = NULL; 509 EXPECT_EQ(MOJO_RESULT_OK, 510 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); 511 EXPECT_TRUE(write_ptr != NULL); 512 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); 513 514 // At this point, it shouldn't be writable. 515 waiter.Init(); 516 EXPECT_EQ(MOJO_RESULT_OK, 517 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 1)); 518 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, NULL)); 519 dp->ProducerRemoveWaiter(&waiter); 520 521 // It shouldn't be readable yet either. 522 waiter.Init(); 523 EXPECT_EQ(MOJO_RESULT_OK, 524 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 2)); 525 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, NULL)); 526 dp->ConsumerRemoveWaiter(&waiter); 527 528 static_cast<int32_t*>(write_ptr)[0] = 123; 529 EXPECT_EQ(MOJO_RESULT_OK, 530 dp->ProducerEndWriteData( 531 static_cast<uint32_t>(1u * sizeof(int32_t)))); 532 533 // It should be writable again. 534 waiter.Init(); 535 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 536 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 3)); 537 538 // And readable. 539 waiter.Init(); 540 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 541 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 4)); 542 543 // Start another two-phase write and check that it's readable even in the 544 // middle of it. 545 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); 546 write_ptr = NULL; 547 EXPECT_EQ(MOJO_RESULT_OK, 548 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); 549 EXPECT_TRUE(write_ptr != NULL); 550 EXPECT_GE(num_bytes, static_cast<uint32_t>(1u * sizeof(int32_t))); 551 552 // It should be readable. 553 waiter.Init(); 554 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 555 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5)); 556 557 // End the two-phase write without writing anything. 558 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); 559 560 // Start a two-phase read. 561 num_bytes = static_cast<uint32_t>(1u * sizeof(int32_t)); 562 const void* read_ptr = NULL; 563 EXPECT_EQ(MOJO_RESULT_OK, 564 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false)); 565 EXPECT_TRUE(read_ptr != NULL); 566 EXPECT_EQ(static_cast<uint32_t>(1u * sizeof(int32_t)), num_bytes); 567 568 // At this point, it should still be writable. 569 waiter.Init(); 570 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 571 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6)); 572 573 // But not readable. 574 waiter.Init(); 575 EXPECT_EQ(MOJO_RESULT_OK, 576 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 7)); 577 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, NULL)); 578 dp->ConsumerRemoveWaiter(&waiter); 579 580 // End the two-phase read without reading anything. 581 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u)); 582 583 // It should be readable again. 584 waiter.Init(); 585 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 586 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 8)); 587 588 dp->ProducerClose(); 589 dp->ConsumerClose(); 590 } 591 592 // Test that a "may discard" data pipe is writable even when it's full. 593 TEST(LocalDataPipeTest, BasicMayDiscardWaiting) { 594 const MojoCreateDataPipeOptions options = { 595 kSizeOfOptions, // |struct_size|. 596 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. 597 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. 598 1 * sizeof(int32_t) // |capacity_num_bytes|. 599 }; 600 MojoCreateDataPipeOptions validated_options = { 0 }; 601 EXPECT_EQ(MOJO_RESULT_OK, 602 DataPipe::ValidateCreateOptions(&options, &validated_options)); 603 604 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 605 Waiter waiter; 606 607 // Writable. 608 waiter.Init(); 609 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 610 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 0)); 611 612 // Not readable. 613 waiter.Init(); 614 EXPECT_EQ(MOJO_RESULT_OK, 615 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 1)); 616 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, NULL)); 617 dp->ConsumerRemoveWaiter(&waiter); 618 619 uint32_t num_bytes = static_cast<uint32_t>(sizeof(int32_t)); 620 int32_t element = 123; 621 EXPECT_EQ(MOJO_RESULT_OK, 622 dp->ProducerWriteData(&element, &num_bytes, false)); 623 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); 624 625 // Still writable (even though it's full). 626 waiter.Init(); 627 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 628 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 2)); 629 630 // Now readable. 631 waiter.Init(); 632 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 633 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 3)); 634 635 // Overwrite that element. 636 num_bytes = static_cast<uint32_t>(sizeof(int32_t)); 637 element = 456; 638 EXPECT_EQ(MOJO_RESULT_OK, 639 dp->ProducerWriteData(&element, &num_bytes, false)); 640 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); 641 642 // Still writable. 643 waiter.Init(); 644 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 645 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 4)); 646 647 // And still readable. 648 waiter.Init(); 649 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 650 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 5)); 651 652 // Read that element. 653 num_bytes = static_cast<uint32_t>(sizeof(int32_t)); 654 element = 0; 655 EXPECT_EQ(MOJO_RESULT_OK, 656 dp->ConsumerReadData(&element, &num_bytes, false)); 657 EXPECT_EQ(static_cast<uint32_t>(sizeof(int32_t)), num_bytes); 658 EXPECT_EQ(456, element); 659 660 // Still writable. 661 waiter.Init(); 662 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 663 dp->ProducerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_WRITABLE, 6)); 664 665 // No longer readable. 666 waiter.Init(); 667 EXPECT_EQ(MOJO_RESULT_OK, 668 dp->ConsumerAddWaiter(&waiter, MOJO_HANDLE_SIGNAL_READABLE, 7)); 669 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, waiter.Wait(0, NULL)); 670 dp->ConsumerRemoveWaiter(&waiter); 671 672 dp->ProducerClose(); 673 dp->ConsumerClose(); 674 } 675 676 void Seq(int32_t start, size_t count, int32_t* out) { 677 for (size_t i = 0; i < count; i++) 678 out[i] = start + static_cast<int32_t>(i); 679 } 680 681 TEST(LocalDataPipeTest, MayDiscard) { 682 const MojoCreateDataPipeOptions options = { 683 kSizeOfOptions, // |struct_size|. 684 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. 685 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. 686 10 * sizeof(int32_t) // |capacity_num_bytes|. 687 }; 688 MojoCreateDataPipeOptions validated_options = { 0 }; 689 EXPECT_EQ(MOJO_RESULT_OK, 690 DataPipe::ValidateCreateOptions(&options, &validated_options)); 691 692 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 693 694 int32_t buffer[100] = { 0 }; 695 uint32_t num_bytes = 0; 696 697 num_bytes = 20u * sizeof(int32_t); 698 Seq(0, arraysize(buffer), buffer); 699 // Try writing more than capacity. (This test relies on the implementation 700 // enforcing the capacity strictly.) 701 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false)); 702 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); 703 704 // Read half of what we wrote. 705 num_bytes = 5u * sizeof(int32_t); 706 memset(buffer, 0xab, sizeof(buffer)); 707 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false)); 708 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); 709 int32_t expected_buffer[100]; 710 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 711 Seq(0, 5u, expected_buffer); 712 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 713 // Internally, a circular buffer would now look like: 714 // -, -, -, -, -, 5, 6, 7, 8, 9 715 716 // Write a bit more than the space that's available. 717 num_bytes = 8u * sizeof(int32_t); 718 Seq(100, arraysize(buffer), buffer); 719 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false)); 720 EXPECT_EQ(8u * sizeof(int32_t), num_bytes); 721 // Internally, a circular buffer would now look like: 722 // 100, 101, 102, 103, 104, 105, 106, 107, 8, 9 723 724 // Read half of what's available. 725 num_bytes = 5u * sizeof(int32_t); 726 memset(buffer, 0xab, sizeof(buffer)); 727 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false)); 728 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); 729 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 730 expected_buffer[0] = 8; 731 expected_buffer[1] = 9; 732 expected_buffer[2] = 100; 733 expected_buffer[3] = 101; 734 expected_buffer[4] = 102; 735 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 736 // Internally, a circular buffer would now look like: 737 // -, -, -, 103, 104, 105, 106, 107, -, - 738 739 // Write one integer. 740 num_bytes = 1u * sizeof(int32_t); 741 Seq(200, arraysize(buffer), buffer); 742 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false)); 743 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 744 // Internally, a circular buffer would now look like: 745 // -, -, -, 103, 104, 105, 106, 107, 200, - 746 747 // Write five more. 748 num_bytes = 5u * sizeof(int32_t); 749 Seq(300, arraysize(buffer), buffer); 750 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, false)); 751 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); 752 // Internally, a circular buffer would now look like: 753 // 301, 302, 303, 304, 104, 105, 106, 107, 200, 300 754 755 // Read it all. 756 num_bytes = sizeof(buffer); 757 memset(buffer, 0xab, sizeof(buffer)); 758 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false)); 759 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); 760 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 761 expected_buffer[0] = 104; 762 expected_buffer[1] = 105; 763 expected_buffer[2] = 106; 764 expected_buffer[3] = 107; 765 expected_buffer[4] = 200; 766 expected_buffer[5] = 300; 767 expected_buffer[6] = 301; 768 expected_buffer[7] = 302; 769 expected_buffer[8] = 303; 770 expected_buffer[9] = 304; 771 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 772 773 // Test two-phase writes, including in all-or-none mode. 774 // 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 776 // buffer has exactly the capacity requested). 777 778 num_bytes = 0u; 779 void* write_ptr = NULL; 780 EXPECT_EQ(MOJO_RESULT_OK, 781 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); 782 EXPECT_TRUE(write_ptr != NULL); 783 EXPECT_EQ(6u * sizeof(int32_t), num_bytes); 784 Seq(400, 6, static_cast<int32_t*>(write_ptr)); 785 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(6u * sizeof(int32_t))); 786 // Internally, a circular buffer would now look like: 787 // -, -, -, -, 400, 401, 402, 403, 404, 405 788 789 // |ProducerBeginWriteData()| ignores |*num_bytes| except in "all-or-none" 790 // mode. 791 num_bytes = 6u * sizeof(int32_t); 792 write_ptr = NULL; 793 EXPECT_EQ(MOJO_RESULT_OK, 794 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); 795 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); 796 static_cast<int32_t*>(write_ptr)[0] = 500; 797 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(1u * sizeof(int32_t))); 798 // Internally, a circular buffer would now look like: 799 // 500, -, -, -, 400, 401, 402, 403, 404, 405 800 801 // Requesting a 10-element buffer in all-or-none mode fails at this point. 802 num_bytes = 10u * sizeof(int32_t); 803 write_ptr = NULL; 804 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 805 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); 806 807 // But requesting, say, a 5-element (up to 9, really) buffer should be okay. 808 // It will discard two elements. 809 num_bytes = 5u * sizeof(int32_t); 810 write_ptr = NULL; 811 EXPECT_EQ(MOJO_RESULT_OK, 812 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); 813 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); 814 // Only write 4 elements though. 815 Seq(600, 4, static_cast<int32_t*>(write_ptr)); 816 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(4u * sizeof(int32_t))); 817 // Internally, a circular buffer would now look like: 818 // 500, 600, 601, 602, 603, -, 402, 403, 404, 405 819 820 // Do this again. Make sure we can get a buffer all the way out to the end of 821 // the internal buffer. 822 num_bytes = 5u * sizeof(int32_t); 823 write_ptr = NULL; 824 EXPECT_EQ(MOJO_RESULT_OK, 825 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); 826 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); 827 // Only write 3 elements though. 828 Seq(700, 3, static_cast<int32_t*>(write_ptr)); 829 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(3u * sizeof(int32_t))); 830 // Internally, a circular buffer would now look like: 831 // 500, 600, 601, 602, 603, 700, 701, 702, -, - 832 833 // Read everything. 834 num_bytes = sizeof(buffer); 835 memset(buffer, 0xab, sizeof(buffer)); 836 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, false)); 837 EXPECT_EQ(8u * sizeof(int32_t), num_bytes); 838 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 839 expected_buffer[0] = 500; 840 expected_buffer[1] = 600; 841 expected_buffer[2] = 601; 842 expected_buffer[3] = 602; 843 expected_buffer[4] = 603; 844 expected_buffer[5] = 700; 845 expected_buffer[6] = 701; 846 expected_buffer[7] = 702; 847 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 848 849 dp->ProducerClose(); 850 dp->ConsumerClose(); 851 } 852 853 TEST(LocalDataPipeTest, AllOrNone) { 854 const MojoCreateDataPipeOptions options = { 855 kSizeOfOptions, // |struct_size|. 856 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 857 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. 858 10 * sizeof(int32_t) // |capacity_num_bytes|. 859 }; 860 MojoCreateDataPipeOptions validated_options = { 0 }; 861 EXPECT_EQ(MOJO_RESULT_OK, 862 DataPipe::ValidateCreateOptions(&options, &validated_options)); 863 864 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 865 866 // Try writing way too much. 867 uint32_t num_bytes = 20u * sizeof(int32_t); 868 int32_t buffer[100]; 869 Seq(0, arraysize(buffer), buffer); 870 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 871 dp->ProducerWriteData(buffer, &num_bytes, true)); 872 873 // Should still be empty. 874 num_bytes = ~0u; 875 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 876 EXPECT_EQ(0u, num_bytes); 877 878 // Write some data. 879 num_bytes = 5u * sizeof(int32_t); 880 Seq(100, arraysize(buffer), buffer); 881 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true)); 882 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); 883 884 // Half full. 885 num_bytes = 0u; 886 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 887 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); 888 889 // Too much. 890 num_bytes = 6u * sizeof(int32_t); 891 Seq(200, arraysize(buffer), buffer); 892 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 893 dp->ProducerWriteData(buffer, &num_bytes, true)); 894 895 // Try reading too much. 896 num_bytes = 11u * sizeof(int32_t); 897 memset(buffer, 0xab, sizeof(buffer)); 898 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 899 dp->ConsumerReadData(buffer, &num_bytes, true)); 900 int32_t expected_buffer[100]; 901 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 902 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 903 904 // Try discarding too much. 905 num_bytes = 11u * sizeof(int32_t); 906 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 907 dp->ConsumerDiscardData(&num_bytes, true)); 908 909 // Just a little. 910 num_bytes = 2u * sizeof(int32_t); 911 Seq(300, arraysize(buffer), buffer); 912 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true)); 913 EXPECT_EQ(2u * sizeof(int32_t), num_bytes); 914 915 // Just right. 916 num_bytes = 3u * sizeof(int32_t); 917 Seq(400, arraysize(buffer), buffer); 918 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true)); 919 EXPECT_EQ(3u * sizeof(int32_t), num_bytes); 920 921 // Exactly full. 922 num_bytes = 0u; 923 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 924 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); 925 926 // Read half. 927 num_bytes = 5u * sizeof(int32_t); 928 memset(buffer, 0xab, sizeof(buffer)); 929 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, true)); 930 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); 931 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 932 Seq(100, 5, expected_buffer); 933 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 934 935 // Try reading too much again. 936 num_bytes = 6u * sizeof(int32_t); 937 memset(buffer, 0xab, sizeof(buffer)); 938 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 939 dp->ConsumerReadData(buffer, &num_bytes, true)); 940 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 941 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 942 943 // Try discarding too much again. 944 num_bytes = 6u * sizeof(int32_t); 945 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 946 dp->ConsumerDiscardData(&num_bytes, true)); 947 948 // Discard a little. 949 num_bytes = 2u * sizeof(int32_t); 950 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerDiscardData(&num_bytes, true)); 951 EXPECT_EQ(2u * sizeof(int32_t), num_bytes); 952 953 // Three left. 954 num_bytes = 0u; 955 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 956 EXPECT_EQ(3u * sizeof(int32_t), num_bytes); 957 958 // Close the producer, then test producer-closed cases. 959 dp->ProducerClose(); 960 961 // Try reading too much; "failed precondition" since the producer is closed. 962 num_bytes = 4u * sizeof(int32_t); 963 memset(buffer, 0xab, sizeof(buffer)); 964 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 965 dp->ConsumerReadData(buffer, &num_bytes, true)); 966 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 967 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 968 969 // Try discarding too much; "failed precondition" again. 970 num_bytes = 4u * sizeof(int32_t); 971 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 972 dp->ConsumerDiscardData(&num_bytes, true)); 973 974 // Read a little. 975 num_bytes = 2u * sizeof(int32_t); 976 memset(buffer, 0xab, sizeof(buffer)); 977 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, true)); 978 EXPECT_EQ(2u * sizeof(int32_t), num_bytes); 979 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 980 Seq(400, 2, expected_buffer); 981 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 982 983 // Discard the remaining element. 984 num_bytes = 1u * sizeof(int32_t); 985 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerDiscardData(&num_bytes, true)); 986 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 987 988 // Empty again. 989 num_bytes = ~0u; 990 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 991 EXPECT_EQ(0u, num_bytes); 992 993 dp->ConsumerClose(); 994 } 995 996 TEST(LocalDataPipeTest, AllOrNoneMayDiscard) { 997 const MojoCreateDataPipeOptions options = { 998 kSizeOfOptions, // |struct_size|. 999 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. 1000 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. 1001 10 * sizeof(int32_t) // |capacity_num_bytes|. 1002 }; 1003 MojoCreateDataPipeOptions validated_options = { 0 }; 1004 EXPECT_EQ(MOJO_RESULT_OK, 1005 DataPipe::ValidateCreateOptions(&options, &validated_options)); 1006 1007 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 1008 1009 // Try writing way too much. 1010 uint32_t num_bytes = 20u * sizeof(int32_t); 1011 int32_t buffer[100]; 1012 Seq(0, arraysize(buffer), buffer); 1013 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 1014 dp->ProducerWriteData(buffer, &num_bytes, true)); 1015 1016 // Write some stuff. 1017 num_bytes = 5u * sizeof(int32_t); 1018 Seq(100, arraysize(buffer), buffer); 1019 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true)); 1020 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); 1021 1022 // Write lots of stuff (discarding all but "104"). 1023 num_bytes = 9u * sizeof(int32_t); 1024 Seq(200, arraysize(buffer), buffer); 1025 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true)); 1026 EXPECT_EQ(9u * sizeof(int32_t), num_bytes); 1027 1028 // Read one. 1029 num_bytes = 1u * sizeof(int32_t); 1030 memset(buffer, 0xab, sizeof(buffer)); 1031 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, true)); 1032 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 1033 int32_t expected_buffer[100]; 1034 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 1035 expected_buffer[0] = 104; 1036 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 1037 1038 // Try reading too many. 1039 num_bytes = 10u * sizeof(int32_t); 1040 memset(buffer, 0xab, sizeof(buffer)); 1041 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 1042 dp->ConsumerReadData(buffer, &num_bytes, true)); 1043 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 1044 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 1045 1046 // Try discarding too many. 1047 num_bytes = 10u * sizeof(int32_t); 1048 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 1049 dp->ConsumerDiscardData(&num_bytes, true)); 1050 1051 // Discard a bunch. 1052 num_bytes = 4u * sizeof(int32_t); 1053 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerDiscardData(&num_bytes, true)); 1054 1055 // Half full. 1056 num_bytes = 0u; 1057 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1058 EXPECT_EQ(5u * sizeof(int32_t), num_bytes); 1059 1060 // Write as much as possible. 1061 num_bytes = 10u * sizeof(int32_t); 1062 Seq(300, arraysize(buffer), buffer); 1063 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true)); 1064 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); 1065 1066 // Read everything. 1067 num_bytes = 10u * sizeof(int32_t); 1068 memset(buffer, 0xab, sizeof(buffer)); 1069 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerReadData(buffer, &num_bytes, true)); 1070 memset(expected_buffer, 0xab, sizeof(expected_buffer)); 1071 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); 1072 Seq(300, 10, expected_buffer); 1073 EXPECT_EQ(0, memcmp(buffer, expected_buffer, sizeof(buffer))); 1074 1075 // Note: All-or-none two-phase writes on a "may discard" data pipe are tested 1076 // in LocalDataPipeTest.MayDiscard. 1077 1078 dp->ProducerClose(); 1079 dp->ConsumerClose(); 1080 } 1081 1082 TEST(LocalDataPipeTest, TwoPhaseAllOrNone) { 1083 const MojoCreateDataPipeOptions options = { 1084 kSizeOfOptions, // |struct_size|. 1085 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 1086 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. 1087 10 * sizeof(int32_t) // |capacity_num_bytes|. 1088 }; 1089 MojoCreateDataPipeOptions validated_options = { 0 }; 1090 EXPECT_EQ(MOJO_RESULT_OK, 1091 DataPipe::ValidateCreateOptions(&options, &validated_options)); 1092 1093 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 1094 1095 // Try writing way too much (two-phase). 1096 uint32_t num_bytes = 20u * sizeof(int32_t); 1097 void* write_ptr = NULL; 1098 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 1099 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); 1100 1101 // Try writing an amount which isn't a multiple of the element size 1102 // (two-phase). 1103 COMPILE_ASSERT(sizeof(int32_t) > 1u, wow_int32_ts_have_size_1); 1104 num_bytes = 1u; 1105 write_ptr = NULL; 1106 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 1107 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); 1108 1109 // Try reading way too much (two-phase). 1110 num_bytes = 20u * sizeof(int32_t); 1111 const void* read_ptr = NULL; 1112 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 1113 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); 1114 1115 // Write half (two-phase). 1116 num_bytes = 5u * sizeof(int32_t); 1117 write_ptr = NULL; 1118 EXPECT_EQ(MOJO_RESULT_OK, 1119 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); 1120 // May provide more space than requested. 1121 EXPECT_GE(num_bytes, 5u * sizeof(int32_t)); 1122 EXPECT_TRUE(write_ptr != NULL); 1123 Seq(0, 5, static_cast<int32_t*>(write_ptr)); 1124 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(5u * sizeof(int32_t))); 1125 1126 // Try reading an amount which isn't a multiple of the element size 1127 // (two-phase). 1128 num_bytes = 1u; 1129 read_ptr = NULL; 1130 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 1131 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); 1132 1133 // Read one (two-phase). 1134 num_bytes = 1u * sizeof(int32_t); 1135 read_ptr = NULL; 1136 EXPECT_EQ(MOJO_RESULT_OK, 1137 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); 1138 EXPECT_GE(num_bytes, 1u * sizeof(int32_t)); 1139 EXPECT_EQ(0, static_cast<const int32_t*>(read_ptr)[0]); 1140 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(1u * sizeof(int32_t))); 1141 1142 // We should have four left, leaving room for six. 1143 num_bytes = 0u; 1144 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1145 EXPECT_EQ(4u * sizeof(int32_t), num_bytes); 1146 1147 // Assuming a tight circular buffer of the specified capacity, we can't do a 1148 // two-phase write of six now. 1149 num_bytes = 6u * sizeof(int32_t); 1150 write_ptr = NULL; 1151 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 1152 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, true)); 1153 1154 // Write six elements (simple), filling the buffer. 1155 num_bytes = 6u * sizeof(int32_t); 1156 int32_t buffer[100]; 1157 Seq(100, 6, buffer); 1158 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(buffer, &num_bytes, true)); 1159 EXPECT_EQ(6u * sizeof(int32_t), num_bytes); 1160 1161 // We have ten. 1162 num_bytes = 0u; 1163 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1164 EXPECT_EQ(10u * sizeof(int32_t), num_bytes); 1165 1166 // But a two-phase read of ten should fail. 1167 num_bytes = 10u * sizeof(int32_t); 1168 read_ptr = NULL; 1169 EXPECT_EQ(MOJO_RESULT_OUT_OF_RANGE, 1170 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); 1171 1172 // Close the producer. 1173 dp->ProducerClose(); 1174 1175 // A two-phase read of nine should work. 1176 num_bytes = 9u * sizeof(int32_t); 1177 read_ptr = NULL; 1178 EXPECT_EQ(MOJO_RESULT_OK, 1179 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); 1180 EXPECT_GE(num_bytes, 9u * sizeof(int32_t)); 1181 EXPECT_EQ(1, static_cast<const int32_t*>(read_ptr)[0]); 1182 EXPECT_EQ(2, static_cast<const int32_t*>(read_ptr)[1]); 1183 EXPECT_EQ(3, static_cast<const int32_t*>(read_ptr)[2]); 1184 EXPECT_EQ(4, static_cast<const int32_t*>(read_ptr)[3]); 1185 EXPECT_EQ(100, static_cast<const int32_t*>(read_ptr)[4]); 1186 EXPECT_EQ(101, static_cast<const int32_t*>(read_ptr)[5]); 1187 EXPECT_EQ(102, static_cast<const int32_t*>(read_ptr)[6]); 1188 EXPECT_EQ(103, static_cast<const int32_t*>(read_ptr)[7]); 1189 EXPECT_EQ(104, static_cast<const int32_t*>(read_ptr)[8]); 1190 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(9u * sizeof(int32_t))); 1191 1192 // A two-phase read of two should fail, with "failed precondition". 1193 num_bytes = 2u * sizeof(int32_t); 1194 read_ptr = NULL; 1195 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 1196 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, true)); 1197 1198 dp->ConsumerClose(); 1199 } 1200 1201 // Tests that |ProducerWriteData()| and |ConsumerReadData()| writes and reads, 1202 // respectively, as much as possible, even if it has to "wrap around" the 1203 // internal circular buffer. (Note that the two-phase write and read do not do 1204 // this.) 1205 TEST(LocalDataPipeTest, WrapAround) { 1206 unsigned char test_data[1000]; 1207 for (size_t i = 0; i < arraysize(test_data); i++) 1208 test_data[i] = static_cast<unsigned char>(i); 1209 1210 const MojoCreateDataPipeOptions options = { 1211 kSizeOfOptions, // |struct_size|. 1212 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 1213 1u, // |element_num_bytes|. 1214 100u // |capacity_num_bytes|. 1215 }; 1216 MojoCreateDataPipeOptions validated_options = { 0 }; 1217 EXPECT_EQ(MOJO_RESULT_OK, 1218 DataPipe::ValidateCreateOptions(&options, &validated_options)); 1219 // This test won't be valid if |ValidateCreateOptions()| decides to give the 1220 // pipe more space. 1221 ASSERT_EQ(100u, validated_options.capacity_num_bytes); 1222 1223 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 1224 1225 // Write 20 bytes. 1226 uint32_t num_bytes = 20u; 1227 EXPECT_EQ(MOJO_RESULT_OK, 1228 dp->ProducerWriteData(&test_data[0], &num_bytes, false)); 1229 EXPECT_EQ(20u, num_bytes); 1230 1231 // Read 10 bytes. 1232 unsigned char read_buffer[1000] = { 0 }; 1233 num_bytes = 10u; 1234 EXPECT_EQ(MOJO_RESULT_OK, 1235 dp->ConsumerReadData(read_buffer, &num_bytes, false)); 1236 EXPECT_EQ(10u, num_bytes); 1237 EXPECT_EQ(0, memcmp(read_buffer, &test_data[0], 10u)); 1238 1239 // 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 1241 // need it for this test.) 1242 void* write_buffer_ptr = NULL; 1243 num_bytes = 0u; 1244 EXPECT_EQ(MOJO_RESULT_OK, 1245 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false)); 1246 EXPECT_TRUE(write_buffer_ptr != NULL); 1247 EXPECT_EQ(80u, num_bytes); 1248 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(0u)); 1249 1250 // Write as much data as we can (using |ProducerWriteData()|). We should write 1251 // 90 bytes. 1252 num_bytes = 200u; 1253 EXPECT_EQ(MOJO_RESULT_OK, 1254 dp->ProducerWriteData(&test_data[20], &num_bytes, false)); 1255 EXPECT_EQ(90u, num_bytes); 1256 1257 // Check that a two-phase read can now only read (at most) 90 bytes. (This 1258 // checks an implementation detail; this behavior is not guaranteed, but we 1259 // need it for this test.) 1260 const void* read_buffer_ptr = NULL; 1261 num_bytes = 0u; 1262 EXPECT_EQ(MOJO_RESULT_OK, 1263 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); 1264 EXPECT_TRUE(read_buffer_ptr != NULL); 1265 EXPECT_EQ(90u, num_bytes); 1266 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(0u)); 1267 1268 // Read as much as possible (using |ConsumerReadData()|). We should read 100 1269 // bytes. 1270 num_bytes = 1271 static_cast<uint32_t>(arraysize(read_buffer) * sizeof(read_buffer[0])); 1272 memset(read_buffer, 0, num_bytes); 1273 EXPECT_EQ(MOJO_RESULT_OK, 1274 dp->ConsumerReadData(read_buffer, &num_bytes, false)); 1275 EXPECT_EQ(100u, num_bytes); 1276 EXPECT_EQ(0, memcmp(read_buffer, &test_data[10], 100u)); 1277 1278 dp->ProducerClose(); 1279 dp->ConsumerClose(); 1280 } 1281 1282 // Tests the behavior of closing the producer or consumer with respect to 1283 // writes and reads (simple and two-phase). 1284 TEST(LocalDataPipeTest, CloseWriteRead) { 1285 const char kTestData[] = "hello world"; 1286 const uint32_t kTestDataSize = static_cast<uint32_t>(sizeof(kTestData)); 1287 1288 const MojoCreateDataPipeOptions options = { 1289 kSizeOfOptions, // |struct_size|. 1290 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 1291 1u, // |element_num_bytes|. 1292 1000u // |capacity_num_bytes|. 1293 }; 1294 MojoCreateDataPipeOptions validated_options = { 0 }; 1295 EXPECT_EQ(MOJO_RESULT_OK, 1296 DataPipe::ValidateCreateOptions(&options, &validated_options)); 1297 1298 // Close producer first, then consumer. 1299 { 1300 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 1301 1302 // Write some data, so we'll have something to read. 1303 uint32_t num_bytes = kTestDataSize; 1304 EXPECT_EQ(MOJO_RESULT_OK, 1305 dp->ProducerWriteData(kTestData, &num_bytes, false)); 1306 EXPECT_EQ(kTestDataSize, num_bytes); 1307 1308 // Write it again, so we'll have something left over. 1309 num_bytes = kTestDataSize; 1310 EXPECT_EQ(MOJO_RESULT_OK, 1311 dp->ProducerWriteData(kTestData, &num_bytes, false)); 1312 EXPECT_EQ(kTestDataSize, num_bytes); 1313 1314 // Start two-phase write. 1315 void* write_buffer_ptr = NULL; 1316 num_bytes = 0u; 1317 EXPECT_EQ(MOJO_RESULT_OK, 1318 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false)); 1319 EXPECT_TRUE(write_buffer_ptr != NULL); 1320 EXPECT_GT(num_bytes, 0u); 1321 1322 // Start two-phase read. 1323 const void* read_buffer_ptr = NULL; 1324 num_bytes = 0u; 1325 EXPECT_EQ(MOJO_RESULT_OK, 1326 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); 1327 EXPECT_TRUE(read_buffer_ptr != NULL); 1328 EXPECT_EQ(2u * kTestDataSize, num_bytes); 1329 1330 // Close the producer. 1331 dp->ProducerClose(); 1332 1333 // The consumer can finish its two-phase read. 1334 EXPECT_EQ(0, memcmp(read_buffer_ptr, kTestData, kTestDataSize)); 1335 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(kTestDataSize)); 1336 1337 // And start another. 1338 read_buffer_ptr = NULL; 1339 num_bytes = 0u; 1340 EXPECT_EQ(MOJO_RESULT_OK, 1341 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); 1342 EXPECT_TRUE(read_buffer_ptr != NULL); 1343 EXPECT_EQ(kTestDataSize, num_bytes); 1344 1345 // Close the consumer, which cancels the two-phase read. 1346 dp->ConsumerClose(); 1347 } 1348 1349 // Close consumer first, then producer. 1350 { 1351 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 1352 1353 // Write some data, so we'll have something to read. 1354 uint32_t num_bytes = kTestDataSize; 1355 EXPECT_EQ(MOJO_RESULT_OK, 1356 dp->ProducerWriteData(kTestData, &num_bytes, false)); 1357 EXPECT_EQ(kTestDataSize, num_bytes); 1358 1359 // Start two-phase write. 1360 void* write_buffer_ptr = NULL; 1361 num_bytes = 0u; 1362 EXPECT_EQ(MOJO_RESULT_OK, 1363 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false)); 1364 EXPECT_TRUE(write_buffer_ptr != NULL); 1365 ASSERT_GT(num_bytes, kTestDataSize); 1366 1367 // Start two-phase read. 1368 const void* read_buffer_ptr = NULL; 1369 num_bytes = 0u; 1370 EXPECT_EQ(MOJO_RESULT_OK, 1371 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); 1372 EXPECT_TRUE(read_buffer_ptr != NULL); 1373 EXPECT_EQ(kTestDataSize, num_bytes); 1374 1375 // Close the consumer. 1376 dp->ConsumerClose(); 1377 1378 // Actually write some data. (Note: Premature freeing of the buffer would 1379 // probably only be detected under ASAN or similar.) 1380 memcpy(write_buffer_ptr, kTestData, kTestDataSize); 1381 // Note: Even though the consumer has been closed, ending the two-phase 1382 // write will report success. 1383 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData(kTestDataSize)); 1384 1385 // But trying to write should result in failure. 1386 num_bytes = kTestDataSize; 1387 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 1388 dp->ProducerWriteData(kTestData, &num_bytes, false)); 1389 1390 // As will trying to start another two-phase write. 1391 write_buffer_ptr = NULL; 1392 num_bytes = 0u; 1393 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 1394 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false)); 1395 1396 dp->ProducerClose(); 1397 } 1398 1399 // Test closing the consumer first, then the producer, with an active 1400 // two-phase write. 1401 { 1402 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 1403 1404 // Start two-phase write. 1405 void* write_buffer_ptr = NULL; 1406 uint32_t num_bytes = 0u; 1407 EXPECT_EQ(MOJO_RESULT_OK, 1408 dp->ProducerBeginWriteData(&write_buffer_ptr, &num_bytes, false)); 1409 EXPECT_TRUE(write_buffer_ptr != NULL); 1410 ASSERT_GT(num_bytes, kTestDataSize); 1411 1412 dp->ConsumerClose(); 1413 dp->ProducerClose(); 1414 } 1415 1416 // Test closing the producer and then trying to read (with no data). 1417 { 1418 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 1419 1420 // Write some data, so we'll have something to read. 1421 uint32_t num_bytes = kTestDataSize; 1422 EXPECT_EQ(MOJO_RESULT_OK, 1423 dp->ProducerWriteData(kTestData, &num_bytes, false)); 1424 EXPECT_EQ(kTestDataSize, num_bytes); 1425 1426 // Close the producer. 1427 dp->ProducerClose(); 1428 1429 // Read that data. 1430 char buffer[1000]; 1431 num_bytes = static_cast<uint32_t>(sizeof(buffer)); 1432 EXPECT_EQ(MOJO_RESULT_OK, 1433 dp->ConsumerReadData(buffer, &num_bytes, false)); 1434 EXPECT_EQ(kTestDataSize, num_bytes); 1435 EXPECT_EQ(0, memcmp(buffer, kTestData, kTestDataSize)); 1436 1437 // A second read should fail. 1438 num_bytes = static_cast<uint32_t>(sizeof(buffer)); 1439 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 1440 dp->ConsumerReadData(buffer, &num_bytes, false)); 1441 1442 // A two-phase read should also fail. 1443 const void* read_buffer_ptr = NULL; 1444 num_bytes = 0u; 1445 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 1446 dp->ConsumerBeginReadData(&read_buffer_ptr, &num_bytes, false)); 1447 1448 // Ditto for discard. 1449 num_bytes = 10u; 1450 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 1451 dp->ConsumerDiscardData(&num_bytes, false)); 1452 1453 dp->ConsumerClose(); 1454 } 1455 } 1456 1457 TEST(LocalDataPipeTest, TwoPhaseMoreInvalidArguments) { 1458 const MojoCreateDataPipeOptions options = { 1459 kSizeOfOptions, // |struct_size|. 1460 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. 1461 static_cast<uint32_t>(sizeof(int32_t)), // |element_num_bytes|. 1462 10 * sizeof(int32_t) // |capacity_num_bytes|. 1463 }; 1464 MojoCreateDataPipeOptions validated_options = { 0 }; 1465 EXPECT_EQ(MOJO_RESULT_OK, 1466 DataPipe::ValidateCreateOptions(&options, &validated_options)); 1467 1468 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 1469 1470 // No data. 1471 uint32_t num_bytes = 1000u; 1472 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1473 EXPECT_EQ(0u, num_bytes); 1474 1475 // Try "ending" a two-phase write when one isn't active. 1476 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 1477 dp->ProducerEndWriteData(1u * sizeof(int32_t))); 1478 1479 // Still no data. 1480 num_bytes = 1000u; 1481 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1482 EXPECT_EQ(0u, num_bytes); 1483 1484 // Try ending a two-phase write with an invalid amount (too much). 1485 num_bytes = 0u; 1486 void* write_ptr = NULL; 1487 EXPECT_EQ(MOJO_RESULT_OK, 1488 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); 1489 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 1490 dp->ProducerEndWriteData( 1491 num_bytes + static_cast<uint32_t>(sizeof(int32_t)))); 1492 1493 // But the two-phase write still ended. 1494 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u)); 1495 1496 // Still no data. 1497 num_bytes = 1000u; 1498 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1499 EXPECT_EQ(0u, num_bytes); 1500 1501 // Try ending a two-phase write with an invalid amount (not a multiple of the 1502 // element size). 1503 num_bytes = 0u; 1504 write_ptr = NULL; 1505 EXPECT_EQ(MOJO_RESULT_OK, 1506 dp->ProducerBeginWriteData(&write_ptr, &num_bytes, false)); 1507 EXPECT_GE(num_bytes, 1u); 1508 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ProducerEndWriteData(1u)); 1509 1510 // But the two-phase write still ended. 1511 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, dp->ProducerEndWriteData(0u)); 1512 1513 // Still no data. 1514 num_bytes = 1000u; 1515 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1516 EXPECT_EQ(0u, num_bytes); 1517 1518 // Now write some data, so we'll be able to try reading. 1519 int32_t element = 123; 1520 num_bytes = 1u * sizeof(int32_t); 1521 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(&element, &num_bytes, false)); 1522 1523 // One element available. 1524 num_bytes = 0u; 1525 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1526 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 1527 1528 // Try "ending" a two-phase read when one isn't active. 1529 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 1530 dp->ConsumerEndReadData(1u * sizeof(int32_t))); 1531 1532 // Still one element available. 1533 num_bytes = 0u; 1534 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1535 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 1536 1537 // Try ending a two-phase read with an invalid amount (too much). 1538 num_bytes = 0u; 1539 const void* read_ptr = NULL; 1540 EXPECT_EQ(MOJO_RESULT_OK, 1541 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false)); 1542 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 1543 dp->ConsumerEndReadData( 1544 num_bytes + static_cast<uint32_t>(sizeof(int32_t)))); 1545 1546 // Still one element available. 1547 num_bytes = 0u; 1548 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1549 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 1550 1551 // Try ending a two-phase read with an invalid amount (not a multiple of the 1552 // element size). 1553 num_bytes = 0u; 1554 read_ptr = NULL; 1555 EXPECT_EQ(MOJO_RESULT_OK, 1556 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false)); 1557 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 1558 EXPECT_EQ(123, static_cast<const int32_t*>(read_ptr)[0]); 1559 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dp->ConsumerEndReadData(1u)); 1560 1561 // Still one element available. 1562 num_bytes = 0u; 1563 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerQueryData(&num_bytes)); 1564 EXPECT_EQ(1u * sizeof(int32_t), num_bytes); 1565 1566 dp->ProducerClose(); 1567 dp->ConsumerClose(); 1568 } 1569 1570 // Tests that even with "may discard", the data won't change under a two-phase 1571 // read. 1572 // TODO(vtl): crbug.com/348644: We currently don't pass this. (There are two 1573 // related issues: First, we don't recognize that the data given to 1574 // |ConsumerBeginReadData()| isn't discardable until |ConsumerEndReadData()|, 1575 // and thus we erroneously allow |ProducerWriteData()| to succeed. Second, the 1576 // |ProducerWriteData()| then changes the data underneath the two-phase read.) 1577 TEST(LocalDataPipeTest, DISABLED_MayDiscardTwoPhaseConsistent) { 1578 const MojoCreateDataPipeOptions options = { 1579 kSizeOfOptions, // |struct_size|. 1580 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD, // |flags|. 1581 1, // |element_num_bytes|. 1582 2 // |capacity_num_bytes|. 1583 }; 1584 MojoCreateDataPipeOptions validated_options = { 0 }; 1585 EXPECT_EQ(MOJO_RESULT_OK, 1586 DataPipe::ValidateCreateOptions(&options, &validated_options)); 1587 1588 scoped_refptr<LocalDataPipe> dp(new LocalDataPipe(validated_options)); 1589 1590 // Write some elements. 1591 char elements[2] = { 'a', 'b' }; 1592 uint32_t num_bytes = 2u; 1593 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false)); 1594 EXPECT_EQ(2u, num_bytes); 1595 1596 // Begin reading. 1597 const void* read_ptr = NULL; 1598 num_bytes = 2u; 1599 EXPECT_EQ(MOJO_RESULT_OK, 1600 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false)); 1601 EXPECT_EQ(2u, num_bytes); 1602 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]); 1603 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]); 1604 1605 // Try to write some more. But nothing should be discardable right now. 1606 elements[0] = 'x'; 1607 elements[1] = 'y'; 1608 num_bytes = 2u; 1609 // TODO(vtl): This should be: 1610 // EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 1611 // dp->ProducerWriteData(elements, &num_bytes, false)); 1612 // but we incorrectly think that the bytes being read are discardable. Letting 1613 // this through reveals the significant consequence. 1614 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false)); 1615 1616 // Check that our read buffer hasn't changed underneath us. 1617 EXPECT_EQ('a', static_cast<const char*>(read_ptr)[0]); 1618 EXPECT_EQ('b', static_cast<const char*>(read_ptr)[1]); 1619 1620 // End reading. 1621 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); 1622 1623 // Now writing should succeed. 1624 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerWriteData(elements, &num_bytes, false)); 1625 1626 // And if we read, we should get the new values. 1627 read_ptr = NULL; 1628 num_bytes = 2u; 1629 EXPECT_EQ(MOJO_RESULT_OK, 1630 dp->ConsumerBeginReadData(&read_ptr, &num_bytes, false)); 1631 EXPECT_EQ(2u, num_bytes); 1632 EXPECT_EQ('x', static_cast<const char*>(read_ptr)[0]); 1633 EXPECT_EQ('y', static_cast<const char*>(read_ptr)[1]); 1634 1635 // End reading. 1636 EXPECT_EQ(MOJO_RESULT_OK, dp->ConsumerEndReadData(2u)); 1637 1638 dp->ProducerClose(); 1639 dp->ConsumerClose(); 1640 } 1641 1642 } // namespace 1643 } // namespace system 1644 } // namespace mojo 1645