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