Packets/DefaultBundle: Implement ICMPv6Packet::create unit test
g0dil [Thu, 24 Sep 2009 10:15:34 +0000 (10:15 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1441 270642c3-0616-0410-b53a-bc976706d245

senf/Packets/DefaultBundle/ICMPv6Packet.cc
senf/Packets/DefaultBundle/ICMPv6Packet.test.cc
senf/Packets/DefaultBundle/ICMPv6TypePacket.cc
senf/Packets/DefaultBundle/ICMPv6TypePacket.hh

index d35e2ae..8c41023 100644 (file)
@@ -30,6 +30,7 @@
 #include <boost/io/ios_state.hpp>
 #include <senf/Packets/DefaultBundle/IPv6Packet.hh>
 #include <senf/Utils/IpChecksum.hh>
+#include <iomanip>
 
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
@@ -73,9 +74,10 @@ prefix_ void senf::ICMPv6PacketType::dump(packet p, std::ostream &os)
 {
     boost::io::ios_all_saver ias(os);
     os << "ICMPv6 protocol:\n"
-       <<     "  type                    : " << (unsigned) p->type() <<"\n"
-       <<     "  code                    : " << (unsigned) p->code() <<"\n"
-       <<     "  checksum                : " << (unsigned) p->checksum() << "\n";
+       <<     "  type                    : " << unsigned(p->type()) <<"\n"
+       <<     "  code                    : " << unsigned(p->code()) <<"\n"
+       <<     "  checksum                : 0x" 
+       << std::hex << std::setw(4) << unsigned(p->checksum()) << "\n";
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
index 9402d96..9b61d53 100644 (file)
@@ -24,6 +24,7 @@
     \brief ICMPv6Packet unit tests */
 
 // Custom includes
+#include <sstream>
 #include "ICMPv6Packet.hh"
 #include "ICMPv6TypePacket.hh"
 
@@ -157,6 +158,65 @@ BOOST_AUTO_UNIT_TEST(ICMPv6Packet_packet)
     
 }
 
+BOOST_AUTO_UNIT_TEST(ICMPv6Packet_create)
+{
+    unsigned char ping[] = { 0x60, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0x40,
+                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+                             0x80, 0x00, 0xda, 0xe0, 0x9f, 0x7e, 0x00, 0x09,
+                             
+                             0xb7, 0x3c, 0xbb, 0x4a, 0x9d, 0x90, 0x0a, 0x00, //payload
+                             0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+                             0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+                             0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+                             0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
+                             0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+                             0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
+    };
+
+    senf::IPv6Packet ip (senf::IPv6Packet::create());
+    ip->hopLimit() = 64;
+    ip->source() = senf::INet6Address::Loopback;
+    ip->destination() = senf::INet6Address::Loopback;
+
+    senf::ICMPv6Packet icmp (senf::ICMPv6Packet::createAfter(ip));
+    icmp->code() = 0;
+    
+    senf::ICMPv6EchoRequest ereq (senf::ICMPv6EchoRequest::createAfter(icmp));
+    ereq->identifier() = 0x9f7e;
+    ereq->seqNr() = 9;
+
+    senf::DataPacket data (
+        senf::DataPacket::createAfter(ereq, std::make_pair(ping+48, ping+sizeof(ping))));
+
+    ip.finalizeAll();
+    
+    std::stringstream ss;
+    ip.dump(ss);
+    BOOST_CHECK_EQUAL( ss.str(), 
+                       "Internet protocol Version 6:\n"
+                       "  version                 : 6\n"
+                       "  traffic class           : 0x00\n"
+                       "  flow label              : 0x00000\n"
+                       "  payload length          : 64\n"
+                       "  next header             : 58\n"
+                       "  hop limit               : 64\n"
+                       "  source                  : ::1\n"
+                       "  destination             : ::1\n"
+                       "ICMPv6 protocol:\n"
+                       "  type                    : 128\n"
+                       "  code                    : 0\n"
+                       "  checksum                : 0xdae0\n"
+                       "ICMPv6 Echo Request:\n"
+                       "  identifier              : 40830\n"
+                       "  sequence nr.            : 9\n"
+                       "Payload data (56 bytes)\n" );
+    SENF_CHECK_EQUAL_COLLECTIONS( ip.data().begin(), ip.data().end(),
+                                  ping, ping+sizeof(ping) );
+}
+
 // Local Variables:
 // mode: c++
 // fill-column: 100
index 57d1dca..a995c38 100644 (file)
 // Definition of non-inline non-template functions
 
 // Custom includes
+#include <boost/io/ios_state.hpp>
 #include <senf/Packets/Packets.hh>
 #include "ICMPv6Packet.hh"
 #include "ICMPv6TypePacket.hh"
 
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
 #ifndef DOXYGEN
 
-namespace {
-    SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 1,   senf::ICMPv6ErrDestUnreachable );
-    SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 2,   senf::ICMPv6ErrTooBig          );
-    SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 3,   senf::ICMPv6ErrTimeExceeded    );
-    SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 4,   senf::ICMPv6ErrParamProblem    );
-    SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 128, senf::ICMPv6EchoRequest        );
-    SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 129, senf::ICMPv6EchoReply          );
-    SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 130, senf::MLDv2ListenerQuery       );
-    SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 143, senf::MLDv2ListenerReport      );
-}
+SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 1,   senf::ICMPv6ErrDestUnreachable );
+SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 2,   senf::ICMPv6ErrTooBig          );
+SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 3,   senf::ICMPv6ErrTimeExceeded    );
+SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 4,   senf::ICMPv6ErrParamProblem    );
+SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 128, senf::ICMPv6EchoRequest        );
+SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 129, senf::ICMPv6EchoReply          );
+SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 130, senf::MLDv2ListenerQuery       );
+SENF_PACKET_REGISTRY_REGISTER( senf::ICMPTypes, 143, senf::MLDv2ListenerReport      );
 
 #endif
+
+prefix_ void senf::ICMPv6EchoRequestType::dump(packet p, std::ostream & os)
+{
+    boost::io::ios_all_saver ias(os);
+    os << "ICMPv6 Echo Request:\n"
+       <<     "  identifier              : " << p->identifier() << "\n"
+       <<     "  sequence nr.            : " << p->seqNr() << "\n";
+}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+
+
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
+// End:
index a87ad56..067f34e 100644 (file)
@@ -66,6 +66,8 @@ namespace senf {
         using mixin::nextPacketRange;
         using mixin::init;
         using mixin::initSize;
+
+        static void dump(packet p, std::ostream & os);
     };
     
     typedef ConcretePacket<ICMPv6EchoRequestType> ICMPv6EchoRequest;