File: connect_job_test_util.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; 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 (112 lines) | stat: -rw-r--r-- 3,785 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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_SOCKET_CONNECT_JOB_TEST_UTIL_H_
#define NET_SOCKET_CONNECT_JOB_TEST_UTIL_H_

#include <memory>
#include <set>

#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "net/base/net_errors.h"
#include "net/http/http_auth_controller.h"
#include "net/http/http_response_info.h"
#include "net/socket/connect_job.h"

namespace net {

class StreamSocket;

class TestConnectJobDelegate : public ConnectJob::Delegate {
 public:
  // Whether a socket should be returned. In most cases, no socket is returned
  // on failure; however, on certain SSL errors, a socket is returned in the
  // case of error.
  enum class SocketExpected {
    ON_SUCCESS_ONLY,
    ALWAYS,
  };

  explicit TestConnectJobDelegate(
      SocketExpected socket_expected = SocketExpected::ON_SUCCESS_ONLY);

  TestConnectJobDelegate(const TestConnectJobDelegate&) = delete;
  TestConnectJobDelegate& operator=(const TestConnectJobDelegate&) = delete;

  ~TestConnectJobDelegate() override;

  // ConnectJob::Delegate implementation.
  void OnConnectJobComplete(int result, ConnectJob* job) override;
  void OnNeedsProxyAuth(const HttpResponseInfo& response,
                        HttpAuthController* auth_controller,
                        base::OnceClosure restart_with_auth_callback,
                        ConnectJob* job) override;
  Error OnDestinationDnsAliasesResolved(const std::set<std::string>& aliases,
                                        ConnectJob* job) override;

  // Waits for the specified number of total auth challenges to be seen. Number
  // includes auth challenges that have already been waited for. Fails the test
  // if more auth challenges are seen than expected.
  void WaitForAuthChallenge(int num_auth_challenges_to_wait_for);

  void RunAuthCallback();

  // Waits for the ConnectJob to complete if it hasn't already and returns the
  // resulting network error code.
  int WaitForResult();

  int num_auth_challenges() const { return num_auth_challenges_; }
  const HttpResponseInfo& auth_response_info() const {
    return auth_response_info_;
  }
  scoped_refptr<HttpAuthController> auth_controller() {
    return auth_controller_.get();
  }

  // Returns true if the ConnectJob has a result.
  bool has_result() const { return has_result_; }

  // Returns true if the ConnectJob called `OnDestinationDnsAliasesResolved` on
  // the delegate.
  bool on_dns_aliases_resolved_called() const {
    return on_dns_aliases_resolved_called_;
  }

  std::set<std::string> dns_aliases() const { return dns_aliases_; }

  void set_error_for_on_destination_dns_aliases_resolved(Error error) {
    error_for_dns_aliases_resolved_ = error;
  }

  void StartJobExpectingResult(ConnectJob* connect_job,
                               net::Error expected_result,
                               bool expect_sync_result);

  StreamSocket* socket() { return socket_.get(); }

  std::unique_ptr<StreamSocket> ReleaseSocket();

 private:
  const SocketExpected socket_expected_;
  bool has_result_ = false;
  bool on_dns_aliases_resolved_called_ = false;
  int result_ = ERR_IO_PENDING;
  std::unique_ptr<StreamSocket> socket_;
  std::set<std::string> dns_aliases_;
  Error error_for_dns_aliases_resolved_ = OK;

  // These values are all updated each time a proxy auth challenge is seen.
  int num_auth_challenges_ = 0;
  HttpResponseInfo auth_response_info_;
  scoped_refptr<HttpAuthController> auth_controller_;
  base::OnceClosure restart_with_auth_callback_;

  base::RunLoop run_loop_;
  std::unique_ptr<base::RunLoop> auth_challenge_run_loop_;
};

}  // namespace net

#endif  // NET_SOCKET_CONNECT_JOB_TEST_UTIL_H_