chromium/chrome/browser/safe_browsing/incident_reporting/download_metadata_manager_unittest.cc

// Copyright 2014 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/safe_browsing/incident_reporting/download_metadata_manager.h"

#include <stdint.h>

#include <memory>
#include <string>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/test/base/testing_profile.h"
#include "components/download/public/common/mock_download_item.h"
#include "components/download/public/common/simple_download_manager_coordinator.h"
#include "components/safe_browsing/core/common/proto/csd.pb.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/browser/download_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/mock_download_manager.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

AllOf;
Eq;
IsNull;
Ne;
NiceMock;
NotNull;
ResultOf;
Return;
SaveArg;
StrEq;
_;

namespace safe_browsing {

namespace {

const uint32_t kTestDownloadId =;
const uint32_t kOtherDownloadId =;
const uint32_t kCrazyDowloadId =;
const uint32_t kUninitializedDowloadId =;
const int64_t kTestDownloadTimeMsec =;
const char kTestUrl[] =;
const uint64_t kTestDownloadLength =;
const double kTestDownloadEndTimeMs =;

// A utility class suitable for mocking that exposes a
// GetDownloadDetailsCallback.
class DownloadDetailsGetter {};

// A mock DownloadDetailsGetter.
class MockDownloadDetailsGetter : public DownloadDetailsGetter {};

// A mock DownloadMetadataManager that can be used to map a BrowserContext to
// a DownloadManager.
class MockDownloadMetadataManager : public DownloadMetadataManager {};

// A helper function that returns the download URL from a DownloadDetails.
const std::string& GetDetailsDownloadUrl(
    const ClientIncidentReport_DownloadDetails* details) {}

// A helper function that returns the open time from a DownloadDetails.
int64_t GetDetailsOpenTime(
    const ClientIncidentReport_DownloadDetails* details) {}

class MockDownloadManager : public content::MockDownloadManager {};

}  // namespace

// The basis upon which unit tests of the DownloadMetadataManager are built.
class DownloadMetadataManagerTestBase : public ::testing::Test {};

// A parameterized test that exercises GetDownloadDetails. The parameters
// dictate the exact state of affairs leading up to the call as follows:
// 0: if "present", the profile has a pre-existing DownloadMetadata file.
// 1: if "managed", the profile's DownloadManager has been created.
// 2: the state of the DownloadItem prior to the call:
//    "not_created": the DownloadItem has not been created.
//    "created": the DownloadItem has been created.
//    "opened": the DownloadItem has been opened.
//    "removed": the DownloadItem has been removed.
// 3: if "loaded", the task to load the DownloadMetadata file is allowed to
//    complete.
// 4: if "early_shutdown", the DownloadManager is shut down before the callback
//    is allowed to complete.
class GetDetailsTest
    : public DownloadMetadataManagerTestBase,
      public ::testing::WithParamInterface<testing::tuple<const char*,
                                                          const char*,
                                                          const char*,
                                                          const char*,
                                                          const char*>> {};

// Tests that DownloadMetadataManager::GetDownloadDetails works for all
// combinations of states.
TEST_P(GetDetailsTest, GetDownloadDetails) {}

INSTANTIATE_TEST_SUITE_P();

// A parameterized test that exercises SetRequest. The parameters dictate the
// exact state of affairs leading up to the call as follows:
// 0: the state of the DownloadMetadata file for the test profile:
//    "absent": no file is present.
//    "this": the file corresponds to the item being updated.
//    "other": the file correponds to a different item.
//    "unknown": the file corresponds to an item that has not been created.
// 1: if "pending", an operation is applied to the item being updated prior to
//    the call.
// 2: if "pending", an operation is applied to a different item prior to the
//    call.
// 3: if "loaded", the task to load the DownloadMetadata file is allowed to
//    complete.
// 4: if "set", the call to SetRequest contains a new request; otherwise it
//    does not, leading to removal of metadata.
class SetRequestTest
    : public DownloadMetadataManagerTestBase,
      public ::testing::WithParamInterface<testing::tuple<const char*,
                                                          const char*,
                                                          const char*,
                                                          const char*,
                                                          const char*>> {};

// Tests that DownloadMetadataManager::SetRequest works for all combinations of
// states.
TEST_P(SetRequestTest, SetRequest) {}

INSTANTIATE_TEST_SUITE_P();

TEST_F(DownloadMetadataManagerTestBase, ActiveDownloadNoRequest) {}

TEST_F(DownloadMetadataManagerTestBase, ActiveDownloadWithRequest) {}

// Regression test for http://crbug.com/504092: open an item with id==0 when
// there is no metadata loaded.
TEST_F(DownloadMetadataManagerTestBase, OpenItemWithZeroId) {}

// Regression test for https://crbug.com/40072145, where observers weren't being
// removed at shutdown from uninitialized active downloads.
TEST_F(DownloadMetadataManagerTestBase, UninitializedActiveDownload) {}

}  // namespace safe_browsing