OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <vector> |
| 6 |
5 #include "base/bind.h" | 7 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
8 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
10 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
12 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
13 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
14 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
15 #include "base/time/time.h" | 17 #include "base/time/time.h" |
16 #include "chrome/browser/net/quota_policy_channel_id_store.h" | 18 #include "chrome/browser/net/quota_policy_channel_id_store.h" |
17 #include "content/public/test/mock_special_storage_policy.h" | 19 #include "content/public/test/mock_special_storage_policy.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "net/base/test_data_directory.h" | 21 #include "net/base/test_data_directory.h" |
20 #include "net/cookies/cookie_util.h" | 22 #include "net/cookies/cookie_util.h" |
21 #include "net/ssl/ssl_client_cert_type.h" | 23 #include "net/ssl/ssl_client_cert_type.h" |
22 #include "net/test/cert_test_util.h" | 24 #include "net/test/cert_test_util.h" |
23 #include "net/test/channel_id_test_util.h" | 25 #include "net/test/channel_id_test_util.h" |
24 #include "sql/statement.h" | 26 #include "sql/statement.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
26 | 28 |
27 const base::FilePath::CharType kTestChannelIDFilename[] = | 29 const base::FilePath::CharType kTestChannelIDFilename[] = |
28 FILE_PATH_LITERAL("ChannelID"); | 30 FILE_PATH_LITERAL("ChannelID"); |
29 | 31 |
30 class QuotaPolicyChannelIDStoreTest : public testing::Test { | 32 class QuotaPolicyChannelIDStoreTest : public testing::Test { |
31 public: | 33 public: |
32 void Load(ScopedVector<net::DefaultChannelIDStore::ChannelID>* channel_ids) { | 34 void Load(std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>>* |
| 35 channel_ids) { |
33 base::RunLoop run_loop; | 36 base::RunLoop run_loop; |
34 store_->Load(base::Bind(&QuotaPolicyChannelIDStoreTest::OnLoaded, | 37 store_->Load(base::Bind(&QuotaPolicyChannelIDStoreTest::OnLoaded, |
35 base::Unretained(this), | 38 base::Unretained(this), |
36 &run_loop)); | 39 &run_loop)); |
37 run_loop.Run(); | 40 run_loop.Run(); |
38 channel_ids->swap(channel_ids_); | 41 channel_ids->swap(channel_ids_); |
39 channel_ids_.clear(); | 42 channel_ids_.clear(); |
40 } | 43 } |
41 | 44 |
42 void OnLoaded(base::RunLoop* run_loop, | 45 void OnLoaded( |
43 scoped_ptr<ScopedVector<net::DefaultChannelIDStore::ChannelID> > | 46 base::RunLoop* run_loop, |
44 channel_ids) { | 47 scoped_ptr<std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>>> |
| 48 channel_ids) { |
45 channel_ids_.swap(*channel_ids); | 49 channel_ids_.swap(*channel_ids); |
46 run_loop->Quit(); | 50 run_loop->Quit(); |
47 } | 51 } |
48 | 52 |
49 protected: | 53 protected: |
50 void SetUp() override { | 54 void SetUp() override { |
51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 55 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
52 store_ = new QuotaPolicyChannelIDStore( | 56 store_ = new QuotaPolicyChannelIDStore( |
53 temp_dir_.path().Append(kTestChannelIDFilename), | 57 temp_dir_.path().Append(kTestChannelIDFilename), |
54 base::ThreadTaskRunnerHandle::Get(), | 58 base::ThreadTaskRunnerHandle::Get(), |
55 NULL); | 59 NULL); |
56 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; | 60 std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>> channel_ids; |
57 Load(&channel_ids); | 61 Load(&channel_ids); |
58 ASSERT_EQ(0u, channel_ids.size()); | 62 ASSERT_EQ(0u, channel_ids.size()); |
59 } | 63 } |
60 | 64 |
61 void TearDown() override { | 65 void TearDown() override { |
62 store_ = NULL; | 66 store_ = NULL; |
63 loop_.RunUntilIdle(); | 67 loop_.RunUntilIdle(); |
64 } | 68 } |
65 | 69 |
66 base::ScopedTempDir temp_dir_; | 70 base::ScopedTempDir temp_dir_; |
67 scoped_refptr<QuotaPolicyChannelIDStore> store_; | 71 scoped_refptr<QuotaPolicyChannelIDStore> store_; |
68 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids_; | 72 std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>> channel_ids_; |
69 base::MessageLoop loop_; | 73 base::MessageLoop loop_; |
70 }; | 74 }; |
71 | 75 |
72 // Test if data is stored as expected in the QuotaPolicy database. | 76 // Test if data is stored as expected in the QuotaPolicy database. |
73 TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) { | 77 TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) { |
74 scoped_ptr<crypto::ECPrivateKey> goog_key(crypto::ECPrivateKey::Create()); | 78 scoped_ptr<crypto::ECPrivateKey> goog_key(crypto::ECPrivateKey::Create()); |
75 scoped_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); | 79 scoped_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); |
76 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 80 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
77 "google.com", base::Time::FromInternalValue(1), | 81 "google.com", base::Time::FromInternalValue(1), |
78 make_scoped_ptr(goog_key->Copy()))); | 82 make_scoped_ptr(goog_key->Copy()))); |
79 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 83 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
80 "foo.com", base::Time::FromInternalValue(3), | 84 "foo.com", base::Time::FromInternalValue(3), |
81 make_scoped_ptr(foo_key->Copy()))); | 85 make_scoped_ptr(foo_key->Copy()))); |
82 | 86 |
83 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; | 87 std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>> channel_ids; |
84 // Replace the store effectively destroying the current one and forcing it | 88 // Replace the store effectively destroying the current one and forcing it |
85 // to write its data to disk. Then we can see if after loading it again it | 89 // to write its data to disk. Then we can see if after loading it again it |
86 // is still there. | 90 // is still there. |
87 store_ = NULL; | 91 store_ = NULL; |
88 // Make sure we wait until the destructor has run. | 92 // Make sure we wait until the destructor has run. |
89 base::RunLoop().RunUntilIdle(); | 93 base::RunLoop().RunUntilIdle(); |
90 store_ = new QuotaPolicyChannelIDStore( | 94 store_ = new QuotaPolicyChannelIDStore( |
91 temp_dir_.path().Append(kTestChannelIDFilename), | 95 temp_dir_.path().Append(kTestChannelIDFilename), |
92 base::ThreadTaskRunnerHandle::Get(), | 96 base::ThreadTaskRunnerHandle::Get(), |
93 NULL); | 97 NULL); |
94 | 98 |
95 // Reload and test for persistence | 99 // Reload and test for persistence |
96 Load(&channel_ids); | 100 Load(&channel_ids); |
97 ASSERT_EQ(2U, channel_ids.size()); | 101 ASSERT_EQ(2U, channel_ids.size()); |
98 net::DefaultChannelIDStore::ChannelID* goog_channel_id; | 102 net::DefaultChannelIDStore::ChannelID* goog_channel_id; |
99 net::DefaultChannelIDStore::ChannelID* foo_channel_id; | 103 net::DefaultChannelIDStore::ChannelID* foo_channel_id; |
100 if (channel_ids[0]->server_identifier() == "google.com") { | 104 if (channel_ids[0]->server_identifier() == "google.com") { |
101 goog_channel_id = channel_ids[0]; | 105 goog_channel_id = channel_ids[0].get(); |
102 foo_channel_id = channel_ids[1]; | 106 foo_channel_id = channel_ids[1].get(); |
103 } else { | 107 } else { |
104 goog_channel_id = channel_ids[1]; | 108 goog_channel_id = channel_ids[1].get(); |
105 foo_channel_id = channel_ids[0]; | 109 foo_channel_id = channel_ids[0].get(); |
106 } | 110 } |
107 ASSERT_EQ("google.com", goog_channel_id->server_identifier()); | 111 ASSERT_EQ("google.com", goog_channel_id->server_identifier()); |
108 EXPECT_TRUE(net::KeysEqual(goog_key.get(), goog_channel_id->key())); | 112 EXPECT_TRUE(net::KeysEqual(goog_key.get(), goog_channel_id->key())); |
109 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue()); | 113 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue()); |
110 ASSERT_EQ("foo.com", foo_channel_id->server_identifier()); | 114 ASSERT_EQ("foo.com", foo_channel_id->server_identifier()); |
111 EXPECT_TRUE(net::KeysEqual(foo_key.get(), foo_channel_id->key())); | 115 EXPECT_TRUE(net::KeysEqual(foo_key.get(), foo_channel_id->key())); |
112 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue()); | 116 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue()); |
113 | 117 |
114 // Now delete the channel ID and check persistence again. | 118 // Now delete the channel ID and check persistence again. |
115 store_->DeleteChannelID(*channel_ids[0]); | 119 store_->DeleteChannelID(*channel_ids[0]); |
(...skipping 14 matching lines...) Expand all Loading... |
130 | 134 |
131 // Test if data is stored as expected in the QuotaPolicy database. | 135 // Test if data is stored as expected in the QuotaPolicy database. |
132 TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) { | 136 TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) { |
133 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 137 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
134 "google.com", base::Time::FromInternalValue(1), | 138 "google.com", base::Time::FromInternalValue(1), |
135 make_scoped_ptr(crypto::ECPrivateKey::Create()))); | 139 make_scoped_ptr(crypto::ECPrivateKey::Create()))); |
136 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 140 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
137 "nonpersistent.com", base::Time::FromInternalValue(3), | 141 "nonpersistent.com", base::Time::FromInternalValue(3), |
138 make_scoped_ptr(crypto::ECPrivateKey::Create()))); | 142 make_scoped_ptr(crypto::ECPrivateKey::Create()))); |
139 | 143 |
140 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; | 144 std::vector<scoped_ptr<net::DefaultChannelIDStore::ChannelID>> channel_ids; |
141 // Replace the store effectively destroying the current one and forcing it | 145 // Replace the store effectively destroying the current one and forcing it |
142 // to write its data to disk. Then we can see if after loading it again it | 146 // to write its data to disk. Then we can see if after loading it again it |
143 // is still there. | 147 // is still there. |
144 store_ = NULL; | 148 store_ = NULL; |
145 // Make sure we wait until the destructor has run. | 149 // Make sure we wait until the destructor has run. |
146 base::RunLoop().RunUntilIdle(); | 150 base::RunLoop().RunUntilIdle(); |
147 // Specify storage policy that makes "nonpersistent.com" session only. | 151 // Specify storage policy that makes "nonpersistent.com" session only. |
148 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | 152 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
149 new content::MockSpecialStoragePolicy(); | 153 new content::MockSpecialStoragePolicy(); |
150 storage_policy->AddSessionOnly( | 154 storage_policy->AddSessionOnly( |
(...skipping 29 matching lines...) Expand all Loading... |
180 base::ThreadTaskRunnerHandle::Get(), | 184 base::ThreadTaskRunnerHandle::Get(), |
181 NULL); | 185 NULL); |
182 | 186 |
183 // Reload and check that the nonpersistent.com channel IDs have been removed. | 187 // Reload and check that the nonpersistent.com channel IDs have been removed. |
184 Load(&channel_ids); | 188 Load(&channel_ids); |
185 ASSERT_EQ(2U, channel_ids.size()); | 189 ASSERT_EQ(2U, channel_ids.size()); |
186 for (const auto& id : channel_ids) { | 190 for (const auto& id : channel_ids) { |
187 ASSERT_NE("nonpersistent.com", id->server_identifier()); | 191 ASSERT_NE("nonpersistent.com", id->server_identifier()); |
188 } | 192 } |
189 } | 193 } |
OLD | NEW |