#include "chrome/browser/media/webrtc/webrtc_event_log_manager_common.h"
#include <memory>
#include <numeric>
#include <optional>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/notreached.h"
#include "base/rand_util.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/media/webrtc/webrtc_event_log_manager_unittest_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/zlib/google/compression_utils.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/test/base/testing_profile.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_names.h"
#include "content/public/test/browser_task_environment.h"
#endif
namespace webrtc_event_logging {
namespace {
constexpr LogCompressor::Result OK = …;
constexpr LogCompressor::Result DISALLOWED = …;
constexpr LogCompressor::Result ERROR_ENCOUNTERED = …;
}
class GzipLogCompressorTest : public ::testing::Test { … };
TEST_F(GzipLogCompressorTest,
GzipLogCompressorFactoryCreatesCompressorIfMinimalSizeOrAbove) { … }
TEST_F(GzipLogCompressorTest,
GzipLogCompressorFactoryDoesNotCreateCompressorIfBelowMinimalSize) { … }
TEST_F(GzipLogCompressorTest, EmptyStreamReasonableMaxSize) { … }
TEST_F(GzipLogCompressorTest, EmptyStreamMinimalSize) { … }
TEST_F(GzipLogCompressorTest, SingleCallToCompress) { … }
TEST_F(GzipLogCompressorTest, MultipleCallsToCompress) { … }
TEST_F(GzipLogCompressorTest, UnlimitedBudgetSanity) { … }
TEST_F(GzipLogCompressorTest, CompressionBigInput) { … }
TEST_F(GzipLogCompressorTest, BudgetExceededByFirstCompressYieldsEmptyFile) { … }
TEST_F(GzipLogCompressorTest,
BudgetExceededByNonFirstCompressYieldsPartialFile) { … }
TEST_F(GzipLogCompressorTest,
ExceedingBudgetDueToOverlyOptimisticEstimationYieldsError) { … }
class LogFileWriterTest
: public ::testing::Test,
public ::testing::WithParamInterface<WebRtcEventLogCompression> { … };
TEST_P(LogFileWriterTest, FactoryCreatesLogFileWriter) { … }
#if BUILDFLAG(IS_POSIX)
TEST_P(LogFileWriterTest, FactoryReturnsEmptyUniquePtrIfCantCreateFile) { … }
#endif
TEST_P(LogFileWriterTest, CloseSucceedsWhenNoErrorsOccurred) { … }
TEST_P(LogFileWriterTest, CallToWriteSuccedsWhenCapacityFarOff) { … }
TEST_P(LogFileWriterTest, CallToWriteWithEmptyStringSucceeds) { … }
TEST_P(LogFileWriterTest, UnlimitedBudgetSanity) { … }
TEST_P(LogFileWriterTest, DeleteRemovesUnclosedFile) { … }
TEST_P(LogFileWriterTest, DeleteRemovesClosedFile) { … }
#if !BUILDFLAG(IS_WIN)
TEST_P(LogFileWriterTest, WriteDoesNotCrashIfFileRemovedExternally) { … }
TEST_P(LogFileWriterTest, CloseDoesNotCrashIfFileRemovedExternally) { … }
TEST_P(LogFileWriterTest, DeleteDoesNotCrashIfFileRemovedExternally) { … }
#endif
TEST_P(LogFileWriterTest, PathReturnsTheCorrectPath) { … }
INSTANTIATE_TEST_SUITE_P(…);
class UncompressedLogFileWriterTest : public LogFileWriterTest { … };
TEST_F(UncompressedLogFileWriterTest,
MaxSizeReachedReturnsFalseWhenMaxNotReached) { … }
TEST_F(UncompressedLogFileWriterTest, MaxSizeReachedReturnsTrueWhenMaxReached) { … }
TEST_F(UncompressedLogFileWriterTest, CallToWriteSuccedsWhenCapacityReached) { … }
TEST_F(UncompressedLogFileWriterTest, CallToWriteFailsWhenCapacityExceeded) { … }
TEST_F(UncompressedLogFileWriterTest, WriteCompleteMessagesOnly) { … }
class GzippedLogFileWriterTest : public LogFileWriterTest { … };
TEST_F(GzippedLogFileWriterTest, FactoryDeletesFileIfMaxSizeBelowMin) { … }
TEST_F(GzippedLogFileWriterTest, MaxSizeReachedReturnsFalseWhenMaxNotReached) { … }
TEST_F(GzippedLogFileWriterTest, MaxSizeReachedReturnsTrueWhenMaxReached) { … }
TEST_F(GzippedLogFileWriterTest, CallToWriteSuccedsWhenCapacityReached) { … }
TEST_F(GzippedLogFileWriterTest,
CallToWriteFailsWhenCapacityWouldBeExceededButEstimationPreventedWrite) { … }
TEST_F(GzippedLogFileWriterTest,
CallToWriteFailsWhenCapacityExceededDespiteEstimationAllowingIt) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
struct DoesProfileDefaultToLoggingEnabledForUserTypeTestCase {
user_manager::UserType user_type;
bool defaults_to_logging_enabled;
};
class DoesProfileDefaultToLoggingEnabledForUserTypeParametrizedTest
: public ::testing::TestWithParam<
DoesProfileDefaultToLoggingEnabledForUserTypeTestCase> {
protected:
content::BrowserTaskEnvironment task_environment_;
};
TEST_P(DoesProfileDefaultToLoggingEnabledForUserTypeParametrizedTest,
WebRtcPolicyDefaultTest) {
DoesProfileDefaultToLoggingEnabledForUserTypeTestCase test_case = GetParam();
TestingProfile::Builder profile_builder;
profile_builder.OverridePolicyConnectorIsManagedForTesting(true);
std::unique_ptr<TestingProfile> testing_profile = profile_builder.Build();
auto fake_user_manager_ = std::make_unique<ash::FakeChromeUserManager>();
AccountId account_id = AccountId::FromUserEmailGaiaId("name", "id");
switch (test_case.user_type) {
case user_manager::UserType::kRegular:
fake_user_manager_->AddUserWithAffiliationAndTypeAndProfile(
account_id, false, test_case.user_type, testing_profile.get());
break;
case user_manager::UserType::kGuest:
account_id = user_manager::GuestAccountId();
fake_user_manager_->AddGuestUser();
break;
case user_manager::UserType::kPublicAccount:
fake_user_manager_->AddPublicAccountUser(account_id);
break;
case user_manager::UserType::kKioskApp:
fake_user_manager_->AddKioskAppUser(account_id);
break;
case user_manager::UserType::kChild:
fake_user_manager_->AddChildUser(account_id);
break;
default:
FAIL() << "Invalid test setup. Unexpected user type.";
}
fake_user_manager_->LoginUser(account_id);
std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_ =
std::make_unique<user_manager::ScopedUserManager>(
std::move(fake_user_manager_));
EXPECT_EQ(DoesProfileDefaultToLoggingEnabled(testing_profile.get()),
test_case.defaults_to_logging_enabled);
}
INSTANTIATE_TEST_SUITE_P(
WebRtcPolicyDefaultTests,
DoesProfileDefaultToLoggingEnabledForUserTypeParametrizedTest,
testing::ValuesIn(
std::vector<DoesProfileDefaultToLoggingEnabledForUserTypeTestCase>{
{user_manager::UserType::kRegular, true},
{user_manager::UserType::kGuest, false},
{user_manager::UserType::kPublicAccount, false},
{user_manager::UserType::kKioskApp, false},
{user_manager::UserType::kChild, false},
}));
#endif
}