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