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