OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/cert/internal/trust_store_collection.h" | 5 #include "net/cert/internal/trust_store_collection.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "net/cert/internal/test_helpers.h" | 7 #include "net/cert/internal/test_helpers.h" |
9 #include "net/cert/internal/trust_store_test_helpers.h" | 8 #include "net/cert/internal/trust_store_in_memory.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
12 | 10 |
13 namespace net { | 11 namespace net { |
14 | 12 |
15 namespace { | 13 namespace { |
16 | 14 |
17 using ::testing::_; | |
18 using ::testing::Property; | |
19 using ::testing::StrictMock; | |
20 | |
21 void NotCalled(TrustAnchors anchors) { | |
22 ADD_FAILURE() << "NotCalled was called"; | |
23 } | |
24 | |
25 class MockTrustStore : public TrustStore { | |
26 public: | |
27 MOCK_CONST_METHOD4(FindTrustAnchorsForCert, | |
28 void(const scoped_refptr<ParsedCertificate>&, | |
29 const TrustAnchorsCallback&, | |
30 TrustAnchors*, | |
31 std::unique_ptr<Request>*)); | |
32 }; | |
33 | |
34 class TrustStoreCollectionTest : public testing::Test { | 15 class TrustStoreCollectionTest : public testing::Test { |
35 public: | 16 public: |
36 void SetUp() override { | 17 void SetUp() override { |
37 ParsedCertificateList chain; | 18 ParsedCertificateList chain; |
38 bool unused_verify_result; | 19 bool unused_verify_result; |
39 der::GeneralizedTime unused_time; | 20 der::GeneralizedTime unused_time; |
40 std::string unused_errors; | 21 std::string unused_errors; |
41 | 22 |
42 ReadVerifyCertChainTestFromFile( | 23 ReadVerifyCertChainTestFromFile( |
43 "net/data/verify_certificate_chain_unittest/key-rollover-oldchain.pem", | 24 "net/data/verify_certificate_chain_unittest/key-rollover-oldchain.pem", |
(...skipping 24 matching lines...) Expand all Loading... |
68 protected: | 49 protected: |
69 scoped_refptr<TrustAnchor> oldroot_; | 50 scoped_refptr<TrustAnchor> oldroot_; |
70 scoped_refptr<TrustAnchor> newroot_; | 51 scoped_refptr<TrustAnchor> newroot_; |
71 scoped_refptr<TrustAnchor> newrootrollover_; | 52 scoped_refptr<TrustAnchor> newrootrollover_; |
72 | 53 |
73 scoped_refptr<ParsedCertificate> target_; | 54 scoped_refptr<ParsedCertificate> target_; |
74 scoped_refptr<ParsedCertificate> oldintermediate_; | 55 scoped_refptr<ParsedCertificate> oldintermediate_; |
75 scoped_refptr<ParsedCertificate> newintermediate_; | 56 scoped_refptr<ParsedCertificate> newintermediate_; |
76 }; | 57 }; |
77 | 58 |
78 // Collection contains no stores, should return no results and complete | 59 // Collection contains no stores, should return no results. |
79 // synchronously. | |
80 TEST_F(TrustStoreCollectionTest, NoStores) { | 60 TEST_F(TrustStoreCollectionTest, NoStores) { |
81 std::unique_ptr<TrustStore::Request> req; | 61 TrustAnchors matches; |
82 TrustAnchors sync_matches; | |
83 | 62 |
84 TrustStoreCollection collection; | 63 TrustStoreCollection collection; |
85 collection.FindTrustAnchorsForCert(target_, base::Bind(&NotCalled), | 64 collection.FindTrustAnchorsForCert(target_, &matches); |
86 &sync_matches, &req); | |
87 | 65 |
88 EXPECT_FALSE(req); | 66 EXPECT_TRUE(matches.empty()); |
89 EXPECT_TRUE(sync_matches.empty()); | |
90 } | 67 } |
91 | 68 |
92 // Collection contains only one synchronous store, should complete | 69 // Collection contains only one store. |
93 // synchronously. | 70 TEST_F(TrustStoreCollectionTest, OneStore) { |
94 TEST_F(TrustStoreCollectionTest, NoPrimaryStoreOneSyncStore) { | 71 TrustAnchors matches; |
95 std::unique_ptr<TrustStore::Request> req; | |
96 TrustAnchors sync_matches; | |
97 | 72 |
98 TrustStoreCollection collection; | 73 TrustStoreCollection collection; |
99 TrustStoreInMemory in_memory; | 74 TrustStoreInMemory in_memory; |
100 in_memory.AddTrustAnchor(newroot_); | 75 in_memory.AddTrustAnchor(newroot_); |
101 collection.AddTrustStoreSynchronousOnly(&in_memory); | 76 collection.AddTrustStore(&in_memory); |
102 collection.FindTrustAnchorsForCert(newintermediate_, base::Bind(&NotCalled), | 77 collection.FindTrustAnchorsForCert(newintermediate_, &matches); |
103 &sync_matches, &req); | |
104 | 78 |
105 EXPECT_FALSE(req); | 79 ASSERT_EQ(1U, matches.size()); |
106 ASSERT_EQ(1U, sync_matches.size()); | 80 EXPECT_EQ(newroot_, matches[0]); |
107 EXPECT_EQ(newroot_, sync_matches[0]); | |
108 } | 81 } |
109 | 82 |
110 // Collection contains two synchronous stores, should complete synchronously. | 83 // Collection contains two stores. |
111 TEST_F(TrustStoreCollectionTest, NoPrimaryStoreTwoSyncStores) { | 84 TEST_F(TrustStoreCollectionTest, TwoStores) { |
112 std::unique_ptr<TrustStore::Request> req; | 85 TrustAnchors matches; |
113 TrustAnchors sync_matches; | |
114 | 86 |
115 TrustStoreCollection collection; | 87 TrustStoreCollection collection; |
116 TrustStoreInMemory in_memory1; | 88 TrustStoreInMemory in_memory1; |
117 TrustStoreInMemory in_memory2; | 89 TrustStoreInMemory in_memory2; |
118 in_memory1.AddTrustAnchor(newroot_); | 90 in_memory1.AddTrustAnchor(newroot_); |
119 in_memory2.AddTrustAnchor(oldroot_); | 91 in_memory2.AddTrustAnchor(oldroot_); |
120 collection.AddTrustStoreSynchronousOnly(&in_memory1); | 92 collection.AddTrustStore(&in_memory1); |
121 collection.AddTrustStoreSynchronousOnly(&in_memory2); | 93 collection.AddTrustStore(&in_memory2); |
122 collection.FindTrustAnchorsForCert(newintermediate_, base::Bind(&NotCalled), | 94 collection.FindTrustAnchorsForCert(newintermediate_, &matches); |
123 &sync_matches, &req); | |
124 | 95 |
125 EXPECT_FALSE(req); | 96 ASSERT_EQ(2U, matches.size()); |
126 ASSERT_EQ(2U, sync_matches.size()); | 97 EXPECT_EQ(newroot_, matches[0]); |
127 EXPECT_EQ(newroot_, sync_matches[0]); | 98 EXPECT_EQ(oldroot_, matches[1]); |
128 EXPECT_EQ(oldroot_, sync_matches[1]); | |
129 } | |
130 | |
131 // The secondary stores in the collection should not be passed a callback to | |
132 // their FindTrustAnchorsForCert call. | |
133 TEST_F(TrustStoreCollectionTest, SyncStoresAreQueriedSynchronously) { | |
134 std::unique_ptr<TrustStore::Request> req; | |
135 TrustAnchors sync_matches; | |
136 | |
137 TrustStoreCollection collection; | |
138 StrictMock<MockTrustStore> store; | |
139 collection.AddTrustStoreSynchronousOnly(&store); | |
140 | |
141 EXPECT_CALL( | |
142 store, | |
143 FindTrustAnchorsForCert( | |
144 _, Property(&TrustStore::TrustAnchorsCallback::is_null, true), _, _)); | |
145 | |
146 collection.FindTrustAnchorsForCert(newintermediate_, base::Bind(&NotCalled), | |
147 &sync_matches, &req); | |
148 | |
149 EXPECT_FALSE(req); | |
150 EXPECT_TRUE(sync_matches.empty()); | |
151 } | |
152 | |
153 // If the primary store completes synchronously, TrustStoreCollection should | |
154 // complete synchronously also. | |
155 TEST_F(TrustStoreCollectionTest, AllStoresAreSynchronous) { | |
156 std::unique_ptr<TrustStore::Request> req; | |
157 TrustAnchors sync_matches; | |
158 | |
159 TrustStoreCollection collection; | |
160 TrustStoreInMemory in_memory1; | |
161 TrustStoreInMemory in_memory2; | |
162 in_memory1.AddTrustAnchor(newroot_); | |
163 in_memory2.AddTrustAnchor(oldroot_); | |
164 collection.SetPrimaryTrustStore(&in_memory1); | |
165 collection.AddTrustStoreSynchronousOnly(&in_memory2); | |
166 collection.FindTrustAnchorsForCert(newintermediate_, base::Bind(&NotCalled), | |
167 &sync_matches, &req); | |
168 | |
169 EXPECT_FALSE(req); | |
170 ASSERT_EQ(2U, sync_matches.size()); | |
171 EXPECT_EQ(newroot_, sync_matches[0]); | |
172 EXPECT_EQ(oldroot_, sync_matches[1]); | |
173 } | |
174 | |
175 // Primary store returns results asynchronously. No secondary stores registered. | |
176 TEST_F(TrustStoreCollectionTest, AsyncPrimaryStore) { | |
177 std::unique_ptr<TrustStore::Request> req; | |
178 TrustAnchors sync_matches; | |
179 | |
180 TrustStoreInMemoryAsync in_memory_async; | |
181 in_memory_async.AddAsyncTrustAnchor(newroot_); | |
182 | |
183 TrustStoreCollection collection; | |
184 collection.SetPrimaryTrustStore(&in_memory_async); | |
185 | |
186 TrustAnchorResultRecorder anchor_results; | |
187 collection.FindTrustAnchorsForCert( | |
188 newintermediate_, anchor_results.Callback(), &sync_matches, &req); | |
189 | |
190 ASSERT_TRUE(req); | |
191 EXPECT_TRUE(sync_matches.empty()); | |
192 | |
193 anchor_results.Run(); | |
194 ASSERT_EQ(1U, anchor_results.matches().size()); | |
195 EXPECT_EQ(newroot_, anchor_results.matches()[0]); | |
196 } | |
197 | |
198 // Primary store returns results both synchronously and asynchronously, and | |
199 // a secondary store returns results synchronously as well. | |
200 TEST_F(TrustStoreCollectionTest, SyncAndAsyncPrimaryStoreAndSyncStore) { | |
201 std::unique_ptr<TrustStore::Request> req; | |
202 TrustAnchors sync_matches; | |
203 | |
204 TrustStoreInMemoryAsync in_memory_async; | |
205 in_memory_async.AddAsyncTrustAnchor(newroot_); | |
206 in_memory_async.AddSyncTrustAnchor(newrootrollover_); | |
207 | |
208 TrustStoreInMemory in_memory; | |
209 in_memory.AddTrustAnchor(oldroot_); | |
210 | |
211 TrustStoreCollection collection; | |
212 collection.SetPrimaryTrustStore(&in_memory_async); | |
213 collection.AddTrustStoreSynchronousOnly(&in_memory); | |
214 | |
215 TrustAnchorResultRecorder anchor_results; | |
216 collection.FindTrustAnchorsForCert( | |
217 newintermediate_, anchor_results.Callback(), &sync_matches, &req); | |
218 | |
219 ASSERT_TRUE(req); | |
220 ASSERT_EQ(2U, sync_matches.size()); | |
221 EXPECT_EQ(newrootrollover_, sync_matches[0]); | |
222 EXPECT_EQ(oldroot_, sync_matches[1]); | |
223 | |
224 anchor_results.Run(); | |
225 ASSERT_EQ(1U, anchor_results.matches().size()); | |
226 EXPECT_EQ(newroot_, anchor_results.matches()[0]); | |
227 } | 99 } |
228 | 100 |
229 } // namespace | 101 } // namespace |
230 | 102 |
231 } // namespace net | 103 } // namespace net |
OLD | NEW |