Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: content/browser/net/quota_policy_channel_id_store_unittest.cc

Issue 381073002: Move sqlite_channel_id_store from chrome/browser/net to net/extras. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address mmenke's comments. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "base/bind.h"
6 #include "base/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/stl_util.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/time/time.h"
15 #include "content/browser/net/quota_policy_channel_id_store.h"
16 #include "content/public/test/mock_special_storage_policy.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "net/base/test_data_directory.h"
19 #include "net/cookies/cookie_util.h"
20 #include "net/ssl/ssl_client_cert_type.h"
21 #include "net/test/cert_test_util.h"
22 #include "sql/statement.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace content {
26
27 const base::FilePath::CharType kTestChannelIDFilename[] =
28 FILE_PATH_LITERAL("ChannelID");
29
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();
40 }
41
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();
47 }
48
49 protected:
50 static base::Time GetTestCertExpirationTime() {
51 // Cert expiration time from 'dumpasn1 unittest.originbound.der':
52 // GeneralizedTime 19/11/2111 02:23:45 GMT
53 // base::Time::FromUTCExploded can't generate values past 2038 on 32-bit
54 // linux, so we use the raw value here.
55 return base::Time::FromInternalValue(GG_INT64_C(16121816625000000));
56 }
57
58 static base::Time GetTestCertCreationTime() {
59 // UTCTime 13/12/2011 02:23:45 GMT
60 base::Time::Exploded exploded_time;
61 exploded_time.year = 2011;
62 exploded_time.month = 12;
63 exploded_time.day_of_week = 0; // Unused.
64 exploded_time.day_of_month = 13;
65 exploded_time.hour = 2;
66 exploded_time.minute = 23;
67 exploded_time.second = 45;
68 exploded_time.millisecond = 0;
69 return base::Time::FromUTCExploded(exploded_time);
70 }
71
72 virtual void SetUp() {
73 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
74 store_ = new QuotaPolicyChannelIDStore(
75 temp_dir_.path().Append(kTestChannelIDFilename),
76 base::ThreadTaskRunnerHandle::Get(),
77 NULL);
78 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids;
79 Load(&channel_ids);
80 ASSERT_EQ(0u, channel_ids.size());
81 // Make sure the store gets written at least once.
82 store_->AddChannelID(
83 net::DefaultChannelIDStore::ChannelID("google.com",
84 base::Time::FromInternalValue(1),
85 base::Time::FromInternalValue(2),
86 "a",
87 "b"));
88 }
89
90 virtual void TearDown() {
91 store_ = NULL;
92 loop_.RunUntilIdle();
93 }
94
95 base::ScopedTempDir temp_dir_;
96 scoped_refptr<QuotaPolicyChannelIDStore> store_;
97 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids_;
98 base::MessageLoop loop_;
99 };
100
101 // Test if data is stored as expected in the QuotaPolicy database.
102 TEST_F(QuotaPolicyChannelIDStoreTest, TestPersistence) {
103 store_->AddChannelID(
104 net::DefaultChannelIDStore::ChannelID("foo.com",
105 base::Time::FromInternalValue(3),
106 base::Time::FromInternalValue(4),
107 "c",
108 "d"));
109
110 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids;
111 // Replace the store effectively destroying the current one and forcing it
112 // to write its data to disk. Then we can see if after loading it again it
113 // is still there.
114 store_ = NULL;
115 // Make sure we wait until the destructor has run.
116 base::RunLoop().RunUntilIdle();
117 store_ = new QuotaPolicyChannelIDStore(
118 temp_dir_.path().Append(kTestChannelIDFilename),
119 base::MessageLoopProxy::current(),
120 NULL);
121
122 // Reload and test for persistence
123 Load(&channel_ids);
124 ASSERT_EQ(2U, channel_ids.size());
125 net::DefaultChannelIDStore::ChannelID* goog_channel_id;
126 net::DefaultChannelIDStore::ChannelID* foo_channel_id;
127 if (channel_ids[0]->server_identifier() == "google.com") {
128 goog_channel_id = channel_ids[0];
129 foo_channel_id = channel_ids[1];
130 } else {
131 goog_channel_id = channel_ids[1];
132 foo_channel_id = channel_ids[0];
133 }
134 ASSERT_EQ("google.com", goog_channel_id->server_identifier());
135 ASSERT_STREQ("a", goog_channel_id->private_key().c_str());
136 ASSERT_STREQ("b", goog_channel_id->cert().c_str());
137 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue());
138 ASSERT_EQ(2, goog_channel_id->expiration_time().ToInternalValue());
139 ASSERT_EQ("foo.com", foo_channel_id->server_identifier());
140 ASSERT_STREQ("c", foo_channel_id->private_key().c_str());
141 ASSERT_STREQ("d", foo_channel_id->cert().c_str());
142 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue());
143 ASSERT_EQ(4, foo_channel_id->expiration_time().ToInternalValue());
144
145 // Now delete the cert and check persistence again.
146 store_->DeleteChannelID(*channel_ids[0]);
147 store_->DeleteChannelID(*channel_ids[1]);
148 store_ = NULL;
149 // Make sure we wait until the destructor has run.
150 base::RunLoop().RunUntilIdle();
151 channel_ids.clear();
152 store_ = new QuotaPolicyChannelIDStore(
153 temp_dir_.path().Append(kTestChannelIDFilename),
154 base::MessageLoopProxy::current(),
155 NULL);
156
157 // Reload and check if the cert has been removed.
158 Load(&channel_ids);
159 ASSERT_EQ(0U, channel_ids.size());
160 }
161
162 // Test if data is stored as expected in the QuotaPolicy database.
163 TEST_F(QuotaPolicyChannelIDStoreTest, TestPolicy) {
164 store_->AddChannelID(
165 net::DefaultChannelIDStore::ChannelID("foo.com",
166 base::Time::FromInternalValue(3),
167 base::Time::FromInternalValue(4),
168 "c",
169 "d"));
170
171 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids;
172 // Replace the store effectively destroying the current one and forcing it
173 // to write its data to disk. Then we can see if after loading it again it
174 // is still there.
175 store_ = NULL;
176 // Make sure we wait until the destructor has run.
177 base::RunLoop().RunUntilIdle();
178 // Specify storage policy that makes "foo.com" session only.
179 scoped_refptr<MockSpecialStoragePolicy> storage_policy =
180 new content::MockSpecialStoragePolicy();
181 storage_policy->AddSessionOnly(
182 net::cookie_util::CookieOriginToURL("foo.com", true));
183 // Reload store, it should still have both channel ids.
184 store_ = new QuotaPolicyChannelIDStore(
185 temp_dir_.path().Append(kTestChannelIDFilename),
186 base::MessageLoopProxy::current(),
187 storage_policy);
188 Load(&channel_ids);
189 ASSERT_EQ(2U, channel_ids.size());
190
191 // Now close the store, and "foo.com" should be deleted according to policy.
192 store_ = NULL;
193 // Make sure we wait until the destructor has run.
194 base::RunLoop().RunUntilIdle();
195 channel_ids.clear();
196 store_ = new QuotaPolicyChannelIDStore(
197 temp_dir_.path().Append(kTestChannelIDFilename),
198 base::MessageLoopProxy::current(),
199 NULL);
200
201 // Reload and check that the "foo.com" cert has been removed.
202 Load(&channel_ids);
203 ASSERT_EQ(1U, channel_ids.size());
204 ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
205 }
206
207 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698