X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Packets%2F80221Bundle%2FTLVPacket.test.cc;h=8dc8df2c7b86af83115639d38544ec82f36e2005;hb=fd3a0e8ac95d1158e9ea661ddf9187b67c70169f;hp=f96e3b56f1498e76740cfa2dc9d72a7611a1feeb;hpb=bd67664cb4ea1b36c9bb52a622e02341e4f0131f;p=senf.git diff --git a/Packets/80221Bundle/TLVPacket.test.cc b/Packets/80221Bundle/TLVPacket.test.cc index f96e3b5..8dc8df2 100644 --- a/Packets/80221Bundle/TLVPacket.test.cc +++ b/Packets/80221Bundle/TLVPacket.test.cc @@ -20,14 +20,15 @@ // Free Software Foundation, Inc., // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -// Unit tests +/** \file + \brief TLVPacket unit tests */ //#include "TLVPacket.test.hh" //#include "TLVPacket.test.ih" // Custom includes #include "TLVPacket.hh" -#include +#include "../DefaultBundle/EthernetPacket.hh" #include "../../Utils/auto_unit_test.hh" #include @@ -37,23 +38,27 @@ using namespace senf; +namespace { void check_TLVPacket(GenericTLVPacket &tlvPacket, boost::uint8_t type, boost::uint32_t length) { BOOST_CHECK_EQUAL( tlvPacket->type(), type ); BOOST_CHECK_EQUAL( tlvPacket->length(), length ); - BOOST_CHECK_EQUAL( tlvPacket->value().size(), length ); + BOOST_CHECK_EQUAL( tlvPacket->value().size(), int(length) ); + std::ostringstream oss (std::ostringstream::out); + SENF_CHECK_NO_THROW( tlvPacket.dump( oss)); senf::PacketData::iterator dataIterator (tlvPacket->value().begin()); for (unsigned i=0; i one byte length following 0x0A, // length (10 bytes value) 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value - }; + }; GenericTLVPacket tlvPacket (GenericTLVPacket::create(data)); check_TLVPacket( tlvPacket, 0x01, 0x0Au ); } @@ -78,7 +83,7 @@ BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_extended_length) BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length) { - unsigned char value[] = { + unsigned char value[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; GenericTLVPacket tlvPacket (GenericTLVPacket::create()); @@ -87,8 +92,8 @@ BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length) tlvPacket.finalizeThis(); check_TLVPacket( tlvPacket, 42u, 0x0Au ); - - unsigned char data[] = { + + unsigned char data[] = { 0x2a, // type 0x0A, // first bit not set, length=10 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value @@ -96,122 +101,100 @@ BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length) BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data )); } -#if 0 -BOOST_AUTO_UNIT_TEST(TLVPacket_create_packet_with_extended_length) + +BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_extended_length) { + unsigned char value[255]; + for (unsigned i=0; imaxLengthValue( DynamicTLVLengthParser::max_value); tlvPacket->type() = 42u; - for (uint8_t i=0; i<129; i++) - tlvPacket->value().push_back( i); - tlvPacket.finalizeAll(); - - check_TLVPacket( tlvPacket, 42u, 129u ); -} + tlvPacket->value( value); + tlvPacket.finalizeThis(); + check_TLVPacket( tlvPacket, 42u, sizeof(value) ); -BOOST_AUTO_UNIT_TEST(TLVPacket_create_invalid_packet) -{ - + unsigned char data[] = { + 0x2a, // type + 0x81, // first and last bit set => one byte length following + 0xff, // length (255 bytes value) + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // first bytes of value + }; + BOOST_CHECK( equal( + tlvPacket.data().begin(), + boost::next( tlvPacket.data().begin(), sizeof(data)), + data )); } -BOOST_AUTO_UNIT_TEST(TLVFixPacket_static) +BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_invalid_packet) { - // check static values: - typedef ConcretePacket > TestTLVPacket8; - typedef ConcretePacket > TestTLVPacket24; - - BOOST_CHECK_EQUAL( TestTLVPacket8::type::initSize(), 4+1u ); - BOOST_CHECK_EQUAL( TestTLVPacket24::type::initSize(), 4+3u ); -} + GenericTLVPacket tlvPacket (GenericTLVPacket::create()); + tlvPacket->type() = 42u; + tlvPacket.finalizeThis(); + unsigned char value[255]; + for (unsigned i=0; i -void test_TLVFixPacket_parsing(unsigned lengthParser_size) -{ - std::vector data; - data.push_back(0x01); data.push_back(0x23); data.push_back(0x45); data.push_back(0x67); // type - data.insert(data.end(), lengthParser_size-1, 0x00); - data.push_back(0x0A); // length - for( int i=0; i < 10; i++ ) { - data.push_back(i); // payload - } - TLVFixPacketType tlvPacket (TLVFixPacketType::create( - boost::make_iterator_range(data.begin(), data.end()))); - check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au ); + BOOST_CHECK_THROW( tlvPacket->value( value), TLVLengthException); + tlvPacket->maxLengthValue( sizeof(value)); + tlvPacket->value( value); + tlvPacket.finalizeThis(); + check_TLVPacket( tlvPacket, 42u, sizeof(value) ); } -BOOST_AUTO_UNIT_TEST(TLVFixPacket_parse_packet) -{ - typedef ConcretePacket > TestTLVPacket8; - typedef ConcretePacket > TestTLVPacket16; - typedef ConcretePacket > TestTLVPacket24; - typedef ConcretePacket > TestTLVPacket32; - - test_TLVFixPacket_parsing( UInt8Parser::fixed_bytes); - test_TLVFixPacket_parsing( UInt16Parser::fixed_bytes); - test_TLVFixPacket_parsing( UInt24Parser::fixed_bytes); - test_TLVFixPacket_parsing( UInt32Parser::fixed_bytes); -} +namespace { -template -void test_TLVFixPacket_creating() -{ - std::string payload ("Hello, world!"); - TLVFixPacketType tlvPacket (TLVFixPacketType::create()); - tlvPacket->type() = 42u; - DataPacket::createAfter( tlvPacket, payload ); - tlvPacket.finalizeAll(); - - BOOST_CHECK_EQUAL( tlvPacket->type(), 42u); - BOOST_CHECK_EQUAL( tlvPacket->length(), 13u); - - PacketData & tlvPacket_value (tlvPacket.next().data()); - BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() )); -} + struct TestMacAddressTLVPacketParser : public BaseTLVPacketParser + { + # include SENF_PARSER() + SENF_PARSER_INHERIT ( BaseTLVPacketParser ); + SENF_PARSER_VECTOR ( value, bytes(length), senf::MACAddressParser ); + SENF_PARSER_FINALIZE( TestMacAddressTLVPacketParser ); + }; -BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_packet) -{ - typedef ConcretePacket > TestTLVPacket8; - typedef ConcretePacket > TestTLVPacket16; - typedef ConcretePacket > TestTLVPacket24; - typedef ConcretePacket > TestTLVPacket32; - - test_TLVFixPacket_creating(); - test_TLVFixPacket_creating(); - test_TLVFixPacket_creating(); - test_TLVFixPacket_creating(); -} + struct TestMacAddressTLVPacketType + : public PacketTypeBase, + public PacketTypeMixin + { + typedef PacketTypeMixin mixin; + typedef TestMacAddressTLVPacketParser parser; + using mixin::nextPacketRange; + using mixin::init; + using mixin::initSize; -template -void test_invalid_TLVFixPacket_creating(boost::uint32_t max_value) -{ - TLVFixPacketType tlvPacket (TLVFixPacketType::create()); - tlvPacket->type() = 42u; - DataPacket payload (DataPacket::createAfter( tlvPacket, max_value+1)); - //DataPacket::createAfter( payload, 1); // this is one byte to much. - BOOST_CHECK_THROW( tlvPacket.finalizeAll(), UnsuportedTLVPacketException); + static void finalize(ConcretePacket p) { + p->shrinkLength(); + } + }; + typedef ConcretePacket TestMacAddressTLVPacket; } -BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_invalid_packet) +BOOST_AUTO_UNIT_TEST(TestMacAddressTLVPacket_create) { - typedef ConcretePacket > TestTLVPacket8; - typedef ConcretePacket > TestTLVPacket16; - typedef ConcretePacket > TestTLVPacket24; - - test_invalid_TLVFixPacket_creating ( UInt8Parser::max_value); - test_invalid_TLVFixPacket_creating( UInt16Parser::max_value); - test_invalid_TLVFixPacket_creating( UInt24Parser::max_value); + TestMacAddressTLVPacket tlvPacket (TestMacAddressTLVPacket::create()); + tlvPacket->type() = 42; + tlvPacket->value().push_back( senf::MACAddress::from_string("01:23:45:67:89:ab") ); + tlvPacket->value().push_back( senf::MACAddress::from_string("cd:ef:01:23:45:67") ); + tlvPacket.finalizeThis(); + + unsigned char data[] = { + 0x2a, // type + 0x0c, // length + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67 // value + }; + BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data )); } -#endif ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ - + // Local Variables: // mode: c++ // fill-column: 100