forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspdy_framer_test.cc
5813 lines (5123 loc) · 200 KB
/
spdy_framer_test.cc
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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/spdy/spdy_framer.h"
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/string_number_conversions.h"
#include "net/quic/quic_flags.h"
#include "net/spdy/hpack/hpack_constants.h"
#include "net/spdy/mock_spdy_framer_visitor.h"
#include "net/spdy/spdy_frame_builder.h"
#include "net/spdy/spdy_frame_reader.h"
#include "net/spdy/spdy_protocol.h"
#include "net/spdy/spdy_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/platform_test.h"
using base::StringPiece;
using std::string;
using testing::_;
namespace net {
namespace test {
static const size_t kMaxDecompressedSize = 1024;
class MockDebugVisitor : public SpdyFramerDebugVisitorInterface {
public:
MOCK_METHOD4(OnSendCompressedFrame, void(SpdyStreamId stream_id,
SpdyFrameType type,
size_t payload_len,
size_t frame_len));
MOCK_METHOD3(OnReceiveCompressedFrame, void(SpdyStreamId stream_id,
SpdyFrameType type,
size_t frame_len));
};
class SpdyFramerTestUtil {
public:
// Decompress a single frame using the decompression context held by
// the SpdyFramer. The implemention is meant for use only in tests
// and will CHECK fail if the input is anything other than a single,
// well-formed compressed frame.
//
// Returns a new decompressed SpdySerializedFrame.
template <class SpdyFrameType>
static SpdySerializedFrame DecompressFrame(SpdyFramer* framer,
const SpdyFrameType& frame) {
DecompressionVisitor visitor(framer->protocol_version());
framer->set_visitor(&visitor);
CHECK_EQ(frame.size(), framer->ProcessInput(frame.data(), frame.size()));
CHECK_EQ(SpdyFramer::SPDY_READY_FOR_FRAME, framer->state());
framer->set_visitor(NULL);
char* buffer = visitor.ReleaseBuffer();
CHECK(buffer != NULL);
SpdySerializedFrame decompressed_frame(buffer, visitor.size(), true);
SetFrameLength(&decompressed_frame,
visitor.size() - framer->GetControlFrameHeaderSize(),
framer->protocol_version());
return decompressed_frame;
}
class DecompressionVisitor : public SpdyFramerVisitorInterface {
public:
explicit DecompressionVisitor(SpdyMajorVersion version)
: version_(version), size_(0), finished_(false) {}
void ResetBuffer() {
CHECK(buffer_.get() == NULL);
CHECK_EQ(0u, size_);
CHECK(!finished_);
buffer_.reset(new char[kMaxDecompressedSize]);
}
void OnError(SpdyFramer* framer) override { LOG(FATAL); }
void OnDataFrameHeader(SpdyStreamId stream_id,
size_t length,
bool fin) override {
LOG(FATAL) << "Unexpected data frame header";
}
void OnStreamFrameData(SpdyStreamId stream_id,
const char* data,
size_t len,
bool fin) override {
LOG(FATAL);
}
void OnStreamEnd(SpdyStreamId stream_id) override { LOG(FATAL); }
void OnStreamPadding(SpdyStreamId stream_id, size_t len) override {
LOG(FATAL);
}
SpdyHeadersHandlerInterface* OnHeaderFrameStart(
SpdyStreamId stream_id) override {
LOG(FATAL);
return nullptr;
}
void OnHeaderFrameEnd(SpdyStreamId stream_id, bool end_headers) override {
LOG(FATAL);
}
bool OnControlFrameHeaderData(SpdyStreamId stream_id,
const char* header_data,
size_t len) override {
CHECK(buffer_.get() != NULL);
CHECK_GE(kMaxDecompressedSize, size_ + len);
CHECK(!finished_);
if (len != 0) {
memcpy(buffer_.get() + size_, header_data, len);
size_ += len;
} else {
// Done.
finished_ = true;
}
return true;
}
void OnSynStream(SpdyStreamId stream_id,
SpdyStreamId associated_stream_id,
SpdyPriority priority,
bool fin,
bool unidirectional) override {
SpdyFramer framer(version_);
framer.set_enable_compression(false);
SpdySynStreamIR syn_stream(stream_id);
syn_stream.set_associated_to_stream_id(associated_stream_id);
syn_stream.set_priority(priority);
syn_stream.set_fin(fin);
syn_stream.set_unidirectional(unidirectional);
SpdySerializedFrame frame(framer.SerializeSynStream(syn_stream));
ResetBuffer();
memcpy(buffer_.get(), frame.data(), framer.GetSynStreamMinimumSize());
size_ += framer.GetSynStreamMinimumSize();
}
void OnSynReply(SpdyStreamId stream_id, bool fin) override {
SpdyFramer framer(version_);
framer.set_enable_compression(false);
SpdyHeadersIR headers(stream_id);
headers.set_fin(fin);
SpdySerializedFrame frame(framer.SerializeHeaders(headers));
ResetBuffer();
memcpy(buffer_.get(), frame.data(), framer.GetHeadersMinimumSize());
size_ += framer.GetSynStreamMinimumSize();
}
void OnRstStream(SpdyStreamId stream_id,
SpdyRstStreamStatus status) override {
LOG(FATAL);
}
void OnSetting(SpdySettingsIds id, uint8_t flags, uint32_t value) override {
LOG(FATAL);
}
void OnPing(SpdyPingId unique_id, bool is_ack) override { LOG(FATAL); }
void OnSettingsEnd() override { LOG(FATAL); }
void OnGoAway(SpdyStreamId last_accepted_stream_id,
SpdyGoAwayStatus status) override {
LOG(FATAL);
}
void OnHeaders(SpdyStreamId stream_id,
bool has_priority,
SpdyPriority priority,
SpdyStreamId parent_stream_id,
bool exclusive,
bool fin,
bool end) override {
SpdyFramer framer(version_);
framer.set_enable_compression(false);
SpdyHeadersIR headers(stream_id);
headers.set_has_priority(has_priority);
headers.set_priority(priority);
headers.set_parent_stream_id(parent_stream_id);
headers.set_exclusive(exclusive);
headers.set_fin(fin);
SpdySerializedFrame frame(framer.SerializeHeaders(headers));
ResetBuffer();
memcpy(buffer_.get(), frame.data(), framer.GetHeadersMinimumSize());
size_ += framer.GetHeadersMinimumSize();
}
void OnWindowUpdate(SpdyStreamId stream_id,
int delta_window_size) override {
LOG(FATAL);
}
void OnPushPromise(SpdyStreamId stream_id,
SpdyStreamId promised_stream_id,
bool end) override {
SpdyFramer framer(version_);
framer.set_enable_compression(false);
SpdyPushPromiseIR push_promise(stream_id, promised_stream_id);
SpdySerializedFrame frame(framer.SerializePushPromise(push_promise));
ResetBuffer();
memcpy(buffer_.get(), frame.data(), framer.GetPushPromiseMinimumSize());
size_ += framer.GetPushPromiseMinimumSize();
}
void OnContinuation(SpdyStreamId stream_id, bool end) override {
LOG(FATAL);
}
void OnPriority(SpdyStreamId stream_id,
SpdyStreamId parent_stream_id,
uint8_t weight,
bool exclusive) override {
// Do nothing.
}
bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) override {
LOG(FATAL);
return false;
}
char* ReleaseBuffer() {
CHECK(finished_);
return buffer_.release();
}
size_t size() const {
CHECK(finished_);
return size_;
}
private:
SpdyMajorVersion version_;
std::unique_ptr<char[]> buffer_;
size_t size_;
bool finished_;
DISALLOW_COPY_AND_ASSIGN(DecompressionVisitor);
};
private:
DISALLOW_COPY_AND_ASSIGN(SpdyFramerTestUtil);
};
class TestSpdyVisitor : public SpdyFramerVisitorInterface,
public SpdyFramerDebugVisitorInterface {
public:
// This is larger than our max frame size because header blocks that
// are too long can spill over into CONTINUATION frames.
static const size_t kDefaultHeaderBufferSize = 16 * 1024 * 1024;
explicit TestSpdyVisitor(SpdyMajorVersion version)
: framer_(version),
use_compression_(false),
error_count_(0),
syn_frame_count_(0),
syn_reply_frame_count_(0),
headers_frame_count_(0),
push_promise_frame_count_(0),
goaway_count_(0),
setting_count_(0),
settings_ack_sent_(0),
settings_ack_received_(0),
continuation_count_(0),
altsvc_count_(0),
priority_count_(0),
test_altsvc_ir_(0),
on_unknown_frame_result_(false),
last_window_update_stream_(0),
last_window_update_delta_(0),
last_push_promise_stream_(0),
last_push_promise_promised_stream_(0),
data_bytes_(0),
fin_frame_count_(0),
fin_opaque_data_(),
fin_flag_count_(0),
end_of_stream_count_(0),
control_frame_header_data_count_(0),
zero_length_control_frame_header_data_count_(0),
data_frame_count_(0),
last_payload_len_(0),
last_frame_len_(0),
header_buffer_(new char[kDefaultHeaderBufferSize]),
header_buffer_length_(0),
header_buffer_size_(kDefaultHeaderBufferSize),
header_stream_id_(static_cast<SpdyStreamId>(-1)),
header_control_type_(DATA),
header_buffer_valid_(false) {}
void OnError(SpdyFramer* f) override {
VLOG(1) << "SpdyFramer Error: "
<< SpdyFramer::ErrorCodeToString(f->error_code());
++error_count_;
}
void OnDataFrameHeader(SpdyStreamId stream_id,
size_t length,
bool fin) override {
++data_frame_count_;
header_stream_id_ = stream_id;
}
void OnStreamFrameData(SpdyStreamId stream_id,
const char* data,
size_t len,
bool fin) override {
VLOG(1) << "OnStreamFrameData(" << stream_id << ", data, " << len << ", "
<< fin << ") data:\n"
<< base::HexEncode(data, len);
EXPECT_EQ(header_stream_id_, stream_id);
data_bytes_ += len;
}
void OnStreamEnd(SpdyStreamId stream_id) override {
VLOG(1) << "OnStreamEnd(" << stream_id << ")";
EXPECT_EQ(header_stream_id_, stream_id);
++end_of_stream_count_;
}
void OnStreamPadding(SpdyStreamId stream_id, size_t len) override {
EXPECT_EQ(header_stream_id_, stream_id);
data_bytes_ += len;
VLOG(1) << "OnStreamPadding(" << stream_id << ", " << len << ")\n";
}
SpdyHeadersHandlerInterface* OnHeaderFrameStart(
SpdyStreamId stream_id) override {
LOG(FATAL);
return nullptr;
}
void OnHeaderFrameEnd(SpdyStreamId stream_id, bool end_headers) override {
LOG(FATAL);
}
bool OnControlFrameHeaderData(SpdyStreamId stream_id,
const char* header_data,
size_t len) override {
++control_frame_header_data_count_;
CHECK_EQ(header_stream_id_, stream_id);
if (len == 0) {
++zero_length_control_frame_header_data_count_;
// Indicates end-of-header-block.
headers_.clear();
CHECK(header_buffer_valid_);
return framer_.ParseHeaderBlockInBuffer(header_buffer_.get(),
header_buffer_length_, &headers_);
}
const size_t available = header_buffer_size_ - header_buffer_length_;
if (len > available) {
header_buffer_valid_ = false;
return false;
}
memcpy(header_buffer_.get() + header_buffer_length_, header_data, len);
header_buffer_length_ += len;
return true;
}
void OnSynStream(SpdyStreamId stream_id,
SpdyStreamId associated_stream_id,
SpdyPriority priority,
bool fin,
bool unidirectional) override {
++syn_frame_count_;
if (framer_.protocol_version() == SPDY3) {
InitHeaderStreaming(SYN_STREAM, stream_id);
} else {
InitHeaderStreaming(HEADERS, stream_id);
}
if (fin) {
++fin_flag_count_;
}
}
void OnSynReply(SpdyStreamId stream_id, bool fin) override {
++syn_reply_frame_count_;
if (framer_.protocol_version() == SPDY3) {
InitHeaderStreaming(SYN_REPLY, stream_id);
} else {
InitHeaderStreaming(HEADERS, stream_id);
}
if (fin) {
++fin_flag_count_;
}
}
void OnRstStream(SpdyStreamId stream_id,
SpdyRstStreamStatus status) override {
++fin_frame_count_;
}
bool OnRstStreamFrameData(const char* rst_stream_data, size_t len) override {
if ((rst_stream_data != NULL) && (len > 0)) {
fin_opaque_data_ += string(rst_stream_data, len);
}
return true;
}
void OnSetting(SpdySettingsIds id, uint8_t flags, uint32_t value) override {
++setting_count_;
}
void OnSettingsAck() override {
DCHECK_EQ(HTTP2, framer_.protocol_version());
++settings_ack_received_;
}
void OnSettingsEnd() override {
if (framer_.protocol_version() == HTTP2) {
++settings_ack_sent_;
}
}
void OnPing(SpdyPingId unique_id, bool is_ack) override { DLOG(FATAL); }
void OnGoAway(SpdyStreamId last_accepted_stream_id,
SpdyGoAwayStatus status) override {
++goaway_count_;
}
void OnHeaders(SpdyStreamId stream_id,
bool has_priority,
SpdyPriority priority,
SpdyStreamId parent_stream_id,
bool exclusive,
bool fin,
bool end) override {
++headers_frame_count_;
InitHeaderStreaming(HEADERS, stream_id);
if (fin) {
++fin_flag_count_;
}
header_has_priority_ = has_priority;
header_parent_stream_id_ = parent_stream_id;
header_exclusive_ = exclusive;
}
void OnWindowUpdate(SpdyStreamId stream_id, int delta_window_size) override {
last_window_update_stream_ = stream_id;
last_window_update_delta_ = delta_window_size;
}
void OnPushPromise(SpdyStreamId stream_id,
SpdyStreamId promised_stream_id,
bool end) override {
++push_promise_frame_count_;
InitHeaderStreaming(PUSH_PROMISE, stream_id);
last_push_promise_stream_ = stream_id;
last_push_promise_promised_stream_ = promised_stream_id;
}
void OnContinuation(SpdyStreamId stream_id, bool end) override {
++continuation_count_;
}
void OnAltSvc(SpdyStreamId stream_id,
StringPiece origin,
const SpdyAltSvcWireFormat::AlternativeServiceVector&
altsvc_vector) override {
test_altsvc_ir_.set_stream_id(stream_id);
if (origin.length() > 0) {
test_altsvc_ir_.set_origin(origin.as_string());
}
for (const SpdyAltSvcWireFormat::AlternativeService& altsvc :
altsvc_vector) {
test_altsvc_ir_.add_altsvc(altsvc);
}
++altsvc_count_;
}
void OnPriority(SpdyStreamId stream_id,
SpdyStreamId parent_stream_id,
uint8_t weight,
bool exclusive) override {
++priority_count_;
}
bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) override {
VLOG(1) << "OnUnknownFrame(" << stream_id << ", " << frame_type << ")";
return on_unknown_frame_result_;
}
void OnSendCompressedFrame(SpdyStreamId stream_id,
SpdyFrameType type,
size_t payload_len,
size_t frame_len) override {
last_payload_len_ = payload_len;
last_frame_len_ = frame_len;
}
void OnReceiveCompressedFrame(SpdyStreamId stream_id,
SpdyFrameType type,
size_t frame_len) override {
last_frame_len_ = frame_len;
}
// Convenience function which runs a framer simulation with particular input.
void SimulateInFramer(const unsigned char* input, size_t size) {
framer_.set_enable_compression(use_compression_);
framer_.set_visitor(this);
size_t input_remaining = size;
const char* input_ptr = reinterpret_cast<const char*>(input);
while (input_remaining > 0 &&
framer_.error_code() == SpdyFramer::SPDY_NO_ERROR) {
// To make the tests more interesting, we feed random (and small) chunks
// into the framer. This simulates getting strange-sized reads from
// the socket.
const size_t kMaxReadSize = 32;
size_t bytes_read =
(rand() % std::min(input_remaining, kMaxReadSize)) + 1;
size_t bytes_processed = framer_.ProcessInput(input_ptr, bytes_read);
input_remaining -= bytes_processed;
input_ptr += bytes_processed;
}
}
void InitHeaderStreaming(SpdyFrameType header_control_type,
SpdyStreamId stream_id) {
if (!SpdyConstants::IsValidFrameType(framer_.protocol_version(),
SpdyConstants::SerializeFrameType(framer_.protocol_version(),
header_control_type))) {
DLOG(FATAL) << "Attempted to init header streaming with "
<< "invalid control frame type: "
<< header_control_type;
}
memset(header_buffer_.get(), 0, header_buffer_size_);
header_buffer_length_ = 0;
header_stream_id_ = stream_id;
header_control_type_ = header_control_type;
header_buffer_valid_ = true;
DCHECK_NE(header_stream_id_, SpdyFramer::kInvalidStream);
}
// Override the default buffer size (16K). Call before using the framer!
void set_header_buffer_size(size_t header_buffer_size) {
header_buffer_size_ = header_buffer_size;
header_buffer_.reset(new char[header_buffer_size]);
}
// Largest control frame that the SPDY implementation sends, including the
// size of the header.
static size_t sent_control_frame_max_size() {
return SpdyFramer::kMaxControlFrameSize;
}
// Largest control frame that the SPDY implementation is willing to receive,
// excluding the size of the header.
static size_t received_control_frame_max_size() {
return kSpdyInitialFrameSizeLimit;
}
static size_t header_data_chunk_max_size() {
return SpdyFramer::kHeaderDataChunkMaxSize;
}
SpdyFramer framer_;
bool use_compression_;
// Counters from the visitor callbacks.
int error_count_;
int syn_frame_count_;
int syn_reply_frame_count_;
int headers_frame_count_;
int push_promise_frame_count_;
int goaway_count_;
int setting_count_;
int settings_ack_sent_;
int settings_ack_received_;
int continuation_count_;
int altsvc_count_;
int priority_count_;
SpdyAltSvcIR test_altsvc_ir_;
bool on_unknown_frame_result_;
SpdyStreamId last_window_update_stream_;
int last_window_update_delta_;
SpdyStreamId last_push_promise_stream_;
SpdyStreamId last_push_promise_promised_stream_;
int data_bytes_;
int fin_frame_count_; // The count of RST_STREAM type frames received.
string fin_opaque_data_;
int fin_flag_count_; // The count of frames with the FIN flag set.
int end_of_stream_count_; // The count of zero-length data frames.
int control_frame_header_data_count_; // The count of chunks received.
// The count of zero-length control frame header data chunks received.
int zero_length_control_frame_header_data_count_;
int data_frame_count_;
size_t last_payload_len_;
size_t last_frame_len_;
// Header block streaming state:
std::unique_ptr<char[]> header_buffer_;
size_t header_buffer_length_;
size_t header_buffer_size_;
SpdyStreamId header_stream_id_;
SpdyFrameType header_control_type_;
bool header_buffer_valid_;
SpdyHeaderBlock headers_;
bool header_has_priority_;
SpdyStreamId header_parent_stream_id_;
bool header_exclusive_;
};
class SpdyFramerPeer {
public:
static size_t ControlFrameBufferSize() {
return SpdyFramer::kControlFrameBufferSize;
}
static size_t GetNumberRequiredContinuationFrames(SpdyFramer* framer,
size_t size) {
return framer->GetNumberRequiredContinuationFrames(size);
}
};
// Retrieves serialized headers from a HEADERS or SYN_STREAM frame.
StringPiece GetSerializedHeaders(const SpdySerializedFrame& frame,
const SpdyFramer& framer) {
SpdyFrameReader reader(frame.data(), frame.size());
if (framer.protocol_version() == SPDY3) {
reader.Seek(2); // Seek past the frame length.
} else {
reader.Seek(3); // Seek past the frame length.
}
SpdyFrameType frame_type;
if (framer.protocol_version() == SPDY3) {
uint16_t serialized_type;
reader.ReadUInt16(&serialized_type);
frame_type = SpdyConstants::ParseFrameType(framer.protocol_version(),
serialized_type);
DCHECK(frame_type == HEADERS || frame_type == SYN_STREAM) << frame_type;
} else {
uint8_t serialized_type;
reader.ReadUInt8(&serialized_type);
frame_type = SpdyConstants::ParseFrameType(framer.protocol_version(),
serialized_type);
DCHECK_EQ(HEADERS, frame_type);
uint8_t flags;
reader.ReadUInt8(&flags);
if (flags & HEADERS_FLAG_PRIORITY) {
frame_type = SYN_STREAM;
}
}
if (frame_type == SYN_STREAM) {
return StringPiece(frame.data() + framer.GetSynStreamMinimumSize(),
frame.size() - framer.GetSynStreamMinimumSize());
} else {
return StringPiece(frame.data() + framer.GetHeadersMinimumSize(),
frame.size() - framer.GetHeadersMinimumSize());
}
}
class SpdyFramerTest : public ::testing::TestWithParam<SpdyMajorVersion> {
protected:
void SetUp() override {
spdy_version_ = GetParam();
}
void CompareFrame(const string& description,
const SpdySerializedFrame& actual_frame,
const unsigned char* expected,
const int expected_len) {
const unsigned char* actual =
reinterpret_cast<const unsigned char*>(actual_frame.data());
CompareCharArraysWithHexError(
description, actual, actual_frame.size(), expected, expected_len);
}
void CompareFrames(const string& description,
const SpdySerializedFrame& expected_frame,
const SpdySerializedFrame& actual_frame) {
CompareCharArraysWithHexError(
description,
reinterpret_cast<const unsigned char*>(expected_frame.data()),
expected_frame.size(),
reinterpret_cast<const unsigned char*>(actual_frame.data()),
actual_frame.size());
}
bool IsSpdy3() { return spdy_version_ == SPDY3; }
bool IsHttp2() { return spdy_version_ == HTTP2; }
// Version of SPDY protocol to be used.
SpdyMajorVersion spdy_version_;
};
// All tests are run with SPDY/3 and HTTP/2.
INSTANTIATE_TEST_CASE_P(SpdyFramerTests,
SpdyFramerTest,
::testing::Values(SPDY3, HTTP2));
// Test that we ignore cookie where both name and value are empty.
TEST_P(SpdyFramerTest, HeaderBlockWithEmptyCookie) {
if (!IsSpdy3()) {
// Not implemented for hpack.
return;
}
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(true);
SpdyHeadersIR headers(1);
headers.set_priority(1);
headers.SetHeader("cookie",
"=; key=value; ; = ; foo; bar=; ; = ; k2=v2 ; =");
SpdySerializedFrame frame(framer.SerializeHeaders(headers));
TestSpdyVisitor visitor(spdy_version_);
visitor.use_compression_ = true;
visitor.SimulateInFramer(reinterpret_cast<unsigned char*>(frame.data()),
frame.size());
EXPECT_EQ(1, visitor.zero_length_control_frame_header_data_count_);
EXPECT_NE(headers.header_block(), visitor.headers_);
EXPECT_EQ(1u, visitor.headers_.size());
EXPECT_EQ("key=value; foo; bar=; k2=v2 ", visitor.headers_["cookie"]);
}
// Test that we can encode and decode a SpdyHeaderBlock in serialized form.
TEST_P(SpdyFramerTest, HeaderBlockInBuffer) {
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(false);
// Encode the header block into a Headers frame.
SpdyHeadersIR headers(1);
headers.set_priority(1);
headers.SetHeader("alpha", "beta");
headers.SetHeader("gamma", "charlie");
headers.SetHeader("cookie", "key1=value1; key2=value2");
SpdySerializedFrame frame(framer.SerializeHeaders(headers));
TestSpdyVisitor visitor(spdy_version_);
visitor.use_compression_ = false;
visitor.SimulateInFramer(reinterpret_cast<unsigned char*>(frame.data()),
frame.size());
EXPECT_EQ(1, visitor.zero_length_control_frame_header_data_count_);
EXPECT_EQ(headers.header_block(), visitor.headers_);
}
// Test that if there's not a full frame, we fail to parse it.
TEST_P(SpdyFramerTest, UndersizedHeaderBlockInBuffer) {
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(false);
// Encode the header block into a Headers frame.
SpdyHeadersIR headers(1);
headers.set_priority(1);
headers.SetHeader("alpha", "beta");
headers.SetHeader("gamma", "charlie");
SpdySerializedFrame frame(framer.SerializeHeaders(headers));
TestSpdyVisitor visitor(spdy_version_);
visitor.use_compression_ = false;
visitor.SimulateInFramer(reinterpret_cast<unsigned char*>(frame.data()),
frame.size() - 2);
EXPECT_EQ(0, visitor.zero_length_control_frame_header_data_count_);
EXPECT_EQ(0u, visitor.headers_.size());
}
// Test that we can encode and decode stream dependency values in a header
// frame.
TEST_P(SpdyFramerTest, HeaderStreamDependencyValues) {
if (!IsHttp2()) {
return;
}
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(false);
const SpdyStreamId parent_stream_id_test_array[] = {0, 3};
for (SpdyStreamId parent_stream_id : parent_stream_id_test_array) {
const bool exclusive_test_array[] = {true, false};
for (bool exclusive : exclusive_test_array) {
SpdyHeadersIR headers(1);
headers.set_has_priority(true);
headers.set_parent_stream_id(parent_stream_id);
headers.set_exclusive(exclusive);
SpdySerializedFrame frame(framer.SerializeHeaders(headers));
TestSpdyVisitor visitor(spdy_version_);
visitor.use_compression_ = false;
visitor.SimulateInFramer(reinterpret_cast<unsigned char*>(frame.data()),
frame.size());
EXPECT_TRUE(visitor.header_has_priority_);
EXPECT_EQ(parent_stream_id, visitor.header_parent_stream_id_);
EXPECT_EQ(exclusive, visitor.header_exclusive_);
}
}
}
// Test that if we receive a DATA frame with padding length larger than the
// payload length, we set an error of SPDY_INVALID_PADDING
TEST_P(SpdyFramerTest, OversizedDataPaddingError) {
if (!IsHttp2()) {
return;
}
testing::StrictMock<test::MockSpdyFramerVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
// DATA frame with invalid padding length.
// |kH2FrameData| has to be |unsigned char|, because Chromium on Windows uses
// MSVC, where |char| is signed by default, which would not compile because of
// the element exceeding 127.
unsigned char kH2FrameData[] = {
0x00, 0x00, 0x05, // Length
0x00, // Type (DATA)
0x09, // Flags (PADDED, END_STREAM)
0x00, 0x00, 0x00, 0x01, // Stream id
0xFF, // Padding length (here larger than length)
0x00, 0x00, 0x00, 0x00, // Arbitrary data payload
};
SpdySerializedFrame frame(reinterpret_cast<char*>(kH2FrameData),
sizeof(kH2FrameData), false);
{
testing::InSequence seq;
EXPECT_CALL(visitor, OnDataFrameHeader(1, 5, 1));
EXPECT_CALL(visitor, OnStreamPadding(1, 1));
EXPECT_CALL(visitor, OnError(testing::Eq(&framer)));
}
EXPECT_GT(frame.size(), framer.ProcessInput(frame.data(), frame.size()));
EXPECT_TRUE(framer.HasError());
EXPECT_EQ(SpdyFramer::SPDY_INVALID_PADDING, framer.error_code())
<< SpdyFramer::ErrorCodeToString(framer.error_code());
}
// Test that if we receive a DATA frame with padding length not larger than the
// payload length, we do not set an error of SPDY_INVALID_PADDING
TEST_P(SpdyFramerTest, CorrectlySizedDataPaddingNoError) {
if (!IsHttp2()) {
return;
}
testing::StrictMock<test::MockSpdyFramerVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
// DATA frame with valid Padding length
char kH2FrameData[] = {
0x00, 0x00, 0x05, // Length
0x00, // Type (DATA)
0x08, // Flags (PADDED)
0x00, 0x00, 0x00, 0x01, // Stream id
0x04, // Padding length (here one less than length)
0x00, 0x00, 0x00, 0x00, // Padding bytes
};
SpdySerializedFrame frame(kH2FrameData, sizeof(kH2FrameData), false);
{
testing::InSequence seq;
EXPECT_CALL(visitor, OnDataFrameHeader(1, 5, false));
EXPECT_CALL(visitor, OnStreamPadding(1, 1));
EXPECT_CALL(visitor, OnError(testing::Eq(&framer))).Times(0);
// Note that OnStreamFrameData(1, _, 1, false)) is never called
// since there is no data, only padding
EXPECT_CALL(visitor, OnStreamPadding(1, 4));
}
EXPECT_EQ(frame.size(), framer.ProcessInput(frame.data(), frame.size()));
EXPECT_FALSE(framer.HasError());
EXPECT_EQ(SpdyFramer::SPDY_NO_ERROR, framer.error_code())
<< SpdyFramer::ErrorCodeToString(framer.error_code());
}
// Test that if we receive a HEADERS frame with padding length larger than the
// payload length, we set an error of SPDY_INVALID_PADDING
TEST_P(SpdyFramerTest, OversizedHeadersPaddingError) {
if (!IsHttp2()) {
return;
}
testing::StrictMock<test::MockSpdyFramerVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
// HEADERS frame with invalid padding length.
// |kH2FrameData| has to be |unsigned char|, because Chromium on Windows uses
// MSVC, where |char| is signed by default, which would not compile because of
// the element exceeding 127.
unsigned char kH2FrameData[] = {
0x00, 0x00, 0x05, // Length
0x01, // Type (HEADERS)
0x08, // Flags (PADDED)
0x00, 0x00, 0x00, 0x01, // Stream id
0xFF, // Padding length (here larger than length)
0x00, 0x00, 0x00, 0x00, // Arbitrary data payload
};
SpdySerializedFrame frame(reinterpret_cast<char*>(kH2FrameData),
sizeof(kH2FrameData), false);
EXPECT_CALL(visitor, OnHeaders(1, false, 0, 0, false, false, false));
EXPECT_CALL(visitor, OnError(testing::Eq(&framer)));
EXPECT_EQ(frame.size(), framer.ProcessInput(frame.data(), frame.size()));
EXPECT_TRUE(framer.HasError());
EXPECT_EQ(SpdyFramer::SPDY_INVALID_PADDING, framer.error_code())
<< SpdyFramer::ErrorCodeToString(framer.error_code());
}
// Test that if we receive a HEADERS frame with padding length not larger
// than the payload length, we do not set an error of SPDY_INVALID_PADDING
TEST_P(SpdyFramerTest, CorrectlySizedHeadersPaddingNoError) {
if (!IsHttp2()) {
return;
}
testing::StrictMock<test::MockSpdyFramerVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
// HEADERS frame with invalid Padding length
char kH2FrameData[] = {
0x00, 0x00, 0x05, // Length
0x01, // Type (HEADERS)
0x08, // Flags (PADDED)
0x00, 0x00, 0x00, 0x01, // Stream id
0x04, // Padding length
0x00, 0x00, 0x00, 0x00, // Padding
};
SpdySerializedFrame frame(kH2FrameData, sizeof(kH2FrameData), false);
EXPECT_CALL(visitor, OnHeaders(1, false, 0, 0, false, false, false));
EXPECT_EQ(frame.size(), framer.ProcessInput(frame.data(), frame.size()));
EXPECT_FALSE(framer.HasError());
EXPECT_EQ(SpdyFramer::SPDY_NO_ERROR, framer.error_code())
<< SpdyFramer::ErrorCodeToString(framer.error_code());
}
// Test that if we receive a SYN_REPLY with stream ID zero, we signal an error
// (but don't crash).
TEST_P(SpdyFramerTest, SynReplyWithStreamIdZero) {
if (!IsSpdy3()) {
return;
}
testing::StrictMock<test::MockSpdyFramerVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
SpdySynReplyIR syn_reply(0);
syn_reply.SetHeader("alpha", "beta");
SpdySerializedFrame frame(framer.SerializeSynReply(syn_reply));
// We shouldn't have to read the whole frame before we signal an error.
EXPECT_CALL(visitor, OnError(testing::Eq(&framer)));
EXPECT_GT(frame.size(), framer.ProcessInput(frame.data(), frame.size()));
EXPECT_TRUE(framer.HasError());
EXPECT_EQ(SpdyFramer::SPDY_INVALID_CONTROL_FRAME, framer.error_code())
<< SpdyFramer::ErrorCodeToString(framer.error_code());
}
// Test that if we receive a DATA with stream ID zero, we signal an error
// (but don't crash).
TEST_P(SpdyFramerTest, DataWithStreamIdZero) {
if (!IsHttp2()) {
return;
}
testing::StrictMock<test::MockSpdyFramerVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
const char bytes[] = "hello";
SpdyDataIR data_ir(0, bytes);
SpdySerializedFrame frame(framer.SerializeData(data_ir));
// We shouldn't have to read the whole frame before we signal an error.
EXPECT_CALL(visitor, OnError(testing::Eq(&framer)));
EXPECT_GT(frame.size(), framer.ProcessInput(frame.data(), frame.size()));
EXPECT_TRUE(framer.HasError());
EXPECT_EQ(SpdyFramer::SPDY_INVALID_STREAM_ID, framer.error_code())
<< SpdyFramer::ErrorCodeToString(framer.error_code());
}
// Test that if we receive a HEADERS with stream ID zero, we signal an error
// (but don't crash).
TEST_P(SpdyFramerTest, HeadersWithStreamIdZero) {
testing::StrictMock<test::MockSpdyFramerVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
SpdyHeadersIR headers_ir(0);
headers_ir.SetHeader("alpha", "beta");