ad8e1e27063d18ccb1df6402e3c25fa578671843
[senf.git] / Packets / 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
31 #include <boost/test/auto_unit_test.hpp>
32 #include <boost/test/test_tools.hpp>
33
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37 using namespace senf;
38
39 BOOST_AUTO_UNIT_TEST(ethernetPacket_parser)
40 {
41     unsigned char data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
42                              0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
43                              0x10, 0x11 };                        // EtherType
44     typedef unsigned char * iterator;
45     Parse_Ethernet<iterator> p(data);
46
47     BOOST_CHECK_EQUAL( p.destination()[2], 0x03 );
48     BOOST_CHECK_EQUAL( p.source()[3], 0x0A );
49     BOOST_CHECK_EQUAL( p.type(), 0x1011 );
50 }
51
52 BOOST_AUTO_UNIT_TEST(ethernetPacket_packet)
53 {
54     unsigned char data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
55                              0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
56                              0x10, 0x11 };                        // EtherType
57     EthernetPacket::ptr p (Packet::create<EthernetPacket>(data, data+sizeof(data)));
58
59     BOOST_CHECK_EQUAL( p->destination()[3], 0x04 );
60     BOOST_CHECK_EQUAL( p->source()[0], 0x07 );
61     BOOST_CHECK_EQUAL( p->type(), 0x1011 );
62
63     BOOST_CHECK_THROW( Packet::create<EthernetPacket>(data, data+sizeof(data)-1), 
64                        TruncatedPacketException );
65 }
66
67 BOOST_AUTO_UNIT_TEST(ethernetPacket_chain)
68 {
69     unsigned char data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,  // destination MAC
70                              0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,  // source MAC
71                              0x81, 0x00,                          // EtherType: VLan
72                              0x92, 0x34,                          // VLAN prio, cfi, id
73                              0xab, 0xcd,                          // EtherType
74                              0xf0, 0xf1, 0xf2, 0xf3, 0xf4 };      // Payload
75
76     EthernetPacket::ptr p (Packet::create<EthernetPacket>(data, data+sizeof(data)));
77
78     BOOST_CHECK( p->next()->is<EthVLanPacket>() );
79     EthVLanPacket::ptr v (p->next()->as<EthVLanPacket>());
80     BOOST_CHECK_EQUAL( v->priority(), 4u );
81     BOOST_CHECK( v->cfi() );
82     BOOST_CHECK_EQUAL( v->vlanId(), 0x234u );
83     BOOST_CHECK_EQUAL( v->type(), 0xabcd );
84     BOOST_CHECK( v->next()->is<DataPacket>() );
85     BOOST_CHECK_EQUAL( *v->next()->begin(), 0xf0 );
86 }
87
88 ///////////////////////////////cc.e////////////////////////////////////////
89 #undef prefix_
90
91 \f
92 // Local Variables:
93 // mode: c++
94 // c-file-style: "senf"
95 // End: