Makes libjingle_peerconnection_android_unittest run on networkless devices.
PeerConnectionTest.java currently works, but only on a device with
network interfaces up. This is not a problem for desktop, but it is a
problem when running on Android devices since the devices in the lab
generally don't have network (due to the chaotic radio environment in
the device labs, devices are simply kept in flight mode).
The test does work if one modifies this line in the file
webrtc/base/network.cc:
bool ignored = ((cursor->ifa_flags & IFF_LOOPBACK) ||
IsIgnoredNetwork(*network));
If we remove the IFF_LOOPBACK clause, the test starts working on
an Android device in flight mode. This is nice - we're running the
call and packets interact with the OS network stack, which is good
for this end-to-end test. We can't just remove the clause though since
having loopback is undesirable for everyone except the test (right)?
so we need to make this behavior configurable.
This CL takes a stab at a complete solution where we pass a boolean
all the way through the Java PeerConnectionFactory down to the
BasicNetworkManager. This comes as a heavy price in interface
changes though. It's pretty out of proportion, but fundamentally we
need some way of telling the network manager that it is on Android
and in test mode. Passing the boolean all the way through is one way.
Another way might be to put the loopback filter behind an ifdef and
link a custom libjingle_peerconnection.so with the test. That is hacky
but doesn't pollute the interfaces. Not sure how to solve that in GYP
but it could mean some duplication between the production and
test .so files.
It would have been perfect to use flags here, but then we need to
hook up gflags parsing to some main() somewhere to make sure the
flag gets parsed, and make sure to pass that flag in our tests.
I'm not sure how that can be done.
Making the loopback filtering conditional is exactly how we solved the
equivalent problem in content_browsertests in Chrome, and it worked
great.
That's all I could think of.
BUG=4181
R=perkj@webrtc.org, pthatcher@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/36769004
git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@8344 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/base/network.cc b/base/network.cc
index 5152a6b..07180d5 100644
--- a/base/network.cc
+++ b/base/network.cc
@@ -120,8 +120,10 @@
return "Cellular";
case ADAPTER_TYPE_VPN:
return "VPN";
+ case ADAPTER_TYPE_LOOPBACK:
+ return "Loopback";
default:
- ASSERT(false);
+ DCHECK(false) << "Invalid type " << type;
return std::string();
}
}
@@ -268,6 +270,7 @@
BasicNetworkManager::BasicNetworkManager()
: thread_(NULL), sent_first_update_(false), start_count_(0),
+ network_ignore_mask_(kDefaultNetworkIgnoreMask),
ignore_non_default_routes_(false) {
}
@@ -331,15 +334,19 @@
prefix, prefix_length);
auto existing_network = current_networks.find(key);
if (existing_network == current_networks.end()) {
+ AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
+ if (cursor->ifa_flags & IFF_LOOPBACK) {
+ // TODO(phoglund): Need to recognize other types as well.
+ adapter_type = ADAPTER_TYPE_LOOPBACK;
+ }
scoped_ptr<Network> network(new Network(cursor->ifa_name,
cursor->ifa_name,
prefix,
- prefix_length));
+ prefix_length,
+ adapter_type));
network->set_scope_id(scope_id);
network->AddIP(ip);
- bool ignored = ((cursor->ifa_flags & IFF_LOOPBACK) ||
- IsIgnoredNetwork(*network));
- network->set_ignored(ignored);
+ network->set_ignored(IsIgnoredNetwork(*network));
if (include_ignored || !network->ignored()) {
networks->push_back(network.release());
}
@@ -477,15 +484,20 @@
std::string key = MakeNetworkKey(name, prefix, prefix_length);
auto existing_network = current_networks.find(key);
if (existing_network == current_networks.end()) {
+ AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
+ if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) {
+ // TODO(phoglund): Need to recognize other types as well.
+ adapter_type = ADAPTER_TYPE_LOOPBACK;
+ }
scoped_ptr<Network> network(new Network(name,
description,
prefix,
- prefix_length));
+ prefix_length,
+ adapter_type));
network->set_scope_id(scope_id);
network->AddIP(ip);
- bool ignore = ((adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) ||
- IsIgnoredNetwork(*network));
- network->set_ignored(ignore);
+ bool ignored = IsIgnoredNetwork(*network);
+ network->set_ignored(ignored);
if (include_ignored || !network->ignored()) {
networks->push_back(network.release());
}
@@ -537,6 +549,10 @@
return true;
}
}
+
+ if (network_ignore_mask_ & network.type()) {
+ return true;
+ }
#if defined(WEBRTC_POSIX)
// Filter out VMware/VirtualBox interfaces, typically named vmnet1,
// vmnet8, or vboxnet0.
diff --git a/base/network.h b/base/network.h
index 7238aa0..cd01300 100644
--- a/base/network.h
+++ b/base/network.h
@@ -33,12 +33,16 @@
enum AdapterType {
// This enum resembles the one in Chromium net::ConnectionType.
ADAPTER_TYPE_UNKNOWN = 0,
- ADAPTER_TYPE_ETHERNET = 1,
- ADAPTER_TYPE_WIFI = 2,
- ADAPTER_TYPE_CELLULAR = 3,
- ADAPTER_TYPE_VPN = 4
+ ADAPTER_TYPE_ETHERNET = 1 << 0,
+ ADAPTER_TYPE_WIFI = 1 << 1,
+ ADAPTER_TYPE_CELLULAR = 1 << 2,
+ ADAPTER_TYPE_VPN = 1 << 3,
+ ADAPTER_TYPE_LOOPBACK = 1 << 4
};
+// By default, ignore loopback interfaces on the host.
+const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
+
// Makes a string key for this network. Used in the network manager's maps.
// Network objects are keyed on interface name, network prefix and the
// length of that prefix.
@@ -61,9 +65,9 @@
sigslot::signal0<> SignalError;
// Start/Stop monitoring of network interfaces
- // list. SignalNetworksChanged or SignalError is emitted immidiately
+ // list. SignalNetworksChanged or SignalError is emitted immediately
// after StartUpdating() is called. After that SignalNetworksChanged
- // is emitted wheneven list of networks changes.
+ // is emitted whenever list of networks changes.
virtual void StartUpdating() = 0;
virtual void StopUpdating() = 0;
@@ -143,11 +147,24 @@
virtual void OnMessage(Message* msg);
bool started() { return start_count_ > 0; }
- // Sets the network ignore list, which is empty by default. Any network on
- // the ignore list will be filtered from network enumeration results.
+ // Sets the network ignore list, which is empty by default. Any network on the
+ // ignore list will be filtered from network enumeration results.
void set_network_ignore_list(const std::vector<std::string>& list) {
network_ignore_list_ = list;
}
+
+ // Sets the network types to ignore. For instance, calling this with
+ // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
+ // loopback interfaces. Set to kDefaultNetworkIgnoreMask by default.
+ void set_network_ignore_mask(int network_ignore_mask) {
+ // TODO(phoglund): implement support for other types than loopback.
+ // See https://code.google.com/p/webrtc/issues/detail?id=4288.
+ // Then remove set_network_ignore_list.
+ network_ignore_mask_ = network_ignore_mask;
+ }
+
+ int network_ignore_mask() const { return network_ignore_mask_; }
+
#if defined(WEBRTC_LINUX)
// Sets the flag for ignoring non-default routes.
void set_ignore_non_default_routes(bool value) {
@@ -178,6 +195,7 @@
bool sent_first_update_;
int start_count_;
std::vector<std::string> network_ignore_list_;
+ int network_ignore_mask_;
bool ignore_non_default_routes_;
};
diff --git a/base/network_unittest.cc b/base/network_unittest.cc
index 36edcc0..662dc70 100644
--- a/base/network_unittest.cc
+++ b/base/network_unittest.cc
@@ -81,16 +81,51 @@
}
// Tests that our ignore function works properly.
-TEST_F(NetworkTest, TestNetworkIgnore) {
+TEST_F(NetworkTest, TestIsIgnoredNetworkIgnoresOnlyLoopbackByDefault) {
Network ipv4_network1("test_eth0", "Test Network Adapter 1",
- IPAddress(0x12345600U), 24);
+ IPAddress(0x12345600U), 24, ADAPTER_TYPE_ETHERNET);
+ Network ipv4_network2("test_wlan0", "Test Network Adapter 2",
+ IPAddress(0x12345601U), 16, ADAPTER_TYPE_WIFI);
+ Network ipv4_network3("test_cell0", "Test Network Adapter 3",
+ IPAddress(0x12345602U), 16, ADAPTER_TYPE_CELLULAR);
+ Network ipv4_network4("test_vpn0", "Test Network Adapter 4",
+ IPAddress(0x12345603U), 16, ADAPTER_TYPE_VPN);
+ Network ipv4_network5("test_lo", "Test Network Adapter 5",
+ IPAddress(0x12345604U), 16, ADAPTER_TYPE_LOOPBACK);
+ BasicNetworkManager network_manager;
+ EXPECT_FALSE(IsIgnoredNetwork(network_manager, ipv4_network1));
+ EXPECT_FALSE(IsIgnoredNetwork(network_manager, ipv4_network2));
+ EXPECT_FALSE(IsIgnoredNetwork(network_manager, ipv4_network3));
+ EXPECT_FALSE(IsIgnoredNetwork(network_manager, ipv4_network4));
+ EXPECT_TRUE(IsIgnoredNetwork(network_manager, ipv4_network5));
+}
+
+TEST_F(NetworkTest, TestIsIgnoredNetworkIgnoresIPsStartingWith0) {
+ Network ipv4_network1("test_eth0", "Test Network Adapter 1",
+ IPAddress(0x12345600U), 24, ADAPTER_TYPE_ETHERNET);
Network ipv4_network2("test_eth1", "Test Network Adapter 2",
- IPAddress(0x00010000U), 16);
+ IPAddress(0x010000U), 24, ADAPTER_TYPE_ETHERNET);
BasicNetworkManager network_manager;
EXPECT_FALSE(IsIgnoredNetwork(network_manager, ipv4_network1));
EXPECT_TRUE(IsIgnoredNetwork(network_manager, ipv4_network2));
}
+TEST_F(NetworkTest, TestIsIgnoredNetworkIgnoresNetworksAccordingToIgnoreMask) {
+ Network ipv4_network1("test_eth0", "Test Network Adapter 1",
+ IPAddress(0x12345600U), 24, ADAPTER_TYPE_ETHERNET);
+ Network ipv4_network2("test_wlan0", "Test Network Adapter 2",
+ IPAddress(0x12345601U), 16, ADAPTER_TYPE_WIFI);
+ Network ipv4_network3("test_cell0", "Test Network Adapter 3",
+ IPAddress(0x12345602U), 16, ADAPTER_TYPE_CELLULAR);
+ BasicNetworkManager network_manager;
+ network_manager.set_network_ignore_mask(
+ ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK | ADAPTER_TYPE_WIFI);
+ EXPECT_TRUE(IsIgnoredNetwork(network_manager, ipv4_network1));
+ EXPECT_TRUE(IsIgnoredNetwork(network_manager, ipv4_network2));
+ EXPECT_FALSE(IsIgnoredNetwork(network_manager, ipv4_network3));
+}
+
+// TODO(phoglund): Remove when ignore list goes away.
TEST_F(NetworkTest, TestIgnoreList) {
Network ignore_me("ignore_me", "Ignore me please!",
IPAddress(0x12345600U), 24);