ftl_registration_manager_unittest.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2019 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. #include "remoting/signaling/ftl_registration_manager.h"
  5. #include "base/guid.h"
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/test/mock_callback.h"
  8. #include "base/test/task_environment.h"
  9. #include "base/time/time.h"
  10. #include "remoting/base/fake_oauth_token_getter.h"
  11. #include "remoting/base/protobuf_http_status.h"
  12. #include "remoting/proto/ftl/v1/ftl_messages.pb.h"
  13. #include "remoting/signaling/ftl_client_uuid_device_id_provider.h"
  14. #include "testing/gmock/include/gmock/gmock.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace remoting {
  17. namespace {
  18. using testing::_;
  19. using SignInGaiaResponseCallback =
  20. base::OnceCallback<void(const ProtobufHttpStatus&,
  21. std::unique_ptr<ftl::SignInGaiaResponse>)>;
  22. constexpr char kAuthToken[] = "auth_token";
  23. constexpr int64_t kAuthTokenExpiresInMicroseconds = 86400000000; // = 1 day
  24. constexpr base::TimeDelta kAuthTokenExpiration =
  25. base::Microseconds(kAuthTokenExpiresInMicroseconds);
  26. MATCHER_P(HasErrorCode, error_code, "") {
  27. return arg.error_code() == error_code;
  28. }
  29. MATCHER(IsStatusOk, "") {
  30. return arg.ok();
  31. }
  32. void VerifySignInGaiaRequest(const ftl::SignInGaiaRequest& request) {
  33. ASSERT_EQ(ftl::SignInGaiaMode_Value_DEFAULT_CREATE_ACCOUNT, request.mode());
  34. ASSERT_TRUE(base::IsValidGUID(request.register_data().device_id().id()));
  35. ASSERT_LT(0, request.register_data().caps_size());
  36. }
  37. decltype(auto) RespondOkToSignInGaia(const std::string& registration_id) {
  38. return [registration_id](const ftl::SignInGaiaRequest& request,
  39. SignInGaiaResponseCallback on_done) {
  40. VerifySignInGaiaRequest(request);
  41. auto response = std::make_unique<ftl::SignInGaiaResponse>();
  42. response->set_registration_id(registration_id);
  43. response->mutable_auth_token()->set_payload(kAuthToken);
  44. response->mutable_auth_token()->set_expires_in(
  45. kAuthTokenExpiresInMicroseconds);
  46. std::move(on_done).Run(ProtobufHttpStatus::OK(), std::move(response));
  47. };
  48. }
  49. } // namespace
  50. class FtlRegistrationManagerTest : public testing::Test {
  51. protected:
  52. class MockRegistrationClient
  53. : public FtlRegistrationManager::RegistrationClient {
  54. public:
  55. MOCK_METHOD2(SignInGaia,
  56. void(const ftl::SignInGaiaRequest&,
  57. SignInGaiaResponseCallback));
  58. MOCK_METHOD0(CancelPendingRequests, void());
  59. };
  60. const net::BackoffEntry& GetBackoff() const {
  61. return registration_manager_.sign_in_backoff_;
  62. }
  63. base::test::TaskEnvironment task_environment_{
  64. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  65. FtlRegistrationManager registration_manager_{
  66. std::make_unique<MockRegistrationClient>(),
  67. std::make_unique<FtlClientUuidDeviceIdProvider>()};
  68. raw_ptr<MockRegistrationClient> registration_client_ =
  69. static_cast<MockRegistrationClient*>(
  70. registration_manager_.registration_client_.get());
  71. base::MockCallback<base::RepeatingCallback<void(const ProtobufHttpStatus&)>>
  72. done_callback_;
  73. };
  74. TEST_F(FtlRegistrationManagerTest, SignInGaiaAndAutorefresh) {
  75. ASSERT_FALSE(registration_manager_.IsSignedIn());
  76. ASSERT_TRUE(registration_manager_.GetRegistrationId().empty());
  77. ASSERT_TRUE(registration_manager_.GetFtlAuthToken().empty());
  78. EXPECT_CALL(*registration_client_, SignInGaia(_, _))
  79. .WillOnce(RespondOkToSignInGaia("registration_id_1"))
  80. .WillOnce(RespondOkToSignInGaia("registration_id_2"));
  81. EXPECT_CALL(done_callback_, Run(IsStatusOk())).Times(1);
  82. registration_manager_.SignInGaia(done_callback_.Get());
  83. task_environment_.FastForwardBy(GetBackoff().GetTimeUntilRelease());
  84. ASSERT_TRUE(registration_manager_.IsSignedIn());
  85. ASSERT_EQ("registration_id_1", registration_manager_.GetRegistrationId());
  86. ASSERT_EQ(kAuthToken, registration_manager_.GetFtlAuthToken());
  87. task_environment_.FastForwardBy(kAuthTokenExpiration);
  88. task_environment_.FastForwardBy(GetBackoff().GetTimeUntilRelease());
  89. ASSERT_EQ("registration_id_2", registration_manager_.GetRegistrationId());
  90. }
  91. TEST_F(FtlRegistrationManagerTest, FailedToSignIn_Backoff) {
  92. ASSERT_FALSE(registration_manager_.IsSignedIn());
  93. ASSERT_TRUE(registration_manager_.GetRegistrationId().empty());
  94. ASSERT_TRUE(registration_manager_.GetFtlAuthToken().empty());
  95. ASSERT_EQ(0, GetBackoff().failure_count());
  96. EXPECT_CALL(*registration_client_, SignInGaia(_, _))
  97. .WillOnce([](const ftl::SignInGaiaRequest& request,
  98. SignInGaiaResponseCallback on_done) {
  99. VerifySignInGaiaRequest(request);
  100. std::move(on_done).Run(
  101. ProtobufHttpStatus(ProtobufHttpStatus::Code::UNAVAILABLE,
  102. "unavailable"),
  103. {});
  104. })
  105. .WillOnce([](const ftl::SignInGaiaRequest& request,
  106. SignInGaiaResponseCallback on_done) {
  107. VerifySignInGaiaRequest(request);
  108. std::move(on_done).Run(
  109. ProtobufHttpStatus(ProtobufHttpStatus::Code::UNAUTHENTICATED,
  110. "unauthenticated"),
  111. {});
  112. })
  113. .WillOnce(RespondOkToSignInGaia("registration_id"));
  114. EXPECT_CALL(done_callback_,
  115. Run(HasErrorCode(ProtobufHttpStatus::Code::UNAVAILABLE)))
  116. .Times(1);
  117. registration_manager_.SignInGaia(done_callback_.Get());
  118. task_environment_.FastForwardBy(GetBackoff().GetTimeUntilRelease());
  119. ASSERT_FALSE(registration_manager_.IsSignedIn());
  120. ASSERT_EQ(1, GetBackoff().failure_count());
  121. EXPECT_CALL(done_callback_,
  122. Run(HasErrorCode(ProtobufHttpStatus::Code::UNAUTHENTICATED)))
  123. .Times(1);
  124. registration_manager_.SignInGaia(done_callback_.Get());
  125. task_environment_.FastForwardBy(GetBackoff().GetTimeUntilRelease());
  126. ASSERT_FALSE(registration_manager_.IsSignedIn());
  127. ASSERT_EQ(2, GetBackoff().failure_count());
  128. EXPECT_CALL(done_callback_, Run(IsStatusOk())).Times(1);
  129. registration_manager_.SignInGaia(done_callback_.Get());
  130. task_environment_.FastForwardBy(GetBackoff().GetTimeUntilRelease());
  131. ASSERT_TRUE(registration_manager_.IsSignedIn());
  132. ASSERT_EQ("registration_id", registration_manager_.GetRegistrationId());
  133. ASSERT_EQ(0, GetBackoff().failure_count());
  134. }
  135. TEST_F(FtlRegistrationManagerTest, SignOut) {
  136. ASSERT_FALSE(registration_manager_.IsSignedIn());
  137. ASSERT_TRUE(registration_manager_.GetRegistrationId().empty());
  138. ASSERT_TRUE(registration_manager_.GetFtlAuthToken().empty());
  139. EXPECT_CALL(*registration_client_, SignInGaia(_, _))
  140. .WillOnce(RespondOkToSignInGaia("registration_id"));
  141. EXPECT_CALL(done_callback_, Run(IsStatusOk())).Times(1);
  142. registration_manager_.SignInGaia(done_callback_.Get());
  143. task_environment_.FastForwardBy(GetBackoff().GetTimeUntilRelease());
  144. ASSERT_TRUE(registration_manager_.IsSignedIn());
  145. ASSERT_EQ("registration_id", registration_manager_.GetRegistrationId());
  146. ASSERT_EQ(kAuthToken, registration_manager_.GetFtlAuthToken());
  147. EXPECT_CALL(*registration_client_, CancelPendingRequests()).Times(1);
  148. registration_manager_.SignOut();
  149. ASSERT_FALSE(registration_manager_.IsSignedIn());
  150. ASSERT_TRUE(registration_manager_.GetRegistrationId().empty());
  151. ASSERT_TRUE(registration_manager_.GetFtlAuthToken().empty());
  152. task_environment_.FastForwardUntilNoTasksRemain();
  153. ASSERT_FALSE(registration_manager_.IsSignedIn());
  154. }
  155. } // namespace remoting