report_sender_unittest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // Copyright 2015 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. #include "net/url_request/report_sender.h"
  5. #include "base/bind.h"
  6. #include "base/callback_helpers.h"
  7. #include "base/run_loop.h"
  8. #include "base/threading/thread_task_runner_handle.h"
  9. #include "net/base/load_flags.h"
  10. #include "net/base/network_delegate_impl.h"
  11. #include "net/base/upload_bytes_element_reader.h"
  12. #include "net/base/upload_data_stream.h"
  13. #include "net/base/upload_element_reader.h"
  14. #include "net/http/http_response_headers.h"
  15. #include "net/http/http_status_code.h"
  16. #include "net/test/test_with_task_environment.h"
  17. #include "net/test/url_request/url_request_failed_job.h"
  18. #include "net/test/url_request/url_request_mock_data_job.h"
  19. #include "net/test/url_request/url_request_mock_http_job.h"
  20. #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
  21. #include "net/url_request/url_request_context.h"
  22. #include "net/url_request/url_request_context_builder.h"
  23. #include "net/url_request/url_request_filter.h"
  24. #include "net/url_request/url_request_test_util.h"
  25. #include "testing/gtest/include/gtest/gtest.h"
  26. namespace net {
  27. namespace {
  28. const char kDummyReport[] = "foo.test";
  29. const char kSecondDummyReport[] = "foo2.test";
  30. const char kServerErrorHostname[] = "mock.server.error";
  31. void MarkURLRequestDestroyed(bool* url_request_destroyed) {
  32. *url_request_destroyed = true;
  33. }
  34. // Checks that data uploaded in the request matches the test report
  35. // data. Erases the sent reports from |expect_reports|.
  36. void CheckUploadData(const URLRequest& request,
  37. std::set<std::string>* expect_reports) {
  38. const UploadDataStream* upload = request.get_upload_for_testing();
  39. ASSERT_TRUE(upload);
  40. ASSERT_TRUE(upload->GetElementReaders());
  41. ASSERT_EQ(1u, upload->GetElementReaders()->size());
  42. const UploadBytesElementReader* reader =
  43. (*upload->GetElementReaders())[0]->AsBytesReader();
  44. ASSERT_TRUE(reader);
  45. std::string upload_data(reader->bytes(), reader->length());
  46. EXPECT_EQ(1u, expect_reports->erase(upload_data));
  47. }
  48. // Error callback for a report with a net error.
  49. void ErrorCallback(bool* called,
  50. const GURL& report_uri,
  51. int net_error,
  52. int http_response_code) {
  53. EXPECT_NE(OK, net_error);
  54. EXPECT_EQ(-1, http_response_code);
  55. *called = true;
  56. }
  57. // Error callback for a report with a non-200 HTTP response code and no net
  58. // errors.
  59. void ServerErrorResponseCallback(bool* called,
  60. const GURL& report_uri,
  61. int net_error,
  62. int http_response_code) {
  63. EXPECT_EQ(OK, net_error);
  64. EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR, http_response_code);
  65. *called = true;
  66. }
  67. void SuccessCallback(bool* called) {
  68. *called = true;
  69. }
  70. // URLRequestJob that returns an HTTP 500 response.
  71. class MockServerErrorJob : public URLRequestJob {
  72. public:
  73. explicit MockServerErrorJob(URLRequest* request) : URLRequestJob(request) {}
  74. MockServerErrorJob(const MockServerErrorJob&) = delete;
  75. MockServerErrorJob& operator=(const MockServerErrorJob&) = delete;
  76. ~MockServerErrorJob() override = default;
  77. protected:
  78. void GetResponseInfo(HttpResponseInfo* info) override {
  79. info->headers = base::MakeRefCounted<HttpResponseHeaders>(
  80. "HTTP/1.1 500 Internal Server Error\n"
  81. "Content-type: text/plain\n"
  82. "Content-Length: 0\n");
  83. }
  84. void Start() override { NotifyHeadersComplete(); }
  85. };
  86. class MockServerErrorJobInterceptor : public URLRequestInterceptor {
  87. public:
  88. MockServerErrorJobInterceptor() = default;
  89. MockServerErrorJobInterceptor(const MockServerErrorJobInterceptor&) = delete;
  90. MockServerErrorJobInterceptor& operator=(
  91. const MockServerErrorJobInterceptor&) = delete;
  92. ~MockServerErrorJobInterceptor() override = default;
  93. std::unique_ptr<URLRequestJob> MaybeInterceptRequest(
  94. URLRequest* request) const override {
  95. return std::make_unique<MockServerErrorJob>(request);
  96. }
  97. };
  98. // A network delegate that lets tests check that a report
  99. // was sent. It counts the number of requests and lets tests register a
  100. // callback to run when the request is destroyed. It also checks that
  101. // the uploaded data is as expected.
  102. class TestReportSenderNetworkDelegate : public NetworkDelegateImpl {
  103. public:
  104. TestReportSenderNetworkDelegate()
  105. : url_request_destroyed_callback_(base::DoNothing()),
  106. all_url_requests_destroyed_callback_(base::DoNothing()) {}
  107. TestReportSenderNetworkDelegate(const TestReportSenderNetworkDelegate&) =
  108. delete;
  109. TestReportSenderNetworkDelegate& operator=(
  110. const TestReportSenderNetworkDelegate&) = delete;
  111. void ExpectReport(const std::string& report) {
  112. expect_reports_.insert(report);
  113. }
  114. void set_all_url_requests_destroyed_callback(
  115. base::RepeatingClosure callback) {
  116. all_url_requests_destroyed_callback_ = std::move(callback);
  117. }
  118. void set_url_request_destroyed_callback(base::RepeatingClosure callback) {
  119. url_request_destroyed_callback_ = std::move(callback);
  120. }
  121. void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; }
  122. size_t num_requests() const { return num_requests_; }
  123. void set_expected_content_type(const std::string& content_type) {
  124. expected_content_type_ = content_type;
  125. }
  126. void set_expected_network_isolation_key(
  127. const NetworkIsolationKey& expected_network_isolation_key) {
  128. expected_network_isolation_key_ = expected_network_isolation_key;
  129. }
  130. // NetworkDelegateImpl implementation.
  131. int OnBeforeURLRequest(URLRequest* request,
  132. CompletionOnceCallback callback,
  133. GURL* new_url) override {
  134. num_requests_++;
  135. EXPECT_EQ(expect_url_, request->url());
  136. EXPECT_STRCASEEQ("POST", request->method().data());
  137. EXPECT_FALSE(request->allow_credentials());
  138. EXPECT_TRUE(request->load_flags() & LOAD_DO_NOT_SAVE_COOKIES);
  139. EXPECT_EQ(expected_network_isolation_key_,
  140. request->isolation_info().network_isolation_key());
  141. EXPECT_EQ(IsolationInfo::RequestType::kOther,
  142. request->isolation_info().request_type());
  143. EXPECT_TRUE(request->site_for_cookies().IsNull());
  144. const HttpRequestHeaders& extra_headers = request->extra_request_headers();
  145. std::string content_type;
  146. EXPECT_TRUE(extra_headers.GetHeader(HttpRequestHeaders::kContentType,
  147. &content_type));
  148. EXPECT_EQ(expected_content_type_, content_type);
  149. CheckUploadData(*request, &expect_reports_);
  150. // Unconditionally return OK, since the sender ignores the results
  151. // anyway.
  152. return OK;
  153. }
  154. void OnURLRequestDestroyed(URLRequest* request) override {
  155. url_request_destroyed_callback_.Run();
  156. if (expect_reports_.empty())
  157. all_url_requests_destroyed_callback_.Run();
  158. }
  159. private:
  160. base::RepeatingClosure url_request_destroyed_callback_;
  161. base::RepeatingClosure all_url_requests_destroyed_callback_;
  162. size_t num_requests_ = 0;
  163. GURL expect_url_;
  164. std::set<std::string> expect_reports_;
  165. std::string expected_content_type_;
  166. NetworkIsolationKey expected_network_isolation_key_;
  167. };
  168. class ReportSenderTest : public TestWithTaskEnvironment {
  169. public:
  170. ReportSenderTest() {
  171. auto builder = CreateTestURLRequestContextBuilder();
  172. builder->set_network_delegate(
  173. std::make_unique<TestReportSenderNetworkDelegate>());
  174. context_ = builder->Build();
  175. }
  176. void SetUp() override {
  177. URLRequestFailedJob::AddUrlHandler();
  178. URLRequestMockDataJob::AddUrlHandler();
  179. URLRequestFilter::GetInstance()->AddHostnameInterceptor(
  180. "http", kServerErrorHostname,
  181. std::make_unique<MockServerErrorJobInterceptor>());
  182. }
  183. void TearDown() override { URLRequestFilter::GetInstance()->ClearHandlers(); }
  184. URLRequestContext* context() { return context_.get(); }
  185. TestReportSenderNetworkDelegate& network_delegate() {
  186. // This cast is safe because we set a TestReportSenderNetworkDelegate in the
  187. // constructor.
  188. return *static_cast<TestReportSenderNetworkDelegate*>(
  189. context_->network_delegate());
  190. }
  191. protected:
  192. void SendReport(
  193. ReportSender* reporter,
  194. const std::string& report,
  195. const GURL& url,
  196. size_t request_sequence_number,
  197. base::OnceCallback<void()> success_callback,
  198. base::OnceCallback<void(const GURL&, int, int)> error_callback) {
  199. NetworkIsolationKey network_isolation_key =
  200. NetworkIsolationKey::CreateTransient();
  201. base::RunLoop run_loop;
  202. network_delegate().set_url_request_destroyed_callback(
  203. run_loop.QuitClosure());
  204. network_delegate().set_expect_url(url);
  205. network_delegate().ExpectReport(report);
  206. network_delegate().set_expected_content_type("application/foobar");
  207. network_delegate().set_expected_network_isolation_key(
  208. network_isolation_key);
  209. EXPECT_EQ(request_sequence_number, network_delegate().num_requests());
  210. reporter->Send(url, "application/foobar", report, network_isolation_key,
  211. std::move(success_callback), std::move(error_callback));
  212. // The report is sent asynchronously, so wait for the report's
  213. // URLRequest to be destroyed before checking that the report was
  214. // sent.
  215. run_loop.Run();
  216. EXPECT_EQ(request_sequence_number + 1, network_delegate().num_requests());
  217. }
  218. void SendReport(ReportSender* reporter,
  219. const std::string& report,
  220. const GURL& url,
  221. size_t request_sequence_number) {
  222. SendReport(reporter, report, url, request_sequence_number,
  223. base::OnceCallback<void()>(),
  224. base::OnceCallback<void(const GURL&, int, int)>());
  225. }
  226. private:
  227. std::unique_ptr<URLRequestContext> context_;
  228. };
  229. // Test that ReportSender::Send creates a URLRequest for the
  230. // endpoint and sends the expected data.
  231. TEST_F(ReportSenderTest, SendsRequest) {
  232. GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
  233. ReportSender reporter(context(), TRAFFIC_ANNOTATION_FOR_TESTS);
  234. SendReport(&reporter, kDummyReport, url, 0);
  235. }
  236. TEST_F(ReportSenderTest, SendMultipleReportsSequentially) {
  237. GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
  238. ReportSender reporter(context(), TRAFFIC_ANNOTATION_FOR_TESTS);
  239. SendReport(&reporter, kDummyReport, url, 0);
  240. SendReport(&reporter, kDummyReport, url, 1);
  241. }
  242. TEST_F(ReportSenderTest, SendMultipleReportsSimultaneously) {
  243. base::RunLoop run_loop;
  244. network_delegate().set_all_url_requests_destroyed_callback(
  245. run_loop.QuitClosure());
  246. GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
  247. network_delegate().set_expect_url(url);
  248. network_delegate().ExpectReport(kDummyReport);
  249. network_delegate().ExpectReport(kSecondDummyReport);
  250. network_delegate().set_expected_content_type("application/foobar");
  251. ReportSender reporter(context(), TRAFFIC_ANNOTATION_FOR_TESTS);
  252. EXPECT_EQ(0u, network_delegate().num_requests());
  253. reporter.Send(url, "application/foobar", kDummyReport, NetworkIsolationKey(),
  254. base::OnceCallback<void()>(),
  255. base::OnceCallback<void(const GURL&, int, int)>());
  256. reporter.Send(url, "application/foobar", kSecondDummyReport,
  257. NetworkIsolationKey(), base::OnceCallback<void()>(),
  258. base::OnceCallback<void(const GURL&, int, int)>());
  259. run_loop.Run();
  260. EXPECT_EQ(2u, network_delegate().num_requests());
  261. }
  262. // Test that pending URLRequests get cleaned up when the report sender
  263. // is deleted.
  264. TEST_F(ReportSenderTest, PendingRequestGetsDeleted) {
  265. bool url_request_destroyed = false;
  266. network_delegate().set_url_request_destroyed_callback(base::BindRepeating(
  267. &MarkURLRequestDestroyed, base::Unretained(&url_request_destroyed)));
  268. GURL url = URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
  269. URLRequestFailedJob::START, ERR_IO_PENDING);
  270. network_delegate().set_expect_url(url);
  271. network_delegate().ExpectReport(kDummyReport);
  272. network_delegate().set_expected_content_type("application/foobar");
  273. EXPECT_EQ(0u, network_delegate().num_requests());
  274. auto reporter =
  275. std::make_unique<ReportSender>(context(), TRAFFIC_ANNOTATION_FOR_TESTS);
  276. reporter->Send(url, "application/foobar", kDummyReport, NetworkIsolationKey(),
  277. base::OnceCallback<void()>(),
  278. base::OnceCallback<void(const GURL&, int, int)>());
  279. reporter.reset();
  280. EXPECT_EQ(1u, network_delegate().num_requests());
  281. EXPECT_TRUE(url_request_destroyed);
  282. }
  283. // Test that a request that returns an error gets cleaned up.
  284. TEST_F(ReportSenderTest, ErroredRequestGetsDeleted) {
  285. GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED);
  286. ReportSender reporter(context(), TRAFFIC_ANNOTATION_FOR_TESTS);
  287. // SendReport will block until the URLRequest is destroyed.
  288. SendReport(&reporter, kDummyReport, url, 0);
  289. }
  290. // Test that the error callback, if provided, gets called when a request
  291. // returns an error and the success callback doesn't get called.
  292. TEST_F(ReportSenderTest, ErroredRequestCallsErrorCallback) {
  293. bool error_callback_called = false;
  294. bool success_callback_called = false;
  295. const GURL url = URLRequestFailedJob::GetMockHttpsUrl(ERR_FAILED);
  296. ReportSender reporter(context(), TRAFFIC_ANNOTATION_FOR_TESTS);
  297. // SendReport will block until the URLRequest is destroyed.
  298. SendReport(&reporter, kDummyReport, url, 0,
  299. base::BindOnce(SuccessCallback, &success_callback_called),
  300. base::BindOnce(ErrorCallback, &error_callback_called));
  301. EXPECT_TRUE(error_callback_called);
  302. EXPECT_FALSE(success_callback_called);
  303. }
  304. // Test that the error callback, if provided, gets called when a request
  305. // finishes successfully but results in a server error, and the success callback
  306. // doesn't get called.
  307. TEST_F(ReportSenderTest, BadResponseCodeCallsErrorCallback) {
  308. bool error_callback_called = false;
  309. bool success_callback_called = false;
  310. const GURL url(std::string("http://") + kServerErrorHostname);
  311. ReportSender reporter(context(), TRAFFIC_ANNOTATION_FOR_TESTS);
  312. // SendReport will block until the URLRequest is destroyed.
  313. SendReport(
  314. &reporter, kDummyReport, url, 0,
  315. base::BindOnce(SuccessCallback, &success_callback_called),
  316. base::BindOnce(ServerErrorResponseCallback, &error_callback_called));
  317. EXPECT_TRUE(error_callback_called);
  318. EXPECT_FALSE(success_callback_called);
  319. }
  320. // Test that the error callback does not get called and the success callback
  321. /// gets called when a request does not return an error.
  322. TEST_F(ReportSenderTest, SuccessfulRequestCallsSuccessCallback) {
  323. bool error_callback_called = false;
  324. bool success_callback_called = false;
  325. const GURL url = URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1);
  326. ReportSender reporter(context(), TRAFFIC_ANNOTATION_FOR_TESTS);
  327. SendReport(&reporter, kDummyReport, url, 0,
  328. base::BindOnce(SuccessCallback, &success_callback_called),
  329. base::BindOnce(ErrorCallback, &error_callback_called));
  330. EXPECT_FALSE(error_callback_called);
  331. EXPECT_TRUE(success_callback_called);
  332. }
  333. } // namespace
  334. } // namespace net