File: InterfacePickerTest.cpp

package info (click to toggle)
ola 0.9.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 21,340 kB
  • ctags: 23,021
  • sloc: cpp: 129,922; python: 12,265; sh: 11,778; makefile: 2,288; ansic: 1,775; java: 518; xml: 214
file content (141 lines) | stat: -rw-r--r-- 4,415 bytes parent folder | download
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
/*
 * 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);
  OLA_ASSERT_TRUE(interfaces.size() > 0);

  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);
  OLA_ASSERT_TRUE(interfaces.size() > 0);

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


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

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

  // now with one iface that doesn't match
  Interface iface1;
  iface1.name = "eth0";
  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";
  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);
}