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 "content/browser/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 "services/service_manager/public/cpp/bind_source_info.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace content { |
| 23 |
| 24 using ::testing::_; |
| 25 |
| 26 using webauth::mojom::RelyingPartyAccount; |
| 27 using webauth::mojom::ScopedCredentialOptions; |
| 28 using webauth::mojom::ScopedCredentialParameters; |
| 29 using webauth::mojom::AuthenticatorPtr; |
| 30 using webauth::mojom::AuthenticatorStatus; |
| 31 using webauth::mojom::RelyingPartyAccountPtr; |
| 32 using webauth::mojom::ScopedCredentialInfoPtr; |
| 33 using webauth::mojom::ScopedCredentialOptionsPtr; |
| 34 using webauth::mojom::ScopedCredentialParametersPtr; |
| 35 |
| 36 const char* kOrigin1 = "https://google.com"; |
| 37 |
| 38 class AuthenticatorImplTest : public content::RenderViewHostTestHarness { |
| 39 public: |
| 40 AuthenticatorImplTest() {} |
| 41 ~AuthenticatorImplTest() override {} |
| 42 |
| 43 protected: |
| 44 // Simulates navigating to a page and getting the page contents and language |
| 45 // for that navigation. |
| 46 void SimulateNavigation(const GURL& url) { |
| 47 if (main_rfh()->GetLastCommittedURL() != url) |
| 48 NavigateAndCommit(url); |
| 49 } |
| 50 |
| 51 AuthenticatorPtr ConnectToAuthenticator() { |
| 52 AuthenticatorPtr authenticator; |
| 53 AuthenticatorImpl::Create(main_rfh(), service_manager::BindSourceInfo(), |
| 54 mojo::MakeRequest(&authenticator)); |
| 55 return authenticator; |
| 56 } |
| 57 }; |
| 58 |
| 59 class TestMakeCredentialCallback { |
| 60 public: |
| 61 TestMakeCredentialCallback() |
| 62 : callback_(base::Bind(&TestMakeCredentialCallback::ReceivedCallback, |
| 63 base::Unretained(this))) {} |
| 64 ~TestMakeCredentialCallback() {} |
| 65 |
| 66 void ReceivedCallback(AuthenticatorStatus status, |
| 67 ScopedCredentialInfoPtr credential) { |
| 68 response_ = std::make_pair(status, std::move(credential)); |
| 69 closure_.Run(); |
| 70 } |
| 71 |
| 72 std::pair<AuthenticatorStatus, ScopedCredentialInfoPtr>& WaitForCallback() { |
| 73 closure_ = run_loop_.QuitClosure(); |
| 74 run_loop_.Run(); |
| 75 return response_; |
| 76 } |
| 77 |
| 78 const base::Callback<void(AuthenticatorStatus status, |
| 79 ScopedCredentialInfoPtr credential)>& |
| 80 callback() { |
| 81 return callback_; |
| 82 } |
| 83 |
| 84 private: |
| 85 std::pair<AuthenticatorStatus, ScopedCredentialInfoPtr> response_; |
| 86 base::Closure closure_; |
| 87 base::Callback<void(AuthenticatorStatus status, |
| 88 ScopedCredentialInfoPtr credential)> |
| 89 callback_; |
| 90 base::RunLoop run_loop_; |
| 91 }; |
| 92 |
| 93 RelyingPartyAccountPtr GetTestRelyingPartyAccount() { |
| 94 RelyingPartyAccountPtr account = RelyingPartyAccount::New(); |
| 95 account->relying_party_display_name = std::string("TestRP"); |
| 96 account->display_name = std::string("Test A. Name"); |
| 97 account->id = std::string("1098237235409872"); |
| 98 account->name = std::string("Testname@example.com"); |
| 99 account->image_url = std::string("fakeurl.png"); |
| 100 return account; |
| 101 } |
| 102 |
| 103 std::vector<ScopedCredentialParametersPtr> GetTestScopedCredentialParameters() { |
| 104 std::vector<ScopedCredentialParametersPtr> parameters; |
| 105 auto fake_parameter = ScopedCredentialParameters::New(); |
| 106 fake_parameter->type = webauth::mojom::ScopedCredentialType::SCOPEDCRED; |
| 107 parameters.push_back(std::move(fake_parameter)); |
| 108 return parameters; |
| 109 } |
| 110 |
| 111 ScopedCredentialOptionsPtr GetTestScopedCredentialOptions() { |
| 112 ScopedCredentialOptionsPtr opts = ScopedCredentialOptions::New(); |
| 113 opts->adjusted_timeout = 60; |
| 114 opts->relying_party_id = std::string("localhost"); |
| 115 return opts; |
| 116 } |
| 117 |
| 118 // Test that service returns NOT_IMPLEMENTED on a call to MakeCredential. |
| 119 TEST_F(AuthenticatorImplTest, MakeCredentialNotImplemented) { |
| 120 SimulateNavigation(GURL(kOrigin1)); |
| 121 AuthenticatorPtr authenticator = ConnectToAuthenticator(); |
| 122 |
| 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 |
| 131 TestMakeCredentialCallback cb; |
| 132 authenticator->MakeCredential(std::move(account), std::move(parameters), |
| 133 buffer, std::move(opts), cb.callback()); |
| 134 std::pair<webauth::mojom::AuthenticatorStatus, |
| 135 webauth::mojom::ScopedCredentialInfoPtr>& response = |
| 136 cb.WaitForCallback(); |
| 137 EXPECT_EQ(webauth::mojom::AuthenticatorStatus::NOT_IMPLEMENTED, |
| 138 response.first); |
| 139 } |
| 140 |
| 141 // Test that service returns NOT_ALLOWED_ERROR on a call to MakeCredential with |
| 142 // an opaque origin. |
| 143 TEST_F(AuthenticatorImplTest, MakeCredentialOpaqueOrigin) { |
| 144 NavigateAndCommit(GURL("data:text/html,opaque")); |
| 145 AuthenticatorPtr authenticator = ConnectToAuthenticator(); |
| 146 RelyingPartyAccountPtr account = GetTestRelyingPartyAccount(); |
| 147 |
| 148 std::vector<ScopedCredentialParametersPtr> parameters = |
| 149 GetTestScopedCredentialParameters(); |
| 150 |
| 151 std::vector<uint8_t> buffer(32, 0x0A); |
| 152 ScopedCredentialOptionsPtr opts = GetTestScopedCredentialOptions(); |
| 153 |
| 154 TestMakeCredentialCallback cb; |
| 155 authenticator->MakeCredential(std::move(account), std::move(parameters), |
| 156 buffer, std::move(opts), cb.callback()); |
| 157 std::pair<webauth::mojom::AuthenticatorStatus, |
| 158 webauth::mojom::ScopedCredentialInfoPtr>& response = |
| 159 cb.WaitForCallback(); |
| 160 EXPECT_EQ(webauth::mojom::AuthenticatorStatus::NOT_ALLOWED_ERROR, |
| 161 response.first); |
| 162 } |
| 163 } // namespace content |
OLD | NEW |