Commit 0c9f1c75 authored by quiche's avatar quiche Committed by Commit bot

sync: add more integration tests for wifi_sync

Add tests which exercise wifi_sync's ability to add networks.
These tests add a network to the platform wifi config, and then
verify that the new network appears in other sync clients.

BUG=chromium:431435
TEST=sync_integration_tests --gtest_filter="*Wifi*"

Review URL: https://codereview.chromium.org/843483004

Cr-Commit-Position: refs/heads/master@{#315070}
parent b16a2761
include_rules = [ include_rules = [
"+components/onc", # wifi_credentials tests
# TODO(akalin): Figure out finer-grained dependencies. # TODO(akalin): Figure out finer-grained dependencies.
"+sync", "+sync",
] ]
\ No newline at end of file
...@@ -4,20 +4,41 @@ ...@@ -4,20 +4,41 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/macros.h" #include "base/macros.h"
#include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
#include "chrome/browser/sync/test/integration/sync_test.h" #include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/wifi_credentials_helper.h" #include "chrome/browser/sync/test/integration/wifi_credentials_helper.h"
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
#include "components/wifi_sync/wifi_credential.h"
#include "components/wifi_sync/wifi_security_class.h"
using sync_integration_test_util::AwaitCommitActivityCompletion;
using wifi_sync::WifiCredential;
using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet;
class SingleClientWifiCredentialsSyncTest : public SyncTest { class SingleClientWifiCredentialsSyncTest : public SyncTest {
public: public:
SingleClientWifiCredentialsSyncTest() : SyncTest(SINGLE_CLIENT) {} SingleClientWifiCredentialsSyncTest() : SyncTest(SINGLE_CLIENT) {}
~SingleClientWifiCredentialsSyncTest() override {} ~SingleClientWifiCredentialsSyncTest() override {}
// SyncTest implementation.
void SetUp() override {
wifi_credentials_helper::SetUp();
SyncTest::SetUp();
}
void SetUpCommandLine(base::CommandLine* command_line) override { void SetUpCommandLine(base::CommandLine* command_line) override {
SyncTest::SetUpCommandLine(command_line); SyncTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kEnableWifiCredentialSync); command_line->AppendSwitch(switches::kEnableWifiCredentialSync);
} }
bool SetupClients() override {
if (!SyncTest::SetupClients())
return false;
wifi_credentials_helper::SetupClients();
return true;
}
private: private:
DISALLOW_COPY_AND_ASSIGN(SingleClientWifiCredentialsSyncTest); DISALLOW_COPY_AND_ASSIGN(SingleClientWifiCredentialsSyncTest);
}; };
...@@ -27,3 +48,26 @@ IN_PROC_BROWSER_TEST_F(SingleClientWifiCredentialsSyncTest, NoCredentials) { ...@@ -27,3 +48,26 @@ IN_PROC_BROWSER_TEST_F(SingleClientWifiCredentialsSyncTest, NoCredentials) {
ASSERT_TRUE(wifi_credentials_helper::VerifierIsEmpty()); ASSERT_TRUE(wifi_credentials_helper::VerifierIsEmpty());
ASSERT_TRUE(wifi_credentials_helper::ProfileMatchesVerifier(0)); ASSERT_TRUE(wifi_credentials_helper::ProfileMatchesVerifier(0));
} }
IN_PROC_BROWSER_TEST_F(SingleClientWifiCredentialsSyncTest, SingleCredential) {
ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
const char ssid[] = "fake-ssid";
scoped_ptr<WifiCredential> credential =
wifi_credentials_helper::MakeWifiCredential(
ssid, wifi_sync::SECURITY_CLASS_PSK, "fake_passphrase");
ASSERT_TRUE(credential);
const size_t profile_index = 0;
wifi_credentials_helper::AddWifiCredential(
profile_index, "fake_id", *credential);
const WifiCredentialSet verifier_credentials =
wifi_credentials_helper::GetWifiCredentialsForProfile(verifier());
EXPECT_EQ(1U, verifier_credentials.size());
EXPECT_EQ(WifiCredential::MakeSsidBytesForTest(ssid),
verifier_credentials.begin()->ssid());
ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((profile_index))));
EXPECT_TRUE(wifi_credentials_helper::ProfileMatchesVerifier(profile_index));
}
...@@ -4,20 +4,40 @@ ...@@ -4,20 +4,40 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/macros.h" #include "base/macros.h"
#include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h" #include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/wifi_credentials_helper.h" #include "chrome/browser/sync/test/integration/wifi_credentials_helper.h"
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
#include "components/wifi_sync/wifi_credential.h"
#include "components/wifi_sync/wifi_security_class.h"
using wifi_sync::WifiCredential;
using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet;
class TwoClientWifiCredentialsSyncTest : public SyncTest { class TwoClientWifiCredentialsSyncTest : public SyncTest {
public: public:
TwoClientWifiCredentialsSyncTest() : SyncTest(TWO_CLIENT) {} TwoClientWifiCredentialsSyncTest() : SyncTest(TWO_CLIENT) {}
~TwoClientWifiCredentialsSyncTest() override {} ~TwoClientWifiCredentialsSyncTest() override {}
// SyncTest implementation.
void SetUp() override {
wifi_credentials_helper::SetUp();
SyncTest::SetUp();
}
void SetUpCommandLine(base::CommandLine* command_line) override { void SetUpCommandLine(base::CommandLine* command_line) override {
SyncTest::SetUpCommandLine(command_line); SyncTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kEnableWifiCredentialSync); command_line->AppendSwitch(switches::kEnableWifiCredentialSync);
} }
bool SetupClients() override {
if (!SyncTest::SetupClients())
return false;
wifi_credentials_helper::SetupClients();
return true;
}
private: private:
DISALLOW_COPY_AND_ASSIGN(TwoClientWifiCredentialsSyncTest); DISALLOW_COPY_AND_ASSIGN(TwoClientWifiCredentialsSyncTest);
}; };
...@@ -27,3 +47,29 @@ IN_PROC_BROWSER_TEST_F(TwoClientWifiCredentialsSyncTest, NoCredentials) { ...@@ -27,3 +47,29 @@ IN_PROC_BROWSER_TEST_F(TwoClientWifiCredentialsSyncTest, NoCredentials) {
ASSERT_TRUE(wifi_credentials_helper::VerifierIsEmpty()); ASSERT_TRUE(wifi_credentials_helper::VerifierIsEmpty());
ASSERT_TRUE(wifi_credentials_helper::AllProfilesMatch()); ASSERT_TRUE(wifi_credentials_helper::AllProfilesMatch());
} }
IN_PROC_BROWSER_TEST_F(TwoClientWifiCredentialsSyncTest, SingleCredential) {
ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
const char ssid[] = "fake-ssid";
scoped_ptr<WifiCredential> credential =
wifi_credentials_helper::MakeWifiCredential(
ssid, wifi_sync::SECURITY_CLASS_PSK, "fake_passphrase");
ASSERT_TRUE(credential);
const size_t profile_a_index = 0;
wifi_credentials_helper::AddWifiCredential(
profile_a_index, "fake_id", *credential);
const WifiCredentialSet verifier_credentials =
wifi_credentials_helper::GetWifiCredentialsForProfile(verifier());
EXPECT_EQ(1U, verifier_credentials.size());
EXPECT_EQ(WifiCredential::MakeSsidBytesForTest(ssid),
verifier_credentials.begin()->ssid());
const size_t profile_b_index = 1;
ASSERT_TRUE(GetClient(profile_a_index)->AwaitMutualSyncCycleCompletion(
GetClient(profile_b_index)));
EXPECT_FALSE(wifi_credentials_helper::VerifierIsEmpty());
EXPECT_TRUE(wifi_credentials_helper::AllProfilesMatch());
}
...@@ -9,14 +9,17 @@ ...@@ -9,14 +9,17 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/test/integration/sync_datatype_helper.h" #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
#include "chrome/browser/sync/test/integration/sync_test.h" #include "chrome/browser/sync/test/integration/sync_test.h"
#include "components/wifi_sync/wifi_credential.h" #include "components/wifi_sync/wifi_credential_syncable_service.h"
#include "components/wifi_sync/wifi_security_class.h" #include "components/wifi_sync/wifi_credential_syncable_service_factory.h"
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
#include "chrome/browser/sync/test/integration/wifi_credentials_helper_chromeos.h" #include "chrome/browser/sync/test/integration/wifi_credentials_helper_chromeos.h"
#endif #endif
using wifi_sync::WifiCredential; using wifi_sync::WifiCredential;
using wifi_sync::WifiCredentialSyncableService;
using wifi_sync::WifiCredentialSyncableServiceFactory;
using wifi_sync::WifiSecurityClass;
using sync_datatype_helper::test; using sync_datatype_helper::test;
using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet; using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet;
...@@ -25,12 +28,31 @@ namespace wifi_credentials_helper { ...@@ -25,12 +28,31 @@ namespace wifi_credentials_helper {
namespace { namespace {
WifiCredentialSet GetWifiCredentialsForProfile(const Profile* profile) { void SetupClientForProfile(Profile* profile) {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
return GetWifiCredentialsForProfileChromeOs(profile); wifi_credentials_helper::chromeos::SetupClientForProfileChromeOs(profile);
#else #else
NOTIMPLEMENTED(); NOTREACHED();
return WifiCredential::MakeSet(); #endif
}
WifiCredentialSyncableService* GetServiceForBrowserContext(
content::BrowserContext* context) {
return WifiCredentialSyncableServiceFactory::GetForBrowserContext(
context);
}
WifiCredentialSyncableService* GetServiceForProfile(int profile_index) {
return GetServiceForBrowserContext(test()->GetProfile(profile_index));
}
void AddWifiCredentialToProfile(
Profile* profile, const WifiCredential& credential) {
#if defined(OS_CHROMEOS)
wifi_credentials_helper::chromeos::AddWifiCredentialToProfileChromeOs(
profile, credential);
#else
NOTREACHED();
#endif #endif
} }
...@@ -43,7 +65,7 @@ bool CredentialsMatch(const WifiCredentialSet& a_credentials, ...@@ -43,7 +65,7 @@ bool CredentialsMatch(const WifiCredentialSet& a_credentials,
return false; return false;
} }
for (const auto &credential : a_credentials) { for (const WifiCredential& credential : a_credentials) {
if (b_credentials.find(credential) == b_credentials.end()) { if (b_credentials.find(credential) == b_credentials.end()) {
LOG(ERROR) LOG(ERROR)
<< "Network from a not found in b. " << "Network from a not found in b. "
...@@ -61,6 +83,20 @@ bool CredentialsMatch(const WifiCredentialSet& a_credentials, ...@@ -61,6 +83,20 @@ bool CredentialsMatch(const WifiCredentialSet& a_credentials,
} // namespace } // namespace
void SetUp() {
#if defined(OS_CHROMEOS)
wifi_credentials_helper::chromeos::SetUpChromeOs();
#else
NOTREACHED();
#endif
}
void SetupClients() {
SetupClientForProfile(test()->verifier());
for (int i = 0; i < test()->num_clients(); ++i)
SetupClientForProfile(test()->GetProfile(i));
}
bool VerifierIsEmpty() { bool VerifierIsEmpty() {
return GetWifiCredentialsForProfile(test()->verifier()).empty(); return GetWifiCredentialsForProfile(test()->verifier()).empty();
} }
...@@ -92,4 +128,36 @@ bool AllProfilesMatch() { ...@@ -92,4 +128,36 @@ bool AllProfilesMatch() {
return true; return true;
} }
scoped_ptr<WifiCredential> MakeWifiCredential(const std::string& ssid,
WifiSecurityClass security_class,
const std::string& passphrase) {
return WifiCredential::Create(WifiCredential::MakeSsidBytesForTest(ssid),
security_class,
passphrase);
}
void AddWifiCredential(int profile_index,
const std::string& sync_id,
const WifiCredential& credential) {
AddWifiCredentialToProfile(test()->GetProfile(profile_index), credential);
if (test()->use_verifier())
AddWifiCredentialToProfile(test()->verifier(), credential);
// TODO(quiche): Remove this, once we have plumbing to route
// NetworkConfigurationObserver events to
// WifiCredentialSyncableService instances.
GetServiceForProfile(profile_index)
->AddToSyncedNetworks(sync_id, credential);
}
WifiCredentialSet GetWifiCredentialsForProfile(const Profile* profile) {
#if defined(OS_CHROMEOS)
return wifi_credentials_helper::chromeos::
GetWifiCredentialsForProfileChromeOs(profile);
#else
NOTREACHED();
return WifiCredential::MakeSet();
#endif
}
} // namespace wifi_credentials_helper } // namespace wifi_credentials_helper
...@@ -5,11 +5,28 @@ ...@@ -5,11 +5,28 @@
#ifndef CHROME_BROWSER_SYNC_TEST_INTEGRATION_WIFI_CREDENTIALS_HELPER_H_ #ifndef CHROME_BROWSER_SYNC_TEST_INTEGRATION_WIFI_CREDENTIALS_HELPER_H_
#define CHROME_BROWSER_SYNC_TEST_INTEGRATION_WIFI_CREDENTIALS_HELPER_H_ #define CHROME_BROWSER_SYNC_TEST_INTEGRATION_WIFI_CREDENTIALS_HELPER_H_
#include <string>
#include "base/memory/scoped_ptr.h"
#include "components/wifi_sync/wifi_credential.h"
#include "components/wifi_sync/wifi_security_class.h"
class Profile;
// Functions needed by multiple wifi_credentials integration // Functions needed by multiple wifi_credentials integration
// tests. This module is platfrom-agnostic, and calls out to // tests. This module is platfrom-agnostic, and calls out to
// platform-specific code as needed. // platform-specific code as needed.
namespace wifi_credentials_helper { namespace wifi_credentials_helper {
// Performs common setup steps, such as configuring factories. Should
// be called before SyncTest::SetUp.
void SetUp();
// Initializes the clients. This includes associating their Chrome
// Profiles with platform-specific networking state. Should be called
// before adding/removing/modifying WiFi credentials.
void SetupClients();
// Checks if the verifier has any items in it. Returns true iff the // Checks if the verifier has any items in it. Returns true iff the
// verifier has no items. // verifier has no items.
bool VerifierIsEmpty(); bool VerifierIsEmpty();
...@@ -21,6 +38,23 @@ bool ProfileMatchesVerifier(int profile_index); ...@@ -21,6 +38,23 @@ bool ProfileMatchesVerifier(int profile_index);
// Returns true iff all BrowserContexts match with the verifier. // Returns true iff all BrowserContexts match with the verifier.
bool AllProfilesMatch(); bool AllProfilesMatch();
// Returns a new WifiCredential constructed from the given parameters.
scoped_ptr<wifi_sync::WifiCredential> MakeWifiCredential(
const std::string& ssid,
wifi_sync::WifiSecurityClass security_class,
const std::string& passphrase);
// Adds a WiFi credential to the service at index |profile_index|,
// and the verifier (if the SyncTest uses a verifier).
void AddWifiCredential(int profile_index,
const std::string& sync_id,
const wifi_sync::WifiCredential& credential);
// Returns the set of WifiCredentials configured in local network
// settings, for |profile|.
wifi_sync::WifiCredential::CredentialSet GetWifiCredentialsForProfile(
const Profile* profile);
} // namespace wifi_credentials_helper } // namespace wifi_credentials_helper
#endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_WIFI_CREDENTIALS_HELPER_H_ #endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_WIFI_CREDENTIALS_HELPER_H_
...@@ -6,14 +6,24 @@ ...@@ -6,14 +6,24 @@
#include <string> #include <string>
#include "base/bind.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/logging.h" #include "base/logging.h"
#include "chrome/browser/profiles/profile.h" #include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/shill_profile_client.h"
#include "chromeos/network/managed_network_configuration_handler.h"
#include "chromeos/network/network_handler.h" #include "chromeos/network/network_handler.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h" #include "chromeos/network/network_state_handler.h"
#include "components/onc/onc_constants.h"
#include "components/wifi_sync/network_state_helper_chromeos.h" #include "components/wifi_sync/network_state_helper_chromeos.h"
#include "components/wifi_sync/wifi_security_class.h" #include "components/wifi_sync/wifi_credential_syncable_service_factory.h"
#include "content/public/browser/browser_context.h"
using wifi_sync::WifiCredential;
using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet; using WifiCredentialSet = wifi_sync::WifiCredential::CredentialSet;
...@@ -23,27 +33,98 @@ namespace { ...@@ -23,27 +33,98 @@ namespace {
const char kProfilePrefix[] = "/profile/"; const char kProfilePrefix[] = "/profile/";
void LogCreateConfigurationFailure(
const std::string& debug_hint,
const std::string& /* network_config_error_message */,
scoped_ptr<base::DictionaryValue> /* network_config_error_data */) {
LOG(FATAL) << debug_hint;
}
std::string ChromeOsUserHashForBrowserContext( std::string ChromeOsUserHashForBrowserContext(
const content::BrowserContext& context) { const content::BrowserContext& context) {
return context.GetPath().BaseName().value(); return context.GetPath().BaseName().value();
} }
// Return value is distinct per |context|, but otherwise arbitrary.
std::string ShillProfilePathForBrowserContext( std::string ShillProfilePathForBrowserContext(
const content::BrowserContext& context) { const content::BrowserContext& context) {
return kProfilePrefix + ChromeOsUserHashForBrowserContext(context); return kProfilePrefix + ChromeOsUserHashForBrowserContext(context);
} }
chromeos::NetworkStateHandler* GetNetworkStateHandler() { ::chromeos::ShillProfileClient::TestInterface*
DCHECK(chromeos::NetworkHandler::Get()->network_state_handler()); GetShillProfileClientTestInterface() {
return chromeos::NetworkHandler::Get()->network_state_handler(); DCHECK(::chromeos::DBusThreadManager::Get()->GetShillProfileClient());
DCHECK(::chromeos::DBusThreadManager::Get()->GetShillProfileClient()
->GetTestInterface());
return ::chromeos::DBusThreadManager::Get()->GetShillProfileClient()
->GetTestInterface();
}
::chromeos::ManagedNetworkConfigurationHandler*
GetManagedNetworkConfigurationHandler() {
DCHECK(::chromeos::NetworkHandler::Get()
->managed_network_configuration_handler());
return ::chromeos::NetworkHandler::Get()
->managed_network_configuration_handler();
}
::chromeos::NetworkStateHandler* GetNetworkStateHandler() {
DCHECK(::chromeos::NetworkHandler::Get()->network_state_handler());
return ::chromeos::NetworkHandler::Get()->network_state_handler();
} }
} // namespace } // namespace
WifiCredentialSet GetWifiCredentialsForProfileChromeOs(const Profile* profile) { namespace chromeos {
DCHECK(profile);
void SetUpChromeOs() {
wifi_sync::WifiCredentialSyncableServiceFactory::GetInstance()
->set_ignore_login_state_for_test(true);
}
void SetupClientForProfileChromeOs(
const content::BrowserContext* browser_context) {
DCHECK(browser_context);
GetShillProfileClientTestInterface()
->AddProfile(ShillProfilePathForBrowserContext(*browser_context),
ChromeOsUserHashForBrowserContext(*browser_context));
const base::ListValue policy_network_configs;
const base::DictionaryValue policy_global_config;
GetManagedNetworkConfigurationHandler()
->SetPolicy(onc::ONC_SOURCE_UNKNOWN,
ChromeOsUserHashForBrowserContext(*browser_context),
policy_network_configs,
policy_global_config);
}
void AddWifiCredentialToProfileChromeOs(
const content::BrowserContext* browser_context,
const WifiCredential& credential) {
DCHECK(browser_context);
scoped_ptr<base::DictionaryValue> onc_properties =
credential.ToOncProperties();
CHECK(onc_properties) << "Failed to generate ONC properties for "
<< credential.ToString();
GetManagedNetworkConfigurationHandler()
->CreateConfiguration(
ChromeOsUserHashForBrowserContext(*browser_context),
*onc_properties,
::chromeos::network_handler::StringResultCallback(),
base::Bind(LogCreateConfigurationFailure,
base::StringPrintf("Failed to add credential %s",
credential.ToString().c_str())));
base::MessageLoop::current()->RunUntilIdle();
}
WifiCredentialSet GetWifiCredentialsForProfileChromeOs(
const content::BrowserContext* browser_context) {
DCHECK(browser_context);
return wifi_sync::GetWifiCredentialsForShillProfile( return wifi_sync::GetWifiCredentialsForShillProfile(
GetNetworkStateHandler(), ShillProfilePathForBrowserContext(*profile)); GetNetworkStateHandler(),
ShillProfilePathForBrowserContext(*browser_context));
} }
} // namespace chromeos
} // namespace wifi_credentials_helper } // namespace wifi_credentials_helper
...@@ -7,13 +7,34 @@ ...@@ -7,13 +7,34 @@
#include "components/wifi_sync/wifi_credential.h" #include "components/wifi_sync/wifi_credential.h"
class Profile; namespace content {
class BrowserContext;
}
namespace wifi_credentials_helper { namespace wifi_credentials_helper {
// Returns the ChromeOS WiFi credentials associated with |profile|. namespace chromeos {
// Performs ChromeOS-specific setup.
void SetUpChromeOs();
// Performs ChromeOS-specific setup for a given sync client, given
// that client's BrowserContext. Should be called only after SetUpChromeOs.
void SetupClientForProfileChromeOs(
const content::BrowserContext* browser_context);
// Adds a WiFi credential to the ChromeOS networking backend,
// associating the credential with the ChromeOS networking state that
// corresponds to |browser_context|.
void AddWifiCredentialToProfileChromeOs(
const content::BrowserContext* browser_context,
const wifi_sync::WifiCredential& credential);
// Returns the ChromeOS WiFi credentials associated with |browser_context|.
wifi_sync::WifiCredential::CredentialSet GetWifiCredentialsForProfileChromeOs( wifi_sync::WifiCredential::CredentialSet GetWifiCredentialsForProfileChromeOs(
const Profile* profile); const content::BrowserContext* profile);
} // namespace chromeos
} // namespace wifi_credentials_helper } // namespace wifi_credentials_helper
......
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