chromium/base/functional/bind_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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "base/functional/bind.h"

#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/allocator/partition_alloc_features.h"
#include "base/allocator/partition_alloc_support.h"
#include "base/functional/callback.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "partition_alloc/buildflags.h"
#include "partition_alloc/dangling_raw_ptr_checks.h"
#include "partition_alloc/partition_alloc_for_testing.h"
#include "partition_alloc/partition_root.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

_;
AnyNumber;
ByMove;
Mock;
Return;
StrictMock;

namespace base {
namespace  // namespace

// This simulates a race weak pointer that, unlike our `WeakPtr<>`,
// may become invalidated between `operator bool()` is tested and `Lock()`
// is called in the implementation of `Unwrap()`.
template <typename T>
struct MockRacyWeakPtr {};

IsWeakReceiver<MockRacyWeakPtr<T>>;

BindUnwrapTraits<MockRacyWeakPtr<T>>;

MaybeValidTraits<MockRacyWeakPtr<T>>;

namespace {

// Note this only covers a case of racy weak pointer invalidation. Other
// weak pointer scenarios (such as a valid pointer) are covered
// in BindTest.WeakPtrFor{Once,Repeating}.
TEST_F(BindTest, BindRacyWeakPtrTest) {}

// Test null callbacks cause a DCHECK.
TEST(BindDeathTest, NullCallback) {}

TEST(BindDeathTest, NullFunctionPointer) {}

TEST(BindDeathTest, NullCallbackWithoutBoundArgs) {}

TEST(BindDeathTest, BanFirstOwnerOfRefCountedType) {}

#if PA_BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT) && \
    PA_BUILDFLAG(USE_RAW_PTR_BACKUP_REF_IMPL)

void HandleOOM(size_t unused_size) {}

// Basic set of options to mostly only enable `BackupRefPtr::kEnabled`.
// This avoids the boilerplate of having too much options enabled for simple
// testing purpose.
static constexpr auto kOnlyEnableBackupRefPtrOptions =::PartitionOptions opts;
  opts.backup_ref_ptr =;

class BindUnretainedDanglingInternalFixture : public BindTest {};

class BindUnretainedDanglingTest
    : public BindUnretainedDanglingInternalFixture {};
class BindUnretainedDanglingDeathTest
    : public BindUnretainedDanglingInternalFixture {};

bool PtrCheckFn(int* p) {}

bool RefCheckFn(const int& p) {}

bool MayBeDanglingCheckFn(MayBeDangling<int> p) {}

bool MayBeDanglingAndDummyTraitCheckFn(
    MayBeDangling<int, RawPtrTraits::kDummyForTest> p) {}

class ClassWithWeakPtr {};

TEST_F(BindUnretainedDanglingTest, UnretainedNoDanglingPtr) {}

TEST_F(BindUnretainedDanglingTest, UnsafeDanglingPtr) {}

TEST_F(BindUnretainedDanglingTest, UnsafeDanglingPtrWithDummyTrait) {}

TEST_F(BindUnretainedDanglingTest,
       UnsafeDanglingPtrWithDummyAndDanglingTraits) {}

TEST_F(BindUnretainedDanglingTest, UnsafeDanglingPtrNoRawPtrReceiver) {}

TEST_F(BindUnretainedDanglingTest, UnsafeDanglingUntriagedPtr) {}

TEST_F(BindUnretainedDanglingTest, UnretainedWeakReceiverValidNoDangling) {}

TEST_F(BindUnretainedDanglingTest, UnretainedRefWeakReceiverValidNoDangling) {}

TEST_F(BindUnretainedDanglingTest, UnretainedWeakReceiverInvalidNoDangling) {}

TEST_F(BindUnretainedDanglingTest, UnretainedRefWeakReceiverInvalidNoDangling) {}

TEST_F(BindUnretainedDanglingTest, UnretainedRefUnsafeDangling) {}

TEST_F(BindUnretainedDanglingTest, UnretainedRefUnsafeDanglingUntriaged) {}

// Death tests misbehave on Android, http://crbug.com/643760.
#if defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)

int FuncWithRefArgument(int& i_ptr) {}

TEST_F(BindUnretainedDanglingDeathTest, UnretainedDanglingPtr) {}

TEST_F(BindUnretainedDanglingDeathTest, UnretainedRefDanglingPtr) {}

TEST_F(BindUnretainedDanglingDeathTest,
       UnretainedRefWithManualUnretainedDanglingPtr) {}

TEST_F(BindUnretainedDanglingDeathTest, UnretainedWeakReceiverDangling) {}

#endif  // defined(GTEST_HAS_DEATH_TEST) && !BUILDFLAG(IS_ANDROID)

#endif  // PA_BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT) &&
        // PA_BUILDFLAG(USE_RAW_PTR_BACKUP_REF_IMPL)

}  // namespace
}  // namespace base