File: drivefs_auth_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (340 lines) | stat: -rw-r--r-- 13,219 bytes parent folder | download | duplicates (6)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/drivefs/drivefs_auth.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/string_split.h"
#include "base/test/bind.h"
#include "base/test/simple_test_clock.h"
#include "base/test/task_environment.h"
#include "base/timer/mock_timer.h"
#include "components/account_id/account_id.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "google_apis/gaia/gaia_id.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace drivefs {
namespace {

using testing::_;

constexpr char kTestEmail[] = "test@example.com";
constexpr base::TimeDelta kTokenLifetime = base::Hours(1);

class AuthDelegateImpl : public DriveFsAuth::Delegate {
 public:
  AuthDelegateImpl(signin::IdentityManager* identity_manager,
                   const AccountId& account_id)
      : identity_manager_(identity_manager), account_id_(account_id) {}

  AuthDelegateImpl(const AuthDelegateImpl&) = delete;
  AuthDelegateImpl& operator=(const AuthDelegateImpl&) = delete;

  ~AuthDelegateImpl() override = default;

 private:
  // AuthDelegate::Delegate:
  scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory()
      override {
    return nullptr;
  }
  signin::IdentityManager* GetIdentityManager() override {
    return identity_manager_;
  }
  const AccountId& GetAccountId() override { return account_id_; }
  std::string GetObfuscatedAccountId() override {
    return "salt-" + account_id_.GetAccountIdKey();
  }

  bool IsMetricsCollectionEnabled() override { return false; }

  const raw_ptr<signin::IdentityManager> identity_manager_;
  const AccountId account_id_;
};

class DriveFsAuthTest : public ::testing::Test {
 public:
  DriveFsAuthTest() = default;

  DriveFsAuthTest(const DriveFsAuthTest&) = delete;
  DriveFsAuthTest& operator=(const DriveFsAuthTest&) = delete;

 protected:
  void SetUp() override {
    clock_.SetNow(base::Time::Now());
    identity_test_env_.MakePrimaryAccountAvailable(
        kTestEmail, signin::ConsentLevel::kSignin);
    auto timer = std::make_unique<base::MockOneShotTimer>();
    timer_ = timer.get();
    delegate_ = std::make_unique<AuthDelegateImpl>(
        identity_test_env_.identity_manager(),
        AccountId::FromUserEmailGaiaId(kTestEmail, GaiaId("ID")));
    auth_ = std::make_unique<DriveFsAuth>(&clock_,
                                          base::FilePath("/path/to/profile"),
                                          std::move(timer), delegate_.get());
  }

  void TearDown() override {
    EXPECT_FALSE(timer_->IsRunning());
    auth_.reset();
  }

  // Helper function for better line wrapping.
  void RespondWithAccessToken(const std::string& token) {
    identity_test_env_.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
        token, clock_.Now() + kTokenLifetime);
  }

  // Helper function for better line wrapping.
  void RespondWithAuthError(GoogleServiceAuthError::State error_state) {
    identity_test_env_.WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
        GoogleServiceAuthError(error_state));
  }

  base::test::TaskEnvironment task_environment_;
  signin::IdentityTestEnvironment identity_test_env_;
  base::SimpleTestClock clock_;

  std::unique_ptr<AuthDelegateImpl> delegate_;
  std::unique_ptr<DriveFsAuth> auth_;
  raw_ptr<base::MockOneShotTimer, DanglingUntriaged> timer_ = nullptr;
};

TEST_F(DriveFsAuthTest, GetAccessToken_Success) {
  const base::Time time_now = base::Time::Now();
  clock_.SetNow(time_now);
  base::RunLoop run_loop;
  auth_->GetAccessToken(
      false,
      base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
                                     mojom::AccessTokenPtr access_token) {
        EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
        EXPECT_EQ("auth token", access_token->token);
        EXPECT_EQ(time_now + kTokenLifetime, access_token->expiry_time);
        run_loop.Quit();
      }));
  RespondWithAccessToken("auth token");
  run_loop.Run();
}

TEST_F(DriveFsAuthTest, GetAccessToken_GetAccessTokenFailure_Permanent) {
  base::RunLoop run_loop;
  auth_->GetAccessToken(
      false,
      base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
                                     mojom::AccessTokenPtr access_token) {
        EXPECT_EQ(mojom::AccessTokenStatus::kAuthError, status);
        EXPECT_FALSE(access_token.is_null());
        run_loop.Quit();
      }));
  RespondWithAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
  run_loop.Run();
}

TEST_F(DriveFsAuthTest, GetAccessToken_GetAccessTokenFailure_Transient) {
  base::RunLoop run_loop;
  auth_->GetAccessToken(
      false,
      base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
                                     mojom::AccessTokenPtr access_token) {
        EXPECT_EQ(mojom::AccessTokenStatus::kTransientError, status);
        EXPECT_FALSE(access_token.is_null());
        run_loop.Quit();
      }));
  RespondWithAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE);
  run_loop.Run();
}

TEST_F(DriveFsAuthTest, GetAccessToken_GetAccessTokenFailure_Timeout) {
  base::RunLoop run_loop;
  auto quit_closure = run_loop.QuitClosure();
  auth_->GetAccessToken(
      false, base::BindLambdaForTesting(
                 [&](mojom::AccessTokenStatus status, mojom::AccessTokenPtr) {
                   EXPECT_EQ(mojom::AccessTokenStatus::kTransientError, status);
                   std::move(quit_closure).Run();
                 }));
  // Timer fires before access token becomes available.
  timer_->Fire();
  run_loop.Run();
}

TEST_F(DriveFsAuthTest, GetAccessToken_GetAccessTokenFailure_TimeoutRace) {
  base::RunLoop run_loop;
  auto quit_closure = run_loop.QuitClosure();
  auth_->GetAccessToken(
      false, base::BindLambdaForTesting(
                 [&](mojom::AccessTokenStatus status, mojom::AccessTokenPtr) {
                   EXPECT_EQ(mojom::AccessTokenStatus::kTransientError, status);
                   std::move(quit_closure).Run();
                 }));
  // Timer fires before access token becomes available.
  timer_->Fire();
  // Timer callback should stop access token retrieval.
  RespondWithAccessToken("auth token");
  run_loop.Run();
}

TEST_F(DriveFsAuthTest, GetAccessToken_ParallelRequests) {
  const base::Time time_now = base::Time::Now();
  clock_.SetNow(time_now);
  base::RunLoop run_loop;
  auto quit_closure = run_loop.QuitClosure();
  auth_->GetAccessToken(
      false,
      base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
                                     mojom::AccessTokenPtr access_token) {
        EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
        EXPECT_EQ("auth token", access_token->token);
        EXPECT_EQ(time_now + kTokenLifetime, access_token->expiry_time);
        std::move(quit_closure).Run();
      }));
  auth_->GetAccessToken(
      false,
      base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
                                     mojom::AccessTokenPtr access_token) {
        EXPECT_EQ(mojom::AccessTokenStatus::kTransientError, status);
        EXPECT_FALSE(access_token.is_null());
      }));
  RespondWithAccessToken("auth token");
  run_loop.Run();
}

TEST_F(DriveFsAuthTest, GetAccessToken_SequentialRequests) {
  for (int i = 0; i < 3; ++i) {
    base::RunLoop run_loop;
    auth_->GetAccessToken(
        false,
        base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
                                       mojom::AccessTokenPtr access_token) {
          EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
          EXPECT_EQ("auth token", access_token->token);
          run_loop.Quit();
        }));
    RespondWithAccessToken("auth token");
    run_loop.Run();
  }
  for (int i = 0; i < 3; ++i) {
    base::RunLoop run_loop;
    auth_->GetAccessToken(
        false,
        base::BindLambdaForTesting([&](mojom::AccessTokenStatus status,
                                       mojom::AccessTokenPtr access_token) {
          EXPECT_EQ(mojom::AccessTokenStatus::kAuthError, status);
          EXPECT_FALSE(access_token.is_null());
          run_loop.Quit();
        }));
    RespondWithAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
    run_loop.Run();
  }
}

TEST_F(DriveFsAuthTest, Caching) {
  const base::Time time_now = base::Time::Now();
  clock_.SetNow(time_now);
  const base::Time expiry_time = time_now + kTokenLifetime;
  auth_->GetAccessToken(
      true, base::BindLambdaForTesting(
                [&expiry_time](mojom::AccessTokenStatus status,
                               mojom::AccessTokenPtr access_token) {
                  EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
                  EXPECT_EQ("auth token", access_token->token);
                  EXPECT_EQ(expiry_time, access_token->expiry_time);
                }));
  EXPECT_TRUE(identity_test_env_.IsAccessTokenRequestPending());
  RespondWithAccessToken("auth token");

  // Second attempt should reuse already available token.
  auth_->GetAccessToken(
      true, base::BindLambdaForTesting(
                [&expiry_time](mojom::AccessTokenStatus status,
                               mojom::AccessTokenPtr access_token) {
                  EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
                  EXPECT_EQ("auth token", access_token->token);
                  EXPECT_EQ(expiry_time, access_token->expiry_time);
                }));
  EXPECT_FALSE(identity_test_env_.IsAccessTokenRequestPending());
}

TEST_F(DriveFsAuthTest, CachedAndNotCached) {
  base::Time time_now = base::Time::Now();
  clock_.SetNow(time_now);
  const base::Time expiry_time = time_now + kTokenLifetime;
  auth_->GetAccessToken(
      true, base::BindLambdaForTesting(
                [&expiry_time](mojom::AccessTokenStatus status,
                               mojom::AccessTokenPtr access_token) {
                  EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
                  EXPECT_EQ("auth token", access_token->token);
                  EXPECT_EQ(expiry_time, access_token->expiry_time);
                }));
  EXPECT_TRUE(identity_test_env_.IsAccessTokenRequestPending());
  RespondWithAccessToken("auth token");

  // Advance the clock 30 mins, the token should still retain the old expiry
  // time.
  clock_.Advance(base::Minutes(30));
  time_now = time_now + base::Minutes(30);

  // Second attempt should reuse already available token.
  auth_->GetAccessToken(
      true, base::BindLambdaForTesting(
                [&expiry_time](mojom::AccessTokenStatus status,
                               mojom::AccessTokenPtr access_token) {
                  EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
                  EXPECT_EQ("auth token", access_token->token);
                  EXPECT_EQ(expiry_time, access_token->expiry_time);
                }));
  EXPECT_FALSE(identity_test_env_.IsAccessTokenRequestPending());

  // Now ask for token explicitly bypassing the cache.
  auth_->GetAccessToken(
      false, base::BindLambdaForTesting(
                 [&time_now](mojom::AccessTokenStatus status,
                             mojom::AccessTokenPtr access_token) {
                   EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
                   EXPECT_EQ("auth token 2", access_token->token);
                   EXPECT_EQ(time_now + kTokenLifetime,
                             access_token->expiry_time);
                 }));
  EXPECT_TRUE(identity_test_env_.IsAccessTokenRequestPending());
  RespondWithAccessToken("auth token 2");
  EXPECT_FALSE(identity_test_env_.IsAccessTokenRequestPending());
}

TEST_F(DriveFsAuthTest, CacheExpired) {
  auth_->GetAccessToken(
      true, base::BindLambdaForTesting([](mojom::AccessTokenStatus status,
                                          mojom::AccessTokenPtr access_token) {
        EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
        EXPECT_EQ("auth token", access_token->token);
      }));
  EXPECT_TRUE(identity_test_env_.IsAccessTokenRequestPending());
  RespondWithAccessToken("auth token");

  clock_.Advance(base::Hours(2));

  // The token expired so a new one is requested.
  auth_->GetAccessToken(
      true, base::BindLambdaForTesting([](mojom::AccessTokenStatus status,
                                          mojom::AccessTokenPtr access_token) {
        EXPECT_EQ(mojom::AccessTokenStatus::kSuccess, status);
        EXPECT_EQ("auth token 2", access_token->token);
      }));
  RespondWithAccessToken("auth token 2");
  EXPECT_FALSE(identity_test_env_.IsAccessTokenRequestPending());
}

}  // namespace
}  // namespace drivefs