52b1289c4856004507eefe1239a5ec5af6586c79
[senf.git] / senf / Socket / Protocols / INet / INet6Address.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 INet6Address unit tests */
25
26 //#include "INet6Address.test.hh"
27 //#include "INet6Address.test.ih"
28
29 // Custom includes
30 #include "INet6Address.hh"
31 #include <senf/Utils/String.hh>
32 #include <senf/Socket/Protocols/AddressExceptions.hh>
33 #include <senf/Socket/Protocols/Raw/EUI64.hh>
34 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
35
36 #include <senf/Utils/auto_unit_test.hh>
37 #include <boost/test/test_tools.hpp>
38
39 #define prefix_
40 ///////////////////////////////cc.p////////////////////////////////////////
41
42 SENF_AUTO_UNIT_TEST(inet6Address)
43 {
44     using senf::INet6Address;
45     using senf::INet4Address;
46     using senf::AddressSyntaxException;
47     using senf::UnknownHostnameException;
48
49     {
50         INet6Address addr1 (INet6Address::from_string("0102:0304:0506:0708:090A:0B0C:0D0E:0F00"));
51         BOOST_CHECK_EQUAL( addr1[0], 1 );
52         BOOST_CHECK_EQUAL( addr1[1], 2 );
53         BOOST_CHECK_EQUAL( addr1[2], 3 );
54         BOOST_CHECK_EQUAL( addr1[3], 4 );
55         BOOST_CHECK_EQUAL( addr1[4], 5 );
56         BOOST_CHECK_EQUAL( addr1[5], 6 );
57         BOOST_CHECK_EQUAL( addr1[6], 7 );
58         BOOST_CHECK_EQUAL( addr1[7], 8 );
59         BOOST_CHECK_EQUAL( addr1[8], 9 );
60         BOOST_CHECK_EQUAL( addr1[9], 10 );
61         BOOST_CHECK_EQUAL( addr1[10], 11 );
62         BOOST_CHECK_EQUAL( addr1[11], 12 );
63         BOOST_CHECK_EQUAL( addr1[12], 13 );
64         BOOST_CHECK_EQUAL( addr1[13], 14 );
65         BOOST_CHECK_EQUAL( addr1[14], 15 );
66         BOOST_CHECK_EQUAL( addr1[15], 0 );
67         SENF_CHECK_NOT_EQUAL( INet6Address::from_string("www.go6.net"), INet6Address::None );
68         INet6Address addr2;
69         BOOST_CHECK_EQUAL( senf::str(addr2), "::" );
70         addr2 = INet6Address::from_string("::1");
71         SENF_CHECK_NOT_EQUAL( addr1, addr2 );
72         addr1 = INet6Address::from_string("::1");
73         BOOST_CHECK_EQUAL( addr1, addr2 );
74         addr1 = INet6Address::None;
75         addr2 = INet6Address::from_string("::");
76         BOOST_CHECK_EQUAL( addr1, addr2 );
77         BOOST_CHECK_THROW( INet6Address::from_string(""), AddressSyntaxException );
78         BOOST_CHECK_EQUAL( senf::str(addr1), "::" );
79         unsigned char data[] = { 0x12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x21, 0 };
80         INet6Address addr3 (INet6Address::from_data(data));
81         BOOST_CHECK_EQUAL( addr3, INet6Address::from_string("1200::21") );
82         BOOST_CHECK_EQUAL( INet6Address::from_inet4address(INet4Address(0x01020304)),
83                            INet6Address::from_string("::ffff:1.2.3.4") );
84         BOOST_CHECK( INet6Address::from_inet4address(INet4Address(0x01020304)).inet4Mapped());
85
86         BOOST_CHECK_THROW( INet6Address::from_string("1.2.3.4"), UnknownHostnameException );
87         BOOST_CHECK_EQUAL( INet6Address::from_string("1.2.3.4", INet6Address::ResolveINet4),
88                            INet6Address::from_string("::ffff:1.2.3.4") );
89     }
90     {
91         INet6Address addr (INet6Address::from_string("2001:dead:beef::1002:3004"));
92         BOOST_CHECK_EQUAL( addr.network(), senf::INet6Network(
93                                senf::INet6Address(0x2001u,0xdeadu, 0xbeef),64) );
94         BOOST_CHECK_EQUAL( addr.id(), senf::EUI64(0x0000000010023004llu) );
95         BOOST_CHECK( addr.hasEUI64() );
96         BOOST_CHECK( ! INet6Address::from_inet4address(INet4Address(0x01020304)).hasEUI64() );
97         BOOST_CHECK( ! addr.universalId() );
98         BOOST_CHECK( ! addr.groupId() );
99         BOOST_CHECK( addr.unicast() );
100         BOOST_CHECK( ! addr.multicast() );
101         BOOST_CHECK( ! INet6Address::AllNodes.unicast() );
102         BOOST_CHECK( INet6Address::AllNodes.multicast() );
103         BOOST_CHECK_EQUAL( addr.scope(), INet6Address::GlobalScope );
104         BOOST_CHECK( addr.globalScope() );
105         BOOST_CHECK_EQUAL( INet6Address(0xfe80).scope(), INet6Address::LinkScope );
106         BOOST_CHECK_EQUAL( INet6Address(0xfec0).scope(), INet6Address::SiteScope );
107         BOOST_CHECK_EQUAL( INet6Address(0xff00).scope(), INet6Address::ReservedScope );
108         BOOST_CHECK_EQUAL( INet6Address(0xff01).scope(), INet6Address::InterfaceScope );
109         BOOST_CHECK_EQUAL( INet6Address(0xff02).scope(), INet6Address::LinkScope );
110         BOOST_CHECK_EQUAL( INet6Address(0xff03).scope(), INet6Address::ReservedScope );
111         BOOST_CHECK_EQUAL( INet6Address(0xff04).scope(), INet6Address::AdminScope );
112         BOOST_CHECK_EQUAL( INet6Address(0xff05).scope(), INet6Address::SiteScope );
113         BOOST_CHECK_EQUAL( INet6Address(0xff06).scope(), INet6Address::UnassignedScope );
114         BOOST_CHECK_EQUAL( INet6Address(0xff07).scope(), INet6Address::UnassignedScope );
115         BOOST_CHECK_EQUAL( INet6Address(0xff08).scope(), INet6Address::OrganizationScope );
116         BOOST_CHECK_EQUAL( INet6Address(0xff09).scope(), INet6Address::UnassignedScope );
117         BOOST_CHECK_EQUAL( INet6Address(0xff0a).scope(), INet6Address::UnassignedScope );
118         BOOST_CHECK_EQUAL( INet6Address(0xff0b).scope(), INet6Address::UnassignedScope );
119         BOOST_CHECK_EQUAL( INet6Address(0xff0c).scope(), INet6Address::UnassignedScope );
120         BOOST_CHECK_EQUAL( INet6Address(0xff0d).scope(), INet6Address::UnassignedScope );
121         BOOST_CHECK_EQUAL( INet6Address(0xff0e).scope(), INet6Address::GlobalScope );
122         BOOST_CHECK_EQUAL( INet6Address(0xff0f).scope(), INet6Address::ReservedScope );
123         BOOST_CHECK_EQUAL( INet6Address(0u,0u,0u,0u,0u,0xFFFF,0x0102u,0x0304).inet4address(),
124                            INet4Address(0x01020304) );
125         BOOST_CHECK( INet6Address(0u,0u,0u,0u,0u,0u,0x0102u,0x0304).inet4Compatible() );
126         BOOST_CHECK( INet6Address(0u,0u,0u,0u,0u,0xFFFF,0x0102,0x0304).inet4Mapped() );
127         BOOST_CHECK( ! addr.inet4Compatible() );
128         BOOST_CHECK( ! addr.inet4Mapped() );
129
130         BOOST_CHECK( INet6Address::AllNodes.globalMulticastAddr() );
131         BOOST_CHECK( ! INet6Address::AllNodes.prefixMulticastAddr() );
132         BOOST_CHECK( ! INet6Address::AllNodes.embeddedRpAddr() );
133
134         BOOST_CHECK( INet6Address::Loopback );
135         BOOST_CHECK( ! INet6Address::None );
136     }
137     {
138         std::stringstream str;
139         INet6Address addr;
140         str >> addr;
141         BOOST_CHECK( str.fail());
142     }
143     {
144         std::stringstream str ("invalid.host.fhg.de");
145         INet6Address addr;
146         str >> addr;
147         BOOST_CHECK( str.fail());
148     }
149     {
150         std::stringstream str;
151         INet6Address addr (INet6Address::from_string("2001:dead:beef::1002:3004"));
152         str << addr;
153         BOOST_CHECK_EQUAL( str.str(), "2001:dead:beef::1002:3004");
154         str >> addr;
155         BOOST_CHECK( ! str.fail());
156         BOOST_CHECK_EQUAL(addr, INet6Address::from_string("2001:dead:beef::1002:3004"));
157     }
158     {
159         INet6Address addr;
160         addr.network(0x2000010203040506ull);
161         BOOST_CHECK_EQUAL( addr, INet6Address(0x2000u,0x0102u,0x0304u,0x0506u) );
162         addr.id(1u);
163         BOOST_CHECK_EQUAL( addr, INet6Address(0x2000u,0x0102u,0x0304u,0x0506u,0u,0u,0u,1u) );
164     }
165     {
166         using senf::MACAddress;
167         BOOST_CHECK_EQUAL(
168                 INet6Address::from_mac( MACAddress::from_string("00-0C-29-C2-52-FF")),
169                 INet6Address::from_string("fe80::20c:29ff:fec2:52ff") );
170         BOOST_CHECK_EQUAL(
171                 INet6Address::from_eui64( senf::EUI64::from_mac( MACAddress::from_string("a4:ba:db:fd:b8:76"))),
172                 INet6Address::from_string("fe80::a6ba:dbff:fefd:b876") );
173     }
174 }
175
176 SENF_AUTO_UNIT_TEST(inet6Network)
177 {
178     using senf::INet6Address;
179     using senf::INet6Network;
180     using senf::AddressSyntaxException;
181
182     INet6Network net (INet6Address(0xFF14u,0x1234u),32u);
183     BOOST_CHECK_EQUAL( senf::str(net.address()), "ff14:1234::");
184     BOOST_CHECK_EQUAL( net.prefix_len(), 32u );
185     BOOST_CHECK( net );
186     BOOST_CHECK( ! INet6Network() );
187
188     INet6Network net2 ("2001:db8:1234::/44");
189     BOOST_CHECK_EQUAL( net2.address(), INet6Address::from_string("2001:db8:1230::") );
190     BOOST_CHECK_EQUAL( net2.prefix_len(), 44u );
191
192     SENF_CHECK_NOT_EQUAL( net, net2 );
193     BOOST_CHECK( net.match(INet6Address::from_string("ff14:1234::1")) );
194     BOOST_CHECK( ! net2.match(INet6Address::from_string("ff13:1234::1")) );
195     BOOST_CHECK( ! net.match(net2) );
196     BOOST_CHECK( net2.match(INet6Network("2001:db8:1234::/48")) );
197     BOOST_CHECK( ! net2.match(INet6Network("2001:db8:1234::/32")) );
198
199     BOOST_CHECK( ! INet6Network("ff14:1234::1/128").match(INet6Network("ff14:1234::2/128")) );
200
201     BOOST_CHECK_EQUAL( senf::str(net2), "2001:db8:1230::/44" );
202
203     BOOST_CHECK_EQUAL( net2.host(0x1234u), INet6Address::from_string("2001:db8:1230::1234") );
204     BOOST_CHECK_EQUAL( senf::str(net2.subnet(2u,48u)), "2001:db8:1232::/48" );
205
206     BOOST_CHECK_THROW( INet6Network(""), AddressSyntaxException );
207     BOOST_CHECK_THROW( INet6Network("2001:db8:1234::/beef"), AddressSyntaxException );
208
209     {
210         std::stringstream str;
211         INet6Network net;
212         str >> net;
213         BOOST_CHECK( str.fail());
214     }
215     {
216         std::stringstream str;
217         INet6Network net ("2001:db8:1230::/44");
218         str << net;
219         BOOST_CHECK_EQUAL( str.str(), "2001:db8:1230::/44");
220         str >> net;
221         BOOST_CHECK( ! str.fail());
222         BOOST_CHECK_EQUAL(net, INet6Network("2001:db8:1230::/44"));
223     }
224 }
225
226 ///////////////////////////////cc.e////////////////////////////////////////
227 #undef prefix_
228
229
230 // Local Variables:
231 // mode: c++
232 // fill-column: 100
233 // comment-column: 40
234 // c-file-style: "senf"
235 // indent-tabs-mode: nil
236 // ispell-local-dictionary: "american"
237 // compile-command: "scons -u test"
238 // End: