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