Move sourcecode into 'senf/' directory
[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 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@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 MACAddress unit tests */
25
26 //#include "MACAddress.test.hh"
27 //#include "MACAddress.test.ih"
28
29 // Custom includes
30 #include <sstream>
31 #include "MACAddress.hh"
32 #include "../../../Utils/String.hh"
33 #include "EUI64.hh"
34
35 #include "../../../Utils/auto_unit_test.hh"
36 #include <boost/test/test_tools.hpp>
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 BOOST_AUTO_UNIT_TEST(macAddress)
42 {
43     using senf::MACAddress;
44     using senf::AddressSyntaxException;
45
46     std::string test ("A1-b2-C3:d4:E5:f6");
47     MACAddress mac (MACAddress::from_string(test));
48
49     BOOST_CHECK_EQUAL( mac[0], 0xA1u );
50     BOOST_CHECK_EQUAL( mac[1], 0xB2u );
51     BOOST_CHECK_EQUAL( mac[2], 0xC3u );
52     BOOST_CHECK_EQUAL( mac[3], 0xD4u );
53     BOOST_CHECK_EQUAL( mac[4], 0xE5u );
54     BOOST_CHECK_EQUAL( mac[5], 0xF6u );
55
56     {
57         std::stringstream str;
58         str >> mac;
59         BOOST_CHECK( str.fail());
60     }
61     {
62         std::stringstream str ( "invalid_mac_address");
63         str >> mac;
64         BOOST_CHECK( str.fail() );
65     }
66     {
67         std::stringstream str;
68         str << mac;
69         BOOST_CHECK_EQUAL( str.str(), "a1:b2:c3:d4:e5:f6" );
70         str >> mac;
71         BOOST_CHECK( ! str.fail() );
72     }
73     
74     BOOST_CHECK_EQUAL(mac, MACAddress::from_string(test));
75     BOOST_CHECK( ! mac.local() );
76     BOOST_CHECK( mac.multicast() );
77     BOOST_CHECK( ! mac.broadcast() );
78     BOOST_CHECK( mac );
79     BOOST_CHECK_EQUAL( mac.oui(), 0xa1b2c3u );
80     BOOST_CHECK_EQUAL( mac.nic(), 0xd4e5f6u );
81     BOOST_CHECK_EQUAL( mac.eui64(), 0xa1b2c3fffed4e5f6llu );
82
83     MACAddress mac2;
84     BOOST_CHECK( ! mac2 );
85     mac2 = MACAddress::from_string("ff:ff:ff:ff:ff:ff");
86     BOOST_CHECK( mac2.broadcast() );
87     BOOST_CHECK_EQUAL( mac2, MACAddress::Broadcast );
88     char data[] = { 0x01,0x02,0x03,0x04,0x05,0x06 };
89     mac2 = MACAddress::from_data(data);
90     BOOST_CHECK_EQUAL( senf::str(mac2), "01:02:03:04:05:06" );
91     BOOST_CHECK( mac != mac2 );
92     mac2 = mac;
93     BOOST_CHECK( mac == mac2 );
94     BOOST_CHECK_EQUAL( senf::str(mac2), "a1:b2:c3:d4:e5:f6" );
95
96     BOOST_CHECK_THROW( MACAddress::from_string("123:2:3:4:5:6"), AddressSyntaxException );
97     BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05"), AddressSyntaxException );
98     // test all invalid ascii ranges
99     BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:z6"), AddressSyntaxException );
100     BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:/6"), 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
104     BOOST_CHECK_EQUAL( mac, MACAddress::from_eui64(senf::EUI64(0xa1b2c3fffed4e5f6llu)) );
105     BOOST_CHECK_THROW( MACAddress::from_eui64(senf::EUI64(0ull)),
106                        AddressSyntaxException );
107
108     BOOST_CHECK_EQUAL( MACAddress(0x1a2b3c4d5e6fULL).uint64(), 0x1a2b3c4d5e6fULL);
109 }
110
111 ///////////////////////////////cc.e////////////////////////////////////////
112 #undef prefix_
113
114
115 // Local Variables:
116 // mode: c++
117 // fill-column: 100
118 // comment-column: 40
119 // c-file-style: "senf"
120 // indent-tabs-mode: nil
121 // ispell-local-dictionary: "american"
122 // compile-command: "scons -u test"
123 // End: