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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
// Copyright (c) 2012 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/quic/quic_session.h"
#include "net/quic/quic_connection.h"
#include <set>
#include "base/hash_tables.h"
#include "net/quic/crypto/crypto_handshake.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::hash_map;
using std::set;
using testing::_;
using testing::InSequence;
namespace net {
namespace test {
namespace {
class TestCryptoStream : public QuicCryptoStream {
public:
explicit TestCryptoStream(QuicSession* session)
: QuicCryptoStream(session) {
}
virtual void OnHandshakeMessage(
const CryptoHandshakeMessage& message) OVERRIDE {
encryption_established_ = true;
handshake_confirmed_ = true;
session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED);
}
};
class TestStream : public ReliableQuicStream {
public:
TestStream(QuicStreamId id, QuicSession* session)
: ReliableQuicStream(id, session) {
}
virtual uint32 ProcessData(const char* data, uint32 data_len) {
return data_len;
}
MOCK_METHOD0(OnCanWrite, void());
};
class TestSession : public QuicSession {
public:
TestSession(QuicConnection* connection, bool is_server)
: QuicSession(connection, is_server),
crypto_stream_(this) {
}
virtual QuicCryptoStream* GetCryptoStream() OVERRIDE {
return &crypto_stream_;
}
virtual TestStream* CreateOutgoingReliableStream() OVERRIDE {
TestStream* stream = new TestStream(GetNextStreamId(), this);
ActivateStream(stream);
return stream;
}
virtual TestStream* CreateIncomingReliableStream(QuicStreamId id) OVERRIDE {
return new TestStream(id, this);
}
bool IsClosedStream(QuicStreamId id) {
return QuicSession::IsClosedStream(id);
}
ReliableQuicStream* GetIncomingReliableStream(QuicStreamId stream_id) {
return QuicSession::GetIncomingReliableStream(stream_id);
}
// Helper method for gmock
void MarkTwoWriteBlocked() {
this->MarkWriteBlocked(2);
}
TestCryptoStream crypto_stream_;
};
class QuicSessionTest : public ::testing::Test {
protected:
QuicSessionTest()
: guid_(1),
connection_(new MockConnection(guid_, IPEndPoint(), false)),
session_(connection_, true) {
}
void CheckClosedStreams() {
for (int i = kCryptoStreamId; i < 100; i++) {
if (closed_streams_.count(i) == 0) {
EXPECT_FALSE(session_.IsClosedStream(i)) << " stream id: " << i;
} else {
EXPECT_TRUE(session_.IsClosedStream(i)) << " stream id: " << i;
}
}
}
void CloseStream(QuicStreamId id) {
session_.CloseStream(id);
closed_streams_.insert(id);
}
QuicGuid guid_;
MockConnection* connection_;
TestSession session_;
QuicConnectionVisitorInterface* visitor_;
hash_map<QuicStreamId, ReliableQuicStream*>* streams_;
set<QuicStreamId> closed_streams_;
};
TEST_F(QuicSessionTest, IsCryptoHandshakeConfirmed) {
EXPECT_FALSE(session_.IsCryptoHandshakeConfirmed());
CryptoHandshakeMessage message;
session_.crypto_stream_.OnHandshakeMessage(message);
EXPECT_TRUE(session_.IsCryptoHandshakeConfirmed());
}
TEST_F(QuicSessionTest, IsClosedStreamDefault) {
// Ensure that no streams are initially closed.
for (int i = kCryptoStreamId; i < 100; i++) {
EXPECT_FALSE(session_.IsClosedStream(i));
}
}
TEST_F(QuicSessionTest, IsClosedStreamLocallyCreated) {
TestStream* stream2 = session_.CreateOutgoingReliableStream();
EXPECT_EQ(2u, stream2->id());
TestStream* stream4 = session_.CreateOutgoingReliableStream();
EXPECT_EQ(4u, stream4->id());
CheckClosedStreams();
CloseStream(4);
CheckClosedStreams();
CloseStream(2);
CheckClosedStreams();
}
TEST_F(QuicSessionTest, IsClosedStreamPeerCreated) {
session_.GetIncomingReliableStream(3);
session_.GetIncomingReliableStream(5);
CheckClosedStreams();
CloseStream(3);
CheckClosedStreams();
CloseStream(5);
// Create stream id 9, and implicitly 7
session_.GetIncomingReliableStream(9);
CheckClosedStreams();
// Close 9, but make sure 7 is still not closed
CloseStream(9);
CheckClosedStreams();
}
TEST_F(QuicSessionTest, StreamIdTooLarge) {
session_.GetIncomingReliableStream(3);
EXPECT_CALL(*connection_, SendConnectionClose(QUIC_INVALID_STREAM_ID));
session_.GetIncomingReliableStream(105);
}
TEST_F(QuicSessionTest, OnCanWrite) {
TestStream* stream2 = session_.CreateOutgoingReliableStream();
TestStream* stream4 = session_.CreateOutgoingReliableStream();
TestStream* stream6 = session_.CreateOutgoingReliableStream();
session_.MarkWriteBlocked(2);
session_.MarkWriteBlocked(6);
session_.MarkWriteBlocked(4);
InSequence s;
EXPECT_CALL(*stream2, OnCanWrite()).WillOnce(
// Reregister, to test the loop limit.
testing::InvokeWithoutArgs(&session_, &TestSession::MarkTwoWriteBlocked));
EXPECT_CALL(*stream6, OnCanWrite());
EXPECT_CALL(*stream4, OnCanWrite());
EXPECT_FALSE(session_.OnCanWrite());
}
TEST_F(QuicSessionTest, OnCanWriteWithClosedStream) {
TestStream* stream2 = session_.CreateOutgoingReliableStream();
TestStream* stream4 = session_.CreateOutgoingReliableStream();
session_.CreateOutgoingReliableStream(); // stream 6
session_.MarkWriteBlocked(2);
session_.MarkWriteBlocked(6);
session_.MarkWriteBlocked(4);
CloseStream(6);
InSequence s;
EXPECT_CALL(*stream2, OnCanWrite());
EXPECT_CALL(*stream4, OnCanWrite());
EXPECT_TRUE(session_.OnCanWrite());
}
TEST_F(QuicSessionTest, SendGoAway) {
// After sending a GoAway, ensure new incoming streams cannot be created and
// result in a RST being sent.
EXPECT_CALL(*connection_,
SendGoAway(QUIC_PEER_GOING_AWAY, 0u, "Going Away."));
session_.SendGoAway(QUIC_PEER_GOING_AWAY, "Going Away.");
EXPECT_TRUE(session_.goaway_sent());
EXPECT_CALL(*connection_, SendRstStream(3u, QUIC_STREAM_PEER_GOING_AWAY));
EXPECT_FALSE(session_.GetIncomingReliableStream(3u));
}
} // namespace
} // namespace test
} // namespace net
|