Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/rmq/rmqamqp/rmqamqp_channelmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ void ChannelMap::resumeReceiveChannels(const bool respectHostHealth)
}
}

bool ChannelMap::hasChannel(uint16_t channelId) const
{
ChannelPtrMap::const_iterator it = d_channels.find(channelId);

return it != d_channels.end() && it->second;
}

bool ChannelMap::processReceived(uint16_t channelId,
const rmqamqp::Message& message)
{
Expand Down
5 changes: 5 additions & 0 deletions src/rmq/rmqamqp/rmqamqp_channelmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class ChannelMap {
/// Call `open` on every channel stored in the container
void openAll();

/// Returns true if `channelId` has a channel associated with it.
/// `assignId` reserves an id before there is a channel for it, so a
/// reserved id returns false until `associateChannel` fills it in.
bool hasChannel(uint16_t channelId) const;

/// Call `processReceived` on the channel associated with `channelId`
/// Returns false if there is no channel associated with that id
bool processReceived(uint16_t channelId, const rmqamqp::Message& message);
Expand Down
25 changes: 25 additions & 0 deletions src/rmq/rmqamqp/rmqamqp_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,31 @@ void Connection::processNextFrame(const rmqamqpt::Frame& frame)
{
d_heartbeatManager->notifyMessageReceived();

if (frame.channel() == 0) {
// Channel 0 only carries connection methods and heartbeats. Reject
// anything else before the framer, which would buffer a content
// frame against channel 0 the same as any other id.
if (frame.type() != rmqamqpt::Constants::METHOD &&
frame.type() != rmqamqpt::Constants::HEARTBEAT) {
if (!isClosingState(d_state)) {
connectionException(
rmqamqpt::Constants::UNEXPECTED_FRAME,
"Received unexpected frame channel=" +
bsl::to_string(frame.channel()) + " frameType=" +
bsl::to_string(static_cast<int>(frame.type())));
}
return;
}
}
else if (!d_channels.hasChannel(frame.channel())) {
if (!isClosingState(d_state)) {
connectionException(rmqamqpt::Constants::CHANNEL_ERROR,
"Received frame on unknown channel: " +
bsl::to_string(frame.channel()));
}
return;
}

uint16_t channel = 0;
Message received;
const Framer::ReturnCode rc =
Expand Down
66 changes: 66 additions & 0 deletions src/tests/rmqamqp/rmqamqp_channelmap.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,72 @@ TEST(ChannelMap, OpenCall)
map.openAll();
}

TEST(ChannelMap, HasChannelUnknownId)
{
rmqamqp::ChannelMap map;

EXPECT_FALSE(map.hasChannel(1));
}

TEST(ChannelMap, HasChannelReservedIdIsNotAssociated)
{
rmqamqp::ChannelMap map;

EXPECT_THAT(map.assignId(), Eq(1));

// assignId reserves the id, but no channel has been associated yet
EXPECT_FALSE(map.hasChannel(1));
}

TEST(ChannelMap, HasChannelAssociatedId)
{
rmqamqp::ChannelMap map;
EXPECT_THAT(map.assignId(), Eq(1));
bsl::shared_ptr<rmqtestutil::MockSendChannel> channel =
bsl::make_shared<rmqtestutil::MockSendChannel>();
map.associateChannel(1, bsl::shared_ptr<rmqamqp::SendChannel>(channel));

EXPECT_TRUE(map.hasChannel(1));
EXPECT_FALSE(map.hasChannel(2));
}

TEST(ChannelMap, HasChannelAfterRemove)
{
rmqamqp::ChannelMap map;
EXPECT_THAT(map.assignId(), Eq(1));
bsl::shared_ptr<rmqtestutil::MockSendChannel> channel =
bsl::make_shared<rmqtestutil::MockSendChannel>();
map.associateChannel(1, bsl::shared_ptr<rmqamqp::SendChannel>(channel));

map.removeChannel(1);

EXPECT_FALSE(map.hasChannel(1));
}

TEST(ChannelMap, HasChannelAgreesWithProcessReceived)
{
rmqamqp::ChannelMap map;
rmqamqp::Message msg;

// Unknown, reserved and removed ids must all be rejected by both
EXPECT_FALSE(map.hasChannel(1));
EXPECT_FALSE(map.processReceived(1, msg));

EXPECT_THAT(map.assignId(), Eq(1));
EXPECT_FALSE(map.hasChannel(1));
EXPECT_FALSE(map.processReceived(1, msg));

bsl::shared_ptr<rmqtestutil::MockSendChannel> channel =
bsl::make_shared<rmqtestutil::MockSendChannel>();
map.associateChannel(1, bsl::shared_ptr<rmqamqp::SendChannel>(channel));

EXPECT_CALL(*channel, processReceived(_))
.WillOnce(Return(rmqamqp::Channel::KEEP));

EXPECT_TRUE(map.hasChannel(1));
EXPECT_TRUE(map.processReceived(1, msg));
}

TEST(ChannelMap, ProcessReceivedWithChannel)
{
rmqamqp::ChannelMap map;
Expand Down
227 changes: 227 additions & 0 deletions src/tests/rmqamqp/rmqamqp_connection.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,64 @@ class ConnectionTests : public ::testing::Test {
}
}

/// Queue an inbound content header announcing `bodySize` body bytes.
/// No body frames follow it, so the content stays incomplete.
void expectInboundContentHeader(uint16_t channel, size_t bodySize)
{
const rmqt::Message message(
bsl::make_shared<bsl::vector<uint8_t> >(bodySize, 0));

d_replayFrame.pushInbound(bsl::make_shared<SerializedFrame>(
Framer::makeContentHeaderFrame(message, channel)));
}

/// Queue an inbound content body frame of `bodySize` bytes
void expectInboundContentBody(uint16_t channel, size_t bodySize)
{
const bsl::vector<uint8_t> payload(bodySize, 0);

d_replayFrame.pushInbound(bsl::make_shared<SerializedFrame>(
Framer::makeContentBodyFrame(&payload[0],
Frame::calculateFrameSize(bodySize),
bodySize,
channel)));
}

/// Queue an inbound empty frame carrying a type the protocol does not
/// define. Built by hand because the Framer only emits valid types.
void expectInboundUndefinedFrameType(uint16_t channel)
{
const uint8_t undefinedType = 7;

bsl::vector<uint8_t> raw;
raw.push_back(undefinedType);
raw.push_back(static_cast<uint8_t>(channel >> 8));
raw.push_back(static_cast<uint8_t>(channel & 0xFF));
raw.push_back(0); // payload length, four octets
raw.push_back(0);
raw.push_back(0);
raw.push_back(0);
raw.push_back(Constants::FRAME_END);

ASSERT_THAT(raw.size(), Eq(Frame::frameOverhead()));

d_replayFrame.pushInbound(
bsl::make_shared<SerializedFrame>(&raw[0], raw.size()));
}

/// Queue the outbound Connection.Close the client is expected to send
/// when it rejects a frame
void expectConnectionCloseFrame(Constants::AMQPReplyCode replyCode,
const bsl::string& replyText)
{
ConnectionClose closeMethod(replyCode, replyText);
Frame closeFrame;
Framer::makeMethodFrame(
&closeFrame, 0, Method(ConnectionMethod(closeMethod)));
d_replayFrame.pushOutbound(
bsl::make_shared<SerializedFrame>(closeFrame));
}

bsl::shared_ptr<rmqamqp::Connection>
createAndStartConnection(const bsl::string& name = "test-connection")
{
Expand Down Expand Up @@ -1300,6 +1358,175 @@ TEST_F(ConnectionTests, AddSendChannelWhenConnected)
d_eventLoop.run();
}

TEST_F(ConnectionTests, ContentHeaderOnUnknownChannelClosesConnection)
{
expectFirstHandshakeFrames();

{
bsl::shared_ptr<rmqamqp::Connection> conn = createAndStartConnection();

// 1. Setup connection
d_eventLoop.run();
d_eventLoop.restart();

EXPECT_THAT(d_replayFrame.getLength(), Eq(0));

// No channel has been created, so channel 1 is unknown. The header
// must be rejected before the framer buffers anything for it.
expectInboundContentHeader(1, 64 * 1024 * 1024);

expectConnectionCloseFrame(Constants::CHANNEL_ERROR,
"Received frame on unknown channel: 1");
expectCloseOkFrame(false);

resetExpectations();
expectHandshakeFrames();

feedNextFrame();

// 2. Reject the frame, close and reconnect
d_eventLoop.run();
d_eventLoop.restart();

EXPECT_THAT(d_replayFrame.getLength(), Eq(0));
EXPECT_THAT(conn->state(), Eq(rmqamqp::Connection::CONNECTED));
expectShutdownCalls();
}

// 3. Process shutdown
d_eventLoop.run();
}

TEST_F(ConnectionTests, ContentHeaderOnChannelZeroClosesConnection)
{
expectFirstHandshakeFrames();

{
bsl::shared_ptr<rmqamqp::Connection> conn = createAndStartConnection();

// 1. Setup connection
d_eventLoop.run();
d_eventLoop.restart();

EXPECT_THAT(d_replayFrame.getLength(), Eq(0));

// Channel 0 is exempt from the unknown channel check, so content
// frames must be rejected on their own account
expectInboundContentHeader(0, 64 * 1024 * 1024);

expectConnectionCloseFrame(
Constants::UNEXPECTED_FRAME,
"Received unexpected frame channel=0 frameType=2");
expectCloseOkFrame(false);

resetExpectations();
expectHandshakeFrames();

feedNextFrame();

// 2. Reject the frame, close and reconnect
d_eventLoop.run();
d_eventLoop.restart();

EXPECT_THAT(d_replayFrame.getLength(), Eq(0));
EXPECT_THAT(conn->state(), Eq(rmqamqp::Connection::CONNECTED));
expectShutdownCalls();
}

// 3. Process shutdown
d_eventLoop.run();
}

TEST_F(ConnectionTests, FramesFollowingARejectionAreDiscarded)
{
expectFirstHandshakeFrames();

{
bsl::shared_ptr<rmqamqp::Connection> conn = createAndStartConnection();

// 1. Setup connection
d_eventLoop.run();
d_eventLoop.restart();

EXPECT_THAT(d_replayFrame.getLength(), Eq(0));

// A broker sends body frames straight after a content header, so the
// rejection has to cope with more frames arriving on the same unknown
// channel. Only the header may close the connection. The body which
// follows is discarded while the close is in flight.
expectInboundContentHeader(1, 4096);
expectConnectionCloseFrame(Constants::CHANNEL_ERROR,
"Received frame on unknown channel: 1");
expectInboundContentBody(1, 4096);

feedNextFrame();

// 2. Reject the header, which writes the close and feeds the body
d_eventLoop.run();
d_eventLoop.restart();

EXPECT_THAT(conn->state(),
Eq(rmqamqp::Connection::CONNECTION_CLOSE_SENT_RECONNECT));

expectCloseOkFrame(false);
resetExpectations();
expectHandshakeFrames();

feedNextFrame();

// 3. Accept close-ok and reconnect
d_eventLoop.run();
d_eventLoop.restart();

EXPECT_THAT(d_replayFrame.getLength(), Eq(0));
EXPECT_THAT(conn->state(), Eq(rmqamqp::Connection::CONNECTED));
expectShutdownCalls();
}

// 4. Process shutdown
d_eventLoop.run();
}

TEST_F(ConnectionTests, UndefinedFrameTypeOnChannelZeroClosesConnection)
{
expectFirstHandshakeFrames();

{
bsl::shared_ptr<rmqamqp::Connection> conn = createAndStartConnection();

// 1. Setup connection
d_eventLoop.run();
d_eventLoop.restart();

EXPECT_THAT(d_replayFrame.getLength(), Eq(0));

// Only method and heartbeat frames belong on channel 0, so a type
// the protocol does not define is rejected the same way content is
expectInboundUndefinedFrameType(0);

expectConnectionCloseFrame(
Constants::UNEXPECTED_FRAME,
"Received unexpected frame channel=0 frameType=7");
expectCloseOkFrame(false);

resetExpectations();
expectHandshakeFrames();

feedNextFrame();

// 2. Reject the frame, close and reconnect
d_eventLoop.run();
d_eventLoop.restart();

EXPECT_THAT(d_replayFrame.getLength(), Eq(0));
EXPECT_THAT(conn->state(), Eq(rmqamqp::Connection::CONNECTED));
expectShutdownCalls();
}

// 3. Process shutdown
d_eventLoop.run();
}

TEST_F(ConnectionTests, DisconnectTriggersChannelReset)
{
expectFirstHandshakeFrames();
Expand Down
Loading