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

content / browser / blob_storage / blob_storage_browsertest.cc [blame]

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

#include <functional>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "storage/browser/blob/blob_storage_constants.h"
#include "storage/browser/blob/blob_storage_context.h"

namespace content {
namespace {
const size_t kTestBlobStorageIPCThresholdBytes = 5;
const size_t kTestBlobStorageMaxSharedMemoryBytes = 10;

const size_t kTestBlobStorageMaxBlobMemorySize = 200;
const uint64_t kTestBlobStorageMaxDiskSpace = 3000;
const uint64_t kTestBlobStorageMinFileSizeBytes = 20;
const uint64_t kTestBlobStorageMaxFileSizeBytes = 50;

void SetBlobLimitsOnIO(scoped_refptr<ChromeBlobStorageContext> context,
                       const storage::BlobStorageLimits& limits) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  CHECK(context);
  CHECK(context->context());
  context->context()->set_limits_for_testing(limits);
}

}  // namespace

// This browser test is aimed towards exercising the blob storage transportation
// strategies and paging memory to disk.
class BlobStorageBrowserTest : public ContentBrowserTest {
 public:
  BlobStorageBrowserTest() {
    limits_.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes;
    limits_.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes;
    limits_.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize;
    limits_.desired_max_disk_space = kTestBlobStorageMaxDiskSpace;
    limits_.effective_max_disk_space = kTestBlobStorageMaxDiskSpace;
    limits_.min_page_file_size = kTestBlobStorageMinFileSizeBytes;
    limits_.max_file_size = kTestBlobStorageMaxFileSizeBytes;
  }

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

  scoped_refptr<ChromeBlobStorageContext> GetBlobContext() {
    return ChromeBlobStorageContext::GetFor(
        shell()->web_contents()->GetBrowserContext());
  }

  void SetBlobLimits() {
    GetIOThreadTaskRunner({})->PostTask(
        FROM_HERE, base::BindOnce(&SetBlobLimitsOnIO, GetBlobContext(),
                                  std::cref(limits_)));
  }

  void SimpleTest(const GURL& test_url, bool incognito = false) {
    // The test page will perform tests on blob storage, then navigate to either
    // a #pass or #fail ref.
    Shell* the_browser = incognito ? CreateOffTheRecordBrowser() : shell();
    ASSERT_TRUE(the_browser);

    VLOG(0) << "Navigating to URL and blocking. " << test_url.spec();
    NavigateToURLBlockUntilNavigationsComplete(the_browser, test_url, 2);
    VLOG(0) << "Navigation done.";
    std::string result =
        the_browser->web_contents()->GetLastCommittedURL().ref();
    if (result != "pass") {
      std::string js_result = EvalJs(the_browser, "getLog()").ExtractString();
      FAIL() << "Failed: " << js_result;
    }
  }

 protected:
  storage::BlobStorageLimits limits_;
};

IN_PROC_BROWSER_TEST_F(BlobStorageBrowserTest, BlobCombinations) {
  SetBlobLimits();
  SimpleTest(GetTestUrl("blob_storage", "blob_creation_and_slicing.html"));

  shell()->Close();

  // Make sure we run all file / io tasks.
  base::RunLoop().RunUntilIdle();
  RunAllTasksUntilIdle();
}

}  // namespace content