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

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

Powered by Google App Engine
This is Rietveld 408576698