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_storage_context.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/files/file_path.h" |
| 10 #include "storage/browser/blob/blob_data_builder.h" |
| 11 #include "storage/browser/blob/blob_data_item.h" |
| 12 #include "storage/browser/blob/blob_entry.h" |
| 13 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 14 #include "storage/common/data_element.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace storage { |
| 18 namespace { |
| 19 const char kType[] = "type"; |
| 20 const char kDisposition[] = ""; |
| 21 } // namespace |
| 22 |
| 23 class BlobSliceTest : public testing::Test { |
| 24 protected: |
| 25 using BlobSlice = BlobStorageContext::BlobSlice; |
| 26 |
| 27 BlobSliceTest() {} |
| 28 ~BlobSliceTest() override {} |
| 29 |
| 30 scoped_refptr<ShareableBlobDataItem> CreateDataItem(size_t size) { |
| 31 std::unique_ptr<DataElement> element(new DataElement()); |
| 32 element->SetToAllocatedBytes(size); |
| 33 for (size_t i = 0; i < size; i++) { |
| 34 *(element->mutable_bytes() + i) = i; |
| 35 } |
| 36 return scoped_refptr<ShareableBlobDataItem>( |
| 37 new ShareableBlobDataItem(new BlobDataItem(std::move(element)), |
| 38 ShareableBlobDataItem::QUOTA_NEEDED)); |
| 39 }; |
| 40 |
| 41 scoped_refptr<ShareableBlobDataItem> CreateFileItem(size_t offset, |
| 42 size_t size) { |
| 43 std::unique_ptr<DataElement> element(new DataElement()); |
| 44 element->SetToFilePathRange(base::FilePath(FILE_PATH_LITERAL("kFakePath")), |
| 45 offset, size, base::Time::Max()); |
| 46 return scoped_refptr<ShareableBlobDataItem>(new ShareableBlobDataItem( |
| 47 new BlobDataItem(std::move(element)), |
| 48 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA)); |
| 49 }; |
| 50 |
| 51 void ExpectFirstSlice(const BlobSlice& slice, |
| 52 scoped_refptr<ShareableBlobDataItem> source_item, |
| 53 size_t first_item_slice_offset, |
| 54 size_t size) { |
| 55 EXPECT_TRUE(slice.first_source_item); |
| 56 EXPECT_EQ(first_item_slice_offset, slice.first_item_slice_offset); |
| 57 |
| 58 ASSERT_LE(1u, slice.dest_items.size()); |
| 59 const DataElement& dest_element = |
| 60 slice.dest_items[0]->item()->data_element(); |
| 61 |
| 62 EXPECT_EQ(DataElement::TYPE_BYTES_DESCRIPTION, dest_element.type()); |
| 63 EXPECT_EQ(static_cast<uint64_t>(size), dest_element.length()); |
| 64 |
| 65 EXPECT_EQ(*source_item, *slice.first_source_item); |
| 66 } |
| 67 |
| 68 void ExpectLastSlice(const BlobSlice& slice, |
| 69 scoped_refptr<ShareableBlobDataItem> source_item, |
| 70 size_t size) { |
| 71 EXPECT_TRUE(slice.last_source_item); |
| 72 |
| 73 ASSERT_LE(2u, slice.dest_items.size()); |
| 74 const DataElement& dest_element = |
| 75 slice.dest_items.back()->item()->data_element(); |
| 76 |
| 77 EXPECT_EQ(DataElement::TYPE_BYTES_DESCRIPTION, dest_element.type()); |
| 78 EXPECT_EQ(static_cast<uint64_t>(size), dest_element.length()); |
| 79 |
| 80 EXPECT_EQ(*source_item, *slice.last_source_item); |
| 81 } |
| 82 }; |
| 83 |
| 84 TEST_F(BlobSliceTest, FullItem) { |
| 85 const std::string kBlobUUID = "kId"; |
| 86 const size_t kSize = 5u; |
| 87 |
| 88 BlobEntry data(kType, kDisposition); |
| 89 scoped_refptr<ShareableBlobDataItem> item = CreateDataItem(kSize); |
| 90 data.AppendSharedBlobItem(item); |
| 91 |
| 92 BlobSlice slice(data, 0, 5); |
| 93 EXPECT_EQ(0u, slice.copying_memory_size.ValueOrDie()); |
| 94 EXPECT_FALSE(slice.first_source_item); |
| 95 EXPECT_FALSE(slice.last_source_item); |
| 96 EXPECT_FALSE(slice.first_source_item); |
| 97 EXPECT_FALSE(slice.last_source_item); |
| 98 ASSERT_EQ(1u, slice.dest_items.size()); |
| 99 EXPECT_EQ(item, slice.dest_items[0]); |
| 100 } |
| 101 |
| 102 TEST_F(BlobSliceTest, SliceSingleItem) { |
| 103 const std::string kBlobUUID = "kId"; |
| 104 const size_t kSize = 5u; |
| 105 |
| 106 BlobEntry data(kType, kDisposition); |
| 107 scoped_refptr<ShareableBlobDataItem> item = CreateDataItem(kSize); |
| 108 data.AppendSharedBlobItem(item); |
| 109 |
| 110 BlobSlice slice(data, 1, 3); |
| 111 EXPECT_EQ(3u, slice.copying_memory_size.ValueOrDie()); |
| 112 EXPECT_FALSE(slice.last_source_item); |
| 113 ExpectFirstSlice(slice, item, 1, 3); |
| 114 ASSERT_EQ(1u, slice.dest_items.size()); |
| 115 } |
| 116 |
| 117 TEST_F(BlobSliceTest, SliceSingleLastItem) { |
| 118 const std::string kBlobUUID = "kId"; |
| 119 const size_t kSize1 = 5u; |
| 120 const size_t kSize2 = 10u; |
| 121 |
| 122 BlobEntry data(kType, kDisposition); |
| 123 scoped_refptr<ShareableBlobDataItem> item1 = CreateDataItem(kSize1); |
| 124 scoped_refptr<ShareableBlobDataItem> item2 = CreateDataItem(kSize2); |
| 125 data.AppendSharedBlobItem(item1); |
| 126 data.AppendSharedBlobItem(item2); |
| 127 |
| 128 BlobSlice slice(data, 6, 2); |
| 129 EXPECT_EQ(2u, slice.copying_memory_size.ValueOrDie()); |
| 130 ExpectFirstSlice(slice, item2, 1, 2); |
| 131 ASSERT_EQ(1u, slice.dest_items.size()); |
| 132 } |
| 133 |
| 134 TEST_F(BlobSliceTest, SliceAcrossTwoItems) { |
| 135 const std::string kBlobUUID = "kId"; |
| 136 const size_t kSize1 = 5u; |
| 137 const size_t kSize2 = 10u; |
| 138 |
| 139 BlobEntry data(kType, kDisposition); |
| 140 scoped_refptr<ShareableBlobDataItem> item1 = CreateDataItem(kSize1); |
| 141 scoped_refptr<ShareableBlobDataItem> item2 = CreateDataItem(kSize2); |
| 142 data.AppendSharedBlobItem(item1); |
| 143 data.AppendSharedBlobItem(item2); |
| 144 |
| 145 BlobSlice slice(data, 4, 10); |
| 146 EXPECT_EQ(10u, slice.copying_memory_size.ValueOrDie()); |
| 147 ExpectFirstSlice(slice, item1, 4, 1); |
| 148 ExpectLastSlice(slice, item2, 9); |
| 149 ASSERT_EQ(2u, slice.dest_items.size()); |
| 150 } |
| 151 |
| 152 TEST_F(BlobSliceTest, SliceFileAndLastItem) { |
| 153 const std::string kBlobUUID = "kId"; |
| 154 const size_t kSize1 = 5u; |
| 155 const size_t kSize2 = 10u; |
| 156 |
| 157 BlobEntry data(kType, kDisposition); |
| 158 scoped_refptr<ShareableBlobDataItem> item1 = CreateFileItem(0u, kSize1); |
| 159 scoped_refptr<ShareableBlobDataItem> item2 = CreateDataItem(kSize2); |
| 160 data.AppendSharedBlobItem(item1); |
| 161 data.AppendSharedBlobItem(item2); |
| 162 |
| 163 BlobSlice slice(data, 4, 2); |
| 164 EXPECT_EQ(1u, slice.copying_memory_size.ValueOrDie()); |
| 165 EXPECT_FALSE(slice.first_source_item); |
| 166 ExpectLastSlice(slice, item2, 1); |
| 167 ASSERT_EQ(2u, slice.dest_items.size()); |
| 168 |
| 169 EXPECT_EQ(*CreateFileItem(4u, 1u)->item(), *slice.dest_items[0]->item()); |
| 170 } |
| 171 |
| 172 TEST_F(BlobSliceTest, SliceAcrossLargeItem) { |
| 173 const std::string kBlobUUID = "kId"; |
| 174 const size_t kSize1 = 5u; |
| 175 const size_t kSize2 = 10u; |
| 176 const size_t kSize3 = 10u; |
| 177 |
| 178 BlobEntry data(kType, kDisposition); |
| 179 scoped_refptr<ShareableBlobDataItem> item1 = CreateDataItem(kSize1); |
| 180 scoped_refptr<ShareableBlobDataItem> item2 = CreateFileItem(0u, kSize2); |
| 181 scoped_refptr<ShareableBlobDataItem> item3 = CreateDataItem(kSize3); |
| 182 data.AppendSharedBlobItem(item1); |
| 183 data.AppendSharedBlobItem(item2); |
| 184 data.AppendSharedBlobItem(item3); |
| 185 |
| 186 BlobSlice slice(data, 2, 20); |
| 187 EXPECT_EQ(3u + 7u, slice.copying_memory_size.ValueOrDie()); |
| 188 ExpectFirstSlice(slice, item1, 2, 3); |
| 189 ExpectLastSlice(slice, item3, 7); |
| 190 ASSERT_EQ(3u, slice.dest_items.size()); |
| 191 |
| 192 EXPECT_EQ(*item2, *slice.dest_items[1]); |
| 193 } |
| 194 |
| 195 } // namespace storage |
OLD | NEW |