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