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
4 changes: 1 addition & 3 deletions src/rmq/rmqamqpt/rmqamqpt_basicproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,7 @@ bool BasicProperties::decode(BasicProperties* props,
}
}
if (flags & maskForProperty(TIMESTAMP)) {
properties.timestamp = bdlt::Datetime();
if (!rmqamqpt::Types::decodeTimestamp(&*properties.timestamp,
&buffer)) {
if (!rmqamqpt::Types::decodeTimestamp(&properties.timestamp, &buffer)) {
BALL_LOG_ERROR << "Decoding fail for basic property: "
<< PROPERTY_NAMES[TIMESTAMP];
success = false;
Expand Down
30 changes: 24 additions & 6 deletions src/rmq/rmqamqpt/rmqamqpt_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <bdlb_bigendian.h>
#include <bdlt_epochutil.h>
#include <bsls_assert.h>
#include <bsls_review.h>

#include <bsl_algorithm.h>
#include <bsl_cstddef.h>
Expand Down Expand Up @@ -291,11 +292,16 @@ bool Types::decodeFieldValue(rmqt::FieldValue* outValue,
*outValue = val;
} break;
case rmqamqpt::FieldValue::TIMESTAMP: {
bdlt::Datetime timestamp;
bsl::optional<bdlt::Datetime> timestamp;
if (!decodeTimestamp(&timestamp, buffer)) {
return false;
}
*outValue = timestamp;
if (timestamp.has_value()) {
*outValue = timestamp.value();
}
else {
outValue->reset();
}
} break;
case rmqamqpt::FieldValue::FIELD_TABLE: {
bsl::shared_ptr<rmqt::FieldTable> table =
Expand Down Expand Up @@ -550,7 +556,8 @@ void Types::encodeTimestamp(Writer& output, const bdlt::Datetime& timestamp)
bdlt::EpochUtil::convertToTimeT64(timestamp)));
}

bool Types::decodeTimestamp(bdlt::Datetime* timestamp, rmqamqpt::Buffer* buffer)
bool Types::decodeTimestamp(bsl::optional<bdlt::Datetime>* timestamp,
rmqamqpt::Buffer* buffer)
{
BSLS_ASSERT(timestamp);

Expand All @@ -561,12 +568,23 @@ bool Types::decodeTimestamp(bdlt::Datetime* timestamp, rmqamqpt::Buffer* buffer)
return false;
}

if (0 != bdlt::EpochUtil::convertFromTimeT64(timestamp, seconds)) {
BALL_LOG_ERROR << "Timestamp not representable as a datetime: "
bdlt::Datetime value;
const bool representable =
0 == bdlt::EpochUtil::convertFromTimeT64(&value, seconds);

BSLS_REVIEW_OPT(representable &&
"AMQP timestamp outside the bdlt::Datetime range");

if (!representable) {
BALL_LOG_ERROR << "Timestamp not representable as a datetime, "
"decoding as unset: "
<< seconds;
return false;
timestamp->reset();
return true;
}

*timestamp = value;

return true;
}

Expand Down
18 changes: 17 additions & 1 deletion src/rmq/rmqamqpt/rmqamqpt_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
#include <rmqt_fieldvalue.h>

#include <bsl_cstdint.h>
#include <bsl_optional.h>
#include <bsl_sstream.h>
#include <bsl_string.h>
#include <bsl_vector.h>

#include <bdlb_bigendian.h>
#include <bdlt_datetime.h>
#include <bsl_cstddef.h>
#include <bsl_type_traits.h>

Expand Down Expand Up @@ -57,6 +59,12 @@ class Types {
static void encodeByteVector(Writer& output,
const bsl::vector<bsl::uint8_t>& data);

/// Read a field value out of `buffer` and advance past it.
///
/// A value this library cannot represent is still consumed, so that
/// whatever follows it decodes, and `value` is left unset. An unset
/// value cannot be told apart from the `NO_FIELD` ('V') type, so
/// re-encoding writes it back out as 'V' and the original is lost.
static bool decodeFieldValue(rmqt::FieldValue* value,
rmqamqpt::Buffer* buffer);

Expand All @@ -75,7 +83,15 @@ class Types {
static void encodeTimestamp(Writer& output,
const bdlt::Datetime& timestamp);

static bool decodeTimestamp(bdlt::Datetime* timestamp,
/// Read an AMQP `timestamp` out of `buffer` and advance past it.
///
/// Return false when there is not enough data for the field, leaving
/// `buffer` untouched. Nothing further can be decoded from it.
///
/// Return true when the field was read, leaving `buffer` positioned at
/// whatever follows it. A value outside the range a `bdlt::Datetime`
/// can hold is not a decode failure: `timestamp` is left unset.
static bool decodeTimestamp(bsl::optional<bdlt::Datetime>* timestamp,
rmqamqpt::Buffer* buffer);

template <typename T>
Expand Down
70 changes: 70 additions & 0 deletions src/tests/rmqamqp/rmqamqp_framer.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ class ContentDecodeTests : public ::testing::Test {

return rmqamqpt::Frame(frameType, channel, data);
}
/// Build a content header frame around an already encoded payload.
rmqamqpt::Frame makeRawHeaderFrame(uint16_t channel,
const uint8_t* payload,
bsl::size_t payloadSize)
{
bsl::shared_ptr<bsl::vector<uint8_t> > data =
bsl::make_shared<bsl::vector<uint8_t> >();

rmqamqpt::Writer writer(data.get());
rmqamqpt::Types::write(
writer, static_cast<uint8_t>(rmqamqpt::Constants::HEADER));
rmqamqpt::Types::write(writer, bdlb::BigEndianUint16::make(channel));
rmqamqpt::Types::write(writer,
bdlb::BigEndianUint32::make(payloadSize));

for (bsl::size_t i = 0; i < payloadSize; ++i) {
rmqamqpt::Types::write(writer, payload[i]);
}
rmqamqpt::Types::write(writer, rmqamqpt::Constants::FRAME_END);

assert(data->size() ==
rmqamqpt::Frame::calculateFrameSize(payloadSize));

return rmqamqpt::Frame(rmqamqpt::Constants::HEADER, channel, data);
}

rmqamqpt::Frame makeBodyFrame(rmqamqpt::Constants::AMQPFrameType frameType,
uint16_t channel,
const rmqamqpt::ContentBody& contentBody)
Expand Down Expand Up @@ -348,6 +374,50 @@ TEST_F(ContentDecodeTests, ContentHeaderBodyEncodeDecode)
EXPECT_EQ(memcmp(message.payload(), data, 5), 0);
}

TEST_F(ContentDecodeTests,
ContentHeaderUnrepresentableTimestampIsNotAnException)
{
// A timestamp the decoder cannot represent is a limit of the type
// decoded into, not a protocol error, so it must not fail the channel.
// The encoder cannot express this header, so it is written by hand.
bsl::vector<uint8_t> payload;
rmqamqpt::Writer payloadWriter(&payload);
rmqamqpt::Types::write(payloadWriter,
bdlb::BigEndianUint16::make(static_cast<uint16_t>(
rmqamqpt::Constants::BASIC)));
rmqamqpt::Types::write(payloadWriter, bdlb::BigEndianUint16::make(0));
rmqamqpt::Types::write(payloadWriter, bdlb::BigEndianUint64::make(5));
rmqamqpt::Types::write(payloadWriter,
bdlb::BigEndianUint16::make(0x0040)); // timestamp
rmqamqpt::Types::write(payloadWriter,
bdlb::BigEndianInt64::make(1795442484034LL));

uint16_t channel;
rmqamqp::Framer framer;
rmqamqp::Message received;

rmqamqpt::Frame frame =
makeRawHeaderFrame(2, payload.data(), payload.size());

EXPECT_THAT(framer.appendFrame(&channel, &received, frame),
Eq(rmqamqp::Framer::PARTIAL));
EXPECT_EQ(channel, 2);

const uint8_t* body = reinterpret_cast<const uint8_t*>("hello");
rmqamqpt::ContentBody contentBody(body, 5);
frame = makeBodyFrame(rmqamqpt::Constants::BODY, 2, contentBody);

EXPECT_THAT(framer.appendFrame(&channel, &received, frame),
Eq(rmqamqp::Framer::OK));
EXPECT_EQ(channel, 2);

ASSERT_TRUE(received.is<rmqt::Message>());
rmqt::Message message = received.the<rmqt::Message>();
EXPECT_EQ(message.payloadSize(), 5);
EXPECT_EQ(memcmp(message.payload(), body, 5), 0);
EXPECT_TRUE(message.properties().timestamp.isNull());
}

TEST_F(ContentDecodeTests, ContentBodyWithoutHeader)
{
rmqamqpt::Frame frame;
Expand Down
28 changes: 28 additions & 0 deletions src/tests/rmqamqpt/rmqamqpt_basicproperties.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,34 @@ TEST(Methods_BasicProperties, TruncatedScalarPropertiesFailToDecode)
}
}

TEST(Methods_BasicProperties, OutOfRangeTimestampKeepsOtherProperties)
{
// A timestamp in milliseconds is out of range read as seconds. The
// properties either side of it must survive. The encoder cannot
// express this, so the property block is written by hand.
const uint16_t flags = 0x0080 | 0x0040 | 0x0008;

bsl::vector<uint8_t> data;
rmqamqpt::Writer writer(&data);
rmqamqpt::Types::write(writer, bdlb::BigEndianUint16::make(flags));
rmqamqpt::Types::encodeShortString(writer, "mid");
rmqamqpt::Types::write(writer, bdlb::BigEndianInt64::make(1795442484034LL));
rmqamqpt::Types::encodeShortString(writer, "app");

rmqamqpt::BasicProperties basicProps;
EXPECT_THAT(rmqamqpt::BasicProperties::decode(
&basicProps, data.data(), data.size()),
Eq(true));

EXPECT_FALSE(basicProps.timestamp());

ASSERT_TRUE(basicProps.messageId());
EXPECT_THAT(basicProps.messageId().value(), Eq("mid"));

ASSERT_TRUE(basicProps.appId());
EXPECT_THAT(basicProps.appId().value(), Eq("app"));
}

TEST(Methods_BasicProperties, TruncatedHeaderTableLengthFailsToDecode)
{
// headers flag, with a partial field table length prefix
Expand Down
87 changes: 71 additions & 16 deletions src/tests/rmqamqpt/rmqamqpt_types.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
#include <bdlb_bigendian.h>
#include <bdlt_datetime.h>

#include <bsls_types.h>

#include <bsl_cstddef.h>
#include <bsl_cstring.h>
#include <bsl_iostream.h>
#include <bsl_limits.h>
#include <bsl_optional.h>
#include <bsl_vector.h>

#include <bsl_iosfwd.h>
Expand Down Expand Up @@ -609,19 +612,20 @@ TEST(TypesEncoding, Timestamp)
EXPECT_THAT(storage[i], Eq(inspect[i]));
}

bdlt::Datetime decoded;
bsl::optional<bdlt::Datetime> decoded;
rmqamqpt::Buffer buf(storage.data(), storage.size());
EXPECT_THAT(rmqamqpt::Types::decodeTimestamp(&decoded, &buf), Eq(true));

EXPECT_THAT(decoded, Eq(millennium));
ASSERT_TRUE(decoded.has_value());
EXPECT_THAT(decoded.value(), Eq(millennium));
}

TEST(TypesTruncatedDecode, TruncatedTimestampFails)
{
const uint8_t truncated[] = {0x00, 0x00, 0x00, 0x00, 0x38, 0x6D, 0x43};

for (bsl::size_t length = 0; length < sizeof(truncated); ++length) {
bdlt::Datetime decoded;
bsl::optional<bdlt::Datetime> decoded;
rmqamqpt::Buffer buffer(truncated, length);

EXPECT_THAT(rmqamqpt::Types::decodeTimestamp(&decoded, &buffer),
Expand All @@ -631,32 +635,83 @@ TEST(TypesTruncatedDecode, TruncatedTimestampFails)
}
}

TEST(TypesTruncatedDecode, OutOfRangeTimestampFails)
// A timestamp in milliseconds rather than the seconds the protocol
// specifies. Any millisecond stamp after 1978-01-11 is out of range.
const bsls::Types::Int64 MILLISECOND_TIMESTAMP = 1795442484034LL;

TEST(TypesTruncatedDecode, OutOfRangeTimestampDecodesAsUnset)
{
// Seconds since the epoch that no bdlt::Datetime can represent
const uint8_t cases[][8] = {
{0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
const bsls::Types::Int64 cases[] = {
bsl::numeric_limits<bsls::Types::Int64>::max(),
bsl::numeric_limits<bsls::Types::Int64>::min(),
MILLISECOND_TIMESTAMP};

for (bsl::size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
bdlt::Datetime decoded;
rmqamqpt::Buffer buffer(cases[i], sizeof(cases[i]));
BufferType storage;
rmqamqpt::Writer writer(&storage);
rmqamqpt::Types::write(writer, bdlb::BigEndianInt64::make(cases[i]));

// Starts set, to show it is cleared
bsl::optional<bdlt::Datetime> decoded = bdlt::Datetime(2000, 1, 1);
rmqamqpt::Buffer buffer(storage.data(), storage.size());

EXPECT_THAT(rmqamqpt::Types::decodeTimestamp(&decoded, &buffer),
Eq(false))
Eq(true))
<< "case: " << i;
EXPECT_FALSE(decoded.has_value()) << "case: " << i;
EXPECT_THAT(buffer.available(), Eq(0u)) << "case: " << i;
}
}

TEST(TypesTruncatedDecode, OutOfRangeTimestampFieldValueFails)
TEST(TypesTruncatedDecode, OutOfRangeTimestampFieldValueDecodesAsUnset)
{
const uint8_t data[] = {
'T', 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
BufferType storage;
rmqamqpt::Writer writer(&storage);
rmqamqpt::Types::write(writer, rmqamqpt::FieldValue::TIMESTAMP);
rmqamqpt::Types::write(writer,
bdlb::BigEndianInt64::make(MILLISECOND_TIMESTAMP));

rmqt::FieldValue value;
rmqamqpt::Buffer buffer(data, sizeof(data));
rmqamqpt::Buffer buffer(storage.data(), storage.size());

EXPECT_THAT(rmqamqpt::Types::decodeFieldValue(&value, &buffer), Eq(true));
EXPECT_TRUE(value.isUnset());
EXPECT_THAT(buffer.available(), Eq(0u));
}

TEST(TypesFieldTableDecode, OutOfRangeTimestampAndLaterFieldsStillDecode)
{
// The 8 octets must still be consumed so that whatever follows the
// timestamp in the table is read from the right place.
BufferType fields;
rmqamqpt::Writer fieldWriter(&fields);
rmqamqpt::Types::encodeShortString(fieldWriter, "at");
rmqamqpt::Types::write(fieldWriter, rmqamqpt::FieldValue::TIMESTAMP);
rmqamqpt::Types::write(fieldWriter,
bdlb::BigEndianInt64::make(MILLISECOND_TIMESTAMP));
rmqamqpt::Types::encodeShortString(fieldWriter, "x");
rmqamqpt::Types::write(fieldWriter, rmqamqpt::FieldValue::SHORT_SHORT_INT);
rmqamqpt::Types::write(fieldWriter, static_cast<signed char>(7));

BufferType storage;
rmqamqpt::Writer writer(&storage);
rmqamqpt::Types::write(
writer,
bdlb::BigEndianUint32::make(static_cast<bsl::uint32_t>(fields.size())));
for (bsl::size_t i = 0; i < fields.size(); ++i) {
rmqamqpt::Types::write(writer, fields[i]);
}

rmqamqpt::Buffer buffer(storage.data(), storage.size());
rmqt::FieldTable ft;

EXPECT_THAT(rmqamqpt::Types::decodeFieldValue(&value, &buffer), Eq(false));
EXPECT_THAT(rmqamqpt::Types::decodeFieldTable(&ft, &buffer), Eq(true));
EXPECT_THAT(ft.size(), Eq(2u));

EXPECT_TRUE(ft["at"].isUnset());

EXPECT_TRUE(ft["x"].is<bsl::int8_t>());
EXPECT_THAT(ft["x"].the<bsl::int8_t>(), Eq(7));
}

TEST(TypesFieldTableDecode, DecimalIsSkippedAndLaterFieldsStillDecode)
Expand Down
Loading