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_credential_syncable_service.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <memory> | |
10 #include <string> | |
11 #include <utility> | |
12 #include <vector> | |
13 | |
14 #include "base/macros.h" | |
15 #include "base/memory/ptr_util.h" | |
16 #include "base/time/time.h" | |
17 #include "base/tracked_objects.h" | |
18 #include "components/sync/model/attachments/attachment_id.h" | |
19 #include "components/sync/model/attachments/attachment_service_proxy_for_test.h" | |
20 #include "components/sync/model/fake_sync_change_processor.h" | |
21 #include "components/sync/model/sync_change.h" | |
22 #include "components/sync/model/sync_data.h" | |
23 #include "components/sync/model/sync_error.h" | |
24 #include "components/sync/model/sync_error_factory_mock.h" | |
25 #include "components/sync/protocol/sync.pb.h" | |
26 #include "components/wifi_sync/wifi_config_delegate.h" | |
27 #include "components/wifi_sync/wifi_credential.h" | |
28 #include "components/wifi_sync/wifi_security_class.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | |
30 | |
31 namespace wifi_sync { | |
32 | |
33 using syncer::FakeSyncChangeProcessor; | |
34 using syncer::SyncErrorFactoryMock; | |
35 | |
36 namespace { | |
37 | |
38 const char kSsid[] = "fake-ssid"; | |
39 const char kSsidNonUtf8[] = "\xc0"; | |
40 | |
41 // Fake implementation of WifiConfigDelegate, which provides the | |
42 // ability to check how many times a WifiConfigDelegate method has | |
43 // been called. | |
44 class FakeWifiConfigDelegate : public WifiConfigDelegate { | |
45 public: | |
46 FakeWifiConfigDelegate() : add_count_(0) {} | |
47 ~FakeWifiConfigDelegate() override {} | |
48 | |
49 // WifiConfigDelegate implementation. | |
50 void AddToLocalNetworks(const WifiCredential& network_credential) override { | |
51 ++add_count_; | |
52 } | |
53 | |
54 // Returns the number of times the AddToLocalNetworks method has | |
55 // been called. | |
56 unsigned int add_count() const { return add_count_; } | |
57 | |
58 private: | |
59 // The number of times AddToLocalNetworks has been called on this fake. | |
60 unsigned int add_count_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(FakeWifiConfigDelegate); | |
63 }; | |
64 | |
65 // Unit tests for WifiCredentialSyncableService. | |
66 class WifiCredentialSyncableServiceTest : public testing::Test { | |
67 protected: | |
68 WifiCredentialSyncableServiceTest() | |
69 : config_delegate_(new FakeWifiConfigDelegate()), | |
70 change_processor_(nullptr) { | |
71 syncable_service_.reset( | |
72 new WifiCredentialSyncableService(base::WrapUnique(config_delegate_))); | |
73 } | |
74 | |
75 // Wrappers for methods in WifiCredentialSyncableService. | |
76 syncer::SyncError ProcessSyncChanges( | |
77 const syncer::SyncChangeList& change_list) { | |
78 return syncable_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
79 } | |
80 bool AddToSyncedNetworks(const std::string& item_id, | |
81 const WifiCredential& credential) { | |
82 return syncable_service_->AddToSyncedNetworks(item_id, credential); | |
83 } | |
84 | |
85 // Returns the number of change requests received by | |
86 // |change_processor_|. | |
87 int change_processor_changes_size() { | |
88 CHECK(change_processor_); | |
89 return change_processor_->changes().size(); | |
90 } | |
91 | |
92 // Returns the number of times AddToLocalNetworks has been called on | |
93 // |config_delegate_|. | |
94 unsigned int config_delegate_add_count() { | |
95 return config_delegate_->add_count(); | |
96 } | |
97 | |
98 // Returns a new WifiCredential constructed from the given parameters. | |
99 WifiCredential MakeCredential(const std::string& ssid, | |
100 WifiSecurityClass security_class, | |
101 const std::string& passphrase) { | |
102 std::unique_ptr<WifiCredential> credential = WifiCredential::Create( | |
103 WifiCredential::MakeSsidBytesForTest(ssid), security_class, passphrase); | |
104 CHECK(credential); | |
105 return *credential; | |
106 } | |
107 | |
108 // Returns a new EntitySpecifics protobuf, with the | |
109 // wifi_credential_specifics fields populated with the given | |
110 // parameters. | |
111 sync_pb::EntitySpecifics MakeWifiCredentialSpecifics( | |
112 const std::string& ssid, | |
113 sync_pb::WifiCredentialSpecifics_SecurityClass security_class, | |
114 const std::string* passphrase) { | |
115 const std::vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end()); | |
116 sync_pb::EntitySpecifics sync_entity_specifics; | |
117 sync_pb::WifiCredentialSpecifics* wifi_credential_specifics = | |
118 sync_entity_specifics.mutable_wifi_credential(); | |
119 CHECK(wifi_credential_specifics); | |
120 wifi_credential_specifics->set_ssid(ssid_bytes.data(), ssid_bytes.size()); | |
121 wifi_credential_specifics->set_security_class(security_class); | |
122 if (passphrase) { | |
123 wifi_credential_specifics->set_passphrase(passphrase->data(), | |
124 passphrase->size()); | |
125 } | |
126 return sync_entity_specifics; | |
127 } | |
128 | |
129 syncer::SyncChange MakeActionAdd( | |
130 int sync_item_id, | |
131 const std::string& ssid, | |
132 sync_pb::WifiCredentialSpecifics_SecurityClass security_class, | |
133 const std::string* passphrase) { | |
134 return syncer::SyncChange( | |
135 FROM_HERE, syncer::SyncChange::ACTION_ADD, | |
136 syncer::SyncData::CreateRemoteData( | |
137 sync_item_id, | |
138 MakeWifiCredentialSpecifics(ssid, security_class, passphrase), | |
139 base::Time(), syncer::AttachmentIdList(), | |
140 syncer::AttachmentServiceProxyForTest::Create())); | |
141 } | |
142 | |
143 void StartSyncing() { | |
144 std::unique_ptr<FakeSyncChangeProcessor> change_processor( | |
145 new FakeSyncChangeProcessor()); | |
146 change_processor_ = change_processor.get(); | |
147 syncable_service_->MergeDataAndStartSyncing( | |
148 syncer::WIFI_CREDENTIALS, syncer::SyncDataList(), | |
149 std::move(change_processor), base::MakeUnique<SyncErrorFactoryMock>()); | |
150 } | |
151 | |
152 private: | |
153 std::unique_ptr<WifiCredentialSyncableService> syncable_service_; | |
154 FakeWifiConfigDelegate* config_delegate_; // Owned by |syncable_service_| | |
155 FakeSyncChangeProcessor* change_processor_; // Owned by |syncable_service_| | |
156 | |
157 DISALLOW_COPY_AND_ASSIGN(WifiCredentialSyncableServiceTest); | |
158 }; | |
159 | |
160 } // namespace | |
161 | |
162 TEST_F(WifiCredentialSyncableServiceTest, ProcessSyncChangesNotStarted) { | |
163 syncer::SyncError sync_error(ProcessSyncChanges(syncer::SyncChangeList())); | |
164 ASSERT_TRUE(sync_error.IsSet()); | |
165 EXPECT_EQ(syncer::SyncError::UNREADY_ERROR, sync_error.error_type()); | |
166 EXPECT_EQ(0U, config_delegate_add_count()); | |
167 } | |
168 | |
169 TEST_F(WifiCredentialSyncableServiceTest, | |
170 ProcessSyncChangesActionAddSecurityClassInvalid) { | |
171 syncer::SyncChangeList changes; | |
172 const int sync_item_id = 1; | |
173 changes.push_back(MakeActionAdd( | |
174 sync_item_id, kSsidNonUtf8, | |
175 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID, nullptr)); | |
176 StartSyncing(); | |
177 syncer::SyncError sync_error = ProcessSyncChanges(changes); | |
178 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored. | |
179 EXPECT_EQ(0U, config_delegate_add_count()); | |
180 } | |
181 | |
182 TEST_F(WifiCredentialSyncableServiceTest, | |
183 ProcessSyncChangesActionAddSecurityClassNoneSuccess) { | |
184 syncer::SyncChangeList changes; | |
185 const int sync_item_id = 1; | |
186 changes.push_back(MakeActionAdd( | |
187 sync_item_id, kSsidNonUtf8, | |
188 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr)); | |
189 StartSyncing(); | |
190 syncer::SyncError sync_error = ProcessSyncChanges(changes); | |
191 EXPECT_FALSE(sync_error.IsSet()); | |
192 EXPECT_EQ(1U, config_delegate_add_count()); | |
193 } | |
194 | |
195 TEST_F(WifiCredentialSyncableServiceTest, | |
196 ProcessSyncChangesActionAddSecurityClassWepMissingPassphrase) { | |
197 syncer::SyncChangeList changes; | |
198 const int sync_item_id = 1; | |
199 changes.push_back(MakeActionAdd( | |
200 sync_item_id, kSsidNonUtf8, | |
201 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, nullptr)); | |
202 StartSyncing(); | |
203 syncer::SyncError sync_error = ProcessSyncChanges(changes); | |
204 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored. | |
205 EXPECT_EQ(0U, config_delegate_add_count()); | |
206 } | |
207 | |
208 TEST_F(WifiCredentialSyncableServiceTest, | |
209 ProcessSyncChangesActionAddSecurityClassWepSuccess) { | |
210 syncer::SyncChangeList changes; | |
211 const int sync_item_id = 1; | |
212 const std::string passphrase("abcde"); | |
213 changes.push_back(MakeActionAdd( | |
214 sync_item_id, kSsidNonUtf8, | |
215 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, &passphrase)); | |
216 StartSyncing(); | |
217 syncer::SyncError sync_error = ProcessSyncChanges(changes); | |
218 EXPECT_FALSE(sync_error.IsSet()); | |
219 EXPECT_EQ(1U, config_delegate_add_count()); | |
220 } | |
221 | |
222 TEST_F(WifiCredentialSyncableServiceTest, | |
223 ProcessSyncChangesActionAddSecurityClassPskMissingPassphrase) { | |
224 syncer::SyncChangeList changes; | |
225 const int sync_item_id = 1; | |
226 changes.push_back(MakeActionAdd( | |
227 sync_item_id, kSsidNonUtf8, | |
228 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_PSK, nullptr)); | |
229 StartSyncing(); | |
230 syncer::SyncError sync_error = ProcessSyncChanges(changes); | |
231 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored. | |
232 EXPECT_EQ(0U, config_delegate_add_count()); | |
233 } | |
234 | |
235 TEST_F(WifiCredentialSyncableServiceTest, | |
236 ProcessSyncChangesActionAddSecurityClassPskSuccess) { | |
237 syncer::SyncChangeList changes; | |
238 const int sync_item_id = 1; | |
239 const std::string passphrase("psk-passphrase"); | |
240 changes.push_back(MakeActionAdd( | |
241 sync_item_id, kSsidNonUtf8, | |
242 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_PSK, &passphrase)); | |
243 StartSyncing(); | |
244 syncer::SyncError sync_error = ProcessSyncChanges(changes); | |
245 EXPECT_FALSE(sync_error.IsSet()); | |
246 EXPECT_EQ(1U, config_delegate_add_count()); | |
247 } | |
248 | |
249 TEST_F(WifiCredentialSyncableServiceTest, | |
250 ProcessSyncChangesContinuesAfterSecurityClassInvalid) { | |
251 syncer::SyncChangeList changes; | |
252 changes.push_back(MakeActionAdd( | |
253 1 /* sync item id */, kSsidNonUtf8, | |
254 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID, nullptr)); | |
255 changes.push_back(MakeActionAdd( | |
256 2 /* sync item id */, kSsidNonUtf8, | |
257 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr)); | |
258 StartSyncing(); | |
259 syncer::SyncError sync_error = ProcessSyncChanges(changes); | |
260 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored. | |
261 EXPECT_EQ(1U, config_delegate_add_count()); | |
262 } | |
263 | |
264 TEST_F(WifiCredentialSyncableServiceTest, | |
265 ProcessSyncChangesContinuesAfterMissingPassphrase) { | |
266 syncer::SyncChangeList changes; | |
267 changes.push_back(MakeActionAdd( | |
268 1 /* sync item id */, kSsidNonUtf8, | |
269 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, nullptr)); | |
270 changes.push_back(MakeActionAdd( | |
271 2 /* sync item id */, kSsidNonUtf8, | |
272 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr)); | |
273 StartSyncing(); | |
274 syncer::SyncError sync_error = ProcessSyncChanges(changes); | |
275 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored. | |
276 EXPECT_EQ(1U, config_delegate_add_count()); | |
277 } | |
278 | |
279 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksNotStarted) { | |
280 EXPECT_FALSE(AddToSyncedNetworks( | |
281 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | |
282 } | |
283 | |
284 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksSuccess) { | |
285 StartSyncing(); | |
286 EXPECT_TRUE(AddToSyncedNetworks( | |
287 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | |
288 EXPECT_EQ(1, change_processor_changes_size()); | |
289 } | |
290 | |
291 TEST_F(WifiCredentialSyncableServiceTest, | |
292 AddToSyncedNetworksDifferentSecurityClassesSuccess) { | |
293 StartSyncing(); | |
294 EXPECT_TRUE(AddToSyncedNetworks( | |
295 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | |
296 EXPECT_TRUE(AddToSyncedNetworks( | |
297 "fake-item-id-2", | |
298 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_WEP, ""))); | |
299 EXPECT_EQ(2, change_processor_changes_size()); | |
300 } | |
301 | |
302 TEST_F(WifiCredentialSyncableServiceTest, | |
303 AddToSyncedNetworksDifferentSsidsSuccess) { | |
304 StartSyncing(); | |
305 EXPECT_TRUE(AddToSyncedNetworks( | |
306 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | |
307 EXPECT_TRUE(AddToSyncedNetworks( | |
308 "fake-item-id-2", MakeCredential(kSsid, SECURITY_CLASS_NONE, ""))); | |
309 EXPECT_EQ(2, change_processor_changes_size()); | |
310 } | |
311 | |
312 TEST_F(WifiCredentialSyncableServiceTest, | |
313 AddToSyncedNetworksDuplicateAddPskNetwork) { | |
314 const std::string passphrase("psk-passphrase"); | |
315 StartSyncing(); | |
316 EXPECT_TRUE( | |
317 AddToSyncedNetworks( | |
318 "fake-item-id", | |
319 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, passphrase))); | |
320 EXPECT_EQ(1, change_processor_changes_size()); | |
321 EXPECT_FALSE( | |
322 AddToSyncedNetworks( | |
323 "fake-item-id", | |
324 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, passphrase))); | |
325 EXPECT_EQ(1, change_processor_changes_size()); | |
326 } | |
327 | |
328 TEST_F(WifiCredentialSyncableServiceTest, | |
329 AddToSyncedNetworksDuplicateAddOpenNetwork) { | |
330 StartSyncing(); | |
331 EXPECT_TRUE( | |
332 AddToSyncedNetworks( | |
333 "fake-item-id", | |
334 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | |
335 EXPECT_EQ(1, change_processor_changes_size()); | |
336 EXPECT_FALSE( | |
337 AddToSyncedNetworks( | |
338 "fake-item-id", | |
339 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | |
340 EXPECT_EQ(1, change_processor_changes_size()); | |
341 } | |
342 | |
343 } // namespace wifi_sync | |
OLD | NEW |