| 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 "chrome/browser/net/quota_policy_channel_id_store.h" | 5 #include "chrome/browser/net/quota_policy_channel_id_store.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 15 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 16 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 18 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 20 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 21 #include "content/public/test/mock_special_storage_policy.h" | 20 #include "content/public/test/mock_special_storage_policy.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 channel_ids_; | 76 channel_ids_; |
| 78 base::MessageLoop loop_; | 77 base::MessageLoop loop_; |
| 79 }; | 78 }; |
| 80 | 79 |
| 81 // Test if data is stored as expected in the QuotaPolicy database. | 80 // Test if data is stored as expected in the QuotaPolicy database. |
| 82 TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) { | 81 TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) { |
| 83 std::unique_ptr<crypto::ECPrivateKey> goog_key( | 82 std::unique_ptr<crypto::ECPrivateKey> goog_key( |
| 84 crypto::ECPrivateKey::Create()); | 83 crypto::ECPrivateKey::Create()); |
| 85 std::unique_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); | 84 std::unique_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); |
| 86 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 85 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
| 87 "google.com", base::Time::FromInternalValue(1), | 86 "google.com", base::Time::FromInternalValue(1), goog_key->Copy())); |
| 88 base::WrapUnique(goog_key->Copy()))); | |
| 89 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 87 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
| 90 "foo.com", base::Time::FromInternalValue(3), | 88 "foo.com", base::Time::FromInternalValue(3), foo_key->Copy())); |
| 91 base::WrapUnique(foo_key->Copy()))); | |
| 92 | 89 |
| 93 std::vector<std::unique_ptr<net::DefaultChannelIDStore::ChannelID>> | 90 std::vector<std::unique_ptr<net::DefaultChannelIDStore::ChannelID>> |
| 94 channel_ids; | 91 channel_ids; |
| 95 // Replace the store effectively destroying the current one and forcing it | 92 // Replace the store effectively destroying the current one and forcing it |
| 96 // to write its data to disk. Then we can see if after loading it again it | 93 // to write its data to disk. Then we can see if after loading it again it |
| 97 // is still there. | 94 // is still there. |
| 98 store_ = NULL; | 95 store_ = NULL; |
| 99 // Make sure we wait until the destructor has run. | 96 // Make sure we wait until the destructor has run. |
| 100 base::RunLoop().RunUntilIdle(); | 97 base::RunLoop().RunUntilIdle(); |
| 101 store_ = new QuotaPolicyChannelIDStore( | 98 store_ = new QuotaPolicyChannelIDStore( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 133 |
| 137 // Reload and check if the channel ID has been removed. | 134 // Reload and check if the channel ID has been removed. |
| 138 Load(&channel_ids); | 135 Load(&channel_ids); |
| 139 ASSERT_EQ(0U, channel_ids.size()); | 136 ASSERT_EQ(0U, channel_ids.size()); |
| 140 } | 137 } |
| 141 | 138 |
| 142 // Test if data is stored as expected in the QuotaPolicy database. | 139 // Test if data is stored as expected in the QuotaPolicy database. |
| 143 TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) { | 140 TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) { |
| 144 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 141 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
| 145 "google.com", base::Time::FromInternalValue(1), | 142 "google.com", base::Time::FromInternalValue(1), |
| 146 base::WrapUnique(crypto::ECPrivateKey::Create()))); | 143 crypto::ECPrivateKey::Create())); |
| 147 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 144 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
| 148 "nonpersistent.com", base::Time::FromInternalValue(3), | 145 "nonpersistent.com", base::Time::FromInternalValue(3), |
| 149 base::WrapUnique(crypto::ECPrivateKey::Create()))); | 146 crypto::ECPrivateKey::Create())); |
| 150 | 147 |
| 151 std::vector<std::unique_ptr<net::DefaultChannelIDStore::ChannelID>> | 148 std::vector<std::unique_ptr<net::DefaultChannelIDStore::ChannelID>> |
| 152 channel_ids; | 149 channel_ids; |
| 153 // Replace the store effectively destroying the current one and forcing it | 150 // Replace the store effectively destroying the current one and forcing it |
| 154 // to write its data to disk. Then we can see if after loading it again it | 151 // to write its data to disk. Then we can see if after loading it again it |
| 155 // is still there. | 152 // is still there. |
| 156 store_ = NULL; | 153 store_ = NULL; |
| 157 // Make sure we wait until the destructor has run. | 154 // Make sure we wait until the destructor has run. |
| 158 base::RunLoop().RunUntilIdle(); | 155 base::RunLoop().RunUntilIdle(); |
| 159 // Specify storage policy that makes "nonpersistent.com" session only. | 156 // Specify storage policy that makes "nonpersistent.com" session only. |
| 160 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | 157 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = |
| 161 new content::MockSpecialStoragePolicy(); | 158 new content::MockSpecialStoragePolicy(); |
| 162 storage_policy->AddSessionOnly( | 159 storage_policy->AddSessionOnly( |
| 163 net::cookie_util::CookieOriginToURL("nonpersistent.com", true)); | 160 net::cookie_util::CookieOriginToURL("nonpersistent.com", true)); |
| 164 // Reload store, it should still have both channel IDs. | 161 // Reload store, it should still have both channel IDs. |
| 165 store_ = new QuotaPolicyChannelIDStore( | 162 store_ = new QuotaPolicyChannelIDStore( |
| 166 temp_dir_.path().Append(kTestChannelIDFilename), | 163 temp_dir_.path().Append(kTestChannelIDFilename), |
| 167 base::ThreadTaskRunnerHandle::Get(), | 164 base::ThreadTaskRunnerHandle::Get(), |
| 168 storage_policy); | 165 storage_policy); |
| 169 Load(&channel_ids); | 166 Load(&channel_ids); |
| 170 ASSERT_EQ(2U, channel_ids.size()); | 167 ASSERT_EQ(2U, channel_ids.size()); |
| 171 | 168 |
| 172 // Add another two channel IDs before closing the store. Because additions are | 169 // Add another two channel IDs before closing the store. Because additions are |
| 173 // delayed and committed to disk in batches, these will not be committed until | 170 // delayed and committed to disk in batches, these will not be committed until |
| 174 // the store is destroyed, which is after the policy is applied. The pending | 171 // the store is destroyed, which is after the policy is applied. The pending |
| 175 // operation pruning logic should prevent the "nonpersistent.com" ID from | 172 // operation pruning logic should prevent the "nonpersistent.com" ID from |
| 176 // being committed to disk. | 173 // being committed to disk. |
| 177 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 174 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
| 178 "nonpersistent.com", base::Time::FromInternalValue(5), | 175 "nonpersistent.com", base::Time::FromInternalValue(5), |
| 179 base::WrapUnique(crypto::ECPrivateKey::Create()))); | 176 crypto::ECPrivateKey::Create())); |
| 180 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( | 177 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID( |
| 181 "persistent.com", base::Time::FromInternalValue(7), | 178 "persistent.com", base::Time::FromInternalValue(7), |
| 182 base::WrapUnique(crypto::ECPrivateKey::Create()))); | 179 crypto::ECPrivateKey::Create())); |
| 183 | 180 |
| 184 // Now close the store, and the nonpersistent.com channel IDs should be | 181 // Now close the store, and the nonpersistent.com channel IDs should be |
| 185 // deleted according to policy. | 182 // deleted according to policy. |
| 186 store_ = NULL; | 183 store_ = NULL; |
| 187 // Make sure we wait until the destructor has run. | 184 // Make sure we wait until the destructor has run. |
| 188 base::RunLoop().RunUntilIdle(); | 185 base::RunLoop().RunUntilIdle(); |
| 189 channel_ids.clear(); | 186 channel_ids.clear(); |
| 190 store_ = new QuotaPolicyChannelIDStore( | 187 store_ = new QuotaPolicyChannelIDStore( |
| 191 temp_dir_.path().Append(kTestChannelIDFilename), | 188 temp_dir_.path().Append(kTestChannelIDFilename), |
| 192 base::ThreadTaskRunnerHandle::Get(), | 189 base::ThreadTaskRunnerHandle::Get(), |
| 193 NULL); | 190 NULL); |
| 194 | 191 |
| 195 // Reload and check that the nonpersistent.com channel IDs have been removed. | 192 // Reload and check that the nonpersistent.com channel IDs have been removed. |
| 196 Load(&channel_ids); | 193 Load(&channel_ids); |
| 197 ASSERT_EQ(2U, channel_ids.size()); | 194 ASSERT_EQ(2U, channel_ids.size()); |
| 198 for (const auto& id : channel_ids) { | 195 for (const auto& id : channel_ids) { |
| 199 ASSERT_NE("nonpersistent.com", id->server_identifier()); | 196 ASSERT_NE("nonpersistent.com", id->server_identifier()); |
| 200 } | 197 } |
| 201 } | 198 } |
| OLD | NEW |