Commit 2e5d9d47 authored by Jon Mann's avatar Jon Mann Committed by Commit Bot

Update unit tests to use the new NetworkIdentifier class.

This updates test classes and unit tests to avoid using ssid as a key
to uniquely identify networks.

Bug: 966270
Change-Id: Ic1ff783e8685b908cb54555b98356314fdba0d12
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838307Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Commit-Queue: Jon Mann <jonmann@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703546}
parent 9d3555c2
...@@ -27,18 +27,32 @@ static_library("sync_wifi") { ...@@ -27,18 +27,32 @@ static_library("sync_wifi") {
] ]
} }
source_set("test_support") {
testonly = true
sources = [
"test_data_generator.cc",
"test_data_generator.h",
]
deps = [
":sync_wifi",
"//base/test:test_support",
"//components/sync:test_support",
]
}
source_set("unit_tests") { source_set("unit_tests") {
testonly = true testonly = true
sources = [ sources = [
"network_identifier_unittest.cc", "network_identifier_unittest.cc",
"pending_network_configuration_tracker_impl_unittest.cc", "pending_network_configuration_tracker_impl_unittest.cc",
"test_specifics_generator.h",
"wifi_configuration_bridge_unittest.cc", "wifi_configuration_bridge_unittest.cc",
] ]
deps = [ deps = [
":sync_wifi", ":sync_wifi",
":test_support",
"//base/test:test_support", "//base/test:test_support",
"//chromeos/network:network", "//chromeos/dbus/shill:test_support",
"//chromeos/network:test_support",
"//components/sync:test_support", "//components/sync:test_support",
"//components/sync_preferences:test_support", "//components/sync_preferences:test_support",
"//testing/gtest", "//testing/gtest",
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "chromeos/components/sync_wifi/network_identifier.h" #include "chromeos/components/sync_wifi/network_identifier.h"
#include "chromeos/components/sync_wifi/test_specifics_generator.h" #include "chromeos/components/sync_wifi/test_data_generator.h"
#include "chromeos/network/network_state.h" #include "chromeos/network/network_state.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h" #include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
...@@ -28,10 +28,12 @@ class NetworkIdentifierTest : public testing::Test { ...@@ -28,10 +28,12 @@ class NetworkIdentifierTest : public testing::Test {
}; };
TEST_F(NetworkIdentifierTest, FromProto) { TEST_F(NetworkIdentifierTest, FromProto) {
NetworkIdentifier expected_id(kHexSsid, shill::kSecurityPsk);
NetworkIdentifier id = NetworkIdentifier id =
NetworkIdentifier::FromProto(CreateSpecifics(kHexSsid)); NetworkIdentifier::FromProto(GenerateTestWifiSpecifics(expected_id));
EXPECT_EQ(kHexSsid, id.hex_ssid()); EXPECT_EQ(kHexSsid, id.hex_ssid());
EXPECT_EQ(shill::kSecurityPsk, id.security_type()); EXPECT_EQ(shill::kSecurityPsk, id.security_type());
EXPECT_EQ(expected_id, id);
} }
TEST_F(NetworkIdentifierTest, FromNetwork) { TEST_F(NetworkIdentifierTest, FromNetwork) {
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/optional.h" #include "base/optional.h"
#include "chromeos/components/sync_wifi/pending_network_configuration_tracker_impl.h" #include "chromeos/components/sync_wifi/pending_network_configuration_tracker_impl.h"
#include "chromeos/components/sync_wifi/test_specifics_generator.h" #include "chromeos/components/sync_wifi/test_data_generator.h"
#include "components/sync_preferences/testing_pref_service_syncable.h" #include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h" #include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
...@@ -80,115 +80,121 @@ class PendingNetworkConfigurationTrackerImplTest : public testing::Test { ...@@ -80,115 +80,121 @@ class PendingNetworkConfigurationTrackerImplTest : public testing::Test {
ASSERT_EQ(serialized_specifics_wants, serialized_specifics_has); ASSERT_EQ(serialized_specifics_wants, serialized_specifics_has);
} }
const NetworkIdentifier& fred_network_id() const { return fred_network_id_; }
const NetworkIdentifier& mango_network_id() const {
return mango_network_id_;
}
private: private:
std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> std::unique_ptr<sync_preferences::TestingPrefServiceSyncable>
test_pref_service_; test_pref_service_;
std::unique_ptr<PendingNetworkConfigurationTrackerImpl> tracker_; std::unique_ptr<PendingNetworkConfigurationTrackerImpl> tracker_;
const NetworkIdentifier fred_network_id_ = GeneratePskNetworkId(kFredSsid);
const NetworkIdentifier mango_network_id_ = GeneratePskNetworkId(kMangoSsid);
DISALLOW_COPY_AND_ASSIGN(PendingNetworkConfigurationTrackerImplTest); DISALLOW_COPY_AND_ASSIGN(PendingNetworkConfigurationTrackerImplTest);
}; };
TEST_F(PendingNetworkConfigurationTrackerImplTest, TestMarkComplete) { TEST_F(PendingNetworkConfigurationTrackerImplTest, TestMarkComplete) {
NetworkIdentifier id(kFredSsid, shill::kSecurityPsk); tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(),
tracker()->TrackPendingUpdate(kChangeGuid1, id,
/*specifics=*/base::nullopt); /*specifics=*/base::nullopt);
AssertTrackerHasMatchingUpdate(kChangeGuid1, id); AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id());
EXPECT_EQ(1u, GetPref()->DictSize()); EXPECT_EQ(1u, GetPref()->DictSize());
EXPECT_TRUE(DoesPrefContainPendingUpdate(id, kChangeGuid1)); EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_network_id(), kChangeGuid1));
tracker()->MarkComplete(kChangeGuid1, id); tracker()->MarkComplete(kChangeGuid1, fred_network_id());
EXPECT_FALSE(tracker()->GetPendingUpdate(kChangeGuid1, id)); EXPECT_FALSE(tracker()->GetPendingUpdate(kChangeGuid1, fred_network_id()));
EXPECT_EQ(0u, GetPref()->DictSize()); EXPECT_EQ(0u, GetPref()->DictSize());
} }
TEST_F(PendingNetworkConfigurationTrackerImplTest, TestTwoChangesSameNetwork) { TEST_F(PendingNetworkConfigurationTrackerImplTest, TestTwoChangesSameNetwork) {
NetworkIdentifier id(kFredSsid, shill::kSecurityPsk); tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(),
tracker()->TrackPendingUpdate(kChangeGuid1, id,
/*specifics=*/base::nullopt); /*specifics=*/base::nullopt);
tracker()->IncrementCompletedAttempts(kChangeGuid1, id); tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
AssertTrackerHasMatchingUpdate(kChangeGuid1, id, AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id(),
/*completed_attempts=*/1); /*completed_attempts=*/1);
EXPECT_EQ(1u, GetPref()->DictSize()); EXPECT_EQ(1u, GetPref()->DictSize());
EXPECT_EQ( EXPECT_EQ(1, tracker()
1, tracker()->GetPendingUpdate(kChangeGuid1, id)->completed_attempts()); ->GetPendingUpdate(kChangeGuid1, fred_network_id())
->completed_attempts());
tracker()->TrackPendingUpdate(kChangeGuid2, id, tracker()->TrackPendingUpdate(kChangeGuid2, fred_network_id(),
/*specifics=*/base::nullopt); /*specifics=*/base::nullopt);
EXPECT_FALSE(tracker()->GetPendingUpdate(kChangeGuid1, id)); EXPECT_FALSE(tracker()->GetPendingUpdate(kChangeGuid1, fred_network_id()));
AssertTrackerHasMatchingUpdate(kChangeGuid2, id); AssertTrackerHasMatchingUpdate(kChangeGuid2, fred_network_id());
EXPECT_EQ( EXPECT_EQ(0, tracker()
0, tracker()->GetPendingUpdate(kChangeGuid2, id)->completed_attempts()); ->GetPendingUpdate(kChangeGuid2, fred_network_id())
->completed_attempts());
EXPECT_EQ(1u, GetPref()->DictSize()); EXPECT_EQ(1u, GetPref()->DictSize());
} }
TEST_F(PendingNetworkConfigurationTrackerImplTest, TEST_F(PendingNetworkConfigurationTrackerImplTest,
TestTwoChangesDifferentNetworks) { TestTwoChangesDifferentNetworks) {
NetworkIdentifier fred_id(kFredSsid, shill::kSecurityPsk); tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(),
NetworkIdentifier mango_id(kMangoSsid, shill::kSecurityPsk);
tracker()->TrackPendingUpdate(kChangeGuid1, fred_id,
/*specifics=*/base::nullopt); /*specifics=*/base::nullopt);
AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_id); AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id());
EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_id, kChangeGuid1)); EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_network_id(), kChangeGuid1));
EXPECT_EQ(1u, GetPref()->DictSize()); EXPECT_EQ(1u, GetPref()->DictSize());
tracker()->TrackPendingUpdate(kChangeGuid2, mango_id, tracker()->TrackPendingUpdate(kChangeGuid2, mango_network_id(),
/*specifics=*/base::nullopt); /*specifics=*/base::nullopt);
AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_id); AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id());
AssertTrackerHasMatchingUpdate(kChangeGuid2, mango_id); AssertTrackerHasMatchingUpdate(kChangeGuid2, mango_network_id());
EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_id, kChangeGuid1)); EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_network_id(), kChangeGuid1));
EXPECT_TRUE(DoesPrefContainPendingUpdate(mango_id, kChangeGuid2)); EXPECT_TRUE(DoesPrefContainPendingUpdate(mango_network_id(), kChangeGuid2));
EXPECT_EQ(2u, GetPref()->DictSize()); EXPECT_EQ(2u, GetPref()->DictSize());
} }
TEST_F(PendingNetworkConfigurationTrackerImplTest, TestGetPendingUpdates) { TEST_F(PendingNetworkConfigurationTrackerImplTest, TestGetPendingUpdates) {
NetworkIdentifier fred_id(kFredSsid, shill::kSecurityPsk); tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(),
NetworkIdentifier mango_id(kMangoSsid, shill::kSecurityPsk);
tracker()->TrackPendingUpdate(kChangeGuid1, fred_id,
/*specifics=*/base::nullopt); /*specifics=*/base::nullopt);
tracker()->TrackPendingUpdate(kChangeGuid2, mango_id, tracker()->TrackPendingUpdate(kChangeGuid2, mango_network_id(),
/*specifics=*/base::nullopt); /*specifics=*/base::nullopt);
std::vector<PendingNetworkConfigurationUpdate> list = std::vector<PendingNetworkConfigurationUpdate> list =
tracker()->GetPendingUpdates(); tracker()->GetPendingUpdates();
EXPECT_EQ(2u, list.size()); EXPECT_EQ(2u, list.size());
EXPECT_EQ(kChangeGuid1, list[0].change_guid()); EXPECT_EQ(kChangeGuid1, list[0].change_guid());
EXPECT_EQ(fred_id, list[0].id()); EXPECT_EQ(fred_network_id(), list[0].id());
EXPECT_EQ(kChangeGuid2, list[1].change_guid()); EXPECT_EQ(kChangeGuid2, list[1].change_guid());
EXPECT_EQ(mango_id, list[1].id()); EXPECT_EQ(mango_network_id(), list[1].id());
tracker()->MarkComplete(kChangeGuid1, fred_id); tracker()->MarkComplete(kChangeGuid1, fred_network_id());
list = tracker()->GetPendingUpdates(); list = tracker()->GetPendingUpdates();
EXPECT_EQ(1u, list.size()); EXPECT_EQ(1u, list.size());
EXPECT_EQ(kChangeGuid2, list[0].change_guid()); EXPECT_EQ(kChangeGuid2, list[0].change_guid());
EXPECT_EQ(mango_id, list[0].id()); EXPECT_EQ(mango_network_id(), list[0].id());
} }
TEST_F(PendingNetworkConfigurationTrackerImplTest, TestGetPendingUpdate) { TEST_F(PendingNetworkConfigurationTrackerImplTest, TestGetPendingUpdate) {
sync_pb::WifiConfigurationSpecificsData specifics = sync_pb::WifiConfigurationSpecificsData specifics =
CreateSpecifics(kFredSsid); GenerateTestWifiSpecifics(fred_network_id());
NetworkIdentifier fred_id(kFredSsid, shill::kSecurityPsk); tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(), specifics);
NetworkIdentifier mango_id(kMangoSsid, shill::kSecurityPsk);
tracker()->TrackPendingUpdate(kChangeGuid1, fred_id, specifics);
AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_id, AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id(),
/*completed_attempts=*/0, specifics); /*completed_attempts=*/0, specifics);
EXPECT_FALSE(tracker()->GetPendingUpdate(kChangeGuid2, mango_id)); EXPECT_FALSE(tracker()->GetPendingUpdate(kChangeGuid2, mango_network_id()));
} }
TEST_F(PendingNetworkConfigurationTrackerImplTest, TestRetryCounting) { TEST_F(PendingNetworkConfigurationTrackerImplTest, TestRetryCounting) {
NetworkIdentifier id(kFredSsid, shill::kSecurityPsk); tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(),
tracker()->TrackPendingUpdate(kChangeGuid1, id,
/*specifics=*/base::nullopt); /*specifics=*/base::nullopt);
AssertTrackerHasMatchingUpdate(kChangeGuid1, id); AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id());
EXPECT_EQ(1u, GetPref()->DictSize()); EXPECT_EQ(1u, GetPref()->DictSize());
EXPECT_EQ( EXPECT_EQ(0, tracker()
0, tracker()->GetPendingUpdate(kChangeGuid1, id)->completed_attempts()); ->GetPendingUpdate(kChangeGuid1, fred_network_id())
tracker()->IncrementCompletedAttempts(kChangeGuid1, id); ->completed_attempts());
tracker()->IncrementCompletedAttempts(kChangeGuid1, id); tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
tracker()->IncrementCompletedAttempts(kChangeGuid1, id); tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
EXPECT_EQ( tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
3, tracker()->GetPendingUpdate(kChangeGuid1, id)->completed_attempts()); EXPECT_EQ(3, tracker()
tracker()->IncrementCompletedAttempts(kChangeGuid1, id); ->GetPendingUpdate(kChangeGuid1, fred_network_id())
tracker()->IncrementCompletedAttempts(kChangeGuid1, id); ->completed_attempts());
EXPECT_EQ( tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
5, tracker()->GetPendingUpdate(kChangeGuid1, id)->completed_attempts()); tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
EXPECT_EQ(5, tracker()
->GetPendingUpdate(kChangeGuid1, fred_network_id())
->completed_attempts());
} }
} // namespace sync_wifi } // namespace sync_wifi
...@@ -2,21 +2,32 @@ ...@@ -2,21 +2,32 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/strings/string_number_conversions.h"
#include "chromeos/components/sync_wifi/network_identifier.h"
#include "components/sync/protocol/wifi_configuration_specifics.pb.h" #include "components/sync/protocol/wifi_configuration_specifics.pb.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
#ifndef CHROMEOS_COMPONENTS_SYNC_WIFI_TEST_SPECIFICS_GENERATOR_H_
#define CHROMEOS_COMPONENTS_SYNC_WIFI_TEST_SPECIFICS_GENERATOR_H_
namespace sync_wifi { namespace sync_wifi {
namespace { NetworkIdentifier GeneratePskNetworkId(const std::string& ssid) {
return NetworkIdentifier(base::HexEncode(ssid.data(), ssid.size()),
shill::kSecurityPsk);
}
sync_pb::WifiConfigurationSpecificsData CreateSpecifics( sync_pb::WifiConfigurationSpecificsData GenerateTestWifiSpecifics(
const std::string& ssid) { const NetworkIdentifier& id) {
sync_pb::WifiConfigurationSpecificsData specifics; sync_pb::WifiConfigurationSpecificsData specifics;
specifics.set_hex_ssid(ssid); specifics.set_hex_ssid(id.hex_ssid());
specifics.set_security_type(
sync_pb::WifiConfigurationSpecificsData::SECURITY_TYPE_PSK); if (id.security_type() == shill::kSecurityPsk) {
specifics.set_security_type(
sync_pb::WifiConfigurationSpecificsData::SECURITY_TYPE_PSK);
} else if (id.security_type() == shill::kSecurityWep) {
specifics.set_security_type(
sync_pb::WifiConfigurationSpecificsData::SECURITY_TYPE_WEP);
} else {
NOTREACHED();
}
specifics.set_passphrase("password"); specifics.set_passphrase("password");
specifics.set_automatically_connect( specifics.set_automatically_connect(
sync_pb::WifiConfigurationSpecificsData::AUTOMATICALLY_CONNECT_ENABLED); sync_pb::WifiConfigurationSpecificsData::AUTOMATICALLY_CONNECT_ENABLED);
...@@ -24,15 +35,10 @@ sync_pb::WifiConfigurationSpecificsData CreateSpecifics( ...@@ -24,15 +35,10 @@ sync_pb::WifiConfigurationSpecificsData CreateSpecifics(
sync_pb::WifiConfigurationSpecificsData::IS_PREFERRED_ENABLED); sync_pb::WifiConfigurationSpecificsData::IS_PREFERRED_ENABLED);
specifics.set_metered( specifics.set_metered(
sync_pb::WifiConfigurationSpecificsData::METERED_OPTION_AUTO); sync_pb::WifiConfigurationSpecificsData::METERED_OPTION_AUTO);
sync_pb::WifiConfigurationSpecificsData_ProxyConfiguration proxy_config; specifics.mutable_proxy_configuration()->set_proxy_option(
proxy_config.set_proxy_option(sync_pb::WifiConfigurationSpecificsData:: sync_pb::WifiConfigurationSpecificsData::ProxyConfiguration::
ProxyConfiguration::PROXY_OPTION_DISABLED); PROXY_OPTION_DISABLED);
specifics.mutable_proxy_configuration()->CopyFrom(proxy_config);
return specifics; return specifics;
} }
} // namespace
} // namespace sync_wifi } // namespace sync_wifi
#endif // CHROMEOS_COMPONENTS_SYNC_WIFI_TEST_SPECIFICS_GENERATOR_H_
// Copyright 2019 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.
#ifndef CHROMEOS_COMPONENTS_SYNC_WIFI_TEST_DATA_GENERATOR_H_
#define CHROMEOS_COMPONENTS_SYNC_WIFI_TEST_DATA_GENERATOR_H_
#include "components/sync/protocol/wifi_configuration_specifics.pb.h"
namespace sync_wifi {
class NetworkIdentifier;
// Creates a NetworkIdentifier with PSK security for the given |ssid|.
NetworkIdentifier GeneratePskNetworkId(const std::string& ssid);
// Creates a proto with default values and sets the hex_ssid and security_type
// based on the input |id|.
sync_pb::WifiConfigurationSpecificsData GenerateTestWifiSpecifics(
const NetworkIdentifier& id);
} // namespace sync_wifi
#endif // CHROMEOS_COMPONENTS_SYNC_WIFI_TEST_DATA_GENERATOR_H_
...@@ -189,12 +189,12 @@ void WifiConfigurationBridge::Commit( ...@@ -189,12 +189,12 @@ void WifiConfigurationBridge::Commit(
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
std::vector<std::string> WifiConfigurationBridge::GetAllSsidsForTesting() { std::vector<NetworkIdentifier> WifiConfigurationBridge::GetAllIdsForTesting() {
std::vector<std::string> ssids; std::vector<NetworkIdentifier> ids;
for (const auto& entry : entries_) for (const auto& entry : entries_)
ssids.push_back(entry.second.hex_ssid()); ids.push_back(NetworkIdentifier::FromProto(entry.second));
return ssids; return ids;
} }
} // namespace sync_wifi } // namespace sync_wifi
...@@ -50,7 +50,7 @@ class WifiConfigurationBridge : public syncer::ModelTypeSyncBridge { ...@@ -50,7 +50,7 @@ class WifiConfigurationBridge : public syncer::ModelTypeSyncBridge {
std::string GetStorageKey(const syncer::EntityData& entity_data) override; std::string GetStorageKey(const syncer::EntityData& entity_data) override;
// Comes from |entries_| the in-memory map. // Comes from |entries_| the in-memory map.
std::vector<std::string> GetAllSsidsForTesting(); std::vector<NetworkIdentifier> GetAllIdsForTesting();
private: private:
void Commit(std::unique_ptr<syncer::ModelTypeStore::WriteBatch> batch); void Commit(std::unique_ptr<syncer::ModelTypeStore::WriteBatch> batch);
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "chromeos/components/sync_wifi/network_identifier.h" #include "chromeos/components/sync_wifi/network_identifier.h"
#include "chromeos/components/sync_wifi/synced_network_updater.h" #include "chromeos/components/sync_wifi/synced_network_updater.h"
#include "chromeos/components/sync_wifi/test_specifics_generator.h" #include "chromeos/components/sync_wifi/test_data_generator.h"
#include "components/sync/model/entity_change.h" #include "components/sync/model/entity_change.h"
#include "components/sync/model/metadata_batch.h" #include "components/sync/model/metadata_batch.h"
#include "components/sync/model/mock_model_type_change_processor.h" #include "components/sync/model/mock_model_type_change_processor.h"
...@@ -51,20 +51,19 @@ std::unique_ptr<syncer::EntityData> GenerateWifiEntityData( ...@@ -51,20 +51,19 @@ std::unique_ptr<syncer::EntityData> GenerateWifiEntityData(
return entity_data; return entity_data;
} }
bool VectorContainsString(std::vector<std::string> v, std::string s) { bool ProtoVectorContainsId(
return std::find(v.begin(), v.end(), s) != v.end(); const std::vector<sync_pb::WifiConfigurationSpecificsData>& protos,
} NetworkIdentifier id) {
return std::find_if(
bool VectorContainsSsid( protos.begin(), protos.end(),
const std::vector<sync_pb::WifiConfigurationSpecificsData>& v, [&id](const sync_pb::WifiConfigurationSpecificsData& specifics) {
std::string s) { return NetworkIdentifier::FromProto(specifics) == id;
for (sync_pb::WifiConfigurationSpecificsData specifics : v) { }) != protos.end();
if (specifics.hex_ssid() == s)
return true;
}
return false;
} }
// Implementation of SyncedNetworkUpdater. This class takes add/update/delete
// network requests and stores them in its internal data structures without
// actually updating anything external.
class TestSyncedNetworkUpdater : public SyncedNetworkUpdater { class TestSyncedNetworkUpdater : public SyncedNetworkUpdater {
public: public:
TestSyncedNetworkUpdater() = default; TestSyncedNetworkUpdater() = default;
...@@ -134,6 +133,10 @@ class WifiConfigurationBridgeTest : public testing::Test { ...@@ -134,6 +133,10 @@ class WifiConfigurationBridgeTest : public testing::Test {
return synced_network_updater_.get(); return synced_network_updater_.get();
} }
const NetworkIdentifier& woof_network_id() const { return woof_network_id_; }
const NetworkIdentifier& meow_network_id() const { return meow_network_id_; }
private: private:
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
...@@ -145,79 +148,89 @@ class WifiConfigurationBridgeTest : public testing::Test { ...@@ -145,79 +148,89 @@ class WifiConfigurationBridgeTest : public testing::Test {
std::unique_ptr<TestSyncedNetworkUpdater> synced_network_updater_; std::unique_ptr<TestSyncedNetworkUpdater> synced_network_updater_;
const NetworkIdentifier woof_network_id_ = GeneratePskNetworkId(kSsidWoof);
const NetworkIdentifier meow_network_id_ = GeneratePskNetworkId(kSsidMeow);
DISALLOW_COPY_AND_ASSIGN(WifiConfigurationBridgeTest); DISALLOW_COPY_AND_ASSIGN(WifiConfigurationBridgeTest);
}; };
TEST_F(WifiConfigurationBridgeTest, InitWithTwoNetworksFromServer) { TEST_F(WifiConfigurationBridgeTest, InitWithTwoNetworksFromServer) {
syncer::EntityChangeList remote_input; syncer::EntityChangeList remote_input;
WifiConfigurationSpecificsData entry1 = CreateSpecifics(kSsidMeow); WifiConfigurationSpecificsData entry1 =
WifiConfigurationSpecificsData entry2 = CreateSpecifics(kSsidWoof); GenerateTestWifiSpecifics(meow_network_id());
WifiConfigurationSpecificsData entry2 =
GenerateTestWifiSpecifics(woof_network_id());
remote_input.push_back(syncer::EntityChange::CreateAdd( remote_input.push_back(syncer::EntityChange::CreateAdd(
kSsidMeow, GenerateWifiEntityData(entry1))); meow_network_id().SerializeToString(), GenerateWifiEntityData(entry1)));
remote_input.push_back(syncer::EntityChange::CreateAdd( remote_input.push_back(syncer::EntityChange::CreateAdd(
kSsidWoof, GenerateWifiEntityData(entry2))); woof_network_id().SerializeToString(), GenerateWifiEntityData(entry2)));
bridge()->MergeSyncData( bridge()->MergeSyncData(
std::make_unique<syncer::InMemoryMetadataChangeList>(), std::make_unique<syncer::InMemoryMetadataChangeList>(),
std::move(remote_input)); std::move(remote_input));
std::vector<std::string> ssids = bridge()->GetAllSsidsForTesting(); std::vector<NetworkIdentifier> ids = bridge()->GetAllIdsForTesting();
EXPECT_EQ(2u, ssids.size()); EXPECT_EQ(2u, ids.size());
EXPECT_TRUE(VectorContainsString(ssids, kSsidMeow)); EXPECT_TRUE(base::Contains(ids, meow_network_id()));
EXPECT_TRUE(VectorContainsString(ssids, kSsidWoof)); EXPECT_TRUE(base::Contains(ids, woof_network_id()));
const std::vector<sync_pb::WifiConfigurationSpecificsData>& networks = const std::vector<sync_pb::WifiConfigurationSpecificsData>& networks =
synced_network_updater()->add_or_update_calls(); synced_network_updater()->add_or_update_calls();
EXPECT_EQ(2u, networks.size()); EXPECT_EQ(2u, networks.size());
EXPECT_TRUE(VectorContainsSsid(networks, kSsidMeow)); EXPECT_TRUE(ProtoVectorContainsId(networks, meow_network_id()));
EXPECT_TRUE(VectorContainsSsid(networks, kSsidWoof)); EXPECT_TRUE(ProtoVectorContainsId(networks, woof_network_id()));
} }
TEST_F(WifiConfigurationBridgeTest, ApplySyncChangesAddTwoSpecifics) { TEST_F(WifiConfigurationBridgeTest, ApplySyncChangesAddTwoSpecifics) {
const WifiConfigurationSpecificsData specifics1 = CreateSpecifics(kSsidMeow); const WifiConfigurationSpecificsData specifics1 =
const WifiConfigurationSpecificsData specifics2 = CreateSpecifics(kSsidWoof); GenerateTestWifiSpecifics(meow_network_id());
const WifiConfigurationSpecificsData specifics2 =
GenerateTestWifiSpecifics(woof_network_id());
base::Optional<syncer::ModelError> error = base::Optional<syncer::ModelError> error =
bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(),
CreateEntityAddList({specifics1, specifics2})); CreateEntityAddList({specifics1, specifics2}));
EXPECT_FALSE(error); EXPECT_FALSE(error);
std::vector<std::string> ssids = bridge()->GetAllSsidsForTesting(); std::vector<NetworkIdentifier> ids = bridge()->GetAllIdsForTesting();
EXPECT_EQ(2u, ssids.size()); EXPECT_EQ(2u, ids.size());
EXPECT_TRUE(VectorContainsString(ssids, kSsidMeow)); EXPECT_TRUE(base::Contains(ids, meow_network_id()));
EXPECT_TRUE(VectorContainsString(ssids, kSsidWoof)); EXPECT_TRUE(base::Contains(ids, woof_network_id()));
const std::vector<sync_pb::WifiConfigurationSpecificsData>& networks = const std::vector<sync_pb::WifiConfigurationSpecificsData>& networks =
synced_network_updater()->add_or_update_calls(); synced_network_updater()->add_or_update_calls();
EXPECT_EQ(2u, networks.size()); EXPECT_EQ(2u, networks.size());
EXPECT_TRUE(VectorContainsSsid(networks, kSsidMeow)); EXPECT_TRUE(ProtoVectorContainsId(networks, meow_network_id()));
EXPECT_TRUE(VectorContainsSsid(networks, kSsidWoof)); EXPECT_TRUE(ProtoVectorContainsId(networks, woof_network_id()));
} }
TEST_F(WifiConfigurationBridgeTest, ApplySyncChangesOneAdd) { TEST_F(WifiConfigurationBridgeTest, ApplySyncChangesOneAdd) {
WifiConfigurationSpecificsData entry = CreateSpecifics(kSsidMeow); WifiConfigurationSpecificsData entry =
GenerateTestWifiSpecifics(meow_network_id());
syncer::EntityChangeList add_changes; syncer::EntityChangeList add_changes;
add_changes.push_back(syncer::EntityChange::CreateAdd( add_changes.push_back(syncer::EntityChange::CreateAdd(
kSsidMeow, GenerateWifiEntityData(entry))); meow_network_id().SerializeToString(), GenerateWifiEntityData(entry)));
bridge()->ApplySyncChanges( bridge()->ApplySyncChanges(
std::make_unique<syncer::InMemoryMetadataChangeList>(), std::make_unique<syncer::InMemoryMetadataChangeList>(),
std::move(add_changes)); std::move(add_changes));
std::vector<std::string> ssids = bridge()->GetAllSsidsForTesting(); std::vector<NetworkIdentifier> ids = bridge()->GetAllIdsForTesting();
EXPECT_EQ(1u, ssids.size()); EXPECT_EQ(1u, ids.size());
EXPECT_TRUE(VectorContainsString(ssids, kSsidMeow)); EXPECT_TRUE(base::Contains(ids, meow_network_id()));
const std::vector<sync_pb::WifiConfigurationSpecificsData>& networks = const std::vector<sync_pb::WifiConfigurationSpecificsData>& networks =
synced_network_updater()->add_or_update_calls(); synced_network_updater()->add_or_update_calls();
EXPECT_EQ(1u, networks.size()); EXPECT_EQ(1u, networks.size());
EXPECT_TRUE(VectorContainsSsid(networks, kSsidMeow)); EXPECT_TRUE(ProtoVectorContainsId(networks, meow_network_id()));
} }
TEST_F(WifiConfigurationBridgeTest, ApplySyncChangesOneDeletion) { TEST_F(WifiConfigurationBridgeTest, ApplySyncChangesOneDeletion) {
WifiConfigurationSpecificsData entry = CreateSpecifics(kSsidMeow); WifiConfigurationSpecificsData entry =
GenerateTestWifiSpecifics(meow_network_id());
NetworkIdentifier id = NetworkIdentifier::FromProto(entry); NetworkIdentifier id = NetworkIdentifier::FromProto(entry);
syncer::EntityChangeList add_changes; syncer::EntityChangeList add_changes;
...@@ -227,14 +240,14 @@ TEST_F(WifiConfigurationBridgeTest, ApplySyncChangesOneDeletion) { ...@@ -227,14 +240,14 @@ TEST_F(WifiConfigurationBridgeTest, ApplySyncChangesOneDeletion) {
bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(),
std::move(add_changes)); std::move(add_changes));
std::vector<std::string> ssids = bridge()->GetAllSsidsForTesting(); std::vector<NetworkIdentifier> ids = bridge()->GetAllIdsForTesting();
EXPECT_EQ(1u, ssids.size()); EXPECT_EQ(1u, ids.size());
EXPECT_TRUE(VectorContainsString(ssids, kSsidMeow)); EXPECT_TRUE(base::Contains(ids, meow_network_id()));
const std::vector<sync_pb::WifiConfigurationSpecificsData>& networks = const std::vector<sync_pb::WifiConfigurationSpecificsData>& networks =
synced_network_updater()->add_or_update_calls(); synced_network_updater()->add_or_update_calls();
EXPECT_EQ(1u, networks.size()); EXPECT_EQ(1u, networks.size());
EXPECT_TRUE(VectorContainsSsid(networks, kSsidMeow)); EXPECT_TRUE(ProtoVectorContainsId(networks, meow_network_id()));
syncer::EntityChangeList delete_changes; syncer::EntityChangeList delete_changes;
delete_changes.push_back( delete_changes.push_back(
...@@ -242,7 +255,7 @@ TEST_F(WifiConfigurationBridgeTest, ApplySyncChangesOneDeletion) { ...@@ -242,7 +255,7 @@ TEST_F(WifiConfigurationBridgeTest, ApplySyncChangesOneDeletion) {
bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(),
std::move(delete_changes)); std::move(delete_changes));
EXPECT_TRUE(bridge()->GetAllSsidsForTesting().empty()); EXPECT_TRUE(bridge()->GetAllIdsForTesting().empty());
const std::vector<NetworkIdentifier>& removed_networks = const std::vector<NetworkIdentifier>& removed_networks =
synced_network_updater()->remove_calls(); synced_network_updater()->remove_calls();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment