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

Side by Side Diff: net/extras/sqlite/sqlite_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: Implemented QuotaPolicyChannelIDStore, added unit tests. 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
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/file_util.h" 6 #include "base/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/stl_util.h" 12 #include "base/stl_util.h"
13 #include "chrome/browser/net/sqlite_channel_id_store.h"
14 #include "chrome/common/chrome_constants.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" 13 #include "net/base/test_data_directory.h"
14 #include "net/extras/sqlite/sqlite_channel_id_store.h"
18 #include "net/ssl/ssl_client_cert_type.h" 15 #include "net/ssl/ssl_client_cert_type.h"
19 #include "net/test/cert_test_util.h" 16 #include "net/test/cert_test_util.h"
20 #include "sql/statement.h" 17 #include "sql/statement.h"
21 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
22 19
20 namespace net {
21
22 const base::FilePath::CharType kTestChannelIDFilename[] =
23 FILE_PATH_LITERAL("ChannelID");
24
23 class SQLiteChannelIDStoreTest : public testing::Test { 25 class SQLiteChannelIDStoreTest : public testing::Test {
24 public: 26 public:
25 void Load( 27 void Load(ScopedVector<DefaultChannelIDStore::ChannelID>* channel_ids) {
26 ScopedVector<net::DefaultChannelIDStore::ChannelID>* channel_ids) {
27 base::RunLoop run_loop; 28 base::RunLoop run_loop;
28 store_->Load(base::Bind(&SQLiteChannelIDStoreTest::OnLoaded, 29 store_->Load(base::Bind(&SQLiteChannelIDStoreTest::OnLoaded,
29 base::Unretained(this), 30 base::Unretained(this),
30 &run_loop)); 31 &run_loop));
31 run_loop.Run(); 32 run_loop.Run();
32 channel_ids->swap(channel_ids_); 33 channel_ids->swap(channel_ids_);
33 channel_ids_.clear(); 34 channel_ids_.clear();
34 } 35 }
35 36
36 void OnLoaded( 37 void OnLoaded(
37 base::RunLoop* run_loop, 38 base::RunLoop* run_loop,
38 scoped_ptr<ScopedVector< 39 scoped_ptr<ScopedVector<DefaultChannelIDStore::ChannelID> > channel_ids) {
39 net::DefaultChannelIDStore::ChannelID> > channel_ids) {
40 channel_ids_.swap(*channel_ids); 40 channel_ids_.swap(*channel_ids);
41 run_loop->Quit(); 41 run_loop->Quit();
42 } 42 }
43 43
44 protected: 44 protected:
45 static void ReadTestKeyAndCert(std::string* key, std::string* cert) { 45 static void ReadTestKeyAndCert(std::string* key, std::string* cert) {
46 base::FilePath key_path = net::GetTestCertsDirectory().AppendASCII( 46 base::FilePath key_path =
47 "unittest.originbound.key.der"); 47 GetTestCertsDirectory().AppendASCII("unittest.originbound.key.der");
48 base::FilePath cert_path = net::GetTestCertsDirectory().AppendASCII( 48 base::FilePath cert_path =
49 "unittest.originbound.der"); 49 GetTestCertsDirectory().AppendASCII("unittest.originbound.der");
50 ASSERT_TRUE(base::ReadFileToString(key_path, key)); 50 ASSERT_TRUE(base::ReadFileToString(key_path, key));
51 ASSERT_TRUE(base::ReadFileToString(cert_path, cert)); 51 ASSERT_TRUE(base::ReadFileToString(cert_path, cert));
52 } 52 }
53 53
54 static base::Time GetTestCertExpirationTime() { 54 static base::Time GetTestCertExpirationTime() {
55 // Cert expiration time from 'dumpasn1 unittest.originbound.der': 55 // Cert expiration time from 'dumpasn1 unittest.originbound.der':
56 // GeneralizedTime 19/11/2111 02:23:45 GMT 56 // GeneralizedTime 19/11/2111 02:23:45 GMT
57 // base::Time::FromUTCExploded can't generate values past 2038 on 32-bit 57 // base::Time::FromUTCExploded can't generate values past 2038 on 32-bit
58 // linux, so we use the raw value here. 58 // linux, so we use the raw value here.
59 return base::Time::FromInternalValue(GG_INT64_C(16121816625000000)); 59 return base::Time::FromInternalValue(GG_INT64_C(16121816625000000));
60 } 60 }
61 61
62 static base::Time GetTestCertCreationTime() { 62 static base::Time GetTestCertCreationTime() {
63 // UTCTime 13/12/2011 02:23:45 GMT 63 // UTCTime 13/12/2011 02:23:45 GMT
64 base::Time::Exploded exploded_time; 64 base::Time::Exploded exploded_time;
65 exploded_time.year = 2011; 65 exploded_time.year = 2011;
66 exploded_time.month = 12; 66 exploded_time.month = 12;
67 exploded_time.day_of_week = 0; // Unused. 67 exploded_time.day_of_week = 0; // Unused.
68 exploded_time.day_of_month = 13; 68 exploded_time.day_of_month = 13;
69 exploded_time.hour = 2; 69 exploded_time.hour = 2;
70 exploded_time.minute = 23; 70 exploded_time.minute = 23;
71 exploded_time.second = 45; 71 exploded_time.second = 45;
72 exploded_time.millisecond = 0; 72 exploded_time.millisecond = 0;
73 return base::Time::FromUTCExploded(exploded_time); 73 return base::Time::FromUTCExploded(exploded_time);
74 } 74 }
75 75
76 virtual void SetUp() { 76 virtual void SetUp() {
77 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 77 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
78 store_ = new SQLiteChannelIDStore( 78 store_ = new SQLiteChannelIDStore(
79 temp_dir_.path().Append(chrome::kChannelIDFilename), 79 temp_dir_.path().Append(kTestChannelIDFilename),
80 base::MessageLoopProxy::current(), 80 base::MessageLoopProxy::current());
81 NULL); 81 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
82 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids;
83 Load(&channel_ids); 82 Load(&channel_ids);
84 ASSERT_EQ(0u, channel_ids.size()); 83 ASSERT_EQ(0u, channel_ids.size());
85 // Make sure the store gets written at least once. 84 // Make sure the store gets written at least once.
86 store_->AddChannelID( 85 store_->AddChannelID(
87 net::DefaultChannelIDStore::ChannelID( 86 DefaultChannelIDStore::ChannelID("google.com",
88 "google.com", 87 base::Time::FromInternalValue(1),
89 base::Time::FromInternalValue(1), 88 base::Time::FromInternalValue(2),
90 base::Time::FromInternalValue(2), 89 "a",
91 "a", "b")); 90 "b"));
92 } 91 }
93 92
94 content::TestBrowserThreadBundle thread_bundle_;
95 base::ScopedTempDir temp_dir_; 93 base::ScopedTempDir temp_dir_;
96 scoped_refptr<SQLiteChannelIDStore> store_; 94 scoped_refptr<SQLiteChannelIDStore> store_;
97 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids_; 95 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids_;
98 }; 96 };
99 97
100 // Test if data is stored as expected in the SQLite database. 98 // Test if data is stored as expected in the SQLite database.
101 TEST_F(SQLiteChannelIDStoreTest, TestPersistence) { 99 TEST_F(SQLiteChannelIDStoreTest, TestPersistence) {
102 store_->AddChannelID( 100 store_->AddChannelID(
103 net::DefaultChannelIDStore::ChannelID( 101 DefaultChannelIDStore::ChannelID("foo.com",
104 "foo.com", 102 base::Time::FromInternalValue(3),
105 base::Time::FromInternalValue(3), 103 base::Time::FromInternalValue(4),
106 base::Time::FromInternalValue(4), 104 "c",
107 "c", "d")); 105 "d"));
108 106
109 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 107 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
110 // Replace the store effectively destroying the current one and forcing it 108 // 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 109 // to write its data to disk. Then we can see if after loading it again it
112 // is still there. 110 // is still there.
113 store_ = NULL; 111 store_ = NULL;
114 // Make sure we wait until the destructor has run. 112 // Make sure we wait until the destructor has run.
115 base::RunLoop().RunUntilIdle(); 113 base::RunLoop().RunUntilIdle();
116 store_ = new SQLiteChannelIDStore( 114 store_ =
117 temp_dir_.path().Append(chrome::kChannelIDFilename), 115 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename),
118 base::MessageLoopProxy::current(), 116 base::MessageLoopProxy::current());
119 NULL);
120 117
121 // Reload and test for persistence 118 // Reload and test for persistence
122 Load(&channel_ids); 119 Load(&channel_ids);
123 ASSERT_EQ(2U, channel_ids.size()); 120 ASSERT_EQ(2U, channel_ids.size());
124 net::DefaultChannelIDStore::ChannelID* goog_channel_id; 121 DefaultChannelIDStore::ChannelID* goog_channel_id;
125 net::DefaultChannelIDStore::ChannelID* foo_channel_id; 122 DefaultChannelIDStore::ChannelID* foo_channel_id;
126 if (channel_ids[0]->server_identifier() == "google.com") { 123 if (channel_ids[0]->server_identifier() == "google.com") {
127 goog_channel_id = channel_ids[0]; 124 goog_channel_id = channel_ids[0];
128 foo_channel_id = channel_ids[1]; 125 foo_channel_id = channel_ids[1];
129 } else { 126 } else {
130 goog_channel_id = channel_ids[1]; 127 goog_channel_id = channel_ids[1];
131 foo_channel_id = channel_ids[0]; 128 foo_channel_id = channel_ids[0];
132 } 129 }
133 ASSERT_EQ("google.com", goog_channel_id->server_identifier()); 130 ASSERT_EQ("google.com", goog_channel_id->server_identifier());
134 ASSERT_STREQ("a", goog_channel_id->private_key().c_str()); 131 ASSERT_STREQ("a", goog_channel_id->private_key().c_str());
135 ASSERT_STREQ("b", goog_channel_id->cert().c_str()); 132 ASSERT_STREQ("b", goog_channel_id->cert().c_str());
136 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue()); 133 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue());
137 ASSERT_EQ(2, goog_channel_id->expiration_time().ToInternalValue()); 134 ASSERT_EQ(2, goog_channel_id->expiration_time().ToInternalValue());
138 ASSERT_EQ("foo.com", foo_channel_id->server_identifier()); 135 ASSERT_EQ("foo.com", foo_channel_id->server_identifier());
139 ASSERT_STREQ("c", foo_channel_id->private_key().c_str()); 136 ASSERT_STREQ("c", foo_channel_id->private_key().c_str());
140 ASSERT_STREQ("d", foo_channel_id->cert().c_str()); 137 ASSERT_STREQ("d", foo_channel_id->cert().c_str());
141 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue()); 138 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue());
142 ASSERT_EQ(4, foo_channel_id->expiration_time().ToInternalValue()); 139 ASSERT_EQ(4, foo_channel_id->expiration_time().ToInternalValue());
143 140
144 // Now delete the cert and check persistence again. 141 // Now delete the cert and check persistence again.
145 store_->DeleteChannelID(*channel_ids[0]); 142 store_->DeleteChannelID(*channel_ids[0]);
146 store_->DeleteChannelID(*channel_ids[1]); 143 store_->DeleteChannelID(*channel_ids[1]);
147 store_ = NULL; 144 store_ = NULL;
148 // Make sure we wait until the destructor has run. 145 // Make sure we wait until the destructor has run.
149 base::RunLoop().RunUntilIdle(); 146 base::RunLoop().RunUntilIdle();
150 channel_ids.clear(); 147 channel_ids.clear();
151 store_ = new SQLiteChannelIDStore( 148 store_ =
152 temp_dir_.path().Append(chrome::kChannelIDFilename), 149 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename),
153 base::MessageLoopProxy::current(), 150 base::MessageLoopProxy::current());
154 NULL);
155 151
156 // Reload and check if the cert has been removed. 152 // Reload and check if the cert has been removed.
157 Load(&channel_ids); 153 Load(&channel_ids);
158 ASSERT_EQ(0U, channel_ids.size()); 154 ASSERT_EQ(0U, channel_ids.size());
159 } 155 }
160 156
161 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV1) { 157 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV1) {
162 // Reset the store. We'll be using a different database for this test. 158 // Reset the store. We'll be using a different database for this test.
163 store_ = NULL; 159 store_ = NULL;
164 160
165 base::FilePath v1_db_path(temp_dir_.path().AppendASCII("v1db")); 161 base::FilePath v1_db_path(temp_dir_.path().AppendASCII("v1db"));
166 162
167 std::string key_data; 163 std::string key_data;
168 std::string cert_data; 164 std::string cert_data;
169 ReadTestKeyAndCert(&key_data, &cert_data); 165 ReadTestKeyAndCert(&key_data, &cert_data);
170 166
171 // Create a version 1 database. 167 // Create a version 1 database.
172 { 168 {
173 sql::Connection db; 169 sql::Connection db;
174 ASSERT_TRUE(db.Open(v1_db_path)); 170 ASSERT_TRUE(db.Open(v1_db_path));
175 ASSERT_TRUE(db.Execute( 171 ASSERT_TRUE(db.Execute(
176 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," 172 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
177 "value LONGVARCHAR);" 173 "value LONGVARCHAR);"
178 "INSERT INTO \"meta\" VALUES('version','1');" 174 "INSERT INTO \"meta\" VALUES('version','1');"
179 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');" 175 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');"
180 "CREATE TABLE origin_bound_certs (" 176 "CREATE TABLE origin_bound_certs ("
181 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," 177 "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
182 "private_key BLOB NOT NULL,cert BLOB NOT NULL);")); 178 "private_key BLOB NOT NULL,cert BLOB NOT NULL);"));
183 179
184 sql::Statement add_smt(db.GetUniqueStatement( 180 sql::Statement add_smt(db.GetUniqueStatement(
185 "INSERT INTO origin_bound_certs (origin, private_key, cert) " 181 "INSERT INTO origin_bound_certs (origin, private_key, cert) "
186 "VALUES (?,?,?)")); 182 "VALUES (?,?,?)"));
187 add_smt.BindString(0, "google.com"); 183 add_smt.BindString(0, "google.com");
188 add_smt.BindBlob(1, key_data.data(), key_data.size()); 184 add_smt.BindBlob(1, key_data.data(), key_data.size());
189 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); 185 add_smt.BindBlob(2, cert_data.data(), cert_data.size());
190 ASSERT_TRUE(add_smt.Run()); 186 ASSERT_TRUE(add_smt.Run());
191 187
192 ASSERT_TRUE(db.Execute( 188 ASSERT_TRUE(db.Execute(
193 "INSERT INTO \"origin_bound_certs\" VALUES(" 189 "INSERT INTO \"origin_bound_certs\" VALUES("
194 "'foo.com',X'AA',X'BB');" 190 "'foo.com',X'AA',X'BB');"));
195 ));
196 } 191 }
197 192
198 // Load and test the DB contents twice. First time ensures that we can use 193 // Load and test the DB contents twice. First time ensures that we can use
199 // the updated values immediately. Second time ensures that the updated 194 // the updated values immediately. Second time ensures that the updated
200 // values are stored and read correctly on next load. 195 // values are stored and read correctly on next load.
201 for (int i = 0; i < 2; ++i) { 196 for (int i = 0; i < 2; ++i) {
202 SCOPED_TRACE(i); 197 SCOPED_TRACE(i);
203 198
204 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 199 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
205 store_ = new SQLiteChannelIDStore( 200 store_ =
206 v1_db_path, base::MessageLoopProxy::current(), NULL); 201 new SQLiteChannelIDStore(v1_db_path, base::MessageLoopProxy::current());
207 202
208 // Load the database. Because the existing v1 certs are implicitly of type 203 // Load the database. Because the existing v1 certs are implicitly of type
209 // RSA, which is unsupported, they're discarded. 204 // RSA, which is unsupported, they're discarded.
210 Load(&channel_ids); 205 Load(&channel_ids);
211 ASSERT_EQ(0U, channel_ids.size()); 206 ASSERT_EQ(0U, channel_ids.size());
212 207
213 store_ = NULL; 208 store_ = NULL;
214 base::RunLoop().RunUntilIdle(); 209 base::RunLoop().RunUntilIdle();
215 210
216 // Verify the database version is updated. 211 // Verify the database version is updated.
(...skipping 18 matching lines...) Expand all
235 std::string key_data; 230 std::string key_data;
236 std::string cert_data; 231 std::string cert_data;
237 ReadTestKeyAndCert(&key_data, &cert_data); 232 ReadTestKeyAndCert(&key_data, &cert_data);
238 233
239 // Create a version 2 database. 234 // Create a version 2 database.
240 { 235 {
241 sql::Connection db; 236 sql::Connection db;
242 ASSERT_TRUE(db.Open(v2_db_path)); 237 ASSERT_TRUE(db.Open(v2_db_path));
243 ASSERT_TRUE(db.Execute( 238 ASSERT_TRUE(db.Execute(
244 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," 239 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
245 "value LONGVARCHAR);" 240 "value LONGVARCHAR);"
246 "INSERT INTO \"meta\" VALUES('version','2');" 241 "INSERT INTO \"meta\" VALUES('version','2');"
247 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');" 242 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');"
248 "CREATE TABLE origin_bound_certs (" 243 "CREATE TABLE origin_bound_certs ("
249 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," 244 "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
250 "private_key BLOB NOT NULL," 245 "private_key BLOB NOT NULL,"
251 "cert BLOB NOT NULL," 246 "cert BLOB NOT NULL,"
252 "cert_type INTEGER);" 247 "cert_type INTEGER);"));
253 ));
254 248
255 sql::Statement add_smt(db.GetUniqueStatement( 249 sql::Statement add_smt(db.GetUniqueStatement(
256 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type) " 250 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type) "
257 "VALUES (?,?,?,?)")); 251 "VALUES (?,?,?,?)"));
258 add_smt.BindString(0, "google.com"); 252 add_smt.BindString(0, "google.com");
259 add_smt.BindBlob(1, key_data.data(), key_data.size()); 253 add_smt.BindBlob(1, key_data.data(), key_data.size());
260 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); 254 add_smt.BindBlob(2, cert_data.data(), cert_data.size());
261 add_smt.BindInt64(3, 64); 255 add_smt.BindInt64(3, 64);
262 ASSERT_TRUE(add_smt.Run()); 256 ASSERT_TRUE(add_smt.Run());
263 257
264 ASSERT_TRUE(db.Execute( 258 ASSERT_TRUE(db.Execute(
265 "INSERT INTO \"origin_bound_certs\" VALUES(" 259 "INSERT INTO \"origin_bound_certs\" VALUES("
266 "'foo.com',X'AA',X'BB',64);" 260 "'foo.com',X'AA',X'BB',64);"));
267 ));
268 } 261 }
269 262
270 // Load and test the DB contents twice. First time ensures that we can use 263 // Load and test the DB contents twice. First time ensures that we can use
271 // the updated values immediately. Second time ensures that the updated 264 // the updated values immediately. Second time ensures that the updated
272 // values are saved and read correctly on next load. 265 // values are saved and read correctly on next load.
273 for (int i = 0; i < 2; ++i) { 266 for (int i = 0; i < 2; ++i) {
274 SCOPED_TRACE(i); 267 SCOPED_TRACE(i);
275 268
276 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 269 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
277 store_ = new SQLiteChannelIDStore( 270 store_ =
278 v2_db_path, base::MessageLoopProxy::current(), NULL); 271 new SQLiteChannelIDStore(v2_db_path, base::MessageLoopProxy::current());
279 272
280 // Load the database and ensure the certs can be read. 273 // Load the database and ensure the certs can be read.
281 Load(&channel_ids); 274 Load(&channel_ids);
282 ASSERT_EQ(2U, channel_ids.size()); 275 ASSERT_EQ(2U, channel_ids.size());
283 276
284 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); 277 ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
285 ASSERT_EQ(GetTestCertExpirationTime(), 278 ASSERT_EQ(GetTestCertExpirationTime(), channel_ids[0]->expiration_time());
286 channel_ids[0]->expiration_time());
287 ASSERT_EQ(key_data, channel_ids[0]->private_key()); 279 ASSERT_EQ(key_data, channel_ids[0]->private_key());
288 ASSERT_EQ(cert_data, channel_ids[0]->cert()); 280 ASSERT_EQ(cert_data, channel_ids[0]->cert());
289 281
290 ASSERT_EQ("foo.com", channel_ids[1]->server_identifier()); 282 ASSERT_EQ("foo.com", channel_ids[1]->server_identifier());
291 // Undecodable cert, expiration time will be uninitialized. 283 // Undecodable cert, expiration time will be uninitialized.
292 ASSERT_EQ(base::Time(), channel_ids[1]->expiration_time()); 284 ASSERT_EQ(base::Time(), channel_ids[1]->expiration_time());
293 ASSERT_STREQ("\xaa", channel_ids[1]->private_key().c_str()); 285 ASSERT_STREQ("\xaa", channel_ids[1]->private_key().c_str());
294 ASSERT_STREQ("\xbb", channel_ids[1]->cert().c_str()); 286 ASSERT_STREQ("\xbb", channel_ids[1]->cert().c_str());
295 287
296 store_ = NULL; 288 store_ = NULL;
(...skipping 22 matching lines...) Expand all
319 std::string key_data; 311 std::string key_data;
320 std::string cert_data; 312 std::string cert_data;
321 ReadTestKeyAndCert(&key_data, &cert_data); 313 ReadTestKeyAndCert(&key_data, &cert_data);
322 314
323 // Create a version 3 database. 315 // Create a version 3 database.
324 { 316 {
325 sql::Connection db; 317 sql::Connection db;
326 ASSERT_TRUE(db.Open(v3_db_path)); 318 ASSERT_TRUE(db.Open(v3_db_path));
327 ASSERT_TRUE(db.Execute( 319 ASSERT_TRUE(db.Execute(
328 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," 320 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
329 "value LONGVARCHAR);" 321 "value LONGVARCHAR);"
330 "INSERT INTO \"meta\" VALUES('version','3');" 322 "INSERT INTO \"meta\" VALUES('version','3');"
331 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');" 323 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');"
332 "CREATE TABLE origin_bound_certs (" 324 "CREATE TABLE origin_bound_certs ("
333 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," 325 "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
334 "private_key BLOB NOT NULL," 326 "private_key BLOB NOT NULL,"
335 "cert BLOB NOT NULL," 327 "cert BLOB NOT NULL,"
336 "cert_type INTEGER," 328 "cert_type INTEGER,"
337 "expiration_time INTEGER);" 329 "expiration_time INTEGER);"));
338 ));
339 330
340 sql::Statement add_smt(db.GetUniqueStatement( 331 sql::Statement add_smt(db.GetUniqueStatement(
341 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type, " 332 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type, "
342 "expiration_time) VALUES (?,?,?,?,?)")); 333 "expiration_time) VALUES (?,?,?,?,?)"));
343 add_smt.BindString(0, "google.com"); 334 add_smt.BindString(0, "google.com");
344 add_smt.BindBlob(1, key_data.data(), key_data.size()); 335 add_smt.BindBlob(1, key_data.data(), key_data.size());
345 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); 336 add_smt.BindBlob(2, cert_data.data(), cert_data.size());
346 add_smt.BindInt64(3, 64); 337 add_smt.BindInt64(3, 64);
347 add_smt.BindInt64(4, 1000); 338 add_smt.BindInt64(4, 1000);
348 ASSERT_TRUE(add_smt.Run()); 339 ASSERT_TRUE(add_smt.Run());
349 340
350 ASSERT_TRUE(db.Execute( 341 ASSERT_TRUE(db.Execute(
351 "INSERT INTO \"origin_bound_certs\" VALUES(" 342 "INSERT INTO \"origin_bound_certs\" VALUES("
352 "'foo.com',X'AA',X'BB',64,2000);" 343 "'foo.com',X'AA',X'BB',64,2000);"));
353 ));
354 } 344 }
355 345
356 // Load and test the DB contents twice. First time ensures that we can use 346 // Load and test the DB contents twice. First time ensures that we can use
357 // the updated values immediately. Second time ensures that the updated 347 // the updated values immediately. Second time ensures that the updated
358 // values are saved and read correctly on next load. 348 // values are saved and read correctly on next load.
359 for (int i = 0; i < 2; ++i) { 349 for (int i = 0; i < 2; ++i) {
360 SCOPED_TRACE(i); 350 SCOPED_TRACE(i);
361 351
362 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 352 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
363 store_ = new SQLiteChannelIDStore( 353 store_ =
364 v3_db_path, base::MessageLoopProxy::current(), NULL); 354 new SQLiteChannelIDStore(v3_db_path, base::MessageLoopProxy::current());
365 355
366 // Load the database and ensure the certs can be read. 356 // Load the database and ensure the certs can be read.
367 Load(&channel_ids); 357 Load(&channel_ids);
368 ASSERT_EQ(2U, channel_ids.size()); 358 ASSERT_EQ(2U, channel_ids.size());
369 359
370 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); 360 ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
371 ASSERT_EQ(1000, channel_ids[0]->expiration_time().ToInternalValue()); 361 ASSERT_EQ(1000, channel_ids[0]->expiration_time().ToInternalValue());
372 ASSERT_EQ(GetTestCertCreationTime(), 362 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time());
373 channel_ids[0]->creation_time());
374 ASSERT_EQ(key_data, channel_ids[0]->private_key()); 363 ASSERT_EQ(key_data, channel_ids[0]->private_key());
375 ASSERT_EQ(cert_data, channel_ids[0]->cert()); 364 ASSERT_EQ(cert_data, channel_ids[0]->cert());
376 365
377 ASSERT_EQ("foo.com", channel_ids[1]->server_identifier()); 366 ASSERT_EQ("foo.com", channel_ids[1]->server_identifier());
378 ASSERT_EQ(2000, channel_ids[1]->expiration_time().ToInternalValue()); 367 ASSERT_EQ(2000, channel_ids[1]->expiration_time().ToInternalValue());
379 // Undecodable cert, creation time will be uninitialized. 368 // Undecodable cert, creation time will be uninitialized.
380 ASSERT_EQ(base::Time(), channel_ids[1]->creation_time()); 369 ASSERT_EQ(base::Time(), channel_ids[1]->creation_time());
381 ASSERT_STREQ("\xaa", channel_ids[1]->private_key().c_str()); 370 ASSERT_STREQ("\xaa", channel_ids[1]->private_key().c_str());
382 ASSERT_STREQ("\xbb", channel_ids[1]->cert().c_str()); 371 ASSERT_STREQ("\xbb", channel_ids[1]->cert().c_str());
383 372
(...skipping 23 matching lines...) Expand all
407 std::string key_data; 396 std::string key_data;
408 std::string cert_data; 397 std::string cert_data;
409 ReadTestKeyAndCert(&key_data, &cert_data); 398 ReadTestKeyAndCert(&key_data, &cert_data);
410 399
411 // Create a version 4 database with a mix of RSA and ECDSA certs. 400 // Create a version 4 database with a mix of RSA and ECDSA certs.
412 { 401 {
413 sql::Connection db; 402 sql::Connection db;
414 ASSERT_TRUE(db.Open(v4_db_path)); 403 ASSERT_TRUE(db.Open(v4_db_path));
415 ASSERT_TRUE(db.Execute( 404 ASSERT_TRUE(db.Execute(
416 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," 405 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
417 "value LONGVARCHAR);" 406 "value LONGVARCHAR);"
418 "INSERT INTO \"meta\" VALUES('version','4');" 407 "INSERT INTO \"meta\" VALUES('version','4');"
419 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');" 408 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');"
420 "CREATE TABLE origin_bound_certs (" 409 "CREATE TABLE origin_bound_certs ("
421 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," 410 "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
422 "private_key BLOB NOT NULL," 411 "private_key BLOB NOT NULL,"
423 "cert BLOB NOT NULL," 412 "cert BLOB NOT NULL,"
424 "cert_type INTEGER," 413 "cert_type INTEGER,"
425 "expiration_time INTEGER," 414 "expiration_time INTEGER,"
426 "creation_time INTEGER);" 415 "creation_time INTEGER);"));
427 ));
428 416
429 sql::Statement add_smt(db.GetUniqueStatement( 417 sql::Statement add_smt(db.GetUniqueStatement(
430 "INSERT INTO origin_bound_certs " 418 "INSERT INTO origin_bound_certs "
431 "(origin, private_key, cert, cert_type, expiration_time, creation_time)" 419 "(origin, private_key, cert, cert_type, expiration_time, creation_time)"
432 " VALUES (?,?,?,?,?,?)")); 420 " VALUES (?,?,?,?,?,?)"));
433 add_smt.BindString(0, "google.com"); 421 add_smt.BindString(0, "google.com");
434 add_smt.BindBlob(1, key_data.data(), key_data.size()); 422 add_smt.BindBlob(1, key_data.data(), key_data.size());
435 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); 423 add_smt.BindBlob(2, cert_data.data(), cert_data.size());
436 add_smt.BindInt64(3, 64); 424 add_smt.BindInt64(3, 64);
437 add_smt.BindInt64(4, GetTestCertExpirationTime().ToInternalValue()); 425 add_smt.BindInt64(4, GetTestCertExpirationTime().ToInternalValue());
438 add_smt.BindInt64(5, base::Time::Now().ToInternalValue()); 426 add_smt.BindInt64(5, base::Time::Now().ToInternalValue());
439 ASSERT_TRUE(add_smt.Run()); 427 ASSERT_TRUE(add_smt.Run());
440 428
441 add_smt.Clear(); 429 add_smt.Clear();
442 add_smt.Assign(db.GetUniqueStatement( 430 add_smt.Assign(db.GetUniqueStatement(
443 "INSERT INTO origin_bound_certs " 431 "INSERT INTO origin_bound_certs "
444 "(origin, private_key, cert, cert_type, expiration_time, creation_time)" 432 "(origin, private_key, cert, cert_type, expiration_time, creation_time)"
445 " VALUES (?,?,?,?,?,?)")); 433 " VALUES (?,?,?,?,?,?)"));
446 add_smt.BindString(0, "foo.com"); 434 add_smt.BindString(0, "foo.com");
447 add_smt.BindBlob(1, key_data.data(), key_data.size()); 435 add_smt.BindBlob(1, key_data.data(), key_data.size());
448 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); 436 add_smt.BindBlob(2, cert_data.data(), cert_data.size());
449 add_smt.BindInt64(3, 1); 437 add_smt.BindInt64(3, 1);
450 add_smt.BindInt64(4, GetTestCertExpirationTime().ToInternalValue()); 438 add_smt.BindInt64(4, GetTestCertExpirationTime().ToInternalValue());
451 add_smt.BindInt64(5, base::Time::Now().ToInternalValue()); 439 add_smt.BindInt64(5, base::Time::Now().ToInternalValue());
452 ASSERT_TRUE(add_smt.Run()); 440 ASSERT_TRUE(add_smt.Run());
453 } 441 }
454 442
455 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 443 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
456 store_ = new SQLiteChannelIDStore( 444 store_ =
457 v4_db_path, base::MessageLoopProxy::current(), NULL); 445 new SQLiteChannelIDStore(v4_db_path, base::MessageLoopProxy::current());
458 446
459 // Load the database and ensure the certs can be read. 447 // Load the database and ensure the certs can be read.
460 Load(&channel_ids); 448 Load(&channel_ids);
461 // Only the ECDSA cert (for google.com) is read, the RSA one is discarded. 449 // Only the ECDSA cert (for google.com) is read, the RSA one is discarded.
462 ASSERT_EQ(1U, channel_ids.size()); 450 ASSERT_EQ(1U, channel_ids.size());
463 451
464 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); 452 ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
465 ASSERT_EQ(GetTestCertExpirationTime(), 453 ASSERT_EQ(GetTestCertExpirationTime(), channel_ids[0]->expiration_time());
466 channel_ids[0]->expiration_time());
467 ASSERT_EQ(key_data, channel_ids[0]->private_key()); 454 ASSERT_EQ(key_data, channel_ids[0]->private_key());
468 ASSERT_EQ(cert_data, channel_ids[0]->cert()); 455 ASSERT_EQ(cert_data, channel_ids[0]->cert());
469 456
470 store_ = NULL; 457 store_ = NULL;
471 // Make sure we wait until the destructor has run. 458 // Make sure we wait until the destructor has run.
472 base::RunLoop().RunUntilIdle(); 459 base::RunLoop().RunUntilIdle();
473 } 460 }
461
462 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698