f96049c3adf744d7cbd77aa0494a5ddf11ec55a0
[senf.git] / Packets / DefaultBundle / EthernetPacket.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 "IpV4Packet.hh"
31
32 #include "../../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 BOOST_AUTO_UNIT_TEST(ethernetPacket_parse)
39 {
40     senf::PacketData::byte data[] = { 
41         0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
42         0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
43         0x10, 0x11 
44     };                        // EtherType
45     senf::EthernetPacket p (senf::EthernetPacket::create(data));
46
47     BOOST_CHECK_EQUAL( p->destination()[3], 0x04 );
48     BOOST_CHECK_EQUAL( p->source()[0], 0x07 );
49     BOOST_CHECK_EQUAL( p->type_length(), 0x1011 );
50 }
51
52 BOOST_AUTO_UNIT_TEST(ethernetPacket_parse_chain)
53 {
54     unsigned char data[] = {
55         0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
56         0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
57         0x81, 0x00,                          // EtherType: VLan
58         0x92, 0x34,                          // VLAN prio, cfi, id
59         0xab, 0xcd,                          // EtherType
60         0xf0, 0xf1, 0xf2, 0xf3, 0xf4 
61     };      // Payload
62     senf::EthernetPacket p (senf::EthernetPacket::create(data));
63
64     BOOST_REQUIRE( p.next().is<senf::EthVLanPacket>() );
65     senf::EthVLanPacket v (p.next().as<senf::EthVLanPacket>());
66     BOOST_CHECK_EQUAL( v->priority(), 4u );
67     BOOST_CHECK( v->cfi() );
68     BOOST_CHECK_EQUAL( v->vlanId(), 0x234u );
69     BOOST_CHECK_EQUAL( v->type(), 0xabcd );
70     BOOST_CHECK( v.next().is<senf::DataPacket>() );
71     BOOST_CHECK_EQUAL( *v.next().data().begin(), 0xf0 );
72 }
73
74 BOOST_AUTO_UNIT_TEST(ethernetPacket_create)
75 {
76     senf::EthernetPacket eth (senf::EthernetPacket::create());
77     eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
78     eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
79     
80     senf::EthVLanPacket vlan (senf::EthVLanPacket::createAfter(eth));
81     vlan->priority() = 9u;
82     vlan->cfi() = true;
83     vlan->vlanId() = 0x234u;
84
85     eth.finalize();
86     BOOST_CHECK_EQUAL(eth->type_length(), 0x8100u);
87     BOOST_CHECK_EQUAL(vlan->type(), 0u);
88
89     senf::IpV4Packet ip (senf::IpV4Packet::createAfter(vlan));
90     eth.finalize();
91     BOOST_CHECK_EQUAL(vlan->type(), 0x0800u);
92 }
93
94 BOOST_AUTO_UNIT_TEST(llcsnap_parse)
95 {
96     senf::PacketData::byte data[] = {
97         0xaa,             // DSAP
98         0xaa,             // SSAP
99         0x03,             // ctrl
100         0x00, 0x00, 0x00, // Protocol Identification Field
101         0x10, 0x11        // EtherType 
102     };
103     senf::EthLlcSnapPacket p (senf::EthLlcSnapPacket::create(data));
104
105     BOOST_CHECK_EQUAL( p->dsap(), 0xaa );
106     BOOST_CHECK_EQUAL( p->ssap(), 0xaa );
107     BOOST_CHECK_EQUAL( p->ctrl(), 0x03 );
108     BOOST_CHECK_EQUAL( p->protocolId(), 0x000000u );
109     BOOST_CHECK_EQUAL( p->type(), 0x1011 );
110 }
111
112 BOOST_AUTO_UNIT_TEST(llcsnap_create)
113 {
114     senf::EthernetPacket eth (senf::EthernetPacket::create());
115     eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
116     eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
117     
118     senf::EthLlcSnapPacket llcsnap (senf::EthLlcSnapPacket::createAfter(eth));
119     senf::DataPacket payload  (senf::DataPacket::createAfter(
120             llcsnap, std::string("Hello, world!")));
121     eth.finalize();
122     
123     BOOST_CHECK_EQUAL( eth->type_length(), 8u + 13u);
124     BOOST_CHECK_EQUAL( llcsnap->dsap(), 0xaa );
125     BOOST_CHECK_EQUAL( llcsnap->ssap(), 0xaa );
126     BOOST_CHECK_EQUAL( llcsnap->ctrl(), 0x03 );
127     BOOST_CHECK_EQUAL( llcsnap->protocolId(), 0x000000u );
128     BOOST_CHECK_EQUAL( llcsnap->type(), 0u);
129
130     senf::IpV4Packet ip (senf::IpV4Packet::createAfter(llcsnap));
131     eth.finalize();
132     BOOST_CHECK_EQUAL(llcsnap->type(), 0x0800u);
133 }
134
135 ///////////////////////////////cc.e////////////////////////////////////////
136 #undef prefix_
137
138 \f
139 // Local Variables:
140 // mode: c++
141 // fill-column: 100
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u test"
146 // comment-column: 40
147 // End: