File: connection_tracker.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (114 lines) | stat: -rw-r--r-- 4,246 bytes parent folder | download | duplicates (2)
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
// Copyright 2021 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_TEST_EMBEDDED_TEST_SERVER_CONNECTION_TRACKER_H_
#define NET_TEST_EMBEDDED_TEST_SERVER_CONNECTION_TRACKER_H_

#include <stdint.h>

#include <map>

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "net/base/net_errors.h"
#include "net/socket/stream_socket.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/embedded_test_server_connection_listener.h"

namespace net::test_server {

// Keeps track of incoming connections being accepted or read from and exposes
// that info to the tests.
// A port being reused is currently considered an error.
// If a test needs to verify multiple connections are opened in sequence, that
// will need to be changed.
class ConnectionTracker {
 public:
  explicit ConnectionTracker(EmbeddedTestServer* test_server);

  ~ConnectionTracker();

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

  // Returns the number of sockets that were accepted by the server.
  size_t GetAcceptedSocketCount() const;

  // Returns the number of sockets that were read from by the server.
  size_t GetReadSocketCount() const;

  // Waits until one connection is read.
  void WaitUntilConnectionRead();

  // Waits until exactly `num_connections` have been made since construction or
  // since ResetCounts() has been invoked. Fails if more connections are made.
  // `num_connections` must be greater than 0.
  void WaitForAcceptedConnections(size_t num_connections);

  // Helper function to stop the waiting for sockets to be accepted for
  // WaitForAcceptedConnections. |num_accepted_connections_loop_| spins
  // until |num_accepted_connections_needed_| sockets are accepted by the test
  // server. The values will be null/0 if the loop is not running.
  void CheckAccepted();

  // This clears all state and counters. If any socket connected before
  // `ResetCounts` is invoked is later read from, the test fails.
  void ResetCounts();

 private:
  // Gets notified by the EmbeddedTestServer on incoming connections being
  // accepted or read from and transfers this information to ConnectionTracker.
  class ConnectionListener : public EmbeddedTestServerConnectionListener {
   public:
    explicit ConnectionListener(ConnectionTracker* tracker);

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

    ~ConnectionListener() override;

    // Gets called from the EmbeddedTestServer thread to be notified that
    // a connection was accepted.
    std::unique_ptr<net::StreamSocket> AcceptedSocket(
        std::unique_ptr<net::StreamSocket> connection) override;

    // Gets called from the EmbeddedTestServer thread to be notified that
    // a connection was read from.
    void ReadFromSocket(const net::StreamSocket& connection, int rv) override;

   private:
    // Task runner on which the connection tracker `tracker_` will be accessed.
    scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

    // This pointer should be only accessed on the `task_runner_` thread.
    raw_ptr<ConnectionTracker> tracker_;
  };

  void AcceptedSocketWithPort(uint16_t port);
  void ReadFromSocketWithPort(uint16_t port);

  enum class SocketStatus { kAccepted, kReadFrom };

  ConnectionListener connection_listener_;

  raw_ptr<base::RunLoop> read_loop_ = nullptr;

  // Port -> SocketStatus.
  using SocketContainer = std::map<uint16_t, SocketStatus>;
  SocketContainer sockets_;

  size_t num_connected_sockets_ = 0;
  size_t num_read_sockets_ = 0;

  // If |num_accepted_connections_needed_| is non zero, then the object is
  // waiting for |num_accepted_connections_needed_| sockets to be accepted
  // before quitting the |num_accepted_connections_loop_|.
  size_t num_accepted_connections_needed_ = 0;
  raw_ptr<base::RunLoop> num_accepted_connections_loop_ = nullptr;
};

}  // namespace net::test_server

#endif  // NET_TEST_EMBEDDED_TEST_SERVER_SIMPLE_CONNECTION_TRACKER_H_