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