diff options
-rw-r--r-- | Android.mk | 1 | ||||
-rw-r--r-- | apmanager.gyp | 1 | ||||
-rw-r--r-- | config.cc | 271 | ||||
-rw-r--r-- | config.h | 81 | ||||
-rw-r--r-- | config_adaptor_interface.h | 7 | ||||
-rw-r--r-- | config_unittest.cc | 252 | ||||
-rw-r--r-- | dbus/config_dbus_adaptor.cc | 36 | ||||
-rw-r--r-- | dbus/config_dbus_adaptor.h | 1 | ||||
-rw-r--r-- | fake_config_adaptor.cc | 119 | ||||
-rw-r--r-- | fake_config_adaptor.h | 75 | ||||
-rw-r--r-- | mock_config.cc | 2 | ||||
-rw-r--r-- | mock_config.h | 5 | ||||
-rw-r--r-- | rpc_interface.h | 29 | ||||
-rw-r--r-- | service.cc | 16 | ||||
-rw-r--r-- | service_unittest.cc | 53 |
15 files changed, 656 insertions, 293 deletions
@@ -111,6 +111,7 @@ LOCAL_SRC_FILES := \ device_unittest.cc \ dhcp_server_unittest.cc \ error_unittest.cc \ + fake_config_adaptor.cc \ fake_device_adaptor.cc \ hostapd_monitor_unittest.cc \ manager_unittest.cc \ diff --git a/apmanager.gyp b/apmanager.gyp index 4ce5251..05ed279 100644 --- a/apmanager.gyp +++ b/apmanager.gyp @@ -147,6 +147,7 @@ 'device_unittest.cc', 'dhcp_server_unittest.cc', 'error_unittest.cc', + 'fake_config_adaptor.cc', 'fake_device_adaptor.cc', 'hostapd_monitor_unittest.cc', 'manager_unittest.cc', @@ -21,16 +21,14 @@ #if !defined(__ANDROID__) #include <chromeos/dbus/service_constants.h> #else -#include "dbus/apmanager/dbus-constants.h" +#include <dbus/apmanager/dbus-constants.h> #endif // __ANDROID__ +#include "apmanager/error.h" #include "apmanager/daemon.h" #include "apmanager/device.h" #include "apmanager/manager.h" -using brillo::dbus_utils::AsyncEventSequencer; -using brillo::dbus_utils::ExportedObjectManager; -using brillo::ErrorPtr; using std::string; namespace apmanager { @@ -91,11 +89,11 @@ const int Config::kSsidMaxLength = 32; const int Config::kPassphraseMinLength = 8; const int Config::kPassphraseMaxLength = 63; -Config::Config(Manager* manager, const string& service_path) - : org::chromium::apmanager::ConfigAdaptor(this), - manager_(manager), - dbus_path_(dbus::ObjectPath( - base::StringPrintf("%s/config", service_path.c_str()))) { +Config::Config(Manager* manager, int service_identifier) + : manager_(manager), + adaptor_( + manager->control_interface()->CreateConfigAdaptor( + this, service_identifier)) { // Initialize default configuration values. SetSecurityMode(kSecurityModeNone); SetHwMode(kHwMode80211g); @@ -122,93 +120,93 @@ bool Config::GetFrequencyFromChannel(uint16_t channel, uint32_t* freq) { return ret_value; } -bool Config::ValidateSsid(ErrorPtr* error, const string& value) { +bool Config::ValidateSsid(Error* error, const string& value) { if (value.length() < kSsidMinLength || value.length() > kSsidMaxLength) { - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "SSID must contain between %d and %d characters", - kSsidMinLength, kSsidMaxLength); + Error::PopulateAndLog( + error, + Error::kInvalidArguments, + base::StringPrintf("SSID must contain between %d and %d characters", + kSsidMinLength, kSsidMaxLength), + FROM_HERE); return false; } return true; } -bool Config::ValidateSecurityMode(ErrorPtr* error, const string& value) { +bool Config::ValidateSecurityMode(Error* error, const string& value) { if (value != kSecurityModeNone && value != kSecurityModeRSN) { - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Invalid/unsupported security mode [%s]", value.c_str()); + Error::PopulateAndLog( + error, + Error::kInvalidArguments, + base::StringPrintf("Invalid/unsupported security mode [%s]", + value.c_str()), + FROM_HERE); return false; } return true; } -bool Config::ValidatePassphrase(ErrorPtr* error, const string& value) { +bool Config::ValidatePassphrase(Error* error, const string& value) { if (value.length() < kPassphraseMinLength || value.length() > kPassphraseMaxLength) { - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Passphrase must contain between %d and %d characters", - kPassphraseMinLength, kPassphraseMaxLength); + Error::PopulateAndLog( + error, + Error::kInvalidArguments, + base::StringPrintf("Passphrase must contain between %d and %d characters", + kPassphraseMinLength, kPassphraseMaxLength), + FROM_HERE); + return false; } return true; } -bool Config::ValidateHwMode(ErrorPtr* error, const string& value) { +bool Config::ValidateHwMode(Error* error, const string& value) { if (value != kHwMode80211a && value != kHwMode80211b && value != kHwMode80211g && value != kHwMode80211n && value != kHwMode80211ac) { - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Invalid HW mode [%s]", value.c_str()); + Error::PopulateAndLog( + error, + Error::kInvalidArguments, + base::StringPrintf("Invalid HW mode [%s]", value.c_str()), + FROM_HERE); return false; } return true; } -bool Config::ValidateOperationMode(ErrorPtr* error, const string& value) { +bool Config::ValidateOperationMode(Error* error, const string& value) { if (value != kOperationModeServer && value != kOperationModeBridge) { - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Invalid operation mode [%s]", value.c_str()); + Error::PopulateAndLog( + error, + Error::kInvalidArguments, + base::StringPrintf("Invalid operation mode [%s]", value.c_str()), + FROM_HERE); return false; } return true; } -bool Config::ValidateChannel(ErrorPtr* error, const uint16_t& value) { +bool Config::ValidateChannel(Error* error, const uint16_t& value) { if ((value >= kBand24GHzChannelLow && value <= kBand24GHzChannelHigh) || (value >= kBand5GHzChannelLow && value <= kBand5GHzChannelHigh)) { return true; } - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Invalid channel [%d]", value); + Error::PopulateAndLog(error, + Error::kInvalidArguments, + base::StringPrintf("Invalid channel [%d]", value), + FROM_HERE); return false; } -void Config::RegisterAsync(ExportedObjectManager* object_manager, - const scoped_refptr<dbus::Bus>& bus, - AsyncEventSequencer* sequencer) { - CHECK(!dbus_object_) << "Already registered"; - 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)); -} - -bool Config::GenerateConfigFile(ErrorPtr* error, string* config_str) { +bool Config::GenerateConfigFile(Error* error, string* config_str) { // SSID. string ssid = GetSsid(); if (ssid.empty()) { - brillo::Error::AddTo( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "SSID not specified"); + Error::PopulateAndLog(error, + Error::kInvalidConfiguration, + "SSID not specified", + FROM_HERE); return false; } base::StringAppendF( @@ -217,9 +215,11 @@ bool Config::GenerateConfigFile(ErrorPtr* error, string* config_str) { // Bridge interface is required for bridge mode operation. if (GetOperationMode() == kOperationModeBridge) { if (GetBridgeInterface().empty()) { - brillo::Error::AddTo( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Bridge interface not specified, required for bridge mode"); + Error::PopulateAndLog( + error, + Error::kInvalidConfiguration, + "Bridge interface not specified, required for bridge mode", + FROM_HERE); return false; } base::StringAppendF(config_str, @@ -283,7 +283,95 @@ bool Config::ReleaseDevice() { return device_->ReleaseDevice(); } -bool Config::AppendHwMode(ErrorPtr* error, std::string* config_str) { +void Config::SetSsid(const string& ssid) { + adaptor_->SetSsid(ssid); +} + +string Config::GetSsid() const { + return adaptor_->GetSsid(); +} + +void Config::SetInterfaceName(const std::string& interface_name) { + adaptor_->SetInterfaceName(interface_name); +} + +string Config::GetInterfaceName() const { + return adaptor_->GetInterfaceName(); +} + +void Config::SetSecurityMode(const std::string& mode) { + adaptor_->SetSecurityMode(mode); +} + +string Config::GetSecurityMode() const { + return adaptor_->GetSecurityMode(); +} + +void Config::SetPassphrase(const std::string& passphrase) { + adaptor_->SetPassphrase(passphrase); +} + +string Config::GetPassphrase() const { + return adaptor_->GetPassphrase(); +} + +void Config::SetHwMode(const std::string& hw_mode) { + adaptor_->SetHwMode(hw_mode); +} + +string Config::GetHwMode() const { + return adaptor_->GetHwMode(); +} + +void Config::SetOperationMode(const std::string& op_mode) { + adaptor_->SetOperationMode(op_mode); +} + +string Config::GetOperationMode() const { + return adaptor_->GetOperationMode(); +} + +void Config::SetChannel(uint16_t channel) { + adaptor_->SetChannel(channel); +} + +uint16_t Config::GetChannel() const { + return adaptor_->GetChannel(); +} + +void Config::SetHiddenNetwork(bool hidden_network) { + adaptor_->SetHiddenNetwork(hidden_network); +} + +bool Config::GetHiddenNetwork() const { + return adaptor_->GetHiddenNetwork(); +} + +void Config::SetBridgeInterface(const std::string& interface_name) { + adaptor_->SetBridgeInterface(interface_name); +} + +string Config::GetBridgeInterface() const { + return adaptor_->GetBridgeInterface(); +} + +void Config::SetServerAddressIndex(uint16_t index) { + adaptor_->SetServerAddressIndex(index); +} + +uint16_t Config::GetServerAddressIndex() const { + return adaptor_->GetServerAddressIndex(); +} + +void Config::SetFullDeviceControl(bool full_control) { + adaptor_->SetFullDeviceControl(full_control); +} + +bool Config::GetFullDeviceControl() const { + return adaptor_->GetFullDeviceControl(); +} + +bool Config::AppendHwMode(Error* error, string* config_str) { string hw_mode = GetHwMode(); string hostapd_hw_mode; if (hw_mode == kHwMode80211a) { @@ -304,9 +392,10 @@ bool Config::AppendHwMode(ErrorPtr* error, std::string* config_str) { // Get HT Capability. string ht_cap; if (!device_->GetHTCapability(GetChannel(), &ht_cap)) { - brillo::Error::AddTo( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Failed to get HT Capability"); + Error::PopulateAndLog(error, + Error::kInvalidConfiguration, + "Failed to get HT Capability", + FROM_HERE); return false; } base::StringAppendF(config_str, "%s=%s\n", @@ -323,9 +412,11 @@ bool Config::AppendHwMode(ErrorPtr* error, std::string* config_str) { // TODO(zqiu): Determine VHT Capabilities based on the interface PHY's // capababilites. } else { - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Invalid hardware mode: %s", hw_mode.c_str()); + Error::PopulateAndLog( + error, + Error::kInvalidConfiguration, + base::StringPrintf("Invalid hardware mode: %s", hw_mode.c_str()), + FROM_HERE); return false; } @@ -334,8 +425,7 @@ bool Config::AppendHwMode(ErrorPtr* error, std::string* config_str) { return true; } -bool Config::AppendHostapdDefaults(ErrorPtr* error, - std::string* config_str) { +bool Config::AppendHostapdDefaults(Error* error, string* config_str) { // Driver: NL80211. base::StringAppendF( config_str, "%s=%s\n", kHostapdConfigKeyDriver, kHostapdDefaultDriver); @@ -355,33 +445,36 @@ bool Config::AppendHostapdDefaults(ErrorPtr* error, return true; } -bool Config::AppendInterface(ErrorPtr* error, - std::string* config_str) { +bool Config::AppendInterface(Error* error, string* config_str) { string interface = GetInterfaceName(); if (interface.empty()) { // Ask manager for unused ap capable device. device_ = manager_->GetAvailableDevice(); if (!device_) { - brillo::Error::AddTo( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "No device available"); + Error::PopulateAndLog( + error, Error::kInternalError, "No device available", FROM_HERE); return false; } } else { device_ = manager_->GetDeviceFromInterfaceName(interface); if (!device_) { - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Unable to find device for the specified interface [%s]", - interface.c_str()); + Error::PopulateAndLog( + error, + Error::kInvalidConfiguration, + base::StringPrintf( + "Unable to find device for the specified interface [%s]", + interface.c_str()), + FROM_HERE); return false; } if (device_->GetInUse()) { - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Device [%s] for interface [%s] already in use", - device_->GetDeviceName().c_str(), - interface.c_str()); + Error::PopulateAndLog( + error, + Error::kInvalidConfiguration, + base::StringPrintf("Device [%s] for interface [%s] already in use", + device_->GetDeviceName().c_str(), + interface.c_str()), + FROM_HERE); return false; } } @@ -395,8 +488,7 @@ bool Config::AppendInterface(ErrorPtr* error, return true; } -bool Config::AppendSecurityMode(ErrorPtr* error, - std::string* config_str) { +bool Config::AppendSecurityMode(Error* error, string* config_str) { string security_mode = GetSecurityMode(); if (security_mode == kSecurityModeNone) { // Nothing need to be done for open network. @@ -406,9 +498,12 @@ bool Config::AppendSecurityMode(ErrorPtr* error, if (security_mode == kSecurityModeRSN) { string passphrase = GetPassphrase(); if (passphrase.empty()) { - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Passphrase not set for security mode: %s", security_mode.c_str()); + Error::PopulateAndLog( + error, + Error::kInvalidConfiguration, + base::StringPrintf("Passphrase not set for security mode: %s", + security_mode.c_str()), + FROM_HERE); return false; } @@ -428,9 +523,11 @@ bool Config::AppendSecurityMode(ErrorPtr* error, return true; } - brillo::Error::AddToPrintf( - error, FROM_HERE, brillo::errors::dbus::kDomain, kConfigError, - "Invalid security mode: %s", security_mode.c_str()); + Error::PopulateAndLog( + error, + Error::kInvalidConfiguration, + base::StringPrintf("Invalid security mode: %s", security_mode.c_str()), + FROM_HERE); return false; } @@ -21,56 +21,66 @@ #include <string> #include <base/macros.h> +#include <base/memory/ref_counted.h> #include <brillo/errors/error.h> -#include "dbus_bindings/org.chromium.apmanager.Config.h" +#include "apmanager/config_adaptor_interface.h" namespace apmanager { +class Error; class Device; class Manager; -class Config - : public org::chromium::apmanager::ConfigAdaptor, - public org::chromium::apmanager::ConfigInterface { +class Config { public: - Config(Manager* manager, const std::string& service_path); + Config(Manager* manager, int service_identifier); virtual ~Config(); - // Override ConfigAdaptor Validate functions. - bool ValidateSsid(brillo::ErrorPtr* error, - const std::string& value) override; - bool ValidateSecurityMode(brillo::ErrorPtr* error, - const std::string& value) override; - bool ValidatePassphrase(brillo::ErrorPtr* error, - const std::string& value) override; - bool ValidateHwMode(brillo::ErrorPtr* error, - const std::string& value) override; - bool ValidateOperationMode(brillo::ErrorPtr* error, - const std::string& value) override; - bool ValidateChannel(brillo::ErrorPtr* error, - const uint16_t& value) override; + bool ValidateSsid(Error* error, const std::string& value); + bool ValidateSecurityMode(Error* error, const std::string& value); + bool ValidatePassphrase(Error* error, const std::string& value); + bool ValidateHwMode(Error* error, const std::string& value); + bool ValidateOperationMode(Error* error, const std::string& value); + bool ValidateChannel(Error* error, const uint16_t& value); // Calculate the frequency based on the given |channel|. Return true and set // the output |frequency| if is valid channel, false otherwise. static bool GetFrequencyFromChannel(uint16_t channel, uint32_t* freq); - // Register Config DBus object. - void RegisterAsync( - brillo::dbus_utils::ExportedObjectManager* object_manager, - const scoped_refptr<dbus::Bus>& bus, - brillo::dbus_utils::AsyncEventSequencer* sequencer); - - // Generate a config file string for a hostapd instance. Raise appropriate - // error when encounter invalid configuration. Return true if success, + // Generate a config file string for a hostapd instance. Populate + // |error| when encounter invalid configuration. Return true if success, // false otherwise. - virtual bool GenerateConfigFile(brillo::ErrorPtr* error, - std::string* config_str); + virtual bool GenerateConfigFile(Error* error, std::string* config_str); // Claim and release the device needed for this configuration. virtual bool ClaimDevice(); virtual bool ReleaseDevice(); + // Getter and setter for configuration properties. + void SetSsid(const std::string& ssid); + std::string GetSsid() const; + void SetInterfaceName(const std::string& interface_name); + std::string GetInterfaceName() const; + void SetSecurityMode(const std::string& security_mode); + std::string GetSecurityMode() const; + void SetPassphrase(const std::string& passphrase); + std::string GetPassphrase() const; + void SetHwMode(const std::string& hw_mode); + std::string GetHwMode() const; + void SetOperationMode(const std::string& op_mode); + std::string GetOperationMode() const; + void SetChannel(uint16_t channel); + uint16_t GetChannel() const; + void SetHiddenNetwork(bool hidden); + bool GetHiddenNetwork() const; + void SetBridgeInterface(const std::string& interface_name); + std::string GetBridgeInterface() const; + void SetServerAddressIndex(uint16_t); + uint16_t GetServerAddressIndex() const; + void SetFullDeviceControl(bool full_control); + bool GetFullDeviceControl() const; + const std::string& control_interface() const { return control_interface_; } void set_control_interface(const std::string& control_interface) { control_interface_ = control_interface; @@ -78,7 +88,7 @@ class Config const std::string& selected_interface() const { return selected_interface_; } - const dbus::ObjectPath& dbus_path() const { return dbus_path_; } + ConfigAdaptorInterface* adaptor() const { return adaptor_.get(); } private: // Keys used in hostapd config file. @@ -135,26 +145,25 @@ class Config static const int kPassphraseMaxLength; // Append default hostapd configurations to the config file. - bool AppendHostapdDefaults(brillo::ErrorPtr* error, - std::string* config_str); + bool AppendHostapdDefaults(Error* error, std::string* config_str); // Append hardware mode related configurations to the config file. - bool AppendHwMode(brillo::ErrorPtr* error, std::string* config_str); + bool AppendHwMode(Error* error, std::string* config_str); // Determine/append interface configuration to the config file. - bool AppendInterface(brillo::ErrorPtr* error, std::string* config_str); + bool AppendInterface(Error* error, std::string* config_str); // Append security related configurations to the config file. - bool AppendSecurityMode(brillo::ErrorPtr* error, std::string* config_str); + bool AppendSecurityMode(Error* error, std::string* config_str); Manager* manager_; - dbus::ObjectPath dbus_path_; std::string control_interface_; // Interface selected for hostapd. std::string selected_interface_; - std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_; scoped_refptr<Device> device_; + std::unique_ptr<ConfigAdaptorInterface> adaptor_; + DISALLOW_COPY_AND_ASSIGN(Config); }; diff --git a/config_adaptor_interface.h b/config_adaptor_interface.h index a7329d8..2b8c8bc 100644 --- a/config_adaptor_interface.h +++ b/config_adaptor_interface.h @@ -19,12 +19,19 @@ #include <string> +#include "apmanager/rpc_interface.h" + namespace apmanager { class ConfigAdaptorInterface { public: virtual ~ConfigAdaptorInterface() {} + // Returns an identifier/handle that represents this object over + // the IPC interface (e.g. dbus::ObjectPath for D-Bus, IBinder + // for Binder). + virtual RPCObjectIdentifier GetRpcObjectIdentifier() = 0; + // Getter/setter for configuration properties. virtual void SetSsid(const std::string& ssid) = 0; virtual std::string GetSsid() = 0; diff --git a/config_unittest.cc b/config_unittest.cc index 93db179..3bc380b 100644 --- a/config_unittest.cc +++ b/config_unittest.cc @@ -29,6 +29,8 @@ #include "dbus/apmanager/dbus-constants.h" #endif +#include "apmanager/error.h" +#include "apmanager/fake_config_adaptor.h" #include "apmanager/fake_device_adaptor.h" #include "apmanager/mock_control.h" #include "apmanager/mock_device.h" @@ -43,7 +45,6 @@ namespace apmanager { namespace { -const char kServicePath[] = "/manager/services/0"; const char kSsid[] = "TestSsid"; const char kInterface[] = "uap0"; const char kBridgeInterface[] = "br0"; @@ -127,10 +128,14 @@ const char kExpectedRsnConfigContent[] = "ssid=TestSsid\n" class ConfigTest : public testing::Test { public: ConfigTest() - : manager_(&control_interface_), - config_(&manager_, kServicePath) { + : manager_(&control_interface_) { ON_CALL(control_interface_, CreateDeviceAdaptorRaw()) .WillByDefault(ReturnNew<FakeDeviceAdaptor>()); + ON_CALL(control_interface_, CreateConfigAdaptorRaw()) + .WillByDefault(ReturnNew<FakeConfigAdaptor>()); + // Defer creation of Config object to allow ControlInterface to setup + // expectations for generating fake adaptors. + config_.reset(new Config(&manager_, 0)); } void SetupDevice(const std::string& interface) { @@ -141,20 +146,21 @@ class ConfigTest : public testing::Test { .WillRepeatedly(Return(device_)); } + void VerifyError(const Error& error, + Error::Type expected_type, + const std::string& expected_message_start) { + EXPECT_EQ(expected_type, error.type()); + EXPECT_TRUE( + base::StartsWithASCII(error.message(), expected_message_start, false)); + } + protected: MockControl control_interface_; MockManager manager_; scoped_refptr<MockDevice> device_; - Config config_; + std::unique_ptr<Config> config_; }; -MATCHER_P(IsConfigErrorStartingWith, message, "") { - return arg != nullptr && - arg->GetDomain() == brillo::errors::dbus::kDomain && - arg->GetCode() == kConfigError && - base::StartsWithASCII(arg->GetMessage(), message, false); -} - TEST_F(ConfigTest, GetFrequencyFromChannel) { uint32_t frequency; // Invalid channel. @@ -179,256 +185,258 @@ TEST_F(ConfigTest, GetFrequencyFromChannel) { } TEST_F(ConfigTest, ValidateSsid) { - brillo::ErrorPtr error; + Error error; // SSID must contain between 1 and 32 characters. - EXPECT_TRUE(config_.ValidateSsid(&error, "s")); - EXPECT_TRUE(config_.ValidateSsid(&error, std::string(32, 'c'))); - EXPECT_FALSE(config_.ValidateSsid(&error, "")); - EXPECT_FALSE(config_.ValidateSsid(&error, std::string(33, 'c'))); + EXPECT_TRUE(config_->ValidateSsid(&error, "s")); + EXPECT_TRUE(config_->ValidateSsid(&error, std::string(32, 'c'))); + EXPECT_FALSE(config_->ValidateSsid(&error, "")); + EXPECT_FALSE(config_->ValidateSsid(&error, std::string(33, 'c'))); } TEST_F(ConfigTest, ValidateSecurityMode) { - brillo::ErrorPtr error; - EXPECT_TRUE(config_.ValidateSecurityMode(&error, kSecurityModeNone)); - EXPECT_TRUE(config_.ValidateSecurityMode(&error, kSecurityModeRSN)); - EXPECT_FALSE(config_.ValidateSecurityMode(&error, "InvalidSecurityMode")); + Error error; + EXPECT_TRUE(config_->ValidateSecurityMode(&error, kSecurityModeNone)); + EXPECT_TRUE(config_->ValidateSecurityMode(&error, kSecurityModeRSN)); + EXPECT_FALSE(config_->ValidateSecurityMode(&error, "InvalidSecurityMode")); } TEST_F(ConfigTest, ValidatePassphrase) { - brillo::ErrorPtr error; + Error error; // Passpharse must contain between 8 and 63 characters. - EXPECT_TRUE(config_.ValidatePassphrase(&error, std::string(8, 'c'))); - EXPECT_TRUE(config_.ValidatePassphrase(&error, std::string(63, 'c'))); - EXPECT_FALSE(config_.ValidatePassphrase(&error, std::string(7, 'c'))); - EXPECT_FALSE(config_.ValidatePassphrase(&error, std::string(64, 'c'))); + EXPECT_TRUE(config_->ValidatePassphrase(&error, std::string(8, 'c'))); + EXPECT_TRUE(config_->ValidatePassphrase(&error, std::string(63, 'c'))); + EXPECT_FALSE(config_->ValidatePassphrase(&error, std::string(7, 'c'))); + EXPECT_FALSE(config_->ValidatePassphrase(&error, std::string(64, 'c'))); } TEST_F(ConfigTest, ValidateHwMode) { - brillo::ErrorPtr error; - EXPECT_TRUE(config_.ValidateHwMode(&error, kHwMode80211a)); - EXPECT_TRUE(config_.ValidateHwMode(&error, kHwMode80211b)); - EXPECT_TRUE(config_.ValidateHwMode(&error, kHwMode80211g)); - EXPECT_TRUE(config_.ValidateHwMode(&error, kHwMode80211n)); - EXPECT_TRUE(config_.ValidateHwMode(&error, kHwMode80211ac)); - EXPECT_FALSE(config_.ValidateSecurityMode(&error, "InvalidHwMode")); + Error error; + EXPECT_TRUE(config_->ValidateHwMode(&error, kHwMode80211a)); + EXPECT_TRUE(config_->ValidateHwMode(&error, kHwMode80211b)); + EXPECT_TRUE(config_->ValidateHwMode(&error, kHwMode80211g)); + EXPECT_TRUE(config_->ValidateHwMode(&error, kHwMode80211n)); + EXPECT_TRUE(config_->ValidateHwMode(&error, kHwMode80211ac)); + EXPECT_FALSE(config_->ValidateSecurityMode(&error, "InvalidHwMode")); } TEST_F(ConfigTest, ValidateOperationMode) { - brillo::ErrorPtr error; - EXPECT_TRUE(config_.ValidateOperationMode(&error, kOperationModeServer)); - EXPECT_TRUE(config_.ValidateOperationMode(&error, kOperationModeBridge)); - EXPECT_FALSE(config_.ValidateOperationMode(&error, "InvalidMode")); + Error error; + EXPECT_TRUE(config_->ValidateOperationMode(&error, kOperationModeServer)); + EXPECT_TRUE(config_->ValidateOperationMode(&error, kOperationModeBridge)); + EXPECT_FALSE(config_->ValidateOperationMode(&error, "InvalidMode")); } TEST_F(ConfigTest, ValidateChannel) { - brillo::ErrorPtr error; - EXPECT_TRUE(config_.ValidateChannel(&error, 1)); - EXPECT_TRUE(config_.ValidateChannel(&error, 13)); - EXPECT_TRUE(config_.ValidateChannel(&error, 34)); - EXPECT_TRUE(config_.ValidateChannel(&error, 165)); - EXPECT_FALSE(config_.ValidateChannel(&error, 0)); - EXPECT_FALSE(config_.ValidateChannel(&error, 14)); - EXPECT_FALSE(config_.ValidateChannel(&error, 33)); - EXPECT_FALSE(config_.ValidateChannel(&error, 166)); + Error error; + EXPECT_TRUE(config_->ValidateChannel(&error, 1)); + EXPECT_TRUE(config_->ValidateChannel(&error, 13)); + EXPECT_TRUE(config_->ValidateChannel(&error, 34)); + EXPECT_TRUE(config_->ValidateChannel(&error, 165)); + EXPECT_FALSE(config_->ValidateChannel(&error, 0)); + EXPECT_FALSE(config_->ValidateChannel(&error, 14)); + EXPECT_FALSE(config_->ValidateChannel(&error, 33)); + EXPECT_FALSE(config_->ValidateChannel(&error, 166)); } TEST_F(ConfigTest, NoSsid) { - config_.SetChannel(k24GHzChannel); - config_.SetHwMode(kHwMode80211g); - config_.SetInterfaceName(kInterface); + config_->SetChannel(k24GHzChannel); + config_->SetHwMode(kHwMode80211g); + config_->SetInterfaceName(kInterface); std::string config_content; - brillo::ErrorPtr error; - EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content)); - EXPECT_THAT(error, IsConfigErrorStartingWith("SSID not specified")); + Error error; + EXPECT_FALSE(config_->GenerateConfigFile(&error, &config_content)); + VerifyError(error, Error::kInvalidConfiguration, "SSID not specified"); } TEST_F(ConfigTest, NoInterface) { // Basic 80211.g configuration. - config_.SetSsid(kSsid); - config_.SetChannel(k24GHzChannel); - config_.SetHwMode(kHwMode80211g); + config_->SetSsid(kSsid); + config_->SetChannel(k24GHzChannel); + config_->SetHwMode(kHwMode80211g); // No device available, fail to generate config file. - brillo::ErrorPtr error; + Error error; std::string config_content; EXPECT_CALL(manager_, GetAvailableDevice()).WillOnce(Return(nullptr)); - EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content)); - EXPECT_THAT(error, IsConfigErrorStartingWith("No device available")); + EXPECT_FALSE(config_->GenerateConfigFile(&error, &config_content)); + VerifyError(error, Error::kInternalError, "No device available"); Mock::VerifyAndClearExpectations(&manager_); // Device available, config file should be generated without any problem. scoped_refptr<MockDevice> device = new MockDevice(&manager_); device->SetPreferredApInterface(kInterface); - brillo::ErrorPtr error1; + error.Reset(); EXPECT_CALL(manager_, GetAvailableDevice()).WillOnce(Return(device)); - EXPECT_TRUE(config_.GenerateConfigFile(&error1, &config_content)); + EXPECT_TRUE(config_->GenerateConfigFile(&error, &config_content)); EXPECT_NE(std::string::npos, config_content.find( kExpected80211gConfigContent)) << "Expected to find the following config...\n" << kExpected80211gConfigContent << "..within content...\n" << config_content; - EXPECT_EQ(nullptr, error1.get()); + EXPECT_TRUE(error.IsSuccess()); Mock::VerifyAndClearExpectations(&manager_); } TEST_F(ConfigTest, InvalidInterface) { // Basic 80211.g configuration. - config_.SetSsid(kSsid); - config_.SetChannel(k24GHzChannel); - config_.SetHwMode(kHwMode80211g); - config_.SetInterfaceName(kInterface); + config_->SetSsid(kSsid); + config_->SetChannel(k24GHzChannel); + config_->SetHwMode(kHwMode80211g); + config_->SetInterfaceName(kInterface); - // No device available, fail to generate config file. - brillo::ErrorPtr error; + // Unable to find the device, fail to generate config file. + Error error; std::string config_content; EXPECT_CALL(manager_, GetDeviceFromInterfaceName(kInterface)) .WillOnce(Return(nullptr)); - EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content)); - EXPECT_THAT(error, - IsConfigErrorStartingWith( - "Unable to find device for the specified interface")); + EXPECT_FALSE(config_->GenerateConfigFile(&error, &config_content)); + VerifyError(error, + Error::kInvalidConfiguration, + "Unable to find device for the specified interface"); Mock::VerifyAndClearExpectations(&manager_); } TEST_F(ConfigTest, BridgeMode) { - config_.SetSsid(kSsid); - config_.SetChannel(k24GHzChannel); - config_.SetHwMode(kHwMode80211g); - config_.SetInterfaceName(kInterface); - config_.SetOperationMode(kOperationModeBridge); + config_->SetSsid(kSsid); + config_->SetChannel(k24GHzChannel); + config_->SetHwMode(kHwMode80211g); + config_->SetInterfaceName(kInterface); + config_->SetOperationMode(kOperationModeBridge); // Bridge interface required for bridge mode. - brillo::ErrorPtr error; + Error error; std::string config_content; - EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content)); - EXPECT_THAT(error, - IsConfigErrorStartingWith("Bridge interface not specified")); + EXPECT_FALSE(config_->GenerateConfigFile(&error, &config_content)); + VerifyError( + error, Error::kInvalidConfiguration, "Bridge interface not specified"); // Set bridge interface, config file should be generated without error. - config_.SetBridgeInterface(kBridgeInterface); + config_->SetBridgeInterface(kBridgeInterface); // Setup mock device. SetupDevice(kInterface); - brillo::ErrorPtr error1; + error.Reset(); std::string config_content1; - EXPECT_TRUE(config_.GenerateConfigFile(&error1, &config_content1)); + EXPECT_TRUE(config_->GenerateConfigFile(&error, &config_content1)); EXPECT_NE(std::string::npos, config_content1.find( kExpected80211gBridgeConfigContent)) << "Expected to find the following config...\n" << kExpected80211gBridgeConfigContent << "..within content...\n" << config_content1; - EXPECT_EQ(nullptr, error1.get()); + EXPECT_TRUE(error.IsSuccess()); } TEST_F(ConfigTest, 80211gConfig) { - config_.SetSsid(kSsid); - config_.SetChannel(k24GHzChannel); - config_.SetHwMode(kHwMode80211g); - config_.SetInterfaceName(kInterface); + config_->SetSsid(kSsid); + config_->SetChannel(k24GHzChannel); + config_->SetHwMode(kHwMode80211g); + config_->SetInterfaceName(kInterface); // Setup mock device. SetupDevice(kInterface); std::string config_content; - brillo::ErrorPtr error; - EXPECT_TRUE(config_.GenerateConfigFile(&error, &config_content)); + Error error; + EXPECT_TRUE(config_->GenerateConfigFile(&error, &config_content)); EXPECT_NE(std::string::npos, config_content.find( kExpected80211gConfigContent)) << "Expected to find the following config...\n" << kExpected80211gConfigContent << "..within content...\n" << config_content; - EXPECT_EQ(nullptr, error.get()); + EXPECT_TRUE(error.IsSuccess()); } TEST_F(ConfigTest, 80211gConfigWithControlInterface) { - config_.SetSsid(kSsid); - config_.SetChannel(k24GHzChannel); - config_.SetHwMode(kHwMode80211g); - config_.SetInterfaceName(kInterface); - config_.set_control_interface(kControlInterfacePath); + config_->SetSsid(kSsid); + config_->SetChannel(k24GHzChannel); + config_->SetHwMode(kHwMode80211g); + config_->SetInterfaceName(kInterface); + config_->set_control_interface(kControlInterfacePath); // Setup mock device. SetupDevice(kInterface); std::string config_content; - brillo::ErrorPtr error; - EXPECT_TRUE(config_.GenerateConfigFile(&error, &config_content)); + Error error; + EXPECT_TRUE(config_->GenerateConfigFile(&error, &config_content)); EXPECT_NE(std::string::npos, config_content.find( kExpected80211gCtrlIfaceConfigContent)) << "Expected to find the following config...\n" << kExpected80211gCtrlIfaceConfigContent << "..within content...\n" << config_content; - EXPECT_EQ(nullptr, error.get()); + EXPECT_TRUE(error.IsSuccess()); } TEST_F(ConfigTest, 80211nConfig) { - config_.SetSsid(kSsid); - config_.SetHwMode(kHwMode80211n); - config_.SetInterfaceName(kInterface); + config_->SetSsid(kSsid); + config_->SetHwMode(kHwMode80211n); + config_->SetInterfaceName(kInterface); // Setup mock device. SetupDevice(kInterface); // 5GHz channel. - config_.SetChannel(k5GHzChannel); + config_->SetChannel(k5GHzChannel); std::string ghz5_config_content; - brillo::ErrorPtr error; + Error error; std::string ht_capab_5ghz(k5GHzHTCapab); EXPECT_CALL(*device_.get(), GetHTCapability(k5GHzChannel, _)) .WillOnce(DoAll(SetArgumentPointee<1>(ht_capab_5ghz), Return(true))); - EXPECT_TRUE(config_.GenerateConfigFile(&error, &ghz5_config_content)); + EXPECT_TRUE(config_->GenerateConfigFile(&error, &ghz5_config_content)); EXPECT_NE(std::string::npos, ghz5_config_content.find( kExpected80211n5GHzConfigContent)) << "Expected to find the following config...\n" << kExpected80211n5GHzConfigContent << "..within content...\n" << ghz5_config_content; - EXPECT_EQ(nullptr, error.get()); + EXPECT_TRUE(error.IsSuccess()); Mock::VerifyAndClearExpectations(device_.get()); // 2.4GHz channel. - config_.SetChannel(k24GHzChannel); + config_->SetChannel(k24GHzChannel); std::string ghz24_config_content; - brillo::ErrorPtr error1; + error.Reset(); std::string ht_capab_24ghz(k24GHzHTCapab); EXPECT_CALL(*device_.get(), GetHTCapability(k24GHzChannel, _)) .WillOnce(DoAll(SetArgumentPointee<1>(ht_capab_24ghz), Return(true))); - EXPECT_TRUE(config_.GenerateConfigFile(&error1, &ghz24_config_content)); + EXPECT_TRUE(config_->GenerateConfigFile(&error, &ghz24_config_content)); EXPECT_NE(std::string::npos, ghz24_config_content.find( kExpected80211n24GHzConfigContent)) << "Expected to find the following config...\n" << kExpected80211n24GHzConfigContent << "..within content...\n" << ghz24_config_content; - EXPECT_EQ(nullptr, error.get()); + EXPECT_TRUE(error.IsSuccess()); Mock::VerifyAndClearExpectations(device_.get()); } TEST_F(ConfigTest, RsnConfig) { - config_.SetSsid(kSsid); - config_.SetChannel(k24GHzChannel); - config_.SetHwMode(kHwMode80211g); - config_.SetInterfaceName(kInterface); - config_.SetSecurityMode(kSecurityModeRSN); + config_->SetSsid(kSsid); + config_->SetChannel(k24GHzChannel); + config_->SetHwMode(kHwMode80211g); + config_->SetInterfaceName(kInterface); + config_->SetSecurityMode(kSecurityModeRSN); // Setup mock device. SetupDevice(kInterface); // Failed due to no passphrase specified. std::string config_content; - brillo::ErrorPtr error; - EXPECT_FALSE(config_.GenerateConfigFile(&error, &config_content)); - EXPECT_THAT(error, IsConfigErrorStartingWith( + Error error; + EXPECT_FALSE(config_->GenerateConfigFile(&error, &config_content)); + VerifyError( + error, + Error::kInvalidConfiguration, base::StringPrintf("Passphrase not set for security mode: %s", - kSecurityModeRSN))); + kSecurityModeRSN)); - brillo::ErrorPtr error1; - config_.SetPassphrase(kPassphrase); - EXPECT_TRUE(config_.GenerateConfigFile(&error1, &config_content)); + error.Reset(); + config_->SetPassphrase(kPassphrase); + EXPECT_TRUE(config_->GenerateConfigFile(&error, &config_content)); EXPECT_NE(std::string::npos, config_content.find( kExpectedRsnConfigContent)) << "Expected to find the following config...\n" << kExpectedRsnConfigContent << "..within content...\n" << config_content; - EXPECT_EQ(nullptr, error1.get()); + EXPECT_TRUE(error.IsSuccess()); } } // namespace apmanager diff --git a/dbus/config_dbus_adaptor.cc b/dbus/config_dbus_adaptor.cc index c99c8a0..eb93c80 100644 --- a/dbus/config_dbus_adaptor.cc +++ b/dbus/config_dbus_adaptor.cc @@ -26,6 +26,7 @@ #endif // __ANDROID__ #include "apmanager/config.h" +#include "apmanager/error.h" using brillo::dbus_utils::ExportedObjectManager; using brillo::ErrorPtr; @@ -48,43 +49,54 @@ ConfigDBusAdaptor::ConfigDBusAdaptor( dbus_object_(object_manager, bus, dbus_path_), config_(config) { // Register D-Bus object. + RegisterWithDBusObject(&dbus_object_); dbus_object_.RegisterAndBlock(); } ConfigDBusAdaptor::~ConfigDBusAdaptor() {} bool ConfigDBusAdaptor::ValidateSsid(ErrorPtr* error, const string& value) { - // TODO(zqiu): To be implemented. - return true; + Error internal_error; + config_->ValidateSsid(&internal_error, value); + return !internal_error.ToDBusError(error); } bool ConfigDBusAdaptor::ValidateSecurityMode(ErrorPtr* error, const string& value) { - // TODO(zqiu): To be implemented. - return true; + Error internal_error; + config_->ValidateSecurityMode(&internal_error, value); + return !internal_error.ToDBusError(error); } bool ConfigDBusAdaptor::ValidatePassphrase(ErrorPtr* error, const string& value) { - // TODO(zqiu): To be implemented. - return true; + Error internal_error; + config_->ValidatePassphrase(&internal_error, value); + return !internal_error.ToDBusError(error); } bool ConfigDBusAdaptor::ValidateHwMode(ErrorPtr* error, const string& value) { - // TODO(zqiu): To be implemented. - return true; + Error internal_error; + config_->ValidateHwMode(&internal_error, value); + return !internal_error.ToDBusError(error); } bool ConfigDBusAdaptor::ValidateOperationMode(ErrorPtr* error, const string& value) { - // TODO(zqiu): To be implemented. - return true; + Error internal_error; + config_->ValidateOperationMode(&internal_error, value); + return !internal_error.ToDBusError(error); } bool ConfigDBusAdaptor::ValidateChannel(ErrorPtr* error, const uint16_t& value) { - // TODO(zqiu): To be implemented. - return true; + Error internal_error; + config_->ValidateChannel(&internal_error, value); + return !internal_error.ToDBusError(error); +} + +RPCObjectIdentifier ConfigDBusAdaptor::GetRpcObjectIdentifier() { + return dbus_path_; } void ConfigDBusAdaptor::SetSsid(const string& ssid) { diff --git a/dbus/config_dbus_adaptor.h b/dbus/config_dbus_adaptor.h index 08f1637..4d14611 100644 --- a/dbus/config_dbus_adaptor.h +++ b/dbus/config_dbus_adaptor.h @@ -55,6 +55,7 @@ class ConfigDBusAdaptor const uint16_t& value) override; // Implementation of ConfigAdaptorInterface. + RPCObjectIdentifier GetRpcObjectIdentifier() override; void SetSsid(const std::string& ssid) override; std::string GetSsid() override; void SetInterfaceName(const std::string& interface_name) override; diff --git a/fake_config_adaptor.cc b/fake_config_adaptor.cc new file mode 100644 index 0000000..2afe8f8 --- /dev/null +++ b/fake_config_adaptor.cc @@ -0,0 +1,119 @@ +// +// 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_config_adaptor.h" + +using std::string; + +namespace apmanager { + +FakeConfigAdaptor::FakeConfigAdaptor() {} + +FakeConfigAdaptor::~FakeConfigAdaptor() {} + +RPCObjectIdentifier FakeConfigAdaptor::GetRpcObjectIdentifier() { + return RPCObjectIdentifier(); +} + +void FakeConfigAdaptor::SetSsid(const string& ssid) { + ssid_ = ssid; +} + +string FakeConfigAdaptor::GetSsid() { + return ssid_; +} + +void FakeConfigAdaptor::SetInterfaceName(const std::string& interface_name) { + interface_name_ = interface_name; +} + +string FakeConfigAdaptor::GetInterfaceName() { + return interface_name_; +} + +void FakeConfigAdaptor::SetSecurityMode(const std::string& mode) { + security_mode_ = mode; +} + +string FakeConfigAdaptor::GetSecurityMode() { + return security_mode_; +} + +void FakeConfigAdaptor::SetPassphrase(const std::string& passphrase) { + passphrase_ = passphrase; +} + +string FakeConfigAdaptor::GetPassphrase() { + return passphrase_; +} + +void FakeConfigAdaptor::SetHwMode(const std::string& hw_mode) { + hw_mode_ = hw_mode; +} + +string FakeConfigAdaptor::GetHwMode() { + return hw_mode_; +} + +void FakeConfigAdaptor::SetOperationMode(const std::string& op_mode) { + op_mode_ = op_mode; +} + +string FakeConfigAdaptor::GetOperationMode() { + return op_mode_; +} + +void FakeConfigAdaptor::SetChannel(uint16_t channel) { + channel_ = channel; +} + +uint16_t FakeConfigAdaptor::GetChannel() { + return channel_; +} + +void FakeConfigAdaptor::SetHiddenNetwork(bool hidden_network) { + hidden_network_ = hidden_network; +} + +bool FakeConfigAdaptor::GetHiddenNetwork() { + return hidden_network_; +} + +void FakeConfigAdaptor::SetBridgeInterface(const std::string& interface_name) { + bridge_interface_ = interface_name; +} + +string FakeConfigAdaptor::GetBridgeInterface() { + return bridge_interface_; +} + +void FakeConfigAdaptor::SetServerAddressIndex(uint16_t index) { + server_address_index_ = index; +} + +uint16_t FakeConfigAdaptor::GetServerAddressIndex() { + return server_address_index_; +} + +void FakeConfigAdaptor::SetFullDeviceControl(bool full_control) { + full_device_control_ = full_control; +} + +bool FakeConfigAdaptor::GetFullDeviceControl() { + return full_device_control_; +} + +} // namespace apmanager diff --git a/fake_config_adaptor.h b/fake_config_adaptor.h new file mode 100644 index 0000000..0d4aa17 --- /dev/null +++ b/fake_config_adaptor.h @@ -0,0 +1,75 @@ +// +// 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_CONFIG_ADAPTOR_H_ +#define APMANAGER_FAKE_CONFIG_ADAPTOR_H_ + +#include <string> + +#include <base/macros.h> + +#include "apmanager/config_adaptor_interface.h" + +namespace apmanager { + +class FakeConfigAdaptor : public ConfigAdaptorInterface { + public: + FakeConfigAdaptor(); + ~FakeConfigAdaptor() override; + + RPCObjectIdentifier GetRpcObjectIdentifier() override; + void SetSsid(const std::string& ssid) override; + std::string GetSsid() override; + void SetInterfaceName(const std::string& interface_name) override; + std::string GetInterfaceName() override; + void SetSecurityMode(const std::string& security_mode) override; + std::string GetSecurityMode() override; + void SetPassphrase(const std::string& passphrase) override; + std::string GetPassphrase() override; + void SetHwMode(const std::string& hw_mode) override; + std::string GetHwMode() override; + void SetOperationMode(const std::string& op_mode) override; + std::string GetOperationMode() override; + void SetChannel(uint16_t channel) override; + uint16_t GetChannel() override; + void SetHiddenNetwork(bool hidden) override; + bool GetHiddenNetwork() override; + void SetBridgeInterface(const std::string& interface_name) override; + std::string GetBridgeInterface() override; + void SetServerAddressIndex(uint16_t) override; + uint16_t GetServerAddressIndex() override; + void SetFullDeviceControl(bool full_control) override; + bool GetFullDeviceControl() override; + + private: + std::string ssid_; + std::string interface_name_; + std::string security_mode_; + std::string passphrase_; + std::string hw_mode_; + std::string op_mode_; + std::string bridge_interface_; + bool hidden_network_; + bool full_device_control_; + uint16_t channel_; + uint16_t server_address_index_; + + DISALLOW_COPY_AND_ASSIGN(FakeConfigAdaptor); +}; + +} // namespace apmanager + +#endif // APMANAGER_FAKE_CONFIG_ADAPTOR_H_ diff --git a/mock_config.cc b/mock_config.cc index 34747a7..5a81b31 100644 --- a/mock_config.cc +++ b/mock_config.cc @@ -18,7 +18,7 @@ namespace apmanager { -MockConfig::MockConfig() : Config(nullptr, std::string()) {} +MockConfig::MockConfig(Manager* manager) : Config(manager, 0) {} MockConfig::~MockConfig() {} diff --git a/mock_config.h b/mock_config.h index 3bce700..ec657b5 100644 --- a/mock_config.h +++ b/mock_config.h @@ -28,12 +28,11 @@ namespace apmanager { class MockConfig : public Config { public: - MockConfig(); + MockConfig(Manager* manager); ~MockConfig() override; MOCK_METHOD2(GenerateConfigFile, - bool(brillo::ErrorPtr *error, - std::string* config_str)); + bool(Error* error, std::string* config_str)); MOCK_METHOD0(ClaimDevice, bool()); MOCK_METHOD0(ReleaseDevice, bool()); diff --git a/rpc_interface.h b/rpc_interface.h new file mode 100644 index 0000000..b181c9f --- /dev/null +++ b/rpc_interface.h @@ -0,0 +1,29 @@ +// +// 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_RPC_INTERFACE_H_ +#define APMANAGER_RPC_INTERFACE_H_ + +// TODO(zqiu): put this under a compiler flag (e.g. __DBUS__). +#include <dbus/object_path.h> + +namespace apmanager { + +// TODO(zqiu): put this under a compiler flag (e.g. __DBUS__). +typedef dbus::ObjectPath RPCObjectIdentifier; + +} // namespace apmanager + +#endif // APMANAGER_RPC_INTERFACE_H_ @@ -33,6 +33,7 @@ #include "apmanager/event_dispatcher.h" #endif // __BRILLO__ +#include "apmanager/error.h" #include "apmanager/manager.h" using brillo::dbus_utils::AsyncEventSequencer; @@ -80,11 +81,11 @@ Service::Service(Manager* manager, int service_identifier) ManagerAdaptor::GetObjectPath().value().c_str(), service_identifier)), dbus_path_(dbus::ObjectPath(service_path_)), - config_(new Config(manager, service_path_)), + config_(new Config(manager, service_identifier)), dhcp_server_factory_(DHCPServerFactory::GetInstance()), file_writer_(FileWriter::GetInstance()), process_factory_(ProcessFactory::GetInstance()) { - SetConfig(config_->dbus_path()); + SetConfig(config_->adaptor()->GetRpcObjectIdentifier()); SetState(kStateIdle); // TODO(zqiu): come up with better server address management. This is good // enough for now. @@ -115,9 +116,6 @@ void Service::RegisterAsync(ExportedObjectManager* object_manager, RegisterWithDBusObject(dbus_object_.get()); dbus_object_->RegisterAsync( sequencer->GetHandler("Service.RegisterAsync() failed.", true)); - - // Register Config DBus object. - config_->RegisterAsync(object_manager, bus, sequencer); } bool Service::StartInternal(brillo::ErrorPtr* error) { @@ -133,10 +131,10 @@ bool Service::StartInternal(brillo::ErrorPtr* error) { // Generate hostapd configuration content. string config_str; - if (!config_->GenerateConfigFile(error, &config_str)) { - brillo::Error::AddTo( - error, FROM_HERE, brillo::errors::dbus::kDomain, kServiceError, - "Failed to generate config file"); + Error internal_error; + if (!config_->GenerateConfigFile(&internal_error, &config_str)) { + // TODO(zqiu): temporary until D-Bus is decoupled from this class. + internal_error.ToDBusError(error); return false; } diff --git a/service_unittest.cc b/service_unittest.cc index f53c84b..2f571ab 100644 --- a/service_unittest.cc +++ b/service_unittest.cc @@ -30,7 +30,10 @@ #include "dbus/apmanager/dbus-constants.h" #endif // __ANDROID__ +#include "apmanager/error.h" +#include "apmanager/fake_config_adaptor.h" #include "apmanager/mock_config.h" +#include "apmanager/mock_control.h" #include "apmanager/mock_dhcp_server.h" #include "apmanager/mock_dhcp_server_factory.h" #include "apmanager/mock_file_writer.h" @@ -42,6 +45,7 @@ using brillo::ProcessMock; using ::testing::_; using ::testing::Mock; using ::testing::Return; +using ::testing::ReturnNew; using ::testing::SetArgPointee; namespace { @@ -63,40 +67,45 @@ namespace apmanager { class ServiceTest : public testing::Test { public: ServiceTest() - : manager_(nullptr), - hostapd_monitor_(new MockHostapdMonitor()), - service_(&manager_, kServiceIdentifier) {} - + : manager_(&control_interface_), + hostapd_monitor_(new MockHostapdMonitor()) { + ON_CALL(control_interface_, CreateConfigAdaptorRaw()) + .WillByDefault(ReturnNew<FakeConfigAdaptor>()); + // Defer creation of Service object to allow ControlInterface to + // setup expectations for generating fake adaptors. + service_.reset(new Service(&manager_, kServiceIdentifier)); + } virtual void SetUp() { - service_.dhcp_server_factory_ = &dhcp_server_factory_; - service_.file_writer_ = &file_writer_; - service_.process_factory_ = &process_factory_; - service_.hostapd_monitor_.reset(hostapd_monitor_); + service_->dhcp_server_factory_ = &dhcp_server_factory_; + service_->file_writer_ = &file_writer_; + service_->process_factory_ = &process_factory_; + service_->hostapd_monitor_.reset(hostapd_monitor_); } bool StartService(brillo::ErrorPtr* error) { - return service_.StartInternal(error); + return service_->StartInternal(error); } void StartDummyProcess() { - service_.hostapd_process_.reset(new brillo::ProcessImpl); - service_.hostapd_process_->AddArg(kBinSleep); - service_.hostapd_process_->AddArg("12345"); - CHECK(service_.hostapd_process_->Start()); - LOG(INFO) << "DummyProcess: " << service_.hostapd_process_->pid(); + service_->hostapd_process_.reset(new brillo::ProcessImpl); + service_->hostapd_process_->AddArg(kBinSleep); + service_->hostapd_process_->AddArg("12345"); + CHECK(service_->hostapd_process_->Start()); + LOG(INFO) << "DummyProcess: " << service_->hostapd_process_->pid(); } void SetConfig(Config* config) { - service_.config_.reset(config); + service_->config_.reset(config); } protected: + MockControl control_interface_; MockManager manager_; MockDHCPServerFactory dhcp_server_factory_; MockFileWriter file_writer_; MockProcessFactory process_factory_; MockHostapdMonitor* hostapd_monitor_; - Service service_; + std::unique_ptr<Service> service_; }; MATCHER_P(IsServiceErrorStartingWith, message, "") { @@ -115,18 +124,16 @@ TEST_F(ServiceTest, StartWhenServiceAlreadyRunning) { } TEST_F(ServiceTest, StartWhenConfigFileFailed) { - MockConfig* config = new MockConfig(); + MockConfig* config = new MockConfig(&manager_); SetConfig(config); brillo::ErrorPtr error; EXPECT_CALL(*config, GenerateConfigFile(_, _)).WillOnce(Return(false)); EXPECT_FALSE(StartService(&error)); - EXPECT_THAT(error, IsServiceErrorStartingWith( - "Failed to generate config file")); } TEST_F(ServiceTest, StartSuccess) { - MockConfig* config = new MockConfig(); + MockConfig* config = new MockConfig(&manager_); SetConfig(config); // Setup mock DHCP server. @@ -154,7 +161,7 @@ TEST_F(ServiceTest, StartSuccess) { TEST_F(ServiceTest, StopWhenServiceNotRunning) { brillo::ErrorPtr error; - EXPECT_FALSE(service_.Stop(&error)); + EXPECT_FALSE(service_->Stop(&error)); EXPECT_THAT(error, IsServiceErrorStartingWith( "Service is not currently running")); } @@ -162,11 +169,11 @@ TEST_F(ServiceTest, StopWhenServiceNotRunning) { TEST_F(ServiceTest, StopSuccess) { StartDummyProcess(); - MockConfig* config = new MockConfig(); + MockConfig* config = new MockConfig(&manager_); SetConfig(config); brillo::ErrorPtr error; EXPECT_CALL(*config, ReleaseDevice()).Times(1); - EXPECT_TRUE(service_.Stop(&error)); + EXPECT_TRUE(service_->Stop(&error)); Mock::VerifyAndClearExpectations(config); } |