shill: Start a mockable GLib interface.

Use the interface to test DHCPConfig::Start.

BUG=chromium-os:16319,chromium-os:16013
TEST=unit test, tested on device

Change-Id: Ice6c65b0cc2ac2dd738cb6aacb426703cce48a3f
Reviewed-on: http://gerrit.chromium.org/gerrit/2356
Tested-by: Darin Petkov <petkov@chromium.org>
Reviewed-by: Chris Masone <cmasone@chromium.org>
diff --git a/dhcp_config_unittest.cc b/dhcp_config_unittest.cc
index 117f23c..1c2b498 100644
--- a/dhcp_config_unittest.cc
+++ b/dhcp_config_unittest.cc
@@ -3,30 +3,37 @@
 // found in the LICENSE file.
 
 #include "shill/dhcp_config.h"
+#include "shill/dhcp_provider.h"
 #include "shill/mock_control.h"
 #include "shill/mock_device.h"
+#include "shill/mock_glib.h"
 
 using std::string;
 using std::vector;
+using testing::_;
+using testing::Return;
+using testing::SetArgumentPointee;
 using testing::Test;
 
 namespace shill {
 
 class DHCPConfigTest : public Test {
  public:
-  DHCPConfigTest() :
-      device_(new MockDevice(&control_interface_, NULL, NULL, "testname", 0)) {}
+  DHCPConfigTest()
+      : device_(new MockDevice(&control_interface_, NULL, NULL, "testname", 0)),
+        config_(new DHCPConfig(DHCPProvider::GetInstance(), device_, &glib_)) {}
 
  protected:
+  MockGLib glib_;
   MockControl control_interface_;
   scoped_refptr<MockDevice> device_;
+  DHCPConfigRefPtr config_;
 };
 
 TEST_F(DHCPConfigTest, GetIPv4AddressString) {
-  DHCPConfigRefPtr config(new DHCPConfig(NULL, device_));
-  EXPECT_EQ("255.255.255.255", config->GetIPv4AddressString(0xffffffff));
-  EXPECT_EQ("0.0.0.0", config->GetIPv4AddressString(0));
-  EXPECT_EQ("1.2.3.4", config->GetIPv4AddressString(0x04030201));
+  EXPECT_EQ("255.255.255.255", config_->GetIPv4AddressString(0xffffffff));
+  EXPECT_EQ("0.0.0.0", config_->GetIPv4AddressString(0));
+  EXPECT_EQ("1.2.3.4", config_->GetIPv4AddressString(0x04030201));
 }
 
 TEST_F(DHCPConfigTest, ParseConfiguration) {
@@ -38,40 +45,35 @@
   conf[DHCPConfig::kConfigurationKeyBroadcastAddress].writer().append_uint32(
       0x10203040);
   {
-    DBus::Variant var;
     vector<unsigned int> routers;
     routers.push_back(0x02040608);
     routers.push_back(0x03050709);
-    DBus::MessageIter writer = var.writer();
+    DBus::MessageIter writer =
+        conf[DHCPConfig::kConfigurationKeyRouters].writer();
     writer << routers;
-    conf[DHCPConfig::kConfigurationKeyRouters] = var;
   }
   {
-    DBus::Variant var;
     vector<unsigned int> dns;
     dns.push_back(0x09070503);
     dns.push_back(0x08060402);
-    DBus::MessageIter writer = var.writer();
+    DBus::MessageIter writer = conf[DHCPConfig::kConfigurationKeyDNS].writer();
     writer << dns;
-    conf[DHCPConfig::kConfigurationKeyDNS] = var;
   }
   conf[DHCPConfig::kConfigurationKeyDomainName].writer().append_string(
       "domain-name");
   {
-    DBus::Variant var;
     vector<string> search;
     search.push_back("foo.com");
     search.push_back("bar.com");
-    DBus::MessageIter writer = var.writer();
+    DBus::MessageIter writer =
+        conf[DHCPConfig::kConfigurationKeyDomainSearch].writer();
     writer << search;
-    conf[DHCPConfig::kConfigurationKeyDomainSearch] = var;
   }
   conf[DHCPConfig::kConfigurationKeyMTU].writer().append_uint16(600);
   conf["UnknownKey"] = DBus::Variant();
 
-  DHCPConfigRefPtr config(new DHCPConfig(NULL, device_));
   IPConfig::Properties properties;
-  ASSERT_TRUE(config->ParseConfiguration(conf, &properties));
+  ASSERT_TRUE(config_->ParseConfiguration(conf, &properties));
   EXPECT_EQ("4.3.2.1", properties.address);
   EXPECT_EQ(16, properties.subnet_cidr);
   EXPECT_EQ("64.48.32.16", properties.broadcast_address);
@@ -86,4 +88,20 @@
   EXPECT_EQ(600, properties.mtu);
 }
 
+TEST_F(DHCPConfigTest, Start) {
+  EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
+      .WillOnce(Return(false));
+  EXPECT_FALSE(config_->Start());
+  EXPECT_EQ(0, config_->pid_);
+
+  const unsigned int kPID = 1234;
+  EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _))
+      .WillOnce(DoAll(SetArgumentPointee<6>(kPID), Return(true)));
+  EXPECT_TRUE(config_->Start());
+  EXPECT_EQ(kPID, config_->pid_);
+  DHCPProvider *provider = DHCPProvider::GetInstance();
+  ASSERT_TRUE(provider->configs_.find(kPID) != provider->configs_.end());
+  EXPECT_EQ(config_.get(), provider->configs_.find(1234)->second.get());
+}
+
 }  // namespace shill