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
|
// 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/buffered_spdy_framer.h"
#include "net/spdy/spdy_test_util_common.h"
#include "testing/platform_test.h"
namespace net {
namespace {
class TestBufferedSpdyVisitor : public BufferedSpdyFramerVisitorInterface {
public:
explicit TestBufferedSpdyVisitor(SpdyMajorVersion spdy_version)
: buffered_spdy_framer_(spdy_version, true),
error_count_(0),
setting_count_(0),
syn_frame_count_(0),
syn_reply_frame_count_(0),
headers_frame_count_(0),
push_promise_frame_count_(0),
header_stream_id_(static_cast<SpdyStreamId>(-1)),
promised_stream_id_(static_cast<SpdyStreamId>(-1)) {
}
void OnError(SpdyFramer::SpdyError error_code) override {
LOG(INFO) << "SpdyFramer Error: " << error_code;
error_count_++;
}
void OnStreamError(SpdyStreamId stream_id,
const std::string& description) override {
LOG(INFO) << "SpdyFramer Error on stream: " << stream_id << " "
<< description;
error_count_++;
}
void OnSynStream(SpdyStreamId stream_id,
SpdyStreamId associated_stream_id,
SpdyPriority priority,
bool fin,
bool unidirectional,
const SpdyHeaderBlock& headers) override {
header_stream_id_ = stream_id;
EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream);
syn_frame_count_++;
headers_ = headers;
}
void OnSynReply(SpdyStreamId stream_id,
bool fin,
const SpdyHeaderBlock& headers) override {
header_stream_id_ = stream_id;
EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream);
syn_reply_frame_count_++;
headers_ = headers;
}
void OnHeaders(SpdyStreamId stream_id,
bool has_priority,
SpdyPriority priority,
SpdyStreamId parent_stream_id,
bool exclusive,
bool fin,
const SpdyHeaderBlock& headers) override {
header_stream_id_ = stream_id;
EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream);
headers_frame_count_++;
headers_ = headers;
}
void OnDataFrameHeader(SpdyStreamId stream_id,
size_t length,
bool fin) override {
ADD_FAILURE() << "Unexpected OnDataFrameHeader call.";
}
void OnStreamFrameData(SpdyStreamId stream_id,
const char* data,
size_t len,
bool fin) override {
LOG(FATAL) << "Unexpected OnStreamFrameData call.";
}
void OnStreamPadding(SpdyStreamId stream_id, size_t len) override {
LOG(FATAL) << "Unexpected OnStreamPadding call.";
}
void OnSettings(bool clear_persisted) override {}
void OnSetting(SpdySettingsIds id, uint8 flags, uint32 value) override {
setting_count_++;
}
void OnPing(SpdyPingId unique_id, bool is_ack) override {}
void OnRstStream(SpdyStreamId stream_id,
SpdyRstStreamStatus status) override {}
void OnGoAway(SpdyStreamId last_accepted_stream_id,
SpdyGoAwayStatus status) override {}
bool OnCredentialFrameData(const char*, size_t) {
LOG(FATAL) << "Unexpected OnCredentialFrameData call.";
return false;
}
void OnDataFrameHeader(const SpdyFrame* frame) {
LOG(FATAL) << "Unexpected OnDataFrameHeader call.";
}
void OnRstStream(const SpdyFrame& frame) {}
void OnGoAway(const SpdyFrame& frame) {}
void OnPing(const SpdyFrame& frame) {}
void OnWindowUpdate(SpdyStreamId stream_id, int delta_window_size) override {}
void OnPushPromise(SpdyStreamId stream_id,
SpdyStreamId promised_stream_id,
const SpdyHeaderBlock& headers) override {
header_stream_id_ = stream_id;
EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream);
push_promise_frame_count_++;
promised_stream_id_ = promised_stream_id;
EXPECT_NE(promised_stream_id_, SpdyFramer::kInvalidStream);
headers_ = headers;
}
bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) override {
return true;
}
void OnCredential(const SpdyFrame& frame) {}
// Convenience function which runs a framer simulation with particular input.
void SimulateInFramer(const unsigned char* input, size_t size) {
buffered_spdy_framer_.set_visitor(this);
size_t input_remaining = size;
const char* input_ptr = reinterpret_cast<const char*>(input);
while (input_remaining > 0 &&
buffered_spdy_framer_.error_code() == SpdyFramer::SPDY_NO_ERROR) {
// To make the tests more interesting, we feed random (amd 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 =
buffered_spdy_framer_.ProcessInput(input_ptr, bytes_read);
input_remaining -= bytes_processed;
input_ptr += bytes_processed;
}
}
BufferedSpdyFramer buffered_spdy_framer_;
// Counters from the visitor callbacks.
int error_count_;
int setting_count_;
int syn_frame_count_;
int syn_reply_frame_count_;
int headers_frame_count_;
int push_promise_frame_count_;
// Header block streaming state:
SpdyStreamId header_stream_id_;
SpdyStreamId promised_stream_id_;
// Headers from OnSyn, OnSynReply, OnHeaders and OnPushPromise for
// verification.
SpdyHeaderBlock headers_;
};
} // namespace
class BufferedSpdyFramerTest
: public PlatformTest,
public ::testing::WithParamInterface<NextProto> {
protected:
// Returns true if the two header blocks have equivalent content.
bool CompareHeaderBlocks(const SpdyHeaderBlock* expected,
const SpdyHeaderBlock* actual) {
if (expected->size() != actual->size()) {
LOG(ERROR) << "Expected " << expected->size() << " headers; actually got "
<< actual->size() << ".";
return false;
}
for (SpdyHeaderBlock::const_iterator it = expected->begin();
it != expected->end();
++it) {
SpdyHeaderBlock::const_iterator it2 = actual->find(it->first);
if (it2 == actual->end()) {
LOG(ERROR) << "Expected header name '" << it->first << "'.";
return false;
}
if (it->second.compare(it2->second) != 0) {
LOG(ERROR) << "Expected header named '" << it->first
<< "' to have a value of '" << it->second
<< "'. The actual value received was '" << it2->second
<< "'.";
return false;
}
}
return true;
}
SpdyMajorVersion spdy_version() {
return NextProtoToSpdyMajorVersion(GetParam());
}
};
INSTANTIATE_TEST_CASE_P(NextProto,
BufferedSpdyFramerTest,
testing::Values(kProtoSPDY31,
kProtoHTTP2));
TEST_P(BufferedSpdyFramerTest, OnSetting) {
SpdyFramer framer(spdy_version());
SpdySettingsIR settings_ir;
settings_ir.AddSetting(SETTINGS_INITIAL_WINDOW_SIZE, false, false, 2);
settings_ir.AddSetting(SETTINGS_MAX_CONCURRENT_STREAMS, false, false, 3);
scoped_ptr<SpdyFrame> control_frame(framer.SerializeSettings(settings_ir));
TestBufferedSpdyVisitor visitor(spdy_version());
visitor.SimulateInFramer(
reinterpret_cast<unsigned char*>(control_frame->data()),
control_frame->size());
EXPECT_EQ(0, visitor.error_count_);
EXPECT_EQ(2, visitor.setting_count_);
}
TEST_P(BufferedSpdyFramerTest, ReadSynStreamHeaderBlock) {
if (spdy_version() > SPDY3) {
// SYN_STREAM not supported in SPDY>3.
return;
}
SpdyHeaderBlock headers;
headers["aa"] = "vv";
headers["bb"] = "ww";
BufferedSpdyFramer framer(spdy_version(), true);
scoped_ptr<SpdyFrame> control_frame(
framer.CreateSynStream(1, // stream_id
0, // associated_stream_id
1, // priority
CONTROL_FLAG_NONE,
&headers));
EXPECT_TRUE(control_frame.get() != NULL);
TestBufferedSpdyVisitor visitor(spdy_version());
visitor.SimulateInFramer(
reinterpret_cast<unsigned char*>(control_frame.get()->data()),
control_frame.get()->size());
EXPECT_EQ(0, visitor.error_count_);
EXPECT_EQ(1, visitor.syn_frame_count_);
EXPECT_EQ(0, visitor.syn_reply_frame_count_);
EXPECT_EQ(0, visitor.headers_frame_count_);
EXPECT_EQ(0, visitor.push_promise_frame_count_);
EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
}
TEST_P(BufferedSpdyFramerTest, ReadSynReplyHeaderBlock) {
if (spdy_version() > SPDY3) {
// SYN_REPLY not supported in SPDY>3.
return;
}
SpdyHeaderBlock headers;
headers["alpha"] = "beta";
headers["gamma"] = "delta";
BufferedSpdyFramer framer(spdy_version(), true);
scoped_ptr<SpdyFrame> control_frame(
framer.CreateSynReply(1, // stream_id
CONTROL_FLAG_NONE,
&headers));
EXPECT_TRUE(control_frame.get() != NULL);
TestBufferedSpdyVisitor visitor(spdy_version());
visitor.SimulateInFramer(
reinterpret_cast<unsigned char*>(control_frame.get()->data()),
control_frame.get()->size());
EXPECT_EQ(0, visitor.error_count_);
EXPECT_EQ(0, visitor.syn_frame_count_);
EXPECT_EQ(0, visitor.push_promise_frame_count_);
if (spdy_version() < HTTP2) {
EXPECT_EQ(1, visitor.syn_reply_frame_count_);
EXPECT_EQ(0, visitor.headers_frame_count_);
} else {
EXPECT_EQ(0, visitor.syn_reply_frame_count_);
EXPECT_EQ(1, visitor.headers_frame_count_);
}
EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
}
TEST_P(BufferedSpdyFramerTest, ReadHeadersHeaderBlock) {
SpdyHeaderBlock headers;
headers["alpha"] = "beta";
headers["gamma"] = "delta";
BufferedSpdyFramer framer(spdy_version(), true);
scoped_ptr<SpdyFrame> control_frame(
framer.CreateHeaders(1, // stream_id
CONTROL_FLAG_NONE,
0, // priority
&headers));
EXPECT_TRUE(control_frame.get() != NULL);
TestBufferedSpdyVisitor visitor(spdy_version());
visitor.SimulateInFramer(
reinterpret_cast<unsigned char*>(control_frame.get()->data()),
control_frame.get()->size());
EXPECT_EQ(0, visitor.error_count_);
EXPECT_EQ(0, visitor.syn_frame_count_);
EXPECT_EQ(0, visitor.syn_reply_frame_count_);
EXPECT_EQ(1, visitor.headers_frame_count_);
EXPECT_EQ(0, visitor.push_promise_frame_count_);
EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
}
TEST_P(BufferedSpdyFramerTest, ReadPushPromiseHeaderBlock) {
if (spdy_version() < HTTP2)
return;
SpdyHeaderBlock headers;
headers["alpha"] = "beta";
headers["gamma"] = "delta";
BufferedSpdyFramer framer(spdy_version(), true);
scoped_ptr<SpdyFrame> control_frame(
framer.CreatePushPromise(1, 2, &headers));
EXPECT_TRUE(control_frame.get() != NULL);
TestBufferedSpdyVisitor visitor(spdy_version());
visitor.SimulateInFramer(
reinterpret_cast<unsigned char*>(control_frame.get()->data()),
control_frame.get()->size());
EXPECT_EQ(0, visitor.error_count_);
EXPECT_EQ(0, visitor.syn_frame_count_);
EXPECT_EQ(0, visitor.syn_reply_frame_count_);
EXPECT_EQ(0, visitor.headers_frame_count_);
EXPECT_EQ(1, visitor.push_promise_frame_count_);
EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
EXPECT_EQ(1u, visitor.header_stream_id_);
EXPECT_EQ(2u, visitor.promised_stream_id_);
}
} // namespace net
|