| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "net/spdy/spdy_test_util_common.h" | 10 #include "net/spdy/spdy_test_util_common.h" |
| 11 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 class TestBufferedSpdyVisitor : public BufferedSpdyFramerVisitorInterface { | 17 class TestBufferedSpdyVisitor : public BufferedSpdyFramerVisitorInterface { |
| 18 public: | 18 public: |
| 19 explicit TestBufferedSpdyVisitor() | 19 explicit TestBufferedSpdyVisitor() |
| 20 : buffered_spdy_framer_(HTTP2), | 20 : buffered_spdy_framer_(), |
| 21 error_count_(0), | 21 error_count_(0), |
| 22 setting_count_(0), | 22 setting_count_(0), |
| 23 syn_frame_count_(0), | |
| 24 syn_reply_frame_count_(0), | |
| 25 headers_frame_count_(0), | 23 headers_frame_count_(0), |
| 26 push_promise_frame_count_(0), | 24 push_promise_frame_count_(0), |
| 27 goaway_count_(0), | 25 goaway_count_(0), |
| 28 altsvc_count_(0), | 26 altsvc_count_(0), |
| 29 header_stream_id_(static_cast<SpdyStreamId>(-1)), | 27 header_stream_id_(static_cast<SpdyStreamId>(-1)), |
| 30 promised_stream_id_(static_cast<SpdyStreamId>(-1)) {} | 28 promised_stream_id_(static_cast<SpdyStreamId>(-1)) {} |
| 31 | 29 |
| 32 void OnError(SpdyFramer::SpdyError error_code) override { | 30 void OnError(SpdyFramer::SpdyError error_code) override { |
| 33 VLOG(1) << "SpdyFramer Error: " << error_code; | 31 VLOG(1) << "SpdyFramer Error: " << error_code; |
| 34 error_count_++; | 32 error_count_++; |
| 35 } | 33 } |
| 36 | 34 |
| 37 void OnStreamError(SpdyStreamId stream_id, | 35 void OnStreamError(SpdyStreamId stream_id, |
| 38 const std::string& description) override { | 36 const std::string& description) override { |
| 39 VLOG(1) << "SpdyFramer Error on stream: " << stream_id << " " | 37 VLOG(1) << "SpdyFramer Error on stream: " << stream_id << " " |
| 40 << description; | 38 << description; |
| 41 error_count_++; | 39 error_count_++; |
| 42 } | 40 } |
| 43 | 41 |
| 44 void OnSynStream(SpdyStreamId stream_id, | |
| 45 SpdyStreamId associated_stream_id, | |
| 46 SpdyPriority priority, | |
| 47 bool fin, | |
| 48 bool unidirectional, | |
| 49 const SpdyHeaderBlock& headers) override { | |
| 50 header_stream_id_ = stream_id; | |
| 51 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream); | |
| 52 syn_frame_count_++; | |
| 53 headers_ = headers.Clone(); | |
| 54 } | |
| 55 | |
| 56 void OnSynReply(SpdyStreamId stream_id, | |
| 57 bool fin, | |
| 58 const SpdyHeaderBlock& headers) override { | |
| 59 header_stream_id_ = stream_id; | |
| 60 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream); | |
| 61 syn_reply_frame_count_++; | |
| 62 headers_ = headers.Clone(); | |
| 63 } | |
| 64 | |
| 65 void OnHeaders(SpdyStreamId stream_id, | 42 void OnHeaders(SpdyStreamId stream_id, |
| 66 bool has_priority, | 43 bool has_priority, |
| 67 int weight, | 44 int weight, |
| 68 SpdyStreamId parent_stream_id, | 45 SpdyStreamId parent_stream_id, |
| 69 bool exclusive, | 46 bool exclusive, |
| 70 bool fin, | 47 bool fin, |
| 71 const SpdyHeaderBlock& headers) override { | 48 const SpdyHeaderBlock& headers) override { |
| 72 header_stream_id_ = stream_id; | 49 header_stream_id_ = stream_id; |
| 73 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream); | 50 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream); |
| 74 headers_frame_count_++; | 51 headers_frame_count_++; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 input_remaining -= bytes_processed; | 144 input_remaining -= bytes_processed; |
| 168 input_ptr += bytes_processed; | 145 input_ptr += bytes_processed; |
| 169 } | 146 } |
| 170 } | 147 } |
| 171 | 148 |
| 172 BufferedSpdyFramer buffered_spdy_framer_; | 149 BufferedSpdyFramer buffered_spdy_framer_; |
| 173 | 150 |
| 174 // Counters from the visitor callbacks. | 151 // Counters from the visitor callbacks. |
| 175 int error_count_; | 152 int error_count_; |
| 176 int setting_count_; | 153 int setting_count_; |
| 177 int syn_frame_count_; | |
| 178 int syn_reply_frame_count_; | |
| 179 int headers_frame_count_; | 154 int headers_frame_count_; |
| 180 int push_promise_frame_count_; | 155 int push_promise_frame_count_; |
| 181 int goaway_count_; | 156 int goaway_count_; |
| 182 int altsvc_count_; | 157 int altsvc_count_; |
| 183 | 158 |
| 184 // Header block streaming state: | 159 // Header block streaming state: |
| 185 SpdyStreamId header_stream_id_; | 160 SpdyStreamId header_stream_id_; |
| 186 SpdyStreamId promised_stream_id_; | 161 SpdyStreamId promised_stream_id_; |
| 187 | 162 |
| 188 // Headers from OnSyn, OnSynReply, OnHeaders and OnPushPromise for | 163 // Headers from OnHeaders and OnPushPromise for verification. |
| 189 // verification. | |
| 190 SpdyHeaderBlock headers_; | 164 SpdyHeaderBlock headers_; |
| 191 | 165 |
| 192 // OnGoAway parameters. | 166 // OnGoAway parameters. |
| 193 SpdyStreamId goaway_last_accepted_stream_id_; | 167 SpdyStreamId goaway_last_accepted_stream_id_; |
| 194 SpdyGoAwayStatus goaway_status_; | 168 SpdyGoAwayStatus goaway_status_; |
| 195 std::string goaway_debug_data_; | 169 std::string goaway_debug_data_; |
| 196 | 170 |
| 197 // OnAltSvc parameters. | 171 // OnAltSvc parameters. |
| 198 SpdyStreamId altsvc_stream_id_; | 172 SpdyStreamId altsvc_stream_id_; |
| 199 std::string altsvc_origin_; | 173 std::string altsvc_origin_; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 216 reinterpret_cast<unsigned char*>(control_frame.data()), | 190 reinterpret_cast<unsigned char*>(control_frame.data()), |
| 217 control_frame.size()); | 191 control_frame.size()); |
| 218 EXPECT_EQ(0, visitor.error_count_); | 192 EXPECT_EQ(0, visitor.error_count_); |
| 219 EXPECT_EQ(2, visitor.setting_count_); | 193 EXPECT_EQ(2, visitor.setting_count_); |
| 220 } | 194 } |
| 221 | 195 |
| 222 TEST_F(BufferedSpdyFramerTest, HeaderListTooLarge) { | 196 TEST_F(BufferedSpdyFramerTest, HeaderListTooLarge) { |
| 223 SpdyHeaderBlock headers; | 197 SpdyHeaderBlock headers; |
| 224 std::string long_header_value(256 * 1024, 'x'); | 198 std::string long_header_value(256 * 1024, 'x'); |
| 225 headers["foo"] = long_header_value; | 199 headers["foo"] = long_header_value; |
| 226 BufferedSpdyFramer framer(HTTP2); | 200 BufferedSpdyFramer framer; |
| 227 std::unique_ptr<SpdySerializedFrame> control_frame( | 201 std::unique_ptr<SpdySerializedFrame> control_frame( |
| 228 framer.CreateHeaders(1, // stream_id | 202 framer.CreateHeaders(1, // stream_id |
| 229 CONTROL_FLAG_NONE, | 203 CONTROL_FLAG_NONE, |
| 230 255, // weight | 204 255, // weight |
| 231 std::move(headers))); | 205 std::move(headers))); |
| 232 EXPECT_TRUE(control_frame); | 206 EXPECT_TRUE(control_frame); |
| 233 | 207 |
| 234 TestBufferedSpdyVisitor visitor; | 208 TestBufferedSpdyVisitor visitor; |
| 235 visitor.SimulateInFramer( | 209 visitor.SimulateInFramer( |
| 236 reinterpret_cast<unsigned char*>(control_frame.get()->data()), | 210 reinterpret_cast<unsigned char*>(control_frame.get()->data()), |
| 237 control_frame.get()->size()); | 211 control_frame.get()->size()); |
| 238 | 212 |
| 239 EXPECT_EQ(1, visitor.error_count_); | 213 EXPECT_EQ(1, visitor.error_count_); |
| 240 EXPECT_EQ(0, visitor.syn_frame_count_); | |
| 241 EXPECT_EQ(0, visitor.syn_reply_frame_count_); | |
| 242 EXPECT_EQ(0, visitor.headers_frame_count_); | 214 EXPECT_EQ(0, visitor.headers_frame_count_); |
| 243 EXPECT_EQ(0, visitor.push_promise_frame_count_); | 215 EXPECT_EQ(0, visitor.push_promise_frame_count_); |
| 244 EXPECT_EQ(SpdyHeaderBlock(), visitor.headers_); | 216 EXPECT_EQ(SpdyHeaderBlock(), visitor.headers_); |
| 245 } | 217 } |
| 246 | 218 |
| 247 TEST_F(BufferedSpdyFramerTest, ReadHeadersHeaderBlock) { | 219 TEST_F(BufferedSpdyFramerTest, ReadHeadersHeaderBlock) { |
| 248 SpdyHeaderBlock headers; | 220 SpdyHeaderBlock headers; |
| 249 headers["alpha"] = "beta"; | 221 headers["alpha"] = "beta"; |
| 250 headers["gamma"] = "delta"; | 222 headers["gamma"] = "delta"; |
| 251 BufferedSpdyFramer framer(HTTP2); | 223 BufferedSpdyFramer framer; |
| 252 std::unique_ptr<SpdySerializedFrame> control_frame( | 224 std::unique_ptr<SpdySerializedFrame> control_frame( |
| 253 framer.CreateHeaders(1, // stream_id | 225 framer.CreateHeaders(1, // stream_id |
| 254 CONTROL_FLAG_NONE, | 226 CONTROL_FLAG_NONE, |
| 255 255, // weight | 227 255, // weight |
| 256 headers.Clone())); | 228 headers.Clone())); |
| 257 EXPECT_TRUE(control_frame.get() != NULL); | 229 EXPECT_TRUE(control_frame.get() != NULL); |
| 258 | 230 |
| 259 TestBufferedSpdyVisitor visitor; | 231 TestBufferedSpdyVisitor visitor; |
| 260 visitor.SimulateInFramer( | 232 visitor.SimulateInFramer( |
| 261 reinterpret_cast<unsigned char*>(control_frame.get()->data()), | 233 reinterpret_cast<unsigned char*>(control_frame.get()->data()), |
| 262 control_frame.get()->size()); | 234 control_frame.get()->size()); |
| 263 EXPECT_EQ(0, visitor.error_count_); | 235 EXPECT_EQ(0, visitor.error_count_); |
| 264 EXPECT_EQ(0, visitor.syn_frame_count_); | |
| 265 EXPECT_EQ(0, visitor.syn_reply_frame_count_); | |
| 266 EXPECT_EQ(1, visitor.headers_frame_count_); | 236 EXPECT_EQ(1, visitor.headers_frame_count_); |
| 267 EXPECT_EQ(0, visitor.push_promise_frame_count_); | 237 EXPECT_EQ(0, visitor.push_promise_frame_count_); |
| 268 EXPECT_EQ(headers, visitor.headers_); | 238 EXPECT_EQ(headers, visitor.headers_); |
| 269 } | 239 } |
| 270 | 240 |
| 271 TEST_F(BufferedSpdyFramerTest, ReadPushPromiseHeaderBlock) { | 241 TEST_F(BufferedSpdyFramerTest, ReadPushPromiseHeaderBlock) { |
| 272 SpdyHeaderBlock headers; | 242 SpdyHeaderBlock headers; |
| 273 headers["alpha"] = "beta"; | 243 headers["alpha"] = "beta"; |
| 274 headers["gamma"] = "delta"; | 244 headers["gamma"] = "delta"; |
| 275 BufferedSpdyFramer framer(HTTP2); | 245 BufferedSpdyFramer framer; |
| 276 std::unique_ptr<SpdySerializedFrame> control_frame( | 246 std::unique_ptr<SpdySerializedFrame> control_frame( |
| 277 framer.CreatePushPromise(1, 2, headers.Clone())); | 247 framer.CreatePushPromise(1, 2, headers.Clone())); |
| 278 EXPECT_TRUE(control_frame.get() != NULL); | 248 EXPECT_TRUE(control_frame.get() != NULL); |
| 279 | 249 |
| 280 TestBufferedSpdyVisitor visitor; | 250 TestBufferedSpdyVisitor visitor; |
| 281 visitor.SimulateInFramer( | 251 visitor.SimulateInFramer( |
| 282 reinterpret_cast<unsigned char*>(control_frame.get()->data()), | 252 reinterpret_cast<unsigned char*>(control_frame.get()->data()), |
| 283 control_frame.get()->size()); | 253 control_frame.get()->size()); |
| 284 EXPECT_EQ(0, visitor.error_count_); | 254 EXPECT_EQ(0, visitor.error_count_); |
| 285 EXPECT_EQ(0, visitor.syn_frame_count_); | |
| 286 EXPECT_EQ(0, visitor.syn_reply_frame_count_); | |
| 287 EXPECT_EQ(0, visitor.headers_frame_count_); | 255 EXPECT_EQ(0, visitor.headers_frame_count_); |
| 288 EXPECT_EQ(1, visitor.push_promise_frame_count_); | 256 EXPECT_EQ(1, visitor.push_promise_frame_count_); |
| 289 EXPECT_EQ(headers, visitor.headers_); | 257 EXPECT_EQ(headers, visitor.headers_); |
| 290 EXPECT_EQ(1u, visitor.header_stream_id_); | 258 EXPECT_EQ(1u, visitor.header_stream_id_); |
| 291 EXPECT_EQ(2u, visitor.promised_stream_id_); | 259 EXPECT_EQ(2u, visitor.promised_stream_id_); |
| 292 } | 260 } |
| 293 | 261 |
| 294 TEST_F(BufferedSpdyFramerTest, GoAwayDebugData) { | 262 TEST_F(BufferedSpdyFramerTest, GoAwayDebugData) { |
| 295 BufferedSpdyFramer framer(HTTP2); | 263 BufferedSpdyFramer framer; |
| 296 std::unique_ptr<SpdySerializedFrame> goaway_frame( | 264 std::unique_ptr<SpdySerializedFrame> goaway_frame( |
| 297 framer.CreateGoAway(2u, GOAWAY_FRAME_SIZE_ERROR, "foo")); | 265 framer.CreateGoAway(2u, GOAWAY_FRAME_SIZE_ERROR, "foo")); |
| 298 | 266 |
| 299 TestBufferedSpdyVisitor visitor; | 267 TestBufferedSpdyVisitor visitor; |
| 300 visitor.SimulateInFramer( | 268 visitor.SimulateInFramer( |
| 301 reinterpret_cast<unsigned char*>(goaway_frame.get()->data()), | 269 reinterpret_cast<unsigned char*>(goaway_frame.get()->data()), |
| 302 goaway_frame.get()->size()); | 270 goaway_frame.get()->size()); |
| 303 EXPECT_EQ(0, visitor.error_count_); | 271 EXPECT_EQ(0, visitor.error_count_); |
| 304 EXPECT_EQ(1, visitor.goaway_count_); | 272 EXPECT_EQ(1, visitor.goaway_count_); |
| 305 EXPECT_EQ(2u, visitor.goaway_last_accepted_stream_id_); | 273 EXPECT_EQ(2u, visitor.goaway_last_accepted_stream_id_); |
| 306 EXPECT_EQ(GOAWAY_FRAME_SIZE_ERROR, visitor.goaway_status_); | 274 EXPECT_EQ(GOAWAY_FRAME_SIZE_ERROR, visitor.goaway_status_); |
| 307 EXPECT_EQ("foo", visitor.goaway_debug_data_); | 275 EXPECT_EQ("foo", visitor.goaway_debug_data_); |
| 308 } | 276 } |
| 309 | 277 |
| 310 TEST_F(BufferedSpdyFramerTest, OnAltSvc) { | 278 TEST_F(BufferedSpdyFramerTest, OnAltSvc) { |
| 311 const SpdyStreamId altsvc_stream_id(1); | 279 const SpdyStreamId altsvc_stream_id(1); |
| 312 const char altsvc_origin[] = "https://www.example.org"; | 280 const char altsvc_origin[] = "https://www.example.org"; |
| 313 SpdyAltSvcIR altsvc_ir(altsvc_stream_id); | 281 SpdyAltSvcIR altsvc_ir(altsvc_stream_id); |
| 314 SpdyAltSvcWireFormat::AlternativeService alternative_service( | 282 SpdyAltSvcWireFormat::AlternativeService alternative_service( |
| 315 "quic", "alternative.example.org", 443, 86400, | 283 "quic", "alternative.example.org", 443, 86400, |
| 316 SpdyAltSvcWireFormat::VersionVector()); | 284 SpdyAltSvcWireFormat::VersionVector()); |
| 317 altsvc_ir.add_altsvc(alternative_service); | 285 altsvc_ir.add_altsvc(alternative_service); |
| 318 altsvc_ir.set_origin(altsvc_origin); | 286 altsvc_ir.set_origin(altsvc_origin); |
| 319 BufferedSpdyFramer framer(HTTP2); | 287 BufferedSpdyFramer framer; |
| 320 SpdySerializedFrame altsvc_frame(framer.SerializeFrame(altsvc_ir)); | 288 SpdySerializedFrame altsvc_frame(framer.SerializeFrame(altsvc_ir)); |
| 321 | 289 |
| 322 TestBufferedSpdyVisitor visitor; | 290 TestBufferedSpdyVisitor visitor; |
| 323 visitor.SimulateInFramer( | 291 visitor.SimulateInFramer( |
| 324 reinterpret_cast<unsigned char*>(altsvc_frame.data()), | 292 reinterpret_cast<unsigned char*>(altsvc_frame.data()), |
| 325 altsvc_frame.size()); | 293 altsvc_frame.size()); |
| 326 EXPECT_EQ(0, visitor.error_count_); | 294 EXPECT_EQ(0, visitor.error_count_); |
| 327 EXPECT_EQ(1, visitor.altsvc_count_); | 295 EXPECT_EQ(1, visitor.altsvc_count_); |
| 328 EXPECT_EQ(altsvc_stream_id, visitor.altsvc_stream_id_); | 296 EXPECT_EQ(altsvc_stream_id, visitor.altsvc_stream_id_); |
| 329 EXPECT_EQ(altsvc_origin, visitor.altsvc_origin_); | 297 EXPECT_EQ(altsvc_origin, visitor.altsvc_origin_); |
| 330 ASSERT_EQ(1u, visitor.altsvc_vector_.size()); | 298 ASSERT_EQ(1u, visitor.altsvc_vector_.size()); |
| 331 EXPECT_EQ(alternative_service, visitor.altsvc_vector_[0]); | 299 EXPECT_EQ(alternative_service, visitor.altsvc_vector_[0]); |
| 332 } | 300 } |
| 333 | 301 |
| 334 } // namespace net | 302 } // namespace net |
| OLD | NEW |