1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461

ash / ambient / managed / screensaver_images_policy_handler_unittest.cc [blame]

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

#include "ash/ambient/managed/screensaver_images_policy_handler.h"

#include <memory>
#include <optional>
#include <vector>

#include "ash/ambient/managed/screensaver_image_downloader.h"
#include "ash/ambient/test/ambient_ash_test_helper.h"
#include "ash/ambient/test/test_ambient_client.h"
#include "ash/constants/ash_paths.h"
#include "ash/public/cpp/ambient/ambient_prefs.h"
#include "ash/public/cpp/ash_prefs.h"
#include "ash/session/session_controller_impl.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_helper.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/hash/sha1.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_path_override.h"
#include "base/test/test_future.h"
#include "components/prefs/testing_pref_service.h"
#include "components/user_manager/user_type.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace {

constexpr char kUserEmail[] = "user@mail.com";
constexpr char kFakeFilePath1[] = "/path/to/file1";
constexpr char kFakeFilePath2[] = "/path/to/file2";

constexpr char kCacheDirectoryName[] = "managed_screensaver";
constexpr char kSigninDirectoryName[] = "signin";
constexpr char kManagedGuestDirectoryName[] = "guest";
constexpr char kCacheFileExt[] = ".cache";

constexpr char kImageUrl1[] = "https://example.com/1.jpg";
constexpr char kImageUrl1Alt[] = "https://EXAMPLE.com/1.jpg";
constexpr char kImageUrl2[] = "https://example.com/2.jpg";
constexpr char kFileContents1[] = "file contents 1";
constexpr char kFileContents2[] = "file contents 2";

constexpr size_t kMaxUrlsToProcessFromPolicy = 25u;

}  // namespace

struct ScreensaverImagesPolicyHandlerTestCase {
  std::string test_name;
  ScreensaverImagesPolicyHandler::HandlerType handle_type;
  std::string base_directory;
};

class ScreensaverImagesPolicyHandlerTest : public AshTestBase {
 public:
  ScreensaverImagesPolicyHandlerTest() = default;

  ScreensaverImagesPolicyHandlerTest(
      const ScreensaverImagesPolicyHandlerTest&) = delete;
  ScreensaverImagesPolicyHandlerTest& operator=(
      const ScreensaverImagesPolicyHandlerTest&) = delete;

  ~ScreensaverImagesPolicyHandlerTest() override = default;

  // AshTestBase:
  void SetUp() override {
    AshTestBase::SetUp();

    EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
    home_dir_override_ = std::make_unique<base::ScopedPathOverride>(
        base::DIR_HOME, temp_dir_.GetPath());
    managed_screensaver_dir_override_ =
        std::make_unique<base::ScopedPathOverride>(
            ash::DIR_DEVICE_POLICY_SCREENSAVER_DATA, temp_dir_.GetPath());
  }

  void TearDown() override {
    policy_handler_.reset();
    // Remove the custom overrides to test directory behavior before the tear
    // down so that original override can be restored.
    home_dir_override_.reset();
    managed_screensaver_dir_override_.reset();
    AshTestBase::TearDown();
  }

  void TriggerOnDownloadedImageListUpdated(
      const std::vector<base::FilePath>& image_list) {
    ASSERT_TRUE(policy_handler());
    policy_handler_->OnDownloadedImageListUpdated(image_list);
  }

  ScreensaverImageDownloader* GetPrivateImageDownloader(
      const ScreensaverImagesPolicyHandler& policy_handler) {
    return policy_handler.image_downloader_.get();
  }

  void VerifyPrivateImageDownloaderDownloadFolder(
      ScreensaverImagesPolicyHandler& policy_handler,
      const base::FilePath& expected_path) {
    ASSERT_TRUE(policy_handler.image_downloader_.get());
    EXPECT_EQ(expected_path,
              policy_handler.image_downloader_->GetDowloadDirForTesting());
  }

  void RegisterUserWithUserPrefs(const AccountId& account_id,
                                 user_manager::UserType user_type) {
    // Create a fake user prefs map.
    auto user_prefs = std::make_unique<TestingPrefServiceSimple>();
    RegisterUserProfilePrefs(user_prefs->registry(), /*country=*/"",
                             /*for_test=*/true);

    // Keep a raw pointer to the user prefs before transferring ownership.
    active_prefs_ = user_prefs.get();

    GetSessionControllerClient()->Reset();
    GetSessionControllerClient()->AddUserSession(
        kUserEmail, user_type,
        /*provide_pref_service=*/false);
    GetSessionControllerClient()->SetUserPrefService(account_id,
                                                     std::move(user_prefs));
    GetSessionControllerClient()->SwitchActiveUser(account_id);
    GetSessionControllerClient()->SetSessionState(
        session_manager::SessionState::ACTIVE);
  }

  TestingPrefServiceSimple* active_prefs() {
    CHECK(active_prefs_);
    return active_prefs_;
  }

  network::TestURLLoaderFactory& url_loader_factory() {
    return GetAmbientAshTestHelper()
        ->ambient_client()
        .test_url_loader_factory();
  }

  ScreensaverImagesPolicyHandler* policy_handler() {
    return policy_handler_.get();
  }

 protected:
  // Temp directory to simulate the download directory.
  base::ScopedTempDir temp_dir_;

  // Used to override the base download path to |temp_dir_|
  std::unique_ptr<base::ScopedPathOverride> home_dir_override_;
  std::unique_ptr<base::ScopedPathOverride> managed_screensaver_dir_override_;

  // Ownership of this pref service is transferred to the session controller
  raw_ptr<TestingPrefServiceSimple, DanglingUntriaged> active_prefs_ = nullptr;

  // Class under test
  std::unique_ptr<ScreensaverImagesPolicyHandler> policy_handler_;
};

TEST_F(ScreensaverImagesPolicyHandlerTest, FactoryFunctionTestSignin) {
  // Signin
  auto handler = ScreensaverImagesPolicyHandler::Create(
      Shell::Get()->session_controller()->GetSigninScreenPrefService());
  // Verify that the policy handler detected the new user and created a new
  // image downloader instance.
  EXPECT_TRUE(GetPrivateImageDownloader(*handler));
  VerifyPrivateImageDownloaderDownloadFolder(
      *handler, temp_dir_.GetPath().AppendASCII(kSigninDirectoryName));
}
TEST_F(ScreensaverImagesPolicyHandlerTest, FactoryFunctionTestUser) {
  // User
  RegisterUserWithUserPrefs(AccountId::FromUserEmail(kUserEmail),
                            user_manager::UserType::kRegular);
  auto handler = ScreensaverImagesPolicyHandler::Create(active_prefs());
  // Verify that the policy handler detected the new user and created a new
  // image downloader instance.
  EXPECT_TRUE(GetPrivateImageDownloader(*handler));
  VerifyPrivateImageDownloaderDownloadFolder(
      *handler, temp_dir_.GetPath().AppendASCII(kCacheDirectoryName));
}
TEST_F(ScreensaverImagesPolicyHandlerTest, FactoryFunctionTestManagedGuest) {
  // Managed Guest
  GetSessionControllerClient()->RequestSignOut();
  RegisterUserWithUserPrefs(AccountId::FromUserEmail(kUserEmail),
                            user_manager::UserType::kPublicAccount);
  auto handler = ScreensaverImagesPolicyHandler::Create(
      Shell::Get()->session_controller()->GetActivePrefService());
  // Verify that the policy handler detected the new user and created a new
  // image downloader instance.
  EXPECT_TRUE(GetPrivateImageDownloader(*handler));
  VerifyPrivateImageDownloaderDownloadFolder(
      *handler, temp_dir_.GetPath().AppendASCII(kManagedGuestDirectoryName));
}

class ScreensaverImagesPolicyHandlerForAnySessionTest
    : public ScreensaverImagesPolicyHandlerTest,
      public ::testing::WithParamInterface<
          ScreensaverImagesPolicyHandlerTestCase> {
 public:
  ScreensaverImagesPolicyHandlerForAnySessionTest() {
    TestSessionControllerClient::DisableAutomaticallyProvideSigninPref();
  }
  // ScreensaverImagesPolicyHandlerTest:
  void SetUp() override {
    ScreensaverImagesPolicyHandlerTest::SetUp();
    RegisterProfilePrefs();
    CreateScreensaverImagesPolicyHandler();
  }

  void RegisterProfilePrefs() {
    ScreensaverImagesPolicyHandlerTestCase test_case = GetParam();
    switch (test_case.handle_type) {
      case ScreensaverImagesPolicyHandler::HandlerType::kUser:
        RegisterUserWithUserPrefs(AccountId::FromUserEmail(kUserEmail),
                                  user_manager::UserType::kRegular);
        break;
      case ScreensaverImagesPolicyHandler::HandlerType::kSignin: {
        auto pref_service = std::make_unique<TestingPrefServiceSimple>();
        active_prefs_ = pref_service.get();
        RegisterSigninProfilePrefs(pref_service->registry(), /*country=*/"",
                                   /*for_test=*/true);
        TestSessionControllerClient* client = GetSessionControllerClient();
        client->SetSigninScreenPrefService(std::move(pref_service));
      } break;
      case ScreensaverImagesPolicyHandler::HandlerType::kManagedGuest:
        RegisterUserWithUserPrefs(AccountId::FromUserEmail(kUserEmail),
                                  user_manager::UserType::kPublicAccount);
        break;
    }
  }

  void CreateScreensaverImagesPolicyHandler() {
    ScreensaverImagesPolicyHandlerTestCase test_case = GetParam();
    switch (test_case.handle_type) {
      case ScreensaverImagesPolicyHandler::HandlerType::kUser:
        policy_handler_ = std::make_unique<ScreensaverImagesPolicyHandler>(
            active_prefs_, ScreensaverImagesPolicyHandler::HandlerType::kUser);
        break;
      case ScreensaverImagesPolicyHandler::HandlerType::kSignin: {
        policy_handler_ = std::make_unique<ScreensaverImagesPolicyHandler>(
            active_prefs_,
            ScreensaverImagesPolicyHandler::HandlerType::kSignin);

        VerifyPrivateImageDownloaderDownloadFolder(
            *policy_handler_,
            temp_dir_.GetPath().AppendASCII(kSigninDirectoryName));
      } break;
      case ScreensaverImagesPolicyHandler::HandlerType::kManagedGuest:
        policy_handler_ = std::make_unique<ScreensaverImagesPolicyHandler>(
            active_prefs_,
            ScreensaverImagesPolicyHandler::HandlerType::kManagedGuest);
        break;
    }
  }

  void ResetScreensaverImagesPolicyHandler() { policy_handler_.reset(); }

  base::FilePath GetExpectedFilePath(const std::string& url) {
    auto hash = base::SHA1Hash(base::as_byte_span(url));
    return temp_dir_.GetPath()
        .AppendASCII(GetParam().base_directory)
        .AppendASCII(base::HexEncode(hash) + kCacheFileExt);
  }
};

INSTANTIATE_TEST_SUITE_P(
    ScreensaverImagesPolicyHandlerForAnySessionTests,
    ScreensaverImagesPolicyHandlerForAnySessionTest,
    ::testing::ValuesIn<ScreensaverImagesPolicyHandlerTestCase>({
        {"Signin", ScreensaverImagesPolicyHandler::HandlerType::kSignin,
         kSigninDirectoryName},
        {"User", ScreensaverImagesPolicyHandler::HandlerType::kUser,
         kCacheDirectoryName},
        {"ManagedGuest",
         ScreensaverImagesPolicyHandler::HandlerType::kManagedGuest,
         kManagedGuestDirectoryName},
    }),
    [](const ::testing::TestParamInfo<
        ScreensaverImagesPolicyHandlerForAnySessionTest::ParamType>& info) {
      return info.param.test_name;
    });

TEST_P(ScreensaverImagesPolicyHandlerForAnySessionTest, DirectoryTest) {
  // Verify that the policy handler creates the new
  // image downloader instance.
  ASSERT_TRUE(GetPrivateImageDownloader(*policy_handler_));
  VerifyPrivateImageDownloaderDownloadFolder(
      *policy_handler_,
      temp_dir_.GetPath().AppendASCII(GetParam().base_directory));
}

TEST_P(ScreensaverImagesPolicyHandlerForAnySessionTest,
       ShouldRunCallbackIfImagesUpdated) {
  base::test::TestFuture<std::vector<base::FilePath>> test_future;
  policy_handler()->SetScreensaverImagesUpdatedCallback(
      test_future.GetRepeatingCallback<const std::vector<base::FilePath>&>());

  // Expect callbacks when images are downloaded.
  base::FilePath file_path1(kFakeFilePath1);
  {
    TriggerOnDownloadedImageListUpdated({file_path1});
    EXPECT_TRUE(test_future.Wait());
    std::vector<base::FilePath> file_paths = test_future.Take();
    ASSERT_EQ(1u, file_paths.size());
    EXPECT_EQ(file_path1, file_paths.front());
  }
  base::FilePath file_path2(kFakeFilePath2);
  {
    TriggerOnDownloadedImageListUpdated({file_path1, file_path2});
    EXPECT_TRUE(test_future.Wait());
    std::vector<base::FilePath> file_paths = test_future.Take();
    ASSERT_EQ(2u, file_paths.size());
    EXPECT_TRUE(base::Contains(file_paths, file_path1));
    EXPECT_TRUE(base::Contains(file_paths, file_path2));
  }

  EXPECT_FALSE(test_future.IsReady());
}

TEST_P(ScreensaverImagesPolicyHandlerForAnySessionTest, DownloadImagesTest) {
  base::test::TestFuture<std::vector<base::FilePath>> test_future;
  policy_handler()->SetScreensaverImagesUpdatedCallback(
      test_future.GetRepeatingCallback<const std::vector<base::FilePath>&>());

  ASSERT_NE(GetExpectedFilePath(kImageUrl1),
            GetExpectedFilePath(kImageUrl1Alt));

  base::Value::List image_urls;
  image_urls.Append(kImageUrl1);
  image_urls.Append(kImageUrl1Alt);
  image_urls.Append(kImageUrl2);

  // Fill the pref service to trigger the logic under test.
  active_prefs()->SetManagedPref(
      ambient::prefs::kAmbientModeManagedScreensaverImages, image_urls.Clone());

  // Verify that request 1 is resolved
  {
    url_loader_factory().AddResponse(image_urls[0].GetString(), kFileContents1);
    EXPECT_TRUE(test_future.Wait());
    std::vector<base::FilePath> file_paths = test_future.Take();
    ASSERT_EQ(1u, file_paths.size());
    EXPECT_EQ(GetExpectedFilePath(kImageUrl1), file_paths.front());
  }

  // Verify that request 2 resolves to the same file as request 1.
  {
    url_loader_factory().AddResponse(image_urls[1].GetString(), kFileContents1);
    EXPECT_TRUE(test_future.Wait());
    std::vector<base::FilePath> file_paths = test_future.Take();
    ASSERT_EQ(1u, file_paths.size());
    EXPECT_NE(GetExpectedFilePath(kImageUrl1Alt), file_paths.front());
  }

  // Verify that request 3 is resolved and both file paths are present.
  {
    url_loader_factory().AddResponse(image_urls[2].GetString(), kFileContents2);
    EXPECT_TRUE(test_future.Wait());
    std::vector<base::FilePath> file_paths = test_future.Take();
    EXPECT_EQ(2u, file_paths.size());
    EXPECT_TRUE(base::Contains(file_paths, GetExpectedFilePath(kImageUrl1)));
    EXPECT_TRUE(base::Contains(file_paths, GetExpectedFilePath(kImageUrl2)));
  }
}

TEST_P(ScreensaverImagesPolicyHandlerForAnySessionTest, VerifyPolicyLimit) {
  base::test::TestFuture<std::vector<base::FilePath>> test_future;
  policy_handler()->SetScreensaverImagesUpdatedCallback(
      test_future.GetRepeatingCallback<const std::vector<base::FilePath>&>());

  base::Value::List image_urls;
  // Append the same URL request `kMaxUrlsToProcessFromPolicy` times. This
  // should be the only URL that can be requested.
  for (size_t i = 0; i < kMaxUrlsToProcessFromPolicy; ++i) {
    image_urls.Append(kImageUrl1);
  }
  // Append a new URL that must be ignored.
  image_urls.Append(kImageUrl2);

  // Add both responses in the URL factory.
  url_loader_factory().AddResponse(image_urls[0].GetString(), kFileContents1);
  url_loader_factory().AddResponse(image_urls[1].GetString(), kFileContents2);

  // Fill the pref service to trigger the logic under test.
  active_prefs()->SetManagedPref(
      ambient::prefs::kAmbientModeManagedScreensaverImages, image_urls.Clone());

  const base::FilePath expected_file_path = GetExpectedFilePath(kImageUrl1);
  for (size_t i = 0; i < kMaxUrlsToProcessFromPolicy; ++i) {
    EXPECT_TRUE(test_future.Wait());
    std::vector<base::FilePath> file_paths = test_future.Take();
    ASSERT_TRUE(file_paths.size());
    ASSERT_GT(kMaxUrlsToProcessFromPolicy, file_paths.size());
    for (const base::FilePath& p : file_paths) {
      EXPECT_EQ(expected_file_path, p);
    }
  }
}

TEST_P(ScreensaverImagesPolicyHandlerForAnySessionTest,
       VerifyUnsetImageListPrefBehavior) {
  // Simulate a populated cache
  {
    base::CreateDirectory(GetExpectedFilePath(kImageUrl1).DirName());
    EXPECT_TRUE(
        base::WriteFile(GetExpectedFilePath(kImageUrl1), kFileContents1));
    EXPECT_TRUE(
        base::WriteFile(GetExpectedFilePath(kImageUrl2), kFileContents2));
  }
  // It is assumed that the image list pref is unset.

  // Case 1: The enabled policy is unset.
  // Expectation: The unset image list pref should not cause a cache cleanup.
  {
    CreateScreensaverImagesPolicyHandler();
    task_environment()->RunUntilIdle();
    EXPECT_TRUE(base::PathExists(GetExpectedFilePath(kImageUrl1)));
    EXPECT_TRUE(base::PathExists(GetExpectedFilePath(kImageUrl2)));
  }

  // Case 2: The enabled policy is set to true.
  // Expectation: The unset image list pref should not cause a cache cleanup.
  {
    ResetScreensaverImagesPolicyHandler();
    active_prefs()->SetManagedPref(
        ambient::prefs::kAmbientModeManagedScreensaverEnabled,
        base::Value(true));

    CreateScreensaverImagesPolicyHandler();
    task_environment()->RunUntilIdle();

    EXPECT_TRUE(base::PathExists(GetExpectedFilePath(kImageUrl1)));
    EXPECT_TRUE(base::PathExists(GetExpectedFilePath(kImageUrl2)));
  }

  // Case 3: The enabled policy is set to false.
  // Expectation: The unset image list pref should cause a cache cleanup.
  {
    ResetScreensaverImagesPolicyHandler();
    active_prefs()->SetManagedPref(
        ambient::prefs::kAmbientModeManagedScreensaverEnabled,
        base::Value(false));

    CreateScreensaverImagesPolicyHandler();
    task_environment()->RunUntilIdle();

    EXPECT_FALSE(base::PathExists(GetExpectedFilePath(kImageUrl1)));
    EXPECT_FALSE(base::PathExists(GetExpectedFilePath(kImageUrl2)));
  }
}
}  // namespace ash