File: in_memory_download_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 (274 lines) | stat: -rw-r--r-- 10,048 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
// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "components/download/internal/background_service/in_memory_download.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/message_loop/message_pump_type.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "base/uuid.h"
#include "net/base/io_buffer.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "services/network/test/test_url_loader_factory.h"
#include "storage/browser/blob/blob_reader.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::NiceMock;

namespace download {
namespace {

const char kTestDownloadData[] =
    "In earlier tellings, the dog had a better reputation than the cat, "
    "however the president veto it.";

MATCHER_P2(InMemoryDownloadMatcher,
           response_headers,
           url_chain,
           "Verify in memory download.") {
  return arg->response_headers()->raw_headers() == response_headers &&
         arg->url_chain() == url_chain;
}

// Dummy callback used for IO_PENDING state in blob operations, this is not
// called when the blob operation is done, but called when chained with other
// IO operations that might return IO_PENDING.
template <typename T>
void SetValue(T* address, T value) {
  *address = value;
}

// Must run on IO thread task runner.
base::WeakPtr<storage::BlobStorageContext> BlobStorageContextGetter(
    storage::BlobStorageContext* blob_context) {
  DCHECK(blob_context);
  return blob_context->AsWeakPtr();
}

class MockDelegate : public InMemoryDownload::Delegate {
 public:
  MockDelegate(BlobContextGetter blob_context_getter,
               network::TestURLLoaderFactory* url_loader_factory)
      : blob_context_getter_(blob_context_getter),
        url_loader_factory_(url_loader_factory) {}

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

  void WaitForCompletion() {
    DCHECK(!run_loop_.running());
    run_loop_.Run();
  }

  // InMemoryDownload::Delegate implementation.
  MOCK_METHOD1(OnDownloadProgress, void(InMemoryDownload*));
  MOCK_METHOD1(OnDownloadStarted, void(InMemoryDownload*));
  void OnDownloadComplete(InMemoryDownload* download) override {
    if (run_loop_.running())
      run_loop_.Quit();
  }
  MOCK_METHOD1(OnUploadProgress, void(InMemoryDownload*));
  void RetrieveBlobContextGetter(
      base::OnceCallback<void(BlobContextGetter)> callback) override {
    std::move(callback).Run(blob_context_getter_);
  }
  void RetrievedURLLoaderFactory(
      URLLoaderFactoryGetterCallback callback) override {
    std::move(callback).Run(
        base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
            url_loader_factory_));
  }

 private:
  base::RunLoop run_loop_;
  BlobContextGetter blob_context_getter_;
  network::TestURLLoaderFactory* url_loader_factory_;
};

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

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

  ~InMemoryDownloadTest() override = default;

  void SetUp() override {
    io_thread_ = std::make_unique<base::Thread>("Network and Blob IO thread");
    base::Thread::Options options(base::MessagePumpType::IO, 0);
    io_thread_->StartWithOptions(std::move(options));

    base::RunLoop loop;
    io_thread_->task_runner()->PostTask(
        FROM_HERE, base::BindLambdaForTesting([&]() {
          blob_storage_context_ =
              std::make_unique<storage::BlobStorageContext>();
          loop.Quit();
        }));
    loop.Run();

    auto blob_storage_context_getter = base::BindRepeating(
        &BlobStorageContextGetter, blob_storage_context_.get());
    mock_delegate_ = std::make_unique<NiceMock<MockDelegate>>(
        blob_storage_context_getter, &url_loader_factory_);
  }

  void TearDown() override {
    // Say goodbye to |blob_storage_context_| on IO thread.
    io_thread_->task_runner()->DeleteSoon(FROM_HERE,
                                          blob_storage_context_.release());
  }

 protected:
  // Helper method to create a download with request_params.
  void CreateDownload(const RequestParams& request_params) {
    download_ = std::make_unique<InMemoryDownloadImpl>(
        base::Uuid::GenerateRandomV4().AsLowercaseString(), request_params,
        /* request_body= */ nullptr, TRAFFIC_ANNOTATION_FOR_TESTS, delegate(),
        io_thread_->task_runner());
  }

  InMemoryDownload* download() { return download_.get(); }
  MockDelegate* delegate() { return mock_delegate_.get(); }
  network::TestURLLoaderFactory* url_loader_factory() {
    return &url_loader_factory_;
  }

  // Verifies if data read from |blob| is identical as |expected|.
  void VerifyBlobData(const std::string& expected,
                      storage::BlobDataHandle* blob) {
    base::RunLoop run_loop;
    // BlobReader needs to work on IO thread of BlobStorageContext.
    io_thread_->task_runner()->PostTaskAndReply(
        FROM_HERE,
        base::BindOnce(&InMemoryDownloadTest::VerifyBlobDataOnIO,
                       base::Unretained(this), expected, blob),
        run_loop.QuitClosure());
    run_loop.Run();
  }

 private:
  void VerifyBlobDataOnIO(const std::string& expected,
                          storage::BlobDataHandle* blob) {
    DCHECK(blob);
    int bytes_read = 0;
    int async_bytes_read = 0;
    auto buffer = base::MakeRefCounted<net::IOBufferWithSize>(expected.size());

    auto blob_reader = blob->CreateReader();

    int blob_size = 0;
    blob_reader->CalculateSize(base::BindRepeating(&SetValue<int>, &blob_size));
    EXPECT_EQ(blob_size, 0) << "In memory blob read data synchronously.";
    EXPECT_FALSE(blob->IsBeingBuilt())
        << "InMemoryDownload ensures blob construction completed.";
    storage::BlobReader::Status status = blob_reader->Read(
        buffer.get(), expected.size(), &bytes_read,
        base::BindRepeating(&SetValue<int>, &async_bytes_read));
    EXPECT_EQ(storage::BlobReader::Status::DONE, status);
    EXPECT_EQ(bytes_read, static_cast<int>(expected.size()));
    EXPECT_EQ(async_bytes_read, 0);
    for (size_t i = 0; i < expected.size(); i++) {
      EXPECT_EQ(expected[i], buffer->data()[i]);
    }
  }

  // IO thread used by network and blob IO tasks.
  std::unique_ptr<base::Thread> io_thread_;

  // Created before other objects to provide test environment.
  base::test::TaskEnvironment task_environment_;

  std::unique_ptr<InMemoryDownloadImpl> download_;
  std::unique_ptr<NiceMock<MockDelegate>> mock_delegate_;

  // Used by SimpleURLLoader network backend.
  network::TestURLLoaderFactory url_loader_factory_;

  // Memory backed blob storage that can never page to disk.
  std::unique_ptr<storage::BlobStorageContext> blob_storage_context_;
};

TEST_F(InMemoryDownloadTest, DownloadTest) {
  RequestParams request_params;
  CreateDownload(request_params);
  url_loader_factory()->AddResponse(request_params.url.spec(),
                                    kTestDownloadData);

  EXPECT_CALL(*delegate(), OnDownloadStarted(_));
  // TODO(xingliu): More tests on pause/resume.
  download()->Start();
  delegate()->WaitForCompletion();

  EXPECT_EQ(InMemoryDownload::State::COMPLETE, download()->state());
  auto blob = download()->ResultAsBlob();
  VerifyBlobData(kTestDownloadData, blob.get());
}

TEST_F(InMemoryDownloadTest, RedirectResponseHeaders) {
  RequestParams request_params;
  request_params.url = GURL("https://example.com/firsturl");
  CreateDownload(request_params);

  // Add a redirect.
  net::RedirectInfo redirect_info;
  redirect_info.new_url = GURL("https://example.com/redirect12345");
  network::TestURLLoaderFactory::Redirects redirects;
  redirects.push_back({redirect_info, network::mojom::URLResponseHead::New()});

  // Add some random header.
  auto response_head = network::mojom::URLResponseHead::New();
  response_head->headers = base::MakeRefCounted<net::HttpResponseHeaders>("");
  response_head->headers->SetHeader("X-Random-Test-Header", "123");

  // The size must match for download as stream from SimpleUrlLoader.
  network::URLLoaderCompletionStatus status;
  status.decoded_body_length = std::size(kTestDownloadData) - 1;

  url_loader_factory()->AddResponse(request_params.url, response_head.Clone(),
                                    kTestDownloadData, status,
                                    std::move(redirects));

  std::vector<GURL> expected_url_chain = {request_params.url,
                                          redirect_info.new_url};

  EXPECT_CALL(*delegate(),
              OnDownloadStarted(InMemoryDownloadMatcher(
                  response_head->headers->raw_headers(), expected_url_chain)));

  download()->Start();
  delegate()->WaitForCompletion();
  EXPECT_EQ(InMemoryDownload::State::COMPLETE, download()->state());

  // Verify the response headers and URL chain. The URL chain should contain
  // the original URL and redirect URL, and should not contain the final URL.
  EXPECT_EQ(download()->url_chain(), expected_url_chain);
  EXPECT_EQ(download()->response_headers()->raw_headers(),
            response_head->headers->raw_headers());

  // Verfiy the data persisted to disk after redirect chain.
  auto blob = download()->ResultAsBlob();
  VerifyBlobData(kTestDownloadData, blob.get());
}

}  // namespace

}  // namespace download