1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/trusted_vault/trusted_vault_access_token_fetcher_frontend.h"
#include <string>
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "components/signin/public/identity_manager/access_token_info.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/trusted_vault/trusted_vault_access_token_fetcher.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/base/net_errors.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace trusted_vault {
namespace {
using testing::Eq;
using testing::Ne;
MATCHER_P(HasExpectedToken, token, "") {
const TrustedVaultAccessTokenFetcher::AccessTokenInfoOrError&
token_info_or_error = arg;
return token_info_or_error.has_value() && token_info_or_error->token == token;
}
MATCHER_P(HasUnexpectedError, error, "") {
const TrustedVaultAccessTokenFetcher::AccessTokenInfoOrError&
token_info_or_error = arg;
return !token_info_or_error.has_value() &&
token_info_or_error.error() == error;
}
class TrustedVaultAccessTokenFetcherFrontendTest : public testing::Test {
public:
TrustedVaultAccessTokenFetcherFrontendTest()
: frontend_(identity_env_.identity_manager()) {}
~TrustedVaultAccessTokenFetcherFrontendTest() override = default;
TrustedVaultAccessTokenFetcherFrontend* frontend() { return &frontend_; }
signin::IdentityTestEnvironment* identity_env() { return &identity_env_; }
private:
base::test::SingleThreadTaskEnvironment task_environment_;
// |identity_env_| must outlive |frontend_|.
signin::IdentityTestEnvironment identity_env_;
TrustedVaultAccessTokenFetcherFrontend frontend_;
};
TEST_F(TrustedVaultAccessTokenFetcherFrontendTest,
ShouldFetchAccessTokenForPrimaryAccount) {
const CoreAccountId kAccountId =
identity_env()
->MakePrimaryAccountAvailable("test@gmail.com",
signin::ConsentLevel::kSignin)
.account_id;
const std::string kAccessToken = "access_token";
base::MockCallback<TrustedVaultAccessTokenFetcher::TokenCallback>
token_callback;
frontend()->FetchAccessToken(kAccountId, token_callback.Get());
// Callback should be called upon fetching access token from the server.
EXPECT_CALL(token_callback, Run(HasExpectedToken(kAccessToken)));
identity_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccountId, kAccessToken, base::Time::Now() + base::Hours(1));
}
TEST_F(TrustedVaultAccessTokenFetcherFrontendTest,
ShouldFetchAccessTokenForUnconsentedPrimaryAccount) {
const CoreAccountId kAccountId =
identity_env()
->MakePrimaryAccountAvailable("test@gmail.com",
signin::ConsentLevel::kSignin)
.account_id;
const std::string kAccessToken = "access_token";
base::MockCallback<TrustedVaultAccessTokenFetcher::TokenCallback>
token_callback;
frontend()->FetchAccessToken(kAccountId, token_callback.Get());
// Callback should be called upon fetching access token from the server.
EXPECT_CALL(token_callback, Run(HasExpectedToken(kAccessToken)));
identity_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccountId, kAccessToken, base::Time::Now() + base::Hours(1));
}
TEST_F(TrustedVaultAccessTokenFetcherFrontendTest,
ShouldRejectFetchAttemptForNonPrimaryAccount) {
identity_env()->MakePrimaryAccountAvailable("test1@gmail.com",
signin::ConsentLevel::kSignin);
const CoreAccountId kSecondaryAccountId =
identity_env()->MakeAccountAvailable("test2@gmail.com").account_id;
// Fetch should be rejected immediately.
base::MockCallback<TrustedVaultAccessTokenFetcher::TokenCallback>
token_callback;
EXPECT_CALL(
token_callback,
Run(HasUnexpectedError(
TrustedVaultAccessTokenFetcher::FetchingError::kNotPrimaryAccount)));
frontend()->FetchAccessToken(kSecondaryAccountId, token_callback.Get());
}
TEST_F(TrustedVaultAccessTokenFetcherFrontendTest,
ShouldReplyOnUnsuccessfulFetchAttemptWithTransientAuthError) {
const CoreAccountId kAccountId =
identity_env()
->MakePrimaryAccountAvailable("test@gmail.com",
signin::ConsentLevel::kSignin)
.account_id;
const std::string kAccessToken = "access_token";
base::MockCallback<TrustedVaultAccessTokenFetcher::TokenCallback>
token_callback;
frontend()->FetchAccessToken(kAccountId, token_callback.Get());
// Callback should be called upon unsuccessful token fetching attempt.
EXPECT_CALL(
token_callback,
Run(HasUnexpectedError(
TrustedVaultAccessTokenFetcher::FetchingError::kTransientAuthError)));
identity_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
GoogleServiceAuthError::FromConnectionError(net::ERR_FAILED));
}
TEST_F(TrustedVaultAccessTokenFetcherFrontendTest,
ShouldReplyOnUnsuccessfulFetchAttemptWithPersistentAuthError) {
const CoreAccountId kAccountId =
identity_env()
->MakePrimaryAccountAvailable("test@gmail.com",
signin::ConsentLevel::kSignin)
.account_id;
const std::string kAccessToken = "access_token";
base::MockCallback<TrustedVaultAccessTokenFetcher::TokenCallback>
token_callback;
frontend()->FetchAccessToken(kAccountId, token_callback.Get());
// Callback should be called upon unsuccessful token fetching attempt.
EXPECT_CALL(token_callback,
Run(HasUnexpectedError(TrustedVaultAccessTokenFetcher::
FetchingError::kPersistentAuthError)));
identity_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
GoogleServiceAuthError::FromUnexpectedServiceResponse("error"));
}
TEST_F(TrustedVaultAccessTokenFetcherFrontendTest, ShouldAllowMultipleFetches) {
const CoreAccountId kAccountId =
identity_env()
->MakePrimaryAccountAvailable("test@gmail.com",
signin::ConsentLevel::kSignin)
.account_id;
const std::string kAccessToken = "access_token";
base::MockCallback<TrustedVaultAccessTokenFetcher::TokenCallback>
token_callback1;
frontend()->FetchAccessToken(kAccountId, token_callback1.Get());
// Start second fetch before the first one completes.
base::MockCallback<TrustedVaultAccessTokenFetcher::TokenCallback>
token_callback2;
frontend()->FetchAccessToken(kAccountId, token_callback2.Get());
// Both token callbacks should be called upon fetching access token from the
// server.
EXPECT_CALL(token_callback1, Run(HasExpectedToken(kAccessToken)));
EXPECT_CALL(token_callback2, Run(HasExpectedToken(kAccessToken)));
identity_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccountId, kAccessToken, base::Time::Now() + base::Hours(1));
}
// Regression test for crbug.com/427316421.
TEST_F(TrustedVaultAccessTokenFetcherFrontendTest,
ShouldHandleDestructionWhenFullfillingPendingRequests) {
std::unique_ptr<TrustedVaultAccessTokenFetcherFrontend> frontend =
std::make_unique<TrustedVaultAccessTokenFetcherFrontend>(
identity_env()->identity_manager());
identity_env()->MakePrimaryAccountAvailable("test1@gmail.com",
signin::ConsentLevel::kSignin);
const CoreAccountId kSecondaryAccountId =
identity_env()->MakeAccountAvailable("test2@gmail.com").account_id;
base::MockCallback<TrustedVaultAccessTokenFetcher::TokenCallback>
token_callback;
EXPECT_CALL(
token_callback,
Run(HasUnexpectedError(
TrustedVaultAccessTokenFetcher::FetchingError::kNotPrimaryAccount)))
.WillOnce(
[&frontend](TrustedVaultAccessTokenFetcher::AccessTokenInfoOrError) {
// This is actually the main part of the test: there should be no
// crash/UAF when the frontend is destroyed inside the callback.
frontend.reset();
});
frontend->FetchAccessToken(kSecondaryAccountId, token_callback.Get());
}
} // namespace
} // namespace trusted_vault
|