[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 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 "base/json/json_file_value_serializer.h" |
[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 6 | #include "base/path_service.h" |
| 7 | #include "chrome/common/chrome_paths.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 8 | #include "content/public/test/browser_task_environment.h" |
[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 9 | #include "extensions/browser/info_map.h" |
| 10 | #include "extensions/common/extension.h" |
| 11 | #include "extensions/common/manifest_constants.h" |
| 12 | #include "extensions/common/permissions/permissions_data.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 15 | namespace extensions { |
| 16 | namespace { |
| 17 | |
| 18 | scoped_refptr<Extension> LoadManifest(const std::string& dir, |
| 19 | const std::string& test_file) { |
| 20 | base::FilePath path; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 21 | base::PathService::Get(chrome::DIR_TEST_DATA, &path); |
[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 22 | path = path.AppendASCII("extensions").AppendASCII(dir).AppendASCII(test_file); |
| 23 | |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 24 | JSONFileValueDeserializer deserializer(path); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 25 | std::unique_ptr<base::Value> result = deserializer.Deserialize(NULL, NULL); |
[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 26 | if (!result) |
| 27 | return NULL; |
| 28 | |
| 29 | std::string error; |
| 30 | scoped_refptr<Extension> extension = |
| 31 | Extension::Create(path, |
| 32 | Manifest::INVALID_LOCATION, |
| 33 | *static_cast<base::DictionaryValue*>(result.get()), |
| 34 | Extension::NO_FLAGS, |
| 35 | &error); |
| 36 | EXPECT_TRUE(extension.get()) << error; |
| 37 | |
| 38 | return extension; |
| 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
| 43 | // This test lives in Chrome because it depends on hosted app permissions |
| 44 | // (specifically, notifications) that do not exist in src/extensions. |
| 45 | class ChromeInfoMapTest : public testing::Test { |
| 46 | public: |
fdoray | 2ce6dc22 | 2017-04-27 14:39:39 | [diff] [blame] | 47 | ChromeInfoMapTest() = default; |
[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 48 | |
| 49 | private: |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 50 | content::BrowserTaskEnvironment task_environment_; |
[email protected] | 76e9211f | 2014-08-22 16:34:10 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | // Tests API access permissions given both extension and app URLs. |
| 54 | TEST_F(ChromeInfoMapTest, CheckPermissions) { |
| 55 | scoped_refptr<InfoMap> info_map(new InfoMap()); |
| 56 | |
| 57 | scoped_refptr<Extension> app( |
| 58 | LoadManifest("manifest_tests", "valid_app.json")); |
| 59 | scoped_refptr<Extension> extension( |
| 60 | LoadManifest("manifest_tests", "tabs_extension.json")); |
| 61 | |
| 62 | GURL app_url("http://www.google.com/mail/foo.html"); |
| 63 | ASSERT_TRUE(app->is_app()); |
| 64 | ASSERT_TRUE(app->web_extent().MatchesURL(app_url)); |
| 65 | |
| 66 | info_map->AddExtension(app.get(), base::Time(), false, false); |
| 67 | info_map->AddExtension(extension.get(), base::Time(), false, false); |
| 68 | |
| 69 | // The app should have the notifications permission, either from a |
| 70 | // chrome-extension URL or from its web extent. |
| 71 | const Extension* match = info_map->extensions().GetExtensionOrAppByURL( |
| 72 | app->GetResourceURL("a.html")); |
| 73 | EXPECT_TRUE(match && |
| 74 | match->permissions_data()->HasAPIPermission( |
| 75 | APIPermission::kNotifications)); |
| 76 | match = info_map->extensions().GetExtensionOrAppByURL(app_url); |
| 77 | EXPECT_TRUE(match && |
| 78 | match->permissions_data()->HasAPIPermission( |
| 79 | APIPermission::kNotifications)); |
| 80 | EXPECT_FALSE( |
| 81 | match && |
| 82 | match->permissions_data()->HasAPIPermission(APIPermission::kTab)); |
| 83 | |
| 84 | // The extension should have the tabs permission. |
| 85 | match = info_map->extensions().GetExtensionOrAppByURL( |
| 86 | extension->GetResourceURL("a.html")); |
| 87 | EXPECT_TRUE(match && |
| 88 | match->permissions_data()->HasAPIPermission(APIPermission::kTab)); |
| 89 | EXPECT_FALSE(match && |
| 90 | match->permissions_data()->HasAPIPermission( |
| 91 | APIPermission::kNotifications)); |
| 92 | |
| 93 | // Random URL should not have any permissions. |
| 94 | GURL evil_url("http://evil.com/a.html"); |
| 95 | match = info_map->extensions().GetExtensionOrAppByURL(evil_url); |
| 96 | EXPECT_FALSE(match); |
| 97 | } |
| 98 | |
| 99 | TEST_F(ChromeInfoMapTest, TestNotificationsDisabled) { |
| 100 | scoped_refptr<InfoMap> info_map(new InfoMap()); |
| 101 | scoped_refptr<Extension> app( |
| 102 | LoadManifest("manifest_tests", "valid_app.json")); |
| 103 | info_map->AddExtension(app.get(), base::Time(), false, false); |
| 104 | |
| 105 | EXPECT_FALSE(info_map->AreNotificationsDisabled(app->id())); |
| 106 | info_map->SetNotificationsDisabled(app->id(), true); |
| 107 | EXPECT_TRUE(info_map->AreNotificationsDisabled(app->id())); |
| 108 | info_map->SetNotificationsDisabled(app->id(), false); |
| 109 | } |
| 110 | |
| 111 | } // namespace extensions |