chromium/chrome/browser/diagnostics/diagnostics_controller_unittest.cc

// Copyright 2013 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/diagnostics/diagnostics_controller.h"

#include <memory>

#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/diagnostics/diagnostics_model.h"
#include "chrome/browser/diagnostics/diagnostics_writer.h"
#include "chrome/browser/diagnostics/sqlite_diagnostics.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_constants.h"
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

namespace diagnostics {

// Basic harness to acquire and release the required temporary environment to
// run a test in.
class DiagnosticsControllerTest : public testing::Test {};

TEST_F(DiagnosticsControllerTest, Diagnostics) {}

TEST_F(DiagnosticsControllerTest, RecoverAllOK) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(DiagnosticsControllerTest, RecoverFromNssCertDbFailure) {
  base::FilePath db_path = profile_dir_.Append(ash::kNssCertDbPath);
  EXPECT_TRUE(base::PathExists(db_path));
  CorruptDataFile(db_path);
  DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get());
  ASSERT_TRUE(DiagnosticsController::GetInstance()->HasResults());
  const DiagnosticsModel& results =
      DiagnosticsController::GetInstance()->GetResults();
  EXPECT_EQ(results.GetTestRunCount(), results.GetTestAvailableCount());
  EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, results.GetTestRunCount());

  const DiagnosticsModel::TestInfo* info = nullptr;
  EXPECT_TRUE(
      results.GetTestInfo(DIAGNOSTICS_SQLITE_INTEGRITY_NSS_CERT_TEST, &info));
  EXPECT_EQ(DiagnosticsModel::TEST_FAIL_CONTINUE, info->GetResult());
  EXPECT_EQ(DIAG_SQLITE_CANNOT_OPEN_DB, info->GetOutcomeCode());

  DiagnosticsController::GetInstance()->RunRecovery(cmdline_, writer_.get());
  EXPECT_EQ(DiagnosticsModel::RECOVERY_OK, info->GetResult());
  EXPECT_FALSE(base::PathExists(db_path));
}

TEST_F(DiagnosticsControllerTest, RecoverFromNssKeyDbFailure) {
  base::FilePath db_path = profile_dir_.Append(ash::kNssKeyDbPath);
  EXPECT_TRUE(base::PathExists(db_path));
  CorruptDataFile(db_path);
  DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get());
  ASSERT_TRUE(DiagnosticsController::GetInstance()->HasResults());
  const DiagnosticsModel& results =
      DiagnosticsController::GetInstance()->GetResults();
  EXPECT_EQ(results.GetTestRunCount(), results.GetTestAvailableCount());
  EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, results.GetTestRunCount());

  const DiagnosticsModel::TestInfo* info = nullptr;
  EXPECT_TRUE(
      results.GetTestInfo(DIAGNOSTICS_SQLITE_INTEGRITY_NSS_KEY_TEST, &info));
  EXPECT_EQ(DiagnosticsModel::TEST_FAIL_CONTINUE, info->GetResult());
  EXPECT_EQ(DIAG_SQLITE_CANNOT_OPEN_DB, info->GetOutcomeCode());

  DiagnosticsController::GetInstance()->RunRecovery(cmdline_, writer_.get());
  EXPECT_EQ(DiagnosticsModel::RECOVERY_OK, info->GetResult());
  EXPECT_FALSE(base::PathExists(db_path));
}
#endif

}  // namespace diagnostics