This source file includes following definitions.
- GenerateNulMaskingKey
- GenerateNonNulMaskingKey
- strict_mode_
- expect_all_io_to_complete_
- MakeTransportSocket
- SetHttpReadBuffer
- CreateStream
- CreateReadOnly
- CreateNullStream
- CreateRead
- CreateChunkedRead
- SetUp
- PrepareWriteFrame
- CreateWriteOnly
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "net/websockets/websocket_basic_stream.h"
#include <string.h>
#include <string>
#include "base/basictypes.h"
#include "base/big_endian.h"
#include "base/port.h"
#include "net/base/capturing_net_log.h"
#include "net/base/test_completion_callback.h"
#include "net/socket/socket_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
#define WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(name, value) \
const char k##name[] = value; \
const size_t k##name##Size = arraysize(k##name) - 1;
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(SampleFrame, "\x81\x06Sample");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(
PartialLargeFrame,
"\x81\x7F\x00\x00\x00\x00\x7F\xFF\xFF\xFF"
"chromiunum ad pasco per loca insanis pullum manducat frumenti");
const size_t kLargeFrameHeaderSize = 10;
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(MultipleFrames,
"\x81\x01X\x81\x01Y\x81\x01Z");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(EmptyFirstFrame, "\x01\x00");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(EmptyMiddleFrame, "\x00\x00");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(EmptyFinalTextFrame, "\x81\x00");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(EmptyFinalContinuationFrame,
"\x80\x00");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(ValidPong, "\x8A\x00");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(InvalidFrame,
"\x81\x7E\x00\x07Invalid");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(PingFrameWithoutFin, "\x09\x00");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(
126BytePong,
"\x8a\x7e\x00\x7eZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(CloseFrame,
"\x88\x09\x03\xe8occludo");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(WriteFrame,
"\x81\x85\x00\x00\x00\x00Write");
WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(MaskedEmptyPong,
"\x8A\x80\x00\x00\x00\x00");
const WebSocketMaskingKey kNulMaskingKey = {{'\0', '\0', '\0', '\0'}};
const WebSocketMaskingKey kNonNulMaskingKey = {
{'\x0d', '\x1b', '\x06', '\x17'}};
WebSocketMaskingKey GenerateNulMaskingKey() { return kNulMaskingKey; }
WebSocketMaskingKey GenerateNonNulMaskingKey() { return kNonNulMaskingKey; }
class WebSocketBasicStreamTest : public ::testing::Test {
protected:
scoped_ptr<WebSocketBasicStream> stream_;
CapturingNetLog net_log_;
};
class StrictStaticSocketDataProvider : public StaticSocketDataProvider {
public:
StrictStaticSocketDataProvider(MockRead* reads,
size_t reads_count,
MockWrite* writes,
size_t writes_count,
bool strict_mode)
: StaticSocketDataProvider(reads, reads_count, writes, writes_count),
strict_mode_(strict_mode) {}
virtual ~StrictStaticSocketDataProvider() {
if (strict_mode_) {
EXPECT_EQ(read_count(), read_index());
EXPECT_EQ(write_count(), write_index());
}
}
private:
const bool strict_mode_;
};
class WebSocketBasicStreamSocketTest : public WebSocketBasicStreamTest {
protected:
WebSocketBasicStreamSocketTest()
: histograms_("a"),
pool_(1, 1, &histograms_, &factory_),
generator_(&GenerateNulMaskingKey),
expect_all_io_to_complete_(true) {}
virtual ~WebSocketBasicStreamSocketTest() {
stream_.reset();
}
scoped_ptr<ClientSocketHandle> MakeTransportSocket(MockRead reads[],
size_t reads_count,
MockWrite writes[],
size_t writes_count) {
socket_data_.reset(new StrictStaticSocketDataProvider(
reads, reads_count, writes, writes_count, expect_all_io_to_complete_));
socket_data_->set_connect_data(MockConnect(SYNCHRONOUS, OK));
factory_.AddSocketDataProvider(socket_data_.get());
scoped_ptr<ClientSocketHandle> transport_socket(new ClientSocketHandle);
scoped_refptr<MockTransportSocketParams> params;
transport_socket->Init("a",
params,
MEDIUM,
CompletionCallback(),
&pool_,
bound_net_log_.bound());
return transport_socket.Pass();
}
void SetHttpReadBuffer(const char* data, size_t size) {
http_read_buffer_ = new GrowableIOBuffer;
http_read_buffer_->SetCapacity(size);
memcpy(http_read_buffer_->data(), data, size);
http_read_buffer_->set_offset(size);
}
void CreateStream(MockRead reads[],
size_t reads_count,
MockWrite writes[],
size_t writes_count) {
stream_ = WebSocketBasicStream::CreateWebSocketBasicStreamForTesting(
MakeTransportSocket(reads, reads_count, writes, writes_count),
http_read_buffer_,
sub_protocol_,
extensions_,
generator_);
}
template <size_t N>
void CreateReadOnly(MockRead (&reads)[N]) {
CreateStream(reads, N, NULL, 0);
}
void CreateNullStream() { CreateStream(NULL, 0, NULL, 0); }
scoped_ptr<SocketDataProvider> socket_data_;
MockClientSocketFactory factory_;
ClientSocketPoolHistograms histograms_;
MockTransportClientSocketPool pool_;
CapturingBoundNetLog(bound_net_log_);
ScopedVector<WebSocketFrame> frames_;
TestCompletionCallback cb_;
scoped_refptr<GrowableIOBuffer> http_read_buffer_;
std::string sub_protocol_;
std::string extensions_;
WebSocketBasicStream::WebSocketMaskingKeyGeneratorFunction generator_;
bool expect_all_io_to_complete_;
};
class WebSocketBasicStreamSocketSingleReadTest
: public WebSocketBasicStreamSocketTest {
protected:
void CreateRead(const MockRead& read) {
reads_[0] = read;
CreateStream(reads_, 1U, NULL, 0);
}
MockRead reads_[1];
};
class WebSocketBasicStreamSocketChunkedReadTest
: public WebSocketBasicStreamSocketTest {
protected:
enum LastFrameBehaviour {
LAST_FRAME_BIG,
LAST_FRAME_NOT_BIG
};
void CreateChunkedRead(IoMode mode,
const char data[],
size_t data_size,
int chunk_size,
int number_of_chunks,
LastFrameBehaviour last_frame_behaviour) {
reads_.reset(new MockRead[number_of_chunks]);
const char* start = data;
for (int i = 0; i < number_of_chunks; ++i) {
int len = chunk_size;
const bool is_last_chunk = (i == number_of_chunks - 1);
if ((last_frame_behaviour == LAST_FRAME_BIG && is_last_chunk) ||
static_cast<int>(data + data_size - start) < len) {
len = static_cast<int>(data + data_size - start);
}
reads_[i] = MockRead(mode, start, len);
start += len;
}
CreateStream(reads_.get(), number_of_chunks, NULL, 0);
}
scoped_ptr<MockRead[]> reads_;
};
class WebSocketBasicStreamSocketWriteTest
: public WebSocketBasicStreamSocketTest {
protected:
virtual void SetUp() OVERRIDE { PrepareWriteFrame(); }
void PrepareWriteFrame() {
scoped_ptr<WebSocketFrame> frame(
new WebSocketFrame(WebSocketFrameHeader::kOpCodeText));
const size_t payload_size =
kWriteFrameSize - (WebSocketFrameHeader::kBaseHeaderSize +
WebSocketFrameHeader::kMaskingKeyLength);
frame->data = new IOBuffer(payload_size);
memcpy(frame->data->data(),
kWriteFrame + kWriteFrameSize - payload_size,
payload_size);
WebSocketFrameHeader& header = frame->header;
header.final = true;
header.masked = true;
header.payload_length = payload_size;
frames_.push_back(frame.release());
}
template <size_t N>
void CreateWriteOnly(MockWrite (&writes)[N]) {
CreateStream(NULL, 0, writes, N);
}
};
TEST_F(WebSocketBasicStreamSocketTest, ConstructionWorks) {
CreateNullStream();
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, SyncReadWorks) {
CreateRead(MockRead(SYNCHRONOUS, kSampleFrame, kSampleFrameSize));
int result = stream_->ReadFrames(&frames_, cb_.callback());
EXPECT_EQ(OK, result);
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length);
EXPECT_TRUE(frames_[0]->header.final);
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, AsyncReadWorks) {
CreateRead(MockRead(ASYNC, kSampleFrame, kSampleFrameSize));
int result = stream_->ReadFrames(&frames_, cb_.callback());
ASSERT_EQ(ERR_IO_PENDING, result);
EXPECT_EQ(OK, cb_.WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length);
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, HeaderFragmentedSync) {
CreateChunkedRead(
SYNCHRONOUS, kSampleFrame, kSampleFrameSize, 1, 2, LAST_FRAME_BIG);
int result = stream_->ReadFrames(&frames_, cb_.callback());
EXPECT_EQ(OK, result);
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length);
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, HeaderFragmentedAsync) {
CreateChunkedRead(
ASYNC, kSampleFrame, kSampleFrameSize, 1, 2, LAST_FRAME_BIG);
int result = stream_->ReadFrames(&frames_, cb_.callback());
ASSERT_EQ(ERR_IO_PENDING, result);
EXPECT_EQ(OK, cb_.WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length);
}
TEST_F(WebSocketBasicStreamSocketTest, HeaderFragmentedSyncAsync) {
MockRead reads[] = {MockRead(SYNCHRONOUS, kSampleFrame, 1),
MockRead(ASYNC, kSampleFrame + 1, kSampleFrameSize - 1)};
CreateReadOnly(reads);
int result = stream_->ReadFrames(&frames_, cb_.callback());
ASSERT_EQ(ERR_IO_PENDING, result);
EXPECT_EQ(OK, cb_.WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length);
}
TEST_F(WebSocketBasicStreamSocketTest, FragmentedLargeHeader) {
MockRead reads[] = {
MockRead(SYNCHRONOUS, kPartialLargeFrame, kLargeFrameHeaderSize - 1),
MockRead(SYNCHRONOUS, ERR_IO_PENDING)};
CreateReadOnly(reads);
EXPECT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, LargeFrameFirstChunk) {
CreateRead(MockRead(SYNCHRONOUS, kPartialLargeFrame, kPartialLargeFrameSize));
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(1U, frames_.size());
EXPECT_FALSE(frames_[0]->header.final);
EXPECT_EQ(kPartialLargeFrameSize - kLargeFrameHeaderSize,
static_cast<size_t>(frames_[0]->header.payload_length));
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, HeaderOnlyChunk) {
CreateRead(MockRead(SYNCHRONOUS, kPartialLargeFrame, kLargeFrameHeaderSize));
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(NULL, frames_[0]->data.get());
EXPECT_EQ(0U, frames_[0]->header.payload_length);
EXPECT_EQ(WebSocketFrameHeader::kOpCodeText, frames_[0]->header.opcode);
}
TEST_F(WebSocketBasicStreamSocketTest, HeaderBodySeparated) {
MockRead reads[] = {
MockRead(SYNCHRONOUS, kPartialLargeFrame, kLargeFrameHeaderSize),
MockRead(ASYNC,
kPartialLargeFrame + kLargeFrameHeaderSize,
kPartialLargeFrameSize - kLargeFrameHeaderSize)};
CreateReadOnly(reads);
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(NULL, frames_[0]->data.get());
EXPECT_EQ(WebSocketFrameHeader::kOpCodeText, frames_[0]->header.opcode);
frames_.clear();
EXPECT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(OK, cb_.WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(kPartialLargeFrameSize - kLargeFrameHeaderSize,
frames_[0]->header.payload_length);
EXPECT_EQ(WebSocketFrameHeader::kOpCodeContinuation,
frames_[0]->header.opcode);
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, LargeFrameTwoChunks) {
const size_t kChunkSize = 16;
CreateChunkedRead(ASYNC,
kPartialLargeFrame,
kPartialLargeFrameSize,
kChunkSize,
2,
LAST_FRAME_NOT_BIG);
TestCompletionCallback cb[2];
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb[0].callback()));
EXPECT_EQ(OK, cb[0].WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(kChunkSize - kLargeFrameHeaderSize,
frames_[0]->header.payload_length);
frames_.clear();
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb[1].callback()));
EXPECT_EQ(OK, cb[1].WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(kChunkSize, frames_[0]->header.payload_length);
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, OnlyFinalChunkIsFinal) {
static const size_t kFirstChunkSize = 4;
CreateChunkedRead(ASYNC,
kSampleFrame,
kSampleFrameSize,
kFirstChunkSize,
2,
LAST_FRAME_BIG);
TestCompletionCallback cb[2];
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb[0].callback()));
EXPECT_EQ(OK, cb[0].WaitForResult());
ASSERT_EQ(1U, frames_.size());
ASSERT_FALSE(frames_[0]->header.final);
frames_.clear();
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb[1].callback()));
EXPECT_EQ(OK, cb[1].WaitForResult());
ASSERT_EQ(1U, frames_.size());
ASSERT_TRUE(frames_[0]->header.final);
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, ContinuationOpCodeUsed) {
const size_t kFirstChunkSize = 3;
const int kChunkCount = 3;
CreateChunkedRead(ASYNC,
kSampleFrame,
kSampleFrameSize,
kFirstChunkSize,
kChunkCount,
LAST_FRAME_BIG);
TestCompletionCallback cb[kChunkCount];
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb[0].callback()));
EXPECT_EQ(OK, cb[0].WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(WebSocketFrameHeader::kOpCodeText, frames_[0]->header.opcode);
for (int i = 1; i < kChunkCount; ++i) {
frames_.clear();
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb[i].callback()));
EXPECT_EQ(OK, cb[i].WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(WebSocketFrameHeader::kOpCodeContinuation,
frames_[0]->header.opcode);
}
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, ThreeFramesTogether) {
CreateRead(MockRead(SYNCHRONOUS, kMultipleFrames, kMultipleFramesSize));
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(3U, frames_.size());
EXPECT_TRUE(frames_[0]->header.final);
EXPECT_TRUE(frames_[1]->header.final);
EXPECT_TRUE(frames_[2]->header.final);
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, SyncClose) {
CreateRead(MockRead(SYNCHRONOUS, "", 0));
EXPECT_EQ(ERR_CONNECTION_CLOSED,
stream_->ReadFrames(&frames_, cb_.callback()));
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, AsyncClose) {
CreateRead(MockRead(ASYNC, "", 0));
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(ERR_CONNECTION_CLOSED, cb_.WaitForResult());
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, SyncCloseWithErr) {
CreateRead(MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED));
EXPECT_EQ(ERR_CONNECTION_CLOSED,
stream_->ReadFrames(&frames_, cb_.callback()));
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, AsyncCloseWithErr) {
CreateRead(MockRead(ASYNC, ERR_CONNECTION_CLOSED));
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(ERR_CONNECTION_CLOSED, cb_.WaitForResult());
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, SyncErrorsPassedThrough) {
CreateRead(MockRead(SYNCHRONOUS, ERR_INSUFFICIENT_RESOURCES));
EXPECT_EQ(ERR_INSUFFICIENT_RESOURCES,
stream_->ReadFrames(&frames_, cb_.callback()));
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, AsyncErrorsPassedThrough) {
CreateRead(MockRead(ASYNC, ERR_INSUFFICIENT_RESOURCES));
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(ERR_INSUFFICIENT_RESOURCES, cb_.WaitForResult());
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, CloseAfterFrame) {
CreateChunkedRead(SYNCHRONOUS,
kSampleFrame,
kSampleFrameSize,
kSampleFrameSize,
2,
LAST_FRAME_NOT_BIG);
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(1U, frames_.size());
frames_.clear();
EXPECT_EQ(ERR_CONNECTION_CLOSED,
stream_->ReadFrames(&frames_, cb_.callback()));
}
TEST_F(WebSocketBasicStreamSocketTest, AsyncCloseAfterIncompleteHeader) {
MockRead reads[] = {MockRead(ASYNC, kSampleFrame, 1U),
MockRead(SYNCHRONOUS, "", 0)};
CreateReadOnly(reads);
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(ERR_CONNECTION_CLOSED, cb_.WaitForResult());
}
TEST_F(WebSocketBasicStreamSocketTest, AsyncErrCloseAfterIncompleteHeader) {
MockRead reads[] = {MockRead(ASYNC, kSampleFrame, 1U),
MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED)};
CreateReadOnly(reads);
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(ERR_CONNECTION_CLOSED, cb_.WaitForResult());
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, EmptyFirstFrame) {
CreateRead(MockRead(SYNCHRONOUS, kEmptyFirstFrame, kEmptyFirstFrameSize));
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(NULL, frames_[0]->data.get());
EXPECT_EQ(0U, frames_[0]->header.payload_length);
}
TEST_F(WebSocketBasicStreamSocketTest, EmptyMiddleFrame) {
MockRead reads[] = {
MockRead(SYNCHRONOUS, kEmptyFirstFrame, kEmptyFirstFrameSize),
MockRead(SYNCHRONOUS, kEmptyMiddleFrame, kEmptyMiddleFrameSize),
MockRead(SYNCHRONOUS, ERR_IO_PENDING)};
CreateReadOnly(reads);
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(1U, frames_.size());
frames_.clear();
EXPECT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
}
TEST_F(WebSocketBasicStreamSocketTest, EmptyMiddleFrameAsync) {
MockRead reads[] = {
MockRead(SYNCHRONOUS, kEmptyFirstFrame, kEmptyFirstFrameSize),
MockRead(ASYNC, kEmptyMiddleFrame, kEmptyMiddleFrameSize),
MockRead(ASYNC, kValidPong, kValidPongSize)};
CreateReadOnly(reads);
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(1U, frames_.size());
frames_.clear();
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(OK, cb_.WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(WebSocketFrameHeader::kOpCodePong, frames_[0]->header.opcode);
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, EmptyFinalFrame) {
CreateRead(
MockRead(SYNCHRONOUS, kEmptyFinalTextFrame, kEmptyFinalTextFrameSize));
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(NULL, frames_[0]->data.get());
EXPECT_EQ(0U, frames_[0]->header.payload_length);
}
TEST_F(WebSocketBasicStreamSocketTest, ThreeFrameEmptyMessage) {
MockRead reads[] = {
MockRead(SYNCHRONOUS, kEmptyFirstFrame, kEmptyFirstFrameSize),
MockRead(SYNCHRONOUS, kEmptyMiddleFrame, kEmptyMiddleFrameSize),
MockRead(SYNCHRONOUS,
kEmptyFinalContinuationFrame,
kEmptyFinalContinuationFrameSize)};
CreateReadOnly(reads);
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(WebSocketFrameHeader::kOpCodeText, frames_[0]->header.opcode);
frames_.clear();
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(1U, frames_.size());
EXPECT_TRUE(frames_[0]->header.final);
}
TEST_F(WebSocketBasicStreamSocketTest, HttpReadBufferIsUsed) {
SetHttpReadBuffer(kSampleFrame, kSampleFrameSize);
CreateNullStream();
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(1U, frames_.size());
ASSERT_TRUE(frames_[0]->data);
EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length);
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest,
PartialFrameHeaderInHttpResponse) {
SetHttpReadBuffer(kSampleFrame, 1);
CreateRead(MockRead(ASYNC, kSampleFrame + 1, kSampleFrameSize - 1));
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(OK, cb_.WaitForResult());
ASSERT_EQ(1U, frames_.size());
ASSERT_TRUE(frames_[0]->data);
EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length);
EXPECT_EQ(WebSocketFrameHeader::kOpCodeText, frames_[0]->header.opcode);
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest,
PartialControlFrameInHttpResponse) {
const size_t kPartialFrameBytes = 3;
SetHttpReadBuffer(kCloseFrame, kPartialFrameBytes);
CreateRead(MockRead(ASYNC,
kCloseFrame + kPartialFrameBytes,
kCloseFrameSize - kPartialFrameBytes));
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(OK, cb_.WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(WebSocketFrameHeader::kOpCodeClose, frames_[0]->header.opcode);
EXPECT_EQ(kCloseFrameSize - 2, frames_[0]->header.payload_length);
EXPECT_EQ(
0,
memcmp(frames_[0]->data->data(), kCloseFrame + 2, kCloseFrameSize - 2));
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest,
PartialControlFrameInHttpResponseSync) {
const size_t kPartialFrameBytes = 3;
SetHttpReadBuffer(kCloseFrame, kPartialFrameBytes);
CreateRead(MockRead(SYNCHRONOUS,
kCloseFrame + kPartialFrameBytes,
kCloseFrameSize - kPartialFrameBytes));
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(WebSocketFrameHeader::kOpCodeClose, frames_[0]->header.opcode);
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, SyncInvalidFrame) {
CreateRead(MockRead(SYNCHRONOUS, kInvalidFrame, kInvalidFrameSize));
EXPECT_EQ(ERR_WS_PROTOCOL_ERROR,
stream_->ReadFrames(&frames_, cb_.callback()));
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, AsyncInvalidFrame) {
CreateRead(MockRead(ASYNC, kInvalidFrame, kInvalidFrameSize));
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(ERR_WS_PROTOCOL_ERROR, cb_.WaitForResult());
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, ControlFrameWithoutFin) {
CreateRead(
MockRead(SYNCHRONOUS, kPingFrameWithoutFin, kPingFrameWithoutFinSize));
EXPECT_EQ(ERR_WS_PROTOCOL_ERROR,
stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_TRUE(frames_.empty());
}
TEST_F(WebSocketBasicStreamSocketSingleReadTest, OverlongControlFrame) {
CreateRead(MockRead(SYNCHRONOUS, k126BytePong, k126BytePongSize));
EXPECT_EQ(ERR_WS_PROTOCOL_ERROR,
stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_TRUE(frames_.empty());
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, SplitOverlongControlFrame) {
const size_t kFirstChunkSize = 16;
expect_all_io_to_complete_ = false;
CreateChunkedRead(SYNCHRONOUS,
k126BytePong,
k126BytePongSize,
kFirstChunkSize,
2,
LAST_FRAME_BIG);
EXPECT_EQ(ERR_WS_PROTOCOL_ERROR,
stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_TRUE(frames_.empty());
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest,
AsyncSplitOverlongControlFrame) {
const size_t kFirstChunkSize = 16;
expect_all_io_to_complete_ = false;
CreateChunkedRead(ASYNC,
k126BytePong,
k126BytePongSize,
kFirstChunkSize,
2,
LAST_FRAME_BIG);
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(ERR_WS_PROTOCOL_ERROR, cb_.WaitForResult());
EXPECT_TRUE(frames_.empty());
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, SyncControlFrameAssembly) {
const size_t kChunkSize = 3;
CreateChunkedRead(
SYNCHRONOUS, kCloseFrame, kCloseFrameSize, kChunkSize, 3, LAST_FRAME_BIG);
EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback()));
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(WebSocketFrameHeader::kOpCodeClose, frames_[0]->header.opcode);
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, AsyncControlFrameAssembly) {
const size_t kChunkSize = 3;
CreateChunkedRead(
ASYNC, kCloseFrame, kCloseFrameSize, kChunkSize, 3, LAST_FRAME_BIG);
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(OK, cb_.WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_EQ(WebSocketFrameHeader::kOpCodeClose, frames_[0]->header.opcode);
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, OneMegFrame) {
const int kReadBufferSize = 32 * 1024;
const uint64 kPayloadSize = 1 << 20;
const size_t kWireSize = kPayloadSize + kLargeFrameHeaderSize;
const size_t kExpectedFrameCount =
(kWireSize + kReadBufferSize - 1) / kReadBufferSize;
scoped_ptr<char[]> big_frame(new char[kWireSize]);
memcpy(big_frame.get(), "\x81\x7F", 2);
base::WriteBigEndian(big_frame.get() + 2, kPayloadSize);
memset(big_frame.get() + kLargeFrameHeaderSize, 'A', kPayloadSize);
CreateChunkedRead(ASYNC,
big_frame.get(),
kWireSize,
kReadBufferSize,
kExpectedFrameCount,
LAST_FRAME_BIG);
for (size_t frame = 0; frame < kExpectedFrameCount; ++frame) {
frames_.clear();
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback()));
EXPECT_EQ(OK, cb_.WaitForResult());
ASSERT_EQ(1U, frames_.size());
size_t expected_payload_size = kReadBufferSize;
if (frame == 0) {
expected_payload_size = kReadBufferSize - kLargeFrameHeaderSize;
} else if (frame == kExpectedFrameCount - 1) {
expected_payload_size = kLargeFrameHeaderSize;
}
EXPECT_EQ(expected_payload_size, frames_[0]->header.payload_length);
}
}
TEST_F(WebSocketBasicStreamSocketChunkedReadTest, ReservedFlagCleared) {
static const char kReservedFlagFrame[] = "\x41\x05Hello";
const size_t kReservedFlagFrameSize = arraysize(kReservedFlagFrame) - 1;
const size_t kChunkSize = 5;
CreateChunkedRead(ASYNC,
kReservedFlagFrame,
kReservedFlagFrameSize,
kChunkSize,
2,
LAST_FRAME_BIG);
TestCompletionCallback cb[2];
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb[0].callback()));
EXPECT_EQ(OK, cb[0].WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_TRUE(frames_[0]->header.reserved1);
frames_.clear();
ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb[1].callback()));
EXPECT_EQ(OK, cb[1].WaitForResult());
ASSERT_EQ(1U, frames_.size());
EXPECT_FALSE(frames_[0]->header.reserved1);
}
TEST_F(WebSocketBasicStreamSocketWriteTest, WriteAtOnce) {
MockWrite writes[] = {MockWrite(SYNCHRONOUS, kWriteFrame, kWriteFrameSize)};
CreateWriteOnly(writes);
EXPECT_EQ(OK, stream_->WriteFrames(&frames_, cb_.callback()));
}
TEST_F(WebSocketBasicStreamSocketWriteTest, AsyncWriteAtOnce) {
MockWrite writes[] = {MockWrite(ASYNC, kWriteFrame, kWriteFrameSize)};
CreateWriteOnly(writes);
ASSERT_EQ(ERR_IO_PENDING, stream_->WriteFrames(&frames_, cb_.callback()));
EXPECT_EQ(OK, cb_.WaitForResult());
}
TEST_F(WebSocketBasicStreamSocketWriteTest, WriteInBits) {
MockWrite writes[] = {MockWrite(SYNCHRONOUS, kWriteFrame, 4),
MockWrite(ASYNC, kWriteFrame + 4, 4),
MockWrite(ASYNC, kWriteFrame + 8, kWriteFrameSize - 8)};
CreateWriteOnly(writes);
ASSERT_EQ(ERR_IO_PENDING, stream_->WriteFrames(&frames_, cb_.callback()));
EXPECT_EQ(OK, cb_.WaitForResult());
}
TEST_F(WebSocketBasicStreamSocketWriteTest, WriteNullPong) {
MockWrite writes[] = {
MockWrite(SYNCHRONOUS, kMaskedEmptyPong, kMaskedEmptyPongSize)};
CreateWriteOnly(writes);
scoped_ptr<WebSocketFrame> frame(
new WebSocketFrame(WebSocketFrameHeader::kOpCodePong));
WebSocketFrameHeader& header = frame->header;
header.final = true;
header.masked = true;
header.payload_length = 0;
ScopedVector<WebSocketFrame> frames;
frames.push_back(frame.release());
EXPECT_EQ(OK, stream_->WriteFrames(&frames, cb_.callback()));
}
TEST_F(WebSocketBasicStreamSocketTest, WriteNonNulMask) {
std::string masked_frame = std::string("\x81\x88");
masked_frame += std::string(kNonNulMaskingKey.key, 4);
masked_frame += "jiggered";
MockWrite writes[] = {
MockWrite(SYNCHRONOUS, masked_frame.data(), masked_frame.size())};
generator_ = &GenerateNonNulMaskingKey;
CreateStream(NULL, 0, writes, arraysize(writes));
scoped_ptr<WebSocketFrame> frame(
new WebSocketFrame(WebSocketFrameHeader::kOpCodeText));
const std::string unmasked_payload = "graphics";
const size_t payload_size = unmasked_payload.size();
frame->data = new IOBuffer(payload_size);
memcpy(frame->data->data(), unmasked_payload.data(), payload_size);
WebSocketFrameHeader& header = frame->header;
header.final = true;
header.masked = true;
header.payload_length = payload_size;
frames_.push_back(frame.release());
EXPECT_EQ(OK, stream_->WriteFrames(&frames_, cb_.callback()));
}
TEST_F(WebSocketBasicStreamSocketTest, GetExtensionsWorks) {
extensions_ = "inflate-uuencode";
CreateNullStream();
EXPECT_EQ("inflate-uuencode", stream_->GetExtensions());
}
TEST_F(WebSocketBasicStreamSocketTest, GetSubProtocolWorks) {
sub_protocol_ = "cyberchat";
CreateNullStream();
EXPECT_EQ("cyberchat", stream_->GetSubProtocol());
}
}
}