switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / Raw / MACAddress.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief MACAddress unit tests */
30
31 //#include "MACAddress.test.hh"
32 //#include "MACAddress.test.ih"
33
34 // Custom includes
35 #include <sstream>
36 #include "MACAddress.hh"
37 #include <senf/Utils/String.hh>
38 #include "EUI64.hh"
39 #include <senf/Socket/Protocols/AddressExceptions.hh>
40
41 #include <senf/Utils/auto_unit_test.hh>
42 #include <boost/test/test_tools.hpp>
43
44 #define prefix_
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46
47 SENF_AUTO_UNIT_TEST(macAddress)
48 {
49     using senf::MACAddress;
50     using senf::AddressSyntaxException;
51
52     std::string test ("A1-b2-C3:d4:E5:f6");
53     MACAddress mac (MACAddress::from_string(test));
54
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 );
61
62     {
63         std::stringstream str;
64         str >> mac;
65         BOOST_CHECK( str.fail());
66     }
67     {
68         std::stringstream str ( "invalid_mac_address");
69         str >> mac;
70         BOOST_CHECK( str.fail() );
71     }
72     {
73         std::stringstream str;
74         str << mac;
75         BOOST_CHECK_EQUAL( str.str(), "a1:b2:c3:d4:e5:f6" );
76         str >> mac;
77         BOOST_CHECK( ! str.fail() );
78     }
79
80     BOOST_CHECK_EQUAL(mac, MACAddress::from_string(test));
81     BOOST_CHECK( ! mac.local() );
82     BOOST_CHECK( mac.multicast() );
83     BOOST_CHECK( ! mac.broadcast() );
84     BOOST_CHECK( mac );
85     BOOST_CHECK_EQUAL( mac.oui(), 0xa1b2c3u );
86     BOOST_CHECK_EQUAL( mac.nic(), 0xd4e5f6u );
87     BOOST_CHECK_EQUAL( mac.eui64(), 0xa1b2c3fffed4e5f6llu );
88
89     MACAddress mac2;
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 );
98     mac2 = mac;
99     BOOST_CHECK( mac == mac2 );
100     BOOST_CHECK_EQUAL( senf::str(mac2), "a1:b2:c3:d4:e5:f6" );
101
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 );
109
110     BOOST_CHECK_EQUAL( mac, MACAddress::from_eui64(senf::EUI64(0xa1b2c3fffed4e5f6llu)) );
111     BOOST_CHECK_THROW( MACAddress::from_eui64(senf::EUI64(0ull)),
112                        AddressSyntaxException );
113
114     BOOST_CHECK_EQUAL( MACAddress(0x1a2b3c4d5e6fULL).uint64(), 0x1a2b3c4d5e6fULL);
115
116     BOOST_CHECK_EQUAL( mac, senf::EUI64::from_mac(mac) );
117     BOOST_CHECK_EQUAL( senf::EUI64::from_mac(mac), mac );
118 }
119
120 //-/////////////////////////////////////////////////////////////////////////////////////////////////
121 #undef prefix_
122
123
124 // Local Variables:
125 // mode: c++
126 // fill-column: 100
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"
132 // End: