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

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 PS6 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 namespace {
33
34 const char kSsidNonUtf8[] = "\xc0";
35
36 // Fake implementation of WifiConfigDelegate, which provides the
37 // ability to check how many times a WifiConfigDelegate method has
38 // been called.
39 class FakeWifiConfigDelegate : public WifiConfigDelegate {
40 public:
41 FakeWifiConfigDelegate() : add_count_(0) {}
42 virtual ~FakeWifiConfigDelegate() {}
43
44 // WifiConfigDelegate implementation.
45 void AddToLocalNetworks(const WifiCredential& network_credential) override {
46 ++add_count_;
47 }
48
49 // Returns the number of times the AddToLocalNetworks method has
50 // been called.
51 unsigned int add_count() const { return add_count_; }
52
53 private:
54 // The number of times AddToLocalNetworks has been called on this fake.
55 unsigned int add_count_;
56
57 DISALLOW_COPY_AND_ASSIGN(FakeWifiConfigDelegate);
58 };
59
60 // Unit tests for WifiCredentialSyncableService.
61 class WifiCredentialSyncableServiceTest : public testing::Test {
62 protected:
63 WifiCredentialSyncableServiceTest()
64 : config_delegate_(new FakeWifiConfigDelegate()),
65 change_processor_(nullptr) {
66 syncable_service_.reset(
67 new WifiCredentialSyncableService(make_scoped_ptr(config_delegate_)));
68 }
69
70 // Wrappers for methods in WifiCredentialSyncableService.
71 syncer::SyncError ProcessSyncChanges(
72 const syncer::SyncChangeList& change_list) {
73 return syncable_service_->ProcessSyncChanges(FROM_HERE, change_list);
74 }
75 bool AddToSyncedNetworks(const std::string& item_id,
76 const WifiCredential& credential) {
77 return syncable_service_->AddToSyncedNetworks(item_id, credential);
78 }
79
80 // Returns the number of change requests received by
81 // |change_processor_|.
82 int change_processor_changes_size() {
83 CHECK(change_processor_);
84 return change_processor_->changes().size();
85 }
86
87 // Returns the number of times AddToLocalNetworks has been called on
88 // |config_delegate_|.
89 unsigned int config_delegate_add_count() {
90 return config_delegate_->add_count();
91 }
92
93 // Returns a new WifiCredential constructed from the given parameters.
94 WifiCredential MakeCredential(const std::string& ssid,
95 WifiSecurityClass security_class,
96 const std::string& passphrase) {
97 scoped_ptr<WifiCredential> credential = WifiCredential::Create(
98 WifiCredential::MakeSsidBytesForTest(ssid), security_class, passphrase);
99 CHECK(credential);
100 return *credential;
101 }
102
103 // Returns a new EntitySpecifics protobuf, with the
104 // wifi_credential_specifics fields populated with the given
105 // parameters.
106 sync_pb::EntitySpecifics MakeWifiCredentialSpecifics(
107 const std::string& ssid,
108 sync_pb::WifiCredentialSpecifics_SecurityClass security_class,
109 const std::string* passphrase) {
110 const std::vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
111 sync_pb::EntitySpecifics sync_entity_specifics;
112 sync_pb::WifiCredentialSpecifics* wifi_credential_specifics =
113 sync_entity_specifics.mutable_wifi_credential();
114 CHECK(wifi_credential_specifics);
115 wifi_credential_specifics->set_ssid(ssid_bytes.data(), ssid_bytes.size());
116 wifi_credential_specifics->set_security_class(security_class);
117 if (passphrase) {
118 wifi_credential_specifics->set_passphrase(passphrase->data(),
119 passphrase->size());
120 }
121 return sync_entity_specifics;
122 }
123
124 syncer::SyncChange MakeActionAdd(
125 int sync_item_id,
126 const std::string& ssid,
127 sync_pb::WifiCredentialSpecifics_SecurityClass security_class,
128 const std::string* passphrase) {
129 return syncer::SyncChange(
130 FROM_HERE, syncer::SyncChange::ACTION_ADD,
131 syncer::SyncData::CreateRemoteData(
132 sync_item_id,
133 MakeWifiCredentialSpecifics(ssid, security_class, passphrase),
134 base::Time(), syncer::AttachmentIdList(),
135 syncer::AttachmentServiceProxyForTest::Create()));
136 }
137
138 void StartSyncing() {
139 scoped_ptr<FakeSyncChangeProcessor> change_processor(
140 new FakeSyncChangeProcessor());
141 change_processor_ = change_processor.get();
142 syncable_service_->MergeDataAndStartSyncing(
143 syncer::WIFI_CREDENTIALS, syncer::SyncDataList(),
144 change_processor.Pass(), make_scoped_ptr(new SyncErrorFactoryMock()));
145 }
146
147 private:
148 scoped_ptr<WifiCredentialSyncableService> syncable_service_;
149 FakeWifiConfigDelegate* config_delegate_; // Owned by |syncable_service_|
150 FakeSyncChangeProcessor* change_processor_; // Owned by |syncable_service_|
151
152 DISALLOW_COPY_AND_ASSIGN(WifiCredentialSyncableServiceTest);
153 };
154
155 } // namespace
156
157 TEST_F(WifiCredentialSyncableServiceTest, ProcessSyncChangesNotStarted) {
158 syncer::SyncError sync_error(ProcessSyncChanges(syncer::SyncChangeList()));
159 ASSERT_TRUE(sync_error.IsSet());
160 EXPECT_EQ(syncer::SyncError::UNREADY_ERROR, sync_error.error_type());
161 EXPECT_EQ(0U, config_delegate_add_count());
162 }
163
164 TEST_F(WifiCredentialSyncableServiceTest,
165 ProcessSyncChangesActionAddSecurityClassInvalid) {
166 syncer::SyncChangeList changes;
167 const int sync_item_id = 1;
168 changes.push_back(MakeActionAdd(
169 sync_item_id, kSsidNonUtf8,
170 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID, nullptr));
171 StartSyncing();
172 syncer::SyncError sync_error = ProcessSyncChanges(changes);
173 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored.
174 EXPECT_EQ(0U, config_delegate_add_count());
175 }
176
177 TEST_F(WifiCredentialSyncableServiceTest,
178 ProcessSyncChangesActionAddSecurityClassNoneSuccess) {
179 syncer::SyncChangeList changes;
180 const int sync_item_id = 1;
181 changes.push_back(MakeActionAdd(
182 sync_item_id, kSsidNonUtf8,
183 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr));
184 StartSyncing();
185 syncer::SyncError sync_error = ProcessSyncChanges(changes);
186 EXPECT_FALSE(sync_error.IsSet());
187 EXPECT_EQ(1U, config_delegate_add_count());
188 }
189
190 TEST_F(WifiCredentialSyncableServiceTest,
191 ProcessSyncChangesActionAddSecurityClassWepMissingPassphrase) {
192 syncer::SyncChangeList changes;
193 const int sync_item_id = 1;
194 changes.push_back(MakeActionAdd(
195 sync_item_id, kSsidNonUtf8,
196 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, nullptr));
197 StartSyncing();
198 syncer::SyncError sync_error = ProcessSyncChanges(changes);
199 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored.
200 EXPECT_EQ(0U, config_delegate_add_count());
201 }
202
203 TEST_F(WifiCredentialSyncableServiceTest,
204 ProcessSyncChangesActionAddSecurityClassWepSuccess) {
205 syncer::SyncChangeList changes;
206 const int sync_item_id = 1;
207 const std::string passphrase("abcde");
208 changes.push_back(MakeActionAdd(
209 sync_item_id, kSsidNonUtf8,
210 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, &passphrase));
211 StartSyncing();
212 syncer::SyncError sync_error = ProcessSyncChanges(changes);
213 EXPECT_FALSE(sync_error.IsSet());
214 EXPECT_EQ(1U, config_delegate_add_count());
215 }
216
217 TEST_F(WifiCredentialSyncableServiceTest,
218 ProcessSyncChangesActionAddSecurityClassPskMissingPassphrase) {
219 syncer::SyncChangeList changes;
220 const int sync_item_id = 1;
221 changes.push_back(MakeActionAdd(
222 sync_item_id, kSsidNonUtf8,
223 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_PSK, nullptr));
224 StartSyncing();
225 syncer::SyncError sync_error = ProcessSyncChanges(changes);
226 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored.
227 EXPECT_EQ(0U, config_delegate_add_count());
228 }
229
230 TEST_F(WifiCredentialSyncableServiceTest,
231 ProcessSyncChangesActionAddSecurityClassPskSuccess) {
232 syncer::SyncChangeList changes;
233 const int sync_item_id = 1;
234 const std::string passphrase("psk-passphrase");
235 changes.push_back(MakeActionAdd(
236 sync_item_id, kSsidNonUtf8,
237 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_PSK, &passphrase));
238 StartSyncing();
239 syncer::SyncError sync_error = ProcessSyncChanges(changes);
240 EXPECT_FALSE(sync_error.IsSet());
241 EXPECT_EQ(1U, config_delegate_add_count());
242 }
243
244 TEST_F(WifiCredentialSyncableServiceTest,
245 ProcessSyncChangesContinuesAfterSecurityClassInvalid) {
246 syncer::SyncChangeList changes;
247 changes.push_back(MakeActionAdd(
248 1 /* sync item id */, kSsidNonUtf8,
249 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID, nullptr));
250 changes.push_back(MakeActionAdd(
251 2 /* sync item id */, kSsidNonUtf8,
252 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr));
253 StartSyncing();
254 syncer::SyncError sync_error = ProcessSyncChanges(changes);
255 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored.
256 EXPECT_EQ(1U, config_delegate_add_count());
257 }
258
259 TEST_F(WifiCredentialSyncableServiceTest,
260 ProcessSyncChangesContinuesAfterMissingPassphrase) {
261 syncer::SyncChangeList changes;
262 changes.push_back(MakeActionAdd(
263 1 /* sync item id */, kSsidNonUtf8,
264 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, nullptr));
265 changes.push_back(MakeActionAdd(
266 2 /* sync item id */, kSsidNonUtf8,
267 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr));
268 StartSyncing();
269 syncer::SyncError sync_error = ProcessSyncChanges(changes);
270 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored.
271 EXPECT_EQ(1U, config_delegate_add_count());
272 }
273
274 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksNotStarted) {
275 EXPECT_FALSE(AddToSyncedNetworks(
276 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
277 }
278
279 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksSuccess) {
280 StartSyncing();
281 EXPECT_TRUE(AddToSyncedNetworks(
282 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
283 EXPECT_EQ(1, change_processor_changes_size());
284 }
285
286 } // namespace wifi_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698