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