Index: content/child/blob_storage/blob_transport_controller_unittest.cc |
diff --git a/content/child/blob_storage/blob_transport_controller_unittest.cc b/content/child/blob_storage/blob_transport_controller_unittest.cc |
index 08120fcaca7c23a4809d5c3410fbaed9240baf77..7c2a1bec45692db37608e891e44e1571826bbffd 100644 |
--- a/content/child/blob_storage/blob_transport_controller_unittest.cc |
+++ b/content/child/blob_storage/blob_transport_controller_unittest.cc |
@@ -7,13 +7,23 @@ |
#include <stddef.h> |
#include <stdint.h> |
-#include "base/memory/ptr_util.h" |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/files/file.h" |
+#include "base/files/file_path.h" |
+#include "base/files/file_util.h" |
#include "base/memory/shared_memory.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/process/process_handle.h" |
+#include "base/test/test_file_util.h" |
#include "base/test/test_simple_task_runner.h" |
+#include "base/thread_task_runner_handle.h" |
#include "base/tuple.h" |
#include "content/child/blob_storage/blob_consolidation.h" |
#include "content/child/thread_safe_sender.h" |
#include "content/common/fileapi/webblob_messages.h" |
+#include "ipc/ipc_message.h" |
+#include "ipc/ipc_platform_file.h" |
#include "ipc/ipc_sender.h" |
#include "ipc/ipc_sync_message_filter.h" |
#include "ipc/ipc_test_sink.h" |
@@ -22,9 +32,13 @@ |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+using base::File; |
+using base::FilePath; |
+using base::TestSimpleTaskRunner; |
using storage::BlobItemBytesRequest; |
using storage::BlobItemBytesResponse; |
using storage::DataElement; |
+using storage::IPCBlobCreationCancelCode; |
namespace content { |
namespace { |
@@ -87,13 +101,28 @@ static blink::WebThreadSafeData CreateData(const std::string& str) { |
class BlobTransportControllerTest : public testing::Test { |
public: |
- BlobTransportControllerTest() |
- : io_thread_runner_(new base::TestSimpleTaskRunner()), |
- main_thread_runner_(new OtherThreadTestSimpleTaskRunner()) {} |
+ BlobTransportControllerTest() : thread_runner_handle_(io_thread_runner_) {} |
void SetUp() override { |
sender_ = new BlobTransportControllerTestSender(&sink_); |
- BlobTransportController::GetInstance()->ClearForTesting(); |
+ BlobTransportController::GetInstance()->CancelAllBlobTransfers(); |
+ } |
+ |
+ void OnMemoryRequest( |
+ BlobTransportController* holder, |
+ const std::string uuid, |
+ const std::vector<storage::BlobItemBytesRequest>& requests, |
+ std::vector<base::SharedMemoryHandle>* memory_handles, |
+ std::vector<IPC::PlatformFileForTransit> file_handles) { |
+ holder->OnMemoryRequest(uuid, requests, memory_handles, file_handles, |
+ file_thread_runner_.get(), &sink_); |
+ } |
+ |
+ FilePath CreateTemporaryFile() { |
+ FilePath path; |
+ EXPECT_TRUE(base::CreateTemporaryFile(&path)); |
+ files_opened_.push_back(path); |
+ return path; |
} |
void ExpectRegisterAndStartMessage(const std::string& expected_uuid, |
@@ -117,16 +146,62 @@ class BlobTransportControllerTest : public testing::Test { |
*descriptions = base::get<1>(start_contents); |
// We don't have dispositions from the renderer. |
EXPECT_TRUE(base::get<2>(register_contents).empty()); |
+ sink_.ClearMessages(); |
+ } |
+ |
+ void ExpectMemoryResponses( |
+ const std::string& expected_uuid, |
+ std::vector<storage::BlobItemBytesResponse> expected_responses) { |
+ const IPC::Message* responses_message = |
+ sink_.GetUniqueMessageMatching(BlobStorageMsg_MemoryItemResponse::ID); |
+ ASSERT_TRUE(responses_message); |
+ base::Tuple<std::string, std::vector<storage::BlobItemBytesResponse>> |
+ responses_content; |
+ BlobStorageMsg_MemoryItemResponse::Read(responses_message, |
+ &responses_content); |
+ EXPECT_EQ(expected_uuid, base::get<0>(responses_content)); |
+ EXPECT_EQ(expected_responses, base::get<1>(responses_content)); |
+ sink_.ClearMessages(); |
+ } |
+ |
+ void ExpectCancel(const std::string& expected_uuid, |
+ storage::IPCBlobCreationCancelCode expected_code) { |
+ const IPC::Message* cancel_message = |
+ sink_.GetUniqueMessageMatching(BlobStorageMsg_CancelBuildingBlob::ID); |
+ ASSERT_TRUE(cancel_message); |
+ base::Tuple<std::string, storage::IPCBlobCreationCancelCode> cancel_content; |
+ BlobStorageMsg_CancelBuildingBlob::Read(cancel_message, &cancel_content); |
+ EXPECT_EQ(expected_uuid, base::get<0>(cancel_content)); |
+ EXPECT_EQ(expected_code, base::get<1>(cancel_content)); |
} |
void TearDown() override { |
- BlobTransportController::GetInstance()->ClearForTesting(); |
+ BlobTransportController::GetInstance()->CancelAllBlobTransfers(); |
+ for (const FilePath& path : files_opened_) { |
+ EXPECT_TRUE(base::DeleteFile(path, false)); |
+ } |
+ EXPECT_FALSE(io_thread_runner_->HasPendingTask()); |
+ EXPECT_FALSE(file_thread_runner_->HasPendingTask()); |
+ EXPECT_FALSE(main_thread_runner_->HasPendingTask()); |
} |
+ protected: |
+ std::vector<FilePath> files_opened_; |
+ |
IPC::TestSink sink_; |
scoped_refptr<BlobTransportControllerTestSender> sender_; |
- scoped_refptr<base::TestSimpleTaskRunner> io_thread_runner_; |
- scoped_refptr<OtherThreadTestSimpleTaskRunner> main_thread_runner_; |
+ |
+ // Thread runners. |
+ scoped_refptr<base::TestSimpleTaskRunner> io_thread_runner_ = |
+ new TestSimpleTaskRunner(); |
+ scoped_refptr<base::TestSimpleTaskRunner> file_thread_runner_ = |
+ new TestSimpleTaskRunner(); |
+ scoped_refptr<OtherThreadTestSimpleTaskRunner> main_thread_runner_ = |
+ new OtherThreadTestSimpleTaskRunner(); |
+ |
+ // We set this to the IO thread runner, as this is used for the |
+ // OnMemoryRequest calls, which are on that thread. |
+ base::ThreadTaskRunnerHandle thread_runner_handle_; |
}; |
TEST_F(BlobTransportControllerTest, Descriptions) { |
@@ -136,7 +211,7 @@ TEST_F(BlobTransportControllerTest, Descriptions) { |
const size_t kShortcutSize = 11; |
// The first two data elements should be combined and the data shortcut. |
- std::unique_ptr<BlobConsolidation> consolidation(new BlobConsolidation()); |
+ scoped_refptr<BlobConsolidation> consolidation(new BlobConsolidation()); |
consolidation->AddBlobItem(KRefBlobUUID, 10, 10); |
consolidation->AddDataItem(CreateData("Hello")); |
consolidation->AddDataItem(CreateData("Hello2")); |
@@ -156,14 +231,13 @@ TEST_F(BlobTransportControllerTest, Descriptions) { |
} |
TEST_F(BlobTransportControllerTest, Responses) { |
- using ResponsesStatus = BlobTransportController::ResponsesStatus; |
const std::string kBlobUUID = "uuid"; |
const std::string KRefBlobUUID = "refuuid"; |
const std::string kBadBlobUUID = "uuuidBad"; |
BlobTransportController* holder = BlobTransportController::GetInstance(); |
// The first two data elements should be combined. |
- BlobConsolidation* consolidation = new BlobConsolidation(); |
+ scoped_refptr<BlobConsolidation> consolidation = new BlobConsolidation(); |
consolidation->AddBlobItem(KRefBlobUUID, 10, 10); |
consolidation->AddDataItem(CreateData("Hello")); |
consolidation->AddDataItem(CreateData("Hello2")); |
@@ -171,57 +245,44 @@ TEST_F(BlobTransportControllerTest, Responses) { |
consolidation->AddDataItem(CreateData("Hello3")); |
// See the above test for the expected descriptions layout. |
- holder->blob_storage_[kBlobUUID] = base::WrapUnique(consolidation); |
+ holder->blob_storage_[kBlobUUID] = consolidation; |
std::vector<BlobItemBytesRequest> requests; |
std::vector<base::SharedMemoryHandle> memory_handles; |
std::vector<IPC::PlatformFileForTransit> file_handles; |
- std::vector<storage::BlobItemBytesResponse> output; |
// Request for all of first data |
requests.push_back(BlobItemBytesRequest::CreateIPCRequest(0, 1, 0, 11)); |
- EXPECT_EQ(ResponsesStatus::SUCCESS, |
- holder->GetResponses(kBlobUUID, requests, &memory_handles, |
- file_handles, &output)); |
- EXPECT_EQ(1u, output.size()); |
+ OnMemoryRequest(holder, kBlobUUID, requests, &memory_handles, file_handles); |
std::vector<storage::BlobItemBytesResponse> expected; |
expected.push_back(ResponseWithData(0, "HelloHello2")); |
- EXPECT_EQ(expected, output); |
+ ExpectMemoryResponses(kBlobUUID, expected); |
// Part of second data |
- output.clear(); |
requests[0] = BlobItemBytesRequest::CreateIPCRequest(1000, 3, 1, 5); |
- EXPECT_EQ(ResponsesStatus::SUCCESS, |
- holder->GetResponses(kBlobUUID, requests, &memory_handles, |
- file_handles, &output)); |
- EXPECT_EQ(1u, output.size()); |
+ OnMemoryRequest(holder, kBlobUUID, requests, &memory_handles, file_handles); |
expected.clear(); |
expected.push_back(ResponseWithData(1000, "ello3")); |
- EXPECT_EQ(expected, output); |
+ ExpectMemoryResponses(kBlobUUID, expected); |
// Both data segments |
- output.clear(); |
requests[0] = BlobItemBytesRequest::CreateIPCRequest(0, 1, 0, 11); |
requests.push_back(BlobItemBytesRequest::CreateIPCRequest(1, 3, 0, 6)); |
- EXPECT_EQ(ResponsesStatus::SUCCESS, |
- holder->GetResponses(kBlobUUID, requests, &memory_handles, |
- file_handles, &output)); |
- EXPECT_EQ(2u, output.size()); |
+ OnMemoryRequest(holder, kBlobUUID, requests, &memory_handles, file_handles); |
expected.clear(); |
expected.push_back(ResponseWithData(0, "HelloHello2")); |
expected.push_back(ResponseWithData(1, "Hello3")); |
- EXPECT_EQ(expected, output); |
+ ExpectMemoryResponses(kBlobUUID, expected); |
} |
TEST_F(BlobTransportControllerTest, SharedMemory) { |
- using ResponsesStatus = BlobTransportController::ResponsesStatus; |
const std::string kBlobUUID = "uuid"; |
const std::string KRefBlobUUID = "refuuid"; |
const std::string kBadBlobUUID = "uuuidBad"; |
BlobTransportController* holder = BlobTransportController::GetInstance(); |
// The first two data elements should be combined. |
- BlobConsolidation* consolidation = new BlobConsolidation(); |
+ scoped_refptr<BlobConsolidation> consolidation = new BlobConsolidation(); |
consolidation->AddBlobItem(KRefBlobUUID, 10, 10); |
consolidation->AddDataItem(CreateData("Hello")); |
consolidation->AddDataItem(CreateData("Hello2")); |
@@ -229,12 +290,11 @@ TEST_F(BlobTransportControllerTest, SharedMemory) { |
consolidation->AddDataItem(CreateData("Hello3")); |
// See the above test for the expected descriptions layout. |
- holder->blob_storage_[kBlobUUID] = base::WrapUnique(consolidation); |
+ holder->blob_storage_[kBlobUUID] = consolidation; |
std::vector<BlobItemBytesRequest> requests; |
std::vector<base::SharedMemoryHandle> memory_handles; |
std::vector<IPC::PlatformFileForTransit> file_handles; |
- std::vector<storage::BlobItemBytesResponse> output; |
// Request for all data in shared memory |
requests.push_back( |
@@ -248,14 +308,11 @@ TEST_F(BlobTransportControllerTest, SharedMemory) { |
CHECK(base::SharedMemory::NULLHandle() != handle); |
memory_handles.push_back(handle); |
- EXPECT_EQ(ResponsesStatus::SUCCESS, |
- holder->GetResponses(kBlobUUID, requests, &memory_handles, |
- file_handles, &output)); |
- EXPECT_EQ(2u, output.size()); |
+ OnMemoryRequest(holder, kBlobUUID, requests, &memory_handles, file_handles); |
std::vector<storage::BlobItemBytesResponse> expected; |
expected.push_back(BlobItemBytesResponse(0)); |
expected.push_back(BlobItemBytesResponse(1)); |
- EXPECT_EQ(expected, output); |
+ ExpectMemoryResponses(kBlobUUID, expected); |
std::string expected_memory = "HelloHello2Hello3"; |
const char* mem_location = static_cast<const char*>(memory.memory()); |
std::vector<char> value(mem_location, mem_location + memory.requested_size()); |
@@ -263,19 +320,90 @@ TEST_F(BlobTransportControllerTest, SharedMemory) { |
expected_memory.size())); |
} |
-TEST_F(BlobTransportControllerTest, TestPublicMethods) { |
+TEST_F(BlobTransportControllerTest, Disk) { |
+ const std::string kBlobUUID = "uuid"; |
+ const std::string kRefBlobUUID = "refuuid"; |
+ const std::string kBadBlobUUID = "uuuidBad"; |
+ const std::string kDataPart1 = "Hello"; |
+ const std::string kDataPart2 = "Hello2"; |
+ const std::string kDataPart3 = "Hello3"; |
+ const std::string kData = "HelloHello2Hello3"; |
+ BlobTransportController* holder = BlobTransportController::GetInstance(); |
+ FilePath path1 = CreateTemporaryFile(); |
+ File file(path1, File::FLAG_OPEN | File::FLAG_WRITE | File::FLAG_READ); |
+ ASSERT_TRUE(file.IsValid()); |
+ ASSERT_TRUE(file.SetLength(11 + 6)); |
+ // The first two data elements should be combined. |
+ scoped_refptr<BlobConsolidation> consolidation(new BlobConsolidation()); |
+ consolidation->AddBlobItem(kRefBlobUUID, 10, 10); |
+ consolidation->AddDataItem(CreateData(kDataPart1)); |
+ consolidation->AddDataItem(CreateData(kDataPart2)); |
+ consolidation->AddBlobItem(kRefBlobUUID, 0, 10); |
+ consolidation->AddDataItem(CreateData(kDataPart3)); |
+ // See the above test for the expected descriptions layout. |
+ holder->blob_storage_[kBlobUUID] = consolidation; |
+ holder->main_thread_runner_ = main_thread_runner_; |
+ std::vector<BlobItemBytesRequest> requests; |
+ std::vector<base::SharedMemoryHandle> memory_handles; |
+ std::vector<IPC::PlatformFileForTransit> file_handles; |
+ // Request for all data in files. |
+ requests.push_back( |
+ BlobItemBytesRequest::CreateFileRequest(0, 1, 0, 11, 0, 0)); |
+ requests.push_back( |
+ BlobItemBytesRequest::CreateFileRequest(1, 3, 0, 6, 0, 11)); |
+ file_handles.push_back(IPC::TakePlatformFileForTransit(std::move(file))); |
+ OnMemoryRequest(holder, kBlobUUID, requests, &memory_handles, file_handles); |
+ file_handles.clear(); |
+ EXPECT_TRUE(file_thread_runner_->HasPendingTask()); |
+ EXPECT_FALSE(io_thread_runner_->HasPendingTask()); |
+ file_thread_runner_->RunPendingTasks(); |
+ EXPECT_FALSE(file_thread_runner_->HasPendingTask()); |
+ EXPECT_TRUE(io_thread_runner_->HasPendingTask()); |
+ io_thread_runner_->RunPendingTasks(); |
+ std::vector<storage::BlobItemBytesResponse> expected = { |
+ BlobItemBytesResponse(0), BlobItemBytesResponse(1)}; |
+ ExpectMemoryResponses(kBlobUUID, expected); |
+ file = File(path1, File::FLAG_OPEN | File::FLAG_READ); |
+ char data[11 + 6]; |
+ file.Read(0, data, 11 + 6); |
+ std::vector<char> value(data, data + 11 + 6); |
+ EXPECT_THAT(value, testing::ElementsAreArray(kData.c_str(), kData.size())); |
+ |
+ // Finally, test that we get errors correctly. |
+ FilePath path2 = CreateTemporaryFile(); |
+ EXPECT_TRUE(base::MakeFileUnwritable(path2)); |
+ File file2(path2, File::FLAG_OPEN | File::FLAG_WRITE); |
+ EXPECT_FALSE(file2.IsValid()); |
+ file_handles.push_back(IPC::TakePlatformFileForTransit(std::move(file2))); |
+ OnMemoryRequest(holder, kBlobUUID, requests, &memory_handles, file_handles); |
+ EXPECT_TRUE(file_thread_runner_->HasPendingTask()); |
+ file_thread_runner_->RunPendingTasks(); |
+ EXPECT_TRUE(io_thread_runner_->HasPendingTask()); |
+ io_thread_runner_->RunPendingTasks(); |
+ // Clear the main thread task, as it has the AddRef job. |
+ EXPECT_TRUE(main_thread_runner_->HasPendingTask()); |
+ main_thread_runner_->ClearPendingTasks(); |
+ ExpectCancel(kBlobUUID, IPCBlobCreationCancelCode::FILE_WRITE_FAILED); |
+} |
+ |
+TEST_F(BlobTransportControllerTest, PublicMethods) { |
const std::string kBlobUUID = "uuid"; |
const std::string kBlobContentType = "content_type"; |
const std::string kBlob2UUID = "uuid2"; |
const std::string kBlob2ContentType = "content_type2"; |
const std::string KRefBlobUUID = "refuuid"; |
+ const std::string kDataPart1 = "Hello"; |
+ const std::string kDataPart2 = "Hello2"; |
+ const std::string kDataPart3 = "Hello3"; |
+ const std::string kData = "HelloHello2Hello3"; |
std::vector<DataElement> message_descriptions; |
BlobTransportController* holder = BlobTransportController::GetInstance(); |
- BlobConsolidation* consolidation = new BlobConsolidation(); |
+ // Here we test that the |
+ scoped_refptr<BlobConsolidation> consolidation = new BlobConsolidation(); |
consolidation->AddBlobItem(KRefBlobUUID, 10, 10); |
BlobTransportController::InitiateBlobTransfer( |
- kBlobUUID, kBlobContentType, base::WrapUnique(consolidation), sender_, |
+ kBlobUUID, kBlobContentType, consolidation, sender_, |
io_thread_runner_.get(), main_thread_runner_); |
// Check that we have the 'increase ref' pending task. |
EXPECT_TRUE(main_thread_runner_->HasPendingTask()); |
@@ -290,11 +418,9 @@ TEST_F(BlobTransportControllerTest, TestPublicMethods) { |
EXPECT_FALSE(holder->IsTransporting(kBlobUUID)); |
io_thread_runner_->RunPendingTasks(); |
EXPECT_TRUE(holder->IsTransporting(kBlobUUID)); |
- base::Tuple<std::string, std::vector<DataElement>> message_contents; |
EXPECT_EQ(MakeBlobElement(KRefBlobUUID, 10, 10), message_descriptions[0]); |
- holder->OnCancel(kBlobUUID, |
- storage::IPCBlobCreationCancelCode::OUT_OF_MEMORY); |
+ holder->OnCancel(kBlobUUID, IPCBlobCreationCancelCode::OUT_OF_MEMORY); |
EXPECT_FALSE(holder->IsTransporting(kBlobUUID)); |
// Check we have the 'decrease ref' task. |
EXPECT_TRUE(main_thread_runner_->HasPendingTask()); |
@@ -302,17 +428,48 @@ TEST_F(BlobTransportControllerTest, TestPublicMethods) { |
sink_.ClearMessages(); |
// Add the second. |
- BlobConsolidation* consolidation2 = new BlobConsolidation(); |
+ scoped_refptr<BlobConsolidation> consolidation2 = new BlobConsolidation(); |
consolidation2->AddBlobItem(KRefBlobUUID, 10, 10); |
+ // These items should be combined. |
+ consolidation2->AddDataItem(CreateData(kDataPart1)); |
+ consolidation2->AddDataItem(CreateData(kDataPart2)); |
+ consolidation2->AddDataItem(CreateData(kDataPart3)); |
BlobTransportController::InitiateBlobTransfer( |
- kBlob2UUID, kBlob2ContentType, base::WrapUnique(consolidation2), sender_, |
+ kBlob2UUID, kBlob2ContentType, consolidation2, sender_, |
io_thread_runner_.get(), main_thread_runner_); |
+ // Check that we have the 'increase ref' pending task. |
EXPECT_TRUE(main_thread_runner_->HasPendingTask()); |
+ // Check that we have the 'store' pending task. |
+ EXPECT_TRUE(io_thread_runner_->HasPendingTask()); |
+ // Check that we've sent the data. |
+ message_descriptions.clear(); |
+ ExpectRegisterAndStartMessage(kBlob2UUID, kBlob2ContentType, |
+ &message_descriptions); |
main_thread_runner_->ClearPendingTasks(); |
- sink_.ClearMessages(); |
+ // Check that we got the correct start message. |
+ EXPECT_FALSE(holder->IsTransporting(kBlob2UUID)); |
io_thread_runner_->RunPendingTasks(); |
EXPECT_TRUE(holder->IsTransporting(kBlob2UUID)); |
+ ASSERT_EQ(2u, message_descriptions.size()); |
+ EXPECT_EQ(MakeBlobElement(KRefBlobUUID, 10, 10), message_descriptions[0]); |
+ EXPECT_EQ(MakeDataElement(kData), message_descriptions[1]); |
+ EXPECT_FALSE(main_thread_runner_->HasPendingTask()); |
+ |
+ // Now we request the memory. |
+ std::vector<BlobItemBytesRequest> requests; |
+ std::vector<base::SharedMemoryHandle> memory_handles; |
+ std::vector<IPC::PlatformFileForTransit> file_handles; |
+ requests.push_back( |
+ BlobItemBytesRequest::CreateIPCRequest(0, 1, 0, kData.size())); |
+ OnMemoryRequest(holder, kBlob2UUID, requests, &memory_handles, file_handles); |
+ |
+ std::vector<BlobItemBytesResponse> expected_responses; |
+ BlobItemBytesResponse expected(0); |
+ expected.inline_data = std::vector<char>(kData.begin(), kData.end()); |
+ expected_responses.push_back(expected); |
+ ExpectMemoryResponses(kBlob2UUID, expected_responses); |
+ EXPECT_FALSE(main_thread_runner_->HasPendingTask()); |
// Finish the second one. |
holder->OnDone(kBlob2UUID); |
@@ -321,28 +478,4 @@ TEST_F(BlobTransportControllerTest, TestPublicMethods) { |
main_thread_runner_->ClearPendingTasks(); |
} |
-TEST_F(BlobTransportControllerTest, ResponsesErrors) { |
- using ResponsesStatus = BlobTransportController::ResponsesStatus; |
- const std::string kBlobUUID = "uuid"; |
- const std::string KRefBlobUUID = "refuuid"; |
- const std::string kBadBlobUUID = "uuuidBad"; |
- BlobTransportController* holder = BlobTransportController::GetInstance(); |
- |
- BlobConsolidation* consolidation = new BlobConsolidation(); |
- consolidation->AddBlobItem(KRefBlobUUID, 10, 10); |
- |
- holder->blob_storage_[kBlobUUID] = base::WrapUnique(consolidation); |
- |
- std::vector<BlobItemBytesRequest> requests; |
- std::vector<base::SharedMemoryHandle> memory_handles; |
- std::vector<IPC::PlatformFileForTransit> file_handles; |
- std::vector<storage::BlobItemBytesResponse> output; |
- |
- // Error conditions |
- EXPECT_EQ(ResponsesStatus::BLOB_NOT_FOUND, |
- holder->GetResponses(kBadBlobUUID, requests, &memory_handles, |
- file_handles, &output)); |
- EXPECT_EQ(0u, output.size()); |
-} |
- |
} // namespace content |