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
base / supports_user_data_unittest.cc [blame]
// 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 "base/supports_user_data.h"
#include "base/features.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
struct TestSupportsUserData : public SupportsUserData {
// Make ClearAllUserData public so tests can access it.
using SupportsUserData::ClearAllUserData;
};
struct UsesItself : public SupportsUserData::Data {
UsesItself(SupportsUserData* supports_user_data, const void* key)
: supports_user_data_(supports_user_data),
key_(key) {
}
~UsesItself() override {
EXPECT_EQ(nullptr, supports_user_data_->GetUserData(key_));
}
raw_ptr<SupportsUserData> supports_user_data_;
raw_ptr<const void> key_;
};
using SupportsUserDataTest = ::testing::Test;
TEST_F(SupportsUserDataTest, ClearWorksRecursively) {
char key = 0; // Must outlive `supports_user_data`.
TestSupportsUserData supports_user_data;
supports_user_data.SetUserData(
&key, std::make_unique<UsesItself>(&supports_user_data, &key));
// Destruction of supports_user_data runs the actual test.
}
struct TestData : public SupportsUserData::Data {};
TEST_F(SupportsUserDataTest, Movable) {
TestSupportsUserData supports_user_data_1;
char key1 = 0;
supports_user_data_1.SetUserData(&key1, std::make_unique<TestData>());
void* data_1_ptr = supports_user_data_1.GetUserData(&key1);
TestSupportsUserData supports_user_data_2;
char key2 = 0;
supports_user_data_2.SetUserData(&key2, std::make_unique<TestData>());
supports_user_data_2 = std::move(supports_user_data_1);
EXPECT_EQ(data_1_ptr, supports_user_data_2.GetUserData(&key1));
EXPECT_EQ(nullptr, supports_user_data_2.GetUserData(&key2));
}
TEST_F(SupportsUserDataTest, ClearAllUserData) {
TestSupportsUserData supports_user_data;
char key1 = 0;
supports_user_data.SetUserData(&key1, std::make_unique<TestData>());
char key2 = 0;
supports_user_data.SetUserData(&key2, std::make_unique<TestData>());
EXPECT_TRUE(supports_user_data.GetUserData(&key1));
EXPECT_TRUE(supports_user_data.GetUserData(&key2));
supports_user_data.ClearAllUserData();
EXPECT_FALSE(supports_user_data.GetUserData(&key1));
EXPECT_FALSE(supports_user_data.GetUserData(&key2));
}
TEST_F(SupportsUserDataTest, TakeUserData) {
TestSupportsUserData supports_user_data;
char key1 = 0;
supports_user_data.SetUserData(&key1, std::make_unique<TestData>());
TestSupportsUserData::Data* data1_ptr = supports_user_data.GetUserData(&key1);
EXPECT_NE(data1_ptr, nullptr);
char wrong_key = 0;
EXPECT_FALSE(supports_user_data.TakeUserData(&wrong_key));
EXPECT_EQ(supports_user_data.GetUserData(&key1), data1_ptr);
std::unique_ptr<TestSupportsUserData::Data> data1 =
supports_user_data.TakeUserData(&key1);
EXPECT_EQ(data1.get(), data1_ptr);
EXPECT_FALSE(supports_user_data.GetUserData(&key1));
EXPECT_FALSE(supports_user_data.TakeUserData(&key1));
}
class DataOwnsSupportsUserData : public SupportsUserData::Data {
public:
TestSupportsUserData* supports_user_data() { return &supports_user_data_; }
private:
TestSupportsUserData supports_user_data_;
};
// Tests that removing a `SupportsUserData::Data` that owns a `SupportsUserData`
// does not crash.
TEST_F(SupportsUserDataTest, ReentrantRemoveUserData) {
DataOwnsSupportsUserData* data = new DataOwnsSupportsUserData;
char key = 0;
data->supports_user_data()->SetUserData(&key, WrapUnique(data));
data->supports_user_data()->RemoveUserData(&key);
}
TEST_F(SupportsUserDataTest, ReentrantSetUserDataDuringRemoval) {
static const char kKey = 0;
class ProblematicSet : public SupportsUserData::Data {
public:
explicit ProblematicSet(const void* const key,
TestSupportsUserData& supports_user_data)
: key_(key), supports_user_data_(supports_user_data) {}
~ProblematicSet() override {
supports_user_data_->SetUserData(
key_, std::make_unique<ProblematicSet>(key_, *supports_user_data_));
}
private:
const raw_ptr<const void> key_;
raw_ref<TestSupportsUserData> supports_user_data_;
};
{
std::optional<TestSupportsUserData> supports_user_data;
supports_user_data.emplace();
// This awkward construction is required since death tests are typically
// implemented using `fork()`, so calling `SetUserData()` outside the
// `EXPECT_CHECK_DEATH()` macro will also crash the process that's trying to
// observe the crash.
EXPECT_CHECK_DEATH([&] {
supports_user_data->SetUserData(
&kKey, std::make_unique<ProblematicSet>(&kKey, *supports_user_data));
// Triggers the reentrant attempt to call `SetUserData()` during
// destruction.
supports_user_data.reset();
}());
}
}
} // namespace
} // namespace base