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

Side by Side Diff: content/browser/indexed_db/indexed_db_dispatcher_host_unittest.cc

Issue 2784003002: [IndexedDB] Mojo testing harness. (Closed)
Patch Set: comments Created 3 years, 7 months 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 2017 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 "content/browser/indexed_db/indexed_db_dispatcher_host.h"
6
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/utf_offset_string_conversions.h"
13 #include "base/test/test_simple_task_runner.h"
14 #include "content/browser/browser_thread_impl.h"
15 #include "content/browser/indexed_db/indexed_db_callbacks.h"
16 #include "content/browser/indexed_db/indexed_db_context_impl.h"
17 #include "content/browser/indexed_db/indexed_db_database_callbacks.h"
18 #include "content/browser/indexed_db/indexed_db_factory.h"
19 #include "content/browser/indexed_db/indexed_db_pending_connection.h"
20 #include "content/browser/indexed_db/mock_mojo_indexed_db_callbacks.h"
21 #include "content/browser/indexed_db/mock_mojo_indexed_db_database_callbacks.h"
22 #include "content/common/indexed_db/indexed_db.mojom.h"
23 #include "content/public/test/test_browser_context.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "mojo/public/cpp/bindings/associated_interface_ptr.h"
26 #include "mojo/public/cpp/bindings/strong_associated_binding.h"
27 #include "net/url_request/url_request_test_util.h"
28 #include "storage/browser/test/mock_quota_manager_proxy.h"
29 #include "storage/browser/test/mock_special_storage_policy.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h"
33 #include "url/origin.h"
34
35 using indexed_db::mojom::Callbacks;
36 using indexed_db::mojom::CallbacksAssociatedPtrInfo;
37 using indexed_db::mojom::DatabaseAssociatedPtr;
38 using indexed_db::mojom::DatabaseAssociatedRequest;
39 using indexed_db::mojom::DatabaseCallbacks;
40 using indexed_db::mojom::DatabaseCallbacksAssociatedPtrInfo;
41 using indexed_db::mojom::FactoryAssociatedPtr;
42 using indexed_db::mojom::FactoryAssociatedRequest;
43 using indexed_db::mojom::KeyPath;
44 using indexed_db::mojom::Value;
45 using indexed_db::mojom::ValuePtr;
46 using mojo::StrongAssociatedBindingPtr;
47 using testing::_;
48
49 namespace content {
50 namespace {
51
52 ACTION_TEMPLATE(MoveArg,
53 HAS_1_TEMPLATE_PARAMS(int, k),
54 AND_1_VALUE_PARAMS(out)) {
55 *out = std::move(*::testing::get<k>(args));
56 };
57
58 MATCHER_P(IsAssociatedInterfacePtrInfoValid,
59 tf,
60 std::string(negation ? "isn't" : "is") + " " +
61 std::string(tf ? "valid" : "invalid")) {
62 return tf == arg->is_valid();
63 }
64
65 MATCHER_P(MatchesIDBKey, key, "") {
66 return arg.Equals(key);
67 }
68
69 static const char kDatabaseName[] = "db";
70 static const char kOrigin[] = "https://www.example.com";
71 static const int kFakeProcessId = 2;
72
73 base::FilePath CreateAndReturnTempDir(base::ScopedTempDir* temp_dir) {
74 CHECK(temp_dir->CreateUniqueTempDir());
75 return temp_dir->GetPath();
76 }
77
78 // Represents a transaction from the renderer side.
79 class TestTransaction {
80 public:
81 using CallbackList = std::list<std::unique_ptr<MockMojoIndexedDBCallbacks>>;
82
83 TestTransaction(::indexed_db::mojom::Database* database,
84 int64_t transaction_id)
85 : database_(database), transaction_id_(transaction_id) {}
86 TestTransaction(TestTransaction&& other) = default;
87
88 ~TestTransaction() {}
89
90 void EnqueueCreateObjectStore(int64_t object_store_id,
91 base::string16 name,
92 const content::IndexedDBKeyPath& key_path,
93 bool auto_increment) {
94 database_->CreateObjectStore(transaction_id_, object_store_id, name,
95 key_path, auto_increment);
96 }
97
98 void EnqueueCommit() { database_->Commit(transaction_id_); }
99
100 CallbackList::iterator EnqueuePut(
101 int64_t object_store_id,
102 ValuePtr value,
103 const content::IndexedDBKey& key,
104 blink::WebIDBPutMode mode,
105 const std::vector<content::IndexedDBIndexKeys>& index_keys) {
106 // Create our callback.
107 CallbacksAssociatedPtrInfo callbacks_ptr_info;
108 std::unique_ptr<MockMojoIndexedDBCallbacks> callbacks(
109 new MockMojoIndexedDBCallbacks(mojo::MakeRequest(&callbacks_ptr_info)));
110 callback_list_.push_back(std::move(callbacks));
111 database_->Put(transaction_id_, object_store_id, std::move(value), key,
112 mode, index_keys, std::move(callbacks_ptr_info));
113 return --callback_list_.end();
114 }
115
116 void ExpectTxnComplete(
117 MockMojoIndexedDBDatabaseCallbacks* connection_callbacks) {
118 EXPECT_CALL(*connection_callbacks, Complete(transaction_id_)).Times(1);
119 }
120
121 MockMojoIndexedDBCallbacks* GetCallbacksObject(CallbackList::iterator it) {
122 return (*it).get();
123 }
124
125 private:
126 ::indexed_db::mojom::Database* database_;
127 int64_t transaction_id_;
128 CallbackList callback_list_;
129
130 DISALLOW_COPY_AND_ASSIGN(TestTransaction);
131 };
132
133 // Represents a database connection from the renderer side.
134 class TestDatabaseConnection {
Reilly Grant (use Gerrit) 2017/05/12 01:46:52 I recommend against using a class like this becaus
135 public:
136 TestDatabaseConnection(url::Origin origin,
137 base::string16 db_name,
138 int64_t version,
139 int64_t upgrade_txn_id)
140 : origin_(std::move(origin)),
141 db_name_(std::move(db_name)),
142 version_(version),
143 upgrade_txn_id_(upgrade_txn_id){};
144 TestDatabaseConnection(TestDatabaseConnection&& other) = default;
145
146 ~TestDatabaseConnection() {}
147
148 // The factory has to be flushed or the io thread run for the message to be
149 // sent.
150 void QueueOpenRequestAndCreateCallbacks(FactoryAssociatedPtr* factory) {
151 ASSERT_FALSE(IsCallbackBound()) << "Connection already opened.";
152
153 CallbacksAssociatedPtrInfo callbacks_ptr_info;
154 DatabaseCallbacksAssociatedPtrInfo database_callbacks_ptr_info;
155
156 open_callbacks_.reset(new MockMojoIndexedDBCallbacks(
157 ::mojo::MakeRequest(&callbacks_ptr_info)));
158 connection_callbacks_.reset(new MockMojoIndexedDBDatabaseCallbacks(
159 ::mojo::MakeRequest(&database_callbacks_ptr_info)));
160
161 (*factory)->Open(std::move(callbacks_ptr_info),
162 std::move(database_callbacks_ptr_info), origin_, db_name_,
163 version_, upgrade_txn_id_);
164 }
165
166 void ExpectUpgradeCall(int64_t old_version) {
167 EXPECT_FALSE(database_info_.is_valid());
168 EXPECT_CALL(*open_callbacks_,
169 MockedUpgradeNeeded(IsAssociatedInterfacePtrInfoValid(true),
170 old_version, blink::kWebIDBDataLossNone,
171 std::string(""), _))
172 .WillOnce(testing::DoAll(MoveArg<0>(&database_info_),
173 testing::SaveArg<4>(&metadata_)));
174 }
175
176 void ExpectDatabaseSuccess(bool with_database) {
177 if (with_database) {
178 EXPECT_CALL(
179 *open_callbacks_,
180 MockedSuccessDatabase(IsAssociatedInterfacePtrInfoValid(true), _))
181 .WillOnce(testing::DoAll(MoveArg<0>(&database_info_),
182 testing::SaveArg<1>(&metadata_)));
183 } else {
184 EXPECT_CALL(
185 *open_callbacks_,
186 MockedSuccessDatabase(IsAssociatedInterfacePtrInfoValid(false), _));
187 }
188 }
189
190 void ValidateMetadata() {
191 EXPECT_EQ(db_name_, metadata_.name);
192 EXPECT_EQ(version_, metadata_.version);
193 }
194
195 bool IsCallbackBound() { return open_callbacks_ && connection_callbacks_; }
196
197 bool IsDatabaseBound() { return database_.is_bound(); }
198
199 void CreateDatabasePtr() {
200 ASSERT_TRUE(database_info_.is_valid());
201 database_.Bind(std::move(database_info_));
202 }
203
204 void FlushDatabaseMessages() { database_.FlushForTesting(); }
205
206 ::indexed_db::mojom::Database* database() {
207 EXPECT_TRUE(IsDatabaseBound());
208 return database_.get();
209 }
210
211 // Callbacks given to the 'open' call - used for upgrading & recieving the db.
212 MockMojoIndexedDBCallbacks* open_callbacks() { return open_callbacks_.get(); }
213
214 // Callbacks given to the 'open' call for interacting with connection.
215 MockMojoIndexedDBDatabaseCallbacks* connection_callbacks() {
216 return connection_callbacks_.get();
217 }
218
219 const IndexedDBDatabaseMetadata& metadata() { return metadata_; }
220
221 private:
222 url::Origin origin_;
223 base::string16 db_name_;
224 int64_t version_;
225 int64_t upgrade_txn_id_;
226
227 DatabaseAssociatedPtr database_;
228 IndexedDBDatabaseMetadata metadata_;
229
230 ::indexed_db::mojom::DatabaseAssociatedPtrInfo database_info_;
231
232 std::unique_ptr<MockMojoIndexedDBCallbacks> open_callbacks_;
233 std::unique_ptr<MockMojoIndexedDBDatabaseCallbacks> connection_callbacks_;
234
235 DISALLOW_COPY_AND_ASSIGN(TestDatabaseConnection);
236 };
237
238 } // namespace
239
240 class IndexedDBDispatcherHostTest : public testing::Test {
241 public:
242 IndexedDBDispatcherHostTest()
243 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
244 db_task_runner_(new base::TestSimpleTaskRunner()),
245 request_context_getter_(
246 new net::TestURLRequestContextGetter(db_task_runner_)),
247 special_storage_policy_(new MockSpecialStoragePolicy()),
248 quota_manager_proxy_(
249 new MockQuotaManagerProxy(nullptr, db_task_runner_.get())),
250 context_impl_(
251 new IndexedDBContextImpl(CreateAndReturnTempDir(&temp_dir_),
252 special_storage_policy_.get(),
253 quota_manager_proxy_.get(),
254 db_task_runner_.get())),
255 blob_storage_(ChromeBlobStorageContext::GetFor(&browser_context_)),
256 host_(new IndexedDBDispatcherHost(kFakeProcessId,
257 request_context_getter_.get(),
258 context_impl_.get(),
259 blob_storage_.get())) {
260 base::RunLoop().RunUntilIdle();
261 }
262
263 void TearDown() override {
264 host_.reset();
265 base::RunLoop().RunUntilIdle();
266 db_task_runner_->RunUntilIdle();
267 base::RunLoop().RunUntilIdle();
268 // We leak files if this doesn't return true.
269 ASSERT_TRUE(temp_dir_.Delete());
270 }
271
272 void SetUp() override {
273 FactoryAssociatedRequest request =
274 ::mojo::MakeIsolatedRequest(&idb_mojo_factory_);
275 host_->AddBinding(std::move(request));
276 }
277
278 protected:
279 TestBrowserThreadBundle thread_bundle_;
280 TestBrowserContext browser_context_;
281
282 base::ScopedTempDir temp_dir_;
283 scoped_refptr<base::TestSimpleTaskRunner> db_task_runner_;
284 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
285 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_;
286 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
287 scoped_refptr<IndexedDBContextImpl> context_impl_;
288 scoped_refptr<ChromeBlobStorageContext> blob_storage_;
289 std::unique_ptr<IndexedDBDispatcherHost, BrowserThread::DeleteOnIOThread>
290 host_;
291 FactoryAssociatedPtr idb_mojo_factory_;
292
293 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcherHostTest);
294 };
295
296 TEST_F(IndexedDBDispatcherHostTest, CloseConnectionBeforeUpgrade) {
297 TestDatabaseConnection connection(url::Origin(GURL(kOrigin)),
298 base::UTF8ToUTF16(kDatabaseName), 1, 1);
299
300 // Queue open request message.
301 connection.QueueOpenRequestAndCreateCallbacks(&idb_mojo_factory_);
302
303 // Create mock expectation.
304 connection.ExpectUpgradeCall(IndexedDBDatabaseMetadata::NO_VERSION);
305
306 // Send message.
307 idb_mojo_factory_.FlushForTesting();
308
309 // Run database tasks and io tasks to send message back.
310 db_task_runner_->RunUntilIdle();
311 base::RunLoop().RunUntilIdle();
312
313 connection.ValidateMetadata();
314 }
315
316 TEST_F(IndexedDBDispatcherHostTest, CloseAfterUpgrade) {
317 // Open connection.
318 TestDatabaseConnection connection(url::Origin(GURL(kOrigin)),
319 base::UTF8ToUTF16(kDatabaseName), 1, 1);
320 connection.QueueOpenRequestAndCreateCallbacks(&idb_mojo_factory_);
321 connection.ExpectUpgradeCall(IndexedDBDatabaseMetadata::NO_VERSION);
322 idb_mojo_factory_.FlushForTesting();
323 db_task_runner_->RunUntilIdle();
324 base::RunLoop().RunUntilIdle();
325 connection.ValidateMetadata();
326 connection.CreateDatabasePtr();
327
328 // Create object store.
329 TestTransaction txn(connection.database(), 1);
330 txn.EnqueueCreateObjectStore(10, base::UTF8ToUTF16(kDatabaseName),
331 content::IndexedDBKeyPath(), false);
332 txn.EnqueueCommit();
333 txn.ExpectTxnComplete(connection.connection_callbacks());
334 connection.ExpectDatabaseSuccess(/* with_database */ false);
335
336 connection.FlushDatabaseMessages();
337 db_task_runner_->RunUntilIdle();
338 base::RunLoop().RunUntilIdle();
339 }
340
341 TEST_F(IndexedDBDispatcherHostTest, OpenNewConnectionWhileUpgrading) {
342 // Open connection 1.
343 TestDatabaseConnection connection1(url::Origin(GURL(kOrigin)),
344 base::UTF8ToUTF16(kDatabaseName), 1, 1);
345 connection1.QueueOpenRequestAndCreateCallbacks(&idb_mojo_factory_);
346 connection1.ExpectUpgradeCall(IndexedDBDatabaseMetadata::NO_VERSION);
347 idb_mojo_factory_.FlushForTesting();
348 db_task_runner_->RunUntilIdle();
349 base::RunLoop().RunUntilIdle();
350 connection1.ValidateMetadata();
351 connection1.CreateDatabasePtr();
352
353 // Open connection 2. Should not open yet, as we are pending upgrade of
354 // database on connection 1.
355 TestDatabaseConnection connection2(url::Origin(GURL(kOrigin)),
356 base::UTF8ToUTF16(kDatabaseName), 1, 0);
357 connection2.QueueOpenRequestAndCreateCallbacks(&idb_mojo_factory_);
358 connection2.ExpectDatabaseSuccess(true);
359 idb_mojo_factory_.FlushForTesting();
360 db_task_runner_->RunUntilIdle();
361 base::RunLoop().RunUntilIdle();
362 EXPECT_EQ(IndexedDBDatabaseMetadata::NO_VERSION,
363 connection2.metadata().version);
364
365 // Create object store on first connection
366 TestTransaction txn(connection1.database(), 1);
367 txn.EnqueueCreateObjectStore(10, base::UTF8ToUTF16(kDatabaseName),
368 content::IndexedDBKeyPath(), false);
369 txn.EnqueueCommit();
370 txn.ExpectTxnComplete(connection1.connection_callbacks());
371 connection1.ExpectDatabaseSuccess(/* with_database */ false);
372 connection1.FlushDatabaseMessages();
373 db_task_runner_->RunUntilIdle();
374 base::RunLoop().RunUntilIdle();
375
376 connection2.ValidateMetadata();
377 connection2.CreateDatabasePtr();
378 }
379
380 TEST_F(IndexedDBDispatcherHostTest, PutWithInvalidBlob) {
381 const int64_t kObjectStoreId = 10;
382 const int64_t kUpgradeTxnId = 1;
383 // Open connection 1.
384 TestDatabaseConnection connection(url::Origin(GURL(kOrigin)),
385 base::UTF8ToUTF16(kDatabaseName), 1,
386 kUpgradeTxnId);
387 connection.QueueOpenRequestAndCreateCallbacks(&idb_mojo_factory_);
388 connection.ExpectUpgradeCall(IndexedDBDatabaseMetadata::NO_VERSION);
389 idb_mojo_factory_.FlushForTesting();
390 db_task_runner_->RunUntilIdle();
391 base::RunLoop().RunUntilIdle();
392 connection.ValidateMetadata();
393 connection.CreateDatabasePtr();
394
395 // Create object store on first connection
396 TestTransaction txn(connection.database(), kUpgradeTxnId);
397 txn.EnqueueCreateObjectStore(kObjectStoreId, base::UTF8ToUTF16(kDatabaseName),
398 content::IndexedDBKeyPath(), false);
399
400 // Call Put with an invalid blob.
401 std::vector<::indexed_db::mojom::BlobInfoPtr> blobs;
402 blobs.push_back(::indexed_db::mojom::BlobInfo::New(
403 "fakeUUID", base::string16(), 100, nullptr));
404 auto callback_it = txn.EnqueuePut(
405 kObjectStoreId, Value::New("hello", std::move(blobs)),
406 content::IndexedDBKey(base::UTF8ToUTF16("hello")),
407 blink::kWebIDBPutModeAddOnly, std::vector<content::IndexedDBIndexKeys>());
408 txn.EnqueueCommit();
409
410 // Test we get right sequence.
411 {
412 ::testing::InSequence dummy;
413 EXPECT_CALL(*txn.GetCallbacksObject(callback_it),
414 Error(blink::kWebIDBDatabaseExceptionUnknownError, _))
415 .Times(1);
416
417 EXPECT_CALL(
418 *connection.connection_callbacks(),
419 Abort(kUpgradeTxnId, blink::kWebIDBDatabaseExceptionUnknownError, _))
420 .Times(1);
421
422 EXPECT_CALL(*connection.open_callbacks(),
423 Error(blink::kWebIDBDatabaseExceptionAbortError, _))
424 .Times(1);
425 }
426 connection.FlushDatabaseMessages();
427 db_task_runner_->RunUntilIdle();
428 base::RunLoop().RunUntilIdle();
429 }
430
431 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/indexed_db_dispatcher_host.cc ('k') | content/browser/indexed_db/indexed_db_transaction.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698