Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / quic / quic_crypto_stream_test.cc
blob76649128dadc884b6b10e9b4b385609d5e7f0cf7
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"
7 #include <string>
8 #include <vector>
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/test_tools/crypto_test_utils.h"
14 #include "net/quic/test_tools/quic_test_utils.h"
15 #include "net/quic/test_tools/reliable_quic_stream_peer.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using std::string;
20 using std::vector;
22 namespace net {
23 namespace test {
24 namespace {
26 class MockQuicCryptoStream : public QuicCryptoStream {
27 public:
28 explicit MockQuicCryptoStream(QuicSession* session)
29 : QuicCryptoStream(session) {
32 virtual void OnHandshakeMessage(
33 const CryptoHandshakeMessage& message) OVERRIDE {
34 messages_.push_back(message);
37 vector<CryptoHandshakeMessage>* messages() {
38 return &messages_;
41 private:
42 vector<CryptoHandshakeMessage> messages_;
44 DISALLOW_COPY_AND_ASSIGN(MockQuicCryptoStream);
47 class QuicCryptoStreamTest : public ::testing::Test {
48 public:
49 QuicCryptoStreamTest()
50 : connection_(new MockConnection(false)),
51 session_(connection_),
52 stream_(&session_) {
53 message_.set_tag(kSHLO);
54 message_.SetStringPiece(1, "abc");
55 message_.SetStringPiece(2, "def");
56 ConstructHandshakeMessage();
59 void ConstructHandshakeMessage() {
60 CryptoFramer framer;
61 message_data_.reset(framer.ConstructHandshakeMessage(message_));
64 protected:
65 MockConnection* connection_;
66 MockSession session_;
67 MockQuicCryptoStream stream_;
68 CryptoHandshakeMessage message_;
69 scoped_ptr<QuicData> message_data_;
71 private:
72 DISALLOW_COPY_AND_ASSIGN(QuicCryptoStreamTest);
75 TEST_F(QuicCryptoStreamTest, NotInitiallyConected) {
76 EXPECT_FALSE(stream_.encryption_established());
77 EXPECT_FALSE(stream_.handshake_confirmed());
80 TEST_F(QuicCryptoStreamTest, ProcessRawData) {
81 EXPECT_EQ(message_data_->length(),
82 stream_.ProcessRawData(message_data_->data(),
83 message_data_->length()));
84 ASSERT_EQ(1u, stream_.messages()->size());
85 const CryptoHandshakeMessage& message = (*stream_.messages())[0];
86 EXPECT_EQ(kSHLO, message.tag());
87 EXPECT_EQ(2u, message.tag_value_map().size());
88 EXPECT_EQ("abc", CryptoTestUtils::GetValueForTag(message, 1));
89 EXPECT_EQ("def", CryptoTestUtils::GetValueForTag(message, 2));
92 TEST_F(QuicCryptoStreamTest, ProcessBadData) {
93 string bad(message_data_->data(), message_data_->length());
94 const int kFirstTagIndex = sizeof(uint32) + // message tag
95 sizeof(uint16) + // number of tag-value pairs
96 sizeof(uint16); // padding
97 EXPECT_EQ(1, bad[kFirstTagIndex]);
98 bad[kFirstTagIndex] = 0x7F; // out of order tag
100 EXPECT_CALL(*connection_,
101 SendConnectionClose(QUIC_CRYPTO_TAGS_OUT_OF_ORDER));
102 EXPECT_EQ(0u, stream_.ProcessRawData(bad.data(), bad.length()));
105 TEST_F(QuicCryptoStreamTest, NoConnectionLevelFlowControl) {
106 if (connection_->version() <= QUIC_VERSION_20) {
107 EXPECT_FALSE(stream_.flow_controller()->IsEnabled());
108 } else {
109 EXPECT_TRUE(stream_.flow_controller()->IsEnabled());
111 EXPECT_FALSE(ReliableQuicStreamPeer::StreamContributesToConnectionFlowControl(
112 &stream_));
115 } // namespace
116 } // namespace test
117 } // namespace net