blob: fc5351be75cfa7e53c83698549b7ee9e0ce3fa0f [file] [log] [blame]
[email protected]76e9211f2014-08-22 16:34:101// 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]76e9211f2014-08-22 16:34:106#include "base/path_service.h"
7#include "chrome/common/chrome_paths.h"
Gabriel Charettec7108742019-08-23 03:31:408#include "content/public/test/browser_task_environment.h"
[email protected]76e9211f2014-08-22 16:34:109#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]76e9211f2014-08-22 16:34:1015namespace extensions {
16namespace {
17
18scoped_refptr<Extension> LoadManifest(const std::string& dir,
19 const std::string& test_file) {
20 base::FilePath path;
Avi Drissman9098f9002018-05-04 00:11:5221 base::PathService::Get(chrome::DIR_TEST_DATA, &path);
[email protected]76e9211f2014-08-22 16:34:1022 path = path.AppendASCII("extensions").AppendASCII(dir).AppendASCII(test_file);
23
prashhir54a994502015-03-05 09:30:5724 JSONFileValueDeserializer deserializer(path);
dchengc963c7142016-04-08 03:55:2225 std::unique_ptr<base::Value> result = deserializer.Deserialize(NULL, NULL);
[email protected]76e9211f2014-08-22 16:34:1026 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.
45class ChromeInfoMapTest : public testing::Test {
46 public:
fdoray2ce6dc222017-04-27 14:39:3947 ChromeInfoMapTest() = default;
[email protected]76e9211f2014-08-22 16:34:1048
49 private:
Gabriel Charette798fde72019-08-20 22:24:0450 content::BrowserTaskEnvironment task_environment_;
[email protected]76e9211f2014-08-22 16:34:1051};
52
53// Tests API access permissions given both extension and app URLs.
54TEST_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
99TEST_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