Permalink
Browse files

Add some tests and fix the InterfacePicker behaviour

  • Loading branch information...
peternewman committed Jan 1, 2019
1 parent a8495fb commit ae77be6f576dfd095436acd4ea63d5d42914a23c
@@ -19,6 +19,7 @@
*/

#include <string.h>
#include <algorithm>
#include <string>
#include <vector>

@@ -140,16 +141,37 @@ bool InterfacePicker::ChooseInterface(
bool InterfacePicker::ChooseInterface(
Interface *iface,
const string &ip_or_name,
Interface default_iface,
const Interface default_iface,
const Options &options) const {
Options restricted_options = options;
// Need to force strict mode in the options here, so we only get a real match
restricted_options.specific_only = true;
if (!ip_or_name.empty() && ChooseInterface(iface, ip_or_name, restricted_options)) {
return true;
} else {
iface = &default_iface;
return true;
vector<Interface> interfaces = GetInterfaces(options.include_loopback);
if (interfaces.empty()) {
OLA_INFO << "No interfaces found";
return false;
}

// Check that default_iface is actually in the picker list
vector<Interface>::const_iterator iter = std::find(interfaces.begin(), interfaces.end(), default_iface);
if (iter != interfaces.end()) {
*iface = *iter;
return true;
} else {
OLA_INFO << "Default interface " << default_iface << " does not exist in "
"the InterfacePicker list";
if (options.specific_only) {
return false;
} else {
*iface = interfaces[0];
OLA_DEBUG << "Falling back to interface " << iface->name << " ("
<< iface->ip_address << ")";
return true;
}
}
}
}

@@ -112,22 +112,40 @@ void InterfacePickerTest::testChooseInterface() {
vector<Interface> interfaces;
FakeInterfacePicker picker(interfaces);

// Some test interfaces
Interface iface1;
iface1.name = "eth0";
iface1.index = 1;
OLA_ASSERT_TRUE(IPV4Address::FromString("10.0.0.1", &iface1.ip_address));

Interface iface2;
iface2.name = "eth1";
iface2.index = 2;
OLA_ASSERT_TRUE(IPV4Address::FromString("192.168.1.1", &iface2.ip_address));

Interface iface2_alt;
iface2_alt.name = "eth1";
iface2_alt.index = 2;
OLA_ASSERT_TRUE(IPV4Address::FromString("192.168.1.1", &iface2_alt.ip_address));

// no interfaces
Interface iface;
OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, ""));
// no interfaces, with default
// no interfaces, with string default
OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, "", ""));
OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, "", "foo"));
OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, "foo", ""));
OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, "foo", "bar"));
// no interfaces, with Interface default
Interface blank_iface;
OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, "", blank_iface));
OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, "", iface1));
OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, "foo", blank_iface));
OLA_ASSERT_FALSE(picker.ChooseInterface(&iface, "foo", iface2));
// 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);
@@ -142,12 +160,17 @@ void InterfacePickerTest::testChooseInterface() {
iface = Interface();
OLA_ASSERT_TRUE(picker2.ChooseInterface(&iface, "", "172.16.0.1"));
OLA_ASSERT_EQ(iface1, iface);
iface = Interface();
OLA_ASSERT_TRUE(picker2.ChooseInterface(&iface, "192.168.1.1", iface2));
OLA_ASSERT_EQ(iface1, iface);
iface = Interface();
OLA_ASSERT_TRUE(picker2.ChooseInterface(&iface, "192.168.1.1", blank_iface));
OLA_ASSERT_EQ(iface1, iface);
iface = Interface();
OLA_ASSERT_TRUE(picker2.ChooseInterface(&iface, "", iface1));
OLA_ASSERT_EQ(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);
@@ -162,16 +185,31 @@ void InterfacePickerTest::testChooseInterface() {
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "192.168.1.1", "foo"));
OLA_ASSERT_EQ(iface2, iface);
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "192.168.1.1", blank_iface));
OLA_ASSERT_EQ(iface2, iface);
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "", iface2_alt));
OLA_ASSERT_EQ(iface2, iface);
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "192.168.1.1", iface1));
OLA_ASSERT_EQ(iface2, iface);

// an invalid address should fallback to default
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "foo", "192.168.1.1"));
OLA_ASSERT_EQ(iface2, iface);
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "foo", iface2_alt));
OLA_ASSERT_EQ(iface2, iface);

// a valid address should ignore the default
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "192.168.1.1", "10.0.0.1"));
OLA_ASSERT_EQ(iface2, iface);
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "192.168.1.1", iface1));
OLA_ASSERT_EQ(iface2, iface);

// now check for iface name
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "eth0"));
@@ -192,6 +230,9 @@ void InterfacePickerTest::testChooseInterface() {
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "eth1", "eth0"));
OLA_ASSERT_EQ(iface2, iface);
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "eth1", iface1));
OLA_ASSERT_EQ(iface2, iface);

// a invalid address should return the first one
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "foo"));
@@ -205,6 +246,9 @@ void InterfacePickerTest::testChooseInterface() {
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "", "bar"));
OLA_ASSERT_EQ(iface1, iface);
iface = Interface();
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, "foo", blank_iface));
OLA_ASSERT_EQ(iface1, iface);

// now check by iface index
OLA_ASSERT_TRUE(picker3.ChooseInterface(&iface, 2));
@@ -89,7 +89,7 @@ class InterfacePicker {
bool ChooseInterface(
Interface *iface,
const std::string &ip_or_name,
Interface default_iface,
const Interface default_iface,
const Options &options = Options()) const;
bool ChooseInterface(
Interface *iface,

0 comments on commit ae77be6

Please sign in to comment.