File: InterfacePickerTest.cpp

package info (click to toggle)
ola 0.10.8.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: cpp: 132,274; python: 14,082; javascript: 6,774; sh: 4,616; ansic: 2,189; java: 518; xml: 253; makefile: 183
file content (167 lines) | stat: -rw-r--r-- 5,326 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * InterfacePickerTest.cpp
 * Test fixture for the InterfacePicker class
 * Copyright (C) 2005 Simon Newton
 */

#include <cppunit/extensions/HelperMacros.h>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

#include "common/network/FakeInterfacePicker.h"
#include "ola/network/InterfacePicker.h"
#include "ola/Logging.h"
#include "ola/testing/TestUtils.h"


using ola::network::FakeInterfacePicker;
using ola::network::IPV4Address;
using ola::network::Interface;
using ola::network::InterfacePicker;
using ola::network::MACAddress;
using std::auto_ptr;
using std::cout;
using std::endl;
using std::string;
using std::vector;

class InterfacePickerTest: public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(InterfacePickerTest);
  CPPUNIT_TEST(testGetInterfaces);
  CPPUNIT_TEST(testGetLoopbackInterfaces);
  CPPUNIT_TEST(testChooseInterface);
  CPPUNIT_TEST_SUITE_END();

 public:
    void testGetInterfaces();
    void testGetLoopbackInterfaces();
    void testChooseInterface();
};

CPPUNIT_TEST_SUITE_REGISTRATION(InterfacePickerTest);


/*
 * Check that we find at least one candidate interface.
 */
void InterfacePickerTest::testGetInterfaces() {
  auto_ptr<InterfacePicker> picker(InterfacePicker::NewPicker());
  vector<Interface> interfaces = picker->GetInterfaces(true);
#ifndef _WIN32
  // If a Windows box is not on a network, and doesn't have it's loopback, there
  // may be zero interfaces present so we skip this check
  OLA_ASSERT_GT(interfaces.size(), 0);
#else
  OLA_WARN << "Windows found " << interfaces.size() << " interfaces";
#endif  // _WIN32

  vector<Interface>::iterator iter;
  cout << endl;
  for (iter = interfaces.begin(); iter != interfaces.end(); ++iter) {
    cout << iter->name << endl;
    cout << " index: " << iter->index << endl;
    cout << " ip: " << iter->ip_address << endl;
    cout << " bcast: " << iter->bcast_address << endl;
    cout << " subnet: " << iter->subnet_mask << endl;
    cout << " type: " << iter->type << endl;
    cout << " hw_addr: " << iter->hw_address << endl;
    cout << endl;
    cout << "---------------" << endl;
  }
}


/*
 * Check that we find a loopback interface.
 */
void InterfacePickerTest::testGetLoopbackInterfaces() {
  auto_ptr<InterfacePicker> picker(InterfacePicker::NewPicker());
  vector<Interface> interfaces = picker->GetInterfaces(true);
#ifndef _WIN32
  // If a Windows box is not on a network, and doesn't have it's loopback, there
  // may be zero interfaces present so we skip this check
  OLA_ASSERT_GT(interfaces.size(), 0);
#else
  OLA_WARN << "Windows found " << interfaces.size() << " interfaces";
#endif  // _WIN32

  vector<Interface>::iterator iter;
  unsigned int loopback_count = 0;
  for (iter = interfaces.begin(); iter != interfaces.end(); ++iter) {
    if (iter->loopback)
      loopback_count++;
  }
#ifndef _WIN32
  OLA_ASSERT_GT(loopback_count, 0);
#endif  // _WIN32
}


void InterfacePickerTest::testChooseInterface() {
  vector<Interface> interfaces;
  FakeInterfacePicker picker(interfaces);

  // no interfaces
  Interface iface;
  OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, ""));
  // no interfaces, by index
  OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, 0));

  // now with one iface that doesn't match
  Interface iface1;
  iface1.name = "eth0";
  iface1.index = 1;
  OLA_ASSERT_TRUE(IPV4Address::FromString("10.0.0.1", &iface1.ip_address));
  interfaces.push_back(iface1);

  FakeInterfacePicker picker2(interfaces);
  OLA_ASSERT_TRUE(picker2.ChooseInterface(&iface, "192.168.1.1"));
  OLA_ASSERT_TRUE(iface1 == iface);

  // check that preferred works
  Interface iface2;
  iface2.name = "eth1";
  iface2.index = 2;
  OLA_ASSERT_TRUE(IPV4Address::FromString("192.168.1.1", &iface2.ip_address));
  interfaces.push_back(iface2);

  FakeInterfacePicker picker3(interfaces);
  OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "192.168.1.1"));
  OLA_ASSERT_TRUE(iface2 == iface);

  // now check for iface name
  OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "eth0"));
  OLA_ASSERT_TRUE(iface1 == iface);

  OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "eth1"));
  OLA_ASSERT_TRUE(iface2 == iface);

  // a invalid address should return the first one
  OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "foo"));
  OLA_ASSERT_TRUE(iface1 == iface);

  // now check by iface index
  OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, 2));
  OLA_ASSERT_TRUE(iface2 == iface);

  // an invalid index should return the first one
  OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, 3));
  OLA_ASSERT_TRUE(iface1 == iface);
}