Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Side by Side Diff: chromeos/network/prohibited_technologies_handler_unittest.cc

Issue 1431563005: Handle prohibited technologies in device policy ONC (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
stevenjb 2015/11/11 18:07:31 no blank line
fqj 2015/11/12 10:23:01 Done.
30 const char* kUserHash = "user_hash";
31 }
32
33 class ProhibitedTechnologiesHandlerTest : public testing::Test {
34 public:
35 ProhibitedTechnologiesHandlerTest() {}
36
37 void SetUp() override {
38 DBusThreadManager::Initialize();
39 LoginState::Initialize();
40 DBusThreadManager* dbus_manager = DBusThreadManager::Get();
41 test_manager_client_ =
42 dbus_manager->GetShillManagerClient()->GetTestInterface();
43
44 test_manager_client_->AddTechnology(shill::kTypeWifi, true /* enabled */);
45 test_manager_client_->AddTechnology(shill::kTypeCellular,
46 true /* enabled */);
47 dbus_manager->GetShillProfileClient()->GetTestInterface()->AddProfile(
48 "shared_profile_path", std::string() /* shared profile */);
49 dbus_manager->GetShillProfileClient()->GetTestInterface()->AddProfile(
50 "user_profile_path", kUserHash);
51
52 base::RunLoop().RunUntilIdle();
53 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
54 network_config_handler_.reset(
55 NetworkConfigurationHandler::InitializeForTest(
56 network_state_handler_.get(), NULL /* network_device_handler */));
57
58 network_profile_handler_.reset(new NetworkProfileHandler());
59 network_profile_handler_->Init();
60
61 managed_config_handler_.reset(new ManagedNetworkConfigurationHandlerImpl());
62 prohibited_technologies_handler_.reset(new ProhibitedTechnologiesHandler());
63
64 managed_config_handler_->Init(
65 network_state_handler_.get(), network_profile_handler_.get(),
66 network_config_handler_.get(), nullptr /* network_device_handler */,
67 prohibited_technologies_handler_.get());
68
69 prohibited_technologies_handler_->Init(managed_config_handler_.get(),
70 network_state_handler_.get());
71
72 base::RunLoop().RunUntilIdle();
73
74 PreparePolicies();
75 }
76
77 void PreparePolicies() {
78 scoped_ptr<base::ListValue> val(new base::ListValue());
79 val->AppendString("WiFi");
80 global_config_disable_wifi.Set("DisableNetworkTypes", val.Pass());
81 val.reset(new base::ListValue());
82 val->AppendString("WiFi");
83 val->AppendString("Cellular");
84 global_config_disable_wifi_and_cell.Set("DisableNetworkTypes", val.Pass());
85 }
86
87 void TearDown() override {
88 prohibited_technologies_handler_.reset();
89 managed_config_handler_.reset();
90 network_profile_handler_.reset();
91 network_config_handler_.reset();
92 network_state_handler_.reset();
93 LoginState::Shutdown();
94 DBusThreadManager::Shutdown();
95 }
96
97 protected:
98 void LoginToRegularUser() {
99 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
100 LoginState::LOGGED_IN_USER_REGULAR);
101 base::RunLoop().RunUntilIdle();
102 }
103
104 void SetupPolicy(const base::DictionaryValue& global_config,
105 bool user_policy) {
106 if (user_policy) {
107 managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
108 kUserHash, base::ListValue(),
109 global_config);
110 } else {
111 managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY,
112 std::string(), // no username hash
113 base::ListValue(), global_config);
114 }
115 base::RunLoop().RunUntilIdle();
116 }
117
118 scoped_ptr<ProhibitedTechnologiesHandler> prohibited_technologies_handler_;
119 scoped_ptr<NetworkStateHandler> network_state_handler_;
120 scoped_ptr<NetworkConfigurationHandler> network_config_handler_;
121 scoped_ptr<ManagedNetworkConfigurationHandlerImpl> managed_config_handler_;
122 scoped_ptr<NetworkProfileHandler> network_profile_handler_;
123 ShillManagerClient::TestInterface* test_manager_client_;
124 base::MessageLoopForUI message_loop_;
125 base::DictionaryValue global_config_disable_wifi;
126 base::DictionaryValue global_config_disable_wifi_and_cell;
127
128 private:
129 DISALLOW_COPY_AND_ASSIGN(ProhibitedTechnologiesHandlerTest);
130 };
131
132 TEST_F(ProhibitedTechnologiesHandlerTest,
133 ProhibitedTechnologiesAllowedLoginScreen) {
134 EXPECT_TRUE(
135 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi()));
136 EXPECT_TRUE(network_state_handler_->IsTechnologyEnabled(
137 NetworkTypePattern::Cellular()));
138 SetupPolicy(global_config_disable_wifi_and_cell, false);
139 EXPECT_TRUE(
140 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi()));
141 EXPECT_TRUE(network_state_handler_->IsTechnologyEnabled(
142 NetworkTypePattern::Cellular()));
143 };
144
145 TEST_F(ProhibitedTechnologiesHandlerTest,
146 ProhibitedTechnologiesNotAllowedUserSession) {
147 EXPECT_TRUE(
148 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi()));
149 EXPECT_TRUE(network_state_handler_->IsTechnologyEnabled(
150 NetworkTypePattern::Cellular()));
151 SetupPolicy(global_config_disable_wifi_and_cell, false);
152
153 LoginToRegularUser();
154 EXPECT_TRUE(
155 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi()));
156 EXPECT_TRUE(network_state_handler_->IsTechnologyEnabled(
157 NetworkTypePattern::Cellular()));
158
159 SetupPolicy(base::DictionaryValue(), true); // wait for user policy
160
161 // Should be disabled after logged in
162 EXPECT_FALSE(
163 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi()));
164 EXPECT_FALSE(network_state_handler_->IsTechnologyEnabled(
165 NetworkTypePattern::Cellular()));
166
167 // Can not enable it back
168 network_state_handler_->SetTechnologyEnabled(
169 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback());
170 network_state_handler_->SetTechnologyEnabled(
171 NetworkTypePattern::Cellular(), true, network_handler::ErrorCallback());
172 message_loop_.RunUntilIdle();
173 EXPECT_FALSE(
174 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi()));
175 EXPECT_FALSE(network_state_handler_->IsTechnologyEnabled(
176 NetworkTypePattern::Cellular()));
177
178 // Can enable Cellular back after modifying policy
179 SetupPolicy(global_config_disable_wifi, false);
180 network_state_handler_->SetTechnologyEnabled(
181 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback());
182 network_state_handler_->SetTechnologyEnabled(
183 NetworkTypePattern::Cellular(), true, network_handler::ErrorCallback());
184 message_loop_.RunUntilIdle();
185 EXPECT_FALSE(
186 network_state_handler_->IsTechnologyEnabled(NetworkTypePattern::WiFi()));
187 EXPECT_TRUE(network_state_handler_->IsTechnologyEnabled(
188 NetworkTypePattern::Cellular()));
189 };
190
191 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698