Home | History | Annotate | Download | only in features
      1 // Copyright (c) 2012 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 "chrome/common/extensions/features/complex_feature.h"
      6 
      7 #include "chrome/common/extensions/features/feature_channel.h"
      8 #include "chrome/common/extensions/features/simple_feature.h"
      9 #include "extensions/common/value_builder.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 using chrome::VersionInfo;
     13 using extensions::ComplexFeature;
     14 using extensions::DictionaryBuilder;
     15 using extensions::Feature;
     16 using extensions::ListBuilder;
     17 using extensions::Manifest;
     18 using extensions::ScopedCurrentChannel;
     19 using extensions::SimpleFeature;
     20 
     21 namespace {
     22 
     23 class ExtensionComplexFeatureTest : public testing::Test {
     24  protected:
     25   ExtensionComplexFeatureTest()
     26       : current_channel_(VersionInfo::CHANNEL_UNKNOWN) {}
     27   virtual ~ExtensionComplexFeatureTest() {}
     28 
     29  private:
     30   ScopedCurrentChannel current_channel_;
     31 };
     32 
     33 TEST_F(ExtensionComplexFeatureTest, MultipleRulesWhitelist) {
     34   const std::string kIdFoo("fooabbbbccccddddeeeeffffgggghhhh");
     35   const std::string kIdBar("barabbbbccccddddeeeeffffgggghhhh");
     36   scoped_ptr<ComplexFeature::FeatureList> features(
     37       new ComplexFeature::FeatureList());
     38 
     39   // Rule: "extension", whitelist "foo".
     40   scoped_ptr<SimpleFeature> simple_feature(new SimpleFeature());
     41   scoped_ptr<base::DictionaryValue> rule(
     42       DictionaryBuilder()
     43       .Set("whitelist", ListBuilder().Append(kIdFoo))
     44       .Set("extension_types", ListBuilder()
     45           .Append("extension")).Build());
     46   simple_feature->Parse(rule.get());
     47   features->push_back(simple_feature.release());
     48 
     49   // Rule: "legacy_packaged_app", whitelist "bar".
     50   simple_feature.reset(new SimpleFeature());
     51   rule = DictionaryBuilder()
     52       .Set("whitelist", ListBuilder().Append(kIdBar))
     53       .Set("extension_types", ListBuilder()
     54           .Append("legacy_packaged_app")).Build();
     55   simple_feature->Parse(rule.get());
     56   features->push_back(simple_feature.release());
     57 
     58   scoped_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass()));
     59 
     60   // Test match 1st rule.
     61   EXPECT_EQ(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
     62       kIdFoo,
     63       Manifest::TYPE_EXTENSION,
     64       Feature::UNSPECIFIED_LOCATION,
     65       Feature::UNSPECIFIED_PLATFORM,
     66       Feature::GetCurrentPlatform()).result());
     67 
     68   // Test match 2nd rule.
     69   EXPECT_EQ(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
     70       kIdBar,
     71       Manifest::TYPE_LEGACY_PACKAGED_APP,
     72       Feature::UNSPECIFIED_LOCATION,
     73       Feature::UNSPECIFIED_PLATFORM,
     74       Feature::GetCurrentPlatform()).result());
     75 
     76   // Test whitelist with wrong extension type.
     77   EXPECT_NE(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
     78       kIdBar,
     79       Manifest::TYPE_EXTENSION,
     80       Feature::UNSPECIFIED_LOCATION,
     81       Feature::UNSPECIFIED_PLATFORM,
     82       Feature::GetCurrentPlatform()).result());
     83   EXPECT_NE(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(kIdFoo,
     84       Manifest::TYPE_LEGACY_PACKAGED_APP,
     85       Feature::UNSPECIFIED_LOCATION,
     86       Feature::UNSPECIFIED_PLATFORM,
     87       Feature::GetCurrentPlatform()).result());
     88 }
     89 
     90 TEST_F(ExtensionComplexFeatureTest, MultipleRulesChannels) {
     91   scoped_ptr<ComplexFeature::FeatureList> features(
     92       new ComplexFeature::FeatureList());
     93 
     94   // Rule: "extension", channel trunk.
     95   scoped_ptr<SimpleFeature> simple_feature(new SimpleFeature());
     96   scoped_ptr<base::DictionaryValue> rule(
     97       DictionaryBuilder()
     98       .Set("channel", "trunk")
     99       .Set("extension_types", ListBuilder().Append("extension")).Build());
    100   simple_feature->Parse(rule.get());
    101   features->push_back(simple_feature.release());
    102 
    103   // Rule: "legacy_packaged_app", channel stable.
    104   simple_feature.reset(new SimpleFeature());
    105   rule = DictionaryBuilder()
    106       .Set("channel", "stable")
    107       .Set("extension_types", ListBuilder()
    108           .Append("legacy_packaged_app")).Build();
    109   simple_feature->Parse(rule.get());
    110   features->push_back(simple_feature.release());
    111 
    112   scoped_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass()));
    113 
    114   // Test match 1st rule.
    115   {
    116     ScopedCurrentChannel current_channel(VersionInfo::CHANNEL_UNKNOWN);
    117     EXPECT_EQ(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
    118         "1",
    119         Manifest::TYPE_EXTENSION,
    120         Feature::UNSPECIFIED_LOCATION,
    121         Feature::UNSPECIFIED_PLATFORM,
    122         Feature::GetCurrentPlatform()).result());
    123   }
    124 
    125   // Test match 2nd rule.
    126   {
    127     ScopedCurrentChannel current_channel(VersionInfo::CHANNEL_BETA);
    128     EXPECT_EQ(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
    129         "2",
    130         Manifest::TYPE_LEGACY_PACKAGED_APP,
    131         Feature::UNSPECIFIED_LOCATION,
    132         Feature::UNSPECIFIED_PLATFORM,
    133         Feature::GetCurrentPlatform()).result());
    134   }
    135 
    136   // Test feature not available to extensions above channel unknown.
    137   {
    138     ScopedCurrentChannel current_channel(VersionInfo::CHANNEL_BETA);
    139     EXPECT_NE(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
    140         "1",
    141         Manifest::TYPE_EXTENSION,
    142         Feature::UNSPECIFIED_LOCATION,
    143         Feature::UNSPECIFIED_PLATFORM,
    144         Feature::GetCurrentPlatform()).result());
    145   }
    146 }
    147 
    148 }  // namespace
    149