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 NavigateAndCommit(url); |
| 48 } |
| 49 |
| 50 AuthenticatorPtr ConnectToAuthenticator() { |
| 51 AuthenticatorPtr authenticator; |
| 52 AuthenticatorImpl::Create(main_rfh(), mojo::MakeRequest(&authenticator)); |
| 53 return authenticator; |
| 54 } |
| 55 }; |
| 56 |
| 57 class TestMakeCredentialCallback { |
| 58 public: |
| 59 TestMakeCredentialCallback() |
| 60 : callback_(base::Bind(&TestMakeCredentialCallback::ReceivedCallback, |
| 61 base::Unretained(this))) {} |
| 62 ~TestMakeCredentialCallback() {} |
| 63 |
| 64 void ReceivedCallback(AuthenticatorStatus status, |
| 65 ScopedCredentialInfoPtr credential) { |
| 66 response_ = std::make_pair(status, std::move(credential)); |
| 67 closure_.Run(); |
| 68 } |
| 69 |
| 70 std::pair<AuthenticatorStatus, ScopedCredentialInfoPtr>& WaitForCallback() { |
| 71 closure_ = run_loop_.QuitClosure(); |
| 72 run_loop_.Run(); |
| 73 return response_; |
| 74 } |
| 75 |
| 76 const base::Callback<void(AuthenticatorStatus status, |
| 77 ScopedCredentialInfoPtr credential)>& |
| 78 callback() { |
| 79 return callback_; |
| 80 } |
| 81 |
| 82 private: |
| 83 std::pair<AuthenticatorStatus, ScopedCredentialInfoPtr> response_; |
| 84 base::Closure closure_; |
| 85 base::Callback<void(AuthenticatorStatus status, |
| 86 ScopedCredentialInfoPtr credential)> |
| 87 callback_; |
| 88 base::RunLoop run_loop_; |
| 89 }; |
| 90 |
| 91 RelyingPartyAccountPtr GetTestRelyingPartyAccount() { |
| 92 RelyingPartyAccountPtr account = RelyingPartyAccount::New(); |
| 93 account->relying_party_display_name = std::string("TestRP"); |
| 94 account->display_name = std::string("Test A. Name"); |
| 95 account->id = std::string("1098237235409872"); |
| 96 account->name = std::string("Testname@example.com"); |
| 97 account->image_url = std::string("fakeurl.png"); |
| 98 return account; |
| 99 } |
| 100 |
| 101 std::vector<ScopedCredentialParametersPtr> GetTestScopedCredentialParameters() { |
| 102 std::vector<ScopedCredentialParametersPtr> parameters; |
| 103 auto fake_parameter = ScopedCredentialParameters::New(); |
| 104 fake_parameter->type = mojom::ScopedCredentialType::SCOPEDCRED; |
| 105 parameters.push_back(std::move(fake_parameter)); |
| 106 return parameters; |
| 107 } |
| 108 |
| 109 ScopedCredentialOptionsPtr GetTestScopedCredentialOptions() { |
| 110 ScopedCredentialOptionsPtr opts = ScopedCredentialOptions::New(); |
| 111 opts->adjusted_timeout = 60; |
| 112 opts->relying_party_id = std::string("localhost"); |
| 113 return opts; |
| 114 } |
| 115 |
| 116 // Test that service returns NOT_IMPLEMENTED on a call to MakeCredential. |
| 117 TEST_F(AuthenticatorImplTest, MakeCredentialNotImplemented) { |
| 118 SimulateNavigation(GURL(kOrigin1)); |
| 119 AuthenticatorPtr authenticator = ConnectToAuthenticator(); |
| 120 |
| 121 RelyingPartyAccountPtr account = GetTestRelyingPartyAccount(); |
| 122 |
| 123 std::vector<ScopedCredentialParametersPtr> parameters = |
| 124 GetTestScopedCredentialParameters(); |
| 125 |
| 126 std::vector<uint8_t> buffer(32, 0x0A); |
| 127 ScopedCredentialOptionsPtr opts = GetTestScopedCredentialOptions(); |
| 128 |
| 129 TestMakeCredentialCallback cb; |
| 130 authenticator->MakeCredential(std::move(account), std::move(parameters), |
| 131 buffer, std::move(opts), cb.callback()); |
| 132 std::pair<mojom::AuthenticatorStatus, mojom::ScopedCredentialInfoPtr>& |
| 133 response = cb.WaitForCallback(); |
| 134 EXPECT_EQ(mojom::AuthenticatorStatus::NOT_IMPLEMENTED, response.first); |
| 135 } |
| 136 |
| 137 // Test that service returns NOT_ALLOWED_ERROR on a call to MakeCredential with |
| 138 // an opaque origin. |
| 139 TEST_F(AuthenticatorImplTest, MakeCredentialOpaqueOrigin) { |
| 140 NavigateAndCommit(GURL("data:text/html,opaque")); |
| 141 AuthenticatorPtr authenticator = ConnectToAuthenticator(); |
| 142 RelyingPartyAccountPtr account = GetTestRelyingPartyAccount(); |
| 143 |
| 144 std::vector<ScopedCredentialParametersPtr> parameters = |
| 145 GetTestScopedCredentialParameters(); |
| 146 |
| 147 std::vector<uint8_t> buffer(32, 0x0A); |
| 148 ScopedCredentialOptionsPtr opts = GetTestScopedCredentialOptions(); |
| 149 |
| 150 TestMakeCredentialCallback cb; |
| 151 authenticator->MakeCredential(std::move(account), std::move(parameters), |
| 152 buffer, std::move(opts), cb.callback()); |
| 153 std::pair<mojom::AuthenticatorStatus, mojom::ScopedCredentialInfoPtr>& |
| 154 response = cb.WaitForCallback(); |
| 155 EXPECT_EQ(mojom::AuthenticatorStatus::NOT_ALLOWED_ERROR, response.first); |
| 156 } |
| 157 } // namespace webauth |
OLD | NEW |