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