Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / net / spdy / buffered_spdy_framer_unittest.cc
blob9a967d246cc081b210be618a20b2ac550afce0b8
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 bool fin,
65 const SpdyHeaderBlock& headers) override {
66 header_stream_id_ = stream_id;
67 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream);
68 headers_frame_count_++;
69 headers_ = headers;
72 void OnDataFrameHeader(SpdyStreamId stream_id,
73 size_t length,
74 bool fin) override {
75 ADD_FAILURE() << "Unexpected OnDataFrameHeader call.";
78 void OnStreamFrameData(SpdyStreamId stream_id,
79 const char* data,
80 size_t len,
81 bool fin) override {
82 LOG(FATAL) << "Unexpected OnStreamFrameData call.";
85 void OnStreamPadding(SpdyStreamId stream_id, size_t len) override {
86 LOG(FATAL) << "Unexpected OnStreamPadding call.";
89 void OnSettings(bool clear_persisted) override {}
91 void OnSetting(SpdySettingsIds id, uint8 flags, uint32 value) override {
92 setting_count_++;
95 void OnPing(SpdyPingId unique_id, bool is_ack) override {}
97 void OnRstStream(SpdyStreamId stream_id,
98 SpdyRstStreamStatus status) override {}
100 void OnGoAway(SpdyStreamId last_accepted_stream_id,
101 SpdyGoAwayStatus status) override {}
103 bool OnCredentialFrameData(const char*, size_t) {
104 LOG(FATAL) << "Unexpected OnCredentialFrameData call.";
105 return false;
108 void OnDataFrameHeader(const SpdyFrame* frame) {
109 LOG(FATAL) << "Unexpected OnDataFrameHeader call.";
112 void OnRstStream(const SpdyFrame& frame) {}
113 void OnGoAway(const SpdyFrame& frame) {}
114 void OnPing(const SpdyFrame& frame) {}
115 void OnWindowUpdate(SpdyStreamId stream_id,
116 uint32 delta_window_size) override {}
118 void OnPushPromise(SpdyStreamId stream_id,
119 SpdyStreamId promised_stream_id,
120 const SpdyHeaderBlock& headers) override {
121 header_stream_id_ = stream_id;
122 EXPECT_NE(header_stream_id_, SpdyFramer::kInvalidStream);
123 push_promise_frame_count_++;
124 promised_stream_id_ = promised_stream_id;
125 EXPECT_NE(promised_stream_id_, SpdyFramer::kInvalidStream);
126 headers_ = headers;
129 bool OnUnknownFrame(SpdyStreamId stream_id, int frame_type) override {
130 return true;
133 void OnCredential(const SpdyFrame& frame) {}
135 // Convenience function which runs a framer simulation with particular input.
136 void SimulateInFramer(const unsigned char* input, size_t size) {
137 buffered_spdy_framer_.set_visitor(this);
138 size_t input_remaining = size;
139 const char* input_ptr = reinterpret_cast<const char*>(input);
140 while (input_remaining > 0 &&
141 buffered_spdy_framer_.error_code() == SpdyFramer::SPDY_NO_ERROR) {
142 // To make the tests more interesting, we feed random (amd small) chunks
143 // into the framer. This simulates getting strange-sized reads from
144 // the socket.
145 const size_t kMaxReadSize = 32;
146 size_t bytes_read =
147 (rand() % std::min(input_remaining, kMaxReadSize)) + 1;
148 size_t bytes_processed =
149 buffered_spdy_framer_.ProcessInput(input_ptr, bytes_read);
150 input_remaining -= bytes_processed;
151 input_ptr += bytes_processed;
155 BufferedSpdyFramer buffered_spdy_framer_;
157 // Counters from the visitor callbacks.
158 int error_count_;
159 int setting_count_;
160 int syn_frame_count_;
161 int syn_reply_frame_count_;
162 int headers_frame_count_;
163 int push_promise_frame_count_;
165 // Header block streaming state:
166 SpdyStreamId header_stream_id_;
167 SpdyStreamId promised_stream_id_;
169 // Headers from OnSyn, OnSynReply, OnHeaders and OnPushPromise for
170 // verification.
171 SpdyHeaderBlock headers_;
174 } // namespace
176 class BufferedSpdyFramerTest
177 : public PlatformTest,
178 public ::testing::WithParamInterface<NextProto> {
179 protected:
180 // Returns true if the two header blocks have equivalent content.
181 bool CompareHeaderBlocks(const SpdyHeaderBlock* expected,
182 const SpdyHeaderBlock* actual) {
183 if (expected->size() != actual->size()) {
184 LOG(ERROR) << "Expected " << expected->size() << " headers; actually got "
185 << actual->size() << ".";
186 return false;
188 for (SpdyHeaderBlock::const_iterator it = expected->begin();
189 it != expected->end();
190 ++it) {
191 SpdyHeaderBlock::const_iterator it2 = actual->find(it->first);
192 if (it2 == actual->end()) {
193 LOG(ERROR) << "Expected header name '" << it->first << "'.";
194 return false;
196 if (it->second.compare(it2->second) != 0) {
197 LOG(ERROR) << "Expected header named '" << it->first
198 << "' to have a value of '" << it->second
199 << "'. The actual value received was '" << it2->second
200 << "'.";
201 return false;
204 return true;
207 SpdyMajorVersion spdy_version() {
208 return NextProtoToSpdyMajorVersion(GetParam());
212 INSTANTIATE_TEST_CASE_P(NextProto,
213 BufferedSpdyFramerTest,
214 testing::Values(kProtoSPDY31,
215 kProtoSPDY4_14,
216 kProtoSPDY4));
218 TEST_P(BufferedSpdyFramerTest, OnSetting) {
219 SpdyFramer framer(spdy_version());
220 SpdySettingsIR settings_ir;
221 settings_ir.AddSetting(SETTINGS_INITIAL_WINDOW_SIZE, false, false, 2);
222 settings_ir.AddSetting(SETTINGS_MAX_CONCURRENT_STREAMS, false, false, 3);
223 scoped_ptr<SpdyFrame> control_frame(framer.SerializeSettings(settings_ir));
224 TestBufferedSpdyVisitor visitor(spdy_version());
226 visitor.SimulateInFramer(
227 reinterpret_cast<unsigned char*>(control_frame->data()),
228 control_frame->size());
229 EXPECT_EQ(0, visitor.error_count_);
230 EXPECT_EQ(2, visitor.setting_count_);
233 TEST_P(BufferedSpdyFramerTest, ReadSynStreamHeaderBlock) {
234 if (spdy_version() > SPDY3) {
235 // SYN_STREAM not supported in SPDY>3.
236 return;
238 SpdyHeaderBlock headers;
239 headers["aa"] = "vv";
240 headers["bb"] = "ww";
241 BufferedSpdyFramer framer(spdy_version(), true);
242 scoped_ptr<SpdyFrame> control_frame(
243 framer.CreateSynStream(1, // stream_id
244 0, // associated_stream_id
245 1, // priority
246 CONTROL_FLAG_NONE,
247 &headers));
248 EXPECT_TRUE(control_frame.get() != NULL);
250 TestBufferedSpdyVisitor visitor(spdy_version());
251 visitor.SimulateInFramer(
252 reinterpret_cast<unsigned char*>(control_frame.get()->data()),
253 control_frame.get()->size());
254 EXPECT_EQ(0, visitor.error_count_);
255 EXPECT_EQ(1, visitor.syn_frame_count_);
256 EXPECT_EQ(0, visitor.syn_reply_frame_count_);
257 EXPECT_EQ(0, visitor.headers_frame_count_);
258 EXPECT_EQ(0, visitor.push_promise_frame_count_);
259 EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
262 TEST_P(BufferedSpdyFramerTest, ReadSynReplyHeaderBlock) {
263 if (spdy_version() > SPDY3) {
264 // SYN_REPLY not supported in SPDY>3.
265 return;
267 SpdyHeaderBlock headers;
268 headers["alpha"] = "beta";
269 headers["gamma"] = "delta";
270 BufferedSpdyFramer framer(spdy_version(), true);
271 scoped_ptr<SpdyFrame> control_frame(
272 framer.CreateSynReply(1, // stream_id
273 CONTROL_FLAG_NONE,
274 &headers));
275 EXPECT_TRUE(control_frame.get() != NULL);
277 TestBufferedSpdyVisitor visitor(spdy_version());
278 visitor.SimulateInFramer(
279 reinterpret_cast<unsigned char*>(control_frame.get()->data()),
280 control_frame.get()->size());
281 EXPECT_EQ(0, visitor.error_count_);
282 EXPECT_EQ(0, visitor.syn_frame_count_);
283 EXPECT_EQ(0, visitor.push_promise_frame_count_);
284 if (spdy_version() < SPDY4) {
285 EXPECT_EQ(1, visitor.syn_reply_frame_count_);
286 EXPECT_EQ(0, visitor.headers_frame_count_);
287 } else {
288 EXPECT_EQ(0, visitor.syn_reply_frame_count_);
289 EXPECT_EQ(1, visitor.headers_frame_count_);
291 EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
294 TEST_P(BufferedSpdyFramerTest, ReadHeadersHeaderBlock) {
295 SpdyHeaderBlock headers;
296 headers["alpha"] = "beta";
297 headers["gamma"] = "delta";
298 BufferedSpdyFramer framer(spdy_version(), true);
299 scoped_ptr<SpdyFrame> control_frame(
300 framer.CreateHeaders(1, // stream_id
301 CONTROL_FLAG_NONE,
302 0, // priority
303 &headers));
304 EXPECT_TRUE(control_frame.get() != NULL);
306 TestBufferedSpdyVisitor visitor(spdy_version());
307 visitor.SimulateInFramer(
308 reinterpret_cast<unsigned char*>(control_frame.get()->data()),
309 control_frame.get()->size());
310 EXPECT_EQ(0, visitor.error_count_);
311 EXPECT_EQ(0, visitor.syn_frame_count_);
312 EXPECT_EQ(0, visitor.syn_reply_frame_count_);
313 EXPECT_EQ(1, visitor.headers_frame_count_);
314 EXPECT_EQ(0, visitor.push_promise_frame_count_);
315 EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
318 TEST_P(BufferedSpdyFramerTest, ReadPushPromiseHeaderBlock) {
319 if (spdy_version() < SPDY4)
320 return;
321 SpdyHeaderBlock headers;
322 headers["alpha"] = "beta";
323 headers["gamma"] = "delta";
324 BufferedSpdyFramer framer(spdy_version(), true);
325 scoped_ptr<SpdyFrame> control_frame(
326 framer.CreatePushPromise(1, 2, &headers));
327 EXPECT_TRUE(control_frame.get() != NULL);
329 TestBufferedSpdyVisitor visitor(spdy_version());
330 visitor.SimulateInFramer(
331 reinterpret_cast<unsigned char*>(control_frame.get()->data()),
332 control_frame.get()->size());
333 EXPECT_EQ(0, visitor.error_count_);
334 EXPECT_EQ(0, visitor.syn_frame_count_);
335 EXPECT_EQ(0, visitor.syn_reply_frame_count_);
336 EXPECT_EQ(0, visitor.headers_frame_count_);
337 EXPECT_EQ(1, visitor.push_promise_frame_count_);
338 EXPECT_TRUE(CompareHeaderBlocks(&headers, &visitor.headers_));
339 EXPECT_EQ(1u, visitor.header_stream_id_);
340 EXPECT_EQ(2u, visitor.promised_stream_id_);
343 } // namespace net