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

Side by Side Diff: chrome/browser/extensions/api/certificate_provider/certificate_provider_apitest.cc

Issue 2094333002: Implementation for chrome.certificateProvider.requestPin/stopPinRequest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improved the browser tests Created 4 years, 1 month 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 <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
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(false);
148 EnterCode(service, base::ASCIIToUTF16("1234"));
149 ASSERT_TRUE(listener.WaitUntilSatisfied());
150 EXPECT_EQ("Success", listener.message());
151 }
152
153 // Enters an invalid code for extensions from local example folders, in the
154 // ShowPinDialog window and waits for the window to update with the error. The
155 // extension code is expected to send "Invalid PIN" message after the validation
156 // and the new requestPin (with the error) is done.
157 void EnterWrongPin(chromeos::CertificateProviderService* service) {
158 ExtensionTestMessageListener listener(false);
159 EnterCode(service, base::ASCIIToUTF16("567"));
160 ASSERT_TRUE(listener.WaitUntilSatisfied());
161 EXPECT_EQ("Invalid PIN", listener.message());
162
163 // Check that we have an error message displayed.
164 chromeos::RequestPinView* view =
165 service->pin_dialog_manager()->active_view_for_testing();
166 EXPECT_EQ(SK_ColorRED, view->error_label_for_testing()->enabled_color());
167 }
168
124 class CertificateProviderApiTest : public ExtensionApiTest { 169 class CertificateProviderApiTest : public ExtensionApiTest {
125 public: 170 public:
126 CertificateProviderApiTest() {} 171 CertificateProviderApiTest() {}
127 172
128 void SetUpInProcessBrowserTestFixture() override { 173 void SetUpInProcessBrowserTestFixture() override {
129 EXPECT_CALL(provider_, IsInitializationComplete(_)) 174 EXPECT_CALL(provider_, IsInitializationComplete(_))
130 .WillRepeatedly(Return(true)); 175 .WillRepeatedly(Return(true));
131 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); 176 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
132 177
133 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); 178 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
(...skipping 15 matching lines...) Expand all
149 nullptr); 194 nullptr);
150 provider_.UpdateChromePolicy(policy); 195 provider_.UpdateChromePolicy(policy);
151 196
152 content::RunAllPendingInMessageLoop(); 197 content::RunAllPendingInMessageLoop();
153 } 198 }
154 199
155 protected: 200 protected:
156 policy::MockConfigurationPolicyProvider provider_; 201 policy::MockConfigurationPolicyProvider provider_;
157 }; 202 };
158 203
204 class CertificateProviderRequestPinTest : public CertificateProviderApiTest {
205 public:
206 // Loads certificate_provider extension from |folder| and |file_name|.
207 // Returns the CertificateProviderService object from browser context.
208 chromeos::CertificateProviderService* LoadRequestPinExtension(
209 const std::string& folder,
210 const std::string& file_name) {
211 const base::FilePath extension_path =
212 test_data_dir_.AppendASCII("certificate_provider/" + folder);
213 const extensions::Extension* const extension =
214 LoadExtension(extension_path);
215 chromeos::CertificateProviderService* service =
216 chromeos::CertificateProviderServiceFactory::GetForBrowserContext(
217 profile());
218 service->pin_dialog_manager()->AddSignRequestId(extension->id(), 123);
219 ui_test_utils::NavigateToURL(browser(),
220 extension->GetResourceURL(file_name));
221 return service;
222 }
223 };
224
159 } // namespace 225 } // namespace
160 226
161 IN_PROC_BROWSER_TEST_F(CertificateProviderApiTest, Basic) { 227 IN_PROC_BROWSER_TEST_F(CertificateProviderApiTest, Basic) {
162 // Start an HTTPS test server that requests a client certificate. 228 // Start an HTTPS test server that requests a client certificate.
163 net::SpawnedTestServer::SSLOptions ssl_options; 229 net::SpawnedTestServer::SSLOptions ssl_options;
164 ssl_options.request_client_certificate = true; 230 ssl_options.request_client_certificate = true;
165 net::SpawnedTestServer https_server(net::SpawnedTestServer::TYPE_HTTPS, 231 net::SpawnedTestServer https_server(net::SpawnedTestServer::TYPE_HTTPS,
166 ssl_options, base::FilePath()); 232 ssl_options, base::FilePath());
167 ASSERT_TRUE(https_server.Start()); 233 ASSERT_TRUE(https_server.Start());
168 234
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 base::RunLoop run_loop; 326 base::RunLoop run_loop;
261 const std::string code = "replyWithSignatureSecondTime();"; 327 const std::string code = "replyWithSignatureSecondTime();";
262 bool result = false; 328 bool result = false;
263 extension_contents->GetMainFrame()->ExecuteJavaScriptForTests( 329 extension_contents->GetMainFrame()->ExecuteJavaScriptForTests(
264 base::ASCIIToUTF16(code), 330 base::ASCIIToUTF16(code),
265 base::Bind(&StoreBool, &result, run_loop.QuitClosure())); 331 base::Bind(&StoreBool, &result, run_loop.QuitClosure()));
266 run_loop.Run(); 332 run_loop.Run();
267 EXPECT_TRUE(result); 333 EXPECT_TRUE(result);
268 } 334 }
269 } 335 }
336
337 // User enters the correct PIN.
338 IN_PROC_BROWSER_TEST_F(CertificateProviderRequestPinTest, ShowPinDialogAccept) {
339 chromeos::CertificateProviderService* service =
340 LoadRequestPinExtension("request_pin", "basic.html");
341
342 // Enter the valid PIN.
343 EnterCorrectPin(service);
344
345 // The view should be set to nullptr when the window is closed.
346 EXPECT_EQ(service->pin_dialog_manager()->active_view_for_testing(), nullptr);
347 }
348
349 // User closes the dialog kMaxClosedDialogsPer10Mins times, and the extension
350 // should be blocked from showing it again.
351 IN_PROC_BROWSER_TEST_F(CertificateProviderRequestPinTest, ShowPinDialogClose) {
352 chromeos::CertificateProviderService* service =
353 LoadRequestPinExtension("request_pin", "basic.html");
354
355 views::Widget* window =
356 service->pin_dialog_manager()->active_window_for_testing();
357 for (int i = 0;
358 i < extensions::api::certificate_provider::kMaxClosedDialogsPer10Mins;
359 i++) {
360 ExtensionTestMessageListener listener(false);
361 window->Close();
362 ASSERT_TRUE(listener.WaitUntilSatisfied());
363 EXPECT_EQ("User closed the dialog", listener.message());
Devlin 2016/11/29 21:09:04 Here and below, you can pass an expected message t
igorcov 2016/11/30 15:32:38 Done.
364 window = service->pin_dialog_manager()->active_window_for_testing();
365 }
366
367 std::unique_ptr<ExtensionTestMessageListener> listener(
368 new ExtensionTestMessageListener(true));
369 window->Close();
370 ASSERT_TRUE(listener->WaitUntilSatisfied());
371 EXPECT_EQ("User closed the dialog", listener->message());
372 listener->Reply("GetLastError");
373 listener.reset(new ExtensionTestMessageListener(false));
Devlin 2016/11/29 21:09:04 nit: I'd just use two different listeners (stack-a
igorcov 2016/11/30 15:32:38 Done.
374 ASSERT_TRUE(listener->WaitUntilSatisfied());
375 EXPECT_EQ(
376 "This request exceeds the MAX_PIN_DIALOGS_CLOSED_PER_10_MINUTES quota.",
377 listener->message());
378 EXPECT_EQ(service->pin_dialog_manager()->active_view_for_testing(), nullptr);
379 }
380
381 // User enters a wrong PIN first and a correct PIN on the second try.
382 IN_PROC_BROWSER_TEST_F(CertificateProviderRequestPinTest,
383 ShowPinDialogWrongPin) {
384 chromeos::CertificateProviderService* service =
385 LoadRequestPinExtension("request_pin", "basic.html");
386 EnterWrongPin(service);
387
388 // The window should be active.
389 EXPECT_EQ(
390 service->pin_dialog_manager()->active_window_for_testing()->IsVisible(),
391 true);
392 EXPECT_NE(service->pin_dialog_manager()->active_view_for_testing(), nullptr);
393
394 // Enter the valid PIN.
395 EnterCorrectPin(service);
396
397 // The view should be set to nullptr when the window is closed.
398 EXPECT_EQ(service->pin_dialog_manager()->active_view_for_testing(), nullptr);
399 }
400
401 // User enters wrong PIN three times.
402 IN_PROC_BROWSER_TEST_F(CertificateProviderRequestPinTest,
403 ShowPinDialogWrongPinThreeTimes) {
404 chromeos::CertificateProviderService* service =
405 LoadRequestPinExtension("request_pin", "basic.html");
406 for (int i = 0; i < 3; i++) {
407 EnterWrongPin(service);
408 }
409
410 chromeos::RequestPinView* view =
411 service->pin_dialog_manager()->active_view_for_testing();
412
413 // The textfield has to be disabled, as extension does not allow input now.
414 EXPECT_EQ(view->textfield_for_testing()->enabled(), false);
415
416 // Close the dialog.
417 ExtensionTestMessageListener listener(false);
418 service->pin_dialog_manager()->active_window_for_testing()->Close();
419 ASSERT_TRUE(listener.WaitUntilSatisfied());
420 EXPECT_EQ("No attempt left", listener.message());
421 EXPECT_EQ(service->pin_dialog_manager()->active_view_for_testing(), nullptr);
422 }
423
424 // User closes the dialog while the extension is processing the request.
425 IN_PROC_BROWSER_TEST_F(CertificateProviderRequestPinTest,
426 ShowPinDialogCloseWhileProcessing) {
427 chromeos::CertificateProviderService* service =
428 LoadRequestPinExtension("request_pin", "basic_lock.html");
429
430 EnterCode(service, base::ASCIIToUTF16("123"));
431 service->pin_dialog_manager()->active_window_for_testing()->Close();
432 base::RunLoop().RunUntilIdle();
433
434 // The view should be set to nullptr when the window is closed.
435 EXPECT_EQ(service->pin_dialog_manager()->active_view_for_testing(), nullptr);
436 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698