Packets/80221Bundle: set messageId on finalize
[senf.git] / Packets / Packet.test.cc
index 8c28f73..6aecfbe 100644 (file)
 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 /** \file
-    \brief Packet.test unit tests */
+    \brief Packet unit tests */
 
 //#include "Packet.test.hh"
 //#include "Packet.test.ih"
 
 // Custom includes
 #include <sstream>
+#include <boost/static_assert.hpp>
 #include "Packets.hh"
 
 #include "../Utils/auto_unit_test.hh"
@@ -89,8 +90,8 @@ namespace {
         using mixin::init;
         static void dump(packet p, std::ostream & os) {
             os << "BarPacket:\n"
-               << "type: " << p->type() << "\n"
-               << "length: " << p->length() << "\n";
+               << "  type: " << p->type() << "\n"
+               << "  length: " << p->length() << "\n";
         }
         static void finalize(packet p) {
             if (p.next(senf::nothrow))
@@ -112,25 +113,46 @@ namespace {
     struct IntAnnotation {
         unsigned value;
     };
+
+    std::ostream & operator<<(std::ostream & os, IntAnnotation const & v)
+    { os << v.value; return os; }
     
     struct LargeAnnotation {
         char value[32];
     };
 
+    std::ostream & operator<<(std::ostream & os, LargeAnnotation const & v)
+    { os << v.value; return os; }
+
     struct ComplexAnnotation : senf::ComplexAnnotation
     {
+        ComplexAnnotation() : s(), i() {}
         std::string s;
         int i;
     };
 
+    std::ostream & operator<<(std::ostream & os, ComplexAnnotation const & v)
+    { os << "('" << v.s << "' " << v.i << ')'; return os; }
+
     struct ComplexEmptyAnnotation : senf::ComplexAnnotation
     {};
 
+    std::ostream & operator<<(std::ostream & os, ComplexEmptyAnnotation const & v)
+    { os << "(empty)"; return os; }
+
+    struct InvalidAnnotation
+    {
+        std::string value;
+    };
+
+    std::ostream & operator<<(std::ostream & os, InvalidAnnotation const & v)
+    { os << v.value; return os; }
+
 }
 
 BOOST_AUTO_UNIT_TEST(packet)
 {
-    senf::Packet packet (FooPacket::create());
+    senf::Packet packet (FooPacket::create());    
     BarPacket::createAfter(packet);
 
     BOOST_REQUIRE( packet );
@@ -174,7 +196,13 @@ BOOST_AUTO_UNIT_TEST(packet)
     
     std::stringstream s;
     packet.dump(s);
-    BOOST_CHECK_EQUAL( s.str(), "BarPacket:\ntype: 0\nlength: 0\n" );
+    BOOST_CHECK_EQUAL( s.str(), 
+                       "Annotations:\n"
+                       "  (anonymous namespace)::ComplexAnnotation: no value\n"
+                       "  (anonymous namespace)::IntAnnotation: 0\n"
+                       "BarPacket:\n"
+                       "  type: 0\n"
+                       "  length: 0\n" );
     
     packet.finalizeAll();
     BOOST_CHECK_EQUAL( packet.last().as<BarPacket>()->type(), 
@@ -191,7 +219,7 @@ BOOST_AUTO_UNIT_TEST(packet)
     senf::PacketData::byte data[] = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                       0x81, 0x82, 0x83 };
 
-    BarPacket::createAfter(packet,data);
+    BarPacket::createAfter(packet, data);
     BOOST_REQUIRE( packet.next() );
     BOOST_REQUIRE( packet.next().is<BarPacket>() );
     BOOST_CHECK( packet.last().is<FooPacket>() );
@@ -208,6 +236,10 @@ BOOST_AUTO_UNIT_TEST(packet)
     BOOST_CHECK( packet.last().rfind<FooPacket>() == packet.last() );
     BOOST_CHECK( packet.next<BarPacket>() == packet.next() );
     BOOST_CHECK( packet.last().prev().prev<FooPacket>() == packet );
+    
+    senf::DataPacket::createAfter(packet);
+    BOOST_CHECK_THROW( packet.next().next().next().parseNextAs<BarPacket>(),
+            senf::InvalidPacketChainException );
 }
 
 BOOST_AUTO_UNIT_TEST(concretePacket)
@@ -251,6 +283,17 @@ BOOST_AUTO_UNIT_TEST(concretePacket)
     BOOST_CHECK_EQUAL( BarPacket::create()->reserved(), 0xA0A0u );
 }
 
+BOOST_AUTO_UNIT_TEST(packetAssign)
+{
+    BarPacket bar1 (BarPacket::create());
+    BarPacket bar2 (BarPacket::create());
+
+    bar2->type() << 0x2A2Bu;
+    bar1.parser() << bar2;
+    
+    BOOST_CHECK_EQUAL( bar1->type(), 0x2A2Bu );
+}
+
 BOOST_AUTO_UNIT_TEST(packetAnnotation)
 {
     senf::Packet packet (FooPacket::create());
@@ -273,6 +316,26 @@ BOOST_AUTO_UNIT_TEST(packetAnnotation)
     BOOST_CHECK( ! senf::detail::AnnotationIndexer<ComplexEmptyAnnotation>::Small );
 }
 
+#ifdef COMPILE_CHECK
+
+COMPILE_FAIL(invalidAnnotation)
+{
+#if 0 // The traits check fails for user defined but trivial constructors so ...
+#   ifdef BOOST_HAS_TYPE_TRAITS_INTRINSICS
+
+    senf::Packet packet (FooPacket::create());
+    (void) packet.annotation<InvalidAnnotation>();
+
+#   else
+#   endif
+#endif
+
+    invalid_annotation_check_disabled();
+
+}
+
+#endif
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_