blob: d731b3adae94abd2f172485d9e23da95d2c134ba [file] [log] [blame]
// 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 <algorithm>
#include <iostream>
#include <limits>
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "net/spdy/spdy_frame_builder.h"
#include "net/spdy/spdy_framer.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 std::string;
using std::max;
using std::min;
using std::numeric_limits;
using testing::_;
namespace net {
namespace test {
static const size_t kMaxDecompressedSize = 1024;
// TODO(akalin): Make sure expectations on mocks are set before mock
// functions are called, as interleaving expectations and calls is
// undefined.
class MockVisitor : public SpdyFramerVisitorInterface {
public:
MOCK_METHOD1(OnError, void(SpdyFramer* framer));
MOCK_METHOD3(OnDataFrameHeader, void(SpdyStreamId stream_id,
size_t length,
bool fin));
MOCK_METHOD4(OnStreamFrameData, void(SpdyStreamId stream_id,
const char* data,
size_t len,
bool fin));
MOCK_METHOD3(OnControlFrameHeaderData, bool(SpdyStreamId stream_id,
const char* header_data,
size_t len));
MOCK_METHOD6(OnSynStream, void(SpdyStreamId stream_id,
SpdyStreamId associated_stream_id,
SpdyPriority priority,
uint8 slot,
bool fin,
bool unidirectional));
MOCK_METHOD2(OnSynReply, void(SpdyStreamId stream_id, bool fin));
MOCK_METHOD2(OnRstStream, void(SpdyStreamId stream_id,
SpdyRstStreamStatus status));
MOCK_METHOD1(OnSettings, void(bool clear_persisted));
MOCK_METHOD3(OnSetting, void(SpdySettingsIds id, uint8 flags, uint32 value));
MOCK_METHOD1(OnPing, void(uint32 unique_id));
MOCK_METHOD2(OnGoAway, void(SpdyStreamId last_accepted_stream_id,
SpdyGoAwayStatus status));
MOCK_METHOD2(OnHeaders, void(SpdyStreamId stream_id, bool fin));
MOCK_METHOD2(OnWindowUpdate, void(SpdyStreamId stream_id,
uint32 delta_window_size));
MOCK_METHOD2(OnCredentialFrameData, bool(const char* credential_data,
size_t len));
MOCK_METHOD1(OnBlocked, void(SpdyStreamId stream_id));
MOCK_METHOD2(OnPushPromise, void(SpdyStreamId stream_id,
SpdyStreamId promised_stream_id));
};
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 SpdyFrame.
template<class SpdyFrameType> static SpdyFrame* 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_RESET, framer->state());
framer->set_visitor(NULL);
char* buffer = visitor.ReleaseBuffer();
CHECK(buffer != NULL);
SpdyFrame* decompressed_frame = new SpdyFrame(buffer, visitor.size(), true);
if (framer->protocol_version() == 4) {
SetFrameLength(decompressed_frame,
visitor.size(),
framer->protocol_version());
} else {
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]);
}
virtual void OnError(SpdyFramer* framer) OVERRIDE { LOG(FATAL); }
virtual void OnDataFrameHeader(SpdyStreamId stream_id,
size_t length,
bool fin) OVERRIDE {
LOG(FATAL) << "Unexpected data frame header";
}
virtual void OnStreamFrameData(SpdyStreamId stream_id,
const char* data,
size_t len,
bool fin) OVERRIDE {
LOG(FATAL);
}
virtual 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;
}
virtual void OnSynStream(SpdyStreamId stream_id,
SpdyStreamId associated_stream_id,
SpdyPriority priority,
uint8 slot,
bool fin,
bool unidirectional) OVERRIDE {
SpdyFramer framer(version_);
framer.set_enable_compression(false);
const SpdyHeaderBlock null_headers;
int flags = CONTROL_FLAG_NONE;
if (fin) {
flags &= CONTROL_FLAG_FIN;
}
if (unidirectional) {
flags &= CONTROL_FLAG_UNIDIRECTIONAL;
}
scoped_ptr<SpdyFrame> frame(
framer.CreateSynStream(stream_id,
associated_stream_id,
priority,
slot,
static_cast<SpdyControlFlags>(flags),
false,
&null_headers));
ResetBuffer();
memcpy(buffer_.get(), frame->data(), framer.GetSynStreamMinimumSize());
size_ += framer.GetSynStreamMinimumSize();
}
virtual void OnSynReply(SpdyStreamId stream_id, bool fin) OVERRIDE {
SpdyFramer framer(version_);
framer.set_enable_compression(false);
const SpdyHeaderBlock null_headers;
int flags = CONTROL_FLAG_NONE;
if (fin) {
flags &= CONTROL_FLAG_FIN;
}
scoped_ptr<SpdyFrame> frame(
framer.CreateHeaders(stream_id,
static_cast<SpdyControlFlags>(flags),
false,
&null_headers));
ResetBuffer();
memcpy(buffer_.get(), frame->data(), framer.GetHeadersMinimumSize());
size_ += framer.GetSynStreamMinimumSize();
}
virtual void OnRstStream(SpdyStreamId stream_id,
SpdyRstStreamStatus status) OVERRIDE {
LOG(FATAL);
}
virtual void OnSetting(SpdySettingsIds id,
uint8 flags,
uint32 value) OVERRIDE {
LOG(FATAL);
}
virtual void OnPing(uint32 unique_id) OVERRIDE {
LOG(FATAL);
}
virtual void OnGoAway(SpdyStreamId last_accepted_stream_id,
SpdyGoAwayStatus status) OVERRIDE {
LOG(FATAL);
}
virtual void OnHeaders(SpdyStreamId stream_id, bool fin) OVERRIDE {
SpdyFramer framer(version_);
framer.set_enable_compression(false);
const SpdyHeaderBlock null_headers;
int flags = CONTROL_FLAG_NONE;
if (fin) {
flags &= CONTROL_FLAG_FIN;
}
scoped_ptr<SpdyFrame> frame(
framer.CreateHeaders(stream_id,
static_cast<SpdyControlFlags>(flags),
false,
&null_headers));
ResetBuffer();
memcpy(buffer_.get(), frame->data(), framer.GetHeadersMinimumSize());
size_ += framer.GetHeadersMinimumSize();
}
virtual void OnWindowUpdate(SpdyStreamId stream_id, int delta_window_size) {
LOG(FATAL);
}
virtual bool OnCredentialFrameData(const char* /*credential_data*/,
size_t /*len*/) OVERRIDE {
LOG(FATAL) << "Unexpected CREDENTIAL Frame";
return false;
}
virtual void OnPushPromise(SpdyStreamId stream_id,
SpdyStreamId promised_stream_id) OVERRIDE {
SpdyFramer framer(version_);
framer.set_enable_compression(false);
const SpdyHeaderBlock null_headers;
scoped_ptr<SpdyFrame> frame(
framer.CreatePushPromise(stream_id, promised_stream_id,
&null_headers));
ResetBuffer();
memcpy(buffer_.get(), frame->data(), framer.GetPushPromiseMinimumSize());
size_ += framer.GetPushPromiseMinimumSize();
}
char* ReleaseBuffer() {
CHECK(finished_);
return buffer_.release();
}
virtual void OnWindowUpdate(SpdyStreamId stream_id,
uint32 delta_window_size) OVERRIDE {
LOG(FATAL);
}
size_t size() const {
CHECK(finished_);
return size_;
}
private:
SpdyMajorVersion version_;
scoped_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:
static const size_t kDefaultHeaderBufferSize = 16 * 1024 * 1024;
static const size_t kDefaultCredentialBufferSize = 16 * 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),
goaway_count_(0),
setting_count_(0),
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_flag_count_(0),
zero_length_data_frame_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_(-1),
header_control_type_(DATA),
header_buffer_valid_(false),
credential_buffer_(new char[kDefaultCredentialBufferSize]),
credential_buffer_length_(0),
credential_buffer_size_(kDefaultCredentialBufferSize) {
}
virtual void OnError(SpdyFramer* f) OVERRIDE {
LOG(INFO) << "SpdyFramer Error: "
<< SpdyFramer::ErrorCodeToString(f->error_code());
error_count_++;
}
virtual void OnDataFrameHeader(SpdyStreamId stream_id,
size_t length,
bool fin) OVERRIDE {
data_frame_count_++;
header_stream_id_ = stream_id;
}
virtual void OnStreamFrameData(SpdyStreamId stream_id,
const char* data,
size_t len,
bool fin) OVERRIDE {
EXPECT_EQ(header_stream_id_, stream_id);
if (len == 0)
++zero_length_data_frame_count_;
data_bytes_ += len;
std::cerr << "OnStreamFrameData(" << stream_id << ", \"";
if (len > 0) {
for (size_t i = 0 ; i < len; ++i) {
std::cerr << std::hex << (0xFF & (unsigned int)data[i]) << std::dec;
}
}
std::cerr << "\", " << len << ")\n";
}
virtual 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.
CHECK(header_buffer_valid_);
size_t parsed_length = framer_.ParseHeaderBlockInBuffer(
header_buffer_.get(), header_buffer_length_, &headers_);
DCHECK_EQ(header_buffer_length_, parsed_length);
return true;
}
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;
}
virtual void OnSynStream(SpdyStreamId stream_id,
SpdyStreamId associated_stream_id,
SpdyPriority priority,
uint8 credential_slot,
bool fin,
bool unidirectional) OVERRIDE {
syn_frame_count_++;
InitHeaderStreaming(SYN_STREAM, stream_id);
if (fin) {
fin_flag_count_++;
}
}
virtual void OnSynReply(SpdyStreamId stream_id, bool fin) OVERRIDE {
syn_reply_frame_count_++;
InitHeaderStreaming(SYN_REPLY, stream_id);
if (fin) {
fin_flag_count_++;
}
}
virtual void OnRstStream(SpdyStreamId stream_id,
SpdyRstStreamStatus status) OVERRIDE {
fin_frame_count_++;
}
virtual void OnSetting(SpdySettingsIds id,
uint8 flags,
uint32 value) OVERRIDE {
setting_count_++;
}
virtual void OnPing(uint32 unique_id) OVERRIDE {
DLOG(FATAL);
}
virtual void OnGoAway(SpdyStreamId last_accepted_stream_id,
SpdyGoAwayStatus status) OVERRIDE {
goaway_count_++;
}
virtual void OnHeaders(SpdyStreamId stream_id, bool fin) OVERRIDE {
headers_frame_count_++;
InitHeaderStreaming(HEADERS, stream_id);
if (fin) {
fin_flag_count_++;
}
}
virtual void OnWindowUpdate(SpdyStreamId stream_id,
uint32 delta_window_size) OVERRIDE {
last_window_update_stream_ = stream_id;
last_window_update_delta_ = delta_window_size;
}
virtual bool OnCredentialFrameData(const char* credential_data,
size_t len) OVERRIDE {
if (len == 0) {
if (!framer_.ParseCredentialData(credential_buffer_.get(),
credential_buffer_length_,
&credential_)) {
LOG(INFO) << "Error parsing credential data.";
++error_count_;
}
return true;
}
const size_t available =
credential_buffer_size_ - credential_buffer_length_;
if (len > available) {
return false;
}
memcpy(credential_buffer_.get() + credential_buffer_length_,
credential_data, len);
credential_buffer_length_ += len;
return true;
}
virtual void OnPushPromise(SpdyStreamId stream_id,
SpdyStreamId promised_stream_id) OVERRIDE {
InitHeaderStreaming(PUSH_PROMISE, stream_id);
last_push_promise_stream_ = stream_id;
last_push_promise_promised_stream_ = promised_stream_id;
}
virtual 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;
}
virtual 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 (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() % 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) {
DCHECK_GE(header_control_type, FIRST_CONTROL_TYPE);
DCHECK_LE(header_control_type, LAST_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]);
}
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 goaway_count_;
int setting_count_;
SpdyStreamId last_window_update_stream_;
uint32 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.
int fin_flag_count_; // The count of frames with the FIN flag set.
int zero_length_data_frame_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:
scoped_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_;
scoped_ptr<char[]> credential_buffer_;
size_t credential_buffer_length_;
size_t credential_buffer_size_;
SpdyCredential credential_;
};
// Retrieves serialized headers from SYN_STREAM frame.
// Does not check that the given frame is a SYN_STREAM.
base::StringPiece GetSerializedHeaders(const SpdyFrame* frame,
const SpdyFramer& framer) {
return base::StringPiece(frame->data() + framer.GetSynStreamMinimumSize(),
frame->size() - framer.GetSynStreamMinimumSize());
}
} // namespace test
} // namespace net
using net::test::SetFrameLength;
using net::test::SetFrameFlags;
using net::test::CompareCharArraysWithHexError;
using net::test::SpdyFramerTestUtil;
using net::test::TestSpdyVisitor;
using net::test::GetSerializedHeaders;
namespace net {
class SpdyFramerTest : public ::testing::TestWithParam<SpdyMajorVersion> {
protected:
virtual void SetUp() {
spdy_version_ = GetParam();
spdy_version_ch_ = static_cast<unsigned char>(spdy_version_);
}
void CompareFrame(const string& description,
const SpdyFrame& 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 SpdyFrame& expected_frame,
const SpdyFrame& 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());
}
// 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;
}
void AddSpdySettingFromWireFormat(SettingsMap* settings,
uint32 key,
uint32 value) {
SettingsFlagsAndId flags_and_id =
SettingsFlagsAndId::FromWireFormat(spdy_version_, key);
SpdySettingsIds id = static_cast<SpdySettingsIds>(flags_and_id.id());
SpdySettingsFlags flags =
static_cast<SpdySettingsFlags>(flags_and_id.flags());
CHECK(settings->find(id) == settings->end());
settings->insert(std::make_pair(id, SettingsFlagsAndValue(flags, value)));
}
bool IsSpdy2() { return spdy_version_ == SPDY2; }
bool IsSpdy3() { return spdy_version_ == SPDY3; }
bool IsSpdy4() { return spdy_version_ == SPDY4; }
// Version of SPDY protocol to be used.
SpdyMajorVersion spdy_version_;
unsigned char spdy_version_ch_;
};
// All tests are run with 3 different SPDY versions: SPDY/2, SPDY/3, SPDY/4.
INSTANTIATE_TEST_CASE_P(SpdyFramerTests,
SpdyFramerTest,
::testing::Values(SPDY2, SPDY3, SPDY4));
// Test that we can encode and decode a SpdyHeaderBlock in serialized form.
TEST_P(SpdyFramerTest, HeaderBlockInBuffer) {
SpdyHeaderBlock headers;
headers["alpha"] = "beta";
headers["gamma"] = "charlie";
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(false);
// Encode the header block into a SynStream frame.
scoped_ptr<SpdyFrame> frame(
framer.CreateSynStream(1, // stream id
0, // associated stream id
1, // priority
0, // credential slot
CONTROL_FLAG_NONE,
false, // compress
&headers));
EXPECT_TRUE(frame.get() != NULL);
base::StringPiece serialized_headers =
GetSerializedHeaders(frame.get(), framer);
SpdyHeaderBlock new_headers;
EXPECT_TRUE(framer.ParseHeaderBlockInBuffer(serialized_headers.data(),
serialized_headers.size(),
&new_headers));
EXPECT_EQ(headers.size(), new_headers.size());
EXPECT_EQ(headers["alpha"], new_headers["alpha"]);
EXPECT_EQ(headers["gamma"], new_headers["gamma"]);
}
// Test that if there's not a full frame, we fail to parse it.
TEST_P(SpdyFramerTest, UndersizedHeaderBlockInBuffer) {
SpdyHeaderBlock headers;
headers["alpha"] = "beta";
headers["gamma"] = "charlie";
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(false);
// Encode the header block into a SynStream frame.
scoped_ptr<SpdyFrame> frame(
framer.CreateSynStream(1, // stream id
0, // associated stream id
1, // priority
0, // credential slot
CONTROL_FLAG_NONE,
false, // compress
&headers));
EXPECT_TRUE(frame.get() != NULL);
base::StringPiece serialized_headers =
GetSerializedHeaders(frame.get(), framer);
SpdyHeaderBlock new_headers;
EXPECT_FALSE(framer.ParseHeaderBlockInBuffer(serialized_headers.data(),
serialized_headers.size() - 2,
&new_headers));
}
TEST_P(SpdyFramerTest, OutOfOrderHeaders) {
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(false);
// Frame builder with plentiful buffer size.
SpdyFrameBuilder frame(1024);
if (spdy_version_ < 4) {
frame.WriteControlFrameHeader(framer, SYN_STREAM, CONTROL_FLAG_NONE);
frame.WriteUInt32(3); // stream_id
} else {
frame.WriteFramePrefix(framer, SYN_STREAM, CONTROL_FLAG_NONE, 3);
}
frame.WriteUInt32(0); // Associated stream id
frame.WriteUInt16(0); // Priority.
if (IsSpdy2()) {
frame.WriteUInt16(2); // Number of headers.
frame.WriteString("gamma");
frame.WriteString("gamma");
frame.WriteString("alpha");
frame.WriteString("alpha");
} else {
frame.WriteUInt32(2); // Number of headers.
frame.WriteStringPiece32("gamma");
frame.WriteStringPiece32("gamma");
frame.WriteStringPiece32("alpha");
frame.WriteStringPiece32("alpha");
}
// write the length
frame.RewriteLength(framer);
SpdyHeaderBlock new_headers;
scoped_ptr<SpdyFrame> control_frame(frame.take());
base::StringPiece serialized_headers =
GetSerializedHeaders(control_frame.get(), framer);
EXPECT_TRUE(framer.ParseHeaderBlockInBuffer(serialized_headers.data(),
serialized_headers.size(),
&new_headers));
}
// Test that if we receive a SYN_STREAM with stream ID zero, we signal an error
// (but don't crash).
TEST_P(SpdyFramerTest, SynStreamWithStreamIdZero) {
testing::StrictMock<net::test::MockVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
SpdyHeaderBlock headers;
headers["alpha"] = "beta";
scoped_ptr<SpdySerializedFrame> frame(
framer.CreateSynStream(0, // stream id
0, // associated stream id
1, // priority
0, // credential slot
CONTROL_FLAG_NONE,
true, // compress
&headers));
ASSERT_TRUE(frame.get() != NULL);
// 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 SYN_REPLY with stream ID zero, we signal an error
// (but don't crash).
TEST_P(SpdyFramerTest, SynReplyWithStreamIdZero) {
testing::StrictMock<net::test::MockVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
SpdyHeaderBlock headers;
headers["alpha"] = "beta";
scoped_ptr<SpdySerializedFrame> frame(
framer.CreateSynReply(0, // stream id
CONTROL_FLAG_NONE,
true, // compress
&headers));
ASSERT_TRUE(frame.get() != NULL);
// 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 HEADERS with stream ID zero, we signal an error
// (but don't crash).
TEST_P(SpdyFramerTest, HeadersWithStreamIdZero) {
testing::StrictMock<net::test::MockVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
SpdyHeaderBlock headers;
headers["alpha"] = "beta";
scoped_ptr<SpdySerializedFrame> frame(
framer.CreateHeaders(0, // stream id
CONTROL_FLAG_NONE,
true, // compress
&headers));
ASSERT_TRUE(frame.get() != NULL);
// 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 PUSH_PROMISE with stream ID zero, we signal an
// error (but don't crash).
TEST_P(SpdyFramerTest, PushPromiseWithStreamIdZero) {
if (spdy_version_ < SPDY4) {
return;
}
testing::StrictMock<net::test::MockVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
SpdyHeaderBlock headers;
headers["alpha"] = "beta";
scoped_ptr<SpdySerializedFrame> frame(
framer.CreatePushPromise(0, // stream id
4, // promised stream id
&headers));
ASSERT_TRUE(frame.get() != NULL);
// 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 PUSH_PROMISE with promised stream ID zero, we
// signal an error (but don't crash).
TEST_P(SpdyFramerTest, PushPromiseWithPromisedStreamIdZero) {
if (spdy_version_ < SPDY4) {
return;
}
testing::StrictMock<net::test::MockVisitor> visitor;
SpdyFramer framer(spdy_version_);
framer.set_visitor(&visitor);
SpdyHeaderBlock headers;
headers["alpha"] = "beta";
scoped_ptr<SpdySerializedFrame> frame(
framer.CreatePushPromise(3, // stream id
0, // promised stream id
&headers));
ASSERT_TRUE(frame.get() != NULL);
// 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_P(SpdyFramerTest, CreateCredential) {
SpdyFramer framer(spdy_version_);
{
const char kDescription[] = "CREDENTIAL frame";
const unsigned char kV3FrameData[] = { // Also applies for V2.
0x80, spdy_version_ch_, 0x00, 0x0A,
0x00, 0x00, 0x00, 0x33,
0x00, 0x03, 0x00, 0x00,
0x00, 0x05, 'p', 'r',
'o', 'o', 'f', 0x00,
0x00, 0x00, 0x06, 'a',
' ', 'c', 'e', 'r',
't', 0x00, 0x00, 0x00,
0x0C, 'a', 'n', 'o',
't', 'h', 'e', 'r',
' ', 'c', 'e', 'r',
't', 0x00, 0x00, 0x00,
0x0A, 'f', 'i', 'n',
'a', 'l', ' ', 'c',
'e', 'r', 't',
};
const unsigned char kV4FrameData[] = {
0x00, 0x3b, 0x0A, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0x00, 0x00,
0x00, 0x05, 'p', 'r',
'o', 'o', 'f', 0x00,
0x00, 0x00, 0x06, 'a',
' ', 'c', 'e', 'r',
't', 0x00, 0x00, 0x00,
0x0C, 'a', 'n', 'o',
't', 'h', 'e', 'r',
' ', 'c', 'e', 'r',
't', 0x00, 0x00, 0x00,
0x0A, 'f', 'i', 'n',
'a', 'l', ' ', 'c',
'e', 'r', 't',
};
SpdyCredential credential;
credential.slot = 3;
credential.proof = "proof";
credential.certs.push_back("a cert");
credential.certs.push_back("another cert");
credential.certs.push_back("final cert");
scoped_ptr<SpdyFrame> frame(framer.CreateCredentialFrame(credential));
if (IsSpdy4()) {
CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
} else {
CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
}
}
}
TEST_P(SpdyFramerTest, ParseCredentialFrameData) {
SpdyFramer framer(spdy_version_);
{
const unsigned char kV3FrameData[] = { // Also applies for V2.
0x80, spdy_version_ch_, 0x00, 0x0A,
0x00, 0x00, 0x00, 0x33,
0x00, 0x03, 0x00, 0x00,
0x00, 0x05, 'p', 'r',
'o', 'o', 'f', 0x00,
0x00, 0x00, 0x06, 'a',
' ', 'c', 'e', 'r',
't', 0x00, 0x00, 0x00,
0x0C, 'a', 'n', 'o',
't', 'h', 'e', 'r',
' ', 'c', 'e', 'r',
't', 0x00, 0x00, 0x00,
0x0A, 'f', 'i', 'n',
'a', 'l', ' ', 'c',
'e', 'r', 't',
};
const unsigned char kV4FrameData[] = {
0x00, 0x37, 0x0A, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0x00, 0x00,
0x00, 0x05, 'p', 'r',
'o', 'o', 'f', 0x00,
0x00, 0x00, 0x06, 'a',
' ', 'c', 'e', 'r',
't', 0x00, 0x00, 0x00,
0x0C, 'a', 'n', 'o',
't', 'h', 'e', 'r',
' ', 'c', 'e', 'r',
't', 0x00, 0x00, 0x00,
0x0A, 'f', 'i', 'n',
'a', 'l', ' ', 'c',
'e', 'r', 't',
};
SpdyCredential credential;
if (IsSpdy4()) {
EXPECT_TRUE(SpdyFramer::ParseCredentialData(
reinterpret_cast<const char*>(kV4FrameData) +
framer.GetControlFrameHeaderSize(),
arraysize(kV4FrameData) - framer.GetControlFrameHeaderSize(),
&credential));
} else {
EXPECT_TRUE(SpdyFramer::ParseCredentialData(
reinterpret_cast<const char*>(kV3FrameData) +
framer.GetControlFrameHeaderSize(),
arraysize(kV3FrameData) - framer.GetControlFrameHeaderSize(),
&credential));
}
EXPECT_EQ(3u, credential.slot);
EXPECT_EQ("proof", credential.proof);
EXPECT_EQ("a cert", credential.certs.front());
credential.certs.erase(credential.certs.begin());
EXPECT_EQ("another cert", credential.certs.front());
credential.certs.erase(credential.certs.begin());
EXPECT_EQ("final cert", credential.certs.front());
credential.certs.erase(credential.certs.begin());
EXPECT_TRUE(credential.certs.empty());
}
}
TEST_P(SpdyFramerTest, DuplicateHeader) {
SpdyFramer framer(spdy_version_);
// Frame builder with plentiful buffer size.
SpdyFrameBuilder frame(1024);
if (spdy_version_ < 4) {
frame.WriteControlFrameHeader(framer, SYN_STREAM, CONTROL_FLAG_NONE);
frame.WriteUInt32(3); // stream_id
} else {
frame.WriteFramePrefix(framer, SYN_STREAM, CONTROL_FLAG_NONE, 3);
}
frame.WriteUInt32(0); // associated stream id
frame.WriteUInt16(0); // Priority.
if (IsSpdy2()) {
frame.WriteUInt16(2); // Number of headers.
frame.WriteString("name");
frame.WriteString("value1");
frame.WriteString("name");
frame.WriteString("value2");
} else {
frame.WriteUInt32(2); // Number of headers.
frame.WriteStringPiece32("name");
frame.WriteStringPiece32("value1");
frame.WriteStringPiece32("name");
frame.WriteStringPiece32("value2");
}
// write the length
frame.RewriteLength(framer);
SpdyHeaderBlock new_headers;
framer.set_enable_compression(false);
scoped_ptr<SpdyFrame> control_frame(frame.take());
base::StringPiece serialized_headers =
GetSerializedHeaders(control_frame.get(), framer);
// This should fail because duplicate headers are verboten by the spec.
EXPECT_FALSE(framer.ParseHeaderBlockInBuffer(serialized_headers.data(),
serialized_headers.size(),
&new_headers));
}
TEST_P(SpdyFramerTest, MultiValueHeader) {
SpdyFramer framer(spdy_version_);
// Frame builder with plentiful buffer size.
SpdyFrameBuilder frame(1024);
if (spdy_version_ < 4) {
frame.WriteControlFrameHeader(framer, SYN_STREAM, CONTROL_FLAG_NONE);
frame.WriteUInt32(3); // stream_id
} else {
frame.WriteFramePrefix(framer, SYN_STREAM, CONTROL_FLAG_NONE, 3);
}
frame.WriteUInt32(0); // associated stream id
frame.WriteUInt16(0); // Priority.
string value("value1\0value2");
if (IsSpdy2()) {
frame.WriteUInt16(1); // Number of headers.
frame.WriteString("name");
frame.WriteString(value);
} else {
frame.WriteUInt32(1); // Number of headers.
frame.WriteStringPiece32("name");
frame.WriteStringPiece32(value);
}
// write the length
frame.RewriteLength(framer);
SpdyHeaderBlock new_headers;
framer.set_enable_compression(false);
scoped_ptr<SpdyFrame> control_frame(frame.take());
base::StringPiece serialized_headers =
GetSerializedHeaders(control_frame.get(), framer);
EXPECT_TRUE(framer.ParseHeaderBlockInBuffer(serialized_headers.data(),
serialized_headers.size(),
&new_headers));
EXPECT_TRUE(new_headers.find("name") != new_headers.end());
EXPECT_EQ(value, new_headers.find("name")->second);
}
TEST_P(SpdyFramerTest, BasicCompression) {
SpdyHeaderBlock headers;
headers["server"] = "SpdyServer 1.0";
headers["date"] = "Mon 12 Jan 2009 12:12:12 PST";
headers["status"] = "200";
headers["version"] = "HTTP/1.1";
headers["content-type"] = "text/html";
headers["content-length"] = "12";
scoped_ptr<TestSpdyVisitor> visitor(new TestSpdyVisitor(spdy_version_));
SpdyFramer framer(spdy_version_);
framer.set_debug_visitor(visitor.get());
scoped_ptr<SpdyFrame> frame1(
framer.CreateSynStream(1, // stream id
0, // associated stream id
1, // priority
0, // credential slot
CONTROL_FLAG_NONE,
true, // compress
&headers));
size_t uncompressed_size1 = visitor->last_payload_len_;
size_t compressed_size1 =
visitor->last_frame_len_ - framer.GetSynStreamMinimumSize();
if (IsSpdy2()) {
EXPECT_EQ(139u, uncompressed_size1);
#if defined(USE_SYSTEM_ZLIB)
EXPECT_EQ(155u, compressed_size1);
#else // !defined(USE_SYSTEM_ZLIB)
EXPECT_EQ(135u, compressed_size1);
#endif // !defined(USE_SYSTEM_ZLIB)
} else {
EXPECT_EQ(165u, uncompressed_size1);
#if defined(USE_SYSTEM_ZLIB)
EXPECT_EQ(181u, compressed_size1);
#else // !defined(USE_SYSTEM_ZLIB)
EXPECT_EQ(117u, compressed_size1);
#endif // !defined(USE_SYSTEM_ZLIB)
}
scoped_ptr<SpdyFrame> frame2(
framer.CreateSynStream(1, // stream id
0, // associated stream id
1, // priority
0, // credential slot
CONTROL_FLAG_NONE,
true, // compress
&headers));
size_t uncompressed_size2 = visitor->last_payload_len_;
size_t compressed_size2 =
visitor->last_frame_len_ - framer.GetSynStreamMinimumSize();
// Expect the second frame to be more compact than the first.
EXPECT_LE(frame2->size(), frame1->size());
// Decompress the first frame
scoped_ptr<SpdyFrame> frame3(SpdyFramerTestUtil::DecompressFrame(
&framer, *frame1.get()));
// Decompress the second frame
visitor.reset(new TestSpdyVisitor(spdy_version_));
framer.set_debug_visitor(visitor.get());
scoped_ptr<SpdyFrame> frame4(SpdyFramerTestUtil::DecompressFrame(
&framer, *frame2.get()));
size_t uncompressed_size4 =
frame4->size() - framer.GetSynStreamMinimumSize();
size_t compressed_size4 =
visitor->last_frame_len_ - framer.GetSynStreamMinimumSize();
if (IsSpdy2()) {
EXPECT_EQ(139u, uncompressed_size4);
#if defined(USE_SYSTEM_ZLIB)
EXPECT_EQ(149u, compressed_size4);
#else // !defined(USE_SYSTEM_ZLIB)
EXPECT_EQ(101u, compressed_size4);
#endif // !defined(USE_SYSTEM_ZLIB)
} else {
EXPECT_EQ(165u, uncompressed_size4);
#if defined(USE_SYSTEM_ZLIB)
EXPECT_EQ(175u, compressed_size4);
#else // !defined(USE_SYSTEM_ZLIB)
EXPECT_EQ(102u, compressed_size4);
#endif // !defined(USE_SYSTEM_ZLIB)
}
EXPECT_EQ(uncompressed_size1, uncompressed_size2);
EXPECT_EQ(uncompressed_size1, uncompressed_size4);
EXPECT_EQ(compressed_size2, compressed_size4);
// Expect frames 3 & 4 to be the same.
CompareFrames("Uncompressed SYN_STREAM", *frame3, *frame4);
// Expect frames 3 to be the same as a uncompressed frame created
// from scratch.
framer.set_enable_compression(false);
scoped_ptr<SpdyFrame> uncompressed_frame(
framer.CreateSynStream(1, // stream id
0, // associated stream id
1, // priority
0, // credential slot
CONTROL_FLAG_NONE,
false, // compress
&headers));
CompareFrames("Uncompressed SYN_STREAM", *frame3, *uncompressed_frame);
}
TEST_P(SpdyFramerTest, CompressEmptyHeaders) {
// See crbug.com/172383
SpdyHeaderBlock headers;
headers["server"] = "SpdyServer 1.0";
headers["date"] = "Mon 12 Jan 2009 12:12:12 PST";
headers["status"] = "200";
headers["version"] = "HTTP/1.1";
headers["content-type"] = "text/html";
headers["content-length"] = "12";
headers["x-empty-header"] = "";
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(true);
scoped_ptr<SpdyFrame> frame1(
framer.CreateSynStream(1, // stream id
0, // associated stream id
1, // priority
0, // credential slot
CONTROL_FLAG_NONE,
true, // compress
&headers));
}
TEST_P(SpdyFramerTest, Basic) {
const unsigned char kV2Input[] = {
0x80, spdy_version_ch_, 0x00, 0x01, // SYN Stream #1
0x00, 0x00, 0x00, 0x14,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01,
0x00, 0x02, 'h', 'h',
0x00, 0x02, 'v', 'v',
0x80, spdy_version_ch_, 0x00, 0x08, // HEADERS on Stream #1
0x00, 0x00, 0x00, 0x18,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
0x00, 0x02, 'h', '2',
0x00, 0x02, 'v', '2',
0x00, 0x02, 'h', '3',
0x00, 0x02, 'v', '3',
0x00, 0x00, 0x00, 0x01, // DATA on Stream #1
0x00, 0x00, 0x00, 0x0c,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0x80, spdy_version_ch_, 0x00, 0x01, // SYN Stream #3
0x00, 0x00, 0x00, 0x0c,
0x00, 0x00, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, // DATA on Stream #3
0x00, 0x00, 0x00, 0x08,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0x00, 0x00, 0x00, 0x01, // DATA on Stream #1
0x00, 0x00, 0x00, 0x04,
0xde, 0xad, 0xbe, 0xef,
0x80, spdy_version_ch_, 0x00, 0x03, // RST_STREAM on Stream #1
0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, // DATA on Stream #3
0x00, 0x00, 0x00, 0x00,
0x80, spdy_version_ch_, 0x00, 0x03, // RST_STREAM on Stream #3
0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00,
};
const unsigned char kV3Input[] = {
0x80, spdy_version_ch_, 0x00, 0x01, // SYN Stream #1
0x00, 0x00, 0x00, 0x1a,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00,
0x00, 0x02, 'h', 'h',
0x00, 0x00, 0x00, 0x02,
'v', 'v',
0x80, spdy_version_ch_, 0x00, 0x08, // HEADERS on Stream #1
0x00, 0x00, 0x00, 0x20,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x02,
'h', '2',
0x00, 0x00, 0x00, 0x02,
'v', '2', 0x00, 0x00,
0x00, 0x02, 'h', '3',
0x00, 0x00, 0x00, 0x02,
'v', '3',
0x00, 0x00, 0x00, 0x01, // DATA on Stream #1
0x00, 0x00, 0x00, 0x0c,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0x80, spdy_version_ch_, 0x00, 0x01, // SYN Stream #3
0x00, 0x00, 0x00, 0x0e,
0x00, 0x00, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
0x00, 0x00, 0x00, 0x03, // DATA on Stream #3
0x00, 0x00, 0x00, 0x08,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0x00, 0x00, 0x00, 0x01, // DATA on Stream #1
0x00, 0x00, 0x00, 0x04,
0xde, 0xad, 0xbe, 0xef,
0x80, spdy_version_ch_, 0x00, 0x03, // RST_STREAM on Stream #1
0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, // DATA on Stream #3
0x00, 0x00, 0x00, 0x00,
0x80, spdy_version_ch_, 0x00, 0x03, // RST_STREAM on Stream #3
0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00,
};
const unsigned char kV4Input[] = {
0x00, 0x1e, 0x01, 0x00, // SYN_STREAM #1
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00,
0x00, 0x02, 'h', 'h',
0x00, 0x00, 0x00, 0x02,
'v', 'v',
0x00, 0x24, 0x08, 0x00, // HEADERS on Stream #1
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x02,
'h', '2', 0x00, 0x00,
0x00, 0x02, 'v', '2',
0x00, 0x00, 0x00, 0x02,
'h', '3', 0x00, 0x00,
0x00, 0x02, 'v', '3',
0x00, 0x14, 0x00, 0x00, // DATA on Stream #1
0x00, 0x00, 0x00, 0x01,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0x00, 0x12, 0x01, 0x00, // SYN Stream #3
0x00, 0x00, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
0x00, 0x10, 0x00, 0x00, // DATA on Stream #3
0x00, 0x00, 0x00, 0x03,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0x00, 0x0c, 0x00, 0x00, // DATA on Stream #1
0x00, 0x00, 0x00, 0x01,
0xde, 0xad, 0xbe, 0xef,
0x00, 0x0c, 0x03, 0x00, // RST_STREAM on Stream #1
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x08, 0x00, 0x00, // DATA on Stream #3
0x00, 0x00, 0x00, 0x03,
0x00, 0x0c, 0x03, 0x00, // RST_STREAM on Stream #3
0x00, 0x00, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00,
};
TestSpdyVisitor visitor(spdy_version_);
if (IsSpdy2()) {
visitor.SimulateInFramer(kV2Input, sizeof(kV2Input));
} else if (IsSpdy3()) {
visitor.SimulateInFramer(kV3Input, sizeof(kV3Input));
} else {
visitor.SimulateInFramer(kV4Input, sizeof(kV4Input));
}
EXPECT_EQ(0, visitor.error_count_);
EXPECT_EQ(2, visitor.syn_frame_count_);
EXPECT_EQ(0, visitor.syn_reply_frame_count_);
EXPECT_EQ(1, visitor.headers_frame_count_);
EXPECT_EQ(24, visitor.data_bytes_);
EXPECT_EQ(2, visitor.fin_frame_count_);
EXPECT_EQ(0, visitor.fin_flag_count_);
EXPECT_EQ(0, visitor.zero_length_data_frame_count_);
EXPECT_EQ(4, visitor.data_frame_count_);
}
// Test that the FIN flag on a data frame signifies EOF.
TEST_P(SpdyFramerTest, FinOnDataFrame) {
const unsigned char kV2Input[] = {
0x80, spdy_version_ch_, 0x00, 0x01, // SYN Stream #1
0x00, 0x00, 0x00, 0x14,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01,
0x00, 0x02, 'h', 'h',
0x00, 0x02, 'v', 'v',
0x80, spdy_version_ch_, 0x00, 0x02, // SYN REPLY Stream #1
0x00, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01,
0x00, 0x02, 'a', 'a',
0x00, 0x02, 'b', 'b',
0x00, 0x00, 0x00, 0x01, // DATA on Stream #1
0x00, 0x00, 0x00, 0x0c,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0x00, 0x00, 0x00, 0x01, // DATA on Stream #1, with EOF
0x01, 0x00, 0x00, 0x04,
0xde, 0xad, 0xbe, 0xef,
};
const unsigned char kV3Input[] = {
0x80, spdy_version_ch_, 0x00, 0x01, // SYN Stream #1
0x00, 0x00, 0x00, 0x1a,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00,
0x00, 0x02, 'h', 'h',
0x00, 0x00, 0x00, 0x02,
'v', 'v',
0x80, spdy_version_ch_, 0x00, 0x02, // SYN REPLY Stream #1
0x00, 0x00, 0x00, 0x14,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
'a', 'a', 0x00, 0x00,
0x00, 0x02, 'b', 'b',
0x00, 0x00, 0x00, 0x01, // DATA on Stream #1
0x00, 0x00, 0x00, 0x0c,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0x00, 0x00, 0x00, 0x01, // DATA on Stream #1, with EOF
0x01, 0x00, 0x00, 0x04,
0xde, 0xad, 0xbe, 0xef,
};
const unsigned char kV4Input[] = {
0x00, 0x1e, 0x01, 0x00, // SYN_STREAM #1
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00,
0x00, 0x02, 'h', 'h',
0x00, 0x00, 0x00, 0x02,
'v', 'v',
0x00, 0x18, 0x02, 0x00, // SYN REPLY Stream #1
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
'a', 'a', 0x00, 0x00,
0x00, 0x02, 'b', 'b',
0x00, 0x14, 0x00, 0x00, // DATA on Stream #1
0x00, 0x00, 0x00, 0x01,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0x00, 0x0c, 0x00, 0x01, // DATA on Stream #1, with FIN
0x00, 0x00, 0x00, 0x01,
0xde, 0xad, 0xbe, 0xef,
};
TestSpdyVisitor visitor(spdy_version_);
if (IsSpdy2()) {
visitor.SimulateInFramer(kV2Input, sizeof(kV2Input));
} else if (IsSpdy3()) {
visitor.SimulateInFramer(kV3Input, sizeof(kV3Input));
} else {
visitor.SimulateInFramer(kV4Input, sizeof(kV4Input));
}
EXPECT_EQ(0, visitor.error_count_);
EXPECT_EQ(1, visitor.syn_frame_count_);
EXPECT_EQ(1, visitor.syn_reply_frame_count_);
EXPECT_EQ(0, visitor.headers_frame_count_);
EXPECT_EQ(16, visitor.data_bytes_);
EXPECT_EQ(0, visitor.fin_frame_count_);
EXPECT_EQ(0, visitor.fin_flag_count_);
EXPECT_EQ(1, visitor.zero_length_data_frame_count_);
EXPECT_EQ(2, visitor.data_frame_count_);
}
// Test that the FIN flag on a SYN reply frame signifies EOF.
TEST_P(SpdyFramerTest, FinOnSynReplyFrame) {
const unsigned char kV2Input[] = {
0x80, spdy_version_ch_, 0x00, 0x01, // SYN Stream #1
0x00, 0x00, 0x00, 0x14,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01,
0x00, 0x02, 'h', 'h',
0x00, 0x02, 'v', 'v',
0x80, spdy_version_ch_, 0x00, 0x02, // SYN REPLY Stream #1
0x01, 0x00, 0x00, 0x10,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01,
0x00, 0x02, 'a', 'a',
0x00, 0x02, 'b', 'b',
};
const unsigned char kV3Input[] = {
0x80, spdy_version_ch_, 0x00, 0x01, // SYN Stream #1
0x00, 0x00, 0x00, 0x1a,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00,
0x00, 0x02, 'h', 'h',
0x00, 0x00, 0x00, 0x02,
'v', 'v',
0x80, spdy_version_ch_, 0x00, 0x02, // SYN REPLY Stream #1
0x01, 0x00, 0x00, 0x14,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
'a', 'a', 0x00, 0x00,
0x00, 0x02, 'b', 'b',
};
const unsigned char kV4Input[] = {
0x00, 0x1e, 0x01, 0x00, // SYN_STREAM #1
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00,
0x00, 0x02, 'h', 'h',
0x00, 0x00, 0x00, 0x02,
'v', 'v',
0x00, 0x18, 0x02, 0x01, // SYN_REPLY #1, with FIN
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
'a', 'a', 0x00, 0x00,
0x00, 0x02, 'b', 'b',
};
TestSpdyVisitor visitor(spdy_version_);
if (IsSpdy2()) {
visitor.SimulateInFramer(kV2Input, sizeof(kV2Input));
} else if (IsSpdy3()) {
visitor.SimulateInFramer(kV3Input, sizeof(kV3Input));
} else {
visitor.SimulateInFramer(kV4Input, sizeof(kV4Input));
}
EXPECT_EQ(0, visitor.error_count_);
EXPECT_EQ(1, visitor.syn_frame_count_);
EXPECT_EQ(1, visitor.syn_reply_frame_count_);
EXPECT_EQ(0, visitor.headers_frame_count_);
EXPECT_EQ(0, visitor.data_bytes_);
EXPECT_EQ(0, visitor.fin_frame_count_);
EXPECT_EQ(1, visitor.fin_flag_count_);
EXPECT_EQ(1, visitor.zero_length_data_frame_count_);
EXPECT_EQ(0, visitor.data_frame_count_);
}
TEST_P(SpdyFramerTest, HeaderCompression) {
SpdyFramer send_framer(spdy_version_);
SpdyFramer recv_framer(spdy_version_);
send_framer.set_enable_compression(true);
recv_framer.set_enable_compression(true);
const char kHeader1[] = "header1";
const char kHeader2[] = "header2";
const char kHeader3[] = "header3";
const char kValue1[] = "value1";
const char kValue2[] = "value2";
const char kValue3[] = "value3";
// SYN_STREAM #1
SpdyHeaderBlock block;
block[kHeader1] = kValue1;
block[kHeader2] = kValue2;
SpdyControlFlags flags(CONTROL_FLAG_NONE);
SpdySynStreamIR syn_ir_1(1);
syn_ir_1.SetHeader(kHeader1, kValue1);
syn_ir_1.SetHeader(kHeader2, kValue2);
scoped_ptr<SpdyFrame> syn_frame_1(send_framer.SerializeFrame(syn_ir_1));
EXPECT_TRUE(syn_frame_1.get() != NULL);
// SYN_STREAM #2
block[kHeader3] = kValue3;
scoped_ptr<SpdyFrame> syn_frame_2(
send_framer.CreateSynStream(3, // stream id
0, // associated stream id
0, // priority
0, // credential slot
flags,
true, // compress
&block));
EXPECT_TRUE(syn_frame_2.get() != NULL);
// Now start decompressing
scoped_ptr<SpdyFrame> decompressed;
scoped_ptr<SpdyFrame> uncompressed;
base::StringPiece serialized_headers;
SpdyHeaderBlock decompressed_headers;
// Decompress SYN_STREAM #1
decompressed.reset(SpdyFramerTestUtil::DecompressFrame(
&recv_framer, *syn_frame_1.get()));
EXPECT_TRUE(decompressed.get() != NULL);
serialized_headers = GetSerializedHeaders(decompressed.get(), send_framer);
EXPECT_TRUE(recv_framer.ParseHeaderBlockInBuffer(serialized_headers.data(),
serialized_headers.size(),
&decompressed_headers));
EXPECT_EQ(2u, decompressed_headers.size());
EXPECT_EQ(kValue1, decompressed_headers[kHeader1]);
EXPECT_EQ(kValue2, decompressed_headers[kHeader2]);
// Decompress SYN_STREAM #2
decompressed.reset(SpdyFramerTestUtil::DecompressFrame(
&recv_framer, *syn_frame_2.get()));
EXPECT_TRUE(decompressed.get() != NULL);
serialized_headers = GetSerializedHeaders(decompressed.get(), send_framer);
decompressed_headers.clear();
EXPECT_TRUE(recv_framer.ParseHeaderBlockInBuffer(serialized_headers.data(),
serialized_headers.size(),
&decompressed_headers));
EXPECT_EQ(3u, decompressed_headers.size());
EXPECT_EQ(kValue1, decompressed_headers[kHeader1]);
EXPECT_EQ(kValue2, decompressed_headers[kHeader2]);
EXPECT_EQ(kValue3, decompressed_headers[kHeader3]);
}
// Verify we don't leak when we leave streams unclosed
TEST_P(SpdyFramerTest, UnclosedStreamDataCompressors) {
SpdyFramer send_framer(spdy_version_);
send_framer.set_enable_compression(true);
const char kHeader1[] = "header1";
const char kHeader2[] = "header2";
const char kValue1[] = "value1";
const char kValue2[] = "value2";
SpdyHeaderBlock block;
block[kHeader1] = kValue1;
block[kHeader2] = kValue2;
SpdyControlFlags flags(CONTROL_FLAG_NONE);
scoped_ptr<SpdyFrame> syn_frame(
send_framer.CreateSynStream(1, // stream id
0, // associated stream id
0, // priority
0, // credential slot
flags,
true, // compress
&block));
EXPECT_TRUE(syn_frame.get() != NULL);
const char bytes[] = "this is a test test test test test!";
scoped_ptr<SpdyFrame> send_frame(
send_framer.CreateDataFrame(
1, bytes, arraysize(bytes),
static_cast<SpdyDataFlags>(DATA_FLAG_FIN)));
EXPECT_TRUE(send_frame.get() != NULL);
// Run the inputs through the framer.
TestSpdyVisitor visitor(spdy_version_);
visitor.use_compression_ = true;
const unsigned char* data;
data = reinterpret_cast<const unsigned char*>(syn_frame->data());
visitor.SimulateInFramer(data, syn_frame->size());
data = reinterpret_cast<const unsigned char*>(send_frame->data());
visitor.SimulateInFramer(data, send_frame->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(arraysize(bytes), static_cast<unsigned>(visitor.data_bytes_));
EXPECT_EQ(0, visitor.fin_frame_count_);
EXPECT_EQ(0, visitor.fin_flag_count_);
EXPECT_EQ(1, visitor.zero_length_data_frame_count_);
EXPECT_EQ(1, visitor.data_frame_count_);
}
// Verify we can decompress the stream even if handed over to the
// framer 1 byte at a time.
TEST_P(SpdyFramerTest, UnclosedStreamDataCompressorsOneByteAtATime) {
SpdyFramer send_framer(spdy_version_);
send_framer.set_enable_compression(true);
const char kHeader1[] = "header1";
const char kHeader2[] = "header2";
const char kValue1[] = "value1";
const char kValue2[] = "value2";
SpdyHeaderBlock block;
block[kHeader1] = kValue1;
block[kHeader2] = kValue2;
SpdyControlFlags flags(CONTROL_FLAG_NONE);
scoped_ptr<SpdyFrame> syn_frame(
send_framer.CreateSynStream(1, // stream id
0, // associated stream id
0, // priority
0, // credential slot
flags,
true, // compress
&block));
EXPECT_TRUE(syn_frame.get() != NULL);
const char bytes[] = "this is a test test test test test!";
scoped_ptr<SpdyFrame> send_frame(
send_framer.CreateDataFrame(
1, bytes, arraysize(bytes),
static_cast<SpdyDataFlags>(DATA_FLAG_FIN)));
EXPECT_TRUE(send_frame.get() != NULL);
// Run the inputs through the framer.
TestSpdyVisitor visitor(spdy_version_);
visitor.use_compression_ = true;
const unsigned char* data;
data = reinterpret_cast<const unsigned char*>(syn_frame->data());
for (size_t idx = 0; idx < syn_frame->size(); ++idx) {
visitor.SimulateInFramer(data + idx, 1);
ASSERT_EQ(0, visitor.error_count_);
}
data = reinterpret_cast<const unsigned char*>(send_frame->data());
for (size_t idx = 0; idx < send_frame->size(); ++idx) {
visitor.SimulateInFramer(data + idx, 1);
ASSERT_EQ(0, visitor.error_count_);
}
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(arraysize(bytes), static_cast<unsigned>(visitor.data_bytes_));
EXPECT_EQ(0, visitor.fin_frame_count_);
EXPECT_EQ(0, visitor.fin_flag_count_);
EXPECT_EQ(1, visitor.zero_length_data_frame_count_);
EXPECT_EQ(1, visitor.data_frame_count_);
}
TEST_P(SpdyFramerTest, WindowUpdateFrame) {
SpdyFramer framer(spdy_version_);
scoped_ptr<SpdyFrame> frame(framer.CreateWindowUpdate(1, 0x12345678));
const char kDescription[] = "WINDOW_UPDATE frame, stream 1, delta 0x12345678";
const unsigned char kV3FrameData[] = { // Also applies for V2.
0x80, spdy_version_ch_, 0x00, 0x09,
0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x01,
0x12, 0x34, 0x56, 0x78
};
const unsigned char kV4FrameData[] = {
0x00, 0x0c, 0x09, 0x00,
0x00, 0x00, 0x00, 0x01,
0x12, 0x34, 0x56, 0x78
};
if (IsSpdy4()) {
CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
} else {
CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
}
}
TEST_P(SpdyFramerTest, CreateDataFrame) {
SpdyFramer framer(spdy_version_);
{
const char kDescription[] = "'hello' data frame, no FIN";
const unsigned char kV3FrameData[] = { // Also applies for V2.
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x05,
'h', 'e', 'l', 'l',
'o'
};
const unsigned char kV4FrameData[] = {
0x00, 0x0d, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01,
'h', 'e', 'l', 'l',
'o'
};
const char bytes[] = "hello";
scoped_ptr<SpdyFrame> frame(framer.CreateDataFrame(
1, bytes, strlen(bytes), DATA_FLAG_NONE));
if (IsSpdy4()) {
CompareFrame(
kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
} else {
CompareFrame(
kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
}
SpdyDataIR data_ir(1);
data_ir.SetDataShallow(base::StringPiece(bytes, strlen(bytes)));
frame.reset(framer.SerializeDataFrameHeader(data_ir));
CompareCharArraysWithHexError(
kDescription,
reinterpret_cast<const unsigned char*>(frame->data()),
framer.GetDataFrameMinimumSize(),
IsSpdy4() ? kV4FrameData : kV3FrameData,
framer.GetDataFrameMinimumSize());
}
{
const char kDescription[] = "Data frame with negative data byte, no FIN";
const unsigned char kV3FrameData[] = { // Also applies for V2.
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01,
0xff
};
const unsigned char kV4FrameData[] = {
0x00, 0x09, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01,
0xff
};
scoped_ptr<SpdyFrame> frame(framer.CreateDataFrame(
1, "\xff", 1, DATA_FLAG_NONE));
if (IsSpdy4()) {
CompareFrame(
kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
} else {
CompareFrame(
kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
}
}
{
const char kDescription[] = "'hello' data frame, with FIN";
const unsigned char kV3FrameData[] = { // Also applies for V2.
0x00, 0x00, 0x00, 0x01,
0x01, 0x00, 0x00, 0x05,
'h', 'e', 'l', 'l',
'o'
};
const unsigned char kV4FrameData[] = {
0x00, 0x0d, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01,
'h', 'e', 'l', 'l',
'o'
};
scoped_ptr<SpdyFrame> frame(framer.CreateDataFrame(
1, "hello", 5, DATA_FLAG_FIN));
if (IsSpdy4()) {
CompareFrame(
kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
} else {
CompareFrame(
kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
}
}
{
const char kDescription[] = "Empty data frame";
const unsigned char kV3FrameData[] = { // Also applies for V2.
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
};
const unsigned char kV4FrameData[] = {
0x00, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01,
};
scoped_ptr<SpdyFrame> frame(framer.CreateDataFrame(
1, "", 0, DATA_FLAG_NONE));
if (IsSpdy4()) {
CompareFrame(
kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
} else {
CompareFrame(
kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
}
}
{
const char kDescription[] = "Data frame with max stream ID";
const unsigned char kV3FrameData[] = { // Also applies for V2.
0x7f, 0xff, 0xff, 0xff,
0x01, 0x00, 0x00, 0x05,
'h', 'e', 'l', 'l',
'o'
};
const unsigned char kV4FrameData[] = {
0x00, 0x0d, 0x00, 0x01,
0x7f, 0xff, 0xff, 0xff,
'h', 'e', 'l', 'l',
'o'
};
scoped_ptr<SpdyFrame> frame(framer.CreateDataFrame(
0x7fffffff, "hello", 5, DATA_FLAG_FIN));
if (IsSpdy4()) {
CompareFrame(
kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
} else {
CompareFrame(
kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
}
}
if (!IsSpdy4()) {
// This test does not apply to SPDY 4 because the max frame size is smaller
// than 4MB.
const char kDescription[] = "Large data frame";
const int kDataSize = 4 * 1024 * 1024; // 4 MB
const string kData(kDataSize, 'A');
const unsigned char kFrameHeader[] = {
0x00, 0x00, 0x00, 0x01,
0x01, 0x40, 0x00, 0x00,
};
const int kFrameSize = arraysize(kFrameHeader) + kDataSize;
scoped_ptr<unsigned char[]> expected_frame_data(
new unsigned char[kFrameSize]);
memcpy(expected_frame_data.get(), kFrameHeader, arraysize(kFrameHeader));
memset(expected_frame_data.get() + arraysize(kFrameHeader), 'A', kDataSize);
scoped_ptr<SpdyFrame> frame(framer.CreateDataFrame(
1, kData.data(), kData.size(), DATA_FLAG_FIN));
CompareFrame(kDescription, *frame, expected_frame_data.get(), kFrameSize);
}
}
TEST_P(SpdyFramerTest, CreateSynStreamUncompressed) {
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(false);
{
const char kDescription[] = "SYN_STREAM frame, lowest pri, slot 2, no FIN";
SpdyHeaderBlock headers;
headers["bar"] = "foo";
headers["foo"] = "bar";
const unsigned char kPri = IsSpdy2() ? 0xC0 : 0xE0;
const unsigned char kCre = IsSpdy2() ? 0 : 2;
const unsigned char kV2FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x01,
0x00, 0x00, 0x00, 0x20,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
kPri, 0x00, 0x00, 0x02,
0x00, 0x03, 'b', 'a',
'r', 0x00, 0x03, 'f',
'o', 'o', 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x03, 'b', 'a', 'r'
};
const unsigned char kV3FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x01,
0x00, 0x00, 0x00, 0x2a,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
kPri, kCre, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00,
0x00, 0x03, 'b', 'a',
'r', 0x00, 0x00, 0x00,
0x03, 'f', 'o', 'o',
0x00, 0x00, 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x00, 0x00, 0x03, 'b',
'a', 'r'
};
const unsigned char kV4FrameData[] = {
0x00, 0x2e, 0x01, 0x00,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
kPri, kCre, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00,
0x00, 0x03, 'b', 'a',
'r', 0x00, 0x00, 0x00,
0x03, 'f', 'o', 'o',
0x00, 0x00, 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x00, 0x00, 0x03, 'b',
'a', 'r'
};
scoped_ptr<SpdyFrame> frame(
framer.CreateSynStream(1, // stream id
0, // associated stream id
framer.GetLowestPriority(),
kCre, // credential slot
CONTROL_FLAG_NONE,
false, // compress
&headers));
if (IsSpdy2()) {
CompareFrame(kDescription, *frame, kV2FrameData, arraysize(kV2FrameData));
} else if (IsSpdy3()) {
CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
} else {
CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
}
}
{
const char kDescription[] =
"SYN_STREAM frame with a 0-length header name, highest pri, FIN, "
"max stream ID";
SpdyHeaderBlock headers;
headers[std::string()] = "foo";
headers["foo"] = "bar";
const unsigned char kV2FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x01,
0x01, 0x00, 0x00, 0x1D,
0x7f, 0xff, 0xff, 0xff,
0x7f, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x03, 'f', 'o', 'o',
0x00, 0x03, 'b', 'a',
'r'
};
const unsigned char kV3FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x01,
0x01, 0x00, 0x00, 0x27,
0x7f, 0xff, 0xff, 0xff,
0x7f, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 'f', 'o',
'o', 0x00, 0x00, 0x00,
0x03, 'f', 'o', 'o',
0x00, 0x00, 0x00, 0x03,
'b', 'a', 'r'
};
const unsigned char kV4FrameData[] = {
0x00, 0x2b, 0x01, 0x01,
0x7f, 0xff, 0xff, 0xff,
0x7f, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 'f', 'o',
'o', 0x00, 0x00, 0x00,
0x03, 'f', 'o', 'o',
0x00, 0x00, 0x00, 0x03,
'b', 'a', 'r'
};
scoped_ptr<SpdyFrame> frame(
framer.CreateSynStream(0x7fffffff, // stream id
0x7fffffff, // associated stream id
framer.GetHighestPriority(),
0, // credential slot
CONTROL_FLAG_FIN,
false, // compress
&headers));
if (IsSpdy2()) {
CompareFrame(kDescription, *frame, kV2FrameData, arraysize(kV2FrameData));
} else if (IsSpdy3()) {
CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
} else {
CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
}
}
{
const char kDescription[] =
"SYN_STREAM frame with a 0-length header val, high pri, FIN, "
"max stream ID";
SpdyHeaderBlock headers;
headers["bar"] = "foo";
headers["foo"] = "";
const unsigned char kPri = IsSpdy2() ? 0x40 : 0x20;
const unsigned char kV2FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x01,
0x01, 0x00, 0x00, 0x1D,
0x7f, 0xff, 0xff, 0xff,
0x7f, 0xff, 0xff, 0xff,
kPri, 0x00, 0x00, 0x02,
0x00, 0x03, 'b', 'a',
'r', 0x00, 0x03, 'f',
'o', 'o', 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x00
};
const unsigned char kV3FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x01,
0x01, 0x00, 0x00, 0x27,
0x7f, 0xff, 0xff, 0xff,
0x7f, 0xff, 0xff, 0xff,
kPri, 0x00, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00,
0x00, 0x03, 'b', 'a',
'r', 0x00, 0x00, 0x00,
0x03, 'f', 'o', 'o',
0x00, 0x00, 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x00, 0x00, 0x00
};
const unsigned char kV4FrameData[] = {
0x00, 0x2b, 0x01, 0x01,
0x7f, 0xff, 0xff, 0xff,
0x7f, 0xff, 0xff, 0xff,
kPri, 0x00, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00,
0x00, 0x03, 'b', 'a',
'r', 0x00, 0x00, 0x00,
0x03, 'f', 'o', 'o',
0x00, 0x00, 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x00, 0x00, 0x00
};
scoped_ptr<SpdyFrame> frame(
framer.CreateSynStream(0x7fffffff, // stream id
0x7fffffff, // associated stream id
1, // priority
0, // credential slot
CONTROL_FLAG_FIN,
false, // compress
&headers));
if (IsSpdy2()) {
CompareFrame(kDescription, *frame, kV2FrameData, arraysize(kV2FrameData));
} else if (IsSpdy3()) {
CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
} else {
CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
}
}
}
// TODO(phajdan.jr): Clean up after we no longer need
// to workaround http://crbug.com/139744.
#if !defined(USE_SYSTEM_ZLIB)
TEST_P(SpdyFramerTest, CreateSynStreamCompressed) {
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(true);
{
const char kDescription[] =
"SYN_STREAM frame, low pri, no FIN";
SpdyHeaderBlock headers;
headers["bar"] = "foo";
headers["foo"] = "bar";
const SpdyPriority priority = IsSpdy2() ? 2 : 4;
const unsigned char kV2FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x01,
0x00, 0x00, 0x00, 0x36,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x38, 0xea,
0xdf, 0xa2, 0x51, 0xb2,
0x62, 0x60, 0x62, 0x60,
0x4e, 0x4a, 0x2c, 0x62,
0x60, 0x06, 0x08, 0xa0,
0xb4, 0xfc, 0x7c, 0x80,
0x00, 0x62, 0x60, 0x4e,
0xcb, 0xcf, 0x67, 0x60,
0x06, 0x08, 0xa0, 0xa4,
0xc4, 0x22, 0x80, 0x00,
0x02, 0x00, 0x00, 0x00,
0xff, 0xff,
};
const unsigned char kV3FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x01,
0x00, 0x00, 0x00, 0x37,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x38, 0xEA,
0xE3, 0xC6, 0xA7, 0xC2,
0x02, 0xE5, 0x0E, 0x50,
0xC2, 0x4B, 0x4A, 0x04,
0xE5, 0x0B, 0x66, 0x80,
0x00, 0x4A, 0xCB, 0xCF,
0x07, 0x08, 0x20, 0x10,
0x95, 0x96, 0x9F, 0x0F,
0xA2, 0x00, 0x02, 0x28,
0x29, 0xB1, 0x08, 0x20,
0x80, 0x00, 0x00, 0x00,
0x00, 0xFF, 0xFF,
};
const unsigned char kV4FrameData[] = {
0x00, 0x3b, 0x01, 0x00,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x38, 0xea,
0xe3, 0xc6, 0xa7, 0xc2,
0x02, 0xe5, 0x0e, 0x50,
0xc2, 0x4b, 0x4a, 0x04,
0xe5, 0x0b, 0x66, 0x80,
0x00, 0x4a, 0xcb, 0xcf,
0x07, 0x08, 0x20, 0x10,
0x95, 0x96, 0x9f, 0x0f,
0xa2, 0x00, 0x02, 0x28,
0x29, 0xb1, 0x08, 0x20,
0x80, 0x00, 0x00, 0x00,
0x00, 0xff, 0xff,
};
scoped_ptr<SpdyFrame> frame(
framer.CreateSynStream(1, // stream id
0, // associated stream id
priority,
0, // credential slot
CONTROL_FLAG_NONE,
true, // compress
&headers));
if (IsSpdy2()) {
CompareFrame(kDescription, *frame, kV2FrameData, arraysize(kV2FrameData));
} else if (IsSpdy3()) {
CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
} else {
CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
}
}
}
#endif // !defined(USE_SYSTEM_ZLIB)
TEST_P(SpdyFramerTest, CreateSynReplyUncompressed) {
SpdyFramer framer(spdy_version_);
framer.set_enable_compression(false);
{
const char kDescription[] = "SYN_REPLY frame, no FIN";
SpdyHeaderBlock headers;
headers["bar"] = "foo";
headers["foo"] = "bar";
const unsigned char kV2FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x02,
0x00, 0x00, 0x00, 0x1C,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
0x00, 0x03, 'b', 'a',
'r', 0x00, 0x03, 'f',
'o', 'o', 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x03, 'b', 'a', 'r'
};
const unsigned char kV3FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x02,
0x00, 0x00, 0x00, 0x24,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x03,
'b', 'a', 'r', 0x00,
0x00, 0x00, 0x03, 'f',
'o', 'o', 0x00, 0x00,
0x00, 0x03, 'f', 'o',
'o', 0x00, 0x00, 0x00,
0x03, 'b', 'a', 'r'
};
const unsigned char kV4FrameData[] = {
0x00, 0x28, 0x02, 0x00,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x03,
'b', 'a', 'r', 0x00,
0x00, 0x00, 0x03, 'f',
'o', 'o', 0x00, 0x00,
0x00, 0x03, 'f', 'o',
'o', 0x00, 0x00, 0x00,
0x03, 'b', 'a', 'r'
};
scoped_ptr<SpdyFrame> frame(framer.CreateSynReply(
1, CONTROL_FLAG_NONE, false, &headers));
if (IsSpdy2()) {
CompareFrame(kDescription, *frame, kV2FrameData, arraysize(kV2FrameData));
} else if (IsSpdy3()) {
CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
} else {
CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
}
}
{
const char kDescription[] =
"SYN_REPLY frame with a 0-length header name, FIN, max stream ID";
SpdyHeaderBlock headers;
headers[std::string()] = "foo";
headers["foo"] = "bar";
const unsigned char kV2FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x02,
0x01, 0x00, 0x00, 0x19,
0x7f, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x03, 'f', 'o', 'o',
0x00, 0x03, 'b', 'a',
'r'
};
const unsigned char kV3FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x02,
0x01, 0x00, 0x00, 0x21,
0x7f, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x00, 0x00, 0x03, 'f',
'o', 'o', 0x00, 0x00,
0x00, 0x03, 'b', 'a',
'r'
};
const unsigned char kV4FrameData[] = {
0x00, 0x25, 0x02, 0x01,
0x7f, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x00, 0x00, 0x03, 'f',
'o', 'o', 0x00, 0x00,
0x00, 0x03, 'b', 'a',
'r'
};
scoped_ptr<SpdyFrame> frame(framer.CreateSynReply(
0x7fffffff, CONTROL_FLAG_FIN, false, &headers));
if (IsSpdy2()) {
CompareFrame(kDescription, *frame, kV2FrameData, arraysize(kV2FrameData));
} else if (IsSpdy3()) {
CompareFrame(kDescription, *frame, kV3FrameData, arraysize(kV3FrameData));
} else {
CompareFrame(kDescription, *frame, kV4FrameData, arraysize(kV4FrameData));
}
}
{
const char kDescription[] =
"SYN_REPLY frame with a 0-length header val, FIN, max stream ID";
SpdyHeaderBlock headers;
headers["bar"] = "foo";
headers["foo"] = "";
const unsigned char kV2FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x02,
0x01, 0x00, 0x00, 0x19,
0x7f, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x02,
0x00, 0x03, 'b', 'a',
'r', 0x00, 0x03, 'f',
'o', 'o', 0x00, 0x03,
'f', 'o', 'o', 0x00,
0x00
};
const unsigned char kV3FrameData[] = {
0x80, spdy_version_ch_, 0x00, 0x02,
0x01, 0x00, 0x00, 0x21,
0x7f,