connect_job_test_util.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2018 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/socket/connect_job_test_util.h"
  5. #include <utility>
  6. #include "base/check.h"
  7. #include "base/run_loop.h"
  8. #include "net/socket/stream_socket.h"
  9. #include "net/test/gtest_util.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace net {
  12. TestConnectJobDelegate::TestConnectJobDelegate(SocketExpected socket_expected)
  13. : socket_expected_(socket_expected) {}
  14. TestConnectJobDelegate::~TestConnectJobDelegate() = default;
  15. void TestConnectJobDelegate::OnConnectJobComplete(int result, ConnectJob* job) {
  16. EXPECT_FALSE(has_result_);
  17. result_ = result;
  18. socket_ = job->PassSocket();
  19. EXPECT_EQ(socket_.get() != nullptr,
  20. result == OK || socket_expected_ == SocketExpected::ALWAYS);
  21. // On success, generally end up with a connected socket. Could theoretically
  22. // be racily disconnected before it was returned, but that case isn't tested
  23. // with this class.
  24. if (result == OK)
  25. EXPECT_TRUE(socket_->IsConnected());
  26. has_result_ = true;
  27. run_loop_.Quit();
  28. }
  29. void TestConnectJobDelegate::OnNeedsProxyAuth(
  30. const HttpResponseInfo& response,
  31. HttpAuthController* auth_controller,
  32. base::OnceClosure restart_with_auth_callback,
  33. ConnectJob* job) {
  34. EXPECT_TRUE(auth_controller);
  35. EXPECT_TRUE(restart_with_auth_callback);
  36. EXPECT_FALSE(has_result_);
  37. EXPECT_FALSE(auth_controller_);
  38. EXPECT_FALSE(restart_with_auth_callback_);
  39. num_auth_challenges_++;
  40. auth_response_info_ = response;
  41. auth_controller_ = auth_controller;
  42. restart_with_auth_callback_ = std::move(restart_with_auth_callback);
  43. if (auth_challenge_run_loop_)
  44. auth_challenge_run_loop_->Quit();
  45. }
  46. void TestConnectJobDelegate::WaitForAuthChallenge(
  47. int num_auth_challenges_to_wait_for) {
  48. // It a bit strange to call this after a job has already complete, and doing
  49. // so probably indicates a bug.
  50. EXPECT_FALSE(has_result_);
  51. while (num_auth_challenges_ < num_auth_challenges_to_wait_for) {
  52. auth_challenge_run_loop_ = std::make_unique<base::RunLoop>();
  53. auth_challenge_run_loop_->Run();
  54. auth_challenge_run_loop_.reset();
  55. }
  56. EXPECT_EQ(num_auth_challenges_to_wait_for, num_auth_challenges_);
  57. }
  58. void TestConnectJobDelegate::RunAuthCallback() {
  59. ASSERT_TRUE(restart_with_auth_callback_);
  60. auth_controller_ = nullptr;
  61. std::move(restart_with_auth_callback_).Run();
  62. }
  63. int TestConnectJobDelegate::WaitForResult() {
  64. run_loop_.Run();
  65. DCHECK(has_result_);
  66. return result_;
  67. }
  68. void TestConnectJobDelegate::StartJobExpectingResult(ConnectJob* connect_job,
  69. net::Error expected_result,
  70. bool expect_sync_result) {
  71. int rv = connect_job->Connect();
  72. if (rv == ERR_IO_PENDING) {
  73. EXPECT_FALSE(expect_sync_result);
  74. EXPECT_THAT(WaitForResult(), test::IsError(expected_result));
  75. } else {
  76. EXPECT_TRUE(expect_sync_result);
  77. // The callback should not have been invoked.
  78. ASSERT_FALSE(has_result_);
  79. OnConnectJobComplete(rv, connect_job);
  80. EXPECT_THAT(result_, test::IsError(expected_result));
  81. }
  82. }
  83. std::unique_ptr<StreamSocket> TestConnectJobDelegate::ReleaseSocket() {
  84. return std::move(socket_);
  85. }
  86. } // namespace net