summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Qiu <zqiu@google.com>2015-11-16 12:09:16 -0800
committerPeter Qiu <zqiu@google.com>2015-11-19 13:43:27 -0800
commitf933540bb968efa2744ee48b40ab713ccd358d51 (patch)
tree6ca9bc31232ad2683fd90719783f37ab9b54bf5e
parent0d70fa725563571fff9e91e7a86386d3d1db4667 (diff)
downloadapmanager-f933540bb968efa2744ee48b40ab713ccd358d51.tar.gz
Refactor D-Bus adaptor for Device out of the Device class
This removes the RPC specific dependencies out of the Device class. Device adaptors will now be created through the ControlInterface. While there, make the D-Bus object registration for Device object to be synchronous instead of asynchronous. Since the daemon will not be doing anything anyway besides waiting for the registration to complete. This avoids unnecessary complexity with the object registration. Currently for D-Bus, the property variables are being created/stored in the generated adaptor code. This means that the property variables will be stored in the adaptor instead of the Device class itself. Even though this is not ideal, there is no good way around it. In the ideal world, the Device would maintain its property variables, and register them with the RPC specific adaptor. So for the unittest, we will use FakeDeviceAdaptor, which provides the storage for the property variables. Also currently gmock doesn't support mocking of a function that returns a unique_ptr, since it only supports copyable return value and unique_ptr is not copyable. To work around this issue, I've created mock functions that return a raw pointer, and override the proxy/adaptor creation functions to use the mock function in MockControl. Bug: 24194427 TEST=Start AP service on both Brillo and Chrome OS TEST=Run unittests on both Brillo and Chrome OS Change-Id: I8e9f736bb27fe6736f616dd752a37b9cc1be8dfe
-rw-r--r--Android.mk3
-rw-r--r--apmanager.gyp3
-rw-r--r--config_unittest.cc17
-rw-r--r--control_interface.h7
-rw-r--r--dbus/dbus_control.cc10
-rw-r--r--dbus/dbus_control.h2
-rw-r--r--dbus/device_dbus_adaptor.cc71
-rw-r--r--dbus/device_dbus_adaptor.h56
-rw-r--r--device.cc58
-rw-r--r--device.h36
-rw-r--r--device_adaptor_interface.h38
-rw-r--r--device_info.cc10
-rw-r--r--device_info.h5
-rw-r--r--device_info_unittest.cc18
-rw-r--r--device_unittest.cc10
-rw-r--r--fake_device_adaptor.cc50
-rw-r--r--fake_device_adaptor.h50
-rw-r--r--manager.cc30
-rw-r--r--manager.h7
-rw-r--r--manager_unittest.cc17
-rw-r--r--mock_control.cc42
-rw-r--r--mock_control.h60
-rw-r--r--mock_device.cc2
-rw-r--r--mock_device.h2
-rw-r--r--mock_manager.cc3
-rw-r--r--mock_manager.h2
-rw-r--r--service_unittest.cc3
27 files changed, 519 insertions, 93 deletions
diff --git a/Android.mk b/Android.mk
index 1d9eebd..584b4cb 100644
--- a/Android.mk
+++ b/Android.mk
@@ -68,6 +68,7 @@ LOCAL_SRC_FILES := \
config.cc \
daemon.cc \
dbus/dbus_control.cc \
+ dbus/device_dbus_adaptor.cc \
dbus/firewalld_dbus_proxy.cc \
dbus/shill_dbus_proxy.cc \
device.cc \
@@ -107,9 +108,11 @@ LOCAL_SRC_FILES := \
device_info_unittest.cc \
device_unittest.cc \
dhcp_server_unittest.cc \
+ fake_device_adaptor.cc \
hostapd_monitor_unittest.cc \
manager_unittest.cc \
mock_config.cc \
+ mock_control.cc \
mock_device.cc \
mock_dhcp_server.cc \
mock_dhcp_server_factory.cc \
diff --git a/apmanager.gyp b/apmanager.gyp
index f3c18a8..290d139 100644
--- a/apmanager.gyp
+++ b/apmanager.gyp
@@ -76,6 +76,7 @@
'config.cc',
'daemon.cc',
'dbus/dbus_control.cc',
+ 'dbus/device_dbus_adaptor.cc',
'dbus/permission_broker_dbus_proxy.cc',
'dbus/shill_dbus_proxy.cc',
'device.cc',
@@ -143,9 +144,11 @@
'device_info_unittest.cc',
'device_unittest.cc',
'dhcp_server_unittest.cc',
+ 'fake_device_adaptor.cc',
'hostapd_monitor_unittest.cc',
'manager_unittest.cc',
'mock_config.cc',
+ 'mock_control.cc',
'mock_device.cc',
'mock_dhcp_server.cc',
'mock_dhcp_server_factory.cc',
diff --git a/config_unittest.cc b/config_unittest.cc
index b8b58ca..93db179 100644
--- a/config_unittest.cc
+++ b/config_unittest.cc
@@ -29,12 +29,15 @@
#include "dbus/apmanager/dbus-constants.h"
#endif
+#include "apmanager/fake_device_adaptor.h"
+#include "apmanager/mock_control.h"
#include "apmanager/mock_device.h"
#include "apmanager/mock_manager.h"
using ::testing::_;
using ::testing::Mock;
using ::testing::Return;
+using ::testing::ReturnNew;
using ::testing::SetArgumentPointee;
namespace apmanager {
@@ -123,20 +126,26 @@ const char kExpectedRsnConfigContent[] = "ssid=TestSsid\n"
class ConfigTest : public testing::Test {
public:
- ConfigTest() : config_(&manager_, kServicePath) {}
+ ConfigTest()
+ : manager_(&control_interface_),
+ config_(&manager_, kServicePath) {
+ ON_CALL(control_interface_, CreateDeviceAdaptorRaw())
+ .WillByDefault(ReturnNew<FakeDeviceAdaptor>());
+ }
void SetupDevice(const std::string& interface) {
// Setup mock device.
- device_ = new MockDevice();
+ device_ = new MockDevice(&manager_);
device_->SetPreferredApInterface(interface);
EXPECT_CALL(manager_, GetDeviceFromInterfaceName(interface))
.WillRepeatedly(Return(device_));
}
protected:
- Config config_;
+ MockControl control_interface_;
MockManager manager_;
scoped_refptr<MockDevice> device_;
+ Config config_;
};
MATCHER_P(IsConfigErrorStartingWith, message, "") {
@@ -249,7 +258,7 @@ TEST_F(ConfigTest, NoInterface) {
Mock::VerifyAndClearExpectations(&manager_);
// Device available, config file should be generated without any problem.
- scoped_refptr<MockDevice> device = new MockDevice();
+ scoped_refptr<MockDevice> device = new MockDevice(&manager_);
device->SetPreferredApInterface(kInterface);
brillo::ErrorPtr error1;
EXPECT_CALL(manager_, GetAvailableDevice()).WillOnce(Return(device));
diff --git a/control_interface.h b/control_interface.h
index 4661eca..58ef1e1 100644
--- a/control_interface.h
+++ b/control_interface.h
@@ -20,11 +20,14 @@
#include <base/callback.h>
#include <base/macros.h>
+#include "apmanager/device_adaptor_interface.h"
#include "apmanager/firewall_proxy_interface.h"
#include "apmanager/shill_proxy_interface.h"
namespace apmanager {
+class Device;
+
// This is the Interface for an object factory that creates adaptor/proxy
// objects
class ControlInterface {
@@ -34,6 +37,10 @@ class ControlInterface {
virtual void Init() = 0;
virtual void Shutdown() = 0;
+ // Adaptor creation APIs.
+ virtual std::unique_ptr<DeviceAdaptorInterface> CreateDeviceAdaptor(
+ Device* device) = 0;
+
// Proxy creation APIs.
virtual std::unique_ptr<FirewallProxyInterface> CreateFirewallProxy(
const base::Closure& service_appeared_callback,
diff --git a/dbus/dbus_control.cc b/dbus/dbus_control.cc
index ed96661..4619af3 100644
--- a/dbus/dbus_control.cc
+++ b/dbus/dbus_control.cc
@@ -16,6 +16,7 @@
#include "apmanager/dbus/dbus_control.h"
+#include "apmanager/dbus/device_dbus_adaptor.h"
#include "apmanager/dbus/shill_dbus_proxy.h"
#include "apmanager/manager.h"
@@ -54,9 +55,8 @@ void DBusControl::Init() {
sequencer->GetHandler("ObjectManager.RegisterAsync() failed.", true));
// Create and register Manager.
- manager_.reset(new Manager());
+ manager_.reset(new Manager(this));
manager_->RegisterAsync(
- this,
object_manager_.get(),
bus_,
sequencer->GetHandler("Manager.RegisterAsync() failed.", true));
@@ -87,6 +87,12 @@ void DBusControl::OnObjectRegistrationCompleted(bool registration_success) {
manager_->Start();
}
+std::unique_ptr<DeviceAdaptorInterface> DBusControl::CreateDeviceAdaptor(
+ Device* device) {
+ return std::unique_ptr<DeviceAdaptorInterface>(
+ new DeviceDBusAdaptor(bus_, object_manager_.get(), device));
+}
+
std::unique_ptr<FirewallProxyInterface> DBusControl::CreateFirewallProxy(
const base::Closure& service_appeared_callback,
const base::Closure& service_vanished_callback) {
diff --git a/dbus/dbus_control.h b/dbus/dbus_control.h
index 0efdc08..4289afd 100644
--- a/dbus/dbus_control.h
+++ b/dbus/dbus_control.h
@@ -35,6 +35,8 @@ class DBusControl : public ControlInterface {
// Inheritted from ControlInterface.
void Init() override;
void Shutdown() override;
+ std::unique_ptr<DeviceAdaptorInterface> CreateDeviceAdaptor(
+ Device* device) override;
std::unique_ptr<FirewallProxyInterface> CreateFirewallProxy(
const base::Closure& service_appeared_callback,
const base::Closure& service_vanished_callback) override;
diff --git a/dbus/device_dbus_adaptor.cc b/dbus/device_dbus_adaptor.cc
new file mode 100644
index 0000000..9091641
--- /dev/null
+++ b/dbus/device_dbus_adaptor.cc
@@ -0,0 +1,71 @@
+//
+// Copyright (C) 2014 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "apmanager/dbus/device_dbus_adaptor.h"
+
+#include <base/strings/stringprintf.h>
+#include <dbus_bindings/org.chromium.apmanager.Manager.h>
+
+#include "apmanager/device.h"
+
+using brillo::dbus_utils::ExportedObjectManager;
+using brillo::dbus_utils::DBusObject;
+using org::chromium::apmanager::ManagerAdaptor;
+using std::string;
+
+namespace apmanager {
+
+DeviceDBusAdaptor::DeviceDBusAdaptor(
+ const scoped_refptr<dbus::Bus>& bus,
+ ExportedObjectManager* object_manager,
+ Device* device)
+ : adaptor_(this),
+ object_path_(
+ base::StringPrintf("%s/devices/%d",
+ ManagerAdaptor::GetObjectPath().value().c_str(),
+ device->identifier())),
+ dbus_object_(object_manager, bus, object_path_) {
+ // Register D-Bus object.
+ dbus_object_.RegisterAndBlock();
+}
+
+DeviceDBusAdaptor::~DeviceDBusAdaptor() {}
+
+void DeviceDBusAdaptor::SetDeviceName(const string& device_name) {
+ adaptor_.SetDeviceName(device_name);
+}
+
+string DeviceDBusAdaptor::GetDeviceName() {
+ return adaptor_.GetDeviceName();
+}
+
+void DeviceDBusAdaptor::SetPreferredApInterface(const string& interface_name) {
+ adaptor_.SetPreferredApInterface(interface_name);
+}
+
+string DeviceDBusAdaptor::GetPreferredApInterface() {
+ return adaptor_.GetPreferredApInterface();
+}
+
+void DeviceDBusAdaptor::SetInUse(bool in_use) {
+ adaptor_.SetInUse(in_use);
+}
+
+bool DeviceDBusAdaptor::GetInUse() {
+ return adaptor_.GetInUse();
+}
+
+} // namespace apmanager
diff --git a/dbus/device_dbus_adaptor.h b/dbus/device_dbus_adaptor.h
new file mode 100644
index 0000000..7eb8251
--- /dev/null
+++ b/dbus/device_dbus_adaptor.h
@@ -0,0 +1,56 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef APMANAGER_DEVICE_DBUS_ADAPTOR_H_
+#define APMANAGER_DEVICE_DBUS_ADAPTOR_H_
+
+#include <base/macros.h>
+
+#include <dbus_bindings/org.chromium.apmanager.Device.h>
+
+#include "apmanager/device_adaptor_interface.h"
+
+namespace apmanager {
+
+class Device;
+
+class DeviceDBusAdaptor : public org::chromium::apmanager::DeviceInterface,
+ public DeviceAdaptorInterface {
+ public:
+ DeviceDBusAdaptor(const scoped_refptr<dbus::Bus>& bus,
+ brillo::dbus_utils::ExportedObjectManager* object_manager,
+ Device* device);
+ ~DeviceDBusAdaptor() override;
+
+ // Implementation of DeviceAdaptorInterface.
+ void SetDeviceName(const std::string& device_name) override;
+ std::string GetDeviceName() override;
+ void SetInUse(bool in_use) override;
+ bool GetInUse() override;
+ void SetPreferredApInterface(const std::string& interface_name) override;
+ std::string GetPreferredApInterface() override;
+
+ private:
+ org::chromium::apmanager::DeviceAdaptor adaptor_;
+ dbus::ObjectPath object_path_;
+ brillo::dbus_utils::DBusObject dbus_object_;
+
+ DISALLOW_COPY_AND_ASSIGN(DeviceDBusAdaptor);
+};
+
+} // namespace apmanager
+
+#endif // APMANAGER_DEVICE_DBUS_ADAPTOR_H_
diff --git a/device.cc b/device.cc
index fc82c43..5219466 100644
--- a/device.cc
+++ b/device.cc
@@ -22,45 +22,27 @@
#include <shill/net/ieee80211.h>
#include "apmanager/config.h"
+#include "apmanager/control_interface.h"
#include "apmanager/manager.h"
-using brillo::dbus_utils::AsyncEventSequencer;
-using brillo::dbus_utils::ExportedObjectManager;
-using org::chromium::apmanager::ManagerAdaptor;
using shill::ByteString;
using std::string;
namespace apmanager {
-Device::Device(Manager* manager, const string& device_name)
- : org::chromium::apmanager::DeviceAdaptor(this),
- manager_(manager),
- supports_ap_mode_(false) {
+Device::Device(Manager* manager,
+ const string& device_name,
+ int identifier)
+ : manager_(manager),
+ supports_ap_mode_(false),
+ identifier_(identifier),
+ adaptor_(manager->control_interface()->CreateDeviceAdaptor(this)) {
SetDeviceName(device_name);
SetInUse(false);
}
Device::~Device() {}
-void Device::RegisterAsync(ExportedObjectManager* object_manager,
- const scoped_refptr<dbus::Bus>& bus,
- AsyncEventSequencer* sequencer,
- int device_identifier) {
- CHECK(!dbus_object_) << "Already registered";
- dbus_path_ = dbus::ObjectPath(
- base::StringPrintf("%s/devices/%d",
- ManagerAdaptor::GetObjectPath().value().c_str(),
- device_identifier));
- dbus_object_.reset(
- new brillo::dbus_utils::DBusObject(
- object_manager,
- bus,
- dbus_path_));
- RegisterWithDBusObject(dbus_object_.get());
- dbus_object_->RegisterAsync(
- sequencer->GetHandler("Config.RegisterAsync() failed.", true));
-}
-
void Device::RegisterInterface(const WiFiInterface& new_interface) {
LOG(INFO) << "RegisteringInterface " << new_interface.iface_name
<< " on device " << GetDeviceName();
@@ -299,6 +281,30 @@ bool Device::GetVHTCapability(uint16_t channel, string* vht_cap) {
return false;
}
+void Device::SetDeviceName(const std::string& device_name) {
+ adaptor_->SetDeviceName(device_name);
+}
+
+string Device::GetDeviceName() const {
+ return adaptor_->GetDeviceName();
+}
+
+void Device::SetPreferredApInterface(const std::string& interface_name) {
+ adaptor_->SetPreferredApInterface(interface_name);
+}
+
+string Device::GetPreferredApInterface() const {
+ return adaptor_->GetPreferredApInterface();
+}
+
+void Device::SetInUse(bool in_use) {
+ return adaptor_->SetInUse(in_use);
+}
+
+bool Device::GetInUse() const {
+ return adaptor_->GetInUse();
+}
+
// static
bool Device::GetHTSecondaryChannelLocation(uint16_t channel, bool* above) {
bool ret_val = true;
diff --git a/device.h b/device.h
index 51fb432..36f605b 100644
--- a/device.h
+++ b/device.h
@@ -26,17 +26,16 @@
#include <shill/net/byte_string.h>
#include <shill/net/nl80211_message.h>
-#include "dbus_bindings/org.chromium.apmanager.Device.h"
+#include "apmanager/device_adaptor_interface.h"
namespace apmanager {
+class ControlInterface;
class Manager;
// Abstraction for WiFi Device (PHY). Each device can have one or more
// interfaces defined on it.
-class Device : public base::RefCounted<Device>,
- public org::chromium::apmanager::DeviceAdaptor,
- public org::chromium::apmanager::DeviceInterface {
+class Device : public base::RefCounted<Device> {
public:
struct WiFiInterface {
WiFiInterface() : iface_index(0), iface_type(0) {}
@@ -66,16 +65,11 @@ class Device : public base::RefCounted<Device>,
uint16_t vht_capability_mask;
};
- Device(Manager* manager, const std::string& device_name);
+ Device(Manager* manager,
+ const std::string& device_name,
+ int identifier);
virtual ~Device();
- // Register Device DBus object.
- void RegisterAsync(
- brillo::dbus_utils::ExportedObjectManager* object_manager,
- const scoped_refptr<dbus::Bus>& bus,
- brillo::dbus_utils::AsyncEventSequencer* sequencer,
- int device_identifier);
-
// Register/deregister WiFi interface on this device.
virtual void RegisterInterface(const WiFiInterface& interface);
virtual void DeregisterInterface(const WiFiInterface& interface);
@@ -101,6 +95,15 @@ class Device : public base::RefCounted<Device>,
virtual bool GetHTCapability(uint16_t channel, std::string* ht_cap);
virtual bool GetVHTCapability(uint16_t channel, std::string* vht_cap);
+ void SetDeviceName(const std::string& device_name);
+ std::string GetDeviceName() const;
+ void SetPreferredApInterface(const std::string& interface_name);
+ std::string GetPreferredApInterface() const;
+ void SetInUse(bool in_use);
+ bool GetInUse() const;
+
+ int identifier() const { return identifier_; }
+
private:
friend class DeviceTest;
@@ -123,9 +126,6 @@ class Device : public base::RefCounted<Device>,
// List of WiFi interfaces live on this device (PHY).
std::vector<WiFiInterface> interface_list_;
- dbus::ObjectPath dbus_path_;
- std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
-
// Flag indicating if this device supports AP mode interface or not.
bool supports_ap_mode_;
@@ -135,6 +135,12 @@ class Device : public base::RefCounted<Device>,
// List of claimed interfaces.
std::set<std::string> claimed_interfaces_;
+ // Unique device identifier.
+ int identifier_;
+
+ // Adaptor for communicating with remote clients.
+ std::unique_ptr<DeviceAdaptorInterface> adaptor_;
+
DISALLOW_COPY_AND_ASSIGN(Device);
};
diff --git a/device_adaptor_interface.h b/device_adaptor_interface.h
new file mode 100644
index 0000000..37460a4
--- /dev/null
+++ b/device_adaptor_interface.h
@@ -0,0 +1,38 @@
+//
+// Copyright 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef APMANAGER_DEVICE_ADAPTOR_INTERFACE_H_
+#define APMANAGER_DEVICE_ADAPTOR_INTERFACE_H_
+
+#include <string>
+
+namespace apmanager {
+
+class DeviceAdaptorInterface {
+ public:
+ virtual ~DeviceAdaptorInterface() {}
+
+ virtual void SetDeviceName(const std::string& device_name) = 0;
+ virtual std::string GetDeviceName() = 0;
+ virtual void SetPreferredApInterface(const std::string& interface_name) = 0;
+ virtual std::string GetPreferredApInterface() = 0;
+ virtual void SetInUse(bool in_use) = 0;
+ virtual bool GetInUse() = 0;
+};
+
+} // namespace apmanager
+
+#endif // APMANAGER_DEVICE_ADAPTOR_INTERFACE_H_
diff --git a/device_info.cc b/device_info.cc
index 94415ae..706a161 100644
--- a/device_info.cc
+++ b/device_info.cc
@@ -31,6 +31,7 @@
#include <shill/net/rtnl_listener.h>
#include <shill/net/rtnl_message.h>
+#include "apmanager/control_interface.h"
#include "apmanager/manager.h"
using base::Bind;
@@ -55,7 +56,8 @@ DeviceInfo::DeviceInfo(Manager* manager)
device_info_root_(kDeviceInfoRoot),
manager_(manager),
netlink_manager_(NetlinkManager::GetInstance()),
- rtnl_handler_(RTNLHandler::GetInstance()) {
+ rtnl_handler_(RTNLHandler::GetInstance()),
+ device_identifier_(0) {
}
DeviceInfo::~DeviceInfo() {}
@@ -121,7 +123,8 @@ void DeviceInfo::OnWiFiPhyInfoReceived(const shill::Nl80211Message& msg) {
return;
}
- scoped_refptr<Device> device = new Device(manager_, device_name);
+ scoped_refptr<Device> device =
+ new Device(manager_, device_name, device_identifier_++);
device->ParseWiphyCapability(msg);
// Register device
@@ -302,7 +305,8 @@ void DeviceInfo::OnWiFiInterfacePhyInfoReceived(
scoped_refptr<Device> device = GetDevice(device_name);
// Create device if it is not enumerated yet.
if (!device) {
- device = new Device(manager_, device_name);
+ device =
+ new Device(manager_, device_name, device_identifier_++);
device->ParseWiphyCapability(msg);
// Register device
diff --git a/device_info.h b/device_info.h
index 1b5bd9a..937359f 100644
--- a/device_info.h
+++ b/device_info.h
@@ -40,6 +40,7 @@ class RTNLListener;
namespace apmanager {
+class ControlInterface;
class Manager;
// DeviceInfo will enumerate WiFi devices (PHYs) during startup and on-demand
@@ -104,12 +105,14 @@ class DeviceInfo : public base::SupportsWeakPtr<DeviceInfo> {
std::unique_ptr<shill::RTNLListener> link_listener_;
base::FilePath device_info_root_;
- Manager *manager_;
+ Manager* manager_;
// Cache copy of singleton pointers.
shill::NetlinkManager* netlink_manager_;
shill::RTNLHandler* rtnl_handler_;
+ int device_identifier_;
+
DISALLOW_COPY_AND_ASSIGN(DeviceInfo);
};
diff --git a/device_info_unittest.cc b/device_info_unittest.cc
index b8ab45f..157d1be 100644
--- a/device_info_unittest.cc
+++ b/device_info_unittest.cc
@@ -34,6 +34,8 @@
#include "shill/net/nl80211_message.h"
#include <shill/net/rtnl_message.h>
+#include "apmanager/fake_device_adaptor.h"
+#include "apmanager/mock_control.h"
#include "apmanager/mock_device.h"
#include "apmanager/mock_manager.h"
@@ -45,6 +47,7 @@ using std::string;
using std::vector;
using ::testing::_;
using ::testing::Mock;
+using ::testing::ReturnNew;
namespace apmanager {
@@ -60,7 +63,9 @@ const uint32_t kTestInterface1Index = 1001;
class DeviceInfoTest : public testing::Test {
public:
- DeviceInfoTest() : device_info_(&manager_) {}
+ DeviceInfoTest()
+ : manager_(&control_interface_),
+ device_info_(&manager_) {}
virtual ~DeviceInfoTest() {}
virtual void SetUp() {
@@ -71,6 +76,9 @@ class DeviceInfoTest : public testing::Test {
// Setup mock pointers;
device_info_.netlink_manager_ = &netlink_manager_;
+
+ ON_CALL(control_interface_, CreateDeviceAdaptorRaw())
+ .WillByDefault(ReturnNew<FakeDeviceAdaptor>());
}
bool IsWifiInterface(const string& interface_name) {
@@ -145,11 +153,13 @@ class DeviceInfoTest : public testing::Test {
}
protected:
- DeviceInfo device_info_;
+ MockControl control_interface_;
MockManager manager_;
+
shill::MockNetlinkManager netlink_manager_;
base::ScopedTempDir temp_dir_;
base::FilePath device_info_root_;
+ DeviceInfo device_info_;
};
MATCHER_P2(IsGetInfoMessage, command, index, "") {
@@ -317,7 +327,7 @@ TEST_F(DeviceInfoTest, ParseWifiInterfaceInfo) {
TEST_F(DeviceInfoTest, ParsePhyInfoForWifiInterface) {
// Register a mock device.
- scoped_refptr<MockDevice> device = new MockDevice();
+ scoped_refptr<MockDevice> device = new MockDevice(&manager_);
device->SetDeviceName(kTestDeviceName);
EXPECT_CALL(manager_, RegisterDevice(_)).Times(1);
RegisterDevice(device);
@@ -376,7 +386,7 @@ TEST_F(DeviceInfoTest, RegisterDevice) {
VerifyDeviceList(device_list);
// Register a device.
- device_list.push_back(new Device(&manager_, kTestDeviceName));
+ device_list.push_back(new Device(&manager_, kTestDeviceName, 0));
EXPECT_CALL(manager_, RegisterDevice(device_list[0]));
RegisterDevice(device_list[0]);
VerifyDeviceList(device_list);
diff --git a/device_unittest.cc b/device_unittest.cc
index 17ceee6..585e9f2 100644
--- a/device_unittest.cc
+++ b/device_unittest.cc
@@ -26,10 +26,13 @@
#include <shill/net/nl80211_attribute.h>
#include <shill/net/nl80211_message.h>
+#include "apmanager/fake_device_adaptor.h"
+#include "apmanager/mock_control.h"
#include "apmanager/mock_manager.h"
using ::testing::_;
using ::testing::Mock;
+using ::testing::ReturnNew;
using std::vector;
namespace apmanager {
@@ -57,7 +60,11 @@ const Device::WiFiInterface kMonitorModeInterface = {
class DeviceTest : public testing::Test {
public:
- DeviceTest() : device_(new Device(&manager_, kDeviceName)) {}
+ DeviceTest() : manager_(&control_interface_) {
+ ON_CALL(control_interface_, CreateDeviceAdaptorRaw())
+ .WillByDefault(ReturnNew<FakeDeviceAdaptor>());
+ device_ = new Device(&manager_, kDeviceName, 0);
+ }
void VerifyInterfaceList(
const vector<Device::WiFiInterface>& interface_list) {
@@ -121,6 +128,7 @@ class DeviceTest : public testing::Test {
}
protected:
+ MockControl control_interface_;
MockManager manager_;
scoped_refptr<Device> device_;
};
diff --git a/fake_device_adaptor.cc b/fake_device_adaptor.cc
new file mode 100644
index 0000000..7dc009c
--- /dev/null
+++ b/fake_device_adaptor.cc
@@ -0,0 +1,50 @@
+//
+// Copyright 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "apmanager/fake_device_adaptor.h"
+
+namespace apmanager {
+
+FakeDeviceAdaptor::FakeDeviceAdaptor() : in_use_(false) {}
+
+FakeDeviceAdaptor::~FakeDeviceAdaptor() {}
+
+void FakeDeviceAdaptor::SetDeviceName(const std::string& device_name) {
+ device_name_ = device_name;
+}
+
+std::string FakeDeviceAdaptor::GetDeviceName() {
+ return device_name_;
+}
+
+void FakeDeviceAdaptor::SetPreferredApInterface(
+ const std::string& interface_name) {
+ preferred_ap_interface_ = interface_name;
+}
+
+std::string FakeDeviceAdaptor::GetPreferredApInterface() {
+ return preferred_ap_interface_;
+}
+
+void FakeDeviceAdaptor::SetInUse(bool in_use) {
+ in_use_ = in_use;
+}
+
+bool FakeDeviceAdaptor::GetInUse() {
+ return in_use_;
+}
+
+} // namespace apmanager
diff --git a/fake_device_adaptor.h b/fake_device_adaptor.h
new file mode 100644
index 0000000..3a618f7
--- /dev/null
+++ b/fake_device_adaptor.h
@@ -0,0 +1,50 @@
+//
+// Copyright 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef APMANAGER_FAKE_DEVICE_ADAPTOR_H_
+#define APMANAGER_FAKE_DEVICE_ADAPTOR_H_
+
+#include <string>
+
+#include <base/macros.h>
+
+#include "apmanager/device_adaptor_interface.h"
+
+namespace apmanager {
+
+class FakeDeviceAdaptor : public DeviceAdaptorInterface {
+ public:
+ FakeDeviceAdaptor();
+ ~FakeDeviceAdaptor() override;
+
+ void SetDeviceName(const std::string& device_name) override;
+ std::string GetDeviceName() override;
+ void SetPreferredApInterface(const std::string& interface_name) override;
+ std::string GetPreferredApInterface() override;
+ void SetInUse(bool in_use) override;
+ bool GetInUse() override;
+
+private:
+ std::string device_name_;
+ std::string preferred_ap_interface_;
+ bool in_use_;
+
+ DISALLOW_COPY_AND_ASSIGN(FakeDeviceAdaptor);
+};
+
+} // namespace apmanager
+
+#endif // APMANAGER_FAKE_DEVICE_ADAPTOR_H_
diff --git a/manager.cc b/manager.cc
index a288141..00839e9 100644
--- a/manager.cc
+++ b/manager.cc
@@ -31,10 +31,10 @@ using std::string;
namespace apmanager {
-Manager::Manager()
+Manager::Manager(ControlInterface* control_interface)
: org::chromium::apmanager::ManagerAdaptor(this),
+ control_interface_(control_interface),
service_identifier_(0),
- device_identifier_(0),
device_info_(this) {}
Manager::~Manager() {
@@ -45,7 +45,6 @@ Manager::~Manager() {
}
void Manager::RegisterAsync(
- ControlInterface* control_interface,
ExportedObjectManager* object_manager,
const scoped_refptr<dbus::Bus>& bus,
const base::Callback<void(bool)>& completion_callback) {
@@ -59,8 +58,8 @@ void Manager::RegisterAsync(
dbus_object_->RegisterAsync(completion_callback);
bus_ = bus;
- shill_manager_.Init(control_interface);
- firewall_manager_.Init(control_interface);
+ shill_manager_.Init(control_interface_);
+ firewall_manager_.Init(control_interface_);
}
void Manager::Start() {
@@ -148,17 +147,8 @@ scoped_refptr<Device> Manager::GetDeviceFromInterfaceName(
void Manager::RegisterDevice(scoped_refptr<Device> device) {
LOG(INFO) << "Manager::RegisterDevice: registering device "
<< device->GetDeviceName();
- // Register device DBbus interfaces.
- scoped_refptr<AsyncEventSequencer> sequencer(new AsyncEventSequencer());
- device->RegisterAsync(dbus_object_->GetObjectManager().get(),
- bus_,
- sequencer.get(),
- device_identifier_++);
- sequencer->OnAllTasksCompletedCall({
- base::Bind(&Manager::OnDeviceRegistered,
- base::Unretained(this),
- device)
- });
+ devices_.push_back(device);
+ // TODO(zqiu): Property update for available devices.
}
void Manager::ClaimInterface(const string& interface_name) {
@@ -213,14 +203,6 @@ void Manager::OnServiceRegistered(
response->Return(service_path);
}
-void Manager::OnDeviceRegistered(scoped_refptr<Device> device, bool success) {
- // Success should always be true since we've said that failures are fatal.
- CHECK(success) << "Init of one or more objects has failed.";
-
- devices_.push_back(device);
- // TODO(zqiu): Property update for available devices.
-}
-
void Manager::OnAPServiceOwnerDisappeared(int service_identifier) {
LOG(INFO) << "Owner for service " << service_identifier << " disappeared";
// Remove service watcher.
diff --git a/manager.h b/manager.h
index ad38a07..5ae8571 100644
--- a/manager.h
+++ b/manager.h
@@ -40,7 +40,7 @@ class Manager : public org::chromium::apmanager::ManagerAdaptor,
template<typename T>
using DBusMethodResponse = brillo::dbus_utils::DBusMethodResponse<T>;
- Manager();
+ explicit Manager(ControlInterface* control_interface);
virtual ~Manager();
// Implementation of ManagerInterface.
@@ -57,7 +57,6 @@ class Manager : public org::chromium::apmanager::ManagerAdaptor,
// Register DBus object.
void RegisterAsync(
- ControlInterface* control_interface,
brillo::dbus_utils::ExportedObjectManager* object_manager,
const scoped_refptr<dbus::Bus>& bus,
const base::Callback<void(bool)>& completion_callback);
@@ -92,6 +91,8 @@ class Manager : public org::chromium::apmanager::ManagerAdaptor,
virtual void RequestDHCPPortAccess(const std::string& interface);
virtual void ReleaseDHCPPortAccess(const std::string& interface);
+ ControlInterface* control_interface() const { return control_interface_; }
+
private:
friend class ManagerTest;
@@ -109,8 +110,8 @@ class Manager : public org::chromium::apmanager::ManagerAdaptor,
// This is invoked when the owner of an AP service disappeared.
void OnAPServiceOwnerDisappeared(int service_identifier);
+ ControlInterface* control_interface_;
int service_identifier_;
- int device_identifier_;
std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
scoped_refptr<dbus::Bus> bus_;
std::vector<std::unique_ptr<Service>> services_;
diff --git a/manager_unittest.cc b/manager_unittest.cc
index cf05abc..91bbc20 100644
--- a/manager_unittest.cc
+++ b/manager_unittest.cc
@@ -18,28 +18,35 @@
#include <gtest/gtest.h>
+#include "apmanager/fake_device_adaptor.h"
+#include "apmanager/mock_control.h"
#include "apmanager/mock_device.h"
using ::testing::_;
using ::testing::Return;
+using ::testing::ReturnNew;
namespace apmanager {
class ManagerTest : public testing::Test {
public:
- ManagerTest() : manager_() {}
+ ManagerTest() : manager_(&control_interface_) {
+ ON_CALL(control_interface_, CreateDeviceAdaptorRaw())
+ .WillByDefault(ReturnNew<FakeDeviceAdaptor>());
+ }
void RegisterDevice(scoped_refptr<Device> device) {
manager_.devices_.push_back(device);
}
protected:
+ MockControl control_interface_;
Manager manager_;
};
TEST_F(ManagerTest, GetAvailableDevice) {
// Register a device without AP support (no preferred AP interface).
- scoped_refptr<MockDevice> device0 = new MockDevice();
+ scoped_refptr<MockDevice> device0 = new MockDevice(&manager_);
RegisterDevice(device0);
// No available device for AP operation.
@@ -52,7 +59,7 @@ TEST_F(ManagerTest, GetAvailableDevice) {
// Register another device with AP support.
const char kTestInterface1[] = "test-interface1";
- scoped_refptr<MockDevice> device1 = new MockDevice();
+ scoped_refptr<MockDevice> device1 = new MockDevice(&manager_);
device1->SetPreferredApInterface(kTestInterface1);
RegisterDevice(device1);
@@ -70,8 +77,8 @@ TEST_F(ManagerTest, GetAvailableDevice) {
TEST_F(ManagerTest, GetDeviceFromInterfaceName) {
// Register two devices
- scoped_refptr<MockDevice> device0 = new MockDevice();
- scoped_refptr<MockDevice> device1 = new MockDevice();
+ scoped_refptr<MockDevice> device0 = new MockDevice(&manager_);
+ scoped_refptr<MockDevice> device1 = new MockDevice(&manager_);
RegisterDevice(device0);
RegisterDevice(device1);
diff --git a/mock_control.cc b/mock_control.cc
new file mode 100644
index 0000000..a7ae8a2
--- /dev/null
+++ b/mock_control.cc
@@ -0,0 +1,42 @@
+//
+// Copyright 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "apmanager/mock_control.h"
+
+namespace apmanager {
+
+MockControl::MockControl() {}
+
+MockControl::~MockControl() {}
+
+std::unique_ptr<DeviceAdaptorInterface> MockControl::CreateDeviceAdaptor(
+ Device* /* device */) {
+ return std::unique_ptr<DeviceAdaptorInterface>(CreateDeviceAdaptorRaw());
+}
+
+std::unique_ptr<FirewallProxyInterface> MockControl::CreateFirewallProxy(
+ const base::Closure& /* service_appeared_callback */,
+ const base::Closure& /* service_vanished_callback */) {
+ return std::unique_ptr<FirewallProxyInterface>(CreateFirewallProxyRaw());
+}
+
+std::unique_ptr<ShillProxyInterface> MockControl::CreateShillProxy(
+ const base::Closure& /* service_appeared_callback */,
+ const base::Closure& /* service_vanished_callback */) {
+ return std::unique_ptr<ShillProxyInterface>(CreateShillProxyRaw());
+}
+
+} // namespace apmanager
diff --git a/mock_control.h b/mock_control.h
new file mode 100644
index 0000000..35d35f1
--- /dev/null
+++ b/mock_control.h
@@ -0,0 +1,60 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef APMANAGER_MOCK_CONTROL_H_
+#define APMANAGER_MOCK_CONTROL_H_
+
+#include <base/macros.h>
+#include <gmock/gmock.h>
+
+#include "apmanager/control_interface.h"
+
+namespace apmanager {
+
+class MockControl : public ControlInterface {
+ public:
+ MockControl();
+ ~MockControl() override;
+
+ MOCK_METHOD0(Init, void());
+ MOCK_METHOD0(Shutdown, void());
+
+ // Provide mock methods for creating raw pointer for adaptor/proxy.
+ // This allows us to set expectations for adaptor/proxy creation
+ // functions, since mock methods only support copyable return values,
+ // and unique_ptr is not copyable.
+ MOCK_METHOD0(CreateDeviceAdaptorRaw, DeviceAdaptorInterface*());
+ MOCK_METHOD0(CreateFirewallProxyRaw, FirewallProxyInterface*());
+ MOCK_METHOD0(CreateShillProxyRaw, ShillProxyInterface*());
+
+ // These functions use the mock methods above for creating
+ // raw object.
+ std::unique_ptr<DeviceAdaptorInterface> CreateDeviceAdaptor(
+ Device* device) override;
+ std::unique_ptr<FirewallProxyInterface> CreateFirewallProxy(
+ const base::Closure& service_appeared_callback,
+ const base::Closure& service_vanished_callback) override;
+ std::unique_ptr<ShillProxyInterface> CreateShillProxy(
+ const base::Closure& service_appeared_callback,
+ const base::Closure& service_vanished_callback) override;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockControl);
+};
+
+} // namespace apmanager
+
+#endif // APMANAGER_MOCK_CONTROL_H_
diff --git a/mock_device.cc b/mock_device.cc
index 70beda6..ee476c3 100644
--- a/mock_device.cc
+++ b/mock_device.cc
@@ -18,7 +18,7 @@
namespace apmanager {
-MockDevice::MockDevice() : Device(nullptr, "") {}
+MockDevice::MockDevice(Manager* manager) : Device(manager, "", 0) {}
MockDevice::~MockDevice() {}
diff --git a/mock_device.h b/mock_device.h
index 3d60f64..bcf92c8 100644
--- a/mock_device.h
+++ b/mock_device.h
@@ -28,7 +28,7 @@ namespace apmanager {
class MockDevice : public Device {
public:
- MockDevice();
+ explicit MockDevice(Manager* manager);
~MockDevice() override;
MOCK_METHOD1(RegisterInterface,
diff --git a/mock_manager.cc b/mock_manager.cc
index 43173c3..1f01f4e 100644
--- a/mock_manager.cc
+++ b/mock_manager.cc
@@ -18,7 +18,8 @@
namespace apmanager {
-MockManager::MockManager() : Manager() {}
+MockManager::MockManager(ControlInterface* control_interface)
+ : Manager(control_interface) {}
MockManager::~MockManager() {}
diff --git a/mock_manager.h b/mock_manager.h
index 9335fdc..d67d87e 100644
--- a/mock_manager.h
+++ b/mock_manager.h
@@ -28,7 +28,7 @@ namespace apmanager {
class MockManager : public Manager {
public:
- MockManager();
+ explicit MockManager(ControlInterface* control_interface);
~MockManager() override;
MOCK_METHOD0(Start, void());
diff --git a/service_unittest.cc b/service_unittest.cc
index 313d3f9..f53c84b 100644
--- a/service_unittest.cc
+++ b/service_unittest.cc
@@ -63,7 +63,8 @@ namespace apmanager {
class ServiceTest : public testing::Test {
public:
ServiceTest()
- : hostapd_monitor_(new MockHostapdMonitor()),
+ : manager_(nullptr),
+ hostapd_monitor_(new MockHostapdMonitor()),
service_(&manager_, kServiceIdentifier) {}
virtual void SetUp() {