chromium/base/path_service_unittest.cc

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

#include "base/path_service.h"

#include "base/base_paths.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/scoped_environment_variable_override.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gtest_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

#if BUILDFLAG(IS_WIN)
#include <shlobj.h>

#include "base/win/windows_version.h"
#endif

#if BUILDFLAG(IS_APPLE)
#include "base/apple/bundle_locations.h"
#endif

namespace base {

namespace {

#if BUILDFLAG(IS_ANDROID)
// Defined in
// //base/test/android/javatests/src/org/chromium/base/test/util/UrlUtils.java.
constexpr char kExpectedChromiumTestsRoot[] =
    "/storage/emulated/0/chromium_tests_root";
#endif

// Returns true if PathService::Get returns true and sets the path parameter
// to non-empty for the given PathService key enumeration value.
bool ReturnsValidPath(int key) {}

// Returns true if PathService::Get returns false and path parameter is empty
// for the given PathService key enumeration value. Used to test path keys that
// are not supported on the platform or on some versions of Windows.
bool ReturnsInvalidPath(int key) {}

}  // namespace

// On the Mac this winds up using some autoreleased objects, so we need to
// be a PlatformTest.
PathServiceTest;

// Test that all PathService::Get calls return a value and a true result
// in the development environment.  (This test was created because a few
// later changes to Get broke the semantics of the function and yielded the
// correct value while returning false.)
// If this test fails for specific value(s) on a specific platform, consider not
// defining the enum value on that platform rather than skipping or expecting
// failure for the value(s) on that platform in this test.
TEST_F(PathServiceTest, Get) {}

// Tests that CheckedGet returns the same path as Get.
TEST_F(PathServiceTest, CheckedGet) {}

#if defined(GTEST_HAS_DEATH_TEST)

// Tests that CheckedGet CHECKs on failure.
TEST_F(PathServiceTest, CheckedGetFailure) {}

#endif  // defined(GTEST_HAS_DEATH_TEST)

// Test that all versions of the Override function of PathService do what they
// are supposed to do.
TEST_F(PathServiceTest, Override) {}

// Check if multiple overrides can co-exist.
TEST_F(PathServiceTest, OverrideMultiple) {}

TEST_F(PathServiceTest, RemoveOverride) {}

#if BUILDFLAG(IS_WIN)
TEST_F(PathServiceTest, GetProgramFiles) {
  FilePath programfiles_dir;
#if defined(_WIN64)
  // 64-bit on 64-bit.
  EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
      &programfiles_dir));
  EXPECT_EQ(programfiles_dir.value(),
      FILE_PATH_LITERAL("C:\\Program Files"));
  EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
      &programfiles_dir));
  EXPECT_EQ(programfiles_dir.value(),
      FILE_PATH_LITERAL("C:\\Program Files (x86)"));
  EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
      &programfiles_dir));
  EXPECT_EQ(programfiles_dir.value(),
      FILE_PATH_LITERAL("C:\\Program Files"));
#else
  if (base::win::OSInfo::GetInstance()->IsWowX86OnAMD64() ||
      base::win::OSInfo::GetInstance()->IsWowX86OnARM64()) {
    // 32-bit on 64-bit.
    EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
        &programfiles_dir));
    EXPECT_EQ(programfiles_dir.value(),
        FILE_PATH_LITERAL("C:\\Program Files (x86)"));
    EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
        &programfiles_dir));
    EXPECT_EQ(programfiles_dir.value(),
        FILE_PATH_LITERAL("C:\\Program Files (x86)"));
    EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
        &programfiles_dir));
    EXPECT_EQ(programfiles_dir.value(),
        FILE_PATH_LITERAL("C:\\Program Files"));
  } else {
    // 32-bit on 32-bit.
    EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES,
        &programfiles_dir));
    EXPECT_EQ(programfiles_dir.value(),
        FILE_PATH_LITERAL("C:\\Program Files"));
    EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILESX86,
        &programfiles_dir));
    EXPECT_EQ(programfiles_dir.value(),
        FILE_PATH_LITERAL("C:\\Program Files"));
    EXPECT_TRUE(PathService::Get(DIR_PROGRAM_FILES6432,
        &programfiles_dir));
    EXPECT_EQ(programfiles_dir.value(),
        FILE_PATH_LITERAL("C:\\Program Files"));
  }
#endif  // defined(_WIN64)
}

TEST_F(PathServiceTest, GetSystemTemp) {
  FilePath secure_system_temp;

  EXPECT_EQ(PathService::Get(DIR_SYSTEM_TEMP, &secure_system_temp),
            ::IsUserAnAdmin());
  if (!secure_system_temp.empty()) {
    FilePath dir_windows;
    ASSERT_TRUE(PathService::Get(DIR_WINDOWS, &dir_windows));
    FilePath dir_program_files;
    ASSERT_TRUE(PathService::Get(DIR_PROGRAM_FILES, &dir_program_files));

    ASSERT_TRUE((dir_windows.AppendASCII("SystemTemp") == secure_system_temp) ||
                (dir_program_files == secure_system_temp));
  }
}
#endif  // BUILDFLAG(IS_WIN)

// Tests that DIR_ASSETS is
// - the package root on Fuchsia,
// - overridden in tests by test_support_android.cc,
// - equals to base::apple::FrameworkBundlePath() on iOS,
// - a sub-directory of base::apple::FrameworkBundlePath() on iOS catalyst,
// - equals to DIR_MODULE otherwise.
TEST_F(PathServiceTest, DIR_ASSETS) {}

// DIR_OUT_TEST_DATA_ROOT is DIR_MODULE except on Fuchsia where it is the
// package root, on ios where it is the resources directory and on Android
// where it is overridden in tests by test_support_android.cc.
TEST_F(PathServiceTest, DIR_OUT_TEST_DATA_ROOT) {}

// Test that DIR_GEN_TEST_DATA_ROOT contains dummy_generated.txt which is
// generated for this test.
TEST_F(PathServiceTest, DIR_GEN_TEST_DATA_ROOT) {}

#if ((BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && \
      !BUILDFLAG(IS_ANDROID)) ||                     \
     BUILDFLAG(IS_WIN))

// Test that CR_SOURCE_ROOT is being used when set.
// By default on those platforms, this directory is set to two directories up
// the current executable directory ("../../").
TEST_F(PathServiceTest, SetTestDataRootAsAbsolutePath) {}

// Test that CR_SOURCE_ROOT is being used when set.
TEST_F(PathServiceTest, SetTestDataRootAsRelativePath) {}

#endif

#if BUILDFLAG(IS_FUCHSIA)
// On Fuchsia, some keys have fixed paths that are easy to test.

TEST_F(PathServiceTest, DIR_SRC_TEST_DATA_ROOT) {
  FilePath test_binary_path;
  EXPECT_EQ(PathService::CheckedGet(DIR_SRC_TEST_DATA_ROOT).value(), "/pkg");
}

#elif BUILDFLAG(IS_ANDROID)

// These keys are overridden in //base/test/test_support_android.cc.
TEST_F(PathServiceTest, AndroidTestOverrides) {
  EXPECT_EQ(PathService::CheckedGet(DIR_ANDROID_APP_DATA).value(),
            kExpectedChromiumTestsRoot);
  EXPECT_EQ(PathService::CheckedGet(DIR_ASSETS).value(),
            kExpectedChromiumTestsRoot);
  EXPECT_EQ(PathService::CheckedGet(DIR_SRC_TEST_DATA_ROOT).value(),
            kExpectedChromiumTestsRoot);
  EXPECT_EQ(PathService::CheckedGet(DIR_OUT_TEST_DATA_ROOT).value(),
            kExpectedChromiumTestsRoot);
}

#endif  // BUILDFLAG(IS_FUCHSIA)

}  // namespace base