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