File: wifi_credential_syncable_service_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (336 lines) | stat: -rw-r--r-- 12,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync_wifi/wifi_credential_syncable_service.h"

#include <stdint.h>

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/time/time.h"
#include "base/tracked_objects.h"
#include "components/sync/model/attachments/attachment_id.h"
#include "components/sync/model/attachments/attachment_service_proxy_for_test.h"
#include "components/sync/model/fake_sync_change_processor.h"
#include "components/sync/model/sync_change.h"
#include "components/sync/model/sync_data.h"
#include "components/sync/model/sync_error.h"
#include "components/sync/model/sync_error_factory_mock.h"
#include "components/sync/protocol/sync.pb.h"
#include "components/sync_wifi/wifi_config_delegate.h"
#include "components/sync_wifi/wifi_credential.h"
#include "components/sync_wifi/wifi_security_class.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace sync_wifi {

using syncer::FakeSyncChangeProcessor;
using syncer::SyncErrorFactoryMock;

namespace {

const char kSsid[] = "fake-ssid";
const char kSsidNonUtf8[] = "\xc0";

// Fake implementation of WifiConfigDelegate, which provides the
// ability to check how many times a WifiConfigDelegate method has
// been called.
class FakeWifiConfigDelegate : public WifiConfigDelegate {
 public:
  FakeWifiConfigDelegate() : add_count_(0) {}
  ~FakeWifiConfigDelegate() override {}

  // WifiConfigDelegate implementation.
  void AddToLocalNetworks(const WifiCredential& network_credential) override {
    ++add_count_;
  }

  // Returns the number of times the AddToLocalNetworks method has
  // been called.
  unsigned int add_count() const { return add_count_; }

 private:
  // The number of times AddToLocalNetworks has been called on this fake.
  unsigned int add_count_;

  DISALLOW_COPY_AND_ASSIGN(FakeWifiConfigDelegate);
};

// Unit tests for WifiCredentialSyncableService.
class WifiCredentialSyncableServiceTest : public testing::Test {
 protected:
  WifiCredentialSyncableServiceTest()
      : config_delegate_(new FakeWifiConfigDelegate()),
        change_processor_(nullptr) {
    syncable_service_.reset(
        new WifiCredentialSyncableService(base::WrapUnique(config_delegate_)));
  }

  // Wrappers for methods in WifiCredentialSyncableService.
  syncer::SyncError ProcessSyncChanges(
      const syncer::SyncChangeList& change_list) {
    return syncable_service_->ProcessSyncChanges(FROM_HERE, change_list);
  }
  bool AddToSyncedNetworks(const std::string& item_id,
                           const WifiCredential& credential) {
    return syncable_service_->AddToSyncedNetworks(item_id, credential);
  }

  // Returns the number of change requests received by
  // |change_processor_|.
  int change_processor_changes_size() {
    CHECK(change_processor_);
    return change_processor_->changes().size();
  }

  // Returns the number of times AddToLocalNetworks has been called on
  // |config_delegate_|.
  unsigned int config_delegate_add_count() {
    return config_delegate_->add_count();
  }

  // Returns a new WifiCredential constructed from the given parameters.
  WifiCredential MakeCredential(const std::string& ssid,
                                WifiSecurityClass security_class,
                                const std::string& passphrase) {
    std::unique_ptr<WifiCredential> credential = WifiCredential::Create(
        WifiCredential::MakeSsidBytesForTest(ssid), security_class, passphrase);
    CHECK(credential);
    return *credential;
  }

  // Returns a new EntitySpecifics protobuf, with the
  // wifi_credential_specifics fields populated with the given
  // parameters.
  sync_pb::EntitySpecifics MakeWifiCredentialSpecifics(
      const std::string& ssid,
      sync_pb::WifiCredentialSpecifics_SecurityClass security_class,
      const std::string* passphrase) {
    const std::vector<uint8_t> ssid_bytes(ssid.begin(), ssid.end());
    sync_pb::EntitySpecifics sync_entity_specifics;
    sync_pb::WifiCredentialSpecifics* wifi_credential_specifics =
        sync_entity_specifics.mutable_wifi_credential();
    CHECK(wifi_credential_specifics);
    wifi_credential_specifics->set_ssid(ssid_bytes.data(), ssid_bytes.size());
    wifi_credential_specifics->set_security_class(security_class);
    if (passphrase) {
      wifi_credential_specifics->set_passphrase(passphrase->data(),
                                                passphrase->size());
    }
    return sync_entity_specifics;
  }

  syncer::SyncChange MakeActionAdd(
      int sync_item_id,
      const std::string& ssid,
      sync_pb::WifiCredentialSpecifics_SecurityClass security_class,
      const std::string* passphrase) {
    return syncer::SyncChange(
        FROM_HERE, syncer::SyncChange::ACTION_ADD,
        syncer::SyncData::CreateRemoteData(
            sync_item_id,
            MakeWifiCredentialSpecifics(ssid, security_class, passphrase),
            base::Time(), syncer::AttachmentIdList(),
            syncer::AttachmentServiceProxyForTest::Create()));
  }

  void StartSyncing() {
    std::unique_ptr<FakeSyncChangeProcessor> change_processor(
        new FakeSyncChangeProcessor());
    change_processor_ = change_processor.get();
    syncable_service_->MergeDataAndStartSyncing(
        syncer::WIFI_CREDENTIALS, syncer::SyncDataList(),
        std::move(change_processor), base::MakeUnique<SyncErrorFactoryMock>());
  }

 private:
  std::unique_ptr<WifiCredentialSyncableService> syncable_service_;
  FakeWifiConfigDelegate* config_delegate_;    // Owned by |syncable_service_|
  FakeSyncChangeProcessor* change_processor_;  // Owned by |syncable_service_|

  DISALLOW_COPY_AND_ASSIGN(WifiCredentialSyncableServiceTest);
};

}  // namespace

TEST_F(WifiCredentialSyncableServiceTest, ProcessSyncChangesNotStarted) {
  syncer::SyncError sync_error(ProcessSyncChanges(syncer::SyncChangeList()));
  ASSERT_TRUE(sync_error.IsSet());
  EXPECT_EQ(syncer::SyncError::UNREADY_ERROR, sync_error.error_type());
  EXPECT_EQ(0U, config_delegate_add_count());
}

TEST_F(WifiCredentialSyncableServiceTest,
       ProcessSyncChangesActionAddSecurityClassInvalid) {
  syncer::SyncChangeList changes;
  const int sync_item_id = 1;
  changes.push_back(MakeActionAdd(
      sync_item_id, kSsidNonUtf8,
      sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID, nullptr));
  StartSyncing();
  syncer::SyncError sync_error = ProcessSyncChanges(changes);
  EXPECT_FALSE(sync_error.IsSet());  // Bad items are ignored.
  EXPECT_EQ(0U, config_delegate_add_count());
}

TEST_F(WifiCredentialSyncableServiceTest,
       ProcessSyncChangesActionAddSecurityClassNoneSuccess) {
  syncer::SyncChangeList changes;
  const int sync_item_id = 1;
  changes.push_back(MakeActionAdd(
      sync_item_id, kSsidNonUtf8,
      sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr));
  StartSyncing();
  syncer::SyncError sync_error = ProcessSyncChanges(changes);
  EXPECT_FALSE(sync_error.IsSet());
  EXPECT_EQ(1U, config_delegate_add_count());
}

TEST_F(WifiCredentialSyncableServiceTest,
       ProcessSyncChangesActionAddSecurityClassWepMissingPassphrase) {
  syncer::SyncChangeList changes;
  const int sync_item_id = 1;
  changes.push_back(MakeActionAdd(
      sync_item_id, kSsidNonUtf8,
      sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, nullptr));
  StartSyncing();
  syncer::SyncError sync_error = ProcessSyncChanges(changes);
  EXPECT_FALSE(sync_error.IsSet());  // Bad items are ignored.
  EXPECT_EQ(0U, config_delegate_add_count());
}

TEST_F(WifiCredentialSyncableServiceTest,
       ProcessSyncChangesActionAddSecurityClassWepSuccess) {
  syncer::SyncChangeList changes;
  const int sync_item_id = 1;
  const std::string passphrase("abcde");
  changes.push_back(MakeActionAdd(
      sync_item_id, kSsidNonUtf8,
      sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, &passphrase));
  StartSyncing();
  syncer::SyncError sync_error = ProcessSyncChanges(changes);
  EXPECT_FALSE(sync_error.IsSet());
  EXPECT_EQ(1U, config_delegate_add_count());
}

TEST_F(WifiCredentialSyncableServiceTest,
       ProcessSyncChangesActionAddSecurityClassPskMissingPassphrase) {
  syncer::SyncChangeList changes;
  const int sync_item_id = 1;
  changes.push_back(MakeActionAdd(
      sync_item_id, kSsidNonUtf8,
      sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_PSK, nullptr));
  StartSyncing();
  syncer::SyncError sync_error = ProcessSyncChanges(changes);
  EXPECT_FALSE(sync_error.IsSet());  // Bad items are ignored.
  EXPECT_EQ(0U, config_delegate_add_count());
}

TEST_F(WifiCredentialSyncableServiceTest,
       ProcessSyncChangesActionAddSecurityClassPskSuccess) {
  syncer::SyncChangeList changes;
  const int sync_item_id = 1;
  const std::string passphrase("psk-passphrase");
  changes.push_back(MakeActionAdd(
      sync_item_id, kSsidNonUtf8,
      sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_PSK, &passphrase));
  StartSyncing();
  syncer::SyncError sync_error = ProcessSyncChanges(changes);
  EXPECT_FALSE(sync_error.IsSet());
  EXPECT_EQ(1U, config_delegate_add_count());
}

TEST_F(WifiCredentialSyncableServiceTest,
       ProcessSyncChangesContinuesAfterSecurityClassInvalid) {
  syncer::SyncChangeList changes;
  changes.push_back(MakeActionAdd(
      1 /* sync item id */, kSsidNonUtf8,
      sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_INVALID, nullptr));
  changes.push_back(MakeActionAdd(
      2 /* sync item id */, kSsidNonUtf8,
      sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr));
  StartSyncing();
  syncer::SyncError sync_error = ProcessSyncChanges(changes);
  EXPECT_FALSE(sync_error.IsSet());  // Bad items are ignored.
  EXPECT_EQ(1U, config_delegate_add_count());
}

TEST_F(WifiCredentialSyncableServiceTest,
       ProcessSyncChangesContinuesAfterMissingPassphrase) {
  syncer::SyncChangeList changes;
  changes.push_back(MakeActionAdd(
      1 /* sync item id */, kSsidNonUtf8,
      sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_WEP, nullptr));
  changes.push_back(MakeActionAdd(
      2 /* sync item id */, kSsidNonUtf8,
      sync_pb::WifiCredentialSpecifics::SECURITY_CLASS_NONE, nullptr));
  StartSyncing();
  syncer::SyncError sync_error = ProcessSyncChanges(changes);
  EXPECT_FALSE(sync_error.IsSet());  // Bad items are ignored.
  EXPECT_EQ(1U, config_delegate_add_count());
}

TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksNotStarted) {
  EXPECT_FALSE(AddToSyncedNetworks(
      "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
}

TEST_F(WifiCredentialSyncableServiceTest, AddToSyncedNetworksSuccess) {
  StartSyncing();
  EXPECT_TRUE(AddToSyncedNetworks(
      "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
  EXPECT_EQ(1, change_processor_changes_size());
}

TEST_F(WifiCredentialSyncableServiceTest,
       AddToSyncedNetworksDifferentSecurityClassesSuccess) {
  StartSyncing();
  EXPECT_TRUE(AddToSyncedNetworks(
      "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
  EXPECT_TRUE(AddToSyncedNetworks(
      "fake-item-id-2", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_WEP, "")));
  EXPECT_EQ(2, change_processor_changes_size());
}

TEST_F(WifiCredentialSyncableServiceTest,
       AddToSyncedNetworksDifferentSsidsSuccess) {
  StartSyncing();
  EXPECT_TRUE(AddToSyncedNetworks(
      "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
  EXPECT_TRUE(AddToSyncedNetworks(
      "fake-item-id-2", MakeCredential(kSsid, SECURITY_CLASS_NONE, "")));
  EXPECT_EQ(2, change_processor_changes_size());
}

TEST_F(WifiCredentialSyncableServiceTest,
       AddToSyncedNetworksDuplicateAddPskNetwork) {
  const std::string passphrase("psk-passphrase");
  StartSyncing();
  EXPECT_TRUE(AddToSyncedNetworks(
      "fake-item-id",
      MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, passphrase)));
  EXPECT_EQ(1, change_processor_changes_size());
  EXPECT_FALSE(AddToSyncedNetworks(
      "fake-item-id",
      MakeCredential(kSsidNonUtf8, SECURITY_CLASS_PSK, passphrase)));
  EXPECT_EQ(1, change_processor_changes_size());
}

TEST_F(WifiCredentialSyncableServiceTest,
       AddToSyncedNetworksDuplicateAddOpenNetwork) {
  StartSyncing();
  EXPECT_TRUE(AddToSyncedNetworks(
      "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
  EXPECT_EQ(1, change_processor_changes_size());
  EXPECT_FALSE(AddToSyncedNetworks(
      "fake-item-id", MakeCredential(kSsidNonUtf8, SECURITY_CLASS_NONE, "")));
  EXPECT_EQ(1, change_processor_changes_size());
}

}  // namespace sync_wifi