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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/sync_wifi/network_test_helper.h"
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chromeos/ash/components/browser_context_helper/browser_context_helper.h"
#include "chromeos/ash/components/browser_context_helper/fake_browser_context_helper_delegate.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "chromeos/ash/components/network/cellular_metrics_logger.h"
#include "chromeos/ash/components/network/network_handler_test_helper.h"
#include "chromeos/ash/components/network/network_metadata_store.h"
#include "chromeos/ash/components/sync_wifi/network_type_conversions.h"
#include "chromeos/ash/services/network_config/in_process_instance.h"
#include "components/account_id/account_id.h"
#include "components/onc/onc_pref_names.h"
#include "components/prefs/testing_pref_service.h"
#include "components/proxy_config/pref_proxy_config_tracker_impl.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/test_helper.h"
#include "google_apis/gaia/gaia_id.h"
namespace ash::sync_wifi {
NetworkTestHelper::NetworkTestHelper()
: CrosNetworkConfigTestHelper(/*initialize= */ false) {
LoginState::Initialize();
PrefProxyConfigTrackerImpl::RegisterProfilePrefs(user_prefs_.registry());
PrefProxyConfigTrackerImpl::RegisterPrefs(local_state_.registry());
auto primary_account_id =
AccountId::FromUserEmailGaiaId("primary@test.com", GaiaId("fakegaia1"));
auto secondary_account_id =
AccountId::FromUserEmailGaiaId("secondary@test.com", GaiaId("fakegaia2"));
network_profile_handler_ = NetworkProfileHandler::InitializeForTesting();
network_configuration_handler_ =
NetworkConfigurationHandler::InitializeForTest(
network_state_helper_.network_state_handler(),
network_device_handler());
ui_proxy_config_service_ = std::make_unique<UIProxyConfigService>(
&user_prefs_, &local_state_,
network_state_helper_.network_state_handler(),
network_profile_handler_.get());
managed_network_configuration_handler_ =
ManagedNetworkConfigurationHandler::InitializeForTesting(
network_state_helper_.network_state_handler(),
network_profile_handler_.get(), network_device_handler(),
network_configuration_handler_.get(), ui_proxy_config_service_.get());
managed_network_configuration_handler_->SetPolicy(
::onc::ONC_SOURCE_DEVICE_POLICY,
/*userhash=*/std::string(),
/*network_configs_onc=*/base::Value::List(),
/*global_network_config=*/base::Value::Dict());
managed_network_configuration_handler_->SetPolicy(
::onc::ONC_SOURCE_USER_POLICY,
user_manager::FakeUserManager::GetFakeUsernameHash(primary_account_id),
/*network_configs_onc=*/base::Value::List(),
/*global_network_config=*/base::Value::Dict());
user_manager::UserManagerImpl::RegisterPrefs(local_state_.registry());
fake_user_manager_.Reset(
std::make_unique<user_manager::FakeUserManager>(&local_state_));
primary_user_ = fake_user_manager_->AddGaiaUser(
primary_account_id, user_manager::UserType::kRegular);
secondary_user_ = fake_user_manager_->AddGaiaUser(
secondary_account_id, user_manager::UserType::kRegular);
browser_context_helper_ = std::make_unique<BrowserContextHelper>(
std::make_unique<FakeBrowserContextHelperDelegate>());
LoginUser(primary_user_);
Initialize(managed_network_configuration_handler_.get());
network_handler_test_helper_ = std::make_unique<NetworkHandlerTestHelper>();
network_handler_test_helper_->RegisterPrefs(user_prefs_.registry(),
local_state_.registry());
}
NetworkTestHelper::~NetworkTestHelper() {
Shutdown();
network_handler_test_helper_.reset();
browser_context_helper_.reset();
secondary_user_ = nullptr;
primary_user_ = nullptr;
fake_user_manager_.Reset();
LoginState::Shutdown();
ui_proxy_config_service_.reset();
}
void NetworkTestHelper::SetUp() {
network_handler_test_helper_->InitializePrefs(&user_prefs_, &local_state_);
network_state_helper_.ResetDevicesAndServices();
network_state_helper_.profile_test()->AddProfile(
BrowserContextHelper::Get()
->GetBrowserContextPathByUserIdHash(primary_user_->username_hash())
.AsUTF8Unsafe(),
primary_user_->username_hash());
base::RunLoop().RunUntilIdle();
}
void NetworkTestHelper::LoginUser(const user_manager::User* user) {
fake_user_manager_->UserLoggedIn(
user->GetAccountId(),
user_manager::TestHelper::GetFakeUsernameHash(user->GetAccountId()));
fake_user_manager_->SwitchActiveUser(user->GetAccountId());
}
std::string NetworkTestHelper::ConfigureWiFiNetwork(
const std::string& ssid,
bool is_secured,
const user_manager::User* user,
bool has_connected,
bool owned_by_user,
bool configured_by_sync,
bool is_from_policy,
bool is_hidden,
bool auto_connect) {
std::string security_entry =
is_secured ? R"("SecurityClass": "psk", "Passphrase": "secretsauce", )"
: R"("SecurityClass": "none", )";
std::string profile_entry = base::StringPrintf(
R"("Profile": "%s", )",
user ? BrowserContextHelper::Get()
->GetBrowserContextPathByUserIdHash(user->username_hash())
.AsUTF8Unsafe()
.c_str()
: "/profile/default");
std::string ui_data = "";
if (is_from_policy) {
ui_data = base::StringPrintf(R"(, "UIData": "{\"onc_source\": \"%s\"}")",
user ? "user_policy" : "device_policy");
}
std::string hidden = "";
if (is_hidden) {
hidden = R"(, "WiFi.HiddenSSID": true)";
}
std::string guid = base::StringPrintf("%s_guid", ssid.c_str());
std::string service_path =
network_state_helper_.ConfigureService(base::StringPrintf(
R"({"GUID": "%s", "Type": "wifi", "SSID": "%s",
%s "State": "ready", "Strength": 100,
%s "AutoConnect": %s, "Connectable": true%s%s})",
guid.c_str(), ssid.c_str(), security_entry.c_str(),
profile_entry.c_str(), auto_connect ? "true" : "false",
ui_data.c_str(), hidden.c_str()));
base::RunLoop().RunUntilIdle();
if (!user) {
if (owned_by_user) {
NetworkHandler::Get()->network_metadata_store()->OnConfigurationCreated(
service_path, guid);
} else {
LoginUser(secondary_user_);
NetworkHandler::Get()->network_metadata_store()->OnConfigurationCreated(
service_path, guid);
LoginUser(primary_user_);
}
}
if (has_connected) {
NetworkHandler::Get()->network_metadata_store()->ConnectSucceeded(
service_path);
}
if (configured_by_sync) {
NetworkHandler::Get()->network_metadata_store()->SetIsConfiguredBySync(
guid);
}
return guid;
}
NetworkStateTestHelper* NetworkTestHelper::network_state_test_helper() {
return &network_state_helper_;
}
} // namespace ash::sync_wifi
|