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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
|
// Copyright 2014 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/network/auto_connect_handler.h"
#include <memory>
#include <string>
#include <utility>
#include "ash/constants/ash_features.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/json/json_reader.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/network/client_cert_resolver.h"
#include "chromeos/ash/components/network/managed_network_configuration_handler_impl.h"
#include "chromeos/ash/components/network/network_cert_loader.h"
#include "chromeos/ash/components/network/network_configuration_handler.h"
#include "chromeos/ash/components/network/network_connection_handler.h"
#include "chromeos/ash/components/network/network_profile_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_test_helper.h"
#include "chromeos/ash/components/network/system_token_cert_db_storage.h"
#include "components/onc/onc_constants.h"
#include "crypto/scoped_nss_types.h"
#include "crypto/scoped_test_nss_db.h"
#include "net/base/net_errors.h"
#include "net/cert/nss_cert_database_chromeos.h"
#include "net/cert/x509_certificate.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace ash {
namespace {
constexpr char kESimDisconnectByPolicyHistogram[] =
"Network.Cellular.ESim.DisconnectByPolicy.Result";
constexpr char kPSimDisconnectByPolicyHistogram[] =
"Network.Cellular.PSim.DisconnectByPolicy.Result";
class TestAutoConnectHandlerObserver : public AutoConnectHandler::Observer {
public:
TestAutoConnectHandlerObserver() = default;
virtual ~TestAutoConnectHandlerObserver() = default;
int num_auto_connect_events() { return num_auto_connect_events_; }
int auto_connect_reasons() { return auto_connect_reasons_; }
// AutoConnectHandler::Observer:
void OnAutoConnectedInitiated(int auto_connect_reasons) override {
++num_auto_connect_events_;
auto_connect_reasons_ = auto_connect_reasons;
}
private:
int num_auto_connect_events_ = 0;
int auto_connect_reasons_ = 0;
};
class TestCertResolveObserver : public ClientCertResolver::Observer {
public:
explicit TestCertResolveObserver(ClientCertResolver* cert_resolver)
: changed_network_properties_(false), cert_resolver_(cert_resolver) {
cert_resolver_->AddObserver(this);
}
void ResolveRequestCompleted(bool changed_network_properties) override {
cert_resolver_->RemoveObserver(this);
changed_network_properties_ = changed_network_properties;
}
bool DidNetworkPropertiesChange() { return changed_network_properties_; }
private:
bool changed_network_properties_;
raw_ptr<ClientCertResolver> cert_resolver_;
};
class TestNetworkConnectionHandler : public NetworkConnectionHandler {
public:
explicit TestNetworkConnectionHandler(
base::RepeatingCallback<void(const std::string&)> disconnect_handler)
: disconnect_handler_(std::move(disconnect_handler)) {}
~TestNetworkConnectionHandler() override = default;
// NetworkConnectionHandler:
void DisconnectNetwork(
const std::string& service_path,
base::OnceClosure success_callback,
network_handler::ErrorCallback error_callback) override {
disconnect_handler_.Run(service_path);
std::move(success_callback).Run();
}
void ConnectToNetwork(const std::string& service_path,
base::OnceClosure success_callback,
network_handler::ErrorCallback error_callback,
bool check_error_state,
ConnectCallbackMode mode) override {}
void Init(
NetworkStateHandler* network_state_handler,
NetworkConfigurationHandler* network_configuration_handler,
ManagedNetworkConfigurationHandler* managed_network_configuration_handler,
CellularConnectionHandler* cellular_connection_handler) override {}
void OnAutoConnectedInitiated(int auto_connect_reasons) override {
++num_auto_connect_events_;
auto_connect_reasons_ = auto_connect_reasons;
}
int num_auto_connect_events() { return num_auto_connect_events_; }
int auto_connect_reasons() { return auto_connect_reasons_; }
private:
base::RepeatingCallback<void(const std::string&)> disconnect_handler_;
int num_auto_connect_events_ = 0;
int auto_connect_reasons_ = 0;
};
} // namespace
class AutoConnectHandlerTest : public testing::Test {
public:
AutoConnectHandlerTest()
: task_environment_(
base::test::TaskEnvironment::MainThreadType::UI,
base::test::SingleThreadTaskEnvironment::TimeSource::MOCK_TIME) {}
AutoConnectHandlerTest(const AutoConnectHandlerTest&) = delete;
AutoConnectHandlerTest& operator=(const AutoConnectHandlerTest&) = delete;
void SetUp() override {
ASSERT_TRUE(test_nssdb_.is_open());
// Use the same DB for public and private slot.
test_nsscertdb_ = std::make_unique<net::NSSCertDatabaseChromeOS>(
crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_.slot())),
crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_.slot())));
SystemTokenCertDbStorage::Initialize();
NetworkCertLoader::Initialize();
NetworkCertLoader::ForceAvailableForNetworkAuthForTesting();
LoginState::Initialize();
LoginState::Get()->set_always_logged_in(false);
network_config_handler_ = NetworkConfigurationHandler::InitializeForTest(
helper_.network_state_handler(), nullptr /* network_device_handler */);
network_profile_handler_.reset(new NetworkProfileHandler());
network_profile_handler_->Init();
managed_config_handler_.reset(new ManagedNetworkConfigurationHandlerImpl());
managed_config_handler_->Init(
/*cellular_policy_handler=*/nullptr,
/*managed_cellular_pref_handler=*/nullptr,
helper_.network_state_handler(), network_profile_handler_.get(),
network_config_handler_.get(), nullptr /* network_device_handler */,
nullptr /* prohibited_technologies_handler */,
/*hotspot_controller=*/nullptr);
test_network_connection_handler_ =
std::make_unique<TestNetworkConnectionHandler>(base::BindRepeating(
&AutoConnectHandlerTest::SetDisconnected, base::Unretained(this)));
client_cert_resolver_ = std::make_unique<ClientCertResolver>();
client_cert_resolver_->Init(helper_.network_state_handler(),
managed_config_handler_.get());
auto_connect_handler_.reset(new AutoConnectHandler());
auto_connect_handler_->Init(
client_cert_resolver_.get(), test_network_connection_handler_.get(),
helper_.network_state_handler(), managed_config_handler_.get());
test_observer_ = std::make_unique<TestAutoConnectHandlerObserver>();
auto_connect_handler_->AddObserver(test_observer_.get());
task_environment_.RunUntilIdle();
}
void TearDown() override {
auto_connect_handler_->RemoveObserver(test_observer_.get());
auto_connect_handler_.reset();
test_network_connection_handler_.reset();
client_cert_resolver_.reset();
managed_config_handler_.reset();
network_profile_handler_.reset();
network_config_handler_.reset();
LoginState::Shutdown();
NetworkCertLoader::Shutdown();
SystemTokenCertDbStorage::Shutdown();
}
protected:
void SetDisconnected(const std::string& service_path) {
helper_.SetServiceProperty(service_path, shill::kStateProperty,
base::Value(shill::kStateIdle));
}
std::string GetServiceState(const std::string& service_path) {
return helper_.GetServiceStringProperty(service_path,
shill::kStateProperty);
}
void StartNetworkCertLoader() {
NetworkCertLoader::Get()->SetUserNSSDB(test_nsscertdb_.get());
task_environment_.RunUntilIdle();
}
void LoginToRegularUser() {
LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
LoginState::LOGGED_IN_USER_REGULAR);
task_environment_.RunUntilIdle();
}
scoped_refptr<net::X509Certificate> ImportTestClientCert() {
net::ScopedCERTCertificateList ca_cert_list =
net::CreateCERTCertificateListFromFile(
net::GetTestCertsDirectory(), "client_1_ca.pem",
net::X509Certificate::FORMAT_AUTO);
if (ca_cert_list.empty()) {
LOG(ERROR) << "No CA cert loaded.";
return nullptr;
}
net::NSSCertDatabase::ImportCertFailureList failures;
EXPECT_TRUE(test_nsscertdb_->ImportCACerts(
ca_cert_list, net::NSSCertDatabase::TRUST_DEFAULT, &failures));
if (!failures.empty()) {
LOG(ERROR) << net::ErrorToString(failures[0].net_error);
return nullptr;
}
// Import a client cert signed by that CA.
scoped_refptr<net::X509Certificate> client_cert(
net::ImportClientCertAndKeyFromFile(net::GetTestCertsDirectory(),
"client_1.pem", "client_1.pk8",
test_nssdb_.slot()));
return client_cert;
}
void SetupUserPolicy(const std::string& network_configs_json) {
base::Value::List network_configs;
if (!network_configs_json.empty()) {
auto parsed_json = base::JSONReader::ReadAndReturnValueWithError(
network_configs_json, base::JSON_ALLOW_TRAILING_COMMAS);
ASSERT_TRUE(parsed_json.has_value()) << parsed_json.error().message;
ASSERT_TRUE(parsed_json->is_list());
network_configs = std::move(parsed_json->GetList());
}
managed_config_handler_->SetPolicy(
::onc::ONC_SOURCE_USER_POLICY, helper_.UserHash(), network_configs,
/*global_network_config=*/base::Value::Dict());
task_environment_.RunUntilIdle();
}
void SetupDevicePolicy(const std::string& network_configs_json,
const base::Value::Dict& global_config) {
base::Value::List network_configs;
if (!network_configs_json.empty()) {
auto parsed_json = base::JSONReader::ReadAndReturnValueWithError(
network_configs_json, base::JSON_ALLOW_TRAILING_COMMAS);
ASSERT_TRUE(parsed_json.has_value()) << parsed_json.error().message;
ASSERT_TRUE(parsed_json->is_list());
network_configs = std::move(parsed_json->GetList());
}
managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY,
std::string(), // no username hash
network_configs, global_config);
task_environment_.RunUntilIdle();
}
std::string ConfigureService(const std::string& shill_json_string) {
return helper_.ConfigureService(shill_json_string);
}
void CheckServiceAutoConnectProperty(const std::string& service_path,
bool expected_auto_connect) {
const base::Value::Dict* properties =
helper().service_test()->GetServiceProperties(service_path);
std::optional<bool> auto_connect =
properties->FindBool(shill::kAutoConnectProperty);
EXPECT_TRUE(auto_connect);
EXPECT_EQ(*auto_connect, expected_auto_connect);
}
NetworkStateTestHelper& helper() { return helper_; }
base::test::TaskEnvironment task_environment_;
base::test::ScopedFeatureList scoped_feature_list_;
NetworkStateTestHelper helper_{/*use_default_devices_and_services=*/false};
std::unique_ptr<AutoConnectHandler> auto_connect_handler_;
std::unique_ptr<ClientCertResolver> client_cert_resolver_;
std::unique_ptr<NetworkConfigurationHandler> network_config_handler_;
std::unique_ptr<ManagedNetworkConfigurationHandlerImpl>
managed_config_handler_;
std::unique_ptr<TestNetworkConnectionHandler>
test_network_connection_handler_;
std::unique_ptr<NetworkProfileHandler> network_profile_handler_;
crypto::ScopedTestNSSDB test_nssdb_;
std::unique_ptr<net::NSSCertDatabaseChromeOS> test_nsscertdb_;
std::unique_ptr<TestAutoConnectHandlerObserver> test_observer_;
};
namespace {
const char* kConfigureCellular1UnmanagedConnected = R"(
{ "GUID": "cellular1", "Type": "cellular", "State": "online",
"AutoConnect": true, "Profile": "/profile/default" })";
const char* kConfigureCellular2ManagedConnectable = R"(
{ "GUID": "cellular2", "Type": "cellular", "State": "idle",
"AutoConnect": true, "Profile": "/profile/default" })";
const char* kConfigureCellular3UnmanagedConnected = R"(
{ "GUID": "cellular3", "Type": "cellular", "State": "online",
"AutoConnect": true, "Profile": "/profile/default",
"Cellular.EID": "1234567890"})";
const char* kConfigWifi0UnmanagedSharedConnected = R"(
{ "GUID": "wifi0", "Type": "wifi", "State": "online",
"Security": "wpa", "Profile": "/profile/default" })";
const char* kConfigWifi1ManagedSharedConnectable = R"(
{ "GUID": "wifi1", "Type": "wifi", "State": "idle",
"Connectable": true, "Security": "wpa", "Profile":
"/profile/default" })";
// HexSSID 7769666931 is "wifi1".
const char* kPolicy = R"(
[ { "GUID": "wifi1",
"Name": "wifi1",
"Type": "WiFi",
"WiFi": {
"Security": "WPA-PSK",
"HexSSID": "7769666931",
"Passphrase": "passphrase"
}
} ])";
// HexSSID 7769666931 is "wifi1".
const char* kPolicyCertPattern = R"(
[ { "GUID": "wifi1",
"Name": "wifi1",
"Type": "WiFi",
"WiFi": {
"Security": "WPA-EAP",
"HexSSID": "7769666931",
"EAP": {
"Outer": "EAP-TLS",
"ClientCertType": "Pattern",
"ClientCertPattern": {
"Issuer": {
"CommonName": "B CA"
}
}
}
}
} ])";
const char* kCellularPolicy = R"(
[
{ "GUID": "cellular2",
"Name": "cellular2",
"Type": "Cellular",
"Cellular": {
"SMDPAddress": "123"
}
}
])";
} // namespace
TEST_F(AutoConnectHandlerTest, ReconnectOnCertLoading) {
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
helper().manager_test()->SetBestServiceToConnect(wifi1_service_path);
// User login shouldn't trigger any change until the certificates and policy
// are loaded.
LoginToRegularUser();
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
// Applying the policy which restricts autoconnect should disconnect from the
// shared, unmanaged network.
auto global_config = base::Value::Dict().Set(
::onc::global_network_config::kAllowOnlyPolicyNetworksToAutoconnect,
true);
SetupUserPolicy(/*network_configs_json=*/std::string());
SetupDevicePolicy(kPolicy, global_config);
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
// Certificate loading should trigger connecting to the 'best' network.
StartNetworkCertLoader();
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi1_service_path));
EXPECT_EQ(1, test_observer_->num_auto_connect_events());
EXPECT_EQ(1, test_network_connection_handler_->num_auto_connect_events());
EXPECT_EQ(AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN |
AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED,
test_observer_->auto_connect_reasons());
EXPECT_EQ(AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN |
AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED,
test_network_connection_handler_->auto_connect_reasons());
}
TEST_F(AutoConnectHandlerTest, ReconnectOnCertPatternResolved) {
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
helper().manager_test()->SetBestServiceToConnect(wifi0_service_path);
SetupDevicePolicy(
/*network_configs_json=*/std::string(),
/*global_config=*/base::Value::Dict());
EXPECT_EQ(0, test_observer_->num_auto_connect_events());
LoginToRegularUser();
SetupUserPolicy(kPolicyCertPattern);
StartNetworkCertLoader();
EXPECT_EQ(1, test_observer_->num_auto_connect_events());
EXPECT_EQ(1, test_network_connection_handler_->num_auto_connect_events());
EXPECT_EQ(AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN |
AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED |
AutoConnectHandler::AUTO_CONNECT_REASON_CERTIFICATE_RESOLVED,
test_observer_->auto_connect_reasons());
EXPECT_EQ(AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN |
AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED |
AutoConnectHandler::AUTO_CONNECT_REASON_CERTIFICATE_RESOLVED,
test_network_connection_handler_->auto_connect_reasons());
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
helper().manager_test()->SetBestServiceToConnect(wifi1_service_path);
TestCertResolveObserver observer(client_cert_resolver_.get());
scoped_refptr<net::X509Certificate> cert = ImportTestClientCert();
ASSERT_TRUE(cert.get());
task_environment_.RunUntilIdle();
EXPECT_TRUE(observer.DidNetworkPropertiesChange());
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi1_service_path));
EXPECT_EQ(2, test_observer_->num_auto_connect_events());
EXPECT_EQ(2, test_network_connection_handler_->num_auto_connect_events());
EXPECT_EQ(AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN |
AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED |
AutoConnectHandler::AUTO_CONNECT_REASON_CERTIFICATE_RESOLVED,
test_observer_->auto_connect_reasons());
EXPECT_EQ(AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN |
AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED |
AutoConnectHandler::AUTO_CONNECT_REASON_CERTIFICATE_RESOLVED,
test_network_connection_handler_->auto_connect_reasons());
}
// Ensure that resolving of certificate patterns only triggers a reconnect if at
// least one pattern was resolved.
TEST_F(AutoConnectHandlerTest, NoReconnectIfNoCertResolved) {
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
helper().manager_test()->SetBestServiceToConnect(wifi0_service_path);
SetupDevicePolicy(
/*network_configs_json=*/std::string(),
/*global_config=*/base::Value::Dict());
LoginToRegularUser();
StartNetworkCertLoader();
SetupUserPolicy(kPolicy);
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
helper().manager_test()->SetBestServiceToConnect(wifi1_service_path);
TestCertResolveObserver observer(client_cert_resolver_.get());
scoped_refptr<net::X509Certificate> cert = ImportTestClientCert();
ASSERT_TRUE(cert.get());
task_environment_.RunUntilIdle();
EXPECT_FALSE(observer.DidNetworkPropertiesChange());
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_EQ(1, test_observer_->num_auto_connect_events());
EXPECT_EQ(1, test_network_connection_handler_->num_auto_connect_events());
EXPECT_EQ(AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN |
AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED,
test_observer_->auto_connect_reasons());
EXPECT_EQ(AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN |
AutoConnectHandler::AUTO_CONNECT_REASON_POLICY_APPLIED,
test_network_connection_handler_->auto_connect_reasons());
}
TEST_F(AutoConnectHandlerTest, DisconnectOnPolicyLoading) {
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
// User login and certificate loading shouldn't trigger any change until the
// policy is loaded.
LoginToRegularUser();
StartNetworkCertLoader();
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
auto global_config = base::Value::Dict().Set(
::onc::global_network_config::kAllowOnlyPolicyNetworksToAutoconnect,
true);
// Applying the policy which restricts autoconnect should disconnect from the
// shared, unmanaged network.
// Because no best service is set, the fake implementation of
// ConnectToBestServices will be a no-op.
SetupDevicePolicy(kPolicy, global_config);
// Should not trigger any change until user policy is loaded
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
SetupUserPolicy(/*network_configs_json=*/std::string());
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_EQ(0, test_observer_->num_auto_connect_events());
}
TEST_F(AutoConnectHandlerTest, AutoConnectOnDevicePolicyApplied) {
// Initial state: wifi0 is online, wifi1 is idle.
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
// When shill's ConnectToBestServices is called, wifi1 should be come online.
helper().manager_test()->SetBestServiceToConnect(wifi1_service_path);
// Starting NetworkCertLoader doesn't change anything yet if policy is not
// applied.
StartNetworkCertLoader();
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
// Apply device policy which should trigger ConnectToBestServices.
SetupDevicePolicy(kPolicy,
/*global_config=*/base::Value::Dict());
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi1_service_path));
EXPECT_EQ(1, test_observer_->num_auto_connect_events());
EXPECT_EQ(1, test_network_connection_handler_->num_auto_connect_events());
}
TEST_F(AutoConnectHandlerTest, AutoConnectOnUserPolicyApplied) {
// Initial state: wifi0 is online, wifi1 is idle.
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
// Device policy has already been applied.
SetupDevicePolicy(std::string(),
/*global_config=*/base::Value::Dict());
// When shill's ConnectToBestServices is called, wifi1 should be come online.
helper().manager_test()->SetBestServiceToConnect(wifi1_service_path);
// Starting NetworkCertLoader and log in as a user. Nothing happens yet
// because user policy is not applied yet.
StartNetworkCertLoader();
LoginToRegularUser();
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
// Applying user policy should trigger connection to "best" service.
SetupUserPolicy(kPolicy);
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi1_service_path));
EXPECT_EQ(1, test_observer_->num_auto_connect_events());
EXPECT_EQ(1, test_network_connection_handler_->num_auto_connect_events());
}
TEST_F(AutoConnectHandlerTest,
DisconnectOnPolicyLoadingAllowOnlyPolicyWiFiToConnect) {
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
// User login and certificate loading shouldn't trigger any change until the
// policy is loaded.
LoginToRegularUser();
StartNetworkCertLoader();
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
auto global_config = base::Value::Dict().Set(
::onc::global_network_config::kAllowOnlyPolicyWiFiToConnect, true);
// Applying the policy which restricts connections should disconnect from the
// shared, unmanaged network.
// Because no best service is set, the fake implementation of
// ConnectToBestServices will be a no-op.
SetupDevicePolicy(kPolicy, global_config);
// Should not trigger any change until user policy is loaded
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
SetupUserPolicy(/*network_configs_json=*/std::string());
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_EQ(0, test_observer_->num_auto_connect_events());
}
// After login a reconnect is triggered even if there is no managed network.
TEST_F(AutoConnectHandlerTest, ReconnectAfterLogin) {
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
helper().manager_test()->SetBestServiceToConnect(wifi1_service_path);
// User login and certificate loading shouldn't trigger any change until the
// policy is loaded.
LoginToRegularUser();
StartNetworkCertLoader();
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
// Applying an empty device policy will not trigger anything yet, until also
// the user policy is applied.
SetupDevicePolicy(std::string(),
/*global_config=*/base::Value::Dict());
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
// Applying also an empty user policy should trigger connecting to the 'best'
// network.
SetupUserPolicy(/*network_configs_json=*/std::string());
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi1_service_path));
EXPECT_EQ(1, test_observer_->num_auto_connect_events());
EXPECT_EQ(1, test_network_connection_handler_->num_auto_connect_events());
EXPECT_EQ(AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN,
test_observer_->auto_connect_reasons());
EXPECT_EQ(AutoConnectHandler::AUTO_CONNECT_REASON_LOGGED_IN,
test_network_connection_handler_->auto_connect_reasons());
}
TEST_F(AutoConnectHandlerTest, ManualConnectAbortsReconnectAfterLogin) {
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
helper().manager_test()->SetBestServiceToConnect(wifi1_service_path);
// User login and certificate loading shouldn't trigger any change until the
// policy is loaded.
LoginToRegularUser();
StartNetworkCertLoader();
SetupDevicePolicy(std::string(),
/*global_config=*/base::Value::Dict());
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
// A manual connect request should prevent a reconnect after login.
auto_connect_handler_->ConnectToNetworkRequested(
std::string() /* service_path */);
// Applying the user policy after login would usually trigger connecting to
// the 'best' network. But the manual connect prevents this.
SetupUserPolicy(/*network_configs_json=*/std::string());
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_EQ(0, test_observer_->num_auto_connect_events());
}
TEST_F(AutoConnectHandlerTest,
DisableCellularAutoConnectOnAllowOnlyPolicyNetworksAutoconnect) {
scoped_feature_list_.InitAndDisableFeature(
ash::features::kAllowApnModificationPolicy);
base::HistogramTester histogram_tester;
std::string cellular1_service_path =
ConfigureService(kConfigureCellular1UnmanagedConnected);
ASSERT_FALSE(cellular1_service_path.empty());
std::string cellular2_service_path =
ConfigureService(kConfigureCellular2ManagedConnectable);
ASSERT_FALSE(cellular2_service_path.empty());
std::string cellular3_service_path =
ConfigureService(kConfigureCellular3UnmanagedConnected);
ASSERT_FALSE(cellular3_service_path.empty());
EXPECT_EQ(shill::kStateOnline, GetServiceState(cellular1_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(cellular2_service_path));
EXPECT_EQ(shill::kStateOnline, GetServiceState(cellular3_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(cellular1_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(cellular2_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(cellular3_service_path));
CheckServiceAutoConnectProperty(cellular1_service_path,
/*expected_auto_connect=*/true);
CheckServiceAutoConnectProperty(cellular3_service_path,
/*expected_auto_connect=*/true);
histogram_tester.ExpectTotalCount(kESimDisconnectByPolicyHistogram, 0);
histogram_tester.ExpectTotalCount(kPSimDisconnectByPolicyHistogram, 0);
// Apply 'AllowOnlyPolicyNetworksToAutoconnect' policy as a device
// policy and provide a network configuration for cellular2 to make it
// managed.
auto global_config = base::Value::Dict().Set(
::onc::global_network_config::kAllowOnlyPolicyNetworksToAutoconnect,
true);
SetupDevicePolicy(kCellularPolicy, global_config);
// cellular1 and cellular3's service state should be set to idle and
// autoconnect property should be set false.
EXPECT_EQ(shill::kStateIdle, GetServiceState(cellular1_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(cellular2_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(cellular3_service_path));
CheckServiceAutoConnectProperty(cellular1_service_path,
/*expected_auto_connect=*/false);
CheckServiceAutoConnectProperty(cellular3_service_path,
/*expected_auto_connect=*/false);
histogram_tester.ExpectTotalCount(kESimDisconnectByPolicyHistogram, 1);
histogram_tester.ExpectBucketCount(kESimDisconnectByPolicyHistogram, true, 1);
histogram_tester.ExpectTotalCount(kPSimDisconnectByPolicyHistogram, 1);
histogram_tester.ExpectBucketCount(kPSimDisconnectByPolicyHistogram, true, 1);
}
TEST_F(AutoConnectHandlerTest,
DisconnectCellularOnPolicyLoadingAllowOnlyPolicyCellularNetworks) {
scoped_feature_list_.InitAndDisableFeature(
ash::features::kAllowApnModificationPolicy);
base::HistogramTester histogram_tester;
std::string cellular1_service_path =
ConfigureService(kConfigureCellular1UnmanagedConnected);
ASSERT_FALSE(cellular1_service_path.empty());
std::string cellular2_service_path =
ConfigureService(kConfigureCellular2ManagedConnectable);
ASSERT_FALSE(cellular2_service_path.empty());
std::string cellular3_service_path =
ConfigureService(kConfigureCellular3UnmanagedConnected);
ASSERT_FALSE(cellular3_service_path.empty());
EXPECT_EQ(shill::kStateOnline, GetServiceState(cellular1_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(cellular2_service_path));
EXPECT_EQ(shill::kStateOnline, GetServiceState(cellular3_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(cellular1_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(cellular3_service_path));
histogram_tester.ExpectTotalCount(kPSimDisconnectByPolicyHistogram, 0);
histogram_tester.ExpectTotalCount(kESimDisconnectByPolicyHistogram, 0);
// Apply 'AllowOnlyPolicyCellularNetworks' policy as a device policy and
// provide a network configuration for cellular2 to make it managed.
auto global_config = base::Value::Dict().Set(
::onc::global_network_config::kAllowOnlyPolicyCellularNetworks, true);
SetupDevicePolicy(kCellularPolicy, global_config);
// Cellular1 and Cellular3's service configuration should be not removed
// because they are cellular networks and auto connect property should set
// to false since they are unmanaged networks.
EXPECT_TRUE(helper().profile_test()->HasService(cellular1_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(cellular3_service_path));
CheckServiceAutoConnectProperty(cellular1_service_path,
/*expected_auto_connect=*/false);
CheckServiceAutoConnectProperty(cellular3_service_path,
/*expected_auto_connect=*/false);
histogram_tester.ExpectTotalCount(kESimDisconnectByPolicyHistogram, 1);
histogram_tester.ExpectBucketCount(kESimDisconnectByPolicyHistogram, true, 1);
histogram_tester.ExpectTotalCount(kPSimDisconnectByPolicyHistogram, 1);
histogram_tester.ExpectBucketCount(kPSimDisconnectByPolicyHistogram, true, 1);
}
TEST_F(AutoConnectHandlerTest, DisconnectFromBlockedNetwork) {
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
LoginToRegularUser();
StartNetworkCertLoader();
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(wifi0_service_path));
// Apply a device policy, which blocks wifi0. No disconnects should occur
// since we wait for both device & user policy before possibly disconnecting.
auto global_config = base::Value::Dict().Set(
::onc::global_network_config::kBlockedHexSSIDs,
base::Value::List().Append("7769666930") // hex(wifi0) = 7769666930
);
SetupDevicePolicy(std::string(), global_config);
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(wifi0_service_path));
// Apply an empty user policy (no allow list for wifi0). Connection to wifi0
// should be disconnected due to being blocked.
SetupUserPolicy(/*network_configs_json=*/std::string());
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_FALSE(helper().profile_test()->HasService(wifi0_service_path));
EXPECT_EQ(0, test_observer_->num_auto_connect_events());
}
// This test verifies that when `AllowOnlyPolicyWiFiToConnectIfAvailable` is
// enabled, explicitly blocked networks are removed. Preserved configurations
// for non-blocked networks are verified in
// AutoConnectHandlerTest.AllowOnlyPolicyWiFiToConnectIfAvailable.
TEST_F(AutoConnectHandlerTest,
DisconnectFromBlockedNetworkAllowOnlyPolicyWiFiToConnectIfAvailable) {
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
LoginToRegularUser();
StartNetworkCertLoader();
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(wifi0_service_path));
// Apply a device policy, which blocks wifi0. No disconnects should occur
// since we wait for both device & user policy before possibly disconnecting.
auto global_config = base::Value::Dict()
.Set(::onc::global_network_config::kBlockedHexSSIDs,
base::Value::List().Append(
"7769666930") // hex(wifi0) = 7769666930
)
.Set(::onc::global_network_config::
kAllowOnlyPolicyWiFiToConnectIfAvailable,
base::Value(true));
SetupDevicePolicy(kPolicy, global_config);
SetupDevicePolicy(std::string(), global_config);
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(wifi0_service_path));
// Apply an empty user policy (no allow list for wifi0). Connection to wifi0
// should be disconnected due to being blocked.
SetupUserPolicy(/*network_configs_json=*/std::string());
// Although AllowOnlyPolicyWiFiToConnectIfAvailable is enabled, the wifi0
// network configuration should be removed because this network is explicitly
// blocked by policy.
EXPECT_FALSE(helper().profile_test()->HasService(wifi0_service_path));
EXPECT_EQ(0, test_observer_->num_auto_connect_events());
}
TEST_F(AutoConnectHandlerTest, AllowOnlyPolicyWiFiToConnectIfAvailable) {
std::string wifi0_service_path =
ConfigureService(kConfigWifi0UnmanagedSharedConnected);
ASSERT_FALSE(wifi0_service_path.empty());
std::string wifi1_service_path =
ConfigureService(kConfigWifi1ManagedSharedConnectable);
ASSERT_FALSE(wifi1_service_path.empty());
LoginToRegularUser();
StartNetworkCertLoader();
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(wifi0_service_path));
// Apply 'AllowOnlyPolicyWiFiToConnectIfAvailable' policy as a device
// policy and provide a network configuration for wifi1 to make it managed.
auto global_config = base::Value::Dict().Set(
::onc::global_network_config::kAllowOnlyPolicyWiFiToConnectIfAvailable,
true);
SetupDevicePolicy(kPolicy, global_config);
EXPECT_EQ(shill::kStateOnline, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(wifi0_service_path));
// Apply an empty user policy (no allow list for wifi0). Connection to wifi0
// should be disconnected due to being unmanaged and managed network wifi1
// being available. wifi0 configuration should not be removed.
SetupUserPolicy(/*network_configs_json=*/std::string());
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi0_service_path));
EXPECT_EQ(shill::kStateIdle, GetServiceState(wifi1_service_path));
EXPECT_TRUE(helper().profile_test()->HasService(wifi0_service_path));
EXPECT_EQ(0, test_observer_->num_auto_connect_events());
}
} // namespace ash
|