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

Side by Side Diff: content/browser/webauth/authenticator_impl_unittest.cc

Issue 2966523002: Blink-layer update to match WebAuthN spec (Closed)
Patch Set: Add ContectLifecycleObserver... part2 Created 3 years, 5 months 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "content/browser/webauth/authenticator_impl.h" 5 #include "content/browser/webauth/authenticator_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "content/public/browser/render_frame_host.h" 12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
14 #include "content/public/test/navigation_simulator.h" 14 #include "content/public/test/navigation_simulator.h"
15 #include "content/public/test/test_renderer_host.h" 15 #include "content/public/test/test_renderer_host.h"
16 #include "content/test/test_render_frame_host.h" 16 #include "content/test/test_render_frame_host.h"
17 #include "mojo/public/cpp/bindings/binding.h" 17 #include "mojo/public/cpp/bindings/binding.h"
18 #include "services/service_manager/public/cpp/bind_source_info.h" 18 #include "services/service_manager/public/cpp/bind_source_info.h"
19 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 namespace content { 22 namespace content {
23 23
24 using ::testing::_; 24 using ::testing::_;
25 25
26 using webauth::mojom::RelyingPartyAccount;
27 using webauth::mojom::ScopedCredentialOptions;
28 using webauth::mojom::ScopedCredentialParameters;
29 using webauth::mojom::AuthenticatorPtr; 26 using webauth::mojom::AuthenticatorPtr;
30 using webauth::mojom::AuthenticatorStatus; 27 using webauth::mojom::AuthenticatorStatus;
31 using webauth::mojom::RelyingPartyAccountPtr; 28 using webauth::mojom::MakeCredentialOptions;
32 using webauth::mojom::ScopedCredentialInfoPtr; 29 using webauth::mojom::MakeCredentialOptionsPtr;
33 using webauth::mojom::ScopedCredentialOptionsPtr; 30 using webauth::mojom::PublicKeyCredentialEntity;
34 using webauth::mojom::ScopedCredentialParametersPtr; 31 using webauth::mojom::PublicKeyCredentialEntityPtr;
32 using webauth::mojom::PublicKeyCredentialInfoPtr;
33 using webauth::mojom::PublicKeyCredentialParameters;
34 using webauth::mojom::PublicKeyCredentialParametersPtr;
35 35
36 const char* kOrigin1 = "https://google.com"; 36 const char* kOrigin1 = "https://google.com";
37 37
38 class AuthenticatorImplTest : public content::RenderViewHostTestHarness { 38 class AuthenticatorImplTest : public content::RenderViewHostTestHarness {
39 public: 39 public:
40 AuthenticatorImplTest() {} 40 AuthenticatorImplTest() {}
41 ~AuthenticatorImplTest() override {} 41 ~AuthenticatorImplTest() override {}
42 42
43 protected: 43 protected:
44 // Simulates navigating to a page and getting the page contents and language 44 // Simulates navigating to a page and getting the page contents and language
(...skipping 12 matching lines...) Expand all
57 }; 57 };
58 58
59 class TestMakeCredentialCallback { 59 class TestMakeCredentialCallback {
60 public: 60 public:
61 TestMakeCredentialCallback() 61 TestMakeCredentialCallback()
62 : callback_(base::Bind(&TestMakeCredentialCallback::ReceivedCallback, 62 : callback_(base::Bind(&TestMakeCredentialCallback::ReceivedCallback,
63 base::Unretained(this))) {} 63 base::Unretained(this))) {}
64 ~TestMakeCredentialCallback() {} 64 ~TestMakeCredentialCallback() {}
65 65
66 void ReceivedCallback(AuthenticatorStatus status, 66 void ReceivedCallback(AuthenticatorStatus status,
67 ScopedCredentialInfoPtr credential) { 67 PublicKeyCredentialInfoPtr credential) {
68 response_ = std::make_pair(status, std::move(credential)); 68 response_ = std::make_pair(status, std::move(credential));
69 closure_.Run(); 69 closure_.Run();
70 } 70 }
71 71
72 std::pair<AuthenticatorStatus, ScopedCredentialInfoPtr>& WaitForCallback() { 72 std::pair<AuthenticatorStatus, PublicKeyCredentialInfoPtr>&
73 WaitForCallback() {
73 closure_ = run_loop_.QuitClosure(); 74 closure_ = run_loop_.QuitClosure();
74 run_loop_.Run(); 75 run_loop_.Run();
75 return response_; 76 return response_;
76 } 77 }
77 78
78 const base::Callback<void(AuthenticatorStatus status, 79 const base::Callback<void(AuthenticatorStatus status,
79 ScopedCredentialInfoPtr credential)>& 80 PublicKeyCredentialInfoPtr credential)>&
80 callback() { 81 callback() {
81 return callback_; 82 return callback_;
82 } 83 }
83 84
84 private: 85 private:
85 std::pair<AuthenticatorStatus, ScopedCredentialInfoPtr> response_; 86 std::pair<AuthenticatorStatus, PublicKeyCredentialInfoPtr> response_;
86 base::Closure closure_; 87 base::Closure closure_;
87 base::Callback<void(AuthenticatorStatus status, 88 base::Callback<void(AuthenticatorStatus status,
88 ScopedCredentialInfoPtr credential)> 89 PublicKeyCredentialInfoPtr credential)>
89 callback_; 90 callback_;
90 base::RunLoop run_loop_; 91 base::RunLoop run_loop_;
91 }; 92 };
92 93
93 RelyingPartyAccountPtr GetTestRelyingPartyAccount() { 94 PublicKeyCredentialEntityPtr GetTestPublicKeyCredentialRPEntity() {
94 RelyingPartyAccountPtr account = RelyingPartyAccount::New(); 95 auto entity = PublicKeyCredentialEntity::New();
95 account->relying_party_display_name = std::string("TestRP"); 96 entity->id = std::string("localhost");
96 account->display_name = std::string("Test A. Name"); 97 entity->name = std::string("TestRP@example.com");
97 account->id = std::string("1098237235409872"); 98 return entity;
98 account->name = std::string("Testname@example.com");
99 account->image_url = std::string("fakeurl.png");
100 return account;
101 } 99 }
102 100
103 std::vector<ScopedCredentialParametersPtr> GetTestScopedCredentialParameters() { 101 PublicKeyCredentialEntityPtr GetTestPublicKeyCredentialUserEntity() {
104 std::vector<ScopedCredentialParametersPtr> parameters; 102 auto entity = PublicKeyCredentialEntity::New();
105 auto fake_parameter = ScopedCredentialParameters::New(); 103 entity->display_name = std::string("User A. Name");
106 fake_parameter->type = webauth::mojom::ScopedCredentialType::SCOPEDCRED; 104 entity->id = std::string("1098237235409872");
105 entity->name = std::string("TestRP@example.com");
106 entity->icon = GURL("fakeurl2.png");
107 return entity;
108 }
109
110 std::vector<PublicKeyCredentialParametersPtr>
111 GetTestPublicKeyCredentialParameters() {
112 std::vector<PublicKeyCredentialParametersPtr> parameters;
113 auto fake_parameter = PublicKeyCredentialParameters::New();
114 fake_parameter->type = webauth::mojom::PublicKeyCredentialType::PUBLIC_KEY;
107 parameters.push_back(std::move(fake_parameter)); 115 parameters.push_back(std::move(fake_parameter));
108 return parameters; 116 return parameters;
109 } 117 }
110 118
111 ScopedCredentialOptionsPtr GetTestScopedCredentialOptions() { 119 MakeCredentialOptionsPtr GetTestMakeCredentialOptions() {
112 ScopedCredentialOptionsPtr opts = ScopedCredentialOptions::New(); 120 auto opts = MakeCredentialOptions::New();
113 opts->adjusted_timeout = 60; 121 std::vector<uint8_t> buffer(32, 0x0A);
114 opts->relying_party_id = std::string("localhost"); 122 opts->relying_party = GetTestPublicKeyCredentialRPEntity();
123 opts->user = GetTestPublicKeyCredentialUserEntity();
124 opts->crypto_parameters = GetTestPublicKeyCredentialParameters();
125 opts->challenge = std::move(buffer);
126 opts->adjusted_timeout = base::TimeDelta::FromMinutes(1);
115 return opts; 127 return opts;
116 } 128 }
117 129
118 // Test that service returns NOT_IMPLEMENTED on a call to MakeCredential. 130 // Test that service returns NOT_IMPLEMENTED on a call to MakeCredential.
119 TEST_F(AuthenticatorImplTest, MakeCredentialNotImplemented) { 131 TEST_F(AuthenticatorImplTest, MakeCredentialNotImplemented) {
120 SimulateNavigation(GURL(kOrigin1)); 132 SimulateNavigation(GURL(kOrigin1));
121 AuthenticatorPtr authenticator = ConnectToAuthenticator(); 133 AuthenticatorPtr authenticator = ConnectToAuthenticator();
122 134 MakeCredentialOptionsPtr opts = GetTestMakeCredentialOptions();
123 RelyingPartyAccountPtr account = GetTestRelyingPartyAccount();
124
125 std::vector<ScopedCredentialParametersPtr> parameters =
126 GetTestScopedCredentialParameters();
127
128 std::vector<uint8_t> buffer(32, 0x0A);
129 ScopedCredentialOptionsPtr opts = GetTestScopedCredentialOptions();
130 135
131 TestMakeCredentialCallback cb; 136 TestMakeCredentialCallback cb;
132 authenticator->MakeCredential(std::move(account), std::move(parameters), 137 authenticator->MakeCredential(std::move(opts), cb.callback());
133 buffer, std::move(opts), cb.callback());
134 std::pair<webauth::mojom::AuthenticatorStatus, 138 std::pair<webauth::mojom::AuthenticatorStatus,
135 webauth::mojom::ScopedCredentialInfoPtr>& response = 139 webauth::mojom::PublicKeyCredentialInfoPtr>& response =
136 cb.WaitForCallback(); 140 cb.WaitForCallback();
137 EXPECT_EQ(webauth::mojom::AuthenticatorStatus::NOT_IMPLEMENTED, 141 EXPECT_EQ(webauth::mojom::AuthenticatorStatus::NOT_IMPLEMENTED,
138 response.first); 142 response.first);
139 } 143 }
140 144
141 // Test that service returns NOT_ALLOWED_ERROR on a call to MakeCredential with 145 // Test that service returns NOT_ALLOWED_ERROR on a call to MakeCredential with
142 // an opaque origin. 146 // an opaque origin.
143 TEST_F(AuthenticatorImplTest, MakeCredentialOpaqueOrigin) { 147 TEST_F(AuthenticatorImplTest, MakeCredentialOpaqueOrigin) {
144 NavigateAndCommit(GURL("data:text/html,opaque")); 148 NavigateAndCommit(GURL("data:text/html,opaque"));
145 AuthenticatorPtr authenticator = ConnectToAuthenticator(); 149 AuthenticatorPtr authenticator = ConnectToAuthenticator();
146 RelyingPartyAccountPtr account = GetTestRelyingPartyAccount();
147 150
148 std::vector<ScopedCredentialParametersPtr> parameters = 151 MakeCredentialOptionsPtr opts = GetTestMakeCredentialOptions();
149 GetTestScopedCredentialParameters();
150
151 std::vector<uint8_t> buffer(32, 0x0A);
152 ScopedCredentialOptionsPtr opts = GetTestScopedCredentialOptions();
153 152
154 TestMakeCredentialCallback cb; 153 TestMakeCredentialCallback cb;
155 authenticator->MakeCredential(std::move(account), std::move(parameters), 154 authenticator->MakeCredential(std::move(opts), cb.callback());
156 buffer, std::move(opts), cb.callback());
157 std::pair<webauth::mojom::AuthenticatorStatus, 155 std::pair<webauth::mojom::AuthenticatorStatus,
158 webauth::mojom::ScopedCredentialInfoPtr>& response = 156 webauth::mojom::PublicKeyCredentialInfoPtr>& response =
159 cb.WaitForCallback(); 157 cb.WaitForCallback();
160 EXPECT_EQ(webauth::mojom::AuthenticatorStatus::NOT_ALLOWED_ERROR, 158 EXPECT_EQ(webauth::mojom::AuthenticatorStatus::NOT_ALLOWED_ERROR,
161 response.first); 159 response.first);
162 } 160 }
163 } // namespace content 161 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698