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