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
& userhash
,
43 const std::string
& service_path
,
44 const DictionaryResultCallback
& callback
,
45 const ErrorCallback
& error_callback
) override
{
48 void GetManagedProperties(
49 const std::string
& userhash
,
50 const std::string
& service_path
,
51 const DictionaryResultCallback
& callback
,
52 const ErrorCallback
& error_callback
) override
{
56 const std::string
& service_path
,
57 const base::DictionaryValue
& user_settings
,
58 const base::Closure
& callback
,
59 const ErrorCallback
& error_callback
) override
{
62 void CreateConfiguration(
63 const std::string
& userhash
,
64 const base::DictionaryValue
& properties
,
65 const StringResultCallback
& callback
,
66 const ErrorCallback
& error_callback
) const override
{
67 EXPECT_FALSE(create_configuration_called_
);
68 create_configuration_called_
= true;
69 create_configuration_success_callback_
= callback
;
70 create_configuration_error_callback_
= error_callback
;
72 void RemoveConfiguration(
73 const std::string
& service_path
,
74 const base::Closure
& callback
,
75 const ErrorCallback
& error_callback
) const override
{
79 ::onc::ONCSource onc_source
,
80 const std::string
& userhash
,
81 const base::ListValue
& network_configs_onc
,
82 const base::DictionaryValue
& global_network_config
) override
{
85 bool IsAnyPolicyApplicationRunning() const override
{
89 const base::DictionaryValue
* FindPolicyByGUID(
90 const std::string userhash
,
91 const std::string
& guid
,
92 ::onc::ONCSource
* onc_source
) const override
{
96 const GuidToPolicyMap
* GetNetworkConfigsFromPolicy(
97 const std::string
& userhash
) const override
{
101 const base::DictionaryValue
* GetGlobalConfigFromPolicy(
102 const std::string
& userhash
) const override
{
106 const base::DictionaryValue
* FindPolicyByGuidAndProfile(
107 const std::string
& guid
,
108 const std::string
& profile_path
) const override
{
113 bool create_configuration_called() const {
114 return create_configuration_called_
;
116 const StringResultCallback
& create_configuration_success_callback() const {
117 return create_configuration_success_callback_
;
119 const ErrorCallback
& create_configuration_error_callback() const {
120 return create_configuration_error_callback_
;
124 // Whether or not CreateConfiguration has been called on this fake.
125 mutable bool create_configuration_called_
;
126 // The last |callback| passed to CreateConfiguration.
127 mutable StringResultCallback create_configuration_success_callback_
;
128 // The last |error_callback| passed to CreateConfiguration.
129 mutable ErrorCallback create_configuration_error_callback_
;
132 class WifiConfigDelegateChromeOsTest
: public testing::Test
{
134 WifiConfigDelegateChromeOsTest()
135 : fake_managed_network_configuration_handler_(
136 new FakeManagedNetworkConfigurationHandler()) {
137 config_delegate_
.reset(
138 new WifiConfigDelegateChromeOs(
140 fake_managed_network_configuration_handler_
.get()));
143 // Wrapper for WifiConfigDelegateChromeOs::AddToLocalNetworks.
144 void AddToLocalNetworks(const WifiCredential
& network_credential
) {
145 config_delegate_
->AddToLocalNetworks(network_credential
);
148 // Returns a new WifiCredential constructed from the given parameters.
149 WifiCredential
MakeCredential(const std::string
& ssid
,
150 WifiSecurityClass security_class
,
151 const std::string
& passphrase
) {
152 scoped_ptr
<WifiCredential
> credential
=
153 WifiCredential::Create(
154 WifiCredential::MakeSsidBytesForTest(ssid
),
161 // Runs the last |callback| passed to CreateConfiguration, unless
162 // that |callback| is null.
163 void RunCreateConfigurationSuccessCallback() {
164 const char new_service_path
[] = "/service/0";
165 const StringResultCallback callback
=
166 fake_managed_network_configuration_handler_
167 ->create_configuration_success_callback();
168 if (!callback
.is_null())
169 callback
.Run(new_service_path
);
172 // Returns whether or not CreateConfiguration has been called
173 // on |fake_managed_network_configuration_handler_|.
174 size_t create_configuration_called() const {
175 return fake_managed_network_configuration_handler_
176 ->create_configuration_called();
179 // Returns the last |error_callback| passed to the CreateConfiguration
180 // method of |fake_managed_network_configuration_handler_|.
181 const ErrorCallback
& create_configuration_error_callback() const {
182 return fake_managed_network_configuration_handler_
183 ->create_configuration_error_callback();
187 scoped_ptr
<WifiConfigDelegateChromeOs
> config_delegate_
;
188 scoped_ptr
<FakeManagedNetworkConfigurationHandler
>
189 fake_managed_network_configuration_handler_
;
191 DISALLOW_COPY_AND_ASSIGN(WifiConfigDelegateChromeOsTest
);
194 TEST_F(WifiConfigDelegateChromeOsTest
, AddToLocalNetworksOpen
) {
195 AddToLocalNetworks(MakeCredential(kSsid
, SECURITY_CLASS_NONE
, ""));
196 ASSERT_TRUE(create_configuration_called());
197 RunCreateConfigurationSuccessCallback();
200 TEST_F(WifiConfigDelegateChromeOsTest
, AddToLocalNetworksWep
) {
201 AddToLocalNetworks(MakeCredential(kSsid
, SECURITY_CLASS_WEP
, "abcde"));
202 ASSERT_TRUE(create_configuration_called());
203 RunCreateConfigurationSuccessCallback();
206 TEST_F(WifiConfigDelegateChromeOsTest
, AddToLocalNetworksPsk
) {
208 MakeCredential(kSsid
, SECURITY_CLASS_PSK
, "fake-psk-passphrase"));
209 ASSERT_TRUE(create_configuration_called());
210 RunCreateConfigurationSuccessCallback();
213 TEST_F(WifiConfigDelegateChromeOsTest
, AddToLocalNetworksNonUtf8
) {
214 AddToLocalNetworks(MakeCredential(kSsidNonUtf8
, SECURITY_CLASS_PSK
, ""));
215 // TODO(quiche): Change to EXPECT_TRUE, once we support non-UTF-8 SSIDs.
216 EXPECT_FALSE(create_configuration_called());
219 TEST_F(WifiConfigDelegateChromeOsTest
,
220 AddToLocalNetworksCreateConfigurationFailure
) {
221 AddToLocalNetworks(MakeCredential(kSsid
, SECURITY_CLASS_NONE
, ""));
222 EXPECT_TRUE(create_configuration_called());
223 if (!create_configuration_error_callback().is_null()) {
224 create_configuration_error_callback().Run(
225 "Config.CreateConfiguration Failed",
226 make_scoped_ptr(new base::DictionaryValue()));
230 } // namespace wifi_sync