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

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

Powered by Google App Engine
This is Rietveld 408576698