summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2008-04-03 22:19:47 +0000
committerAlan Conway <aconway@apache.org>2008-04-03 22:19:47 +0000
commit83026d0f664b69cc8ed74b1ed52df212f0a66ab7 (patch)
tree757296a69f9499afb5e11202e9a89846c682b985
parent39c9c45c38d82914324cbdf4c89d5ad92d9f8e6d (diff)
downloadqpid-python-83026d0f664b69cc8ed74b1ed52df212f0a66ab7.tar.gz
qpid/Serializer.h, qpid/amqp_0_10/Codec.h:
- serializer enforces overrunning encode/decode limits. tests/amqp_0_10: Moved unit tests for amqp_0_10 namespace into amqp_0_10 directory. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@644533 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--cpp/src/qpid/Serializer.h47
-rw-r--r--cpp/src/qpid/amqp_0_10/Codec.h34
-rw-r--r--cpp/src/tests/Makefile.am6
-rw-r--r--cpp/src/tests/amqp_0_10/ProxyTemplate.cpp (renamed from cpp/src/tests/ProxyTemplate.cpp)0
-rw-r--r--cpp/src/tests/amqp_0_10/apply.cpp (renamed from cpp/src/tests/apply.cpp)0
-rw-r--r--cpp/src/tests/amqp_0_10/serialize.cpp (renamed from cpp/src/tests/serialize.cpp)34
6 files changed, 109 insertions, 12 deletions
diff --git a/cpp/src/qpid/Serializer.h b/cpp/src/qpid/Serializer.h
index 6ff701f346..d3e5264864 100644
--- a/cpp/src/qpid/Serializer.h
+++ b/cpp/src/qpid/Serializer.h
@@ -23,6 +23,7 @@
*/
#include <algorithm>
+#include "qpid/Exception.h" // FIXME aconway 2008-04-03: proper exception class.
namespace qpid {
@@ -52,6 +53,23 @@ SerializablePair<T,U> serializable(std::pair<T,U>& p) {
*/
template <class Derived> class Serializer {
public:
+ /** Temporarily set a lower relative limit on the serializer */
+ class ScopedLimit {
+ public:
+ ScopedLimit(Serializer& s, size_t l)
+ : serializer(s), save(serializer.setLimit(l)) {}
+
+ ~ScopedLimit() { serializer.setAbsLimit(save); }
+
+ private:
+ Serializer& serializer;
+ size_t save;
+ };
+
+ static const size_t maxLimit() { return std::numeric_limits<size_t>::max(); }
+
+ Serializer() : bytes(0), limit(maxLimit()) {}
+
typedef Derived& result_type; // unary functor requirement.
/** Wrapper functor to pass serializer functors by reference. */
@@ -74,8 +92,37 @@ template <class Derived> class Serializer {
return self();
}
+ /** Set limit relative to current position.
+ * @return old absolute limit.
+ */
+ size_t setLimit(size_t n) {
+ size_t l=limit;
+ limit = bytes+n;
+ return l;
+ }
+
+ /** Set absolute limit. */
+ void setAbsLimit(size_t n) {
+ limit = n;
+ if (bytes > limit)
+ throw Exception("Framing error: data overrun"); // FIXME aconway 2008-04-03: proper exception.
+ }
+
protected:
Derived& self() { return *static_cast<Derived*>(this); }
+ void addBytes(size_t n) {
+ size_t newBytes=bytes+n;
+ if (newBytes > limit)
+ throw Exception("Framing error: data overrun"); // FIXME aconway 2008-04-03: proper exception.
+ bytes = newBytes;
+ }
+
+ private:
+ void checkLimit() {
+ }
+
+ size_t bytes; // how many bytes serialized.
+ size_t limit; // bytes may not exceed this limit.
};
/**
diff --git a/cpp/src/qpid/amqp_0_10/Codec.h b/cpp/src/qpid/amqp_0_10/Codec.h
index a819aec845..622032fddc 100644
--- a/cpp/src/qpid/amqp_0_10/Codec.h
+++ b/cpp/src/qpid/amqp_0_10/Codec.h
@@ -52,16 +52,21 @@ struct Codec {
class Encoder : public EncoderBase<Encoder<OutIter> >
{
public:
- Encoder(OutIter o) : out(o) {}
+ typedef EncoderBase<Encoder<OutIter> > Base;
+ typedef OutIter Iterator;
+
+ Encoder(OutIter o, size_t limit=Base::maxLimit()) : out(o) {
+ this->setLimit(limit);
+ }
using EncoderBase<Encoder<OutIter> >::operator();
// FIXME aconway 2008-03-10: wrong encoding, need packing support
- Encoder& operator()(bool x) { *out++=x; return *this;}
+ Encoder& operator()(bool x) { raw(x); return *this;}
- Encoder& operator()(char x) { *out++=x; return *this; }
- Encoder& operator()(int8_t x) { *out++=x; return *this; }
- Encoder& operator()(uint8_t x) { *out++=x; return *this; }
+ Encoder& operator()(char x) { raw(x); return *this; }
+ Encoder& operator()(int8_t x) { raw(x); return *this; }
+ Encoder& operator()(uint8_t x) { raw(x); return *this; }
Encoder& operator()(int16_t x) { return endian(x); }
Encoder& operator()(int32_t x) { return endian(x); }
@@ -76,9 +81,12 @@ struct Codec {
void raw(const void* p, size_t n) {
+ this->addBytes(n);
out = std::copy((const char*)p, (const char*)p+n, out);
}
+ void raw(char b) { this->addBytes(1); *out++=b; }
+
OutIter pos() const { return out; }
private:
@@ -93,18 +101,21 @@ struct Codec {
template <class InIter>
class Decoder : public DecoderBase<Decoder<InIter> > {
public:
+ typedef DecoderBase<Decoder<InIter> > Base;
typedef InIter Iterator;
- Decoder(InIter i) : in(i) {}
+ Decoder(InIter i, size_t limit=Base::maxLimit()) : in(i) {
+ this->setLimit(limit);
+ }
using DecoderBase<Decoder<InIter> >::operator();
// FIXME aconway 2008-03-10: wrong encoding, need packing support
- Decoder& operator()(bool& x) { x=*in++; return *this; }
+ Decoder& operator()(bool& x) { raw((char&)x); return *this; }
- Decoder& operator()(char& x) { x=*in++; return *this; }
- Decoder& operator()(int8_t& x) { x=*in++; return *this; }
- Decoder& operator()(uint8_t& x) { x=*in++; return *this; }
+ Decoder& operator()(char& x) { raw((char&)x); return *this; }
+ Decoder& operator()(int8_t& x) { raw((char&)x); return *this; }
+ Decoder& operator()(uint8_t& x) { raw((char&)x); return *this; }
Decoder& operator()(int16_t& x) { return endian(x); }
Decoder& operator()(int32_t& x) { return endian(x); }
@@ -118,10 +129,13 @@ struct Codec {
Decoder& operator()(double& x) { return endian(x); }
void raw(void *p, size_t n) {
+ this->addBytes(n);
std::copy(in, in+n, (char*)p);
std::advance(in, n);
}
+ void raw(char &b) { this->addBytes(1); b=*in++; }
+
InIter pos() const { return in; }
private:
diff --git a/cpp/src/tests/Makefile.am b/cpp/src/tests/Makefile.am
index ae47241e67..fd3f67e99f 100644
--- a/cpp/src/tests/Makefile.am
+++ b/cpp/src/tests/Makefile.am
@@ -39,8 +39,10 @@ unit_test_SOURCES= unit_test.cpp unit_test.h \
ISList.cpp IList.cpp \
ClientSessionTest.cpp \
SequenceSet.cpp \
- serialize.cpp \
- ProxyTemplate.cpp apply.cpp BoundedIterator.cpp \
+ amqp_0_10/serialize.cpp \
+ amqp_0_10/ProxyTemplate.cpp \
+ amqp_0_10/apply.cpp \
+ BoundedIterator.cpp \
IncompleteMessageList.cpp \
amqp_0_10/Map.cpp
diff --git a/cpp/src/tests/ProxyTemplate.cpp b/cpp/src/tests/amqp_0_10/ProxyTemplate.cpp
index cab87b6511..cab87b6511 100644
--- a/cpp/src/tests/ProxyTemplate.cpp
+++ b/cpp/src/tests/amqp_0_10/ProxyTemplate.cpp
diff --git a/cpp/src/tests/apply.cpp b/cpp/src/tests/amqp_0_10/apply.cpp
index 450aeac356..450aeac356 100644
--- a/cpp/src/tests/apply.cpp
+++ b/cpp/src/tests/amqp_0_10/apply.cpp
diff --git a/cpp/src/tests/serialize.cpp b/cpp/src/tests/amqp_0_10/serialize.cpp
index 386f721338..a6cb28caee 100644
--- a/cpp/src/tests/serialize.cpp
+++ b/cpp/src/tests/amqp_0_10/serialize.cpp
@@ -97,6 +97,40 @@ BOOST_AUTO_TEST_CASE(testNetworkByteOrder) {
BOOST_CHECK_EQUAL(s, s2);
}
+BOOST_AUTO_TEST_CASE(testSetLimit) {
+ typedef Codec::Encoder<back_insert_iterator<string> > Encoder;
+ string data;
+ Encoder encode(back_inserter(data), 3);
+ encode('1')('2')('3');
+ try {
+ encode('4');
+ BOOST_FAIL("Expected exception");
+ } catch (...) {} // FIXME aconway 2008-04-03: catch proper exception
+ BOOST_CHECK_EQUAL(data, "123");
+}
+
+BOOST_AUTO_TEST_CASE(testScopedLimit) {
+ typedef Codec::Encoder<back_insert_iterator<string> > Encoder;
+ string data;
+ Encoder encode(back_inserter(data), 10);
+ encode(Str8("123")); // 4 bytes
+ {
+ Encoder::ScopedLimit l(encode, 3);
+ encode('a')('b')('c');
+ try {
+ encode('d');
+ BOOST_FAIL("Expected exception");
+ } catch(...) {} // FIXME aconway 2008-04-03: catch proper exception
+ }
+ BOOST_CHECK_EQUAL(data, "\003123abc");
+ encode('x')('y')('z');
+ try {
+ encode('!');
+ BOOST_FAIL("Expected exception");
+ } catch(...) {} // FIXME aconway 2008-04-03: catch proper exception
+ BOOST_CHECK_EQUAL(data.size(), 10u);
+}
+
// Assign test values to the various types.
void testValue(bool& b) { b = true; }
void testValue(Bit&) { }