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