feature_cache_unittest.cc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2017 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. #include "extensions/renderer/feature_cache.h"
  5. #include "base/containers/contains.h"
  6. #include "components/crx_file/id_util.h"
  7. #include "content/public/test/test_utils.h"
  8. #include "extensions/common/extension.h"
  9. #include "extensions/common/extension_builder.h"
  10. #include "extensions/common/permissions/permissions_data.h"
  11. #include "extensions/common/value_builder.h"
  12. #include "extensions/renderer/bindings/api_binding_test.h"
  13. #include "extensions/renderer/scoped_web_frame.h"
  14. #include "extensions/renderer/script_context.h"
  15. #include "v8/include/v8.h"
  16. #include "third_party/blink/public/platform/web_data.h"
  17. #include "third_party/blink/public/platform/web_url.h"
  18. #include "third_party/blink/public/web/web_document.h"
  19. #include "third_party/blink/public/web/web_local_frame.h"
  20. namespace extensions {
  21. namespace {
  22. struct FakeContext {
  23. Feature::Context context_type;
  24. const Extension* extension;
  25. const GURL url;
  26. };
  27. bool HasFeature(FeatureCache& cache,
  28. const FakeContext& context,
  29. const std::string& feature) {
  30. return base::Contains(
  31. cache.GetAvailableFeatures(context.context_type, context.extension,
  32. context.url),
  33. feature);
  34. }
  35. } // namespace
  36. using FeatureCacheTest = testing::Test;
  37. TEST_F(FeatureCacheTest, Basic) {
  38. FeatureCache cache;
  39. scoped_refptr<const Extension> extension_a = ExtensionBuilder("a").Build();
  40. scoped_refptr<const Extension> extension_b =
  41. ExtensionBuilder("b").AddPermission("storage").Build();
  42. FakeContext context_a = {Feature::BLESSED_EXTENSION_CONTEXT,
  43. extension_a.get(), extension_a->url()};
  44. FakeContext context_b = {Feature::BLESSED_EXTENSION_CONTEXT,
  45. extension_b.get(), extension_b->url()};
  46. // To start, context a should not have access to storage, but context b
  47. // should.
  48. EXPECT_FALSE(HasFeature(cache, context_a, "storage"));
  49. EXPECT_TRUE(HasFeature(cache, context_b, "storage"));
  50. // Update extension b's permissions and invalidate the cache.
  51. extension_b->permissions_data()->SetPermissions(
  52. std::make_unique<PermissionSet>(), std::make_unique<PermissionSet>());
  53. cache.InvalidateExtension(extension_b->id());
  54. // Now, neither context should have storage access.
  55. EXPECT_FALSE(HasFeature(cache, context_a, "storage"));
  56. EXPECT_FALSE(HasFeature(cache, context_b, "storage"));
  57. }
  58. TEST_F(FeatureCacheTest, WebUIContexts) {
  59. FeatureCache cache;
  60. scoped_refptr<const Extension> extension_a = ExtensionBuilder("a").Build();
  61. // The chrome://extensions page is allowlisted for the management API.
  62. FakeContext webui_context = {Feature::WEBUI_CONTEXT, nullptr,
  63. content::GetWebUIURL("extensions")};
  64. // chrome://baz is not allowlisted, and should not have access.
  65. FakeContext webui_context_without_access = {Feature::WEBUI_CONTEXT, nullptr,
  66. content::GetWebUIURL("baz")};
  67. EXPECT_TRUE(HasFeature(cache, webui_context, "management"));
  68. EXPECT_FALSE(HasFeature(cache, webui_context_without_access, "management"));
  69. // No webui context is allowlisted for, e.g., the idle API, so neither should
  70. // have access.
  71. EXPECT_FALSE(HasFeature(cache, webui_context, "idle"));
  72. EXPECT_FALSE(HasFeature(cache, webui_context_without_access, "idle"));
  73. }
  74. } // namespace extensions