OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "storage/browser/blob/blob_flattener.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/time/time.h" |
| 13 #include "storage/browser/blob/blob_data_builder.h" |
| 14 #include "storage/browser/blob/blob_data_handle.h" |
| 15 #include "storage/browser/blob/blob_data_item.h" |
| 16 #include "storage/browser/blob/blob_storage_context.h" |
| 17 #include "storage/browser/blob/blob_storage_registry.h" |
| 18 #include "storage/browser/blob/internal_blob_data.h" |
| 19 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 20 #include "storage/common/data_element.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 namespace storage { |
| 24 namespace { |
| 25 |
| 26 scoped_refptr<BlobDataItem> CreateDataDescriptionItem(size_t size) { |
| 27 std::unique_ptr<DataElement> element(new DataElement()); |
| 28 element->SetToBytesDescription(size); |
| 29 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); |
| 30 }; |
| 31 |
| 32 scoped_refptr<BlobDataItem> CreateDataItem(const char* memory, size_t size) { |
| 33 std::unique_ptr<DataElement> element(new DataElement()); |
| 34 element->SetToBytes(memory, size); |
| 35 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); |
| 36 }; |
| 37 |
| 38 scoped_refptr<BlobDataItem> CreateFileItem(size_t offset, size_t size) { |
| 39 std::unique_ptr<DataElement> element(new DataElement()); |
| 40 element->SetToFilePathRange(base::FilePath("kFakePath"), offset, size, |
| 41 base::Time::Max()); |
| 42 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); |
| 43 }; |
| 44 |
| 45 scoped_refptr<BlobDataItem> CreateFutureFileItem(size_t offset, size_t size) { |
| 46 std::unique_ptr<DataElement> element(new DataElement()); |
| 47 element->SetToFilePathRange( |
| 48 base::FilePath(BlobDataBuilder::kAppendFutureFileTemporaryFileName), |
| 49 offset, size, base::Time::Max()); |
| 50 return scoped_refptr<BlobDataItem>(new BlobDataItem(std::move(element))); |
| 51 }; |
| 52 |
| 53 void ExpectNoErrors(BlobFlattener* flattener) { |
| 54 EXPECT_FALSE(flattener->contains_invalid_references); |
| 55 EXPECT_FALSE(flattener->contains_broken_references); |
| 56 } |
| 57 |
| 58 } // namespace |
| 59 |
| 60 class BlobFlattenerTest : public testing::Test { |
| 61 protected: |
| 62 BlobFlattenerTest() {} |
| 63 ~BlobFlattenerTest() override {} |
| 64 |
| 65 void TearDown() override { base::RunLoop().RunUntilIdle(); } |
| 66 |
| 67 std::unique_ptr<BlobDataHandle> SetupBasicBlob(const std::string& id) { |
| 68 BlobDataBuilder builder(id); |
| 69 builder.AppendData("1", 1); |
| 70 builder.set_content_type("text/plain"); |
| 71 return context_.AddFinishedBlob(builder); |
| 72 } |
| 73 |
| 74 BlobStorageRegistry* registry() { return &context_.registry_; } |
| 75 |
| 76 const ShareableBlobDataItem& GetItemInBlob(const std::string& uuid, |
| 77 size_t index) { |
| 78 BlobStorageRegistry::Entry* entry = registry()->GetEntry(uuid); |
| 79 EXPECT_TRUE(entry); |
| 80 return *entry->data.items()[index]; |
| 81 } |
| 82 |
| 83 base::MessageLoop fake_io_message_loop; |
| 84 BlobStorageContext context_; |
| 85 }; |
| 86 |
| 87 TEST_F(BlobFlattenerTest, NoBlobItems) { |
| 88 const std::string kBlobUUID = "kId"; |
| 89 |
| 90 BlobDataBuilder builder(kBlobUUID); |
| 91 builder.AppendData("hi", 2u); |
| 92 builder.AppendFile(base::FilePath("kFakePath"), 0u, 10u, base::Time::Max()); |
| 93 InternalBlobData output; |
| 94 BlobFlattener flattener(builder, &output, registry()); |
| 95 |
| 96 ExpectNoErrors(&flattener); |
| 97 EXPECT_EQ(0u, flattener.dependent_blobs.size()); |
| 98 EXPECT_EQ(0u, flattener.copies.size()); |
| 99 EXPECT_EQ(12u, flattener.total_size.ValueOrDie()); |
| 100 EXPECT_EQ(2u, flattener.memory_needed.ValueOrDie()); |
| 101 |
| 102 ASSERT_EQ(2u, output.items().size()); |
| 103 EXPECT_EQ(*CreateDataItem("hi", 2u), *output.items()[0]->item()); |
| 104 EXPECT_EQ(*CreateFileItem(0, 10u), *output.items()[1]->item()); |
| 105 } |
| 106 |
| 107 TEST_F(BlobFlattenerTest, ErrorCases) { |
| 108 const std::string kBlobUUID = "kId"; |
| 109 const std::string kBlob2UUID = "kId2"; |
| 110 |
| 111 // Invalid blob reference. |
| 112 { |
| 113 BlobDataBuilder builder(kBlobUUID); |
| 114 builder.AppendBlob("doesnotexist"); |
| 115 InternalBlobData output; |
| 116 BlobFlattener flattener(builder, &output, registry()); |
| 117 EXPECT_TRUE(flattener.contains_broken_references); |
| 118 } |
| 119 |
| 120 // Circular reference. |
| 121 { |
| 122 BlobDataBuilder builder(kBlobUUID); |
| 123 builder.AppendBlob(kBlobUUID); |
| 124 InternalBlobData output; |
| 125 BlobFlattener flattener(builder, &output, registry()); |
| 126 EXPECT_TRUE(flattener.contains_broken_references); |
| 127 } |
| 128 |
| 129 // Bad slice. |
| 130 { |
| 131 std::unique_ptr<BlobDataHandle> handle = SetupBasicBlob(kBlob2UUID); |
| 132 BlobDataBuilder builder(kBlobUUID); |
| 133 builder.AppendBlob(kBlob2UUID, 1, 2); |
| 134 InternalBlobData output; |
| 135 BlobFlattener flattener(builder, &output, registry()); |
| 136 EXPECT_TRUE(flattener.contains_invalid_references); |
| 137 } |
| 138 } |
| 139 |
| 140 TEST_F(BlobFlattenerTest, BlobWithSlices) { |
| 141 const std::string kBlobUUID = "kId"; |
| 142 const std::string kDataBlob = "kId2"; |
| 143 const std::string kFileBlob = "kId3"; |
| 144 const std::string kPendingFileBlob = "kId4"; |
| 145 |
| 146 // We have the following: |
| 147 // * data, |
| 148 // * sliced data blob, |
| 149 // * file |
| 150 // * full data blob, |
| 151 // * pending data, |
| 152 // * sliced pending file. |
| 153 |
| 154 std::unique_ptr<BlobDataHandle> data_blob; |
| 155 { |
| 156 BlobDataBuilder builder(kDataBlob); |
| 157 builder.AppendData("12345", 5); |
| 158 builder.set_content_type("text/plain"); |
| 159 data_blob = context_.AddFinishedBlob(builder); |
| 160 } |
| 161 |
| 162 std::unique_ptr<BlobDataHandle> file_blob; |
| 163 { |
| 164 BlobDataBuilder builder(kFileBlob); |
| 165 builder.AppendFile(base::FilePath("kFakePath"), 1u, 10u, base::Time::Max()); |
| 166 file_blob = context_.AddFinishedBlob(builder); |
| 167 } |
| 168 |
| 169 std::unique_ptr<BlobDataHandle> future_file_blob; |
| 170 { |
| 171 BlobDataBuilder builder(kPendingFileBlob); |
| 172 builder.AppendFile( |
| 173 base::FilePath(BlobDataBuilder::kAppendFutureFileTemporaryFileName), 2u, |
| 174 5u, base::Time::Max()); |
| 175 future_file_blob = context_.AddFinishedBlob(builder); |
| 176 } |
| 177 |
| 178 BlobDataBuilder builder(kBlobUUID); |
| 179 builder.AppendData("hi", 2u); |
| 180 builder.AppendBlob(kDataBlob, 1u, 2u); |
| 181 builder.AppendFile(base::FilePath("kFakePath"), 3u, 5u, base::Time::Max()); |
| 182 builder.AppendBlob(kDataBlob); |
| 183 builder.AppendBlob(kFileBlob, 1u, 3u); |
| 184 builder.AppendFutureData(12u); |
| 185 builder.AppendBlob(kPendingFileBlob, 1u, 2u); |
| 186 |
| 187 InternalBlobData output; |
| 188 BlobFlattener flattener(builder, &output, registry()); |
| 189 ExpectNoErrors(&flattener); |
| 190 EXPECT_EQ(true, flattener.contains_pending_content); |
| 191 |
| 192 EXPECT_EQ(3u, flattener.dependent_blobs.size()); |
| 193 EXPECT_EQ(31u, flattener.total_size.ValueOrDie()); |
| 194 EXPECT_EQ(16u, flattener.memory_needed.ValueOrDie()); |
| 195 |
| 196 ASSERT_EQ(7u, output.items().size()); |
| 197 EXPECT_EQ(*CreateDataItem("hi", 2u), *output.items()[0]->item()); |
| 198 EXPECT_EQ(*CreateDataDescriptionItem(2u), *output.items()[1]->item()); |
| 199 EXPECT_EQ(*CreateFileItem(3u, 5u), *output.items()[2]->item()); |
| 200 EXPECT_EQ(GetItemInBlob(kDataBlob, 0), *output.items()[3]); |
| 201 EXPECT_EQ(*CreateFileItem(2u, 3u), *output.items()[4]->item()); |
| 202 EXPECT_EQ(*CreateDataDescriptionItem(12u), *output.items()[5]->item()); |
| 203 EXPECT_EQ(*CreateFutureFileItem(3u, 2u), *output.items()[6]->item()); |
| 204 |
| 205 // We're copying item at index 1 and 6. |
| 206 ASSERT_EQ(2u, flattener.copies.size()); |
| 207 EXPECT_EQ(*flattener.copies[0].dest_item, *output.items()[1]); |
| 208 EXPECT_EQ(GetItemInBlob(kDataBlob, 0), *flattener.copies[0].source_item); |
| 209 EXPECT_EQ(1u, flattener.copies[0].source_item_offset); |
| 210 |
| 211 EXPECT_EQ(*flattener.copies[1].dest_item, *output.items()[6]); |
| 212 EXPECT_EQ(GetItemInBlob(kPendingFileBlob, 0), |
| 213 *flattener.copies[1].source_item); |
| 214 EXPECT_EQ(1u, flattener.copies[0].source_item_offset); |
| 215 } |
| 216 |
| 217 } // namespace storage |
OLD | NEW |