1 // Copyright 2015 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.
5 #include "components/wifi_sync/wifi_config_delegate_chromeos.h"
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "base/values.h"
10 #include "chromeos/network/managed_network_configuration_handler.h"
11 #include "chromeos/network/network_handler_callbacks.h"
12 #include "components/wifi_sync/wifi_credential.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 const char kSsid
[] = "fake-ssid";
19 const char kSsidNonUtf8
[] = "\xc0";
20 const char kUserHash
[] = "fake-user-hash";
23 using chromeos::network_handler::DictionaryResultCallback
;
24 using chromeos::network_handler::ErrorCallback
;
25 using chromeos::network_handler::StringResultCallback
;
27 class FakeManagedNetworkConfigurationHandler
28 : public chromeos::ManagedNetworkConfigurationHandler
{
30 FakeManagedNetworkConfigurationHandler()
31 : create_configuration_called_(false) {
34 // ManagedNetworkConfigurationHandler implementation.
35 void AddObserver(chromeos::NetworkPolicyObserver
* observer
) override
{
38 void RemoveObserver(chromeos::NetworkPolicyObserver
* observer
) override
{
42 const std::string
& service_path
,
43 const DictionaryResultCallback
& callback
,
44 const ErrorCallback
& error_callback
) override
{
47 void GetManagedProperties(
48 const std::string
& userhash
,
49 const std::string
& service_path
,
50 const DictionaryResultCallback
& callback
,
51 const ErrorCallback
& error_callback
) override
{
55 const std::string
& service_path
,
56 const base::DictionaryValue
& user_settings
,
57 const base::Closure
& callback
,
58 const ErrorCallback
& error_callback
) override
{
61 void CreateConfiguration(
62 const std::string
& userhash
,
63 const base::DictionaryValue
& properties
,
64 const StringResultCallback
& callback
,
65 const ErrorCallback
& error_callback
) const override
{
66 EXPECT_FALSE(create_configuration_called_
);
67 create_configuration_called_
= true;
68 create_configuration_success_callback_
= callback
;
69 create_configuration_error_callback_
= error_callback
;
71 void RemoveConfiguration(
72 const std::string
& service_path
,
73 const base::Closure
& callback
,
74 const ErrorCallback
& error_callback
) const override
{
78 ::onc::ONCSource onc_source
,
79 const std::string
& userhash
,
80 const base::ListValue
& network_configs_onc
,
81 const base::DictionaryValue
& global_network_config
) override
{
84 bool IsAnyPolicyApplicationRunning() const override
{
88 const base::DictionaryValue
* FindPolicyByGUID(
89 const std::string userhash
,
90 const std::string
& guid
,
91 ::onc::ONCSource
* onc_source
) const override
{
95 const GuidToPolicyMap
* GetNetworkConfigsFromPolicy(
96 const std::string
& userhash
) const override
{
100 const base::DictionaryValue
* GetGlobalConfigFromPolicy(
101 const std::string
& userhash
) const override
{
105 const base::DictionaryValue
* FindPolicyByGuidAndProfile(
106 const std::string
& guid
,
107 const std::string
& profile_path
) const override
{
112 bool create_configuration_called() const {
113 return create_configuration_called_
;
115 const StringResultCallback
& create_configuration_success_callback() const {
116 return create_configuration_success_callback_
;
118 const ErrorCallback
& create_configuration_error_callback() const {
119 return create_configuration_error_callback_
;
123 // Whether or not CreateConfiguration has been called on this fake.
124 mutable bool create_configuration_called_
;
125 // The last |callback| passed to CreateConfiguration.
126 mutable StringResultCallback create_configuration_success_callback_
;
127 // The last |error_callback| passed to CreateConfiguration.
128 mutable ErrorCallback create_configuration_error_callback_
;
131 class WifiConfigDelegateChromeOsTest
: public testing::Test
{
133 WifiConfigDelegateChromeOsTest()
134 : fake_managed_network_configuration_handler_(
135 new FakeManagedNetworkConfigurationHandler()) {
136 config_delegate_
.reset(
137 new WifiConfigDelegateChromeOs(
139 fake_managed_network_configuration_handler_
.get()));
142 // Wrapper for WifiConfigDelegateChromeOs::AddToLocalNetworks.
143 void AddToLocalNetworks(const WifiCredential
& network_credential
) {
144 config_delegate_
->AddToLocalNetworks(network_credential
);
147 // Returns a new WifiCredential constructed from the given parameters.
148 WifiCredential
MakeCredential(const std::string
& ssid
,
149 WifiSecurityClass security_class
,
150 const std::string
& passphrase
) {
151 scoped_ptr
<WifiCredential
> credential
=
152 WifiCredential::Create(
153 WifiCredential::MakeSsidBytesForTest(ssid
),
160 // Runs the last |callback| passed to CreateConfiguration, unless
161 // that |callback| is null.
162 void RunCreateConfigurationSuccessCallback() {
163 const char new_service_path
[] = "/service/0";
164 const StringResultCallback callback
=
165 fake_managed_network_configuration_handler_
166 ->create_configuration_success_callback();
167 if (!callback
.is_null())
168 callback
.Run(new_service_path
);
171 // Returns whether or not CreateConfiguration has been called
172 // on |fake_managed_network_configuration_handler_|.
173 size_t create_configuration_called() const {
174 return fake_managed_network_configuration_handler_
175 ->create_configuration_called();
178 // Returns the last |error_callback| passed to the CreateConfiguration
179 // method of |fake_managed_network_configuration_handler_|.
180 const ErrorCallback
& create_configuration_error_callback() const {
181 return fake_managed_network_configuration_handler_
182 ->create_configuration_error_callback();
186 scoped_ptr
<WifiConfigDelegateChromeOs
> config_delegate_
;
187 scoped_ptr
<FakeManagedNetworkConfigurationHandler
>
188 fake_managed_network_configuration_handler_
;
190 DISALLOW_COPY_AND_ASSIGN(WifiConfigDelegateChromeOsTest
);
193 TEST_F(WifiConfigDelegateChromeOsTest
, AddToLocalNetworksOpen
) {
194 AddToLocalNetworks(MakeCredential(kSsid
, SECURITY_CLASS_NONE
, ""));
195 ASSERT_TRUE(create_configuration_called());
196 RunCreateConfigurationSuccessCallback();
199 TEST_F(WifiConfigDelegateChromeOsTest
, AddToLocalNetworksWep
) {
200 AddToLocalNetworks(MakeCredential(kSsid
, SECURITY_CLASS_WEP
, "abcde"));
201 ASSERT_TRUE(create_configuration_called());
202 RunCreateConfigurationSuccessCallback();
205 TEST_F(WifiConfigDelegateChromeOsTest
, AddToLocalNetworksPsk
) {
207 MakeCredential(kSsid
, SECURITY_CLASS_PSK
, "fake-psk-passphrase"));
208 ASSERT_TRUE(create_configuration_called());
209 RunCreateConfigurationSuccessCallback();
212 TEST_F(WifiConfigDelegateChromeOsTest
, AddToLocalNetworksNonUtf8
) {
213 AddToLocalNetworks(MakeCredential(kSsidNonUtf8
, SECURITY_CLASS_PSK
, ""));
214 // TODO(quiche): Change to EXPECT_TRUE, once we support non-UTF-8 SSIDs.
215 EXPECT_FALSE(create_configuration_called());
218 TEST_F(WifiConfigDelegateChromeOsTest
,
219 AddToLocalNetworksCreateConfigurationFailure
) {
220 AddToLocalNetworks(MakeCredential(kSsid
, SECURITY_CLASS_NONE
, ""));
221 EXPECT_TRUE(create_configuration_called());
222 if (!create_configuration_error_callback().is_null()) {
223 create_configuration_error_callback().Run(
224 "Config.CreateConfiguration Failed",
225 make_scoped_ptr(new base::DictionaryValue()));
229 } // namespace wifi_sync