switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / EthernetPacket.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief EthernetPacket unit tests */
30
31 //#include "EthernetPacket.test.hh"
32 //#include "EthernetPacket.test.ih"
33
34 // Custom includes
35 #include "EthernetPacket.hh"
36 #include "LlcSnapPacket.hh"
37 #include "IPv4Packet.hh"
38
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
41
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 SENF_AUTO_UNIT_TEST(ethernetPacket_parse)
46 {
47     senf::PacketData::byte data[] = {
48         0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
49         0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
50         0x10, 0x11
51     };                        // EtherType
52     senf::EthernetPacket p (senf::EthernetPacket::create(data));
53
54     BOOST_CHECK_EQUAL( p->destination()[3], 0x04 );
55     BOOST_CHECK_EQUAL( p->source()[0], 0x07 );
56     BOOST_CHECK_EQUAL( p->type_length(), 0x1011 );
57
58     std::ostringstream oss (std::ostringstream::out);
59     SENF_CHECK_NO_THROW( p.dump( oss));
60 }
61
62 SENF_AUTO_UNIT_TEST(ethernetPacket_parse_chain)
63 {
64     unsigned char data[] = {
65         0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
66         0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
67         0x81, 0x00,                          // EtherType: VLan
68         0x92, 0x34,                          // VLAN prio, cfi, id
69         0xab, 0xcd,                          // EtherType
70         0xf0, 0xf1, 0xf2, 0xf3, 0xf4
71     };      // Payload
72     senf::EthernetPacket p (senf::EthernetPacket::create(data));
73
74     BOOST_REQUIRE( p.next().is<senf::EthVLanPacket>() );
75     senf::EthVLanPacket v (p.next().as<senf::EthVLanPacket>());
76     BOOST_CHECK_EQUAL( v->priority(), 4u );
77     BOOST_CHECK( v->cfi() );
78     BOOST_CHECK_EQUAL( v->vlanId(), 0x234u );
79     BOOST_CHECK_EQUAL( v->type(), 0xabcd );
80     BOOST_CHECK( v.next().is<senf::DataPacket>() );
81     BOOST_CHECK_EQUAL( *v.next().data().begin(), 0xf0 );
82
83     std::ostringstream oss (std::ostringstream::out);
84     SENF_CHECK_NO_THROW( p.dump( oss));
85 }
86
87 SENF_AUTO_UNIT_TEST(ethernetPacket_create)
88 {
89     senf::EthernetPacket eth (senf::EthernetPacket::create());
90     eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
91     eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
92
93     senf::EthVLanPacket vlan (senf::EthVLanPacket::createAfter(eth));
94     vlan->priority() = 9u;
95     vlan->cfi() = true;
96     vlan->vlanId() = 0x234u;
97
98     eth.finalizeAll();
99     BOOST_CHECK_EQUAL(eth->type_length(), 0x8100u);
100     BOOST_CHECK_EQUAL(vlan->type(), 0u);
101
102     senf::IPv4Packet ip (senf::IPv4Packet::createAfter(vlan));
103     eth.finalizeAll();
104     BOOST_CHECK_EQUAL(vlan->type(), 0x0800u);
105 }
106
107 SENF_AUTO_UNIT_TEST(ethernetPacket_llcsnap)
108 {
109     senf::EthernetPacket eth (senf::EthernetPacket::create());
110     eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
111     eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
112
113     senf::LlcSnapPacket llcsnap (senf::LlcSnapPacket::createAfter(eth));
114     senf::DataPacket payload  (senf::DataPacket::createAfter(
115             llcsnap, std::string("Hello, world!")));
116     eth.finalizeAll();
117
118     BOOST_CHECK_EQUAL( eth->type_length(), 8u + 13u);
119     BOOST_CHECK_EQUAL( llcsnap->dsap(), 0xaa );
120     BOOST_CHECK_EQUAL( llcsnap->ssap(), 0xaa );
121     BOOST_CHECK_EQUAL( llcsnap->ctrl(), 0x03 );
122     BOOST_CHECK_EQUAL( llcsnap->protocolId(), 0x000000u );
123     BOOST_CHECK_EQUAL( llcsnap->type_length(), 0u);
124
125     senf::IPv4Packet ip (senf::IPv4Packet::createAfter(llcsnap));
126     eth.finalizeAll();
127     BOOST_CHECK_EQUAL(llcsnap->type_length(), 0x0800u);
128 }
129
130 //-/////////////////////////////////////////////////////////////////////////////////////////////////
131 #undef prefix_
132
133 \f
134 // Local Variables:
135 // mode: c++
136 // fill-column: 100
137 // c-file-style: "senf"
138 // indent-tabs-mode: nil
139 // ispell-local-dictionary: "american"
140 // compile-command: "scons -u test"
141 // comment-column: 40
142 // End: