1 // Copyright (c) 2012 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/quic_crypto_stream.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "net/quic/crypto/crypto_handshake.h"
12 #include "net/quic/crypto/crypto_protocol.h"
13 #include "net/quic/quic_flags.h"
14 #include "net/quic/test_tools/crypto_test_utils.h"
15 #include "net/quic/test_tools/quic_test_utils.h"
16 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
27 class MockQuicCryptoStream
: public QuicCryptoStream
{
29 explicit MockQuicCryptoStream(QuicSession
* session
)
30 : QuicCryptoStream(session
) {
33 virtual void OnHandshakeMessage(
34 const CryptoHandshakeMessage
& message
) OVERRIDE
{
35 messages_
.push_back(message
);
38 vector
<CryptoHandshakeMessage
>* messages() {
43 vector
<CryptoHandshakeMessage
> messages_
;
45 DISALLOW_COPY_AND_ASSIGN(MockQuicCryptoStream
);
48 class QuicCryptoStreamTest
: public ::testing::Test
{
50 QuicCryptoStreamTest()
51 : connection_(new MockConnection(false)),
52 session_(connection_
),
54 message_
.set_tag(kSHLO
);
55 message_
.SetStringPiece(1, "abc");
56 message_
.SetStringPiece(2, "def");
57 ConstructHandshakeMessage();
60 void ConstructHandshakeMessage() {
62 message_data_
.reset(framer
.ConstructHandshakeMessage(message_
));
66 MockConnection
* connection_
;
68 MockQuicCryptoStream stream_
;
69 CryptoHandshakeMessage message_
;
70 scoped_ptr
<QuicData
> message_data_
;
73 DISALLOW_COPY_AND_ASSIGN(QuicCryptoStreamTest
);
76 TEST_F(QuicCryptoStreamTest
, NotInitiallyConected
) {
77 EXPECT_FALSE(stream_
.encryption_established());
78 EXPECT_FALSE(stream_
.handshake_confirmed());
81 TEST_F(QuicCryptoStreamTest
, ProcessRawData
) {
82 EXPECT_EQ(message_data_
->length(),
83 stream_
.ProcessRawData(message_data_
->data(),
84 message_data_
->length()));
85 ASSERT_EQ(1u, stream_
.messages()->size());
86 const CryptoHandshakeMessage
& message
= (*stream_
.messages())[0];
87 EXPECT_EQ(kSHLO
, message
.tag());
88 EXPECT_EQ(2u, message
.tag_value_map().size());
89 EXPECT_EQ("abc", CryptoTestUtils::GetValueForTag(message
, 1));
90 EXPECT_EQ("def", CryptoTestUtils::GetValueForTag(message
, 2));
93 TEST_F(QuicCryptoStreamTest
, ProcessBadData
) {
94 string
bad(message_data_
->data(), message_data_
->length());
95 const int kFirstTagIndex
= sizeof(uint32
) + // message tag
96 sizeof(uint16
) + // number of tag-value pairs
97 sizeof(uint16
); // padding
98 EXPECT_EQ(1, bad
[kFirstTagIndex
]);
99 bad
[kFirstTagIndex
] = 0x7F; // out of order tag
101 EXPECT_CALL(*connection_
,
102 SendConnectionClose(QUIC_CRYPTO_TAGS_OUT_OF_ORDER
));
103 EXPECT_EQ(0u, stream_
.ProcessRawData(bad
.data(), bad
.length()));
106 TEST_F(QuicCryptoStreamTest
, NoConnectionLevelFlowControl
) {
107 ValueRestore
<bool> old_flag(&FLAGS_enable_quic_connection_flow_control_2
,
109 if (connection_
->version() <= QUIC_VERSION_20
) {
110 EXPECT_FALSE(stream_
.flow_controller()->IsEnabled());
112 EXPECT_TRUE(stream_
.flow_controller()->IsEnabled());
114 EXPECT_FALSE(ReliableQuicStreamPeer::StreamContributesToConnectionFlowControl(