1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/metrics/metrics_service.h"
6
7 #include "base/command_line.h"
8 #include "base/functional/bind.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "components/metrics/metrics_switches.h"
11 #include "components/metrics/test/test_metrics_service_client.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace metrics {
15 namespace {
16
17 class MetricsServiceClientTest : public testing::Test {
18 public:
MetricsServiceClientTest()19 MetricsServiceClientTest() {}
20
21 MetricsServiceClientTest(const MetricsServiceClientTest&) = delete;
22 MetricsServiceClientTest& operator=(const MetricsServiceClientTest&) = delete;
23
~MetricsServiceClientTest()24 ~MetricsServiceClientTest() override {}
25 };
26
27 } // namespace
28
TEST_F(MetricsServiceClientTest,TestUploadIntervalDefaultsToStandard)29 TEST_F(MetricsServiceClientTest, TestUploadIntervalDefaultsToStandard) {
30 TestMetricsServiceClient client;
31
32 ASSERT_EQ(client.GetStandardUploadInterval(), client.GetUploadInterval());
33 }
34
TEST_F(MetricsServiceClientTest,TestModifyMetricsUploadInterval)35 TEST_F(MetricsServiceClientTest, TestModifyMetricsUploadInterval) {
36 TestMetricsServiceClient client;
37
38 // Flip it a few times to make sure we really can modify it. Values are
39 // arbitrary (but positive, because the upload interval should be).
40 int specified_upload_sec = 800;
41 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
42 switches::kMetricsUploadIntervalSec,
43 base::NumberToString(specified_upload_sec));
44 ASSERT_EQ(base::Seconds(specified_upload_sec), client.GetUploadInterval());
45
46 base::CommandLine::ForCurrentProcess()->RemoveSwitch(
47 switches::kMetricsUploadIntervalSec);
48
49 specified_upload_sec = 30;
50 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
51 switches::kMetricsUploadIntervalSec,
52 base::NumberToString(specified_upload_sec));
53 ASSERT_EQ(base::Seconds(specified_upload_sec), client.GetUploadInterval());
54 }
55
TEST_F(MetricsServiceClientTest,TestUploadIntervalLimitedForDos)56 TEST_F(MetricsServiceClientTest, TestUploadIntervalLimitedForDos) {
57 TestMetricsServiceClient client;
58
59 // If we set the upload interval too small, it should be limited to prevent
60 // the possibility of DOS'ing the backend. This should be a safe guess for a
61 // value strictly smaller than the DOS limit.
62 int too_short_upload_sec = 2;
63 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
64 switches::kMetricsUploadIntervalSec,
65 base::NumberToString(too_short_upload_sec));
66 // Upload interval should be the DOS rate limit.
67 ASSERT_EQ(base::Seconds(20), client.GetUploadInterval());
68 }
69
70 } // namespace metrics
71