Add 'did_proceed' and 'repeat_visit' to ClientMalwareReportRequest to track CTR.
[chromium-blink-merge.git] / sync / internal_api / syncapi_server_connection_manager_unittest.cc
blobe438ac181fc06e9c8a17f9a68b6cd76841d8e1da
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sync/internal_api/syncapi_server_connection_manager.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/test/test_timeouts.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "net/base/net_errors.h"
15 #include "sync/internal_api/public/base/cancelation_signal.h"
16 #include "sync/internal_api/public/http_post_provider_factory.h"
17 #include "sync/internal_api/public/http_post_provider_interface.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace syncer {
21 namespace {
23 using base::TimeDelta;
25 class BlockingHttpPost : public HttpPostProviderInterface {
26 public:
27 BlockingHttpPost() : wait_for_abort_(false, false) {}
28 ~BlockingHttpPost() override {}
30 void SetExtraRequestHeaders(const char* headers) override {}
31 void SetURL(const char* url, int port) override {}
32 void SetPostPayload(const char* content_type,
33 int content_length,
34 const char* content) override {}
35 bool MakeSynchronousPost(int* error_code, int* response_code) override {
36 wait_for_abort_.TimedWait(TestTimeouts::action_max_timeout());
37 *error_code = net::ERR_ABORTED;
38 return false;
40 int GetResponseContentLength() const override { return 0; }
41 const char* GetResponseContent() const override { return ""; }
42 const std::string GetResponseHeaderValue(
43 const std::string& name) const override {
44 return std::string();
46 void Abort() override { wait_for_abort_.Signal(); }
47 private:
48 base::WaitableEvent wait_for_abort_;
51 class BlockingHttpPostFactory : public HttpPostProviderFactory {
52 public:
53 ~BlockingHttpPostFactory() override {}
54 void Init(const std::string& user_agent,
55 const BindToTrackerCallback& bind_to_tracker_callback) override {}
57 HttpPostProviderInterface* Create() override {
58 return new BlockingHttpPost();
60 void Destroy(HttpPostProviderInterface* http) override {
61 delete static_cast<BlockingHttpPost*>(http);
65 } // namespace
67 // Ask the ServerConnectionManager to stop before it is created.
68 TEST(SyncAPIServerConnectionManagerTest, VeryEarlyAbortPost) {
69 CancelationSignal signal;
70 signal.Signal();
71 SyncAPIServerConnectionManager server(
72 "server", 0, true, new BlockingHttpPostFactory(), &signal);
74 ServerConnectionManager::PostBufferParams params;
75 ScopedServerStatusWatcher watcher(&server, &params.response);
77 bool result = server.PostBufferToPath(
78 &params, "/testpath", "testauth", &watcher);
80 EXPECT_FALSE(result);
81 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE,
82 params.response.server_status);
85 // Ask the ServerConnectionManager to stop before its first request is made.
86 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) {
87 CancelationSignal signal;
88 SyncAPIServerConnectionManager server(
89 "server", 0, true, new BlockingHttpPostFactory(), &signal);
91 ServerConnectionManager::PostBufferParams params;
92 ScopedServerStatusWatcher watcher(&server, &params.response);
94 signal.Signal();
95 bool result = server.PostBufferToPath(
96 &params, "/testpath", "testauth", &watcher);
98 EXPECT_FALSE(result);
99 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE,
100 params.response.server_status);
103 // Ask the ServerConnectionManager to stop during a request.
104 TEST(SyncAPIServerConnectionManagerTest, AbortPost) {
105 CancelationSignal signal;
106 SyncAPIServerConnectionManager server(
107 "server", 0, true, new BlockingHttpPostFactory(), &signal);
109 ServerConnectionManager::PostBufferParams params;
110 ScopedServerStatusWatcher watcher(&server, &params.response);
112 base::Thread abort_thread("Test_AbortThread");
113 ASSERT_TRUE(abort_thread.Start());
114 abort_thread.message_loop()->PostDelayedTask(
115 FROM_HERE,
116 base::Bind(&CancelationSignal::Signal,
117 base::Unretained(&signal)),
118 TestTimeouts::tiny_timeout());
120 bool result = server.PostBufferToPath(
121 &params, "/testpath", "testauth", &watcher);
123 EXPECT_FALSE(result);
124 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE,
125 params.response.server_status);
126 abort_thread.Stop();
129 } // namespace syncer