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

Side by Side Diff: content/browser/dom_storage/local_storage_context_mojo_unittest.cc

Issue 2605133002: Add method to delete storage for origins to LocalStorageContextMojo. (Closed)
Patch Set: delete storage for physical origin Created 3 years, 11 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/dom_storage/local_storage_context_mojo.h" 5 #include "content/browser/dom_storage/local_storage_context_mojo.h"
6 6
7 #include "base/files/file_enumerator.h" 7 #include "base/files/file_enumerator.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "components/filesystem/public/interfaces/file_system.mojom.h" 10 #include "components/filesystem/public/interfaces/file_system.mojom.h"
11 #include "components/leveldb/public/cpp/util.h" 11 #include "components/leveldb/public/cpp/util.h"
12 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/local_storage_usage_info.h" 13 #include "content/public/browser/local_storage_usage_info.h"
14 #include "content/public/test/test_browser_thread_bundle.h" 14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "content/test/mock_leveldb_database.h" 15 #include "content/test/mock_leveldb_database.h"
16 #include "mojo/public/cpp/bindings/associated_binding.h"
16 #include "mojo/public/cpp/bindings/binding.h" 17 #include "mojo/public/cpp/bindings/binding.h"
17 #include "mojo/public/cpp/bindings/binding_set.h" 18 #include "mojo/public/cpp/bindings/binding_set.h"
18 #include "services/file/file_service.h" 19 #include "services/file/file_service.h"
19 #include "services/file/public/interfaces/constants.mojom.h" 20 #include "services/file/public/interfaces/constants.mojom.h"
20 #include "services/file/user_id_map.h" 21 #include "services/file/user_id_map.h"
21 #include "services/service_manager/public/cpp/interface_factory.h" 22 #include "services/service_manager/public/cpp/interface_factory.h"
22 #include "services/service_manager/public/cpp/interface_registry.h" 23 #include "services/service_manager/public/cpp/interface_registry.h"
23 #include "services/service_manager/public/cpp/service_context.h" 24 #include "services/service_manager/public/cpp/service_context.h"
24 #include "services/service_manager/public/cpp/service_test.h" 25 #include "services/service_manager/public/cpp/service_test.h"
25 #include "services/service_manager/public/interfaces/service_factory.mojom.h" 26 #include "services/service_manager/public/interfaces/service_factory.mojom.h"
(...skipping 18 matching lines...) Expand all
44 void GetCallback(const base::Closure& callback, 45 void GetCallback(const base::Closure& callback,
45 bool* success_out, 46 bool* success_out,
46 std::vector<uint8_t>* value_out, 47 std::vector<uint8_t>* value_out,
47 bool success, 48 bool success,
48 const std::vector<uint8_t>& value) { 49 const std::vector<uint8_t>& value) {
49 *success_out = success; 50 *success_out = success;
50 *value_out = value; 51 *value_out = value;
51 callback.Run(); 52 callback.Run();
52 } 53 }
53 54
55 class TestLevelDBObserver : public mojom::LevelDBObserver {
56 public:
57 struct Observation {
58 enum { kAdd, kChange, kDelete, kDeleteAll } type;
59 std::string key;
60 std::string old_value;
61 std::string new_value;
62 std::string source;
63 };
64
65 TestLevelDBObserver() : binding_(this) {}
66
67 mojom::LevelDBObserverAssociatedPtrInfo Bind(
68 mojo::AssociatedGroup* associated_group) {
69 mojom::LevelDBObserverAssociatedPtrInfo ptr_info;
70 binding_.Bind(&ptr_info, associated_group);
71 return ptr_info;
72 }
73
74 const std::vector<Observation>& observations() { return observations_; }
75
76 private:
77 void KeyAdded(const std::vector<uint8_t>& key,
78 const std::vector<uint8_t>& value,
79 const std::string& source) override {
80 observations_.push_back({Observation::kAdd, Uint8VectorToStdString(key), "",
81 Uint8VectorToStdString(value), source});
82 }
83 void KeyChanged(const std::vector<uint8_t>& key,
84 const std::vector<uint8_t>& new_value,
85 const std::vector<uint8_t>& old_value,
86 const std::string& source) override {
87 observations_.push_back({Observation::kChange, Uint8VectorToStdString(key),
88 Uint8VectorToStdString(old_value),
89 Uint8VectorToStdString(new_value), source});
90 }
91 void KeyDeleted(const std::vector<uint8_t>& key,
92 const std::vector<uint8_t>& old_value,
93 const std::string& source) override {
94 observations_.push_back({Observation::kDelete, Uint8VectorToStdString(key),
95 Uint8VectorToStdString(old_value), "", source});
96 }
97 void AllDeleted(const std::string& source) override {
98 observations_.push_back({Observation::kDeleteAll, "", "", "", source});
99 }
100
101 std::vector<Observation> observations_;
102 mojo::AssociatedBinding<mojom::LevelDBObserver> binding_;
103 };
104
54 } // namespace 105 } // namespace
55 106
56 class LocalStorageContextMojoTest : public testing::Test { 107 class LocalStorageContextMojoTest : public testing::Test {
57 public: 108 public:
58 LocalStorageContextMojoTest() : db_(&mock_data_), db_binding_(&db_) {} 109 LocalStorageContextMojoTest() : db_(&mock_data_), db_binding_(&db_) {}
59 110
60 LocalStorageContextMojo* context() { 111 LocalStorageContextMojo* context() {
61 if (!context_) { 112 if (!context_) {
62 context_ = 113 context_ =
63 base::MakeUnique<LocalStorageContextMojo>(nullptr, base::FilePath()); 114 base::MakeUnique<LocalStorageContextMojo>(nullptr, base::FilePath());
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 for (const auto& it : mock_data()) { 344 for (const auto& it : mock_data()) {
294 if (Uint8VectorToStdString(it.first) == "VERSION") 345 if (Uint8VectorToStdString(it.first) == "VERSION")
295 continue; 346 continue;
296 EXPECT_EQ(std::string::npos, 347 EXPECT_EQ(std::string::npos,
297 Uint8VectorToStdString(it.first).find(origin1.Serialize())); 348 Uint8VectorToStdString(it.first).find(origin1.Serialize()));
298 EXPECT_NE(std::string::npos, 349 EXPECT_NE(std::string::npos,
299 Uint8VectorToStdString(it.first).find(origin2.Serialize())); 350 Uint8VectorToStdString(it.first).find(origin2.Serialize()));
300 } 351 }
301 } 352 }
302 353
354 TEST_F(LocalStorageContextMojoTest, DeleteStorage) {
355 set_mock_data("VERSION", "1");
356 set_mock_data(std::string("_http://foobar.com") + '\x00' + "key", "value");
357
358 context()->DeleteStorage(url::Origin(GURL("http://foobar.com")));
359 base::RunLoop().RunUntilIdle();
360 EXPECT_EQ(1u, mock_data().size());
361 }
362
363 TEST_F(LocalStorageContextMojoTest, DeleteStorageWithoutConnection) {
364 url::Origin origin1(GURL("http://foobar.com"));
365 url::Origin origin2(GURL("http://example.com"));
366 auto key = StdStringToUint8Vector("key");
367 auto value = StdStringToUint8Vector("value");
368
369 mojom::LevelDBWrapperPtr wrapper;
370 context()->OpenLocalStorage(origin1, MakeRequest(&wrapper));
371 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess));
372 wrapper.reset();
373
374 context()->OpenLocalStorage(origin2, MakeRequest(&wrapper));
375 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess));
376 wrapper.reset();
377
378 // Make sure all data gets committed to disk.
379 base::RunLoop().RunUntilIdle();
380 EXPECT_FALSE(mock_data().empty());
381
382 context()->DeleteStorage(origin1);
383 base::RunLoop().RunUntilIdle();
384
385 // Data from origin2 should exist, including meta-data, but nothing should
386 // exist for origin1.
387 EXPECT_EQ(3u, mock_data().size());
388 for (const auto& it : mock_data()) {
389 if (Uint8VectorToStdString(it.first) == "VERSION")
390 continue;
391 EXPECT_EQ(std::string::npos,
392 Uint8VectorToStdString(it.first).find(origin1.Serialize()));
393 EXPECT_NE(std::string::npos,
394 Uint8VectorToStdString(it.first).find(origin2.Serialize()));
395 }
396 }
397
398 TEST_F(LocalStorageContextMojoTest, DeleteStorageNotifiesWrapper) {
399 url::Origin origin1(GURL("http://foobar.com"));
400 url::Origin origin2(GURL("http://example.com"));
401 auto key = StdStringToUint8Vector("key");
402 auto value = StdStringToUint8Vector("value");
403
404 mojom::LevelDBWrapperPtr wrapper;
405 context()->OpenLocalStorage(origin1, MakeRequest(&wrapper));
406 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess));
407 wrapper.reset();
408
409 context()->OpenLocalStorage(origin2, MakeRequest(&wrapper));
410 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess));
411 wrapper.reset();
412
413 // Make sure all data gets committed to disk.
414 base::RunLoop().RunUntilIdle();
415 EXPECT_FALSE(mock_data().empty());
416
417 TestLevelDBObserver observer;
418 context()->OpenLocalStorage(origin1, MakeRequest(&wrapper));
419 wrapper->AddObserver(observer.Bind(wrapper.associated_group()));
420 base::RunLoop().RunUntilIdle();
421
422 context()->DeleteStorage(origin1);
423 base::RunLoop().RunUntilIdle();
424
425 ASSERT_EQ(1u, observer.observations().size());
426 EXPECT_EQ(TestLevelDBObserver::Observation::kDeleteAll,
427 observer.observations()[0].type);
428
429 // Data from origin2 should exist, including meta-data, but nothing should
430 // exist for origin1.
431 EXPECT_EQ(3u, mock_data().size());
432 for (const auto& it : mock_data()) {
433 if (Uint8VectorToStdString(it.first) == "VERSION")
434 continue;
435 EXPECT_EQ(std::string::npos,
436 Uint8VectorToStdString(it.first).find(origin1.Serialize()));
437 EXPECT_NE(std::string::npos,
438 Uint8VectorToStdString(it.first).find(origin2.Serialize()));
439 }
440 }
441
442 TEST_F(LocalStorageContextMojoTest, DeleteStorageWithPendingWrites) {
443 url::Origin origin1(GURL("http://foobar.com"));
444 url::Origin origin2(GURL("http://example.com"));
445 auto key = StdStringToUint8Vector("key");
446 auto value = StdStringToUint8Vector("value");
447
448 mojom::LevelDBWrapperPtr wrapper;
449 context()->OpenLocalStorage(origin1, MakeRequest(&wrapper));
450 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess));
451 wrapper.reset();
452
453 context()->OpenLocalStorage(origin2, MakeRequest(&wrapper));
454 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess));
455 wrapper.reset();
456
457 // Make sure all data gets committed to disk.
458 base::RunLoop().RunUntilIdle();
459 EXPECT_FALSE(mock_data().empty());
460
461 TestLevelDBObserver observer;
462 context()->OpenLocalStorage(origin1, MakeRequest(&wrapper));
463 wrapper->AddObserver(observer.Bind(wrapper.associated_group()));
464 wrapper->Put(StdStringToUint8Vector("key2"), value, "source",
465 base::Bind(&NoOpSuccess));
466 base::RunLoop().RunUntilIdle();
467
468 context()->DeleteStorage(origin1);
469 base::RunLoop().RunUntilIdle();
470
471 ASSERT_EQ(2u, observer.observations().size());
472 EXPECT_EQ(TestLevelDBObserver::Observation::kAdd,
473 observer.observations()[0].type);
474 EXPECT_EQ(TestLevelDBObserver::Observation::kDeleteAll,
475 observer.observations()[1].type);
476
477 // Data from origin2 should exist, including meta-data, but nothing should
478 // exist for origin1.
479 EXPECT_EQ(3u, mock_data().size());
480 for (const auto& it : mock_data()) {
481 if (Uint8VectorToStdString(it.first) == "VERSION")
482 continue;
483 EXPECT_EQ(std::string::npos,
484 Uint8VectorToStdString(it.first).find(origin1.Serialize()));
485 EXPECT_NE(std::string::npos,
486 Uint8VectorToStdString(it.first).find(origin2.Serialize()));
487 }
488 }
489
490 TEST_F(LocalStorageContextMojoTest, DeleteStorageForPhysicalOrigin) {
491 url::Origin origin1a(GURL("http://foobar.com"));
492 url::Origin origin1b(GURL("http-so://suborigin.foobar.com"));
493 url::Origin origin2(GURL("https://foobar.com"));
494 auto key = StdStringToUint8Vector("key");
495 auto value = StdStringToUint8Vector("value");
496
497 mojom::LevelDBWrapperPtr wrapper;
498 context()->OpenLocalStorage(origin1a, MakeRequest(&wrapper));
499 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess));
500 wrapper.reset();
501 context()->OpenLocalStorage(origin1b, MakeRequest(&wrapper));
502 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess));
503 wrapper.reset();
504
505 context()->OpenLocalStorage(origin2, MakeRequest(&wrapper));
506 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess));
507 wrapper.reset();
508
509 // Make sure all data gets committed to disk.
510 base::RunLoop().RunUntilIdle();
511 EXPECT_FALSE(mock_data().empty());
512
513 context()->DeleteStorageForPhysicalOrigin(origin1b);
514 base::RunLoop().RunUntilIdle();
515
516 // Data from origin2 should exist, including meta-data, but nothing should
517 // exist for origin1.
518 EXPECT_EQ(3u, mock_data().size());
519 for (const auto& it : mock_data()) {
520 if (Uint8VectorToStdString(it.first) == "VERSION")
521 continue;
522 EXPECT_EQ(std::string::npos,
523 Uint8VectorToStdString(it.first).find(origin1a.Serialize()));
524 EXPECT_EQ(std::string::npos,
525 Uint8VectorToStdString(it.first).find(origin1b.Serialize()));
526 EXPECT_NE(std::string::npos,
527 Uint8VectorToStdString(it.first).find(origin2.Serialize()));
528 }
529 }
530
303 namespace { 531 namespace {
304 532
305 class ServiceTestClient : public service_manager::test::ServiceTestClient, 533 class ServiceTestClient : public service_manager::test::ServiceTestClient,
306 public service_manager::mojom::ServiceFactory, 534 public service_manager::mojom::ServiceFactory,
307 public service_manager::InterfaceFactory< 535 public service_manager::InterfaceFactory<
308 service_manager::mojom::ServiceFactory> { 536 service_manager::mojom::ServiceFactory> {
309 public: 537 public:
310 explicit ServiceTestClient(service_manager::test::ServiceTest* test) 538 explicit ServiceTestClient(service_manager::test::ServiceTest* test)
311 : service_manager::test::ServiceTestClient(test) {} 539 : service_manager::test::ServiceTestClient(test) {}
312 ~ServiceTestClient() override {} 540 ~ServiceTestClient() override {}
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 // Should have created files. 726 // Should have created files.
499 EXPECT_EQ(test_path, FirstEntryInDir().BaseName()); 727 EXPECT_EQ(test_path, FirstEntryInDir().BaseName());
500 728
501 // Should be able to re-open. 729 // Should be able to re-open.
502 context.reset(new LocalStorageContextMojo(connector(), test_path)); 730 context.reset(new LocalStorageContextMojo(connector(), test_path));
503 EXPECT_TRUE(DoTestGet(context.get(), key, &result)); 731 EXPECT_TRUE(DoTestGet(context.get(), key, &result));
504 EXPECT_EQ(value, result); 732 EXPECT_EQ(value, result);
505 } 733 }
506 734
507 } // namespace content 735 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/dom_storage/local_storage_context_mojo.cc ('k') | content/browser/leveldb_wrapper_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698