#include "components/enterprise/browser/reporting/report_generator.h"
#include <set>
#include <string>
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/profiles/profile_attributes_init_params.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/account_id/account_id.h"
#include "components/enterprise/browser/reporting/report_request.h"
#include "components/enterprise/browser/reporting/report_type.h"
#include "components/policy/core/common/cloud/cloud_policy_util.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/components/arc/arc_prefs.h"
#include "ash/components/arc/test/fake_app_instance.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/app_list/arc/arc_app_test.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/enterprise/reporting/reporting_delegate_factory_android.h"
#else
#include "chrome/browser/enterprise/reporting/reporting_delegate_factory_desktop.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension_builder.h"
#endif
em;
namespace enterprise_reporting {
namespace {
constexpr char kProfile[] = …;
#if BUILDFLAG(IS_CHROMEOS_ASH)
const char kArcAppName1[] = "app_name1";
const char kArcPackageName1[] = "package_name1";
const char kArcActivityName1[] = "activity_name1";
const char kArcAppName2[] = "app_name2";
const char kArcPackageName2[] = "package_name2";
const char kArcActivityName2[] = "activity_name2";
#endif
#if !BUILDFLAG(IS_CHROMEOS_ASH)
void VerifySerialNumber(const std::string& serial_number) { … }
#endif
enum ProfileStatus { … };
void FindAndRemoveProfileName(std::set<std::string>* names,
const std::string& name) { … }
void AddExtensionToProfile(TestingProfile* profile) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
arc::mojom::AppInfoPtr CreateArcApp(const std::string& app_name,
const std::string& package_name,
const std::string& activity_name) {
auto app = arc::mojom::AppInfo::New();
app->name = app_name;
app->package_name = package_name;
app->activity = activity_name;
app->suspended = false;
app->sticky = true;
app->notifications_enabled = true;
return app;
}
arc::mojom::ArcPackageInfoPtr CreateArcPackage(
const std::string& package_name) {
return arc::mojom::ArcPackageInfo::New(
package_name, 0 , 0 ,
0 , false );
}
void AddArcPackageAndApp(ArcAppTest* arc_app_test,
const std::string& app_name,
const std::string& package_name,
const std::string& activity_name) {
arc::mojom::ArcPackageInfoPtr package = CreateArcPackage(package_name);
arc_app_test->app_instance()->SendPackageAdded(std::move(package));
arc::mojom::AppInfoPtr app =
CreateArcApp(app_name, package_name, activity_name);
arc_app_test->app_instance()->SendAppAdded(*app);
}
#endif
}
class ReportGeneratorTest : public ::testing::Test { … };
#if BUILDFLAG(IS_ANDROID)
TEST_F(ReportGeneratorTest, GenerateBasicReport) {
auto requests = GenerateRequests(ReportType::kFull);
EXPECT_EQ(1u, requests.size());
auto* basic_request = requests[0].get();
EXPECT_NE(std::string(),
basic_request->GetDeviceReportRequest().brand_name());
EXPECT_NE(std::string(),
basic_request->GetDeviceReportRequest().device_model());
VerifySerialNumber(basic_request->GetDeviceReportRequest().serial_number());
EXPECT_EQ(policy::GetBrowserDeviceIdentifier()->SerializePartialAsString(),
basic_request->GetDeviceReportRequest()
.browser_device_identifier()
.SerializePartialAsString());
EXPECT_TRUE(basic_request->GetDeviceReportRequest().has_os_report());
auto& os_report = basic_request->GetDeviceReportRequest().os_report();
EXPECT_NE(std::string(), os_report.name());
EXPECT_NE(std::string(), os_report.arch());
EXPECT_NE(std::string(), os_report.version());
EXPECT_TRUE(basic_request->GetDeviceReportRequest().has_browser_report());
auto& browser_report =
basic_request->GetDeviceReportRequest().browser_report();
EXPECT_NE(std::string(), browser_report.browser_version());
EXPECT_TRUE(browser_report.has_channel());
EXPECT_NE(std::string(), browser_report.executable_path());
}
#else
TEST_F(ReportGeneratorTest, GenerateBasicReport) { … }
TEST_F(ReportGeneratorTest, GenerateWithoutProfiles) { … }
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(ReportGeneratorTest, ReportArcAppInChromeOS) {
ArcAppTest arc_app_test;
TestingProfile primary_profile;
arc_app_test.SetUp(&primary_profile);
AddArcPackageAndApp(&arc_app_test, kArcAppName1, kArcPackageName1,
kArcActivityName1);
AddArcPackageAndApp(&arc_app_test, kArcAppName2, kArcPackageName2,
kArcActivityName2);
EXPECT_EQ(2u, arc_app_test.arc_app_list_prefs()->GetAppIds().size());
auto requests = GenerateRequests(ReportType::kFull);
EXPECT_EQ(1u, requests.size());
ReportRequest* request = requests.front().get();
EXPECT_EQ(2, request->GetDeviceReportRequest().android_app_infos_size());
em::AndroidAppInfo app_info1 =
request->GetDeviceReportRequest().android_app_infos(1);
EXPECT_EQ(kArcAppName1, app_info1.app_name());
em::AndroidAppInfo app_info2 =
request->GetDeviceReportRequest().android_app_infos(0);
EXPECT_EQ(kArcAppName2, app_info2.app_name());
requests = GenerateRequests(ReportType::kFull);
EXPECT_EQ(1u, requests.size());
request = requests.front().get();
EXPECT_EQ(2, request->GetDeviceReportRequest().android_app_infos_size());
app_info1 = request->GetDeviceReportRequest().android_app_infos(1);
EXPECT_EQ(kArcAppName1, app_info1.app_name());
app_info2 = request->GetDeviceReportRequest().android_app_infos(0);
EXPECT_EQ(kArcAppName2, app_info2.app_name());
arc_app_test.TearDown();
}
TEST_F(ReportGeneratorTest, ArcPlayStoreDisabled) {
ArcAppTest arc_app_test;
TestingProfile primary_profile;
arc_app_test.SetUp(&primary_profile);
AddArcPackageAndApp(&arc_app_test, kArcAppName1, kArcPackageName1,
kArcActivityName1);
AddArcPackageAndApp(&arc_app_test, kArcAppName2, kArcPackageName2,
kArcActivityName2);
EXPECT_EQ(2u, arc_app_test.arc_app_list_prefs()->GetAppIds().size());
primary_profile.GetPrefs()->SetBoolean(arc::prefs::kArcEnabled, false);
auto requests = GenerateRequests(ReportType::kFull);
EXPECT_EQ(1u, requests.size());
ReportRequest* request = requests.front().get();
EXPECT_EQ(0, request->GetDeviceReportRequest().android_app_infos_size());
arc_app_test.TearDown();
}
#endif
}