File: sync_stopped_reporter_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (182 lines) | stat: -rw-r--r-- 7,239 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
// Copyright 2015 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/sync/service/sync_stopped_reporter.h"

#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_mock_time_message_loop_task_runner.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/sync/protocol/sync.pb.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/resource_request.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/test/test_url_loader_factory.h"
#include "services/network/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace syncer {

const char kTestURL[] = "http://chromium.org/test";
const char kTestURLTrailingSlash[] = "http://chromium.org/test/";
const char kEventURL[] = "http://chromium.org/test/event";

const char kTestUserAgent[] = "the_fifth_element";
const char kAuthToken[] = "multipass";
const char kCacheGuid[] = "leeloo";
const char kBirthday[] = "2263";

const char kAuthHeaderPrefix[] = "Bearer ";

class SyncStoppedReporterTest : public testing::Test {
 public:
  SyncStoppedReporterTest(const SyncStoppedReporterTest&) = delete;
  SyncStoppedReporterTest& operator=(const SyncStoppedReporterTest&) = delete;

 protected:
  SyncStoppedReporterTest() = default;
  ~SyncStoppedReporterTest() override = default;

  void SetUp() override {
    test_shared_loader_factory_ =
        base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
            url_loader_factory());
  }

  GURL interception_url(const GURL& url) {
    return SyncStoppedReporter::GetSyncEventURL(url);
  }

  GURL test_url() { return GURL(kTestURL); }

  std::string user_agent() const { return std::string(kTestUserAgent); }

  network::TestURLLoaderFactory* url_loader_factory() {
    return &test_url_loader_factory_;
  }

  scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory() {
    return test_shared_loader_factory_;
  }

 private:
  base::test::SingleThreadTaskEnvironment task_environment_;
  network::TestURLLoaderFactory test_url_loader_factory_;
  scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
};

// Test that the event URL gets constructed correctly.
TEST_F(SyncStoppedReporterTest, EventURL) {
  GURL intercepted_url;
  url_loader_factory()->AddResponse(interception_url(GURL(kTestURL)).spec(),
                                    "");
  url_loader_factory()->SetInterceptor(
      base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
        intercepted_url = request.url;
      }));
  SyncStoppedReporter ssr(GURL(kTestURL), user_agent(),
                          shared_url_loader_factory());
  ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
  EXPECT_EQ(kEventURL, intercepted_url.spec());
}

// Test that the event URL gets constructed correctly with a trailing slash.
TEST_F(SyncStoppedReporterTest, EventURLWithSlash) {
  GURL intercepted_url;
  url_loader_factory()->AddResponse(
      interception_url(GURL(kTestURLTrailingSlash)).spec(), "");
  url_loader_factory()->SetInterceptor(
      base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
        intercepted_url = request.url;
      }));
  SyncStoppedReporter ssr(GURL(kTestURLTrailingSlash), user_agent(),
                          shared_url_loader_factory());
  ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
  EXPECT_EQ(kEventURL, intercepted_url.spec());
}

// Test that the URLFetcher gets configured correctly.
TEST_F(SyncStoppedReporterTest, FetcherConfiguration) {
  GURL intercepted_url;
  net::HttpRequestHeaders intercepted_headers;
  std::string intercepted_body;
  url_loader_factory()->AddResponse(interception_url(test_url()).spec(), "");
  url_loader_factory()->SetInterceptor(
      base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
        intercepted_url = request.url;
        intercepted_headers = request.headers;
        intercepted_body = network::GetUploadData(request);
      }));
  SyncStoppedReporter ssr(test_url(), user_agent(),
                          shared_url_loader_factory());
  ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);

  // Ensure the headers are set correctly.
  EXPECT_EQ(
      base::StrCat({kAuthHeaderPrefix, kAuthToken}),
      intercepted_headers.GetHeader(net::HttpRequestHeaders::kAuthorization)
          .value_or(std::string()));
  EXPECT_EQ(user_agent(),
            intercepted_headers.GetHeader(net::HttpRequestHeaders::kUserAgent)
                .value_or(std::string()));

  sync_pb::EventRequest event_request;
  event_request.ParseFromString(intercepted_body);

  EXPECT_EQ(kCacheGuid, event_request.sync_disabled().cache_guid());
  EXPECT_EQ(kBirthday, event_request.sync_disabled().store_birthday());
  EXPECT_EQ(kEventURL, intercepted_url.spec());
}

TEST_F(SyncStoppedReporterTest, HappyCase) {
  base::HistogramTester histogram_tester;
  url_loader_factory()->AddResponse(interception_url(test_url()).spec(), "");
  SyncStoppedReporter ssr(test_url(), user_agent(),
                          shared_url_loader_factory());
  ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
  base::RunLoop run_loop;
  run_loop.RunUntilIdle();
  histogram_tester.ExpectUniqueSample("Sync.SyncStoppedURLFetchResponse",
                                      net::HTTP_OK, 1);
  histogram_tester.ExpectUniqueSample("Sync.SyncStoppedURLFetchTimedOut", false,
                                      1);
}

TEST_F(SyncStoppedReporterTest, ServerNotFound) {
  base::HistogramTester histogram_tester;
  url_loader_factory()->AddResponse(interception_url(test_url()).spec(), "",
                                    net::HTTP_NOT_FOUND);
  SyncStoppedReporter ssr(test_url(), user_agent(),
                          shared_url_loader_factory());
  ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);
  base::RunLoop run_loop;
  run_loop.RunUntilIdle();
  histogram_tester.ExpectUniqueSample("Sync.SyncStoppedURLFetchResponse",
                                      net::ERR_HTTP_RESPONSE_CODE_FAILURE, 1);
  histogram_tester.ExpectTotalCount("Sync.SyncStoppedURLFetchTimedOut", 0);
}

TEST_F(SyncStoppedReporterTest, Timeout) {
  base::HistogramTester histogram_tester;
  // Mock the underlying loop's clock to trigger the timer at will.
  base::ScopedMockTimeMessageLoopTaskRunner mock_main_runner;
  // No TestURLLoaderFactory::AddResponse(), so the request stays pending.

  SyncStoppedReporter ssr(test_url(), user_agent(),
                          shared_url_loader_factory());

  // Begin request.
  ssr.ReportSyncStopped(kAuthToken, kCacheGuid, kBirthday);

  // Trigger the timeout, 30 seconds should be more than enough.
  ASSERT_TRUE(mock_main_runner->HasPendingTask());
  mock_main_runner->FastForwardBy(base::Seconds(30));
  histogram_tester.ExpectUniqueSample("Sync.SyncStoppedURLFetchTimedOut", true,
                                      1);
}

}  // namespace syncer