Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Side by Side Diff: components/wifi_sync/wifi_credential_syncable_service_unittest.cc

Issue 836303004: wifi_sync: implement ACTION_ADD in WifiCredentialSyncableService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@submit-4.2-wifi-config-delegate
Patch Set: address comments on PS3 (stevenjb) and PS4 (pavely) Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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,
174 ProcessSyncChangesActionAddSecurityClassInvalid) {
175 syncer::SyncChangeList changes;
176 const int sync_item_id = 1;
177 changes.push_back(
178 MakeActionAdd(
179 FROM_HERE,
180 sync_item_id,
181 kSsidNonUtf8,
182 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID,
183 nullptr));
184 StartSyncing();
185 syncer::SyncError sync_error = ProcessSyncChanges(FROM_HERE, changes);
186 EXPECT_FALSE(sync_error.IsSet()); // Just ignore bad items.
187 EXPECT_EQ(0, GetWifiDelegateAddCount());
188 }
189
190 TEST_F(WifiCredentialSyncableServiceTest,
191 ProcessSyncChangesActionAddSecurityClassNoneSuccess) {
192 syncer::SyncChangeList changes;
193 const int sync_item_id = 1;
194 changes.push_back(
195 MakeActionAdd(
196 FROM_HERE,
197 sync_item_id,
198 kSsidNonUtf8,
199 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE,
200 nullptr));
201 StartSyncing();
202 syncer::SyncError sync_error = ProcessSyncChanges(FROM_HERE, changes);
203 EXPECT_FALSE(sync_error.IsSet());
204 EXPECT_EQ(1, GetWifiDelegateAddCount());
205 }
206
207 TEST_F(WifiCredentialSyncableServiceTest,
208 ProcessSyncChangesActionAddSecurityClassWepMissingPassphrase) {
209 syncer::SyncChangeList changes;
210 const int sync_item_id = 1;
211 changes.push_back(
212 MakeActionAdd(
213 FROM_HERE,
214 sync_item_id,
215 kSsidNonUtf8,
216 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP,
217 nullptr));
218 StartSyncing();
219 syncer::SyncError sync_error = ProcessSyncChanges(FROM_HERE, changes);
220 EXPECT_FALSE(sync_error.IsSet()); // Just ignore bad items.
221 EXPECT_EQ(0, GetWifiDelegateAddCount());
222 }
223
224 TEST_F(WifiCredentialSyncableServiceTest,
225 ProcessSyncChangesActionAddSecurityClassWepSuccess) {
226 syncer::SyncChangeList changes;
227 const int sync_item_id = 1;
228 const std::string passphrase("abcde");
229 changes.push_back(
230 MakeActionAdd(
231 FROM_HERE,
232 sync_item_id,
233 kSsidNonUtf8,
234 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP,
235 &passphrase));
236 StartSyncing();
237 syncer::SyncError sync_error = ProcessSyncChanges(FROM_HERE, changes);
238 EXPECT_FALSE(sync_error.IsSet());
239 EXPECT_EQ(1, GetWifiDelegateAddCount());
240 }
241
242 TEST_F(WifiCredentialSyncableServiceTest,
243 ProcessSyncChangesActionAddSecurityClassPskMissingPassphrase) {
244 syncer::SyncChangeList changes;
245 const int sync_item_id = 1;
246 changes.push_back(
247 MakeActionAdd(
248 FROM_HERE,
249 sync_item_id,
250 kSsidNonUtf8,
251 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_PSK,
252 nullptr));
253 StartSyncing();
254 syncer::SyncError sync_error = ProcessSyncChanges(FROM_HERE, changes);
255 EXPECT_FALSE(sync_error.IsSet()); // Just ignore bad items.
256 EXPECT_EQ(0, GetWifiDelegateAddCount());
257 }
258
259 TEST_F(WifiCredentialSyncableServiceTest,
260 ProcessSyncChangesActionAddSecurityClassPskSuccess) {
261 syncer::SyncChangeList changes;
262 const int sync_item_id = 1;
263 const std::string passphrase("psk-passphrase");
264 changes.push_back(
265 MakeActionAdd(
266 FROM_HERE,
267 sync_item_id,
268 kSsidNonUtf8,
269 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_PSK,
270 &passphrase));
271 StartSyncing();
272 syncer::SyncError sync_error = ProcessSyncChanges(FROM_HERE, changes);
273 EXPECT_FALSE(sync_error.IsSet());
274 EXPECT_EQ(1, GetWifiDelegateAddCount());
275 }
276
277 TEST_F(WifiCredentialSyncableServiceTest,
278 ProcessSyncChangesContinuesAfterSecurityClassInvalid) {
279 syncer::SyncChangeList changes;
280 changes.push_back(
281 MakeActionAdd(
282 FROM_HERE,
283 1 /* sync item id */,
284 kSsidNonUtf8,
285 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID,
286 nullptr));
287 changes.push_back(
288 MakeActionAdd(
289 FROM_HERE,
290 2 /* sync item id */,
291 kSsidNonUtf8,
292 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE,
293 nullptr));
294 StartSyncing();
295 syncer::SyncError sync_error = ProcessSyncChanges(FROM_HERE, changes);
296 EXPECT_FALSE(sync_error.IsSet()); // Just ignore bad items.
297 EXPECT_EQ(1, GetWifiDelegateAddCount());
298 }
299
300 TEST_F(WifiCredentialSyncableServiceTest,
301 ProcessSyncChangesContinuesAfterMissingPassphrase) {
302 syncer::SyncChangeList changes;
303 changes.push_back(
304 MakeActionAdd(
305 FROM_HERE,
306 1 /* sync item id */,
307 kSsidNonUtf8,
308 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP,
309 nullptr));
310 changes.push_back(
311 MakeActionAdd(
312 FROM_HERE,
313 2 /* sync item id */,
314 kSsidNonUtf8,
315 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE,
316 nullptr));
317 StartSyncing();
318 syncer::SyncError sync_error = ProcessSyncChanges(FROM_HERE, changes);
319 EXPECT_FALSE(sync_error.IsSet()); // Just ignore bad items.
320 EXPECT_EQ(1, GetWifiDelegateAddCount());
321 }
322
323 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksNotStarted) {
324 EXPECT_FALSE(
325 AddToSyncedNetworks(
326 "fake-item-id",
327 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
328 }
329
330 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksSuccess) {
331 StartSyncing();
332 EXPECT_TRUE(
333 AddToSyncedNetworks(
334 "fake-item-id",
335 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
336 EXPECT_EQ(1, GetChangeProcessorChangeCount());
337 }
338
339 } // namespace wifi_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698