80221Bundle: implemented standard compliant parser for MIHF_Id
[senf.git] / Packets / 80221Bundle / MIHPacket.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@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 MIHPacket unit tests */
25
26 //#include "MIHPacket.test.hh"
27 //#include "MIHPacket.test.ih"
28
29 #include "../../Utils/auto_unit_test.hh"
30 #include <boost/test/test_tools.hpp>
31
32 #include "MIHPacket.hh"
33
34
35 using namespace senf;
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 BOOST_AUTO_UNIT_TEST(MIHPacket_create_string)
41 {
42     MIHPacket mihPacket (MIHPacket::create());
43     // set some fields
44     mihPacket->fragmentNr() = 42;
45     mihPacket->transactionId() = 21;
46     mihPacket->src_mihfId().setString( "senf@berlios.de");
47     mihPacket->dst_mihfId().setString( "test");
48     mihPacket.finalizeThis();
49     
50     unsigned char data[] = { 
51             // MIH header
52             0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x17,
53             // source MIHF_ID TLV:
54             0x01, 0x0f, // type, length
55             0x73, 0x65, 0x6e, 0x66, 0x40, 0x62, 0x65, 0x72, 0x6c, 
56             0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65,  // value
57             // destination MIHF_ID TLV:
58             0x02, 0x04, 0x74, 0x65, 0x73, 0x74
59     };
60     BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
61     BOOST_CHECK_EQUAL( mihPacket->src_mihfId().asString(), "senf@berlios.de");
62     BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().asString(), "test");
63     
64     // now expand a MIHF_ID
65     mihPacket->dst_mihfId().maxLengthValue(253);
66     mihPacket->dst_mihfId().setString( std::string(200, 'x'));
67     mihPacket.finalizeThis();
68     
69     BOOST_CHECK_EQUAL( mihPacket.size(), 8 + 17 + 203);
70     BOOST_CHECK_EQUAL( mihPacket->payloadLength(), 17 + 203);
71     BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().length(), 200);
72     BOOST_CHECK_EQUAL( senf::bytes(mihPacket->dst_mihfId()), 203);
73 }
74
75
76 BOOST_AUTO_UNIT_TEST(MIHPacket_create_mac)
77 {
78     MIHPacket mihPacket (MIHPacket::create());
79     // set some fields
80     mihPacket->fragmentNr() = 42;
81     mihPacket->transactionId() = 21;
82     mihPacket->src_mihfId().setMACAddress( MACAddress::from_string("01:02:03:04:05:06"));
83     mihPacket->dst_mihfId().setMACAddress( MACAddress::from_string("07:08:09:0a:0b:0c"));
84     mihPacket.finalizeThis();
85     
86     unsigned char data[] = { 
87             // MIH header
88             0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x1c,
89             // source MIHF_ID TLV:
90             0x01, 0x0c, // type, length
91             0x5c, 0x01, 0x5c, 0x02, 0x5c, 0x03, 0x5c, 0x04, 0x5c, 0x05, 0x5c, 0x06, // value (nai-encoded)
92             // destination MIHF_ID TLV:
93             0x02, 0x0c,  // type, length
94             0x5c, 0x07, 0x5c, 0x08, 0x5c, 0x09, 0x5c, 0x0a, 0x5c, 0x0b, 0x5c, 0x0c  // value (nai-encoded)
95     };
96     BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
97     BOOST_CHECK_EQUAL( 
98             mihPacket->src_mihfId().asMACAddress(), 
99             MACAddress::from_string("01:02:03:04:05:06"));
100     BOOST_CHECK_EQUAL( 
101             mihPacket->dst_mihfId().asMACAddress(), 
102             MACAddress::from_string("07:08:09:0a:0b:0c"));
103 }
104
105
106 BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet4)
107 {
108     MIHPacket mihPacket (MIHPacket::create());
109     // set some fields
110     mihPacket->fragmentNr() = 42;
111     mihPacket->transactionId() = 21;
112     mihPacket->src_mihfId().setINet4Address( INet4Address::from_string("128.129.130.131"));
113     mihPacket->dst_mihfId().setINet4Address( INet4Address::from_string("132.133.134.135"));
114     mihPacket.finalizeThis();
115     
116     unsigned char data[] = { 
117             // MIH header
118             0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x14,
119             // source MIHF_ID TLV:
120             0x01, 0x08, // type, length
121             0x5c, 0x80, 0x5c, 0x81, 0x5c, 0x82, 0x5c, 0x83, // value (nai-encoded)
122             // destination MIHF_ID TLV:
123             0x02, 0x08, // type, length
124             0x5c, 0x84, 0x5c, 0x85, 0x5c, 0x86, 0x5c, 0x87  // value (nai-encoded)
125     };
126     BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
127     BOOST_CHECK_EQUAL( 
128             mihPacket->src_mihfId().asINet4Address(), 
129             INet4Address::from_string("128.129.130.131"));
130     BOOST_CHECK_EQUAL( 
131             mihPacket->dst_mihfId().asINet4Address(), 
132             INet4Address::from_string("132.133.134.135"));
133 }
134
135
136 BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet6)
137 {
138     MIHPacket mihPacket (MIHPacket::create());
139     // set some fields
140     mihPacket->fragmentNr() = 42;
141     mihPacket->transactionId() = 21;
142     mihPacket->src_mihfId().setINet6Address( INet6Address::from_string("::ffff:1.2.3.4"));
143     mihPacket->dst_mihfId().setINet6Address( INet6Address::from_string("::ffff:5.6.7.8"));
144     mihPacket.finalizeThis();
145     
146     unsigned char data[] = { 
147             // MIH header
148             0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x44,
149             // source MIHF_ID TLV:
150             0x01, 0x20, // type, length
151             // value (nai-encoded):
152             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
153             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
154             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0xff, 0x5c, 0xff,
155             0x5c, 0x01, 0x5c, 0x02, 0x5c, 0x03, 0x5c, 0x04,
156             // destination MIHF_ID TLV:
157             0x02, 0x20, // type, length
158             // value (nai-encoded):
159             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
160             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
161             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0xff, 0x5c, 0xff,
162             0x5c, 0x05, 0x5c, 0x06, 0x5c, 0x07, 0x5c, 0x08
163     };
164     BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
165     BOOST_CHECK_EQUAL( 
166             mihPacket->src_mihfId().asINet6Address(), 
167             INet6Address::from_string("::ffff:1.2.3.4"));
168     BOOST_CHECK_EQUAL( 
169             mihPacket->dst_mihfId().asINet6Address(), 
170             INet6Address::from_string("::ffff:5.6.7.8") );
171 }
172
173 ///////////////////////////////cc.e////////////////////////////////////////
174 #undef prefix_
175
176 \f
177 // Local Variables:
178 // mode: c++
179 // fill-column: 100
180 // c-file-style: "senf"
181 // indent-tabs-mode: nil
182 // ispell-local-dictionary: "american"
183 // compile-command: "scons -u test"
184 // comment-column: 40
185 // End: