OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/wifi_sync/wifi_config_delegate_chromeos.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/values.h" |
| 10 #include "chromeos/dbus/dbus_thread_manager.h" |
| 11 #include "chromeos/dbus/shill_profile_client.h" |
| 12 #include "chromeos/network/managed_network_configuration_handler.h" |
| 13 #include "chromeos/network/network_handler.h" |
| 14 #include "components/wifi_sync/network_state_helper_chromeos.h" |
| 15 #include "components/wifi_sync/wifi_credential.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace wifi_sync { |
| 19 |
| 20 namespace { |
| 21 const char *kProfilePath = "/profile/1"; |
| 22 const char *kUserHash = "fake-user-hash"; |
| 23 } |
| 24 |
| 25 class WifiConfigDelegateChromeOsTest : public testing::Test { |
| 26 void SetUp() { |
| 27 chromeos::DBusThreadManager::Initialize(); |
| 28 chromeos::NetworkHandler::Initialize(); |
| 29 network_handler_ = chromeos::NetworkHandler::Get(); |
| 30 |
| 31 const base::ListValue policy_network_configs; |
| 32 const base::DictionaryValue policy_global_config; |
| 33 network_handler_ |
| 34 ->managed_network_configuration_handler() |
| 35 ->SetPolicy(onc::ONC_SOURCE_USER_POLICY, |
| 36 kUserHash, |
| 37 policy_network_configs, |
| 38 policy_global_config); |
| 39 |
| 40 chromeos::DBusThreadManager::Get() |
| 41 ->GetShillProfileClient() |
| 42 ->GetTestInterface() |
| 43 ->AddProfile(kProfilePath, kUserHash); |
| 44 |
| 45 config_delegate_.reset( |
| 46 new WifiConfigDelegateChromeOs( |
| 47 kUserHash, |
| 48 network_handler_->managed_network_configuration_handler())); |
| 49 |
| 50 message_loop_.RunUntilIdle(); // Process updates queued by Shill clients. |
| 51 } |
| 52 |
| 53 void TearDown() { |
| 54 // Reset singletons. |
| 55 chromeos::NetworkHandler::Shutdown(); |
| 56 chromeos::DBusThreadManager::Shutdown(); |
| 57 } |
| 58 |
| 59 protected: |
| 60 WifiConfigDelegateChromeOsTest() {} |
| 61 |
| 62 // Wrappers for methods in WifiConfigDelegateChromeOs. Sorted by |
| 63 // the declaration order of the WifiConfigDelegateChromeOs methods |
| 64 // they call. |
| 65 void AddToLocalNetworks(const WifiCredential& network_credential) { |
| 66 config_delegate_->AddToLocalNetworks(network_credential); |
| 67 message_loop_.RunUntilIdle(); // Process updates queued by Shill clients. |
| 68 } |
| 69 |
| 70 // Returns true if |credential_to_find| exists in the set of |
| 71 // credentials configured in Shill. |
| 72 bool CredentialIsConfigured(const WifiCredential& credential_to_find) { |
| 73 auto credentials(GetWifiCredentialsForShillProfile( |
| 74 network_handler_->network_state_handler(), kProfilePath)); |
| 75 for (const auto &credential : credentials) { |
| 76 if (credential.ssid() == credential_to_find.ssid() && |
| 77 credential.security_class() == credential_to_find.security_class() && |
| 78 credential.passphrase() == credential_to_find.passphrase()) { |
| 79 return true; |
| 80 } |
| 81 } |
| 82 return false; |
| 83 } |
| 84 // Returns a new WifiCredential constructed from the given parameters. |
| 85 WifiCredential MakeCredential(const std::string& ssid, |
| 86 const WifiSecurityClass security_class, |
| 87 const std::string& passphrase) { |
| 88 return WifiCredential( |
| 89 WifiCredential::MakeSsidBytes(ssid), security_class, passphrase); |
| 90 } |
| 91 |
| 92 private: |
| 93 base::MessageLoop message_loop_; // For ManagedNetworkConfigurationHandler. |
| 94 scoped_ptr<WifiConfigDelegateChromeOs> config_delegate_; |
| 95 chromeos::NetworkHandler* network_handler_; // Unowned. |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(WifiConfigDelegateChromeOsTest); |
| 98 }; |
| 99 |
| 100 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksOpen) { |
| 101 const auto kCredential(MakeCredential("fake-ssid", SECURITY_CLASS_NONE, "")); |
| 102 EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
| 103 AddToLocalNetworks(kCredential); |
| 104 EXPECT_TRUE(CredentialIsConfigured(kCredential)); |
| 105 } |
| 106 |
| 107 // TODO(quiche): Use a non-empty passphrase, once |
| 108 // GetWifiCredentialsForShillProfile provides passphrases. |
| 109 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksWep) { |
| 110 const auto kCredential(MakeCredential("fake-ssid", SECURITY_CLASS_WEP, "")); |
| 111 EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
| 112 AddToLocalNetworks(kCredential); |
| 113 EXPECT_TRUE(CredentialIsConfigured(kCredential)); |
| 114 } |
| 115 |
| 116 // TODO(quiche): Use a non-empty passphrase, once |
| 117 // GetWifiCredentialsForShillProfile provides passphrases. |
| 118 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksPsk) { |
| 119 const auto kCredential(MakeCredential("fake-ssid", SECURITY_CLASS_PSK, "")); |
| 120 EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
| 121 AddToLocalNetworks(kCredential); |
| 122 EXPECT_TRUE(CredentialIsConfigured(kCredential)); |
| 123 } |
| 124 |
| 125 // TODO(quiche): Use a non-empty passphrase, once |
| 126 // GetWifiCredentialsForShillProfile provides passphrases. |
| 127 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksNonUtf8) { |
| 128 const auto kCredential(MakeCredential("\xc0", SECURITY_CLASS_PSK, "")); |
| 129 EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
| 130 AddToLocalNetworks(kCredential); |
| 131 // TODO(quiche): Change to EXPECT_TRUE, once we support non-UTF-8 SSIDs. |
| 132 EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
| 133 } |
| 134 |
| 135 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksBadSecurityFails) { |
| 136 const auto kCredential( |
| 137 MakeCredential("fake-ssid", SECURITY_CLASS_INVALID, "")); |
| 138 EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
| 139 AddToLocalNetworks(kCredential); |
| 140 EXPECT_FALSE(CredentialIsConfigured(kCredential)); |
| 141 } |
| 142 |
| 143 } // namespace wifi_sync |
OLD | NEW |