#include <sstream>
#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "chromeos/dbus/shill_client_helper.h"
#include "chromeos/network/managed_network_configuration_handler_impl.h"
#include "chromeos/network/network_configuration_handler.h"
+#include "chromeos/network/network_policy_observer.h"
#include "chromeos/network/network_profile_handler.h"
#include "chromeos/network/onc/onc_test_utils.h"
#include "chromeos/network/onc/onc_utils.h"
return str.str();
}
+void DereferenceAndCall(
+ base::Callback<void(const base::DictionaryValue& result)> callback,
+ const base::DictionaryValue* value) {
+ callback.Run(*value);
+}
+
const char kUser1[] = "user1";
const char kUser1ProfilePath[] = "/profile/user1/shill";
const std::string& userhash = profile_to_user_[profile_path.value()];
result->SetStringWithoutPathExpansion(shill::kUserHashProperty, userhash);
- callback.Run(*result);
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(base::Bind(&DereferenceAndCall, callback),
+ base::Owned(result.release())));
}
void GetEntry(const dbus::ObjectPath& profile_path,
base::DictionaryValue* entry = NULL;
entries->GetDictionaryWithoutPathExpansion(entry_path, &entry);
ASSERT_TRUE(entry);
- callback.Run(*entry);
+ base::MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(base::Bind(&DereferenceAndCall, callback),
+ base::Owned(entry->DeepCopy())));
}
protected:
DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler);
};
+class TestNetworkPolicyObserver : public NetworkPolicyObserver {
+ public:
+ TestNetworkPolicyObserver() : policies_applied_count_(0) {}
+
+ void PoliciesApplied(const std::string& userhash) override {
+ policies_applied_count_++;
+ };
+
+ int GetPoliciesAppliedCountAndReset() {
+ int count = policies_applied_count_;
+ policies_applied_count_ = 0;
+ return count;
+ }
+
+ private:
+ int policies_applied_count_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestNetworkPolicyObserver);
+};
+
} // namespace
class ManagedNetworkConfigurationHandlerTest : public testing::Test {
virtual ~ManagedNetworkConfigurationHandlerTest() {
}
- virtual void SetUp() OVERRIDE {
+ void SetUp() override {
scoped_ptr<DBusThreadManagerSetter> dbus_setter =
DBusThreadManager::GetSetterForTesting();
mock_manager_client_ = new StrictMock<MockShillManagerClient>();
network_profile_handler_.get(),
network_configuration_handler_.get(),
NULL /* no DeviceHandler */);
+ managed_network_configuration_handler_->AddObserver(&policy_observer_);
message_loop_.RunUntilIdle();
}
- virtual void TearDown() OVERRIDE {
+ void TearDown() override {
+ if (managed_network_configuration_handler_)
+ managed_network_configuration_handler_->RemoveObserver(&policy_observer_);
managed_network_configuration_handler_.reset();
network_configuration_handler_.reset();
network_profile_handler_.reset();
MockShillServiceClient* mock_service_client_;
ShillProfileTestClient profiles_stub_;
ShillServiceTestClient services_stub_;
+ TestNetworkPolicyObserver policy_observer_;
scoped_ptr<TestNetworkProfileHandler> network_profile_handler_;
scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
scoped_ptr<ManagedNetworkConfigurationHandlerImpl>
message_loop_.RunUntilIdle();
}
+TEST_F(ManagedNetworkConfigurationHandlerTest, EnableManagedCredentialsWiFi) {
+ InitializeStandardProfiles();
+ scoped_ptr<base::DictionaryValue> expected_shill_properties =
+ test_utils::ReadTestDictionary(
+ "policy/shill_policy_autoconnect_on_unconfigured_wifi1.json");
+
+ EXPECT_CALL(*mock_profile_client_,
+ GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
+
+ EXPECT_CALL(*mock_manager_client_,
+ ConfigureServiceForProfile(
+ dbus::ObjectPath(kUser1ProfilePath),
+ IsEqualTo(expected_shill_properties.get()),
+ _, _));
+
+ SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
+ "policy/policy_wifi1_autoconnect.onc");
+ message_loop_.RunUntilIdle();
+}
+
+TEST_F(ManagedNetworkConfigurationHandlerTest, EnableManagedCredentialsVPN) {
+ InitializeStandardProfiles();
+ scoped_ptr<base::DictionaryValue> expected_shill_properties =
+ test_utils::ReadTestDictionary(
+ "policy/shill_policy_autoconnect_on_unconfigured_vpn.json");
+
+ EXPECT_CALL(*mock_profile_client_,
+ GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
+
+ EXPECT_CALL(*mock_manager_client_,
+ ConfigureServiceForProfile(
+ dbus::ObjectPath(kUser1ProfilePath),
+ IsEqualTo(expected_shill_properties.get()),
+ _, _));
+
+ SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1,
+ "policy/policy_vpn_autoconnect.onc");
+ message_loop_.RunUntilIdle();
+}
+
// Ensure that EAP settings for ethernet are matched with the right profile
// entry and written to the dedicated EthernetEAP service.
TEST_F(ManagedNetworkConfigurationHandlerTest,
SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
message_loop_.RunUntilIdle();
+ EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
VerifyAndClearExpectations();
SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
GetEntry(dbus::ObjectPath(kUser1ProfilePath), "some_entry_path", _, _));
SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
+
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
+}
+
+TEST_F(ManagedNetworkConfigurationHandlerTest, PolicyApplicationRunning) {
+ InitializeStandardProfiles();
+ EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _)).Times(AnyNumber());
+ EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _))
+ .Times(AnyNumber());
+ EXPECT_CALL(*mock_profile_client_, GetEntry(_, _, _, _)).Times(AnyNumber());
+
+ EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
+
+ SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
+ managed_handler()->SetPolicy(
+ ::onc::ONC_SOURCE_DEVICE_POLICY,
+ std::string(), // no userhash
+ base::ListValue(), // no device network policy
+ base::DictionaryValue()); // no device global config
+
+ EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
+ message_loop_.RunUntilIdle();
+ EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
+
+ SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
+ kUser1ProfilePath,
+ "some_entry_path");
+
+ SetPolicy(
+ ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
+ EXPECT_TRUE(managed_handler()->IsAnyPolicyApplicationRunning());
+ message_loop_.RunUntilIdle();
+ EXPECT_FALSE(managed_handler()->IsAnyPolicyApplicationRunning());
+}
+
+TEST_F(ManagedNetworkConfigurationHandlerTest, UpdatePolicyAfterFinished) {
+ InitializeStandardProfiles();
+ EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
+ EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
+
+ SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
+ VerifyAndClearExpectations();
+
+ SetUpEntry("policy/shill_policy_on_unmanaged_wifi1.json",
+ kUser1ProfilePath,
+ "some_entry_path");
+
+ EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _));
+ EXPECT_CALL(
+ *mock_profile_client_,
+ GetEntry(dbus::ObjectPath(kUser1ProfilePath), "some_entry_path", _, _));
+ EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _));
+
+ SetPolicy(
+ ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
+ message_loop_.RunUntilIdle();
+ EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
+}
+
+TEST_F(ManagedNetworkConfigurationHandlerTest, UpdatePolicyBeforeFinished) {
+ InitializeStandardProfiles();
+ EXPECT_CALL(*mock_profile_client_, GetProperties(_, _, _)).Times(2);
+ EXPECT_CALL(*mock_manager_client_, ConfigureServiceForProfile(_, _, _, _))
+ .Times(2);
+
+ SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
+ // Usually the first call will cause a profile entry to be created, which we
+ // don't fake here.
+ SetPolicy(
+ ::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1_update.onc");
+
message_loop_.RunUntilIdle();
+ EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyManageUnmanaged) {
SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "");
message_loop_.RunUntilIdle();
+ EXPECT_EQ(1, policy_observer_.GetPoliciesAppliedCountAndReset());
}
TEST_F(ManagedNetworkConfigurationHandlerTest, SetPolicyIgnoreUnmanaged) {
class ManagedNetworkConfigurationHandlerShutdownTest
: public ManagedNetworkConfigurationHandlerTest {
public:
- virtual void SetUp() OVERRIDE {
+ void SetUp() override {
ManagedNetworkConfigurationHandlerTest::SetUp();
ON_CALL(*mock_profile_client_, GetProperties(_, _, _)).WillByDefault(
Invoke(&ManagedNetworkConfigurationHandlerShutdownTest::GetProperties));
GetProperties(dbus::ObjectPath(kUser1ProfilePath), _, _));
SetPolicy(::onc::ONC_SOURCE_USER_POLICY, kUser1, "policy/policy_wifi1.onc");
+ managed_network_configuration_handler_->RemoveObserver(&policy_observer_);
managed_network_configuration_handler_.reset();
message_loop_.RunUntilIdle();
}