Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / net / quota_policy_channel_id_store_unittest.cc
blob670bf883b8118c53ab184f4ba615b7c1442056bf
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
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/stl_util.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/net/quota_policy_channel_id_store.h"
17 #include "content/public/test/mock_special_storage_policy.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "net/base/test_data_directory.h"
20 #include "net/cookies/cookie_util.h"
21 #include "net/ssl/ssl_client_cert_type.h"
22 #include "net/test/cert_test_util.h"
23 #include "net/test/channel_id_test_util.h"
24 #include "sql/statement.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 const base::FilePath::CharType kTestChannelIDFilename[] =
28 FILE_PATH_LITERAL("ChannelID");
30 class QuotaPolicyChannelIDStoreTest : public testing::Test {
31 public:
32 void Load(ScopedVector<net::DefaultChannelIDStore::ChannelID>* channel_ids) {
33 base::RunLoop run_loop;
34 store_->Load(base::Bind(&QuotaPolicyChannelIDStoreTest::OnLoaded,
35 base::Unretained(this),
36 &run_loop));
37 run_loop.Run();
38 channel_ids->swap(channel_ids_);
39 channel_ids_.clear();
42 void OnLoaded(base::RunLoop* run_loop,
43 scoped_ptr<ScopedVector<net::DefaultChannelIDStore::ChannelID> >
44 channel_ids) {
45 channel_ids_.swap(*channel_ids);
46 run_loop->Quit();
49 protected:
50 void SetUp() override {
51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
52 store_ = new QuotaPolicyChannelIDStore(
53 temp_dir_.path().Append(kTestChannelIDFilename),
54 base::ThreadTaskRunnerHandle::Get(),
55 NULL);
56 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids;
57 Load(&channel_ids);
58 ASSERT_EQ(0u, channel_ids.size());
61 void TearDown() override {
62 store_ = NULL;
63 loop_.RunUntilIdle();
66 base::ScopedTempDir temp_dir_;
67 scoped_refptr<QuotaPolicyChannelIDStore> store_;
68 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids_;
69 base::MessageLoop loop_;
72 // Test if data is stored as expected in the QuotaPolicy database.
73 TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) {
74 scoped_ptr<crypto::ECPrivateKey> goog_key(crypto::ECPrivateKey::Create());
75 scoped_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create());
76 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
77 "google.com", base::Time::FromInternalValue(1),
78 make_scoped_ptr(goog_key->Copy())));
79 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
80 "foo.com", base::Time::FromInternalValue(3),
81 make_scoped_ptr(foo_key->Copy())));
83 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids;
84 // 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
86 // is still there.
87 store_ = NULL;
88 // Make sure we wait until the destructor has run.
89 base::RunLoop().RunUntilIdle();
90 store_ = new QuotaPolicyChannelIDStore(
91 temp_dir_.path().Append(kTestChannelIDFilename),
92 base::ThreadTaskRunnerHandle::Get(),
93 NULL);
95 // Reload and test for persistence
96 Load(&channel_ids);
97 ASSERT_EQ(2U, channel_ids.size());
98 net::DefaultChannelIDStore::ChannelID* goog_channel_id;
99 net::DefaultChannelIDStore::ChannelID* foo_channel_id;
100 if (channel_ids[0]->server_identifier() == "google.com") {
101 goog_channel_id = channel_ids[0];
102 foo_channel_id = channel_ids[1];
103 } else {
104 goog_channel_id = channel_ids[1];
105 foo_channel_id = channel_ids[0];
107 ASSERT_EQ("google.com", goog_channel_id->server_identifier());
108 EXPECT_TRUE(net::KeysEqual(goog_key.get(), goog_channel_id->key()));
109 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue());
110 ASSERT_EQ("foo.com", foo_channel_id->server_identifier());
111 EXPECT_TRUE(net::KeysEqual(foo_key.get(), foo_channel_id->key()));
112 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue());
114 // Now delete the channel ID and check persistence again.
115 store_->DeleteChannelID(*channel_ids[0]);
116 store_->DeleteChannelID(*channel_ids[1]);
117 store_ = NULL;
118 // Make sure we wait until the destructor has run.
119 base::RunLoop().RunUntilIdle();
120 channel_ids.clear();
121 store_ = new QuotaPolicyChannelIDStore(
122 temp_dir_.path().Append(kTestChannelIDFilename),
123 base::ThreadTaskRunnerHandle::Get(),
124 NULL);
126 // Reload and check if the channel ID has been removed.
127 Load(&channel_ids);
128 ASSERT_EQ(0U, channel_ids.size());
131 // Test if data is stored as expected in the QuotaPolicy database.
132 TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) {
133 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
134 "google.com", base::Time::FromInternalValue(1),
135 make_scoped_ptr(crypto::ECPrivateKey::Create())));
136 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
137 "nonpersistent.com", base::Time::FromInternalValue(3),
138 make_scoped_ptr(crypto::ECPrivateKey::Create())));
140 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids;
141 // 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
143 // is still there.
144 store_ = NULL;
145 // Make sure we wait until the destructor has run.
146 base::RunLoop().RunUntilIdle();
147 // Specify storage policy that makes "nonpersistent.com" session only.
148 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
149 new content::MockSpecialStoragePolicy();
150 storage_policy->AddSessionOnly(
151 net::cookie_util::CookieOriginToURL("nonpersistent.com", true));
152 // Reload store, it should still have both channel IDs.
153 store_ = new QuotaPolicyChannelIDStore(
154 temp_dir_.path().Append(kTestChannelIDFilename),
155 base::ThreadTaskRunnerHandle::Get(),
156 storage_policy);
157 Load(&channel_ids);
158 ASSERT_EQ(2U, channel_ids.size());
160 // Add another two channel IDs before closing the store. Because additions are
161 // delayed and committed to disk in batches, these will not be committed until
162 // the store is destroyed, which is after the policy is applied. The pending
163 // operation pruning logic should prevent the "nonpersistent.com" ID from
164 // being committed to disk.
165 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
166 "nonpersistent.com", base::Time::FromInternalValue(5),
167 make_scoped_ptr(crypto::ECPrivateKey::Create())));
168 store_->AddChannelID(net::DefaultChannelIDStore::ChannelID(
169 "persistent.com", base::Time::FromInternalValue(7),
170 make_scoped_ptr(crypto::ECPrivateKey::Create())));
172 // Now close the store, and the nonpersistent.com channel IDs should be
173 // deleted according to policy.
174 store_ = NULL;
175 // Make sure we wait until the destructor has run.
176 base::RunLoop().RunUntilIdle();
177 channel_ids.clear();
178 store_ = new QuotaPolicyChannelIDStore(
179 temp_dir_.path().Append(kTestChannelIDFilename),
180 base::ThreadTaskRunnerHandle::Get(),
181 NULL);
183 // Reload and check that the nonpersistent.com channel IDs have been removed.
184 Load(&channel_ids);
185 ASSERT_EQ(2U, channel_ids.size());
186 for (const auto& id : channel_ids) {
187 ASSERT_NE("nonpersistent.com", id->server_identifier());