• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "extensions/common/features/base_feature_provider.h"
6 
7 #include <set>
8 #include <string>
9 
10 #include "extensions/common/extension_builder.h"
11 #include "extensions/common/features/feature.h"
12 #include "extensions/common/features/simple_feature.h"
13 #include "extensions/common/manifest.h"
14 #include "extensions/common/value_builder.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 
17 namespace extensions {
18 
19 // Tests that a real manifest feature is available for the correct types of
20 // extensions and apps.
TEST(BaseFeatureProviderTest,ManifestFeatureTypes)21 TEST(BaseFeatureProviderTest, ManifestFeatureTypes) {
22   const FeatureProvider* provider = BaseFeatureProvider::GetByName("manifest");
23   // NOTE: This feature cannot have multiple rules, otherwise it is not a
24   // SimpleFeature.
25   SimpleFeature* feature =
26       static_cast<SimpleFeature*>(provider->GetFeature("description"));
27   ASSERT_TRUE(feature);
28   std::set<Manifest::Type>* extension_types = feature->extension_types();
29   EXPECT_EQ(6u, extension_types->size());
30   EXPECT_EQ(1u, extension_types->count(Manifest::TYPE_EXTENSION));
31   EXPECT_EQ(1u, extension_types->count(Manifest::TYPE_LEGACY_PACKAGED_APP));
32   EXPECT_EQ(1u, extension_types->count(Manifest::TYPE_PLATFORM_APP));
33   EXPECT_EQ(1u, extension_types->count(Manifest::TYPE_HOSTED_APP));
34   EXPECT_EQ(1u, extension_types->count(Manifest::TYPE_THEME));
35   EXPECT_EQ(1u, extension_types->count(Manifest::TYPE_SHARED_MODULE));
36 }
37 
38 // Tests that real manifest features have the correct availability for an
39 // extension.
TEST(BaseFeatureProviderTest,ManifestFeatureAvailability)40 TEST(BaseFeatureProviderTest, ManifestFeatureAvailability) {
41   const FeatureProvider* provider = BaseFeatureProvider::GetByName("manifest");
42 
43   scoped_refptr<const Extension> extension =
44       ExtensionBuilder()
45           .SetManifest(DictionaryBuilder()
46                            .Set("name", "test extension")
47                            .Set("version", "1")
48                            .Set("description", "hello there"))
49           .Build();
50   ASSERT_TRUE(extension.get());
51 
52   Feature* feature = provider->GetFeature("description");
53   EXPECT_EQ(Feature::IS_AVAILABLE,
54             feature->IsAvailableToContext(extension.get(),
55                                           Feature::UNSPECIFIED_CONTEXT,
56                                           GURL()).result());
57 
58   // This is a generic extension, so an app-only feature isn't allowed.
59   feature = provider->GetFeature("app.background");
60   ASSERT_TRUE(feature);
61   EXPECT_EQ(Feature::INVALID_TYPE,
62             feature->IsAvailableToContext(extension.get(),
63                                           Feature::UNSPECIFIED_CONTEXT,
64                                           GURL()).result());
65 
66   // A feature not listed in the manifest isn't allowed.
67   feature = provider->GetFeature("background");
68   ASSERT_TRUE(feature);
69   EXPECT_EQ(Feature::NOT_PRESENT,
70             feature->IsAvailableToContext(extension.get(),
71                                           Feature::UNSPECIFIED_CONTEXT,
72                                           GURL()).result());
73 }
74 
75 // Tests that a real permission feature is available for the correct types of
76 // extensions and apps.
TEST(BaseFeatureProviderTest,PermissionFeatureTypes)77 TEST(BaseFeatureProviderTest, PermissionFeatureTypes) {
78   const FeatureProvider* provider =
79       BaseFeatureProvider::GetByName("permission");
80   // NOTE: This feature cannot have multiple rules, otherwise it is not a
81   // SimpleFeature.
82   SimpleFeature* feature =
83       static_cast<SimpleFeature*>(provider->GetFeature("power"));
84   ASSERT_TRUE(feature);
85   std::set<Manifest::Type>* extension_types = feature->extension_types();
86   EXPECT_EQ(3u, extension_types->size());
87   EXPECT_EQ(1u, extension_types->count(Manifest::TYPE_EXTENSION));
88   EXPECT_EQ(1u, extension_types->count(Manifest::TYPE_LEGACY_PACKAGED_APP));
89   EXPECT_EQ(1u, extension_types->count(Manifest::TYPE_PLATFORM_APP));
90 }
91 
92 // Tests that real permission features have the correct availability for an app.
TEST(BaseFeatureProviderTest,PermissionFeatureAvailability)93 TEST(BaseFeatureProviderTest, PermissionFeatureAvailability) {
94   const FeatureProvider* provider =
95       BaseFeatureProvider::GetByName("permission");
96 
97   scoped_refptr<const Extension> app =
98       ExtensionBuilder()
99           .SetManifest(DictionaryBuilder()
100                            .Set("name", "test app")
101                            .Set("version", "1")
102                            .Set("app",
103                                 DictionaryBuilder().Set(
104                                     "background",
105                                     DictionaryBuilder().Set(
106                                         "scripts",
107                                         ListBuilder().Append("background.js"))))
108                            .Set("permissions", ListBuilder().Append("power")))
109           .Build();
110   ASSERT_TRUE(app.get());
111   ASSERT_TRUE(app->is_platform_app());
112 
113   // A permission requested in the manifest is available.
114   Feature* feature = provider->GetFeature("power");
115   EXPECT_EQ(
116       Feature::IS_AVAILABLE,
117       feature->IsAvailableToContext(
118                    app.get(), Feature::UNSPECIFIED_CONTEXT, GURL()).result());
119 
120   // A permission only available to whitelisted extensions returns availability
121   // NOT_FOUND_IN_WHITELIST.
122   feature = provider->GetFeature("bluetoothPrivate");
123   ASSERT_TRUE(feature);
124   EXPECT_EQ(
125       Feature::NOT_FOUND_IN_WHITELIST,
126       feature->IsAvailableToContext(
127                    app.get(), Feature::UNSPECIFIED_CONTEXT, GURL()).result());
128 
129   // A permission that isn't part of the manifest returns NOT_PRESENT.
130   feature = provider->GetFeature("serial");
131   ASSERT_TRUE(feature);
132   EXPECT_EQ(
133       Feature::NOT_PRESENT,
134       feature->IsAvailableToContext(
135                    app.get(), Feature::UNSPECIFIED_CONTEXT, GURL()).result());
136 }
137 
138 }  // namespace extensions
139