This source file includes following definitions.
- OnStartOpeningHandshake
- OnFinishOpeningHandshake
- OnAddChannelResponse
- OnDataFrame
- OnFlowControl
- OnClosingHandshake
- OnFailChannel
- OnDropChannel
- OnStartOpeningHandshake
- OnFinishOpeningHandshake
- extensions_
- ReadFrames
- WriteFrames
- Close
- GetSubProtocol
- GetExtensions
- CreateFrameVector
- ACTION_P
- MatchAndExplain
- DescribeTo
- DescribeNegationTo
- EqualsFrames
- closure
- WaitForResult
- ACTION_P
- ACTION_P
- read_frames_pending_
- PrepareReadFrames
- PrepareRawReadFrames
- PrepareReadFramesError
- ReadFrames
- DoCallback
- WriteFrames
- WriteFrames
- done_
- WriteFrames
- ReadFrames
- PostCallback
- DoCallback
- MoveFrames
- weak_ptr_factory_
- WriteFrames
- ReadFrames
- Close
- CallCallbackUnlessClosed
- AsVector
- CreateChannelAndConnect
- CreateChannelAndConnectSuccessfully
- CreateEventInterface
- set_stream
- DeleteIfDeleting
- OnAddChannelResponse
- OnDataFrame
- OnFlowControl
- OnClosingHandshake
- OnFailChannel
- OnDropChannel
- OnStartOpeningHandshake
- OnFinishOpeningHandshake
- CreateEventInterface
- CreateEventInterface
- CreateChannelAndConnectSuccessfully
- SetUp
- CreateChannelAndConnectWithQuota
- CreateChannelAndConnectSuccesfully
- SetUp
- 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
- 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
- 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
- CreateChannelAndConnectSuccessfully
- TEST_F
- TEST_F
- TEST_F
#include "net/websockets/websocket_channel.h"
#include <limits.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/location.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_piece.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request_context.h"
#include "net/websockets/websocket_errors.h"
#include "net/websockets/websocket_event_interface.h"
#include "net/websockets/websocket_handshake_request_info.h"
#include "net/websockets/websocket_handshake_response_info.h"
#include "net/websockets/websocket_mux.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
#define CLOSE_DATA(code, string) WEBSOCKET_CLOSE_CODE_AS_STRING_##code string
#define WEBSOCKET_CLOSE_CODE_AS_STRING_NORMAL_CLOSURE "\x03\xe8"
#define WEBSOCKET_CLOSE_CODE_AS_STRING_GOING_AWAY "\x03\xe9"
#define WEBSOCKET_CLOSE_CODE_AS_STRING_PROTOCOL_ERROR "\x03\xea"
#define WEBSOCKET_CLOSE_CODE_AS_STRING_ABNORMAL_CLOSURE "\x03\xee"
#define WEBSOCKET_CLOSE_CODE_AS_STRING_SERVER_ERROR "\x03\xf3"
namespace net {
std::ostream& operator<<(std::ostream& os, const WebSocketFrameHeader& header) {
return os << (header.final ? "FINAL_FRAME" : "NOT_FINAL_FRAME") << ", "
<< header.opcode << ", "
<< (header.masked ? "MASKED" : "NOT_MASKED");
}
std::ostream& operator<<(std::ostream& os, const WebSocketFrame& frame) {
os << "{" << frame.header << ", ";
if (frame.data) {
return os << "\"" << base::StringPiece(frame.data->data(),
frame.header.payload_length)
<< "\"}";
}
return os << "NULL}";
}
std::ostream& operator<<(std::ostream& os,
const ScopedVector<WebSocketFrame>& vector) {
os << "{";
bool first = true;
for (ScopedVector<WebSocketFrame>::const_iterator it = vector.begin();
it != vector.end();
++it) {
if (!first) {
os << ",\n";
} else {
first = false;
}
os << **it;
}
return os << "}";
}
std::ostream& operator<<(std::ostream& os,
const ScopedVector<WebSocketFrame>* vector) {
return os << '&' << *vector;
}
namespace {
using ::base::TimeDelta;
using ::testing::AnyNumber;
using ::testing::DefaultValue;
using ::testing::InSequence;
using ::testing::MockFunction;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::StrictMock;
using ::testing::_;
const char kBinaryBlob[] = {'\n', '\r',
'\0',
'\x7F',
'\x80', '\xFF',
'\x1A',
'\x03',
'\x04',
'\x1B',
'\b',
'\'',
};
const size_t kBinaryBlobSize = arraysize(kBinaryBlob);
const size_t kDefaultInitialQuota = 1 << 17;
const size_t kDefaultQuotaRefreshTrigger = (1 << 16) + 1;
const int kVeryTinyTimeoutMillis = 1;
const int64 kPlentyOfQuota = INT_MAX;
typedef WebSocketEventInterface::ChannelState ChannelState;
const ChannelState CHANNEL_ALIVE = WebSocketEventInterface::CHANNEL_ALIVE;
const ChannelState CHANNEL_DELETED = WebSocketEventInterface::CHANNEL_DELETED;
typedef StrictMock< MockFunction<void(int)> > Checkpoint;
class MockWebSocketEventInterface : public WebSocketEventInterface {
public:
MockWebSocketEventInterface() {}
MOCK_METHOD3(OnAddChannelResponse,
ChannelState(bool,
const std::string&,
const std::string&));
MOCK_METHOD3(OnDataFrame,
ChannelState(bool,
WebSocketMessageType,
const std::vector<char>&));
MOCK_METHOD1(OnFlowControl, ChannelState(int64));
MOCK_METHOD0(OnClosingHandshake, ChannelState(void));
MOCK_METHOD1(OnFailChannel, ChannelState(const std::string&));
MOCK_METHOD3(OnDropChannel,
ChannelState(bool, uint16, const std::string&));
ChannelState OnStartOpeningHandshake(
scoped_ptr<WebSocketHandshakeRequestInfo>) OVERRIDE {
OnStartOpeningHandshakeCalled();
return CHANNEL_ALIVE;
}
ChannelState OnFinishOpeningHandshake(
scoped_ptr<WebSocketHandshakeResponseInfo>) OVERRIDE {
OnFinishOpeningHandshakeCalled();
return CHANNEL_ALIVE;
}
MOCK_METHOD0(OnStartOpeningHandshakeCalled, void());
MOCK_METHOD0(OnFinishOpeningHandshakeCalled, void());
};
class FakeWebSocketEventInterface : public WebSocketEventInterface {
virtual ChannelState OnAddChannelResponse(
bool fail,
const std::string& selected_protocol,
const std::string& extensions) OVERRIDE {
return fail ? CHANNEL_DELETED : CHANNEL_ALIVE;
}
virtual ChannelState OnDataFrame(bool fin,
WebSocketMessageType type,
const std::vector<char>& data) OVERRIDE {
return CHANNEL_ALIVE;
}
virtual ChannelState OnFlowControl(int64 quota) OVERRIDE {
return CHANNEL_ALIVE;
}
virtual ChannelState OnClosingHandshake() OVERRIDE { return CHANNEL_ALIVE; }
virtual ChannelState OnFailChannel(const std::string& message) OVERRIDE {
return CHANNEL_DELETED;
}
virtual ChannelState OnDropChannel(bool was_clean,
uint16 code,
const std::string& reason) OVERRIDE {
return CHANNEL_DELETED;
}
virtual ChannelState OnStartOpeningHandshake(
scoped_ptr<WebSocketHandshakeRequestInfo> request) OVERRIDE {
return CHANNEL_ALIVE;
}
virtual ChannelState OnFinishOpeningHandshake(
scoped_ptr<WebSocketHandshakeResponseInfo> response) OVERRIDE {
return CHANNEL_ALIVE;
}
};
class FakeWebSocketStream : public WebSocketStream {
public:
FakeWebSocketStream() {}
FakeWebSocketStream(const std::string& protocol,
const std::string& extensions)
: protocol_(protocol), extensions_(extensions) {}
virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback) OVERRIDE {
return ERR_IO_PENDING;
}
virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback) OVERRIDE {
return ERR_IO_PENDING;
}
virtual void Close() OVERRIDE {}
virtual std::string GetSubProtocol() const OVERRIDE { return protocol_; }
virtual std::string GetExtensions() const OVERRIDE { return extensions_; }
private:
std::string protocol_;
std::string extensions_;
};
enum IsFinal { NOT_FINAL_FRAME, FINAL_FRAME };
enum IsMasked { NOT_MASKED, MASKED };
struct InitFrame {
IsFinal final;
WebSocketFrameHeader::OpCode opcode;
IsMasked masked;
const char* const data;
};
std::ostream& operator<<(std::ostream& os, const InitFrame& frame) {
os << "{" << (frame.final == FINAL_FRAME ? "FINAL_FRAME" : "NOT_FINAL_FRAME")
<< ", " << frame.opcode << ", "
<< (frame.masked == MASKED ? "MASKED" : "NOT_MASKED") << ", ";
if (frame.data) {
return os << "\"" << frame.data << "\"}";
}
return os << "NULL}";
}
template <size_t N>
std::ostream& operator<<(std::ostream& os, const InitFrame (&frames)[N]) {
os << "{";
bool first = true;
for (size_t i = 0; i < N; ++i) {
if (!first) {
os << ",\n";
} else {
first = false;
}
os << frames[i];
}
return os << "}";
}
template <size_t N>
ScopedVector<WebSocketFrame> CreateFrameVector(
const InitFrame (&source_frames)[N]) {
ScopedVector<WebSocketFrame> result_frames;
result_frames.reserve(N);
for (size_t i = 0; i < N; ++i) {
const InitFrame& source_frame = source_frames[i];
scoped_ptr<WebSocketFrame> result_frame(
new WebSocketFrame(source_frame.opcode));
size_t frame_length = source_frame.data ? strlen(source_frame.data) : 0;
WebSocketFrameHeader& result_header = result_frame->header;
result_header.final = (source_frame.final == FINAL_FRAME);
result_header.masked = (source_frame.masked == MASKED);
result_header.payload_length = frame_length;
if (source_frame.data) {
result_frame->data = new IOBuffer(frame_length);
memcpy(result_frame->data->data(), source_frame.data, frame_length);
}
result_frames.push_back(result_frame.release());
}
return result_frames.Pass();
}
ACTION_P(ReturnFrames, source_frames) {
*arg0 = CreateFrameVector(*source_frames);
return OK;
}
template <size_t N>
class EqualsFramesMatcher
: public ::testing::MatcherInterface<ScopedVector<WebSocketFrame>*> {
public:
EqualsFramesMatcher(const InitFrame (*expect_frames)[N])
: expect_frames_(expect_frames) {}
virtual bool MatchAndExplain(ScopedVector<WebSocketFrame>* actual_frames,
::testing::MatchResultListener* listener) const {
if (actual_frames->size() != N) {
*listener << "the vector size is " << actual_frames->size();
return false;
}
for (size_t i = 0; i < N; ++i) {
const WebSocketFrame& actual_frame = *(*actual_frames)[i];
const InitFrame& expected_frame = (*expect_frames_)[i];
if (actual_frame.header.final != (expected_frame.final == FINAL_FRAME)) {
*listener << "the frame is marked as "
<< (actual_frame.header.final ? "" : "not ") << "final";
return false;
}
if (actual_frame.header.opcode != expected_frame.opcode) {
*listener << "the opcode is " << actual_frame.header.opcode;
return false;
}
if (actual_frame.header.masked != (expected_frame.masked == MASKED)) {
*listener << "the frame is "
<< (actual_frame.header.masked ? "masked" : "not masked");
return false;
}
const size_t expected_length =
expected_frame.data ? strlen(expected_frame.data) : 0;
if (actual_frame.header.payload_length != expected_length) {
*listener << "the payload length is "
<< actual_frame.header.payload_length;
return false;
}
if (expected_length != 0 &&
memcmp(actual_frame.data->data(),
expected_frame.data,
actual_frame.header.payload_length) != 0) {
*listener << "the data content differs";
return false;
}
}
return true;
}
virtual void DescribeTo(std::ostream* os) const {
*os << "matches " << *expect_frames_;
}
virtual void DescribeNegationTo(std::ostream* os) const {
*os << "does not match " << *expect_frames_;
}
private:
const InitFrame (*expect_frames_)[N];
};
template <size_t N>
::testing::Matcher<ScopedVector<WebSocketFrame>*> EqualsFrames(
const InitFrame (&frames)[N]) {
return ::testing::MakeMatcher(new EqualsFramesMatcher<N>(&frames));
}
class TestClosure {
public:
base::Closure closure() { return base::Bind(callback_.callback(), OK); }
void WaitForResult() { callback_.WaitForResult(); }
private:
TestCompletionCallback callback_;
};
ACTION_P(InvokeClosure, closure) { closure.Run(); }
ACTION_P(InvokeClosureReturnDeleted, closure) {
closure.Run();
return WebSocketEventInterface::CHANNEL_DELETED;
}
class ReadableFakeWebSocketStream : public FakeWebSocketStream {
public:
enum IsSync { SYNC, ASYNC };
ReadableFakeWebSocketStream() : index_(0), read_frames_pending_(false) {}
virtual ~ReadableFakeWebSocketStream() {
CHECK(index_ >= responses_.size());
CHECK(!read_frames_pending_);
}
template <size_t N>
void PrepareReadFrames(IsSync async,
int error,
const InitFrame (&frames)[N]) {
responses_.push_back(new Response(async, error, CreateFrameVector(frames)));
}
void PrepareRawReadFrames(IsSync async,
int error,
ScopedVector<WebSocketFrame> frames) {
responses_.push_back(new Response(async, error, frames.Pass()));
}
void PrepareReadFramesError(IsSync async, int error) {
responses_.push_back(
new Response(async, error, ScopedVector<WebSocketFrame>()));
}
virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback) OVERRIDE {
CHECK(!read_frames_pending_);
if (index_ >= responses_.size())
return ERR_IO_PENDING;
if (responses_[index_]->async == ASYNC) {
read_frames_pending_ = true;
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&ReadableFakeWebSocketStream::DoCallback,
base::Unretained(this),
frames,
callback));
return ERR_IO_PENDING;
} else {
frames->swap(responses_[index_]->frames);
return responses_[index_++]->error;
}
}
private:
void DoCallback(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback) {
read_frames_pending_ = false;
frames->swap(responses_[index_]->frames);
callback.Run(responses_[index_++]->error);
return;
}
struct Response {
Response(IsSync async, int error, ScopedVector<WebSocketFrame> frames)
: async(async), error(error), frames(frames.Pass()) {}
IsSync async;
int error;
ScopedVector<WebSocketFrame> frames;
private:
DISALLOW_COPY_AND_ASSIGN(Response);
};
ScopedVector<Response> responses_;
size_t index_;
bool read_frames_pending_;
};
class WriteableFakeWebSocketStream : public FakeWebSocketStream {
public:
virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback) OVERRIDE {
return OK;
}
};
class UnWriteableFakeWebSocketStream : public FakeWebSocketStream {
public:
virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback) OVERRIDE {
return ERR_CONNECTION_RESET;
}
};
class EchoeyFakeWebSocketStream : public FakeWebSocketStream {
public:
EchoeyFakeWebSocketStream() : read_frames_(NULL), done_(false) {}
virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback) OVERRIDE {
stored_frames_.insert(stored_frames_.end(), frames->begin(), frames->end());
frames->weak_clear();
PostCallback();
return OK;
}
virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback) OVERRIDE {
read_callback_ = callback;
read_frames_ = frames;
if (done_)
PostCallback();
return ERR_IO_PENDING;
}
private:
void PostCallback() {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&EchoeyFakeWebSocketStream::DoCallback,
base::Unretained(this)));
}
void DoCallback() {
if (done_) {
read_callback_.Run(ERR_CONNECTION_CLOSED);
} else if (!stored_frames_.empty()) {
done_ = MoveFrames(read_frames_);
read_frames_ = NULL;
read_callback_.Run(OK);
}
}
bool MoveFrames(ScopedVector<WebSocketFrame>* out) {
bool seen_close = false;
*out = stored_frames_.Pass();
for (ScopedVector<WebSocketFrame>::iterator it = out->begin();
it != out->end();
++it) {
WebSocketFrameHeader& header = (*it)->header;
header.masked = false;
if (header.opcode == WebSocketFrameHeader::kOpCodeClose)
seen_close = true;
}
return seen_close;
}
ScopedVector<WebSocketFrame> stored_frames_;
CompletionCallback read_callback_;
ScopedVector<WebSocketFrame>* read_frames_;
bool done_;
};
class ResetOnWriteFakeWebSocketStream : public FakeWebSocketStream {
public:
ResetOnWriteFakeWebSocketStream() : closed_(false), weak_ptr_factory_(this) {}
virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback) OVERRIDE {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&ResetOnWriteFakeWebSocketStream::CallCallbackUnlessClosed,
weak_ptr_factory_.GetWeakPtr(),
callback,
ERR_CONNECTION_RESET));
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&ResetOnWriteFakeWebSocketStream::CallCallbackUnlessClosed,
weak_ptr_factory_.GetWeakPtr(),
read_callback_,
ERR_CONNECTION_RESET));
return ERR_IO_PENDING;
}
virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback) OVERRIDE {
read_callback_ = callback;
return ERR_IO_PENDING;
}
virtual void Close() OVERRIDE { closed_ = true; }
private:
void CallCallbackUnlessClosed(const CompletionCallback& callback, int value) {
if (!closed_)
callback.Run(value);
}
CompletionCallback read_callback_;
bool closed_;
base::WeakPtrFactory<ResetOnWriteFakeWebSocketStream> weak_ptr_factory_;
};
class MockWebSocketStream : public WebSocketStream {
public:
MOCK_METHOD2(ReadFrames,
int(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback));
MOCK_METHOD2(WriteFrames,
int(ScopedVector<WebSocketFrame>* frames,
const CompletionCallback& callback));
MOCK_METHOD0(Close, void());
MOCK_CONST_METHOD0(GetSubProtocol, std::string());
MOCK_CONST_METHOD0(GetExtensions, std::string());
MOCK_METHOD0(AsWebSocketStream, WebSocketStream*());
};
struct ArgumentCopyingWebSocketStreamCreator {
scoped_ptr<WebSocketStreamRequest> Create(
const GURL& socket_url,
const std::vector<std::string>& requested_subprotocols,
const url::Origin& origin,
URLRequestContext* url_request_context,
const BoundNetLog& net_log,
scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate) {
this->socket_url = socket_url;
this->requested_subprotocols = requested_subprotocols;
this->origin = origin;
this->url_request_context = url_request_context;
this->net_log = net_log;
this->connect_delegate = connect_delegate.Pass();
return make_scoped_ptr(new WebSocketStreamRequest);
}
GURL socket_url;
url::Origin origin;
std::vector<std::string> requested_subprotocols;
URLRequestContext* url_request_context;
BoundNetLog net_log;
scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate;
};
std::vector<char> AsVector(const std::string& s) {
return std::vector<char>(s.begin(), s.end());
}
class WebSocketChannelTest : public ::testing::Test {
protected:
WebSocketChannelTest() : stream_(new FakeWebSocketStream) {}
void CreateChannelAndConnect() {
channel_.reset(new WebSocketChannel(CreateEventInterface(),
&connect_data_.url_request_context));
channel_->SendAddChannelRequestForTesting(
connect_data_.socket_url,
connect_data_.requested_subprotocols,
connect_data_.origin,
base::Bind(&ArgumentCopyingWebSocketStreamCreator::Create,
base::Unretained(&connect_data_.creator)));
}
virtual void CreateChannelAndConnectSuccessfully() {
CreateChannelAndConnect();
channel_->SendFlowControl(kPlentyOfQuota);
connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass());
}
virtual scoped_ptr<WebSocketEventInterface> CreateEventInterface() {
return scoped_ptr<WebSocketEventInterface>(new FakeWebSocketEventInterface);
}
template <class T>
void set_stream(scoped_ptr<T> stream) {
stream_ = stream.template PassAs<WebSocketStream>();
}
struct ConnectData {
ConnectData() : socket_url("ws://ws/"), origin("http://ws") {}
URLRequestContext url_request_context;
GURL socket_url;
std::vector<std::string> requested_subprotocols;
url::Origin origin;
ArgumentCopyingWebSocketStreamCreator creator;
};
ConnectData connect_data_;
scoped_ptr<WebSocketChannel> channel_;
scoped_ptr<WebSocketStream> stream_;
};
enum EventInterfaceCall {
EVENT_ON_ADD_CHANNEL_RESPONSE = 0x1,
EVENT_ON_DATA_FRAME = 0x2,
EVENT_ON_FLOW_CONTROL = 0x4,
EVENT_ON_CLOSING_HANDSHAKE = 0x8,
EVENT_ON_FAIL_CHANNEL = 0x10,
EVENT_ON_DROP_CHANNEL = 0x20,
EVENT_ON_START_OPENING_HANDSHAKE = 0x40,
EVENT_ON_FINISH_OPENING_HANDSHAKE = 0x80,
};
class WebSocketChannelDeletingTest : public WebSocketChannelTest {
public:
ChannelState DeleteIfDeleting(EventInterfaceCall call) {
if (deleting_ & call) {
channel_.reset();
return CHANNEL_DELETED;
} else {
return CHANNEL_ALIVE;
}
}
protected:
WebSocketChannelDeletingTest()
: deleting_(EVENT_ON_ADD_CHANNEL_RESPONSE | EVENT_ON_DATA_FRAME |
EVENT_ON_FLOW_CONTROL |
EVENT_ON_CLOSING_HANDSHAKE |
EVENT_ON_FAIL_CHANNEL |
EVENT_ON_DROP_CHANNEL |
EVENT_ON_START_OPENING_HANDSHAKE |
EVENT_ON_FINISH_OPENING_HANDSHAKE) {}
virtual scoped_ptr<WebSocketEventInterface> CreateEventInterface() OVERRIDE;
int deleting_;
};
class ChannelDeletingFakeWebSocketEventInterface
: public FakeWebSocketEventInterface {
public:
ChannelDeletingFakeWebSocketEventInterface(
WebSocketChannelDeletingTest* fixture)
: fixture_(fixture) {}
virtual ChannelState OnAddChannelResponse(
bool fail,
const std::string& selected_protocol,
const std::string& extensions) OVERRIDE {
return fixture_->DeleteIfDeleting(EVENT_ON_ADD_CHANNEL_RESPONSE);
}
virtual ChannelState OnDataFrame(bool fin,
WebSocketMessageType type,
const std::vector<char>& data) OVERRIDE {
return fixture_->DeleteIfDeleting(EVENT_ON_DATA_FRAME);
}
virtual ChannelState OnFlowControl(int64 quota) OVERRIDE {
return fixture_->DeleteIfDeleting(EVENT_ON_FLOW_CONTROL);
}
virtual ChannelState OnClosingHandshake() OVERRIDE {
return fixture_->DeleteIfDeleting(EVENT_ON_CLOSING_HANDSHAKE);
}
virtual ChannelState OnFailChannel(const std::string& message) OVERRIDE {
return fixture_->DeleteIfDeleting(EVENT_ON_FAIL_CHANNEL);
}
virtual ChannelState OnDropChannel(bool was_clean,
uint16 code,
const std::string& reason) OVERRIDE {
return fixture_->DeleteIfDeleting(EVENT_ON_DROP_CHANNEL);
}
virtual ChannelState OnStartOpeningHandshake(
scoped_ptr<WebSocketHandshakeRequestInfo> request) OVERRIDE {
return fixture_->DeleteIfDeleting(EVENT_ON_START_OPENING_HANDSHAKE);
}
virtual ChannelState OnFinishOpeningHandshake(
scoped_ptr<WebSocketHandshakeResponseInfo> response) OVERRIDE {
return fixture_->DeleteIfDeleting(EVENT_ON_FINISH_OPENING_HANDSHAKE);
}
private:
WebSocketChannelDeletingTest* fixture_;
};
scoped_ptr<WebSocketEventInterface>
WebSocketChannelDeletingTest::CreateEventInterface() {
return scoped_ptr<WebSocketEventInterface>(
new ChannelDeletingFakeWebSocketEventInterface(this));
}
class WebSocketChannelEventInterfaceTest : public WebSocketChannelTest {
protected:
WebSocketChannelEventInterfaceTest()
: event_interface_(new StrictMock<MockWebSocketEventInterface>) {
DefaultValue<ChannelState>::Set(CHANNEL_ALIVE);
ON_CALL(*event_interface_, OnAddChannelResponse(true, _, _))
.WillByDefault(Return(CHANNEL_DELETED));
ON_CALL(*event_interface_, OnDropChannel(_, _, _))
.WillByDefault(Return(CHANNEL_DELETED));
ON_CALL(*event_interface_, OnFailChannel(_))
.WillByDefault(Return(CHANNEL_DELETED));
}
virtual ~WebSocketChannelEventInterfaceTest() {
DefaultValue<ChannelState>::Clear();
}
virtual scoped_ptr<WebSocketEventInterface> CreateEventInterface() OVERRIDE {
return scoped_ptr<WebSocketEventInterface>(event_interface_.release());
}
scoped_ptr<MockWebSocketEventInterface> event_interface_;
};
class WebSocketChannelStreamTest : public WebSocketChannelTest {
protected:
WebSocketChannelStreamTest()
: mock_stream_(new StrictMock<MockWebSocketStream>) {}
virtual void CreateChannelAndConnectSuccessfully() OVERRIDE {
set_stream(mock_stream_.Pass());
WebSocketChannelTest::CreateChannelAndConnectSuccessfully();
}
scoped_ptr<MockWebSocketStream> mock_stream_;
};
class WebSocketChannelSendUtf8Test
: public WebSocketChannelEventInterfaceTest {
public:
virtual void SetUp() {
set_stream(make_scoped_ptr(new WriteableFakeWebSocketStream));
EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _, _))
.Times(AnyNumber());
EXPECT_CALL(*event_interface_, OnFlowControl(_))
.Times(AnyNumber());
}
};
class WebSocketChannelFlowControlTest
: public WebSocketChannelEventInterfaceTest {
protected:
void CreateChannelAndConnectWithQuota(int64 quota) {
CreateChannelAndConnect();
channel_->SendFlowControl(quota);
connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass());
}
virtual void CreateChannelAndConnectSuccesfully() { NOTREACHED(); }
};
class WebSocketChannelReceiveUtf8Test : public WebSocketChannelStreamTest {
public:
virtual void SetUp() {
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
}
};
TEST_F(WebSocketChannelTest, EverythingIsPassedToTheCreatorFunction) {
connect_data_.socket_url = GURL("ws://example.com/test");
connect_data_.origin = url::Origin("http://example.com");
connect_data_.requested_subprotocols.push_back("Sinbad");
CreateChannelAndConnect();
const ArgumentCopyingWebSocketStreamCreator& actual = connect_data_.creator;
EXPECT_EQ(&connect_data_.url_request_context, actual.url_request_context);
EXPECT_EQ(connect_data_.socket_url, actual.socket_url);
EXPECT_EQ(connect_data_.requested_subprotocols,
actual.requested_subprotocols);
EXPECT_EQ(connect_data_.origin.string(), actual.origin.string());
}
TEST_F(WebSocketChannelTest, SendFlowControlDuringHandshakeOkay) {
CreateChannelAndConnect();
ASSERT_TRUE(channel_);
channel_->SendFlowControl(65536);
}
TEST_F(WebSocketChannelDeletingTest, OnAddChannelResponseFail) {
CreateChannelAndConnect();
EXPECT_TRUE(channel_);
connect_data_.creator.connect_delegate->OnFailure("bye");
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnAddChannelResponseSuccess) {
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnDataFrameSync) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_DATA_FRAME;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnDataFrameAsync) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_DATA_FRAME;
CreateChannelAndConnectSuccessfully();
EXPECT_TRUE(channel_);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnFlowControlAfterConnect) {
deleting_ = EVENT_ON_FLOW_CONTROL;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnFlowControlAfterSend) {
set_stream(make_scoped_ptr(new WriteableFakeWebSocketStream));
deleting_ = EVENT_ON_FAIL_CHANNEL | EVENT_ON_DROP_CHANNEL;
CreateChannelAndConnectSuccessfully();
ASSERT_TRUE(channel_);
deleting_ = EVENT_ON_FLOW_CONTROL;
channel_->SendFrame(true,
WebSocketFrameHeader::kOpCodeText,
std::vector<char>(kDefaultInitialQuota, 'B'));
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnClosingHandshakeSync) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Success")}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_CLOSING_HANDSHAKE;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnClosingHandshakeAsync) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Success")}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_CLOSING_HANDSHAKE;
CreateChannelAndConnectSuccessfully();
ASSERT_TRUE(channel_);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnDropChannelWriteError) {
set_stream(make_scoped_ptr(new UnWriteableFakeWebSocketStream));
deleting_ = EVENT_ON_DROP_CHANNEL;
CreateChannelAndConnectSuccessfully();
ASSERT_TRUE(channel_);
channel_->SendFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("this will fail"));
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnDropChannelReadError) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC,
ERR_FAILED);
set_stream(stream.Pass());
deleting_ = EVENT_ON_DROP_CHANNEL;
CreateChannelAndConnectSuccessfully();
ASSERT_TRUE(channel_);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnNotifyStartOpeningHandshakeError) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_START_OPENING_HANDSHAKE;
CreateChannelAndConnectSuccessfully();
ASSERT_TRUE(channel_);
channel_->OnStartOpeningHandshake(scoped_ptr<WebSocketHandshakeRequestInfo>(
new WebSocketHandshakeRequestInfo(GURL("http://www.example.com/"),
base::Time())));
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, OnNotifyFinishOpeningHandshakeError) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_FINISH_OPENING_HANDSHAKE;
CreateChannelAndConnectSuccessfully();
ASSERT_TRUE(channel_);
scoped_refptr<HttpResponseHeaders> response_headers(
new HttpResponseHeaders(""));
channel_->OnFinishOpeningHandshake(scoped_ptr<WebSocketHandshakeResponseInfo>(
new WebSocketHandshakeResponseInfo(GURL("http://www.example.com/"),
200,
"OK",
response_headers,
base::Time())));
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, FailChannelInSendFrame) {
set_stream(make_scoped_ptr(new WriteableFakeWebSocketStream));
deleting_ = EVENT_ON_FAIL_CHANNEL;
CreateChannelAndConnectSuccessfully();
ASSERT_TRUE(channel_);
channel_->SendFrame(true,
WebSocketFrameHeader::kOpCodeText,
std::vector<char>(kDefaultInitialQuota * 2, 'T'));
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, FailChannelInOnReadDone) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC,
ERR_WS_PROTOCOL_ERROR);
set_stream(stream.Pass());
deleting_ = EVENT_ON_FAIL_CHANNEL;
CreateChannelAndConnectSuccessfully();
ASSERT_TRUE(channel_);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, FailChannelDueToMaskedFrame) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "HELLO"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_FAIL_CHANNEL;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, FailChannelDueToBadControlFrame) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, 0xF, NOT_MASKED, ""}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_FAIL_CHANNEL;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, FailChannelDueToBadControlFrameNull) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, 0xF, NOT_MASKED, NULL}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_FAIL_CHANNEL;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, FailChannelDueToPongAfterClose) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED,
CLOSE_DATA(NORMAL_CLOSURE, "Success")},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, NOT_MASKED, ""}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_FAIL_CHANNEL;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, FailChannelDueToPongAfterCloseNull) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED,
CLOSE_DATA(NORMAL_CLOSURE, "Success")},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, NOT_MASKED, NULL}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_FAIL_CHANNEL;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, FailChannelDueToUnknownOpCode) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {{FINAL_FRAME, 0x7, NOT_MASKED, ""}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_FAIL_CHANNEL;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, FailChannelDueToUnknownOpCodeNull) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {{FINAL_FRAME, 0x7, NOT_MASKED, NULL}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_FAIL_CHANNEL;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelDeletingTest, FailChannelDueInvalidCloseReason) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "\xFF")}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
deleting_ = EVENT_ON_FAIL_CHANNEL;
CreateChannelAndConnectSuccessfully();
EXPECT_EQ(NULL, channel_.get());
}
TEST_F(WebSocketChannelEventInterfaceTest, ConnectSuccessReported) {
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, "", ""));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
CreateChannelAndConnect();
connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass());
}
TEST_F(WebSocketChannelEventInterfaceTest, ConnectFailureReported) {
EXPECT_CALL(*event_interface_, OnFailChannel("hello"));
CreateChannelAndConnect();
connect_data_.creator.connect_delegate->OnFailure("hello");
}
TEST_F(WebSocketChannelEventInterfaceTest, NonWebSocketSchemeRejected) {
EXPECT_CALL(*event_interface_, OnAddChannelResponse(true, "", ""));
connect_data_.socket_url = GURL("http://www.google.com/");
CreateChannelAndConnect();
}
TEST_F(WebSocketChannelEventInterfaceTest, ProtocolPassed) {
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, "Bob", ""));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
CreateChannelAndConnect();
connect_data_.creator.connect_delegate->OnSuccess(
scoped_ptr<WebSocketStream>(new FakeWebSocketStream("Bob", "")));
}
TEST_F(WebSocketChannelEventInterfaceTest, ExtensionsPassed) {
EXPECT_CALL(*event_interface_,
OnAddChannelResponse(false, "", "extension1, extension2"));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
CreateChannelAndConnect();
connect_data_.creator.connect_delegate->OnSuccess(scoped_ptr<WebSocketStream>(
new FakeWebSocketStream("", "extension1, extension2")));
}
TEST_F(WebSocketChannelEventInterfaceTest, DataLeftFromHandshake) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("HELLO")));
}
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, CloseAfterHandshake) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(SERVER_ERROR, "Internal Server Error")}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC,
ERR_CONNECTION_CLOSED);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_, OnClosingHandshake());
EXPECT_CALL(
*event_interface_,
OnDropChannel(
true, kWebSocketErrorInternalServerError, "Internal Server Error"));
}
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, ConnectionCloseAfterHandshake) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC,
ERR_CONNECTION_CLOSED);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_,
OnDropChannel(false, kWebSocketErrorAbnormalClosure, _));
}
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, NormalAsyncRead) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}};
Checkpoint checkpoint;
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("HELLO")));
EXPECT_CALL(checkpoint, Call(2));
}
CreateChannelAndConnectSuccessfully();
checkpoint.Call(1);
base::MessageLoop::current()->RunUntilIdle();
checkpoint.Call(2);
}
TEST_F(WebSocketChannelEventInterfaceTest, AsyncThenSyncRead) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames1[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}};
static const InitFrame frames2[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "WORLD"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1);
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames2);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("HELLO")));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("WORLD")));
}
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, FragmentedMessage) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames1[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "THREE"},
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, " "}};
static const InitFrame frames2[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "SMALL"}};
static const InitFrame frames3[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, " "},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "FRAMES"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1);
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames2);
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames3);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
false, WebSocketFrameHeader::kOpCodeText, AsVector("THREE")));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
false, WebSocketFrameHeader::kOpCodeContinuation, AsVector(" ")));
EXPECT_CALL(*event_interface_,
OnDataFrame(false,
WebSocketFrameHeader::kOpCodeContinuation,
AsVector("SMALL")));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
false, WebSocketFrameHeader::kOpCodeContinuation, AsVector(" ")));
EXPECT_CALL(*event_interface_,
OnDataFrame(true,
WebSocketFrameHeader::kOpCodeContinuation,
AsVector("FRAMES")));
}
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, NullMessage) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, NULL}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnDataFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("")));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, AsyncAbnormalClosure) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC,
ERR_CONNECTION_CLOSED);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_,
OnDropChannel(false, kWebSocketErrorAbnormalClosure, _));
}
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, ConnectionReset) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC,
ERR_CONNECTION_RESET);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_,
OnDropChannel(false, kWebSocketErrorAbnormalClosure, _));
}
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, MaskedFramesAreRejected) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "HELLO"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnFailChannel(
"A server must not mask any frames that it sends to the client."));
}
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, UnknownOpCodeIsRejected) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {{FINAL_FRAME, 4, NOT_MASKED, "HELLO"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_,
OnFailChannel("Unrecognized frame opcode: 4"));
}
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, ControlFrameInDataMessage) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames1[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText,
NOT_MASKED, "SPLIT "}};
static const InitFrame frames2[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, NOT_MASKED, ""}};
static const InitFrame frames3[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "MESSAGE"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1);
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames2);
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames3);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
false, WebSocketFrameHeader::kOpCodeText, AsVector("SPLIT ")));
EXPECT_CALL(*event_interface_,
OnDataFrame(true,
WebSocketFrameHeader::kOpCodeContinuation,
AsVector("MESSAGE")));
}
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, PongWithNullData) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, NOT_MASKED, NULL}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, FrameAfterInvalidFrame) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "HELLO"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, " WORLD"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnFailChannel(
"A server must not mask any frames that it sends to the client."));
}
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, SmallWriteDoesntUpdateQuota) {
set_stream(make_scoped_ptr(new WriteableFakeWebSocketStream));
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
}
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("B"));
}
TEST_F(WebSocketChannelEventInterfaceTest, LargeWriteUpdatesQuota) {
set_stream(make_scoped_ptr(new WriteableFakeWebSocketStream));
Checkpoint checkpoint;
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(checkpoint, Call(2));
}
CreateChannelAndConnectSuccessfully();
checkpoint.Call(1);
channel_->SendFrame(true,
WebSocketFrameHeader::kOpCodeText,
std::vector<char>(kDefaultInitialQuota, 'B'));
checkpoint.Call(2);
}
TEST_F(WebSocketChannelEventInterfaceTest, QuotaReallyIsRefreshed) {
set_stream(make_scoped_ptr(new WriteableFakeWebSocketStream));
Checkpoint checkpoint;
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(checkpoint, Call(2));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(checkpoint, Call(3));
}
CreateChannelAndConnectSuccessfully();
checkpoint.Call(1);
channel_->SendFrame(true,
WebSocketFrameHeader::kOpCodeText,
std::vector<char>(kDefaultQuotaRefreshTrigger, 'D'));
checkpoint.Call(2);
channel_->SendFrame(true,
WebSocketFrameHeader::kOpCodeText,
std::vector<char>(kDefaultQuotaRefreshTrigger, 'E'));
checkpoint.Call(3);
}
TEST_F(WebSocketChannelEventInterfaceTest, WriteOverQuotaIsRejected) {
set_stream(make_scoped_ptr(new WriteableFakeWebSocketStream));
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota));
EXPECT_CALL(*event_interface_, OnFailChannel("Send quota exceeded"));
}
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(true,
WebSocketFrameHeader::kOpCodeText,
std::vector<char>(kDefaultInitialQuota + 1, 'C'));
}
TEST_F(WebSocketChannelEventInterfaceTest, FailedWrite) {
set_stream(make_scoped_ptr(new UnWriteableFakeWebSocketStream));
Checkpoint checkpoint;
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*event_interface_,
OnDropChannel(false, kWebSocketErrorAbnormalClosure, _));
EXPECT_CALL(checkpoint, Call(2));
}
CreateChannelAndConnectSuccessfully();
checkpoint.Call(1);
channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("H"));
checkpoint.Call(2);
}
TEST_F(WebSocketChannelEventInterfaceTest, SendCloseDropsChannel) {
set_stream(make_scoped_ptr(new EchoeyFakeWebSocketStream));
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_,
OnDropChannel(true, kWebSocketNormalClosure, "Fred"));
}
CreateChannelAndConnectSuccessfully();
channel_->StartClosingHandshake(kWebSocketNormalClosure, "Fred");
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, CloseDuringConnection) {
EXPECT_CALL(*event_interface_,
OnDropChannel(false, kWebSocketErrorAbnormalClosure, ""));
CreateChannelAndConnect();
channel_->StartClosingHandshake(kWebSocketNormalClosure, "Joe");
}
TEST_F(WebSocketChannelEventInterfaceTest, OnDropChannelCalledOnce) {
set_stream(make_scoped_ptr(new ResetOnWriteFakeWebSocketStream));
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_,
OnDropChannel(false, kWebSocketErrorAbnormalClosure, ""))
.Times(1);
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("yt?"));
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, CloseWithNoPayloadGivesStatus1005) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, ""}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC,
ERR_CONNECTION_CLOSED);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_, OnClosingHandshake());
EXPECT_CALL(*event_interface_,
OnDropChannel(true, kWebSocketErrorNoStatusReceived, _));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest,
CloseWithNullPayloadGivesStatus1005) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, NULL}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC,
ERR_CONNECTION_CLOSED);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_, OnClosingHandshake());
EXPECT_CALL(*event_interface_,
OnDropChannel(true, kWebSocketErrorNoStatusReceived, _));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, SyncProtocolErrorGivesStatus1002) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC,
ERR_WS_PROTOCOL_ERROR);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_, OnFailChannel("Invalid frame header"));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, AsyncProtocolErrorGivesStatus1002) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC,
ERR_WS_PROTOCOL_ERROR);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_, OnFailChannel("Invalid frame header"));
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, StartHandshakeRequest) {
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_, OnStartOpeningHandshakeCalled());
}
CreateChannelAndConnectSuccessfully();
scoped_ptr<WebSocketHandshakeRequestInfo> request_info(
new WebSocketHandshakeRequestInfo(GURL("ws://www.example.com/"),
base::Time()));
connect_data_.creator.connect_delegate->OnStartOpeningHandshake(
request_info.Pass());
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, FinishHandshakeRequest) {
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_, OnFinishOpeningHandshakeCalled());
}
CreateChannelAndConnectSuccessfully();
scoped_refptr<HttpResponseHeaders> response_headers(
new HttpResponseHeaders(""));
scoped_ptr<WebSocketHandshakeResponseInfo> response_info(
new WebSocketHandshakeResponseInfo(GURL("ws://www.example.com/"),
200,
"OK",
response_headers,
base::Time()));
connect_data_.creator.connect_delegate->OnFinishOpeningHandshake(
response_info.Pass());
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, FailJustAfterHandshake) {
{
InSequence s;
EXPECT_CALL(*event_interface_, OnStartOpeningHandshakeCalled());
EXPECT_CALL(*event_interface_, OnFinishOpeningHandshakeCalled());
EXPECT_CALL(*event_interface_, OnFailChannel("bye"));
}
CreateChannelAndConnect();
WebSocketStream::ConnectDelegate* connect_delegate =
connect_data_.creator.connect_delegate.get();
GURL url("ws://www.example.com/");
scoped_ptr<WebSocketHandshakeRequestInfo> request_info(
new WebSocketHandshakeRequestInfo(url, base::Time()));
scoped_refptr<HttpResponseHeaders> response_headers(
new HttpResponseHeaders(""));
scoped_ptr<WebSocketHandshakeResponseInfo> response_info(
new WebSocketHandshakeResponseInfo(url,
200,
"OK",
response_headers,
base::Time()));
connect_delegate->OnStartOpeningHandshake(request_info.Pass());
connect_delegate->OnFinishOpeningHandshake(response_info.Pass());
connect_delegate->OnFailure("bye");
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelEventInterfaceTest, DataAfterCloseIsRejected) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED,
CLOSE_DATA(NORMAL_CLOSURE, "OK")},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "Payload"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
{
InSequence s;
EXPECT_CALL(*event_interface_, OnClosingHandshake());
EXPECT_CALL(*event_interface_,
OnFailChannel("Data frame received after close"));
}
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, OneByteClosePayloadMessage) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, "\x03"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnFailChannel(
"Received a broken close frame containing an invalid size body."));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, ClosePayloadReservedStatusMessage) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(ABNORMAL_CLOSURE, "Not valid on wire")}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnFailChannel(
"Received a broken close frame containing a reserved status code."));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, ClosePayloadInvalidReason) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "\xFF")}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnFailChannel(
"Received a broken close frame containing invalid UTF-8."));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, ReservedBitsMustNotBeSet) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText,
NOT_MASKED, "sakana"}};
ScopedVector<WebSocketFrame> raw_frames = CreateFrameVector(frames);
raw_frames[0]->header.reserved1 = true;
stream->PrepareRawReadFrames(
ReadableFakeWebSocketStream::SYNC, OK, raw_frames.Pass());
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_,
OnFailChannel(
"One or more reserved bits are on: reserved1 = 1, "
"reserved2 = 0, reserved3 = 0"));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest,
ClientInitiatedClosingHandshakeTimesOut) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC,
ERR_IO_PENDING);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
Checkpoint checkpoint;
TestClosure completion;
{
InSequence s;
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*event_interface_,
OnDropChannel(false, kWebSocketErrorAbnormalClosure, _))
.WillOnce(InvokeClosureReturnDeleted(completion.closure()));
}
CreateChannelAndConnectSuccessfully();
channel_->SetClosingHandshakeTimeoutForTesting(
TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis));
channel_->StartClosingHandshake(kWebSocketNormalClosure, "");
checkpoint.Call(1);
completion.WaitForResult();
}
TEST_F(WebSocketChannelEventInterfaceTest,
ServerInitiatedClosingHandshakeTimesOut) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
Checkpoint checkpoint;
TestClosure completion;
{
InSequence s;
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*event_interface_, OnClosingHandshake());
EXPECT_CALL(*event_interface_,
OnDropChannel(false, kWebSocketErrorAbnormalClosure, _))
.WillOnce(InvokeClosureReturnDeleted(completion.closure()));
}
CreateChannelAndConnectSuccessfully();
channel_->SetClosingHandshakeTimeoutForTesting(
TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis));
checkpoint.Call(1);
completion.WaitForResult();
}
TEST_F(WebSocketChannelStreamTest, FlowControlEarly) {
Checkpoint checkpoint;
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
{
InSequence s;
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(checkpoint, Call(2));
}
set_stream(mock_stream_.Pass());
CreateChannelAndConnect();
channel_->SendFlowControl(kPlentyOfQuota);
checkpoint.Call(1);
connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass());
checkpoint.Call(2);
}
TEST_F(WebSocketChannelStreamTest, FlowControlLate) {
Checkpoint checkpoint;
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
{
InSequence s;
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(checkpoint, Call(2));
}
set_stream(mock_stream_.Pass());
CreateChannelAndConnect();
connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass());
checkpoint.Call(1);
channel_->SendFlowControl(kPlentyOfQuota);
checkpoint.Call(2);
}
TEST_F(WebSocketChannelStreamTest, FlowControlStopsReadFrames) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames));
set_stream(mock_stream_.Pass());
CreateChannelAndConnect();
channel_->SendFlowControl(4);
connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass());
}
TEST_F(WebSocketChannelStreamTest, FlowControlStartsWithMoreQuota) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}};
Checkpoint checkpoint;
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
{
InSequence s;
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames));
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(Return(ERR_IO_PENDING));
}
set_stream(mock_stream_.Pass());
CreateChannelAndConnect();
channel_->SendFlowControl(4);
connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass());
checkpoint.Call(1);
channel_->SendFlowControl(4);
}
TEST_F(WebSocketChannelStreamTest, ReadFramesNotCalledUntilQuotaAvailable) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}};
Checkpoint checkpoint;
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
{
InSequence s;
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames));
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(checkpoint, Call(2));
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(Return(ERR_IO_PENDING));
}
set_stream(mock_stream_.Pass());
CreateChannelAndConnect();
channel_->SendFlowControl(2);
connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass());
checkpoint.Call(1);
channel_->SendFlowControl(2);
checkpoint.Call(2);
channel_->SendFlowControl(2);
}
TEST_F(WebSocketChannelFlowControlTest, SingleFrameMessageSplitSync) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(
*event_interface_,
OnDataFrame(false, WebSocketFrameHeader::kOpCodeText, AsVector("FO")));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("U")));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("R")));
}
CreateChannelAndConnectWithQuota(2);
channel_->SendFlowControl(1);
channel_->SendFlowControl(1);
}
TEST_F(WebSocketChannelFlowControlTest, SingleFrameMessageSplitAsync) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
set_stream(stream.Pass());
Checkpoint checkpoint;
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(
*event_interface_,
OnDataFrame(false, WebSocketFrameHeader::kOpCodeText, AsVector("FO")));
EXPECT_CALL(checkpoint, Call(2));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("U")));
EXPECT_CALL(checkpoint, Call(3));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("R")));
}
CreateChannelAndConnectWithQuota(2);
checkpoint.Call(1);
base::MessageLoop::current()->RunUntilIdle();
checkpoint.Call(2);
channel_->SendFlowControl(1);
checkpoint.Call(3);
channel_->SendFlowControl(1);
}
TEST_F(WebSocketChannelFlowControlTest, MultipleFrameSplit) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText,
NOT_MASKED, "FIRST FRAME IS 25 BYTES. "},
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "SECOND FRAME IS 26 BYTES. "},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "FINAL FRAME IS 24 BYTES."}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_,
OnDataFrame(false,
WebSocketFrameHeader::kOpCodeText,
AsVector("FIRST FRAME IS")));
EXPECT_CALL(*event_interface_,
OnDataFrame(false,
WebSocketFrameHeader::kOpCodeContinuation,
AsVector(" 25 BYTES. ")));
EXPECT_CALL(*event_interface_,
OnDataFrame(false,
WebSocketFrameHeader::kOpCodeContinuation,
AsVector("SECOND FRAME IS 26 BYTES. ")));
EXPECT_CALL(*event_interface_,
OnDataFrame(false,
WebSocketFrameHeader::kOpCodeContinuation,
AsVector("FINAL ")));
EXPECT_CALL(*event_interface_,
OnDataFrame(true,
WebSocketFrameHeader::kOpCodeContinuation,
AsVector("FRAME IS 24 BYTES.")));
}
CreateChannelAndConnectWithQuota(14);
channel_->SendFlowControl(43);
channel_->SendFlowControl(32);
}
TEST_F(WebSocketChannelFlowControlTest, EmptyMessageNoQuota) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText,
NOT_MASKED, "FIRST MESSAGE"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText,
NOT_MASKED, ""},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText,
NOT_MASKED, "THIRD MESSAGE"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
{
InSequence s;
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_,
OnDataFrame(false,
WebSocketFrameHeader::kOpCodeText,
AsVector("FIRST ")));
EXPECT_CALL(*event_interface_,
OnDataFrame(true,
WebSocketFrameHeader::kOpCodeContinuation,
AsVector("MESSAGE")));
EXPECT_CALL(*event_interface_,
OnDataFrame(true,
WebSocketFrameHeader::kOpCodeText,
AsVector("")));
EXPECT_CALL(*event_interface_,
OnDataFrame(true,
WebSocketFrameHeader::kOpCodeText,
AsVector("THIRD MESSAGE")));
}
CreateChannelAndConnectWithQuota(6);
channel_->SendFlowControl(128);
}
TEST_F(WebSocketChannelStreamTest, SentFramesAreMasked) {
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText,
MASKED, "NEEDS MASKING"}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("NEEDS MASKING"));
}
TEST_F(WebSocketChannelStreamTest, NothingIsSentAfterClose) {
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Success")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
CreateChannelAndConnectSuccessfully();
channel_->StartClosingHandshake(1000, "Success");
channel_->SendFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("SHOULD BE IGNORED"));
}
TEST_F(WebSocketChannelStreamTest, CloseIsEchoedBack) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Close")}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Close")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelStreamTest, CloseOnlySentOnce) {
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Close")}};
static const InitFrame frames_init[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Close")}};
CompletionCallback read_callback;
ScopedVector<WebSocketFrame>* frames = NULL;
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
Checkpoint checkpoint;
{
InSequence s;
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(DoAll(SaveArg<0>(&frames),
SaveArg<1>(&read_callback),
Return(ERR_IO_PENDING)));
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(checkpoint, Call(2));
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(checkpoint, Call(3));
}
CreateChannelAndConnectSuccessfully();
checkpoint.Call(1);
channel_->StartClosingHandshake(kWebSocketNormalClosure, "Close");
checkpoint.Call(2);
*frames = CreateFrameVector(frames_init);
read_callback.Run(OK);
checkpoint.Call(3);
}
TEST_F(WebSocketChannelStreamTest, InvalidCloseStatusCodeNotSent) {
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(SERVER_ERROR, "")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _));
CreateChannelAndConnectSuccessfully();
channel_->StartClosingHandshake(999, "");
}
TEST_F(WebSocketChannelStreamTest, LongCloseReasonNotSent) {
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(SERVER_ERROR, "")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _));
CreateChannelAndConnectSuccessfully();
channel_->StartClosingHandshake(1000, std::string(124, 'A'));
}
TEST_F(WebSocketChannelStreamTest, Code1005IsNotEchoed) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, ""}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED, ""}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelStreamTest, Code1005IsNotEchoedNull) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, NULL}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED, ""}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelStreamTest, CloseFrameInvalidUtf8) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "\xFF")}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in Close frame")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close());
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelStreamTest, PingRepliedWithPong) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePing,
NOT_MASKED, "Application data"}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePong,
MASKED, "Application data"}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelStreamTest, NullPingRepliedWithNullPong) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePing, NOT_MASKED, NULL}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, MASKED, NULL}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelStreamTest, PongInTheMiddleOfDataMessage) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePing,
NOT_MASKED, "Application data"}};
static const InitFrame expected1[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "Hello "}};
static const InitFrame expected2[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePong,
MASKED, "Application data"}};
static const InitFrame expected3[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
MASKED, "World"}};
ScopedVector<WebSocketFrame>* read_frames;
CompletionCallback read_callback;
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(DoAll(SaveArg<0>(&read_frames),
SaveArg<1>(&read_callback),
Return(ERR_IO_PENDING)))
.WillRepeatedly(Return(ERR_IO_PENDING));
{
InSequence s;
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected3), _))
.WillOnce(Return(OK));
}
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
false, WebSocketFrameHeader::kOpCodeText, AsVector("Hello "));
*read_frames = CreateFrameVector(frames);
read_callback.Run(OK);
channel_->SendFrame(
true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("World"));
}
TEST_F(WebSocketChannelStreamTest, WriteFramesOneAtATime) {
static const InitFrame expected1[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "Hello "}};
static const InitFrame expected2[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "World"}};
CompletionCallback write_callback;
Checkpoint checkpoint;
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
{
InSequence s;
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _))
.WillOnce(DoAll(SaveArg<1>(&write_callback), Return(ERR_IO_PENDING)));
EXPECT_CALL(checkpoint, Call(2));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _))
.WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(checkpoint, Call(3));
}
CreateChannelAndConnectSuccessfully();
checkpoint.Call(1);
channel_->SendFrame(
false, WebSocketFrameHeader::kOpCodeText, AsVector("Hello "));
channel_->SendFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("World"));
checkpoint.Call(2);
write_callback.Run(OK);
checkpoint.Call(3);
}
TEST_F(WebSocketChannelStreamTest, WaitingMessagesAreBatched) {
static const char input_letters[] = "Hello";
static const InitFrame expected1[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "H"}};
static const InitFrame expected2[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "e"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "l"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "l"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "o"}};
CompletionCallback write_callback;
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
{
InSequence s;
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _))
.WillOnce(DoAll(SaveArg<1>(&write_callback), Return(ERR_IO_PENDING)));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _))
.WillOnce(Return(ERR_IO_PENDING));
}
CreateChannelAndConnectSuccessfully();
for (size_t i = 0; i < strlen(input_letters); ++i) {
channel_->SendFrame(true,
WebSocketFrameHeader::kOpCodeText,
std::vector<char>(1, input_letters[i]));
}
write_callback.Run(OK);
}
TEST_F(WebSocketChannelStreamTest, SendGoingAwayOnRendererQuotaExceeded) {
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(GOING_AWAY, "")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close());
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(true,
WebSocketFrameHeader::kOpCodeText,
std::vector<char>(kDefaultInitialQuota + 1, 'C'));
}
TEST_F(WebSocketChannelStreamTest, WrittenBinaryFramesAre8BitClean) {
ScopedVector<WebSocketFrame>* frames = NULL;
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(_, _))
.WillOnce(DoAll(SaveArg<0>(&frames), Return(ERR_IO_PENDING)));
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
true,
WebSocketFrameHeader::kOpCodeBinary,
std::vector<char>(kBinaryBlob, kBinaryBlob + kBinaryBlobSize));
ASSERT_TRUE(frames != NULL);
ASSERT_EQ(1U, frames->size());
const WebSocketFrame* out_frame = (*frames)[0];
EXPECT_EQ(kBinaryBlobSize, out_frame->header.payload_length);
ASSERT_TRUE(out_frame->data);
EXPECT_EQ(0, memcmp(kBinaryBlob, out_frame->data->data(), kBinaryBlobSize));
}
TEST_F(WebSocketChannelEventInterfaceTest, ReadBinaryFramesAre8BitClean) {
scoped_ptr<WebSocketFrame> frame(
new WebSocketFrame(WebSocketFrameHeader::kOpCodeBinary));
WebSocketFrameHeader& frame_header = frame->header;
frame_header.final = true;
frame_header.payload_length = kBinaryBlobSize;
frame->data = new IOBuffer(kBinaryBlobSize);
memcpy(frame->data->data(), kBinaryBlob, kBinaryBlobSize);
ScopedVector<WebSocketFrame> frames;
frames.push_back(frame.release());
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
stream->PrepareRawReadFrames(
ReadableFakeWebSocketStream::SYNC, OK, frames.Pass());
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(_));
EXPECT_CALL(*event_interface_,
OnDataFrame(true,
WebSocketFrameHeader::kOpCodeBinary,
std::vector<char>(kBinaryBlob,
kBinaryBlob + kBinaryBlobSize)));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelSendUtf8Test, InvalidUtf8Rejected) {
EXPECT_CALL(
*event_interface_,
OnFailChannel("Browser sent a text frame containing invalid UTF-8"));
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("\xff"));
}
TEST_F(WebSocketChannelSendUtf8Test, IncompleteCharacterInFinalFrame) {
EXPECT_CALL(
*event_interface_,
OnFailChannel("Browser sent a text frame containing invalid UTF-8"));
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("\xc2"));
}
TEST_F(WebSocketChannelSendUtf8Test, IncompleteCharacterInNonFinalFrame) {
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
false, WebSocketFrameHeader::kOpCodeText, AsVector("\xc2"));
}
TEST_F(WebSocketChannelSendUtf8Test, ValidCharacterSplitBetweenFrames) {
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
false, WebSocketFrameHeader::kOpCodeText, AsVector("\xf1"));
channel_->SendFrame(true,
WebSocketFrameHeader::kOpCodeContinuation,
AsVector("\x80\xa0\xbf"));
}
TEST_F(WebSocketChannelSendUtf8Test, InvalidCharacterSplit) {
EXPECT_CALL(
*event_interface_,
OnFailChannel("Browser sent a text frame containing invalid UTF-8"));
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
false, WebSocketFrameHeader::kOpCodeText, AsVector("\xe1"));
channel_->SendFrame(true,
WebSocketFrameHeader::kOpCodeContinuation,
AsVector("\x80\xa0\xbf"));
}
TEST_F(WebSocketChannelSendUtf8Test, InvalidByteInContinuation) {
EXPECT_CALL(
*event_interface_,
OnFailChannel("Browser sent a text frame containing invalid UTF-8"));
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
false, WebSocketFrameHeader::kOpCodeText, AsVector("foo"));
channel_->SendFrame(
false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("bar"));
channel_->SendFrame(
true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("\xff"));
}
TEST_F(WebSocketChannelSendUtf8Test, BinaryContinuationNotChecked) {
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
false, WebSocketFrameHeader::kOpCodeBinary, AsVector("foo"));
channel_->SendFrame(
false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("bar"));
channel_->SendFrame(
true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("\xff"));
}
TEST_F(WebSocketChannelSendUtf8Test, ValidateMultipleTextMessages) {
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("foo"));
channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("bar"));
}
TEST_F(WebSocketChannelEventInterfaceTest, ReceivedInvalidUtf8) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xff"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota));
EXPECT_CALL(*event_interface_,
OnFailChannel("Could not decode a text frame as UTF-8."));
CreateChannelAndConnectSuccessfully();
base::MessageLoop::current()->RunUntilIdle();
}
TEST_F(WebSocketChannelStreamTest, InvalidUtf8TextFrameNotSent) {
static const InitFrame expected[] = {{FINAL_FRAME,
WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(GOING_AWAY, "")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close()).Times(1);
CreateChannelAndConnectSuccessfully();
channel_->SendFrame(
true, WebSocketFrameHeader::kOpCodeText, AsVector("\xff"));
}
TEST_F(WebSocketChannelReceiveUtf8Test, InvalidTextFrameRejected) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xff"}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED,
CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in text frame")}};
{
InSequence s;
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close()).Times(1);
}
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelReceiveUtf8Test, IncompleteCharacterReceived) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xc2"}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED,
CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in text frame")}};
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close()).Times(1);
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelReceiveUtf8Test, IncompleteCharacterIncompleteMessage) {
static const InitFrame frames[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xc2"}};
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelReceiveUtf8Test, TricksyIncompleteCharacter) {
static const InitFrame frames[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xc2"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, NOT_MASKED, ""}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED,
CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in text frame")}};
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close()).Times(1);
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelReceiveUtf8Test, ReceivedParsingContextRetained) {
static const InitFrame frames[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xf1"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "\x80\xa0\xbf"}};
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelReceiveUtf8Test, SplitInvalidCharacterReceived) {
static const InitFrame frames[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xe1"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "\x80\xa0\xbf"}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED,
CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in text frame")}};
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close()).Times(1);
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelReceiveUtf8Test, InvalidReceivedIncontinuation) {
static const InitFrame frames[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "foo"},
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "bar"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "\xff"}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED,
CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in text frame")}};
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close()).Times(1);
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelReceiveUtf8Test, ReceivedBinaryNotUtf8Tested) {
static const InitFrame frames[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeBinary, NOT_MASKED, "foo"},
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "bar"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "\xff"}};
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelReceiveUtf8Test, ValidateMultipleReceived) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "foo"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "bar"}};
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, BogusContinuation) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeBinary,
NOT_MASKED, "frame1"},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeText,
NOT_MASKED, "frame2"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota));
EXPECT_CALL(
*event_interface_,
OnDataFrame(
false, WebSocketFrameHeader::kOpCodeBinary, AsVector("frame1")));
EXPECT_CALL(
*event_interface_,
OnFailChannel(
"Received start of new message but previous message is unfinished."));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, MessageStartingWithContinuation) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, "continuation"}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota));
EXPECT_CALL(*event_interface_,
OnFailChannel("Received unexpected continuation frame."));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelEventInterfaceTest, DataFramesNonEmptyOrFinal) {
scoped_ptr<ReadableFakeWebSocketStream> stream(
new ReadableFakeWebSocketStream);
static const InitFrame frames[] = {
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, ""},
{NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
NOT_MASKED, ""},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, NOT_MASKED, ""}};
stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
set_stream(stream.Pass());
EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, _, _));
EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota));
EXPECT_CALL(
*event_interface_,
OnDataFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("")));
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelStreamTest, PingAfterCloseIsRejected) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")},
{FINAL_FRAME, WebSocketFrameHeader::kOpCodePing,
NOT_MASKED, "Ping body"}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
{
InSequence s;
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close()).Times(1);
}
CreateChannelAndConnectSuccessfully();
}
TEST_F(WebSocketChannelStreamTest, ProtocolError) {
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(PROTOCOL_ERROR, "WebSocket Protocol Error")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(Return(ERR_WS_PROTOCOL_ERROR));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close());
CreateChannelAndConnectSuccessfully();
}
class WebSocketChannelStreamTimeoutTest : public WebSocketChannelStreamTest {
protected:
WebSocketChannelStreamTimeoutTest() {}
virtual void CreateChannelAndConnectSuccessfully() OVERRIDE {
set_stream(mock_stream_.Pass());
CreateChannelAndConnect();
channel_->SendFlowControl(kPlentyOfQuota);
channel_->SetClosingHandshakeTimeoutForTesting(
TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis));
connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass());
}
};
TEST_F(WebSocketChannelStreamTimeoutTest, ServerInitiatedCloseTimesOut) {
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}};
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(ReturnFrames(&frames))
.WillRepeatedly(Return(ERR_IO_PENDING));
Checkpoint checkpoint;
TestClosure completion;
{
InSequence s;
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(checkpoint, Call(1));
EXPECT_CALL(*mock_stream_, Close())
.WillOnce(InvokeClosure(completion.closure()));
}
CreateChannelAndConnectSuccessfully();
checkpoint.Call(1);
completion.WaitForResult();
}
TEST_F(WebSocketChannelStreamTimeoutTest, ClientInitiatedCloseTimesOut) {
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillRepeatedly(Return(ERR_IO_PENDING));
TestClosure completion;
{
InSequence s;
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, Close())
.WillOnce(InvokeClosure(completion.closure()));
}
CreateChannelAndConnectSuccessfully();
channel_->StartClosingHandshake(kWebSocketNormalClosure, "OK");
completion.WaitForResult();
}
TEST_F(WebSocketChannelStreamTimeoutTest, ConnectionCloseTimesOut) {
static const InitFrame expected[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}};
static const InitFrame frames[] = {
{FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}};
EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
TestClosure completion;
ScopedVector<WebSocketFrame>* read_frames = NULL;
CompletionCallback read_callback;
{
InSequence s;
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(DoAll(SaveArg<0>(&read_frames),
SaveArg<1>(&read_callback),
Return(ERR_IO_PENDING)));
EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
.WillOnce(Return(OK));
EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
.WillOnce(Return(ERR_IO_PENDING));
EXPECT_CALL(*mock_stream_, Close())
.WillOnce(InvokeClosure(completion.closure()));
}
CreateChannelAndConnectSuccessfully();
channel_->StartClosingHandshake(kWebSocketNormalClosure, "OK");
ASSERT_TRUE(read_frames);
*read_frames = CreateFrameVector(frames);
read_callback.Run(OK);
completion.WaitForResult();
}
}
}