Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/proto_fuzzer/mock_server.h
Line
Count
Source
1
/*
2
 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3
 *
4
 * SPDX-License-Identifier: curl
5
 */
6
7
/// @file
8
/// @brief MockConnection and MockServer — the in-process peer that feeds
9
///        canned response bytes to libcurl over a socketpair.
10
11
#ifndef PROTO_FUZZER_MOCK_SERVER_H_
12
#define PROTO_FUZZER_MOCK_SERVER_H_
13
14
#include <curl/curl.h>
15
16
#include <array>
17
#include <cstddef>
18
#include <memory>
19
#include <string>
20
#include <vector>
21
22
#include "curl_fuzzer.pb.h"
23
#include "proto_fuzzer/incoming_data_observer.h"
24
#include "proto_fuzzer/mock_server_base.h"
25
#include "proto_fuzzer/scenario_limits.h"
26
27
namespace proto_fuzzer {
28
29
class MockConnection {
30
 public:
31
  MockConnection();
32
  virtual ~MockConnection();
33
34
  MockConnection(const MockConnection&) = delete;
35
  MockConnection& operator=(const MockConnection&) = delete;
36
37
  virtual bool ok() const;
38
  curl_socket_t take_client_fd();
39
  int server_fd() const;
40
41
  std::size_t client_send_buffer_size() const;
42
43
  bool EnsureClientSendBufferSize(std::size_t minimum);
44
45
  bool FillClientSendBuffer();
46
47
  virtual bool WriteAll(const unsigned char* data, std::size_t size);
48
  virtual std::size_t DrainIncoming();
49
  void SetIncomingDataObserver(IncomingDataObserver* observer);
50
  void ReadAvailable(std::string* out);
51
  virtual void ShutdownWrite();
52
53
  void ApplyBackpressure(int recv_buf_bytes, std::size_t drain_limit);
54
55
 protected:
56
  explicit MockConnection(int adopted_server_fd);
57
58
  void NotifyIncomingData(const unsigned char* data, std::size_t size);
59
60
 private:
61
  int server_fd_;
62
  int client_fd_;
63
  std::size_t drain_limit_;
64
  IncomingDataObserver* incoming_data_observer_;
65
};
66
67
/// @class proto_fuzzer::MockServer
68
/// @brief HTTP in-process mock peer. Assigns one bounded response script to
69
///        each socket curl opens, allowing redirects and authentication
70
///        retries to progress without permitting an unbounded connection
71
///        graph. WebSocketMockServer keeps its separate single-socket model.
72
class MockServer : public MockServerBase {
73
 public:
74
  explicit MockServer(MultiDrivePolicy drive_policy = MultiDrivePolicy::kPerform);
75
  ~MockServer() override;
76
77
  void SetScripts(const curl::fuzzer::proto::Scenario& scenario);
78
79
  void SetKeepConnectionsOpen(bool keep_open);
80
81
  CURLcode DriveEasyScenario(CURL* easy, const curl::fuzzer::proto::Scenario& scenario,
82
                             bool use_events = false) override;
83
84
  ConnectOnlyRunStats DriveConnectOnlyScenario(CURL* easy, const curl::fuzzer::proto::Scenario& scenario) override;
85
86
  bool DeliverNextChunk();
87
  bool has_more_chunks() const;
88
89
  bool ServiceConnections();
90
91
  std::size_t opened_connection_count() const;
92
93
 protected:
94
  virtual std::unique_ptr<MockConnection> CreateConnection();
95
96
  void ResetConnections();
97
98
  virtual void ObserveActiveTransfer(CURL* easy);
99
100
  // Whether this peer can service repeated zero-timeout public polls.
101
  virtual bool SupportsRepeatedPollsetProbe() const;
102
103
  curl_socket_t HandleOpenSocket(curlsocktype purpose = CURLSOCKTYPE_IPCXN,
104
                                 struct curl_sockaddr* address = nullptr) override;
105
  void RunLoop(CURLM* multi, CURL* easy, const curl::fuzzer::proto::Scenario& scenario) override;
106
107
 private:
108
  /// One socket's borrowed response configuration plus its delivery cursor.
109
  /// The fixed script array is fully populated before curl runs, so both this
110
  /// object and its pointer into the caller-owned Scenario remain stable across
111
  /// callbacks. Counts record the exact runtime-visible prefix: raw chunks come
112
  /// first and structured frames consume only the remaining shared budget.
113
  struct ConnectionScript {
114
    const curl::fuzzer::proto::Connection* connection = nullptr;
115
    std::size_t raw_chunk_count = 0;
116
    std::size_t frame_chunk_count = 0;
117
    std::size_t next_chunk = 0;
118
119
    /// @return Number of raw and structured chunks visible to the runtime.
120
3.92M
    std::size_t chunk_count() const { return raw_chunk_count + frame_chunk_count; }
121
  };
122
123
  std::size_t DrainIncomingConnections();
124
125
  void RunSocketActionLoop(CURLM* multi, CURL* easy);
126
127
  std::array<ConnectionScript, scenario_limits::kMaxConnections> scripts_;
128
  std::size_t script_count_;
129
  std::size_t next_script_;
130
  ConnectionScript* active_script_;
131
132
  /// True only while HandleOpenSocket is preparing a synchronous easy drive.
133
  /// Every response chunk must be queued before the callback returns because
134
  /// curl_easy_perform does not yield control to the mock.
135
  bool preload_all_chunks_;
136
137
  /// Suppress response-side half-close after the final scripted chunk. This
138
  /// is false for every historical target and enabled only by MultiPlan.
139
  bool keep_connections_open_;
140
141
  /// Old server halves must outlive their active role: libcurl owns the client
142
  /// fds and may close or briefly revisit them after opening the next socket.
143
  /// Destroying a MockConnection at that boundary would turn valid lifecycle
144
  /// traffic into harness-generated ECONNRESET/SIGPIPE behavior.
145
  std::vector<std::unique_ptr<MockConnection>> previous_connections_;
146
};
147
148
}  // namespace proto_fuzzer
149
150
#endif  // PROTO_FUZZER_MOCK_SERVER_H_