Socket/Protocols/INet: Implement INet4Address add rename old INet4Address to INet4Soc...
[senf.git] / Socket / Protocols / Raw / MACAddress.test.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief MACAddress.test unit tests */
23
24 //#include "MACAddress.test.hh"
25 //#include "MACAddress.test.ih"
26
27 // Custom includes
28 #include <sstream>
29 #include <boost/lexical_cast.hpp>
30 #include "MACAddress.hh"
31
32 #include <boost/test/auto_unit_test.hpp>
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 BOOST_AUTO_UNIT_TEST(macAddress)
39 {
40     senf::MACAddress mac (senf::MACAddress::from_string("A1-b2-C3:d4:E5:f6"));
41
42     BOOST_CHECK_EQUAL( mac[0], 0xA1u );
43     BOOST_CHECK_EQUAL( mac[1], 0xB2u );
44     BOOST_CHECK_EQUAL( mac[2], 0xC3u );
45     BOOST_CHECK_EQUAL( mac[3], 0xD4u );
46     BOOST_CHECK_EQUAL( mac[4], 0xE5u );
47     BOOST_CHECK_EQUAL( mac[5], 0xF6u );
48
49     std::stringstream str;
50     str << mac;
51     BOOST_CHECK_EQUAL( str.str(), "a1:b2:c3:d4:e5:f6" );
52
53     BOOST_CHECK( ! mac.local() );
54     BOOST_CHECK( mac.multicast() );
55     BOOST_CHECK( ! mac.broadcast() );
56     BOOST_CHECK( mac );
57     BOOST_CHECK_EQUAL( mac.oui(), 0xa1b2c3u );
58     BOOST_CHECK_EQUAL( mac.nic(), 0xd4e5f6u );
59     BOOST_CHECK_EQUAL( mac.eui64(), 0xa1b2c3fffed4e5f6llu );
60
61     senf::MACAddress mac2;
62     BOOST_CHECK( ! mac2 );
63     mac2 = senf::MACAddress::from_string("ff:ff:ff:ff:ff:ff");
64     BOOST_CHECK( mac2.broadcast() );
65     BOOST_CHECK_EQUAL( mac2, senf::MACAddress::Broadcast );
66     char data[] = { 0x01,0x02,0x03,0x04,0x05,0x06 };
67     mac2 = senf::MACAddress::from_data(data);
68     BOOST_CHECK_EQUAL( boost::lexical_cast<std::string>(mac2), "01:02:03:04:05:06" );
69     BOOST_CHECK( mac != mac2 );
70     mac2 = mac;
71     BOOST_CHECK( mac == mac2 );
72     BOOST_CHECK_EQUAL( boost::lexical_cast<std::string>(mac2), "a1:b2:c3:d4:e5:f6" );
73
74     BOOST_CHECK_THROW( senf::MACAddress::from_string("1:2:3:4:5:6"), 
75                        senf::MACAddress::SyntaxException );
76     BOOST_CHECK_THROW( senf::MACAddress::from_string("01:02:03:04:05"), 
77                        senf::MACAddress::SyntaxException );
78     BOOST_CHECK_THROW( senf::MACAddress::from_string("01:02:03:04:05:z6"), 
79                        senf::MACAddress::SyntaxException );
80
81     BOOST_CHECK_EQUAL( mac, senf::MACAddress::from_eui64(0xa1b2c3fffed4e5f6llu) );
82     BOOST_CHECK_THROW( senf::MACAddress::from_eui64(0u), senf::MACAddress::SyntaxException );
83 }
84
85 ///////////////////////////////cc.e////////////////////////////////////////
86 #undef prefix_
87
88 \f
89 // Local Variables:
90 // mode: c++
91 // fill-column: 100
92 // comment-column: 40
93 // c-file-style: "senf"
94 // indent-tabs-mode: nil
95 // ispell-local-dictionary: "american"
96 // compile-command: "scons -u test"
97 // End: