xref: /haiku/src/tests/kits/net/libnetapi/NetworkInterfaceTest.cpp (revision ce9e476882a6d9f5ff13efb45809906f05d9aa40)
1*ce9e4768SAxel Dörfler /*
2*ce9e4768SAxel Dörfler  * Copyright 2011, Axel Dörfler, axeld@pinc-software.de.
3*ce9e4768SAxel Dörfler  * Distributed under the terms of the MIT License.
4*ce9e4768SAxel Dörfler  */
5*ce9e4768SAxel Dörfler 
6*ce9e4768SAxel Dörfler 
7*ce9e4768SAxel Dörfler #include "NetworkInterfaceTest.h"
8*ce9e4768SAxel Dörfler 
9*ce9e4768SAxel Dörfler #include <NetworkInterface.h>
10*ce9e4768SAxel Dörfler #include <NetworkRoster.h>
11*ce9e4768SAxel Dörfler 
12*ce9e4768SAxel Dörfler #include <cppunit/TestCaller.h>
13*ce9e4768SAxel Dörfler #include <cppunit/TestSuite.h>
14*ce9e4768SAxel Dörfler 
15*ce9e4768SAxel Dörfler 
16*ce9e4768SAxel Dörfler NetworkInterfaceTest::NetworkInterfaceTest()
17*ce9e4768SAxel Dörfler {
18*ce9e4768SAxel Dörfler }
19*ce9e4768SAxel Dörfler 
20*ce9e4768SAxel Dörfler 
21*ce9e4768SAxel Dörfler NetworkInterfaceTest::~NetworkInterfaceTest()
22*ce9e4768SAxel Dörfler {
23*ce9e4768SAxel Dörfler }
24*ce9e4768SAxel Dörfler 
25*ce9e4768SAxel Dörfler 
26*ce9e4768SAxel Dörfler void
27*ce9e4768SAxel Dörfler NetworkInterfaceTest::TestUnset()
28*ce9e4768SAxel Dörfler {
29*ce9e4768SAxel Dörfler 	BNetworkInterface unset("test");
30*ce9e4768SAxel Dörfler 	unset.Unset();
31*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(unset.Name() == NULL || unset.Name()[0] == '\0');
32*ce9e4768SAxel Dörfler }
33*ce9e4768SAxel Dörfler 
34*ce9e4768SAxel Dörfler 
35*ce9e4768SAxel Dörfler void
36*ce9e4768SAxel Dörfler NetworkInterfaceTest::TestFindAddress()
37*ce9e4768SAxel Dörfler {
38*ce9e4768SAxel Dörfler 	BNetworkInterface interface("testInterface");
39*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(BNetworkRoster::Default().AddInterface(interface) == B_OK);
40*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.CountAddresses() == 0);
41*ce9e4768SAxel Dörfler 
42*ce9e4768SAxel Dörfler 	// Add first
43*ce9e4768SAxel Dörfler 
44*ce9e4768SAxel Dörfler 	BNetworkInterfaceAddress first;
45*ce9e4768SAxel Dörfler 	first.SetAddress(BNetworkAddress(AF_INET, "8.8.8.8"));
46*ce9e4768SAxel Dörfler 
47*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindAddress(first.Address()) < 0);
48*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.AddAddress(first) == B_OK);
49*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindAddress(first.Address()) == 0);
50*ce9e4768SAxel Dörfler 
51*ce9e4768SAxel Dörfler 	// Add second
52*ce9e4768SAxel Dörfler 
53*ce9e4768SAxel Dörfler 	BNetworkInterfaceAddress second;
54*ce9e4768SAxel Dörfler 	second.SetAddress(BNetworkAddress(AF_INET6, "::1"));
55*ce9e4768SAxel Dörfler 
56*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindAddress(second.Address()) < 0);
57*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.AddAddress(second) == B_OK);
58*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindAddress(second.Address()) >= 0);
59*ce9e4768SAxel Dörfler 
60*ce9e4768SAxel Dörfler 	// Remove them again
61*ce9e4768SAxel Dörfler 
62*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.RemoveAddress(first) == B_OK);
63*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindAddress(first.Address()) < 0);
64*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindAddress(second.Address()) >= 0);
65*ce9e4768SAxel Dörfler 
66*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.RemoveAddress(second.Address()) == B_OK);
67*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindAddress(first.Address()) < 0);
68*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindAddress(second.Address()) < 0);
69*ce9e4768SAxel Dörfler 
70*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(BNetworkRoster::Default().RemoveInterface(interface)
71*ce9e4768SAxel Dörfler 		== B_OK);
72*ce9e4768SAxel Dörfler }
73*ce9e4768SAxel Dörfler 
74*ce9e4768SAxel Dörfler 
75*ce9e4768SAxel Dörfler void
76*ce9e4768SAxel Dörfler NetworkInterfaceTest::TestFindFirstAddress()
77*ce9e4768SAxel Dörfler {
78*ce9e4768SAxel Dörfler 	BNetworkInterface interface("testInterface");
79*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(BNetworkRoster::Default().AddInterface(interface) == B_OK);
80*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.CountAddresses() == 0);
81*ce9e4768SAxel Dörfler 
82*ce9e4768SAxel Dörfler 	// Add first
83*ce9e4768SAxel Dörfler 
84*ce9e4768SAxel Dörfler 	BNetworkInterfaceAddress first;
85*ce9e4768SAxel Dörfler 	first.SetAddress(BNetworkAddress(AF_INET, "8.8.8.8"));
86*ce9e4768SAxel Dörfler 
87*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) < 0);
88*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET6) < 0);
89*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.AddAddress(first) == B_OK);
90*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) == 0);
91*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET6) < 0);
92*ce9e4768SAxel Dörfler 
93*ce9e4768SAxel Dörfler 	// Add second
94*ce9e4768SAxel Dörfler 
95*ce9e4768SAxel Dörfler 	BNetworkInterfaceAddress second;
96*ce9e4768SAxel Dörfler 	second.SetAddress(BNetworkAddress(AF_INET6, "::1"));
97*ce9e4768SAxel Dörfler 
98*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.AddAddress(second) == B_OK);
99*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) >= 0);
100*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET6) >= 0);
101*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET)
102*ce9e4768SAxel Dörfler 		!= interface.FindFirstAddress(AF_INET6));
103*ce9e4768SAxel Dörfler 
104*ce9e4768SAxel Dörfler 	// Remove them again
105*ce9e4768SAxel Dörfler 
106*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.RemoveAddress(first) == B_OK);
107*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) < 0);
108*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET6) >= 0);
109*ce9e4768SAxel Dörfler 
110*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.RemoveAddress(second.Address()) == B_OK);
111*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET) < 0);
112*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(interface.FindFirstAddress(AF_INET6) < 0);
113*ce9e4768SAxel Dörfler 
114*ce9e4768SAxel Dörfler 	CPPUNIT_ASSERT(BNetworkRoster::Default().RemoveInterface(interface)
115*ce9e4768SAxel Dörfler 		== B_OK);
116*ce9e4768SAxel Dörfler }
117*ce9e4768SAxel Dörfler 
118*ce9e4768SAxel Dörfler 
119*ce9e4768SAxel Dörfler /*static*/ void
120*ce9e4768SAxel Dörfler NetworkInterfaceTest::AddTests(BTestSuite& parent)
121*ce9e4768SAxel Dörfler {
122*ce9e4768SAxel Dörfler 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NetworkInterfaceTest");
123*ce9e4768SAxel Dörfler 
124*ce9e4768SAxel Dörfler 	suite.addTest(new CppUnit::TestCaller<NetworkInterfaceTest>(
125*ce9e4768SAxel Dörfler 		"NetworkInterfaceTest::TestUnset", &NetworkInterfaceTest::TestUnset));
126*ce9e4768SAxel Dörfler 	suite.addTest(new CppUnit::TestCaller<NetworkInterfaceTest>(
127*ce9e4768SAxel Dörfler 		"NetworkInterfaceTest::TestFindAddress",
128*ce9e4768SAxel Dörfler 		&NetworkInterfaceTest::TestFindAddress));
129*ce9e4768SAxel Dörfler 	suite.addTest(new CppUnit::TestCaller<NetworkInterfaceTest>(
130*ce9e4768SAxel Dörfler 		"NetworkInterfaceTest::TestFindFirstAddress",
131*ce9e4768SAxel Dörfler 		&NetworkInterfaceTest::TestFindFirstAddress));
132*ce9e4768SAxel Dörfler 
133*ce9e4768SAxel Dörfler 	parent.addTest("NetworkInterfaceTest", &suite);
134*ce9e4768SAxel Dörfler }
135