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

Side by Side Diff: net/extras/sqlite/sqlite_channel_id_store_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698