Reland: Change the video color space default.
[chromium-blink-merge.git] / components / wifi_sync / wifi_credential_syncable_service_unittest.cc
blob13c8092852ccf310813adb5ccce7184a0be63131
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.
5 #include "components/wifi_sync/wifi_credential_syncable_service.h"
7 #include <string>
8 #include <vector>
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"
27 namespace wifi_sync {
29 using syncer::FakeSyncChangeProcessor;
30 using syncer::SyncErrorFactoryMock;
32 namespace {
34 const char kSsid[] = "fake-ssid";
35 const char kSsidNonUtf8[] = "\xc0";
37 // Fake implementation of WifiConfigDelegate, which provides the
38 // ability to check how many times a WifiConfigDelegate method has
39 // been called.
40 class FakeWifiConfigDelegate : public WifiConfigDelegate {
41 public:
42 FakeWifiConfigDelegate() : add_count_(0) {}
43 ~FakeWifiConfigDelegate() override {}
45 // WifiConfigDelegate implementation.
46 void AddToLocalNetworks(const WifiCredential& network_credential) override {
47 ++add_count_;
50 // Returns the number of times the AddToLocalNetworks method has
51 // been called.
52 unsigned int add_count() const { return add_count_; }
54 private:
55 // The number of times AddToLocalNetworks has been called on this fake.
56 unsigned int add_count_;
58 DISALLOW_COPY_AND_ASSIGN(FakeWifiConfigDelegate);
61 // Unit tests for WifiCredentialSyncableService.
62 class WifiCredentialSyncableServiceTest : public testing::Test {
63 protected:
64 WifiCredentialSyncableServiceTest()
65 : config_delegate_(new FakeWifiConfigDelegate()),
66 change_processor_(nullptr) {
67 syncable_service_.reset(
68 new WifiCredentialSyncableService(make_scoped_ptr(config_delegate_)));
71 // Wrappers for methods in WifiCredentialSyncableService.
72 syncer::SyncError ProcessSyncChanges(
73 const syncer::SyncChangeList& change_list) {
74 return syncable_service_->ProcessSyncChanges(FROM_HERE, change_list);
76 bool AddToSyncedNetworks(const std::string& item_id,
77 const WifiCredential& credential) {
78 return syncable_service_->AddToSyncedNetworks(item_id, credential);
81 // Returns the number of change requests received by
82 // |change_processor_|.
83 int change_processor_changes_size() {
84 CHECK(change_processor_);
85 return change_processor_->changes().size();
88 // Returns the number of times AddToLocalNetworks has been called on
89 // |config_delegate_|.
90 unsigned int config_delegate_add_count() {
91 return config_delegate_->add_count();
94 // Returns a new WifiCredential constructed from the given parameters.
95 WifiCredential MakeCredential(const std::string& ssid,
96 WifiSecurityClass security_class,
97 const std::string& passphrase) {
98 scoped_ptr<WifiCredential> credential = WifiCredential::Create(
99 WifiCredential::MakeSsidBytesForTest(ssid), security_class, passphrase);
100 CHECK(credential);
101 return *credential;
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(passphrase->data(),
120 passphrase->size());
122 return sync_entity_specifics;
125 syncer::SyncChange MakeActionAdd(
126 int sync_item_id,
127 const std::string& ssid,
128 sync_pb::WifiCredentialSpecifics_SecurityClass security_class,
129 const std::string* passphrase) {
130 return syncer::SyncChange(
131 FROM_HERE, syncer::SyncChange::ACTION_ADD,
132 syncer::SyncData::CreateRemoteData(
133 sync_item_id,
134 MakeWifiCredentialSpecifics(ssid, security_class, passphrase),
135 base::Time(), syncer::AttachmentIdList(),
136 syncer::AttachmentServiceProxyForTest::Create()));
139 void StartSyncing() {
140 scoped_ptr<FakeSyncChangeProcessor> change_processor(
141 new FakeSyncChangeProcessor());
142 change_processor_ = change_processor.get();
143 syncable_service_->MergeDataAndStartSyncing(
144 syncer::WIFI_CREDENTIALS, syncer::SyncDataList(),
145 change_processor.Pass(), make_scoped_ptr(new SyncErrorFactoryMock()));
148 private:
149 scoped_ptr<WifiCredentialSyncableService> syncable_service_;
150 FakeWifiConfigDelegate* config_delegate_; // Owned by |syncable_service_|
151 FakeSyncChangeProcessor* change_processor_; // Owned by |syncable_service_|
153 DISALLOW_COPY_AND_ASSIGN(WifiCredentialSyncableServiceTest);
156 } // namespace
158 TEST_F(WifiCredentialSyncableServiceTest, ProcessSyncChangesNotStarted) {
159 syncer::SyncError sync_error(ProcessSyncChanges(syncer::SyncChangeList()));
160 ASSERT_TRUE(sync_error.IsSet());
161 EXPECT_EQ(syncer::SyncError::UNREADY_ERROR, sync_error.error_type());
162 EXPECT_EQ(0U, config_delegate_add_count());
165 TEST_F(WifiCredentialSyncableServiceTest,
166 ProcessSyncChangesActionAddSecurityClassInvalid) {
167 syncer::SyncChangeList changes;
168 const int sync_item_id = 1;
169 changes.push_back(MakeActionAdd(
170 sync_item_id, kSsidNonUtf8,
171 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID, nullptr));
172 StartSyncing();
173 syncer::SyncError sync_error = ProcessSyncChanges(changes);
174 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored.
175 EXPECT_EQ(0U, config_delegate_add_count());
178 TEST_F(WifiCredentialSyncableServiceTest,
179 ProcessSyncChangesActionAddSecurityClassNoneSuccess) {
180 syncer::SyncChangeList changes;
181 const int sync_item_id = 1;
182 changes.push_back(MakeActionAdd(
183 sync_item_id, kSsidNonUtf8,
184 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr));
185 StartSyncing();
186 syncer::SyncError sync_error = ProcessSyncChanges(changes);
187 EXPECT_FALSE(sync_error.IsSet());
188 EXPECT_EQ(1U, config_delegate_add_count());
191 TEST_F(WifiCredentialSyncableServiceTest,
192 ProcessSyncChangesActionAddSecurityClassWepMissingPassphrase) {
193 syncer::SyncChangeList changes;
194 const int sync_item_id = 1;
195 changes.push_back(MakeActionAdd(
196 sync_item_id, kSsidNonUtf8,
197 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, nullptr));
198 StartSyncing();
199 syncer::SyncError sync_error = ProcessSyncChanges(changes);
200 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored.
201 EXPECT_EQ(0U, config_delegate_add_count());
204 TEST_F(WifiCredentialSyncableServiceTest,
205 ProcessSyncChangesActionAddSecurityClassWepSuccess) {
206 syncer::SyncChangeList changes;
207 const int sync_item_id = 1;
208 const std::string passphrase("abcde");
209 changes.push_back(MakeActionAdd(
210 sync_item_id, kSsidNonUtf8,
211 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, &passphrase));
212 StartSyncing();
213 syncer::SyncError sync_error = ProcessSyncChanges(changes);
214 EXPECT_FALSE(sync_error.IsSet());
215 EXPECT_EQ(1U, config_delegate_add_count());
218 TEST_F(WifiCredentialSyncableServiceTest,
219 ProcessSyncChangesActionAddSecurityClassPskMissingPassphrase) {
220 syncer::SyncChangeList changes;
221 const int sync_item_id = 1;
222 changes.push_back(MakeActionAdd(
223 sync_item_id, kSsidNonUtf8,
224 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_PSK, nullptr));
225 StartSyncing();
226 syncer::SyncError sync_error = ProcessSyncChanges(changes);
227 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored.
228 EXPECT_EQ(0U, config_delegate_add_count());
231 TEST_F(WifiCredentialSyncableServiceTest,
232 ProcessSyncChangesActionAddSecurityClassPskSuccess) {
233 syncer::SyncChangeList changes;
234 const int sync_item_id = 1;
235 const std::string passphrase("psk-passphrase");
236 changes.push_back(MakeActionAdd(
237 sync_item_id, kSsidNonUtf8,
238 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_PSK, &passphrase));
239 StartSyncing();
240 syncer::SyncError sync_error = ProcessSyncChanges(changes);
241 EXPECT_FALSE(sync_error.IsSet());
242 EXPECT_EQ(1U, config_delegate_add_count());
245 TEST_F(WifiCredentialSyncableServiceTest,
246 ProcessSyncChangesContinuesAfterSecurityClassInvalid) {
247 syncer::SyncChangeList changes;
248 changes.push_back(MakeActionAdd(
249 1 /* sync item id */, kSsidNonUtf8,
250 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID, nullptr));
251 changes.push_back(MakeActionAdd(
252 2 /* sync item id */, kSsidNonUtf8,
253 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr));
254 StartSyncing();
255 syncer::SyncError sync_error = ProcessSyncChanges(changes);
256 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored.
257 EXPECT_EQ(1U, config_delegate_add_count());
260 TEST_F(WifiCredentialSyncableServiceTest,
261 ProcessSyncChangesContinuesAfterMissingPassphrase) {
262 syncer::SyncChangeList changes;
263 changes.push_back(MakeActionAdd(
264 1 /* sync item id */, kSsidNonUtf8,
265 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, nullptr));
266 changes.push_back(MakeActionAdd(
267 2 /* sync item id */, kSsidNonUtf8,
268 sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr));
269 StartSyncing();
270 syncer::SyncError sync_error = ProcessSyncChanges(changes);
271 EXPECT_FALSE(sync_error.IsSet()); // Bad items are ignored.
272 EXPECT_EQ(1U, config_delegate_add_count());
275 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksNotStarted) {
276 EXPECT_FALSE(AddToSyncedNetworks(
277 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
280 TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksSuccess) {
281 StartSyncing();
282 EXPECT_TRUE(AddToSyncedNetworks(
283 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
284 EXPECT_EQ(1, change_processor_changes_size());
287 TEST_F(WifiCredentialSyncableServiceTest,
288 AddToSyncedNetworksDifferentSecurityClassesSuccess) {
289 StartSyncing();
290 EXPECT_TRUE(AddToSyncedNetworks(
291 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
292 EXPECT_TRUE(AddToSyncedNetworks(
293 "fake-item-id-2",
294 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_WEP, "")));
295 EXPECT_EQ(2, change_processor_changes_size());
298 TEST_F(WifiCredentialSyncableServiceTest,
299 AddToSyncedNetworksDifferentSsidsSuccess) {
300 StartSyncing();
301 EXPECT_TRUE(AddToSyncedNetworks(
302 "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
303 EXPECT_TRUE(AddToSyncedNetworks(
304 "fake-item-id-2", MakeCredential(kSsid, SECURITY_CLASS_NONE, "")));
305 EXPECT_EQ(2, change_processor_changes_size());
308 TEST_F(WifiCredentialSyncableServiceTest,
309 AddToSyncedNetworksDuplicateAddPskNetwork) {
310 const std::string passphrase("psk-passphrase");
311 StartSyncing();
312 EXPECT_TRUE(
313 AddToSyncedNetworks(
314 "fake-item-id",
315 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, passphrase)));
316 EXPECT_EQ(1, change_processor_changes_size());
317 EXPECT_FALSE(
318 AddToSyncedNetworks(
319 "fake-item-id",
320 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, passphrase)));
321 EXPECT_EQ(1, change_processor_changes_size());
324 TEST_F(WifiCredentialSyncableServiceTest,
325 AddToSyncedNetworksDuplicateAddOpenNetwork) {
326 StartSyncing();
327 EXPECT_TRUE(
328 AddToSyncedNetworks(
329 "fake-item-id",
330 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
331 EXPECT_EQ(1, change_processor_changes_size());
332 EXPECT_FALSE(
333 AddToSyncedNetworks(
334 "fake-item-id",
335 MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
336 EXPECT_EQ(1, change_processor_changes_size());
339 } // namespace wifi_sync