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