| 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 #include <vector> |
| 10 | 10 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 EXPECT_TRUE(persister_->LoadEntries(output, &dirty)); | 60 EXPECT_TRUE(persister_->LoadEntries(output, &dirty)); |
| 61 EXPECT_FALSE(dirty); | 61 EXPECT_FALSE(dirty); |
| 62 } | 62 } |
| 63 | 63 |
| 64 TEST_F(TransportSecurityPersisterTest, SerializeData2) { | 64 TEST_F(TransportSecurityPersisterTest, SerializeData2) { |
| 65 TransportSecurityState::DomainState domain_state; | 65 TransportSecurityState::DomainState domain_state; |
| 66 const base::Time current_time(base::Time::Now()); | 66 const base::Time current_time(base::Time::Now()); |
| 67 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); | 67 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 68 static const char kYahooDomain[] = "yahoo.com"; | 68 static const char kYahooDomain[] = "yahoo.com"; |
| 69 | 69 |
| 70 EXPECT_FALSE(state_.GetDomainState(kYahooDomain, true, &domain_state)); | 70 EXPECT_FALSE(state_.GetDomainState(kYahooDomain, true, true, &domain_state)); |
| 71 | 71 |
| 72 bool include_subdomains = true; | 72 bool include_subdomains = true; |
| 73 state_.AddHSTS(kYahooDomain, expiry, include_subdomains); | 73 state_.AddHSTS(kYahooDomain, expiry, include_subdomains); |
| 74 | 74 |
| 75 std::string output; | 75 std::string output; |
| 76 bool dirty; | 76 bool dirty; |
| 77 EXPECT_TRUE(persister_->SerializeData(&output)); | 77 EXPECT_TRUE(persister_->SerializeData(&output)); |
| 78 EXPECT_TRUE(persister_->LoadEntries(output, &dirty)); | 78 EXPECT_TRUE(persister_->LoadEntries(output, &dirty)); |
| 79 | 79 |
| 80 EXPECT_TRUE(state_.GetDomainState(kYahooDomain, true, &domain_state)); | 80 EXPECT_TRUE(state_.GetDomainState(kYahooDomain, true, true, &domain_state)); |
| 81 EXPECT_EQ(domain_state.upgrade_mode, | 81 EXPECT_EQ(domain_state.upgrade_mode, |
| 82 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); | 82 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); |
| 83 EXPECT_TRUE(state_.GetDomainState("foo.yahoo.com", true, &domain_state)); | 83 EXPECT_TRUE(state_.GetDomainState("foo.yahoo.com", true, true, |
| 84 &domain_state)); |
| 84 EXPECT_EQ(domain_state.upgrade_mode, | 85 EXPECT_EQ(domain_state.upgrade_mode, |
| 85 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); | 86 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); |
| 86 EXPECT_TRUE(state_.GetDomainState("foo.bar.yahoo.com", true, &domain_state)); | 87 EXPECT_TRUE(state_.GetDomainState("foo.bar.yahoo.com", true, true, |
| 88 &domain_state)); |
| 87 EXPECT_EQ(domain_state.upgrade_mode, | 89 EXPECT_EQ(domain_state.upgrade_mode, |
| 88 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); | 90 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); |
| 89 EXPECT_TRUE(state_.GetDomainState("foo.bar.baz.yahoo.com", true, | 91 EXPECT_TRUE(state_.GetDomainState("foo.bar.baz.yahoo.com", true, |
| 90 &domain_state)); | 92 true, &domain_state)); |
| 91 EXPECT_EQ(domain_state.upgrade_mode, | 93 EXPECT_EQ(domain_state.upgrade_mode, |
| 92 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); | 94 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); |
| 93 EXPECT_FALSE(state_.GetDomainState("com", true, &domain_state)); | 95 EXPECT_FALSE(state_.GetDomainState("com", true, true, &domain_state)); |
| 94 } | 96 } |
| 95 | 97 |
| 96 TEST_F(TransportSecurityPersisterTest, SerializeData3) { | 98 TEST_F(TransportSecurityPersisterTest, SerializeData3) { |
| 97 // Add an entry. | 99 // Add an entry. |
| 98 net::HashValue fp1(net::HASH_VALUE_SHA1); | 100 net::HashValue fp1(net::HASH_VALUE_SHA1); |
| 99 memset(fp1.data(), 0, fp1.size()); | 101 memset(fp1.data(), 0, fp1.size()); |
| 100 net::HashValue fp2(net::HASH_VALUE_SHA1); | 102 net::HashValue fp2(net::HASH_VALUE_SHA1); |
| 101 memset(fp2.data(), 1, fp2.size()); | 103 memset(fp2.data(), 1, fp2.size()); |
| 102 base::Time expiry = | 104 base::Time expiry = |
| 103 base::Time::Now() + base::TimeDelta::FromSeconds(1000); | 105 base::Time::Now() + base::TimeDelta::FromSeconds(1000); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 "}" | 172 "}" |
| 171 "}"; | 173 "}"; |
| 172 bool dirty; | 174 bool dirty; |
| 173 EXPECT_TRUE(persister_->LoadEntries(output, &dirty)); | 175 EXPECT_TRUE(persister_->LoadEntries(output, &dirty)); |
| 174 EXPECT_TRUE(dirty); | 176 EXPECT_TRUE(dirty); |
| 175 } | 177 } |
| 176 | 178 |
| 177 TEST_F(TransportSecurityPersisterTest, PublicKeyHashes) { | 179 TEST_F(TransportSecurityPersisterTest, PublicKeyHashes) { |
| 178 TransportSecurityState::DomainState domain_state; | 180 TransportSecurityState::DomainState domain_state; |
| 179 static const char kTestDomain[] = "example.com"; | 181 static const char kTestDomain[] = "example.com"; |
| 180 EXPECT_FALSE(state_.GetDomainState(kTestDomain, false, &domain_state)); | 182 EXPECT_FALSE(state_.GetDomainState(kTestDomain, false, true, &domain_state)); |
| 181 net::HashValueVector hashes; | 183 net::HashValueVector hashes; |
| 182 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes)); | 184 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes)); |
| 183 | 185 |
| 184 net::HashValue sha1(net::HASH_VALUE_SHA1); | 186 net::HashValue sha1(net::HASH_VALUE_SHA1); |
| 185 memset(sha1.data(), '1', sha1.size()); | 187 memset(sha1.data(), '1', sha1.size()); |
| 186 domain_state.dynamic_spki_hashes.push_back(sha1); | 188 domain_state.dynamic_spki_hashes.push_back(sha1); |
| 187 | 189 |
| 188 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes)); | 190 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes)); |
| 189 | 191 |
| 190 hashes.push_back(sha1); | 192 hashes.push_back(sha1); |
| 191 EXPECT_TRUE(domain_state.CheckPublicKeyPins(hashes)); | 193 EXPECT_TRUE(domain_state.CheckPublicKeyPins(hashes)); |
| 192 | 194 |
| 193 hashes[0].data()[0] = '2'; | 195 hashes[0].data()[0] = '2'; |
| 194 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes)); | 196 EXPECT_FALSE(domain_state.CheckPublicKeyPins(hashes)); |
| 195 | 197 |
| 196 const base::Time current_time(base::Time::Now()); | 198 const base::Time current_time(base::Time::Now()); |
| 197 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); | 199 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 198 bool include_subdomains = false; | 200 bool include_subdomains = false; |
| 199 state_.AddHSTS(kTestDomain, expiry, include_subdomains); | 201 state_.AddHSTS(kTestDomain, expiry, include_subdomains); |
| 200 state_.AddHPKP(kTestDomain, expiry, include_subdomains, | 202 state_.AddHPKP(kTestDomain, expiry, include_subdomains, |
| 201 domain_state.dynamic_spki_hashes); | 203 domain_state.dynamic_spki_hashes); |
| 202 std::string ser; | 204 std::string ser; |
| 203 EXPECT_TRUE(persister_->SerializeData(&ser)); | 205 EXPECT_TRUE(persister_->SerializeData(&ser)); |
| 204 bool dirty; | 206 bool dirty; |
| 205 EXPECT_TRUE(persister_->LoadEntries(ser, &dirty)); | 207 EXPECT_TRUE(persister_->LoadEntries(ser, &dirty)); |
| 206 EXPECT_TRUE(state_.GetDomainState(kTestDomain, false, &domain_state)); | 208 EXPECT_TRUE(state_.GetDomainState(kTestDomain, false, true, &domain_state)); |
| 207 EXPECT_EQ(1u, domain_state.dynamic_spki_hashes.size()); | 209 EXPECT_EQ(1u, domain_state.dynamic_spki_hashes.size()); |
| 208 EXPECT_EQ(sha1.tag, domain_state.dynamic_spki_hashes[0].tag); | 210 EXPECT_EQ(sha1.tag, domain_state.dynamic_spki_hashes[0].tag); |
| 209 EXPECT_EQ(0, memcmp(domain_state.dynamic_spki_hashes[0].data(), sha1.data(), | 211 EXPECT_EQ(0, memcmp(domain_state.dynamic_spki_hashes[0].data(), sha1.data(), |
| 210 sha1.size())); | 212 sha1.size())); |
| 211 } | 213 } |
| OLD | NEW |