OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "storage/browser/blob/blob_async_builder_host.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/shared_memory.h" |
| 10 #include "storage/common/blob_storage/blob_storage_constants.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace storage { |
| 14 namespace { |
| 15 const std::string kBlobUUID = "blobUUIDYAY"; |
| 16 const std::string kFakeBlobUUID = "fakeBlob"; |
| 17 const std::string kBlobType = "blobtypeYAY"; |
| 18 |
| 19 const size_t kTestBlobStorageIPCThresholdBytes = 5; |
| 20 const size_t kTestBlobStorageMaxSharedMemoryBytes = 20; |
| 21 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; |
| 22 |
| 23 void PopulateBytes(char* bytes, size_t start_num, size_t length) { |
| 24 for (size_t i = 0; i < length; i++) { |
| 25 bytes[i] = static_cast<char>(start_num + i); |
| 26 } |
| 27 } |
| 28 |
| 29 void PopulateBytes(char* bytes, size_t length) { |
| 30 PopulateBytes(bytes, 0, length); |
| 31 } |
| 32 |
| 33 void AddMemoryItem(size_t length, std::vector<DataElement>* out) { |
| 34 DataElement bytes; |
| 35 bytes.SetToBytesDescription(length); |
| 36 out->push_back(bytes); |
| 37 } |
| 38 |
| 39 void AddShortcutMemoryItem(size_t length, std::vector<DataElement>* out) { |
| 40 DataElement bytes; |
| 41 bytes.SetToAllocatedBytes(length); |
| 42 PopulateBytes(bytes.mutable_bytes(), length); |
| 43 out->push_back(bytes); |
| 44 } |
| 45 |
| 46 void AddShortcutMemoryItem(size_t length, BlobDataBuilder* out) { |
| 47 DataElement bytes; |
| 48 bytes.SetToAllocatedBytes(length); |
| 49 PopulateBytes(bytes.mutable_bytes(), length); |
| 50 out->AppendData(bytes.bytes(), length); |
| 51 } |
| 52 |
| 53 void AddBlobItem(std::vector<DataElement>* out) { |
| 54 DataElement blob; |
| 55 blob.SetToBlob(kFakeBlobUUID); |
| 56 out->push_back(blob); |
| 57 } |
| 58 |
| 59 class BlobAsyncBuilderHostTest : public testing::Test { |
| 60 protected: |
| 61 BlobAsyncBuilderHostTest() |
| 62 : matching_builder_(nullptr), |
| 63 done_called_(false), |
| 64 cancel_called_(false), |
| 65 cancel_code_(IPCBlobCreationCancelCode::UNKNOWN), |
| 66 request_called_(false) {} |
| 67 ~BlobAsyncBuilderHostTest() override {} |
| 68 |
| 69 void SetUp() override { |
| 70 matching_builder_ = nullptr; |
| 71 done_called_ = false; |
| 72 cancel_called_ = false; |
| 73 cancel_code_ = IPCBlobCreationCancelCode::UNKNOWN; |
| 74 request_called_ = false; |
| 75 requests_.clear(); |
| 76 memory_handles_.clear(); |
| 77 file_handles_.clear(); |
| 78 host_.SetMemoryConstantsForTesting(kTestBlobStorageIPCThresholdBytes, |
| 79 kTestBlobStorageMaxSharedMemoryBytes, |
| 80 kTestBlobStorageMaxFileSizeBytes); |
| 81 } |
| 82 |
| 83 void SetMatchingBuilder(BlobDataBuilder* builder) { |
| 84 matching_builder_ = builder; |
| 85 } |
| 86 |
| 87 void CancelCallback(IPCBlobCreationCancelCode code) { |
| 88 cancel_called_ = true; |
| 89 cancel_code_ = code; |
| 90 } |
| 91 |
| 92 void DoneCallback(const BlobDataBuilder& builder) { |
| 93 if (matching_builder_) |
| 94 EXPECT_EQ(*matching_builder_, builder); |
| 95 done_called_ = true; |
| 96 } |
| 97 |
| 98 void RequestMemoryCallback( |
| 99 const std::vector<storage::BlobItemBytesRequest>& requests, |
| 100 const std::vector<base::SharedMemoryHandle>& sms, |
| 101 const std::vector<uint64_t>& pfs) { |
| 102 this->requests_ = requests; |
| 103 memory_handles_ = sms; |
| 104 file_handles_ = pfs; |
| 105 request_called_ = true; |
| 106 } |
| 107 |
| 108 void BuildBlobAsync(const std::vector<DataElement>& descriptions, |
| 109 size_t memory_available) { |
| 110 host_.StartBuildingBlob( |
| 111 kBlobUUID, kBlobType, descriptions, memory_available, |
| 112 base::Bind(&BlobAsyncBuilderHostTest::RequestMemoryCallback, |
| 113 base::Unretained(this)), |
| 114 base::Bind(&BlobAsyncBuilderHostTest::DoneCallback, |
| 115 base::Unretained(this)), |
| 116 base::Bind(&BlobAsyncBuilderHostTest::CancelCallback, |
| 117 base::Unretained(this))); |
| 118 } |
| 119 |
| 120 BlobDataBuilder* matching_builder_; |
| 121 BlobAsyncBuilderHost host_; |
| 122 bool done_called_; |
| 123 bool cancel_called_; |
| 124 IPCBlobCreationCancelCode cancel_code_; |
| 125 |
| 126 bool request_called_; |
| 127 std::vector<storage::BlobItemBytesRequest> requests_; |
| 128 std::vector<base::SharedMemoryHandle> memory_handles_; |
| 129 std::vector<uint64_t> file_handles_; |
| 130 }; |
| 131 |
| 132 TEST_F(BlobAsyncBuilderHostTest, TestShortcut) { |
| 133 std::vector<DataElement> descriptions; |
| 134 |
| 135 AddShortcutMemoryItem(10, &descriptions); |
| 136 AddBlobItem(&descriptions); |
| 137 AddShortcutMemoryItem(5000, &descriptions); |
| 138 |
| 139 BlobDataBuilder expected(kBlobUUID); |
| 140 expected.set_content_type(kBlobType); |
| 141 AddShortcutMemoryItem(10, &expected); |
| 142 expected.AppendBlob(kFakeBlobUUID); |
| 143 AddShortcutMemoryItem(5000, &expected); |
| 144 SetMatchingBuilder(&expected); |
| 145 |
| 146 BuildBlobAsync(descriptions, 5010); |
| 147 |
| 148 EXPECT_TRUE(done_called_); |
| 149 EXPECT_FALSE(cancel_called_); |
| 150 EXPECT_FALSE(request_called_); |
| 151 EXPECT_EQ(0u, host_.blob_building_count()); |
| 152 }; |
| 153 |
| 154 TEST_F(BlobAsyncBuilderHostTest, TestSingleSharedMemRequest) { |
| 155 std::vector<DataElement> descriptions; |
| 156 const size_t kSize = kTestBlobStorageIPCThresholdBytes + 1; |
| 157 AddMemoryItem(kSize, &descriptions); |
| 158 |
| 159 BuildBlobAsync(descriptions, kTestBlobStorageIPCThresholdBytes + 1); |
| 160 |
| 161 EXPECT_FALSE(done_called_); |
| 162 EXPECT_FALSE(cancel_called_); |
| 163 EXPECT_TRUE(request_called_); |
| 164 EXPECT_EQ(1u, host_.blob_building_count()); |
| 165 ASSERT_EQ(1u, requests_.size()); |
| 166 request_called_ = false; |
| 167 |
| 168 EXPECT_EQ( |
| 169 BlobItemBytesRequest::CreateSharedMemoryRequest(0, 0, 0, kSize, 0, 0), |
| 170 requests_.at(0)); |
| 171 }; |
| 172 |
| 173 TEST_F(BlobAsyncBuilderHostTest, TestMultipleSharedMemRequests) { |
| 174 std::vector<DataElement> descriptions; |
| 175 const size_t kSize = kTestBlobStorageMaxSharedMemoryBytes + 1; |
| 176 const char kFirstBlockByte = 7; |
| 177 const char kSecondBlockByte = 19; |
| 178 AddMemoryItem(kSize, &descriptions); |
| 179 |
| 180 BlobDataBuilder expected(kBlobUUID); |
| 181 expected.set_content_type(kBlobType); |
| 182 char data[kSize]; |
| 183 memset(data, kFirstBlockByte, kTestBlobStorageMaxSharedMemoryBytes); |
| 184 expected.AppendData(data, kTestBlobStorageMaxSharedMemoryBytes); |
| 185 expected.AppendData(&kSecondBlockByte, 1); |
| 186 SetMatchingBuilder(&expected); |
| 187 |
| 188 BuildBlobAsync(descriptions, kTestBlobStorageMaxSharedMemoryBytes + 1); |
| 189 |
| 190 EXPECT_FALSE(done_called_); |
| 191 EXPECT_FALSE(cancel_called_); |
| 192 EXPECT_TRUE(request_called_); |
| 193 EXPECT_EQ(1u, host_.blob_building_count()); |
| 194 ASSERT_EQ(1u, requests_.size()); |
| 195 request_called_ = false; |
| 196 |
| 197 // We need to grab a duplicate handle so we can have two blocks open at the |
| 198 // same time. |
| 199 base::SharedMemoryHandle handle = |
| 200 base::SharedMemory::DuplicateHandle(memory_handles_.at(0)); |
| 201 EXPECT_TRUE(base::SharedMemory::IsHandleValid(handle)); |
| 202 base::SharedMemory shared_memory(handle, false); |
| 203 EXPECT_TRUE(shared_memory.Map(kTestBlobStorageMaxSharedMemoryBytes)); |
| 204 |
| 205 EXPECT_EQ(BlobItemBytesRequest::CreateSharedMemoryRequest( |
| 206 0, 0, 0, kTestBlobStorageMaxSharedMemoryBytes, 0, 0), |
| 207 requests_.at(0)); |
| 208 |
| 209 memset(shared_memory.memory(), kFirstBlockByte, |
| 210 kTestBlobStorageMaxSharedMemoryBytes); |
| 211 |
| 212 BlobItemBytesResponse response(0); |
| 213 std::vector<BlobItemBytesResponse> responses = {response}; |
| 214 host_.OnMemoryResponses(kBlobUUID, responses); |
| 215 |
| 216 EXPECT_FALSE(done_called_); |
| 217 EXPECT_FALSE(cancel_called_); |
| 218 EXPECT_TRUE(request_called_); |
| 219 EXPECT_EQ(1u, host_.blob_building_count()); |
| 220 ASSERT_EQ(1u, requests_.size()); |
| 221 request_called_ = false; |
| 222 |
| 223 EXPECT_EQ(BlobItemBytesRequest::CreateSharedMemoryRequest( |
| 224 1, 0, kTestBlobStorageMaxSharedMemoryBytes, 1, 0, 0), |
| 225 requests_.at(0)); |
| 226 |
| 227 memset(shared_memory.memory(), kSecondBlockByte, 1); |
| 228 |
| 229 response.request_number = 1; |
| 230 responses[0] = response; |
| 231 host_.OnMemoryResponses(kBlobUUID, responses); |
| 232 EXPECT_TRUE(done_called_); |
| 233 EXPECT_FALSE(cancel_called_); |
| 234 EXPECT_FALSE(request_called_); |
| 235 EXPECT_EQ(0u, host_.blob_building_count()); |
| 236 }; |
| 237 |
| 238 } // namespace |
| 239 } // namespace storage |
OLD | NEW |