OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/wifi_sync/wifi_credential_syncable_service_impl.h" | 5 #include "components/wifi_sync/wifi_credential_syncable_service_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
14 #include "components/wifi_sync/wifi_config_delegate.h" | 14 #include "components/wifi_sync/wifi_config_delegate.h" |
| 15 #include "components/wifi_sync/wifi_config_observer.h" |
15 #include "components/wifi_sync/wifi_credential.h" | 16 #include "components/wifi_sync/wifi_credential.h" |
16 #include "components/wifi_sync/wifi_security_class.h" | 17 #include "components/wifi_sync/wifi_security_class.h" |
17 #include "sync/api/attachments/attachment_id.h" | 18 #include "sync/api/attachments/attachment_id.h" |
18 #include "sync/api/fake_sync_change_processor.h" | 19 #include "sync/api/fake_sync_change_processor.h" |
19 #include "sync/api/sync_change.h" | 20 #include "sync/api/sync_change.h" |
20 #include "sync/api/sync_data.h" | 21 #include "sync/api/sync_data.h" |
21 #include "sync/api/sync_error.h" | 22 #include "sync/api/sync_error.h" |
22 #include "sync/api/sync_error_factory_mock.h" | 23 #include "sync/api/sync_error_factory_mock.h" |
23 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test
.h" | 24 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test
.h" |
24 #include "sync/protocol/sync.pb.h" | 25 #include "sync/protocol/sync.pb.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 namespace wifi_sync { | 28 namespace wifi_sync { |
28 | 29 |
29 using syncer::FakeSyncChangeProcessor; | 30 using syncer::FakeSyncChangeProcessor; |
30 using syncer::SyncErrorFactoryMock; | 31 using syncer::SyncErrorFactoryMock; |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 const char kSsid[] = "fake-ssid"; | 35 const char kSsid[] = "fake-ssid"; |
35 const char kSsidNonUtf8[] = "\xc0"; | 36 const char kSsidNonUtf8[] = "\xc0"; |
36 | 37 |
37 // Fake implementation of WifiConfigDelegate, which provides the | 38 // Fake implementation of WifiConfigDelegate, which provides the |
38 // ability to check how many times a WifiConfigDelegate method has | 39 // ability to check how many times a WifiConfigDelegate method has |
39 // been called. | 40 // been called. |
40 class FakeWifiConfigDelegate : public WifiConfigDelegate { | 41 class FakeWifiConfigDelegate : public WifiConfigDelegate { |
41 public: | 42 public: |
42 FakeWifiConfigDelegate() : add_count_(0) {} | 43 FakeWifiConfigDelegate() |
| 44 : add_count_(0) {} |
43 ~FakeWifiConfigDelegate() override {} | 45 ~FakeWifiConfigDelegate() override {} |
44 | 46 |
45 // WifiConfigDelegate implementation. | 47 // WifiConfigDelegate implementation. |
46 void AddToLocalNetworks(const WifiCredential& network_credential) override { | 48 void AddToLocalNetworks(const WifiCredential& network_credential) override { |
47 ++add_count_; | 49 ++add_count_; |
48 } | 50 } |
49 | 51 |
50 // Returns the number of times the AddToLocalNetworks method has | 52 // Returns the number of times the AddToLocalNetworks method has |
51 // been called. | 53 // been called. |
52 unsigned int add_count() const { return add_count_; } | 54 unsigned int add_count() const { return add_count_; } |
53 | 55 |
54 private: | 56 private: |
55 // The number of times AddToLocalNetworks has been called on this fake. | 57 // The number of times AddToLocalNetworks has been called on this fake. |
56 unsigned int add_count_; | 58 unsigned int add_count_; |
57 | 59 |
58 DISALLOW_COPY_AND_ASSIGN(FakeWifiConfigDelegate); | 60 DISALLOW_COPY_AND_ASSIGN(FakeWifiConfigDelegate); |
59 }; | 61 }; |
60 | 62 |
| 63 // Fake implementation of WifiConfigObserver, which provides the |
| 64 // ability to check how many times a WifiConfigObserver method has |
| 65 // been called. |
| 66 class FakeWifiConfigObserver : public WifiConfigObserver { |
| 67 public: |
| 68 FakeWifiConfigObserver() |
| 69 : start_syncing_called_(false), |
| 70 stop_syncing_called_(false) {} |
| 71 ~FakeWifiConfigObserver() override {} |
| 72 |
| 73 // WifiConfigObserver implementation. |
| 74 void StartSyncing( |
| 75 base::WeakPtr<WifiCredentialSyncableService> syncable_service) override { |
| 76 start_syncing_called_ = true; |
| 77 } |
| 78 void StopSyncing() override { |
| 79 stop_syncing_called_ = true; |
| 80 } |
| 81 |
| 82 // Returns whether or not the StartSyncing method has been called. |
| 83 bool start_syncing_called() const { return start_syncing_called_; } |
| 84 |
| 85 // Returns whether or not the StopSyncing method has been called. |
| 86 bool stop_syncing_called() const { return stop_syncing_called_; } |
| 87 |
| 88 private: |
| 89 // Whether or not StartSyncing has been called on this fake. |
| 90 bool start_syncing_called_; |
| 91 |
| 92 // Whether or not StopSyncing has been called on this fake. |
| 93 bool stop_syncing_called_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(FakeWifiConfigObserver); |
| 96 }; |
| 97 |
61 // Unit tests for WifiCredentialSyncableService. | 98 // Unit tests for WifiCredentialSyncableService. |
62 class WifiCredentialSyncableServiceTest : public testing::Test { | 99 class WifiCredentialSyncableServiceTest : public testing::Test { |
63 protected: | 100 protected: |
64 WifiCredentialSyncableServiceTest() | 101 WifiCredentialSyncableServiceTest() |
65 : config_delegate_(new FakeWifiConfigDelegate()), | 102 : config_delegate_(new FakeWifiConfigDelegate()), |
| 103 config_observer_(new FakeWifiConfigObserver()), |
66 change_processor_(nullptr) { | 104 change_processor_(nullptr) { |
67 syncable_service_.reset( | 105 syncable_service_.reset( |
68 new WifiCredentialSyncableServiceImpl( | 106 new WifiCredentialSyncableServiceImpl( |
69 make_scoped_ptr(config_delegate_))); | 107 make_scoped_ptr(config_delegate_), |
| 108 make_scoped_ptr(config_observer_))); |
70 } | 109 } |
71 | 110 |
72 // Wrappers for methods in WifiCredentialSyncableService. | 111 // Wrappers for methods in WifiCredentialSyncableService. |
| 112 void StopSyncing() { |
| 113 syncable_service_->StopSyncing(syncer::WIFI_CREDENTIALS); |
| 114 } |
73 syncer::SyncError ProcessSyncChanges( | 115 syncer::SyncError ProcessSyncChanges( |
74 const syncer::SyncChangeList& change_list) { | 116 const syncer::SyncChangeList& change_list) { |
75 return syncable_service_->ProcessSyncChanges(FROM_HERE, change_list); | 117 return syncable_service_->ProcessSyncChanges(FROM_HERE, change_list); |
76 } | 118 } |
77 bool AddToSyncedNetworks(const std::string& item_id, | 119 bool AddToSyncedNetworks(const WifiCredential& credential) { |
78 const WifiCredential& credential) { | 120 return syncable_service_->AddToSyncedNetworks(credential); |
79 return syncable_service_->AddToSyncedNetworks(item_id, credential); | |
80 } | 121 } |
81 | 122 |
82 // Returns the number of change requests received by | 123 // Returns the number of change requests received by |
83 // |change_processor_|. | 124 // |change_processor_|. |
84 int change_processor_changes_size() { | 125 int change_processor_changes_size() { |
85 CHECK(change_processor_); | 126 CHECK(change_processor_); |
86 return change_processor_->changes().size(); | 127 return change_processor_->changes().size(); |
87 } | 128 } |
88 | 129 |
| 130 // Returns whether or not StartSyncing has been called on |
| 131 // |config_observer_|. |
| 132 bool config_observer_start_syncing_called() { |
| 133 return config_observer_->start_syncing_called(); |
| 134 } |
| 135 |
| 136 // Returns whether or not StopSyncing been called on |
| 137 // |config_observer_|. |
| 138 bool config_observer_stop_syncing_called() { |
| 139 return config_observer_->stop_syncing_called(); |
| 140 } |
| 141 |
89 // Returns the number of times AddToLocalNetworks has been called on | 142 // Returns the number of times AddToLocalNetworks has been called on |
90 // |config_delegate_|. | 143 // |config_delegate_|. |
91 unsigned int config_delegate_add_count() { | 144 unsigned int config_delegate_add_count() { |
92 return config_delegate_->add_count(); | 145 return config_delegate_->add_count(); |
93 } | 146 } |
94 | 147 |
95 // Returns a new WifiCredential constructed from the given parameters. | 148 // Returns a new WifiCredential constructed from the given parameters. |
96 WifiCredential MakeCredential(const std::string& ssid, | 149 WifiCredential MakeCredential(const std::string& ssid, |
97 WifiSecurityClass security_class, | 150 WifiSecurityClass security_class, |
98 const std::string& passphrase) { | 151 const std::string& passphrase) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 new FakeSyncChangeProcessor()); | 195 new FakeSyncChangeProcessor()); |
143 change_processor_ = change_processor.get(); | 196 change_processor_ = change_processor.get(); |
144 syncable_service_->MergeDataAndStartSyncing( | 197 syncable_service_->MergeDataAndStartSyncing( |
145 syncer::WIFI_CREDENTIALS, syncer::SyncDataList(), | 198 syncer::WIFI_CREDENTIALS, syncer::SyncDataList(), |
146 change_processor.Pass(), make_scoped_ptr(new SyncErrorFactoryMock())); | 199 change_processor.Pass(), make_scoped_ptr(new SyncErrorFactoryMock())); |
147 } | 200 } |
148 | 201 |
149 private: | 202 private: |
150 scoped_ptr<WifiCredentialSyncableService> syncable_service_; | 203 scoped_ptr<WifiCredentialSyncableService> syncable_service_; |
151 FakeWifiConfigDelegate* config_delegate_; // Owned by |syncable_service_| | 204 FakeWifiConfigDelegate* config_delegate_; // Owned by |syncable_service_| |
| 205 FakeWifiConfigObserver* config_observer_; // Owned by |syncable_service_| |
152 FakeSyncChangeProcessor* change_processor_; // Owned by |syncable_service_| | 206 FakeSyncChangeProcessor* change_processor_; // Owned by |syncable_service_| |
153 | 207 |
154 DISALLOW_COPY_AND_ASSIGN(WifiCredentialSyncableServiceTest); | 208 DISALLOW_COPY_AND_ASSIGN(WifiCredentialSyncableServiceTest); |
155 }; | 209 }; |
156 | 210 |
157 } // namespace | 211 } // namespace |
158 | 212 |
159 TEST_F(WifiCredentialSyncableServiceTest, ProcessSyncChangesNotStarted) { | 213 TEST_F(WifiCredentialSyncableServiceTest, ProcessSyncChangesNotStarted) { |
160 syncer::SyncError sync_error(ProcessSyncChanges(syncer::SyncChangeList())); | 214 syncer::SyncError sync_error(ProcessSyncChanges(syncer::SyncChangeList())); |
161 ASSERT_TRUE(sync_error.IsSet()); | 215 ASSERT_TRUE(sync_error.IsSet()); |
162 EXPECT_EQ(syncer::SyncError::UNREADY_ERROR, sync_error.error_type()); | 216 EXPECT_EQ(syncer::SyncError::UNREADY_ERROR, sync_error.error_type()); |
163 EXPECT_EQ(0U, config_delegate_add_count()); | 217 EXPECT_EQ(0U, config_delegate_add_count()); |
164 } | 218 } |
165 | 219 |
| 220 TEST_F(WifiCredentialSyncableServiceTest, StartSyncing) { |
| 221 StartSyncing(); |
| 222 EXPECT_TRUE(config_observer_start_syncing_called()); |
| 223 } |
| 224 |
| 225 TEST_F(WifiCredentialSyncableServiceTest, StopSyncing) { |
| 226 StartSyncing(); |
| 227 StopSyncing(); |
| 228 EXPECT_TRUE(config_observer_stop_syncing_called()); |
| 229 } |
| 230 |
166 TEST_F(WifiCredentialSyncableServiceTest, | 231 TEST_F(WifiCredentialSyncableServiceTest, |
167 ProcessSyncChangesActionAddSecurityClassInvalid) { | 232 ProcessSyncChangesActionAddSecurityClassInvalid) { |
168 syncer::SyncChangeList changes; | 233 syncer::SyncChangeList changes; |
169 const int sync_item_id = 1; | 234 const int sync_item_id = 1; |
170 changes.push_back(MakeActionAdd( | 235 changes.push_back(MakeActionAdd( |
171 sync_item_id, kSsidNonUtf8, | 236 sync_item_id, kSsidNonUtf8, |
172 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID, nullptr)); | 237 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID, nullptr)); |
173 StartSyncing(); | 238 StartSyncing(); |
174 syncer::SyncError sync_error = ProcessSyncChanges(changes); | 239 syncer::SyncError sync_error = ProcessSyncChanges(changes); |
175 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored. | 240 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 2 /* sync item id */, kSsidNonUtf8, | 333 2 /* sync item id */, kSsidNonUtf8, |
269 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr)); | 334 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr)); |
270 StartSyncing(); | 335 StartSyncing(); |
271 syncer::SyncError sync_error = ProcessSyncChanges(changes); | 336 syncer::SyncError sync_error = ProcessSyncChanges(changes); |
272 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored. | 337 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored. |
273 EXPECT_EQ(1U, config_delegate_add_count()); | 338 EXPECT_EQ(1U, config_delegate_add_count()); |
274 } | 339 } |
275 | 340 |
276 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksNotStarted) { | 341 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksNotStarted) { |
277 EXPECT_FALSE(AddToSyncedNetworks( | 342 EXPECT_FALSE(AddToSyncedNetworks( |
278 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | 343 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); |
279 } | 344 } |
280 | 345 |
281 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksSuccess) { | 346 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksSuccess) { |
282 StartSyncing(); | 347 StartSyncing(); |
283 EXPECT_TRUE(AddToSyncedNetworks( | 348 EXPECT_TRUE(AddToSyncedNetworks( |
284 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | 349 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); |
285 EXPECT_EQ(1, change_processor_changes_size()); | 350 EXPECT_EQ(1, change_processor_changes_size()); |
286 } | 351 } |
287 | 352 |
288 TEST_F(WifiCredentialSyncableServiceTest, | 353 TEST_F(WifiCredentialSyncableServiceTest, |
289 AddToSyncedNetworksDifferentSecurityClassesSuccess) { | 354 AddToSyncedNetworksDifferentSecurityClassesSuccess) { |
290 StartSyncing(); | 355 StartSyncing(); |
291 EXPECT_TRUE(AddToSyncedNetworks( | 356 EXPECT_TRUE(AddToSyncedNetworks( |
292 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | 357 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); |
293 EXPECT_TRUE(AddToSyncedNetworks( | 358 EXPECT_TRUE(AddToSyncedNetworks( |
294 "fake-item-id-2", | |
295 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_WEP, ""))); | 359 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_WEP, ""))); |
296 EXPECT_EQ(2, change_processor_changes_size()); | 360 EXPECT_EQ(2, change_processor_changes_size()); |
297 } | 361 } |
298 | 362 |
299 TEST_F(WifiCredentialSyncableServiceTest, | 363 TEST_F(WifiCredentialSyncableServiceTest, |
300 AddToSyncedNetworksDifferentSsidsSuccess) { | 364 AddToSyncedNetworksDifferentSsidsSuccess) { |
301 StartSyncing(); | 365 StartSyncing(); |
302 EXPECT_TRUE(AddToSyncedNetworks( | 366 EXPECT_TRUE(AddToSyncedNetworks( |
303 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | 367 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); |
304 EXPECT_TRUE(AddToSyncedNetworks( | 368 EXPECT_TRUE(AddToSyncedNetworks( |
305 "fake-item-id-2", MakeCredential(kSsid, SECURITY_CLASS_NONE, ""))); | 369 MakeCredential(kSsid, SECURITY_CLASS_NONE, ""))); |
306 EXPECT_EQ(2, change_processor_changes_size()); | 370 EXPECT_EQ(2, change_processor_changes_size()); |
307 } | 371 } |
308 | 372 |
309 TEST_F(WifiCredentialSyncableServiceTest, | 373 TEST_F(WifiCredentialSyncableServiceTest, |
310 AddToSyncedNetworksDuplicateAddPskNetwork) { | 374 AddToSyncedNetworksDuplicateAddPskNetwork) { |
311 const std::string passphrase("psk-passphrase"); | 375 const std::string passphrase("psk-passphrase"); |
312 StartSyncing(); | 376 StartSyncing(); |
313 EXPECT_TRUE( | 377 EXPECT_TRUE( |
314 AddToSyncedNetworks( | 378 AddToSyncedNetworks( |
315 "fake-item-id", | |
316 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, passphrase))); | 379 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, passphrase))); |
317 EXPECT_EQ(1, change_processor_changes_size()); | 380 EXPECT_EQ(1, change_processor_changes_size()); |
318 EXPECT_FALSE( | 381 EXPECT_FALSE( |
319 AddToSyncedNetworks( | 382 AddToSyncedNetworks( |
320 "fake-item-id", | |
321 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, passphrase))); | 383 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, passphrase))); |
322 EXPECT_EQ(1, change_processor_changes_size()); | 384 EXPECT_EQ(1, change_processor_changes_size()); |
323 } | 385 } |
324 | 386 |
325 TEST_F(WifiCredentialSyncableServiceTest, | 387 TEST_F(WifiCredentialSyncableServiceTest, |
326 AddToSyncedNetworksDuplicateAddOpenNetwork) { | 388 AddToSyncedNetworksDuplicateAddOpenNetwork) { |
327 StartSyncing(); | 389 StartSyncing(); |
328 EXPECT_TRUE( | 390 EXPECT_TRUE( |
329 AddToSyncedNetworks( | 391 AddToSyncedNetworks( |
330 "fake-item-id", | |
331 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | 392 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); |
332 EXPECT_EQ(1, change_processor_changes_size()); | 393 EXPECT_EQ(1, change_processor_changes_size()); |
333 EXPECT_FALSE( | 394 EXPECT_FALSE( |
334 AddToSyncedNetworks( | 395 AddToSyncedNetworks( |
335 "fake-item-id", | |
336 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); | 396 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, ""))); |
337 EXPECT_EQ(1, change_processor_changes_size()); | 397 EXPECT_EQ(1, change_processor_changes_size()); |
338 } | 398 } |
339 | 399 |
340 } // namespace wifi_sync | 400 } // namespace wifi_sync |
OLD | NEW |