xref: /haiku/src/tests/kits/net/libnetapi/NetworkAddressTest.cpp (revision aa3083e086e5a929c061c72983e09d916c548a38)
1 /*
2  * Copyright 2010-2015, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "NetworkAddressTest.h"
8 
9 #include <arpa/inet.h>
10 #include <netinet6/in6.h>
11 
12 #include <NetworkAddress.h>
13 #include <TypeConstants.h>
14 
15 #include <cppunit/TestCaller.h>
16 #include <cppunit/TestSuite.h>
17 
18 
19 NetworkAddressTest::NetworkAddressTest()
20 {
21 }
22 
23 
24 NetworkAddressTest::~NetworkAddressTest()
25 {
26 }
27 
28 
29 void
30 NetworkAddressTest::TestUnset()
31 {
32 	BNetworkAddress unset;
33 
34 	CPPUNIT_ASSERT(unset.Family() == AF_UNSPEC);
35 	CPPUNIT_ASSERT(unset.Port() == 0);
36 
37 	BNetworkAddress set(AF_INET, NULL);
38 	CPPUNIT_ASSERT(set.Family() == AF_INET);
39 	CPPUNIT_ASSERT(unset != set);
40 
41 	set.Unset();
42 	CPPUNIT_ASSERT(unset == set);
43 }
44 
45 
46 void
47 NetworkAddressTest::TestSetTo()
48 {
49 	BNetworkAddress address;
50 
51 	CPPUNIT_ASSERT(address.SetTo("127.0.0.1") == B_OK);
52 	CPPUNIT_ASSERT(address.Family() == AF_INET);
53 	CPPUNIT_ASSERT(address == BNetworkAddress(htonl(INADDR_LOOPBACK)));
54 
55 	CPPUNIT_ASSERT(address.SetTo("::1") == B_OK);
56 	CPPUNIT_ASSERT(address.Family() == AF_INET6);
57 	CPPUNIT_ASSERT(address == BNetworkAddress(in6addr_loopback));
58 
59 	CPPUNIT_ASSERT(address.SetTo(AF_INET, "::1") != B_OK);
60 	CPPUNIT_ASSERT(address.SetTo(AF_INET6, "127.0.0.1") != B_OK);
61 	CPPUNIT_ASSERT(address.SetTo(AF_INET, "127.0.0.1") == B_OK);
62 	CPPUNIT_ASSERT(address.SetTo(AF_INET6, "::1") == B_OK);
63 }
64 
65 
66 void
67 NetworkAddressTest::TestWildcard()
68 {
69 	BNetworkAddress wildcard;
70 	wildcard.SetToWildcard(AF_INET);
71 
72 	CPPUNIT_ASSERT(wildcard.Family() == AF_INET);
73 	CPPUNIT_ASSERT(wildcard.Length() == sizeof(sockaddr_in));
74 	CPPUNIT_ASSERT(wildcard.Port() == 0);
75 	CPPUNIT_ASSERT(((sockaddr_in&)wildcard.SockAddr()).sin_addr.s_addr
76 		== INADDR_ANY);
77 	CPPUNIT_ASSERT(wildcard.IsEmpty());
78 
79 	BNetworkAddress null(AF_INET, NULL);
80 	CPPUNIT_ASSERT(wildcard == null);
81 
82 	wildcard.SetPort(555);
83 	CPPUNIT_ASSERT(!wildcard.IsEmpty());
84 }
85 
86 
87 void
88 NetworkAddressTest::TestNullAddr()
89 {
90 	BNetworkAddress nullAddr(AF_INET, NULL, 555);
91 	CPPUNIT_ASSERT(nullAddr.InitCheck() == B_OK);
92 
93 	CPPUNIT_ASSERT(nullAddr.Family() == AF_INET);
94 	CPPUNIT_ASSERT(nullAddr.Length() == sizeof(sockaddr_in));
95 	CPPUNIT_ASSERT(nullAddr.Port() == 555);
96 	CPPUNIT_ASSERT(!nullAddr.IsEmpty());
97 }
98 
99 
100 void
101 NetworkAddressTest::TestSetAddressFromFamilyPort()
102 {
103 	BString addressStr = "192.168.1.1";
104 	BNetworkAddress nullAddr(AF_INET, NULL, 555);
105 	in_addr_t inetAddress = inet_addr(addressStr.String());
106 	CPPUNIT_ASSERT(nullAddr.SetAddress(inetAddress) == B_OK);
107 	CPPUNIT_ASSERT(nullAddr.InitCheck() == B_OK);
108 
109 	CPPUNIT_ASSERT(nullAddr.Family() == AF_INET);
110 	CPPUNIT_ASSERT(nullAddr.Length() == sizeof(sockaddr_in));
111 
112 	sockaddr_in& sin = (sockaddr_in&)nullAddr.SockAddr();
113 	CPPUNIT_ASSERT(addressStr == inet_ntoa(sin.sin_addr));
114 	CPPUNIT_ASSERT(nullAddr.Port() == 555);
115 	CPPUNIT_ASSERT(!nullAddr.IsEmpty());
116 }
117 
118 
119 void
120 NetworkAddressTest::TestIsLocal()
121 {
122 	BNetworkAddress local(AF_INET, "localhost");
123 	CPPUNIT_ASSERT(local.IsLocal());
124 
125 	BNetworkAddress google(AF_INET, "google.com");
126 	CPPUNIT_ASSERT(!google.IsLocal());
127 }
128 
129 
130 void
131 NetworkAddressTest::TestFlatten()
132 {
133 	// IPv4
134 
135 	BNetworkAddress ipv4(AF_INET, "localhost", 9999);
136 
137 	char buffer[256];
138 	CPPUNIT_ASSERT(ipv4.Flatten(buffer, sizeof(buffer)) == B_OK);
139 
140 	BNetworkAddress unflattened;
141 	CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer,
142 		sizeof(buffer)) == B_OK);
143 
144 	// unflatten buffer too small
145 	CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 0)
146 		!= B_OK);
147 	CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 3)
148 		!= B_OK);
149 	CPPUNIT_ASSERT(unflattened.Unflatten(B_NETWORK_ADDRESS_TYPE, buffer, 16)
150 		!= B_OK);
151 
152 	CPPUNIT_ASSERT(ipv4 == unflattened);
153 }
154 
155 
156 /*static*/ void
157 NetworkAddressTest::AddTests(BTestSuite& parent)
158 {
159 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NetworkAddressTest");
160 
161 	suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>(
162 		"NetworkAddressTest::TestUnset", &NetworkAddressTest::TestUnset));
163 	suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>(
164 		"NetworkAddressTest::TestSetTo", &NetworkAddressTest::TestSetTo));
165 	suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>(
166 		"NetworkAddressTest::TestWildcard", &NetworkAddressTest::TestWildcard));
167 	suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>(
168 		"NetworkAddressTest::TestNullAddr", &NetworkAddressTest::TestNullAddr));
169 	suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>(
170 		"NetworkAddressTest::TestSetAddressFromFamilyPort",
171 		&NetworkAddressTest::TestSetAddressFromFamilyPort));
172 	suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>(
173 		"NetworkAddressTest::TestIsLocal", &NetworkAddressTest::TestIsLocal));
174 	suite.addTest(new CppUnit::TestCaller<NetworkAddressTest>(
175 		"NetworkAddressTest::TestFlatten", &NetworkAddressTest::TestFlatten));
176 
177 	parent.addTest("NetworkAddressTest", &suite);
178 }
179