Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: extensions/browser/info_map_unittest.cc

Issue 63933003: Moved ExtensionInfoMap and ExtensionsQuotaService to extensions/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment fix Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « extensions/browser/info_map.cc ('k') | extensions/browser/quota_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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" 5 #include "base/json/json_file_value_serializer.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "chrome/browser/extensions/extension_info_map.h"
9 #include "chrome/common/chrome_paths.h" 8 #include "chrome/common/chrome_paths.h"
10 #include "chrome/common/extensions/extension.h" 9 #include "chrome/common/extensions/extension.h"
11 #include "content/public/test/test_browser_thread.h" 10 #include "content/public/test/test_browser_thread.h"
11 #include "extensions/browser/info_map.h"
12 #include "extensions/common/manifest_constants.h" 12 #include "extensions/common/manifest_constants.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 using content::BrowserThread; 15 using content::BrowserThread;
16 using extensions::APIPermission;
17 using extensions::Extension;
18 using extensions::Manifest;
19 16
20 namespace keys = extensions::manifest_keys; 17 namespace keys = extensions::manifest_keys;
21 18
22 namespace { 19 namespace extensions {
23 20
24 class ExtensionInfoMapTest : public testing::Test { 21 class InfoMapTest : public testing::Test {
25 public: 22 public:
26 ExtensionInfoMapTest() 23 InfoMapTest()
27 : ui_thread_(BrowserThread::UI, &message_loop_), 24 : ui_thread_(BrowserThread::UI, &message_loop_),
28 io_thread_(BrowserThread::IO, &message_loop_) { 25 io_thread_(BrowserThread::IO, &message_loop_) {}
29 }
30 26
31 private: 27 private:
32 base::MessageLoop message_loop_; 28 base::MessageLoop message_loop_;
33 content::TestBrowserThread ui_thread_; 29 content::TestBrowserThread ui_thread_;
34 content::TestBrowserThread io_thread_; 30 content::TestBrowserThread io_thread_;
35 }; 31 };
36 32
37 // Returns a barebones test Extension object with the given name. 33 // Returns a barebones test Extension object with the given name.
38 static scoped_refptr<Extension> CreateExtension(const std::string& name) { 34 static scoped_refptr<Extension> CreateExtension(const std::string& name) {
39 #if defined(OS_WIN) 35 #if defined(OS_WIN)
40 base::FilePath path(FILE_PATH_LITERAL("c:\\foo")); 36 base::FilePath path(FILE_PATH_LITERAL("c:\\foo"));
41 #elif defined(OS_POSIX) 37 #elif defined(OS_POSIX)
42 base::FilePath path(FILE_PATH_LITERAL("/foo")); 38 base::FilePath path(FILE_PATH_LITERAL("/foo"));
43 #endif 39 #endif
44 40
45 DictionaryValue manifest; 41 DictionaryValue manifest;
46 manifest.SetString(keys::kVersion, "1.0.0.0"); 42 manifest.SetString(keys::kVersion, "1.0.0.0");
47 manifest.SetString(keys::kName, name); 43 manifest.SetString(keys::kName, name);
48 44
49 std::string error; 45 std::string error;
50 scoped_refptr<Extension> extension = Extension::Create( 46 scoped_refptr<Extension> extension =
51 path.AppendASCII(name), Manifest::INVALID_LOCATION, manifest, 47 Extension::Create(path.AppendASCII(name),
52 Extension::NO_FLAGS, &error); 48 Manifest::INVALID_LOCATION,
49 manifest,
50 Extension::NO_FLAGS,
51 &error);
53 EXPECT_TRUE(extension.get()) << error; 52 EXPECT_TRUE(extension.get()) << error;
54 53
55 return extension; 54 return extension;
56 } 55 }
57 56
58 static scoped_refptr<Extension> LoadManifest(const std::string& dir, 57 static scoped_refptr<Extension> LoadManifest(const std::string& dir,
59 const std::string& test_file) { 58 const std::string& test_file) {
60 base::FilePath path; 59 base::FilePath path;
61 PathService::Get(chrome::DIR_TEST_DATA, &path); 60 PathService::Get(chrome::DIR_TEST_DATA, &path);
62 path = path.AppendASCII("extensions") 61 path = path.AppendASCII("extensions").AppendASCII(dir).AppendASCII(test_file);
63 .AppendASCII(dir)
64 .AppendASCII(test_file);
65 62
66 JSONFileValueSerializer serializer(path); 63 JSONFileValueSerializer serializer(path);
67 scoped_ptr<Value> result(serializer.Deserialize(NULL, NULL)); 64 scoped_ptr<Value> result(serializer.Deserialize(NULL, NULL));
68 if (!result) 65 if (!result)
69 return NULL; 66 return NULL;
70 67
71 std::string error; 68 std::string error;
72 scoped_refptr<Extension> extension = Extension::Create( 69 scoped_refptr<Extension> extension =
73 path, Manifest::INVALID_LOCATION, 70 Extension::Create(path,
74 *static_cast<DictionaryValue*>(result.get()), 71 Manifest::INVALID_LOCATION,
75 Extension::NO_FLAGS, &error); 72 *static_cast<DictionaryValue*>(result.get()),
73 Extension::NO_FLAGS,
74 &error);
76 EXPECT_TRUE(extension.get()) << error; 75 EXPECT_TRUE(extension.get()) << error;
77 76
78 return extension; 77 return extension;
79 } 78 }
80 79
81 // Test that the ExtensionInfoMap handles refcounting properly. 80 // Test that the InfoMap handles refcounting properly.
82 TEST_F(ExtensionInfoMapTest, RefCounting) { 81 TEST_F(InfoMapTest, RefCounting) {
83 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); 82 scoped_refptr<InfoMap> info_map(new InfoMap());
84 83
85 // New extensions should have a single reference holding onto them. 84 // New extensions should have a single reference holding onto them.
86 scoped_refptr<Extension> extension1(CreateExtension("extension1")); 85 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
87 scoped_refptr<Extension> extension2(CreateExtension("extension2")); 86 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
88 scoped_refptr<Extension> extension3(CreateExtension("extension3")); 87 scoped_refptr<Extension> extension3(CreateExtension("extension3"));
89 EXPECT_TRUE(extension1->HasOneRef()); 88 EXPECT_TRUE(extension1->HasOneRef());
90 EXPECT_TRUE(extension2->HasOneRef()); 89 EXPECT_TRUE(extension2->HasOneRef());
91 EXPECT_TRUE(extension3->HasOneRef()); 90 EXPECT_TRUE(extension3->HasOneRef());
92 91
93 // Add a ref to each extension and give it to the info map. 92 // Add a ref to each extension and give it to the info map.
94 info_map->AddExtension(extension1.get(), base::Time(), false); 93 info_map->AddExtension(extension1.get(), base::Time(), false);
95 info_map->AddExtension(extension2.get(), base::Time(), false); 94 info_map->AddExtension(extension2.get(), base::Time(), false);
96 info_map->AddExtension(extension3.get(), base::Time(), false); 95 info_map->AddExtension(extension3.get(), base::Time(), false);
97 96
98 // Release extension1, and the info map should have the only ref. 97 // Release extension1, and the info map should have the only ref.
99 const Extension* weak_extension1 = extension1.get(); 98 const Extension* weak_extension1 = extension1.get();
100 extension1 = NULL; 99 extension1 = NULL;
101 EXPECT_TRUE(weak_extension1->HasOneRef()); 100 EXPECT_TRUE(weak_extension1->HasOneRef());
102 101
103 // Remove extension2, and the extension2 object should have the only ref. 102 // Remove extension2, and the extension2 object should have the only ref.
104 info_map->RemoveExtension( 103 info_map->RemoveExtension(
105 extension2->id(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL); 104 extension2->id(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL);
106 EXPECT_TRUE(extension2->HasOneRef()); 105 EXPECT_TRUE(extension2->HasOneRef());
107 106
108 // Delete the info map, and the extension3 object should have the only ref. 107 // Delete the info map, and the extension3 object should have the only ref.
109 info_map = NULL; 108 info_map = NULL;
110 EXPECT_TRUE(extension3->HasOneRef()); 109 EXPECT_TRUE(extension3->HasOneRef());
111 } 110 }
112 111
113 // Tests that we can query a few extension properties from the ExtensionInfoMap. 112 // Tests that we can query a few extension properties from the InfoMap.
114 TEST_F(ExtensionInfoMapTest, Properties) { 113 TEST_F(InfoMapTest, Properties) {
115 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); 114 scoped_refptr<InfoMap> info_map(new InfoMap());
116 115
117 scoped_refptr<Extension> extension1(CreateExtension("extension1")); 116 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
118 scoped_refptr<Extension> extension2(CreateExtension("extension2")); 117 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
119 118
120 info_map->AddExtension(extension1.get(), base::Time(), false); 119 info_map->AddExtension(extension1.get(), base::Time(), false);
121 info_map->AddExtension(extension2.get(), base::Time(), false); 120 info_map->AddExtension(extension2.get(), base::Time(), false);
122 121
123 EXPECT_EQ(2u, info_map->extensions().size()); 122 EXPECT_EQ(2u, info_map->extensions().size());
124 EXPECT_EQ(extension1.get(), info_map->extensions().GetByID(extension1->id())); 123 EXPECT_EQ(extension1.get(), info_map->extensions().GetByID(extension1->id()));
125 EXPECT_EQ(extension2.get(), info_map->extensions().GetByID(extension2->id())); 124 EXPECT_EQ(extension2.get(), info_map->extensions().GetByID(extension2->id()));
126 } 125 }
127 126
128 // Tests CheckURLAccessToExtensionPermission given both extension and app URLs. 127 // Tests CheckURLAccessToExtensionPermission given both extension and app URLs.
129 TEST_F(ExtensionInfoMapTest, CheckPermissions) { 128 TEST_F(InfoMapTest, CheckPermissions) {
130 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); 129 scoped_refptr<InfoMap> info_map(new InfoMap());
131 130
132 scoped_refptr<Extension> app(LoadManifest("manifest_tests", 131 scoped_refptr<Extension> app(
133 "valid_app.json")); 132 LoadManifest("manifest_tests", "valid_app.json"));
134 scoped_refptr<Extension> extension(LoadManifest("manifest_tests", 133 scoped_refptr<Extension> extension(
135 "tabs_extension.json")); 134 LoadManifest("manifest_tests", "tabs_extension.json"));
136 135
137 GURL app_url("http://www.google.com/mail/foo.html"); 136 GURL app_url("http://www.google.com/mail/foo.html");
138 ASSERT_TRUE(app->is_app()); 137 ASSERT_TRUE(app->is_app());
139 ASSERT_TRUE(app->web_extent().MatchesURL(app_url)); 138 ASSERT_TRUE(app->web_extent().MatchesURL(app_url));
140 139
141 info_map->AddExtension(app.get(), base::Time(), false); 140 info_map->AddExtension(app.get(), base::Time(), false);
142 info_map->AddExtension(extension.get(), base::Time(), false); 141 info_map->AddExtension(extension.get(), base::Time(), false);
143 142
144 // The app should have the notifications permission, either from a 143 // The app should have the notifications permission, either from a
145 // chrome-extension URL or from its web extent. 144 // chrome-extension URL or from its web extent.
146 const Extension* match = info_map->extensions().GetExtensionOrAppByURL( 145 const Extension* match = info_map->extensions().GetExtensionOrAppByURL(
147 app->GetResourceURL("a.html")); 146 app->GetResourceURL("a.html"));
148 EXPECT_TRUE(match && 147 EXPECT_TRUE(match && match->HasAPIPermission(APIPermission::kNotification));
149 match->HasAPIPermission(APIPermission::kNotification));
150 match = info_map->extensions().GetExtensionOrAppByURL(app_url); 148 match = info_map->extensions().GetExtensionOrAppByURL(app_url);
151 EXPECT_TRUE(match && 149 EXPECT_TRUE(match && match->HasAPIPermission(APIPermission::kNotification));
152 match->HasAPIPermission(APIPermission::kNotification)); 150 EXPECT_FALSE(match && match->HasAPIPermission(APIPermission::kTab));
153 EXPECT_FALSE(match &&
154 match->HasAPIPermission(APIPermission::kTab));
155 151
156 // The extension should have the tabs permission. 152 // The extension should have the tabs permission.
157 match = info_map->extensions().GetExtensionOrAppByURL( 153 match = info_map->extensions().GetExtensionOrAppByURL(
158 extension->GetResourceURL("a.html")); 154 extension->GetResourceURL("a.html"));
159 EXPECT_TRUE(match && 155 EXPECT_TRUE(match && match->HasAPIPermission(APIPermission::kTab));
160 match->HasAPIPermission(APIPermission::kTab)); 156 EXPECT_FALSE(match && match->HasAPIPermission(APIPermission::kNotification));
161 EXPECT_FALSE(match &&
162 match->HasAPIPermission(APIPermission::kNotification));
163 157
164 // Random URL should not have any permissions. 158 // Random URL should not have any permissions.
165 GURL evil_url("http://evil.com/a.html"); 159 GURL evil_url("http://evil.com/a.html");
166 match = info_map->extensions().GetExtensionOrAppByURL(evil_url); 160 match = info_map->extensions().GetExtensionOrAppByURL(evil_url);
167 EXPECT_FALSE(match); 161 EXPECT_FALSE(match);
168 } 162 }
169 163
170 } // namespace 164 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/info_map.cc ('k') | extensions/browser/quota_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698