| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "storage/browser/blob/blob_storage_context.h" | 5 #include "storage/browser/blob/blob_storage_context.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 10 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 16 #include "base/test/test_simple_task_runner.h" | 14 #include "base/test/test_simple_task_runner.h" |
| 17 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 18 #include "storage/browser/blob/blob_data_builder.h" | 16 #include "storage/browser/blob/blob_data_builder.h" |
| 19 #include "storage/browser/blob/blob_data_handle.h" | 17 #include "storage/browser/blob/blob_data_handle.h" |
| 20 #include "storage/browser/blob/blob_data_item.h" | 18 #include "storage/browser/blob/blob_data_item.h" |
| 21 #include "storage/browser/blob/blob_entry.h" | 19 #include "storage/browser/blob/blob_entry.h" |
| 22 #include "storage/browser/blob/blob_memory_controller.h" | |
| 23 #include "storage/browser/blob/blob_storage_registry.h" | 20 #include "storage/browser/blob/blob_storage_registry.h" |
| 24 #include "storage/browser/blob/shareable_blob_data_item.h" | 21 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 25 #include "storage/common/data_element.h" | 22 #include "storage/common/data_element.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 24 |
| 28 namespace storage { | 25 namespace storage { |
| 29 namespace { | 26 namespace { |
| 30 using base::TestSimpleTaskRunner; | 27 using base::TestSimpleTaskRunner; |
| 31 using FileCreationInfo = BlobMemoryController::FileCreationInfo; | |
| 32 | 28 |
| 33 const char kType[] = "type"; | 29 const char kType[] = "type"; |
| 34 const char kDisposition[] = ""; | 30 const char kDisposition[] = ""; |
| 35 const size_t kTestBlobStorageIPCThresholdBytes = 20; | |
| 36 const size_t kTestBlobStorageMaxSharedMemoryBytes = 50; | |
| 37 | |
| 38 const size_t kTestBlobStorageMaxBlobMemorySize = 400; | |
| 39 const uint64_t kTestBlobStorageMaxDiskSpace = 4000; | |
| 40 const uint64_t kTestBlobStorageMinFileSizeBytes = 10; | |
| 41 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; | |
| 42 | |
| 43 void SaveBlobStatusAndFiles(BlobStatus* status_ptr, | |
| 44 std::vector<FileCreationInfo>* files_ptr, | |
| 45 BlobStatus status, | |
| 46 std::vector<FileCreationInfo> files) { | |
| 47 EXPECT_FALSE(BlobStatusIsError(status)); | |
| 48 *status_ptr = status; | |
| 49 std::move(files.begin(), files.end(), std::back_inserter(*files_ptr)); | |
| 50 } | |
| 51 | 31 |
| 52 } // namespace | 32 } // namespace |
| 53 | 33 |
| 54 class BlobFlattenerTest : public testing::Test { | 34 class BlobFlattenerTest : public testing::Test { |
| 55 protected: | 35 protected: |
| 56 using BlobFlattener = BlobStorageContext::BlobFlattener; | 36 using BlobFlattener = BlobStorageContext::BlobFlattener; |
| 57 | 37 |
| 58 BlobFlattenerTest() | 38 BlobFlattenerTest() |
| 59 : fake_file_path_(base::FilePath(FILE_PATH_LITERAL("kFakePath"))) {} | 39 : fake_file_path_(base::FilePath(FILE_PATH_LITERAL("kFakePath"))) {} |
| 60 ~BlobFlattenerTest() override {} | 40 ~BlobFlattenerTest() override {} |
| 61 | 41 |
| 62 void SetUp() override { | 42 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
| 63 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 64 context_ = base::MakeUnique<BlobStorageContext>(); | |
| 65 } | |
| 66 | 43 |
| 67 void TearDown() override { | 44 void TearDown() override { |
| 68 base::RunLoop().RunUntilIdle(); | 45 base::RunLoop().RunUntilIdle(); |
| 69 file_runner_->RunPendingTasks(); | 46 file_runner_->RunPendingTasks(); |
| 70 base::RunLoop().RunUntilIdle(); | |
| 71 ASSERT_TRUE(temp_dir_.Delete()); | 47 ASSERT_TRUE(temp_dir_.Delete()); |
| 72 } | 48 } |
| 73 | 49 |
| 74 scoped_refptr<BlobDataItem> CreateDataDescriptionItem(size_t size) { | 50 scoped_refptr<BlobDataItem> CreateDataDescriptionItem(size_t size) { |
| 75 std::unique_ptr<DataElement> element(new DataElement()); | 51 std::unique_ptr<DataElement> element(new DataElement()); |
| 76 element->SetToBytesDescription(size); | 52 element->SetToBytesDescription(size); |
| 77 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); | 53 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); |
| 78 }; | 54 }; |
| 79 | 55 |
| 80 scoped_refptr<BlobDataItem> CreateDataItem(const char* memory, size_t size) { | 56 scoped_refptr<BlobDataItem> CreateDataItem(const char* memory, size_t size) { |
| 81 std::unique_ptr<DataElement> element(new DataElement()); | 57 std::unique_ptr<DataElement> element(new DataElement()); |
| 82 element->SetToBytes(memory, size); | 58 element->SetToBytes(memory, size); |
| 83 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); | 59 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); |
| 84 }; | 60 }; |
| 85 | 61 |
| 86 scoped_refptr<BlobDataItem> CreateFileItem(size_t offset, size_t size) { | 62 scoped_refptr<BlobDataItem> CreateFileItem(size_t offset, size_t size) { |
| 87 std::unique_ptr<DataElement> element(new DataElement()); | 63 std::unique_ptr<DataElement> element(new DataElement()); |
| 88 element->SetToFilePathRange(fake_file_path_, offset, size, | 64 element->SetToFilePathRange(fake_file_path_, offset, size, |
| 89 base::Time::Max()); | 65 base::Time::Max()); |
| 90 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); | 66 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); |
| 91 }; | 67 }; |
| 92 | 68 |
| 93 scoped_refptr<BlobDataItem> CreateFutureFileItem(size_t offset, size_t size) { | |
| 94 std::unique_ptr<DataElement> element(new DataElement()); | |
| 95 element->SetToFilePathRange(BlobDataBuilder::GetFutureFileItemPath(0), | |
| 96 offset, size, base::Time()); | |
| 97 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); | |
| 98 }; | |
| 99 | |
| 100 std::unique_ptr<BlobDataHandle> SetupBasicBlob(const std::string& id) { | 69 std::unique_ptr<BlobDataHandle> SetupBasicBlob(const std::string& id) { |
| 101 BlobDataBuilder builder(id); | 70 BlobDataBuilder builder(id); |
| 102 builder.AppendData("1", 1); | 71 builder.AppendData("1", 1); |
| 103 builder.set_content_type("text/plain"); | 72 builder.set_content_type("text/plain"); |
| 104 return context_->AddFinishedBlob(builder); | 73 return context_.AddFinishedBlob(builder); |
| 105 } | 74 } |
| 106 | 75 |
| 107 BlobStorageRegistry* registry() { return context_->mutable_registry(); } | 76 BlobStorageRegistry* registry() { return context_.mutable_registry(); } |
| 108 | 77 |
| 109 const ShareableBlobDataItem& GetItemInBlob(const std::string& uuid, | 78 const ShareableBlobDataItem& GetItemInBlob(const std::string& uuid, |
| 110 size_t index) { | 79 size_t index) { |
| 111 BlobEntry* entry = registry()->GetEntry(uuid); | 80 BlobEntry* entry = registry()->GetEntry(uuid); |
| 112 EXPECT_TRUE(entry); | 81 EXPECT_TRUE(entry); |
| 113 return *entry->items()[index]; | 82 return *entry->items()[index]; |
| 114 } | 83 } |
| 115 | 84 |
| 116 void SetTestMemoryLimits() { | |
| 117 BlobStorageLimits limits; | |
| 118 limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes; | |
| 119 limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes; | |
| 120 limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize; | |
| 121 limits.max_blob_disk_space = kTestBlobStorageMaxDiskSpace; | |
| 122 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; | |
| 123 limits.max_file_size = kTestBlobStorageMaxFileSizeBytes; | |
| 124 context_->mutable_memory_controller()->set_limits_for_testing(limits); | |
| 125 } | |
| 126 | |
| 127 base::FilePath fake_file_path_; | 85 base::FilePath fake_file_path_; |
| 128 base::ScopedTempDir temp_dir_; | 86 base::ScopedTempDir temp_dir_; |
| 129 scoped_refptr<TestSimpleTaskRunner> file_runner_ = new TestSimpleTaskRunner(); | 87 scoped_refptr<TestSimpleTaskRunner> file_runner_ = new TestSimpleTaskRunner(); |
| 130 | 88 |
| 131 base::MessageLoop fake_io_message_loop; | 89 base::MessageLoop fake_io_message_loop; |
| 132 std::unique_ptr<BlobStorageContext> context_; | 90 BlobStorageContext context_; |
| 133 }; | 91 }; |
| 134 | 92 |
| 135 TEST_F(BlobFlattenerTest, NoBlobItems) { | 93 TEST_F(BlobFlattenerTest, NoBlobItems) { |
| 136 const std::string kBlobUUID = "kId"; | 94 const std::string kBlobUUID = "kId"; |
| 137 | 95 |
| 138 BlobDataBuilder builder(kBlobUUID); | 96 BlobDataBuilder builder(kBlobUUID); |
| 139 builder.AppendData("hi", 2u); | 97 builder.AppendData("hi", 2u); |
| 140 builder.AppendFile(fake_file_path_, 0u, 10u, base::Time::Max()); | 98 builder.AppendFile(fake_file_path_, 0u, 10u, base::Time::Max()); |
| 141 BlobEntry output(kType, kDisposition); | 99 BlobEntry output(kType, kDisposition); |
| 142 BlobFlattener flattener(builder, &output, registry()); | 100 BlobFlattener flattener(builder, &output, registry()); |
| 143 | 101 |
| 144 EXPECT_EQ(BlobStatus::PENDING_QUOTA, flattener.status); | 102 EXPECT_EQ(BlobStatus::PENDING_QUOTA, flattener.status); |
| 145 EXPECT_EQ(0u, flattener.dependent_blobs.size()); | 103 EXPECT_EQ(0u, flattener.dependent_blobs.size()); |
| 146 EXPECT_EQ(0u, flattener.copies.size()); | 104 EXPECT_EQ(0u, flattener.copies.size()); |
| 147 EXPECT_EQ(12u, flattener.total_size); | 105 EXPECT_EQ(12u, flattener.total_size); |
| 148 EXPECT_EQ(2u, flattener.transport_quota_needed); | 106 EXPECT_EQ(2u, flattener.memory_quota_needed); |
| 149 | 107 |
| 150 ASSERT_EQ(2u, output.items().size()); | 108 ASSERT_EQ(2u, output.items().size()); |
| 151 EXPECT_EQ(*CreateDataItem("hi", 2u), *output.items()[0]->item()); | 109 EXPECT_EQ(*CreateDataItem("hi", 2u), *output.items()[0]->item()); |
| 152 EXPECT_EQ(*CreateFileItem(0, 10u), *output.items()[1]->item()); | 110 EXPECT_EQ(*CreateFileItem(0, 10u), *output.items()[1]->item()); |
| 153 } | 111 } |
| 154 | 112 |
| 155 TEST_F(BlobFlattenerTest, ErrorCases) { | 113 TEST_F(BlobFlattenerTest, ErrorCases) { |
| 156 const std::string kBlobUUID = "kId"; | 114 const std::string kBlobUUID = "kId"; |
| 157 const std::string kBlob2UUID = "kId2"; | 115 const std::string kBlob2UUID = "kId2"; |
| 158 | 116 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 182 BlobEntry output(kType, kDisposition); | 140 BlobEntry output(kType, kDisposition); |
| 183 BlobFlattener flattener(builder, &output, registry()); | 141 BlobFlattener flattener(builder, &output, registry()); |
| 184 EXPECT_EQ(BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, flattener.status); | 142 EXPECT_EQ(BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, flattener.status); |
| 185 } | 143 } |
| 186 } | 144 } |
| 187 | 145 |
| 188 TEST_F(BlobFlattenerTest, BlobWithSlices) { | 146 TEST_F(BlobFlattenerTest, BlobWithSlices) { |
| 189 const std::string kBlobUUID = "kId"; | 147 const std::string kBlobUUID = "kId"; |
| 190 const std::string kDataBlob = "kId2"; | 148 const std::string kDataBlob = "kId2"; |
| 191 const std::string kFileBlob = "kId3"; | 149 const std::string kFileBlob = "kId3"; |
| 192 const std::string kPendingFileBlob = "kId4"; | |
| 193 | 150 |
| 194 // We have the following: | 151 // We have the following: |
| 195 // * data, | 152 // * data, |
| 196 // * sliced data blob, | 153 // * sliced data blob, |
| 197 // * file | 154 // * file |
| 198 // * full data blob, | 155 // * full data blob, |
| 199 // * pending data, | 156 // * pending data, |
| 200 | 157 |
| 201 context_ = | |
| 202 base::MakeUnique<BlobStorageContext>(temp_dir_.GetPath(), file_runner_); | |
| 203 SetTestMemoryLimits(); | |
| 204 | |
| 205 std::unique_ptr<BlobDataHandle> data_blob; | 158 std::unique_ptr<BlobDataHandle> data_blob; |
| 206 { | 159 { |
| 207 BlobDataBuilder builder(kDataBlob); | 160 BlobDataBuilder builder(kDataBlob); |
| 208 builder.AppendData("12345", 5); | 161 builder.AppendData("12345", 5); |
| 209 builder.set_content_type("text/plain"); | 162 builder.set_content_type("text/plain"); |
| 210 data_blob = context_->AddFinishedBlob(builder); | 163 data_blob = context_.AddFinishedBlob(builder); |
| 211 } | 164 } |
| 212 | 165 |
| 213 std::unique_ptr<BlobDataHandle> file_blob; | 166 std::unique_ptr<BlobDataHandle> file_blob; |
| 214 { | 167 { |
| 215 BlobDataBuilder builder(kFileBlob); | 168 BlobDataBuilder builder(kFileBlob); |
| 216 builder.AppendFile(fake_file_path_, 1u, 10u, base::Time::Max()); | 169 builder.AppendFile(fake_file_path_, 1u, 10u, base::Time::Max()); |
| 217 file_blob = context_->AddFinishedBlob(builder); | 170 file_blob = context_.AddFinishedBlob(builder); |
| 218 } | |
| 219 | |
| 220 BlobStatus file_status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; | |
| 221 std::vector<FileCreationInfo> file_handles; | |
| 222 std::unique_ptr<BlobDataHandle> future_file_blob; | |
| 223 { | |
| 224 BlobDataBuilder builder(kPendingFileBlob); | |
| 225 builder.AppendFutureFile(0u, 2u, 0); | |
| 226 builder.AppendFutureFile(2u, 5u, 0); | |
| 227 future_file_blob = context_->BuildBlob( | |
| 228 builder, | |
| 229 base::Bind(&SaveBlobStatusAndFiles, &file_status, &file_handles)); | |
| 230 } | 171 } |
| 231 | 172 |
| 232 BlobDataBuilder builder(kBlobUUID); | 173 BlobDataBuilder builder(kBlobUUID); |
| 233 builder.AppendData("hi", 2u); | 174 builder.AppendData("hi", 2u); |
| 234 builder.AppendBlob(kDataBlob, 1u, 2u); | 175 builder.AppendBlob(kDataBlob, 1u, 2u); |
| 235 builder.AppendFile(fake_file_path_, 3u, 5u, base::Time::Max()); | 176 builder.AppendFile(fake_file_path_, 3u, 5u, base::Time::Max()); |
| 236 builder.AppendBlob(kDataBlob); | 177 builder.AppendBlob(kDataBlob); |
| 237 builder.AppendBlob(kFileBlob, 1u, 3u); | 178 builder.AppendBlob(kFileBlob, 1u, 3u); |
| 238 builder.AppendFutureData(12u); | 179 builder.AppendFutureData(12u); |
| 239 builder.AppendBlob(kPendingFileBlob, 1u, 3u); | |
| 240 | 180 |
| 241 BlobEntry output(kType, kDisposition); | 181 BlobEntry output(kType, kDisposition); |
| 242 BlobFlattener flattener(builder, &output, registry()); | 182 BlobFlattener flattener(builder, &output, registry()); |
| 243 EXPECT_EQ(BlobStatus::PENDING_QUOTA, flattener.status); | 183 EXPECT_EQ(BlobStatus::PENDING_QUOTA, flattener.status); |
| 244 | 184 |
| 245 EXPECT_EQ(3u, flattener.dependent_blobs.size()); | 185 EXPECT_EQ(2u, flattener.dependent_blobs.size()); |
| 246 EXPECT_EQ(32u, flattener.total_size); | 186 EXPECT_EQ(29u, flattener.total_size); |
| 247 EXPECT_EQ(14u, flattener.transport_quota_needed); | 187 EXPECT_EQ(16u, flattener.memory_quota_needed); |
| 248 EXPECT_EQ(2u, flattener.copy_quota_needed); | |
| 249 | 188 |
| 250 ASSERT_EQ(8u, output.items().size()); | 189 ASSERT_EQ(6u, output.items().size()); |
| 251 EXPECT_EQ(*CreateDataItem("hi", 2u), *output.items()[0]->item()); | 190 EXPECT_EQ(*CreateDataItem("hi", 2u), *output.items()[0]->item()); |
| 252 EXPECT_EQ(*CreateDataDescriptionItem(2u), *output.items()[1]->item()); | 191 EXPECT_EQ(*CreateDataDescriptionItem(2u), *output.items()[1]->item()); |
| 253 EXPECT_EQ(*CreateFileItem(3u, 5u), *output.items()[2]->item()); | 192 EXPECT_EQ(*CreateFileItem(3u, 5u), *output.items()[2]->item()); |
| 254 EXPECT_EQ(GetItemInBlob(kDataBlob, 0), *output.items()[3]); | 193 EXPECT_EQ(GetItemInBlob(kDataBlob, 0), *output.items()[3]); |
| 255 EXPECT_EQ(*CreateFileItem(2u, 3u), *output.items()[4]->item()); | 194 EXPECT_EQ(*CreateFileItem(2u, 3u), *output.items()[4]->item()); |
| 256 EXPECT_EQ(*CreateDataDescriptionItem(12u), *output.items()[5]->item()); | 195 EXPECT_EQ(*CreateDataDescriptionItem(12u), *output.items()[5]->item()); |
| 257 EXPECT_EQ(*CreateFutureFileItem(1u, 1u), *output.items()[6]->item()); | |
| 258 EXPECT_EQ(*CreateFutureFileItem(2u, 2u), *output.items()[7]->item()); | |
| 259 | 196 |
| 260 // We're copying items at index 1, 6, and 7. | 197 // We're copying item at index 1 |
| 261 ASSERT_EQ(3u, flattener.copies.size()); | 198 ASSERT_EQ(1u, flattener.copies.size()); |
| 262 EXPECT_EQ(*flattener.copies[0].dest_item, *output.items()[1]); | 199 EXPECT_EQ(*flattener.copies[0].dest_item, *output.items()[1]); |
| 263 EXPECT_EQ(GetItemInBlob(kDataBlob, 0), *flattener.copies[0].source_item); | 200 EXPECT_EQ(GetItemInBlob(kDataBlob, 0), *flattener.copies[0].source_item); |
| 264 EXPECT_EQ(1u, flattener.copies[0].source_item_offset); | 201 EXPECT_EQ(1u, flattener.copies[0].source_item_offset); |
| 265 EXPECT_EQ(*flattener.copies[1].dest_item, *output.items()[6]); | |
| 266 EXPECT_EQ(GetItemInBlob(kPendingFileBlob, 0), | |
| 267 *flattener.copies[1].source_item); | |
| 268 EXPECT_EQ(1u, flattener.copies[1].source_item_offset); | |
| 269 EXPECT_EQ(*flattener.copies[2].dest_item, *output.items()[7]); | |
| 270 EXPECT_EQ(GetItemInBlob(kPendingFileBlob, 1), | |
| 271 *flattener.copies[2].source_item); | |
| 272 EXPECT_EQ(0u, flattener.copies[2].source_item_offset); | |
| 273 | |
| 274 // Clean up temp files. | |
| 275 EXPECT_TRUE(file_runner_->HasPendingTask()); | |
| 276 file_runner_->RunPendingTasks(); | |
| 277 base::RunLoop().RunUntilIdle(); | |
| 278 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, file_status); | |
| 279 EXPECT_FALSE(file_handles.empty()); | |
| 280 } | 202 } |
| 281 | 203 |
| 282 } // namespace storage | 204 } // namespace storage |
| OLD | NEW |