| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // This file tests the C API, but using the explicit MojoSystemImpl parameter. | 5 // This file tests the C API, but using the explicit MojoSystemImpl parameter. |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "mojo/public/platform/native/system_impl_private.h" | 9 #include "mojo/public/platform/native/system_impl_private.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); | 262 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); |
| 263 | 263 |
| 264 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplClose(sys1, hc)); | 264 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplClose(sys1, hc)); |
| 265 | 265 |
| 266 // TODO(vtl): Test the other way around -- closing the consumer should make | 266 // TODO(vtl): Test the other way around -- closing the consumer should make |
| 267 // the producer never-writable? | 267 // the producer never-writable? |
| 268 | 268 |
| 269 // 2 SystemImpls are leaked... | 269 // 2 SystemImpls are leaked... |
| 270 } | 270 } |
| 271 | 271 |
| 272 // TODO(vtl): Once thunks are in: | 272 TEST(SystemImplTest, DataPipeWriteThreshold) { |
| 273 // TEST(SystemImplTest, DataPipeWriteThreshold) { ... } | 273 MojoSystemImpl sys0 = MojoSystemImplCreateImpl(); |
| 274 MojoSystemImpl sys1 = MojoSystemImplCreateImpl(); |
| 275 EXPECT_NE(sys0, sys1); |
| 276 |
| 277 const MojoCreateDataPipeOptions options = { |
| 278 static_cast<uint32_t>( |
| 279 sizeof(MojoCreateDataPipeOptions)), // |struct_size|. |
| 280 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, // |flags|. |
| 281 2u, // |element_num_bytes|. |
| 282 4u // |capacity_num_bytes|. |
| 283 }; |
| 284 MojoHandle hp = MOJO_HANDLE_INVALID; |
| 285 MojoHandle hc = MOJO_HANDLE_INVALID; |
| 286 EXPECT_EQ(MOJO_RESULT_OK, |
| 287 MojoSystemImplCreateDataPipe(sys0, &options, &hp, &hc)); |
| 288 EXPECT_NE(hp, MOJO_HANDLE_INVALID); |
| 289 EXPECT_NE(hc, MOJO_HANDLE_INVALID); |
| 290 EXPECT_NE(hc, hp); |
| 291 |
| 292 // Move the other end of the pipe to a different SystemImpl. |
| 293 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplTransferHandle(sys0, hc, sys1, &hc)); |
| 294 EXPECT_NE(hc, MOJO_HANDLE_INVALID); |
| 295 |
| 296 MojoDataPipeProducerOptions popts; |
| 297 static const uint32_t kPoptsSize = static_cast<uint32_t>(sizeof(popts)); |
| 298 |
| 299 // Check the current write threshold; should be the default. |
| 300 memset(&popts, 255, kPoptsSize); |
| 301 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetDataPipeProducerOptions( |
| 302 sys0, hp, &popts, kPoptsSize)); |
| 303 EXPECT_EQ(kPoptsSize, popts.struct_size); |
| 304 EXPECT_EQ(0u, popts.write_threshold_num_bytes); |
| 305 |
| 306 // Should already have the write threshold signal. |
| 307 MojoHandleSignalsState state = {}; |
| 308 EXPECT_EQ(MOJO_RESULT_OK, |
| 309 MojoSystemImplWait(sys0, hp, MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, 0, |
| 310 &state)); |
| 311 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, |
| 312 state.satisfied_signals); |
| 313 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED | |
| 314 MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, |
| 315 state.satisfiable_signals); |
| 316 |
| 317 // Try setting the write threshold to something invalid. |
| 318 popts.struct_size = kPoptsSize; |
| 319 popts.write_threshold_num_bytes = 1u; |
| 320 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 321 MojoSystemImplSetDataPipeProducerOptions(sys0, hp, &popts)); |
| 322 // It shouldn't change the options. |
| 323 memset(&popts, 255, kPoptsSize); |
| 324 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetDataPipeProducerOptions( |
| 325 sys0, hp, &popts, kPoptsSize)); |
| 326 EXPECT_EQ(kPoptsSize, popts.struct_size); |
| 327 EXPECT_EQ(0u, popts.write_threshold_num_bytes); |
| 328 |
| 329 // Write an element. |
| 330 static const uint16_t kTestElem = 12345u; |
| 331 uint32_t num_bytes = 2u; |
| 332 EXPECT_EQ(MOJO_RESULT_OK, |
| 333 MojoSystemImplWriteData(sys0, hp, &kTestElem, &num_bytes, |
| 334 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 335 EXPECT_EQ(2u, num_bytes); |
| 336 |
| 337 // Should still have the write threshold signal. |
| 338 EXPECT_EQ(MOJO_RESULT_OK, |
| 339 MojoSystemImplWait(sys0, hp, MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, 0, |
| 340 nullptr)); |
| 341 |
| 342 // Write another element. |
| 343 static const uint16_t kAnotherTestElem = 12345u; |
| 344 num_bytes = 2u; |
| 345 EXPECT_EQ(MOJO_RESULT_OK, |
| 346 MojoSystemImplWriteData(sys0, hp, &kAnotherTestElem, &num_bytes, |
| 347 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 348 EXPECT_EQ(2u, num_bytes); |
| 349 |
| 350 // Should no longer have the write threshold signal. |
| 351 state = MojoHandleSignalsState(); |
| 352 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, |
| 353 MojoSystemImplWait(sys0, hp, MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, 0, |
| 354 &state)); |
| 355 EXPECT_EQ(0u, state.satisfied_signals); |
| 356 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED | |
| 357 MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, |
| 358 state.satisfiable_signals); |
| 359 |
| 360 // Set the write threshold to 2 (one element). |
| 361 popts.struct_size = kPoptsSize; |
| 362 popts.write_threshold_num_bytes = 2u; |
| 363 EXPECT_EQ(MOJO_RESULT_OK, |
| 364 MojoSystemImplSetDataPipeProducerOptions(sys0, hp, &popts)); |
| 365 // It should actually change the options. |
| 366 memset(&popts, 255, kPoptsSize); |
| 367 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplGetDataPipeProducerOptions( |
| 368 sys0, hp, &popts, kPoptsSize)); |
| 369 EXPECT_EQ(kPoptsSize, popts.struct_size); |
| 370 EXPECT_EQ(2u, popts.write_threshold_num_bytes); |
| 371 |
| 372 // Should still not have the write threshold signal. |
| 373 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, |
| 374 MojoSystemImplWait(sys0, hp, MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, 0, |
| 375 nullptr)); |
| 376 |
| 377 // Read an element. |
| 378 uint16_t read_elem = 0u; |
| 379 num_bytes = 2u; |
| 380 EXPECT_EQ(MOJO_RESULT_OK, |
| 381 MojoSystemImplReadData(sys1, hc, &read_elem, &num_bytes, |
| 382 MOJO_READ_DATA_FLAG_NONE)); |
| 383 EXPECT_EQ(2u, num_bytes); |
| 384 EXPECT_EQ(kTestElem, read_elem); |
| 385 |
| 386 // Should get the write threshold signal now. |
| 387 state = MojoHandleSignalsState(); |
| 388 EXPECT_EQ(MOJO_RESULT_OK, |
| 389 MojoSystemImplWait(sys0, hp, MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, |
| 390 1000, &state)); |
| 391 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, |
| 392 state.satisfied_signals); |
| 393 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED | |
| 394 MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, |
| 395 state.satisfiable_signals); |
| 396 |
| 397 // Set the write threshold to 4 (two elements). |
| 398 popts.struct_size = kPoptsSize; |
| 399 popts.write_threshold_num_bytes = 4u; |
| 400 EXPECT_EQ(MOJO_RESULT_OK, |
| 401 MojoSystemImplSetDataPipeProducerOptions(sys0, hp, &popts)); |
| 402 |
| 403 // Should again not have the write threshold signal. |
| 404 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, |
| 405 MojoSystemImplWait(sys0, hp, MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, 0, |
| 406 nullptr)); |
| 407 |
| 408 // Close the consumer. |
| 409 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplClose(sys1, hc)); |
| 410 |
| 411 // The write threshold signal should now be unsatisfiable. |
| 412 state = MojoHandleSignalsState(); |
| 413 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, |
| 414 MojoSystemImplWait(sys0, hp, MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD, 0, |
| 415 &state)); |
| 416 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); |
| 417 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); |
| 418 |
| 419 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplClose(sys0, hp)); |
| 420 } |
| 274 | 421 |
| 275 TEST(SystemImplTest, DataPipeReadThreshold) { | 422 TEST(SystemImplTest, DataPipeReadThreshold) { |
| 276 MojoSystemImpl sys0 = MojoSystemImplCreateImpl(); | 423 MojoSystemImpl sys0 = MojoSystemImplCreateImpl(); |
| 277 MojoSystemImpl sys1 = MojoSystemImplCreateImpl(); | 424 MojoSystemImpl sys1 = MojoSystemImplCreateImpl(); |
| 278 EXPECT_NE(sys0, sys1); | 425 EXPECT_NE(sys0, sys1); |
| 279 | 426 |
| 280 MojoHandle hp = MOJO_HANDLE_INVALID; | 427 MojoHandle hp = MOJO_HANDLE_INVALID; |
| 281 MojoHandle hc = MOJO_HANDLE_INVALID; | 428 MojoHandle hc = MOJO_HANDLE_INVALID; |
| 282 EXPECT_EQ(MOJO_RESULT_OK, | 429 EXPECT_EQ(MOJO_RESULT_OK, |
| 283 MojoSystemImplCreateDataPipe(sys0, nullptr, &hp, &hc)); | 430 MojoSystemImplCreateDataPipe(sys0, nullptr, &hp, &hc)); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 // Unmap it. | 635 // Unmap it. |
| 489 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplUnmapBuffer(sys1, pointer)); | 636 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplUnmapBuffer(sys1, pointer)); |
| 490 | 637 |
| 491 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplClose(sys1, h1)); | 638 EXPECT_EQ(MOJO_RESULT_OK, MojoSystemImplClose(sys1, h1)); |
| 492 | 639 |
| 493 // 2 SystemImpls are leaked... | 640 // 2 SystemImpls are leaked... |
| 494 } | 641 } |
| 495 | 642 |
| 496 } // namespace | 643 } // namespace |
| 497 } // namespace mojo | 644 } // namespace mojo |
| OLD | NEW |