| OLD | NEW |
| 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/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/file_path.h" | 6 #include "base/file_path.h" |
| 7 #include "base/scoped_temp_dir.h" | 7 #include "base/scoped_temp_dir.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "chrome/browser/extensions/api/management/management_api.h" | 10 #include "chrome/browser/extensions/api/management/management_api.h" |
| 11 #include "chrome/browser/extensions/api/management/management_api_constants.h" | 11 #include "chrome/browser/extensions/api/management/management_api_constants.h" |
| 12 #include "chrome/browser/extensions/extension_browsertest.h" | 12 #include "chrome/browser/extensions/extension_browsertest.h" |
| 13 #include "chrome/browser/extensions/extension_function_test_utils.h" | 13 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 14 #include "chrome/browser/extensions/extension_host.h" |
| 14 #include "chrome/browser/extensions/extension_install_dialog.h" | 15 #include "chrome/browser/extensions/extension_install_dialog.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/extension_test_message_listener.h" | 17 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
| 20 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/common/extensions/extension.h" |
| 23 #include "content/public/browser/render_process_host.h" |
| 24 #include "content/public/browser/render_view_host.h" |
| 25 #include "content/public/common/result_codes.h" |
| 21 | 26 |
| 22 namespace keys = extension_management_api_constants; | 27 namespace keys = extension_management_api_constants; |
| 23 namespace util = extension_function_test_utils; | 28 namespace util = extension_function_test_utils; |
| 24 | 29 |
| 25 class ExtensionManagementApiBrowserTest : public ExtensionBrowserTest {}; | 30 class ExtensionManagementApiBrowserTest : public ExtensionBrowserTest {}; |
| 26 | 31 |
| 27 // We test this here instead of in an ExtensionApiTest because normal extensions | 32 // We test this here instead of in an ExtensionApiTest because normal extensions |
| 28 // are not allowed to call the install function. | 33 // are not allowed to call the install function. |
| 29 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, InstallEvent) { | 34 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, InstallEvent) { |
| 30 ExtensionTestMessageListener listener1("ready", false); | 35 ExtensionTestMessageListener listener1("ready", false); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 EXPECT_TRUE(service->GetExtensionById(kId, false) != NULL); | 142 EXPECT_TRUE(service->GetExtensionById(kId, false) != NULL); |
| 138 | 143 |
| 139 // Update to a high-permission version - it should get disabled. | 144 // Update to a high-permission version - it should get disabled. |
| 140 EXPECT_FALSE(UpdateExtension(kId, path_v2, -1)); | 145 EXPECT_FALSE(UpdateExtension(kId, path_v2, -1)); |
| 141 EXPECT_TRUE(service->GetExtensionById(kId, false) == NULL); | 146 EXPECT_TRUE(service->GetExtensionById(kId, false) == NULL); |
| 142 EXPECT_TRUE(service->GetExtensionById(kId, true) != NULL); | 147 EXPECT_TRUE(service->GetExtensionById(kId, true) != NULL); |
| 143 EXPECT_TRUE( | 148 EXPECT_TRUE( |
| 144 service->extension_prefs()->DidExtensionEscalatePermissions(kId)); | 149 service->extension_prefs()->DidExtensionEscalatePermissions(kId)); |
| 145 } | 150 } |
| 146 | 151 |
| 147 void ReEnable(bool user_gesture, const std::string& expected_error) { | 152 void SetEnabled(bool enabled, bool user_gesture, |
| 153 const std::string& expected_error) { |
| 148 scoped_refptr<SetEnabledFunction> function(new SetEnabledFunction); | 154 scoped_refptr<SetEnabledFunction> function(new SetEnabledFunction); |
| 155 const char* enabled_string = enabled ? "true" : "false"; |
| 149 if (user_gesture) | 156 if (user_gesture) |
| 150 function->set_user_gesture(true); | 157 function->set_user_gesture(true); |
| 151 bool response = util::RunFunction( | 158 bool response = util::RunFunction( |
| 152 function.get(), | 159 function.get(), |
| 153 base::StringPrintf("[\"%s\", true]", kId), | 160 base::StringPrintf("[\"%s\", %s]", kId, enabled_string), |
| 154 browser(), | 161 browser(), |
| 155 util::NONE); | 162 util::NONE); |
| 156 if (expected_error.empty()) { | 163 if (expected_error.empty()) { |
| 157 EXPECT_EQ(true, response); | 164 EXPECT_EQ(true, response); |
| 158 } else { | 165 } else { |
| 159 EXPECT_TRUE(response == false); | 166 EXPECT_TRUE(response == false); |
| 160 EXPECT_EQ(expected_error, function->GetError()); | 167 EXPECT_EQ(expected_error, function->GetError()); |
| 161 } | 168 } |
| 162 } | 169 } |
| 163 | 170 |
| 171 bool CrashEnabledExtension(const std::string& extension_id) { |
| 172 const extensions::Extension* extension = browser()->profile()-> |
| 173 GetExtensionService()->GetExtensionById(extension_id, false); |
| 174 if (!extension) |
| 175 return false; |
| 176 extensions::ExtensionHost* extension_host = browser()->profile()-> |
| 177 GetExtensionProcessManager()->GetBackgroundHostForExtension( |
| 178 extension_id); |
| 179 if (!extension_host) |
| 180 return false; |
| 181 base::KillProcess( |
| 182 extension_host->render_view_host()->GetProcess()->GetHandle(), |
| 183 content::RESULT_CODE_KILLED, false); |
| 184 return WaitForExtensionCrash(extension_id); |
| 185 } |
| 186 |
| 164 private: | 187 private: |
| 165 ScopedTempDir scoped_temp_dir_; | 188 ScopedTempDir scoped_temp_dir_; |
| 166 }; | 189 }; |
| 167 | 190 |
| 168 const char ExtensionManagementApiEscalationTest::kId[] = | 191 const char ExtensionManagementApiEscalationTest::kId[] = |
| 169 "pgdpcfcocojkjfbgpiianjngphoopgmo"; | 192 "pgdpcfcocojkjfbgpiianjngphoopgmo"; |
| 170 | 193 |
| 171 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest, | 194 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest, |
| 172 DisabledReason) { | 195 DisabledReason) { |
| 173 scoped_refptr<GetExtensionByIdFunction> function = | 196 scoped_refptr<GetExtensionByIdFunction> function = |
| 174 new GetExtensionByIdFunction(); | 197 new GetExtensionByIdFunction(); |
| 175 scoped_ptr<base::Value> result(util::RunFunctionAndReturnSingleResult( | 198 scoped_ptr<base::Value> result(util::RunFunctionAndReturnSingleResult( |
| 176 function.get(), | 199 function.get(), |
| 177 base::StringPrintf("[\"%s\"]", kId), | 200 base::StringPrintf("[\"%s\"]", kId), |
| 178 browser())); | 201 browser())); |
| 179 ASSERT_TRUE(result.get() != NULL); | 202 ASSERT_TRUE(result.get() != NULL); |
| 180 ASSERT_TRUE(result->IsType(base::Value::TYPE_DICTIONARY)); | 203 ASSERT_TRUE(result->IsType(base::Value::TYPE_DICTIONARY)); |
| 181 base::DictionaryValue* dict = | 204 base::DictionaryValue* dict = |
| 182 static_cast<base::DictionaryValue*>(result.get()); | 205 static_cast<base::DictionaryValue*>(result.get()); |
| 183 std::string reason; | 206 std::string reason; |
| 184 EXPECT_TRUE(dict->GetStringASCII(keys::kDisabledReasonKey, &reason)); | 207 EXPECT_TRUE(dict->GetStringASCII(keys::kDisabledReasonKey, &reason)); |
| 185 EXPECT_EQ(reason, std::string(keys::kDisabledReasonPermissionsIncrease)); | 208 EXPECT_EQ(reason, std::string(keys::kDisabledReasonPermissionsIncrease)); |
| 186 } | 209 } |
| 187 | 210 |
| 188 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest, | 211 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest, |
| 189 ReEnable) { | 212 SetEnabled) { |
| 190 // Expect an error about no gesture. | 213 // Expect an error about no gesture. |
| 191 ReEnable(false, keys::kGestureNeededForEscalationError); | 214 SetEnabled(true, false, keys::kGestureNeededForEscalationError); |
| 192 | 215 |
| 193 // Expect an error that user cancelled the dialog. | 216 // Expect an error that user cancelled the dialog. |
| 194 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 217 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 195 switches::kAppsGalleryInstallAutoConfirmForTests, "cancel"); | 218 switches::kAppsGalleryInstallAutoConfirmForTests, "cancel"); |
| 196 ReEnable(true, keys::kUserDidNotReEnableError); | 219 SetEnabled(true, true, keys::kUserDidNotReEnableError); |
| 197 | 220 |
| 198 // This should succeed when user accepts dialog. | 221 // This should succeed when user accepts dialog. |
| 199 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 222 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 200 switches::kAppsGalleryInstallAutoConfirmForTests, "accept"); | 223 switches::kAppsGalleryInstallAutoConfirmForTests, "accept"); |
| 201 ReEnable(true, ""); | 224 SetEnabled(true, true, ""); |
| 225 |
| 226 // Crash the extension and try to enable. The extension should be reloaded and |
| 227 // enabled. |
| 228 ASSERT_TRUE(CrashEnabledExtension(kId)); |
| 229 SetEnabled(true, true, ""); |
| 230 |
| 231 // Crash the extension again and try to disable. The extension should be |
| 232 // reloaded and remain disabled. |
| 233 ASSERT_TRUE(CrashEnabledExtension(kId)); |
| 234 SetEnabled(false, true, ""); |
| 202 } | 235 } |
| OLD | NEW |