QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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/third_party/quiche/src/quic/test_tools/quic_crypto_server_config_peer.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/test_tools/mock_clock.h" |
| 8 | #include "net/third_party/quiche/src/quic/test_tools/mock_random.h" |
| 9 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 10 | |
| 11 | namespace quic { |
| 12 | namespace test { |
| 13 | |
| 14 | QuicReferenceCountedPointer<QuicCryptoServerConfig::Config> |
| 15 | QuicCryptoServerConfigPeer::GetPrimaryConfig() { |
| 16 | QuicReaderMutexLock locked(&server_config_->configs_lock_); |
| 17 | return QuicReferenceCountedPointer<QuicCryptoServerConfig::Config>( |
| 18 | server_config_->primary_config_); |
| 19 | } |
| 20 | |
| 21 | QuicReferenceCountedPointer<QuicCryptoServerConfig::Config> |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 22 | QuicCryptoServerConfigPeer::GetConfig(std::string config_id) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | QuicReaderMutexLock locked(&server_config_->configs_lock_); |
| 24 | if (config_id == "<primary>") { |
| 25 | return QuicReferenceCountedPointer<QuicCryptoServerConfig::Config>( |
| 26 | server_config_->primary_config_); |
| 27 | } else { |
| 28 | return server_config_->GetConfigWithScid(config_id); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | ProofSource* QuicCryptoServerConfigPeer::GetProofSource() const { |
| 33 | return server_config_->proof_source_.get(); |
| 34 | } |
| 35 | |
| 36 | void QuicCryptoServerConfigPeer::ResetProofSource( |
| 37 | std::unique_ptr<ProofSource> proof_source) { |
| 38 | server_config_->proof_source_ = std::move(proof_source); |
| 39 | } |
| 40 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 41 | std::string QuicCryptoServerConfigPeer::NewSourceAddressToken( |
| 42 | std::string config_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 43 | SourceAddressTokens previous_tokens, |
| 44 | const QuicIpAddress& ip, |
| 45 | QuicRandom* rand, |
| 46 | QuicWallTime now, |
| 47 | CachedNetworkParameters* cached_network_params) { |
| 48 | return server_config_->NewSourceAddressToken(*GetConfig(config_id), |
| 49 | previous_tokens, ip, rand, now, |
| 50 | cached_network_params); |
| 51 | } |
| 52 | |
| 53 | HandshakeFailureReason QuicCryptoServerConfigPeer::ValidateSourceAddressTokens( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 54 | std::string config_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 55 | QuicStringPiece srct, |
| 56 | const QuicIpAddress& ip, |
| 57 | QuicWallTime now, |
| 58 | CachedNetworkParameters* cached_network_params) { |
| 59 | SourceAddressTokens tokens; |
| 60 | HandshakeFailureReason reason = server_config_->ParseSourceAddressToken( |
| 61 | *GetConfig(config_id), srct, &tokens); |
| 62 | if (reason != HANDSHAKE_OK) { |
| 63 | return reason; |
| 64 | } |
| 65 | |
| 66 | return server_config_->ValidateSourceAddressTokens(tokens, ip, now, |
| 67 | cached_network_params); |
| 68 | } |
| 69 | |
| 70 | HandshakeFailureReason |
| 71 | QuicCryptoServerConfigPeer::ValidateSingleSourceAddressToken( |
| 72 | QuicStringPiece token, |
| 73 | const QuicIpAddress& ip, |
| 74 | QuicWallTime now) { |
| 75 | SourceAddressTokens tokens; |
| 76 | HandshakeFailureReason parse_status = server_config_->ParseSourceAddressToken( |
| 77 | *GetPrimaryConfig(), token, &tokens); |
| 78 | if (HANDSHAKE_OK != parse_status) { |
| 79 | return parse_status; |
| 80 | } |
| 81 | EXPECT_EQ(1, tokens.tokens_size()); |
| 82 | return server_config_->ValidateSingleSourceAddressToken(tokens.tokens(0), ip, |
| 83 | now); |
| 84 | } |
| 85 | |
| 86 | void QuicCryptoServerConfigPeer::CheckConfigs( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 87 | std::vector<std::pair<std::string, bool>> expected_ids_and_status) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 88 | QuicReaderMutexLock locked(&server_config_->configs_lock_); |
| 89 | |
| 90 | ASSERT_EQ(expected_ids_and_status.size(), server_config_->configs_.size()) |
| 91 | << ConfigsDebug(); |
| 92 | |
| 93 | for (const std::pair< |
| 94 | const ServerConfigID, |
| 95 | QuicReferenceCountedPointer<QuicCryptoServerConfig::Config>>& i : |
| 96 | server_config_->configs_) { |
| 97 | bool found = false; |
| 98 | for (std::pair<ServerConfigID, bool>& j : expected_ids_and_status) { |
| 99 | if (i.first == j.first && i.second->is_primary == j.second) { |
| 100 | found = true; |
| 101 | j.first.clear(); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | ASSERT_TRUE(found) << "Failed to find match for " << i.first |
| 107 | << " in configs:\n" |
| 108 | << ConfigsDebug(); |
| 109 | } |
| 110 | } |
| 111 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 112 | // ConfigsDebug returns a std::string that contains debugging information about |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 113 | // the set of Configs loaded in |server_config_| and their status. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 114 | std::string QuicCryptoServerConfigPeer::ConfigsDebug() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 115 | if (server_config_->configs_.empty()) { |
| 116 | return "No Configs in QuicCryptoServerConfig"; |
| 117 | } |
| 118 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 119 | std::string s; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 120 | |
| 121 | for (const auto& i : server_config_->configs_) { |
| 122 | const QuicReferenceCountedPointer<QuicCryptoServerConfig::Config> config = |
| 123 | i.second; |
| 124 | if (config->is_primary) { |
| 125 | s += "(primary) "; |
| 126 | } else { |
| 127 | s += " "; |
| 128 | } |
| 129 | s += config->id; |
| 130 | s += "\n"; |
| 131 | } |
| 132 | |
| 133 | return s; |
| 134 | } |
| 135 | |
| 136 | void QuicCryptoServerConfigPeer::SelectNewPrimaryConfig(int seconds) { |
| 137 | QuicWriterMutexLock locked(&server_config_->configs_lock_); |
| 138 | server_config_->SelectNewPrimaryConfig( |
| 139 | QuicWallTime::FromUNIXSeconds(seconds)); |
| 140 | } |
| 141 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 142 | std::string QuicCryptoServerConfigPeer::CompressChain( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 143 | QuicCompressedCertsCache* compressed_certs_cache, |
| 144 | const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 145 | const std::string& client_common_set_hashes, |
| 146 | const std::string& client_cached_cert_hashes, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 147 | const CommonCertSets* common_sets) { |
| 148 | return QuicCryptoServerConfig::CompressChain( |
| 149 | compressed_certs_cache, chain, client_common_set_hashes, |
| 150 | client_cached_cert_hashes, common_sets); |
| 151 | } |
| 152 | |
| 153 | uint32_t QuicCryptoServerConfigPeer::source_address_token_future_secs() { |
| 154 | return server_config_->source_address_token_future_secs_; |
| 155 | } |
| 156 | |
| 157 | uint32_t QuicCryptoServerConfigPeer::source_address_token_lifetime_secs() { |
| 158 | return server_config_->source_address_token_lifetime_secs_; |
| 159 | } |
| 160 | |
| 161 | } // namespace test |
| 162 | } // namespace quic |