File: instance_identity_token_getter_impl_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 (483 lines) | stat: -rw-r--r-- 17,248 bytes parent folder | download | duplicates (3)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2025 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/base/instance_identity_token_getter_impl.h"

#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>

#include "base/base64.h"
#include "base/environment.h"
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/task_environment.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {

namespace {
constexpr char kTestAudience[] = "audience_for_testing";
// Matches the URL generated for requests from ComputeEngineServiceClient.
constexpr char kHttpMetadataRequestUrl[] =
    "http://metadata.google.internal/computeMetadata/v1/instance/"
    "service-accounts/default/identity?audience=audience_for_testing&"
    "format=full";
// The environment variable used to override the default metadata server host
// when code is run on a Compute Engine instance.
constexpr char kGceMetadataHostVarName[] = "GCE_METADATA_HOST";

// Constants used for testing Compute Engine Metadata server overrides.
constexpr char kTestMetadataServerHost[] = "override.google.internal";
constexpr char kOverriddenMetadataRequestUrl[] =
    "http://override.google.internal/computeMetadata/v1/instance/"
    "service-accounts/default/identity?audience=audience_for_testing&"
    "format=full";

// This JWT is valid but decoding will fail if kStrict is used because the
// length of the header and payload are not divisible by 4 as the padding has
// been stripped which is how tokens received from the metadata server are
// formatted.
constexpr std::string_view kJwtWithoutPadding =
    // Header
    "eyJhbGciOiJSUzI1NiIsImtpZCI6ImtpZC1zaWduYXR1cmUiLCJ0eXAiOiJKV1QifQ."
    // Payload
    "eyJhdWQiOiJhdWRpZW5jZV9mb3JfdGVzdGluZyIsImF6cCI6IjEyMzQ1IiwiZXhwIjo1NzQw"
    "MTcsImdvb2dsZSI6eyJjb21wdXRlX2VuZ2luZSI6eyJpbnN0YW5jZV9pZCI6IjEyMzQ1Njc4"
    "OSIsImluc3RhbmNlX25hbWUiOiJteS1pbnN0YW5jZSIsInByb2plY3RfaWQiOiJteS1wcm9q"
    "ZWN0IiwicHJvamVjdF9udW1iZXIiOjU0MzIxLCJ6b25lIjoidXMtd2lsZC13ZXN0MS16In19"
    "LCJpYXQiOjU3MDQxNywiaXNzIjoiRzAwZ2xlIiwic3ViIjoiMTIzNDUifQ."
    // Signature
    "SIGNATURE!!";

base::Value::Dict CreateJwtHeaderDict() {
  return base::Value::Dict()
      .Set("typ", "JWT")
      .Set("kid", "kid-signature")
      .Set("alg", "RS256");
}

base::Value::Dict CreateJwtPayloadDict(base::Time now = base::Time::Now()) {
  auto compute_engine_dict = base::Value::Dict()
                                 .Set("instance_id", "123456789")
                                 .Set("instance_name", "my-instance")
                                 .Set("project_id", "my-project")
                                 .Set("project_number", 54321)
                                 .Set("zone", "us-wild-west1-z");

  return base::Value::Dict()
      .Set("iss", "G00gle")
      .Set("iat", static_cast<int>(now.InSecondsFSinceUnixEpoch()))
      .Set("exp", static_cast<int>(
                      (now + base::Minutes(60)).InSecondsFSinceUnixEpoch()))
      .Set("aud", kTestAudience)
      .Set("sub", "12345")
      .Set("azp", "12345")
      .Set("google", base::Value::Dict().Set("compute_engine",
                                             std::move(compute_engine_dict)));
}

std::string Base64EncodeDict(base::Value::Dict dict) {
  return base::Base64Encode(*base::WriteJson(std::move(dict)));
}

std::string GetBase64EncodedHeader() {
  return Base64EncodeDict(CreateJwtHeaderDict());
}

std::string GetBase64EncodedPayload(base::Time now = base::Time::Now()) {
  return Base64EncodeDict(CreateJwtPayloadDict(now));
}

std::string GenerateValidJwt(std::string header, std::string payload) {
  return header + "." + payload + "." + "signature";
}

std::string GenerateValidJwt(base::Time now = base::Time::Now()) {
  return GenerateValidJwt(GetBase64EncodedHeader(), GetBase64EncodedPayload());
}

struct TokenParams {
  TokenParams(std::string header, std::string payload, std::string test_name)
      : header(std::move(header)),
        payload(std::move(payload)),
        test_name(std::move(test_name)) {}

  std::string header;
  std::string payload;
  std::string test_name;
};

}  // namespace

class InstanceIdentityTokenGetterImplTest
    : public testing::Test,
      public testing::WithParamInterface<TokenParams> {
 public:
  InstanceIdentityTokenGetterImplTest();
  ~InstanceIdentityTokenGetterImplTest() override;

  void SetUp() override;

  void OnTokenRetrieved(std::string_view token);

 protected:
  void RunUntilQuit();
  void SetTokenResponse(
      std::string_view response_body,
      std::string_view metadata_server_url = kHttpMetadataRequestUrl);
  void SetErrorResponse(net::HttpStatusCode status);
  void ResetQuitClosure();
  void ClearTokenResponse();
  void FastForwardBy(base::TimeDelta duration);
  void SetMetadataServerEnvVar(std::string_view metadata_server_host);

  InstanceIdentityTokenGetterImpl& instance_identity_token_getter() {
    return *instance_identity_token_getter_;
  }

  void set_pending_callback_count(int count) {
    pending_callback_count_ = count;
  }

  const std::optional<std::string>& token() { return token_; }
  void clear_token() { token_.reset(); }

  size_t url_loader_request_count() {
    return test_url_loader_factory_.total_requests();
  }

  std::string_view valid_jwt() { return valid_jwt_; }

 private:
  int pending_callback_count_ = 1;
  std::optional<std::string> token_;
  // Generate once and store so this token can be used for comparisons.
  std::string valid_jwt_;

  base::RepeatingClosure quit_closure_;

  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::MainThreadType::IO,
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};

  std::unique_ptr<base::Environment> environment_ = base::Environment::Create();
  network::TestURLLoaderFactory test_url_loader_factory_;
  scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;

  std::unique_ptr<InstanceIdentityTokenGetterImpl>
      instance_identity_token_getter_;
};

InstanceIdentityTokenGetterImplTest::InstanceIdentityTokenGetterImplTest() =
    default;
InstanceIdentityTokenGetterImplTest::~InstanceIdentityTokenGetterImplTest() =
    default;

void InstanceIdentityTokenGetterImplTest::SetUp() {
  valid_jwt_ = GenerateValidJwt();
  shared_url_loader_factory_ = test_url_loader_factory_.GetSafeWeakWrapper();
  // Unset any pre-existing environment vars before constructing the
  // InstanceIdentityTokenGetterImpl so the externally set values are not used.
  environment_->UnSetVar(kGceMetadataHostVarName);
  instance_identity_token_getter_ =
      std::make_unique<InstanceIdentityTokenGetterImpl>(
          kTestAudience, shared_url_loader_factory_);

  quit_closure_ = task_environment_.QuitClosure();
}

void InstanceIdentityTokenGetterImplTest::RunUntilQuit() {
  task_environment_.RunUntilQuit();
}

void InstanceIdentityTokenGetterImplTest::SetTokenResponse(
    std::string_view response_body,
    std::string_view metadata_server_url) {
  ClearTokenResponse();
  test_url_loader_factory_.AddResponse(metadata_server_url, response_body);
}

void InstanceIdentityTokenGetterImplTest::ClearTokenResponse() {
  test_url_loader_factory_.ClearResponses();
}

void InstanceIdentityTokenGetterImplTest::SetErrorResponse(
    net::HttpStatusCode status) {
  ClearTokenResponse();
  test_url_loader_factory_.AddResponse(kHttpMetadataRequestUrl,
                                       /*content=*/std::string(), status);
}

void InstanceIdentityTokenGetterImplTest::ResetQuitClosure() {
  ASSERT_TRUE(quit_closure_.is_null());
  quit_closure_ = task_environment_.QuitClosure();
}

void InstanceIdentityTokenGetterImplTest::OnTokenRetrieved(
    std::string_view token) {
  // If the callback has been run previously, make sure each callback receives
  // the same value.
  if (token_.has_value()) {
    EXPECT_EQ(*token_, token);
  } else {
    token_ = token;
  }

  pending_callback_count_--;
  if (pending_callback_count_ == 0) {
    std::move(quit_closure_).Run();
  }
}

void InstanceIdentityTokenGetterImplTest::FastForwardBy(
    base::TimeDelta duration) {
  task_environment_.FastForwardBy(duration);
}

void InstanceIdentityTokenGetterImplTest::SetMetadataServerEnvVar(
    std::string_view metadata_server_host) {
  environment_->SetVar(kGceMetadataHostVarName,
                       std::string(metadata_server_host));
  // Recreate the InstanceIdentityTokenGetterImpl so the new value is used.
  instance_identity_token_getter_ =
      std::make_unique<InstanceIdentityTokenGetterImpl>(
          kTestAudience, shared_url_loader_factory_);
}

TEST_F(InstanceIdentityTokenGetterImplTest, SingleRequest) {
  SetTokenResponse(valid_jwt());

  instance_identity_token_getter().RetrieveToken(
      base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                     base::Unretained(this)));

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_EQ(*token(), valid_jwt());
  ASSERT_EQ(url_loader_request_count(), 1U);
}

TEST_F(InstanceIdentityTokenGetterImplTest,
       SingleRequestWithCustomMetadataServer) {
  SetMetadataServerEnvVar(kTestMetadataServerHost);
  SetTokenResponse(valid_jwt(), kOverriddenMetadataRequestUrl);

  instance_identity_token_getter().RetrieveToken(
      base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                     base::Unretained(this)));

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_EQ(*token(), valid_jwt());
  ASSERT_EQ(url_loader_request_count(), 1U);
}

TEST_F(InstanceIdentityTokenGetterImplTest, JwtWithoutPadding) {
  // Base64 decode will fail if kStrict is used.
  SetTokenResponse(kJwtWithoutPadding);

  instance_identity_token_getter().RetrieveToken(
      base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                     base::Unretained(this)));

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_EQ(*token(), kJwtWithoutPadding);
  ASSERT_EQ(url_loader_request_count(), 1U);
}

TEST_F(InstanceIdentityTokenGetterImplTest, MultipleRequests) {
  const int kQueuedCallbackCount = 10;

  set_pending_callback_count(kQueuedCallbackCount);
  for (int i = 0; i < kQueuedCallbackCount; i++) {
    instance_identity_token_getter().RetrieveToken(
        base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                       base::Unretained(this)));
  }

  SetTokenResponse(valid_jwt());

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_EQ(*token(), valid_jwt());
  ASSERT_EQ(url_loader_request_count(), 1U);
}

TEST_F(InstanceIdentityTokenGetterImplTest, CachedTokenReturned) {
  instance_identity_token_getter().RetrieveToken(
      base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                     base::Unretained(this)));

  SetTokenResponse(valid_jwt());

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_EQ(*token(), valid_jwt());

  // Call a second time and verify a token is provided w/o calling the service.
  ClearTokenResponse();
  ResetQuitClosure();
  set_pending_callback_count(1);

  instance_identity_token_getter().RetrieveToken(
      base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                     base::Unretained(this)));

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_EQ(*token(), valid_jwt());
  ASSERT_EQ(url_loader_request_count(), 1U);
}

TEST_F(InstanceIdentityTokenGetterImplTest, CachedTokenIgnored) {
  auto first_jwt_response = valid_jwt();
  instance_identity_token_getter().RetrieveToken(
      base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                     base::Unretained(this)));

  SetTokenResponse(first_jwt_response);

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_EQ(*token(), first_jwt_response);

  // Call again and verify a token is provided after calling the service.
  FastForwardBy(base::Hours(1));
  ResetQuitClosure();
  set_pending_callback_count(1);
  clear_token();
  // Generate a new JWT with updated timestamp.
  auto second_jwt_response = GenerateValidJwt();
  SetTokenResponse(second_jwt_response);

  instance_identity_token_getter().RetrieveToken(
      base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                     base::Unretained(this)));

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_EQ(*token(), second_jwt_response);
  ASSERT_EQ(url_loader_request_count(), 2U);
}

TEST_F(InstanceIdentityTokenGetterImplTest, ServiceFailureReturnsEmptyString) {
  instance_identity_token_getter().RetrieveToken(
      base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                     base::Unretained(this)));

  SetErrorResponse(net::HTTP_BAD_REQUEST);

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_TRUE(token()->empty());
}

TEST_F(InstanceIdentityTokenGetterImplTest, UnsignedJwtReturnsEmptyToken) {
  // Set response to a token which is missing the signature.
  SetTokenResponse(GetBase64EncodedHeader() + "." + GetBase64EncodedPayload());

  instance_identity_token_getter().RetrieveToken(
      base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                     base::Unretained(this)));

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_TRUE(token()->empty());
}

TEST_P(InstanceIdentityTokenGetterImplTest, InvalidJwtReturnsEmptyToken) {
  SetTokenResponse(GetParam().header + "." + GetParam().payload + ".signature");

  instance_identity_token_getter().RetrieveToken(
      base::BindOnce(&InstanceIdentityTokenGetterImplTest::OnTokenRetrieved,
                     base::Unretained(this)));

  RunUntilQuit();

  ASSERT_TRUE(token().has_value());
  ASSERT_TRUE(token()->empty());
}

INSTANTIATE_TEST_SUITE_P(
    InstanceIdentityTokenGetterImplTest,
    InstanceIdentityTokenGetterImplTest,
    ::testing::Values(
        TokenParams("", "", "EmptyHeaderAndPayload"),
        TokenParams("a." + GetBase64EncodedHeader(),
                    GetBase64EncodedPayload(),
                    "ExtraTokenSegment"),
        TokenParams(GetBase64EncodedHeader() + "====",
                    GetBase64EncodedPayload() + "====",
                    "ExtraPadding"),
        TokenParams("", GetBase64EncodedPayload(), "EmptyHeader"),
        TokenParams(GetBase64EncodedHeader(), "", "EmptyPayload"),
        TokenParams(Base64EncodeDict(base::Value::Dict()),
                    GetBase64EncodedPayload(),
                    "HeaderIsAnEmptyDict"),
        TokenParams(GetBase64EncodedHeader(),
                    Base64EncodeDict(base::Value::Dict()),
                    "PayloadIsAnEmptyDict"),
        TokenParams(*base::WriteJson(CreateJwtHeaderDict()),
                    GetBase64EncodedPayload(),
                    "HeaderNotBase64Encoded"),
        TokenParams(GetBase64EncodedHeader(),
                    *base::WriteJson(CreateJwtPayloadDict()),
                    "PayloadNotBase64Encoded"),
        TokenParams(base::Base64Encode("I'm JSON!"),
                    GetBase64EncodedPayload(),
                    "HeaderIsNotValidJson"),
        TokenParams(GetBase64EncodedHeader(),
                    base::Base64Encode("I'm JSON!"),
                    "PayloadIsNotValidJson"),
        TokenParams(Base64EncodeDict(base::Value::Dict()
                                         .Set("alg", "RS256")
                                         .Set("typ", "JWT")),
                    GetBase64EncodedPayload(),
                    "HeaderIsMissingMembers"),
        TokenParams(GetBase64EncodedHeader(),
                    Base64EncodeDict(base::Value::Dict().Set("iss", "blergh")),
                    "PayloadIsMissingMembers"),
        TokenParams(GetBase64EncodedHeader(),
                    Base64EncodeDict(CreateJwtPayloadDict().SetByDottedPath(
                        "google",
                        base::Value::Dict())),
                    "NoComputeEngineDict"),
        TokenParams(GetBase64EncodedHeader(),
                    Base64EncodeDict(CreateJwtPayloadDict().SetByDottedPath(
                        "google.compute_engine",
                        base::Value::Dict())),
                    "EmptyComputeEngineDict"),
        TokenParams(GetBase64EncodedHeader(),
                    Base64EncodeDict(CreateJwtPayloadDict().SetByDottedPath(
                        "google.compute_engine",
                        base::Value::Dict().Set("instance_id",
                                                "test-instance-id"))),
                    "ComputeEngineDictMissingValues")),
    [](const testing::TestParamInfo<
        InstanceIdentityTokenGetterImplTest::ParamType>& info) {
      return info.param.test_name;
    });

}  // namespace remoting