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 "content/child/blob_storage/blob_transport_controller.h" | 5 #include "content/child/blob_storage/blob_transport_controller.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
| 11 #include "base/test/test_simple_task_runner.h" |
| 12 #include "base/tuple.h" |
11 #include "content/child/blob_storage/blob_consolidation.h" | 13 #include "content/child/blob_storage/blob_consolidation.h" |
| 14 #include "content/common/fileapi/webblob_messages.h" |
| 15 #include "ipc/ipc_sender.h" |
| 16 #include "ipc/ipc_test_sink.h" |
12 #include "storage/common/blob_storage/blob_item_bytes_request.h" | 17 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
13 #include "storage/common/blob_storage/blob_item_bytes_response.h" | 18 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
16 | 21 |
17 using storage::BlobItemBytesRequest; | 22 using storage::BlobItemBytesRequest; |
18 using storage::BlobItemBytesResponse; | 23 using storage::BlobItemBytesResponse; |
19 using storage::DataElement; | 24 using storage::DataElement; |
20 | 25 |
21 namespace content { | 26 namespace content { |
(...skipping 27 matching lines...) Expand all Loading... |
49 } | 54 } |
50 | 55 |
51 static blink::WebThreadSafeData CreateData(const std::string& str) { | 56 static blink::WebThreadSafeData CreateData(const std::string& str) { |
52 return blink::WebThreadSafeData(str.c_str(), str.size()); | 57 return blink::WebThreadSafeData(str.c_str(), str.size()); |
53 } | 58 } |
54 | 59 |
55 } // namespace | 60 } // namespace |
56 | 61 |
57 class BlobTransportControllerTest : public testing::Test { | 62 class BlobTransportControllerTest : public testing::Test { |
58 public: | 63 public: |
59 void SetUp() override { BlobTransportController::GetInstance()->Clear(); } | 64 BlobTransportControllerTest() |
| 65 : main_thread_runner_(new base::TestSimpleTaskRunner()) {} |
60 | 66 |
61 void TearDown() override { BlobTransportController::GetInstance()->Clear(); } | 67 void SetUp() override { |
| 68 BlobTransportController::GetInstance()->ClearForTesting(); |
| 69 } |
| 70 |
| 71 void TearDown() override { |
| 72 BlobTransportController::GetInstance()->ClearForTesting(); |
| 73 } |
| 74 |
| 75 scoped_refptr<base::TestSimpleTaskRunner> main_thread_runner_; |
62 }; | 76 }; |
63 | 77 |
64 TEST_F(BlobTransportControllerTest, Descriptions) { | 78 TEST_F(BlobTransportControllerTest, Descriptions) { |
65 const std::string kBlobUUID = "uuid"; | 79 const std::string kBlobUUID = "uuid"; |
66 const std::string KRefBlobUUID = "refuuid"; | 80 const std::string KRefBlobUUID = "refuuid"; |
67 const std::string kBadBlobUUID = "uuuidBad"; | 81 const std::string kBadBlobUUID = "uuuidBad"; |
68 const size_t kShortcutSize = 11; | 82 const size_t kShortcutSize = 11; |
69 BlobTransportController* holder = BlobTransportController::GetInstance(); | 83 BlobTransportController* holder = BlobTransportController::GetInstance(); |
70 | 84 |
71 // The first two data elements should be combined and the data shortcut. | 85 // The first two data elements should be combined and the data shortcut. |
(...skipping 24 matching lines...) Expand all Loading... |
96 | 110 |
97 // The first two data elements should be combined. | 111 // The first two data elements should be combined. |
98 BlobConsolidation* consolidation = new BlobConsolidation(); | 112 BlobConsolidation* consolidation = new BlobConsolidation(); |
99 consolidation->AddBlobItem(KRefBlobUUID, 10, 10); | 113 consolidation->AddBlobItem(KRefBlobUUID, 10, 10); |
100 consolidation->AddDataItem(CreateData("Hello")); | 114 consolidation->AddDataItem(CreateData("Hello")); |
101 consolidation->AddDataItem(CreateData("Hello2")); | 115 consolidation->AddDataItem(CreateData("Hello2")); |
102 consolidation->AddBlobItem(KRefBlobUUID, 0, 10); | 116 consolidation->AddBlobItem(KRefBlobUUID, 0, 10); |
103 consolidation->AddDataItem(CreateData("Hello3")); | 117 consolidation->AddDataItem(CreateData("Hello3")); |
104 // See the above test for the expected descriptions layout. | 118 // See the above test for the expected descriptions layout. |
105 | 119 |
106 holder->blob_storage_.insert( | 120 holder->blob_storage_[kBlobUUID] = make_scoped_ptr(consolidation); |
107 std::make_pair(kBlobUUID, make_scoped_ptr(consolidation))); | |
108 | 121 |
109 std::vector<BlobItemBytesRequest> requests; | 122 std::vector<BlobItemBytesRequest> requests; |
110 std::vector<base::SharedMemoryHandle> memory_handles; | 123 std::vector<base::SharedMemoryHandle> memory_handles; |
111 std::vector<IPC::PlatformFileForTransit> file_handles; | 124 std::vector<IPC::PlatformFileForTransit> file_handles; |
112 std::vector<storage::BlobItemBytesResponse> output; | 125 std::vector<storage::BlobItemBytesResponse> output; |
113 | 126 |
114 // Request for all of first data | 127 // Request for all of first data |
115 requests.push_back(BlobItemBytesRequest::CreateIPCRequest(0, 1, 0, 11)); | 128 requests.push_back(BlobItemBytesRequest::CreateIPCRequest(0, 1, 0, 11)); |
116 EXPECT_EQ(ResponsesStatus::SUCCESS, | 129 EXPECT_EQ(ResponsesStatus::SUCCESS, |
117 holder->GetResponses(kBlobUUID, requests, &memory_handles, | 130 holder->GetResponses(kBlobUUID, requests, &memory_handles, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 168 |
156 // The first two data elements should be combined. | 169 // The first two data elements should be combined. |
157 BlobConsolidation* consolidation = new BlobConsolidation(); | 170 BlobConsolidation* consolidation = new BlobConsolidation(); |
158 consolidation->AddBlobItem(KRefBlobUUID, 10, 10); | 171 consolidation->AddBlobItem(KRefBlobUUID, 10, 10); |
159 consolidation->AddDataItem(CreateData("Hello")); | 172 consolidation->AddDataItem(CreateData("Hello")); |
160 consolidation->AddDataItem(CreateData("Hello2")); | 173 consolidation->AddDataItem(CreateData("Hello2")); |
161 consolidation->AddBlobItem(KRefBlobUUID, 0, 10); | 174 consolidation->AddBlobItem(KRefBlobUUID, 0, 10); |
162 consolidation->AddDataItem(CreateData("Hello3")); | 175 consolidation->AddDataItem(CreateData("Hello3")); |
163 // See the above test for the expected descriptions layout. | 176 // See the above test for the expected descriptions layout. |
164 | 177 |
165 holder->blob_storage_.insert( | 178 holder->blob_storage_[kBlobUUID] = make_scoped_ptr(consolidation); |
166 std::make_pair(kBlobUUID, make_scoped_ptr(consolidation))); | |
167 | 179 |
168 std::vector<BlobItemBytesRequest> requests; | 180 std::vector<BlobItemBytesRequest> requests; |
169 std::vector<base::SharedMemoryHandle> memory_handles; | 181 std::vector<base::SharedMemoryHandle> memory_handles; |
170 std::vector<IPC::PlatformFileForTransit> file_handles; | 182 std::vector<IPC::PlatformFileForTransit> file_handles; |
171 std::vector<storage::BlobItemBytesResponse> output; | 183 std::vector<storage::BlobItemBytesResponse> output; |
172 | 184 |
173 // Request for all data in shared memory | 185 // Request for all data in shared memory |
174 requests.push_back( | 186 requests.push_back( |
175 BlobItemBytesRequest::CreateSharedMemoryRequest(0, 1, 0, 11, 0, 0)); | 187 BlobItemBytesRequest::CreateSharedMemoryRequest(0, 1, 0, 11, 0, 0)); |
176 requests.push_back( | 188 requests.push_back( |
(...skipping 15 matching lines...) Expand all Loading... |
192 EXPECT_EQ(expected, output); | 204 EXPECT_EQ(expected, output); |
193 std::string expected_memory = "HelloHello2Hello3"; | 205 std::string expected_memory = "HelloHello2Hello3"; |
194 const char* mem_location = static_cast<const char*>(memory.memory()); | 206 const char* mem_location = static_cast<const char*>(memory.memory()); |
195 std::vector<char> value(mem_location, mem_location + memory.requested_size()); | 207 std::vector<char> value(mem_location, mem_location + memory.requested_size()); |
196 EXPECT_THAT(value, testing::ElementsAreArray(expected_memory.c_str(), | 208 EXPECT_THAT(value, testing::ElementsAreArray(expected_memory.c_str(), |
197 expected_memory.size())); | 209 expected_memory.size())); |
198 } | 210 } |
199 | 211 |
200 TEST_F(BlobTransportControllerTest, TestPublicMethods) { | 212 TEST_F(BlobTransportControllerTest, TestPublicMethods) { |
201 const std::string kBlobUUID = "uuid"; | 213 const std::string kBlobUUID = "uuid"; |
| 214 const std::string kBlob2UUID = "uuid2"; |
202 const std::string KRefBlobUUID = "refuuid"; | 215 const std::string KRefBlobUUID = "refuuid"; |
203 const std::string kBadBlobUUID = "uuuidBad"; | |
204 BlobTransportController* holder = BlobTransportController::GetInstance(); | 216 BlobTransportController* holder = BlobTransportController::GetInstance(); |
| 217 IPC::TestSink sink; |
205 | 218 |
206 BlobConsolidation* consolidation = new BlobConsolidation(); | 219 BlobConsolidation* consolidation = new BlobConsolidation(); |
207 consolidation->AddBlobItem(KRefBlobUUID, 10, 10); | 220 consolidation->AddBlobItem(KRefBlobUUID, 10, 10); |
208 holder->InitiateBlobTransfer(kBlobUUID, "", make_scoped_ptr(consolidation), | 221 holder->InitiateBlobTransfer(kBlobUUID, make_scoped_ptr(consolidation), &sink, |
209 nullptr); | 222 main_thread_runner_); |
| 223 // Check that we have the 'increase ref' pending task. |
| 224 EXPECT_TRUE(main_thread_runner_->HasPendingTask()); |
| 225 main_thread_runner_->ClearPendingTasks(); |
| 226 |
| 227 // Check that we got the correct start message. |
| 228 const IPC::Message* message = |
| 229 sink.GetUniqueMessageMatching(BlobStorageMsg_StartBuildingBlob::ID); |
| 230 ASSERT_TRUE(message); |
| 231 std::vector<DataElement> expected; |
| 232 expected.push_back(MakeBlobElement(KRefBlobUUID, 10, 10)); |
| 233 base::Tuple<std::string, std::vector<DataElement>> message_contents; |
| 234 BlobStorageMsg_StartBuildingBlob::Read(message, &message_contents); |
| 235 EXPECT_EQ(kBlobUUID, base::get<0>(message_contents)); |
| 236 EXPECT_EQ(MakeBlobElement(KRefBlobUUID, 10, 10), |
| 237 base::get<1>(message_contents)[0]); |
| 238 |
210 holder->OnCancel(kBlobUUID, | 239 holder->OnCancel(kBlobUUID, |
211 storage::IPCBlobCreationCancelCode::OUT_OF_MEMORY); | 240 storage::IPCBlobCreationCancelCode::OUT_OF_MEMORY); |
212 EXPECT_FALSE(holder->IsTransporting(kBlobUUID)); | 241 EXPECT_FALSE(holder->IsTransporting(kBlobUUID)); |
| 242 // Check we have the 'decrease ref' task. |
| 243 EXPECT_TRUE(main_thread_runner_->HasPendingTask()); |
| 244 main_thread_runner_->ClearPendingTasks(); |
| 245 message = nullptr; |
| 246 sink.ClearMessages(); |
213 | 247 |
| 248 // Check that we can complete the blob, and check that we only update the |
| 249 // child process ref on the first and last transfer. |
214 consolidation = new BlobConsolidation(); | 250 consolidation = new BlobConsolidation(); |
215 consolidation->AddBlobItem(KRefBlobUUID, 10, 10); | 251 consolidation->AddBlobItem(KRefBlobUUID, 10, 10); |
216 holder->InitiateBlobTransfer(kBlobUUID, "", make_scoped_ptr(consolidation), | 252 holder->InitiateBlobTransfer(kBlobUUID, make_scoped_ptr(consolidation), &sink, |
217 nullptr); | 253 main_thread_runner_); |
| 254 EXPECT_TRUE(main_thread_runner_->HasPendingTask()); |
| 255 main_thread_runner_->ClearPendingTasks(); |
| 256 EXPECT_TRUE( |
| 257 sink.GetUniqueMessageMatching(BlobStorageMsg_StartBuildingBlob::ID)); |
| 258 sink.ClearMessages(); |
| 259 |
| 260 // Add the second and check that we don't have a task. |
| 261 BlobConsolidation* consolidation2 = new BlobConsolidation(); |
| 262 consolidation2->AddBlobItem(KRefBlobUUID, 10, 10); |
| 263 holder->InitiateBlobTransfer(kBlob2UUID, make_scoped_ptr(consolidation2), |
| 264 &sink, main_thread_runner_); |
| 265 EXPECT_FALSE(main_thread_runner_->HasPendingTask()); |
| 266 sink.ClearMessages(); |
| 267 |
| 268 // Finish the first one. |
218 holder->OnDone(kBlobUUID); | 269 holder->OnDone(kBlobUUID); |
219 EXPECT_FALSE(holder->IsTransporting(kBlobUUID)); | 270 EXPECT_FALSE(holder->IsTransporting(kBlobUUID)); |
| 271 EXPECT_FALSE(main_thread_runner_->HasPendingTask()); |
| 272 |
| 273 // Finish the second one, and verify that we have the task. |
| 274 holder->OnDone(kBlob2UUID); |
| 275 EXPECT_FALSE(holder->IsTransporting(kBlob2UUID)); |
| 276 EXPECT_TRUE(main_thread_runner_->HasPendingTask()); |
| 277 main_thread_runner_->ClearPendingTasks(); |
220 } | 278 } |
221 | 279 |
222 TEST_F(BlobTransportControllerTest, ResponsesErrors) { | 280 TEST_F(BlobTransportControllerTest, ResponsesErrors) { |
223 using ResponsesStatus = BlobTransportController::ResponsesStatus; | 281 using ResponsesStatus = BlobTransportController::ResponsesStatus; |
224 const std::string kBlobUUID = "uuid"; | 282 const std::string kBlobUUID = "uuid"; |
225 const std::string KRefBlobUUID = "refuuid"; | 283 const std::string KRefBlobUUID = "refuuid"; |
226 const std::string kBadBlobUUID = "uuuidBad"; | 284 const std::string kBadBlobUUID = "uuuidBad"; |
227 BlobTransportController* holder = BlobTransportController::GetInstance(); | 285 BlobTransportController* holder = BlobTransportController::GetInstance(); |
228 | 286 |
229 BlobConsolidation* consolidation = new BlobConsolidation(); | 287 BlobConsolidation* consolidation = new BlobConsolidation(); |
230 consolidation->AddBlobItem(KRefBlobUUID, 10, 10); | 288 consolidation->AddBlobItem(KRefBlobUUID, 10, 10); |
231 | 289 |
232 holder->blob_storage_.insert( | 290 holder->blob_storage_[kBlobUUID] = make_scoped_ptr(consolidation); |
233 std::make_pair(kBlobUUID, make_scoped_ptr(consolidation))); | |
234 | 291 |
235 std::vector<BlobItemBytesRequest> requests; | 292 std::vector<BlobItemBytesRequest> requests; |
236 std::vector<base::SharedMemoryHandle> memory_handles; | 293 std::vector<base::SharedMemoryHandle> memory_handles; |
237 std::vector<IPC::PlatformFileForTransit> file_handles; | 294 std::vector<IPC::PlatformFileForTransit> file_handles; |
238 std::vector<storage::BlobItemBytesResponse> output; | 295 std::vector<storage::BlobItemBytesResponse> output; |
239 | 296 |
240 // Error conditions | 297 // Error conditions |
241 EXPECT_EQ(ResponsesStatus::BLOB_NOT_FOUND, | 298 EXPECT_EQ(ResponsesStatus::BLOB_NOT_FOUND, |
242 holder->GetResponses(kBadBlobUUID, requests, &memory_handles, | 299 holder->GetResponses(kBadBlobUUID, requests, &memory_handles, |
243 file_handles, &output)); | 300 file_handles, &output)); |
244 EXPECT_EQ(0u, output.size()); | 301 EXPECT_EQ(0u, output.size()); |
245 } | 302 } |
246 | 303 |
247 } // namespace content | 304 } // namespace content |
OLD | NEW |