#include "chrome/browser/policy/messaging_layer/util/reporting_server_connector.h"
#include <cstddef>
#include <memory>
#include <string>
#include <vector>
#include "base/functional/bind.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "chrome/browser/enterprise/browser_management/management_service_factory.h"
#include "chrome/browser/policy/messaging_layer/util/reporting_server_connector_test_util.h"
#include "chrome/browser/policy/messaging_layer/util/upload_response_parser.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/encrypted_reporting_job_configuration.h"
#include "components/policy/core/common/management/scoped_management_service_override_for_testing.h"
#include "components/reporting/proto/synced/record.pb.h"
#include "components/reporting/resources/resource_manager.h"
#include "components/reporting/util/status.h"
#include "components/reporting/util/statusor.h"
#include "components/reporting/util/test_support_callbacks.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_task_environment.h"
#include "net/http/http_request_headers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "base/test/scoped_feature_list.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chromeos/ash/components/install_attributes/stub_install_attributes.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chromeos/startup/browser_init_params.h"
#endif
_;
ContainerEq;
DoAll;
Eq;
HasSubstr;
Invoke;
SizeIs;
StartsWith;
WithArg;
namespace reporting {
namespace {
constexpr int kGenerationId = …;
constexpr char kGenerationGuid[] = …;
constexpr char kEncryptedRecord[] = …;
size_t RecordsSize(const std::vector<EncryptedRecord>& records) { … }
}
class ReportingServerConnectorTest : public ::testing::Test { … };
TEST_F(ReportingServerConnectorTest,
ExecuteUploadEncryptedReportingOnUIThread) { … }
TEST_F(ReportingServerConnectorTest,
ExecuteUploadEncryptedReportingOnArbitraryThread) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
TEST_F(ReportingServerConnectorTest, UploadFromUnmanagedDevice) {
#if BUILDFLAG(IS_CHROMEOS_ASH)
install_attributes_.Get()->SetConsumerOwned();
#elif BUILDFLAG(IS_CHROMEOS_LACROS)
auto params = crosapi::mojom::BrowserInitParams::New();
params->is_device_enterprised_managed = false;
chromeos::BrowserInitParams::SetInitParamsForTests(std::move(params));
#endif
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeatures(
{kEnableReportingFromUnmanagedDevices},
{});
ComposePayload(1);
const auto expected_cached_seq_ids = GetExpectedCachedSeqIds();
test::TestEvent<StatusOr<std::list<int64_t>>> enqueued_event;
test::TestEvent<StatusOr<UploadResponseParser>> response_event;
base::ThreadPool::PostTask(
FROM_HERE,
base::BindOnce(
&ReportingServerConnector::UploadEncryptedReport,
false,
0,
payload_records_,
ScopedReservation(RecordsSize(payload_records_), memory_resource_),
enqueued_event.cb(), response_event.cb()));
const auto& enqueued_result = enqueued_event.result();
EXPECT_TRUE(enqueued_result.has_value());
EXPECT_THAT(enqueued_result.value(), ContainerEq(expected_cached_seq_ids));
task_environment_.RunUntilIdle();
ASSERT_THAT(*test_env_->url_loader_factory()->pending_requests(), SizeIs(1));
const net::HttpRequestHeaders& headers =
test_env_->url_loader_factory()->GetPendingRequest(0)->request.headers;
EXPECT_FALSE(headers.HasHeader(policy::dm_protocol::kAuthHeader));
test_env_->SimulateResponseForRequest(0);
EXPECT_TRUE(response_event.result().has_value());
}
#endif
}