4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief EthernetPacket unit tests */
31 //#include "EthernetPacket.test.hh"
32 //#include "EthernetPacket.test.ih"
35 #include "EthernetPacket.hh"
36 #include "LlcSnapPacket.hh"
37 #include "IPv4Packet.hh"
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 SENF_AUTO_UNIT_TEST(ethernetPacket_parse)
47 senf::PacketData::byte data[] = {
48 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // destination MAC
49 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, // source MAC
52 senf::EthernetPacket p (senf::EthernetPacket::create(data));
54 BOOST_CHECK_EQUAL( p->destination()[3], 0x04 );
55 BOOST_CHECK_EQUAL( p->source()[0], 0x07 );
56 BOOST_CHECK_EQUAL( p->type_length(), 0x1011 );
58 std::ostringstream oss (std::ostringstream::out);
59 SENF_CHECK_NO_THROW( p.dump( oss));
62 SENF_AUTO_UNIT_TEST(ethernetPacket_parse_chain)
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
72 senf::EthernetPacket p (senf::EthernetPacket::create(data));
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 );
83 std::ostringstream oss (std::ostringstream::out);
84 SENF_CHECK_NO_THROW( p.dump( oss));
87 SENF_AUTO_UNIT_TEST(ethernetPacket_create)
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");
93 senf::EthVLanPacket vlan (senf::EthVLanPacket::createAfter(eth));
94 vlan->priority() = 9u;
96 vlan->vlanId() = 0x234u;
99 BOOST_CHECK_EQUAL(eth->type_length(), 0x8100u);
100 BOOST_CHECK_EQUAL(vlan->type(), 0u);
102 senf::IPv4Packet ip (senf::IPv4Packet::createAfter(vlan));
104 BOOST_CHECK_EQUAL(vlan->type(), 0x0800u);
107 SENF_AUTO_UNIT_TEST(ethernetPacket_llcsnap)
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");
113 senf::LlcSnapPacket llcsnap (senf::LlcSnapPacket::createAfter(eth));
114 senf::DataPacket payload (senf::DataPacket::createAfter(
115 llcsnap, std::string("Hello, world!")));
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);
125 senf::IPv4Packet ip (senf::IPv4Packet::createAfter(llcsnap));
127 BOOST_CHECK_EQUAL(llcsnap->type_length(), 0x0800u);
130 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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