[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / net / spdy / buffered_spdy_framer_unittest.cc
blob05e2b60dab0d9b5677897160712ebae270959b2c
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.
5 #include "net/spdy/buffered_spdy_framer.h"
7 #include "net/spdy/spdy_test_util_common.h"
8 #include "testing/platform_test.h"
10 namespace net {
12 namespace {
14 class TestBufferedSpdyVisitor : public BufferedSpdyFramerVisitorInterface {
15 public:
16 explicit TestBufferedSpdyVisitor(SpdyMajorVersion spdy_version)
17 : buffered_spdy_framer_(spdy_version, true),
18 error_count_(0),
19 setting_count_(0),
20 syn_frame_count_(0),
21 syn_reply_frame_count_(0),
22 headers_frame_count_(0),
23 push_promise_frame_count_(0),
24 header_stream_id_(static_cast<SpdyStreamId>(-1)),
25 promised_stream_id_(static_cast<SpdyStreamId>(-1)) {
28 void OnError(SpdyFramer::SpdyError error_code) override {
29 LOG(INFO) << "SpdyFramer Error: " << error_code;
30 error_count_++;
33 void OnStreamError(SpdyStreamId stream_id,
34 const std::string& description) override {
35 LOG(INFO) << "SpdyFramer Error on stream: " << stream_id << " "
36 << description;
37 error_count_++;
40 void OnSynStream(SpdyStreamId stream_id,
41 SpdyStreamId associated_stream_id,
42 SpdyPriority priority,
43 bool fin,
44 bool unidirectional,
45 const SpdyHeaderBlock& headers) override {
46 header_stream_id_ = stream_id;
47 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream);
48 syn_frame_count_++;
49 headers_ = headers;
52 void OnSynReply(SpdyStreamId stream_id,
53 bool fin,
54 const SpdyHeaderBlock& headers) override {
55 header_stream_id_ = stream_id;
56 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream);
57 syn_reply_frame_count_++;
58 headers_ = headers;
61 void OnHeaders(SpdyStreamId stream_id,
62 bool has_priority,
63 SpdyPriority priority,
64 SpdyStreamId parent_stream_id,
65 bool exclusive,
66 bool fin,
67 const SpdyHeaderBlock& headers) override {
68 header_stream_id_ = stream_id;
69 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream);
70 headers_frame_count_++;
71 headers_ = headers;
74 void OnDataFrameHeader(SpdyStreamId stream_id,
75 size_t length,
76 bool fin) override {
77 ADD_FAILURE() << "Unexpected OnDataFrameHeader call.";
80 void OnStreamFrameData(SpdyStreamId stream_id,
81 const char* data,
82 size_t len,
83 bool fin) override {
84 LOG(FATAL) << "Unexpected OnStreamFrameData call.";
87 void OnStreamPadding(SpdyStreamId stream_id, size_t len) override {
88 LOG(FATAL) << "Unexpected OnStreamPadding call.";
91 void OnSettings(bool clear_persisted) override {}
93 void OnSetting(SpdySettingsIds id, uint8 flags, uint32 value) override {
94 setting_count_++;
97 void OnPing(SpdyPingId unique_id, bool is_ack) override {}
99 void OnRstStream(SpdyStreamId stream_id,
100 SpdyRstStreamStatus status) override {}
102 void OnGoAway(SpdyStreamId last_accepted_stream_id,
103 SpdyGoAwayStatus status) override {}
105 bool OnCredentialFrameData(const char*, size_t) {
106 LOG(FATAL) << "Unexpected OnCredentialFrameData call.";
107 return false;
110 void OnDataFrameHeader(const SpdyFrame* frame) {
111 LOG(FATAL) << "Unexpected OnDataFrameHeader call.";
114 void OnRstStream(const SpdyFrame& frame) {}
115 void OnGoAway(const SpdyFrame& frame) {}
116 void OnPing(const SpdyFrame& frame) {}
117 void OnWindowUpdate(SpdyStreamId stream_id, int delta_window_size) override {}
119 void OnPushPromise(SpdyStreamId stream_id,
120 SpdyStreamId promised_stream_id,
121 const SpdyHeaderBlock& headers) override {
122 header_stream_id_ = stream_id;
123 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream);
124 push_promise_frame_count_++;
125 promised_stream_id_ = promised_stream_id;
126 EXPECT_NE(promised_stream_id_, SpdyFramer::kInvalidStream);
127 headers_ = headers;
130 bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) override {
131 return true;
134 void OnCredential(const SpdyFrame& frame) {}
136 // Convenience function which runs a framer simulation with particular input.
137 void SimulateInFramer(const unsigned char* input, size_t size) {
138 buffered_spdy_framer_.set_visitor(this);
139 size_t input_remaining = size;
140 const char* input_ptr = reinterpret_cast<const char*>(input);
141 while (input_remaining > 0 &&
142 buffered_spdy_framer_.error_code() == SpdyFramer::SPDY_NO_ERROR) {
143 // To make the tests more interesting, we feed random (amd small) chunks
144 // into the framer. This simulates getting strange-sized reads from
145 // the socket.
146 const size_t kMaxReadSize = 32;
147 size_t bytes_read =
148 (rand() % std::min(input_remaining, kMaxReadSize)) + 1;
149 size_t bytes_processed =
150 buffered_spdy_framer_.ProcessInput(input_ptr, bytes_read);
151 input_remaining -= bytes_processed;
152 input_ptr += bytes_processed;
156 BufferedSpdyFramer buffered_spdy_framer_;
158 // Counters from the visitor callbacks.
159 int error_count_;
160 int setting_count_;
161 int syn_frame_count_;
162 int syn_reply_frame_count_;
163 int headers_frame_count_;
164 int push_promise_frame_count_;
166 // Header block streaming state:
167 SpdyStreamId header_stream_id_;
168 SpdyStreamId promised_stream_id_;
170 // Headers from OnSyn, OnSynReply, OnHeaders and OnPushPromise for
171 // verification.
172 SpdyHeaderBlock headers_;
175 } // namespace
177 class BufferedSpdyFramerTest
178 : public PlatformTest,
179 public ::testing::WithParamInterface<NextProto> {
180 protected:
181 // Returns true if the two header blocks have equivalent content.
182 bool CompareHeaderBlocks(const SpdyHeaderBlock* expected,
183 const SpdyHeaderBlock* actual) {
184 if (expected->size() != actual->size()) {
185 LOG(ERROR) << "Expected " << expected->size() << " headers; actually got "
186 << actual->size() << ".";
187 return false;
189 for (SpdyHeaderBlock::const_iterator it = expected->begin();
190 it != expected->end();
191 ++it) {
192 SpdyHeaderBlock::const_iterator it2 = actual->find(it->first);
193 if (it2 == actual->end()) {
194 LOG(ERROR) << "Expected header name '" << it->first << "'.";
195 return false;
197 if (it->second.compare(it2->second) != 0) {
198 LOG(ERROR) << "Expected header named '" << it->first
199 << "' to have a value of '" << it->second
200 << "'. The actual value received was '" << it2->second
201 << "'.";
202 return false;
205 return true;
208 SpdyMajorVersion spdy_version() {
209 return NextProtoToSpdyMajorVersion(GetParam());
213 INSTANTIATE_TEST_CASE_P(NextProto,
214 BufferedSpdyFramerTest,
215 testing::Values(kProtoSPDY31,
216 kProtoHTTP2_14,
217 kProtoHTTP2));
219 TEST_P(BufferedSpdyFramerTest, OnSetting) {
220 SpdyFramer framer(spdy_version());
221 SpdySettingsIR settings_ir;
222 settings_ir.AddSetting(SETTINGS_INITIAL_WINDOW_SIZE, false, false, 2);
223 settings_ir.AddSetting(SETTINGS_MAX_CONCURRENT_STREAMS, false, false, 3);
224 scoped_ptr<SpdyFrame> control_frame(framer.SerializeSettings(settings_ir));
225 TestBufferedSpdyVisitor visitor(spdy_version());
227 visitor.SimulateInFramer(
228 reinterpret_cast<unsigned char*>(control_frame->data()),
229 control_frame->size());
230 EXPECT_EQ(0, visitor.error_count_);
231 EXPECT_EQ(2, visitor.setting_count_);
234 TEST_P(BufferedSpdyFramerTest, ReadSynStreamHeaderBlock) {
235 if (spdy_version() > SPDY3) {
236 // SYN_STREAM not supported in SPDY>3.
237 return;
239 SpdyHeaderBlock headers;
240 headers["aa"] = "vv";
241 headers["bb"] = "ww";
242 BufferedSpdyFramer framer(spdy_version(), true);
243 scoped_ptr<SpdyFrame> control_frame(
244 framer.CreateSynStream(1, // stream_id
245 0, // associated_stream_id
246 1, // priority
247 CONTROL_FLAG_NONE,
248 &headers));
249 EXPECT_TRUE(control_frame.get() != NULL);
251 TestBufferedSpdyVisitor visitor(spdy_version());
252 visitor.SimulateInFramer(
253 reinterpret_cast<unsigned char*>(control_frame.get()->data()),
254 control_frame.get()->size());
255 EXPECT_EQ(0, visitor.error_count_);
256 EXPECT_EQ(1, visitor.syn_frame_count_);
257 EXPECT_EQ(0, visitor.syn_reply_frame_count_);
258 EXPECT_EQ(0, visitor.headers_frame_count_);
259 EXPECT_EQ(0, visitor.push_promise_frame_count_);
260 EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
263 TEST_P(BufferedSpdyFramerTest, ReadSynReplyHeaderBlock) {
264 if (spdy_version() > SPDY3) {
265 // SYN_REPLY not supported in SPDY>3.
266 return;
268 SpdyHeaderBlock headers;
269 headers["alpha"] = "beta";
270 headers["gamma"] = "delta";
271 BufferedSpdyFramer framer(spdy_version(), true);
272 scoped_ptr<SpdyFrame> control_frame(
273 framer.CreateSynReply(1, // stream_id
274 CONTROL_FLAG_NONE,
275 &headers));
276 EXPECT_TRUE(control_frame.get() != NULL);
278 TestBufferedSpdyVisitor visitor(spdy_version());
279 visitor.SimulateInFramer(
280 reinterpret_cast<unsigned char*>(control_frame.get()->data()),
281 control_frame.get()->size());
282 EXPECT_EQ(0, visitor.error_count_);
283 EXPECT_EQ(0, visitor.syn_frame_count_);
284 EXPECT_EQ(0, visitor.push_promise_frame_count_);
285 if (spdy_version() < HTTP2) {
286 EXPECT_EQ(1, visitor.syn_reply_frame_count_);
287 EXPECT_EQ(0, visitor.headers_frame_count_);
288 } else {
289 EXPECT_EQ(0, visitor.syn_reply_frame_count_);
290 EXPECT_EQ(1, visitor.headers_frame_count_);
292 EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
295 TEST_P(BufferedSpdyFramerTest, ReadHeadersHeaderBlock) {
296 SpdyHeaderBlock headers;
297 headers["alpha"] = "beta";
298 headers["gamma"] = "delta";
299 BufferedSpdyFramer framer(spdy_version(), true);
300 scoped_ptr<SpdyFrame> control_frame(
301 framer.CreateHeaders(1, // stream_id
302 CONTROL_FLAG_NONE,
303 0, // priority
304 &headers));
305 EXPECT_TRUE(control_frame.get() != NULL);
307 TestBufferedSpdyVisitor visitor(spdy_version());
308 visitor.SimulateInFramer(
309 reinterpret_cast<unsigned char*>(control_frame.get()->data()),
310 control_frame.get()->size());
311 EXPECT_EQ(0, visitor.error_count_);
312 EXPECT_EQ(0, visitor.syn_frame_count_);
313 EXPECT_EQ(0, visitor.syn_reply_frame_count_);
314 EXPECT_EQ(1, visitor.headers_frame_count_);
315 EXPECT_EQ(0, visitor.push_promise_frame_count_);
316 EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
319 TEST_P(BufferedSpdyFramerTest, ReadPushPromiseHeaderBlock) {
320 if (spdy_version() < HTTP2)
321 return;
322 SpdyHeaderBlock headers;
323 headers["alpha"] = "beta";
324 headers["gamma"] = "delta";
325 BufferedSpdyFramer framer(spdy_version(), true);
326 scoped_ptr<SpdyFrame> control_frame(
327 framer.CreatePushPromise(1, 2, &headers));
328 EXPECT_TRUE(control_frame.get() != NULL);
330 TestBufferedSpdyVisitor visitor(spdy_version());
331 visitor.SimulateInFramer(
332 reinterpret_cast<unsigned char*>(control_frame.get()->data()),
333 control_frame.get()->size());
334 EXPECT_EQ(0, visitor.error_count_);
335 EXPECT_EQ(0, visitor.syn_frame_count_);
336 EXPECT_EQ(0, visitor.syn_reply_frame_count_);
337 EXPECT_EQ(0, visitor.headers_frame_count_);
338 EXPECT_EQ(1, visitor.push_promise_frame_count_);
339 EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
340 EXPECT_EQ(1u, visitor.header_stream_id_);
341 EXPECT_EQ(2u, visitor.promised_stream_id_);
344 } // namespace net