shill: Provide notification callbacks on DHCP configuration updates.
BUG=chromium-os:16257
TEST=unit tests
Change-Id: Ifef3eb4e295bea21a05bce4af440536472953a8b
Reviewed-on: http://gerrit.chromium.org/gerrit/2273
Tested-by: Darin Petkov <petkov@chromium.org>
Reviewed-by: Chris Masone <cmasone@chromium.org>
diff --git a/ipconfig.cc b/ipconfig.cc
index b386c46..12c20f0 100644
--- a/ipconfig.cc
+++ b/ipconfig.cc
@@ -34,7 +34,14 @@
void IPConfig::UpdateProperties(const Properties &properties) {
properties_ = properties;
- // TODO(petkov): Notify listeners about the new properties.
+ if (update_callback_.get()) {
+ update_callback_->Run(this);
+ }
+}
+
+void IPConfig::RegisterUpdateCallback(
+ Callback1<IPConfigRefPtr>::Type *callback) {
+ update_callback_.reset(callback);
}
} // namespace shill
diff --git a/ipconfig.h b/ipconfig.h
index 78ace70..5e2ae32 100644
--- a/ipconfig.h
+++ b/ipconfig.h
@@ -8,12 +8,19 @@
#include <string>
#include <vector>
+#include <base/callback_old.h>
#include <base/memory/ref_counted.h>
+#include <base/memory/scoped_ptr.h>
+#include <gtest/gtest_prod.h> // for FRIEND_TEST
#include "shill/device.h"
namespace shill {
+class IPConfig;
+
+typedef scoped_refptr<IPConfig> IPConfigRefPtr;
+
// IPConfig superclass. Individual IP configuration types will inherit from this
// class.
class IPConfig : public base::RefCounted<IPConfig> {
@@ -37,9 +44,11 @@
DeviceConstRefPtr device() const { return device_; }
const std::string &GetDeviceName() const;
- // Updates the IP configuration properties and notifies registered listeners
- // about the event.
- void UpdateProperties(const Properties &properties);
+ // Registers a callback that's executed every time the configuration
+ // properties change. Takes ownership of |callback|. Pass NULL to remove a
+ // callback. The callback's argument is a pointer to this IP configuration
+ // instance allowing clients to more easily manage multiple IP configurations.
+ void RegisterUpdateCallback(Callback1<IPConfigRefPtr>::Type *callback);
const Properties &properties() const { return properties_; }
@@ -49,9 +58,18 @@
virtual bool Request();
virtual bool Renew();
+ protected:
+ // Updates the IP configuration properties and notifies registered listeners
+ // about the event.
+ void UpdateProperties(const Properties &properties);
+
private:
+ FRIEND_TEST(IPConfigTest, UpdateCallback);
+ FRIEND_TEST(IPConfigTest, UpdateProperties);
+
DeviceConstRefPtr device_;
Properties properties_;
+ scoped_ptr<Callback1<IPConfigRefPtr>::Type> update_callback_;
DISALLOW_COPY_AND_ASSIGN(IPConfig);
};
diff --git a/ipconfig_unittest.cc b/ipconfig_unittest.cc
index e6854b1..d5e9d35 100644
--- a/ipconfig_unittest.cc
+++ b/ipconfig_unittest.cc
@@ -2,26 +2,92 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <base/callback_old.h>
+
#include "shill/ipconfig.h"
#include "shill/mock_control.h"
#include "shill/mock_device.h"
+using testing::Test;
+
namespace shill {
-using ::testing::Test;
class IPConfigTest : public Test {
public:
- IPConfigTest() :
- device_(new MockDevice(&control_interface_, NULL, NULL, "testname", 0)) {}
+ IPConfigTest()
+ : device_(new MockDevice(&control_interface_, NULL, NULL, "testname", 0)),
+ ipconfig_(new IPConfig(device_)) {}
protected:
MockControl control_interface_;
scoped_refptr<const MockDevice> device_;
+ IPConfigRefPtr ipconfig_;
};
-TEST_F(IPConfigTest, GetDeviceNameTest) {
- scoped_refptr<IPConfig> ipconfig(new IPConfig(device_));
- EXPECT_EQ("testname", ipconfig->GetDeviceName());
+TEST_F(IPConfigTest, GetDeviceName) {
+ EXPECT_EQ("testname", ipconfig_->GetDeviceName());
+}
+
+TEST_F(IPConfigTest, Request) {
+ EXPECT_FALSE(ipconfig_->Request());
+}
+
+TEST_F(IPConfigTest, Renew) {
+ EXPECT_FALSE(ipconfig_->Renew());
+}
+
+TEST_F(IPConfigTest, UpdateProperties) {
+ IPConfig::Properties properties;
+ properties.address = "1.2.3.4";
+ properties.subnet_cidr = 24;
+ properties.broadcast_address = "11.22.33.44";
+ properties.gateway = "5.6.7.8";
+ properties.dns_servers.push_back("10.20.30.40");
+ properties.dns_servers.push_back("20.30.40.50");
+ properties.domain_name = "foo.org";
+ properties.domain_search.push_back("zoo.org");
+ properties.domain_search.push_back("zoo.com");
+ properties.mtu = 700;
+ ipconfig_->UpdateProperties(properties);
+ EXPECT_EQ("1.2.3.4", ipconfig_->properties().address);
+ EXPECT_EQ(24, ipconfig_->properties().subnet_cidr);
+ EXPECT_EQ("11.22.33.44", ipconfig_->properties().broadcast_address);
+ EXPECT_EQ("5.6.7.8", ipconfig_->properties().gateway);
+ ASSERT_EQ(2, ipconfig_->properties().dns_servers.size());
+ EXPECT_EQ("10.20.30.40", ipconfig_->properties().dns_servers[0]);
+ EXPECT_EQ("20.30.40.50", ipconfig_->properties().dns_servers[1]);
+ ASSERT_EQ(2, ipconfig_->properties().domain_search.size());
+ EXPECT_EQ("zoo.org", ipconfig_->properties().domain_search[0]);
+ EXPECT_EQ("zoo.com", ipconfig_->properties().domain_search[1]);
+ EXPECT_EQ("foo.org", ipconfig_->properties().domain_name);
+ EXPECT_EQ(700, ipconfig_->properties().mtu);
+}
+
+class UpdateCallbackTest {
+ public:
+ UpdateCallbackTest(IPConfigRefPtr ipconfig)
+ : ipconfig_(ipconfig),
+ called_(false) {}
+
+ void Callback(IPConfigRefPtr ipconfig) {
+ called_ = true;
+ EXPECT_EQ(ipconfig.get(), ipconfig_.get());
+ }
+
+ bool called() const { return called_; }
+
+ private:
+ IPConfigRefPtr ipconfig_;
+ bool called_;
+};
+
+TEST_F(IPConfigTest, UpdateCallback) {
+ UpdateCallbackTest callback_test(ipconfig_);
+ ASSERT_FALSE(callback_test.called());
+ ipconfig_->RegisterUpdateCallback(
+ NewCallback(&callback_test, &UpdateCallbackTest::Callback));
+ ipconfig_->UpdateProperties(IPConfig::Properties());
+ EXPECT_TRUE(callback_test.called());
}
} // namespace shill