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