scclib
Stable Cloud Computing C++ Library
inetaddr.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <net/inet.h>
32 #include <gtest/gtest.h>
33 #include <iostream>
34 
41 using std::cout;
42 using std::endl;
43 using scc::net::InetAddr;
45 
46 TEST(inetaddr_test, Bad_addr)
47 {
48  InetAddr sa;
49  ASSERT_ANY_THROW(sa.host("deadbeef"));
50 }
51 
52 TEST(inetaddr_test, init_addr)
53 {
54  InetAddr sa;
55  cout << sa << endl;
56  ASSERT_EQ(sa.host(), "::");
57  ASSERT_EQ(sa.port(), 0);
58  ASSERT_EQ(sa.scope_id(), 0);
59  ASSERT_TRUE(sa.test_flags(InetAddrFlag::ipv6));
60  ASSERT_FALSE(sa.test_flags(InetAddrFlag::ipv4));
61 }
62 
63 TEST(inetaddr_test, any_addrs)
64 {
65  InetAddr sa;
66  sa.host("::ffff:0.0.0.0");
67  cout << sa << endl;
68  ASSERT_EQ(sa.host(), "::ffff:0.0.0.0");
70 
71  sa = InetAddr();
72  sa.host("::");
73  cout << sa << endl;
74  ASSERT_EQ(sa.host(), "::");
76 }
77 
78 TEST(inetaddr_test, loop_addrs)
79 {
80  InetAddr sa;
81  sa.host("::ffff:127.0.0.1");
82  cout << sa << endl;
83  ASSERT_EQ(sa.host(), "::ffff:127.0.0.1");
85 
86  sa = InetAddr();
87  sa.host("::1");
88  cout << sa << endl;
89  ASSERT_EQ(sa.host(), "::1");
91 }
92 
93 TEST(inetaddr_test, unicast_global)
94 {
95  InetAddr sa;
96  sa.host("::ffff:192.168.12.24");
97  cout << sa << endl;
98  ASSERT_EQ(sa.host(), "::ffff:192.168.12.24");
100 
101  sa = InetAddr();
102  sa.host("dead::beef:feed");
103  cout << sa << endl;
104  ASSERT_EQ(sa.host(), "dead::beef:feed");
106 }
107 
108 TEST(inetaddr_test, unicast_link)
109 {
110  InetAddr sa;
111  sa.host("fe80::dead:beef");
112  cout << sa << endl;
113  ASSERT_EQ(sa.host(),"fe80::dead:beef");
115 }
116 
117 TEST(inetaddr_test, unicast_site)
118 {
119  InetAddr sa;
120  sa.host("fd00::dead:beef");
121  cout << sa << endl;
122  ASSERT_EQ(sa.host(),"fd00::dead:beef");
124 }
125 
126 
127 TEST(inetaddr_test, multicast_ipv4)
128 {
129  InetAddr sa;
130  sa.host("::ffff:224.1.2.3");
131  cout << sa << endl;
133 
134  sa = InetAddr();
135  sa.host("::ffff:239.1.2.3");
136  cout << sa << endl;
138 
139  sa = InetAddr();
140  sa.host("::ffff:223.1.2.3");
141  cout << sa << endl;
143 
144  sa = InetAddr();
145  sa.host("::ffff:240.1.2.3");
146  cout << sa << endl;
148 }
149 
150 TEST(inetaddr_test, multicast_ipv6)
151 {
152  InetAddr sa;
153  sa.host("ff01::1");
154  cout << sa << endl;
156 
157  sa = InetAddr();
158  sa.host("ff02::1");
159  cout << sa << endl;
161 
162  sa = InetAddr();
163  sa.host("ff01::2");
164  cout << sa << endl;
166 
167  sa = InetAddr();
168  sa.host("ff02::2");
169  cout << sa << endl;
171 
172  sa = InetAddr();
173  sa.host("ff05::2");
174  cout << sa << endl;
176 
177  sa = InetAddr();
178  sa.host("ff18::dead:beef");
179  cout << sa << endl;
181 
182  sa.host("ff03::dead:beef");
183  cout << sa << endl;
185 }
Ipv6 internet address.
Definition: inet.h:120
bool test_flags(int f) const
Test if flags are set in this address.
Definition: inet.h:240
unsigned port() const
Get the port.
Definition: inet.cc:159
void scope_id(uint32_t)
Set the scope id of the address.
Definition: inet.cc:165
virtual std::string host() const
Get host.
Definition: inet.cc:188
InetAddrFlag
Internet address flags.
Definition: inet.h:74
@ unicast
Unicast address.
Definition: inet.h:82
@ link_local
Traffic is restricted to the local link.
Definition: inet.h:87
@ realm_local
Traffic is restricted to the local realm.
Definition: inet.h:88
@ mcast_all_nodes
Reaches all nodes in the scope, e.g. ff0X::1.
Definition: inet.h:100
@ global
Global traffic is allowed.
Definition: inet.h:92
@ org_local
Traffic is restricted to the local organization.
Definition: inet.h:91
@ mcast_all_routers
Reaches all routers in the scope, e.g. ff0X::2.
Definition: inet.h:101
@ any
Any address.
Definition: inet.h:80
@ loopback
Loopback address.
Definition: inet.h:81
@ if_local
Traffic is restricted to the local interface.
Definition: inet.h:86
@ ipv4
IPv4.
Definition: inet.h:76
@ mcast_dynamic
Dynamic (temporary) address, otherwise permanent (assigned).
Definition: inet.h:97
@ ipv6
IPv6.
Definition: inet.h:77
@ site_local
Traffic is restricted to the local site.
Definition: inet.h:90
@ multicast
Multicast address.
Definition: inet.h:83
@ unique_local_address
Address which can be used freely within a site: e.g. fd00::/8.
Definition: inet.h:104
Internet tcp and udp networking.
TEST(inet_example, client_server_stream_test)
[Inet client server]
Definition: inet.cc:521