OLD | NEW |
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/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "components/leveldb/public/cpp/util.h" | 8 #include "components/leveldb/public/cpp/util.h" |
9 #include "content/public/test/test_browser_thread_bundle.h" | 9 #include "content/public/test/test_browser_thread_bundle.h" |
10 #include "content/test/mock_leveldb_database.h" | 10 #include "content/test/mock_leveldb_database.h" |
11 #include "mojo/public/cpp/bindings/binding.h" | 11 #include "mojo/public/cpp/bindings/binding.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using leveldb::StdStringToUint8Vector; | 14 using leveldb::StdStringToUint8Vector; |
15 using leveldb::Uint8VectorToStdString; | 15 using leveldb::Uint8VectorToStdString; |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 void NoOpSuccess(bool success) {} | 21 void NoOpSuccess(bool success) {} |
22 | 22 |
| 23 void GetCallback(const base::Closure& callback, |
| 24 bool* success_out, |
| 25 std::vector<uint8_t>* value_out, |
| 26 bool success, |
| 27 const std::vector<uint8_t>& value) { |
| 28 *success_out = success; |
| 29 *value_out = value; |
| 30 callback.Run(); |
| 31 } |
| 32 |
23 } // namespace | 33 } // namespace |
24 | 34 |
25 class LocalStorageContextMojoTest : public testing::Test { | 35 class LocalStorageContextMojoTest : public testing::Test { |
26 public: | 36 public: |
27 LocalStorageContextMojoTest() | 37 LocalStorageContextMojoTest() : db_(&mock_data_), db_binding_(&db_) {} |
28 : db_(&mock_data_), | 38 |
29 db_binding_(&db_), | 39 LocalStorageContextMojo* context() { |
30 context_(nullptr, base::FilePath()) { | 40 if (!context_) { |
31 context_.SetDatabaseForTesting(db_binding_.CreateInterfacePtrAndBind()); | 41 context_ = |
| 42 base::MakeUnique<LocalStorageContextMojo>(nullptr, base::FilePath()); |
| 43 context_->SetDatabaseForTesting(db_binding_.CreateInterfacePtrAndBind()); |
| 44 } |
| 45 return context_.get(); |
| 46 } |
| 47 const std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data() { |
| 48 return mock_data_; |
32 } | 49 } |
33 | 50 |
34 LocalStorageContextMojo* context() { return &context_; } | 51 void set_mock_data(const std::string& key, const std::string& value) { |
35 const std::map<std::vector<uint8_t>, std::vector<uint8_t>>& mock_data() { | 52 mock_data_[StdStringToUint8Vector(key)] = StdStringToUint8Vector(value); |
36 return mock_data_; | |
37 } | 53 } |
38 | 54 |
39 private: | 55 private: |
40 TestBrowserThreadBundle thread_bundle_; | 56 TestBrowserThreadBundle thread_bundle_; |
41 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_; | 57 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_; |
42 MockLevelDBDatabase db_; | 58 MockLevelDBDatabase db_; |
43 mojo::Binding<leveldb::mojom::LevelDBDatabase> db_binding_; | 59 mojo::Binding<leveldb::mojom::LevelDBDatabase> db_binding_; |
44 | 60 |
45 LocalStorageContextMojo context_; | 61 std::unique_ptr<LocalStorageContextMojo> context_; |
46 }; | 62 }; |
47 | 63 |
48 TEST_F(LocalStorageContextMojoTest, Basic) { | 64 TEST_F(LocalStorageContextMojoTest, Basic) { |
49 auto key = StdStringToUint8Vector("key"); | 65 auto key = StdStringToUint8Vector("key"); |
50 auto value = StdStringToUint8Vector("value"); | 66 auto value = StdStringToUint8Vector("value"); |
51 | 67 |
52 mojom::LevelDBWrapperPtr wrapper; | 68 mojom::LevelDBWrapperPtr wrapper; |
53 context()->OpenLocalStorage(url::Origin(GURL("http://foobar.com")), | 69 context()->OpenLocalStorage(url::Origin(GURL("http://foobar.com")), |
54 MakeRequest(&wrapper)); | 70 MakeRequest(&wrapper)); |
55 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess)); | 71 wrapper->Put(key, value, "source", base::Bind(&NoOpSuccess)); |
56 wrapper.reset(); | 72 wrapper.reset(); |
57 | 73 |
58 base::RunLoop().RunUntilIdle(); | 74 base::RunLoop().RunUntilIdle(); |
59 ASSERT_EQ(1u, mock_data().size()); | 75 ASSERT_EQ(2u, mock_data().size()); |
60 EXPECT_EQ(value, mock_data().begin()->second); | 76 EXPECT_EQ(value, mock_data().rbegin()->second); |
61 } | 77 } |
62 | 78 |
63 TEST_F(LocalStorageContextMojoTest, OriginsAreIndependent) { | 79 TEST_F(LocalStorageContextMojoTest, OriginsAreIndependent) { |
64 url::Origin origin1(GURL("http://foobar.com:123")); | 80 url::Origin origin1(GURL("http://foobar.com:123")); |
65 url::Origin origin2(GURL("http://foobar.com:1234")); | 81 url::Origin origin2(GURL("http://foobar.com:1234")); |
66 auto key1 = StdStringToUint8Vector("4key"); | 82 auto key1 = StdStringToUint8Vector("4key"); |
67 auto key2 = StdStringToUint8Vector("key"); | 83 auto key2 = StdStringToUint8Vector("key"); |
68 auto value = StdStringToUint8Vector("value"); | 84 auto value = StdStringToUint8Vector("value"); |
69 | 85 |
70 mojom::LevelDBWrapperPtr wrapper; | 86 mojom::LevelDBWrapperPtr wrapper; |
71 context()->OpenLocalStorage(origin1, MakeRequest(&wrapper)); | 87 context()->OpenLocalStorage(origin1, MakeRequest(&wrapper)); |
72 wrapper->Put(key1, value, "source", base::Bind(&NoOpSuccess)); | 88 wrapper->Put(key1, value, "source", base::Bind(&NoOpSuccess)); |
73 wrapper.reset(); | 89 wrapper.reset(); |
74 | 90 |
75 context()->OpenLocalStorage(origin2, MakeRequest(&wrapper)); | 91 context()->OpenLocalStorage(origin2, MakeRequest(&wrapper)); |
76 wrapper->Put(key2, value, "source", base::Bind(&NoOpSuccess)); | 92 wrapper->Put(key2, value, "source", base::Bind(&NoOpSuccess)); |
77 wrapper.reset(); | 93 wrapper.reset(); |
78 | 94 |
79 base::RunLoop().RunUntilIdle(); | 95 base::RunLoop().RunUntilIdle(); |
80 ASSERT_EQ(2u, mock_data().size()); | 96 ASSERT_EQ(3u, mock_data().size()); |
81 EXPECT_EQ(value, mock_data().begin()->second); | 97 EXPECT_EQ(value, mock_data().rbegin()->second); |
| 98 } |
| 99 |
| 100 TEST_F(LocalStorageContextMojoTest, ValidVersion) { |
| 101 set_mock_data("VERSION", "1"); |
| 102 set_mock_data(std::string("_http://foobar.com") + '\x00' + "key", "value"); |
| 103 |
| 104 mojom::LevelDBWrapperPtr wrapper; |
| 105 context()->OpenLocalStorage(url::Origin(GURL("http://foobar.com")), |
| 106 MakeRequest(&wrapper)); |
| 107 |
| 108 base::RunLoop run_loop; |
| 109 bool success = false; |
| 110 std::vector<uint8_t> result; |
| 111 wrapper->Get( |
| 112 StdStringToUint8Vector("key"), |
| 113 base::Bind(&GetCallback, run_loop.QuitClosure(), &success, &result)); |
| 114 run_loop.Run(); |
| 115 EXPECT_TRUE(success); |
| 116 EXPECT_EQ(StdStringToUint8Vector("value"), result); |
| 117 } |
| 118 |
| 119 TEST_F(LocalStorageContextMojoTest, InvalidVersion) { |
| 120 set_mock_data("VERSION", "foobar"); |
| 121 set_mock_data(std::string("_http://foobar.com") + '\x00' + "key", "value"); |
| 122 |
| 123 mojom::LevelDBWrapperPtr wrapper; |
| 124 context()->OpenLocalStorage(url::Origin(GURL("http://foobar.com")), |
| 125 MakeRequest(&wrapper)); |
| 126 |
| 127 base::RunLoop run_loop; |
| 128 bool success = false; |
| 129 std::vector<uint8_t> result; |
| 130 wrapper->Get( |
| 131 StdStringToUint8Vector("key"), |
| 132 base::Bind(&GetCallback, run_loop.QuitClosure(), &success, &result)); |
| 133 run_loop.Run(); |
| 134 EXPECT_FALSE(success); |
82 } | 135 } |
83 | 136 |
84 } // namespace content | 137 } // namespace content |
OLD | NEW |