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

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

Issue 2095523002: Make //crypto factories return std::unique_ptr<>s (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix comment Created 4 years, 6 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 "net/extras/sqlite/sqlite_channel_id_store.h" 5 #include "net/extras/sqlite/sqlite_channel_id_store.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
davidben 2016/06/23 03:09:36 No longer used I think.
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
17 #include "crypto/ec_private_key.h" 17 #include "crypto/ec_private_key.h"
18 #include "net/cert/asn1_util.h" 18 #include "net/cert/asn1_util.h"
19 #include "net/ssl/channel_id_service.h" 19 #include "net/ssl/channel_id_service.h"
20 #include "net/ssl/ssl_client_cert_type.h" 20 #include "net/ssl/ssl_client_cert_type.h"
21 #include "net/test/cert_test_util.h" 21 #include "net/test/cert_test_util.h"
22 #include "net/test/channel_id_test_util.h" 22 #include "net/test/channel_id_test_util.h"
23 #include "net/test/test_data_directory.h" 23 #include "net/test/test_data_directory.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 base::FilePath cert_path = 59 base::FilePath cert_path =
60 GetTestCertsDirectory().AppendASCII("unittest.originbound.der"); 60 GetTestCertsDirectory().AppendASCII("unittest.originbound.der");
61 ASSERT_TRUE(base::ReadFileToString(key_path, key_data)); 61 ASSERT_TRUE(base::ReadFileToString(key_path, key_data));
62 ASSERT_TRUE(base::ReadFileToString(cert_path, cert_data)); 62 ASSERT_TRUE(base::ReadFileToString(cert_path, cert_data));
63 std::vector<uint8_t> private_key(key_data->size()); 63 std::vector<uint8_t> private_key(key_data->size());
64 memcpy(private_key.data(), key_data->data(), key_data->size()); 64 memcpy(private_key.data(), key_data->data(), key_data->size());
65 base::StringPiece spki; 65 base::StringPiece spki;
66 ASSERT_TRUE(asn1::ExtractSPKIFromDERCert(*cert_data, &spki)); 66 ASSERT_TRUE(asn1::ExtractSPKIFromDERCert(*cert_data, &spki));
67 std::vector<uint8_t> public_key(spki.size()); 67 std::vector<uint8_t> public_key(spki.size());
68 memcpy(public_key.data(), spki.data(), spki.size()); 68 memcpy(public_key.data(), spki.data(), spki.size());
69 key->reset(crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo( 69 *key = crypto::ECPrivateKey::CreateFromEncryptedPrivateKeyInfo(
70 ChannelIDService::kEPKIPassword, private_key, public_key)); 70 ChannelIDService::kEPKIPassword, private_key, public_key);
71 } 71 }
72 72
73 static base::Time GetTestCertExpirationTime() { 73 static base::Time GetTestCertExpirationTime() {
74 // Cert expiration time from 'openssl asn1parse -inform der -in 74 // Cert expiration time from 'openssl asn1parse -inform der -in
75 // unittest.originbound.der': 75 // unittest.originbound.der':
76 // UTCTIME :160507022239Z 76 // UTCTIME :160507022239Z
77 // base::Time::FromUTCExploded can't generate values past 2038 on 32-bit 77 // base::Time::FromUTCExploded can't generate values past 2038 on 32-bit
78 // linux, so we use the raw value here. 78 // linux, so we use the raw value here.
79 base::Time::Exploded exploded_time; 79 base::Time::Exploded exploded_time;
80 exploded_time.year = 2016; 80 exploded_time.year = 2016;
(...skipping 23 matching lines...) Expand all
104 104
105 void SetUp() override { 105 void SetUp() override {
106 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 106 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
107 store_ = new SQLiteChannelIDStore( 107 store_ = new SQLiteChannelIDStore(
108 temp_dir_.path().Append(kTestChannelIDFilename), 108 temp_dir_.path().Append(kTestChannelIDFilename),
109 base::ThreadTaskRunnerHandle::Get()); 109 base::ThreadTaskRunnerHandle::Get());
110 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; 110 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids;
111 Load(&channel_ids); 111 Load(&channel_ids);
112 ASSERT_EQ(0u, channel_ids.size()); 112 ASSERT_EQ(0u, channel_ids.size());
113 // Make sure the store gets written at least once. 113 // Make sure the store gets written at least once.
114 google_key_.reset(crypto::ECPrivateKey::Create()); 114 google_key_ = crypto::ECPrivateKey::Create();
115 store_->AddChannelID(DefaultChannelIDStore::ChannelID( 115 store_->AddChannelID(DefaultChannelIDStore::ChannelID(
116 "google.com", base::Time::FromInternalValue(1), 116 "google.com", base::Time::FromInternalValue(1), google_key_->Copy()));
117 base::WrapUnique(google_key_->Copy())));
118 } 117 }
119 118
120 base::ScopedTempDir temp_dir_; 119 base::ScopedTempDir temp_dir_;
121 scoped_refptr<SQLiteChannelIDStore> store_; 120 scoped_refptr<SQLiteChannelIDStore> store_;
122 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids_; 121 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids_;
123 std::unique_ptr<crypto::ECPrivateKey> google_key_; 122 std::unique_ptr<crypto::ECPrivateKey> google_key_;
124 }; 123 };
125 124
126 // Test if data is stored as expected in the SQLite database. 125 // Test if data is stored as expected in the SQLite database.
127 TEST_F(SQLiteChannelIDStoreTest, TestPersistence) { 126 TEST_F(SQLiteChannelIDStoreTest, TestPersistence) {
128 std::unique_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create()); 127 std::unique_ptr<crypto::ECPrivateKey> foo_key(crypto::ECPrivateKey::Create());
129 store_->AddChannelID(DefaultChannelIDStore::ChannelID( 128 store_->AddChannelID(DefaultChannelIDStore::ChannelID(
130 "foo.com", base::Time::FromInternalValue(3), 129 "foo.com", base::Time::FromInternalValue(3), foo_key->Copy()));
131 base::WrapUnique(foo_key->Copy())));
132 130
133 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; 131 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids;
134 // Replace the store effectively destroying the current one and forcing it 132 // Replace the store effectively destroying the current one and forcing it
135 // to write its data to disk. Then we can see if after loading it again it 133 // to write its data to disk. Then we can see if after loading it again it
136 // is still there. 134 // is still there.
137 store_ = NULL; 135 store_ = NULL;
138 // Make sure we wait until the destructor has run. 136 // Make sure we wait until the destructor has run.
139 base::RunLoop().RunUntilIdle(); 137 base::RunLoop().RunUntilIdle();
140 store_ = 138 store_ =
141 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), 139 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename),
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 // Close the store. 175 // Close the store.
178 store_ = NULL; 176 store_ = NULL;
179 // Make sure we wait until the destructor has run. 177 // Make sure we wait until the destructor has run.
180 base::RunLoop().RunUntilIdle(); 178 base::RunLoop().RunUntilIdle();
181 } 179 }
182 180
183 // Test if data is stored as expected in the SQLite database. 181 // Test if data is stored as expected in the SQLite database.
184 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) { 182 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) {
185 store_->AddChannelID(DefaultChannelIDStore::ChannelID( 183 store_->AddChannelID(DefaultChannelIDStore::ChannelID(
186 "foo.com", base::Time::FromInternalValue(3), 184 "foo.com", base::Time::FromInternalValue(3),
187 base::WrapUnique(crypto::ECPrivateKey::Create()))); 185 crypto::ECPrivateKey::Create()));
188 186
189 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids; 187 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>> channel_ids;
190 // Replace the store effectively destroying the current one and forcing it 188 // Replace the store effectively destroying the current one and forcing it
191 // to write its data to disk. Then we can see if after loading it again it 189 // to write its data to disk. Then we can see if after loading it again it
192 // is still there. 190 // is still there.
193 store_ = NULL; 191 store_ = NULL;
194 // Make sure we wait until the destructor has run. 192 // Make sure we wait until the destructor has run.
195 base::RunLoop().RunUntilIdle(); 193 base::RunLoop().RunUntilIdle();
196 store_ = 194 store_ =
197 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename), 195 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename),
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 sql::Statement smt(db.GetUniqueStatement( 533 sql::Statement smt(db.GetUniqueStatement(
536 "SELECT value FROM meta WHERE key = \"version\"")); 534 "SELECT value FROM meta WHERE key = \"version\""));
537 ASSERT_TRUE(smt.Step()); 535 ASSERT_TRUE(smt.Step());
538 EXPECT_EQ(5, smt.ColumnInt(0)); 536 EXPECT_EQ(5, smt.ColumnInt(0));
539 EXPECT_FALSE(smt.Step()); 537 EXPECT_FALSE(smt.Step());
540 } 538 }
541 } 539 }
542 } 540 }
543 541
544 } // namespace net 542 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698