Commit 6bf5b850 authored by Marc Grimme's avatar Marc Grimme Committed by Commit Bot

Unittests for ManagementUIHandler::AddDeviceReportingInfo()

Currently no unit tests are covering the logic behind
ManagementUIHandler::AddDeviceReportingInfo().
This CL adds the missing unit tests and does some basic refactoring in
the test class itself.

R=marcgrimme

Bug: 1044954
Change-Id: I1767deec4d6754e9bb55ad544fb6f0a59ca697cd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2016640
Commit-Queue: Marc Grimme <marcgrimme@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarRoman Aleksandrov <raleksandrov@google.com>
Cr-Commit-Position: refs/heads/master@{#762800}
parent 3ab6b3bc
...@@ -46,6 +46,7 @@ ...@@ -46,6 +46,7 @@
#include "chrome/browser/chromeos/policy/policy_cert_service.h" #include "chrome/browser/chromeos/policy/policy_cert_service.h"
#include "chrome/browser/chromeos/policy/policy_cert_service_factory.h" #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
#include "chrome/browser/chromeos/policy/status_collector/device_status_collector.h" #include "chrome/browser/chromeos/policy/status_collector/device_status_collector.h"
#include "chrome/browser/chromeos/policy/status_collector/status_collector.h"
#include "chrome/browser/chromeos/policy/status_uploader.h" #include "chrome/browser/chromeos/policy/status_uploader.h"
#include "chrome/browser/chromeos/policy/system_log_uploader.h" #include "chrome/browser/chromeos/policy/system_log_uploader.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h" #include "chrome/browser/chromeos/profiles/profile_helper.h"
...@@ -228,85 +229,6 @@ void AddDeviceReportingElement(base::Value* report_sources, ...@@ -228,85 +229,6 @@ void AddDeviceReportingElement(base::Value* report_sources,
data.SetKey("reportingType", base::Value(ToJSDeviceReportingType(type))); data.SetKey("reportingType", base::Value(ToJSDeviceReportingType(type)));
report_sources->Append(std::move(data)); report_sources->Append(std::move(data));
} }
void AddDeviceReportingInfo(base::Value* report_sources, Profile* profile) {
policy::BrowserPolicyConnectorChromeOS* connector =
g_browser_process->platform_part()->browser_policy_connector_chromeos();
// Only check for report status in managed environment.
if (!connector->IsEnterpriseManaged())
return;
policy::DeviceCloudPolicyManagerChromeOS* manager =
connector->GetDeviceCloudPolicyManager();
if (!manager)
return;
const policy::StatusCollector* collector =
manager->GetStatusUploader()->status_collector();
// Elements appear on the page in the order they are added.
if (collector->ShouldReportActivityTimes()) {
AddDeviceReportingElement(report_sources, kManagementReportActivityTimes,
DeviceReportingType::kDeviceActivity);
} else {
if (collector->ShouldReportUsers()) {
AddDeviceReportingElement(report_sources, kManagementReportUsers,
DeviceReportingType::kSupervisedUser);
}
}
if (collector->ShouldReportHardwareStatus()) {
AddDeviceReportingElement(report_sources, kManagementReportHardwareStatus,
DeviceReportingType::kDeviceStatistics);
}
if (collector->ShouldReportNetworkInterfaces()) {
AddDeviceReportingElement(report_sources,
kManagementReportNetworkInterfaces,
DeviceReportingType::kDevice);
}
if (collector->ShouldReportCrashReportInfo()) {
AddDeviceReportingElement(report_sources, kManagementReportCrashReports,
DeviceReportingType::kCrashReport);
}
if (manager->GetSystemLogUploader()->upload_enabled()) {
AddDeviceReportingElement(report_sources, kManagementLogUploadEnabled,
DeviceReportingType::kLogs);
}
if (profile->GetPrefs()->GetBoolean(
prefs::kPrintingSendUsernameAndFilenameEnabled)) {
AddDeviceReportingElement(report_sources, kManagementPrinting,
DeviceReportingType::kPrint);
}
if (crostini::CrostiniFeatures::Get()->IsAllowed(profile)) {
if (!profile->GetPrefs()
->GetFilePath(crostini::prefs::kCrostiniAnsiblePlaybookFilePath)
.empty()) {
AddDeviceReportingElement(report_sources,
kManagementCrostiniContainerConfiguration,
DeviceReportingType::kCrostini);
} else if (profile->GetPrefs()->GetBoolean(
crostini::prefs::kReportCrostiniUsageEnabled)) {
AddDeviceReportingElement(report_sources, kManagementCrostini,
DeviceReportingType::kCrostini);
}
}
if (g_browser_process->local_state()->GetBoolean(
prefs::kCloudReportingEnabled) &&
base::FeatureList::IsEnabled(features::kEnterpriseReportingInChromeOS)) {
AddDeviceReportingElement(report_sources,
kManagementExtensionReportUsername,
DeviceReportingType::kUsername);
AddDeviceReportingElement(report_sources, kManagementReportExtensions,
DeviceReportingType::kExtensions);
AddDeviceReportingElement(report_sources,
kManagementReportAndroidApplications,
DeviceReportingType::kAndroidApplication);
}
}
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
std::vector<base::Value> GetPermissionsForExtension( std::vector<base::Value> GetPermissionsForExtension(
...@@ -566,6 +488,64 @@ void ManagementUIHandler::AddReportingInfo(base::Value* report_sources) { ...@@ -566,6 +488,64 @@ void ManagementUIHandler::AddReportingInfo(base::Value* report_sources) {
} }
} }
#if defined(OS_CHROMEOS)
const policy::DeviceCloudPolicyManagerChromeOS*
ManagementUIHandler::GetDeviceCloudPolicyManager() const {
// Only check for report status in managed environment.
if (!device_managed_)
return nullptr;
const policy::BrowserPolicyConnectorChromeOS* connector =
g_browser_process->platform_part()->browser_policy_connector_chromeos();
return connector->GetDeviceCloudPolicyManager();
}
void ManagementUIHandler::AddDeviceReportingInfo(
base::Value* report_sources,
const policy::StatusCollector* collector,
const policy::SystemLogUploader* uploader,
const Profile* profile) const {
if (!collector || !profile || !uploader)
return;
// Elements appear on the page in the order they are added.
if (collector->ShouldReportActivityTimes()) {
AddDeviceReportingElement(report_sources, kManagementReportActivityTimes,
DeviceReportingType::kDeviceActivity);
} else {
if (collector->ShouldReportUsers()) {
AddDeviceReportingElement(report_sources, kManagementReportUsers,
DeviceReportingType::kSupervisedUser);
}
}
if (collector->ShouldReportHardwareStatus()) {
AddDeviceReportingElement(report_sources, kManagementReportHardwareStatus,
DeviceReportingType::kDeviceStatistics);
}
if (collector->ShouldReportNetworkInterfaces()) {
AddDeviceReportingElement(report_sources,
kManagementReportNetworkInterfaces,
DeviceReportingType::kDevice);
}
if (uploader->upload_enabled()) {
AddDeviceReportingElement(report_sources, kManagementLogUploadEnabled,
DeviceReportingType::kLogs);
}
if (profile->GetPrefs()->GetBoolean(
prefs::kPrintingSendUsernameAndFilenameEnabled)) {
AddDeviceReportingElement(report_sources, kManagementPrinting,
DeviceReportingType::kPrint);
}
if (profile->GetPrefs()->GetBoolean(
crostini::prefs::kReportCrostiniUsageEnabled)) {
AddDeviceReportingElement(report_sources, kManagementCrostini,
DeviceReportingType::kCrostini);
}
}
#endif
base::Value ManagementUIHandler::GetContextualManagedData(Profile* profile) { base::Value ManagementUIHandler::GetContextualManagedData(Profile* profile) {
base::Value response(base::Value::Type::DICTIONARY); base::Value response(base::Value::Type::DICTIONARY);
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -861,7 +841,19 @@ void ManagementUIHandler::HandleGetDeviceReportingInfo( ...@@ -861,7 +841,19 @@ void ManagementUIHandler::HandleGetDeviceReportingInfo(
base::Value report_sources(base::Value::Type::LIST); base::Value report_sources(base::Value::Type::LIST);
AllowJavascript(); AllowJavascript();
AddDeviceReportingInfo(&report_sources, Profile::FromWebUI(web_ui())); const policy::DeviceCloudPolicyManagerChromeOS* manager =
GetDeviceCloudPolicyManager();
policy::StatusUploader* uploader = nullptr;
policy::SystemLogUploader* syslog_uploader = nullptr;
policy::StatusCollector* collector = nullptr;
if (manager) {
uploader = manager->GetStatusUploader();
syslog_uploader = manager->GetSystemLogUploader();
if (uploader)
collector = uploader->status_collector();
}
AddDeviceReportingInfo(&report_sources, collector, syslog_uploader,
Profile::FromWebUI(web_ui()));
ResolveJavascriptCallback(args->GetList()[0] /* callback_id */, ResolveJavascriptCallback(args->GetList()[0] /* callback_id */,
report_sources); report_sources);
......
...@@ -85,7 +85,10 @@ class Extension; ...@@ -85,7 +85,10 @@ class Extension;
} // namespace extensions } // namespace extensions
namespace policy { namespace policy {
class DeviceCloudPolicyManagerChromeOS;
class PolicyService; class PolicyService;
class StatusCollector;
class SystemLogUploader;
} // namespace policy } // namespace policy
class Profile; class Profile;
...@@ -129,6 +132,12 @@ class ManagementUIHandler : public content::WebUIMessageHandler, ...@@ -129,6 +132,12 @@ class ManagementUIHandler : public content::WebUIMessageHandler,
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
// Protected for testing. // Protected for testing.
virtual const std::string GetDeviceDomain() const; virtual const std::string GetDeviceDomain() const;
virtual const policy::DeviceCloudPolicyManagerChromeOS*
GetDeviceCloudPolicyManager() const;
void AddDeviceReportingInfo(base::Value* report_sources,
const policy::StatusCollector* collector,
const policy::SystemLogUploader* uploader,
const Profile* profile) const;
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
private: private:
void GetManagementStatus(Profile* profile, base::Value* status) const; void GetManagementStatus(Profile* profile, base::Value* status) const;
......
...@@ -6,11 +6,13 @@ ...@@ -6,11 +6,13 @@
#include <set> #include <set>
#include <string> #include <string>
#include "base/memory/scoped_refptr.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/webui/management_ui_handler.h" #include "chrome/browser/ui/webui/management_ui_handler.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/mock_policy_service.h" #include "components/policy/core/common/mock_policy_service.h"
#include "components/policy/core/common/policy_map.h" #include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_namespace.h" #include "components/policy/core/common/policy_namespace.h"
...@@ -22,14 +24,40 @@ ...@@ -22,14 +24,40 @@
#include "extensions/common/extension_builder.h" #include "extensions/common/extension_builder.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
#include "base/test/test_simple_task_runner.h"
#include "chrome/browser/chromeos/crostini/crostini_pref_names.h"
#include "chrome/browser/chromeos/policy/device_cloud_policy_initializer.h"
#include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
#include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h"
#include "chrome/browser/chromeos/policy/status_collector/device_status_collector.h"
#include "chrome/browser/chromeos/policy/status_collector/status_collector.h"
#include "chrome/browser/chromeos/policy/status_uploader.h"
#include "chrome/browser/chromeos/policy/system_log_uploader.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
#include "chrome/browser/chromeos/settings/scoped_testing_cros_settings.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chromeos/cryptohome/async_method_caller.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "chromeos/system/fake_statistics_provider.h"
#include "chromeos/tpm/stub_install_attributes.h"
#include "components/policy/core/common/cloud/cloud_external_data_manager.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "components/policy/core/common/cloud/mock_signing_service.h"
#include "components/prefs/testing_pref_service.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "ui/chromeos/devicetype_utils.h" #include "ui/chromeos/devicetype_utils.h"
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
using testing::_; using testing::_;
using testing::AssertionFailure;
using testing::AssertionResult;
using testing::AssertionSuccess;
using testing::Return; using testing::Return;
using testing::ReturnRef; using testing::ReturnRef;
...@@ -44,6 +72,59 @@ struct ContextualManagementSourceUpdate { ...@@ -44,6 +72,59 @@ struct ContextualManagementSourceUpdate {
bool managed; bool managed;
}; };
#if defined(OS_CHROMEOS)
// This class is just to mock the behaviour of the few flags we need for
// simulating the behaviour of the policy::DeviceStatusCollector.
// The expected flags are passed to the constructor.
class TestDeviceStatusCollector : public policy::DeviceStatusCollector {
public:
TestDeviceStatusCollector(PrefService* local_state,
bool report_activity_times,
bool report_nics,
bool report_users,
bool report_hw_status)
: policy::DeviceStatusCollector(local_state, nullptr),
report_activity_times_(report_activity_times),
report_nics_(report_nics),
report_users_(report_users),
report_hw_status_(report_hw_status) {}
~TestDeviceStatusCollector() override = default;
bool ShouldReportActivityTimes() const override {
return report_activity_times_;
}
bool ShouldReportNetworkInterfaces() const override { return report_nics_; }
bool ShouldReportUsers() const override { return report_users_; }
bool ShouldReportHardwareStatus() const override { return report_hw_status_; }
// empty methods that need to be implemented but are of no use for this case.
void GetStatusAsync(
const policy::StatusCollectorCallback& callback) override {}
void OnSubmittedSuccessfully() override {}
private:
bool report_activity_times_;
bool report_nics_;
bool report_users_;
bool report_hw_status_;
};
class TestDeviceCloudPolicyManagerChromeOS
: public policy::DeviceCloudPolicyManagerChromeOS {
public:
TestDeviceCloudPolicyManagerChromeOS(
std::unique_ptr<policy::DeviceCloudPolicyStoreChromeOS> store,
policy::ServerBackedStateKeysBroker* state_keys_broker)
: DeviceCloudPolicyManagerChromeOS(std::move(store),
nullptr,
nullptr,
state_keys_broker) {
set_component_policy_disabled_for_testing(true);
}
~TestDeviceCloudPolicyManagerChromeOS() override = default;
};
#endif
class TestManagementUIHandler : public ManagementUIHandler { class TestManagementUIHandler : public ManagementUIHandler {
public: public:
TestManagementUIHandler() = default; TestManagementUIHandler() = default;
...@@ -81,6 +162,21 @@ class TestManagementUIHandler : public ManagementUIHandler { ...@@ -81,6 +162,21 @@ class TestManagementUIHandler : public ManagementUIHandler {
} }
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
MOCK_METHOD(policy::DeviceCloudPolicyManagerChromeOS*,
GetDeviceCloudPolicyManager,
(),
(const, override));
base::Value GetDeviceReportingInfo(
const TestDeviceCloudPolicyManagerChromeOS* manager,
const TestDeviceStatusCollector* collector,
const policy::SystemLogUploader* uploader,
const Profile* profile) {
base::Value report_sources = base::Value(base::Value::Type::LIST);
AddDeviceReportingInfo(&report_sources, collector, uploader, profile);
return report_sources;
}
const std::string GetDeviceDomain() const override { return device_domain; } const std::string GetDeviceDomain() const override { return device_domain; }
void SetDeviceDomain(const std::string& domain) { device_domain = domain; } void SetDeviceDomain(const std::string& domain) { device_domain = domain; }
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
...@@ -91,14 +187,34 @@ class TestManagementUIHandler : public ManagementUIHandler { ...@@ -91,14 +187,34 @@ class TestManagementUIHandler : public ManagementUIHandler {
std::string device_domain = "devicedomain.com"; std::string device_domain = "devicedomain.com";
}; };
class ManagementUIHandlerTests : public testing::Test { // We need to use a different base class for ChromeOS and non ChromeOS case.
// TODO(marcgrimme): refactor so that ChromeOS and non ChromeOS part is better
// separated.
#if defined(OS_CHROMEOS)
using TestingBaseClass = chromeos::DeviceSettingsTestBase;
#else
using TestingBaseClass = testing::Test;
#endif
class ManagementUIHandlerTests : public TestingBaseClass {
public: public:
#if defined(OS_CHROMEOS)
ManagementUIHandlerTests() ManagementUIHandlerTests()
: handler_(&policy_service_), : TestingBaseClass(),
device_domain_(base::UTF8ToUTF16("devicedomain.com")) { handler_(&policy_service_),
device_domain_(base::UTF8ToUTF16("devicedomain.com")),
task_runner_(base::MakeRefCounted<base::TestSimpleTaskRunner>()),
state_keys_broker_(&session_manager_client_) {
ON_CALL(policy_service_, GetPolicies(_))
.WillByDefault(ReturnRef(empty_policy_map_));
}
#else
ManagementUIHandlerTests() : TestingBaseClass(), handler_(&policy_service_) {
ON_CALL(policy_service_, GetPolicies(_)) ON_CALL(policy_service_, GetPolicies(_))
.WillByDefault(ReturnRef(empty_policy_map_)); .WillByDefault(ReturnRef(empty_policy_map_));
} }
#endif
~ManagementUIHandlerTests() override = default;
base::string16 device_domain() { return device_domain_; } base::string16 device_domain() { return device_domain_; }
void EnablePolicy(const char* policy_key, policy::PolicyMap& policies) { void EnablePolicy(const char* policy_key, policy::PolicyMap& policies) {
...@@ -143,34 +259,106 @@ class ManagementUIHandlerTests : public testing::Test { ...@@ -143,34 +259,106 @@ class ManagementUIHandlerTests : public testing::Test {
extracted_.managed = managed.has_value() && managed.value(); extracted_.managed = managed.has_value() && managed.value();
} }
void PrepareProfileAndHandler() { /* Structure to organize the different configuration settings for each test
PrepareProfileAndHandler(std::string(), false, true, false, * into configuration for a test case. */
"devicedomain.com"); struct TestConfig {
bool report_activity_times;
bool report_nics;
bool report_users;
bool report_hw_status;
bool upload_enabled;
bool printing_send_username_and_filename;
bool crostini_report_usage;
std::string profile_name;
bool override_policy_connector_is_managed;
bool managed_account;
bool managed_device;
std::string device_domain;
};
void ResetTestConfig() { ResetTestConfig(true); }
void ResetTestConfig(bool default_value) {
setup_config_.report_activity_times = default_value;
setup_config_.report_nics = default_value;
setup_config_.report_users = default_value;
setup_config_.report_hw_status = default_value;
setup_config_.upload_enabled = default_value;
setup_config_.printing_send_username_and_filename = default_value;
setup_config_.crostini_report_usage = default_value;
setup_config_.profile_name = "";
setup_config_.override_policy_connector_is_managed = false;
setup_config_.managed_account = true;
setup_config_.managed_device = false;
setup_config_.device_domain = "devicedomain.com";
}
#if defined(OS_CHROMEOS)
void SetUp() override {
DeviceSettingsTestBase::SetUp();
install_attributes_ =
std::make_unique<chromeos::ScopedStubInstallAttributes>(
chromeos::StubInstallAttributes::CreateUnset());
SetUpConnectManager();
}
void TearDown() override {
TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr);
DeviceSettingsTestBase::TearDown();
}
void SetUpConnectManager() {
RegisterLocalState(local_state_.registry());
std::unique_ptr<policy::DeviceCloudPolicyStoreChromeOS> store =
std::make_unique<policy::DeviceCloudPolicyStoreChromeOS>(
device_settings_service_.get(), install_attributes_->Get(),
base::ThreadTaskRunnerHandle::Get());
manager_ = std::make_unique<TestDeviceCloudPolicyManagerChromeOS>(
std::move(store), &state_keys_broker_);
TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
manager_.get()->Initialize(&local_state_);
} }
void PrepareProfileAndHandler(const std::string& profile_name, base::Value SetUpForReportingInfo() {
bool override_policy_connector_is_managed, GetTestConfig().override_policy_connector_is_managed = true;
bool use_account, GetTestConfig().managed_device = true;
bool use_device) { SetUpProfileAndHandler();
PrepareProfileAndHandler(profile_name, override_policy_connector_is_managed, const TestDeviceStatusCollector* status_collector =
use_account, use_device, "devicedomain.com"); new TestDeviceStatusCollector(
&local_state_, GetTestConfig().report_activity_times,
GetTestConfig().report_nics, GetTestConfig().report_users,
GetTestConfig().report_hw_status);
settings_.device_settings()->SetTrustedStatus(
chromeos::CrosSettingsProvider::TRUSTED);
settings_.device_settings()->SetBoolean(chromeos::kSystemLogUploadEnabled,
GetTestConfig().upload_enabled);
profile_->GetPrefs()->SetBoolean(
prefs::kPrintingSendUsernameAndFilenameEnabled,
GetTestConfig().printing_send_username_and_filename);
profile_->GetPrefs()->SetBoolean(
crostini::prefs::kReportCrostiniUsageEnabled,
GetTestConfig().crostini_report_usage);
const policy::SystemLogUploader* system_uploader =
new policy::SystemLogUploader(/*syslog_delegate=*/nullptr,
/*task_runner=*/task_runner_);
ON_CALL(testing::Const(handler_), GetDeviceCloudPolicyManager())
.WillByDefault(Return(manager_.get()));
return handler_.GetDeviceReportingInfo(manager_.get(), status_collector,
system_uploader, GetProfile());
} }
#endif // defined(OS_CHROMEOS)
void PrepareProfileAndHandler(const std::string& profile_name, void SetUpProfileAndHandler() {
bool override_policy_connector_is_managed,
bool use_account,
bool use_device,
const std::string& device_domain) {
TestingProfile::Builder builder; TestingProfile::Builder builder;
builder.SetProfileName(profile_name); builder.SetProfileName(GetTestConfig().profile_name);
if (override_policy_connector_is_managed) { if (GetTestConfig().override_policy_connector_is_managed) {
builder.OverridePolicyConnectorIsManagedForTesting(true); builder.OverridePolicyConnectorIsManagedForTesting(true);
} }
profile_ = builder.Build(); profile_ = builder.Build();
handler_.SetAccountManagedForTesting(use_account); handler_.SetAccountManagedForTesting(GetTestConfig().managed_account);
handler_.SetDeviceManagedForTesting(use_device); handler_.SetDeviceManagedForTesting(GetTestConfig().managed_device);
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
handler_.SetDeviceDomain(device_domain); handler_.SetDeviceDomain(GetTestConfig().device_domain);
#endif #endif
base::Value data = base::Value data =
handler_.GetContextualManagedDataForTesting(profile_.get()); handler_.GetContextualManagedDataForTesting(profile_.get());
...@@ -195,37 +383,117 @@ class ManagementUIHandlerTests : public testing::Test { ...@@ -195,37 +383,117 @@ class ManagementUIHandlerTests : public testing::Test {
base::string16 GetPageSubtitle() const { return extracted_.subtitle; } base::string16 GetPageSubtitle() const { return extracted_.subtitle; }
const TestingProfile* GetProfile() const { return profile_.get(); }
TestConfig& GetTestConfig() { return setup_config_; }
protected: protected:
TestConfig setup_config_;
TestManagementUIHandler handler_; TestManagementUIHandler handler_;
content::BrowserTaskEnvironment task_environment_;
policy::MockPolicyService policy_service_; policy::MockPolicyService policy_service_;
policy::PolicyMap empty_policy_map_; policy::PolicyMap empty_policy_map_;
base::string16 device_domain_; base::string16 device_domain_;
ContextualManagementSourceUpdate extracted_; ContextualManagementSourceUpdate extracted_;
#if defined(OS_CHROMEOS)
std::unique_ptr<chromeos::ScopedStubInstallAttributes> install_attributes_;
TestingPrefServiceSimple local_state_;
std::unique_ptr<TestDeviceCloudPolicyManagerChromeOS> manager_;
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
policy::ServerBackedStateKeysBroker state_keys_broker_;
chromeos::ScopedTestingCrosSettings settings_;
chromeos::system::ScopedFakeStatisticsProvider fake_statistics_provider_;
#else
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<TestingProfile> profile_; std::unique_ptr<TestingProfile> profile_;
#endif
}; };
void ExpectMessagesToBeEQ(base::Value::ConstListView infolist, AssertionResult MessagesToBeEQ(const char* infolist_expr,
const std::set<std::string>& expected_messages) { const char* expected_infolist_expr,
ASSERT_EQ(infolist.size(), expected_messages.size()); base::Value::ConstListView infolist,
const std::set<std::string>& expected_messages) {
if (infolist.size() != expected_messages.size()) {
return AssertionFailure()
<< " " << infolist_expr << " and " << expected_infolist_expr
<< " don't have the same size. (" << infolist.size() << ", "
<< expected_messages.size() << ")";
}
std::set<std::string> tmp_expected(expected_messages); std::set<std::string> tmp_expected(expected_messages);
for (const base::Value& info : infolist) { for (const base::Value& info : infolist) {
const std::string* message_id = info.FindStringKey("messageId"); const std::string* message_id = info.FindStringKey("messageId");
if (message_id) { if (message_id) {
EXPECT_EQ(1u, tmp_expected.erase(*message_id)); if (tmp_expected.erase(*message_id) != 1u) {
return AssertionFailure() << " message " << *message_id << " is not in "
<< expected_infolist_expr;
}
}
}
if (!tmp_expected.empty()) {
return AssertionFailure()
<< " " << infolist_expr << " and " << expected_infolist_expr
<< " have different contents " << infolist.data();
}
return AssertionSuccess();
}
#if defined(OS_CHROMEOS)
AssertionResult ReportingElementsToBeEQ(
const char* elements_expr,
const char* expected_elements_expr,
base::Value::ConstListView elements,
const std::map<std::string, std::string> expected_elements) {
if (elements.size() != expected_elements.size()) {
return AssertionFailure()
<< elements_expr << " and " << expected_elements_expr
<< " don't have the same size. (" << elements.size() << ", "
<< expected_elements.size() << ")";
}
std::map<std::string, std::string> tmp_expected(expected_elements);
for (const base::Value& element : elements) {
const std::string* message_id = element.FindStringKey("messageId");
const std::string* js_reporting_type =
element.FindStringKey("reportingType");
if (message_id && js_reporting_type) {
auto tmp_reporting_type = tmp_expected.find(*message_id);
if (tmp_reporting_type == tmp_expected.end()) {
return AssertionFailure() << " could not find message " << *message_id
<< " in " << expected_elements_expr;
}
if (tmp_reporting_type->second != *js_reporting_type) {
return AssertionFailure()
<< " expected reporting element \"" << *js_reporting_type
<< "\" with key \"" << *message_id << "\" doesn't match \""
<< tmp_reporting_type->second << "\" in \""
<< expected_elements_expr << "\"";
}
tmp_expected.erase(tmp_reporting_type);
} else {
return AssertionFailure()
<< " couldn't find key messageId or reportingType in "
<< elements.data();
}
}
if (!tmp_expected.empty()) {
AssertionResult result = AssertionFailure();
result
<< " the following messageId and reportingTypes could not be matched {";
for (const auto& element : tmp_expected) {
result << " messageId: " << element.first << ", reportingType "
<< element.second;
} }
result << "}";
return result;
} }
EXPECT_TRUE(tmp_expected.empty()); return AssertionSuccess();
} }
#endif
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateUnmanagedNoDomain) { ManagementContextualSourceUpdateUnmanagedNoDomain) {
PrepareProfileAndHandler( ResetTestConfig();
/* profile_name= */ "", GetTestConfig().managed_account = false;
/* override_policy_connector_is_managed= */ false, SetUpProfileAndHandler();
/* use_account= */ false,
/* use_device= */ false);
EXPECT_EQ(GetExtensionReportingTitle(), EXPECT_EQ(GetExtensionReportingTitle(),
l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED)); l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
...@@ -239,7 +507,8 @@ TEST_F(ManagementUIHandlerTests, ...@@ -239,7 +507,8 @@ TEST_F(ManagementUIHandlerTests,
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateManagedNoDomain) { ManagementContextualSourceUpdateManagedNoDomain) {
PrepareProfileAndHandler(); ResetTestConfig();
SetUpProfileAndHandler();
EXPECT_EQ(GetExtensionReportingTitle(), EXPECT_EQ(GetExtensionReportingTitle(),
l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED)); l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
...@@ -254,10 +523,9 @@ TEST_F(ManagementUIHandlerTests, ...@@ -254,10 +523,9 @@ TEST_F(ManagementUIHandlerTests,
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateManagedConsumerDomain) { ManagementContextualSourceUpdateManagedConsumerDomain) {
PrepareProfileAndHandler( ResetTestConfig();
/* profile_name= */ "managed@gmail.com", GetTestConfig().override_policy_connector_is_managed = true;
/* override_policy_connector_is_managed= */ true, SetUpProfileAndHandler();
/* use_account= */ true, /* use_device= */ false);
EXPECT_EQ(GetExtensionReportingTitle(), EXPECT_EQ(GetExtensionReportingTitle(),
l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED)); l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
...@@ -273,10 +541,11 @@ TEST_F(ManagementUIHandlerTests, ...@@ -273,10 +541,11 @@ TEST_F(ManagementUIHandlerTests,
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateUnmanagedKnownDomain) { ManagementContextualSourceUpdateUnmanagedKnownDomain) {
const std::string domain = "manager.com"; const std::string domain = "manager.com";
PrepareProfileAndHandler( ResetTestConfig();
/* profile_name= */ "managed@" + domain, GetTestConfig().profile_name = "managed@" + domain;
/* override_policy_connector_is_managed= */ true, GetTestConfig().override_policy_connector_is_managed = true;
/* use_account= */ false, /* use_device= */ false); GetTestConfig().managed_account = false;
SetUpProfileAndHandler();
EXPECT_EQ(GetExtensionReportingTitle(), EXPECT_EQ(GetExtensionReportingTitle(),
l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY, l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY,
...@@ -292,10 +561,10 @@ TEST_F(ManagementUIHandlerTests, ...@@ -292,10 +561,10 @@ TEST_F(ManagementUIHandlerTests,
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateUnmanagedCustomerDomain) { ManagementContextualSourceUpdateUnmanagedCustomerDomain) {
PrepareProfileAndHandler( ResetTestConfig();
/* profile_name= */ "managed@googlemail.com", GetTestConfig().managed_account = false;
/* override_policy_connector_is_managed= */ false, SetUpProfileAndHandler();
/* use_account= */ false, /* use_device= */ false);
EXPECT_EQ(GetExtensionReportingTitle(), EXPECT_EQ(GetExtensionReportingTitle(),
l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED)); l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
EXPECT_EQ(GetBrowserManagementNotice(), EXPECT_EQ(GetBrowserManagementNotice(),
...@@ -310,10 +579,10 @@ TEST_F(ManagementUIHandlerTests, ...@@ -310,10 +579,10 @@ TEST_F(ManagementUIHandlerTests,
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateManagedKnownDomain) { ManagementContextualSourceUpdateManagedKnownDomain) {
const std::string domain = "gmail.com.manager.com.gmail.com"; const std::string domain = "gmail.com.manager.com.gmail.com";
PrepareProfileAndHandler( ResetTestConfig();
/* profile_name= */ "managed@" + domain, GetTestConfig().profile_name = "managed@" + domain;
/* override_policy_connector_is_managed= */ true, GetTestConfig().override_policy_connector_is_managed = true;
/* use_account_for_testing= */ true, /* use_device= */ false); SetUpProfileAndHandler();
EXPECT_EQ(GetExtensionReportingTitle(), EXPECT_EQ(GetExtensionReportingTitle(),
l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY, l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY,
...@@ -334,12 +603,12 @@ TEST_F(ManagementUIHandlerTests, ...@@ -334,12 +603,12 @@ TEST_F(ManagementUIHandlerTests,
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateManagedAccountKnownDomain) { ManagementContextualSourceUpdateManagedAccountKnownDomain) {
const std::string domain = "manager.com"; const std::string domain = "manager.com";
PrepareProfileAndHandler(
/* profile_name= */ "managed@" + domain,
/* override_policy_connector_is_managed= */ true,
/* use_account= */ true, /* use_device= */ false,
/* device_name= */ "");
const auto device_type = ui::GetChromeOSDeviceTypeResourceId(); const auto device_type = ui::GetChromeOSDeviceTypeResourceId();
ResetTestConfig();
GetTestConfig().profile_name = "managed@" + domain;
GetTestConfig().override_policy_connector_is_managed = true;
GetTestConfig().device_domain = "";
SetUpProfileAndHandler();
EXPECT_EQ(GetExtensionReportingTitle(), EXPECT_EQ(GetExtensionReportingTitle(),
l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY, l10n_util::GetStringFUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED_BY,
...@@ -356,12 +625,10 @@ TEST_F(ManagementUIHandlerTests, ...@@ -356,12 +625,10 @@ TEST_F(ManagementUIHandlerTests,
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateManagedAccountUnknownDomain) { ManagementContextualSourceUpdateManagedAccountUnknownDomain) {
PrepareProfileAndHandler(
/* profile_name= */ "",
/* override_policy_connector_is_managed= */ false,
/* use_account= */ true, /* use_device= */ false,
/* device_name= */ "");
const auto device_type = ui::GetChromeOSDeviceTypeResourceId(); const auto device_type = ui::GetChromeOSDeviceTypeResourceId();
ResetTestConfig();
GetTestConfig().device_domain = "";
SetUpProfileAndHandler();
EXPECT_EQ(GetExtensionReportingTitle(), EXPECT_EQ(GetExtensionReportingTitle(),
l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED)); l10n_util::GetStringUTF16(IDS_MANAGEMENT_EXTENSIONS_INSTALLED));
...@@ -374,11 +641,11 @@ TEST_F(ManagementUIHandlerTests, ...@@ -374,11 +641,11 @@ TEST_F(ManagementUIHandlerTests,
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateManagedDevice) { ManagementContextualSourceUpdateManagedDevice) {
PrepareProfileAndHandler(
/* profile_name= */ "managed@manager.com",
/* override_policy_connector_is_managed= */ false,
/* use_account= */ false, /* use_device= */ true);
const auto device_type = ui::GetChromeOSDeviceTypeResourceId(); const auto device_type = ui::GetChromeOSDeviceTypeResourceId();
ResetTestConfig();
GetTestConfig().managed_account = false;
GetTestConfig().managed_device = true;
SetUpProfileAndHandler();
EXPECT_EQ(GetPageSubtitle(), EXPECT_EQ(GetPageSubtitle(),
l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY, l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY,
...@@ -393,11 +660,11 @@ TEST_F(ManagementUIHandlerTests, ...@@ -393,11 +660,11 @@ TEST_F(ManagementUIHandlerTests,
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateManagedDeviceAndAccount) { ManagementContextualSourceUpdateManagedDeviceAndAccount) {
PrepareProfileAndHandler(
/* profile_name= */ "managed@devicedomain.com",
/* override_policy_connector_is_managed= */ false,
/* use_account= */ true, /* use_device= */ true);
const auto device_type = ui::GetChromeOSDeviceTypeResourceId(); const auto device_type = ui::GetChromeOSDeviceTypeResourceId();
ResetTestConfig();
GetTestConfig().profile_name = "managed@devicedomain.com";
GetTestConfig().managed_device = true;
SetUpProfileAndHandler();
EXPECT_EQ(GetPageSubtitle(), EXPECT_EQ(GetPageSubtitle(),
l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY, l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY,
...@@ -415,11 +682,12 @@ TEST_F(ManagementUIHandlerTests, ...@@ -415,11 +682,12 @@ TEST_F(ManagementUIHandlerTests,
TEST_F(ManagementUIHandlerTests, TEST_F(ManagementUIHandlerTests,
ManagementContextualSourceUpdateManagedDeviceAndAccountMultipleDomains) { ManagementContextualSourceUpdateManagedDeviceAndAccountMultipleDomains) {
const std::string domain = "manager.com"; const std::string domain = "manager.com";
PrepareProfileAndHandler(
/* profile_name= */ "managed@" + domain,
/* override_policy_connector_is_managed= */ true,
/* use_account= */ true, /* use_device= */ true);
const auto device_type = ui::GetChromeOSDeviceTypeResourceId(); const auto device_type = ui::GetChromeOSDeviceTypeResourceId();
ResetTestConfig();
GetTestConfig().profile_name = "managed@" + domain;
GetTestConfig().override_policy_connector_is_managed = true;
GetTestConfig().managed_device = true;
SetUpProfileAndHandler();
EXPECT_EQ(GetPageSubtitle(), EXPECT_EQ(GetPageSubtitle(),
l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY, l10n_util::GetStringFUTF16(IDS_MANAGEMENT_SUBTITLE_MANAGED_BY,
...@@ -436,12 +704,12 @@ TEST_F(ManagementUIHandlerTests, ...@@ -436,12 +704,12 @@ TEST_F(ManagementUIHandlerTests,
} }
TEST_F(ManagementUIHandlerTests, ManagementContextualSourceUpdateUnmanaged) { TEST_F(ManagementUIHandlerTests, ManagementContextualSourceUpdateUnmanaged) {
PrepareProfileAndHandler(
/* profile_name= */ "",
/* override_policy_connector_is_managed= */ false,
/* use_account= */ false, /* use_device= */ false,
/* device_domain= */ "");
const auto device_type = ui::GetChromeOSDeviceTypeResourceId(); const auto device_type = ui::GetChromeOSDeviceTypeResourceId();
ResetTestConfig();
GetTestConfig().profile_name = "";
GetTestConfig().managed_account = false;
GetTestConfig().device_domain = "";
SetUpProfileAndHandler();
EXPECT_EQ(GetPageSubtitle(), EXPECT_EQ(GetPageSubtitle(),
l10n_util::GetStringFUTF16(IDS_MANAGEMENT_NOT_MANAGED_SUBTITLE, l10n_util::GetStringFUTF16(IDS_MANAGEMENT_NOT_MANAGED_SUBTITLE,
...@@ -452,6 +720,54 @@ TEST_F(ManagementUIHandlerTests, ManagementContextualSourceUpdateUnmanaged) { ...@@ -452,6 +720,54 @@ TEST_F(ManagementUIHandlerTests, ManagementContextualSourceUpdateUnmanaged) {
l10n_util::GetStringUTF16(IDS_MANAGEMENT_DEVICE_NOT_MANAGED)); l10n_util::GetStringUTF16(IDS_MANAGEMENT_DEVICE_NOT_MANAGED));
EXPECT_FALSE(GetManaged()); EXPECT_FALSE(GetManaged());
} }
TEST_F(ManagementUIHandlerTests, NoDeviceReportingInfo) {
ResetTestConfig();
GetTestConfig().override_policy_connector_is_managed = true;
GetTestConfig().managed_account = false;
SetUpProfileAndHandler();
base::Value info =
handler_.GetDeviceReportingInfo(nullptr, nullptr, nullptr, GetProfile());
EXPECT_EQ(info.GetList().size(), 0u);
}
TEST_F(ManagementUIHandlerTests, AllEnabledDeviceReportingInfo) {
ResetTestConfig(true);
GetTestConfig().report_users = false;
const base::Value info = SetUpForReportingInfo();
const std::map<std::string, std::string> expected_elements = {
{kManagementReportActivityTimes, "device activity"},
{kManagementReportHardwareStatus, "device statistics"},
{kManagementReportNetworkInterfaces, "device"},
{kManagementLogUploadEnabled, "logs"},
{kManagementPrinting, "print"},
{kManagementCrostini, "crostini"}};
ASSERT_PRED_FORMAT2(ReportingElementsToBeEQ, info.GetList(),
expected_elements);
}
TEST_F(ManagementUIHandlerTests, OnlyReportUsersDeviceReportingInfo) {
ResetTestConfig(false);
GetTestConfig().report_users = true;
base::Value info = SetUpForReportingInfo();
const std::map<std::string, std::string> expected_elements = {
{kManagementReportUsers, "supervised user"}};
ASSERT_PRED_FORMAT2(ReportingElementsToBeEQ, info.GetList(),
expected_elements);
}
TEST_F(ManagementUIHandlerTests, AllDisabledDeviceReportingInfo) {
ResetTestConfig(false);
const base::Value info = SetUpForReportingInfo();
const std::map<std::string, std::string> expected_elements = {};
ASSERT_PRED_FORMAT2(ReportingElementsToBeEQ, info.GetList(),
expected_elements);
}
#endif #endif
TEST_F(ManagementUIHandlerTests, ExtensionReportingInfoNoPolicySetNoMessage) { TEST_F(ManagementUIHandlerTests, ExtensionReportingInfoNoPolicySetNoMessage) {
...@@ -470,8 +786,9 @@ TEST_F(ManagementUIHandlerTests, ...@@ -470,8 +786,9 @@ TEST_F(ManagementUIHandlerTests,
kManagementExtensionReportExtensionsPlugin, kManagementExtensionReportExtensionsPlugin,
kManagementExtensionReportSafeBrowsingWarnings}; kManagementExtensionReportSafeBrowsingWarnings};
ExpectMessagesToBeEQ(handler_.GetExtensionReportingInfo().GetList(), ASSERT_PRED_FORMAT2(MessagesToBeEQ,
expected_messages); handler_.GetExtensionReportingInfo().GetList(),
expected_messages);
} }
TEST_F(ManagementUIHandlerTests, CloudReportingPolicy) { TEST_F(ManagementUIHandlerTests, CloudReportingPolicy) {
...@@ -484,13 +801,14 @@ TEST_F(ManagementUIHandlerTests, CloudReportingPolicy) { ...@@ -484,13 +801,14 @@ TEST_F(ManagementUIHandlerTests, CloudReportingPolicy) {
.WillRepeatedly(ReturnRef(chrome_policies)); .WillRepeatedly(ReturnRef(chrome_policies));
SetPolicyValue(policy::key::kCloudReportingEnabled, chrome_policies, true); SetPolicyValue(policy::key::kCloudReportingEnabled, chrome_policies, true);
std::set<std::string> expected_messages = { const std::set<std::string> expected_messages = {
kManagementExtensionReportMachineName, kManagementExtensionReportUsername, kManagementExtensionReportMachineName, kManagementExtensionReportUsername,
kManagementExtensionReportVersion, kManagementExtensionReportVersion,
kManagementExtensionReportExtensionsPlugin}; kManagementExtensionReportExtensionsPlugin};
ExpectMessagesToBeEQ(handler_.GetExtensionReportingInfo().GetList(), ASSERT_PRED_FORMAT2(MessagesToBeEQ,
expected_messages); handler_.GetExtensionReportingInfo().GetList(),
expected_messages);
} }
TEST_F(ManagementUIHandlerTests, ExtensionReportingInfoPoliciesMerge) { TEST_F(ManagementUIHandlerTests, ExtensionReportingInfoPoliciesMerge) {
...@@ -548,8 +866,9 @@ TEST_F(ManagementUIHandlerTests, ExtensionReportingInfoPoliciesMerge) { ...@@ -548,8 +866,9 @@ TEST_F(ManagementUIHandlerTests, ExtensionReportingInfoPoliciesMerge) {
kManagementExtensionReportUserBrowsingData, kManagementExtensionReportUserBrowsingData,
kManagementExtensionReportPerfCrash}; kManagementExtensionReportPerfCrash};
ExpectMessagesToBeEQ(handler_.GetExtensionReportingInfo().GetList(), ASSERT_PRED_FORMAT2(MessagesToBeEQ,
expected_messages); handler_.GetExtensionReportingInfo().GetList(),
expected_messages);
} }
TEST_F(ManagementUIHandlerTests, ThreatReportingInfo) { TEST_F(ManagementUIHandlerTests, ThreatReportingInfo) {
......
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