|
OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/transport_security_persister.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/file_path.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/scoped_temp_dir.h" | |
13 #include "content/test/test_browser_thread.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "net/base/transport_security_state.h" | |
16 #include "net/base/x509_cert_types.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using net::TransportSecurityState; | |
20 using content::BrowserThread; | |
21 | |
22 class TransportSecurityPersisterTest : public testing::Test { | |
23 public: | |
24 TransportSecurityPersisterTest() | |
25 : test_io_thread(BrowserThread::IO, &message_loop), | |
26 persister(&state, temp_dir.path(), false) | |
27 { } | |
Ryan Sleevi
2012/04/26 19:21:12
nit: the opening brace should be on the end of lin
palmer
2012/04/27 23:52:34
Done.
| |
28 | |
29 virtual void SetUp() { | |
30 } | |
Ryan Sleevi
2012/04/26 19:21:12
nit: Move this up to line 29, in keeping with
htt
palmer
2012/04/27 23:52:34
Done.
| |
31 | |
32 MessageLoop message_loop; | |
33 content::TestBrowserThread test_io_thread; | |
Ryan Sleevi
2012/04/26 19:21:12
I don't see any tests covering serializing from ac
palmer
2012/04/27 23:52:34
Done.
| |
34 ScopedTempDir temp_dir; | |
35 TransportSecurityPersister persister; | |
36 TransportSecurityState state; | |
37 }; | |
38 | |
39 TEST_F(TransportSecurityPersisterTest, SerializeData1) { | |
40 std::string output; | |
41 bool dirty; | |
42 | |
43 EXPECT_TRUE(persister.SerializeData(&output)); | |
44 EXPECT_TRUE(persister.LoadEntries(output, &dirty)); | |
45 EXPECT_FALSE(dirty); | |
46 } | |
47 | |
48 TEST_F(TransportSecurityPersisterTest, SerializeData2) { | |
49 TransportSecurityState::DomainState domain_state; | |
50 const base::Time current_time(base::Time::Now()); | |
51 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); | |
52 | |
53 EXPECT_FALSE(state.GetDomainState("yahoo.com", true, &domain_state)); | |
54 domain_state.upgrade_mode = | |
55 TransportSecurityState::DomainState::MODE_FORCE_HTTPS; | |
56 domain_state.upgrade_expiry = expiry; | |
57 domain_state.include_subdomains = true; | |
58 state.EnableHost("yahoo.com", domain_state); | |
59 | |
60 std::string output; | |
61 bool dirty; | |
62 EXPECT_TRUE(persister.SerializeData(&output)); | |
63 EXPECT_TRUE(persister.LoadEntries(output, &dirty)); | |
64 | |
65 EXPECT_TRUE(state.GetDomainState("yahoo.com", true, &domain_state)); | |
66 EXPECT_EQ(domain_state.upgrade_mode, | |
67 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); | |
68 EXPECT_TRUE(state.GetDomainState("foo.yahoo.com", true, &domain_state)); | |
69 EXPECT_EQ(domain_state.upgrade_mode, | |
70 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); | |
71 EXPECT_TRUE(state.GetDomainState("foo.bar.yahoo.com", true, &domain_state)); | |
72 EXPECT_EQ(domain_state.upgrade_mode, | |
73 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); | |
74 EXPECT_TRUE(state.GetDomainState("foo.bar.baz.yahoo.com", true, | |
75 &domain_state)); | |
76 EXPECT_EQ(domain_state.upgrade_mode, | |
77 TransportSecurityState::DomainState::MODE_FORCE_HTTPS); | |
78 EXPECT_FALSE(state.GetDomainState("com", true, &domain_state)); | |
79 } | |
80 | |
81 TEST_F(TransportSecurityPersisterTest, SerializeDataOld) { | |
82 // This is an old-style piece of transport state JSON, which has no creation | |
83 // date. | |
84 std::string output = | |
85 "{ " | |
86 "\"NiyD+3J1r6z1wjl2n1ALBu94Zj9OsEAMo0kCN8js0Uk=\": {" | |
87 "\"expiry\": 1266815027.983453, " | |
88 "\"include_subdomains\": false, " | |
89 "\"mode\": \"strict\" " | |
90 "}" | |
91 "}"; | |
92 bool dirty; | |
93 EXPECT_TRUE(persister.LoadEntries(output, &dirty)); | |
94 EXPECT_TRUE(dirty); | |
95 } | |
96 | |
97 TEST_F(TransportSecurityPersisterTest, PublicKeyHashes) { | |
98 TransportSecurityState::DomainState domain_state; | |
99 EXPECT_FALSE(state.GetDomainState("example.com", false, &domain_state)); | |
100 net::FingerprintVector hashes; | |
101 EXPECT_TRUE(domain_state.IsChainOfPublicKeysPermitted(hashes)); | |
102 | |
103 net::SHA1Fingerprint hash; | |
104 memset(hash.data, '1', sizeof(hash.data)); | |
105 domain_state.static_spki_hashes.push_back(hash); | |
106 | |
107 EXPECT_FALSE(domain_state.IsChainOfPublicKeysPermitted(hashes)); | |
108 hashes.push_back(hash); | |
109 EXPECT_TRUE(domain_state.IsChainOfPublicKeysPermitted(hashes)); | |
110 hashes[0].data[0] = '2'; | |
111 EXPECT_FALSE(domain_state.IsChainOfPublicKeysPermitted(hashes)); | |
112 | |
113 const base::Time current_time(base::Time::Now()); | |
114 const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); | |
115 domain_state.upgrade_expiry = expiry; | |
116 state.EnableHost("example.com", domain_state); | |
117 std::string ser; | |
118 EXPECT_TRUE(persister.SerializeData(&ser)); | |
119 bool dirty; | |
120 EXPECT_TRUE(persister.LoadEntries(ser, &dirty)); | |
121 EXPECT_TRUE(state.GetDomainState("example.com", false, &domain_state)); | |
122 EXPECT_EQ(1u, domain_state.static_spki_hashes.size()); | |
123 EXPECT_EQ(0, memcmp(domain_state.static_spki_hashes[0].data, hash.data, | |
124 sizeof(hash.data))); | |
125 } | |
126 | |
127 TEST_F(TransportSecurityPersisterTest, ForcePreloads) { | |
128 // The static state for docs.google.com, defined in | |
129 // net/base/transport_security_state_static.h, has pins and mode strict. | |
130 // This new policy overrides that with no pins and a weaker mode. We apply | |
131 // this new policy with |DeserializeFromCommandLine| and expect that the | |
132 // new policy is in effect, overriding the static policy. | |
133 std::string preload("{" | |
134 "\"4AGT3lHihuMSd5rUj7B4u6At0jlSH3HFePovjPR+oLE=\": {" | |
135 "\"created\": 0.0," | |
136 "\"expiry\": 2000000000.0," | |
137 "\"include_subdomains\": false," | |
138 "\"mode\": \"pinning-only\"" | |
139 "}}"); | |
140 | |
141 EXPECT_TRUE(persister.DeserializeFromCommandLine(preload)); | |
142 | |
143 TransportSecurityState::DomainState domain_state; | |
144 EXPECT_TRUE(state.GetDomainState("docs.google.com", true, &domain_state)); | |
145 EXPECT_FALSE(domain_state.HasPins()); | |
146 EXPECT_FALSE(domain_state.ShouldRedirectHTTPToHTTPS()); | |
147 } | |
148 | |
OLD | NEW |