[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 1 | // Copyright 2014 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. |
| 4 | |
| 5 | #include "components/rappor/log_uploader.h" |
| 6 | |
[email protected] | ccb4926 | 2014-03-26 04:10:17 | [diff] [blame] | 7 | #include "base/compiler_specific.h" |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 8 | #include "base/macros.h" |
fdoray | 87801424 | 2016-06-10 04:43:54 | [diff] [blame] | 9 | #include "base/run_loop.h" |
fdoray | 66a9a08 | 2017-05-01 18:31:27 | [diff] [blame] | 10 | #include "base/test/scoped_task_environment.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 11 | #include "base/threading/thread_task_runner_handle.h" |
Mark Pilgrim | edc1d1563 | 2018-05-22 17:38:23 | [diff] [blame] | 12 | #include "net/http/http_response_headers.h" |
| 13 | #include "net/http/http_status_code.h" |
| 14 | #include "net/http/http_util.h" |
| 15 | #include "services/network/public/cpp/shared_url_loader_factory.h" |
| 16 | #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h" |
| 17 | #include "services/network/test/test_url_loader_factory.h" |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | namespace rappor { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | const char kTestServerURL[] = "http://a.com/"; |
| 25 | const char kTestMimeType[] = "text/plain"; |
| 26 | |
| 27 | class TestLogUploader : public LogUploader { |
| 28 | public: |
Mark Pilgrim | edc1d1563 | 2018-05-22 17:38:23 | [diff] [blame] | 29 | explicit TestLogUploader( |
| 30 | scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) |
| 31 | : LogUploader(GURL(kTestServerURL), kTestMimeType, url_loader_factory) { |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 32 | Start(); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 33 | } |
| 34 | |
thestig | 550eecbe | 2016-04-22 23:55:47 | [diff] [blame] | 35 | base::TimeDelta last_interval_set() const { return last_interval_set_; } |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 36 | |
| 37 | void StartUpload() { |
| 38 | last_interval_set_ = base::TimeDelta(); |
| 39 | StartScheduledUpload(); |
| 40 | } |
| 41 | |
| 42 | static base::TimeDelta BackOff(base::TimeDelta t) { |
| 43 | return LogUploader::BackOffUploadInterval(t); |
| 44 | } |
| 45 | |
| 46 | protected: |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 47 | bool IsUploadScheduled() const override { |
thestig | 550eecbe | 2016-04-22 23:55:47 | [diff] [blame] | 48 | return !last_interval_set().is_zero(); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Schedules a future call to StartScheduledUpload if one isn't already |
| 52 | // pending. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 53 | void ScheduleNextUpload(base::TimeDelta interval) override { |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 54 | EXPECT_EQ(last_interval_set(), base::TimeDelta()); |
| 55 | last_interval_set_ = interval; |
| 56 | } |
| 57 | |
| 58 | base::TimeDelta last_interval_set_; |
| 59 | |
thestig | 550eecbe | 2016-04-22 23:55:47 | [diff] [blame] | 60 | private: |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 61 | DISALLOW_COPY_AND_ASSIGN(TestLogUploader); |
| 62 | }; |
| 63 | |
| 64 | } // namespace |
| 65 | |
| 66 | class LogUploaderTest : public testing::Test { |
| 67 | public: |
| 68 | LogUploaderTest() |
fdoray | 66a9a08 | 2017-05-01 18:31:27 | [diff] [blame] | 69 | : scoped_task_environment_( |
| 70 | base::test::ScopedTaskEnvironment::MainThreadType::UI), |
Mark Pilgrim | edc1d1563 | 2018-05-22 17:38:23 | [diff] [blame] | 71 | test_shared_loader_factory_( |
| 72 | base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( |
| 73 | &test_url_loader_factory_)) {} |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 74 | |
| 75 | protected: |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 76 | // Required for base::ThreadTaskRunnerHandle::Get(). |
fdoray | 66a9a08 | 2017-05-01 18:31:27 | [diff] [blame] | 77 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
Mark Pilgrim | edc1d1563 | 2018-05-22 17:38:23 | [diff] [blame] | 78 | network::TestURLLoaderFactory test_url_loader_factory_; |
| 79 | scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_; |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 80 | |
thestig | 550eecbe | 2016-04-22 23:55:47 | [diff] [blame] | 81 | private: |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 82 | DISALLOW_COPY_AND_ASSIGN(LogUploaderTest); |
| 83 | }; |
| 84 | |
| 85 | TEST_F(LogUploaderTest, Success) { |
Mark Pilgrim | edc1d1563 | 2018-05-22 17:38:23 | [diff] [blame] | 86 | TestLogUploader uploader(test_shared_loader_factory_); |
| 87 | test_url_loader_factory_.AddResponse(kTestServerURL, ""); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 88 | |
| 89 | uploader.QueueLog("log1"); |
fdoray | 87801424 | 2016-06-10 04:43:54 | [diff] [blame] | 90 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 91 | // Log should be discarded instead of retransmitted. |
| 92 | EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); |
| 93 | } |
| 94 | |
| 95 | TEST_F(LogUploaderTest, Rejection) { |
Mark Pilgrim | edc1d1563 | 2018-05-22 17:38:23 | [diff] [blame] | 96 | TestLogUploader uploader(test_shared_loader_factory_); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 97 | |
Mark Pilgrim | edc1d1563 | 2018-05-22 17:38:23 | [diff] [blame] | 98 | network::ResourceResponseHead response_head; |
| 99 | std::string headers("HTTP/1.1 400 Bad Request\nContent-type: text/html\n\n"); |
| 100 | response_head.headers = base::MakeRefCounted<net::HttpResponseHeaders>( |
| 101 | net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size())); |
| 102 | response_head.mime_type = "text/html"; |
| 103 | test_url_loader_factory_.AddResponse(GURL(kTestServerURL), response_head, "", |
| 104 | network::URLLoaderCompletionStatus()); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 105 | |
| 106 | uploader.QueueLog("log1"); |
fdoray | 87801424 | 2016-06-10 04:43:54 | [diff] [blame] | 107 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 108 | // Log should be discarded instead of retransmitted. |
| 109 | EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); |
| 110 | } |
| 111 | |
| 112 | TEST_F(LogUploaderTest, Failure) { |
Mark Pilgrim | edc1d1563 | 2018-05-22 17:38:23 | [diff] [blame] | 113 | TestLogUploader uploader(test_shared_loader_factory_); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 114 | |
Mark Pilgrim | edc1d1563 | 2018-05-22 17:38:23 | [diff] [blame] | 115 | network::ResourceResponseHead response_head; |
| 116 | std::string headers( |
| 117 | "HTTP/1.1 500 Internal Server Error\nContent-type: text/html\n\n"); |
| 118 | response_head.headers = base::MakeRefCounted<net::HttpResponseHeaders>( |
| 119 | net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size())); |
| 120 | response_head.mime_type = "text/html"; |
| 121 | test_url_loader_factory_.AddResponse(GURL(kTestServerURL), response_head, "", |
| 122 | network::URLLoaderCompletionStatus()); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 123 | |
| 124 | uploader.QueueLog("log1"); |
fdoray | 87801424 | 2016-06-10 04:43:54 | [diff] [blame] | 125 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 126 | // Log should be scheduled for retransmission. |
| 127 | base::TimeDelta error_interval = uploader.last_interval_set(); |
| 128 | EXPECT_GT(error_interval, base::TimeDelta()); |
| 129 | |
| 130 | for (int i = 0; i < 10; i++) { |
| 131 | uploader.QueueLog("logX"); |
| 132 | } |
| 133 | |
| 134 | // A second failure should lead to a longer interval, and the log should |
| 135 | // be discarded due to full queue. |
| 136 | uploader.StartUpload(); |
fdoray | 87801424 | 2016-06-10 04:43:54 | [diff] [blame] | 137 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 138 | EXPECT_GT(uploader.last_interval_set(), error_interval); |
| 139 | |
Mark Pilgrim | edc1d1563 | 2018-05-22 17:38:23 | [diff] [blame] | 140 | test_url_loader_factory_.AddResponse(kTestServerURL, ""); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 141 | |
| 142 | // A success should revert to base interval while queue is not empty. |
| 143 | for (int i = 0; i < 9; i++) { |
| 144 | uploader.StartUpload(); |
fdoray | 87801424 | 2016-06-10 04:43:54 | [diff] [blame] | 145 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 146 | EXPECT_LT(uploader.last_interval_set(), error_interval); |
| 147 | } |
| 148 | |
| 149 | // Queue should be empty. |
| 150 | uploader.StartUpload(); |
fdoray | 87801424 | 2016-06-10 04:43:54 | [diff] [blame] | 151 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2a172e4 | 2014-02-21 04:06:10 | [diff] [blame] | 152 | EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); |
| 153 | } |
| 154 | |
| 155 | TEST_F(LogUploaderTest, Backoff) { |
| 156 | base::TimeDelta current = base::TimeDelta(); |
| 157 | base::TimeDelta next = base::TimeDelta::FromSeconds(1); |
| 158 | // Backoff until the maximum is reached. |
| 159 | while (next > current) { |
| 160 | current = next; |
| 161 | next = TestLogUploader::BackOff(current); |
| 162 | } |
| 163 | // Maximum backoff should have been reached. |
| 164 | EXPECT_EQ(next, current); |
| 165 | } |
| 166 | |
| 167 | } // namespace rappor |