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

content / browser / service_worker / service_worker_context_wrapper_unittest.cc [blame]

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/service_worker/service_worker_context_wrapper.h"

#include <memory>
#include <vector>

#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "components/services/storage/service_worker/service_worker_storage_control_impl.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_test_utils.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "net/base/features.h"
#include "net/base/schemeful_site.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
#include "url/origin.h"

namespace content {

// Fixture for the ServiceWorkerContextWrapper test. It uses a disk user data
// directory in order to test starting the browser with a registration already
// written to storage.
class ServiceWorkerContextWrapperTest : public testing::Test {
 public:
  ServiceWorkerContextWrapperTest() = default;

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

  void SetUp() override {
    ASSERT_TRUE(user_data_directory_.CreateUniqueTempDir());
    InitWrapper();
  }

  void TearDown() override {
    // Shutdown or else ASAN complains of leaks.
    wrapper_->Shutdown();
    base::RunLoop().RunUntilIdle();
  }

  void InitWrapper() {
    browser_context_ = std::make_unique<TestBrowserContext>();
    wrapper_ = base::MakeRefCounted<ServiceWorkerContextWrapper>(
        browser_context_.get());
    // Set up a mojo connection binder which binds a connection to a
    // ServiceWorkerStorageControlImpl instance owned by `this`. This is needed
    // to work around strange situations (e.g. nested
    // ServiceWorkerContextWrapper::Init() calls) caused by overwriting
    // StoragePartitionImpl the below.
    wrapper_->SetStorageControlBinderForTest(base::BindRepeating(
        &ServiceWorkerContextWrapperTest::BindStorageControl,
        base::Unretained(this)));
    StoragePartitionImpl* storage_partition =
        static_cast<StoragePartitionImpl*>(
            browser_context_->GetStoragePartitionForUrl(
                GURL("https://example.com")));
    wrapper_->set_storage_partition(storage_partition);
    wrapper_->Init(user_data_directory_.GetPath(), nullptr, nullptr, nullptr);
    // Init() posts a couple tasks to the IO thread. Let them finish.
    base::RunLoop().RunUntilIdle();
  }

  ServiceWorkerContextCore* context() { return wrapper_->context(); }
  ServiceWorkerRegistry* registry() { return context()->registry(); }

  blink::ServiceWorkerStatusCode StoreRegistration(
      scoped_refptr<ServiceWorkerRegistration> registration) {
    blink::ServiceWorkerStatusCode result;
    base::RunLoop loop;
    registry()->StoreRegistration(
        registration.get(), registration->waiting_version(),
        base::BindLambdaForTesting([&](blink::ServiceWorkerStatusCode status) {
          result = status;
          loop.Quit();
        }));
    loop.Run();
    return result;
  }

  blink::ServiceWorkerStatusCode DeleteRegistration(
      scoped_refptr<ServiceWorkerRegistration> registration) {
    blink::ServiceWorkerStatusCode result;
    base::RunLoop loop;
    registry()->DeleteRegistration(
        registration,
        base::BindLambdaForTesting([&](blink::ServiceWorkerStatusCode status) {
          result = status;
          loop.Quit();
        }));
    loop.Run();
    return result;
  }

 protected:
  void BindStorageControl(
      mojo::PendingReceiver<storage::mojom::ServiceWorkerStorageControl>
          receiver) {
    storage_control_ =
        std::make_unique<storage::ServiceWorkerStorageControlImpl>(
            user_data_directory_.GetPath(),
            /*database_task_runner=*/
            base::SingleThreadTaskRunner::GetCurrentDefault(),
            std::move(receiver));
  }

  BrowserTaskEnvironment task_environment_{BrowserTaskEnvironment::IO_MAINLOOP};
  base::ScopedTempDir user_data_directory_;
  std::unique_ptr<TestBrowserContext> browser_context_;
  scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
  std::unique_ptr<storage::ServiceWorkerStorageControlImpl> storage_control_;
};

// Test that the UI thread knows which origins have registrations upon
// browser startup. Regression test for https://crbug.com/991143.
TEST_F(ServiceWorkerContextWrapperTest, HasRegistration) {
  // Make a service worker.
  GURL scope("https://example.com/");
  const blink::StorageKey key =
      blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
  GURL script("https://example.com/sw.js");
  scoped_refptr<ServiceWorkerRegistration> registration =
      CreateServiceWorkerRegistrationAndVersion(context(), scope, script, key,
                                                /*resource_id=*/1);

  // Store it.
  base::RunLoop loop;
  registry()->StoreRegistration(
      registration.get(), registration->waiting_version(),
      base::BindLambdaForTesting(
          [&loop](blink::ServiceWorkerStatusCode status) {
            ASSERT_EQ(blink::ServiceWorkerStatusCode::kOk, status);
            loop.Quit();
          }));
  loop.Run();

  // Simulate browser shutdown and restart.
  wrapper_->Shutdown();
  base::RunLoop().RunUntilIdle();
  wrapper_.reset();
  InitWrapper();

  // Now test that registrations are recognized.
  wrapper_->context()->WaitForRegistrationsInitializedForTest();
  EXPECT_TRUE(wrapper_->MaybeHasRegistrationForStorageKey(key));
  EXPECT_FALSE(wrapper_->MaybeHasRegistrationForStorageKey(
      blink::StorageKey::CreateFromStringForTesting("https://example.org")));
}

// This test involves storing two registrations for the same key to storage
// and deleting one of them to check that MaybeHasRegistrationForStorageKey
// still correctly returns TRUE since there is still one registration for the
// key, and should only return FALSE when ALL registrations for that key
// have been deleted from storage.
TEST_F(ServiceWorkerContextWrapperTest, DeleteRegistrationsForSameKey) {
  wrapper_->context()->WaitForRegistrationsInitializedForTest();

  // Make two registrations for same origin.
  GURL scope1("https://example1.com/abc/");
  const blink::StorageKey key =
      blink::StorageKey::CreateFirstParty(url::Origin::Create(scope1));
  GURL script1("https://example1.com/abc/sw.js");
  scoped_refptr<ServiceWorkerRegistration> registration1 =
      CreateServiceWorkerRegistrationAndVersion(context(), scope1, script1, key,
                                                /*resource_id=*/1);
  GURL scope2("https://example1.com/xyz/");
  GURL script2("https://example1.com/xyz/sw.js");
  scoped_refptr<ServiceWorkerRegistration> registration2 =
      CreateServiceWorkerRegistrationAndVersion(context(), scope2, script2, key,
                                                1);

  // Store both registrations.
  ASSERT_EQ(StoreRegistration(registration1),
            blink::ServiceWorkerStatusCode::kOk);
  ASSERT_EQ(StoreRegistration(registration2),
            blink::ServiceWorkerStatusCode::kOk);

  // Delete one of the registrations.
  ASSERT_EQ(DeleteRegistration(registration1),
            blink::ServiceWorkerStatusCode::kOk);

  // Run loop until idle to wait for
  // ServiceWorkerRegistry::DidDeleteRegistration() to be executed, and make
  // sure that NotifyAllRegistrationsDeletedForStorageKey() is not called.
  base::RunLoop().RunUntilIdle();

  // Now test that a registration for a key is still recognized.
  EXPECT_TRUE(wrapper_->MaybeHasRegistrationForStorageKey(key));

  // Remove second registration.
  ASSERT_EQ(DeleteRegistration(registration2),
            blink::ServiceWorkerStatusCode::kOk);

  // Run loop until idle to wait for
  // ServiceWorkerRegistry::DidDeleteRegistration() to be executed, and make
  // sure that this time NotifyAllRegistrationsDeletedForStorageKey() is called.
  base::RunLoop().RunUntilIdle();

  // Now test that key does not have any registrations.
  EXPECT_FALSE(wrapper_->MaybeHasRegistrationForStorageKey(key));
}

// This tests installs two registrations with the same origin but different
// top-level site, then deletes one, then confirms that the other still appears
// for MaybeHasRegistrationForStorageKey().
TEST_F(ServiceWorkerContextWrapperTest, DeleteRegistrationsForPartitionedKeys) {
  base::test::ScopedFeatureList scope_feature_list_;
  scope_feature_list_.InitAndEnableFeature(
      net::features::kThirdPartyStoragePartitioning);

  wrapper_->context()->WaitForRegistrationsInitializedForTest();

  // Make two registrations for same origin, but different top-level site.
  GURL scope("https://example1.com/abc/");
  net::SchemefulSite site1 = net::SchemefulSite(GURL("https://site1.example"));
  blink::StorageKey key1 =
      blink::StorageKey::Create(url::Origin::Create(scope), site1,
                                blink::mojom::AncestorChainBit::kCrossSite);
  GURL script("https://example1.com/abc/sw.js");
  scoped_refptr<ServiceWorkerRegistration> registration1 =
      CreateServiceWorkerRegistrationAndVersion(context(), scope, script, key1,
                                                /*resource_id=*/1);

  net::SchemefulSite site2 = net::SchemefulSite(GURL("https://site2.example"));
  blink::StorageKey key2 =
      blink::StorageKey::Create(url::Origin::Create(scope), site2,
                                blink::mojom::AncestorChainBit::kCrossSite);
  scoped_refptr<ServiceWorkerRegistration> registration2 =
      CreateServiceWorkerRegistrationAndVersion(context(), scope, script, key2,
                                                2);

  // Store both registrations.
  ASSERT_EQ(StoreRegistration(registration1),
            blink::ServiceWorkerStatusCode::kOk);
  ASSERT_EQ(StoreRegistration(registration2),
            blink::ServiceWorkerStatusCode::kOk);

  // Delete one of the registrations.
  ASSERT_EQ(DeleteRegistration(registration1),
            blink::ServiceWorkerStatusCode::kOk);

  // Run loop until idle to wait for
  // ServiceWorkerRegistry::DidDeleteRegistration() to be executed, and make
  // sure that NotifyAllRegistrationsDeletedForStorageKey() is not called.
  base::RunLoop().RunUntilIdle();

  // The first key should be gone.
  EXPECT_FALSE(wrapper_->MaybeHasRegistrationForStorageKey(key1));
  // Now test that a registration for the second is still recognized.
  EXPECT_TRUE(wrapper_->MaybeHasRegistrationForStorageKey(key2));

  // Remove second registration.
  ASSERT_EQ(DeleteRegistration(registration2),
            blink::ServiceWorkerStatusCode::kOk);

  // Run loop until idle to wait for
  // ServiceWorkerRegistry::DidDeleteRegistration() to be executed, and make
  // sure that this time NotifyAllRegistrationsDeletedForStorageKey() is called.
  base::RunLoop().RunUntilIdle();

  // Now test that key does not have any registrations.
  EXPECT_FALSE(wrapper_->MaybeHasRegistrationForStorageKey(key2));
}

// This tests deleting registrations from storage and checking that even if live
// registrations may exist, MaybeHasRegistrationForStorageKey correctly returns
// FALSE since the registrations do not exist in storage.
TEST_F(ServiceWorkerContextWrapperTest, DeleteRegistration) {
  wrapper_->context()->WaitForRegistrationsInitializedForTest();

  // Make registration.
  GURL scope1("https://example2.com/");
  const blink::StorageKey key =
      blink::StorageKey::CreateFirstParty(url::Origin::Create(scope1));
  GURL script1("https://example2.com/");
  scoped_refptr<ServiceWorkerRegistration> registration =
      CreateServiceWorkerRegistrationAndVersion(context(), scope1, script1, key,
                                                /*resource_id=*/1);

  // Store registration.
  ASSERT_EQ(StoreRegistration(registration),
            blink::ServiceWorkerStatusCode::kOk);

  wrapper_->OnRegistrationCompleted(registration->id(), registration->scope(),
                                    registration->key());
  base::RunLoop().RunUntilIdle();

  // Now test that a registration is recognized.
  EXPECT_TRUE(wrapper_->MaybeHasRegistrationForStorageKey(key));

  // Delete registration from storage.
  ASSERT_EQ(DeleteRegistration(registration),
            blink::ServiceWorkerStatusCode::kOk);

  // Finish deleting registration from storage.
  base::RunLoop().RunUntilIdle();

  // Now test that key does not have any registrations. This should return
  // FALSE even when live registrations may exist, as the registrations have
  // been deleted from storage.
  EXPECT_FALSE(wrapper_->MaybeHasRegistrationForStorageKey(key));
}

}  // namespace content