1 // Copyright 2013 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.
5 #include "net/quic/crypto/quic_crypto_client_config.h"
7 #include "net/quic/crypto/proof_verifier.h"
8 #include "net/quic/quic_session_key.h"
9 #include "net/quic/test_tools/quic_test_utils.h"
10 #include "testing/gtest/include/gtest/gtest.h"
17 TEST(QuicCryptoClientConfigTest
, CachedState_IsEmpty
) {
18 QuicCryptoClientConfig::CachedState state
;
19 EXPECT_TRUE(state
.IsEmpty());
22 TEST(QuicCryptoClientConfigTest
, CachedState_IsComplete
) {
23 QuicCryptoClientConfig::CachedState state
;
24 EXPECT_FALSE(state
.IsComplete(QuicWallTime::FromUNIXSeconds(0)));
27 TEST(QuicCryptoClientConfigTest
, CachedState_GenerationCounter
) {
28 QuicCryptoClientConfig::CachedState state
;
29 EXPECT_EQ(0u, state
.generation_counter());
30 state
.SetProofInvalid();
31 EXPECT_EQ(1u, state
.generation_counter());
34 TEST(QuicCryptoClientConfigTest
, CachedState_SetProofVerifyDetails
) {
35 QuicCryptoClientConfig::CachedState state
;
36 EXPECT_TRUE(state
.proof_verify_details() == NULL
);
37 ProofVerifyDetails
* details
= new ProofVerifyDetails
;
38 state
.SetProofVerifyDetails(details
);
39 EXPECT_EQ(details
, state
.proof_verify_details());
42 TEST(QuicCryptoClientConfigTest
, CachedState_InitializeFrom
) {
43 QuicCryptoClientConfig::CachedState state
;
44 QuicCryptoClientConfig::CachedState other
;
45 state
.set_source_address_token("TOKEN");
46 // TODO(rch): Populate other fields of |state|.
47 other
.InitializeFrom(state
);
48 EXPECT_EQ(state
.server_config(), other
.server_config());
49 EXPECT_EQ(state
.source_address_token(), other
.source_address_token());
50 EXPECT_EQ(state
.certs(), other
.certs());
51 EXPECT_EQ(1u, other
.generation_counter());
54 TEST(QuicCryptoClientConfigTest
, InchoateChlo
) {
55 QuicCryptoClientConfig::CachedState state
;
56 QuicCryptoClientConfig config
;
57 QuicCryptoNegotiatedParameters params
;
58 CryptoHandshakeMessage msg
;
59 QuicSessionKey
server_key("www.google.com", 80, false, kPrivacyModeDisabled
);
60 config
.FillInchoateClientHello(server_key
, QuicVersionMax(), &state
,
64 EXPECT_EQ(QUIC_NO_ERROR
, msg
.GetUint32(kVER
, &cver
));
65 EXPECT_EQ(QuicVersionToQuicTag(QuicVersionMax()), cver
);
68 TEST(QuicCryptoClientConfigTest
, PreferAesGcm
) {
69 QuicCryptoClientConfig config
;
71 if (config
.aead
.size() > 1)
72 EXPECT_NE(kAESG
, config
.aead
[0]);
73 config
.PreferAesGcm();
74 EXPECT_EQ(kAESG
, config
.aead
[0]);
77 TEST(QuicCryptoClientConfigTest
, InchoateChloSecure
) {
78 QuicCryptoClientConfig::CachedState state
;
79 QuicCryptoClientConfig config
;
80 QuicCryptoNegotiatedParameters params
;
81 CryptoHandshakeMessage msg
;
82 QuicSessionKey
server_key("www.google.com", 443, true, kPrivacyModeDisabled
);
83 config
.FillInchoateClientHello(server_key
, QuicVersionMax(), &state
,
87 EXPECT_EQ(QUIC_NO_ERROR
, msg
.GetUint32(kPDMD
, &pdmd
));
88 EXPECT_EQ(kX509
, pdmd
);
91 TEST(QuicCryptoClientConfigTest
, InchoateChloSecureNoEcdsa
) {
92 QuicCryptoClientConfig::CachedState state
;
93 QuicCryptoClientConfig config
;
94 config
.DisableEcdsa();
95 QuicCryptoNegotiatedParameters params
;
96 CryptoHandshakeMessage msg
;
97 QuicSessionKey
server_key("www.google.com", 443, true, kPrivacyModeDisabled
);
98 config
.FillInchoateClientHello(server_key
, QuicVersionMax(), &state
,
102 EXPECT_EQ(QUIC_NO_ERROR
, msg
.GetUint32(kPDMD
, &pdmd
));
103 EXPECT_EQ(kX59R
, pdmd
);
106 TEST(QuicCryptoClientConfigTest
, ProcessServerDowngradeAttack
) {
107 QuicVersionVector supported_versions
= QuicSupportedVersions();
108 if (supported_versions
.size() == 1) {
109 // No downgrade attack is possible if the client only supports one version.
112 QuicTagVector supported_version_tags
;
113 for (size_t i
= supported_versions
.size(); i
> 0; --i
) {
114 supported_version_tags
.push_back(
115 QuicVersionToQuicTag(supported_versions
[i
- 1]));
117 CryptoHandshakeMessage msg
;
119 msg
.SetVector(kVER
, supported_version_tags
);
121 QuicCryptoClientConfig::CachedState cached
;
122 QuicCryptoNegotiatedParameters out_params
;
124 QuicCryptoClientConfig config
;
125 EXPECT_EQ(QUIC_VERSION_NEGOTIATION_MISMATCH
,
126 config
.ProcessServerHello(msg
, 0, supported_versions
,
127 &cached
, &out_params
, &error
));
128 EXPECT_EQ("Downgrade attack detected", error
);
131 TEST(QuicCryptoClientConfigTest
, InitializeFrom
) {
132 QuicCryptoClientConfig config
;
133 QuicSessionKey
canonical_key1("www.google.com", 80, false,
134 kPrivacyModeDisabled
);
135 QuicCryptoClientConfig::CachedState
* state
=
136 config
.LookupOrCreate(canonical_key1
);
137 // TODO(rch): Populate other fields of |state|.
138 state
->set_source_address_token("TOKEN");
139 state
->SetProofValid();
141 QuicSessionKey
other_key("mail.google.com", 80, false, kPrivacyModeDisabled
);
142 config
.InitializeFrom(other_key
, canonical_key1
, &config
);
143 QuicCryptoClientConfig::CachedState
* other
= config
.LookupOrCreate(other_key
);
145 EXPECT_EQ(state
->server_config(), other
->server_config());
146 EXPECT_EQ(state
->source_address_token(), other
->source_address_token());
147 EXPECT_EQ(state
->certs(), other
->certs());
148 EXPECT_EQ(1u, other
->generation_counter());