OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/wifi_sync/wifi_config_delegate_chromeos.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <memory> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/values.h" | |
15 #include "chromeos/network/managed_network_configuration_handler.h" | |
16 #include "chromeos/network/network_handler_callbacks.h" | |
17 #include "components/wifi_sync/wifi_credential.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace wifi_sync { | |
21 | |
22 namespace { | |
23 const char kSsid[] = "fake-ssid"; | |
24 const char kSsidNonUtf8[] = "\xc0"; | |
25 const char kUserHash[] = "fake-user-hash"; | |
26 } | |
27 | |
28 using chromeos::network_handler::DictionaryResultCallback; | |
29 using chromeos::network_handler::ErrorCallback; | |
30 using chromeos::network_handler::ServiceResultCallback; | |
31 | |
32 class FakeManagedNetworkConfigurationHandler | |
33 : public chromeos::ManagedNetworkConfigurationHandler { | |
34 public: | |
35 FakeManagedNetworkConfigurationHandler() | |
36 : create_configuration_called_(false) { | |
37 } | |
38 | |
39 // ManagedNetworkConfigurationHandler implementation. | |
40 void AddObserver(chromeos::NetworkPolicyObserver* observer) override { | |
41 NOTIMPLEMENTED(); | |
42 } | |
43 void RemoveObserver(chromeos::NetworkPolicyObserver* observer) override { | |
44 NOTIMPLEMENTED(); | |
45 } | |
46 void GetProperties( | |
47 const std::string& userhash, | |
48 const std::string& service_path, | |
49 const DictionaryResultCallback& callback, | |
50 const ErrorCallback& error_callback) override { | |
51 NOTIMPLEMENTED(); | |
52 } | |
53 void GetManagedProperties( | |
54 const std::string& userhash, | |
55 const std::string& service_path, | |
56 const DictionaryResultCallback& callback, | |
57 const ErrorCallback& error_callback) override { | |
58 NOTIMPLEMENTED(); | |
59 } | |
60 void SetProperties( | |
61 const std::string& service_path, | |
62 const base::DictionaryValue& user_settings, | |
63 const base::Closure& callback, | |
64 const ErrorCallback& error_callback) override { | |
65 NOTIMPLEMENTED(); | |
66 } | |
67 void CreateConfiguration(const std::string& userhash, | |
68 const base::DictionaryValue& properties, | |
69 const ServiceResultCallback& callback, | |
70 const ErrorCallback& error_callback) const override { | |
71 EXPECT_FALSE(create_configuration_called_); | |
72 create_configuration_called_ = true; | |
73 create_configuration_success_callback_ = callback; | |
74 create_configuration_error_callback_ = error_callback; | |
75 } | |
76 void RemoveConfiguration( | |
77 const std::string& service_path, | |
78 const base::Closure& callback, | |
79 const ErrorCallback& error_callback) const override { | |
80 NOTIMPLEMENTED(); | |
81 } | |
82 void SetPolicy( | |
83 ::onc::ONCSource onc_source, | |
84 const std::string& userhash, | |
85 const base::ListValue& network_configs_onc, | |
86 const base::DictionaryValue& global_network_config) override { | |
87 NOTIMPLEMENTED(); | |
88 } | |
89 bool IsAnyPolicyApplicationRunning() const override { | |
90 NOTIMPLEMENTED(); | |
91 return false; | |
92 } | |
93 const base::DictionaryValue* FindPolicyByGUID( | |
94 const std::string userhash, | |
95 const std::string& guid, | |
96 ::onc::ONCSource* onc_source) const override { | |
97 NOTIMPLEMENTED(); | |
98 return nullptr; | |
99 } | |
100 const GuidToPolicyMap* GetNetworkConfigsFromPolicy( | |
101 const std::string& userhash) const override { | |
102 NOTIMPLEMENTED(); | |
103 return nullptr; | |
104 } | |
105 const base::DictionaryValue* GetGlobalConfigFromPolicy( | |
106 const std::string& userhash) const override { | |
107 NOTIMPLEMENTED(); | |
108 return nullptr; | |
109 } | |
110 const base::DictionaryValue* FindPolicyByGuidAndProfile( | |
111 const std::string& guid, | |
112 const std::string& profile_path) const override { | |
113 NOTIMPLEMENTED(); | |
114 return nullptr; | |
115 } | |
116 | |
117 bool create_configuration_called() const { | |
118 return create_configuration_called_; | |
119 } | |
120 const ServiceResultCallback& create_configuration_success_callback() const { | |
121 return create_configuration_success_callback_; | |
122 } | |
123 const ErrorCallback& create_configuration_error_callback() const { | |
124 return create_configuration_error_callback_; | |
125 } | |
126 | |
127 private: | |
128 // Whether or not CreateConfiguration has been called on this fake. | |
129 mutable bool create_configuration_called_; | |
130 // The last |callback| passed to CreateConfiguration. | |
131 mutable ServiceResultCallback create_configuration_success_callback_; | |
132 // The last |error_callback| passed to CreateConfiguration. | |
133 mutable ErrorCallback create_configuration_error_callback_; | |
134 }; | |
135 | |
136 class WifiConfigDelegateChromeOsTest : public testing::Test { | |
137 protected: | |
138 WifiConfigDelegateChromeOsTest() | |
139 : fake_managed_network_configuration_handler_( | |
140 new FakeManagedNetworkConfigurationHandler()) { | |
141 config_delegate_.reset( | |
142 new WifiConfigDelegateChromeOs( | |
143 kUserHash, | |
144 fake_managed_network_configuration_handler_.get())); | |
145 } | |
146 | |
147 // Wrapper for WifiConfigDelegateChromeOs::AddToLocalNetworks. | |
148 void AddToLocalNetworks(const WifiCredential& network_credential) { | |
149 config_delegate_->AddToLocalNetworks(network_credential); | |
150 } | |
151 | |
152 // Returns a new WifiCredential constructed from the given parameters. | |
153 WifiCredential MakeCredential(const std::string& ssid, | |
154 WifiSecurityClass security_class, | |
155 const std::string& passphrase) { | |
156 std::unique_ptr<WifiCredential> credential = WifiCredential::Create( | |
157 WifiCredential::MakeSsidBytesForTest(ssid), security_class, passphrase); | |
158 CHECK(credential); | |
159 return *credential; | |
160 } | |
161 | |
162 // Runs the last |callback| passed to CreateConfiguration, unless | |
163 // that |callback| is null. | |
164 void RunCreateConfigurationSuccessCallback() { | |
165 const char new_service_path[] = "/service/0"; | |
166 const ServiceResultCallback callback = | |
167 fake_managed_network_configuration_handler_ | |
168 ->create_configuration_success_callback(); | |
169 if (!callback.is_null()) | |
170 callback.Run(new_service_path, nullptr); | |
171 } | |
172 | |
173 // Returns whether or not CreateConfiguration has been called | |
174 // on |fake_managed_network_configuration_handler_|. | |
175 size_t create_configuration_called() const { | |
176 return fake_managed_network_configuration_handler_ | |
177 ->create_configuration_called(); | |
178 } | |
179 | |
180 // Returns the last |error_callback| passed to the CreateConfiguration | |
181 // method of |fake_managed_network_configuration_handler_|. | |
182 const ErrorCallback& create_configuration_error_callback() const { | |
183 return fake_managed_network_configuration_handler_ | |
184 ->create_configuration_error_callback(); | |
185 } | |
186 | |
187 private: | |
188 std::unique_ptr<WifiConfigDelegateChromeOs> config_delegate_; | |
189 std::unique_ptr<FakeManagedNetworkConfigurationHandler> | |
190 fake_managed_network_configuration_handler_; | |
191 | |
192 DISALLOW_COPY_AND_ASSIGN(WifiConfigDelegateChromeOsTest); | |
193 }; | |
194 | |
195 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksOpen) { | |
196 AddToLocalNetworks(MakeCredential(kSsid, SECURITY_CLASS_NONE, "")); | |
197 ASSERT_TRUE(create_configuration_called()); | |
198 RunCreateConfigurationSuccessCallback(); | |
199 } | |
200 | |
201 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksWep) { | |
202 AddToLocalNetworks(MakeCredential(kSsid, SECURITY_CLASS_WEP, "abcde")); | |
203 ASSERT_TRUE(create_configuration_called()); | |
204 RunCreateConfigurationSuccessCallback(); | |
205 } | |
206 | |
207 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksPsk) { | |
208 AddToLocalNetworks( | |
209 MakeCredential(kSsid, SECURITY_CLASS_PSK, "fake-psk-passphrase")); | |
210 ASSERT_TRUE(create_configuration_called()); | |
211 RunCreateConfigurationSuccessCallback(); | |
212 } | |
213 | |
214 TEST_F(WifiConfigDelegateChromeOsTest, AddToLocalNetworksNonUtf8) { | |
215 AddToLocalNetworks(MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, "")); | |
216 // TODO(quiche): Change to EXPECT_TRUE, once we support non-UTF-8 SSIDs. | |
217 EXPECT_FALSE(create_configuration_called()); | |
218 } | |
219 | |
220 TEST_F(WifiConfigDelegateChromeOsTest, | |
221 AddToLocalNetworksCreateConfigurationFailure) { | |
222 AddToLocalNetworks(MakeCredential(kSsid, SECURITY_CLASS_NONE, "")); | |
223 EXPECT_TRUE(create_configuration_called()); | |
224 if (!create_configuration_error_callback().is_null()) { | |
225 create_configuration_error_callback().Run( | |
226 "Config.CreateConfiguration Failed", | |
227 base::MakeUnique<base::DictionaryValue>()); | |
228 } | |
229 } | |
230 | |
231 } // namespace wifi_sync | |
OLD | NEW |