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

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

Issue 1476263004: Remove ScopedVector from IDStores (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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/files/file_util.h" 6 #include "base/files/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"
10 #include "base/run_loop.h" 9 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
12 #include "crypto/ec_private_key.h" 11 #include "crypto/ec_private_key.h"
13 #include "net/base/test_data_directory.h" 12 #include "net/base/test_data_directory.h"
14 #include "net/cert/asn1_util.h" 13 #include "net/cert/asn1_util.h"
15 #include "net/extras/sqlite/sqlite_channel_id_store.h" 14 #include "net/extras/sqlite/sqlite_channel_id_store.h"
16 #include "net/ssl/channel_id_service.h" 15 #include "net/ssl/channel_id_service.h"
17 #include "net/ssl/ssl_client_cert_type.h" 16 #include "net/ssl/ssl_client_cert_type.h"
18 #include "net/test/cert_test_util.h" 17 #include "net/test/cert_test_util.h"
19 #include "net/test/channel_id_test_util.h" 18 #include "net/test/channel_id_test_util.h"
20 #include "sql/statement.h" 19 #include "sql/statement.h"
21 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
22 21
23 namespace net { 22 namespace net {
24 23
25 const base::FilePath::CharType kTestChannelIDFilename[] = 24 const base::FilePath::CharType kTestChannelIDFilename[] =
26 FILE_PATH_LITERAL("ChannelID"); 25 FILE_PATH_LITERAL("ChannelID");
27 26
28 class SQLiteChannelIDStoreTest : public testing::Test { 27 class SQLiteChannelIDStoreTest : public testing::Test {
29 public: 28 public:
30 void Load(ScopedVector<DefaultChannelIDStore::ChannelID>* channel_ids) { 29 void Load(
30 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>>* channel_ids) {
mmenke 2015/11/30 20:33:16 include vector, scoped_ptr
31 base::RunLoop run_loop; 31 base::RunLoop run_loop;
32 store_->Load(base::Bind(&SQLiteChannelIDStoreTest::OnLoaded, 32 store_->Load(base::Bind(&SQLiteChannelIDStoreTest::OnLoaded,
33 base::Unretained(this), 33 base::Unretained(this),
34 &run_loop)); 34 &run_loop));
35 run_loop.Run(); 35 run_loop.Run();
36 channel_ids->swap(channel_ids_); 36 channel_ids->swap(channel_ids_);
37 channel_ids_.clear(); 37 channel_ids_.clear();
38 } 38 }
39 39
40 void OnLoaded( 40 void OnLoaded(
41 base::RunLoop* run_loop, 41 base::RunLoop* run_loop,
42 scoped_ptr<ScopedVector<DefaultChannelIDStore::ChannelID> > channel_ids) { 42 scoped_ptr<std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>>>
43 channel_ids) {
43 channel_ids_.swap(*channel_ids); 44 channel_ids_.swap(*channel_ids);
44 run_loop->Quit(); 45 run_loop->Quit();
45 } 46 }
46 47
47 protected: 48 protected:
48 static void ReadTestKeyAndCert(std::string* key_data, 49 static void ReadTestKeyAndCert(std::string* key_data,
49 std::string* cert_data, 50 std::string* cert_data,
50 scoped_ptr<crypto::ECPrivateKey>* key) { 51 scoped_ptr<crypto::ECPrivateKey>* key) {
51 base::FilePath key_path = 52 base::FilePath key_path =
52 GetTestCertsDirectory().AppendASCII("unittest.originbound.key.der"); 53 GetTestCertsDirectory().AppendASCII("unittest.originbound.key.der");
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 exploded_time.second = 39; 95 exploded_time.second = 39;
95 exploded_time.millisecond = 0; 96 exploded_time.millisecond = 0;
96 return base::Time::FromUTCExploded(exploded_time); 97 return base::Time::FromUTCExploded(exploded_time);
97 } 98 }
98 99
99 void SetUp() override { 100 void SetUp() override {
100 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
101 store_ = new SQLiteChannelIDStore( 102 store_ = new SQLiteChannelIDStore(
102 temp_dir_.path().Append(kTestChannelIDFilename), 103 temp_dir_.path().Append(kTestChannelIDFilename),
103 base::ThreadTaskRunnerHandle::Get()); 104 base::ThreadTaskRunnerHandle::Get());
104 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; 105 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids;
105 Load(&channel_ids); 106 Load(&channel_ids);
106 ASSERT_EQ(0u, channel_ids.size()); 107 ASSERT_EQ(0u, channel_ids.size());
107 // Make sure the store gets written at least once. 108 // Make sure the store gets written at least once.
108 google_key_.reset(crypto::ECPrivateKey::Create()); 109 google_key_.reset(crypto::ECPrivateKey::Create());
109 store_->AddChannelID(DefaultChannelIDStore::ChannelID( 110 store_->AddChannelID(DefaultChannelIDStore::ChannelID(
110 "google.com", base::Time::FromInternalValue(1), 111 "google.com", base::Time::FromInternalValue(1),
111 make_scoped_ptr(google_key_->Copy()))); 112 make_scoped_ptr(google_key_->Copy())));
112 } 113 }
113 114
114 base::ScopedTempDir temp_dir_; 115 base::ScopedTempDir temp_dir_;
115 scoped_refptr<SQLiteChannelIDStore> store_; 116 scoped_refptr<SQLiteChannelIDStore> store_;
116 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids_; 117 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids_;
117 scoped_ptr<crypto::ECPrivateKey> google_key_; 118 scoped_ptr<crypto::ECPrivateKey> google_key_;
118 }; 119 };
119 120
120 // Test if data is stored as expected in the SQLite database. 121 // Test if data is stored as expected in the SQLite database.
121 TEST_F(SQLiteChannelIDStoreTest, TestPersistence) { 122 TEST_F(SQLiteChannelIDStoreTest, TestPersistence) {
122 scoped_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); 123 scoped_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create());
123 store_->AddChannelID(DefaultChannelIDStore::ChannelID( 124 store_->AddChannelID(DefaultChannelIDStore::ChannelID(
124 "foo.com", base::Time::FromInternalValue(3), 125 "foo.com", base::Time::FromInternalValue(3),
125 make_scoped_ptr(foo_key->Copy()))); 126 make_scoped_ptr(foo_key->Copy())));
126 127
127 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; 128 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids;
128 // Replace the store effectively destroying the current one and forcing it 129 // Replace the store effectively destroying the current one and forcing it
129 // to write its data to disk. Then we can see if after loading it again it 130 // to write its data to disk. Then we can see if after loading it again it
130 // is still there. 131 // is still there.
131 store_ = NULL; 132 store_ = NULL;
132 // Make sure we wait until the destructor has run. 133 // Make sure we wait until the destructor has run.
133 base::RunLoop().RunUntilIdle(); 134 base::RunLoop().RunUntilIdle();
134 store_ = 135 store_ =
135 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), 136 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename),
136 base::ThreadTaskRunnerHandle::Get()); 137 base::ThreadTaskRunnerHandle::Get());
137 138
138 // Reload and test for persistence 139 // Reload and test for persistence
139 Load(&channel_ids); 140 Load(&channel_ids);
140 ASSERT_EQ(2U, channel_ids.size()); 141 ASSERT_EQ(2U, channel_ids.size());
141 DefaultChannelIDStore::ChannelID* goog_channel_id; 142 scoped_ptr<DefaultChannelIDStore::ChannelID> goog_channel_id;
142 DefaultChannelIDStore::ChannelID* foo_channel_id; 143 scoped_ptr<DefaultChannelIDStore::ChannelID> foo_channel_id;
143 if (channel_ids[0]->server_identifier() == "google.com") { 144 if (channel_ids[0]->server_identifier() == "google.com") {
144 goog_channel_id = channel_ids[0]; 145 goog_channel_id = std::move(channel_ids[0]);
145 foo_channel_id = channel_ids[1]; 146 foo_channel_id = std::move(channel_ids[1]);
146 } else { 147 } else {
147 goog_channel_id = channel_ids[1]; 148 goog_channel_id = std::move(channel_ids[1]);
148 foo_channel_id = channel_ids[0]; 149 foo_channel_id = std::move(channel_ids[0]);
mmenke 2015/11/30 20:33:16 Again, not sure this really gets us much.
149 } 150 }
150 ASSERT_EQ("google.com", goog_channel_id->server_identifier()); 151 ASSERT_EQ("google.com", goog_channel_id->server_identifier());
151 EXPECT_TRUE(KeysEqual(google_key_.get(), goog_channel_id->key())); 152 EXPECT_TRUE(KeysEqual(google_key_.get(), goog_channel_id->key()));
152 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue()); 153 ASSERT_EQ(1, goog_channel_id->creation_time().ToInternalValue());
153 ASSERT_EQ("foo.com", foo_channel_id->server_identifier()); 154 ASSERT_EQ("foo.com", foo_channel_id->server_identifier());
154 EXPECT_TRUE(KeysEqual(foo_key.get(), foo_channel_id->key())); 155 EXPECT_TRUE(KeysEqual(foo_key.get(), foo_channel_id->key()));
155 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue()); 156 ASSERT_EQ(3, foo_channel_id->creation_time().ToInternalValue());
156 157
157 // Now delete the keypair and check persistence again. 158 // Now delete the keypair and check persistence again.
158 store_->DeleteChannelID(*channel_ids[0]); 159 store_->DeleteChannelID(*goog_channel_id);
159 store_->DeleteChannelID(*channel_ids[1]); 160 store_->DeleteChannelID(*foo_channel_id);
160 store_ = NULL; 161 store_ = NULL;
161 // Make sure we wait until the destructor has run. 162 // Make sure we wait until the destructor has run.
162 base::RunLoop().RunUntilIdle(); 163 base::RunLoop().RunUntilIdle();
163 channel_ids.clear(); 164 channel_ids.clear();
164 store_ = 165 store_ =
165 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), 166 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename),
166 base::ThreadTaskRunnerHandle::Get()); 167 base::ThreadTaskRunnerHandle::Get());
167 168
168 // Reload and check if the keypair has been removed. 169 // Reload and check if the keypair has been removed.
169 Load(&channel_ids); 170 Load(&channel_ids);
170 ASSERT_EQ(0U, channel_ids.size()); 171 ASSERT_EQ(0U, channel_ids.size());
171 // Close the store. 172 // Close the store.
172 store_ = NULL; 173 store_ = NULL;
173 // Make sure we wait until the destructor has run. 174 // Make sure we wait until the destructor has run.
174 base::RunLoop().RunUntilIdle(); 175 base::RunLoop().RunUntilIdle();
175 } 176 }
176 177
177 // Test if data is stored as expected in the SQLite database. 178 // Test if data is stored as expected in the SQLite database.
178 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) { 179 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) {
179 store_->AddChannelID(DefaultChannelIDStore::ChannelID( 180 store_->AddChannelID(DefaultChannelIDStore::ChannelID(
180 "foo.com", base::Time::FromInternalValue(3), 181 "foo.com", base::Time::FromInternalValue(3),
181 make_scoped_ptr(crypto::ECPrivateKey::Create()))); 182 make_scoped_ptr(crypto::ECPrivateKey::Create())));
182 183
183 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; 184 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids;
184 // Replace the store effectively destroying the current one and forcing it 185 // Replace the store effectively destroying the current one and forcing it
185 // to write its data to disk. Then we can see if after loading it again it 186 // to write its data to disk. Then we can see if after loading it again it
186 // is still there. 187 // is still there.
187 store_ = NULL; 188 store_ = NULL;
188 // Make sure we wait until the destructor has run. 189 // Make sure we wait until the destructor has run.
189 base::RunLoop().RunUntilIdle(); 190 base::RunLoop().RunUntilIdle();
190 store_ = 191 store_ =
191 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), 192 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename),
192 base::ThreadTaskRunnerHandle::Get()); 193 base::ThreadTaskRunnerHandle::Get());
193 194
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 "INSERT INTO \"origin_bound_certs\" VALUES(" 256 "INSERT INTO \"origin_bound_certs\" VALUES("
256 "'foo.com',X'AA',X'BB');")); 257 "'foo.com',X'AA',X'BB');"));
257 } 258 }
258 259
259 // Load and test the DB contents twice. First time ensures that we can use 260 // Load and test the DB contents twice. First time ensures that we can use
260 // the updated values immediately. Second time ensures that the updated 261 // the updated values immediately. Second time ensures that the updated
261 // values are stored and read correctly on next load. 262 // values are stored and read correctly on next load.
262 for (int i = 0; i < 2; ++i) { 263 for (int i = 0; i < 2; ++i) {
263 SCOPED_TRACE(i); 264 SCOPED_TRACE(i);
264 265
265 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; 266 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids;
266 store_ = new SQLiteChannelIDStore(v1_db_path, 267 store_ = new SQLiteChannelIDStore(v1_db_path,
267 base::ThreadTaskRunnerHandle::Get()); 268 base::ThreadTaskRunnerHandle::Get());
268 269
269 // Load the database. Because the existing v1 certs are implicitly of type 270 // Load the database. Because the existing v1 certs are implicitly of type
270 // RSA, which is unsupported, they're discarded. 271 // RSA, which is unsupported, they're discarded.
271 Load(&channel_ids); 272 Load(&channel_ids);
272 ASSERT_EQ(0U, channel_ids.size()); 273 ASSERT_EQ(0U, channel_ids.size());
273 274
274 store_ = NULL; 275 store_ = NULL;
275 base::RunLoop().RunUntilIdle(); 276 base::RunLoop().RunUntilIdle();
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 "INSERT INTO \"origin_bound_certs\" VALUES(" 328 "INSERT INTO \"origin_bound_certs\" VALUES("
328 "'foo.com',X'AA',X'BB',64);")); 329 "'foo.com',X'AA',X'BB',64);"));
329 } 330 }
330 331
331 // Load and test the DB contents twice. First time ensures that we can use 332 // Load and test the DB contents twice. First time ensures that we can use
332 // the updated values immediately. Second time ensures that the updated 333 // the updated values immediately. Second time ensures that the updated
333 // values are saved and read correctly on next load. 334 // values are saved and read correctly on next load.
334 for (int i = 0; i < 2; ++i) { 335 for (int i = 0; i < 2; ++i) {
335 SCOPED_TRACE(i); 336 SCOPED_TRACE(i);
336 337
337 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; 338 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids;
338 store_ = new SQLiteChannelIDStore(v2_db_path, 339 store_ = new SQLiteChannelIDStore(v2_db_path,
339 base::ThreadTaskRunnerHandle::Get()); 340 base::ThreadTaskRunnerHandle::Get());
340 341
341 // Load the database and ensure the certs can be read. 342 // Load the database and ensure the certs can be read.
342 Load(&channel_ids); 343 Load(&channel_ids);
343 ASSERT_EQ(1U, channel_ids.size()); 344 ASSERT_EQ(1U, channel_ids.size());
344 345
345 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); 346 ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
346 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); 347 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time());
347 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key())); 348 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key()));
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 "INSERT INTO \"origin_bound_certs\" VALUES(" 406 "INSERT INTO \"origin_bound_certs\" VALUES("
406 "'foo.com',X'AA',X'BB',64,2000);")); 407 "'foo.com',X'AA',X'BB',64,2000);"));
407 } 408 }
408 409
409 // Load and test the DB contents twice. First time ensures that we can use 410 // Load and test the DB contents twice. First time ensures that we can use
410 // the updated values immediately. Second time ensures that the updated 411 // the updated values immediately. Second time ensures that the updated
411 // values are saved and read correctly on next load. 412 // values are saved and read correctly on next load.
412 for (int i = 0; i < 2; ++i) { 413 for (int i = 0; i < 2; ++i) {
413 SCOPED_TRACE(i); 414 SCOPED_TRACE(i);
414 415
415 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; 416 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids;
416 store_ = new SQLiteChannelIDStore(v3_db_path, 417 store_ = new SQLiteChannelIDStore(v3_db_path,
417 base::ThreadTaskRunnerHandle::Get()); 418 base::ThreadTaskRunnerHandle::Get());
418 419
419 // Load the database and ensure the certs can be read. 420 // Load the database and ensure the certs can be read.
420 Load(&channel_ids); 421 Load(&channel_ids);
421 ASSERT_EQ(1U, channel_ids.size()); 422 ASSERT_EQ(1U, channel_ids.size());
422 423
423 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); 424 ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
424 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); 425 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time());
425 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key())); 426 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key()));
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 "INSERT INTO \"origin_bound_certs\" VALUES(" 500 "INSERT INTO \"origin_bound_certs\" VALUES("
500 "'bar.com',X'AA',X'BB',64,2000,3000);")); 501 "'bar.com',X'AA',X'BB',64,2000,3000);"));
501 } 502 }
502 503
503 // Load and test the DB contents twice. First time ensures that we can use 504 // Load and test the DB contents twice. First time ensures that we can use
504 // the updated values immediately. Second time ensures that the updated 505 // the updated values immediately. Second time ensures that the updated
505 // values are saved and read correctly on next load. 506 // values are saved and read correctly on next load.
506 for (int i = 0; i < 2; ++i) { 507 for (int i = 0; i < 2; ++i) {
507 SCOPED_TRACE(i); 508 SCOPED_TRACE(i);
508 509
509 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids; 510 std::vector<scoped_ptr<DefaultChannelIDStore::ChannelID>> channel_ids;
510 store_ = new SQLiteChannelIDStore(v4_db_path, 511 store_ = new SQLiteChannelIDStore(v4_db_path,
511 base::ThreadTaskRunnerHandle::Get()); 512 base::ThreadTaskRunnerHandle::Get());
512 513
513 // Load the database and ensure the certs can be read. 514 // Load the database and ensure the certs can be read.
514 Load(&channel_ids); 515 Load(&channel_ids);
515 ASSERT_EQ(1U, channel_ids.size()); 516 ASSERT_EQ(1U, channel_ids.size());
516 517
517 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); 518 ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
518 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time()); 519 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time());
519 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key())); 520 EXPECT_TRUE(KeysEqual(key.get(), channel_ids[0]->key()));
520 521
521 store_ = NULL; 522 store_ = NULL;
522 // Make sure we wait until the destructor has run. 523 // Make sure we wait until the destructor has run.
523 base::RunLoop().RunUntilIdle(); 524 base::RunLoop().RunUntilIdle();
524 525
525 // Verify the database version is updated. 526 // Verify the database version is updated.
526 { 527 {
527 sql::Connection db; 528 sql::Connection db;
528 ASSERT_TRUE(db.Open(v4_db_path)); 529 ASSERT_TRUE(db.Open(v4_db_path));
529 sql::Statement smt(db.GetUniqueStatement( 530 sql::Statement smt(db.GetUniqueStatement(
530 "SELECT value FROM meta WHERE key = \"version\"")); 531 "SELECT value FROM meta WHERE key = \"version\""));
531 ASSERT_TRUE(smt.Step()); 532 ASSERT_TRUE(smt.Step());
532 EXPECT_EQ(5, smt.ColumnInt(0)); 533 EXPECT_EQ(5, smt.ColumnInt(0));
533 EXPECT_FALSE(smt.Step()); 534 EXPECT_FALSE(smt.Step());
534 } 535 }
535 } 536 }
536 } 537 }
537 538
538 } // namespace net 539 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698