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