diff options
author | quiche <quiche@chromium.org> | 2015-01-16 16:35:22 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-17 00:36:23 +0000 |
commit | 9d40dcfee9aa0ca890d187df2b31365e6c96878f (patch) | |
tree | 09e9754352023e671bdd9e501df32ab13f6197cc | |
parent | 4069d9b15ca7d8744728161df7e70945cfb8cccc (diff) | |
download | chromium_src-9d40dcfee9aa0ca890d187df2b31365e6c96878f.zip chromium_src-9d40dcfee9aa0ca890d187df2b31365e6c96878f.tar.gz chromium_src-9d40dcfee9aa0ca890d187df2b31365e6c96878f.tar.bz2 |
wifi_sync: add WifiConfigDelegate
WifiConfigDelegate provides the ability to modify the platform's
Wi-Fi settings. This CL includes an abstract interface class, and
a concrete implementatin for ChromeOS.
This class will be used by WifiCredentialSyncableService, to
effect the changes requested by Chrome Sync.
BUG=chromium:431435
TEST=components_unittests --gtest_filter="Wifi*"
Review URL: https://codereview.chromium.org/836363002
Cr-Commit-Position: refs/heads/master@{#311991}
-rw-r--r-- | components/components_tests.gyp | 1 | ||||
-rw-r--r-- | components/wifi_sync.gypi | 3 | ||||
-rw-r--r-- | components/wifi_sync/BUILD.gn | 4 | ||||
-rw-r--r-- | components/wifi_sync/DEPS | 1 | ||||
-rw-r--r-- | components/wifi_sync/wifi_config_delegate.h | 27 | ||||
-rw-r--r-- | components/wifi_sync/wifi_config_delegate_chromeos.cc | 61 | ||||
-rw-r--r-- | components/wifi_sync/wifi_config_delegate_chromeos.h | 49 | ||||
-rw-r--r-- | components/wifi_sync/wifi_config_delegate_chromeos_unittest.cc | 229 |
8 files changed, 375 insertions, 0 deletions
diff --git a/components/components_tests.gyp b/components/components_tests.gyp index 969b959..a02d5d2 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -826,6 +826,7 @@ 'sources': [ 'pairing/message_buffer_unittest.cc', 'timers/alarm_timer_unittest.cc', + 'wifi_sync/wifi_config_delegate_chromeos_unittest.cc', 'wifi_sync/wifi_credential_unittest.cc', 'wifi_sync/wifi_security_class_chromeos_unittest.cc', 'wifi_sync/wifi_security_class_unittest.cc', diff --git a/components/wifi_sync.gypi b/components/wifi_sync.gypi index c840af8..ad3d6fd 100644 --- a/components/wifi_sync.gypi +++ b/components/wifi_sync.gypi @@ -16,6 +16,9 @@ 'sources': [ 'wifi_sync/network_state_helper_chromeos.cc', 'wifi_sync/network_state_helper_chromeos.h', + 'wifi_sync/wifi_config_delegate.h', + 'wifi_sync/wifi_config_delegate_chromeos.cc', + 'wifi_sync/wifi_config_delegate_chromeos.h', 'wifi_sync/wifi_credential.cc', 'wifi_sync/wifi_credential.h', 'wifi_sync/wifi_credential_syncable_service.cc', diff --git a/components/wifi_sync/BUILD.gn b/components/wifi_sync/BUILD.gn index 9ca41db..324bc8a 100644 --- a/components/wifi_sync/BUILD.gn +++ b/components/wifi_sync/BUILD.gn @@ -6,6 +6,9 @@ source_set("wifi_sync") { sources = [ "network_state_helper_chromeos.cc", "network_state_helper_chromeos.h", + "wifi_config_delegate.h", + "wifi_config_delegate_chromeos.cc", + "wifi_config_delegate_chromeos.h", "wifi_credential.cc", "wifi_credential.h", "wifi_credential_syncable_service.cc", @@ -34,6 +37,7 @@ source_set("unit_tests") { ] sources = [ + "wifi_config_delegate_chromeos_unittest.cc", "wifi_credential_unittest.cc", "wifi_security_class_chromeos_unittest.cc", "wifi_security_class_unittest.cc", diff --git a/components/wifi_sync/DEPS b/components/wifi_sync/DEPS index 15f1539..c9d0bc0 100644 --- a/components/wifi_sync/DEPS +++ b/components/wifi_sync/DEPS @@ -1,4 +1,5 @@ include_rules = [ + "+chromeos/dbus", "+chromeos/network", "+components/keyed_service/content", "+components/keyed_service/core", diff --git a/components/wifi_sync/wifi_config_delegate.h b/components/wifi_sync/wifi_config_delegate.h new file mode 100644 index 0000000..74a7013 --- /dev/null +++ b/components/wifi_sync/wifi_config_delegate.h @@ -0,0 +1,27 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_WIFI_SYNC_WIFI_CONFIG_DELEGATE_H_ +#define COMPONENTS_WIFI_SYNC_WIFI_CONFIG_DELEGATE_H_ + +namespace wifi_sync { + +class WifiCredential; + +// Interface for platform-specific delegates, which provide the ability +// to configure local WiFi networks settings. +class WifiConfigDelegate { + public: + virtual ~WifiConfigDelegate() {} + + // Adds a local network configuration entry for a WiFi network using + // the properties of |network_credential|. If the entry already + // exists, the entry's passphrase is updated. + virtual void AddToLocalNetworks( + const WifiCredential& network_credential) = 0; +}; + +} // namespace wifi_sync + +#endif // COMPONENTS_WIFI_SYNC_WIFI_CONFIG_DELEGATE_H_ diff --git a/components/wifi_sync/wifi_config_delegate_chromeos.cc b/components/wifi_sync/wifi_config_delegate_chromeos.cc new file mode 100644 index 0000000..d8d241f --- /dev/null +++ b/components/wifi_sync/wifi_config_delegate_chromeos.cc @@ -0,0 +1,61 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/wifi_sync/wifi_config_delegate_chromeos.h" + +#include "base/bind.h" +#include "base/logging.h" +#include "base/memory/scoped_ptr.h" +#include "base/values.h" +#include "chromeos/network/managed_network_configuration_handler.h" +#include "components/wifi_sync/wifi_credential.h" + +namespace wifi_sync { + +namespace { + +void OnCreateConfigurationFailed( + const WifiCredential& wifi_credential, + const std::string& config_handler_error_message, + scoped_ptr<base::DictionaryValue> error_data) { + LOG(ERROR) << "Create configuration failed"; + // TODO(quiche): check if there is a matching network already. If + // so, try to configure it with |wifi_credential|. +} + +} // namespace + +WifiConfigDelegateChromeOs::WifiConfigDelegateChromeOs( + const std::string& user_hash, + chromeos::ManagedNetworkConfigurationHandler* managed_net_config_handler) + : user_hash_(user_hash), + managed_network_configuration_handler_(managed_net_config_handler) { + DCHECK(!user_hash_.empty()); + DCHECK(managed_network_configuration_handler_); +} + +WifiConfigDelegateChromeOs::~WifiConfigDelegateChromeOs() { +} + +void WifiConfigDelegateChromeOs::AddToLocalNetworks( + const WifiCredential& network_credential) { + scoped_ptr<base::DictionaryValue> onc_properties( + network_credential.ToOncProperties()); + // TODO(quiche): Replace with DCHECK, once ONC supports non-UTF-8 SSIDs. + // crbug.com/432546 + if (!onc_properties) { + LOG(ERROR) << "Failed to generate ONC properties for " + << network_credential.ToString(); + return; + } + + managed_network_configuration_handler_ + ->CreateConfiguration( + user_hash_, + *onc_properties, + chromeos::network_handler::StringResultCallback(), + base::Bind(OnCreateConfigurationFailed, network_credential)); +} + +} // namespace wifi_sync diff --git a/components/wifi_sync/wifi_config_delegate_chromeos.h b/components/wifi_sync/wifi_config_delegate_chromeos.h new file mode 100644 index 0000000..94f91d7 --- /dev/null +++ b/components/wifi_sync/wifi_config_delegate_chromeos.h @@ -0,0 +1,49 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_WIFI_SYNC_WIFI_CONFIG_DELEGATE_CHROMEOS_H_ +#define COMPONENTS_WIFI_SYNC_WIFI_CONFIG_DELEGATE_CHROMEOS_H_ + +#include <string> + +#include "base/macros.h" +#include "components/wifi_sync/wifi_config_delegate.h" + +namespace chromeos { +class ManagedNetworkConfigurationHandler; +} + +namespace wifi_sync { + +// ChromeOS-specific implementation of the WifiConfigDelegate interface. +class WifiConfigDelegateChromeOs : public WifiConfigDelegate { + public: + // Constructs a delegate, which is responsible for applying changes + // to the local network configuration. Any changes made by this + // delegate will be applied to the Shill profile that corresponds to + // |user_hash|. The caller must ensure that the + // |managed_net_config_handler| outlives the delegate. + WifiConfigDelegateChromeOs( + const std::string& user_hash, + chromeos::ManagedNetworkConfigurationHandler* managed_net_config_handler); + ~WifiConfigDelegateChromeOs() override; + + // WifiConfigDelegate implementation. + void AddToLocalNetworks(const WifiCredential& network_credential) override; + + private: + // The ChromeOS user-hash that should be used when modifying network + // configurations. + const std::string user_hash_; + + // The object we use to configure ChromeOS network state. + chromeos::ManagedNetworkConfigurationHandler* const + managed_network_configuration_handler_; + + DISALLOW_COPY_AND_ASSIGN(WifiConfigDelegateChromeOs); +}; + +} // namespace wifi_sync + +#endif // COMPONENTS_WIFI_SYNC_WIFI_CONFIG_DELEGATE_CHROMEOS_H_ diff --git a/components/wifi_sync/wifi_config_delegate_chromeos_unittest.cc b/components/wifi_sync/wifi_config_delegate_chromeos_unittest.cc new file mode 100644 index 0000000..e00a99d --- /dev/null +++ b/components/wifi_sync/wifi_config_delegate_chromeos_unittest.cc @@ -0,0 +1,229 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/wifi_sync/wifi_config_delegate_chromeos.h" + +#include "base/logging.h" +#include "base/macros.h" +#include "base/values.h" +#include "chromeos/network/managed_network_configuration_handler.h" +#include "chromeos/network/network_handler_callbacks.h" +#include "components/wifi_sync/wifi_credential.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace wifi_sync { + +namespace { +const char kSsid[] = "fake-ssid"; +const char kSsidNonUtf8[] = "\xc0"; +const char kUserHash[] = "fake-user-hash"; +} + +using chromeos::network_handler::DictionaryResultCallback; +using chromeos::network_handler::ErrorCallback; +using chromeos::network_handler::StringResultCallback; + +class FakeManagedNetworkConfigurationHandler + : public chromeos::ManagedNetworkConfigurationHandler { + public: + FakeManagedNetworkConfigurationHandler() + : create_configuration_called_(false) { + } + + // ManagedNetworkConfigurationHandler implementation. + void AddObserver(chromeos::NetworkPolicyObserver* observer) override { + NOTIMPLEMENTED(); + } + void RemoveObserver(chromeos::NetworkPolicyObserver* observer) override { + NOTIMPLEMENTED(); + } + void GetProperties( + const std::string& service_path, + const DictionaryResultCallback& callback, + const ErrorCallback& error_callback) override { + NOTIMPLEMENTED(); + } + void GetManagedProperties( + const std::string& userhash, + const std::string& service_path, + const DictionaryResultCallback& callback, + const ErrorCallback& error_callback) override { + NOTIMPLEMENTED(); + } + void SetProperties( + const std::string& service_path, + const base::DictionaryValue& user_settings, + const base::Closure& callback, + const ErrorCallback& error_callback) const override { + NOTIMPLEMENTED(); + } + void CreateConfiguration( + const std::string& userhash, + const base::DictionaryValue& properties, + const StringResultCallback& callback, + const ErrorCallback& error_callback) const override { + EXPECT_FALSE(create_configuration_called_); + create_configuration_called_ = true; + create_configuration_success_callback_ = callback; + create_configuration_error_callback_ = error_callback; + } + void RemoveConfiguration( + const std::string& service_path, + const base::Closure& callback, + const ErrorCallback& error_callback) const override { + NOTIMPLEMENTED(); + } + void SetPolicy( + ::onc::ONCSource onc_source, + const std::string& userhash, + const base::ListValue& network_configs_onc, + const base::DictionaryValue& global_network_config) override { + NOTIMPLEMENTED(); + } + bool IsAnyPolicyApplicationRunning() const override { + NOTIMPLEMENTED(); + return false; + } + const base::DictionaryValue* FindPolicyByGUID( + const std::string userhash, + const std::string& guid, + ::onc::ONCSource* onc_source) const override { + NOTIMPLEMENTED(); + return nullptr; + } + const GuidToPolicyMap* GetNetworkConfigsFromPolicy( + const std::string& userhash) const override { + NOTIMPLEMENTED(); + return nullptr; + } + const base::DictionaryValue* GetGlobalConfigFromPolicy( + const std::string& userhash) const override { + NOTIMPLEMENTED(); + return nullptr; + } + const base::DictionaryValue* FindPolicyByGuidAndProfile( + const std::string& guid, + const std::string& profile_path) const override { + NOTIMPLEMENTED(); + return nullptr; + } + + bool create_configuration_called() const { + return create_configuration_called_; + } + const StringResultCallback& create_configuration_success_callback() const { + return create_configuration_success_callback_; + } + const ErrorCallback& create_configuration_error_callback() const { + return create_configuration_error_callback_; + } + + private: + // Whether or not CreateConfiguration has been called on this fake. + mutable bool create_configuration_called_; + // The last |callback| passed to CreateConfiguration. + mutable StringResultCallback create_configuration_success_callback_; + // The last |error_callback| passed to CreateConfiguration. + mutable ErrorCallback create_configuration_error_callback_; +}; + +class WifiConfigDelegateChromeOsTest : public testing::Test { + protected: + WifiConfigDelegateChromeOsTest() + : fake_managed_network_configuration_handler_( + new FakeManagedNetworkConfigurationHandler()) { + config_delegate_.reset( + new WifiConfigDelegateChromeOs( + kUserHash, + fake_managed_network_configuration_handler_.get())); + } + + // Wrapper for WifiConfigDelegateChromeOs::AddToLocalNetworks. + void AddToLocalNetworks(const WifiCredential& network_credential) { + config_delegate_->AddToLocalNetworks(network_credential); + } + + // Returns a new WifiCredential constructed from the given parameters. + WifiCredential MakeCredential(const std::string& ssid, + WifiSecurityClass security_class, + const std::string& passphrase) { + scoped_ptr<WifiCredential> credential = + WifiCredential::Create( + WifiCredential::MakeSsidBytesForTest(ssid), + security_class, + passphrase); + CHECK(credential); + return *credential; + } + + // Runs the last |callback| passed to CreateConfiguration, unless + // that |callback| is null. + void RunCreateConfigurationSuccessCallback() { + const char new_service_path[] = "/service/0"; + const StringResultCallback callback = + fake_managed_network_configuration_handler_ + ->create_configuration_success_callback(); + if (!callback.is_null()) + callback.Run(new_service_path); + } + + // Returns whether or not CreateConfiguration has been called + // on |fake_managed_network_configuration_handler_|. + size_t create_configuration_called() const { + return fake_managed_network_configuration_handler_ + ->create_configuration_called(); + } + + // Returns the last |error_callback| passed to the CreateConfiguration + // method of |fake_managed_network_configuration_handler_|. + const ErrorCallback& create_configuration_error_callback() const { + return fake_managed_network_configuration_handler_ + ->create_configuration_error_callback(); + } + + private: + scoped_ptr<WifiConfigDelegateChromeOs> config_delegate_; + scoped_ptr<FakeManagedNetworkConfigurationHandler> + fake_managed_network_configuration_handler_; + + DISALLOW_COPY_AND_ASSIGN(WifiConfigDelegateChromeOsTest); +}; + +TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksOpen) { + AddToLocalNetworks(MakeCredential(kSsid, SECURITY_CLASS_NONE, "")); + ASSERT_TRUE(create_configuration_called()); + RunCreateConfigurationSuccessCallback(); +} + +TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksWep) { + AddToLocalNetworks(MakeCredential(kSsid, SECURITY_CLASS_WEP, "abcde")); + ASSERT_TRUE(create_configuration_called()); + RunCreateConfigurationSuccessCallback(); +} + +TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksPsk) { + AddToLocalNetworks( + MakeCredential(kSsid, SECURITY_CLASS_PSK, "fake-psk-passphrase")); + ASSERT_TRUE(create_configuration_called()); + RunCreateConfigurationSuccessCallback(); +} + +TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksNonUtf8) { + AddToLocalNetworks(MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, "")); + // TODO(quiche): Change to EXPECT_TRUE, once we support non-UTF-8 SSIDs. + EXPECT_FALSE(create_configuration_called()); +} + +TEST_F(WifiConfigDelegateChromeOsTest, + AddToLocalNetworksCreateConfigurationFailure) { + AddToLocalNetworks(MakeCredential(kSsid, SECURITY_CLASS_NONE, "")); + EXPECT_TRUE(create_configuration_called()); + if (!create_configuration_error_callback().is_null()) { + create_configuration_error_callback().Run( + "Config.CreateConfiguration Failed", + make_scoped_ptr(new base::DictionaryValue())); + } +} + +} // namespace wifi_sync |