File: blob_url_store_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 (568 lines) | stat: -rw-r--r-- 22,610 bytes parent folder | download | duplicates (5)
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "storage/browser/blob/blob_url_store_impl.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/unguessable_token.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "mojo/public/cpp/system/functions.h"
#include "net/base/features.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_impl.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/blob/blob_url_registry.h"
#include "storage/browser/blob/features.h"
#include "testing/gtest/include/gtest/gtest.h"

using blink::mojom::BlobURLStore;

namespace storage {

namespace {

enum class PartitionedBlobUrlTestCase {
  kPartitioningDisabled,
  kBlockCrossPartitionBlobUrlFetchingEnabled,
  kBlockCrossPartitionBlobUrlFetchingDisabled,
};

class BlobURLStoreImplTestP
    : public testing::Test,
      public testing::WithParamInterface<PartitionedBlobUrlTestCase> {
 public:
  void SetUp() override {
    test_case_ = GetParam();
    InitializeScopedFeatureList();

    context_ = std::make_unique<BlobStorageContext>();
    agent_cluster_id_ = base::UnguessableToken::Create();

    mojo::SetDefaultProcessErrorHandler(base::BindRepeating(
        &BlobURLStoreImplTestP::OnBadMessage, base::Unretained(this)));
  }

  void InitializeScopedFeatureList() {
    std::vector<base::test::FeatureRef> enabled_features{};
    std::vector<base::test::FeatureRef> disabled_features{};

    if (BlockCrossPartitionBlobUrlFetchingEnabled()) {
      enabled_features.push_back(features::kBlockCrossPartitionBlobUrlFetching);
    } else {
      disabled_features.push_back(
          features::kBlockCrossPartitionBlobUrlFetching);
    }

    if (StoragePartitioningEnabled()) {
      enabled_features.push_back(net::features::kThirdPartyStoragePartitioning);
    } else {
      disabled_features.push_back(
          net::features::kThirdPartyStoragePartitioning);
    }

    scoped_feature_list_.InitWithFeatures(enabled_features, disabled_features);
  }

  bool BlockCrossPartitionBlobUrlFetchingEnabled() {
    switch (test_case_) {
      case PartitionedBlobUrlTestCase::
          kBlockCrossPartitionBlobUrlFetchingEnabled:
        return true;
      case PartitionedBlobUrlTestCase::
          kBlockCrossPartitionBlobUrlFetchingDisabled:
      default:
        return false;
    }
  }

  bool StoragePartitioningEnabled() {
    return test_case_ == PartitionedBlobUrlTestCase::
                             kBlockCrossPartitionBlobUrlFetchingEnabled;
  }

  void TearDown() override {
    mojo::SetDefaultProcessErrorHandler(base::NullCallback());
  }

  void OnBadMessage(const std::string& error) {
    bad_messages_.push_back(error);
  }

  mojo::PendingRemote<blink::mojom::Blob> CreateBlobFromString(
      const std::string& uuid,
      const std::string& contents) {
    auto builder = std::make_unique<BlobDataBuilder>(uuid);
    builder->set_content_type("text/plain");
    builder->AppendData(contents);
    mojo::PendingRemote<blink::mojom::Blob> blob;
    BlobImpl::Create(context_->AddFinishedBlob(std::move(builder)),
                     blob.InitWithNewPipeAndPassReceiver());
    return blob;
  }

  std::string UUIDFromBlob(blink::mojom::Blob* blob) {
    base::RunLoop loop;
    std::string received_uuid;
    blob->GetInternalUUID(base::BindOnce(
        [](base::OnceClosure quit_closure, std::string* uuid_out,
           const std::string& uuid) {
          *uuid_out = uuid;
          std::move(quit_closure).Run();
        },
        loop.QuitClosure(), &received_uuid));
    loop.Run();
    return received_uuid;
  }

  mojo::PendingRemote<BlobURLStore> CreateURLStore() {
    mojo::PendingRemote<BlobURLStore> result;
    url_registry_.AddReceiver(kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
                              result.InitWithNewPipeAndPassReceiver());
    return result;
  }

  void RegisterURL(BlobURLStore* store,
                   mojo::PendingRemote<blink::mojom::Blob> blob,
                   const GURL& url) {
    base::RunLoop loop;
    store->Register(std::move(blob), url, agent_cluster_id_,
                    net::SchemefulSite(), loop.QuitClosure());
    loop.Run();
  }

  const std::string kId = "id";
  const url::Origin kOrigin = url::Origin::Create(GURL("https://example.com"));
  const blink::StorageKey kStorageKey =
      blink::StorageKey::CreateFirstParty(kOrigin);
  const GURL kValidUrl = GURL("blob:" + kOrigin.Serialize() + "/id1");
  const GURL kValidUrl2 = GURL("blob:" + kOrigin.Serialize() + "/id2");
  const GURL kInvalidUrl = GURL("bolb:id");
  const GURL kFragmentUrl = GURL(kValidUrl.spec() + "#fragment");
  const url::Origin kWrongOrigin =
      url::Origin::Create(GURL("https://test.com"));
  const GURL kWrongOriginUrl = GURL("blob:" + kWrongOrigin.Serialize() + "/id");
  const net::SchemefulSite kWrongTopLevelSite =
      net::SchemefulSite(kWrongOriginUrl);

 protected:
  base::test::ScopedFeatureList scoped_feature_list_;
  base::test::TaskEnvironment task_environment_;
  PartitionedBlobUrlTestCase test_case_;
  std::unique_ptr<BlobStorageContext> context_;
  BlobUrlRegistry url_registry_;
  std::vector<std::string> bad_messages_;
  base::UnguessableToken agent_cluster_id_;
};

TEST_P(BlobURLStoreImplTestP, BasicRegisterRevoke) {
  mojo::PendingRemote<blink::mojom::Blob> blob =
      CreateBlobFromString(kId, "hello world");

  // Register a URL and make sure the URL keeps the blob alive.
  BlobURLStoreImpl url_store(kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
                             url_registry_.AsWeakPtr());
  RegisterURL(&url_store, std::move(blob), kValidUrl);

  blob = url_registry_.GetBlobFromUrl(kValidUrl);
  ASSERT_TRUE(blob);
  mojo::Remote<blink::mojom::Blob> blob_remote(std::move(blob));
  EXPECT_EQ(kId, UUIDFromBlob(blob_remote.get()));
  blob_remote.reset();

  // Revoke the URL.
  url_store.Revoke(kValidUrl);
  blob = url_registry_.GetBlobFromUrl(kValidUrl);
  EXPECT_FALSE(blob);

  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(context_->registry().HasEntry(kId));
}

TEST_P(BlobURLStoreImplTestP, RegisterInvalidScheme) {
  mojo::PendingRemote<blink::mojom::Blob> blob =
      CreateBlobFromString(kId, "hello world");

  mojo::Remote<BlobURLStore> url_store(CreateURLStore());
  RegisterURL(url_store.get(), std::move(blob), kInvalidUrl);
  EXPECT_FALSE(url_registry_.GetBlobFromUrl(kInvalidUrl));
  EXPECT_EQ(1u, bad_messages_.size());
}

TEST_P(BlobURLStoreImplTestP, RegisterWrongOrigin) {
  mojo::PendingRemote<blink::mojom::Blob> blob =
      CreateBlobFromString(kId, "hello world");

  mojo::Remote<BlobURLStore> url_store(CreateURLStore());
  RegisterURL(url_store.get(), std::move(blob), kWrongOriginUrl);
  EXPECT_FALSE(url_registry_.GetBlobFromUrl(kWrongOriginUrl));
  EXPECT_EQ(1u, bad_messages_.size());
}

TEST_P(BlobURLStoreImplTestP, RegisterUrlFragment) {
  mojo::PendingRemote<blink::mojom::Blob> blob =
      CreateBlobFromString(kId, "hello world");

  mojo::Remote<BlobURLStore> url_store(CreateURLStore());
  RegisterURL(url_store.get(), std::move(blob), kFragmentUrl);
  EXPECT_FALSE(url_registry_.GetBlobFromUrl(kFragmentUrl));
  EXPECT_EQ(1u, bad_messages_.size());
}

TEST_P(BlobURLStoreImplTestP, ImplicitRevoke) {
  mojo::Remote<blink::mojom::Blob> blob(
      CreateBlobFromString(kId, "hello world"));
  mojo::PendingRemote<blink::mojom::Blob> blob2;
  blob->Clone(blob2.InitWithNewPipeAndPassReceiver());

  auto url_store = std::make_unique<BlobURLStoreImpl>(
      kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
      url_registry_.AsWeakPtr());
  RegisterURL(url_store.get(), blob.Unbind(), kValidUrl);
  EXPECT_TRUE(url_registry_.GetBlobFromUrl(kValidUrl));
  RegisterURL(url_store.get(), std::move(blob2), kValidUrl2);
  EXPECT_TRUE(url_registry_.GetBlobFromUrl(kValidUrl2));

  // Destroy URL Store, should revoke URLs.
  url_store = nullptr;
  EXPECT_FALSE(url_registry_.GetBlobFromUrl(kValidUrl));
  EXPECT_FALSE(url_registry_.GetBlobFromUrl(kValidUrl2));
}

TEST_P(BlobURLStoreImplTestP, RevokeThroughDifferentURLStore) {
  mojo::PendingRemote<blink::mojom::Blob> blob =
      CreateBlobFromString(kId, "hello world");

  BlobURLStoreImpl url_store1(kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
                              url_registry_.AsWeakPtr());
  BlobURLStoreImpl url_store2(kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
                              url_registry_.AsWeakPtr());

  RegisterURL(&url_store1, std::move(blob), kValidUrl);
  EXPECT_TRUE(url_registry_.GetBlobFromUrl(kValidUrl));

  url_store2.Revoke(kValidUrl);
  EXPECT_FALSE(url_registry_.GetBlobFromUrl(kValidUrl));
}

TEST_P(BlobURLStoreImplTestP, RevokeInvalidScheme) {
  mojo::Remote<BlobURLStore> url_store(CreateURLStore());
  url_store->Revoke(kInvalidUrl);
  url_store.FlushForTesting();
  EXPECT_EQ(1u, bad_messages_.size());
}

TEST_P(BlobURLStoreImplTestP, RevokeWrongOrigin) {
  mojo::Remote<BlobURLStore> url_store(CreateURLStore());
  url_store->Revoke(kWrongOriginUrl);
  url_store.FlushForTesting();
  EXPECT_EQ(1u, bad_messages_.size());
}

TEST_P(BlobURLStoreImplTestP, RevokeURLWithFragment) {
  mojo::Remote<BlobURLStore> url_store(CreateURLStore());
  url_store->Revoke(kFragmentUrl);
  url_store.FlushForTesting();
  EXPECT_EQ(1u, bad_messages_.size());
}

TEST_P(BlobURLStoreImplTestP, RevokeWrongStorageKey) {
  const blink::StorageKey kWrongStorageKey = blink::StorageKey::Create(
      kOrigin, kWrongTopLevelSite, blink::mojom::AncestorChainBit::kCrossSite);

  mojo::PendingRemote<blink::mojom::Blob> blob =
      CreateBlobFromString(kId, "hello world");

  BlobURLStoreImpl url_store1(kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
                              url_registry_.AsWeakPtr());
  BlobURLStoreImpl url_store2(kWrongStorageKey, kWrongStorageKey.origin(),
                              /*rph_id=*/0, url_registry_.AsWeakPtr());

  RegisterURL(&url_store1, std::move(blob), kValidUrl);
  EXPECT_TRUE(url_registry_.GetBlobFromUrl(kValidUrl));

  url_store2.Revoke(kValidUrl);
  if (StoragePartitioningEnabled()) {
    EXPECT_TRUE(url_registry_.GetBlobFromUrl(kValidUrl));
  } else {
    // The storage keys are either the same or are ignored by the Revoke call.
    EXPECT_FALSE(url_registry_.GetBlobFromUrl(kValidUrl));
  }
}

TEST_P(BlobURLStoreImplTestP, ResolveAsURLLoaderFactoryNonExistentURL) {
  BlobURLStoreImpl url_store(kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
                             url_registry_.AsWeakPtr());
  mojo::Remote<network::mojom::URLLoaderFactory> factory;
  base::RunLoop resolve_loop;
  url_store.ResolveAsURLLoaderFactory(
      kValidUrl, factory.BindNewPipeAndPassReceiver(),
      base::BindOnce(
          [](base::OnceClosure done,
             const std::optional<base::UnguessableToken>&
                 unsafe_agent_cluster_id,
             const std::optional<net::SchemefulSite>& unsafe_top_level_site) {
            EXPECT_FALSE(unsafe_agent_cluster_id.has_value());
            EXPECT_FALSE(unsafe_top_level_site.has_value());
            std::move(done).Run();
          },
          resolve_loop.QuitClosure()));
  resolve_loop.Run();
  auto request = std::make_unique<network::ResourceRequest>();
  request->url = kValidUrl;
  auto loader = network::SimpleURLLoader::Create(std::move(request),
                                                 TRAFFIC_ANNOTATION_FOR_TESTS);
  base::RunLoop download_loop;
  loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
      factory.get(), base::BindLambdaForTesting(
                         [&](std::unique_ptr<std::string> response_body) {
                           download_loop.Quit();
                           EXPECT_FALSE(response_body);
                         }));
  download_loop.Run();
}
TEST_P(BlobURLStoreImplTestP, ResolveAsURLLoaderFactoryInvalidURL) {
  BlobURLStoreImpl url_store(kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
                             url_registry_.AsWeakPtr());
  mojo::Remote<network::mojom::URLLoaderFactory> factory;
  base::RunLoop resolve_loop;
  url_store.ResolveAsURLLoaderFactory(
      kInvalidUrl, factory.BindNewPipeAndPassReceiver(),
      base::BindOnce(
          [](base::OnceClosure done,
             const std::optional<base::UnguessableToken>&
                 unsafe_agent_cluster_id,
             const std::optional<net::SchemefulSite>& unsafe_top_level_site) {
            EXPECT_FALSE(unsafe_agent_cluster_id.has_value());
            EXPECT_FALSE(unsafe_top_level_site.has_value());
            std::move(done).Run();
          },
          resolve_loop.QuitClosure()));
  resolve_loop.Run();
  auto request = std::make_unique<network::ResourceRequest>();
  request->url = kInvalidUrl;
  auto loader = network::SimpleURLLoader::Create(std::move(request),
                                                 TRAFFIC_ANNOTATION_FOR_TESTS);
  base::RunLoop download_loop;
  loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
      factory.get(), base::BindLambdaForTesting(
                         [&](std::unique_ptr<std::string> response_body) {
                           download_loop.Quit();
                           EXPECT_FALSE(response_body);
                         }));
  download_loop.Run();
}

TEST_P(BlobURLStoreImplTestP, ResolveAsURLLoaderFactory) {
  mojo::PendingRemote<blink::mojom::Blob> blob =
      CreateBlobFromString(kId, "hello world");

  BlobURLStoreImpl url_store(kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
                             url_registry_.AsWeakPtr());
  RegisterURL(&url_store, std::move(blob), kValidUrl);

  mojo::Remote<network::mojom::URLLoaderFactory> factory;
  base::RunLoop resolve_loop;
  url_store.ResolveAsURLLoaderFactory(
      kValidUrl, factory.BindNewPipeAndPassReceiver(),
      base::BindOnce(
          [](base::OnceClosure done,
             const base::UnguessableToken& agent_registered,
             const std::optional<base::UnguessableToken>&
                 unsafe_agent_cluster_id,
             const std::optional<net::SchemefulSite>& unsafe_top_level_site) {
            EXPECT_EQ(agent_registered, unsafe_agent_cluster_id);
            std::move(done).Run();
          },
          resolve_loop.QuitClosure(), agent_cluster_id_));
  resolve_loop.Run();

  auto request = std::make_unique<network::ResourceRequest>();
  request->url = kValidUrl;
  auto loader = network::SimpleURLLoader::Create(std::move(request),
                                                 TRAFFIC_ANNOTATION_FOR_TESTS);
  base::RunLoop download_loop;
  loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
      factory.get(), base::BindLambdaForTesting(
                         [&](std::unique_ptr<std::string> response_body) {
                           download_loop.Quit();
                           ASSERT_TRUE(response_body);
                           EXPECT_EQ("hello world", *response_body);
                         }));
  download_loop.Run();
}

TEST_P(BlobURLStoreImplTestP,
       ResolveAsURLLoaderFactoryWithSeparateStorageKeys) {
  const blink::StorageKey kWrongStorageKey = blink::StorageKey::Create(
      kOrigin, kWrongTopLevelSite, blink::mojom::AncestorChainBit::kCrossSite);

  mojo::PendingRemote<blink::mojom::Blob> blob =
      CreateBlobFromString(kId, "hello world");

  BlobURLStoreImpl url_store1(kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
                              url_registry_.AsWeakPtr());
  BlobURLStoreImpl url_store2(kWrongStorageKey, kStorageKey.origin(),
                              /*rph_id=*/0, url_registry_.AsWeakPtr());

  RegisterURL(&url_store1, std::move(blob), kValidUrl);

  mojo::Remote<network::mojom::URLLoaderFactory> factory;
  base::RunLoop resolve_loop;
  url_store2.ResolveAsURLLoaderFactory(
      kValidUrl, factory.BindNewPipeAndPassReceiver(),
      base::BindLambdaForTesting(
          [&](const std::optional<base::UnguessableToken>&
                  unsafe_agent_cluster_id,
              const std::optional<net::SchemefulSite>& unsafe_top_level_site) {
            if (BlockCrossPartitionBlobUrlFetchingEnabled()) {
              EXPECT_FALSE(unsafe_agent_cluster_id.has_value());
              EXPECT_FALSE(unsafe_top_level_site.has_value());
            } else {
              EXPECT_EQ(*unsafe_agent_cluster_id, agent_cluster_id_);
            }
            resolve_loop.Quit();
          }));

  resolve_loop.Run();
  auto request = std::make_unique<network::ResourceRequest>();
  request->url = kValidUrl;
  auto loader = network::SimpleURLLoader::Create(std::move(request),
                                                 TRAFFIC_ANNOTATION_FOR_TESTS);
  base::RunLoop download_loop;
  loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
      factory.get(), base::BindLambdaForTesting(
                         [&](std::unique_ptr<std::string> response_body) {
                           download_loop.Quit();
                           if (BlockCrossPartitionBlobUrlFetchingEnabled()) {
                             EXPECT_FALSE(response_body);
                           } else {
                             ASSERT_TRUE(response_body);
                             EXPECT_EQ("hello world", *response_body);
                           }
                         }));
  download_loop.Run();
}

TEST_P(BlobURLStoreImplTestP, ResolveAsURLLoaderFactoryWithFragmentUrl) {
  const blink::StorageKey kWrongStorageKey = blink::StorageKey::Create(
      kOrigin, kWrongTopLevelSite, blink::mojom::AncestorChainBit::kCrossSite);

  mojo::PendingRemote<blink::mojom::Blob> blob =
      CreateBlobFromString(kId, "hello world");

  BlobURLStoreImpl url_store1(kWrongStorageKey, kStorageKey.origin(),
                              /*rph_id=*/0, url_registry_.AsWeakPtr());
  mojo::Remote<BlobURLStore> url_store2(CreateURLStore());
  RegisterURL(url_store2.get(), std::move(blob), kFragmentUrl);

  mojo::Remote<network::mojom::URLLoaderFactory> factory;
  base::RunLoop resolve_loop;
  url_store1.ResolveAsURLLoaderFactory(
      kFragmentUrl, factory.BindNewPipeAndPassReceiver(),
      base::BindLambdaForTesting(
          [&](const std::optional<base::UnguessableToken>&
                  unsafe_agent_cluster_id,
              const std::optional<net::SchemefulSite>& unsafe_top_level_site) {
            // TODO(crbug.com/40775506): Fix fragment URL bug.
            if (BlockCrossPartitionBlobUrlFetchingEnabled() ||
                !StoragePartitioningEnabled()) {
              EXPECT_FALSE(unsafe_agent_cluster_id.has_value());
              EXPECT_FALSE(unsafe_top_level_site.has_value());
            } else {
              EXPECT_EQ(*unsafe_agent_cluster_id, agent_cluster_id_);
            }
            resolve_loop.Quit();
          }));

  resolve_loop.Run();
  auto request = std::make_unique<network::ResourceRequest>();
  request->url = kFragmentUrl;
  auto loader = network::SimpleURLLoader::Create(std::move(request),
                                                 TRAFFIC_ANNOTATION_FOR_TESTS);
  base::RunLoop download_loop;
  loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
      factory.get(), base::BindLambdaForTesting(
                         [&](std::unique_ptr<std::string> response_body) {
                           download_loop.Quit();
                           if (BlockCrossPartitionBlobUrlFetchingEnabled() ||
                               !StoragePartitioningEnabled()) {
                             EXPECT_FALSE(response_body);
                           } else {
                             ASSERT_TRUE(response_body);
                             EXPECT_EQ("hello world", *response_body);
                           }
                         }));
  download_loop.Run();
}

TEST_P(BlobURLStoreImplTestP, ResolveForNavigation) {
  mojo::PendingRemote<blink::mojom::Blob> blob =
      CreateBlobFromString(kId, "hello world");

  BlobURLStoreImpl url_store(kStorageKey, kStorageKey.origin(), /*rph_id=*/0,
                             url_registry_.AsWeakPtr());
  RegisterURL(&url_store, std::move(blob), kValidUrl);

  base::RunLoop loop0;
  mojo::Remote<blink::mojom::BlobURLToken> token_remote;
  url_store.ResolveAsBlobURLToken(
      kValidUrl, token_remote.BindNewPipeAndPassReceiver(),
      /*is_top_level_navigation=*/false,
      base::BindOnce(
          [](base::OnceClosure done,
             const base::UnguessableToken& agent_registered,
             const std::optional<base::UnguessableToken>&
                 unsafe_agent_cluster_id) {
            EXPECT_EQ(agent_registered, unsafe_agent_cluster_id);
            std::move(done).Run();
          },
          loop0.QuitClosure(), agent_cluster_id_));
  loop0.Run();

  base::UnguessableToken token;
  base::RunLoop loop;
  token_remote->GetToken(base::BindLambdaForTesting(
      [&](const base::UnguessableToken& received_token) {
        token = received_token;
        loop.Quit();
      }));
  loop.Run();

  GURL blob_url;
  EXPECT_TRUE(url_registry_.GetTokenMapping(token, &blob_url, &blob));
  EXPECT_EQ(kValidUrl, blob_url);
  mojo::Remote<blink::mojom::Blob> blob_remote(std::move(blob));
  EXPECT_EQ(kId, UUIDFromBlob(blob_remote.get()));
}

INSTANTIATE_TEST_SUITE_P(
    BlobURLStoreImplTests,
    BlobURLStoreImplTestP,
    testing::Values(
        PartitionedBlobUrlTestCase::kPartitioningDisabled,
        PartitionedBlobUrlTestCase::kBlockCrossPartitionBlobUrlFetchingDisabled,
        PartitionedBlobUrlTestCase::kBlockCrossPartitionBlobUrlFetchingEnabled),
    [](const testing::TestParamInfo<PartitionedBlobUrlTestCase>& info) {
      switch (info.param) {
        case PartitionedBlobUrlTestCase::kPartitioningDisabled:
          return "PartitioningDisabled";
        case PartitionedBlobUrlTestCase::
            kBlockCrossPartitionBlobUrlFetchingDisabled:
          return "BlockCrossPartitionBlobUrlFetchingDisabled";
        case PartitionedBlobUrlTestCase::
            kBlockCrossPartitionBlobUrlFetchingEnabled:
          return "BlockCrossPartitionBlobUrlFetchingEnabled";
      }
    });

}  // namespace
}  // namespace storage