- Added Unittests for all ICMPv6 - MLDv2 Packet Types
[senf.git] / Packets / DefaultBundle / EthernetPacket.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 // Unit tests
24
25 //#include "EthernetPacket.test.hh"
26 //#include "EthernetPacket.test.ih"
27
28 // Custom includes
29 #include "EthernetPacket.hh"
30 #include "LlcSnapPacket.hh"
31 #include "IPv4Packet.hh"
32
33 #include "../../Utils/auto_unit_test.hh"
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 BOOST_AUTO_UNIT_TEST(ethernetPacket_parse)
40 {
41     senf::PacketData::byte data[] = {
42         0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
43         0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
44         0x10, 0x11
45     };                        // EtherType
46     senf::EthernetPacket p (senf::EthernetPacket::create(data));
47
48     BOOST_CHECK_EQUAL( p->destination()[3], 0x04 );
49     BOOST_CHECK_EQUAL( p->source()[0], 0x07 );
50     BOOST_CHECK_EQUAL( p->type_length(), 0x1011 );
51
52     std::ostringstream oss (std::ostringstream::out);
53     SENF_CHECK_NO_THROW( p.dump( oss));
54 }
55
56 BOOST_AUTO_UNIT_TEST(ethernetPacket_parse_chain)
57 {
58     unsigned char data[] = {
59         0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
60         0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
61         0x81, 0x00,                          // EtherType: VLan
62         0x92, 0x34,                          // VLAN prio, cfi, id
63         0xab, 0xcd,                          // EtherType
64         0xf0, 0xf1, 0xf2, 0xf3, 0xf4
65     };      // Payload
66     senf::EthernetPacket p (senf::EthernetPacket::create(data));
67
68     BOOST_REQUIRE( p.next().is<senf::EthVLanPacket>() );
69     senf::EthVLanPacket v (p.next().as<senf::EthVLanPacket>());
70     BOOST_CHECK_EQUAL( v->priority(), 4u );
71     BOOST_CHECK( v->cfi() );
72     BOOST_CHECK_EQUAL( v->vlanId(), 0x234u );
73     BOOST_CHECK_EQUAL( v->type(), 0xabcd );
74     BOOST_CHECK( v.next().is<senf::DataPacket>() );
75     BOOST_CHECK_EQUAL( *v.next().data().begin(), 0xf0 );
76 }
77
78 BOOST_AUTO_UNIT_TEST(ethernetPacket_create)
79 {
80     senf::EthernetPacket eth (senf::EthernetPacket::create());
81     eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
82     eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
83
84     senf::EthVLanPacket vlan (senf::EthVLanPacket::createAfter(eth));
85     vlan->priority() = 9u;
86     vlan->cfi() = true;
87     vlan->vlanId() = 0x234u;
88
89     eth.finalizeAll();
90     BOOST_CHECK_EQUAL(eth->type_length(), 0x8100u);
91     BOOST_CHECK_EQUAL(vlan->type(), 0u);
92
93     senf::IPv4Packet ip (senf::IPv4Packet::createAfter(vlan));
94     eth.finalizeAll();
95     BOOST_CHECK_EQUAL(vlan->type(), 0x0800u);
96 }
97
98 BOOST_AUTO_UNIT_TEST(ethernetPacket_llcsnap)
99 {
100     senf::EthernetPacket eth (senf::EthernetPacket::create());
101     eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
102     eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
103
104     senf::LlcSnapPacket llcsnap (senf::LlcSnapPacket::createAfter(eth));
105     senf::DataPacket payload  (senf::DataPacket::createAfter(
106             llcsnap, std::string("Hello, world!")));
107     eth.finalizeAll();
108
109     BOOST_CHECK_EQUAL( eth->type_length(), 8u + 13u);
110     BOOST_CHECK_EQUAL( llcsnap->dsap(), 0xaa );
111     BOOST_CHECK_EQUAL( llcsnap->ssap(), 0xaa );
112     BOOST_CHECK_EQUAL( llcsnap->ctrl(), 0x03 );
113     BOOST_CHECK_EQUAL( llcsnap->protocolId(), 0x000000u );
114     BOOST_CHECK_EQUAL( llcsnap->type_length(), 0u);
115
116     senf::IPv4Packet ip (senf::IPv4Packet::createAfter(llcsnap));
117     eth.finalizeAll();
118     BOOST_CHECK_EQUAL(llcsnap->type_length(), 0x0800u);
119 }
120
121 ///////////////////////////////cc.e////////////////////////////////////////
122 #undef prefix_
123
124
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // c-file-style: "senf"
129 // indent-tabs-mode: nil
130 // ispell-local-dictionary: "american"
131 // compile-command: "scons -u test"
132 // comment-column: 40
133 // End: