File: extension_downloader_test_helper.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 (206 lines) | stat: -rw-r--r-- 7,777 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
// 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 "extensions/browser/updater/extension_downloader_test_helper.h"

#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "extensions/common/verifier_formats.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"

using testing::_;
using testing::Invoke;

namespace extensions {

const net::BackoffEntry::Policy kZeroBackoffPolicy = {
    // Number of initial errors (in sequence) to ignore before applying
    // exponential back-off rules.
    0,

    // Initial delay for exponential back-off in ms.
    0,

    // Factor by which the waiting time will be multiplied.
    2,

    // Fuzzing percentage. ex: 10% will spread requests randomly
    // between 90%-100% of the calculated time.
    0.1,

    // Maximum amount of time we are willing to delay our request in ms.
    600000,  // Ten minutes.

    // Time to keep an entry from being discarded even when it
    // has no significant state, -1 to never discard.
    -1,

    // Don't use initial delay unless the last request was an error.
    false,
};

MockExtensionDownloaderDelegate::MockExtensionDownloaderDelegate() = default;

MockExtensionDownloaderDelegate::~MockExtensionDownloaderDelegate() = default;

void MockExtensionDownloaderDelegate::Wait() {
  std::unique_ptr<base::RunLoop> runner = std::make_unique<base::RunLoop>();
  quit_closure_ = runner->QuitClosure();
  runner->Run();
  quit_closure_.Reset();
}

void MockExtensionDownloaderDelegate::Quit() {
  quit_closure_.Run();
}

void MockExtensionDownloaderDelegate::DelegateTo(
    ExtensionDownloaderDelegate* delegate) {
  ON_CALL(*this, OnExtensionDownloadFailed(_, _, _, _, _))
      .WillByDefault(Invoke(
          delegate, &ExtensionDownloaderDelegate::OnExtensionDownloadFailed));
  ON_CALL(*this, OnExtensionDownloadStageChanged(_, _))
      .WillByDefault(Invoke(
          delegate,
          &ExtensionDownloaderDelegate::OnExtensionDownloadStageChanged));
  ON_CALL(*this, OnExtensionDownloadFinished_(_, _, _, _, _, _))
      .WillByDefault(Invoke(
          [delegate](const CRXFileInfo& file, bool file_ownership_passed,
                     const GURL& download_url, const PingResult& ping_result,
                     const std::set<int>& request_ids,
                     InstallCallback& callback) {
            delegate->OnExtensionDownloadFinished(
                file, file_ownership_passed, download_url, ping_result,
                request_ids, std::move(callback));
          }));
  ON_CALL(*this, OnExtensionDownloadRetryForTests())
      .WillByDefault(Invoke(
          delegate,
          &ExtensionDownloaderDelegate::OnExtensionDownloadRetryForTests));
  ON_CALL(*this, GetPingDataForExtension(_, _))
      .WillByDefault(Invoke(
          delegate, &ExtensionDownloaderDelegate::GetPingDataForExtension));
  ON_CALL(*this, IsExtensionPending(_))
      .WillByDefault(
          Invoke(delegate, &ExtensionDownloaderDelegate::IsExtensionPending));
  ON_CALL(*this, GetExtensionExistingVersion(_, _))
      .WillByDefault(Invoke(
          delegate, &ExtensionDownloaderDelegate::GetExtensionExistingVersion));
}

MockExtensionCache::MockExtensionCache() = default;
MockExtensionCache::~MockExtensionCache() = default;

void MockExtensionCache::Start(base::OnceClosure callback) {
  std::move(callback).Run();
}

void MockExtensionCache::Shutdown(base::OnceClosure callback) {
  std::move(callback).Run();
}

ExtensionDownloaderTestHelper::ExtensionDownloaderTestHelper()
    : test_shared_url_loader_factory_(
          base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
              &test_url_loader_factory_)),
      delegate_(),
      downloader_(&delegate_,
                  test_shared_url_loader_factory_,
                  GetTestVerifierFormat()) {}

ExtensionDownloaderTestHelper::~ExtensionDownloaderTestHelper() = default;

void ExtensionDownloaderTestHelper::StartUpdateCheck(
    std::unique_ptr<ManifestFetchData> fetch_data) {
  downloader_.StartUpdateCheck(std::move(fetch_data));
}

network::TestURLLoaderFactory::PendingRequest*
ExtensionDownloaderTestHelper::GetPendingRequest(size_t index) {
  if (index >= test_url_loader_factory_.pending_requests()->size()) {
    return nullptr;
  }
  return &(*test_url_loader_factory_.pending_requests())[index];
}

void ExtensionDownloaderTestHelper::ClearURLLoaderFactoryResponses() {
  test_url_loader_factory_.ClearResponses();
}

std::unique_ptr<ExtensionDownloader>
ExtensionDownloaderTestHelper::CreateDownloader() {
  return std::make_unique<ExtensionDownloader>(
      &delegate_, test_shared_url_loader_factory_, GetTestVerifierFormat());
}

const DownloadPingData* ExtensionDownloaderTestHelper::GetTestPingData() {
  static const DownloadPingData kNeverPingedData =
      DownloadPingData(/*rollcall=*/ManifestFetchData::kNeverPinged,
                       /*active=*/ManifestFetchData::kNeverPinged,
                       /*enabled=*/true,
                       /*disable_reasons=*/{});
  return &kNeverPingedData;
}

ExtensionDownloaderTask CreateDownloaderTask(const ExtensionId& id,
                                             const GURL& update_url) {
  return ExtensionDownloaderTask(
      id, update_url, mojom::ManifestLocation::kInternal,
      false /* is_corrupt_reinstall */, 0 /* request_id */,
      DownloadFetchPriority::kBackground);
}

void AddExtensionToFetchDataForTesting(ManifestFetchData* fetch_data,
                                       const ExtensionId& id,
                                       const std::string& version,
                                       const GURL& update_url,
                                       const DownloadPingData& ping_data) {
  fetch_data->AddExtension(id, version, &ping_data,
                           ExtensionDownloaderTestHelper::kEmptyUpdateUrlData,
                           std::string(), mojom::ManifestLocation::kInternal,
                           DownloadFetchPriority::kBackground);
  fetch_data->AddAssociatedTask(CreateDownloaderTask(id, update_url));
}

void AddExtensionToFetchDataForTesting(ManifestFetchData* fetch_data,
                                       const ExtensionId& id,
                                       const std::string& version,
                                       const GURL& update_url) {
  AddExtensionToFetchDataForTesting(
      fetch_data, id, version, update_url,
      *ExtensionDownloaderTestHelper::GetTestPingData());
}

UpdateManifestItem::UpdateManifestItem(ExtensionId id) : id(std::move(id)) {}
UpdateManifestItem::~UpdateManifestItem() = default;
UpdateManifestItem::UpdateManifestItem(const UpdateManifestItem&) = default;
UpdateManifestItem& UpdateManifestItem::operator=(const UpdateManifestItem&) =
    default;
UpdateManifestItem::UpdateManifestItem(UpdateManifestItem&&) = default;
UpdateManifestItem& UpdateManifestItem::operator=(UpdateManifestItem&&) =
    default;

std::string CreateUpdateManifest(
    const std::vector<UpdateManifestItem>& extensions) {
  std::string content =
      "<?xml version='1.0' encoding='UTF-8'?>"
      "<gupdate xmlns='http://www.google.com/update2/response'"
      "                protocol='2.0'>";
  for (const auto& update_item : extensions) {
    content += base::StringPrintf(
        " <app appid='%s'>"
        "  <updatecheck",
        update_item.id.c_str());
    for (const auto& [name, value] : update_item.updatecheck_params) {
      content += base::StringPrintf(" %s='%s'", name.c_str(), value.c_str());
    }
    content +=
        " />"
        " </app>";
  }
  content += "</gupdate>";
  return content;
}

}  // namespace extensions