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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "chrome/browser/net/quota_policy_channel_id_store.h" | 16 #include "chrome/browser/net/quota_policy_channel_id_store.h" |
17 #include "content/public/test/mock_special_storage_policy.h" | 17 #include "content/public/test/mock_special_storage_policy.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "net/base/test_data_directory.h" | 19 #include "net/base/test_data_directory.h" |
20 #include "net/cookies/cookie_util.h" | 20 #include "net/cookies/cookie_util.h" |
21 #include "net/ssl/ssl_client_cert_type.h" | 21 #include "net/ssl/ssl_client_cert_type.h" |
22 #include "net/test/cert_test_util.h" | 22 #include "net/test/cert_test_util.h" |
| 23 #include "net/test/channel_id_test_util.h" |
23 #include "sql/statement.h" | 24 #include "sql/statement.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 const base::FilePath::CharType kTestChannelIDFilename[] = | 27 const base::FilePath::CharType kTestChannelIDFilename[] = |
27 FILE_PATH_LITERAL("ChannelID"); | 28 FILE_PATH_LITERAL("ChannelID"); |
28 | 29 |
29 class QuotaPolicyChannelIDStoreTest : public testing::Test { | 30 class QuotaPolicyChannelIDStoreTest : public testing::Test { |
30 public: | 31 public: |
31 void Load(ScopedVector<net::DefaultChannelIDStore::ChannelID>* channel_ids) { | 32 void Load(ScopedVector<net::DefaultChannelIDStore::ChannelID>* channel_ids) { |
32 base::RunLoop run_loop; | 33 base::RunLoop run_loop; |
(...skipping 15 matching lines...) Expand all Loading... |
48 protected: | 49 protected: |
49 void SetUp() override { | 50 void SetUp() override { |
50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
51 store_ = new QuotaPolicyChannelIDStore( | 52 store_ = new QuotaPolicyChannelIDStore( |
52 temp_dir_.path().Append(kTestChannelIDFilename), | 53 temp_dir_.path().Append(kTestChannelIDFilename), |
53 base::ThreadTaskRunnerHandle::Get(), | 54 base::ThreadTaskRunnerHandle::Get(), |
54 NULL); | 55 NULL); |
55 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; | 56 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; |
56 Load(&channel_ids); | 57 Load(&channel_ids); |
57 ASSERT_EQ(0u, channel_ids.size()); | 58 ASSERT_EQ(0u, channel_ids.size()); |
58 // Make sure the store gets written at least once. | |
59 store_->AddChannelID( | |
60 net::DefaultChannelIDStore::ChannelID("google.com", | |
61 base::Time::FromInternalValue(1), | |
62 base::Time::FromInternalValue(2), | |
63 "a", | |
64 "b")); | |
65 } | 59 } |
66 | 60 |
67 void TearDown() override { | 61 void TearDown() override { |
68 store_ = NULL; | 62 store_ = NULL; |
69 loop_.RunUntilIdle(); | 63 loop_.RunUntilIdle(); |
70 } | 64 } |
71 | 65 |
72 base::ScopedTempDir temp_dir_; | 66 base::ScopedTempDir temp_dir_; |
73 scoped_refptr<QuotaPolicyChannelIDStore> store_; | 67 scoped_refptr<QuotaPolicyChannelIDStore> store_; |
74 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids_; | 68 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids_; |
75 base::MessageLoop loop_; | 69 base::MessageLoop loop_; |
76 }; | 70 }; |
77 | 71 |
78 // Test if data is stored as expected in the QuotaPolicy database. | 72 // Test if data is stored as expected in the QuotaPolicy database. |
79 TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) { | 73 TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) { |
80 store_->AddChannelID( | 74 scoped_ptr<crypto::ECPrivateKey> goog_key(crypto::ECPrivateKey::Create()); |
81 net::DefaultChannelIDStore::ChannelID("foo.com", | 75 scoped_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); |
82 base::Time::FromInternalValue(3), | 76 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
83 base::Time::FromInternalValue(4), | 77 "google.com", base::Time::FromInternalValue(1), |
84 "c", | 78 make_scoped_ptr(goog_key->Copy()))); |
85 "d")); | 79 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
| 80 "foo.com", base::Time::FromInternalValue(3), |
| 81 make_scoped_ptr(foo_key->Copy()))); |
86 | 82 |
87 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; | 83 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; |
88 // Replace the store effectively destroying the current one and forcing it | 84 // Replace the store effectively destroying the current one and forcing it |
89 // to write its data to disk. Then we can see if after loading it again it | 85 // to write its data to disk. Then we can see if after loading it again it |
90 // is still there. | 86 // is still there. |
91 store_ = NULL; | 87 store_ = NULL; |
92 // Make sure we wait until the destructor has run. | 88 // Make sure we wait until the destructor has run. |
93 base::RunLoop().RunUntilIdle(); | 89 base::RunLoop().RunUntilIdle(); |
94 store_ = new QuotaPolicyChannelIDStore( | 90 store_ = new QuotaPolicyChannelIDStore( |
95 temp_dir_.path().Append(kTestChannelIDFilename), | 91 temp_dir_.path().Append(kTestChannelIDFilename), |
96 base::ThreadTaskRunnerHandle::Get(), | 92 base::ThreadTaskRunnerHandle::Get(), |
97 NULL); | 93 NULL); |
98 | 94 |
99 // Reload and test for persistence | 95 // Reload and test for persistence |
100 Load(&channel_ids); | 96 Load(&channel_ids); |
101 ASSERT_EQ(2U, channel_ids.size()); | 97 ASSERT_EQ(2U, channel_ids.size()); |
102 net::DefaultChannelIDStore::ChannelID* goog_channel_id; | 98 net::DefaultChannelIDStore::ChannelID* goog_channel_id; |
103 net::DefaultChannelIDStore::ChannelID* foo_channel_id; | 99 net::DefaultChannelIDStore::ChannelID* foo_channel_id; |
104 if (channel_ids[0]->server_identifier() == "google.com") { | 100 if (channel_ids[0]->server_identifier() == "google.com") { |
105 goog_channel_id = channel_ids[0]; | 101 goog_channel_id = channel_ids[0]; |
106 foo_channel_id = channel_ids[1]; | 102 foo_channel_id = channel_ids[1]; |
107 } else { | 103 } else { |
108 goog_channel_id = channel_ids[1]; | 104 goog_channel_id = channel_ids[1]; |
109 foo_channel_id = channel_ids[0]; | 105 foo_channel_id = channel_ids[0]; |
110 } | 106 } |
111 ASSERT_EQ("google.com", goog_channel_id->server_identifier()); | 107 ASSERT_EQ("google.com", goog_channel_id->server_identifier()); |
112 ASSERT_STREQ("a", goog_channel_id->private_key().c_str()); | 108 EXPECT_TRUE(net::KeysEqual(goog_key.get(), goog_channel_id->key())); |
113 ASSERT_STREQ("b", goog_channel_id->cert().c_str()); | |
114 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue()); | 109 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue()); |
115 ASSERT_EQ(2, goog_channel_id->expiration_time().ToInternalValue()); | |
116 ASSERT_EQ("foo.com", foo_channel_id->server_identifier()); | 110 ASSERT_EQ("foo.com", foo_channel_id->server_identifier()); |
117 ASSERT_STREQ("c", foo_channel_id->private_key().c_str()); | 111 EXPECT_TRUE(net::KeysEqual(foo_key.get(), foo_channel_id->key())); |
118 ASSERT_STREQ("d", foo_channel_id->cert().c_str()); | |
119 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue()); | 112 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue()); |
120 ASSERT_EQ(4, foo_channel_id->expiration_time().ToInternalValue()); | |
121 | 113 |
122 // Now delete the channel ID and check persistence again. | 114 // Now delete the channel ID and check persistence again. |
123 store_->DeleteChannelID(*channel_ids[0]); | 115 store_->DeleteChannelID(*channel_ids[0]); |
124 store_->DeleteChannelID(*channel_ids[1]); | 116 store_->DeleteChannelID(*channel_ids[1]); |
125 store_ = NULL; | 117 store_ = NULL; |
126 // Make sure we wait until the destructor has run. | 118 // Make sure we wait until the destructor has run. |
127 base::RunLoop().RunUntilIdle(); | 119 base::RunLoop().RunUntilIdle(); |
128 channel_ids.clear(); | 120 channel_ids.clear(); |
129 store_ = new QuotaPolicyChannelIDStore( | 121 store_ = new QuotaPolicyChannelIDStore( |
130 temp_dir_.path().Append(kTestChannelIDFilename), | 122 temp_dir_.path().Append(kTestChannelIDFilename), |
131 base::ThreadTaskRunnerHandle::Get(), | 123 base::ThreadTaskRunnerHandle::Get(), |
132 NULL); | 124 NULL); |
133 | 125 |
134 // Reload and check if the channel ID has been removed. | 126 // Reload and check if the channel ID has been removed. |
135 Load(&channel_ids); | 127 Load(&channel_ids); |
136 ASSERT_EQ(0U, channel_ids.size()); | 128 ASSERT_EQ(0U, channel_ids.size()); |
137 } | 129 } |
138 | 130 |
139 // Test if data is stored as expected in the QuotaPolicy database. | 131 // Test if data is stored as expected in the QuotaPolicy database. |
140 TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) { | 132 TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) { |
141 store_->AddChannelID( | 133 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
142 net::DefaultChannelIDStore::ChannelID("nonpersistent.com", | 134 "google.com", base::Time::FromInternalValue(1), |
143 base::Time::FromInternalValue(3), | 135 make_scoped_ptr(crypto::ECPrivateKey::Create()))); |
144 base::Time::FromInternalValue(4), | 136 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
145 "c", | 137 "nonpersistent.com", base::Time::FromInternalValue(3), |
146 "d")); | 138 make_scoped_ptr(crypto::ECPrivateKey::Create()))); |
147 | 139 |
148 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; | 140 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; |
149 // Replace the store effectively destroying the current one and forcing it | 141 // Replace the store effectively destroying the current one and forcing it |
150 // to write its data to disk. Then we can see if after loading it again it | 142 // to write its data to disk. Then we can see if after loading it again it |
151 // is still there. | 143 // is still there. |
152 store_ = NULL; | 144 store_ = NULL; |
153 // Make sure we wait until the destructor has run. | 145 // Make sure we wait until the destructor has run. |
154 base::RunLoop().RunUntilIdle(); | 146 base::RunLoop().RunUntilIdle(); |
155 // Specify storage policy that makes "nonpersistent.com" session only. | 147 // Specify storage policy that makes "nonpersistent.com" session only. |
156 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | 148 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
157 new content::MockSpecialStoragePolicy(); | 149 new content::MockSpecialStoragePolicy(); |
158 storage_policy->AddSessionOnly( | 150 storage_policy->AddSessionOnly( |
159 net::cookie_util::CookieOriginToURL("nonpersistent.com", true)); | 151 net::cookie_util::CookieOriginToURL("nonpersistent.com", true)); |
160 // Reload store, it should still have both channel IDs. | 152 // Reload store, it should still have both channel IDs. |
161 store_ = new QuotaPolicyChannelIDStore( | 153 store_ = new QuotaPolicyChannelIDStore( |
162 temp_dir_.path().Append(kTestChannelIDFilename), | 154 temp_dir_.path().Append(kTestChannelIDFilename), |
163 base::ThreadTaskRunnerHandle::Get(), | 155 base::ThreadTaskRunnerHandle::Get(), |
164 storage_policy); | 156 storage_policy); |
165 Load(&channel_ids); | 157 Load(&channel_ids); |
166 ASSERT_EQ(2U, channel_ids.size()); | 158 ASSERT_EQ(2U, channel_ids.size()); |
167 | 159 |
168 // Add another two channel IDs before closing the store. Because additions are | 160 // Add another two channel IDs before closing the store. Because additions are |
169 // delayed and committed to disk in batches, these will not be committed until | 161 // delayed and committed to disk in batches, these will not be committed until |
170 // the store is destroyed, which is after the policy is applied. The pending | 162 // the store is destroyed, which is after the policy is applied. The pending |
171 // operation pruning logic should prevent the "nonpersistent.com" ID from | 163 // operation pruning logic should prevent the "nonpersistent.com" ID from |
172 // being committed to disk. | 164 // being committed to disk. |
173 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 165 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
174 "nonpersistent.com", base::Time::FromInternalValue(5), | 166 "nonpersistent.com", base::Time::FromInternalValue(5), |
175 base::Time::FromInternalValue(6), "e", "f")); | 167 make_scoped_ptr(crypto::ECPrivateKey::Create()))); |
176 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 168 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
177 "persistent.com", base::Time::FromInternalValue(7), | 169 "persistent.com", base::Time::FromInternalValue(7), |
178 base::Time::FromInternalValue(8), "g", "h")); | 170 make_scoped_ptr(crypto::ECPrivateKey::Create()))); |
179 | 171 |
180 // Now close the store, and the nonpersistent.com channel IDs should be | 172 // Now close the store, and the nonpersistent.com channel IDs should be |
181 // deleted according to policy. | 173 // deleted according to policy. |
182 store_ = NULL; | 174 store_ = NULL; |
183 // Make sure we wait until the destructor has run. | 175 // Make sure we wait until the destructor has run. |
184 base::RunLoop().RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
185 channel_ids.clear(); | 177 channel_ids.clear(); |
186 store_ = new QuotaPolicyChannelIDStore( | 178 store_ = new QuotaPolicyChannelIDStore( |
187 temp_dir_.path().Append(kTestChannelIDFilename), | 179 temp_dir_.path().Append(kTestChannelIDFilename), |
188 base::ThreadTaskRunnerHandle::Get(), | 180 base::ThreadTaskRunnerHandle::Get(), |
189 NULL); | 181 NULL); |
190 | 182 |
191 // Reload and check that the nonpersistent.com channel IDs have been removed. | 183 // Reload and check that the nonpersistent.com channel IDs have been removed. |
192 Load(&channel_ids); | 184 Load(&channel_ids); |
193 ASSERT_EQ(2U, channel_ids.size()); | 185 ASSERT_EQ(2U, channel_ids.size()); |
194 for (const auto& id : channel_ids) { | 186 for (const auto& id : channel_ids) { |
195 ASSERT_NE("nonpersistent.com", id->server_identifier()); | 187 ASSERT_NE("nonpersistent.com", id->server_identifier()); |
196 } | 188 } |
197 } | 189 } |
OLD | NEW |