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