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/complex_feature.h"
6
7#include <string>
8
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"
13
14namespace extensions {
15
16TEST(ComplexFeatureTest, MultipleRulesWhitelist) {
17  const std::string kIdFoo("fooabbbbccccddddeeeeffffgggghhhh");
18  const std::string kIdBar("barabbbbccccddddeeeeffffgggghhhh");
19  scoped_ptr<ComplexFeature::FeatureList> features(
20      new ComplexFeature::FeatureList());
21
22  // Rule: "extension", whitelist "foo".
23  scoped_ptr<SimpleFeature> simple_feature(new SimpleFeature);
24  scoped_ptr<base::DictionaryValue> rule(
25      DictionaryBuilder()
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.release());
31
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.release());
40
41  scoped_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass()));
42
43  // Test match 1st rule.
44  EXPECT_EQ(
45      Feature::IS_AVAILABLE,
46      feature->IsAvailableToManifest(kIdFoo,
47                                     Manifest::TYPE_EXTENSION,
48                                     Manifest::INVALID_LOCATION,
49                                     Feature::UNSPECIFIED_PLATFORM,
50                                     Feature::GetCurrentPlatform()).result());
51
52  // Test match 2nd rule.
53  EXPECT_EQ(
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());
60
61  // Test whitelist with wrong extension type.
62  EXPECT_NE(
63      Feature::IS_AVAILABLE,
64      feature->IsAvailableToManifest(kIdBar,
65                                     Manifest::TYPE_EXTENSION,
66                                     Manifest::INVALID_LOCATION,
67                                     Feature::UNSPECIFIED_PLATFORM,
68                                     Feature::GetCurrentPlatform()).result());
69  EXPECT_NE(
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());
76}
77
78// Tests that dependencies are correctly checked.
79TEST(ComplexFeatureTest, Dependencies) {
80  scoped_ptr<ComplexFeature::FeatureList> features(
81      new ComplexFeature::FeatureList());
82
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 =
86      DictionaryBuilder()
87          .Set("dependencies",
88               ListBuilder().Append("manifest:content_security_policy"))
89          .Build();
90  simple_feature->Parse(rule.get());
91  features->push_back(simple_feature.release());
92
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"))
97             .Build();
98  simple_feature->Parse(rule.get());
99  features->push_back(simple_feature.release());
100
101  scoped_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass()));
102
103  // Available to extensions because of the content_security_policy rule.
104  EXPECT_EQ(
105      Feature::IS_AVAILABLE,
106      feature->IsAvailableToManifest("extensionid",
107                                     Manifest::TYPE_EXTENSION,
108                                     Manifest::INVALID_LOCATION,
109                                     Feature::UNSPECIFIED_PLATFORM,
110                                     Feature::GetCurrentPlatform()).result());
111
112  // Available to platform apps because of the serial rule.
113  EXPECT_EQ(
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());
120
121  // Not available to hosted apps.
122  EXPECT_EQ(
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());
129}
130
131}  // namespace extensions
132