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

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: Add DeleteAll(server_identifiers) method. 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
157 // Test if data is stored as expected in the SQLite database.
158 TEST_F(SQLiteChannelIDStoreTest, TestDeleteAll) {
159 store_->AddChannelID(
160 DefaultChannelIDStore::ChannelID("foo.com",
161 base::Time::FromInternalValue(3),
162 base::Time::FromInternalValue(4),
163 "c",
164 "d"));
165
166 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
167 // Replace the store effectively destroying the current one and forcing it
168 // to write its data to disk. Then we can see if after loading it again it
169 // is still there.
170 store_ = NULL;
171 // Make sure we wait until the destructor has run.
172 base::RunLoop().RunUntilIdle();
173 store_ =
174 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename),
175 base::MessageLoopProxy::current());
176
177 // Reload and test for persistence
178 Load(&channel_ids);
179 ASSERT_EQ(2U, channel_ids.size());
180 // DeleteAll except foo.com (shouldn't fail if one is missing either).
181 std::vector<std::string> delete_server_identifiers;
182 delete_server_identifiers.push_back("google.com");
183 delete_server_identifiers.push_back("missing.com");
184 store_->DeleteAll(delete_server_identifiers);
185
186 // Now check persistence again.
187 store_ = NULL;
188 // Make sure we wait until the destructor has run.
189 base::RunLoop().RunUntilIdle();
190 channel_ids.clear();
191 store_ =
192 new SQLiteChannelIDStore(temp_dir_.path().Append(kTestChannelIDFilename),
193 base::MessageLoopProxy::current());
194
195 // Reload and check that only foo.com persisted in store.
196 Load(&channel_ids);
197 ASSERT_EQ(1U, channel_ids.size());
198 ASSERT_EQ("foo.com", channel_ids[0]->server_identifier());
199 }
200
161 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV1) { 201 TEST_F(SQLiteChannelIDStoreTest, TestUpgradeV1) {
162 // Reset the store. We'll be using a different database for this test. 202 // Reset the store. We'll be using a different database for this test.
163 store_ = NULL; 203 store_ = NULL;
164 204
165 base::FilePath v1_db_path(temp_dir_.path().AppendASCII("v1db")); 205 base::FilePath v1_db_path(temp_dir_.path().AppendASCII("v1db"));
166 206
167 std::string key_data; 207 std::string key_data;
168 std::string cert_data; 208 std::string cert_data;
169 ReadTestKeyAndCert(&key_data, &cert_data); 209 ReadTestKeyAndCert(&key_data, &cert_data);
170 210
171 // Create a version 1 database. 211 // Create a version 1 database.
172 { 212 {
173 sql::Connection db; 213 sql::Connection db;
174 ASSERT_TRUE(db.Open(v1_db_path)); 214 ASSERT_TRUE(db.Open(v1_db_path));
175 ASSERT_TRUE(db.Execute( 215 ASSERT_TRUE(db.Execute(
176 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," 216 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
177 "value LONGVARCHAR);" 217 "value LONGVARCHAR);"
178 "INSERT INTO \"meta\" VALUES('version','1');" 218 "INSERT INTO \"meta\" VALUES('version','1');"
179 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');" 219 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');"
180 "CREATE TABLE origin_bound_certs (" 220 "CREATE TABLE origin_bound_certs ("
181 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," 221 "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
182 "private_key BLOB NOT NULL,cert BLOB NOT NULL);")); 222 "private_key BLOB NOT NULL,cert BLOB NOT NULL);"));
183 223
184 sql::Statement add_smt(db.GetUniqueStatement( 224 sql::Statement add_smt(db.GetUniqueStatement(
185 "INSERT INTO origin_bound_certs (origin, private_key, cert) " 225 "INSERT INTO origin_bound_certs (origin, private_key, cert) "
186 "VALUES (?,?,?)")); 226 "VALUES (?,?,?)"));
187 add_smt.BindString(0, "google.com"); 227 add_smt.BindString(0, "google.com");
188 add_smt.BindBlob(1, key_data.data(), key_data.size()); 228 add_smt.BindBlob(1, key_data.data(), key_data.size());
189 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); 229 add_smt.BindBlob(2, cert_data.data(), cert_data.size());
190 ASSERT_TRUE(add_smt.Run()); 230 ASSERT_TRUE(add_smt.Run());
191 231
192 ASSERT_TRUE(db.Execute( 232 ASSERT_TRUE(db.Execute(
193 "INSERT INTO \"origin_bound_certs\" VALUES(" 233 "INSERT INTO \"origin_bound_certs\" VALUES("
194 "'foo.com',X'AA',X'BB');" 234 "'foo.com',X'AA',X'BB');"));
195 ));
196 } 235 }
197 236
198 // Load and test the DB contents twice. First time ensures that we can use 237 // 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 238 // the updated values immediately. Second time ensures that the updated
200 // values are stored and read correctly on next load. 239 // values are stored and read correctly on next load.
201 for (int i = 0; i < 2; ++i) { 240 for (int i = 0; i < 2; ++i) {
202 SCOPED_TRACE(i); 241 SCOPED_TRACE(i);
203 242
204 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 243 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
205 store_ = new SQLiteChannelIDStore( 244 store_ =
206 v1_db_path, base::MessageLoopProxy::current(), NULL); 245 new SQLiteChannelIDStore(v1_db_path, base::MessageLoopProxy::current());
207 246
208 // Load the database. Because the existing v1 certs are implicitly of type 247 // Load the database. Because the existing v1 certs are implicitly of type
209 // RSA, which is unsupported, they're discarded. 248 // RSA, which is unsupported, they're discarded.
210 Load(&channel_ids); 249 Load(&channel_ids);
211 ASSERT_EQ(0U, channel_ids.size()); 250 ASSERT_EQ(0U, channel_ids.size());
212 251
213 store_ = NULL; 252 store_ = NULL;
214 base::RunLoop().RunUntilIdle(); 253 base::RunLoop().RunUntilIdle();
215 254
216 // Verify the database version is updated. 255 // Verify the database version is updated.
(...skipping 18 matching lines...) Expand all
235 std::string key_data; 274 std::string key_data;
236 std::string cert_data; 275 std::string cert_data;
237 ReadTestKeyAndCert(&key_data, &cert_data); 276 ReadTestKeyAndCert(&key_data, &cert_data);
238 277
239 // Create a version 2 database. 278 // Create a version 2 database.
240 { 279 {
241 sql::Connection db; 280 sql::Connection db;
242 ASSERT_TRUE(db.Open(v2_db_path)); 281 ASSERT_TRUE(db.Open(v2_db_path));
243 ASSERT_TRUE(db.Execute( 282 ASSERT_TRUE(db.Execute(
244 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," 283 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
245 "value LONGVARCHAR);" 284 "value LONGVARCHAR);"
246 "INSERT INTO \"meta\" VALUES('version','2');" 285 "INSERT INTO \"meta\" VALUES('version','2');"
247 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');" 286 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');"
248 "CREATE TABLE origin_bound_certs (" 287 "CREATE TABLE origin_bound_certs ("
249 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," 288 "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
250 "private_key BLOB NOT NULL," 289 "private_key BLOB NOT NULL,"
251 "cert BLOB NOT NULL," 290 "cert BLOB NOT NULL,"
252 "cert_type INTEGER);" 291 "cert_type INTEGER);"));
253 ));
254 292
255 sql::Statement add_smt(db.GetUniqueStatement( 293 sql::Statement add_smt(db.GetUniqueStatement(
256 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type) " 294 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type) "
257 "VALUES (?,?,?,?)")); 295 "VALUES (?,?,?,?)"));
258 add_smt.BindString(0, "google.com"); 296 add_smt.BindString(0, "google.com");
259 add_smt.BindBlob(1, key_data.data(), key_data.size()); 297 add_smt.BindBlob(1, key_data.data(), key_data.size());
260 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); 298 add_smt.BindBlob(2, cert_data.data(), cert_data.size());
261 add_smt.BindInt64(3, 64); 299 add_smt.BindInt64(3, 64);
262 ASSERT_TRUE(add_smt.Run()); 300 ASSERT_TRUE(add_smt.Run());
263 301
264 ASSERT_TRUE(db.Execute( 302 ASSERT_TRUE(db.Execute(
265 "INSERT INTO \"origin_bound_certs\" VALUES(" 303 "INSERT INTO \"origin_bound_certs\" VALUES("
266 "'foo.com',X'AA',X'BB',64);" 304 "'foo.com',X'AA',X'BB',64);"));
267 ));
268 } 305 }
269 306
270 // Load and test the DB contents twice. First time ensures that we can use 307 // 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 308 // the updated values immediately. Second time ensures that the updated
272 // values are saved and read correctly on next load. 309 // values are saved and read correctly on next load.
273 for (int i = 0; i < 2; ++i) { 310 for (int i = 0; i < 2; ++i) {
274 SCOPED_TRACE(i); 311 SCOPED_TRACE(i);
275 312
276 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 313 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
277 store_ = new SQLiteChannelIDStore( 314 store_ =
278 v2_db_path, base::MessageLoopProxy::current(), NULL); 315 new SQLiteChannelIDStore(v2_db_path, base::MessageLoopProxy::current());
279 316
280 // Load the database and ensure the certs can be read. 317 // Load the database and ensure the certs can be read.
281 Load(&channel_ids); 318 Load(&channel_ids);
282 ASSERT_EQ(2U, channel_ids.size()); 319 ASSERT_EQ(2U, channel_ids.size());
283 320
284 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); 321 ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
285 ASSERT_EQ(GetTestCertExpirationTime(), 322 ASSERT_EQ(GetTestCertExpirationTime(), channel_ids[0]->expiration_time());
286 channel_ids[0]->expiration_time());
287 ASSERT_EQ(key_data, channel_ids[0]->private_key()); 323 ASSERT_EQ(key_data, channel_ids[0]->private_key());
288 ASSERT_EQ(cert_data, channel_ids[0]->cert()); 324 ASSERT_EQ(cert_data, channel_ids[0]->cert());
289 325
290 ASSERT_EQ("foo.com", channel_ids[1]->server_identifier()); 326 ASSERT_EQ("foo.com", channel_ids[1]->server_identifier());
291 // Undecodable cert, expiration time will be uninitialized. 327 // Undecodable cert, expiration time will be uninitialized.
292 ASSERT_EQ(base::Time(), channel_ids[1]->expiration_time()); 328 ASSERT_EQ(base::Time(), channel_ids[1]->expiration_time());
293 ASSERT_STREQ("\xaa", channel_ids[1]->private_key().c_str()); 329 ASSERT_STREQ("\xaa", channel_ids[1]->private_key().c_str());
294 ASSERT_STREQ("\xbb", channel_ids[1]->cert().c_str()); 330 ASSERT_STREQ("\xbb", channel_ids[1]->cert().c_str());
295 331
296 store_ = NULL; 332 store_ = NULL;
(...skipping 22 matching lines...) Expand all
319 std::string key_data; 355 std::string key_data;
320 std::string cert_data; 356 std::string cert_data;
321 ReadTestKeyAndCert(&key_data, &cert_data); 357 ReadTestKeyAndCert(&key_data, &cert_data);
322 358
323 // Create a version 3 database. 359 // Create a version 3 database.
324 { 360 {
325 sql::Connection db; 361 sql::Connection db;
326 ASSERT_TRUE(db.Open(v3_db_path)); 362 ASSERT_TRUE(db.Open(v3_db_path));
327 ASSERT_TRUE(db.Execute( 363 ASSERT_TRUE(db.Execute(
328 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," 364 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
329 "value LONGVARCHAR);" 365 "value LONGVARCHAR);"
330 "INSERT INTO \"meta\" VALUES('version','3');" 366 "INSERT INTO \"meta\" VALUES('version','3');"
331 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');" 367 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');"
332 "CREATE TABLE origin_bound_certs (" 368 "CREATE TABLE origin_bound_certs ("
333 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," 369 "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
334 "private_key BLOB NOT NULL," 370 "private_key BLOB NOT NULL,"
335 "cert BLOB NOT NULL," 371 "cert BLOB NOT NULL,"
336 "cert_type INTEGER," 372 "cert_type INTEGER,"
337 "expiration_time INTEGER);" 373 "expiration_time INTEGER);"));
338 ));
339 374
340 sql::Statement add_smt(db.GetUniqueStatement( 375 sql::Statement add_smt(db.GetUniqueStatement(
341 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type, " 376 "INSERT INTO origin_bound_certs (origin, private_key, cert, cert_type, "
342 "expiration_time) VALUES (?,?,?,?,?)")); 377 "expiration_time) VALUES (?,?,?,?,?)"));
343 add_smt.BindString(0, "google.com"); 378 add_smt.BindString(0, "google.com");
344 add_smt.BindBlob(1, key_data.data(), key_data.size()); 379 add_smt.BindBlob(1, key_data.data(), key_data.size());
345 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); 380 add_smt.BindBlob(2, cert_data.data(), cert_data.size());
346 add_smt.BindInt64(3, 64); 381 add_smt.BindInt64(3, 64);
347 add_smt.BindInt64(4, 1000); 382 add_smt.BindInt64(4, 1000);
348 ASSERT_TRUE(add_smt.Run()); 383 ASSERT_TRUE(add_smt.Run());
349 384
350 ASSERT_TRUE(db.Execute( 385 ASSERT_TRUE(db.Execute(
351 "INSERT INTO \"origin_bound_certs\" VALUES(" 386 "INSERT INTO \"origin_bound_certs\" VALUES("
352 "'foo.com',X'AA',X'BB',64,2000);" 387 "'foo.com',X'AA',X'BB',64,2000);"));
353 ));
354 } 388 }
355 389
356 // Load and test the DB contents twice. First time ensures that we can use 390 // 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 391 // the updated values immediately. Second time ensures that the updated
358 // values are saved and read correctly on next load. 392 // values are saved and read correctly on next load.
359 for (int i = 0; i < 2; ++i) { 393 for (int i = 0; i < 2; ++i) {
360 SCOPED_TRACE(i); 394 SCOPED_TRACE(i);
361 395
362 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 396 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
363 store_ = new SQLiteChannelIDStore( 397 store_ =
364 v3_db_path, base::MessageLoopProxy::current(), NULL); 398 new SQLiteChannelIDStore(v3_db_path, base::MessageLoopProxy::current());
365 399
366 // Load the database and ensure the certs can be read. 400 // Load the database and ensure the certs can be read.
367 Load(&channel_ids); 401 Load(&channel_ids);
368 ASSERT_EQ(2U, channel_ids.size()); 402 ASSERT_EQ(2U, channel_ids.size());
369 403
370 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); 404 ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
371 ASSERT_EQ(1000, channel_ids[0]->expiration_time().ToInternalValue()); 405 ASSERT_EQ(1000, channel_ids[0]->expiration_time().ToInternalValue());
372 ASSERT_EQ(GetTestCertCreationTime(), 406 ASSERT_EQ(GetTestCertCreationTime(), channel_ids[0]->creation_time());
373 channel_ids[0]->creation_time());
374 ASSERT_EQ(key_data, channel_ids[0]->private_key()); 407 ASSERT_EQ(key_data, channel_ids[0]->private_key());
375 ASSERT_EQ(cert_data, channel_ids[0]->cert()); 408 ASSERT_EQ(cert_data, channel_ids[0]->cert());
376 409
377 ASSERT_EQ("foo.com", channel_ids[1]->server_identifier()); 410 ASSERT_EQ("foo.com", channel_ids[1]->server_identifier());
378 ASSERT_EQ(2000, channel_ids[1]->expiration_time().ToInternalValue()); 411 ASSERT_EQ(2000, channel_ids[1]->expiration_time().ToInternalValue());
379 // Undecodable cert, creation time will be uninitialized. 412 // Undecodable cert, creation time will be uninitialized.
380 ASSERT_EQ(base::Time(), channel_ids[1]->creation_time()); 413 ASSERT_EQ(base::Time(), channel_ids[1]->creation_time());
381 ASSERT_STREQ("\xaa", channel_ids[1]->private_key().c_str()); 414 ASSERT_STREQ("\xaa", channel_ids[1]->private_key().c_str());
382 ASSERT_STREQ("\xbb", channel_ids[1]->cert().c_str()); 415 ASSERT_STREQ("\xbb", channel_ids[1]->cert().c_str());
383 416
(...skipping 23 matching lines...) Expand all
407 std::string key_data; 440 std::string key_data;
408 std::string cert_data; 441 std::string cert_data;
409 ReadTestKeyAndCert(&key_data, &cert_data); 442 ReadTestKeyAndCert(&key_data, &cert_data);
410 443
411 // Create a version 4 database with a mix of RSA and ECDSA certs. 444 // Create a version 4 database with a mix of RSA and ECDSA certs.
412 { 445 {
413 sql::Connection db; 446 sql::Connection db;
414 ASSERT_TRUE(db.Open(v4_db_path)); 447 ASSERT_TRUE(db.Open(v4_db_path));
415 ASSERT_TRUE(db.Execute( 448 ASSERT_TRUE(db.Execute(
416 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY," 449 "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY,"
417 "value LONGVARCHAR);" 450 "value LONGVARCHAR);"
418 "INSERT INTO \"meta\" VALUES('version','4');" 451 "INSERT INTO \"meta\" VALUES('version','4');"
419 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');" 452 "INSERT INTO \"meta\" VALUES('last_compatible_version','1');"
420 "CREATE TABLE origin_bound_certs (" 453 "CREATE TABLE origin_bound_certs ("
421 "origin TEXT NOT NULL UNIQUE PRIMARY KEY," 454 "origin TEXT NOT NULL UNIQUE PRIMARY KEY,"
422 "private_key BLOB NOT NULL," 455 "private_key BLOB NOT NULL,"
423 "cert BLOB NOT NULL," 456 "cert BLOB NOT NULL,"
424 "cert_type INTEGER," 457 "cert_type INTEGER,"
425 "expiration_time INTEGER," 458 "expiration_time INTEGER,"
426 "creation_time INTEGER);" 459 "creation_time INTEGER);"));
427 ));
428 460
429 sql::Statement add_smt(db.GetUniqueStatement( 461 sql::Statement add_smt(db.GetUniqueStatement(
430 "INSERT INTO origin_bound_certs " 462 "INSERT INTO origin_bound_certs "
431 "(origin, private_key, cert, cert_type, expiration_time, creation_time)" 463 "(origin, private_key, cert, cert_type, expiration_time, creation_time)"
432 " VALUES (?,?,?,?,?,?)")); 464 " VALUES (?,?,?,?,?,?)"));
433 add_smt.BindString(0, "google.com"); 465 add_smt.BindString(0, "google.com");
434 add_smt.BindBlob(1, key_data.data(), key_data.size()); 466 add_smt.BindBlob(1, key_data.data(), key_data.size());
435 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); 467 add_smt.BindBlob(2, cert_data.data(), cert_data.size());
436 add_smt.BindInt64(3, 64); 468 add_smt.BindInt64(3, 64);
437 add_smt.BindInt64(4, GetTestCertExpirationTime().ToInternalValue()); 469 add_smt.BindInt64(4, GetTestCertExpirationTime().ToInternalValue());
438 add_smt.BindInt64(5, base::Time::Now().ToInternalValue()); 470 add_smt.BindInt64(5, base::Time::Now().ToInternalValue());
439 ASSERT_TRUE(add_smt.Run()); 471 ASSERT_TRUE(add_smt.Run());
440 472
441 add_smt.Clear(); 473 add_smt.Clear();
442 add_smt.Assign(db.GetUniqueStatement( 474 add_smt.Assign(db.GetUniqueStatement(
443 "INSERT INTO origin_bound_certs " 475 "INSERT INTO origin_bound_certs "
444 "(origin, private_key, cert, cert_type, expiration_time, creation_time)" 476 "(origin, private_key, cert, cert_type, expiration_time, creation_time)"
445 " VALUES (?,?,?,?,?,?)")); 477 " VALUES (?,?,?,?,?,?)"));
446 add_smt.BindString(0, "foo.com"); 478 add_smt.BindString(0, "foo.com");
447 add_smt.BindBlob(1, key_data.data(), key_data.size()); 479 add_smt.BindBlob(1, key_data.data(), key_data.size());
448 add_smt.BindBlob(2, cert_data.data(), cert_data.size()); 480 add_smt.BindBlob(2, cert_data.data(), cert_data.size());
449 add_smt.BindInt64(3, 1); 481 add_smt.BindInt64(3, 1);
450 add_smt.BindInt64(4, GetTestCertExpirationTime().ToInternalValue()); 482 add_smt.BindInt64(4, GetTestCertExpirationTime().ToInternalValue());
451 add_smt.BindInt64(5, base::Time::Now().ToInternalValue()); 483 add_smt.BindInt64(5, base::Time::Now().ToInternalValue());
452 ASSERT_TRUE(add_smt.Run()); 484 ASSERT_TRUE(add_smt.Run());
453 } 485 }
454 486
455 ScopedVector<net::DefaultChannelIDStore::ChannelID> channel_ids; 487 ScopedVector<DefaultChannelIDStore::ChannelID> channel_ids;
456 store_ = new SQLiteChannelIDStore( 488 store_ =
457 v4_db_path, base::MessageLoopProxy::current(), NULL); 489 new SQLiteChannelIDStore(v4_db_path, base::MessageLoopProxy::current());
458 490
459 // Load the database and ensure the certs can be read. 491 // Load the database and ensure the certs can be read.
460 Load(&channel_ids); 492 Load(&channel_ids);
461 // Only the ECDSA cert (for google.com) is read, the RSA one is discarded. 493 // Only the ECDSA cert (for google.com) is read, the RSA one is discarded.
462 ASSERT_EQ(1U, channel_ids.size()); 494 ASSERT_EQ(1U, channel_ids.size());
463 495
464 ASSERT_EQ("google.com", channel_ids[0]->server_identifier()); 496 ASSERT_EQ("google.com", channel_ids[0]->server_identifier());
465 ASSERT_EQ(GetTestCertExpirationTime(), 497 ASSERT_EQ(GetTestCertExpirationTime(), channel_ids[0]->expiration_time());
466 channel_ids[0]->expiration_time());
467 ASSERT_EQ(key_data, channel_ids[0]->private_key()); 498 ASSERT_EQ(key_data, channel_ids[0]->private_key());
468 ASSERT_EQ(cert_data, channel_ids[0]->cert()); 499 ASSERT_EQ(cert_data, channel_ids[0]->cert());
469 500
470 store_ = NULL; 501 store_ = NULL;
471 // Make sure we wait until the destructor has run. 502 // Make sure we wait until the destructor has run.
472 base::RunLoop().RunUntilIdle(); 503 base::RunLoop().RunUntilIdle();
473 } 504 }
505
506 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698