| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/network/prohibited_technologies_handler.h" | 5 #include "chromeos/network/prohibited_technologies_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" | 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 #include "chromeos/dbus/shill_manager_client.h" | |
| 19 #include "chromeos/dbus/shill_profile_client.h" | |
| 20 #include "chromeos/network/managed_network_configuration_handler_impl.h" | 18 #include "chromeos/network/managed_network_configuration_handler_impl.h" |
| 21 #include "chromeos/network/network_configuration_handler.h" | 19 #include "chromeos/network/network_configuration_handler.h" |
| 22 #include "chromeos/network/network_profile_handler.h" | 20 #include "chromeos/network/network_profile_handler.h" |
| 23 #include "chromeos/network/network_state_handler.h" | 21 #include "chromeos/network/network_state_test.h" |
| 24 #include "chromeos/network/onc/onc_utils.h" | 22 #include "chromeos/network/onc/onc_utils.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/cros_system_api/dbus/service_constants.h" | 24 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 27 | 25 |
| 28 namespace chromeos { | 26 namespace chromeos { |
| 29 | 27 |
| 30 namespace { | 28 class ProhibitedTechnologiesHandlerTest : public NetworkStateTest { |
| 31 const char* kUserHash = "user_hash"; | |
| 32 } | |
| 33 | |
| 34 class ProhibitedTechnologiesHandlerTest : public testing::Test { | |
| 35 public: | 29 public: |
| 36 ProhibitedTechnologiesHandlerTest() {} | 30 ProhibitedTechnologiesHandlerTest() {} |
| 37 | 31 |
| 38 void SetUp() override { | 32 void SetUp() override { |
| 39 DBusThreadManager::Initialize(); | 33 DBusThreadManager::Initialize(); |
| 40 LoginState::Initialize(); | 34 LoginState::Initialize(); |
| 41 DBusThreadManager* dbus_manager = DBusThreadManager::Get(); | |
| 42 test_manager_client_ = | |
| 43 dbus_manager->GetShillManagerClient()->GetTestInterface(); | |
| 44 | 35 |
| 45 test_manager_client_->AddTechnology(shill::kTypeWifi, true /* enabled */); | 36 NetworkStateTest::SetUp(); |
| 46 test_manager_client_->AddTechnology(shill::kTypeCellular, | |
| 47 true /* enabled */); | |
| 48 dbus_manager->GetShillProfileClient()->GetTestInterface()->AddProfile( | |
| 49 "shared_profile_path", std::string() /* shared profile */); | |
| 50 dbus_manager->GetShillProfileClient()->GetTestInterface()->AddProfile( | |
| 51 "user_profile_path", kUserHash); | |
| 52 | 37 |
| 53 base::RunLoop().RunUntilIdle(); | 38 test_manager_client()->AddTechnology(shill::kTypeCellular, |
| 54 network_state_handler_ = NetworkStateHandler::InitializeForTest(); | 39 true /* enabled */); |
| 40 |
| 55 network_config_handler_.reset( | 41 network_config_handler_.reset( |
| 56 NetworkConfigurationHandler::InitializeForTest( | 42 NetworkConfigurationHandler::InitializeForTest( |
| 57 network_state_handler_.get(), NULL /* network_device_handler */)); | 43 network_state_handler(), nullptr /* network_device_handler */)); |
| 58 | 44 |
| 59 network_profile_handler_.reset(new NetworkProfileHandler()); | 45 network_profile_handler_.reset(new NetworkProfileHandler()); |
| 60 network_profile_handler_->Init(); | 46 network_profile_handler_->Init(); |
| 61 | 47 |
| 62 managed_config_handler_.reset(new ManagedNetworkConfigurationHandlerImpl()); | 48 managed_config_handler_.reset(new ManagedNetworkConfigurationHandlerImpl()); |
| 63 prohibited_technologies_handler_.reset(new ProhibitedTechnologiesHandler()); | 49 prohibited_technologies_handler_.reset(new ProhibitedTechnologiesHandler()); |
| 64 | 50 |
| 65 managed_config_handler_->Init( | 51 managed_config_handler_->Init( |
| 66 network_state_handler_.get(), network_profile_handler_.get(), | 52 network_state_handler(), network_profile_handler_.get(), |
| 67 network_config_handler_.get(), nullptr /* network_device_handler */, | 53 network_config_handler_.get(), nullptr /* network_device_handler */, |
| 68 prohibited_technologies_handler_.get()); | 54 prohibited_technologies_handler_.get()); |
| 69 | 55 |
| 70 prohibited_technologies_handler_->Init(managed_config_handler_.get(), | 56 prohibited_technologies_handler_->Init(managed_config_handler_.get(), |
| 71 network_state_handler_.get()); | 57 network_state_handler()); |
| 72 | 58 |
| 73 base::RunLoop().RunUntilIdle(); | 59 base::RunLoop().RunUntilIdle(); |
| 74 | 60 |
| 75 PreparePolicies(); | 61 PreparePolicies(); |
| 76 } | 62 } |
| 77 | 63 |
| 78 void PreparePolicies() { | 64 void PreparePolicies() { |
| 79 std::unique_ptr<base::ListValue> val(new base::ListValue()); | 65 std::unique_ptr<base::ListValue> val(new base::ListValue()); |
| 80 val->AppendString("WiFi"); | 66 val->AppendString("WiFi"); |
| 81 global_config_disable_wifi.Set("DisableNetworkTypes", std::move(val)); | 67 global_config_disable_wifi.Set("DisableNetworkTypes", std::move(val)); |
| 82 val.reset(new base::ListValue()); | 68 val.reset(new base::ListValue()); |
| 83 val->AppendString("WiFi"); | 69 val->AppendString("WiFi"); |
| 84 val->AppendString("Cellular"); | 70 val->AppendString("Cellular"); |
| 85 global_config_disable_wifi_and_cell.Set("DisableNetworkTypes", | 71 global_config_disable_wifi_and_cell.Set("DisableNetworkTypes", |
| 86 std::move(val)); | 72 std::move(val)); |
| 87 } | 73 } |
| 88 | 74 |
| 89 void TearDown() override { | 75 void TearDown() override { |
| 90 network_state_handler_->Shutdown(); | 76 ShutdownNetworkState(); |
| 91 prohibited_technologies_handler_.reset(); | 77 prohibited_technologies_handler_.reset(); |
| 92 managed_config_handler_.reset(); | 78 managed_config_handler_.reset(); |
| 93 network_profile_handler_.reset(); | 79 network_profile_handler_.reset(); |
| 94 network_config_handler_.reset(); | 80 network_config_handler_.reset(); |
| 95 network_state_handler_.reset(); | 81 NetworkStateTest::TearDown(); |
| 96 LoginState::Shutdown(); | 82 LoginState::Shutdown(); |
| 97 DBusThreadManager::Shutdown(); | 83 DBusThreadManager::Shutdown(); |
| 98 } | 84 } |
| 99 | 85 |
| 100 protected: | 86 protected: |
| 101 void LoginToRegularUser() { | 87 void LoginToRegularUser() { |
| 102 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE, | 88 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE, |
| 103 LoginState::LOGGED_IN_USER_REGULAR); | 89 LoginState::LOGGED_IN_USER_REGULAR); |
| 104 base::RunLoop().RunUntilIdle(); | 90 base::RunLoop().RunUntilIdle(); |
| 105 } | 91 } |
| 106 | 92 |
| 107 void SetupPolicy(const base::DictionaryValue& global_config, | 93 void SetupPolicy(const base::DictionaryValue& global_config, |
| 108 bool user_policy) { | 94 bool user_policy) { |
| 109 if (user_policy) { | 95 if (user_policy) { |
| 110 managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_USER_POLICY, | 96 managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_USER_POLICY, |
| 111 kUserHash, base::ListValue(), | 97 kUserHash, base::ListValue(), |
| 112 global_config); | 98 global_config); |
| 113 } else { | 99 } else { |
| 114 managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, | 100 managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, |
| 115 std::string(), // no username hash | 101 std::string(), // no username hash |
| 116 base::ListValue(), global_config); | 102 base::ListValue(), global_config); |
| 117 } | 103 } |
| 118 base::RunLoop().RunUntilIdle(); | 104 base::RunLoop().RunUntilIdle(); |
| 119 } | 105 } |
| 120 | 106 |
| 121 std::unique_ptr<ProhibitedTechnologiesHandler> | 107 std::unique_ptr<ProhibitedTechnologiesHandler> |
| 122 prohibited_technologies_handler_; | 108 prohibited_technologies_handler_; |
| 123 std::unique_ptr<NetworkStateHandler> network_state_handler_; | |
| 124 std::unique_ptr<NetworkConfigurationHandler> network_config_handler_; | 109 std::unique_ptr<NetworkConfigurationHandler> network_config_handler_; |
| 125 std::unique_ptr<ManagedNetworkConfigurationHandlerImpl> | 110 std::unique_ptr<ManagedNetworkConfigurationHandlerImpl> |
| 126 managed_config_handler_; | 111 managed_config_handler_; |
| 127 std::unique_ptr<NetworkProfileHandler> network_profile_handler_; | 112 std::unique_ptr<NetworkProfileHandler> network_profile_handler_; |
| 128 ShillManagerClient::TestInterface* test_manager_client_; | |
| 129 base::MessageLoopForUI message_loop_; | 113 base::MessageLoopForUI message_loop_; |
| 130 base::DictionaryValue global_config_disable_wifi; | 114 base::DictionaryValue global_config_disable_wifi; |
| 131 base::DictionaryValue global_config_disable_wifi_and_cell; | 115 base::DictionaryValue global_config_disable_wifi_and_cell; |
| 132 | 116 |
| 133 private: | 117 private: |
| 134 DISALLOW_COPY_AND_ASSIGN(ProhibitedTechnologiesHandlerTest); | 118 DISALLOW_COPY_AND_ASSIGN(ProhibitedTechnologiesHandlerTest); |
| 135 }; | 119 }; |
| 136 | 120 |
| 137 TEST_F(ProhibitedTechnologiesHandlerTest, | 121 TEST_F(ProhibitedTechnologiesHandlerTest, |
| 138 ProhibitedTechnologiesAllowedLoginScreen) { | 122 ProhibitedTechnologiesAllowedLoginScreen) { |
| 139 EXPECT_TRUE( | 123 EXPECT_TRUE( |
| 140 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi())); | 124 network_state_handler()->IsTechnologyEnabled(NetworkTypePattern::WiFi())); |
| 141 EXPECT_TRUE(network_state_handler_->IsTechnologyEnabled( | 125 EXPECT_TRUE(network_state_handler()->IsTechnologyEnabled( |
| 142 NetworkTypePattern::Cellular())); | 126 NetworkTypePattern::Cellular())); |
| 143 SetupPolicy(global_config_disable_wifi_and_cell, false); | 127 SetupPolicy(global_config_disable_wifi_and_cell, false); |
| 144 EXPECT_TRUE( | 128 EXPECT_TRUE( |
| 145 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi())); | 129 network_state_handler()->IsTechnologyEnabled(NetworkTypePattern::WiFi())); |
| 146 EXPECT_TRUE(network_state_handler_->IsTechnologyEnabled( | 130 EXPECT_TRUE(network_state_handler()->IsTechnologyEnabled( |
| 147 NetworkTypePattern::Cellular())); | 131 NetworkTypePattern::Cellular())); |
| 148 } | 132 } |
| 149 | 133 |
| 150 TEST_F(ProhibitedTechnologiesHandlerTest, | 134 TEST_F(ProhibitedTechnologiesHandlerTest, |
| 151 ProhibitedTechnologiesNotAllowedUserSession) { | 135 ProhibitedTechnologiesNotAllowedUserSession) { |
| 152 EXPECT_TRUE( | 136 EXPECT_TRUE( |
| 153 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi())); | 137 network_state_handler()->IsTechnologyEnabled(NetworkTypePattern::WiFi())); |
| 154 EXPECT_TRUE(network_state_handler_->IsTechnologyEnabled( | 138 EXPECT_TRUE(network_state_handler()->IsTechnologyEnabled( |
| 155 NetworkTypePattern::Cellular())); | 139 NetworkTypePattern::Cellular())); |
| 156 SetupPolicy(global_config_disable_wifi_and_cell, false); | 140 SetupPolicy(global_config_disable_wifi_and_cell, false); |
| 157 | 141 |
| 158 LoginToRegularUser(); | 142 LoginToRegularUser(); |
| 159 EXPECT_TRUE( | 143 EXPECT_TRUE( |
| 160 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi())); | 144 network_state_handler()->IsTechnologyEnabled(NetworkTypePattern::WiFi())); |
| 161 EXPECT_TRUE(network_state_handler_->IsTechnologyEnabled( | 145 EXPECT_TRUE(network_state_handler()->IsTechnologyEnabled( |
| 162 NetworkTypePattern::Cellular())); | 146 NetworkTypePattern::Cellular())); |
| 163 | 147 |
| 164 SetupPolicy(base::DictionaryValue(), true); // wait for user policy | 148 SetupPolicy(base::DictionaryValue(), true); // wait for user policy |
| 165 | 149 |
| 166 // Should be disabled after logged in | 150 // Should be disabled after logged in |
| 167 EXPECT_FALSE( | 151 EXPECT_FALSE( |
| 168 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi())); | 152 network_state_handler()->IsTechnologyEnabled(NetworkTypePattern::WiFi())); |
| 169 EXPECT_FALSE(network_state_handler_->IsTechnologyEnabled( | 153 EXPECT_FALSE(network_state_handler()->IsTechnologyEnabled( |
| 170 NetworkTypePattern::Cellular())); | 154 NetworkTypePattern::Cellular())); |
| 171 | 155 |
| 172 // Can not enable it back | 156 // Can not enable it back |
| 173 network_state_handler_->SetTechnologyEnabled( | 157 network_state_handler()->SetTechnologyEnabled( |
| 174 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback()); | 158 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback()); |
| 175 network_state_handler_->SetTechnologyEnabled( | 159 network_state_handler()->SetTechnologyEnabled( |
| 176 NetworkTypePattern::Cellular(), true, network_handler::ErrorCallback()); | 160 NetworkTypePattern::Cellular(), true, network_handler::ErrorCallback()); |
| 177 base::RunLoop().RunUntilIdle(); | 161 base::RunLoop().RunUntilIdle(); |
| 178 EXPECT_FALSE( | 162 EXPECT_FALSE( |
| 179 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi())); | 163 network_state_handler()->IsTechnologyEnabled(NetworkTypePattern::WiFi())); |
| 180 EXPECT_FALSE(network_state_handler_->IsTechnologyEnabled( | 164 EXPECT_FALSE(network_state_handler()->IsTechnologyEnabled( |
| 181 NetworkTypePattern::Cellular())); | 165 NetworkTypePattern::Cellular())); |
| 182 | 166 |
| 183 // Can enable Cellular back after modifying policy | 167 // Can enable Cellular back after modifying policy |
| 184 SetupPolicy(global_config_disable_wifi, false); | 168 SetupPolicy(global_config_disable_wifi, false); |
| 185 network_state_handler_->SetTechnologyEnabled( | 169 network_state_handler()->SetTechnologyEnabled( |
| 186 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback()); | 170 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback()); |
| 187 network_state_handler_->SetTechnologyEnabled( | 171 network_state_handler()->SetTechnologyEnabled( |
| 188 NetworkTypePattern::Cellular(), true, network_handler::ErrorCallback()); | 172 NetworkTypePattern::Cellular(), true, network_handler::ErrorCallback()); |
| 189 base::RunLoop().RunUntilIdle(); | 173 base::RunLoop().RunUntilIdle(); |
| 190 EXPECT_FALSE( | 174 EXPECT_FALSE( |
| 191 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi())); | 175 network_state_handler()->IsTechnologyEnabled(NetworkTypePattern::WiFi())); |
| 192 EXPECT_TRUE(network_state_handler_->IsTechnologyEnabled( | 176 EXPECT_TRUE(network_state_handler()->IsTechnologyEnabled( |
| 193 NetworkTypePattern::Cellular())); | 177 NetworkTypePattern::Cellular())); |
| 194 } | 178 } |
| 195 | 179 |
| 196 } // namespace chromeos | 180 } // namespace chromeos |
| OLD | NEW |