Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: content/browser/blob_storage/blob_async_builder_host_unittest.cc

Issue 1098853003: [BlobAsync] Patch 4: Browser Classes & Logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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) {
kinuko 2015/11/25 16:08:17 start_num seems to be always 0? It looks we don't
dmurph 2015/11/25 21:16:30 Done.
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);
kinuko 2015/11/25 16:08:17 nit: could we add a short comment to note that thi
dmurph 2015/11/25 21:16:30 Done.
95 done_called_ = true;
96 }
97
98 void RequestMemoryCallback(
99 const std::vector<storage::BlobItemBytesRequest>& requests,
100 const std::vector<base::SharedMemoryHandle>& sms,
kinuko 2015/11/25 16:08:17 nit: what sms stands for? smh if it stands for Sha
dmurph 2015/11/25 21:16:30 made non-acronyms.
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 bool BuildBlobAsync(const std::vector<DataElement>& descriptions,
109 size_t memory_available) {
110 return 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 EXPECT_TRUE(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 EXPECT_TRUE(
160 BuildBlobAsync(descriptions, kTestBlobStorageIPCThresholdBytes + 1));
161
162 EXPECT_FALSE(done_called_);
163 EXPECT_FALSE(cancel_called_);
164 EXPECT_TRUE(request_called_);
165 EXPECT_EQ(1u, host_.blob_building_count());
166 ASSERT_EQ(1u, requests_.size());
167 request_called_ = false;
168
169 EXPECT_EQ(
170 BlobItemBytesRequest::CreateSharedMemoryRequest(0, 0, 0, kSize, 0, 0),
171 requests_.at(0));
172 };
173
174 TEST_F(BlobAsyncBuilderHostTest, TestMultipleSharedMemRequests) {
175 std::vector<DataElement> descriptions;
176 const size_t kSize = kTestBlobStorageMaxSharedMemoryBytes + 1;
177 const char kFirstBlockByte = 7;
178 const char kSecondBlockByte = 19;
179 AddMemoryItem(kSize, &descriptions);
180
181 BlobDataBuilder expected(kBlobUUID);
182 expected.set_content_type(kBlobType);
183 char data[kSize];
184 memset(data, kFirstBlockByte, kTestBlobStorageMaxSharedMemoryBytes);
185 expected.AppendData(data, kTestBlobStorageMaxSharedMemoryBytes);
186 expected.AppendData(&kSecondBlockByte, 1);
187 SetMatchingBuilder(&expected);
188
189 EXPECT_TRUE(
190 BuildBlobAsync(descriptions, kTestBlobStorageMaxSharedMemoryBytes + 1));
191
192 EXPECT_FALSE(done_called_);
193 EXPECT_FALSE(cancel_called_);
194 EXPECT_TRUE(request_called_);
195 EXPECT_EQ(1u, host_.blob_building_count());
196 ASSERT_EQ(1u, requests_.size());
197 request_called_ = false;
198
199 // We need to grab a duplicate handle so we can have two blocks open at the
200 // same time.
201 base::SharedMemoryHandle handle =
202 base::SharedMemory::DuplicateHandle(memory_handles_.at(0));
203 EXPECT_TRUE(base::SharedMemory::IsHandleValid(handle));
204 base::SharedMemory shared_memory(handle, false);
205 EXPECT_TRUE(shared_memory.Map(kTestBlobStorageMaxSharedMemoryBytes));
206
207 EXPECT_EQ(BlobItemBytesRequest::CreateSharedMemoryRequest(
208 0, 0, 0, kTestBlobStorageMaxSharedMemoryBytes, 0, 0),
209 requests_.at(0));
210
211 memset(shared_memory.memory(), kFirstBlockByte,
212 kTestBlobStorageMaxSharedMemoryBytes);
213
214 BlobItemBytesResponse response(0);
215 std::vector<BlobItemBytesResponse> responses = {response};
216 host_.OnMemoryResponses(kBlobUUID, responses);
217
218 EXPECT_FALSE(done_called_);
219 EXPECT_FALSE(cancel_called_);
220 EXPECT_TRUE(request_called_);
221 EXPECT_EQ(1u, host_.blob_building_count());
222 ASSERT_EQ(1u, requests_.size());
223 request_called_ = false;
224
225 EXPECT_EQ(BlobItemBytesRequest::CreateSharedMemoryRequest(
226 1, 0, kTestBlobStorageMaxSharedMemoryBytes, 1, 0, 0),
227 requests_.at(0));
228
229 memset(shared_memory.memory(), kSecondBlockByte, 1);
230
231 response.request_number = 1;
232 responses[0] = response;
233 host_.OnMemoryResponses(kBlobUUID, responses);
234 EXPECT_TRUE(done_called_);
235 EXPECT_FALSE(cancel_called_);
236 EXPECT_FALSE(request_called_);
237 EXPECT_EQ(0u, host_.blob_building_count());
238 };
239
240 TEST_F(BlobAsyncBuilderHostTest, TestBasicIPCAndStopBuilding) {
241 std::vector<DataElement> descriptions;
242
243 AddMemoryItem(10, &descriptions);
244 AddBlobItem(&descriptions);
245 AddMemoryItem(5000, &descriptions);
246
247 BlobDataBuilder expected(kBlobUUID);
248 expected.set_content_type(kBlobType);
249 expected.AppendFutureData(10);
250 expected.AppendBlob(kFakeBlobUUID);
251 expected.AppendFutureData(5000);
252 SetMatchingBuilder(&expected);
253
254 EXPECT_TRUE(BuildBlobAsync(descriptions, 5010));
255 EXPECT_FALSE(BuildBlobAsync(descriptions, 5010));
michaeln 2015/11/24 23:19:40 tests for other 'bad ipc' cases for start() and on
dmurph 2015/11/25 21:16:30 Done.
256 host_.StopBuildingBlob(kBlobUUID);
257 EXPECT_TRUE(BuildBlobAsync(descriptions, 5010));
258
259 EXPECT_FALSE(done_called_);
260 EXPECT_FALSE(cancel_called_);
261 EXPECT_TRUE(request_called_);
262 EXPECT_EQ(1u, host_.blob_building_count());
263 };
264
265 } // namespace
266 } // namespace storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698