OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/json/json_file_value_serializer.h" | |
6 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
7 #include "base/path_service.h" | 6 #include "base/path_service.h" |
8 #include "chrome/common/chrome_paths.h" | |
9 #include "content/public/test/test_browser_thread.h" | 7 #include "content/public/test/test_browser_thread.h" |
10 #include "extensions/browser/info_map.h" | 8 #include "extensions/browser/info_map.h" |
11 #include "extensions/common/extension.h" | 9 #include "extensions/common/extension.h" |
| 10 #include "extensions/common/extension_paths.h" |
12 #include "extensions/common/manifest_constants.h" | 11 #include "extensions/common/manifest_constants.h" |
13 #include "extensions/common/permissions/permissions_data.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
15 | 13 |
16 using content::BrowserThread; | 14 using content::BrowserThread; |
17 | 15 |
18 namespace keys = extensions::manifest_keys; | 16 namespace keys = extensions::manifest_keys; |
19 | 17 |
20 namespace extensions { | 18 namespace extensions { |
21 | 19 |
22 class InfoMapTest : public testing::Test { | 20 class InfoMapTest : public testing::Test { |
23 public: | 21 public: |
24 InfoMapTest() | 22 InfoMapTest() |
25 : ui_thread_(BrowserThread::UI, &message_loop_), | 23 : ui_thread_(BrowserThread::UI, &message_loop_), |
26 io_thread_(BrowserThread::IO, &message_loop_) {} | 24 io_thread_(BrowserThread::IO, &message_loop_) {} |
27 | 25 |
28 private: | 26 private: |
29 base::MessageLoop message_loop_; | 27 base::MessageLoop message_loop_; |
30 content::TestBrowserThread ui_thread_; | 28 content::TestBrowserThread ui_thread_; |
31 content::TestBrowserThread io_thread_; | 29 content::TestBrowserThread io_thread_; |
32 }; | 30 }; |
33 | 31 |
34 // Returns a barebones test Extension object with the given name. | 32 // Returns a barebones test Extension object with the given name. |
35 static scoped_refptr<Extension> CreateExtension(const std::string& name) { | 33 static scoped_refptr<Extension> CreateExtension(const std::string& name) { |
36 base::FilePath path; | 34 base::FilePath path; |
37 PathService::Get(chrome::DIR_TEST_DATA, &path); | 35 PathService::Get(DIR_TEST_DATA, &path); |
38 path = path.AppendASCII("extensions"); | |
39 | 36 |
40 base::DictionaryValue manifest; | 37 base::DictionaryValue manifest; |
41 manifest.SetString(keys::kVersion, "1.0.0.0"); | 38 manifest.SetString(keys::kVersion, "1.0.0.0"); |
42 manifest.SetString(keys::kName, name); | 39 manifest.SetString(keys::kName, name); |
43 | 40 |
44 std::string error; | 41 std::string error; |
45 scoped_refptr<Extension> extension = | 42 scoped_refptr<Extension> extension = |
46 Extension::Create(path.AppendASCII(name), | 43 Extension::Create(path.AppendASCII(name), |
47 Manifest::INVALID_LOCATION, | 44 Manifest::INVALID_LOCATION, |
48 manifest, | 45 manifest, |
49 Extension::NO_FLAGS, | 46 Extension::NO_FLAGS, |
50 &error); | 47 &error); |
51 EXPECT_TRUE(extension.get()) << error; | 48 EXPECT_TRUE(extension.get()) << error; |
52 | 49 |
53 return extension; | 50 return extension; |
54 } | 51 } |
55 | 52 |
56 static scoped_refptr<Extension> LoadManifest(const std::string& dir, | |
57 const std::string& test_file) { | |
58 base::FilePath path; | |
59 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
60 path = path.AppendASCII("extensions").AppendASCII(dir).AppendASCII(test_file); | |
61 | |
62 JSONFileValueSerializer serializer(path); | |
63 scoped_ptr<base::Value> result(serializer.Deserialize(NULL, NULL)); | |
64 if (!result) | |
65 return NULL; | |
66 | |
67 std::string error; | |
68 scoped_refptr<Extension> extension = | |
69 Extension::Create(path, | |
70 Manifest::INVALID_LOCATION, | |
71 *static_cast<base::DictionaryValue*>(result.get()), | |
72 Extension::NO_FLAGS, | |
73 &error); | |
74 EXPECT_TRUE(extension.get()) << error; | |
75 | |
76 return extension; | |
77 } | |
78 | |
79 // Test that the InfoMap handles refcounting properly. | 53 // Test that the InfoMap handles refcounting properly. |
80 TEST_F(InfoMapTest, RefCounting) { | 54 TEST_F(InfoMapTest, RefCounting) { |
81 scoped_refptr<InfoMap> info_map(new InfoMap()); | 55 scoped_refptr<InfoMap> info_map(new InfoMap()); |
82 | 56 |
83 // New extensions should have a single reference holding onto them. | 57 // New extensions should have a single reference holding onto them. |
84 scoped_refptr<Extension> extension1(CreateExtension("extension1")); | 58 scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
85 scoped_refptr<Extension> extension2(CreateExtension("extension2")); | 59 scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
86 scoped_refptr<Extension> extension3(CreateExtension("extension3")); | 60 scoped_refptr<Extension> extension3(CreateExtension("extension3")); |
87 EXPECT_TRUE(extension1->HasOneRef()); | 61 EXPECT_TRUE(extension1->HasOneRef()); |
88 EXPECT_TRUE(extension2->HasOneRef()); | 62 EXPECT_TRUE(extension2->HasOneRef()); |
(...skipping 27 matching lines...) Expand all Loading... |
116 scoped_refptr<Extension> extension2(CreateExtension("extension2")); | 90 scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
117 | 91 |
118 info_map->AddExtension(extension1.get(), base::Time(), false, false); | 92 info_map->AddExtension(extension1.get(), base::Time(), false, false); |
119 info_map->AddExtension(extension2.get(), base::Time(), false, false); | 93 info_map->AddExtension(extension2.get(), base::Time(), false, false); |
120 | 94 |
121 EXPECT_EQ(2u, info_map->extensions().size()); | 95 EXPECT_EQ(2u, info_map->extensions().size()); |
122 EXPECT_EQ(extension1.get(), info_map->extensions().GetByID(extension1->id())); | 96 EXPECT_EQ(extension1.get(), info_map->extensions().GetByID(extension1->id())); |
123 EXPECT_EQ(extension2.get(), info_map->extensions().GetByID(extension2->id())); | 97 EXPECT_EQ(extension2.get(), info_map->extensions().GetByID(extension2->id())); |
124 } | 98 } |
125 | 99 |
126 // Tests CheckURLAccessToExtensionPermission given both extension and app URLs. | |
127 TEST_F(InfoMapTest, CheckPermissions) { | |
128 scoped_refptr<InfoMap> info_map(new InfoMap()); | |
129 | |
130 scoped_refptr<Extension> app( | |
131 LoadManifest("manifest_tests", "valid_app.json")); | |
132 scoped_refptr<Extension> extension( | |
133 LoadManifest("manifest_tests", "tabs_extension.json")); | |
134 | |
135 GURL app_url("http://www.google.com/mail/foo.html"); | |
136 ASSERT_TRUE(app->is_app()); | |
137 ASSERT_TRUE(app->web_extent().MatchesURL(app_url)); | |
138 | |
139 info_map->AddExtension(app.get(), base::Time(), false, false); | |
140 info_map->AddExtension(extension.get(), base::Time(), false, false); | |
141 | |
142 // The app should have the notifications permission, either from a | |
143 // chrome-extension URL or from its web extent. | |
144 const Extension* match = info_map->extensions().GetExtensionOrAppByURL( | |
145 app->GetResourceURL("a.html")); | |
146 EXPECT_TRUE(match && | |
147 match->permissions_data()->HasAPIPermission( | |
148 APIPermission::kNotifications)); | |
149 match = info_map->extensions().GetExtensionOrAppByURL(app_url); | |
150 EXPECT_TRUE(match && | |
151 match->permissions_data()->HasAPIPermission( | |
152 APIPermission::kNotifications)); | |
153 EXPECT_FALSE( | |
154 match && | |
155 match->permissions_data()->HasAPIPermission(APIPermission::kTab)); | |
156 | |
157 // The extension should have the tabs permission. | |
158 match = info_map->extensions().GetExtensionOrAppByURL( | |
159 extension->GetResourceURL("a.html")); | |
160 EXPECT_TRUE(match && | |
161 match->permissions_data()->HasAPIPermission(APIPermission::kTab)); | |
162 EXPECT_FALSE(match && | |
163 match->permissions_data()->HasAPIPermission( | |
164 APIPermission::kNotifications)); | |
165 | |
166 // Random URL should not have any permissions. | |
167 GURL evil_url("http://evil.com/a.html"); | |
168 match = info_map->extensions().GetExtensionOrAppByURL(evil_url); | |
169 EXPECT_FALSE(match); | |
170 } | |
171 | |
172 TEST_F(InfoMapTest, TestNotificationsDisabled) { | |
173 scoped_refptr<InfoMap> info_map(new InfoMap()); | |
174 scoped_refptr<Extension> app(LoadManifest("manifest_tests", | |
175 "valid_app.json")); | |
176 info_map->AddExtension(app.get(), base::Time(), false, false); | |
177 | |
178 EXPECT_FALSE(info_map->AreNotificationsDisabled(app->id())); | |
179 info_map->SetNotificationsDisabled(app->id(), true); | |
180 EXPECT_TRUE(info_map->AreNotificationsDisabled(app->id())); | |
181 info_map->SetNotificationsDisabled(app->id(), false); | |
182 } | |
183 | |
184 // Tests that extension URLs are properly mapped to local file paths. | 100 // Tests that extension URLs are properly mapped to local file paths. |
185 TEST_F(InfoMapTest, MapUrlToLocalFilePath) { | 101 TEST_F(InfoMapTest, MapUrlToLocalFilePath) { |
186 scoped_refptr<InfoMap> info_map(new InfoMap()); | 102 scoped_refptr<InfoMap> info_map(new InfoMap()); |
187 scoped_refptr<Extension> app(CreateExtension("app")); | 103 scoped_refptr<Extension> app(CreateExtension("platform_app")); |
188 info_map->AddExtension(app.get(), base::Time(), false, false); | 104 info_map->AddExtension(app.get(), base::Time(), false, false); |
189 | 105 |
190 // Non-extension URLs don't map to anything. | 106 // Non-extension URLs don't map to anything. |
191 base::FilePath non_extension_path; | 107 base::FilePath non_extension_path; |
192 GURL non_extension_url("http://not-an-extension.com/"); | 108 GURL non_extension_url("http://not-an-extension.com/"); |
193 EXPECT_FALSE(info_map->MapUrlToLocalFilePath( | 109 EXPECT_FALSE(info_map->MapUrlToLocalFilePath( |
194 non_extension_url, false, &non_extension_path)); | 110 non_extension_url, false, &non_extension_path)); |
195 EXPECT_TRUE(non_extension_path.empty()); | 111 EXPECT_TRUE(non_extension_path.empty()); |
196 | 112 |
197 // Valid resources return a valid path. | 113 // Valid resources return a valid path. |
(...skipping 11 matching lines...) Expand all Loading... |
209 EXPECT_TRUE(does_not_exist_path.empty()); | 125 EXPECT_TRUE(does_not_exist_path.empty()); |
210 | 126 |
211 // A file does not need to exist to be mapped to a path with the non-blocking | 127 // A file does not need to exist to be mapped to a path with the non-blocking |
212 // API. This avoids hitting the disk to see if it exists. | 128 // API. This avoids hitting the disk to see if it exists. |
213 EXPECT_TRUE(info_map->MapUrlToLocalFilePath( | 129 EXPECT_TRUE(info_map->MapUrlToLocalFilePath( |
214 does_not_exist_url, false /* use_blocking_api */, &does_not_exist_path)); | 130 does_not_exist_url, false /* use_blocking_api */, &does_not_exist_path)); |
215 EXPECT_FALSE(does_not_exist_path.empty()); | 131 EXPECT_FALSE(does_not_exist_path.empty()); |
216 } | 132 } |
217 | 133 |
218 } // namespace extensions | 134 } // namespace extensions |
OLD | NEW |