Termlib: fixed endian bug while reading terminfo file
[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 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief INet6Address unit tests */
30
31 //#include "INet6Address.test.hh"
32 //#include "INet6Address.test.ih"
33
34 // Custom includes
35 #include "INet6Address.hh"
36 #include <senf/Utils/String.hh>
37 #include <senf/Socket/Protocols/AddressExceptions.hh>
38 #include <senf/Socket/Protocols/Raw/EUI64.hh>
39 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
40
41 #include <senf/Utils/auto_unit_test.hh>
42 #include <boost/test/test_tools.hpp>
43
44 #define prefix_
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46
47 SENF_AUTO_UNIT_TEST(inet6Address)
48 {
49     using senf::INet6Address;
50     using senf::INet4Address;
51     using senf::AddressSyntaxException;
52     using senf::UnknownHostnameException;
53
54     {
55         INet6Address addr1 (INet6Address::from_string("0102:0304:0506:0708:090A:0B0C:0D0E:0F00"));
56         BOOST_CHECK_EQUAL( addr1[0], 1 );
57         BOOST_CHECK_EQUAL( addr1[1], 2 );
58         BOOST_CHECK_EQUAL( addr1[2], 3 );
59         BOOST_CHECK_EQUAL( addr1[3], 4 );
60         BOOST_CHECK_EQUAL( addr1[4], 5 );
61         BOOST_CHECK_EQUAL( addr1[5], 6 );
62         BOOST_CHECK_EQUAL( addr1[6], 7 );
63         BOOST_CHECK_EQUAL( addr1[7], 8 );
64         BOOST_CHECK_EQUAL( addr1[8], 9 );
65         BOOST_CHECK_EQUAL( addr1[9], 10 );
66         BOOST_CHECK_EQUAL( addr1[10], 11 );
67         BOOST_CHECK_EQUAL( addr1[11], 12 );
68         BOOST_CHECK_EQUAL( addr1[12], 13 );
69         BOOST_CHECK_EQUAL( addr1[13], 14 );
70         BOOST_CHECK_EQUAL( addr1[14], 15 );
71         BOOST_CHECK_EQUAL( addr1[15], 0 );
72         SENF_CHECK_NOT_EQUAL( INet6Address::from_string("www.go6.net"), INet6Address::None );
73         INet6Address addr2;
74         BOOST_CHECK_EQUAL( senf::str(addr2), "::" );
75         addr2 = INet6Address::from_string("::1");
76         SENF_CHECK_NOT_EQUAL( addr1, addr2 );
77         addr1 = INet6Address::from_string("::1");
78         BOOST_CHECK_EQUAL( addr1, addr2 );
79         addr1 = INet6Address::None;
80         addr2 = INet6Address::from_string("::");
81         BOOST_CHECK_EQUAL( addr1, addr2 );
82         BOOST_CHECK_THROW( INet6Address::from_string(""), AddressSyntaxException );
83         BOOST_CHECK_EQUAL( senf::str(addr1), "::" );
84         unsigned char data[] = { 0x12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x21, 0 };
85         INet6Address addr3 (INet6Address::from_data(data));
86         BOOST_CHECK_EQUAL( addr3, INet6Address::from_string("1200::21") );
87         BOOST_CHECK_EQUAL( INet6Address::from_inet4address(INet4Address(0x01020304)),
88                            INet6Address::from_string("::ffff:1.2.3.4") );
89         BOOST_CHECK( INet6Address::from_inet4address(INet4Address(0x01020304)).inet4Mapped());
90
91         BOOST_CHECK_THROW( INet6Address::from_string("1.2.3.4"), UnknownHostnameException );
92         BOOST_CHECK_EQUAL( INet6Address::from_string("1.2.3.4", INet6Address::ResolveINet4),
93                            INet6Address::from_string("::ffff:1.2.3.4") );
94     }
95     {
96         INet6Address addr (INet6Address::from_string("2001:dead:beef::1002:3004"));
97         BOOST_CHECK_EQUAL( addr.network(), senf::INet6Network(
98                                senf::INet6Address(0x2001u,0xdeadu, 0xbeef),64) );
99         BOOST_CHECK_EQUAL( addr.id(), senf::EUI64(0x0000000010023004llu) );
100         BOOST_CHECK( addr.hasEUI64() );
101         BOOST_CHECK( ! INet6Address::from_inet4address(INet4Address(0x01020304)).hasEUI64() );
102         BOOST_CHECK( ! addr.universalId() );
103         BOOST_CHECK( ! addr.groupId() );
104         BOOST_CHECK( addr.unicast() );
105         BOOST_CHECK( ! addr.multicast() );
106         BOOST_CHECK( ! INet6Address::AllNodes.unicast() );
107         BOOST_CHECK( INet6Address::AllNodes.multicast() );
108         BOOST_CHECK_EQUAL( addr.scope(), INet6Address::GlobalScope );
109         BOOST_CHECK( addr.globalScope() );
110         BOOST_CHECK_EQUAL( INet6Address(0xfe80).scope(), INet6Address::LinkScope );
111         BOOST_CHECK_EQUAL( INet6Address(0xfec0).scope(), INet6Address::SiteScope );
112         BOOST_CHECK_EQUAL( INet6Address(0xff00).scope(), INet6Address::ReservedScope );
113         BOOST_CHECK_EQUAL( INet6Address(0xff01).scope(), INet6Address::InterfaceScope );
114         BOOST_CHECK_EQUAL( INet6Address(0xff02).scope(), INet6Address::LinkScope );
115         BOOST_CHECK_EQUAL( INet6Address(0xff03).scope(), INet6Address::ReservedScope );
116         BOOST_CHECK_EQUAL( INet6Address(0xff04).scope(), INet6Address::AdminScope );
117         BOOST_CHECK_EQUAL( INet6Address(0xff05).scope(), INet6Address::SiteScope );
118         BOOST_CHECK_EQUAL( INet6Address(0xff06).scope(), INet6Address::UnassignedScope );
119         BOOST_CHECK_EQUAL( INet6Address(0xff07).scope(), INet6Address::UnassignedScope );
120         BOOST_CHECK_EQUAL( INet6Address(0xff08).scope(), INet6Address::OrganizationScope );
121         BOOST_CHECK_EQUAL( INet6Address(0xff09).scope(), INet6Address::UnassignedScope );
122         BOOST_CHECK_EQUAL( INet6Address(0xff0a).scope(), INet6Address::UnassignedScope );
123         BOOST_CHECK_EQUAL( INet6Address(0xff0b).scope(), INet6Address::UnassignedScope );
124         BOOST_CHECK_EQUAL( INet6Address(0xff0c).scope(), INet6Address::UnassignedScope );
125         BOOST_CHECK_EQUAL( INet6Address(0xff0d).scope(), INet6Address::UnassignedScope );
126         BOOST_CHECK_EQUAL( INet6Address(0xff0e).scope(), INet6Address::GlobalScope );
127         BOOST_CHECK_EQUAL( INet6Address(0xff0f).scope(), INet6Address::ReservedScope );
128         BOOST_CHECK_EQUAL( INet6Address(0u,0u,0u,0u,0u,0xFFFF,0x0102u,0x0304).inet4address(),
129                            INet4Address(0x01020304) );
130         BOOST_CHECK( INet6Address(0u,0u,0u,0u,0u,0u,0x0102u,0x0304).inet4Compatible() );
131         BOOST_CHECK( INet6Address(0u,0u,0u,0u,0u,0xFFFF,0x0102,0x0304).inet4Mapped() );
132         BOOST_CHECK( ! addr.inet4Compatible() );
133         BOOST_CHECK( ! addr.inet4Mapped() );
134
135         BOOST_CHECK( INet6Address::AllNodes.globalMulticastAddr() );
136         BOOST_CHECK( ! INet6Address::AllNodes.prefixMulticastAddr() );
137         BOOST_CHECK( ! INet6Address::AllNodes.embeddedRpAddr() );
138
139         BOOST_CHECK( INet6Address::Loopback );
140         BOOST_CHECK( ! INet6Address::None );
141     }
142     {
143         std::stringstream str;
144         INet6Address addr;
145         str >> addr;
146         BOOST_CHECK( str.fail());
147     }
148     {
149         std::stringstream str ("invalid.host.fhg.de");
150         INet6Address addr;
151         str >> addr;
152         BOOST_CHECK( str.fail());
153     }
154     {
155         std::stringstream str;
156         INet6Address addr (INet6Address::from_string("2001:dead:beef::1002:3004"));
157         str << addr;
158         BOOST_CHECK_EQUAL( str.str(), "2001:dead:beef::1002:3004");
159         str >> addr;
160         BOOST_CHECK( ! str.fail());
161         BOOST_CHECK_EQUAL(addr, INet6Address::from_string("2001:dead:beef::1002:3004"));
162     }
163     {
164         INet6Address addr;
165         addr.network(0x2000010203040506ull);
166         BOOST_CHECK_EQUAL( addr, INet6Address(0x2000u,0x0102u,0x0304u,0x0506u) );
167         addr.id(1u);
168         BOOST_CHECK_EQUAL( addr, INet6Address(0x2000u,0x0102u,0x0304u,0x0506u,0u,0u,0u,1u) );
169     }
170     {
171         using senf::MACAddress;
172         BOOST_CHECK_EQUAL(
173                 INet6Address::from_mac( MACAddress::from_string("00-0C-29-C2-52-FF")),
174                 INet6Address::from_string("fe80::20c:29ff:fec2:52ff") );
175         BOOST_CHECK_EQUAL(
176                 INet6Address::from_eui64( senf::EUI64::from_mac( MACAddress::from_string("a4:ba:db:fd:b8:76"))),
177                 INet6Address::from_string("fe80::a6ba:dbff:fefd:b876") );
178     }
179 }
180
181 SENF_AUTO_UNIT_TEST(inet6Network)
182 {
183     using senf::INet6Address;
184     using senf::INet6Network;
185     using senf::AddressSyntaxException;
186
187     INet6Network net (INet6Address(0xFF14u,0x1234u),32u);
188     BOOST_CHECK_EQUAL( senf::str(net.address()), "ff14:1234::");
189     BOOST_CHECK_EQUAL( net.prefix_len(), 32u );
190     BOOST_CHECK( net );
191     BOOST_CHECK( ! INet6Network() );
192
193     INet6Network net2 ("2001:db8:1234::/44");
194     BOOST_CHECK_EQUAL( net2.address(), INet6Address::from_string("2001:db8:1230::") );
195     BOOST_CHECK_EQUAL( net2.prefix_len(), 44u );
196
197     SENF_CHECK_NOT_EQUAL( net, net2 );
198     BOOST_CHECK( net.match(INet6Address::from_string("ff14:1234::1")) );
199     BOOST_CHECK( ! net2.match(INet6Address::from_string("ff13:1234::1")) );
200     BOOST_CHECK( ! net.match(net2) );
201     BOOST_CHECK( net2.match(INet6Network("2001:db8:1234::/48")) );
202     BOOST_CHECK( ! net2.match(INet6Network("2001:db8:1234::/32")) );
203
204     BOOST_CHECK( ! INet6Network("ff14:1234::1/128").match(INet6Network("ff14:1234::2/128")) );
205
206     BOOST_CHECK_EQUAL( senf::str(net2), "2001:db8:1230::/44" );
207
208     BOOST_CHECK_EQUAL( net2.host(0x1234u), INet6Address::from_string("2001:db8:1230::1234") );
209     BOOST_CHECK_EQUAL( senf::str(net2.subnet(2u,48u)), "2001:db8:1232::/48" );
210
211     BOOST_CHECK_THROW( INet6Network(""), AddressSyntaxException );
212     BOOST_CHECK_THROW( INet6Network("2001:db8:1234::/beef"), AddressSyntaxException );
213     BOOST_CHECK_THROW( INet6Network("2001:db8:1234::/129"), AddressSyntaxException );
214
215     {
216         std::stringstream str;
217         INet6Network net;
218         str >> net;
219         BOOST_CHECK( str.fail());
220     }
221     {
222         std::stringstream str;
223         INet6Network net ("2001:db8:1230::/44");
224         str << net;
225         BOOST_CHECK_EQUAL( str.str(), "2001:db8:1230::/44");
226         str >> net;
227         BOOST_CHECK( ! str.fail());
228         BOOST_CHECK_EQUAL(net, INet6Network("2001:db8:1230::/44"));
229     }
230 }
231
232 //-/////////////////////////////////////////////////////////////////////////////////////////////////
233 #undef prefix_
234
235
236 // Local Variables:
237 // mode: c++
238 // fill-column: 100
239 // comment-column: 40
240 // c-file-style: "senf"
241 // indent-tabs-mode: nil
242 // ispell-local-dictionary: "american"
243 // compile-command: "scons -u test"
244 // End: