| 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 "chrome/browser/net/transport_security_persister.h" | 5 #include "chrome/browser/net/transport_security_persister.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 12 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 13 #include "base/scoped_temp_dir.h" | 14 #include "base/scoped_temp_dir.h" |
| 14 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 15 #include "net/base/transport_security_state.h" | 16 #include "net/base/transport_security_state.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 using net::TransportSecurityState; | 19 using net::TransportSecurityState; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); | 90 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); |
| 90 EXPECT_TRUE(state_.GetDomainState("foo.bar.baz.yahoo.com", true, | 91 EXPECT_TRUE(state_.GetDomainState("foo.bar.baz.yahoo.com", true, |
| 91 &domain_state)); | 92 &domain_state)); |
| 92 EXPECT_EQ(domain_state.upgrade_mode, | 93 EXPECT_EQ(domain_state.upgrade_mode, |
| 93 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); | 94 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); |
| 94 EXPECT_FALSE(state_.GetDomainState("com", true, &domain_state)); | 95 EXPECT_FALSE(state_.GetDomainState("com", true, &domain_state)); |
| 95 } | 96 } |
| 96 | 97 |
| 97 TEST_F(TransportSecurityPersisterTest, SerializeData3) { | 98 TEST_F(TransportSecurityPersisterTest, SerializeData3) { |
| 98 // Add an entry. | 99 // Add an entry. |
| 99 net::SHA1Fingerprint fp1; | 100 net::HashValue fp1; |
| 100 memset(fp1.data, 0, sizeof(fp1.data)); | 101 fp1.tag = net::HASH_VALUE_SHA1; |
| 101 net::SHA1Fingerprint fp2; | 102 memset(fp1.data(), 0, fp1.size()); |
| 102 memset(fp2.data, 1, sizeof(fp2.data)); | 103 net::HashValue fp2; |
| 104 fp2.tag = net::HASH_VALUE_SHA1; |
| 105 memset(fp2.data(), 1, fp2.size()); |
| 103 TransportSecurityState::DomainState example_state; | 106 TransportSecurityState::DomainState example_state; |
| 104 example_state.upgrade_expiry = | 107 example_state.upgrade_expiry = |
| 105 base::Time::Now() + base::TimeDelta::FromSeconds(1000); | 108 base::Time::Now() + base::TimeDelta::FromSeconds(1000); |
| 106 example_state.upgrade_mode = | 109 example_state.upgrade_mode = |
| 107 TransportSecurityState::DomainState::MODE_FORCE_HTTPS; | 110 TransportSecurityState::DomainState::MODE_FORCE_HTTPS; |
| 108 example_state.dynamic_spki_hashes_expiry = example_state.upgrade_expiry; | 111 example_state.dynamic_spki_hashes_expiry = example_state.upgrade_expiry; |
| 109 example_state.dynamic_spki_hashes.push_back(fp1); | 112 example_state.dynamic_spki_hashes.push_back(fp1); |
| 110 example_state.dynamic_spki_hashes.push_back(fp2); | 113 example_state.dynamic_spki_hashes.push_back(fp2); |
| 111 state_.EnableHost("www.example.com", example_state); | 114 state_.EnableHost("www.example.com", example_state); |
| 112 | 115 |
| 113 // Add another entry. | 116 // Add another entry. |
| 114 memset(fp1.data, 2, sizeof(fp1.data)); | 117 memset(fp1.data(), 2, fp1.size()); |
| 115 memset(fp2.data, 3, sizeof(fp2.data)); | 118 memset(fp2.data(), 3, fp2.size()); |
| 116 example_state.upgrade_expiry = | 119 example_state.upgrade_expiry = |
| 117 base::Time::Now() + base::TimeDelta::FromSeconds(3000); | 120 base::Time::Now() + base::TimeDelta::FromSeconds(3000); |
| 118 example_state.upgrade_mode = | 121 example_state.upgrade_mode = |
| 119 TransportSecurityState::DomainState::MODE_DEFAULT; | 122 TransportSecurityState::DomainState::MODE_DEFAULT; |
| 120 example_state.dynamic_spki_hashes_expiry = example_state.upgrade_expiry; | 123 example_state.dynamic_spki_hashes_expiry = example_state.upgrade_expiry; |
| 121 example_state.dynamic_spki_hashes.push_back(fp1); | 124 example_state.dynamic_spki_hashes.push_back(fp1); |
| 122 example_state.dynamic_spki_hashes.push_back(fp2); | 125 example_state.dynamic_spki_hashes.push_back(fp2); |
| 123 state_.EnableHost("www.example.net", example_state); | 126 state_.EnableHost("www.example.net", example_state); |
| 124 | 127 |
| 125 // Save a copy of everything. | 128 // Save a copy of everything. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 "}"; | 177 "}"; |
| 175 bool dirty; | 178 bool dirty; |
| 176 EXPECT_TRUE(persister_->LoadEntries(output, &dirty)); | 179 EXPECT_TRUE(persister_->LoadEntries(output, &dirty)); |
| 177 EXPECT_TRUE(dirty); | 180 EXPECT_TRUE(dirty); |
| 178 } | 181 } |
| 179 | 182 |
| 180 TEST_F(TransportSecurityPersisterTest, PublicKeyHashes) { | 183 TEST_F(TransportSecurityPersisterTest, PublicKeyHashes) { |
| 181 TransportSecurityState::DomainState domain_state; | 184 TransportSecurityState::DomainState domain_state; |
| 182 static const char kTestDomain[] = "example.com"; | 185 static const char kTestDomain[] = "example.com"; |
| 183 EXPECT_FALSE(state_.GetDomainState(kTestDomain, false, &domain_state)); | 186 EXPECT_FALSE(state_.GetDomainState(kTestDomain, false, &domain_state)); |
| 184 net::FingerprintVector hashes; | 187 std::vector<net::HashValueVector> hashes(net::HASH_VALUE_TAGS_COUNT); |
| 188 EXPECT_FALSE(domain_state.IsChainOfPublicKeysPermitted(hashes)); |
| 189 |
| 190 net::HashValue sha1; |
| 191 sha1.tag = net::HASH_VALUE_SHA1; |
| 192 memset(sha1.data(), '1', sha1.size()); |
| 193 domain_state.static_spki_hashes.push_back(sha1); |
| 194 |
| 195 EXPECT_FALSE(domain_state.IsChainOfPublicKeysPermitted(hashes)); |
| 196 |
| 197 hashes[net::HASH_VALUE_SHA1].push_back(sha1); |
| 185 EXPECT_TRUE(domain_state.IsChainOfPublicKeysPermitted(hashes)); | 198 EXPECT_TRUE(domain_state.IsChainOfPublicKeysPermitted(hashes)); |
| 186 | 199 |
| 187 net::SHA1Fingerprint hash; | 200 hashes[net::HASH_VALUE_SHA1][0].data()[0] = '2'; |
| 188 memset(hash.data, '1', sizeof(hash.data)); | |
| 189 domain_state.static_spki_hashes.push_back(hash); | |
| 190 | |
| 191 EXPECT_FALSE(domain_state.IsChainOfPublicKeysPermitted(hashes)); | |
| 192 hashes.push_back(hash); | |
| 193 EXPECT_TRUE(domain_state.IsChainOfPublicKeysPermitted(hashes)); | |
| 194 hashes[0].data[0] = '2'; | |
| 195 EXPECT_FALSE(domain_state.IsChainOfPublicKeysPermitted(hashes)); | 201 EXPECT_FALSE(domain_state.IsChainOfPublicKeysPermitted(hashes)); |
| 196 | 202 |
| 197 const base::Time current_time(base::Time::Now()); | 203 const base::Time current_time(base::Time::Now()); |
| 198 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); | 204 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 199 domain_state.upgrade_expiry = expiry; | 205 domain_state.upgrade_expiry = expiry; |
| 200 state_.EnableHost(kTestDomain, domain_state); | 206 state_.EnableHost(kTestDomain, domain_state); |
| 201 std::string ser; | 207 std::string ser; |
| 202 EXPECT_TRUE(persister_->SerializeData(&ser)); | 208 EXPECT_TRUE(persister_->SerializeData(&ser)); |
| 203 bool dirty; | 209 bool dirty; |
| 204 EXPECT_TRUE(persister_->LoadEntries(ser, &dirty)); | 210 EXPECT_TRUE(persister_->LoadEntries(ser, &dirty)); |
| 205 EXPECT_TRUE(state_.GetDomainState(kTestDomain, false, &domain_state)); | 211 EXPECT_TRUE(state_.GetDomainState(kTestDomain, false, &domain_state)); |
| 206 EXPECT_EQ(1u, domain_state.static_spki_hashes.size()); | 212 EXPECT_EQ(1u, domain_state.static_spki_hashes.size()); |
| 207 EXPECT_EQ(0, memcmp(domain_state.static_spki_hashes[0].data, hash.data, | 213 EXPECT_EQ(sha1.tag, domain_state.static_spki_hashes[0].tag); |
| 208 sizeof(hash.data))); | 214 EXPECT_EQ(0, memcmp(domain_state.static_spki_hashes[0].data(), sha1.data(), |
| 215 sha1.size())); |
| 209 } | 216 } |
| 210 | 217 |
| 211 TEST_F(TransportSecurityPersisterTest, ForcePreloads) { | 218 TEST_F(TransportSecurityPersisterTest, ForcePreloads) { |
| 212 // The static state for docs.google.com, defined in | 219 // The static state for docs.google.com, defined in |
| 213 // net/base/transport_security_state_static.h, has pins and mode strict. | 220 // net/base/transport_security_state_static.h, has pins and mode strict. |
| 214 // This new policy overrides that with no pins and a weaker mode. We apply | 221 // This new policy overrides that with no pins and a weaker mode. We apply |
| 215 // this new policy with |DeserializeFromCommandLine| and expect that the | 222 // this new policy with |DeserializeFromCommandLine| and expect that the |
| 216 // new policy is in effect, overriding the static policy. | 223 // new policy is in effect, overriding the static policy. |
| 217 std::string preload("{" | 224 std::string preload("{" |
| 218 "\"4AGT3lHihuMSd5rUj7B4u6At0jlSH3HFePovjPR+oLE=\": {" | 225 "\"4AGT3lHihuMSd5rUj7B4u6At0jlSH3HFePovjPR+oLE=\": {" |
| 219 "\"created\": 0.0," | 226 "\"created\": 0.0," |
| 220 "\"expiry\": 2000000000.0," | 227 "\"expiry\": 2000000000.0," |
| 221 "\"include_subdomains\": false," | 228 "\"include_subdomains\": false," |
| 222 "\"mode\": \"pinning-only\"" | 229 "\"mode\": \"pinning-only\"" |
| 223 "}}"); | 230 "}}"); |
| 224 | 231 |
| 225 EXPECT_TRUE(persister_->DeserializeFromCommandLine(preload)); | 232 EXPECT_TRUE(persister_->DeserializeFromCommandLine(preload)); |
| 226 | 233 |
| 227 TransportSecurityState::DomainState domain_state; | 234 TransportSecurityState::DomainState domain_state; |
| 228 EXPECT_TRUE(state_.GetDomainState("docs.google.com", true, &domain_state)); | 235 EXPECT_TRUE(state_.GetDomainState("docs.google.com", true, &domain_state)); |
| 229 EXPECT_FALSE(domain_state.HasPins()); | 236 EXPECT_FALSE(domain_state.HasPins()); |
| 230 EXPECT_FALSE(domain_state.ShouldRedirectHTTPToHTTPS()); | 237 EXPECT_FALSE(domain_state.ShouldRedirectHTTPToHTTPS()); |
| 231 } | 238 } |
| OLD | NEW |