chromium/chrome/browser/media/webrtc/webrtc_event_log_manager_common_unittest.cc

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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 =;
}  // namespace

// Tests for GzipLogCompressor.
// Note that these tests may not use GzippedSize(), or they would be assuming
// what they set out to prove. (Subsequent tests may use it, though.)
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 once with a big input, to provide coverage over inputs that could
// exceed the size of some local buffers in the UUT.
TEST_F(GzipLogCompressorTest, CompressionBigInput) {}

TEST_F(GzipLogCompressorTest, BudgetExceededByFirstCompressYieldsEmptyFile) {}

TEST_F(GzipLogCompressorTest,
       BudgetExceededByNonFirstCompressYieldsPartialFile) {}

TEST_F(GzipLogCompressorTest,
       ExceedingBudgetDueToOverlyOptimisticEstimationYieldsError) {}

// Tests relevant to all LogFileWriter subclasses.
class LogFileWriterTest
    : public ::testing::Test,
      public ::testing::WithParamInterface<WebRtcEventLogCompression> {};

TEST_P(LogFileWriterTest, FactoryCreatesLogFileWriter) {}

#if BUILDFLAG(IS_POSIX)
TEST_P(LogFileWriterTest, FactoryReturnsEmptyUniquePtrIfCantCreateFile) {}
#endif  // BUILDFLAG(IS_POSIX)

TEST_P(LogFileWriterTest, CloseSucceedsWhenNoErrorsOccurred) {}

// Other tests check check the case of compression where the estimation is
// close to the file's capacity, reaches or exceeds it.
TEST_P(LogFileWriterTest, CallToWriteSuccedsWhenCapacityFarOff) {}

TEST_P(LogFileWriterTest, CallToWriteWithEmptyStringSucceeds) {}

TEST_P(LogFileWriterTest, UnlimitedBudgetSanity) {}

TEST_P(LogFileWriterTest, DeleteRemovesUnclosedFile) {}

TEST_P(LogFileWriterTest, DeleteRemovesClosedFile) {}

#if !BUILDFLAG(IS_WIN)  // Deleting the open file does not work on Windows.
TEST_P(LogFileWriterTest, WriteDoesNotCrashIfFileRemovedExternally) {}

TEST_P(LogFileWriterTest, CloseDoesNotCrashIfFileRemovedExternally) {}

TEST_P(LogFileWriterTest, DeleteDoesNotCrashIfFileRemovedExternally) {}
#endif  // !BUILDFLAG(IS_WIN)

TEST_P(LogFileWriterTest, PathReturnsTheCorrectPath) {}

INSTANTIATE_TEST_SUITE_P();

// Tests for UncompressedLogFileWriterTest only.
class UncompressedLogFileWriterTest : public LogFileWriterTest {};

TEST_F(UncompressedLogFileWriterTest,
       MaxSizeReachedReturnsFalseWhenMaxNotReached) {}

TEST_F(UncompressedLogFileWriterTest, MaxSizeReachedReturnsTrueWhenMaxReached) {}

TEST_F(UncompressedLogFileWriterTest, CallToWriteSuccedsWhenCapacityReached) {}

TEST_F(UncompressedLogFileWriterTest, CallToWriteFailsWhenCapacityExceeded) {}

TEST_F(UncompressedLogFileWriterTest, WriteCompleteMessagesOnly) {}

// Tests for GzippedLogFileWriterTest only.
class GzippedLogFileWriterTest : public LogFileWriterTest {};

TEST_F(GzippedLogFileWriterTest, FactoryDeletesFileIfMaxSizeBelowMin) {}

TEST_F(GzippedLogFileWriterTest, MaxSizeReachedReturnsFalseWhenMaxNotReached) {}

TEST_F(GzippedLogFileWriterTest, MaxSizeReachedReturnsTrueWhenMaxReached) {}

TEST_F(GzippedLogFileWriterTest, CallToWriteSuccedsWhenCapacityReached) {}

// Also tests the scenario WriteCompleteMessagesOnly.
TEST_F(GzippedLogFileWriterTest,
       CallToWriteFailsWhenCapacityWouldBeExceededButEstimationPreventedWrite) {}

// This tests the case when the estimation fails to warn us of a pending
// over-budget write, which leaves us unable to produce a valid compression
// footer for the truncated file. This forces us to discard the file.
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>();
  // We use a standard Gaia account by default:
  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  // BUILDFLAG(IS_CHROMEOS_ASH)

}  // namespace webrtc_event_logging