Index: components/wifi_sync/wifi_config_delegate_chromeos_unittest.cc |
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 0000000000000000000000000000000000000000..f286650ca92566b689d4672e59c2e8cebb7bfecf |
--- /dev/null |
+++ b/components/wifi_sync/wifi_config_delegate_chromeos_unittest.cc |
@@ -0,0 +1,143 @@ |
+// Copyright 2014 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/macros.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/values.h" |
+#include "chromeos/dbus/dbus_thread_manager.h" |
+#include "chromeos/dbus/shill_profile_client.h" |
+#include "chromeos/network/managed_network_configuration_handler.h" |
+#include "chromeos/network/network_handler.h" |
+#include "components/wifi_sync/network_state_helper_chromeos.h" |
+#include "components/wifi_sync/wifi_credential.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace wifi_sync { |
+ |
+namespace { |
+const char *kProfilePath = "/profile/1"; |
+const char *kUserHash = "fake-user-hash"; |
+} |
+ |
+class WifiConfigDelegateChromeOsTest : public testing::Test { |
+ void SetUp() { |
+ chromeos::DBusThreadManager::Initialize(); |
+ chromeos::NetworkHandler::Initialize(); |
+ network_handler_ = chromeos::NetworkHandler::Get(); |
+ |
+ const base::ListValue policy_network_configs; |
+ const base::DictionaryValue policy_global_config; |
+ network_handler_ |
+ ->managed_network_configuration_handler() |
+ ->SetPolicy(onc::ONC_SOURCE_USER_POLICY, |
+ kUserHash, |
+ policy_network_configs, |
+ policy_global_config); |
+ |
+ chromeos::DBusThreadManager::Get() |
+ ->GetShillProfileClient() |
+ ->GetTestInterface() |
+ ->AddProfile(kProfilePath, kUserHash); |
+ |
+ config_delegate_.reset( |
+ new WifiConfigDelegateChromeOs( |
+ kUserHash, |
+ network_handler_->managed_network_configuration_handler())); |
+ |
+ message_loop_.RunUntilIdle(); // Process updates queued by Shill clients. |
+ } |
+ |
+ void TearDown() { |
+ // Reset singletons. |
+ chromeos::NetworkHandler::Shutdown(); |
+ chromeos::DBusThreadManager::Shutdown(); |
+ } |
+ |
+ protected: |
+ WifiConfigDelegateChromeOsTest() {} |
+ |
+ // Wrappers for methods in WifiConfigDelegateChromeOs. Sorted by |
+ // the declaration order of the WifiConfigDelegateChromeOs methods |
+ // they call. |
+ void AddToLocalNetworks(const WifiCredential& network_credential) { |
+ config_delegate_->AddToLocalNetworks(network_credential); |
+ message_loop_.RunUntilIdle(); // Process updates queued by Shill clients. |
+ } |
+ |
+ // Returns true if |credential_to_find| exists in the set of |
+ // credentials configured in Shill. |
+ bool CredentialIsConfigured(const WifiCredential& credential_to_find) { |
+ auto credentials(GetWifiCredentialsForShillProfile( |
+ network_handler_->network_state_handler(), kProfilePath)); |
+ for (const auto &credential : credentials) { |
+ if (credential.ssid() == credential_to_find.ssid() && |
+ credential.security_class() == credential_to_find.security_class() && |
+ credential.passphrase() == credential_to_find.passphrase()) { |
+ return true; |
+ } |
+ } |
+ return false; |
+ } |
+ // Returns a new WifiCredential constructed from the given parameters. |
+ WifiCredential MakeCredential(const std::string& ssid, |
+ const WifiSecurityClass security_class, |
+ const std::string& passphrase) { |
+ return WifiCredential( |
+ WifiCredential::MakeSsidBytes(ssid), security_class, passphrase); |
+ } |
+ |
+ private: |
+ base::MessageLoop message_loop_; // For ManagedNetworkConfigurationHandler. |
+ scoped_ptr<WifiConfigDelegateChromeOs> config_delegate_; |
+ chromeos::NetworkHandler* network_handler_; // Unowned. |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WifiConfigDelegateChromeOsTest); |
+}; |
+ |
+TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksOpen) { |
+ const auto kCredential(MakeCredential("fake-ssid", SECURITY_CLASS_NONE, "")); |
+ EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
+ AddToLocalNetworks(kCredential); |
+ EXPECT_TRUE(CredentialIsConfigured(kCredential)); |
+} |
+ |
+// TODO(quiche): Use a non-empty passphrase, once |
+// GetWifiCredentialsForShillProfile provides passphrases. |
+TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksWep) { |
+ const auto kCredential(MakeCredential("fake-ssid", SECURITY_CLASS_WEP, "")); |
+ EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
+ AddToLocalNetworks(kCredential); |
+ EXPECT_TRUE(CredentialIsConfigured(kCredential)); |
+} |
+ |
+// TODO(quiche): Use a non-empty passphrase, once |
+// GetWifiCredentialsForShillProfile provides passphrases. |
+TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksPsk) { |
+ const auto kCredential(MakeCredential("fake-ssid", SECURITY_CLASS_PSK, "")); |
+ EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
+ AddToLocalNetworks(kCredential); |
+ EXPECT_TRUE(CredentialIsConfigured(kCredential)); |
+} |
+ |
+// TODO(quiche): Use a non-empty passphrase, once |
+// GetWifiCredentialsForShillProfile provides passphrases. |
+TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksNonUtf8) { |
+ const auto kCredential(MakeCredential("\xc0", SECURITY_CLASS_PSK, "")); |
+ EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
+ AddToLocalNetworks(kCredential); |
+ // TODO(quiche): Change to EXPECT_TRUE, once we support non-UTF-8 SSIDs. |
+ EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
+} |
+ |
+TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksBadSecurityFails) { |
+ const auto kCredential( |
+ MakeCredential("fake-ssid", SECURITY_CLASS_INVALID, "")); |
+ EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
+ AddToLocalNetworks(kCredential); |
+ EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
+} |
+ |
+} // namespace wifi_sync |