OLD | NEW |
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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "chrome/browser/chromeos/certificate_provider/certificate_provider_serv
ice.h" |
| 22 #include "chrome/browser/chromeos/certificate_provider/certificate_provider_serv
ice_factory.h" |
| 23 #include "chrome/browser/extensions/api/certificate_provider/certificate_provide
r_api.h" |
21 #include "chrome/browser/extensions/extension_apitest.h" | 24 #include "chrome/browser/extensions/extension_apitest.h" |
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
23 #include "chrome/test/base/ui_test_utils.h" | 26 #include "chrome/test/base/ui_test_utils.h" |
24 #include "components/policy/core/browser/browser_policy_connector.h" | 27 #include "components/policy/core/browser/browser_policy_connector.h" |
25 #include "components/policy/core/common/mock_configuration_policy_provider.h" | 28 #include "components/policy/core/common/mock_configuration_policy_provider.h" |
26 #include "components/policy/core/common/policy_map.h" | 29 #include "components/policy/core/common/policy_map.h" |
27 #include "components/policy/core/common/policy_types.h" | 30 #include "components/policy/core/common/policy_types.h" |
28 #include "components/policy/policy_constants.h" | 31 #include "components/policy/policy_constants.h" |
29 #include "content/public/browser/render_frame_host.h" | 32 #include "content/public/browser/render_frame_host.h" |
30 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
31 #include "content/public/test/test_navigation_observer.h" | 34 #include "content/public/test/test_navigation_observer.h" |
32 #include "content/public/test/test_utils.h" | 35 #include "content/public/test/test_utils.h" |
33 #include "crypto/rsa_private_key.h" | 36 #include "crypto/rsa_private_key.h" |
34 #include "extensions/common/extension.h" | 37 #include "extensions/common/extension.h" |
| 38 #include "extensions/test/extension_test_message_listener.h" |
35 #include "extensions/test/result_catcher.h" | 39 #include "extensions/test/result_catcher.h" |
36 #include "net/test/spawned_test_server/spawned_test_server.h" | 40 #include "net/test/spawned_test_server/spawned_test_server.h" |
37 #include "testing/gmock/include/gmock/gmock.h" | 41 #include "testing/gmock/include/gmock/gmock.h" |
38 #include "third_party/boringssl/src/include/openssl/evp.h" | 42 #include "third_party/boringssl/src/include/openssl/evp.h" |
39 #include "third_party/boringssl/src/include/openssl/rsa.h" | 43 #include "third_party/boringssl/src/include/openssl/rsa.h" |
| 44 #include "ui/views/controls/label.h" |
| 45 #include "ui/views/controls/textfield/textfield.h" |
| 46 #include "ui/views/widget/widget.h" |
| 47 #include "ui/views/widget/widget_observer.h" |
40 | 48 |
41 using testing::Return; | 49 using testing::Return; |
42 using testing::_; | 50 using testing::_; |
43 | 51 |
44 namespace { | 52 namespace { |
45 | 53 |
46 void IgnoreResult(const base::Closure& callback, const base::Value* value) { | 54 void IgnoreResult(const base::Closure& callback, const base::Value* value) { |
47 callback.Run(); | 55 callback.Run(); |
48 } | 56 } |
49 | 57 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 std::string JsUint8Array(const std::vector<uint8_t>& bytes) { | 122 std::string JsUint8Array(const std::vector<uint8_t>& bytes) { |
115 std::string res = "new Uint8Array(["; | 123 std::string res = "new Uint8Array(["; |
116 for (const uint8_t byte : bytes) { | 124 for (const uint8_t byte : bytes) { |
117 res += base::UintToString(byte); | 125 res += base::UintToString(byte); |
118 res += ", "; | 126 res += ", "; |
119 } | 127 } |
120 res += "])"; | 128 res += "])"; |
121 return res; | 129 return res; |
122 } | 130 } |
123 | 131 |
| 132 // Enters the code in the ShowPinDialog window and pushes the OK event. |
| 133 void EnterCode(chromeos::CertificateProviderService* service, |
| 134 const base::string16& code) { |
| 135 chromeos::RequestPinView* view = |
| 136 service->pin_dialog_manager()->active_view_for_testing(); |
| 137 view->textfield_for_testing()->SetText(code); |
| 138 view->Accept(); |
| 139 base::RunLoop().RunUntilIdle(); |
| 140 } |
| 141 |
| 142 // Enters the valid code for extensions from local example folders, in the |
| 143 // ShowPinDialog window and waits for the window to close. The extension code |
| 144 // is expected to send "Success" message after the validation and request to |
| 145 // stopPinRequest is done. |
| 146 void EnterCorrectPin(chromeos::CertificateProviderService* service) { |
| 147 ExtensionTestMessageListener listener("Success", false); |
| 148 EnterCode(service, base::ASCIIToUTF16("1234")); |
| 149 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 150 } |
| 151 |
| 152 // Enters an invalid code for extensions from local example folders, in the |
| 153 // ShowPinDialog window and waits for the window to update with the error. The |
| 154 // extension code is expected to send "Invalid PIN" message after the validation |
| 155 // and the new requestPin (with the error) is done. |
| 156 void EnterWrongPin(chromeos::CertificateProviderService* service) { |
| 157 ExtensionTestMessageListener listener("Invalid PIN", false); |
| 158 EnterCode(service, base::ASCIIToUTF16("567")); |
| 159 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 160 |
| 161 // Check that we have an error message displayed. |
| 162 chromeos::RequestPinView* view = |
| 163 service->pin_dialog_manager()->active_view_for_testing(); |
| 164 EXPECT_EQ(SK_ColorRED, view->error_label_for_testing()->enabled_color()); |
| 165 } |
| 166 |
124 class CertificateProviderApiTest : public ExtensionApiTest { | 167 class CertificateProviderApiTest : public ExtensionApiTest { |
125 public: | 168 public: |
126 CertificateProviderApiTest() {} | 169 CertificateProviderApiTest() {} |
127 | 170 |
128 void SetUpInProcessBrowserTestFixture() override { | 171 void SetUpInProcessBrowserTestFixture() override { |
129 EXPECT_CALL(provider_, IsInitializationComplete(_)) | 172 EXPECT_CALL(provider_, IsInitializationComplete(_)) |
130 .WillRepeatedly(Return(true)); | 173 .WillRepeatedly(Return(true)); |
131 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | 174 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
132 | 175 |
133 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); | 176 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); |
(...skipping 16 matching lines...) Expand all Loading... |
150 nullptr); | 193 nullptr); |
151 provider_.UpdateChromePolicy(policy); | 194 provider_.UpdateChromePolicy(policy); |
152 | 195 |
153 content::RunAllPendingInMessageLoop(); | 196 content::RunAllPendingInMessageLoop(); |
154 } | 197 } |
155 | 198 |
156 protected: | 199 protected: |
157 policy::MockConfigurationPolicyProvider provider_; | 200 policy::MockConfigurationPolicyProvider provider_; |
158 }; | 201 }; |
159 | 202 |
| 203 class CertificateProviderRequestPinTest : public CertificateProviderApiTest { |
| 204 public: |
| 205 // Loads certificate_provider extension from |folder| and |file_name|. |
| 206 // Returns the CertificateProviderService object from browser context. |
| 207 chromeos::CertificateProviderService* LoadRequestPinExtension( |
| 208 const std::string& folder, |
| 209 const std::string& file_name) { |
| 210 const base::FilePath extension_path = |
| 211 test_data_dir_.AppendASCII("certificate_provider/" + folder); |
| 212 const extensions::Extension* const extension = |
| 213 LoadExtension(extension_path); |
| 214 chromeos::CertificateProviderService* service = |
| 215 chromeos::CertificateProviderServiceFactory::GetForBrowserContext( |
| 216 profile()); |
| 217 service->pin_dialog_manager()->AddSignRequestId(extension->id(), 123); |
| 218 ui_test_utils::NavigateToURL(browser(), |
| 219 extension->GetResourceURL(file_name)); |
| 220 return service; |
| 221 } |
| 222 }; |
| 223 |
160 } // namespace | 224 } // namespace |
161 | 225 |
162 IN_PROC_BROWSER_TEST_F(CertificateProviderApiTest, Basic) { | 226 IN_PROC_BROWSER_TEST_F(CertificateProviderApiTest, Basic) { |
163 // Start an HTTPS test server that requests a client certificate. | 227 // Start an HTTPS test server that requests a client certificate. |
164 net::SpawnedTestServer::SSLOptions ssl_options; | 228 net::SpawnedTestServer::SSLOptions ssl_options; |
165 ssl_options.request_client_certificate = true; | 229 ssl_options.request_client_certificate = true; |
166 net::SpawnedTestServer https_server(net::SpawnedTestServer::TYPE_HTTPS, | 230 net::SpawnedTestServer https_server(net::SpawnedTestServer::TYPE_HTTPS, |
167 ssl_options, base::FilePath()); | 231 ssl_options, base::FilePath()); |
168 ASSERT_TRUE(https_server.Start()); | 232 ASSERT_TRUE(https_server.Start()); |
169 | 233 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 base::RunLoop run_loop; | 325 base::RunLoop run_loop; |
262 const std::string code = "replyWithSignatureSecondTime();"; | 326 const std::string code = "replyWithSignatureSecondTime();"; |
263 bool result = false; | 327 bool result = false; |
264 extension_contents->GetMainFrame()->ExecuteJavaScriptForTests( | 328 extension_contents->GetMainFrame()->ExecuteJavaScriptForTests( |
265 base::ASCIIToUTF16(code), | 329 base::ASCIIToUTF16(code), |
266 base::Bind(&StoreBool, &result, run_loop.QuitClosure())); | 330 base::Bind(&StoreBool, &result, run_loop.QuitClosure())); |
267 run_loop.Run(); | 331 run_loop.Run(); |
268 EXPECT_TRUE(result); | 332 EXPECT_TRUE(result); |
269 } | 333 } |
270 } | 334 } |
| 335 |
| 336 // User enters the correct PIN. |
| 337 IN_PROC_BROWSER_TEST_F(CertificateProviderRequestPinTest, ShowPinDialogAccept) { |
| 338 chromeos::CertificateProviderService* service = |
| 339 LoadRequestPinExtension("request_pin", "basic.html"); |
| 340 |
| 341 // Enter the valid PIN. |
| 342 EnterCorrectPin(service); |
| 343 |
| 344 // The view should be set to nullptr when the window is closed. |
| 345 EXPECT_EQ(service->pin_dialog_manager()->active_view_for_testing(), nullptr); |
| 346 } |
| 347 |
| 348 // User closes the dialog kMaxClosedDialogsPer10Mins times, and the extension |
| 349 // should be blocked from showing it again. |
| 350 IN_PROC_BROWSER_TEST_F(CertificateProviderRequestPinTest, ShowPinDialogClose) { |
| 351 chromeos::CertificateProviderService* service = |
| 352 LoadRequestPinExtension("request_pin", "basic.html"); |
| 353 |
| 354 views::Widget* window = |
| 355 service->pin_dialog_manager()->active_window_for_testing(); |
| 356 for (int i = 0; |
| 357 i < extensions::api::certificate_provider::kMaxClosedDialogsPer10Mins; |
| 358 i++) { |
| 359 ExtensionTestMessageListener listener("User closed the dialog", false); |
| 360 window->Close(); |
| 361 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 362 window = service->pin_dialog_manager()->active_window_for_testing(); |
| 363 } |
| 364 |
| 365 ExtensionTestMessageListener close_listener("User closed the dialog", true); |
| 366 window->Close(); |
| 367 ASSERT_TRUE(close_listener.WaitUntilSatisfied()); |
| 368 close_listener.Reply("GetLastError"); |
| 369 ExtensionTestMessageListener last_error_listener( |
| 370 "This request exceeds the MAX_PIN_DIALOGS_CLOSED_PER_10_MINUTES quota.", |
| 371 false); |
| 372 ASSERT_TRUE(last_error_listener.WaitUntilSatisfied()); |
| 373 EXPECT_EQ(service->pin_dialog_manager()->active_view_for_testing(), nullptr); |
| 374 } |
| 375 |
| 376 // User enters a wrong PIN first and a correct PIN on the second try. |
| 377 IN_PROC_BROWSER_TEST_F(CertificateProviderRequestPinTest, |
| 378 ShowPinDialogWrongPin) { |
| 379 chromeos::CertificateProviderService* service = |
| 380 LoadRequestPinExtension("request_pin", "basic.html"); |
| 381 EnterWrongPin(service); |
| 382 |
| 383 // The window should be active. |
| 384 EXPECT_EQ( |
| 385 service->pin_dialog_manager()->active_window_for_testing()->IsVisible(), |
| 386 true); |
| 387 EXPECT_NE(service->pin_dialog_manager()->active_view_for_testing(), nullptr); |
| 388 |
| 389 // Enter the valid PIN. |
| 390 EnterCorrectPin(service); |
| 391 |
| 392 // The view should be set to nullptr when the window is closed. |
| 393 EXPECT_EQ(service->pin_dialog_manager()->active_view_for_testing(), nullptr); |
| 394 } |
| 395 |
| 396 // User enters wrong PIN three times. |
| 397 IN_PROC_BROWSER_TEST_F(CertificateProviderRequestPinTest, |
| 398 ShowPinDialogWrongPinThreeTimes) { |
| 399 chromeos::CertificateProviderService* service = |
| 400 LoadRequestPinExtension("request_pin", "basic.html"); |
| 401 for (int i = 0; i < 3; i++) { |
| 402 EnterWrongPin(service); |
| 403 } |
| 404 |
| 405 chromeos::RequestPinView* view = |
| 406 service->pin_dialog_manager()->active_view_for_testing(); |
| 407 |
| 408 // The textfield has to be disabled, as extension does not allow input now. |
| 409 EXPECT_EQ(view->textfield_for_testing()->enabled(), false); |
| 410 |
| 411 // Close the dialog. |
| 412 ExtensionTestMessageListener listener("No attempt left", false); |
| 413 service->pin_dialog_manager()->active_window_for_testing()->Close(); |
| 414 ASSERT_TRUE(listener.WaitUntilSatisfied()); |
| 415 EXPECT_EQ(service->pin_dialog_manager()->active_view_for_testing(), nullptr); |
| 416 } |
| 417 |
| 418 // User closes the dialog while the extension is processing the request. |
| 419 IN_PROC_BROWSER_TEST_F(CertificateProviderRequestPinTest, |
| 420 ShowPinDialogCloseWhileProcessing) { |
| 421 chromeos::CertificateProviderService* service = |
| 422 LoadRequestPinExtension("request_pin", "basic_lock.html"); |
| 423 |
| 424 EnterCode(service, base::ASCIIToUTF16("123")); |
| 425 service->pin_dialog_manager()->active_window_for_testing()->Close(); |
| 426 base::RunLoop().RunUntilIdle(); |
| 427 |
| 428 // The view should be set to nullptr when the window is closed. |
| 429 EXPECT_EQ(service->pin_dialog_manager()->active_view_for_testing(), nullptr); |
| 430 } |
OLD | NEW |