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