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 #include "modules/fetch/DataConsumerTee.h" | 5 #include "modules/fetch/DataConsumerTee.h" |
6 | 6 |
7 #include "core/testing/DummyPageHolder.h" | 7 #include "core/testing/DummyPageHolder.h" |
8 #include "core/testing/NullExecutionContext.h" | 8 #include "core/testing/NullExecutionContext.h" |
9 #include "modules/fetch/DataConsumerHandleTestUtil.h" | 9 #include "modules/fetch/DataConsumerHandleTestUtil.h" |
10 #include "platform/CrossThreadFunctional.h" | 10 #include "platform/CrossThreadFunctional.h" |
11 #include "platform/WaitableEvent.h" | 11 #include "platform/WaitableEvent.h" |
12 #include "platform/WebThreadSupportingGC.h" | 12 #include "platform/WebThreadSupportingGC.h" |
13 #include "public/platform/Platform.h" | 13 #include "public/platform/Platform.h" |
14 #include "public/platform/WebThread.h" | 14 #include "public/platform/WebThread.h" |
15 #include "public/platform/WebTraceLocation.h" | 15 #include "public/platform/WebTraceLocation.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "wtf/PassRefPtr.h" | 17 #include "wtf/PassRefPtr.h" |
18 #include "wtf/PtrUtil.h" | 18 #include "wtf/PtrUtil.h" |
19 #include "wtf/RefPtr.h" | 19 #include "wtf/RefPtr.h" |
20 #include <memory> | 20 #include <memory> |
21 #include <string.h> | 21 #include <string.h> |
22 #include <v8.h> | 22 #include <v8.h> |
23 | 23 |
24 namespace blink { | 24 namespace blink { |
25 namespace { | 25 namespace { |
26 | 26 |
| 27 using ::testing::ByMove; |
27 using ::testing::InSequence; | 28 using ::testing::InSequence; |
28 using ::testing::Return; | 29 using ::testing::Return; |
29 using ::testing::StrictMock; | 30 using ::testing::StrictMock; |
30 using ::testing::_; | 31 using ::testing::_; |
31 using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>; | 32 using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>; |
32 | 33 |
33 using Result = WebDataConsumerHandle::Result; | 34 using Result = WebDataConsumerHandle::Result; |
34 using Thread = DataConsumerHandleTestUtil::Thread; | 35 using Thread = DataConsumerHandleTestUtil::Thread; |
35 const Result kDone = WebDataConsumerHandle::Done; | 36 const Result kDone = WebDataConsumerHandle::Done; |
36 const Result kUnexpectedError = WebDataConsumerHandle::UnexpectedError; | 37 const Result kUnexpectedError = WebDataConsumerHandle::UnexpectedError; |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 | 324 |
324 // Collect garbage to finalize the source reader. | 325 // Collect garbage to finalize the source reader. |
325 ThreadHeap::collectAllGarbage(); | 326 ThreadHeap::collectAllGarbage(); |
326 context->detached()->wait(); | 327 context->detached()->wait(); |
327 } | 328 } |
328 | 329 |
329 TEST(FetchDataConsumerTeeTest, Create) | 330 TEST(FetchDataConsumerTeeTest, Create) |
330 { | 331 { |
331 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(); | 332 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(); |
332 std::unique_ptr<MockFetchDataConsumerHandle> src(MockFetchDataConsumerHandle
::create()); | 333 std::unique_ptr<MockFetchDataConsumerHandle> src(MockFetchDataConsumerHandle
::create()); |
333 std::unique_ptr<MockFetchDataConsumerReader> reader(MockFetchDataConsumerRea
der::create()); | 334 |
| 335 // |reader| will be adopted by |obtainFetchDataReader|. |
| 336 MockFetchDataConsumerReader* reader = MockFetchDataConsumerReader::create().
release(); |
334 | 337 |
335 Checkpoint checkpoint; | 338 Checkpoint checkpoint; |
336 InSequence s; | 339 InSequence s; |
337 EXPECT_CALL(checkpoint, Call(1)); | 340 EXPECT_CALL(checkpoint, Call(1)); |
338 EXPECT_CALL(*src, obtainReaderInternal(_)).WillOnce(Return(reader.get())); | 341 EXPECT_CALL(*src, obtainFetchDataReader(_)).WillOnce(Return(ByMove(WTF::wrap
Unique(reader)))); |
339 EXPECT_CALL(*reader, drainAsBlobDataHandle(kAllowBlobWithInvalidSize)).WillO
nce(Return(blobDataHandle)); | 342 EXPECT_CALL(*reader, drainAsBlobDataHandle(kAllowBlobWithInvalidSize)).WillO
nce(Return(blobDataHandle)); |
340 EXPECT_CALL(*reader, destruct()); | 343 EXPECT_CALL(*reader, destruct()); |
341 EXPECT_CALL(checkpoint, Call(2)); | 344 EXPECT_CALL(checkpoint, Call(2)); |
342 | 345 |
343 // |reader| is adopted by |obtainReader|. | |
344 ASSERT_TRUE(reader.release()); | |
345 | |
346 std::unique_ptr<FetchDataConsumerHandle> dest1, dest2; | 346 std::unique_ptr<FetchDataConsumerHandle> dest1, dest2; |
347 std::unique_ptr<TeeCreationThread<FetchDataConsumerHandle>> t = wrapUnique(n
ew TeeCreationThread<FetchDataConsumerHandle>()); | 347 std::unique_ptr<TeeCreationThread<FetchDataConsumerHandle>> t = wrapUnique(n
ew TeeCreationThread<FetchDataConsumerHandle>()); |
348 | 348 |
349 checkpoint.Call(1); | 349 checkpoint.Call(1); |
350 t->run(std::move(src), &dest1, &dest2); | 350 t->run(std::move(src), &dest1, &dest2); |
351 checkpoint.Call(2); | 351 checkpoint.Call(2); |
352 | 352 |
353 ASSERT_TRUE(dest1); | 353 ASSERT_TRUE(dest1); |
354 ASSERT_TRUE(dest2); | 354 ASSERT_TRUE(dest2); |
355 EXPECT_EQ(blobDataHandle, dest1->obtainReader(nullptr)->drainAsBlobDataHandl
e(kAllowBlobWithInvalidSize)); | 355 EXPECT_EQ(blobDataHandle, dest1->obtainFetchDataReader(nullptr)->drainAsBlob
DataHandle(kAllowBlobWithInvalidSize)); |
356 EXPECT_EQ(blobDataHandle, dest2->obtainReader(nullptr)->drainAsBlobDataHandl
e(kAllowBlobWithInvalidSize)); | 356 EXPECT_EQ(blobDataHandle, dest2->obtainFetchDataReader(nullptr)->drainAsBlob
DataHandle(kAllowBlobWithInvalidSize)); |
357 } | 357 } |
358 | 358 |
359 TEST(FetchDataConsumerTeeTest, CreateFromBlobWithInvalidSize) | 359 TEST(FetchDataConsumerTeeTest, CreateFromBlobWithInvalidSize) |
360 { | 360 { |
361 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), -1); | 361 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre
ate(), -1); |
362 std::unique_ptr<MockFetchDataConsumerHandle> src(MockFetchDataConsumerHandle
::create()); | 362 std::unique_ptr<MockFetchDataConsumerHandle> src(MockFetchDataConsumerHandle
::create()); |
363 std::unique_ptr<MockFetchDataConsumerReader> reader(MockFetchDataConsumerRea
der::create()); | 363 |
| 364 // |reader| is adopted by |obtainFetchDataReader|. |
| 365 MockFetchDataConsumerReader* reader = MockFetchDataConsumerReader::create().
release(); |
364 | 366 |
365 Checkpoint checkpoint; | 367 Checkpoint checkpoint; |
366 InSequence s; | 368 InSequence s; |
367 EXPECT_CALL(checkpoint, Call(1)); | 369 EXPECT_CALL(checkpoint, Call(1)); |
368 EXPECT_CALL(*src, obtainReaderInternal(_)).WillOnce(Return(reader.get())); | 370 EXPECT_CALL(*src, obtainFetchDataReader(_)).WillOnce(Return(ByMove(WTF::wrap
Unique(reader)))); |
369 EXPECT_CALL(*reader, drainAsBlobDataHandle(kAllowBlobWithInvalidSize)).WillO
nce(Return(blobDataHandle)); | 371 EXPECT_CALL(*reader, drainAsBlobDataHandle(kAllowBlobWithInvalidSize)).WillO
nce(Return(blobDataHandle)); |
370 EXPECT_CALL(*reader, destruct()); | 372 EXPECT_CALL(*reader, destruct()); |
371 EXPECT_CALL(checkpoint, Call(2)); | 373 EXPECT_CALL(checkpoint, Call(2)); |
372 | 374 |
373 // |reader| is adopted by |obtainReader|. | |
374 ASSERT_TRUE(reader.release()); | |
375 | |
376 std::unique_ptr<FetchDataConsumerHandle> dest1, dest2; | 375 std::unique_ptr<FetchDataConsumerHandle> dest1, dest2; |
377 std::unique_ptr<TeeCreationThread<FetchDataConsumerHandle>> t = wrapUnique(n
ew TeeCreationThread<FetchDataConsumerHandle>()); | 376 std::unique_ptr<TeeCreationThread<FetchDataConsumerHandle>> t = wrapUnique(n
ew TeeCreationThread<FetchDataConsumerHandle>()); |
378 | 377 |
379 checkpoint.Call(1); | 378 checkpoint.Call(1); |
380 t->run(std::move(src), &dest1, &dest2); | 379 t->run(std::move(src), &dest1, &dest2); |
381 checkpoint.Call(2); | 380 checkpoint.Call(2); |
382 | 381 |
383 ASSERT_TRUE(dest1); | 382 ASSERT_TRUE(dest1); |
384 ASSERT_TRUE(dest2); | 383 ASSERT_TRUE(dest2); |
385 EXPECT_FALSE(dest1->obtainReader(nullptr)->drainAsBlobDataHandle(kDisallowBl
obWithInvalidSize)); | 384 EXPECT_FALSE(dest1->obtainFetchDataReader(nullptr)->drainAsBlobDataHandle(kD
isallowBlobWithInvalidSize)); |
386 EXPECT_EQ(blobDataHandle, dest1->obtainReader(nullptr)->drainAsBlobDataHandl
e(kAllowBlobWithInvalidSize)); | 385 EXPECT_EQ(blobDataHandle, dest1->obtainFetchDataReader(nullptr)->drainAsBlob
DataHandle(kAllowBlobWithInvalidSize)); |
387 EXPECT_FALSE(dest2->obtainReader(nullptr)->drainAsBlobDataHandle(kDisallowBl
obWithInvalidSize)); | 386 EXPECT_FALSE(dest2->obtainFetchDataReader(nullptr)->drainAsBlobDataHandle(kD
isallowBlobWithInvalidSize)); |
388 EXPECT_EQ(blobDataHandle, dest2->obtainReader(nullptr)->drainAsBlobDataHandl
e(kAllowBlobWithInvalidSize)); | 387 EXPECT_EQ(blobDataHandle, dest2->obtainFetchDataReader(nullptr)->drainAsBlob
DataHandle(kAllowBlobWithInvalidSize)); |
389 } | 388 } |
390 | 389 |
391 TEST(FetchDataConsumerTeeTest, CreateDone) | 390 TEST(FetchDataConsumerTeeTest, CreateDone) |
392 { | 391 { |
393 std::unique_ptr<Handle> src(Handle::create()); | 392 std::unique_ptr<Handle> src(Handle::create()); |
394 std::unique_ptr<FetchDataConsumerHandle> dest1, dest2; | 393 std::unique_ptr<FetchDataConsumerHandle> dest1, dest2; |
395 | 394 |
396 src->add(Command(Command::Done)); | 395 src->add(Command(Command::Done)); |
397 | 396 |
398 std::unique_ptr<TeeCreationThread<FetchDataConsumerHandle>> t = wrapUnique(n
ew TeeCreationThread<FetchDataConsumerHandle>()); | 397 std::unique_ptr<TeeCreationThread<FetchDataConsumerHandle>> t = wrapUnique(n
ew TeeCreationThread<FetchDataConsumerHandle>()); |
399 t->run(createFetchDataConsumerHandleFromWebHandle(std::move(src)), &dest1, &
dest2); | 398 t->run(createFetchDataConsumerHandleFromWebHandle(std::move(src)), &dest1, &
dest2); |
400 | 399 |
401 ASSERT_TRUE(dest1); | 400 ASSERT_TRUE(dest1); |
402 ASSERT_TRUE(dest2); | 401 ASSERT_TRUE(dest2); |
403 | 402 |
404 EXPECT_FALSE(dest1->obtainReader(nullptr)->drainAsBlobDataHandle(kAllowBlobW
ithInvalidSize)); | 403 EXPECT_FALSE(dest1->obtainFetchDataReader(nullptr)->drainAsBlobDataHandle(kA
llowBlobWithInvalidSize)); |
405 EXPECT_FALSE(dest2->obtainReader(nullptr)->drainAsBlobDataHandle(kAllowBlobW
ithInvalidSize)); | 404 EXPECT_FALSE(dest2->obtainFetchDataReader(nullptr)->drainAsBlobDataHandle(kA
llowBlobWithInvalidSize)); |
406 | 405 |
407 HandleReaderRunner<HandleReader> r1(std::move(dest1)), r2(std::move(dest2)); | 406 HandleReaderRunner<HandleReader> r1(std::move(dest1)), r2(std::move(dest2)); |
408 | 407 |
409 std::unique_ptr<HandleReadResult> res1 = r1.wait(); | 408 std::unique_ptr<HandleReadResult> res1 = r1.wait(); |
410 std::unique_ptr<HandleReadResult> res2 = r2.wait(); | 409 std::unique_ptr<HandleReadResult> res2 = r2.wait(); |
411 | 410 |
412 EXPECT_EQ(kDone, res1->result()); | 411 EXPECT_EQ(kDone, res1->result()); |
413 EXPECT_EQ(0u, res1->data().size()); | 412 EXPECT_EQ(0u, res1->data().size()); |
414 EXPECT_EQ(kDone, res2->result()); | 413 EXPECT_EQ(kDone, res2->result()); |
415 EXPECT_EQ(0u, res2->data().size()); | 414 EXPECT_EQ(0u, res2->data().size()); |
416 } | 415 } |
417 | 416 |
418 } // namespace | 417 } // namespace |
419 } // namespace blink | 418 } // namespace blink |
OLD | NEW |