removed some useless spaces; not very important, I know :)
[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
53 BOOST_AUTO_UNIT_TEST(ethernetPacket_parse_chain)
54 {
55     unsigned char data[] = {
56         0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
57         0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
58         0x81, 0x00,                          // EtherType: VLan
59         0x92, 0x34,                          // VLAN prio, cfi, id
60         0xab, 0xcd,                          // EtherType
61         0xf0, 0xf1, 0xf2, 0xf3, 0xf4 
62     };      // Payload
63     senf::EthernetPacket p (senf::EthernetPacket::create(data));
64
65     BOOST_REQUIRE( p.next().is<senf::EthVLanPacket>() );
66     senf::EthVLanPacket v (p.next().as<senf::EthVLanPacket>());
67     BOOST_CHECK_EQUAL( v->priority(), 4u );
68     BOOST_CHECK( v->cfi() );
69     BOOST_CHECK_EQUAL( v->vlanId(), 0x234u );
70     BOOST_CHECK_EQUAL( v->type(), 0xabcd );
71     BOOST_CHECK( v.next().is<senf::DataPacket>() );
72     BOOST_CHECK_EQUAL( *v.next().data().begin(), 0xf0 );
73 }
74
75 BOOST_AUTO_UNIT_TEST(ethernetPacket_create)
76 {
77     senf::EthernetPacket eth (senf::EthernetPacket::create());
78     eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
79     eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
80     
81     senf::EthVLanPacket vlan (senf::EthVLanPacket::createAfter(eth));
82     vlan->priority() = 9u;
83     vlan->cfi() = true;
84     vlan->vlanId() = 0x234u;
85
86     eth.finalize();
87     BOOST_CHECK_EQUAL(eth->type_length(), 0x8100u);
88     BOOST_CHECK_EQUAL(vlan->type(), 0u);
89
90     senf::IPv4Packet ip (senf::IPv4Packet::createAfter(vlan));
91     eth.finalize();
92     BOOST_CHECK_EQUAL(vlan->type(), 0x0800u);
93 }
94
95 BOOST_AUTO_UNIT_TEST(ethernetPacket_llcsnap)
96 {
97     senf::EthernetPacket eth (senf::EthernetPacket::create());
98     eth->source() = senf::MACAddress::from_string("01:02:03:04:05:06");
99     eth->destination() = senf::MACAddress::from_string("07:08:09:0a:0b:0c");
100     
101     senf::LlcSnapPacket llcsnap (senf::LlcSnapPacket::createAfter(eth));
102     senf::DataPacket payload  (senf::DataPacket::createAfter(
103             llcsnap, std::string("Hello, world!")));
104     eth.finalize();
105     
106     BOOST_CHECK_EQUAL( eth->type_length(), 8u + 13u);
107     BOOST_CHECK_EQUAL( llcsnap->dsap(), 0xaa );
108     BOOST_CHECK_EQUAL( llcsnap->ssap(), 0xaa );
109     BOOST_CHECK_EQUAL( llcsnap->ctrl(), 0x03 );
110     BOOST_CHECK_EQUAL( llcsnap->protocolId(), 0x000000u );
111     BOOST_CHECK_EQUAL( llcsnap->type_length(), 0u);
112
113     senf::IPv4Packet ip (senf::IPv4Packet::createAfter(llcsnap));
114     eth.finalize();
115     BOOST_CHECK_EQUAL(llcsnap->type_length(), 0x0800u);
116 }
117
118 ///////////////////////////////cc.e////////////////////////////////////////
119 #undef prefix_
120
121 \f
122 // Local Variables:
123 // mode: c++
124 // fill-column: 100
125 // c-file-style: "senf"
126 // indent-tabs-mode: nil
127 // ispell-local-dictionary: "american"
128 // compile-command: "scons -u test"
129 // comment-column: 40
130 // End: