OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/webauth/authenticator_impl.h" | |
6 | |
7 #include <stdint.h> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/run_loop.h" | |
12 #include "content/public/browser/render_frame_host.h" | |
13 #include "content/public/browser/web_contents.h" | |
14 #include "content/public/test/navigation_simulator.h" | |
15 #include "content/public/test/test_renderer_host.h" | |
16 #include "content/test/test_render_frame_host.h" | |
17 #include "mojo/public/cpp/bindings/binding.h" | |
18 #include "testing/gmock/include/gmock/gmock.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 using ::testing::_; | |
22 | |
23 namespace webauth { | |
24 | |
25 using mojom::RelyingPartyAccount; | |
26 using mojom::ScopedCredentialOptions; | |
27 using mojom::ScopedCredentialParameters; | |
28 using mojom::AuthenticatorPtr; | |
29 using mojom::AuthenticatorStatus; | |
30 using mojom::RelyingPartyAccountPtr; | |
31 using mojom::ScopedCredentialInfoPtr; | |
32 using mojom::ScopedCredentialOptionsPtr; | |
33 using mojom::ScopedCredentialParametersPtr; | |
34 | |
35 const char* kOrigin1 = "https://google.com"; | |
36 | |
37 class AuthenticatorImplTest : public content::RenderViewHostTestHarness { | |
38 public: | |
39 AuthenticatorImplTest() {} | |
40 ~AuthenticatorImplTest() override {} | |
41 | |
42 protected: | |
43 // Simulates navigating to a page and getting the page contents and language | |
44 // for that navigation. | |
45 void SimulateNavigation(const GURL& url) { | |
46 if (main_rfh()->GetLastCommittedURL() == url) | |
47 Reload(); | |
Mike West
2017/06/27 08:03:30
Why do you need the reload logic here? Would `Navi
| |
48 else | |
49 NavigateAndCommit(url); | |
50 } | |
51 | |
52 AuthenticatorPtr ConnectToAuthenticator() { | |
53 AuthenticatorPtr authenticator; | |
54 AuthenticatorImpl::Create(main_rfh(), mojo::MakeRequest(&authenticator)); | |
55 return authenticator; | |
56 } | |
57 | |
58 private: | |
59 void SetUp() override { content::RenderViewHostTestHarness::SetUp(); } | |
60 | |
61 void TearDown() override { content::RenderViewHostTestHarness::TearDown(); } | |
Mike West
2017/06/27 08:03:29
Do we need to define either of these? It looks lik
| |
62 }; | |
63 | |
64 class TestMakeCredentialCallback { | |
65 public: | |
66 TestMakeCredentialCallback() | |
67 : callback_(base::Bind(&TestMakeCredentialCallback::ReceivedCallback, | |
68 base::Unretained(this))) {} | |
69 ~TestMakeCredentialCallback() {} | |
70 | |
71 void ReceivedCallback(AuthenticatorStatus status, | |
72 ScopedCredentialInfoPtr credential) { | |
73 response_ = std::make_pair(status, std::move(credential)); | |
74 closure_.Run(); | |
75 } | |
76 | |
77 std::pair<AuthenticatorStatus, ScopedCredentialInfoPtr>& WaitForCallback() { | |
78 closure_ = run_loop_.QuitClosure(); | |
79 run_loop_.Run(); | |
80 return response_; | |
81 } | |
82 | |
83 const base::Callback<void(AuthenticatorStatus status, | |
84 ScopedCredentialInfoPtr credential)>& | |
85 callback() { | |
86 return callback_; | |
87 } | |
88 | |
89 private: | |
90 std::pair<AuthenticatorStatus, ScopedCredentialInfoPtr> response_; | |
91 base::Closure closure_; | |
92 base::Callback<void(AuthenticatorStatus status, | |
93 ScopedCredentialInfoPtr credential)> | |
94 callback_; | |
95 base::RunLoop run_loop_; | |
96 }; | |
97 | |
98 RelyingPartyAccountPtr GetTestRelyingPartyAccount() { | |
99 RelyingPartyAccountPtr account = RelyingPartyAccount::New(); | |
100 account->relying_party_display_name = std::string("TestRP"); | |
101 account->display_name = std::string("Test A. Name"); | |
102 account->id = std::string("1098237235409872"); | |
103 account->name = std::string("Testname@example.com"); | |
104 account->image_url = std::string("fakeurl.png"); | |
105 return account; | |
106 } | |
107 | |
108 std::vector<ScopedCredentialParametersPtr> GetTestScopedCredentialParameters() { | |
109 std::vector<ScopedCredentialParametersPtr> parameters; | |
110 auto fake_parameter = ScopedCredentialParameters::New(); | |
111 fake_parameter->type = mojom::ScopedCredentialType::SCOPEDCRED; | |
112 parameters.push_back(std::move(fake_parameter)); | |
113 return parameters; | |
114 } | |
115 | |
116 ScopedCredentialOptionsPtr GetTestScopedCredentialOptions() { | |
117 ScopedCredentialOptionsPtr opts = ScopedCredentialOptions::New(); | |
118 opts->adjusted_timeout = 60; | |
119 opts->relying_party_id = std::string("localhost"); | |
Mike West
2017/06/27 08:03:29
Note that this doesn't match either of the URLs te
| |
120 return opts; | |
121 } | |
122 | |
123 // Test that service returns NOT_IMPLEMENTED on a call to MakeCredential. | |
124 TEST_F(AuthenticatorImplTest, MakeCredentialNotImplemented) { | |
125 SimulateNavigation(GURL(kOrigin1)); | |
126 AuthenticatorPtr authenticator = ConnectToAuthenticator(); | |
127 | |
128 RelyingPartyAccountPtr account = GetTestRelyingPartyAccount(); | |
129 | |
130 std::vector<ScopedCredentialParametersPtr> parameters = | |
131 GetTestScopedCredentialParameters(); | |
132 | |
133 std::vector<uint8_t> buffer(32, 0x0A); | |
Mike West
2017/06/27 08:03:30
Why `0x0A`?
| |
134 ScopedCredentialOptionsPtr opts = GetTestScopedCredentialOptions(); | |
135 | |
136 TestMakeCredentialCallback cb; | |
137 authenticator->MakeCredential(std::move(account), std::move(parameters), | |
138 buffer, std::move(opts), cb.callback()); | |
139 std::pair<mojom::AuthenticatorStatus, mojom::ScopedCredentialInfoPtr>& | |
140 response = cb.WaitForCallback(); | |
141 EXPECT_EQ(mojom::AuthenticatorStatus::NOT_IMPLEMENTED, response.first); | |
142 } | |
143 | |
144 // Test that service returns NOT_ALLOWED_ERROR on a call to MakeCredential with | |
145 // an opaque origin. | |
146 TEST_F(AuthenticatorImplTest, MakeCredentialOpaqueOrigin) { | |
147 NavigateAndCommit(GURL(base::UTF8ToUTF16(""))); | |
Mike West
2017/06/27 08:03:29
Hrm. It kinda surprises me that navigation to an e
| |
148 AuthenticatorPtr authenticator = ConnectToAuthenticator(); | |
149 RelyingPartyAccountPtr account = GetTestRelyingPartyAccount(); | |
150 | |
151 std::vector<ScopedCredentialParametersPtr> parameters = | |
152 GetTestScopedCredentialParameters(); | |
153 | |
154 std::vector<uint8_t> buffer(32, 0x0A); | |
155 ScopedCredentialOptionsPtr opts = GetTestScopedCredentialOptions(); | |
156 | |
157 TestMakeCredentialCallback cb; | |
158 authenticator->MakeCredential(std::move(account), std::move(parameters), | |
159 buffer, std::move(opts), cb.callback()); | |
160 std::pair<mojom::AuthenticatorStatus, mojom::ScopedCredentialInfoPtr>& | |
161 response = cb.WaitForCallback(); | |
162 EXPECT_EQ(mojom::AuthenticatorStatus::NOT_ALLOWED_ERROR, response.first); | |
163 } | |
164 | |
165 // Test that service returns SECURITY_ERROR on call to MakeCredential if | |
166 // effectivedomain is empty. | |
Mike West
2017/06/27 08:03:29
Nit: Drop the comment, as I think we're now DCHECK
| |
167 } // namespace webauth | |
OLD | NEW |