Allow unset default gateway

Default gateway addresses cannot be unset in current implementation as
it requires a valid IP address and writes it to systemd.network files
every time. This patch supports setting empty string on DefaultGateway
and DefaultGateway6 DBus properties to unset the value as empty strings
will not be written to config file.

Tested:
Disable DHCP and set default gateway for both IPv4 and IPv6, then set
empty string on DefaultGateway and/or DefaultGateway6, verified the
corresponding [Route] section is removed in config file.

Change-Id: I3be81f9c7c605b12cb225beeefec02f9841b0dcd
Signed-off-by: Jiaqing Zhao <jiaqing.zhao@intel.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 864e252..52f68d1 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -1234,7 +1234,7 @@
         return gw;
     }
 
-    if (!isValidIP(AF_INET, gateway))
+    if (!isValidIP(AF_INET, gateway) && !gateway.empty())
     {
         log<level::ERR>("Not a valid v4 Gateway",
                         entry("GATEWAY=%s", gateway.c_str()));
@@ -1257,7 +1257,7 @@
         return gw;
     }
 
-    if (!isValidIP(AF_INET6, gateway))
+    if (!isValidIP(AF_INET6, gateway) && !gateway.empty())
     {
         log<level::ERR>("Not a valid v6 Gateway",
                         entry("GATEWAY=%s", gateway.c_str()));
diff --git a/src/routing_table.cpp b/src/routing_table.cpp
index 70131e8..0206743 100644
--- a/src/routing_table.cpp
+++ b/src/routing_table.cpp
@@ -25,6 +25,8 @@
 
 void Table::refresh()
 {
+    defaultGateway.clear();
+    defaultGateway6.clear();
     try
     {
         rtmsg msg{};
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
index a7e7302..913b6e1 100644
--- a/test/test_ethernet_interface.cpp
+++ b/test/test_ethernet_interface.cpp
@@ -217,6 +217,8 @@
     std::string gateway = "10.3.3.3";
     interface.defaultGateway(gateway);
     EXPECT_EQ(interface.defaultGateway(), gateway);
+    interface.defaultGateway("");
+    EXPECT_EQ(interface.defaultGateway(), "");
 }
 
 TEST_F(TestEthernetInterface, addGateway6)
@@ -224,6 +226,8 @@
     std::string gateway6 = "ffff:ffff:ffff:fe80::1";
     interface.defaultGateway6(gateway6);
     EXPECT_EQ(interface.defaultGateway6(), gateway6);
+    interface.defaultGateway6("");
+    EXPECT_EQ(interface.defaultGateway6(), "");
 }
 
 } // namespace network