drivefs_auth_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Copyright 2018 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 "ash/components/drivefs/drivefs_auth.h"
  5. #include "base/bind.h"
  6. #include "base/callback_helpers.h"
  7. #include "base/run_loop.h"
  8. #include "base/strings/strcat.h"
  9. #include "base/strings/string_split.h"
  10. #include "base/test/bind.h"
  11. #include "base/test/simple_test_clock.h"
  12. #include "base/test/task_environment.h"
  13. #include "base/timer/mock_timer.h"
  14. #include "components/account_id/account_id.h"
  15. #include "components/signin/public/identity_manager/identity_manager.h"
  16. #include "components/signin/public/identity_manager/identity_test_environment.h"
  17. #include "google_apis/gaia/google_service_auth_error.h"
  18. #include "mojo/public/cpp/bindings/receiver_set.h"
  19. #include "services/network/public/cpp/shared_url_loader_factory.h"
  20. #include "testing/gmock/include/gmock/gmock.h"
  21. #include "testing/gtest/include/gtest/gtest.h"
  22. namespace drivefs {
  23. namespace {
  24. using testing::_;
  25. constexpr char kTestEmail[] = "test@example.com";
  26. constexpr base::TimeDelta kTokenLifetime = base::Hours(1);
  27. class AuthDelegateImpl : public DriveFsAuth::Delegate {
  28. public:
  29. AuthDelegateImpl(signin::IdentityManager* identity_manager,
  30. const AccountId& account_id)
  31. : identity_manager_(identity_manager), account_id_(account_id) {}
  32. AuthDelegateImpl(const AuthDelegateImpl&) = delete;
  33. AuthDelegateImpl& operator=(const AuthDelegateImpl&) = delete;
  34. ~AuthDelegateImpl() override = default;
  35. private:
  36. // AuthDelegate::Delegate:
  37. scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory()
  38. override {
  39. return nullptr;
  40. }
  41. signin::IdentityManager* GetIdentityManager() override {
  42. return identity_manager_;
  43. }
  44. const AccountId& GetAccountId() override { return account_id_; }
  45. std::string GetObfuscatedAccountId() override {
  46. return "salt-" + account_id_.GetAccountIdKey();
  47. }
  48. bool IsMetricsCollectionEnabled() override { return false; }
  49. signin::IdentityManager* const identity_manager_;
  50. const AccountId account_id_;
  51. };
  52. class DriveFsAuthTest : public ::testing::Test {
  53. public:
  54. DriveFsAuthTest() = default;
  55. DriveFsAuthTest(const DriveFsAuthTest&) = delete;
  56. DriveFsAuthTest& operator=(const DriveFsAuthTest&) = delete;
  57. protected:
  58. void SetUp() override {
  59. clock_.SetNow(base::Time::Now());
  60. identity_test_env_.MakePrimaryAccountAvailable(
  61. kTestEmail, signin::ConsentLevel::kSignin);
  62. auto timer = std::make_unique<base::MockOneShotTimer>();
  63. timer_ = timer.get();
  64. delegate_ = std::make_unique<AuthDelegateImpl>(
  65. identity_test_env_.identity_manager(),
  66. AccountId::FromUserEmailGaiaId(kTestEmail, "ID"));
  67. auth_ = std::make_unique<DriveFsAuth>(&clock_,
  68. base::FilePath("/path/to/profile"),
  69. std::move(timer), delegate_.get());
  70. }
  71. void TearDown() override {
  72. EXPECT_FALSE(timer_->IsRunning());
  73. auth_.reset();
  74. }
  75. // Helper function for better line wrapping.
  76. void RespondWithAccessToken(const std::string& token) {
  77. identity_test_env_.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
  78. token, clock_.Now() + kTokenLifetime);
  79. }
  80. // Helper function for better line wrapping.
  81. void RespondWithAuthError(GoogleServiceAuthError::State error_state) {
  82. identity_test_env_.WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
  83. GoogleServiceAuthError(error_state));
  84. }
  85. base::test::TaskEnvironment task_environment_;
  86. signin::IdentityTestEnvironment identity_test_env_;
  87. base::SimpleTestClock clock_;
  88. std::unique_ptr<AuthDelegateImpl> delegate_;
  89. std::unique_ptr<DriveFsAuth> auth_;
  90. base::MockOneShotTimer* timer_ = nullptr;
  91. };
  92. TEST_F(DriveFsAuthTest, GetAccessToken_Success) {
  93. base::RunLoop run_loop;
  94. auth_->GetAccessToken(
  95. false, base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
  96. const std::string& token) {
  97. EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
  98. EXPECT_EQ("auth token", token);
  99. run_loop.Quit();
  100. }));
  101. RespondWithAccessToken("auth token");
  102. run_loop.Run();
  103. }
  104. TEST_F(DriveFsAuthTest, GetAccessToken_GetAccessTokenFailure_Permanent) {
  105. base::RunLoop run_loop;
  106. auth_->GetAccessToken(
  107. false, base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
  108. const std::string& token) {
  109. EXPECT_EQ(mojom::AccessTokenStatus::kAuthError, status);
  110. EXPECT_TRUE(token.empty());
  111. run_loop.Quit();
  112. }));
  113. RespondWithAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
  114. run_loop.Run();
  115. }
  116. TEST_F(DriveFsAuthTest, GetAccessToken_GetAccessTokenFailure_Transient) {
  117. base::RunLoop run_loop;
  118. auth_->GetAccessToken(
  119. false, base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
  120. const std::string& token) {
  121. EXPECT_EQ(mojom::AccessTokenStatus::kTransientError, status);
  122. EXPECT_TRUE(token.empty());
  123. run_loop.Quit();
  124. }));
  125. RespondWithAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE);
  126. run_loop.Run();
  127. }
  128. TEST_F(DriveFsAuthTest, GetAccessToken_GetAccessTokenFailure_Timeout) {
  129. base::RunLoop run_loop;
  130. auto quit_closure = run_loop.QuitClosure();
  131. auth_->GetAccessToken(
  132. false, base::BindLambdaForTesting(
  133. [&](mojom::AccessTokenStatus status, const std::string&) {
  134. EXPECT_EQ(mojom::AccessTokenStatus::kTransientError, status);
  135. std::move(quit_closure).Run();
  136. }));
  137. // Timer fires before access token becomes available.
  138. timer_->Fire();
  139. run_loop.Run();
  140. }
  141. TEST_F(DriveFsAuthTest, GetAccessToken_GetAccessTokenFailure_TimeoutRace) {
  142. base::RunLoop run_loop;
  143. auto quit_closure = run_loop.QuitClosure();
  144. auth_->GetAccessToken(
  145. false, base::BindLambdaForTesting(
  146. [&](mojom::AccessTokenStatus status, const std::string&) {
  147. EXPECT_EQ(mojom::AccessTokenStatus::kTransientError, status);
  148. std::move(quit_closure).Run();
  149. }));
  150. // Timer fires before access token becomes available.
  151. timer_->Fire();
  152. // Timer callback should stop access token retrieval.
  153. RespondWithAccessToken("auth token");
  154. run_loop.Run();
  155. }
  156. TEST_F(DriveFsAuthTest, GetAccessToken_ParallelRequests) {
  157. base::RunLoop run_loop;
  158. auto quit_closure = run_loop.QuitClosure();
  159. auth_->GetAccessToken(
  160. false, base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
  161. const std::string& token) {
  162. EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
  163. EXPECT_EQ("auth token", token);
  164. std::move(quit_closure).Run();
  165. }));
  166. auth_->GetAccessToken(
  167. false, base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
  168. const std::string& token) {
  169. EXPECT_EQ(mojom::AccessTokenStatus::kTransientError, status);
  170. EXPECT_TRUE(token.empty());
  171. }));
  172. RespondWithAccessToken("auth token");
  173. run_loop.Run();
  174. }
  175. TEST_F(DriveFsAuthTest, GetAccessToken_SequentialRequests) {
  176. for (int i = 0; i < 3; ++i) {
  177. base::RunLoop run_loop;
  178. auth_->GetAccessToken(
  179. false, base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
  180. const std::string& token) {
  181. EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
  182. EXPECT_EQ("auth token", token);
  183. run_loop.Quit();
  184. }));
  185. RespondWithAccessToken("auth token");
  186. run_loop.Run();
  187. }
  188. for (int i = 0; i < 3; ++i) {
  189. base::RunLoop run_loop;
  190. auth_->GetAccessToken(
  191. false, base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
  192. const std::string& token) {
  193. EXPECT_EQ(mojom::AccessTokenStatus::kAuthError, status);
  194. EXPECT_TRUE(token.empty());
  195. run_loop.Quit();
  196. }));
  197. RespondWithAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
  198. run_loop.Run();
  199. }
  200. }
  201. TEST_F(DriveFsAuthTest, Caching) {
  202. auth_->GetAccessToken(true, base::BindOnce([](mojom::AccessTokenStatus status,
  203. const std::string& token) {
  204. EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
  205. EXPECT_EQ("auth token", token);
  206. }));
  207. EXPECT_TRUE(identity_test_env_.IsAccessTokenRequestPending());
  208. RespondWithAccessToken("auth token");
  209. // Second attempt should reuse already available token.
  210. auth_->GetAccessToken(true, base::BindOnce([](mojom::AccessTokenStatus status,
  211. const std::string& token) {
  212. EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
  213. EXPECT_EQ("auth token", token);
  214. }));
  215. EXPECT_FALSE(identity_test_env_.IsAccessTokenRequestPending());
  216. }
  217. TEST_F(DriveFsAuthTest, CachedAndNotCached) {
  218. auth_->GetAccessToken(true, base::BindOnce([](mojom::AccessTokenStatus status,
  219. const std::string& token) {
  220. EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
  221. EXPECT_EQ("auth token", token);
  222. }));
  223. EXPECT_TRUE(identity_test_env_.IsAccessTokenRequestPending());
  224. RespondWithAccessToken("auth token");
  225. // Second attempt should reuse already available token.
  226. auth_->GetAccessToken(true, base::BindOnce([](mojom::AccessTokenStatus status,
  227. const std::string& token) {
  228. EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
  229. EXPECT_EQ("auth token", token);
  230. }));
  231. EXPECT_FALSE(identity_test_env_.IsAccessTokenRequestPending());
  232. // Now ask for token explicitly bypassing the cache.
  233. auth_->GetAccessToken(
  234. false, base::BindOnce(
  235. [](mojom::AccessTokenStatus status, const std::string& token) {
  236. EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
  237. EXPECT_EQ("auth token 2", token);
  238. }));
  239. EXPECT_TRUE(identity_test_env_.IsAccessTokenRequestPending());
  240. RespondWithAccessToken("auth token 2");
  241. EXPECT_FALSE(identity_test_env_.IsAccessTokenRequestPending());
  242. }
  243. TEST_F(DriveFsAuthTest, CacheExpired) {
  244. auth_->GetAccessToken(true, base::BindOnce([](mojom::AccessTokenStatus status,
  245. const std::string& token) {
  246. EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
  247. EXPECT_EQ("auth token", token);
  248. }));
  249. EXPECT_TRUE(identity_test_env_.IsAccessTokenRequestPending());
  250. RespondWithAccessToken("auth token");
  251. clock_.Advance(base::Hours(2));
  252. // The token expired so a new one is requested.
  253. auth_->GetAccessToken(true, base::BindOnce([](mojom::AccessTokenStatus status,
  254. const std::string& token) {
  255. EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
  256. EXPECT_EQ("auth token 2", token);
  257. }));
  258. RespondWithAccessToken("auth token 2");
  259. EXPECT_FALSE(identity_test_env_.IsAccessTokenRequestPending());
  260. }
  261. } // namespace
  262. } // namespace drivefs