blob: 009dfe63475565160910a0a9ae5973128b65a53c [file] [log] [blame]
// Copyright 2020 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/ash/crosapi/browser_util.h"
#include <memory>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "base/command_line.h"
#include "base/containers/fixed_flat_map.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_command_line.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "chrome/browser/ash/crosapi/crosapi_util.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/account_id/account_id.h"
#include "components/policy/policy_constants.h"
#include "components/user_manager/known_user.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/version_info/channel.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
using user_manager::User;
namespace crosapi {
class BrowserUtilTest : public testing::Test {
public:
BrowserUtilTest() = default;
~BrowserUtilTest() override = default;
void SetUp() override {
fake_user_manager_.Reset(std::make_unique<ash::FakeChromeUserManager>());
}
void TearDown() override {
fake_user_manager_.Reset();
}
const user_manager::User* AddRegularUser(const std::string& email,
bool login = true) {
AccountId account_id = AccountId::FromUserEmail(email);
const User* user = fake_user_manager_->AddUser(account_id);
user_manager::KnownUser(fake_user_manager_->GetLocalState())
.SaveKnownUser(account_id);
if (login) {
fake_user_manager_->UserLoggedIn(account_id, user->username_hash(),
/*browser_restart=*/false,
/*is_child=*/false);
}
return user;
}
TestingPrefServiceSimple* local_state() { return local_state_.Get(); }
content::BrowserTaskEnvironment task_environment_;
// Set up local_state of BrowserProcess before initializing
// FakeChromeUserManager, since its ctor injects local_state to the instance.
// This is to inject local_state to functions in browser_util, because
// some of them depend on local_state returned from UserManager.
ScopedTestingLocalState local_state_{TestingBrowserProcess::GetGlobal()};
user_manager::TypedScopedUserManager<ash::FakeChromeUserManager>
fake_user_manager_;
};
TEST_F(BrowserUtilTest, BlockedForChildUser) {
AccountId account_id = AccountId::FromUserEmail("user@test.com");
const User* user = fake_user_manager_->AddChildUser(account_id);
fake_user_manager_->UserLoggedIn(account_id, user->username_hash(),
/*browser_restart=*/false,
/*is_child=*/true);
EXPECT_FALSE(browser_util::IsLacrosEnabled());
}
TEST_F(BrowserUtilTest, IsAshWebBrowserDisabled) {
AddRegularUser("user@managedchrome.com");
EXPECT_TRUE(browser_util::IsAshWebBrowserEnabled());
}
TEST_F(BrowserUtilTest, GetRootfsLacrosVersionMayBlock) {
base::ScopedTempDir tmp_dir;
ASSERT_TRUE(tmp_dir.CreateUniqueTempDir());
const std::string kVersion = "91.0.4457";
const std::string kContent =
"{\"content\":{\"version\":\"" + kVersion + "\"}}";
auto path = tmp_dir.GetPath().Append("file");
ASSERT_TRUE(base::WriteFile(path, kContent));
EXPECT_EQ(browser_util::GetRootfsLacrosVersionMayBlock(path),
base::Version(kVersion));
}
TEST_F(BrowserUtilTest, GetRootfsLacrosVersionMayBlockMissingVersion) {
base::ScopedTempDir tmp_dir;
ASSERT_TRUE(tmp_dir.CreateUniqueTempDir());
const std::string kContent = "{\"content\":{}}";
auto path = tmp_dir.GetPath().Append("file");
ASSERT_TRUE(base::WriteFile(path, kContent));
EXPECT_FALSE(browser_util::GetRootfsLacrosVersionMayBlock(path).IsValid());
}
TEST_F(BrowserUtilTest, GetRootfsLacrosVersionMayBlockMissingContent) {
base::ScopedTempDir tmp_dir;
ASSERT_TRUE(tmp_dir.CreateUniqueTempDir());
const std::string kContent = "{}";
auto path = tmp_dir.GetPath().Append("file");
ASSERT_TRUE(base::WriteFile(path, kContent));
EXPECT_FALSE(browser_util::GetRootfsLacrosVersionMayBlock(path).IsValid());
}
TEST_F(BrowserUtilTest, GetRootfsLacrosVersionMayBlockMissingFile) {
base::ScopedTempDir tmp_dir;
ASSERT_TRUE(tmp_dir.CreateUniqueTempDir());
auto bad_path = tmp_dir.GetPath().Append("file");
EXPECT_FALSE(
browser_util::GetRootfsLacrosVersionMayBlock(bad_path).IsValid());
}
TEST_F(BrowserUtilTest, GetRootfsLacrosVersionMayBlockBadJson) {
base::ScopedTempDir tmp_dir;
ASSERT_TRUE(tmp_dir.CreateUniqueTempDir());
const std::string kContent = "!@#$";
auto path = tmp_dir.GetPath().Append("file");
ASSERT_TRUE(base::WriteFile(path, kContent));
EXPECT_FALSE(browser_util::GetRootfsLacrosVersionMayBlock(path).IsValid());
}
} // namespace crosapi