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
|
// Copyright 2014 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/metrics/reporting_service.h"
#include <stdint.h>
#include <deque>
#include <memory>
#include <string>
#include "base/functional/bind.h"
#include "base/hash/sha1.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/test_simple_task_runner.h"
#include "components/metrics/log_store.h"
#include "components/metrics/test/test_metrics_service_client.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/zlib/google/compression_utils.h"
namespace metrics {
namespace {
// Represent a flushed log and its metadata to be used for testing.
struct TestLog {
explicit TestLog(const std::string& log) : log(log), user_id(absl::nullopt) {}
TestLog(const std::string& log, uint64_t user_id)
: log(log), user_id(user_id) {}
TestLog(const TestLog& other) = default;
~TestLog() = default;
const std::string log;
const absl::optional<uint64_t> user_id;
};
const char kTestUploadUrl[] = "test_url";
const char kTestMimeType[] = "test_mime_type";
class TestLogStore : public LogStore {
public:
TestLogStore() = default;
~TestLogStore() override = default;
void AddLog(const TestLog& log) { logs_.push_back(log); }
// LogStore:
bool has_unsent_logs() const override { return !logs_.empty(); }
bool has_staged_log() const override { return !staged_log_hash_.empty(); }
const std::string& staged_log() const override { return logs_.front().log; }
const std::string& staged_log_hash() const override {
return staged_log_hash_;
}
absl::optional<uint64_t> staged_log_user_id() const override {
return logs_.front().user_id;
}
const std::string& staged_log_signature() const override {
return base::EmptyString();
}
void StageNextLog() override {
if (has_unsent_logs()) {
staged_log_hash_ = base::SHA1HashString(logs_.front().log);
}
}
void DiscardStagedLog(base::StringPiece reason) override {
if (!has_staged_log())
return;
logs_.pop_front();
staged_log_hash_.clear();
}
void MarkStagedLogAsSent() override {}
void TrimAndPersistUnsentLogs(bool overwrite_in_memory_store) override {}
void LoadPersistedUnsentLogs() override {}
private:
std::string staged_log_hash_;
std::deque<TestLog> logs_;
};
class TestReportingService : public ReportingService {
public:
TestReportingService(MetricsServiceClient* client, PrefService* local_state)
: ReportingService(client,
local_state,
100,
/*logs_event_manager=*/nullptr) {
Initialize();
}
TestReportingService(const TestReportingService&) = delete;
TestReportingService& operator=(const TestReportingService&) = delete;
~TestReportingService() override = default;
void AddLog(const TestLog& log) { log_store_.AddLog(log); }
bool HasUnsentLogs() { return log_store_.has_unsent_logs(); }
private:
// ReportingService:
LogStore* log_store() override { return &log_store_; }
GURL GetUploadUrl() const override { return GURL(kTestUploadUrl); }
GURL GetInsecureUploadUrl() const override { return GURL(kTestUploadUrl); }
base::StringPiece upload_mime_type() const override { return kTestMimeType; }
MetricsLogUploader::MetricServiceType service_type() const override {
return MetricsLogUploader::MetricServiceType::UMA;
}
TestLogStore log_store_;
};
class ReportingServiceTest : public testing::Test {
public:
ReportingServiceTest()
: task_runner_(new base::TestSimpleTaskRunner),
task_runner_current_default_handle_(task_runner_) {
ReportingService::RegisterPrefs(testing_local_state_.registry());
}
ReportingServiceTest(const ReportingServiceTest&) = delete;
ReportingServiceTest& operator=(const ReportingServiceTest&) = delete;
~ReportingServiceTest() override {}
PrefService* GetLocalState() { return &testing_local_state_; }
protected:
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
base::SingleThreadTaskRunner::CurrentDefaultHandle
task_runner_current_default_handle_;
TestMetricsServiceClient client_;
private:
TestingPrefServiceSimple testing_local_state_;
};
} // namespace
TEST_F(ReportingServiceTest, BasicTest) {
TestReportingService service(&client_, GetLocalState());
service.AddLog(TestLog("log1"));
service.AddLog(TestLog("log2"));
service.EnableReporting();
task_runner_->RunPendingTasks();
EXPECT_TRUE(client_.uploader()->is_uploading());
EXPECT_EQ(1, client_.uploader()->reporting_info().attempt_count());
EXPECT_FALSE(client_.uploader()->reporting_info().has_last_response_code());
client_.uploader()->CompleteUpload(404);
task_runner_->RunPendingTasks();
EXPECT_TRUE(client_.uploader()->is_uploading());
EXPECT_EQ(2, client_.uploader()->reporting_info().attempt_count());
EXPECT_EQ(404, client_.uploader()->reporting_info().last_response_code());
client_.uploader()->CompleteUpload(200);
task_runner_->RunPendingTasks();
EXPECT_TRUE(client_.uploader()->is_uploading());
EXPECT_EQ(1, client_.uploader()->reporting_info().attempt_count());
EXPECT_EQ(200, client_.uploader()->reporting_info().last_response_code());
client_.uploader()->CompleteUpload(200);
EXPECT_EQ(0U, task_runner_->NumPendingTasks());
EXPECT_FALSE(client_.uploader()->is_uploading());
}
TEST_F(ReportingServiceTest, UserIdLogsUploadedIfUserConsented) {
uint64_t user_id = 12345;
TestReportingService service(&client_, GetLocalState());
service.AddLog(TestLog("log1", user_id));
service.AddLog(TestLog("log2", user_id));
service.EnableReporting();
client_.AllowMetricUploadForUserId(user_id);
task_runner_->RunPendingTasks();
EXPECT_TRUE(client_.uploader()->is_uploading());
EXPECT_EQ(1, client_.uploader()->reporting_info().attempt_count());
EXPECT_FALSE(client_.uploader()->reporting_info().has_last_response_code());
client_.uploader()->CompleteUpload(200);
// Upload 2nd log and last response code logged.
task_runner_->RunPendingTasks();
EXPECT_EQ(200, client_.uploader()->reporting_info().last_response_code());
EXPECT_TRUE(client_.uploader()->is_uploading());
client_.uploader()->CompleteUpload(200);
EXPECT_EQ(0U, task_runner_->NumPendingTasks());
EXPECT_FALSE(client_.uploader()->is_uploading());
}
TEST_F(ReportingServiceTest, UserIdLogsNotUploadedIfUserNotConsented) {
TestReportingService service(&client_, GetLocalState());
service.AddLog(TestLog("log1", 12345));
service.AddLog(TestLog("log2", 12345));
service.EnableReporting();
// Log with user id should never be in uploading state if user upload
// disabled. |client_.uploader()| should be nullptr since it is lazily
// created when a log is to be uploaded for the first time.
EXPECT_EQ(client_.uploader(), nullptr);
}
TEST_F(ReportingServiceTest, ForceDiscard) {
TestReportingService service(&client_, GetLocalState());
service.AddLog(TestLog("log1"));
service.EnableReporting();
// Simulate the server returning a 500 error, which indicates that the server
// is unhealthy.
task_runner_->RunPendingTasks();
EXPECT_TRUE(client_.uploader()->is_uploading());
client_.uploader()->CompleteUpload(500);
task_runner_->RunPendingTasks();
// Verify that the log is not discarded so that it can be re-sent later.
EXPECT_TRUE(service.HasUnsentLogs());
EXPECT_TRUE(client_.uploader()->is_uploading());
// Simulate the server returning a 500 error again, but this time, with
// |force_discard| set to true.
client_.uploader()->CompleteUpload(500, /*force_discard=*/true);
task_runner_->RunPendingTasks();
// Verify that the log was discarded, and that |service| is not uploading
// anymore since there are no more logs.
EXPECT_FALSE(service.HasUnsentLogs());
EXPECT_EQ(0U, task_runner_->NumPendingTasks());
EXPECT_FALSE(client_.uploader()->is_uploading());
}
} // namespace metrics
|