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.
5 #include "extensions/common/features/complex_feature.h"
9 #include "extensions/common/features/simple_feature.h"
10 #include "extensions/common/manifest.h"
11 #include "extensions/common/value_builder.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace extensions
{
16 TEST(ComplexFeatureTest
, MultipleRulesWhitelist
) {
17 const std::string
kIdFoo("fooabbbbccccddddeeeeffffgggghhhh");
18 const std::string
kIdBar("barabbbbccccddddeeeeffffgggghhhh");
19 scoped_ptr
<ComplexFeature::FeatureList
> features(
20 new ComplexFeature::FeatureList());
22 // Rule: "extension", whitelist "foo".
23 scoped_ptr
<SimpleFeature
> simple_feature(new SimpleFeature
);
24 scoped_ptr
<base::DictionaryValue
> rule(
26 .Set("whitelist", ListBuilder().Append(kIdFoo
))
27 .Set("extension_types", ListBuilder()
28 .Append("extension")).Build());
29 simple_feature
->Parse(rule
.get());
30 features
->push_back(simple_feature
.Pass());
32 // Rule: "legacy_packaged_app", whitelist "bar".
33 simple_feature
.reset(new SimpleFeature
);
34 rule
= DictionaryBuilder()
35 .Set("whitelist", ListBuilder().Append(kIdBar
))
36 .Set("extension_types", ListBuilder()
37 .Append("legacy_packaged_app")).Build();
38 simple_feature
->Parse(rule
.get());
39 features
->push_back(simple_feature
.Pass());
41 scoped_ptr
<ComplexFeature
> feature(new ComplexFeature(features
.Pass()));
43 // Test match 1st rule.
45 Feature::IS_AVAILABLE
,
46 feature
->IsAvailableToManifest(kIdFoo
,
47 Manifest::TYPE_EXTENSION
,
48 Manifest::INVALID_LOCATION
,
49 Feature::UNSPECIFIED_PLATFORM
,
50 Feature::GetCurrentPlatform()).result());
52 // Test match 2nd rule.
54 Feature::IS_AVAILABLE
,
55 feature
->IsAvailableToManifest(kIdBar
,
56 Manifest::TYPE_LEGACY_PACKAGED_APP
,
57 Manifest::INVALID_LOCATION
,
58 Feature::UNSPECIFIED_PLATFORM
,
59 Feature::GetCurrentPlatform()).result());
61 // Test whitelist with wrong extension type.
63 Feature::IS_AVAILABLE
,
64 feature
->IsAvailableToManifest(kIdBar
,
65 Manifest::TYPE_EXTENSION
,
66 Manifest::INVALID_LOCATION
,
67 Feature::UNSPECIFIED_PLATFORM
,
68 Feature::GetCurrentPlatform()).result());
70 Feature::IS_AVAILABLE
,
71 feature
->IsAvailableToManifest(kIdFoo
,
72 Manifest::TYPE_LEGACY_PACKAGED_APP
,
73 Manifest::INVALID_LOCATION
,
74 Feature::UNSPECIFIED_PLATFORM
,
75 Feature::GetCurrentPlatform()).result());
78 // Tests that dependencies are correctly checked.
79 TEST(ComplexFeatureTest
, Dependencies
) {
80 scoped_ptr
<ComplexFeature::FeatureList
> features(
81 new ComplexFeature::FeatureList());
83 // Rule which depends on an extension-only feature (content_security_policy).
84 scoped_ptr
<SimpleFeature
> simple_feature(new SimpleFeature
);
85 scoped_ptr
<base::DictionaryValue
> rule
=
88 ListBuilder().Append("manifest:content_security_policy"))
90 simple_feature
->Parse(rule
.get());
91 features
->push_back(simple_feature
.Pass());
93 // Rule which depends on an platform-app-only feature (serial).
94 simple_feature
.reset(new SimpleFeature
);
95 rule
= DictionaryBuilder()
96 .Set("dependencies", ListBuilder().Append("permission:serial"))
98 simple_feature
->Parse(rule
.get());
99 features
->push_back(simple_feature
.Pass());
101 scoped_ptr
<ComplexFeature
> feature(new ComplexFeature(features
.Pass()));
103 // Available to extensions because of the content_security_policy rule.
105 Feature::IS_AVAILABLE
,
106 feature
->IsAvailableToManifest("extensionid",
107 Manifest::TYPE_EXTENSION
,
108 Manifest::INVALID_LOCATION
,
109 Feature::UNSPECIFIED_PLATFORM
,
110 Feature::GetCurrentPlatform()).result());
112 // Available to platform apps because of the serial rule.
114 Feature::IS_AVAILABLE
,
115 feature
->IsAvailableToManifest("platformappid",
116 Manifest::TYPE_PLATFORM_APP
,
117 Manifest::INVALID_LOCATION
,
118 Feature::UNSPECIFIED_PLATFORM
,
119 Feature::GetCurrentPlatform()).result());
121 // Not available to hosted apps.
123 Feature::INVALID_TYPE
,
124 feature
->IsAvailableToManifest("hostedappid",
125 Manifest::TYPE_HOSTED_APP
,
126 Manifest::INVALID_LOCATION
,
127 Feature::UNSPECIFIED_PLATFORM
,
128 Feature::GetCurrentPlatform()).result());
131 } // namespace extensions