File: feature_provider_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (245 lines) | stat: -rw-r--r-- 10,417 bytes parent folder | download | duplicates (6)
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
// 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 "extensions/common/features/feature_provider.h"

#include <algorithm>
#include <set>
#include <string>
#include <string_view>
#include <utility>

#include "base/test/bind.h"
#include "build/android_buildflags.h"
#include "build/build_config.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extensions_client.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/features/simple_feature.h"
#include "extensions/common/manifest.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/test/test_context_data.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

// Tests that a real manifest feature is available for the correct types of
// extensions and apps.
TEST(FeatureProviderTest, ManifestFeatureTypes) {
  // NOTE: This feature cannot have multiple rules, otherwise it is not a
  // SimpleFeature.
  const SimpleFeature* feature = static_cast<const SimpleFeature*>(
      FeatureProvider::GetManifestFeature("description"));
  ASSERT_TRUE(feature);
  const std::vector<Manifest::Type>& extension_types =
      feature->extension_types();
  EXPECT_EQ(8u, extension_types.size());
  EXPECT_EQ(1, std::ranges::count(extension_types, Manifest::TYPE_EXTENSION));
  EXPECT_EQ(1, std::ranges::count(extension_types,
                                  Manifest::TYPE_LEGACY_PACKAGED_APP));
  EXPECT_EQ(1,
            std::ranges::count(extension_types, Manifest::TYPE_PLATFORM_APP));
  EXPECT_EQ(1, std::ranges::count(extension_types, Manifest::TYPE_HOSTED_APP));
  EXPECT_EQ(1, std::ranges::count(extension_types, Manifest::TYPE_THEME));
  EXPECT_EQ(1,
            std::ranges::count(extension_types, Manifest::TYPE_SHARED_MODULE));
  EXPECT_EQ(1, std::ranges::count(extension_types,
                                  Manifest::TYPE_LOGIN_SCREEN_EXTENSION));
  EXPECT_EQ(1, std::ranges::count(extension_types,
                                  Manifest::TYPE_CHROMEOS_SYSTEM_EXTENSION));
}

// Tests that real manifest features have the correct availability for an
// extension.
TEST(FeatureProviderTest, ManifestFeatureAvailability) {
  const FeatureProvider* provider = FeatureProvider::GetByName("manifest");

  scoped_refptr<const Extension> extension =
      ExtensionBuilder("test extension").Build();

  const Feature* feature = provider->GetFeature("description");
  EXPECT_EQ(Feature::IS_AVAILABLE,
            feature
                ->IsAvailableToContext(extension.get(),
                                       mojom::ContextType::kUnspecified, GURL(),
                                       kUnspecifiedContextId, TestContextData())
                .result());

  // This is a generic extension, so an app-only feature isn't allowed.
  feature = provider->GetFeature("app.background");
  ASSERT_TRUE(feature);
  EXPECT_EQ(Feature::INVALID_TYPE,
            feature
                ->IsAvailableToContext(extension.get(),
                                       mojom::ContextType::kUnspecified, GURL(),
                                       kUnspecifiedContextId, TestContextData())
                .result());

  // A feature not listed in the manifest isn't allowed.
  feature = provider->GetFeature("background");
  ASSERT_TRUE(feature);
  EXPECT_EQ(Feature::NOT_PRESENT,
            feature
                ->IsAvailableToContext(extension.get(),
                                       mojom::ContextType::kUnspecified, GURL(),
                                       kUnspecifiedContextId, TestContextData())
                .result());
}

// Tests that a real permission feature is available for the correct types of
// extensions and apps.
TEST(FeatureProviderTest, PermissionFeatureTypes) {
  // NOTE: This feature cannot have multiple rules, otherwise it is not a
  // SimpleFeature.
  const SimpleFeature* feature = static_cast<const SimpleFeature*>(
      FeatureProvider::GetPermissionFeature("alarms"));
  ASSERT_TRUE(feature);
  const std::vector<Manifest::Type>& extension_types =
      feature->extension_types();
  EXPECT_EQ(3u, extension_types.size());
  EXPECT_EQ(1, std::ranges::count(extension_types, Manifest::TYPE_EXTENSION));
  EXPECT_EQ(1, std::ranges::count(extension_types,
                                  Manifest::TYPE_LEGACY_PACKAGED_APP));
  EXPECT_EQ(1,
            std::ranges::count(extension_types, Manifest::TYPE_PLATFORM_APP));
}

// Tests that real permission features have the correct availability for an app.
TEST(FeatureProviderTest, PermissionFeatureAvailability) {
  const FeatureProvider* provider = FeatureProvider::GetByName("permission");

  scoped_refptr<const Extension> app =
      ExtensionBuilder("test app", ExtensionBuilder::Type::PLATFORM_APP)
          .AddAPIPermission("power")
          .Build();
  ASSERT_TRUE(app->is_platform_app());

  // A permission requested in the manifest is available.
  const Feature* feature = provider->GetFeature("power");
  EXPECT_EQ(Feature::IS_AVAILABLE,
            feature
                ->IsAvailableToContext(app.get(),
                                       mojom::ContextType::kUnspecified, GURL(),
                                       kUnspecifiedContextId, TestContextData())
                .result());

  // A permission only available to allowlisted extensions returns availability
  // NOT_FOUND_IN_ALLOWLIST.
  // TODO(crbug.com/40198321): Port //device/bluetooth to Fuchsia to
  // enable bluetooth extensions.
  // bluetoothPrivate is unsupported in desktop-android build.
#if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_DESKTOP_ANDROID)
  feature = provider->GetFeature("bluetoothPrivate");
  ASSERT_TRUE(feature);
  EXPECT_EQ(Feature::NOT_FOUND_IN_ALLOWLIST,
            feature
                ->IsAvailableToContext(app.get(),
                                       mojom::ContextType::kUnspecified, GURL(),
                                       kUnspecifiedContextId, TestContextData())
                .result());
#endif  // !BUILDFLAG(IS_FUCHSIA)

  // A permission that isn't part of the manifest returns NOT_PRESENT.
  feature = provider->GetFeature("serial");
  ASSERT_TRUE(feature);
  EXPECT_EQ(Feature::NOT_PRESENT,
            feature
                ->IsAvailableToContext(app.get(),
                                       mojom::ContextType::kUnspecified, GURL(),
                                       kUnspecifiedContextId, TestContextData())
                .result());
}

TEST(FeatureProviderTest, GetChildren) {
  FeatureProvider provider;

  auto add_feature = [&provider](std::string_view name,
                                 bool no_parent = false) {
    auto feature = std::make_unique<SimpleFeature>();
    feature->set_name(name);
    feature->set_noparent(no_parent);
    provider.AddFeature(name, std::move(feature));
  };

  add_feature("parent");
  add_feature("parent.child");
  add_feature("parent.child.grandchild");
  add_feature("parent.other_child.other_grandchild");
  add_feature("parent.unparented_child", true);

  const Feature* parent = provider.GetFeature("parent");
  ASSERT_TRUE(parent);
  std::vector<const Feature*> children = provider.GetChildren(*parent);
  std::set<std::string> children_names;
  for (const Feature* child : children)
    children_names.insert(child->name());
  EXPECT_THAT(children_names, testing::UnorderedElementsAre(
                                  "parent.child", "parent.child.grandchild",
                                  "parent.other_child.other_grandchild"));
}

TEST(FeatureProviderTest, InstallFeatureDelegatedAvailabilityCheck) {
  Feature::FeatureDelegatedAvailabilityCheckMap map;
  static constexpr const char* kDelegatedFeatureName = "delegatedFeature";
  static constexpr const char* kNondelgatedFeatureName = "nondelegatedFeature";
  static constexpr const char* kMissingRequiresDelegatedCheckFeatureName =
      "missingRequiresDelegatedCheckFeature";

  auto delegated_availability_check =
      [&](const std::string& api_full_name, const Extension* extension,
          mojom::ContextType context, const GURL& url,
          Feature::Platform platform, int context_id, bool check_developer_mode,
          const ContextData& context_data) { return false; };
  map.emplace(kDelegatedFeatureName,
              base::BindLambdaForTesting(delegated_availability_check));
  map.emplace(kMissingRequiresDelegatedCheckFeatureName,
              base::BindLambdaForTesting(delegated_availability_check));
  ExtensionsClient::Get()->SetFeatureDelegatedAvailabilityCheckMap(
      std::move(map));

  FeatureProvider provider;

  // Verify that the delegated check handler is installed for a feature that
  // requires it and has a handler in the map.
  {
    auto feature = std::make_unique<SimpleFeature>();
    feature->set_name(kDelegatedFeatureName);
    feature->set_requires_delegated_availability_check(true);
    provider.AddFeature(kDelegatedFeatureName, std::move(feature));

    const auto* delegated_feature = provider.GetFeature(kDelegatedFeatureName);
    EXPECT_TRUE(
        delegated_feature->HasDelegatedAvailabilityCheckHandlerForTesting());
  }

  // Verify that a delegated check handler is not installed for a feature that
  // doesn't require it.
  {
    auto feature = std::make_unique<SimpleFeature>();
    feature->set_name(kNondelgatedFeatureName);
    provider.AddFeature(kNondelgatedFeatureName, std::move(feature));

    const auto* nondelegated_feature =
        provider.GetFeature(kNondelgatedFeatureName);
    EXPECT_FALSE(
        nondelegated_feature->HasDelegatedAvailabilityCheckHandlerForTesting());
  }

  // Verify that a delegated check handler is not installed for a feature that
  // doesn't require it but has a handler in the map.
  {
    auto feature = std::make_unique<SimpleFeature>();
    feature->set_name(kMissingRequiresDelegatedCheckFeatureName);
    provider.AddFeature(kMissingRequiresDelegatedCheckFeatureName,
                        std::move(feature));

    const auto* missing_requires_delegated_check_feature =
        provider.GetFeature(kMissingRequiresDelegatedCheckFeatureName);
    EXPECT_FALSE(missing_requires_delegated_check_feature
                     ->HasDelegatedAvailabilityCheckHandlerForTesting());
  }
}

}  // namespace extensions