Commit 8e7751df authored by Timothy Loh's avatar Timothy Loh Committed by Commit Bot

Add basic unit tests for the PluginVmManager

This CL adds unit tests for PluginVmManager::LaunchPluginVM, checking
that it calls the expected DBus functions and requires Plugin VM to be
allowed.

Bug: 904853
Test: unit_tests --gtest_filter="PluginVmManagerTest.*"
Change-Id: Iea991c6a84679c8756c8a0ca31862ef50a033cde
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1608967
Commit-Queue: Timothy Loh <timloh@chromium.org>
Reviewed-by: default avatarRyo Hashimoto <hashimoto@chromium.org>
Reviewed-by: default avatarAlex Oldemeier <aoldemeier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#659330}
parent 2bbaa3f7
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_manager.h"
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_test_helper.h"
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_util.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/fake_vm_plugin_dispatcher_client.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace plugin_vm {
class PluginVmManagerTest : public testing::Test {
public:
PluginVmManagerTest()
: test_helper_(&testing_profile_), plugin_vm_manager_(&testing_profile_) {
chromeos::DBusThreadManager::Initialize();
}
~PluginVmManagerTest() override { chromeos::DBusThreadManager::Shutdown(); }
protected:
chromeos::FakeVmPluginDispatcherClient& VmPluginDispatcherClient() {
return *static_cast<chromeos::FakeVmPluginDispatcherClient*>(
chromeos::DBusThreadManager::Get()->GetVmPluginDispatcherClient());
}
content::TestBrowserThreadBundle thread_bundle_;
TestingProfile testing_profile_;
PluginVmTestHelper test_helper_;
PluginVmManager plugin_vm_manager_;
private:
DISALLOW_COPY_AND_ASSIGN(PluginVmManagerTest);
};
TEST_F(PluginVmManagerTest, LaunchPluginVmRequiresPluginVmAllowed) {
EXPECT_FALSE(IsPluginVmAllowedForProfile(&testing_profile_));
plugin_vm_manager_.LaunchPluginVm();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(VmPluginDispatcherClient().list_vms_called());
EXPECT_FALSE(VmPluginDispatcherClient().start_vm_called());
EXPECT_FALSE(VmPluginDispatcherClient().show_vm_called());
}
TEST_F(PluginVmManagerTest, LaunchPluginVmStartAndShow) {
test_helper_.AllowPluginVm();
EXPECT_TRUE(IsPluginVmAllowedForProfile(&testing_profile_));
// The PluginVmManager calls StartVm when the VM is not yet running.
vm_tools::plugin_dispatcher::ListVmResponse list_vms_response;
list_vms_response.add_vm_info()->set_state(
vm_tools::plugin_dispatcher::VmState::VM_STATE_STOPPED);
VmPluginDispatcherClient().set_list_vms_response(list_vms_response);
plugin_vm_manager_.LaunchPluginVm();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(VmPluginDispatcherClient().list_vms_called());
EXPECT_TRUE(VmPluginDispatcherClient().start_vm_called());
EXPECT_TRUE(VmPluginDispatcherClient().show_vm_called());
}
TEST_F(PluginVmManagerTest, LaunchPluginVmShow) {
test_helper_.AllowPluginVm();
EXPECT_TRUE(IsPluginVmAllowedForProfile(&testing_profile_));
// The PluginVmManager skips calling StartVm when the VM is already running.
vm_tools::plugin_dispatcher::ListVmResponse list_vms_response;
list_vms_response.add_vm_info()->set_state(
vm_tools::plugin_dispatcher::VmState::VM_STATE_RUNNING);
VmPluginDispatcherClient().set_list_vms_response(list_vms_response);
plugin_vm_manager_.LaunchPluginVm();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(VmPluginDispatcherClient().list_vms_called());
EXPECT_FALSE(VmPluginDispatcherClient().start_vm_called());
EXPECT_TRUE(VmPluginDispatcherClient().show_vm_called());
}
} // namespace plugin_vm
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_test_helper.h"
#include "base/json/json_reader.h"
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_pref_names.h"
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_util.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/test/base/testing_profile.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace plugin_vm {
PluginVmTestHelper::PluginVmTestHelper(TestingProfile* testing_profile)
: testing_profile_(testing_profile) {
testing_profile_->ScopedCrosSettingsTestHelper()
->ReplaceDeviceSettingsProviderWithStub();
}
PluginVmTestHelper::~PluginVmTestHelper() = default;
void PluginVmTestHelper::SetPolicyRequirementsToAllowPluginVm() {
testing_profile_->ScopedCrosSettingsTestHelper()->SetBoolean(
chromeos::kPluginVmAllowed, true);
testing_profile_->ScopedCrosSettingsTestHelper()->SetString(
chromeos::kPluginVmLicenseKey, "LICENSE_KEY");
testing_profile_->GetPrefs()->Set(plugin_vm::prefs::kPluginVmImage,
*base::JSONReader::ReadDeprecated(R"(
{
"url": "https://example.com/plugin_vm_image",
"hash": "842841a4c75a55ad050d686f4ea5f77e83ae059877fe9b6946aa63d3d057ed32"
}
)"));
}
void PluginVmTestHelper::SetUserRequirementsToAllowPluginVm() {
// User for the profile should be affiliated with the device.
const AccountId account_id(AccountId::FromUserEmailGaiaId(
testing_profile_->GetProfileUserName(), "id"));
user_manager_.AddUserWithAffiliationAndType(account_id, true,
user_manager::USER_TYPE_REGULAR);
chromeos::ProfileHelper::Get()->SetProfileToUserMappingForTesting(
user_manager_.GetActiveUser());
}
void PluginVmTestHelper::AllowPluginVm() {
ASSERT_FALSE(IsPluginVmAllowedForProfile(testing_profile_));
SetPolicyRequirementsToAllowPluginVm();
SetUserRequirementsToAllowPluginVm();
ASSERT_TRUE(IsPluginVmAllowedForProfile(testing_profile_));
}
} // namespace plugin_vm
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_PLUGIN_VM_PLUGIN_VM_TEST_HELPER_H_
#define CHROME_BROWSER_CHROMEOS_PLUGIN_VM_PLUGIN_VM_TEST_HELPER_H_
#include "chrome/browser/chromeos/login/users/mock_user_manager.h"
class TestingProfile;
namespace plugin_vm {
// A helper class for enabling Plugin VM in unit tests.
class PluginVmTestHelper {
public:
explicit PluginVmTestHelper(TestingProfile* testing_profile);
~PluginVmTestHelper();
void SetPolicyRequirementsToAllowPluginVm();
void SetUserRequirementsToAllowPluginVm();
void AllowPluginVm();
private:
TestingProfile* testing_profile_;
chromeos::MockUserManager user_manager_;
DISALLOW_COPY_AND_ASSIGN(PluginVmTestHelper);
};
} // namespace plugin_vm
#endif // CHROME_BROWSER_CHROMEOS_PLUGIN_VM_PLUGIN_VM_TEST_HELPER_H_
...@@ -4,15 +4,12 @@ ...@@ -4,15 +4,12 @@
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_util.h" #include "chrome/browser/chromeos/plugin_vm/plugin_vm_util.h"
#include "base/json/json_reader.h"
#include "base/test/mock_callback.h"
#include "chrome/browser/chromeos/login/users/mock_user_manager.h"
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_pref_names.h" #include "chrome/browser/chromeos/plugin_vm/plugin_vm_pref_names.h"
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_test_helper.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h" #include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/chromeos/settings/cros_settings.h" #include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h" #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -21,57 +18,12 @@ namespace plugin_vm { ...@@ -21,57 +18,12 @@ namespace plugin_vm {
class PluginVmUtilTest : public testing::Test { class PluginVmUtilTest : public testing::Test {
public: public:
PluginVmUtilTest() = default; PluginVmUtilTest() : test_helper_(&testing_profile_) {}
void SetUp() override {
settings_helper_.ReplaceDeviceSettingsProviderWithStub();
profile_builder.SetProfileName("user0");
testing_profile_ = profile_builder.Build();
}
void TearDown() override {
settings_helper_.RestoreRealDeviceSettingsProvider();
testing_profile_.reset();
}
protected: protected:
TestingProfile::Builder profile_builder;
chromeos::ScopedCrosSettingsTestHelper settings_helper_;
std::unique_ptr<TestingProfile> testing_profile_;
content::TestBrowserThreadBundle thread_bundle_; content::TestBrowserThreadBundle thread_bundle_;
chromeos::MockUserManager user_manager_; TestingProfile testing_profile_;
PluginVmTestHelper test_helper_;
void SetPolicyRequirementsToAllowPluginVm() {
settings_helper_.SetBoolean(chromeos::kPluginVmAllowed, true);
EXPECT_FALSE(IsPluginVmAllowedForProfile(testing_profile_.get()));
settings_helper_.SetString(chromeos::kPluginVmLicenseKey, "LICENSE_KEY");
EXPECT_FALSE(IsPluginVmAllowedForProfile(testing_profile_.get()));
testing_profile_->GetPrefs()->Set(plugin_vm::prefs::kPluginVmImage,
*base::JSONReader::ReadDeprecated(R"(
{
"url": "https://example.com/plugin_vm_image",
"hash": "842841a4c75a55ad050d686f4ea5f77e83ae059877fe9b6946aa63d3d057ed32"
}
)"));
}
void SetUserRequirementsToAllowPluginVm() {
// User for the profile should be affiliated with the device.
const AccountId account_id(AccountId::FromUserEmailGaiaId(
testing_profile_->GetProfileUserName(), "id"));
user_manager_.AddUserWithAffiliationAndType(
account_id, true, user_manager::USER_TYPE_REGULAR);
chromeos::ProfileHelper::Get()->SetProfileToUserMappingForTesting(
user_manager_.GetActiveUser());
}
void AllowPluginVm() {
SetPolicyRequirementsToAllowPluginVm();
EXPECT_FALSE(IsPluginVmAllowedForProfile(testing_profile_.get()));
SetUserRequirementsToAllowPluginVm();
}
private: private:
DISALLOW_COPY_AND_ASSIGN(PluginVmUtilTest); DISALLOW_COPY_AND_ASSIGN(PluginVmUtilTest);
...@@ -79,21 +31,21 @@ class PluginVmUtilTest : public testing::Test { ...@@ -79,21 +31,21 @@ class PluginVmUtilTest : public testing::Test {
TEST_F(PluginVmUtilTest, TEST_F(PluginVmUtilTest,
IsPluginVmAllowedForProfileReturnsTrueOnceAllConditionsAreMet) { IsPluginVmAllowedForProfileReturnsTrueOnceAllConditionsAreMet) {
EXPECT_FALSE(IsPluginVmAllowedForProfile(testing_profile_.get())); EXPECT_FALSE(IsPluginVmAllowedForProfile(&testing_profile_));
AllowPluginVm(); test_helper_.AllowPluginVm();
EXPECT_TRUE(IsPluginVmAllowedForProfile(testing_profile_.get())); EXPECT_TRUE(IsPluginVmAllowedForProfile(&testing_profile_));
} }
TEST_F(PluginVmUtilTest, TEST_F(PluginVmUtilTest,
IsPluginVmConfiguredReturnsTrueOnceAllConditionsAreMet) { IsPluginVmConfiguredReturnsTrueOnceAllConditionsAreMet) {
EXPECT_FALSE(IsPluginVmConfigured(testing_profile_.get())); EXPECT_FALSE(IsPluginVmConfigured(&testing_profile_));
testing_profile_->GetPrefs()->SetBoolean( testing_profile_.GetPrefs()->SetBoolean(
plugin_vm::prefs::kPluginVmImageExists, true); plugin_vm::prefs::kPluginVmImageExists, true);
EXPECT_TRUE(IsPluginVmConfigured(testing_profile_.get())); EXPECT_TRUE(IsPluginVmConfigured(&testing_profile_));
} }
TEST_F(PluginVmUtilTest, GetPluginVmLicenseKey) { TEST_F(PluginVmUtilTest, GetPluginVmLicenseKey) {
...@@ -101,7 +53,8 @@ TEST_F(PluginVmUtilTest, GetPluginVmLicenseKey) { ...@@ -101,7 +53,8 @@ TEST_F(PluginVmUtilTest, GetPluginVmLicenseKey) {
EXPECT_EQ(std::string(), GetPluginVmLicenseKey()); EXPECT_EQ(std::string(), GetPluginVmLicenseKey());
const std::string kLicenseKey = "LICENSE_KEY"; const std::string kLicenseKey = "LICENSE_KEY";
settings_helper_.SetString(chromeos::kPluginVmLicenseKey, kLicenseKey); testing_profile_.ScopedCrosSettingsTestHelper()->SetString(
chromeos::kPluginVmLicenseKey, kLicenseKey);
EXPECT_EQ(kLicenseKey, GetPluginVmLicenseKey()); EXPECT_EQ(kLicenseKey, GetPluginVmLicenseKey());
} }
......
...@@ -3764,6 +3764,9 @@ test("unit_tests") { ...@@ -3764,6 +3764,9 @@ test("unit_tests") {
"../browser/chromeos/crostini/crostini_registry_service_unittest.cc", "../browser/chromeos/crostini/crostini_registry_service_unittest.cc",
"../browser/chromeos/crostini/crostini_reporting_util_unittest.cc", "../browser/chromeos/crostini/crostini_reporting_util_unittest.cc",
"../browser/chromeos/plugin_vm/plugin_vm_files_unittest.cc", "../browser/chromeos/plugin_vm/plugin_vm_files_unittest.cc",
"../browser/chromeos/plugin_vm/plugin_vm_manager_unittest.cc",
"../browser/chromeos/plugin_vm/plugin_vm_test_helper.cc",
"../browser/chromeos/plugin_vm/plugin_vm_test_helper.h",
"../browser/chromeos/plugin_vm/plugin_vm_util_unittest.cc", "../browser/chromeos/plugin_vm/plugin_vm_util_unittest.cc",
"../browser/chromeos/usb/cros_usb_detector_unittest.cc", "../browser/chromeos/usb/cros_usb_detector_unittest.cc",
"../browser/component_updater/cros_component_installer_chromeos_unittest.cc", "../browser/component_updater/cros_component_installer_chromeos_unittest.cc",
......
...@@ -20,6 +20,7 @@ FakeVmPluginDispatcherClient::~FakeVmPluginDispatcherClient() = default; ...@@ -20,6 +20,7 @@ FakeVmPluginDispatcherClient::~FakeVmPluginDispatcherClient() = default;
void FakeVmPluginDispatcherClient::StartVm( void FakeVmPluginDispatcherClient::StartVm(
const StartVmRequest& request, const StartVmRequest& request,
DBusMethodCallback<StartVmResponse> callback) { DBusMethodCallback<StartVmResponse> callback) {
start_vm_called_ = true;
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), StartVmResponse())); FROM_HERE, base::BindOnce(std::move(callback), StartVmResponse()));
} }
...@@ -27,15 +28,15 @@ void FakeVmPluginDispatcherClient::StartVm( ...@@ -27,15 +28,15 @@ void FakeVmPluginDispatcherClient::StartVm(
void FakeVmPluginDispatcherClient::ListVms( void FakeVmPluginDispatcherClient::ListVms(
const ListVmRequest& request, const ListVmRequest& request,
DBusMethodCallback<ListVmResponse> callback) { DBusMethodCallback<ListVmResponse> callback) {
ListVmResponse response; list_vms_called_ = true;
response.add_vm_info()->set_name("vm");
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), response)); FROM_HERE, base::BindOnce(std::move(callback), list_vms_response_));
} }
void FakeVmPluginDispatcherClient::StopVm( void FakeVmPluginDispatcherClient::StopVm(
const StopVmRequest& request, const StopVmRequest& request,
DBusMethodCallback<StopVmResponse> callback) { DBusMethodCallback<StopVmResponse> callback) {
stop_vm_called_ = true;
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), StopVmResponse())); FROM_HERE, base::BindOnce(std::move(callback), StopVmResponse()));
} }
...@@ -43,6 +44,7 @@ void FakeVmPluginDispatcherClient::StopVm( ...@@ -43,6 +44,7 @@ void FakeVmPluginDispatcherClient::StopVm(
void FakeVmPluginDispatcherClient::SuspendVm( void FakeVmPluginDispatcherClient::SuspendVm(
const SuspendVmRequest& request, const SuspendVmRequest& request,
DBusMethodCallback<SuspendVmResponse> callback) { DBusMethodCallback<SuspendVmResponse> callback) {
suspend_vm_called_ = true;
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), SuspendVmResponse())); FROM_HERE, base::BindOnce(std::move(callback), SuspendVmResponse()));
} }
...@@ -50,6 +52,7 @@ void FakeVmPluginDispatcherClient::SuspendVm( ...@@ -50,6 +52,7 @@ void FakeVmPluginDispatcherClient::SuspendVm(
void FakeVmPluginDispatcherClient::ShowVm( void FakeVmPluginDispatcherClient::ShowVm(
const ShowVmRequest& request, const ShowVmRequest& request,
DBusMethodCallback<ShowVmResponse> callback) { DBusMethodCallback<ShowVmResponse> callback) {
show_vm_called_ = true;
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), ShowVmResponse())); FROM_HERE, base::BindOnce(std::move(callback), ShowVmResponse()));
} }
......
...@@ -39,10 +39,30 @@ class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeVmPluginDispatcherClient ...@@ -39,10 +39,30 @@ class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeVmPluginDispatcherClient
void WaitForServiceToBeAvailable( void WaitForServiceToBeAvailable(
dbus::ObjectProxy::WaitForServiceToBeAvailableCallback callback) override; dbus::ObjectProxy::WaitForServiceToBeAvailableCallback callback) override;
// Helper functions for testing interaction with the VmPluginDispatcherClient.
bool start_vm_called() const { return start_vm_called_; }
bool list_vms_called() const { return list_vms_called_; }
bool stop_vm_called() const { return stop_vm_called_; }
bool suspend_vm_called() const { return suspend_vm_called_; }
bool show_vm_called() const { return show_vm_called_; }
void set_list_vms_response(
const vm_tools::plugin_dispatcher::ListVmResponse& response) {
list_vms_response_ = response;
}
protected: protected:
void Init(dbus::Bus* bus) override {} void Init(dbus::Bus* bus) override {}
private: private:
bool start_vm_called_ = false;
bool list_vms_called_ = false;
bool stop_vm_called_ = false;
bool suspend_vm_called_ = false;
bool show_vm_called_ = false;
vm_tools::plugin_dispatcher::ListVmResponse list_vms_response_;
DISALLOW_COPY_AND_ASSIGN(FakeVmPluginDispatcherClient); DISALLOW_COPY_AND_ASSIGN(FakeVmPluginDispatcherClient);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment