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.
4
5#include "net/quic/quic_crypto_stream.h"
6
7#include <string>
8#include <vector>
9
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"
18
19using std::string;
20using std::vector;
21
22namespace net {
23namespace test {
24namespace {
25
26class MockQuicCryptoStream : public QuicCryptoStream {
27 public:
28  explicit MockQuicCryptoStream(QuicSession* session)
29      : QuicCryptoStream(session) {
30  }
31
32  virtual void OnHandshakeMessage(
33      const CryptoHandshakeMessage& message) OVERRIDE {
34    messages_.push_back(message);
35  }
36
37  vector<CryptoHandshakeMessage>* messages() {
38    return &messages_;
39  }
40
41 private:
42  vector<CryptoHandshakeMessage> messages_;
43
44  DISALLOW_COPY_AND_ASSIGN(MockQuicCryptoStream);
45};
46
47class 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();
57  }
58
59  void ConstructHandshakeMessage() {
60    CryptoFramer framer;
61    message_data_.reset(framer.ConstructHandshakeMessage(message_));
62  }
63
64 protected:
65  MockConnection* connection_;
66  MockSession session_;
67  MockQuicCryptoStream stream_;
68  CryptoHandshakeMessage message_;
69  scoped_ptr<QuicData> message_data_;
70
71 private:
72  DISALLOW_COPY_AND_ASSIGN(QuicCryptoStreamTest);
73};
74
75TEST_F(QuicCryptoStreamTest, NotInitiallyConected) {
76  EXPECT_FALSE(stream_.encryption_established());
77  EXPECT_FALSE(stream_.handshake_confirmed());
78}
79
80TEST_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));
90}
91
92TEST_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
99
100  EXPECT_CALL(*connection_,
101              SendConnectionClose(QUIC_CRYPTO_TAGS_OUT_OF_ORDER));
102  EXPECT_EQ(0u, stream_.ProcessRawData(bad.data(), bad.length()));
103}
104
105TEST_F(QuicCryptoStreamTest, NoConnectionLevelFlowControl) {
106  if (connection_->version() < QUIC_VERSION_21) {
107    EXPECT_FALSE(stream_.flow_controller()->IsEnabled());
108  } else {
109    EXPECT_TRUE(stream_.flow_controller()->IsEnabled());
110  }
111  EXPECT_FALSE(ReliableQuicStreamPeer::StreamContributesToConnectionFlowControl(
112      &stream_));
113}
114
115}  // namespace
116}  // namespace test
117}  // namespace net
118