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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/common/features/base_feature_provider.h"
#include <algorithm>
#include <set>
#include <string>
#include <utility>
#include "base/stl_util.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/features/simple_feature.h"
#include "extensions/common/manifest.h"
#include "extensions/common/value_builder.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(BaseFeatureProviderTest, 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(6u, extension_types->size());
EXPECT_EQ(1, STLCount(*(extension_types), Manifest::TYPE_EXTENSION));
EXPECT_EQ(1,
STLCount(*(extension_types), Manifest::TYPE_LEGACY_PACKAGED_APP));
EXPECT_EQ(1, STLCount(*(extension_types), Manifest::TYPE_PLATFORM_APP));
EXPECT_EQ(1, STLCount(*(extension_types), Manifest::TYPE_HOSTED_APP));
EXPECT_EQ(1, STLCount(*(extension_types), Manifest::TYPE_THEME));
EXPECT_EQ(1, STLCount(*(extension_types), Manifest::TYPE_SHARED_MODULE));
}
// Tests that real manifest features have the correct availability for an
// extension.
TEST(BaseFeatureProviderTest, ManifestFeatureAvailability) {
const FeatureProvider* provider = BaseFeatureProvider::GetByName("manifest");
scoped_refptr<const Extension> extension =
ExtensionBuilder()
.SetManifest(std::move(DictionaryBuilder()
.Set("name", "test extension")
.Set("version", "1")
.Set("description", "hello there")))
.Build();
ASSERT_TRUE(extension.get());
Feature* feature = provider->GetFeature("description");
EXPECT_EQ(Feature::IS_AVAILABLE,
feature->IsAvailableToContext(extension.get(),
Feature::UNSPECIFIED_CONTEXT,
GURL()).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(),
Feature::UNSPECIFIED_CONTEXT,
GURL()).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(),
Feature::UNSPECIFIED_CONTEXT,
GURL()).result());
}
// Tests that a real permission feature is available for the correct types of
// extensions and apps.
TEST(BaseFeatureProviderTest, PermissionFeatureTypes) {
// NOTE: This feature cannot have multiple rules, otherwise it is not a
// SimpleFeature.
const SimpleFeature* feature = static_cast<const SimpleFeature*>(
BaseFeatureProvider::GetPermissionFeature("power"));
ASSERT_TRUE(feature);
const std::vector<Manifest::Type>* extension_types =
feature->extension_types();
EXPECT_EQ(3u, extension_types->size());
EXPECT_EQ(1, STLCount(*(extension_types), Manifest::TYPE_EXTENSION));
EXPECT_EQ(1,
STLCount(*(extension_types), Manifest::TYPE_LEGACY_PACKAGED_APP));
EXPECT_EQ(1, STLCount(*(extension_types), Manifest::TYPE_PLATFORM_APP));
}
// Tests that real permission features have the correct availability for an app.
TEST(BaseFeatureProviderTest, PermissionFeatureAvailability) {
const FeatureProvider* provider =
BaseFeatureProvider::GetByName("permission");
scoped_refptr<const Extension> app =
ExtensionBuilder()
.SetManifest(std::move(
DictionaryBuilder()
.Set("name", "test app")
.Set("version", "1")
.Set("app", std::move(DictionaryBuilder().Set(
"background",
std::move(DictionaryBuilder().Set(
"scripts", std::move(ListBuilder().Append(
"background.js")))))))
.Set("permissions",
std::move(ListBuilder().Append("power")))))
.Build();
ASSERT_TRUE(app.get());
ASSERT_TRUE(app->is_platform_app());
// A permission requested in the manifest is available.
Feature* feature = provider->GetFeature("power");
EXPECT_EQ(
Feature::IS_AVAILABLE,
feature->IsAvailableToContext(
app.get(), Feature::UNSPECIFIED_CONTEXT, GURL()).result());
// A permission only available to whitelisted extensions returns availability
// NOT_FOUND_IN_WHITELIST.
feature = provider->GetFeature("bluetoothPrivate");
ASSERT_TRUE(feature);
EXPECT_EQ(
Feature::NOT_FOUND_IN_WHITELIST,
feature->IsAvailableToContext(
app.get(), Feature::UNSPECIFIED_CONTEXT, GURL()).result());
// 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(), Feature::UNSPECIFIED_CONTEXT, GURL()).result());
}
} // namespace extensions
|