File: blocked_iph_features_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (316 lines) | stat: -rw-r--r-- 12,061 bytes parent folder | download | duplicates (3)
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
// 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 "components/feature_engagement/internal/blocked_iph_features.h"

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "components/feature_engagement/test/scoped_iph_feature_list.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace feature_engagement {

namespace {

BASE_FEATURE(kBlockedIphFeaturesTestFeature1,
             "BlockedIphFeaturesTestFeature1",
             base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kBlockedIphFeaturesTestFeature2,
             "BlockedIphFeaturesTestFeature2",
             base::FEATURE_ENABLED_BY_DEFAULT);
BASE_FEATURE(kBlockedIphFeaturesTestFeature3,
             "BlockedIphFeaturesTestFeature3",
             base::FEATURE_DISABLED_BY_DEFAULT);
}  // namespace

class BlockedIphFeaturesTest : public testing::Test {
 public:
  void SetUp() override { ClearData(); }

  void TearDown() override {
    {
      auto* const allowed = BlockedIphFeatures::GetInstance();
      base::AutoLock lock(allowed->GetLock());
      EXPECT_EQ(expect_refcount_on_teardown_,
                allowed->global_block_count_ != 0);
    }

    // Clear out the map data so it doesn't pollute later tests.
    ClearData();
  }

  void ExpectRefCountOnTeardown() { expect_refcount_on_teardown_ = true; }

  static const char* GetCommandLineSwitch() {
    return BlockedIphFeatures::kPropagateIPHForTestingSwitch;
  }

 private:
  void ClearData() {
    auto* const allowed = BlockedIphFeatures::GetInstance();
    base::AutoLock lock(allowed->GetLock());
    allowed->read_from_command_line_ = false;
    allowed->global_block_count_ = 0U;
    allowed->allow_feature_counts_.clear();
  }

  bool expect_refcount_on_teardown_ = false;
};

TEST_F(BlockedIphFeaturesTest, IsFeatureBlockedDefaultValue) {
  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());

  // Allowed should be irrespective of enabled or disabled.
  EXPECT_FALSE(blocked->AreAnyFeaturesBlocked());
  EXPECT_FALSE(blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
  EXPECT_FALSE(blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
}

TEST_F(BlockedIphFeaturesTest, IsFeatureBlockedWithEmptyScope) {
  {
    test::ScopedIphFeatureList list;
    list.InitWithNoFeaturesAllowed();

    // Blocked should be irrespective of enabled or disabled.
    auto* const blocked = BlockedIphFeatures::GetInstance();
    base::AutoLock lock(blocked->GetLock());
    EXPECT_TRUE(blocked->AreAnyFeaturesBlocked());
    EXPECT_TRUE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
    EXPECT_TRUE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
  }

  {
    // Now no one is blocking IPH.
    auto* const blocked = BlockedIphFeatures::GetInstance();
    base::AutoLock lock(blocked->GetLock());
    EXPECT_FALSE(blocked->AreAnyFeaturesBlocked());
    EXPECT_FALSE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
    EXPECT_FALSE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
  }
}

TEST_F(BlockedIphFeaturesTest, IsFeatureBlockedInnerScopeHasException) {
  test::ScopedIphFeatureList list1;
  list1.InitWithNoFeaturesAllowed();
  {
    test::ScopedIphFeatureList list2;
    list2.InitAndEnableFeatures({kBlockedIphFeaturesTestFeature1});

    auto* const blocked = BlockedIphFeatures::GetInstance();
    base::AutoLock lock(blocked->GetLock());

    // This feature is explicitly allowed.
    EXPECT_FALSE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
    EXPECT_TRUE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
  }

  {
    // Now no features are allowed.
    auto* const blocked = BlockedIphFeatures::GetInstance();
    base::AutoLock lock(blocked->GetLock());
    EXPECT_TRUE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
    EXPECT_TRUE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
  }
}

TEST_F(BlockedIphFeaturesTest, IsFeatureBlockedOuterScopeHasException) {
  test::ScopedIphFeatureList list1;
  list1.InitAndEnableFeatures({kBlockedIphFeaturesTestFeature1});
  {
    test::ScopedIphFeatureList list2;
    list2.InitWithNoFeaturesAllowed();

    auto* const blocked = BlockedIphFeatures::GetInstance();
    base::AutoLock lock(blocked->GetLock());

    // This feature is explicitly allowed.
    EXPECT_FALSE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
    EXPECT_TRUE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
  }

  {
    // Allowed feature should persist.
    auto* const blocked = BlockedIphFeatures::GetInstance();
    base::AutoLock lock(blocked->GetLock());
    EXPECT_FALSE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
    EXPECT_TRUE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
  }
}

TEST_F(BlockedIphFeaturesTest, IsFeatureBlockedMultipleExceptions) {
  test::ScopedIphFeatureList list1;
  list1.InitAndEnableFeatures({kBlockedIphFeaturesTestFeature1});
  {
    test::ScopedIphFeatureList list2;
    list2.InitAndEnableFeatures({kBlockedIphFeaturesTestFeature2});

    auto* const blocked = BlockedIphFeatures::GetInstance();
    base::AutoLock lock(blocked->GetLock());

    // Both features are allowed.
    EXPECT_FALSE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
    EXPECT_FALSE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
  }

  {
    // Outer allowed feature should persist.
    auto* const blocked = BlockedIphFeatures::GetInstance();
    base::AutoLock lock(blocked->GetLock());
    EXPECT_FALSE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
    EXPECT_TRUE(
        blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
  }
}
TEST_F(BlockedIphFeaturesTest, MaybeWriteToCommandLineWithNoBlocking) {
  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  blocked->MaybeWriteToCommandLine(command_line);
  EXPECT_FALSE(command_line.HasSwitch(GetCommandLineSwitch()));
  EXPECT_FALSE(command_line.HasSwitch(switches::kEnableFeatures));
}

TEST_F(BlockedIphFeaturesTest, MaybeWriteToCommandLineBlockingAllIph) {
  test::ScopedIphFeatureList list;
  list.InitWithNoFeaturesAllowed();

  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  blocked->MaybeWriteToCommandLine(command_line);
  EXPECT_TRUE(command_line.HasSwitch(GetCommandLineSwitch()));
  EXPECT_TRUE(command_line.GetSwitchValueASCII(GetCommandLineSwitch()).empty());
  EXPECT_FALSE(command_line.HasSwitch(switches::kEnableFeatures));
}

TEST_F(BlockedIphFeaturesTest, MaybeWriteToCommandLineBlockingAllButOneIph) {
  test::ScopedIphFeatureList list;
  list.InitAndEnableFeatures({kBlockedIphFeaturesTestFeature1});

  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  blocked->MaybeWriteToCommandLine(command_line);
  EXPECT_TRUE(command_line.HasSwitch(GetCommandLineSwitch()));
  EXPECT_EQ(kBlockedIphFeaturesTestFeature1.name,
            command_line.GetSwitchValueASCII(GetCommandLineSwitch()));
  EXPECT_TRUE(command_line.HasSwitch(switches::kEnableFeatures));
  EXPECT_EQ(kBlockedIphFeaturesTestFeature1.name,
            command_line.GetSwitchValueASCII(switches::kEnableFeatures));
}

TEST_F(BlockedIphFeaturesTest, MaybeWriteToCommandLineBlockingAllButTwoIph) {
  test::ScopedIphFeatureList list;
  list.InitAndEnableFeatures(
      {kBlockedIphFeaturesTestFeature1, kBlockedIphFeaturesTestFeature2});

  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
  blocked->MaybeWriteToCommandLine(command_line);
  const std::string expected =
      base::JoinString({kBlockedIphFeaturesTestFeature1.name,
                        kBlockedIphFeaturesTestFeature2.name},
                       ",");
  EXPECT_TRUE(command_line.HasSwitch(GetCommandLineSwitch()));
  EXPECT_EQ(expected, command_line.GetSwitchValueASCII(GetCommandLineSwitch()));
  EXPECT_TRUE(command_line.HasSwitch(switches::kEnableFeatures));
  EXPECT_EQ(expected,
            command_line.GetSwitchValueASCII(switches::kEnableFeatures));
}

TEST_F(BlockedIphFeaturesTest,
       MaybeWriteToCommandLinePreviouslyHadEnabledFeatures) {
  test::ScopedIphFeatureList list;
  list.InitAndEnableFeatures({kBlockedIphFeaturesTestFeature2});

  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  base::CommandLine command_line(base::CommandLine::NO_PROGRAM);

  // Add feature 1 to the general enabled list.
  command_line.AppendSwitchASCII(switches::kEnableFeatures,
                                 kBlockedIphFeaturesTestFeature1.name);
  blocked->MaybeWriteToCommandLine(command_line);

  // Only the explicitly allowed IPH should be present in the BlockedIphFeatures
  // command-line switch.
  EXPECT_TRUE(command_line.HasSwitch(GetCommandLineSwitch()));
  EXPECT_EQ(kBlockedIphFeaturesTestFeature2.name,
            command_line.GetSwitchValueASCII(GetCommandLineSwitch()));

  // Both values should be in the final enabled features argument.
  EXPECT_TRUE(command_line.HasSwitch(switches::kEnableFeatures));
  const std::string expected =
      base::JoinString({kBlockedIphFeaturesTestFeature1.name,
                        kBlockedIphFeaturesTestFeature2.name},
                       ",");
  EXPECT_EQ(expected,
            command_line.GetSwitchValueASCII(switches::kEnableFeatures));
}

TEST_F(BlockedIphFeaturesTest, ReadFromCommandLineBlockAll) {
  ExpectRefCountOnTeardown();

  // Append a switch that indicates blocked IPH with no exceptions.
  base::CommandLine::ForCurrentProcess()->AppendSwitch(GetCommandLineSwitch());

  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  EXPECT_TRUE(blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
  EXPECT_TRUE(blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
}

TEST_F(BlockedIphFeaturesTest, ReadFromCommandLineAllowOneIph) {
  ExpectRefCountOnTeardown();

  // Append a switch that indicates blocked IPH with one exceptions.
  base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
      GetCommandLineSwitch(), kBlockedIphFeaturesTestFeature1.name);

  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  EXPECT_FALSE(blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
  EXPECT_TRUE(blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));
}

TEST_F(BlockedIphFeaturesTest, ReadFromCommandLineAllowTwoIph) {
  ExpectRefCountOnTeardown();

  // Append a switch that indicates blocked IPH with two exceptions.
  base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
      GetCommandLineSwitch(),
      base::JoinString({kBlockedIphFeaturesTestFeature1.name,
                        kBlockedIphFeaturesTestFeature2.name},
                       ","));

  auto* const blocked = BlockedIphFeatures::GetInstance();
  base::AutoLock lock(blocked->GetLock());
  EXPECT_FALSE(blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature1.name));
  EXPECT_FALSE(blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature2.name));

  // An unrelated IPH should still be blocked.
  EXPECT_TRUE(blocked->IsFeatureBlocked(kBlockedIphFeaturesTestFeature3.name));
}

}  // namespace feature_engagement