OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/buffered_spdy_framer.h" | 5 #include "net/spdy/buffered_spdy_framer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "net/spdy/spdy_test_util_common.h" | 8 #include "net/spdy/spdy_test_util_common.h" |
9 #include "testing/platform_test.h" | 9 #include "testing/platform_test.h" |
10 | 10 |
11 namespace net { | 11 namespace net { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 class TestBufferedSpdyVisitor : public BufferedSpdyFramerVisitorInterface { | 15 class TestBufferedSpdyVisitor : public BufferedSpdyFramerVisitorInterface { |
16 public: | 16 public: |
17 explicit TestBufferedSpdyVisitor(SpdyMajorVersion spdy_version) | 17 explicit TestBufferedSpdyVisitor(SpdyMajorVersion spdy_version) |
18 : buffered_spdy_framer_(spdy_version), | 18 : buffered_spdy_framer_(spdy_version), |
19 error_count_(0), | 19 error_count_(0), |
20 setting_count_(0), | 20 setting_count_(0), |
21 syn_frame_count_(0), | 21 syn_frame_count_(0), |
22 syn_reply_frame_count_(0), | 22 syn_reply_frame_count_(0), |
23 headers_frame_count_(0), | 23 headers_frame_count_(0), |
24 push_promise_frame_count_(0), | 24 push_promise_frame_count_(0), |
25 goaway_count_(0), | 25 goaway_count_(0), |
| 26 altsvc_count_(0), |
26 header_stream_id_(static_cast<SpdyStreamId>(-1)), | 27 header_stream_id_(static_cast<SpdyStreamId>(-1)), |
27 promised_stream_id_(static_cast<SpdyStreamId>(-1)) {} | 28 promised_stream_id_(static_cast<SpdyStreamId>(-1)) {} |
28 | 29 |
29 void OnError(SpdyFramer::SpdyError error_code) override { | 30 void OnError(SpdyFramer::SpdyError error_code) override { |
30 VLOG(1) << "SpdyFramer Error: " << error_code; | 31 VLOG(1) << "SpdyFramer Error: " << error_code; |
31 error_count_++; | 32 error_count_++; |
32 } | 33 } |
33 | 34 |
34 void OnStreamError(SpdyStreamId stream_id, | 35 void OnStreamError(SpdyStreamId stream_id, |
35 const std::string& description) override { | 36 const std::string& description) override { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 SpdyStreamId promised_stream_id, | 126 SpdyStreamId promised_stream_id, |
126 const SpdyHeaderBlock& headers) override { | 127 const SpdyHeaderBlock& headers) override { |
127 header_stream_id_ = stream_id; | 128 header_stream_id_ = stream_id; |
128 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream); | 129 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream); |
129 push_promise_frame_count_++; | 130 push_promise_frame_count_++; |
130 promised_stream_id_ = promised_stream_id; | 131 promised_stream_id_ = promised_stream_id; |
131 EXPECT_NE(promised_stream_id_, SpdyFramer::kInvalidStream); | 132 EXPECT_NE(promised_stream_id_, SpdyFramer::kInvalidStream); |
132 headers_ = headers; | 133 headers_ = headers; |
133 } | 134 } |
134 | 135 |
| 136 void OnAltSvc(SpdyStreamId stream_id, |
| 137 base::StringPiece origin, |
| 138 const SpdyAltSvcWireFormat::AlternativeServiceVector& |
| 139 altsvc_vector) override { |
| 140 altsvc_count_++; |
| 141 altsvc_stream_id_ = stream_id; |
| 142 origin.CopyToString(&altsvc_origin_); |
| 143 altsvc_vector_ = altsvc_vector; |
| 144 } |
| 145 |
135 bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) override { | 146 bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) override { |
136 return true; | 147 return true; |
137 } | 148 } |
138 | 149 |
139 // Convenience function which runs a framer simulation with particular input. | 150 // Convenience function which runs a framer simulation with particular input. |
140 void SimulateInFramer(const unsigned char* input, size_t size) { | 151 void SimulateInFramer(const unsigned char* input, size_t size) { |
141 buffered_spdy_framer_.set_visitor(this); | 152 buffered_spdy_framer_.set_visitor(this); |
142 size_t input_remaining = size; | 153 size_t input_remaining = size; |
143 const char* input_ptr = reinterpret_cast<const char*>(input); | 154 const char* input_ptr = reinterpret_cast<const char*>(input); |
144 while (input_remaining > 0 && | 155 while (input_remaining > 0 && |
(...skipping 14 matching lines...) Expand all Loading... |
159 BufferedSpdyFramer buffered_spdy_framer_; | 170 BufferedSpdyFramer buffered_spdy_framer_; |
160 | 171 |
161 // Counters from the visitor callbacks. | 172 // Counters from the visitor callbacks. |
162 int error_count_; | 173 int error_count_; |
163 int setting_count_; | 174 int setting_count_; |
164 int syn_frame_count_; | 175 int syn_frame_count_; |
165 int syn_reply_frame_count_; | 176 int syn_reply_frame_count_; |
166 int headers_frame_count_; | 177 int headers_frame_count_; |
167 int push_promise_frame_count_; | 178 int push_promise_frame_count_; |
168 int goaway_count_; | 179 int goaway_count_; |
| 180 int altsvc_count_; |
169 | 181 |
170 // Header block streaming state: | 182 // Header block streaming state: |
171 SpdyStreamId header_stream_id_; | 183 SpdyStreamId header_stream_id_; |
172 SpdyStreamId promised_stream_id_; | 184 SpdyStreamId promised_stream_id_; |
173 | 185 |
174 // Headers from OnSyn, OnSynReply, OnHeaders and OnPushPromise for | 186 // Headers from OnSyn, OnSynReply, OnHeaders and OnPushPromise for |
175 // verification. | 187 // verification. |
176 SpdyHeaderBlock headers_; | 188 SpdyHeaderBlock headers_; |
177 | 189 |
178 // OnGoAway parameters. | 190 // OnGoAway parameters. |
179 SpdyStreamId goaway_last_accepted_stream_id_; | 191 SpdyStreamId goaway_last_accepted_stream_id_; |
180 SpdyGoAwayStatus goaway_status_; | 192 SpdyGoAwayStatus goaway_status_; |
181 std::string goaway_debug_data_; | 193 std::string goaway_debug_data_; |
| 194 |
| 195 // OnAltSvc parameters. |
| 196 SpdyStreamId altsvc_stream_id_; |
| 197 std::string altsvc_origin_; |
| 198 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector_; |
182 }; | 199 }; |
183 | 200 |
184 } // namespace | 201 } // namespace |
185 | 202 |
186 class BufferedSpdyFramerTest | 203 class BufferedSpdyFramerTest |
187 : public PlatformTest, | 204 : public PlatformTest, |
188 public ::testing::WithParamInterface<NextProto> { | 205 public ::testing::WithParamInterface<NextProto> { |
189 protected: | 206 protected: |
190 SpdyMajorVersion spdy_version() { | 207 SpdyMajorVersion spdy_version() { |
191 return NextProtoToSpdyMajorVersion(GetParam()); | 208 return NextProtoToSpdyMajorVersion(GetParam()); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 visitor.SimulateInFramer( | 348 visitor.SimulateInFramer( |
332 reinterpret_cast<unsigned char*>(goaway_frame.get()->data()), | 349 reinterpret_cast<unsigned char*>(goaway_frame.get()->data()), |
333 goaway_frame.get()->size()); | 350 goaway_frame.get()->size()); |
334 EXPECT_EQ(0, visitor.error_count_); | 351 EXPECT_EQ(0, visitor.error_count_); |
335 EXPECT_EQ(1, visitor.goaway_count_); | 352 EXPECT_EQ(1, visitor.goaway_count_); |
336 EXPECT_EQ(2u, visitor.goaway_last_accepted_stream_id_); | 353 EXPECT_EQ(2u, visitor.goaway_last_accepted_stream_id_); |
337 EXPECT_EQ(GOAWAY_FRAME_SIZE_ERROR, visitor.goaway_status_); | 354 EXPECT_EQ(GOAWAY_FRAME_SIZE_ERROR, visitor.goaway_status_); |
338 EXPECT_EQ("foo", visitor.goaway_debug_data_); | 355 EXPECT_EQ("foo", visitor.goaway_debug_data_); |
339 } | 356 } |
340 | 357 |
| 358 TEST_P(BufferedSpdyFramerTest, OnAltSvc) { |
| 359 if (spdy_version() < HTTP2) |
| 360 return; |
| 361 |
| 362 const SpdyStreamId altsvc_stream_id(1); |
| 363 const char altsvc_origin[] = "https://www.example.org"; |
| 364 SpdyAltSvcIR altsvc_ir(altsvc_stream_id); |
| 365 SpdyAltSvcWireFormat::AlternativeService alternative_service( |
| 366 "quic", "alternative.example.org", 443, 86400, |
| 367 SpdyAltSvcWireFormat::VersionVector()); |
| 368 altsvc_ir.add_altsvc(alternative_service); |
| 369 altsvc_ir.set_origin(altsvc_origin); |
| 370 BufferedSpdyFramer framer(spdy_version()); |
| 371 SpdySerializedFrame altsvc_frame(framer.SerializeFrame(altsvc_ir)); |
| 372 |
| 373 TestBufferedSpdyVisitor visitor(spdy_version()); |
| 374 visitor.SimulateInFramer( |
| 375 reinterpret_cast<unsigned char*>(altsvc_frame.data()), |
| 376 altsvc_frame.size()); |
| 377 EXPECT_EQ(0, visitor.error_count_); |
| 378 EXPECT_EQ(1, visitor.altsvc_count_); |
| 379 EXPECT_EQ(altsvc_stream_id, visitor.altsvc_stream_id_); |
| 380 EXPECT_EQ(altsvc_origin, visitor.altsvc_origin_); |
| 381 ASSERT_EQ(1u, visitor.altsvc_vector_.size()); |
| 382 EXPECT_EQ(alternative_service, visitor.altsvc_vector_[0]); |
| 383 } |
| 384 |
341 } // namespace net | 385 } // namespace net |
OLD | NEW |