OLD | NEW |
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 "net/http/transport_security_persister.h" | 5 #include "net/http/transport_security_persister.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "net/http/transport_security_state.h" | 15 #include "net/http/transport_security_state.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
| 22 const char kReportUri[] = "http://www.example.com/report"; |
| 23 |
22 class TransportSecurityPersisterTest : public testing::Test { | 24 class TransportSecurityPersisterTest : public testing::Test { |
23 public: | 25 public: |
24 TransportSecurityPersisterTest() { | 26 TransportSecurityPersisterTest() { |
25 } | 27 } |
26 | 28 |
27 ~TransportSecurityPersisterTest() override { | 29 ~TransportSecurityPersisterTest() override { |
28 base::MessageLoopForIO::current()->RunUntilIdle(); | 30 base::MessageLoopForIO::current()->RunUntilIdle(); |
29 } | 31 } |
30 | 32 |
31 void SetUp() override { | 33 void SetUp() override { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 HashValue fp2(HASH_VALUE_SHA1); | 92 HashValue fp2(HASH_VALUE_SHA1); |
91 memset(fp2.data(), 1, fp2.size()); | 93 memset(fp2.data(), 1, fp2.size()); |
92 base::Time expiry = | 94 base::Time expiry = |
93 base::Time::Now() + base::TimeDelta::FromSeconds(1000); | 95 base::Time::Now() + base::TimeDelta::FromSeconds(1000); |
94 HashValueVector dynamic_spki_hashes; | 96 HashValueVector dynamic_spki_hashes; |
95 dynamic_spki_hashes.push_back(fp1); | 97 dynamic_spki_hashes.push_back(fp1); |
96 dynamic_spki_hashes.push_back(fp2); | 98 dynamic_spki_hashes.push_back(fp2); |
97 bool include_subdomains = false; | 99 bool include_subdomains = false; |
98 state_.AddHSTS("www.example.com", expiry, include_subdomains); | 100 state_.AddHSTS("www.example.com", expiry, include_subdomains); |
99 state_.AddHPKP("www.example.com", expiry, include_subdomains, | 101 state_.AddHPKP("www.example.com", expiry, include_subdomains, |
100 dynamic_spki_hashes); | 102 dynamic_spki_hashes, kReportUri); |
101 | 103 |
102 // Add another entry. | 104 // Add another entry. |
103 memset(fp1.data(), 2, fp1.size()); | 105 memset(fp1.data(), 2, fp1.size()); |
104 memset(fp2.data(), 3, fp2.size()); | 106 memset(fp2.data(), 3, fp2.size()); |
105 expiry = | 107 expiry = |
106 base::Time::Now() + base::TimeDelta::FromSeconds(3000); | 108 base::Time::Now() + base::TimeDelta::FromSeconds(3000); |
107 dynamic_spki_hashes.push_back(fp1); | 109 dynamic_spki_hashes.push_back(fp1); |
108 dynamic_spki_hashes.push_back(fp2); | 110 dynamic_spki_hashes.push_back(fp2); |
109 state_.AddHSTS("www.example.net", expiry, include_subdomains); | 111 state_.AddHSTS("www.example.net", expiry, include_subdomains); |
110 state_.AddHPKP("www.example.net", expiry, include_subdomains, | 112 state_.AddHPKP("www.example.net", expiry, include_subdomains, |
111 dynamic_spki_hashes); | 113 dynamic_spki_hashes, kReportUri); |
112 | 114 |
113 // Save a copy of everything. | 115 // Save a copy of everything. |
114 std::map<std::string, TransportSecurityState::DomainState> saved; | 116 std::map<std::string, TransportSecurityState::DomainState> saved; |
115 TransportSecurityState::Iterator i(state_); | 117 TransportSecurityState::Iterator i(state_); |
116 while (i.HasNext()) { | 118 while (i.HasNext()) { |
117 saved[i.hostname()] = i.domain_state(); | 119 saved[i.hostname()] = i.domain_state(); |
118 i.Advance(); | 120 i.Advance(); |
119 } | 121 } |
120 | 122 |
121 std::string serialized; | 123 std::string serialized; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 hashes.push_back(sha1); | 182 hashes.push_back(sha1); |
181 EXPECT_TRUE(domain_state.CheckPublicKeyPins(hashes, &failure_log)); | 183 EXPECT_TRUE(domain_state.CheckPublicKeyPins(hashes, &failure_log)); |
182 | 184 |
183 hashes[0].data()[0] = '2'; | 185 hashes[0].data()[0] = '2'; |
184 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes, &failure_log)); | 186 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes, &failure_log)); |
185 | 187 |
186 const base::Time current_time(base::Time::Now()); | 188 const base::Time current_time(base::Time::Now()); |
187 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); | 189 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
188 bool include_subdomains = false; | 190 bool include_subdomains = false; |
189 state_.AddHSTS(kTestDomain, expiry, include_subdomains); | 191 state_.AddHSTS(kTestDomain, expiry, include_subdomains); |
190 state_.AddHPKP( | 192 state_.AddHPKP(kTestDomain, expiry, include_subdomains, |
191 kTestDomain, expiry, include_subdomains, domain_state.pkp.spki_hashes); | 193 domain_state.pkp.spki_hashes, kReportUri); |
192 std::string serialized; | 194 std::string serialized; |
193 EXPECT_TRUE(persister_->SerializeData(&serialized)); | 195 EXPECT_TRUE(persister_->SerializeData(&serialized)); |
194 bool dirty; | 196 bool dirty; |
| 197 EXPECT_TRUE(persister_->LoadEntries(serialized, &dirty)); |
| 198 |
| 199 TransportSecurityState::DomainState new_domain_state; |
| 200 EXPECT_TRUE(state_.GetDynamicDomainState(kTestDomain, &new_domain_state)); |
| 201 EXPECT_EQ(1u, new_domain_state.pkp.spki_hashes.size()); |
| 202 EXPECT_EQ(sha1.tag, new_domain_state.pkp.spki_hashes[0].tag); |
| 203 EXPECT_EQ(0, memcmp(new_domain_state.pkp.spki_hashes[0].data(), sha1.data(), |
| 204 sha1.size())); |
| 205 } |
| 206 |
| 207 TEST_F(TransportSecurityPersisterTest, PublicKeyPinReportUri) { |
| 208 TransportSecurityState::DomainState domain_state; |
| 209 static const char kTestDomain[] = "example.com"; |
| 210 static const char kTestReportUri[] = "http://example.com/report"; |
| 211 |
| 212 EXPECT_FALSE(state_.GetDynamicDomainState(kTestDomain, &domain_state)); |
| 213 HashValueVector hashes; |
| 214 std::string failure_log; |
| 215 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes, &failure_log)); |
| 216 |
| 217 HashValue sha1(HASH_VALUE_SHA1); |
| 218 memset(sha1.data(), '1', sha1.size()); |
| 219 domain_state.pkp.spki_hashes.push_back(sha1); |
| 220 |
| 221 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes, &failure_log)); |
| 222 |
| 223 hashes.push_back(sha1); |
| 224 EXPECT_TRUE(domain_state.CheckPublicKeyPins(hashes, &failure_log)); |
| 225 |
| 226 hashes[0].data()[0] = '2'; |
| 227 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes, &failure_log)); |
| 228 |
| 229 const base::Time current_time(base::Time::Now()); |
| 230 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 231 bool include_subdomains = false; |
| 232 state_.AddHPKP(kTestDomain, expiry, include_subdomains, |
| 233 domain_state.pkp.spki_hashes, std::string(kTestReportUri)); |
| 234 std::string serialized; |
| 235 EXPECT_TRUE(persister_->SerializeData(&serialized)); |
| 236 bool dirty; |
195 EXPECT_TRUE(persister_->LoadEntries(serialized, &dirty)); | 237 EXPECT_TRUE(persister_->LoadEntries(serialized, &dirty)); |
196 | 238 |
197 TransportSecurityState::DomainState new_domain_state; | 239 TransportSecurityState::DomainState new_domain_state; |
198 EXPECT_TRUE(state_.GetDynamicDomainState(kTestDomain, &new_domain_state)); | 240 EXPECT_TRUE(state_.GetDynamicDomainState(kTestDomain, &new_domain_state)); |
199 EXPECT_EQ(1u, new_domain_state.pkp.spki_hashes.size()); | 241 EXPECT_EQ(1u, new_domain_state.pkp.spki_hashes.size()); |
200 EXPECT_EQ(sha1.tag, new_domain_state.pkp.spki_hashes[0].tag); | 242 EXPECT_EQ(sha1.tag, new_domain_state.pkp.spki_hashes[0].tag); |
201 EXPECT_EQ(0, | 243 EXPECT_EQ(0, |
202 memcmp(new_domain_state.pkp.spki_hashes[0].data(), | 244 memcmp(new_domain_state.pkp.spki_hashes[0].data(), |
203 sha1.data(), | 245 sha1.data(), |
204 sha1.size())); | 246 sha1.size())); |
| 247 EXPECT_EQ(kTestReportUri, new_domain_state.pkp.report_uri); |
205 } | 248 } |
206 | 249 |
207 } // namespace | 250 } // namespace |
208 | 251 |
209 } // namespace net | 252 } // namespace net |
OLD | NEW |