summaryrefslogtreecommitdiffstats
path: root/net/tools/quic/quic_server_session_test.cc
blob: 8690886f0d7cf44b13901e3d5ef2c53fa3a86d33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/tools/quic/quic_server_session.h"

#include "net/quic/crypto/quic_crypto_server_config.h"
#include "net/quic/crypto/quic_random.h"
#include "net/quic/quic_connection.h"
#include "net/quic/quic_utils.h"
#include "net/quic/test_tools/quic_connection_peer.h"
#include "net/quic/test_tools/quic_data_stream_peer.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "net/tools/epoll_server/epoll_server.h"
#include "net/tools/quic/quic_spdy_server_stream.h"
#include "net/tools/quic/test_tools/quic_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using __gnu_cxx::vector;
using net::test::MockConnection;
using net::test::QuicConnectionPeer;
using net::test::QuicDataStreamPeer;
using net::test::SupportedVersions;
using testing::_;
using testing::StrictMock;

namespace net {
namespace tools {
namespace test {

class QuicServerSessionPeer {
 public:
  static QuicDataStream* GetIncomingDataStream(
      QuicServerSession* s, QuicStreamId id) {
    return s->GetIncomingDataStream(id);
  }
  static QuicDataStream* GetDataStream(QuicServerSession* s, QuicStreamId id) {
    return s->GetDataStream(id);
  }
};

namespace {

class QuicServerSessionTest : public ::testing::TestWithParam<QuicVersion> {
 protected:
  QuicServerSessionTest()
      : crypto_config_(QuicCryptoServerConfig::TESTING,
                       QuicRandom::GetInstance()) {
    config_.SetDefaults();
    config_.set_max_streams_per_connection(3, 3);

    connection_ =
        new StrictMock<MockConnection>(true, SupportedVersions(GetParam()));
    session_.reset(new QuicServerSession(
        config_, connection_, &owner_));
    session_->InitializeSession(crypto_config_);
    visitor_ = QuicConnectionPeer::GetVisitor(connection_);
  }

  QuicVersion version() const { return connection_->version(); }

  StrictMock<MockQuicServerSessionVisitor> owner_;
  StrictMock<MockConnection>* connection_;
  QuicConfig config_;
  QuicCryptoServerConfig crypto_config_;
  scoped_ptr<QuicServerSession> session_;
  QuicConnectionVisitorInterface* visitor_;
};

INSTANTIATE_TEST_CASE_P(Tests, QuicServerSessionTest,
                        ::testing::ValuesIn(QuicSupportedVersions()));

TEST_P(QuicServerSessionTest, CloseStreamDueToReset) {
  QuicStreamId stream_id = 5;
  // Open a stream, then reset it.
  // Send two bytes of payload to open it.
  QuicStreamFrame data1(stream_id, false, 0, MakeIOVector("HT"));
  vector<QuicStreamFrame> frames;
  frames.push_back(data1);
  EXPECT_TRUE(visitor_->OnStreamFrames(frames));
  EXPECT_EQ(1u, session_->GetNumOpenStreams());

  // Send a reset (and expect the peer to send a RST in response).
  QuicRstStreamFrame rst1(stream_id, QUIC_STREAM_NO_ERROR, 0);
  if (version() > QUIC_VERSION_13) {
    EXPECT_CALL(*connection_,
                SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 0));
  }
  visitor_->OnRstStream(rst1);
  EXPECT_EQ(0u, session_->GetNumOpenStreams());

  // Send the same two bytes of payload in a new packet.
  EXPECT_TRUE(visitor_->OnStreamFrames(frames));

  // The stream should not be re-opened.
  EXPECT_EQ(0u, session_->GetNumOpenStreams());
}

TEST_P(QuicServerSessionTest, NeverOpenStreamDueToReset) {
  QuicStreamId stream_id = 5;

  // Send a reset (and expect the peer to send a RST in response).
  QuicRstStreamFrame rst1(stream_id, QUIC_STREAM_NO_ERROR, 0);
  if (version() > QUIC_VERSION_13) {
    EXPECT_CALL(*connection_,
                SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 0));
  }
  visitor_->OnRstStream(rst1);
  EXPECT_EQ(0u, session_->GetNumOpenStreams());

  // Send two bytes of payload.
  QuicStreamFrame data1(stream_id, false, 0, MakeIOVector("HT"));
  vector<QuicStreamFrame> frames;
  frames.push_back(data1);

  EXPECT_TRUE(visitor_->OnStreamFrames(frames));

  // The stream should never be opened, now that the reset is received.
  EXPECT_EQ(0u, session_->GetNumOpenStreams());
}

TEST_P(QuicServerSessionTest, AcceptClosedStream) {
  QuicStreamId stream_id = 5;
  vector<QuicStreamFrame> frames;
  // Send (empty) compressed headers followed by two bytes of data.
  frames.push_back(QuicStreamFrame(stream_id, false, 0,
                                   MakeIOVector("\1\0\0\0\0\0\0\0HT")));
  frames.push_back(QuicStreamFrame(stream_id + 2, false, 0,
                                   MakeIOVector("\2\0\0\0\0\0\0\0HT")));
  EXPECT_TRUE(visitor_->OnStreamFrames(frames));

  // Send a reset (and expect the peer to send a RST in response).
  QuicRstStreamFrame rst(stream_id, QUIC_STREAM_NO_ERROR, 0);
  if (version() > QUIC_VERSION_13) {
    EXPECT_CALL(*connection_,
                SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 0));
  }
  visitor_->OnRstStream(rst);

  // If we were tracking, we'd probably want to reject this because it's data
  // past the reset point of stream 3.  As it's a closed stream we just drop the
  // data on the floor, but accept the packet because it has data for stream 5.
  frames.clear();
  frames.push_back(QuicStreamFrame(stream_id, false, 2, MakeIOVector("TP")));
  frames.push_back(QuicStreamFrame(stream_id + 2, false, 2,
                                   MakeIOVector("TP")));
  EXPECT_TRUE(visitor_->OnStreamFrames(frames));
}

TEST_P(QuicServerSessionTest, MaxNumConnections) {
  QuicStreamId stream_id = 5;
  EXPECT_EQ(0u, session_->GetNumOpenStreams());
  EXPECT_TRUE(QuicServerSessionPeer::GetIncomingDataStream(session_.get(),
                                                           stream_id));
  EXPECT_TRUE(QuicServerSessionPeer::GetIncomingDataStream(session_.get(),
                                                           stream_id + 2));
  EXPECT_TRUE(QuicServerSessionPeer::GetIncomingDataStream(session_.get(),
                                                           stream_id + 4));
  EXPECT_CALL(*connection_, SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS));
  EXPECT_FALSE(QuicServerSessionPeer::GetIncomingDataStream(session_.get(),
                                                            stream_id + 6));
}

TEST_P(QuicServerSessionTest, MaxNumConnectionsImplicit) {
  QuicStreamId stream_id = 5;
  EXPECT_EQ(0u, session_->GetNumOpenStreams());
  EXPECT_TRUE(QuicServerSessionPeer::GetIncomingDataStream(session_.get(),
                                                           stream_id));
  // Implicitly opens two more streams.
  EXPECT_CALL(*connection_, SendConnectionClose(QUIC_TOO_MANY_OPEN_STREAMS));
  EXPECT_FALSE(QuicServerSessionPeer::GetIncomingDataStream(session_.get(),
                                                            stream_id + 6));
}

TEST_P(QuicServerSessionTest, GetEvenIncomingError) {
  // Incoming streams on the server session must be odd.
  EXPECT_CALL(*connection_, SendConnectionClose(QUIC_INVALID_STREAM_ID));
  EXPECT_EQ(NULL,
            QuicServerSessionPeer::GetIncomingDataStream(session_.get(), 4));
}

}  // namespace
}  // namespace test
}  // namespace tools
}  // namespace net