| OLD | NEW |
| (Empty) |
| 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/simple_feature.h" | |
| 6 | |
| 7 #include "chrome/common/extensions/features/chrome_channel_feature_filter.h" | |
| 8 #include "chrome/common/extensions/features/feature_channel.h" | |
| 9 #include "extensions/common/value_builder.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 using chrome::VersionInfo; | |
| 13 using extensions::DictionaryBuilder; | |
| 14 using extensions::Extension; | |
| 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 struct IsAvailableTestData { | |
| 24 std::string extension_id; | |
| 25 Manifest::Type extension_type; | |
| 26 Feature::Location location; | |
| 27 Feature::Platform platform; | |
| 28 int manifest_version; | |
| 29 Feature::AvailabilityResult expected_result; | |
| 30 }; | |
| 31 | |
| 32 class ExtensionSimpleFeatureTest : public testing::Test { | |
| 33 protected: | |
| 34 ExtensionSimpleFeatureTest() | |
| 35 : current_channel_(VersionInfo::CHANNEL_UNKNOWN) {} | |
| 36 virtual ~ExtensionSimpleFeatureTest() {} | |
| 37 | |
| 38 private: | |
| 39 ScopedCurrentChannel current_channel_; | |
| 40 }; | |
| 41 | |
| 42 TEST_F(ExtensionSimpleFeatureTest, IsAvailableNullCase) { | |
| 43 const IsAvailableTestData tests[] = { | |
| 44 { "", Manifest::TYPE_UNKNOWN, | |
| 45 Feature::UNSPECIFIED_LOCATION, Feature::UNSPECIFIED_PLATFORM, -1, | |
| 46 Feature::IS_AVAILABLE }, | |
| 47 { "random-extension", Manifest::TYPE_UNKNOWN, | |
| 48 Feature::UNSPECIFIED_LOCATION, Feature::UNSPECIFIED_PLATFORM, -1, | |
| 49 Feature::IS_AVAILABLE }, | |
| 50 { "", Manifest::TYPE_LEGACY_PACKAGED_APP, | |
| 51 Feature::UNSPECIFIED_LOCATION, Feature::UNSPECIFIED_PLATFORM, -1, | |
| 52 Feature::IS_AVAILABLE }, | |
| 53 { "", Manifest::TYPE_UNKNOWN, | |
| 54 Feature::UNSPECIFIED_LOCATION, Feature::UNSPECIFIED_PLATFORM, -1, | |
| 55 Feature::IS_AVAILABLE }, | |
| 56 { "", Manifest::TYPE_UNKNOWN, | |
| 57 Feature::COMPONENT_LOCATION, Feature::UNSPECIFIED_PLATFORM, -1, | |
| 58 Feature::IS_AVAILABLE }, | |
| 59 { "", Manifest::TYPE_UNKNOWN, | |
| 60 Feature::UNSPECIFIED_LOCATION, Feature::CHROMEOS_PLATFORM, -1, | |
| 61 Feature::IS_AVAILABLE }, | |
| 62 { "", Manifest::TYPE_UNKNOWN, | |
| 63 Feature::UNSPECIFIED_LOCATION, Feature::UNSPECIFIED_PLATFORM, 25, | |
| 64 Feature::IS_AVAILABLE } | |
| 65 }; | |
| 66 | |
| 67 SimpleFeature feature; | |
| 68 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 69 const IsAvailableTestData& test = tests[i]; | |
| 70 EXPECT_EQ(test.expected_result, | |
| 71 feature.IsAvailableToManifest(test.extension_id, | |
| 72 test.extension_type, | |
| 73 test.location, | |
| 74 test.manifest_version, | |
| 75 test.platform).result()); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 TEST_F(ExtensionSimpleFeatureTest, Whitelist) { | |
| 80 const std::string kIdFoo("fooabbbbccccddddeeeeffffgggghhhh"); | |
| 81 const std::string kIdBar("barabbbbccccddddeeeeffffgggghhhh"); | |
| 82 const std::string kIdBaz("bazabbbbccccddddeeeeffffgggghhhh"); | |
| 83 SimpleFeature feature; | |
| 84 feature.whitelist()->insert(kIdFoo); | |
| 85 feature.whitelist()->insert(kIdBar); | |
| 86 | |
| 87 EXPECT_EQ(Feature::IS_AVAILABLE, feature.IsAvailableToManifest( | |
| 88 kIdFoo, Manifest::TYPE_UNKNOWN, Feature::UNSPECIFIED_LOCATION, -1, | |
| 89 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 90 EXPECT_EQ(Feature::IS_AVAILABLE, feature.IsAvailableToManifest( | |
| 91 kIdBar, Manifest::TYPE_UNKNOWN, Feature::UNSPECIFIED_LOCATION, -1, | |
| 92 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 93 | |
| 94 EXPECT_EQ(Feature::NOT_FOUND_IN_WHITELIST, feature.IsAvailableToManifest( | |
| 95 kIdBaz, Manifest::TYPE_UNKNOWN, Feature::UNSPECIFIED_LOCATION, -1, | |
| 96 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 97 EXPECT_EQ( | |
| 98 Feature::NOT_FOUND_IN_WHITELIST, | |
| 99 feature.IsAvailableToManifest(std::string(), | |
| 100 Manifest::TYPE_UNKNOWN, | |
| 101 Feature::UNSPECIFIED_LOCATION, | |
| 102 -1, | |
| 103 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 104 | |
| 105 feature.extension_types()->insert(Manifest::TYPE_LEGACY_PACKAGED_APP); | |
| 106 EXPECT_EQ(Feature::NOT_FOUND_IN_WHITELIST, feature.IsAvailableToManifest( | |
| 107 kIdBaz, Manifest::TYPE_LEGACY_PACKAGED_APP, | |
| 108 Feature::UNSPECIFIED_LOCATION, -1, | |
| 109 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 110 } | |
| 111 | |
| 112 TEST_F(ExtensionSimpleFeatureTest, HashedIdWhitelist) { | |
| 113 // echo -n "fooabbbbccccddddeeeeffffgggghhhh" | | |
| 114 // sha1sum | tr '[:lower:]' '[:upper:]' | |
| 115 const std::string kIdFoo("fooabbbbccccddddeeeeffffgggghhhh"); | |
| 116 const std::string kIdFooHashed("55BC7228A0D502A2A48C9BB16B07062A01E62897"); | |
| 117 SimpleFeature feature; | |
| 118 | |
| 119 feature.whitelist()->insert(kIdFooHashed); | |
| 120 | |
| 121 EXPECT_EQ(Feature::IS_AVAILABLE, feature.IsAvailableToManifest( | |
| 122 kIdFoo, Manifest::TYPE_UNKNOWN, Feature::UNSPECIFIED_LOCATION, -1, | |
| 123 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 124 EXPECT_NE(Feature::IS_AVAILABLE, feature.IsAvailableToManifest( | |
| 125 kIdFooHashed, Manifest::TYPE_UNKNOWN, | |
| 126 Feature::UNSPECIFIED_LOCATION, -1, | |
| 127 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 128 EXPECT_EQ(Feature::NOT_FOUND_IN_WHITELIST, feature.IsAvailableToManifest( | |
| 129 "slightlytoooolongforanextensionid", Manifest::TYPE_UNKNOWN, | |
| 130 Feature::UNSPECIFIED_LOCATION, -1, | |
| 131 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 132 EXPECT_EQ(Feature::NOT_FOUND_IN_WHITELIST, feature.IsAvailableToManifest( | |
| 133 "tooshortforanextensionid", Manifest::TYPE_UNKNOWN, | |
| 134 Feature::UNSPECIFIED_LOCATION, -1, | |
| 135 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 136 } | |
| 137 | |
| 138 TEST_F(ExtensionSimpleFeatureTest, PackageType) { | |
| 139 SimpleFeature feature; | |
| 140 feature.extension_types()->insert(Manifest::TYPE_EXTENSION); | |
| 141 feature.extension_types()->insert(Manifest::TYPE_LEGACY_PACKAGED_APP); | |
| 142 | |
| 143 EXPECT_EQ( | |
| 144 Feature::IS_AVAILABLE, | |
| 145 feature.IsAvailableToManifest(std::string(), | |
| 146 Manifest::TYPE_EXTENSION, | |
| 147 Feature::UNSPECIFIED_LOCATION, | |
| 148 -1, | |
| 149 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 150 EXPECT_EQ( | |
| 151 Feature::IS_AVAILABLE, | |
| 152 feature.IsAvailableToManifest(std::string(), | |
| 153 Manifest::TYPE_LEGACY_PACKAGED_APP, | |
| 154 Feature::UNSPECIFIED_LOCATION, | |
| 155 -1, | |
| 156 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 157 | |
| 158 EXPECT_EQ( | |
| 159 Feature::INVALID_TYPE, | |
| 160 feature.IsAvailableToManifest(std::string(), | |
| 161 Manifest::TYPE_UNKNOWN, | |
| 162 Feature::UNSPECIFIED_LOCATION, | |
| 163 -1, | |
| 164 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 165 EXPECT_EQ( | |
| 166 Feature::INVALID_TYPE, | |
| 167 feature.IsAvailableToManifest(std::string(), | |
| 168 Manifest::TYPE_THEME, | |
| 169 Feature::UNSPECIFIED_LOCATION, | |
| 170 -1, | |
| 171 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 172 } | |
| 173 | |
| 174 TEST_F(ExtensionSimpleFeatureTest, Context) { | |
| 175 SimpleFeature feature; | |
| 176 feature.set_name("somefeature"); | |
| 177 feature.GetContexts()->insert(Feature::BLESSED_EXTENSION_CONTEXT); | |
| 178 feature.extension_types()->insert(Manifest::TYPE_LEGACY_PACKAGED_APP); | |
| 179 feature.platforms()->insert(Feature::CHROMEOS_PLATFORM); | |
| 180 feature.set_min_manifest_version(21); | |
| 181 feature.set_max_manifest_version(25); | |
| 182 | |
| 183 base::DictionaryValue manifest; | |
| 184 manifest.SetString("name", "test"); | |
| 185 manifest.SetString("version", "1"); | |
| 186 manifest.SetInteger("manifest_version", 21); | |
| 187 manifest.SetString("app.launch.local_path", "foo.html"); | |
| 188 | |
| 189 std::string error; | |
| 190 scoped_refptr<const Extension> extension(Extension::Create( | |
| 191 base::FilePath(), Manifest::INTERNAL, manifest, Extension::NO_FLAGS, | |
| 192 &error)); | |
| 193 EXPECT_EQ("", error); | |
| 194 ASSERT_TRUE(extension.get()); | |
| 195 | |
| 196 feature.whitelist()->insert("monkey"); | |
| 197 EXPECT_EQ(Feature::NOT_FOUND_IN_WHITELIST, feature.IsAvailableToContext( | |
| 198 extension.get(), Feature::BLESSED_EXTENSION_CONTEXT, | |
| 199 Feature::CHROMEOS_PLATFORM).result()); | |
| 200 feature.whitelist()->clear(); | |
| 201 | |
| 202 feature.extension_types()->clear(); | |
| 203 feature.extension_types()->insert(Manifest::TYPE_THEME); | |
| 204 { | |
| 205 Feature::Availability availability = feature.IsAvailableToContext( | |
| 206 extension.get(), Feature::BLESSED_EXTENSION_CONTEXT, | |
| 207 Feature::CHROMEOS_PLATFORM); | |
| 208 EXPECT_EQ(Feature::INVALID_TYPE, availability.result()); | |
| 209 EXPECT_EQ("'somefeature' is only allowed for themes, " | |
| 210 "but this is a legacy packaged app.", | |
| 211 availability.message()); | |
| 212 } | |
| 213 | |
| 214 feature.extension_types()->clear(); | |
| 215 feature.extension_types()->insert(Manifest::TYPE_LEGACY_PACKAGED_APP); | |
| 216 feature.GetContexts()->clear(); | |
| 217 feature.GetContexts()->insert(Feature::UNBLESSED_EXTENSION_CONTEXT); | |
| 218 feature.GetContexts()->insert(Feature::CONTENT_SCRIPT_CONTEXT); | |
| 219 { | |
| 220 Feature::Availability availability = feature.IsAvailableToContext( | |
| 221 extension.get(), Feature::BLESSED_EXTENSION_CONTEXT, | |
| 222 Feature::CHROMEOS_PLATFORM); | |
| 223 EXPECT_EQ(Feature::INVALID_CONTEXT, availability.result()); | |
| 224 EXPECT_EQ("'somefeature' is only allowed to run in extension iframes and " | |
| 225 "content scripts, but this is a privileged page", | |
| 226 availability.message()); | |
| 227 } | |
| 228 | |
| 229 feature.GetContexts()->insert(Feature::WEB_PAGE_CONTEXT); | |
| 230 { | |
| 231 Feature::Availability availability = feature.IsAvailableToContext( | |
| 232 extension.get(), Feature::BLESSED_EXTENSION_CONTEXT, | |
| 233 Feature::CHROMEOS_PLATFORM); | |
| 234 EXPECT_EQ(Feature::INVALID_CONTEXT, availability.result()); | |
| 235 EXPECT_EQ("'somefeature' is only allowed to run in extension iframes, " | |
| 236 "content scripts, and web pages, but this is a privileged page", | |
| 237 availability.message()); | |
| 238 } | |
| 239 | |
| 240 feature.GetContexts()->clear(); | |
| 241 feature.GetContexts()->insert(Feature::BLESSED_EXTENSION_CONTEXT); | |
| 242 feature.set_location(Feature::COMPONENT_LOCATION); | |
| 243 EXPECT_EQ(Feature::INVALID_LOCATION, feature.IsAvailableToContext( | |
| 244 extension.get(), Feature::BLESSED_EXTENSION_CONTEXT, | |
| 245 Feature::CHROMEOS_PLATFORM).result()); | |
| 246 feature.set_location(Feature::UNSPECIFIED_LOCATION); | |
| 247 | |
| 248 EXPECT_EQ(Feature::INVALID_PLATFORM, feature.IsAvailableToContext( | |
| 249 extension.get(), Feature::BLESSED_EXTENSION_CONTEXT, | |
| 250 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 251 | |
| 252 feature.set_min_manifest_version(22); | |
| 253 EXPECT_EQ(Feature::INVALID_MIN_MANIFEST_VERSION, feature.IsAvailableToContext( | |
| 254 extension.get(), Feature::BLESSED_EXTENSION_CONTEXT, | |
| 255 Feature::CHROMEOS_PLATFORM).result()); | |
| 256 feature.set_min_manifest_version(21); | |
| 257 | |
| 258 feature.set_max_manifest_version(18); | |
| 259 EXPECT_EQ(Feature::INVALID_MAX_MANIFEST_VERSION, feature.IsAvailableToContext( | |
| 260 extension.get(), Feature::BLESSED_EXTENSION_CONTEXT, | |
| 261 Feature::CHROMEOS_PLATFORM).result()); | |
| 262 feature.set_max_manifest_version(25); | |
| 263 } | |
| 264 | |
| 265 TEST_F(ExtensionSimpleFeatureTest, Location) { | |
| 266 SimpleFeature feature; | |
| 267 | |
| 268 // If the feature specifies "component" as its location, then only component | |
| 269 // extensions can access it. | |
| 270 feature.set_location(Feature::COMPONENT_LOCATION); | |
| 271 EXPECT_EQ( | |
| 272 Feature::IS_AVAILABLE, | |
| 273 feature.IsAvailableToManifest(std::string(), | |
| 274 Manifest::TYPE_UNKNOWN, | |
| 275 Feature::COMPONENT_LOCATION, | |
| 276 -1, | |
| 277 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 278 EXPECT_EQ( | |
| 279 Feature::INVALID_LOCATION, | |
| 280 feature.IsAvailableToManifest(std::string(), | |
| 281 Manifest::TYPE_UNKNOWN, | |
| 282 Feature::UNSPECIFIED_LOCATION, | |
| 283 -1, | |
| 284 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 285 | |
| 286 // But component extensions can access anything else, whatever their location. | |
| 287 feature.set_location(Feature::UNSPECIFIED_LOCATION); | |
| 288 EXPECT_EQ( | |
| 289 Feature::IS_AVAILABLE, | |
| 290 feature.IsAvailableToManifest(std::string(), | |
| 291 Manifest::TYPE_UNKNOWN, | |
| 292 Feature::COMPONENT_LOCATION, | |
| 293 -1, | |
| 294 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 295 } | |
| 296 | |
| 297 TEST_F(ExtensionSimpleFeatureTest, Platform) { | |
| 298 SimpleFeature feature; | |
| 299 feature.platforms()->insert(Feature::CHROMEOS_PLATFORM); | |
| 300 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 301 feature.IsAvailableToManifest(std::string(), | |
| 302 Manifest::TYPE_UNKNOWN, | |
| 303 Feature::UNSPECIFIED_LOCATION, | |
| 304 -1, | |
| 305 Feature::CHROMEOS_PLATFORM).result()); | |
| 306 EXPECT_EQ( | |
| 307 Feature::INVALID_PLATFORM, | |
| 308 feature.IsAvailableToManifest(std::string(), | |
| 309 Manifest::TYPE_UNKNOWN, | |
| 310 Feature::UNSPECIFIED_LOCATION, | |
| 311 -1, | |
| 312 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 313 } | |
| 314 | |
| 315 TEST_F(ExtensionSimpleFeatureTest, Version) { | |
| 316 SimpleFeature feature; | |
| 317 feature.set_min_manifest_version(5); | |
| 318 | |
| 319 EXPECT_EQ( | |
| 320 Feature::INVALID_MIN_MANIFEST_VERSION, | |
| 321 feature.IsAvailableToManifest(std::string(), | |
| 322 Manifest::TYPE_UNKNOWN, | |
| 323 Feature::UNSPECIFIED_LOCATION, | |
| 324 0, | |
| 325 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 326 EXPECT_EQ( | |
| 327 Feature::INVALID_MIN_MANIFEST_VERSION, | |
| 328 feature.IsAvailableToManifest(std::string(), | |
| 329 Manifest::TYPE_UNKNOWN, | |
| 330 Feature::UNSPECIFIED_LOCATION, | |
| 331 4, | |
| 332 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 333 | |
| 334 EXPECT_EQ( | |
| 335 Feature::IS_AVAILABLE, | |
| 336 feature.IsAvailableToManifest(std::string(), | |
| 337 Manifest::TYPE_UNKNOWN, | |
| 338 Feature::UNSPECIFIED_LOCATION, | |
| 339 5, | |
| 340 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 341 EXPECT_EQ( | |
| 342 Feature::IS_AVAILABLE, | |
| 343 feature.IsAvailableToManifest(std::string(), | |
| 344 Manifest::TYPE_UNKNOWN, | |
| 345 Feature::UNSPECIFIED_LOCATION, | |
| 346 10, | |
| 347 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 348 | |
| 349 feature.set_max_manifest_version(8); | |
| 350 | |
| 351 EXPECT_EQ( | |
| 352 Feature::INVALID_MAX_MANIFEST_VERSION, | |
| 353 feature.IsAvailableToManifest(std::string(), | |
| 354 Manifest::TYPE_UNKNOWN, | |
| 355 Feature::UNSPECIFIED_LOCATION, | |
| 356 10, | |
| 357 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 358 EXPECT_EQ( | |
| 359 Feature::IS_AVAILABLE, | |
| 360 feature.IsAvailableToManifest(std::string(), | |
| 361 Manifest::TYPE_UNKNOWN, | |
| 362 Feature::UNSPECIFIED_LOCATION, | |
| 363 8, | |
| 364 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 365 EXPECT_EQ( | |
| 366 Feature::IS_AVAILABLE, | |
| 367 feature.IsAvailableToManifest(std::string(), | |
| 368 Manifest::TYPE_UNKNOWN, | |
| 369 Feature::UNSPECIFIED_LOCATION, | |
| 370 7, | |
| 371 Feature::UNSPECIFIED_PLATFORM).result()); | |
| 372 } | |
| 373 | |
| 374 TEST_F(ExtensionSimpleFeatureTest, ParseNull) { | |
| 375 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | |
| 376 scoped_ptr<SimpleFeature> feature(new SimpleFeature()); | |
| 377 feature->Parse(value.get()); | |
| 378 EXPECT_TRUE(feature->whitelist()->empty()); | |
| 379 EXPECT_TRUE(feature->extension_types()->empty()); | |
| 380 EXPECT_TRUE(feature->GetContexts()->empty()); | |
| 381 EXPECT_EQ(Feature::UNSPECIFIED_LOCATION, feature->location()); | |
| 382 EXPECT_TRUE(feature->platforms()->empty()); | |
| 383 EXPECT_EQ(0, feature->min_manifest_version()); | |
| 384 EXPECT_EQ(0, feature->max_manifest_version()); | |
| 385 } | |
| 386 | |
| 387 TEST_F(ExtensionSimpleFeatureTest, ParseWhitelist) { | |
| 388 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | |
| 389 base::ListValue* whitelist = new base::ListValue(); | |
| 390 whitelist->Append(new base::StringValue("foo")); | |
| 391 whitelist->Append(new base::StringValue("bar")); | |
| 392 value->Set("whitelist", whitelist); | |
| 393 scoped_ptr<SimpleFeature> feature(new SimpleFeature()); | |
| 394 feature->Parse(value.get()); | |
| 395 EXPECT_EQ(2u, feature->whitelist()->size()); | |
| 396 EXPECT_TRUE(feature->whitelist()->count("foo")); | |
| 397 EXPECT_TRUE(feature->whitelist()->count("bar")); | |
| 398 } | |
| 399 | |
| 400 TEST_F(ExtensionSimpleFeatureTest, ParsePackageTypes) { | |
| 401 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | |
| 402 base::ListValue* extension_types = new base::ListValue(); | |
| 403 extension_types->Append(new base::StringValue("extension")); | |
| 404 extension_types->Append(new base::StringValue("theme")); | |
| 405 extension_types->Append(new base::StringValue("legacy_packaged_app")); | |
| 406 extension_types->Append(new base::StringValue("hosted_app")); | |
| 407 extension_types->Append(new base::StringValue("platform_app")); | |
| 408 extension_types->Append(new base::StringValue("shared_module")); | |
| 409 value->Set("extension_types", extension_types); | |
| 410 scoped_ptr<SimpleFeature> feature(new SimpleFeature()); | |
| 411 feature->Parse(value.get()); | |
| 412 EXPECT_EQ(6u, feature->extension_types()->size()); | |
| 413 EXPECT_TRUE(feature->extension_types()->count(Manifest::TYPE_EXTENSION)); | |
| 414 EXPECT_TRUE(feature->extension_types()->count(Manifest::TYPE_THEME)); | |
| 415 EXPECT_TRUE(feature->extension_types()->count( | |
| 416 Manifest::TYPE_LEGACY_PACKAGED_APP)); | |
| 417 EXPECT_TRUE(feature->extension_types()->count(Manifest::TYPE_HOSTED_APP)); | |
| 418 EXPECT_TRUE(feature->extension_types()->count(Manifest::TYPE_PLATFORM_APP)); | |
| 419 EXPECT_TRUE(feature->extension_types()->count(Manifest::TYPE_SHARED_MODULE)); | |
| 420 | |
| 421 value->SetString("extension_types", "all"); | |
| 422 scoped_ptr<SimpleFeature> feature2(new SimpleFeature()); | |
| 423 feature2->Parse(value.get()); | |
| 424 EXPECT_EQ(*(feature->extension_types()), *(feature2->extension_types())); | |
| 425 } | |
| 426 | |
| 427 TEST_F(ExtensionSimpleFeatureTest, ParseContexts) { | |
| 428 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | |
| 429 base::ListValue* contexts = new base::ListValue(); | |
| 430 contexts->Append(new base::StringValue("blessed_extension")); | |
| 431 contexts->Append(new base::StringValue("unblessed_extension")); | |
| 432 contexts->Append(new base::StringValue("content_script")); | |
| 433 contexts->Append(new base::StringValue("web_page")); | |
| 434 contexts->Append(new base::StringValue("blessed_web_page")); | |
| 435 value->Set("contexts", contexts); | |
| 436 scoped_ptr<SimpleFeature> feature(new SimpleFeature()); | |
| 437 feature->Parse(value.get()); | |
| 438 EXPECT_EQ(5u, feature->GetContexts()->size()); | |
| 439 EXPECT_TRUE( | |
| 440 feature->GetContexts()->count(Feature::BLESSED_EXTENSION_CONTEXT)); | |
| 441 EXPECT_TRUE( | |
| 442 feature->GetContexts()->count(Feature::UNBLESSED_EXTENSION_CONTEXT)); | |
| 443 EXPECT_TRUE( | |
| 444 feature->GetContexts()->count(Feature::CONTENT_SCRIPT_CONTEXT)); | |
| 445 EXPECT_TRUE( | |
| 446 feature->GetContexts()->count(Feature::WEB_PAGE_CONTEXT)); | |
| 447 EXPECT_TRUE( | |
| 448 feature->GetContexts()->count(Feature::BLESSED_WEB_PAGE_CONTEXT)); | |
| 449 | |
| 450 value->SetString("contexts", "all"); | |
| 451 scoped_ptr<SimpleFeature> feature2(new SimpleFeature()); | |
| 452 feature2->Parse(value.get()); | |
| 453 EXPECT_EQ(*(feature->GetContexts()), *(feature2->GetContexts())); | |
| 454 } | |
| 455 | |
| 456 TEST_F(ExtensionSimpleFeatureTest, ParseLocation) { | |
| 457 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | |
| 458 value->SetString("location", "component"); | |
| 459 scoped_ptr<SimpleFeature> feature(new SimpleFeature()); | |
| 460 feature->Parse(value.get()); | |
| 461 EXPECT_EQ(Feature::COMPONENT_LOCATION, feature->location()); | |
| 462 } | |
| 463 | |
| 464 TEST_F(ExtensionSimpleFeatureTest, ParsePlatforms) { | |
| 465 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | |
| 466 scoped_ptr<SimpleFeature> feature(new SimpleFeature()); | |
| 467 base::ListValue* platforms = new base::ListValue(); | |
| 468 value->Set("platforms", platforms); | |
| 469 feature->Parse(value.get()); | |
| 470 EXPECT_TRUE(feature->platforms()->empty()); | |
| 471 | |
| 472 platforms->AppendString("chromeos"); | |
| 473 feature->Parse(value.get()); | |
| 474 EXPECT_FALSE(feature->platforms()->empty()); | |
| 475 EXPECT_EQ(Feature::CHROMEOS_PLATFORM, *feature->platforms()->begin()); | |
| 476 | |
| 477 platforms->Clear(); | |
| 478 platforms->AppendString("win"); | |
| 479 feature->Parse(value.get()); | |
| 480 EXPECT_FALSE(feature->platforms()->empty()); | |
| 481 EXPECT_EQ(Feature::WIN_PLATFORM, *feature->platforms()->begin()); | |
| 482 | |
| 483 platforms->Clear(); | |
| 484 platforms->AppendString("win"); | |
| 485 platforms->AppendString("chromeos"); | |
| 486 feature->Parse(value.get()); | |
| 487 std::set<Feature::Platform> expected_platforms; | |
| 488 expected_platforms.insert(Feature::CHROMEOS_PLATFORM); | |
| 489 expected_platforms.insert(Feature::WIN_PLATFORM); | |
| 490 | |
| 491 EXPECT_FALSE(feature->platforms()->empty()); | |
| 492 EXPECT_EQ(expected_platforms, *feature->platforms()); | |
| 493 } | |
| 494 | |
| 495 TEST_F(ExtensionSimpleFeatureTest, ManifestVersion) { | |
| 496 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | |
| 497 value->SetInteger("min_manifest_version", 1); | |
| 498 value->SetInteger("max_manifest_version", 5); | |
| 499 scoped_ptr<SimpleFeature> feature(new SimpleFeature()); | |
| 500 feature->Parse(value.get()); | |
| 501 EXPECT_EQ(1, feature->min_manifest_version()); | |
| 502 EXPECT_EQ(5, feature->max_manifest_version()); | |
| 503 } | |
| 504 | |
| 505 TEST_F(ExtensionSimpleFeatureTest, Inheritance) { | |
| 506 SimpleFeature feature; | |
| 507 feature.whitelist()->insert("foo"); | |
| 508 feature.extension_types()->insert(Manifest::TYPE_THEME); | |
| 509 feature.GetContexts()->insert(Feature::BLESSED_EXTENSION_CONTEXT); | |
| 510 feature.set_location(Feature::COMPONENT_LOCATION); | |
| 511 feature.platforms()->insert(Feature::CHROMEOS_PLATFORM); | |
| 512 feature.set_min_manifest_version(1); | |
| 513 feature.set_max_manifest_version(2); | |
| 514 | |
| 515 // Test additive parsing. Parsing an empty dictionary should result in no | |
| 516 // changes to a SimpleFeature. | |
| 517 base::DictionaryValue definition; | |
| 518 feature.Parse(&definition); | |
| 519 EXPECT_EQ(1u, feature.whitelist()->size()); | |
| 520 EXPECT_EQ(1u, feature.extension_types()->size()); | |
| 521 EXPECT_EQ(1u, feature.GetContexts()->size()); | |
| 522 EXPECT_EQ(1u, feature.whitelist()->count("foo")); | |
| 523 EXPECT_EQ(Feature::COMPONENT_LOCATION, feature.location()); | |
| 524 EXPECT_EQ(1u, feature.platforms()->size()); | |
| 525 EXPECT_EQ(1u, feature.platforms()->count(Feature::CHROMEOS_PLATFORM)); | |
| 526 EXPECT_EQ(1, feature.min_manifest_version()); | |
| 527 EXPECT_EQ(2, feature.max_manifest_version()); | |
| 528 | |
| 529 base::ListValue* whitelist = new base::ListValue(); | |
| 530 base::ListValue* extension_types = new base::ListValue(); | |
| 531 base::ListValue* contexts = new base::ListValue(); | |
| 532 whitelist->Append(new base::StringValue("bar")); | |
| 533 extension_types->Append(new base::StringValue("extension")); | |
| 534 contexts->Append(new base::StringValue("unblessed_extension")); | |
| 535 definition.Set("whitelist", whitelist); | |
| 536 definition.Set("extension_types", extension_types); | |
| 537 definition.Set("contexts", contexts); | |
| 538 // Can't test location or platform because we only have one value so far. | |
| 539 definition.Set("min_manifest_version", new base::FundamentalValue(2)); | |
| 540 definition.Set("max_manifest_version", new base::FundamentalValue(3)); | |
| 541 | |
| 542 feature.Parse(&definition); | |
| 543 EXPECT_EQ(1u, feature.whitelist()->size()); | |
| 544 EXPECT_EQ(1u, feature.extension_types()->size()); | |
| 545 EXPECT_EQ(1u, feature.GetContexts()->size()); | |
| 546 EXPECT_EQ(1u, feature.whitelist()->count("bar")); | |
| 547 EXPECT_EQ(1u, feature.extension_types()->count(Manifest::TYPE_EXTENSION)); | |
| 548 EXPECT_EQ(1u, | |
| 549 feature.GetContexts()->count(Feature::UNBLESSED_EXTENSION_CONTEXT)); | |
| 550 EXPECT_EQ(2, feature.min_manifest_version()); | |
| 551 EXPECT_EQ(3, feature.max_manifest_version()); | |
| 552 } | |
| 553 | |
| 554 Feature::AvailabilityResult IsAvailableInChannel( | |
| 555 const std::string& channel, VersionInfo::Channel channel_for_testing) { | |
| 556 ScopedCurrentChannel current_channel(channel_for_testing); | |
| 557 | |
| 558 SimpleFeature feature; | |
| 559 feature.AddFilter(scoped_ptr<extensions::SimpleFeatureFilter>( | |
| 560 new extensions::ChromeChannelFeatureFilter(&feature))); | |
| 561 if (!channel.empty()) { | |
| 562 base::DictionaryValue feature_value; | |
| 563 feature_value.SetString("channel", channel); | |
| 564 feature.Parse(&feature_value); | |
| 565 } | |
| 566 | |
| 567 return feature.IsAvailableToManifest( | |
| 568 "random-extension", | |
| 569 Manifest::TYPE_UNKNOWN, | |
| 570 Feature::UNSPECIFIED_LOCATION, | |
| 571 -1, | |
| 572 Feature::GetCurrentPlatform()).result(); | |
| 573 } | |
| 574 | |
| 575 TEST_F(ExtensionSimpleFeatureTest, SupportedChannel) { | |
| 576 // stable supported. | |
| 577 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 578 IsAvailableInChannel("stable", VersionInfo::CHANNEL_UNKNOWN)); | |
| 579 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 580 IsAvailableInChannel("stable", VersionInfo::CHANNEL_CANARY)); | |
| 581 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 582 IsAvailableInChannel("stable", VersionInfo::CHANNEL_DEV)); | |
| 583 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 584 IsAvailableInChannel("stable", VersionInfo::CHANNEL_BETA)); | |
| 585 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 586 IsAvailableInChannel("stable", VersionInfo::CHANNEL_STABLE)); | |
| 587 | |
| 588 // beta supported. | |
| 589 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 590 IsAvailableInChannel("beta", VersionInfo::CHANNEL_UNKNOWN)); | |
| 591 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 592 IsAvailableInChannel("beta", VersionInfo::CHANNEL_CANARY)); | |
| 593 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 594 IsAvailableInChannel("beta", VersionInfo::CHANNEL_DEV)); | |
| 595 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 596 IsAvailableInChannel("beta", VersionInfo::CHANNEL_BETA)); | |
| 597 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | |
| 598 IsAvailableInChannel("beta", VersionInfo::CHANNEL_STABLE)); | |
| 599 | |
| 600 // dev supported. | |
| 601 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 602 IsAvailableInChannel("dev", VersionInfo::CHANNEL_UNKNOWN)); | |
| 603 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 604 IsAvailableInChannel("dev", VersionInfo::CHANNEL_CANARY)); | |
| 605 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 606 IsAvailableInChannel("dev", VersionInfo::CHANNEL_DEV)); | |
| 607 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | |
| 608 IsAvailableInChannel("dev", VersionInfo::CHANNEL_BETA)); | |
| 609 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | |
| 610 IsAvailableInChannel("dev", VersionInfo::CHANNEL_STABLE)); | |
| 611 | |
| 612 // canary supported. | |
| 613 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 614 IsAvailableInChannel("canary", VersionInfo::CHANNEL_UNKNOWN)); | |
| 615 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 616 IsAvailableInChannel("canary", VersionInfo::CHANNEL_CANARY)); | |
| 617 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | |
| 618 IsAvailableInChannel("canary", VersionInfo::CHANNEL_DEV)); | |
| 619 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | |
| 620 IsAvailableInChannel("canary", VersionInfo::CHANNEL_BETA)); | |
| 621 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | |
| 622 IsAvailableInChannel("canary", VersionInfo::CHANNEL_STABLE)); | |
| 623 | |
| 624 // trunk supported. | |
| 625 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 626 IsAvailableInChannel("trunk", VersionInfo::CHANNEL_UNKNOWN)); | |
| 627 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | |
| 628 IsAvailableInChannel("trunk", VersionInfo::CHANNEL_CANARY)); | |
| 629 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | |
| 630 IsAvailableInChannel("trunk", VersionInfo::CHANNEL_DEV)); | |
| 631 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | |
| 632 IsAvailableInChannel("trunk", VersionInfo::CHANNEL_BETA)); | |
| 633 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | |
| 634 IsAvailableInChannel("trunk", VersionInfo::CHANNEL_STABLE)); | |
| 635 } | |
| 636 | |
| 637 } // namespace | |
| OLD | NEW |