File: connect_job_test_util.cc

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 (117 lines) | stat: -rw-r--r-- 3,748 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
113
114
115
116
117
// 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.

#include "net/socket/connect_job_test_util.h"

#include <set>
#include <utility>

#include "base/check.h"
#include "base/run_loop.h"
#include "net/base/net_errors.h"
#include "net/socket/stream_socket.h"
#include "net/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

TestConnectJobDelegate::TestConnectJobDelegate(SocketExpected socket_expected)
    : socket_expected_(socket_expected) {}

TestConnectJobDelegate::~TestConnectJobDelegate() = default;

void TestConnectJobDelegate::OnConnectJobComplete(int result, ConnectJob* job) {
  EXPECT_FALSE(has_result_);
  result_ = result;
  socket_ = job->PassSocket();
  EXPECT_EQ(socket_.get() != nullptr,
            result == OK || socket_expected_ == SocketExpected::ALWAYS);
  // On success, generally end up with a connected socket. Could theoretically
  // be racily disconnected before it was returned, but that case isn't tested
  // with this class.
  if (result == OK)
    EXPECT_TRUE(socket_->IsConnected());
  has_result_ = true;
  run_loop_.Quit();
}

void TestConnectJobDelegate::OnNeedsProxyAuth(
    const HttpResponseInfo& response,
    HttpAuthController* auth_controller,
    base::OnceClosure restart_with_auth_callback,
    ConnectJob* job) {
  EXPECT_TRUE(auth_controller);
  EXPECT_TRUE(restart_with_auth_callback);

  EXPECT_FALSE(has_result_);
  EXPECT_FALSE(auth_controller_);
  EXPECT_FALSE(restart_with_auth_callback_);

  num_auth_challenges_++;
  auth_response_info_ = response;
  auth_controller_ = auth_controller;
  restart_with_auth_callback_ = std::move(restart_with_auth_callback);
  if (auth_challenge_run_loop_)
    auth_challenge_run_loop_->Quit();
}

Error TestConnectJobDelegate::OnDestinationDnsAliasesResolved(
    const std::set<std::string>& aliases,
    ConnectJob* job) {
  EXPECT_FALSE(aliases.empty());
  EXPECT_FALSE(on_dns_aliases_resolved_called_);
  EXPECT_TRUE(dns_aliases_.empty());

  on_dns_aliases_resolved_called_ = true;
  dns_aliases_ = aliases;
  return error_for_dns_aliases_resolved_;
}

void TestConnectJobDelegate::WaitForAuthChallenge(
    int num_auth_challenges_to_wait_for) {
  // It a bit strange to call this after a job has already complete, and doing
  // so probably indicates a bug.
  EXPECT_FALSE(has_result_);

  while (num_auth_challenges_ < num_auth_challenges_to_wait_for) {
    auth_challenge_run_loop_ = std::make_unique<base::RunLoop>();
    auth_challenge_run_loop_->Run();
    auth_challenge_run_loop_.reset();
  }
  EXPECT_EQ(num_auth_challenges_to_wait_for, num_auth_challenges_);
}

void TestConnectJobDelegate::RunAuthCallback() {
  ASSERT_TRUE(restart_with_auth_callback_);
  auth_controller_ = nullptr;
  std::move(restart_with_auth_callback_).Run();
}

int TestConnectJobDelegate::WaitForResult() {
  run_loop_.Run();
  DCHECK(has_result_);
  return result_;
}

void TestConnectJobDelegate::StartJobExpectingResult(ConnectJob* connect_job,
                                                     net::Error expected_result,
                                                     bool expect_sync_result) {
  int rv = connect_job->Connect();
  if (rv == ERR_IO_PENDING) {
    EXPECT_FALSE(expect_sync_result);
    EXPECT_THAT(WaitForResult(), test::IsError(expected_result));
  } else {
    EXPECT_TRUE(expect_sync_result);
    // The callback should not have been invoked.
    ASSERT_FALSE(has_result_);
    OnConnectJobComplete(rv, connect_job);
    EXPECT_THAT(result_, test::IsError(expected_result));
  }
}

std::unique_ptr<StreamSocket> TestConnectJobDelegate::ReleaseSocket() {
  return std::move(socket_);
}

}  // namespace net