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

Side by Side Diff: chrome/browser/extensions/api/developer_private/developer_private_api_unittest.cc

Issue 958803004: [Extensions] Make chrome://extensions use developerPrivate for packing crxs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/files/file_util.h" 5 #include "base/files/file_util.h"
6 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h" 6 #include "chrome/browser/extensions/api/developer_private/developer_private_api. h"
7 #include "chrome/browser/extensions/extension_function_test_utils.h" 7 #include "chrome/browser/extensions/extension_function_test_utils.h"
8 #include "chrome/browser/extensions/extension_service.h" 8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_service_test_base.h" 9 #include "chrome/browser/extensions/extension_service_test_base.h"
10 #include "chrome/browser/extensions/extension_util.h" 10 #include "chrome/browser/extensions/extension_util.h"
11 #include "chrome/browser/extensions/test_extension_dir.h" 11 #include "chrome/browser/extensions/test_extension_dir.h"
12 #include "chrome/browser/extensions/unpacked_installer.h" 12 #include "chrome/browser/extensions/unpacked_installer.h"
13 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/host_desktop.h" 14 #include "chrome/browser/ui/host_desktop.h"
15 #include "chrome/common/extensions/api/developer_private.h"
15 #include "chrome/test/base/test_browser_window.h" 16 #include "chrome/test/base/test_browser_window.h"
16 #include "extensions/browser/extension_prefs.h" 17 #include "extensions/browser/extension_prefs.h"
17 #include "extensions/browser/extension_registry.h" 18 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/extension_system.h" 19 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/test_extension_registry_observer.h" 20 #include "extensions/browser/test_extension_registry_observer.h"
20 #include "extensions/common/extension.h" 21 #include "extensions/common/extension.h"
21 #include "extensions/common/extension_set.h" 22 #include "extensions/common/extension_set.h"
22 #include "extensions/common/test_util.h" 23 #include "extensions/common/test_util.h"
23 24
24 namespace extensions { 25 namespace extensions {
(...skipping 10 matching lines...) Expand all
35 36
36 // Loads an unpacked extension that is backed by a real directory, allowing 37 // Loads an unpacked extension that is backed by a real directory, allowing
37 // it to be reloaded. 38 // it to be reloaded.
38 const Extension* LoadUnpackedExtension(); 39 const Extension* LoadUnpackedExtension();
39 40
40 // Tests a developer private function that sets an extension pref. 41 // Tests a developer private function that sets an extension pref.
41 void TestExtensionPrefSetting( 42 void TestExtensionPrefSetting(
42 UIThreadExtensionFunction* (*create_function)(), 43 UIThreadExtensionFunction* (*create_function)(),
43 bool (*has_pref)(const std::string&, content::BrowserContext*)); 44 bool (*has_pref)(const std::string&, content::BrowserContext*));
44 45
46 testing::AssertionResult TestPackExtensionFunction(
47 const base::ListValue& args,
48 api::developer_private::PackStatus expected_status,
49 int expected_flags);
50
45 Browser* browser() { return browser_.get(); } 51 Browser* browser() { return browser_.get(); }
46 52
47 private: 53 private:
48 // ExtensionServiceTestBase: 54 // ExtensionServiceTestBase:
49 void SetUp() override; 55 void SetUp() override;
50 void TearDown() override; 56 void TearDown() override;
51 57
52 // The browser (and accompanying window). 58 // The browser (and accompanying window).
53 scoped_ptr<TestBrowserWindow> browser_window_; 59 scoped_ptr<TestBrowserWindow> browser_window_;
54 scoped_ptr<Browser> browser_; 60 scoped_ptr<Browser> browser_;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 EXPECT_TRUE(has_pref(extension_id, profile())); 132 EXPECT_TRUE(has_pref(extension_id, profile()));
127 133
128 base::ListValue disable_args; 134 base::ListValue disable_args;
129 disable_args.AppendString(extension_id); 135 disable_args.AppendString(extension_id);
130 disable_args.AppendBoolean(false); 136 disable_args.AppendBoolean(false);
131 function = create_function(); 137 function = create_function();
132 EXPECT_TRUE(RunFunction(function, disable_args)); 138 EXPECT_TRUE(RunFunction(function, disable_args));
133 EXPECT_FALSE(has_pref(extension_id, profile())); 139 EXPECT_FALSE(has_pref(extension_id, profile()));
134 } 140 }
135 141
142 testing::AssertionResult DeveloperPrivateApiUnitTest::TestPackExtensionFunction(
143 const base::ListValue& args,
144 api::developer_private::PackStatus expected_status,
145 int expected_flags) {
146 scoped_refptr<UIThreadExtensionFunction> function(
147 new api::DeveloperPrivatePackDirectoryFunction());
148 if (!RunFunction(function, args))
149 return testing::AssertionFailure() << "Could not run function.";
150
151 // Extract the result. We don't have to test this here, since it's verified as
152 // part of the general extension api system.
153 const base::Value* response_value = nullptr;
154 CHECK(function->GetResultList()->Get(0u, &response_value));
155 scoped_ptr<api::developer_private::PackDirectoryResponse> response =
156 api::developer_private::PackDirectoryResponse::FromValue(*response_value);
157 CHECK(response);
158
159 if (response->status != expected_status) {
160 return testing::AssertionFailure() << "Expected status: " <<
161 expected_status << ", found status: " << response->status <<
162 ", message: " << response->message;
163 }
164
165 if (response->override_flags != expected_flags) {
166 return testing::AssertionFailure() << "Expected flags: " <<
167 expected_flags << ", found flags: " << response->override_flags;
168 }
169
170 return testing::AssertionSuccess();
171 }
172
136 void DeveloperPrivateApiUnitTest::SetUp() { 173 void DeveloperPrivateApiUnitTest::SetUp() {
137 ExtensionServiceTestBase::SetUp(); 174 ExtensionServiceTestBase::SetUp();
138 InitializeEmptyExtensionService(); 175 InitializeEmptyExtensionService();
139 176
140 browser_window_.reset(new TestBrowserWindow()); 177 browser_window_.reset(new TestBrowserWindow());
141 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE); 178 Browser::CreateParams params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE);
142 params.type = Browser::TYPE_TABBED; 179 params.type = Browser::TYPE_TABBED;
143 params.window = browser_window_.get(); 180 params.window = browser_window_.get();
144 browser_.reset(new Browser(params)); 181 browser_.reset(new Browser(params));
145 } 182 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 return static_cast<UIThreadExtensionFunction*>( 224 return static_cast<UIThreadExtensionFunction*>(
188 new api::DeveloperPrivateAllowFileAccessFunction()); 225 new api::DeveloperPrivateAllowFileAccessFunction());
189 }; 226 };
190 auto has_pref = [](const std::string& extension_id, 227 auto has_pref = [](const std::string& extension_id,
191 content::BrowserContext* browser_context) { 228 content::BrowserContext* browser_context) {
192 return util::AllowFileAccess(extension_id, browser_context); 229 return util::AllowFileAccess(extension_id, browser_context);
193 }; 230 };
194 TestExtensionPrefSetting(+create_function, +has_pref); 231 TestExtensionPrefSetting(+create_function, +has_pref);
195 } 232 }
196 233
234 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivatePackFunction) {
235 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT);
236
237 base::FilePath root_path = data_dir().AppendASCII("good_unpacked");
238 base::FilePath crx_path = data_dir().AppendASCII("good_unpacked.crx");
239 base::FilePath pem_path = data_dir().AppendASCII("good_unpacked.pem");
240
241 // First, test a directory that should pack properly.
242 base::ListValue pack_args;
243 pack_args.AppendString(root_path.AsUTF8Unsafe());
244 EXPECT_TRUE(TestPackExtensionFunction(
245 pack_args, api::developer_private::PACK_STATUS_SUCCESS, 0));
246
247 // Should have created crx file and pem file.
248 EXPECT_TRUE(base::PathExists(crx_path));
249 EXPECT_TRUE(base::PathExists(pem_path));
250
251 // Deliberately don't cleanup the files, and append the pem path.
252 pack_args.AppendString(pem_path.AsUTF8Unsafe());
253
254 // Try to pack again - we should get a warning abot overwriting the crx.
255 EXPECT_TRUE(TestPackExtensionFunction(
256 pack_args,
257 api::developer_private::PACK_STATUS_WARNING,
258 ExtensionCreator::kOverwriteCRX));
259
260 // Try to pack again, with the overwrite flag; this should succeed.
261 pack_args.AppendInteger(ExtensionCreator::kOverwriteCRX);
262 EXPECT_TRUE(TestPackExtensionFunction(
263 pack_args, api::developer_private::PACK_STATUS_SUCCESS, 0));
264
265 // Try to pack a final time when omitting (an existing) pem file. We should
266 // get an error.
267 base::DeleteFile(crx_path, false);
268 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the pem key argument.
269 EXPECT_TRUE(pack_args.Remove(1u, nullptr)); // Remove the flags argument.
270 EXPECT_TRUE(TestPackExtensionFunction(
271 pack_args, api::developer_private::PACK_STATUS_ERROR, 0));
272
273 base::DeleteFile(crx_path, false);
274 base::DeleteFile(pem_path, false);
275 }
276
197 } // namespace extensions 277 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698