4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief MACAddress unit tests */
26 //#include "MACAddress.test.hh"
27 //#include "MACAddress.test.ih"
31 #include "MACAddress.hh"
32 #include <senf/Utils/String.hh>
34 #include <senf/Socket/Protocols/AddressExceptions.hh>
36 #include <senf/Utils/auto_unit_test.hh>
37 #include <boost/test/test_tools.hpp>
40 ///////////////////////////////cc.p////////////////////////////////////////
42 SENF_AUTO_UNIT_TEST(macAddress)
44 using senf::MACAddress;
45 using senf::AddressSyntaxException;
47 std::string test ("A1-b2-C3:d4:E5:f6");
48 MACAddress mac (MACAddress::from_string(test));
50 BOOST_CHECK_EQUAL( mac[0], 0xA1u );
51 BOOST_CHECK_EQUAL( mac[1], 0xB2u );
52 BOOST_CHECK_EQUAL( mac[2], 0xC3u );
53 BOOST_CHECK_EQUAL( mac[3], 0xD4u );
54 BOOST_CHECK_EQUAL( mac[4], 0xE5u );
55 BOOST_CHECK_EQUAL( mac[5], 0xF6u );
58 std::stringstream str;
60 BOOST_CHECK( str.fail());
63 std::stringstream str ( "invalid_mac_address");
65 BOOST_CHECK( str.fail() );
68 std::stringstream str;
70 BOOST_CHECK_EQUAL( str.str(), "a1:b2:c3:d4:e5:f6" );
72 BOOST_CHECK( ! str.fail() );
75 BOOST_CHECK_EQUAL(mac, MACAddress::from_string(test));
76 BOOST_CHECK( ! mac.local() );
77 BOOST_CHECK( mac.multicast() );
78 BOOST_CHECK( ! mac.broadcast() );
80 BOOST_CHECK_EQUAL( mac.oui(), 0xa1b2c3u );
81 BOOST_CHECK_EQUAL( mac.nic(), 0xd4e5f6u );
82 BOOST_CHECK_EQUAL( mac.eui64(), 0xa1b2c3fffed4e5f6llu );
85 BOOST_CHECK( ! mac2 );
86 mac2 = MACAddress::from_string("ff:ff:ff:ff:ff:ff");
87 BOOST_CHECK( mac2.broadcast() );
88 BOOST_CHECK_EQUAL( mac2, MACAddress::Broadcast );
89 char data[] = { 0x01,0x02,0x03,0x04,0x05,0x06 };
90 mac2 = MACAddress::from_data(data);
91 BOOST_CHECK_EQUAL( senf::str(mac2), "01:02:03:04:05:06" );
92 BOOST_CHECK( mac != mac2 );
94 BOOST_CHECK( mac == mac2 );
95 BOOST_CHECK_EQUAL( senf::str(mac2), "a1:b2:c3:d4:e5:f6" );
97 BOOST_CHECK_THROW( MACAddress::from_string("123:2:3:4:5:6"), AddressSyntaxException );
98 BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05"), AddressSyntaxException );
99 // test all invalid ascii ranges
100 BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:z6"), AddressSyntaxException );
101 BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:/6"), AddressSyntaxException );
102 BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:@6"), AddressSyntaxException );
103 BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:[6"), AddressSyntaxException );
105 BOOST_CHECK_EQUAL( mac, MACAddress::from_eui64(senf::EUI64(0xa1b2c3fffed4e5f6llu)) );
106 BOOST_CHECK_THROW( MACAddress::from_eui64(senf::EUI64(0ull)),
107 AddressSyntaxException );
109 BOOST_CHECK_EQUAL( MACAddress(0x1a2b3c4d5e6fULL).uint64(), 0x1a2b3c4d5e6fULL);
112 ///////////////////////////////cc.e////////////////////////////////////////
119 // comment-column: 40
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"