GetDefaultLocalAddress should return false when the address is invalid

BUG=
R=pthatcher@webrtc.org

Review URL: https://codereview.webrtc.org/1471203002 .

Cr-Commit-Position: refs/heads/master@{#10779}
diff --git a/webrtc/base/network.cc b/webrtc/base/network.cc
index f35dcba..678541d 100644
--- a/webrtc/base/network.cc
+++ b/webrtc/base/network.cc
@@ -345,10 +345,10 @@
 
 bool NetworkManagerBase::GetDefaultLocalAddress(int family,
                                                 IPAddress* ipaddr) const {
-  if (family == AF_INET) {
+  if (family == AF_INET && !default_local_ipv4_address_.IsNil()) {
     *ipaddr = default_local_ipv4_address_;
     return true;
-  } else if (family == AF_INET6) {
+  } else if (family == AF_INET6 && !default_local_ipv6_address_.IsNil()) {
     *ipaddr = default_local_ipv6_address_;
     return true;
   }
diff --git a/webrtc/base/network_unittest.cc b/webrtc/base/network_unittest.cc
index 5324e86..cfba62b 100644
--- a/webrtc/base/network_unittest.cc
+++ b/webrtc/base/network_unittest.cc
@@ -100,6 +100,7 @@
 class TestBasicNetworkManager : public BasicNetworkManager {
  public:
   using BasicNetworkManager::QueryDefaultLocalAddress;
+  using BasicNetworkManager::set_default_local_addresses;
 };
 
 // Test that the Network ctor works properly.
@@ -848,9 +849,17 @@
   NetworkMonitorFactory::ReleaseFactory(factory);
 }
 
-TEST_F(NetworkTest, DefaultPrivateAddress) {
+TEST_F(NetworkTest, DefaultLocalAddress) {
   TestBasicNetworkManager manager;
   manager.StartUpdating();
+  IPAddress ip;
+
+  // GetDefaultLocalAddress should return false when not set.
+  EXPECT_FALSE(manager.GetDefaultLocalAddress(AF_INET, &ip));
+  EXPECT_FALSE(manager.GetDefaultLocalAddress(AF_INET6, &ip));
+
+  // Make sure we can query default local address when an address for such
+  // address family exists.
   std::vector<Network*> networks;
   manager.GetNetworks(&networks);
   for (auto& network : networks) {
@@ -860,6 +869,14 @@
       EXPECT_TRUE(manager.QueryDefaultLocalAddress(AF_INET6) != IPAddress());
     }
   }
+
+  // GetDefaultLocalAddress should return the valid default address after set.
+  manager.set_default_local_addresses(GetLoopbackIP(AF_INET),
+                                      GetLoopbackIP(AF_INET6));
+  EXPECT_TRUE(manager.GetDefaultLocalAddress(AF_INET, &ip));
+  EXPECT_EQ(ip, GetLoopbackIP(AF_INET));
+  EXPECT_TRUE(manager.GetDefaultLocalAddress(AF_INET6, &ip));
+  EXPECT_EQ(ip, GetLoopbackIP(AF_INET6));
   manager.StopUpdating();
 }
 
diff --git a/webrtc/p2p/base/stunport.cc b/webrtc/p2p/base/stunport.cc
index 13ecca4..0ae3f60 100644
--- a/webrtc/p2p/base/stunport.cc
+++ b/webrtc/p2p/base/stunport.cc
@@ -416,10 +416,12 @@
   bool result =
       Network()->default_local_address_provider()->GetDefaultLocalAddress(
           addr->family(), &default_address);
-  if (!result || default_address.IsNil()) {
+  if (!result) {
     return false;
   }
 
+  RTC_DCHECK(!default_address.IsNil())
+
   addr->SetIP(default_address);
   return true;
 }