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
|
// 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 "components/feedback/feedback_uploader.h"
#include <string>
#include "base/files/scoped_temp_dir.h"
#include "base/metrics/field_trial.h"
#include "base/run_loop.h"
#include "base/task/task_traits.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "components/feedback/feedback_common.h"
#include "components/feedback/feedback_constants.h"
#include "components/variations/net/variations_http_headers.h"
#include "components/variations/scoped_variations_ids_provider.h"
#include "components/variations/variations_associated_data.h"
#include "components/variations/variations_ids_provider.h"
#include "net/http/http_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.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 "testing/gtest/include/gtest/gtest.h"
namespace feedback {
namespace {
constexpr base::TimeDelta kTestRetryDelay = base::Milliseconds(1);
constexpr char kFeedbackPostUrl[] =
"https://www.google.com/tools/feedback/chrome/__submit";
void QueueReport(FeedbackUploader* uploader,
const std::string& report_data,
bool has_email = true,
int product_id = 0) {
uploader->QueueReport(std::make_unique<std::string>(report_data), has_email,
product_id);
}
class TestFeedbackUploader final : public FeedbackUploader {
public:
TestFeedbackUploader(
bool is_off_the_record,
const base::FilePath& state_path,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: FeedbackUploader(is_off_the_record,
state_path,
std::move(url_loader_factory)) {}
TestFeedbackUploader(const TestFeedbackUploader&) = delete;
TestFeedbackUploader& operator=(const TestFeedbackUploader&) = delete;
~TestFeedbackUploader() override = default;
base::WeakPtr<FeedbackUploader> AsWeakPtr() override {
return weak_ptr_factory_.GetWeakPtr();
}
private:
base::WeakPtrFactory<TestFeedbackUploader> weak_ptr_factory_{this};
};
// Stand-in for the FeedbackUploaderChrome class that adds a fake bearer token
// to the request.
class MockFeedbackUploaderChrome final : public FeedbackUploader {
public:
MockFeedbackUploaderChrome(
bool is_off_the_record,
const base::FilePath& state_path,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: FeedbackUploader(is_off_the_record, state_path, url_loader_factory) {}
void AppendExtraHeadersToUploadRequest(
network::ResourceRequest* resource_request) override {
resource_request->headers.SetHeader(net::HttpRequestHeaders::kAuthorization,
"Bearer abcdefg");
}
base::WeakPtr<FeedbackUploader> AsWeakPtr() override {
return weak_ptr_factory_.GetWeakPtr();
}
private:
base::WeakPtrFactory<MockFeedbackUploaderChrome> weak_ptr_factory_{this};
};
} // namespace
class FeedbackUploaderDispatchTest : public ::testing::Test {
protected:
FeedbackUploaderDispatchTest()
: shared_url_loader_factory_(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_)) {
EXPECT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
}
FeedbackUploaderDispatchTest(const FeedbackUploaderDispatchTest&) = delete;
FeedbackUploaderDispatchTest& operator=(const FeedbackUploaderDispatchTest&) =
delete;
~FeedbackUploaderDispatchTest() override {
// Clean up registered ids.
variations::testing::ClearAllVariationIDs();
}
// Registers a field trial with the specified name and group and an associated
// google web property variation id.
void CreateFieldTrialWithId(const std::string& trial_name,
const std::string& group_name,
int variation_id) {
variations::AssociateGoogleVariationID(
variations::GOOGLE_WEB_PROPERTIES_ANY_CONTEXT, trial_name, group_name,
static_cast<variations::VariationID>(variation_id));
base::FieldTrialList::CreateFieldTrial(trial_name, group_name)->Activate();
}
scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory() {
return shared_url_loader_factory_;
}
network::TestURLLoaderFactory* test_url_loader_factory() {
return &test_url_loader_factory_;
}
const base::FilePath& state_path() { return scoped_temp_dir_.GetPath(); }
private:
base::test::TaskEnvironment task_environment_;
base::ScopedTempDir scoped_temp_dir_;
variations::ScopedVariationsIdsProvider scoped_variations_ids_provider_{
variations::VariationsIdsProvider::Mode::kUseSignedInState};
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;
};
TEST_F(FeedbackUploaderDispatchTest, VariationHeaders) {
// Register a trial and variation id, so that there is data in variations
// headers.
CreateFieldTrialWithId("Test", "Group1", 123);
TestFeedbackUploader uploader(/*is_off_the_record=*/false, state_path(),
shared_url_loader_factory());
net::HttpRequestHeaders headers;
test_url_loader_factory()->SetInterceptor(
base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
EXPECT_TRUE(variations::HasVariationsHeader(request));
}));
QueueReport(&uploader, "test");
base::RunLoop().RunUntilIdle();
}
TEST_F(FeedbackUploaderDispatchTest, VariationHeadersForProductID) {
// Register a trial and variation id, so that there is data in variations
// headers.
CreateFieldTrialWithId("Test", "Group1", 123);
TestFeedbackUploader uploader(/*is_off_the_record=*/false, state_path(),
shared_url_loader_factory());
net::HttpRequestHeaders headers;
test_url_loader_factory()->SetInterceptor(
base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
EXPECT_TRUE(variations::HasVariationsHeader(request));
}));
QueueReport(&uploader, "test", /*has_email=*/false, /*product_id=*/208);
base::RunLoop().RunUntilIdle();
}
TEST_F(FeedbackUploaderDispatchTest, NoVariationHeadersForOrcaProductID) {
// Register a trial and variation id, so that there is data in variations
// headers.
CreateFieldTrialWithId("Test", "Group1", 123);
TestFeedbackUploader uploader(/*is_off_the_record=*/false, state_path(),
shared_url_loader_factory());
net::HttpRequestHeaders headers;
test_url_loader_factory()->SetInterceptor(
base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
EXPECT_FALSE(variations::HasVariationsHeader(request));
}));
QueueReport(&uploader, "test", /*has_email=*/false,
/*product_id=*/feedback::kOrcaFeedbackProductId);
base::RunLoop().RunUntilIdle();
}
// Test that the bearer token is present if there is an email address present in
// the report.
TEST_F(FeedbackUploaderDispatchTest, BearerTokenWithEmail) {
MockFeedbackUploaderChrome uploader(/*is_off_the_record=*/false, state_path(),
shared_url_loader_factory());
test_url_loader_factory()->SetInterceptor(
base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
EXPECT_TRUE(request.headers.HasHeader("Authorization"));
}));
QueueReport(&uploader, "test", /*has_email=*/true);
base::RunLoop().RunUntilIdle();
}
TEST_F(FeedbackUploaderDispatchTest, BearerTokenNoEmail) {
MockFeedbackUploaderChrome uploader(/*is_off_the_record=*/false, state_path(),
shared_url_loader_factory());
test_url_loader_factory()->SetInterceptor(
base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
EXPECT_FALSE(request.headers.HasHeader("Authorization"));
}));
QueueReport(&uploader, "test", /*has_email=*/false);
base::RunLoop().RunUntilIdle();
}
TEST_F(FeedbackUploaderDispatchTest, 204Response) {
FeedbackUploader::SetMinimumRetryDelayForTesting(kTestRetryDelay);
TestFeedbackUploader uploader(/*is_off_the_record=*/false, state_path(),
shared_url_loader_factory());
EXPECT_EQ(kTestRetryDelay, uploader.retry_delay());
// Successful reports should not introduce any retries, and should not
// increase the backoff delay.
auto head = network::mojom::URLResponseHead::New();
std::string headers("HTTP/1.1 204 No Content\n\n");
head->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
net::HttpUtil::AssembleRawHeaders(headers));
network::URLLoaderCompletionStatus status;
test_url_loader_factory()->AddResponse(GURL(kFeedbackPostUrl),
std::move(head), "", status);
QueueReport(&uploader, "Successful report");
base::RunLoop().RunUntilIdle();
EXPECT_EQ(kTestRetryDelay, uploader.retry_delay());
EXPECT_TRUE(uploader.QueueEmpty());
}
TEST_F(FeedbackUploaderDispatchTest, 400Response) {
FeedbackUploader::SetMinimumRetryDelayForTesting(kTestRetryDelay);
TestFeedbackUploader uploader(/*is_off_the_record=*/false, state_path(),
shared_url_loader_factory());
EXPECT_EQ(kTestRetryDelay, uploader.retry_delay());
// Failed reports due to client errors are not retried. No backoff delay
// should be doubled.
auto head = network::mojom::URLResponseHead::New();
std::string headers("HTTP/1.1 400 Bad Request\n\n");
head->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
net::HttpUtil::AssembleRawHeaders(headers));
network::URLLoaderCompletionStatus status;
test_url_loader_factory()->AddResponse(GURL(kFeedbackPostUrl),
std::move(head), "", status);
QueueReport(&uploader, "Client error failed report");
base::RunLoop().RunUntilIdle();
EXPECT_EQ(kTestRetryDelay, uploader.retry_delay());
EXPECT_TRUE(uploader.QueueEmpty());
}
TEST_F(FeedbackUploaderDispatchTest, 500Response) {
FeedbackUploader::SetMinimumRetryDelayForTesting(kTestRetryDelay);
TestFeedbackUploader uploader(/*is_off_the_record=*/false, state_path(),
shared_url_loader_factory());
EXPECT_EQ(kTestRetryDelay, uploader.retry_delay());
// Failed reports due to server errors are retried.
auto head = network::mojom::URLResponseHead::New();
std::string headers("HTTP/1.1 500 Server Error\n\n");
head->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
net::HttpUtil::AssembleRawHeaders(headers));
network::URLLoaderCompletionStatus status;
test_url_loader_factory()->AddResponse(GURL(kFeedbackPostUrl),
std::move(head), "", status);
QueueReport(&uploader, "Server error failed report");
base::RunLoop().RunUntilIdle();
EXPECT_LT(kTestRetryDelay, uploader.retry_delay());
EXPECT_FALSE(uploader.QueueEmpty());
}
} // namespace feedback
|