OLD | NEW |
| (Empty) |
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_session.h" | |
6 #include "net/quic/quic_connection.h" | |
7 | |
8 #include <set> | |
9 | |
10 #include "base/hash_tables.h" | |
11 #include "net/quic/test_tools/quic_test_utils.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 using base::hash_map; | |
16 using std::set; | |
17 using testing::_; | |
18 | |
19 namespace net { | |
20 namespace test { | |
21 namespace { | |
22 | |
23 class TestCryptoStream : public QuicCryptoStream { | |
24 public: | |
25 explicit TestCryptoStream(QuicSession* session) | |
26 : QuicCryptoStream(session) { | |
27 } | |
28 | |
29 void OnHandshakeMessage(const CryptoHandshakeMessage& message) { | |
30 set_handshake_complete(true); | |
31 } | |
32 }; | |
33 | |
34 class TestStream : public ReliableQuicStream { | |
35 public: | |
36 TestStream(QuicStreamId id, QuicSession* session) | |
37 : ReliableQuicStream(id, session) { | |
38 } | |
39 | |
40 virtual uint32 ProcessData(const char* data, uint32 data_len) { | |
41 return data_len; | |
42 } | |
43 }; | |
44 | |
45 class TestSession : public QuicSession { | |
46 public: | |
47 TestSession(QuicConnection* connection, bool is_server) | |
48 : QuicSession(connection, is_server), | |
49 crypto_stream_(this) { | |
50 } | |
51 | |
52 virtual QuicCryptoStream* GetCryptoStream() { | |
53 return &crypto_stream_; | |
54 } | |
55 | |
56 virtual TestStream* CreateOutgoingReliableStream() { | |
57 TestStream* stream = new TestStream(GetNextStreamId(), this); | |
58 ActivateStream(stream); | |
59 return stream; | |
60 } | |
61 | |
62 virtual TestStream* CreateIncomingReliableStream(QuicStreamId id) { | |
63 return new TestStream(id, this); | |
64 } | |
65 | |
66 bool IsClosedStream(QuicStreamId id) { | |
67 return QuicSession::IsClosedStream(id); | |
68 } | |
69 | |
70 ReliableQuicStream* GetIncomingReliableStream(QuicStreamId stream_id) { | |
71 return QuicSession::GetIncomingReliableStream(stream_id); | |
72 } | |
73 | |
74 TestCryptoStream crypto_stream_; | |
75 }; | |
76 | |
77 class QuicSessionTest : public ::testing::Test { | |
78 protected: | |
79 QuicSessionTest() | |
80 : guid_(1), | |
81 connection_(new MockConnection(guid_, IPEndPoint())), | |
82 session_(connection_, true) { | |
83 } | |
84 | |
85 void CheckClosedStreams() { | |
86 for (int i = kCryptoStreamId; i < 100; i++) { | |
87 if (closed_streams_.count(i) == 0) { | |
88 EXPECT_FALSE(session_.IsClosedStream(i)) << " stream id: " << i; | |
89 } else { | |
90 EXPECT_TRUE(session_.IsClosedStream(i)) << " stream id: " << i; | |
91 } | |
92 } | |
93 } | |
94 | |
95 void CloseStream(QuicStreamId id) { | |
96 session_.CloseStream(id); | |
97 closed_streams_.insert(id); | |
98 } | |
99 | |
100 QuicGuid guid_; | |
101 MockConnection* connection_; | |
102 TestSession session_; | |
103 QuicConnectionVisitorInterface* visitor_; | |
104 hash_map<QuicStreamId, ReliableQuicStream*>* streams_; | |
105 set<QuicStreamId> closed_streams_; | |
106 }; | |
107 | |
108 TEST_F(QuicSessionTest, IsHandshakeComplete) { | |
109 EXPECT_FALSE(session_.IsHandshakeComplete()); | |
110 CryptoHandshakeMessage message; | |
111 session_.crypto_stream_.OnHandshakeMessage(message); | |
112 EXPECT_TRUE(session_.IsHandshakeComplete()); | |
113 } | |
114 | |
115 TEST_F(QuicSessionTest, IsClosedStreamDefault) { | |
116 // Ensure that no streams are initially closed. | |
117 for (int i = kCryptoStreamId; i < 100; i++) { | |
118 EXPECT_FALSE(session_.IsClosedStream(i)); | |
119 } | |
120 } | |
121 | |
122 TEST_F(QuicSessionTest, IsClosedStreamLocallyCreated) { | |
123 scoped_ptr<TestStream> stream2(session_.CreateOutgoingReliableStream()); | |
124 scoped_ptr<TestStream> stream4(session_.CreateOutgoingReliableStream()); | |
125 | |
126 CheckClosedStreams(); | |
127 CloseStream(4); | |
128 CheckClosedStreams(); | |
129 CloseStream(2); | |
130 CheckClosedStreams(); | |
131 } | |
132 | |
133 TEST_F(QuicSessionTest, IsClosedStreamPeerCreated) { | |
134 scoped_ptr<ReliableQuicStream> stream3(session_.GetIncomingReliableStream(3)); | |
135 scoped_ptr<ReliableQuicStream> stream5(session_.GetIncomingReliableStream(5)); | |
136 | |
137 CheckClosedStreams(); | |
138 CloseStream(3); | |
139 CheckClosedStreams(); | |
140 CloseStream(5); | |
141 // Create stream id 9, and implicitly 7 | |
142 scoped_ptr<ReliableQuicStream> stream9(session_.GetIncomingReliableStream(9)); | |
143 CheckClosedStreams(); | |
144 // Close 9, but make sure 7 is still not closed | |
145 CloseStream(9); | |
146 CheckClosedStreams(); | |
147 } | |
148 | |
149 TEST_F(QuicSessionTest, StreamIdTooLarge) { | |
150 scoped_ptr<ReliableQuicStream> stream3(session_.GetIncomingReliableStream(3)); | |
151 EXPECT_CALL(*connection_, SendConnectionClose(QUIC_INVALID_STREAM_ID)); | |
152 scoped_ptr<ReliableQuicStream> stream5( | |
153 session_.GetIncomingReliableStream(105)); | |
154 } | |
155 | |
156 } // namespace | |
157 } // namespace test | |
158 } // namespace net | |
OLD | NEW |