Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / database_tracker_unittest.cc
blob14557de29c85f2372b8ae0ea72b17f7a76668e59
1 // Copyright 2014 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.
5 #include "base/files/file.h"
6 #include "base/files/file_path.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "content/public/test/mock_special_storage_policy.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/test_completion_callback.h"
17 #include "storage/browser/database/database_tracker.h"
18 #include "storage/browser/quota/quota_manager_proxy.h"
19 #include "storage/common/database/database_identifier.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/sqlite/sqlite3.h"
23 using base::ASCIIToUTF16;
24 using storage::DatabaseConnections;
25 using storage::DatabaseTracker;
26 using storage::OriginInfo;
28 namespace {
30 const char kOrigin1Url[] = "http://origin1";
31 const char kOrigin2Url[] = "http://protected_origin2";
33 class TestObserver : public storage::DatabaseTracker::Observer {
34 public:
35 TestObserver()
36 : new_notification_received_(false),
37 observe_size_changes_(true),
38 observe_scheduled_deletions_(true) {
40 TestObserver(bool observe_size_changes, bool observe_scheduled_deletions)
41 : new_notification_received_(false),
42 observe_size_changes_(observe_size_changes),
43 observe_scheduled_deletions_(observe_scheduled_deletions) {
46 ~TestObserver() override {}
47 void OnDatabaseSizeChanged(const std::string& origin_identifier,
48 const base::string16& database_name,
49 int64 database_size) override {
50 if (!observe_size_changes_)
51 return;
52 new_notification_received_ = true;
53 origin_identifier_ = origin_identifier;
54 database_name_ = database_name;
55 database_size_ = database_size;
57 void OnDatabaseScheduledForDeletion(
58 const std::string& origin_identifier,
59 const base::string16& database_name) override {
60 if (!observe_scheduled_deletions_)
61 return;
62 new_notification_received_ = true;
63 origin_identifier_ = origin_identifier;
64 database_name_ = database_name;
66 bool DidReceiveNewNotification() {
67 bool temp_new_notification_received = new_notification_received_;
68 new_notification_received_ = false;
69 return temp_new_notification_received;
71 std::string GetNotificationOriginIdentifier() {
72 return origin_identifier_;
74 base::string16 GetNotificationDatabaseName() { return database_name_; }
75 int64 GetNotificationDatabaseSize() { return database_size_; }
77 private:
78 bool new_notification_received_;
79 bool observe_size_changes_;
80 bool observe_scheduled_deletions_;
81 std::string origin_identifier_;
82 base::string16 database_name_;
83 int64 database_size_;
86 void CheckNotificationReceived(TestObserver* observer,
87 const std::string& expected_origin_identifier,
88 const base::string16& expected_database_name,
89 int64 expected_database_size) {
90 EXPECT_TRUE(observer->DidReceiveNewNotification());
91 EXPECT_EQ(expected_origin_identifier,
92 observer->GetNotificationOriginIdentifier());
93 EXPECT_EQ(expected_database_name,
94 observer->GetNotificationDatabaseName());
95 EXPECT_EQ(expected_database_size,
96 observer->GetNotificationDatabaseSize());
99 class TestQuotaManagerProxy : public storage::QuotaManagerProxy {
100 public:
101 TestQuotaManagerProxy()
102 : QuotaManagerProxy(NULL, NULL),
103 registered_client_(NULL) {
106 void RegisterClient(storage::QuotaClient* client) override {
107 EXPECT_FALSE(registered_client_);
108 registered_client_ = client;
111 void NotifyStorageAccessed(storage::QuotaClient::ID client_id,
112 const GURL& origin,
113 storage::StorageType type) override {
114 EXPECT_EQ(storage::QuotaClient::kDatabase, client_id);
115 EXPECT_EQ(storage::kStorageTypeTemporary, type);
116 accesses_[origin] += 1;
119 void NotifyStorageModified(storage::QuotaClient::ID client_id,
120 const GURL& origin,
121 storage::StorageType type,
122 int64 delta) override {
123 EXPECT_EQ(storage::QuotaClient::kDatabase, client_id);
124 EXPECT_EQ(storage::kStorageTypeTemporary, type);
125 modifications_[origin].first += 1;
126 modifications_[origin].second += delta;
129 // Not needed for our tests.
130 void NotifyOriginInUse(const GURL& origin) override {}
131 void NotifyOriginNoLongerInUse(const GURL& origin) override {}
132 void SetUsageCacheEnabled(storage::QuotaClient::ID client_id,
133 const GURL& origin,
134 storage::StorageType type,
135 bool enabled) override {}
136 void GetUsageAndQuota(base::SequencedTaskRunner* original_task_runner,
137 const GURL& origin,
138 storage::StorageType type,
139 const GetUsageAndQuotaCallback& callback) override {}
141 void SimulateQuotaManagerDestroyed() {
142 if (registered_client_) {
143 registered_client_->OnQuotaManagerDestroyed();
144 registered_client_ = NULL;
148 bool WasAccessNotified(const GURL& origin) {
149 return accesses_[origin] != 0;
152 bool WasModificationNotified(const GURL& origin, int64 amount) {
153 return modifications_[origin].first != 0 &&
154 modifications_[origin].second == amount;
157 void reset() {
158 accesses_.clear();
159 modifications_.clear();
162 storage::QuotaClient* registered_client_;
164 // Map from origin to count of access notifications.
165 std::map<GURL, int> accesses_;
167 // Map from origin to <count, sum of deltas>
168 std::map<GURL, std::pair<int, int64> > modifications_;
170 protected:
171 ~TestQuotaManagerProxy() override { EXPECT_FALSE(registered_client_); }
175 bool EnsureFileOfSize(const base::FilePath& file_path, int64 length) {
176 base::File file(file_path,
177 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE);
178 if (!file.IsValid())
179 return false;
180 return file.SetLength(length);
183 } // namespace
185 namespace content {
187 // We declare a helper class, and make it a friend of DatabaseTracker using
188 // the FORWARD_DECLARE_TEST macro, and we implement all tests we want to run as
189 // static methods of this class. Then we make our TEST() targets call these
190 // static functions. This allows us to run each test in normal mode and
191 // incognito mode without writing the same code twice.
192 class DatabaseTracker_TestHelper_Test {
193 public:
194 static void TestDeleteOpenDatabase(bool incognito_mode) {
195 // Initialize the tracker database.
196 base::ScopedTempDir temp_dir;
197 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
198 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy =
199 new MockSpecialStoragePolicy;
200 special_storage_policy->AddProtected(GURL(kOrigin2Url));
201 scoped_refptr<DatabaseTracker> tracker(
202 new DatabaseTracker(temp_dir.path(),
203 incognito_mode,
204 special_storage_policy.get(),
205 NULL,
206 NULL));
208 // Create and open three databases.
209 int64 database_size = 0;
210 const std::string kOrigin1 =
211 storage::GetIdentifierFromOrigin(GURL(kOrigin1Url));
212 const std::string kOrigin2 =
213 storage::GetIdentifierFromOrigin(GURL(kOrigin2Url));
214 const base::string16 kDB1 = ASCIIToUTF16("db1");
215 const base::string16 kDB2 = ASCIIToUTF16("db2");
216 const base::string16 kDB3 = ASCIIToUTF16("db3");
217 const base::string16 kDescription = ASCIIToUTF16("database_description");
219 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
220 &database_size);
221 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0,
222 &database_size);
223 tracker->DatabaseOpened(kOrigin2, kDB3, kDescription, 0,
224 &database_size);
226 EXPECT_TRUE(base::CreateDirectory(
227 tracker->DatabaseDirectory().Append(base::FilePath::FromUTF16Unsafe(
228 tracker->GetOriginDirectory(kOrigin1)))));
229 EXPECT_TRUE(base::CreateDirectory(
230 tracker->DatabaseDirectory().Append(base::FilePath::FromUTF16Unsafe(
231 tracker->GetOriginDirectory(kOrigin2)))));
232 EXPECT_EQ(1, base::WriteFile(
233 tracker->GetFullDBFilePath(kOrigin1, kDB1), "a", 1));
234 EXPECT_EQ(2, base::WriteFile(
235 tracker->GetFullDBFilePath(kOrigin2, kDB2), "aa", 2));
236 EXPECT_EQ(3, base::WriteFile(
237 tracker->GetFullDBFilePath(kOrigin2, kDB3), "aaa", 3));
238 tracker->DatabaseModified(kOrigin1, kDB1);
239 tracker->DatabaseModified(kOrigin2, kDB2);
240 tracker->DatabaseModified(kOrigin2, kDB3);
242 // Delete db1. Should also delete origin1.
243 TestObserver observer;
244 tracker->AddObserver(&observer);
245 net::TestCompletionCallback callback;
246 int result = tracker->DeleteDatabase(kOrigin1, kDB1, callback.callback());
247 EXPECT_EQ(net::ERR_IO_PENDING, result);
248 ASSERT_FALSE(callback.have_result());
249 EXPECT_TRUE(observer.DidReceiveNewNotification());
250 EXPECT_EQ(kOrigin1, observer.GetNotificationOriginIdentifier());
251 EXPECT_EQ(kDB1, observer.GetNotificationDatabaseName());
252 tracker->DatabaseClosed(kOrigin1, kDB1);
253 result = callback.GetResult(result);
254 EXPECT_EQ(net::OK, result);
255 EXPECT_FALSE(base::PathExists(
256 tracker->DatabaseDirectory().AppendASCII(kOrigin1)));
258 // Recreate db1.
259 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
260 &database_size);
261 EXPECT_TRUE(base::CreateDirectory(
262 tracker->DatabaseDirectory().Append(base::FilePath::FromUTF16Unsafe(
263 tracker->GetOriginDirectory(kOrigin1)))));
264 EXPECT_EQ(1, base::WriteFile(
265 tracker->GetFullDBFilePath(kOrigin1, kDB1), "a", 1));
266 tracker->DatabaseModified(kOrigin1, kDB1);
268 // Setup file modification times. db1 and db2 are modified now, db3 three
269 // days ago.
270 base::Time now = base::Time::Now();
271 EXPECT_TRUE(base::TouchFile(tracker->GetFullDBFilePath(kOrigin1, kDB1),
272 now, now));
273 EXPECT_TRUE(base::TouchFile(tracker->GetFullDBFilePath(kOrigin2, kDB2),
274 now, now));
275 base::Time three_days_ago = now - base::TimeDelta::FromDays(3);
276 EXPECT_TRUE(base::TouchFile(tracker->GetFullDBFilePath(kOrigin2, kDB3),
277 three_days_ago, three_days_ago));
279 // Delete databases modified since yesterday. db2 is whitelisted.
280 base::Time yesterday = base::Time::Now();
281 yesterday -= base::TimeDelta::FromDays(1);
282 result = tracker->DeleteDataModifiedSince(
283 yesterday, callback.callback());
284 EXPECT_EQ(net::ERR_IO_PENDING, result);
285 ASSERT_FALSE(callback.have_result());
286 EXPECT_TRUE(observer.DidReceiveNewNotification());
287 tracker->DatabaseClosed(kOrigin1, kDB1);
288 tracker->DatabaseClosed(kOrigin2, kDB2);
289 result = callback.GetResult(result);
290 EXPECT_EQ(net::OK, result);
291 EXPECT_FALSE(base::PathExists(
292 tracker->DatabaseDirectory().AppendASCII(kOrigin1)));
293 EXPECT_TRUE(
294 base::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB2)));
295 EXPECT_TRUE(
296 base::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB3)));
298 tracker->DatabaseClosed(kOrigin2, kDB3);
299 tracker->RemoveObserver(&observer);
302 static void TestDatabaseTracker(bool incognito_mode) {
303 // Initialize the tracker database.
304 base::ScopedTempDir temp_dir;
305 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
306 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy =
307 new MockSpecialStoragePolicy;
308 special_storage_policy->AddProtected(GURL(kOrigin2Url));
309 scoped_refptr<DatabaseTracker> tracker(
310 new DatabaseTracker(temp_dir.path(),
311 incognito_mode,
312 special_storage_policy.get(),
313 NULL,
314 NULL));
316 // Add two observers.
317 TestObserver observer1;
318 TestObserver observer2;
319 tracker->AddObserver(&observer1);
320 tracker->AddObserver(&observer2);
322 // Open three new databases.
323 int64 database_size = 0;
324 const std::string kOrigin1 =
325 storage::GetIdentifierFromOrigin(GURL(kOrigin1Url));
326 const std::string kOrigin2 =
327 storage::GetIdentifierFromOrigin(GURL(kOrigin2Url));
328 const base::string16 kDB1 = ASCIIToUTF16("db1");
329 const base::string16 kDB2 = ASCIIToUTF16("db2");
330 const base::string16 kDB3 = ASCIIToUTF16("db3");
331 const base::string16 kDescription = ASCIIToUTF16("database_description");
333 // Get the info for kOrigin1 and kOrigin2
334 DatabaseTracker::CachedOriginInfo* origin1_info =
335 tracker->GetCachedOriginInfo(kOrigin1);
336 DatabaseTracker::CachedOriginInfo* origin2_info =
337 tracker->GetCachedOriginInfo(kOrigin1);
338 EXPECT_TRUE(origin1_info);
339 EXPECT_TRUE(origin2_info);
342 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
343 &database_size);
344 EXPECT_EQ(0, database_size);
345 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0,
346 &database_size);
347 EXPECT_EQ(0, database_size);
348 tracker->DatabaseOpened(kOrigin1, kDB3, kDescription, 0,
349 &database_size);
350 EXPECT_EQ(0, database_size);
352 // Write some data to each file and check that the listeners are
353 // called with the appropriate values.
354 EXPECT_TRUE(base::CreateDirectory(
355 tracker->DatabaseDirectory().Append(base::FilePath::FromUTF16Unsafe(
356 tracker->GetOriginDirectory(kOrigin1)))));
357 EXPECT_TRUE(base::CreateDirectory(
358 tracker->DatabaseDirectory().Append(base::FilePath::FromUTF16Unsafe(
359 tracker->GetOriginDirectory(kOrigin2)))));
360 EXPECT_EQ(1, base::WriteFile(
361 tracker->GetFullDBFilePath(kOrigin1, kDB1), "a", 1));
362 EXPECT_EQ(2, base::WriteFile(
363 tracker->GetFullDBFilePath(kOrigin2, kDB2), "aa", 2));
364 EXPECT_EQ(4, base::WriteFile(
365 tracker->GetFullDBFilePath(kOrigin1, kDB3), "aaaa", 4));
366 tracker->DatabaseModified(kOrigin1, kDB1);
367 CheckNotificationReceived(&observer1, kOrigin1, kDB1, 1);
368 CheckNotificationReceived(&observer2, kOrigin1, kDB1, 1);
369 tracker->DatabaseModified(kOrigin2, kDB2);
370 CheckNotificationReceived(&observer1, kOrigin2, kDB2, 2);
371 CheckNotificationReceived(&observer2, kOrigin2, kDB2, 2);
372 tracker->DatabaseModified(kOrigin1, kDB3);
373 CheckNotificationReceived(&observer1, kOrigin1, kDB3, 4);
374 CheckNotificationReceived(&observer2, kOrigin1, kDB3, 4);
376 // Close all databases
377 tracker->DatabaseClosed(kOrigin1, kDB1);
378 tracker->DatabaseClosed(kOrigin2, kDB2);
379 tracker->DatabaseClosed(kOrigin1, kDB3);
381 // Open an existing database and check the reported size
382 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
383 &database_size);
384 EXPECT_EQ(1, database_size);
385 tracker->DatabaseClosed(kOrigin1, kDB1);
387 // Remove an observer; this should clear all caches.
388 tracker->RemoveObserver(&observer2);
390 // Close the tracker database and clear all caches.
391 // Then make sure that DatabaseOpened() still returns the correct result.
392 tracker->CloseTrackerDatabaseAndClearCaches();
393 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
394 &database_size);
395 EXPECT_EQ(1, database_size);
396 tracker->DatabaseClosed(kOrigin1, kDB1);
398 // Remove all observers.
399 tracker->RemoveObserver(&observer1);
401 // Trying to delete a database in use should fail
402 tracker->DatabaseOpened(kOrigin1, kDB3, kDescription, 0,
403 &database_size);
404 EXPECT_FALSE(tracker->DeleteClosedDatabase(kOrigin1, kDB3));
405 origin1_info = tracker->GetCachedOriginInfo(kOrigin1);
406 EXPECT_TRUE(origin1_info);
407 EXPECT_EQ(4, origin1_info->GetDatabaseSize(kDB3));
408 tracker->DatabaseClosed(kOrigin1, kDB3);
410 // Delete a database and make sure the space used by that origin is updated
411 EXPECT_TRUE(tracker->DeleteClosedDatabase(kOrigin1, kDB3));
412 origin1_info = tracker->GetCachedOriginInfo(kOrigin1);
413 EXPECT_TRUE(origin1_info);
414 EXPECT_EQ(1, origin1_info->GetDatabaseSize(kDB1));
415 EXPECT_EQ(0, origin1_info->GetDatabaseSize(kDB3));
417 // Get all data for all origins
418 std::vector<OriginInfo> origins_info;
419 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info));
420 EXPECT_EQ(size_t(2), origins_info.size());
421 EXPECT_EQ(kOrigin1, origins_info[0].GetOriginIdentifier());
422 EXPECT_EQ(1, origins_info[0].TotalSize());
423 EXPECT_EQ(1, origins_info[0].GetDatabaseSize(kDB1));
424 EXPECT_EQ(0, origins_info[0].GetDatabaseSize(kDB3));
426 EXPECT_EQ(kOrigin2, origins_info[1].GetOriginIdentifier());
427 EXPECT_EQ(2, origins_info[1].TotalSize());
429 // Trying to delete an origin with databases in use should fail
430 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
431 &database_size);
432 EXPECT_FALSE(tracker->DeleteOrigin(kOrigin1, false));
433 origin1_info = tracker->GetCachedOriginInfo(kOrigin1);
434 EXPECT_TRUE(origin1_info);
435 EXPECT_EQ(1, origin1_info->GetDatabaseSize(kDB1));
436 tracker->DatabaseClosed(kOrigin1, kDB1);
438 // Delete an origin that doesn't have any database in use
439 EXPECT_TRUE(tracker->DeleteOrigin(kOrigin1, false));
440 origins_info.clear();
441 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info));
442 EXPECT_EQ(size_t(1), origins_info.size());
443 EXPECT_EQ(kOrigin2, origins_info[0].GetOriginIdentifier());
445 origin1_info = tracker->GetCachedOriginInfo(kOrigin1);
446 EXPECT_TRUE(origin1_info);
447 EXPECT_EQ(0, origin1_info->TotalSize());
450 static void DatabaseTrackerQuotaIntegration() {
451 const GURL kOrigin(kOrigin1Url);
452 const std::string kOriginId = storage::GetIdentifierFromOrigin(kOrigin);
453 const base::string16 kName = ASCIIToUTF16("name");
454 const base::string16 kDescription = ASCIIToUTF16("description");
456 base::ScopedTempDir temp_dir;
457 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
459 // Initialize the tracker with a QuotaManagerProxy
460 scoped_refptr<TestQuotaManagerProxy> test_quota_proxy(
461 new TestQuotaManagerProxy);
462 scoped_refptr<DatabaseTracker> tracker(
463 new DatabaseTracker(temp_dir.path(),
464 false /* incognito */,
465 NULL,
466 test_quota_proxy.get(),
467 NULL));
468 EXPECT_TRUE(test_quota_proxy->registered_client_);
470 // Create a database and modify it a couple of times, close it,
471 // then delete it. Observe the tracker notifies accordingly.
473 int64 database_size = 0;
474 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0,
475 &database_size);
476 EXPECT_TRUE(test_quota_proxy->WasAccessNotified(kOrigin));
477 test_quota_proxy->reset();
479 base::FilePath db_file(tracker->GetFullDBFilePath(kOriginId, kName));
480 EXPECT_TRUE(base::CreateDirectory(db_file.DirName()));
481 EXPECT_TRUE(EnsureFileOfSize(db_file, 10));
482 tracker->DatabaseModified(kOriginId, kName);
483 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, 10));
484 test_quota_proxy->reset();
486 EXPECT_TRUE(EnsureFileOfSize(db_file, 100));
487 tracker->DatabaseModified(kOriginId, kName);
488 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, 90));
489 test_quota_proxy->reset();
491 tracker->DatabaseClosed(kOriginId, kName);
492 EXPECT_TRUE(test_quota_proxy->WasAccessNotified(kOrigin));
493 EXPECT_EQ(net::OK, tracker->DeleteDatabase(
494 kOriginId, kName, net::CompletionCallback()));
495 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, -100));
496 test_quota_proxy->reset();
498 // Create a database and modify it, try to delete it while open,
499 // then close it (at which time deletion will actually occur).
500 // Observe the tracker notifies accordingly.
502 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0,
503 &database_size);
504 EXPECT_TRUE(test_quota_proxy->WasAccessNotified(kOrigin));
505 test_quota_proxy->reset();
507 db_file = tracker->GetFullDBFilePath(kOriginId, kName);
508 EXPECT_TRUE(base::CreateDirectory(db_file.DirName()));
509 EXPECT_TRUE(EnsureFileOfSize(db_file, 100));
510 tracker->DatabaseModified(kOriginId, kName);
511 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, 100));
512 test_quota_proxy->reset();
514 EXPECT_EQ(net::ERR_IO_PENDING,
515 tracker->DeleteDatabase(kOriginId, kName,
516 net::CompletionCallback()));
517 EXPECT_FALSE(test_quota_proxy->WasModificationNotified(kOrigin, -100));
519 tracker->DatabaseClosed(kOriginId, kName);
520 EXPECT_TRUE(test_quota_proxy->WasAccessNotified(kOrigin));
521 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, -100));
522 test_quota_proxy->reset();
524 // Create a database and up the file size without telling
525 // the tracker about the modification, than simulate a
526 // a renderer crash.
527 // Observe the tracker notifies accordingly.
529 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0,
530 &database_size);
531 EXPECT_TRUE(test_quota_proxy->WasAccessNotified(kOrigin));
532 test_quota_proxy->reset();
533 db_file = tracker->GetFullDBFilePath(kOriginId, kName);
534 EXPECT_TRUE(base::CreateDirectory(db_file.DirName()));
535 EXPECT_TRUE(EnsureFileOfSize(db_file, 100));
536 DatabaseConnections crashed_renderer_connections;
537 crashed_renderer_connections.AddConnection(kOriginId, kName);
538 EXPECT_FALSE(test_quota_proxy->WasModificationNotified(kOrigin, 100));
539 tracker->CloseDatabases(crashed_renderer_connections);
540 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, 100));
542 // Cleanup.
543 crashed_renderer_connections.RemoveAllConnections();
544 test_quota_proxy->SimulateQuotaManagerDestroyed();
547 static void DatabaseTrackerClearSessionOnlyDatabasesOnExit() {
548 int64 database_size = 0;
549 const std::string kOrigin1 =
550 storage::GetIdentifierFromOrigin(GURL(kOrigin1Url));
551 const std::string kOrigin2 =
552 storage::GetIdentifierFromOrigin(GURL(kOrigin2Url));
553 const base::string16 kDB1 = ASCIIToUTF16("db1");
554 const base::string16 kDB2 = ASCIIToUTF16("db2");
555 const base::string16 kDescription = ASCIIToUTF16("database_description");
557 // Initialize the tracker database.
558 base::MessageLoop message_loop;
559 base::ScopedTempDir temp_dir;
560 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
561 base::FilePath origin1_db_dir;
562 base::FilePath origin2_db_dir;
564 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy =
565 new MockSpecialStoragePolicy;
566 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url));
567 scoped_refptr<DatabaseTracker> tracker(
568 new DatabaseTracker(temp_dir.path(),
569 false,
570 special_storage_policy.get(),
571 NULL,
572 base::MessageLoopProxy::current().get()));
574 // Open two new databases.
575 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
576 &database_size);
577 EXPECT_EQ(0, database_size);
578 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0,
579 &database_size);
580 EXPECT_EQ(0, database_size);
582 // Write some data to each file.
583 base::FilePath db_file;
584 db_file = tracker->GetFullDBFilePath(kOrigin1, kDB1);
585 EXPECT_TRUE(base::CreateDirectory(db_file.DirName()));
586 EXPECT_TRUE(EnsureFileOfSize(db_file, 1));
588 db_file = tracker->GetFullDBFilePath(kOrigin2, kDB2);
589 EXPECT_TRUE(base::CreateDirectory(db_file.DirName()));
590 EXPECT_TRUE(EnsureFileOfSize(db_file, 2));
592 // Store the origin database directories as long as they still exist.
593 origin1_db_dir = tracker->GetFullDBFilePath(kOrigin1, kDB1).DirName();
594 origin2_db_dir = tracker->GetFullDBFilePath(kOrigin2, kDB2).DirName();
596 tracker->DatabaseModified(kOrigin1, kDB1);
597 tracker->DatabaseModified(kOrigin2, kDB2);
599 // Close all databases.
600 tracker->DatabaseClosed(kOrigin1, kDB1);
601 tracker->DatabaseClosed(kOrigin2, kDB2);
603 tracker->Shutdown();
606 // At this point, the database tracker should be gone. Create a new one.
607 scoped_refptr<DatabaseTracker> tracker(
608 new DatabaseTracker(temp_dir.path(), false, NULL, NULL, NULL));
610 // Get all data for all origins.
611 std::vector<OriginInfo> origins_info;
612 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info));
613 // kOrigin1 was not session-only, so it survived. kOrigin2 was session-only
614 // and it got deleted.
615 EXPECT_EQ(size_t(1), origins_info.size());
616 EXPECT_EQ(kOrigin1, origins_info[0].GetOriginIdentifier());
617 EXPECT_TRUE(
618 base::PathExists(tracker->GetFullDBFilePath(kOrigin1, kDB1)));
619 EXPECT_EQ(base::FilePath(), tracker->GetFullDBFilePath(kOrigin2, kDB2));
621 // The origin directory of kOrigin1 remains, but the origin directory of
622 // kOrigin2 is deleted.
623 EXPECT_TRUE(base::PathExists(origin1_db_dir));
624 EXPECT_FALSE(base::PathExists(origin2_db_dir));
627 static void DatabaseTrackerSetForceKeepSessionState() {
628 int64 database_size = 0;
629 const std::string kOrigin1 =
630 storage::GetIdentifierFromOrigin(GURL(kOrigin1Url));
631 const std::string kOrigin2 =
632 storage::GetIdentifierFromOrigin(GURL(kOrigin2Url));
633 const base::string16 kDB1 = ASCIIToUTF16("db1");
634 const base::string16 kDB2 = ASCIIToUTF16("db2");
635 const base::string16 kDescription = ASCIIToUTF16("database_description");
637 // Initialize the tracker database.
638 base::MessageLoop message_loop;
639 base::ScopedTempDir temp_dir;
640 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
641 base::FilePath origin1_db_dir;
642 base::FilePath origin2_db_dir;
644 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy =
645 new MockSpecialStoragePolicy;
646 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url));
647 scoped_refptr<DatabaseTracker> tracker(
648 new DatabaseTracker(temp_dir.path(),
649 false,
650 special_storage_policy.get(),
651 NULL,
652 base::MessageLoopProxy::current().get()));
653 tracker->SetForceKeepSessionState();
655 // Open two new databases.
656 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
657 &database_size);
658 EXPECT_EQ(0, database_size);
659 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0,
660 &database_size);
661 EXPECT_EQ(0, database_size);
663 // Write some data to each file.
664 base::FilePath db_file;
665 db_file = tracker->GetFullDBFilePath(kOrigin1, kDB1);
666 EXPECT_TRUE(base::CreateDirectory(db_file.DirName()));
667 EXPECT_TRUE(EnsureFileOfSize(db_file, 1));
669 db_file = tracker->GetFullDBFilePath(kOrigin2, kDB2);
670 EXPECT_TRUE(base::CreateDirectory(db_file.DirName()));
671 EXPECT_TRUE(EnsureFileOfSize(db_file, 2));
673 // Store the origin database directories as long as they still exist.
674 origin1_db_dir = tracker->GetFullDBFilePath(kOrigin1, kDB1).DirName();
675 origin2_db_dir = tracker->GetFullDBFilePath(kOrigin2, kDB2).DirName();
677 tracker->DatabaseModified(kOrigin1, kDB1);
678 tracker->DatabaseModified(kOrigin2, kDB2);
680 // Close all databases.
681 tracker->DatabaseClosed(kOrigin1, kDB1);
682 tracker->DatabaseClosed(kOrigin2, kDB2);
684 tracker->Shutdown();
687 // At this point, the database tracker should be gone. Create a new one.
688 scoped_refptr<DatabaseTracker> tracker(
689 new DatabaseTracker(temp_dir.path(), false, NULL, NULL, NULL));
691 // Get all data for all origins.
692 std::vector<OriginInfo> origins_info;
693 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info));
694 // No origins were deleted.
695 EXPECT_EQ(size_t(2), origins_info.size());
696 EXPECT_TRUE(
697 base::PathExists(tracker->GetFullDBFilePath(kOrigin1, kDB1)));
698 EXPECT_TRUE(
699 base::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB2)));
701 EXPECT_TRUE(base::PathExists(origin1_db_dir));
702 EXPECT_TRUE(base::PathExists(origin2_db_dir));
705 static void EmptyDatabaseNameIsValid() {
706 const GURL kOrigin(kOrigin1Url);
707 const std::string kOriginId = storage::GetIdentifierFromOrigin(kOrigin);
708 const base::string16 kEmptyName;
709 const base::string16 kDescription(ASCIIToUTF16("description"));
710 const base::string16 kChangedDescription(
711 ASCIIToUTF16("changed_description"));
713 // Initialize a tracker database, no need to put it on disk.
714 const bool kUseInMemoryTrackerDatabase = true;
715 base::ScopedTempDir temp_dir;
716 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
717 scoped_refptr<DatabaseTracker> tracker(
718 new DatabaseTracker(temp_dir.path(), kUseInMemoryTrackerDatabase,
719 NULL, NULL, NULL));
721 // Starts off with no databases.
722 std::vector<OriginInfo> infos;
723 EXPECT_TRUE(tracker->GetAllOriginsInfo(&infos));
724 EXPECT_TRUE(infos.empty());
726 // Create a db with an empty name.
727 int64 database_size = -1;
728 tracker->DatabaseOpened(kOriginId, kEmptyName, kDescription, 0,
729 &database_size);
730 EXPECT_EQ(0, database_size);
731 tracker->DatabaseModified(kOriginId, kEmptyName);
732 EXPECT_TRUE(tracker->GetAllOriginsInfo(&infos));
733 EXPECT_EQ(1u, infos.size());
734 EXPECT_EQ(kDescription, infos[0].GetDatabaseDescription(kEmptyName));
735 EXPECT_FALSE(tracker->GetFullDBFilePath(kOriginId, kEmptyName).empty());
736 tracker->DatabaseOpened(kOriginId, kEmptyName, kChangedDescription, 0,
737 &database_size);
738 infos.clear();
739 EXPECT_TRUE(tracker->GetAllOriginsInfo(&infos));
740 EXPECT_EQ(1u, infos.size());
741 EXPECT_EQ(kChangedDescription, infos[0].GetDatabaseDescription(kEmptyName));
742 tracker->DatabaseClosed(kOriginId, kEmptyName);
743 tracker->DatabaseClosed(kOriginId, kEmptyName);
745 // Deleting it should return to the initial state.
746 EXPECT_EQ(net::OK, tracker->DeleteDatabase(kOriginId, kEmptyName,
747 net::CompletionCallback()));
748 infos.clear();
749 EXPECT_TRUE(tracker->GetAllOriginsInfo(&infos));
750 EXPECT_TRUE(infos.empty());
753 static void HandleSqliteError() {
754 const GURL kOrigin(kOrigin1Url);
755 const std::string kOriginId = storage::GetIdentifierFromOrigin(kOrigin);
756 const base::string16 kName(ASCIIToUTF16("name"));
757 const base::string16 kDescription(ASCIIToUTF16("description"));
759 // Initialize a tracker database, no need to put it on disk.
760 const bool kUseInMemoryTrackerDatabase = true;
761 base::ScopedTempDir temp_dir;
762 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
763 scoped_refptr<DatabaseTracker> tracker(
764 new DatabaseTracker(temp_dir.path(), kUseInMemoryTrackerDatabase,
765 NULL, NULL, NULL));
767 // Setup to observe OnScheduledForDelete notifications.
768 TestObserver observer(false, true);
769 tracker->AddObserver(&observer);
771 // Verify does no harm when there is no such database.
772 tracker->HandleSqliteError(kOriginId, kName, SQLITE_CORRUPT);
773 EXPECT_FALSE(tracker->IsDatabaseScheduledForDeletion(kOriginId, kName));
774 EXPECT_FALSE(observer.DidReceiveNewNotification());
776 // --------------------------------------------------------
777 // Create a record of a database in the tracker db and create
778 // a spoof_db_file on disk in the expected location.
779 int64 database_size = 0;
780 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0,
781 &database_size);
782 base::FilePath spoof_db_file = tracker->GetFullDBFilePath(kOriginId, kName);
783 EXPECT_FALSE(tracker->GetFullDBFilePath(kOriginId, kName).empty());
784 EXPECT_TRUE(base::CreateDirectory(spoof_db_file.DirName()));
785 EXPECT_TRUE(EnsureFileOfSize(spoof_db_file, 1));
787 // Verify does no harm with a non-error is reported.
788 tracker->HandleSqliteError(kOriginId, kName, SQLITE_OK);
789 EXPECT_FALSE(tracker->IsDatabaseScheduledForDeletion(kOriginId, kName));
790 EXPECT_FALSE(observer.DidReceiveNewNotification());
792 // Verify that with a connection open, the db is scheduled for deletion,
793 // but that the file still exists.
794 tracker->HandleSqliteError(kOriginId, kName, SQLITE_CORRUPT);
795 EXPECT_TRUE(tracker->IsDatabaseScheduledForDeletion(kOriginId, kName));
796 EXPECT_TRUE(observer.DidReceiveNewNotification());
797 EXPECT_TRUE(base::PathExists(spoof_db_file));
799 // Verify that once closed, the file is deleted and the record in the
800 // tracker db is removed.
801 tracker->DatabaseClosed(kOriginId, kName);
802 EXPECT_FALSE(base::PathExists(spoof_db_file));
803 EXPECT_TRUE(tracker->GetFullDBFilePath(kOriginId, kName).empty());
805 // --------------------------------------------------------
806 // Create another record of a database in the tracker db and create
807 // a spoof_db_file on disk in the expected location.
808 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0,
809 &database_size);
810 base::FilePath spoof_db_file2 = tracker->GetFullDBFilePath(kOriginId,
811 kName);
812 EXPECT_FALSE(tracker->GetFullDBFilePath(kOriginId, kName).empty());
813 EXPECT_NE(spoof_db_file, spoof_db_file2);
814 EXPECT_TRUE(base::CreateDirectory(spoof_db_file2.DirName()));
815 EXPECT_TRUE(EnsureFileOfSize(spoof_db_file2, 1));
817 // Verify that with no connection open, the db is deleted immediately.
818 tracker->DatabaseClosed(kOriginId, kName);
819 tracker->HandleSqliteError(kOriginId, kName, SQLITE_CORRUPT);
820 EXPECT_FALSE(tracker->IsDatabaseScheduledForDeletion(kOriginId, kName));
821 EXPECT_FALSE(observer.DidReceiveNewNotification());
822 EXPECT_TRUE(tracker->GetFullDBFilePath(kOriginId, kName).empty());
823 EXPECT_FALSE(base::PathExists(spoof_db_file2));
825 tracker->RemoveObserver(&observer);
829 TEST(DatabaseTrackerTest, DeleteOpenDatabase) {
830 DatabaseTracker_TestHelper_Test::TestDeleteOpenDatabase(false);
833 TEST(DatabaseTrackerTest, DeleteOpenDatabaseIncognitoMode) {
834 DatabaseTracker_TestHelper_Test::TestDeleteOpenDatabase(true);
837 TEST(DatabaseTrackerTest, DatabaseTracker) {
838 DatabaseTracker_TestHelper_Test::TestDatabaseTracker(false);
841 TEST(DatabaseTrackerTest, DatabaseTrackerIncognitoMode) {
842 DatabaseTracker_TestHelper_Test::TestDatabaseTracker(true);
845 TEST(DatabaseTrackerTest, DatabaseTrackerQuotaIntegration) {
846 // There is no difference in behavior between incognito and not.
847 DatabaseTracker_TestHelper_Test::DatabaseTrackerQuotaIntegration();
850 TEST(DatabaseTrackerTest, DatabaseTrackerClearSessionOnlyDatabasesOnExit) {
851 // Only works for regular mode.
852 DatabaseTracker_TestHelper_Test::
853 DatabaseTrackerClearSessionOnlyDatabasesOnExit();
856 TEST(DatabaseTrackerTest, DatabaseTrackerSetForceKeepSessionState) {
857 // Only works for regular mode.
858 DatabaseTracker_TestHelper_Test::DatabaseTrackerSetForceKeepSessionState();
861 TEST(DatabaseTrackerTest, EmptyDatabaseNameIsValid) {
862 DatabaseTracker_TestHelper_Test::EmptyDatabaseNameIsValid();
865 TEST(DatabaseTrackerTest, HandleSqliteError) {
866 DatabaseTracker_TestHelper_Test::HandleSqliteError();
869 } // namespace content