4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief MACAddress unit tests */
31 //#include "MACAddress.test.hh"
32 //#include "MACAddress.test.ih"
36 #include "MACAddress.hh"
37 #include <senf/Utils/String.hh>
39 #include <senf/Socket/Protocols/AddressExceptions.hh>
41 #include <senf/Utils/auto_unit_test.hh>
42 #include <boost/test/test_tools.hpp>
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47 SENF_AUTO_UNIT_TEST(macAddress)
49 using senf::MACAddress;
50 using senf::AddressSyntaxException;
52 std::string test ("A1-b2-C3:d4:E5:f6");
53 MACAddress mac (MACAddress::from_string(test));
55 BOOST_CHECK_EQUAL( mac[0], 0xA1u );
56 BOOST_CHECK_EQUAL( mac[1], 0xB2u );
57 BOOST_CHECK_EQUAL( mac[2], 0xC3u );
58 BOOST_CHECK_EQUAL( mac[3], 0xD4u );
59 BOOST_CHECK_EQUAL( mac[4], 0xE5u );
60 BOOST_CHECK_EQUAL( mac[5], 0xF6u );
63 std::stringstream str;
65 BOOST_CHECK( str.fail());
68 std::stringstream str ( "invalid_mac_address");
70 BOOST_CHECK( str.fail() );
73 std::stringstream str;
75 BOOST_CHECK_EQUAL( str.str(), "a1:b2:c3:d4:e5:f6" );
77 BOOST_CHECK( ! str.fail() );
80 BOOST_CHECK_EQUAL(mac, MACAddress::from_string(test));
81 BOOST_CHECK( ! mac.local() );
82 BOOST_CHECK( mac.multicast() );
83 BOOST_CHECK( ! mac.broadcast() );
85 BOOST_CHECK_EQUAL( mac.oui(), 0xa1b2c3u );
86 BOOST_CHECK_EQUAL( mac.nic(), 0xd4e5f6u );
87 BOOST_CHECK_EQUAL( mac.eui64(), 0xa1b2c3fffed4e5f6llu );
90 BOOST_CHECK( ! mac2 );
91 mac2 = MACAddress::from_string("ff:ff:ff:ff:ff:ff");
92 BOOST_CHECK( mac2.broadcast() );
93 BOOST_CHECK_EQUAL( mac2, MACAddress::Broadcast );
94 char data[] = { 0x01,0x02,0x03,0x04,0x05,0x06 };
95 mac2 = MACAddress::from_data(data);
96 BOOST_CHECK_EQUAL( senf::str(mac2), "01:02:03:04:05:06" );
97 SENF_CHECK_NOT_EQUAL( mac, mac2 );
99 BOOST_CHECK( mac == mac2 );
100 BOOST_CHECK_EQUAL( senf::str(mac2), "a1:b2:c3:d4:e5:f6" );
102 BOOST_CHECK_THROW( MACAddress::from_string("123:2:3:4:5:6"), AddressSyntaxException );
103 BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05"), AddressSyntaxException );
104 // test all invalid ascii ranges
105 BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:z6"), AddressSyntaxException );
106 BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:/6"), AddressSyntaxException );
107 BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:@6"), AddressSyntaxException );
108 BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:[6"), AddressSyntaxException );
110 BOOST_CHECK_EQUAL( mac, MACAddress::from_eui64(senf::EUI64(0xa1b2c3fffed4e5f6llu)) );
111 BOOST_CHECK_THROW( MACAddress::from_eui64(senf::EUI64(0ull)),
112 AddressSyntaxException );
114 BOOST_CHECK_EQUAL( MACAddress(0x1a2b3c4d5e6fULL).uint64(), 0x1a2b3c4d5e6fULL);
116 BOOST_CHECK_EQUAL( mac, senf::EUI64::from_mac(mac) );
117 BOOST_CHECK_EQUAL( senf::EUI64::from_mac(mac), mac );
120 //-/////////////////////////////////////////////////////////////////////////////////////////////////
127 // comment-column: 40
128 // c-file-style: "senf"
129 // indent-tabs-mode: nil
130 // ispell-local-dictionary: "american"
131 // compile-command: "scons -u test"