4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief EthernetPacket unit tests */
26 //#include "EthernetPacket.test.hh"
27 //#include "EthernetPacket.test.ih"
30 #include "EthernetPacket.hh"
31 #include "LlcSnapPacket.hh"
32 #include "IPv4Packet.hh"
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
38 ///////////////////////////////cc.p////////////////////////////////////////
40 SENF_AUTO_UNIT_TEST(ethernetPacket_parse)
42 senf::PacketData::byte data[] = {
43 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // destination MAC
44 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, // source MAC
47 senf::EthernetPacket p (senf::EthernetPacket::create(data));
49 BOOST_CHECK_EQUAL( p->destination()[3], 0x04 );
50 BOOST_CHECK_EQUAL( p->source()[0], 0x07 );
51 BOOST_CHECK_EQUAL( p->type_length(), 0x1011 );
53 std::ostringstream oss (std::ostringstream::out);
54 SENF_CHECK_NO_THROW( p.dump( oss));
57 SENF_AUTO_UNIT_TEST(ethernetPacket_parse_chain)
59 unsigned char data[] = {
60 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // destination MAC
61 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, // source MAC
62 0x81, 0x00, // EtherType: VLan
63 0x92, 0x34, // VLAN prio, cfi, id
64 0xab, 0xcd, // EtherType
65 0xf0, 0xf1, 0xf2, 0xf3, 0xf4
67 senf::EthernetPacket p (senf::EthernetPacket::create(data));
69 BOOST_REQUIRE( p.next().is<senf::EthVLanPacket>() );
70 senf::EthVLanPacket v (p.next().as<senf::EthVLanPacket>());
71 BOOST_CHECK_EQUAL( v->priority(), 4u );
72 BOOST_CHECK( v->cfi() );
73 BOOST_CHECK_EQUAL( v->vlanId(), 0x234u );
74 BOOST_CHECK_EQUAL( v->type(), 0xabcd );
75 BOOST_CHECK( v.next().is<senf::DataPacket>() );
76 BOOST_CHECK_EQUAL( *v.next().data().begin(), 0xf0 );
79 SENF_AUTO_UNIT_TEST(ethernetPacket_create)
81 senf::EthernetPacket eth (senf::EthernetPacket::create());
82 eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
83 eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
85 senf::EthVLanPacket vlan (senf::EthVLanPacket::createAfter(eth));
86 vlan->priority() = 9u;
88 vlan->vlanId() = 0x234u;
91 BOOST_CHECK_EQUAL(eth->type_length(), 0x8100u);
92 BOOST_CHECK_EQUAL(vlan->type(), 0u);
94 senf::IPv4Packet ip (senf::IPv4Packet::createAfter(vlan));
96 BOOST_CHECK_EQUAL(vlan->type(), 0x0800u);
99 SENF_AUTO_UNIT_TEST(ethernetPacket_llcsnap)
101 senf::EthernetPacket eth (senf::EthernetPacket::create());
102 eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
103 eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
105 senf::LlcSnapPacket llcsnap (senf::LlcSnapPacket::createAfter(eth));
106 senf::DataPacket payload (senf::DataPacket::createAfter(
107 llcsnap, std::string("Hello, world!")));
110 BOOST_CHECK_EQUAL( eth->type_length(), 8u + 13u);
111 BOOST_CHECK_EQUAL( llcsnap->dsap(), 0xaa );
112 BOOST_CHECK_EQUAL( llcsnap->ssap(), 0xaa );
113 BOOST_CHECK_EQUAL( llcsnap->ctrl(), 0x03 );
114 BOOST_CHECK_EQUAL( llcsnap->protocolId(), 0x000000u );
115 BOOST_CHECK_EQUAL( llcsnap->type_length(), 0u);
117 senf::IPv4Packet ip (senf::IPv4Packet::createAfter(llcsnap));
119 BOOST_CHECK_EQUAL(llcsnap->type_length(), 0x0800u);
122 ///////////////////////////////cc.e////////////////////////////////////////
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // comment-column: 40