OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/command_line.h" | |
6 #include "base/file_path.h" | |
7 #include "base/scoped_temp_dir.h" | |
8 #include "base/string_util.h" | |
9 #include "base/stringprintf.h" | |
10 #include "chrome/browser/extensions/extension_browsertest.h" | |
11 #include "chrome/browser/extensions/extension_function_test_utils.h" | |
12 #include "chrome/browser/extensions/extension_install_dialog.h" | |
13 #include "chrome/browser/extensions/extension_management_api.h" | |
14 #include "chrome/browser/extensions/extension_management_api_constants.h" | |
15 #include "chrome/browser/extensions/extension_service.h" | |
16 #include "chrome/browser/extensions/extension_test_message_listener.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/browser/ui/browser.h" | |
19 #include "chrome/common/chrome_notification_types.h" | |
20 #include "chrome/common/chrome_switches.h" | |
21 | |
22 namespace keys = extension_management_api_constants; | |
23 namespace util = extension_function_test_utils; | |
24 | |
25 class ExtensionManagementApiBrowserTest : public ExtensionBrowserTest {}; | |
26 | |
27 // We test this here instead of in an ExtensionApiTest because normal extensions | |
28 // are not allowed to call the install function. | |
29 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, InstallEvent) { | |
30 ExtensionTestMessageListener listener1("ready", false); | |
31 ASSERT_TRUE(LoadExtension( | |
32 test_data_dir_.AppendASCII("management/install_event"))); | |
33 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | |
34 | |
35 ExtensionTestMessageListener listener2("got_event", false); | |
36 ASSERT_TRUE(LoadExtension( | |
37 test_data_dir_.AppendASCII("api_test/management/enabled_extension"))); | |
38 ASSERT_TRUE(listener2.WaitUntilSatisfied()); | |
39 } | |
40 | |
41 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, LaunchApp) { | |
42 ExtensionTestMessageListener listener1("app_launched", false); | |
43 ExtensionTestMessageListener listener2("got_expected_error", false); | |
44 ASSERT_TRUE(LoadExtension( | |
45 test_data_dir_.AppendASCII("management/simple_extension"))); | |
46 ASSERT_TRUE(LoadExtension( | |
47 test_data_dir_.AppendASCII("management/packaged_app"))); | |
48 ASSERT_TRUE(LoadExtension( | |
49 test_data_dir_.AppendASCII("management/launch_app"))); | |
50 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | |
51 ASSERT_TRUE(listener2.WaitUntilSatisfied()); | |
52 } | |
53 | |
54 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, | |
55 LaunchAppFromBackground) { | |
56 ExtensionTestMessageListener listener1("success", false); | |
57 ASSERT_TRUE(LoadExtension( | |
58 test_data_dir_.AppendASCII("management/packaged_app"))); | |
59 ASSERT_TRUE(LoadExtension( | |
60 test_data_dir_.AppendASCII("management/launch_app_from_background"))); | |
61 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | |
62 } | |
63 | |
64 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, | |
65 SelfUninstall) { | |
66 ExtensionTestMessageListener listener1("success", false); | |
67 ASSERT_TRUE(LoadExtension( | |
68 test_data_dir_.AppendASCII("management/self_uninstall_helper"))); | |
69 ASSERT_TRUE(LoadExtension( | |
70 test_data_dir_.AppendASCII("management/self_uninstall"))); | |
71 ASSERT_TRUE(listener1.WaitUntilSatisfied()); | |
72 } | |
73 | |
74 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, | |
75 UninstallWithConfirmDialog) { | |
76 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
77 | |
78 // Install an extension. | |
79 const extensions::Extension* extension = InstallExtension( | |
80 test_data_dir_.AppendASCII("api_test/management/enabled_extension"), 1); | |
81 ASSERT_TRUE(extension); | |
82 | |
83 const std::string id = extension->id(); | |
84 | |
85 // Uninstall, then cancel via the confirm dialog. | |
86 scoped_refptr<UninstallFunction> uninstall_function(new UninstallFunction()); | |
87 UninstallFunction::SetAutoConfirmForTest(false); | |
88 | |
89 EXPECT_TRUE(MatchPattern( | |
90 util::RunFunctionAndReturnError( | |
91 uninstall_function, | |
92 base::StringPrintf("[\"%s\", {\"showConfirmDialog\": true}]", | |
93 id.c_str()), | |
94 browser()), | |
95 keys::kUninstallCanceledError)); | |
96 | |
97 // Make sure the extension wasn't uninstalled. | |
98 EXPECT_TRUE(service->GetExtensionById(id, false) != NULL); | |
99 | |
100 // Uninstall, then accept via the confirm dialog. | |
101 uninstall_function = new UninstallFunction(); | |
102 UninstallFunction::SetAutoConfirmForTest(true); | |
103 | |
104 util::RunFunctionAndReturnSingleResult( | |
105 uninstall_function, | |
106 base::StringPrintf("[\"%s\", {\"showConfirmDialog\": true}]", id.c_str()), | |
107 browser()); | |
108 | |
109 // Make sure the extension was uninstalled. | |
110 EXPECT_TRUE(service->GetExtensionById(id, false) == NULL); | |
111 } | |
112 | |
113 class ExtensionManagementApiEscalationTest : public ExtensionBrowserTest { | |
114 protected: | |
115 // The id of the permissions escalation test extension we use. | |
116 static const char kId[]; | |
117 | |
118 virtual void SetUpOnMainThread() OVERRIDE { | |
119 EXPECT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | |
120 FilePath pem_path = test_data_dir_. | |
121 AppendASCII("permissions_increase").AppendASCII("permissions.pem"); | |
122 FilePath path_v1 = PackExtensionWithOptions( | |
123 test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v1"), | |
124 scoped_temp_dir_.path().AppendASCII("permissions1.crx"), | |
125 pem_path, | |
126 FilePath()); | |
127 FilePath path_v2 = PackExtensionWithOptions( | |
128 test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v2"), | |
129 scoped_temp_dir_.path().AppendASCII("permissions2.crx"), | |
130 pem_path, | |
131 FilePath()); | |
132 | |
133 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
134 | |
135 // Install low-permission version of the extension. | |
136 ASSERT_TRUE(InstallExtension(path_v1, 1)); | |
137 EXPECT_TRUE(service->GetExtensionById(kId, false) != NULL); | |
138 | |
139 // Update to a high-permission version - it should get disabled. | |
140 EXPECT_FALSE(UpdateExtension(kId, path_v2, -1)); | |
141 EXPECT_TRUE(service->GetExtensionById(kId, false) == NULL); | |
142 EXPECT_TRUE(service->GetExtensionById(kId, true) != NULL); | |
143 EXPECT_TRUE( | |
144 service->extension_prefs()->DidExtensionEscalatePermissions(kId)); | |
145 } | |
146 | |
147 void ReEnable(bool user_gesture, const std::string& expected_error) { | |
148 scoped_refptr<SetEnabledFunction> function(new SetEnabledFunction); | |
149 if (user_gesture) | |
150 function->set_user_gesture(true); | |
151 bool response = util::RunFunction( | |
152 function.get(), | |
153 base::StringPrintf("[\"%s\", true]", kId), | |
154 browser(), | |
155 util::NONE); | |
156 if (expected_error.empty()) { | |
157 EXPECT_EQ(true, response); | |
158 } else { | |
159 EXPECT_TRUE(response == false); | |
160 EXPECT_EQ(expected_error, function->GetError()); | |
161 } | |
162 } | |
163 | |
164 private: | |
165 ScopedTempDir scoped_temp_dir_; | |
166 }; | |
167 | |
168 const char ExtensionManagementApiEscalationTest::kId[] = | |
169 "pgdpcfcocojkjfbgpiianjngphoopgmo"; | |
170 | |
171 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest, | |
172 DisabledReason) { | |
173 scoped_refptr<GetExtensionByIdFunction> function = | |
174 new GetExtensionByIdFunction(); | |
175 scoped_ptr<base::Value> result(util::RunFunctionAndReturnSingleResult( | |
176 function.get(), | |
177 base::StringPrintf("[\"%s\"]", kId), | |
178 browser())); | |
179 ASSERT_TRUE(result.get() != NULL); | |
180 ASSERT_TRUE(result->IsType(base::Value::TYPE_DICTIONARY)); | |
181 base::DictionaryValue* dict = | |
182 static_cast<base::DictionaryValue*>(result.get()); | |
183 std::string reason; | |
184 EXPECT_TRUE(dict->GetStringASCII(keys::kDisabledReasonKey, &reason)); | |
185 EXPECT_EQ(reason, std::string(keys::kDisabledReasonPermissionsIncrease)); | |
186 } | |
187 | |
188 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiEscalationTest, | |
189 ReEnable) { | |
190 // Expect an error about no gesture. | |
191 ReEnable(false, keys::kGestureNeededForEscalationError); | |
192 | |
193 // Expect an error that user cancelled the dialog. | |
194 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
195 switches::kAppsGalleryInstallAutoConfirmForTests, "cancel"); | |
196 ReEnable(true, keys::kUserDidNotReEnableError); | |
197 | |
198 // This should succeed when user accepts dialog. | |
199 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
200 switches::kAppsGalleryInstallAutoConfirmForTests, "accept"); | |
201 ReEnable(true, ""); | |
202 } | |
OLD | NEW |