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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/protocol/session_authz_reauthorizer.h"
#include <memory>
#include <string_view>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "net/base/backoff_entry.h"
#include "net/http/http_status_code.h"
#include "remoting/base/http_status.h"
#include "remoting/base/mock_session_authz_service_client.h"
#include "remoting/proto/session_authz_service.h"
#include "remoting/protocol/errors.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting::protocol {
namespace {
using testing::_;
constexpr std::string_view kSessionId = "fake_session_id";
constexpr std::string_view kInitialReauthToken = "fake_initial_reauth_token";
constexpr base::TimeDelta kInitialTokenLifetime = base::Minutes(10);
auto Respond(std::string_view session_reauth_token,
base::TimeDelta session_reauth_token_lifetime) {
auto response = std::make_unique<internal::ReauthorizeHostResponseStruct>();
response->session_reauth_token = session_reauth_token;
response->session_reauth_token_lifetime = session_reauth_token_lifetime;
return base::test::RunOnceCallback<2>(HttpStatus::OK(), std::move(response));
}
template <typename CodeType>
auto RespondError(CodeType code) {
return base::test::RunOnceCallbackRepeatedly<2>(HttpStatus(code), nullptr);
}
} // namespace
class SessionAuthzReauthorizerTest : public testing::Test {
public:
SessionAuthzReauthorizerTest();
~SessionAuthzReauthorizerTest() override;
protected:
auto ResetReauthorizer();
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
base::MockCallback<SessionAuthzReauthorizer::OnReauthorizationFailedCallback>
on_reauthorization_failed_callback_;
std::unique_ptr<SessionAuthzReauthorizer> reauthorizer_;
MockSessionAuthzServiceClient service_client_;
};
SessionAuthzReauthorizerTest::SessionAuthzReauthorizerTest() {
reauthorizer_ = std::make_unique<SessionAuthzReauthorizer>(
&service_client_, kSessionId, kInitialReauthToken, kInitialTokenLifetime,
on_reauthorization_failed_callback_.Get());
reauthorizer_->Start();
}
SessionAuthzReauthorizerTest::~SessionAuthzReauthorizerTest() = default;
auto SessionAuthzReauthorizerTest::ResetReauthorizer() {
return [&]() { reauthorizer_.reset(); };
}
TEST_F(SessionAuthzReauthorizerTest, MultipleSuccessfulReauths) {
// Reauth is not triggered before half of the token lifetime has passed.
EXPECT_CALL(service_client_, ReauthorizeHost(_, _, _)).Times(0);
task_environment_.FastForwardBy(kInitialTokenLifetime / 2 -
base::Seconds(10));
// Reauth is triggered now.
EXPECT_CALL(service_client_,
ReauthorizeHost(kInitialReauthToken, kSessionId, _))
.WillOnce(Respond("fake_second_reauth_token", base::Minutes(8)));
task_environment_.FastForwardBy(base::Seconds(10));
EXPECT_CALL(service_client_, ReauthorizeHost(_, _, _)).Times(0);
task_environment_.FastForwardBy(base::Minutes(4) - base::Seconds(10));
EXPECT_CALL(service_client_,
ReauthorizeHost("fake_second_reauth_token", kSessionId, _))
.WillOnce(Respond("fake_third_reauth_token", base::Minutes(6)));
task_environment_.FastForwardBy(base::Seconds(10));
}
TEST_F(SessionAuthzReauthorizerTest,
ReauthFailedWithNonretriableError_ClosesSession) {
EXPECT_CALL(service_client_,
ReauthorizeHost(kInitialReauthToken, kSessionId, _))
.WillOnce(RespondError(net::HTTP_FORBIDDEN));
EXPECT_CALL(on_reauthorization_failed_callback_,
Run(HttpStatus::Code::PERMISSION_DENIED, _))
.WillOnce(ResetReauthorizer());
task_environment_.FastForwardBy(kInitialTokenLifetime / 2);
}
TEST_F(SessionAuthzReauthorizerTest, RetryReauthAndSucceed) {
EXPECT_CALL(service_client_,
ReauthorizeHost(kInitialReauthToken, kSessionId, _))
.WillRepeatedly(RespondError(net::ERR_INTERNET_DISCONNECTED));
task_environment_.FastForwardBy(kInitialTokenLifetime / 2);
const net::BackoffEntry* backoff_entry =
reauthorizer_->GetBackoffEntryForTest();
task_environment_.FastForwardBy(backoff_entry->GetTimeUntilRelease());
task_environment_.FastForwardBy(backoff_entry->GetTimeUntilRelease());
EXPECT_CALL(service_client_,
ReauthorizeHost(kInitialReauthToken, kSessionId, _))
.WillOnce(Respond("fake_second_reauth_token", base::Minutes(8)));
task_environment_.FastForwardBy(backoff_entry->GetTimeUntilRelease());
EXPECT_CALL(service_client_,
ReauthorizeHost("fake_second_reauth_token", kSessionId, _))
.WillOnce(Respond("fake_third_reauth_token", base::Minutes(6)));
task_environment_.FastForwardBy(base::Minutes(4));
}
TEST_F(SessionAuthzReauthorizerTest, RetriesExceedTokenLifetime_ClosesSession) {
EXPECT_CALL(service_client_,
ReauthorizeHost(kInitialReauthToken, kSessionId, _))
.WillRepeatedly(RespondError(net::ERR_INTERNET_DISCONNECTED));
EXPECT_CALL(on_reauthorization_failed_callback_, Run(_, _)).Times(0);
task_environment_.FastForwardBy(kInitialTokenLifetime / 2);
const net::BackoffEntry* backoff_entry =
reauthorizer_->GetBackoffEntryForTest();
task_environment_.FastForwardBy(backoff_entry->GetTimeUntilRelease());
task_environment_.FastForwardBy(backoff_entry->GetTimeUntilRelease());
EXPECT_CALL(on_reauthorization_failed_callback_,
Run(HttpStatus::Code::NETWORK_ERROR, _))
.WillOnce(ResetReauthorizer());
task_environment_.FastForwardBy(kInitialTokenLifetime / 2);
}
} // namespace remoting::protocol
|