From 83026d0f664b69cc8ed74b1ed52df212f0a66ab7 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Thu, 3 Apr 2008 22:19:47 +0000 Subject: 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 --- cpp/src/qpid/Serializer.h | 47 ++++++ cpp/src/qpid/amqp_0_10/Codec.h | 34 ++-- cpp/src/tests/Makefile.am | 6 +- cpp/src/tests/ProxyTemplate.cpp | 49 ------ cpp/src/tests/amqp_0_10/ProxyTemplate.cpp | 49 ++++++ cpp/src/tests/amqp_0_10/apply.cpp | 99 +++++++++++ cpp/src/tests/amqp_0_10/serialize.cpp | 268 ++++++++++++++++++++++++++++++ cpp/src/tests/apply.cpp | 99 ----------- cpp/src/tests/serialize.cpp | 234 -------------------------- 9 files changed, 491 insertions(+), 394 deletions(-) delete mode 100644 cpp/src/tests/ProxyTemplate.cpp create mode 100644 cpp/src/tests/amqp_0_10/ProxyTemplate.cpp create mode 100644 cpp/src/tests/amqp_0_10/apply.cpp create mode 100644 cpp/src/tests/amqp_0_10/serialize.cpp delete mode 100644 cpp/src/tests/apply.cpp delete mode 100644 cpp/src/tests/serialize.cpp (limited to 'cpp/src') 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 +#include "qpid/Exception.h" // FIXME aconway 2008-04-03: proper exception class. namespace qpid { @@ -52,6 +53,23 @@ SerializablePair serializable(std::pair& p) { */ template 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::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 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(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 > { public: - Encoder(OutIter o) : out(o) {} + typedef EncoderBase > Base; + typedef OutIter Iterator; + + Encoder(OutIter o, size_t limit=Base::maxLimit()) : out(o) { + this->setLimit(limit); + } using EncoderBase >::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 Decoder : public DecoderBase > { public: + typedef DecoderBase > Base; typedef InIter Iterator; - Decoder(InIter i) : in(i) {} + Decoder(InIter i, size_t limit=Base::maxLimit()) : in(i) { + this->setLimit(limit); + } using DecoderBase >::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/ProxyTemplate.cpp deleted file mode 100644 index cab87b6511..0000000000 --- a/cpp/src/tests/ProxyTemplate.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -#include "unit_test.h" -#include "qpid/amqp_0_10/ProxyTemplate.h" -#include - -QPID_AUTO_TEST_SUITE(ProxyTemplateTestSuite) - -using namespace qpid::amqp_0_10; - -struct ToAny { - template - boost::any operator()(const T& t) { return boost::any(t); } -}; - -struct AnyProxy : public ProxyTemplate {}; - -BOOST_AUTO_TEST_CASE(testAnyProxy) { - AnyProxy p; - boost::any a=p.connectionTune(1,2,3,4); - BOOST_CHECK_EQUAL(a.type().name(), typeid(connection::Tune).name()); - connection::Tune* tune=boost::any_cast(&a); - BOOST_REQUIRE(tune); - BOOST_CHECK_EQUAL(tune->channelMax, 1u); - BOOST_CHECK_EQUAL(tune->maxFrameSize, 2u); - BOOST_CHECK_EQUAL(tune->heartbeatMin, 3u); - BOOST_CHECK_EQUAL(tune->heartbeatMax, 4u); -} - -QPID_AUTO_TEST_SUITE_END() diff --git a/cpp/src/tests/amqp_0_10/ProxyTemplate.cpp b/cpp/src/tests/amqp_0_10/ProxyTemplate.cpp new file mode 100644 index 0000000000..cab87b6511 --- /dev/null +++ b/cpp/src/tests/amqp_0_10/ProxyTemplate.cpp @@ -0,0 +1,49 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include "unit_test.h" +#include "qpid/amqp_0_10/ProxyTemplate.h" +#include + +QPID_AUTO_TEST_SUITE(ProxyTemplateTestSuite) + +using namespace qpid::amqp_0_10; + +struct ToAny { + template + boost::any operator()(const T& t) { return boost::any(t); } +}; + +struct AnyProxy : public ProxyTemplate {}; + +BOOST_AUTO_TEST_CASE(testAnyProxy) { + AnyProxy p; + boost::any a=p.connectionTune(1,2,3,4); + BOOST_CHECK_EQUAL(a.type().name(), typeid(connection::Tune).name()); + connection::Tune* tune=boost::any_cast(&a); + BOOST_REQUIRE(tune); + BOOST_CHECK_EQUAL(tune->channelMax, 1u); + BOOST_CHECK_EQUAL(tune->maxFrameSize, 2u); + BOOST_CHECK_EQUAL(tune->heartbeatMin, 3u); + BOOST_CHECK_EQUAL(tune->heartbeatMax, 4u); +} + +QPID_AUTO_TEST_SUITE_END() diff --git a/cpp/src/tests/amqp_0_10/apply.cpp b/cpp/src/tests/amqp_0_10/apply.cpp new file mode 100644 index 0000000000..450aeac356 --- /dev/null +++ b/cpp/src/tests/amqp_0_10/apply.cpp @@ -0,0 +1,99 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +#include "unit_test.h" +#include "qpid/amqp_0_10/specification.h" +#include "qpid/amqp_0_10/ApplyControl.h" + +QPID_AUTO_TEST_SUITE(VisitorTestSuite) + +using namespace qpid::amqp_0_10; + +struct GetCode : public ApplyFunctor { + template uint8_t operator()(const T&) const { return T::CODE; } +}; + +struct SetChannelMax : ApplyFunctor { + template void operator()(T&) const { BOOST_FAIL(""); } + void operator()(connection::Tune& t) const { t.channelMax=42; } +}; + +struct TestFunctor { + typedef bool result_type; + bool operator()(const connection::Tune& tune) { + BOOST_CHECK_EQUAL(tune.channelMax, 1u); + BOOST_CHECK_EQUAL(tune.maxFrameSize, 2u); + BOOST_CHECK_EQUAL(tune.heartbeatMin, 3u); + BOOST_CHECK_EQUAL(tune.heartbeatMax, 4u); + return true; + } + template + bool operator()(const T&) { return false; } +}; + +BOOST_AUTO_TEST_CASE(testApply) { + connection::Tune tune(1,2,3,4); + Control* p = &tune; + + // boost oddity - without the cast we get undefined symbol errors. + BOOST_CHECK_EQUAL(apply(GetCode(), *p), (uint8_t)connection::Tune::CODE); + + TestFunctor tf; + BOOST_CHECK(apply(tf, *p)); + + connection::Start start; + p = &start; + BOOST_CHECK(!apply(tf, *p)); + + apply(SetChannelMax(), tune); + BOOST_CHECK_EQUAL(tune.channelMax, 42); +} + +struct VoidTestFunctor { + typedef void result_type; + + int code; + VoidTestFunctor() : code() {} + + void operator()(const connection::Tune& tune) { + BOOST_CHECK_EQUAL(tune.channelMax, 1u); + BOOST_CHECK_EQUAL(tune.maxFrameSize, 2u); + BOOST_CHECK_EQUAL(tune.heartbeatMin, 3u); + BOOST_CHECK_EQUAL(tune.heartbeatMax, 4u); + code=connection::Tune::CODE; + } + template + void operator()(const T&) { code=0xFF; } +}; + +BOOST_AUTO_TEST_CASE(testApplyVoid) { + connection::Tune tune(1,2,3,4); + Control* p = &tune; + VoidTestFunctor tf; + apply(tf, *p); + BOOST_CHECK_EQUAL(uint8_t(connection::Tune::CODE), tf.code); + + connection::Start start; + p = &start; + apply(tf, *p); + BOOST_CHECK_EQUAL(0xFF, tf.code); +} + +QPID_AUTO_TEST_SUITE_END() diff --git a/cpp/src/tests/amqp_0_10/serialize.cpp b/cpp/src/tests/amqp_0_10/serialize.cpp new file mode 100644 index 0000000000..a6cb28caee --- /dev/null +++ b/cpp/src/tests/amqp_0_10/serialize.cpp @@ -0,0 +1,268 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include "unit_test.h" +#include "qpid/amqp_0_10/built_in_types.h" +#include "qpid/amqp_0_10/Codec.h" +#include "qpid/amqp_0_10/PackedCodec.h" +#include "qpid/amqp_0_10/specification.h" +#include "qpid/amqp_0_10/ControlHolder.h" +#include "qpid/amqp_0_10/Frame.h" +#include "qpid/amqp_0_10/Map.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Missing operators needed for tests. +namespace boost { +template +std::ostream& operator<<(std::ostream& out, const array& a) { + std::ostream_iterator o(out, " "); + std::copy(a.begin(), a.end(), o); + return out; +} +} // boost + +namespace qpid { +namespace sys { + +std::ostream& operator<<(std::ostream& out, const AbsTime& t) { + return out << t.timeValue(); +} +} + +} // qpid + +QPID_AUTO_TEST_SUITE(SerializeTestSuite) + +using namespace std; +namespace mpl=boost::mpl; +using namespace qpid::amqp_0_10; +using qpid::framing::in_place; + +template struct concat2 { typedef typename mpl::copy >::type type; }; +template struct concat3 { typedef typename concat2::type>::type type; }; +template struct concat4 { typedef typename concat2::type>::type type; }; + +typedef mpl::vector::type IntegralTypes; +typedef mpl::vector::type BinTypes; +typedef mpl::vector::type FloatTypes; +typedef mpl::vector FixedSizeClassTypes; +typedef mpl::vector VariableSizeTypes; + +typedef concat4::type FixedSizeTypes; +typedef concat2::type AllTypes; + +// TODO aconway 2008-02-20: should test 64 bit integrals for order also. +BOOST_AUTO_TEST_CASE(testNetworkByteOrder) { + string data; + + uint32_t l = 0x11223344; + Codec::encode(std::back_inserter(data))(l); + uint32_t enc=reinterpret_cast(*data.data()); + uint32_t l2 = ntohl(enc); + BOOST_CHECK_EQUAL(l, l2); + + data.clear(); + uint16_t s = 0x1122; + Codec::encode(std::back_inserter(data))(s); + uint32_t s2 = ntohs(*reinterpret_cast(data.data())); + BOOST_CHECK_EQUAL(s, s2); +} + +BOOST_AUTO_TEST_CASE(testSetLimit) { + typedef Codec::Encoder > 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 > 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&) { } +template typename boost::enable_if >::type testValue(T& n) { n=42; } +void testValue(CharUtf32& c) { c = 43; } +void testValue(long long& l) { l = 0x012345; } +void testValue(Datetime& dt) { dt = qpid::sys::now(); } +void testValue(Uuid& uuid) { uuid=Uuid(true); } +template void testValue(Decimal& d) { d.exponent=2; d.mantissa=0x1122; } +void testValue(SequenceNo& s) { s = 42; } +template void testValue(Bin& a) { a.assign(42); } +template void testValue(SerializableString& s) { + char msg[]="foobar"; + s.assign(msg, msg+sizeof(msg)); +} +void testValue(Str16& s) { s = "the quick brown fox jumped over the lazy dog"; } +void testValue(Str8& s) { s = "foobar"; } +void testValue(Map& m) { m["s"] = Str8("foobar"); m["b"] = true; m["c"] = uint16_t(42); } + +//typedef mpl::vector::type TestTypes; +BOOST_AUTO_TEST_CASE_TEMPLATE(testEncodeDecode, T, AllTypes) +{ + string data; + T t; + testValue(t); + Codec::encode(std::back_inserter(data))(t); + + BOOST_CHECK_EQUAL(Codec::size(t), data.size()); + + T t2; + Codec::decode(data.begin())(t2); + BOOST_CHECK_EQUAL(t,t2); +} + +struct TestMe { + bool encoded, decoded; + char value; + TestMe(char v) : encoded(), decoded(), value(v) {} + template void encode(S& s) const { + const_cast(this)->encoded=true; s(value); + } + template void decode(S& s) { decoded=true; s(value); } + template void serialize(S& s) { s.split(*this); } +}; + +BOOST_AUTO_TEST_CASE(testSplit) { + string data; + TestMe t1('x'); + Codec::encode(std::back_inserter(data))(t1); + BOOST_CHECK(t1.encoded); + BOOST_CHECK(!t1.decoded); + BOOST_CHECK_EQUAL(data, "x"); + + TestMe t2('y'); + Codec::decode(data.begin())(t2); + BOOST_CHECK(!t2.encoded); + BOOST_CHECK(t2.decoded); + BOOST_CHECK_EQUAL(t2.value, 'x'); +} + +BOOST_AUTO_TEST_CASE(testControlEncodeDecode) { + string data; + Control::Holder h(in_place(1,2,3,4)); + Codec::encode(std::back_inserter(data))(h); + + BOOST_CHECK_EQUAL(data.size(), Codec::size(h)); + + Codec::Decoder decode(data.begin()); + Control::Holder h2; + decode(h2); + + BOOST_REQUIRE(h2.get()); + BOOST_CHECK_EQUAL(h2.get()->getClassCode(), connection::CODE); + BOOST_CHECK_EQUAL(h2.get()->getCode(), uint8_t(connection::Tune::CODE)); + connection::Tune& tune=static_cast(*h2.get()); + BOOST_CHECK_EQUAL(tune.channelMax, 1u); + BOOST_CHECK_EQUAL(tune.maxFrameSize, 2u); + BOOST_CHECK_EQUAL(tune.heartbeatMin, 3u); + BOOST_CHECK_EQUAL(tune.heartbeatMax, 4u); +} + +BOOST_AUTO_TEST_CASE(testFrameEncodeDecode) { + static const int overhead=12; + string data; + Frame r, c; + char d1[]="abcdefg"; + r.refer(d1, d1+sizeof(d1)); + r.setFlags(Frame::FIRST_FRAME); + r.setType(CONTROL); + r.setChannel(32); + r.setTrack(1); + char d2[]="01234567"; + c.copy(d2, d2+sizeof(d2)); + + BOOST_CHECK_EQUAL(overhead+sizeof(d1), Codec::size(r)); + BOOST_CHECK_EQUAL(overhead+sizeof(d2), Codec::size(c)); + Codec::encode(std::back_inserter(data))(r)(c); + BOOST_CHECK_EQUAL(data.size(), Codec::size(r)+Codec::size(c)); + + FrameHeader fh; + std::string::iterator i = Codec::decode(data.begin())(fh).pos(); + size_t s = fh.size(); + BOOST_CHECK_EQUAL(s, sizeof(d1)); + BOOST_CHECK_EQUAL(std::string(i, i+s), std::string(d1, d1+s)); + + + Frame f1, f2; + Codec::decode(data.begin())(f1)(f2); + BOOST_CHECK_EQUAL(f1.size(), sizeof(d1)); + BOOST_CHECK_EQUAL(std::string(f1.begin(), f1.size()), + std::string(d1, sizeof(d1))); + BOOST_CHECK_EQUAL(f1.size(), r.size()); + BOOST_CHECK_EQUAL(f1.getFlags(), Frame::FIRST_FRAME); + BOOST_CHECK_EQUAL(f1.getType(), CONTROL); + BOOST_CHECK_EQUAL(f1.getChannel(), 32); + BOOST_CHECK_EQUAL(f1.getTrack(), 1); + + BOOST_CHECK_EQUAL(f2.size(), c.size()); + BOOST_CHECK_EQUAL(std::string(f2.begin(), f2.end()), + std::string(d2, d2+sizeof(d2))); + +} + +BOOST_AUTO_TEST_CASE(testPackedCodec) { + int i=-1, j=-1, k=-1, l=-1; + std::string data; + Codec::encode(std::back_inserter(data))(1)(3); + PackedCodec::decode(0x5, Codec::decode(data.begin()))(i)(j)(k)(l); + BOOST_CHECK_EQUAL(i, 1); + BOOST_CHECK_EQUAL(j, 0); + BOOST_CHECK_EQUAL(k, 3); + BOOST_CHECK_EQUAL(l, 0); +} + +QPID_AUTO_TEST_SUITE_END() diff --git a/cpp/src/tests/apply.cpp b/cpp/src/tests/apply.cpp deleted file mode 100644 index 450aeac356..0000000000 --- a/cpp/src/tests/apply.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -#include "unit_test.h" -#include "qpid/amqp_0_10/specification.h" -#include "qpid/amqp_0_10/ApplyControl.h" - -QPID_AUTO_TEST_SUITE(VisitorTestSuite) - -using namespace qpid::amqp_0_10; - -struct GetCode : public ApplyFunctor { - template uint8_t operator()(const T&) const { return T::CODE; } -}; - -struct SetChannelMax : ApplyFunctor { - template void operator()(T&) const { BOOST_FAIL(""); } - void operator()(connection::Tune& t) const { t.channelMax=42; } -}; - -struct TestFunctor { - typedef bool result_type; - bool operator()(const connection::Tune& tune) { - BOOST_CHECK_EQUAL(tune.channelMax, 1u); - BOOST_CHECK_EQUAL(tune.maxFrameSize, 2u); - BOOST_CHECK_EQUAL(tune.heartbeatMin, 3u); - BOOST_CHECK_EQUAL(tune.heartbeatMax, 4u); - return true; - } - template - bool operator()(const T&) { return false; } -}; - -BOOST_AUTO_TEST_CASE(testApply) { - connection::Tune tune(1,2,3,4); - Control* p = &tune; - - // boost oddity - without the cast we get undefined symbol errors. - BOOST_CHECK_EQUAL(apply(GetCode(), *p), (uint8_t)connection::Tune::CODE); - - TestFunctor tf; - BOOST_CHECK(apply(tf, *p)); - - connection::Start start; - p = &start; - BOOST_CHECK(!apply(tf, *p)); - - apply(SetChannelMax(), tune); - BOOST_CHECK_EQUAL(tune.channelMax, 42); -} - -struct VoidTestFunctor { - typedef void result_type; - - int code; - VoidTestFunctor() : code() {} - - void operator()(const connection::Tune& tune) { - BOOST_CHECK_EQUAL(tune.channelMax, 1u); - BOOST_CHECK_EQUAL(tune.maxFrameSize, 2u); - BOOST_CHECK_EQUAL(tune.heartbeatMin, 3u); - BOOST_CHECK_EQUAL(tune.heartbeatMax, 4u); - code=connection::Tune::CODE; - } - template - void operator()(const T&) { code=0xFF; } -}; - -BOOST_AUTO_TEST_CASE(testApplyVoid) { - connection::Tune tune(1,2,3,4); - Control* p = &tune; - VoidTestFunctor tf; - apply(tf, *p); - BOOST_CHECK_EQUAL(uint8_t(connection::Tune::CODE), tf.code); - - connection::Start start; - p = &start; - apply(tf, *p); - BOOST_CHECK_EQUAL(0xFF, tf.code); -} - -QPID_AUTO_TEST_SUITE_END() diff --git a/cpp/src/tests/serialize.cpp b/cpp/src/tests/serialize.cpp deleted file mode 100644 index 386f721338..0000000000 --- a/cpp/src/tests/serialize.cpp +++ /dev/null @@ -1,234 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -#include "unit_test.h" -#include "qpid/amqp_0_10/built_in_types.h" -#include "qpid/amqp_0_10/Codec.h" -#include "qpid/amqp_0_10/PackedCodec.h" -#include "qpid/amqp_0_10/specification.h" -#include "qpid/amqp_0_10/ControlHolder.h" -#include "qpid/amqp_0_10/Frame.h" -#include "qpid/amqp_0_10/Map.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// Missing operators needed for tests. -namespace boost { -template -std::ostream& operator<<(std::ostream& out, const array& a) { - std::ostream_iterator o(out, " "); - std::copy(a.begin(), a.end(), o); - return out; -} -} // boost - -namespace qpid { -namespace sys { - -std::ostream& operator<<(std::ostream& out, const AbsTime& t) { - return out << t.timeValue(); -} -} - -} // qpid - -QPID_AUTO_TEST_SUITE(SerializeTestSuite) - -using namespace std; -namespace mpl=boost::mpl; -using namespace qpid::amqp_0_10; -using qpid::framing::in_place; - -template struct concat2 { typedef typename mpl::copy >::type type; }; -template struct concat3 { typedef typename concat2::type>::type type; }; -template struct concat4 { typedef typename concat2::type>::type type; }; - -typedef mpl::vector::type IntegralTypes; -typedef mpl::vector::type BinTypes; -typedef mpl::vector::type FloatTypes; -typedef mpl::vector FixedSizeClassTypes; -typedef mpl::vector VariableSizeTypes; - -typedef concat4::type FixedSizeTypes; -typedef concat2::type AllTypes; - -// TODO aconway 2008-02-20: should test 64 bit integrals for order also. -BOOST_AUTO_TEST_CASE(testNetworkByteOrder) { - string data; - - uint32_t l = 0x11223344; - Codec::encode(std::back_inserter(data))(l); - uint32_t enc=reinterpret_cast(*data.data()); - uint32_t l2 = ntohl(enc); - BOOST_CHECK_EQUAL(l, l2); - - data.clear(); - uint16_t s = 0x1122; - Codec::encode(std::back_inserter(data))(s); - uint32_t s2 = ntohs(*reinterpret_cast(data.data())); - BOOST_CHECK_EQUAL(s, s2); -} - -// Assign test values to the various types. -void testValue(bool& b) { b = true; } -void testValue(Bit&) { } -template typename boost::enable_if >::type testValue(T& n) { n=42; } -void testValue(CharUtf32& c) { c = 43; } -void testValue(long long& l) { l = 0x012345; } -void testValue(Datetime& dt) { dt = qpid::sys::now(); } -void testValue(Uuid& uuid) { uuid=Uuid(true); } -template void testValue(Decimal& d) { d.exponent=2; d.mantissa=0x1122; } -void testValue(SequenceNo& s) { s = 42; } -template void testValue(Bin& a) { a.assign(42); } -template void testValue(SerializableString& s) { - char msg[]="foobar"; - s.assign(msg, msg+sizeof(msg)); -} -void testValue(Str16& s) { s = "the quick brown fox jumped over the lazy dog"; } -void testValue(Str8& s) { s = "foobar"; } -void testValue(Map& m) { m["s"] = Str8("foobar"); m["b"] = true; m["c"] = uint16_t(42); } - -//typedef mpl::vector::type TestTypes; -BOOST_AUTO_TEST_CASE_TEMPLATE(testEncodeDecode, T, AllTypes) -{ - string data; - T t; - testValue(t); - Codec::encode(std::back_inserter(data))(t); - - BOOST_CHECK_EQUAL(Codec::size(t), data.size()); - - T t2; - Codec::decode(data.begin())(t2); - BOOST_CHECK_EQUAL(t,t2); -} - -struct TestMe { - bool encoded, decoded; - char value; - TestMe(char v) : encoded(), decoded(), value(v) {} - template void encode(S& s) const { - const_cast(this)->encoded=true; s(value); - } - template void decode(S& s) { decoded=true; s(value); } - template void serialize(S& s) { s.split(*this); } -}; - -BOOST_AUTO_TEST_CASE(testSplit) { - string data; - TestMe t1('x'); - Codec::encode(std::back_inserter(data))(t1); - BOOST_CHECK(t1.encoded); - BOOST_CHECK(!t1.decoded); - BOOST_CHECK_EQUAL(data, "x"); - - TestMe t2('y'); - Codec::decode(data.begin())(t2); - BOOST_CHECK(!t2.encoded); - BOOST_CHECK(t2.decoded); - BOOST_CHECK_EQUAL(t2.value, 'x'); -} - -BOOST_AUTO_TEST_CASE(testControlEncodeDecode) { - string data; - Control::Holder h(in_place(1,2,3,4)); - Codec::encode(std::back_inserter(data))(h); - - BOOST_CHECK_EQUAL(data.size(), Codec::size(h)); - - Codec::Decoder decode(data.begin()); - Control::Holder h2; - decode(h2); - - BOOST_REQUIRE(h2.get()); - BOOST_CHECK_EQUAL(h2.get()->getClassCode(), connection::CODE); - BOOST_CHECK_EQUAL(h2.get()->getCode(), uint8_t(connection::Tune::CODE)); - connection::Tune& tune=static_cast(*h2.get()); - BOOST_CHECK_EQUAL(tune.channelMax, 1u); - BOOST_CHECK_EQUAL(tune.maxFrameSize, 2u); - BOOST_CHECK_EQUAL(tune.heartbeatMin, 3u); - BOOST_CHECK_EQUAL(tune.heartbeatMax, 4u); -} - -BOOST_AUTO_TEST_CASE(testFrameEncodeDecode) { - static const int overhead=12; - string data; - Frame r, c; - char d1[]="abcdefg"; - r.refer(d1, d1+sizeof(d1)); - r.setFlags(Frame::FIRST_FRAME); - r.setType(CONTROL); - r.setChannel(32); - r.setTrack(1); - char d2[]="01234567"; - c.copy(d2, d2+sizeof(d2)); - - BOOST_CHECK_EQUAL(overhead+sizeof(d1), Codec::size(r)); - BOOST_CHECK_EQUAL(overhead+sizeof(d2), Codec::size(c)); - Codec::encode(std::back_inserter(data))(r)(c); - BOOST_CHECK_EQUAL(data.size(), Codec::size(r)+Codec::size(c)); - - FrameHeader fh; - std::string::iterator i = Codec::decode(data.begin())(fh).pos(); - size_t s = fh.size(); - BOOST_CHECK_EQUAL(s, sizeof(d1)); - BOOST_CHECK_EQUAL(std::string(i, i+s), std::string(d1, d1+s)); - - - Frame f1, f2; - Codec::decode(data.begin())(f1)(f2); - BOOST_CHECK_EQUAL(f1.size(), sizeof(d1)); - BOOST_CHECK_EQUAL(std::string(f1.begin(), f1.size()), - std::string(d1, sizeof(d1))); - BOOST_CHECK_EQUAL(f1.size(), r.size()); - BOOST_CHECK_EQUAL(f1.getFlags(), Frame::FIRST_FRAME); - BOOST_CHECK_EQUAL(f1.getType(), CONTROL); - BOOST_CHECK_EQUAL(f1.getChannel(), 32); - BOOST_CHECK_EQUAL(f1.getTrack(), 1); - - BOOST_CHECK_EQUAL(f2.size(), c.size()); - BOOST_CHECK_EQUAL(std::string(f2.begin(), f2.end()), - std::string(d2, d2+sizeof(d2))); - -} - -BOOST_AUTO_TEST_CASE(testPackedCodec) { - int i=-1, j=-1, k=-1, l=-1; - std::string data; - Codec::encode(std::back_inserter(data))(1)(3); - PackedCodec::decode(0x5, Codec::decode(data.begin()))(i)(j)(k)(l); - BOOST_CHECK_EQUAL(i, 1); - BOOST_CHECK_EQUAL(j, 0); - BOOST_CHECK_EQUAL(k, 3); - BOOST_CHECK_EQUAL(l, 0); -} - -QPID_AUTO_TEST_SUITE_END() -- cgit v1.2.1