79414ba14c8e8b44d0bcf4d37a00f511bcb3ebb7
[senf.git] / senf / 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 /** \file
24     \brief EthernetPacket unit tests */
25
26 //#include "EthernetPacket.test.hh"
27 //#include "EthernetPacket.test.ih"
28
29 // Custom includes
30 #include "EthernetPacket.hh"
31 #include "LlcSnapPacket.hh"
32 #include "IPv4Packet.hh"
33
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 SENF_AUTO_UNIT_TEST(ethernetPacket_parse)
41 {
42     senf::PacketData::byte data[] = {
43         0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
44         0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
45         0x10, 0x11
46     };                        // EtherType
47     senf::EthernetPacket p (senf::EthernetPacket::create(data));
48
49     BOOST_CHECK_EQUAL( p->destination()[3], 0x04 );
50     BOOST_CHECK_EQUAL( p->source()[0], 0x07 );
51     BOOST_CHECK_EQUAL( p->type_length(), 0x1011 );
52
53     std::ostringstream oss (std::ostringstream::out);
54     SENF_CHECK_NO_THROW( p.dump( oss));
55 }
56
57 SENF_AUTO_UNIT_TEST(ethernetPacket_parse_chain)
58 {
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
66     };      // Payload
67     senf::EthernetPacket p (senf::EthernetPacket::create(data));
68
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 );
77
78     std::ostringstream oss (std::ostringstream::out);
79     SENF_CHECK_NO_THROW( p.dump( oss));
80 }
81
82 SENF_AUTO_UNIT_TEST(ethernetPacket_create)
83 {
84     senf::EthernetPacket eth (senf::EthernetPacket::create());
85     eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
86     eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
87
88     senf::EthVLanPacket vlan (senf::EthVLanPacket::createAfter(eth));
89     vlan->priority() = 9u;
90     vlan->cfi() = true;
91     vlan->vlanId() = 0x234u;
92
93     eth.finalizeAll();
94     BOOST_CHECK_EQUAL(eth->type_length(), 0x8100u);
95     BOOST_CHECK_EQUAL(vlan->type(), 0u);
96
97     senf::IPv4Packet ip (senf::IPv4Packet::createAfter(vlan));
98     eth.finalizeAll();
99     BOOST_CHECK_EQUAL(vlan->type(), 0x0800u);
100 }
101
102 SENF_AUTO_UNIT_TEST(ethernetPacket_llcsnap)
103 {
104     senf::EthernetPacket eth (senf::EthernetPacket::create());
105     eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
106     eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
107
108     senf::LlcSnapPacket llcsnap (senf::LlcSnapPacket::createAfter(eth));
109     senf::DataPacket payload  (senf::DataPacket::createAfter(
110             llcsnap, std::string("Hello, world!")));
111     eth.finalizeAll();
112
113     BOOST_CHECK_EQUAL( eth->type_length(), 8u + 13u);
114     BOOST_CHECK_EQUAL( llcsnap->dsap(), 0xaa );
115     BOOST_CHECK_EQUAL( llcsnap->ssap(), 0xaa );
116     BOOST_CHECK_EQUAL( llcsnap->ctrl(), 0x03 );
117     BOOST_CHECK_EQUAL( llcsnap->protocolId(), 0x000000u );
118     BOOST_CHECK_EQUAL( llcsnap->type_length(), 0u);
119
120     senf::IPv4Packet ip (senf::IPv4Packet::createAfter(llcsnap));
121     eth.finalizeAll();
122     BOOST_CHECK_EQUAL(llcsnap->type_length(), 0x0800u);
123 }
124
125 ///////////////////////////////cc.e////////////////////////////////////////
126 #undef prefix_
127
128 \f
129 // Local Variables:
130 // mode: c++
131 // fill-column: 100
132 // c-file-style: "senf"
133 // indent-tabs-mode: nil
134 // ispell-local-dictionary: "american"
135 // compile-command: "scons -u test"
136 // comment-column: 40
137 // End: