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/gopher_mock_server.cc
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 Bounded transport probes for curl's synchronous Gopher selector.
9
10
#include "proto_fuzzer/gopher_mock_server.h"
11
12
#include <cstdlib>
13
#include <string>
14
15
namespace proto_fuzzer {
16
17
namespace {
18
19
/// Replace one environment value only while curl constructs and drives the
20
/// probe's debug socket filter. libFuzzer executes callbacks serially.
21
class ScopedEnvironmentOverride {
22
 public:
23
179
  ScopedEnvironmentOverride(const char* name, const char* value) : name_(name) {
24
179
    const char* previous = std::getenv(name);
25
179
    if (previous != nullptr) {
26
0
      had_previous_ = true;
27
0
      previous_ = previous;
28
0
    }
29
179
    active_ = setenv(name, value, 1) == 0;
30
179
  }
31
32
179
  ~ScopedEnvironmentOverride() {
33
179
    if (!active_) {
34
0
      return;
35
0
    }
36
179
    if (had_previous_) {
37
0
      (void)setenv(name_.c_str(), previous_.c_str(), 1);
38
179
    } else {
39
179
      (void)unsetenv(name_.c_str());
40
179
    }
41
179
  }
42
43
  ScopedEnvironmentOverride(const ScopedEnvironmentOverride&) = delete;
44
  ScopedEnvironmentOverride& operator=(const ScopedEnvironmentOverride&) = delete;
45
46
 private:
47
  std::string name_;
48
  std::string previous_;
49
  bool had_previous_ = false;
50
  bool active_ = false;
51
};
52
53
constexpr int kTimeoutProbeBufferBytes = 2048;
54
constexpr long kTimeoutProbeMilliseconds = 5;
55
56
}  // namespace
57
58
/// Construct a closed Gopher probe peer.
59
/// @param mode Fixed probe selected by normalized target policy.
60
5.47k
GopherMockServer::GopherMockServer(curl::fuzzer::proto::GopherProbeMode mode) : mode_(mode) {}
61
62
5.47k
GopherMockServer::~GopherMockServer() = default;
63
64
/// Build an ordinary socketpair, then saturate the client-to-server direction
65
/// for the timeout probe before curl can write its selector.
66
/// @return A ready socketpair, or nullptr if saturation could not be
67
/// established.
68
4.85k
std::unique_ptr<MockConnection> GopherMockServer::CreateConnection() {
69
4.85k
  std::unique_ptr<MockConnection> connection = MockServer::CreateConnection();
70
4.85k
  if (mode_ != curl::fuzzer::proto::GOPHER_PROBE_SEND_TIMEOUT) {
71
4.77k
    return connection;
72
4.77k
  }
73
87
  if (connection == nullptr || !connection->ok()) {
74
0
    return nullptr;
75
0
  }
76
87
  connection->ApplyBackpressure(kTimeoutProbeBufferBytes, 0);
77
87
  if (!connection->FillClientSendBuffer()) {
78
0
    return nullptr;
79
0
  }
80
87
  return connection;
81
87
}
82
83
/// Apply the two fixed timing policies immediately before curl's first
84
/// perform. Scenario options cannot replace these harness-owned values.
85
/// @param multi Multi handle driving the transfer.
86
/// @param easy Easy handle receiving fixed timeout policy.
87
/// @param scenario Normalized response script.
88
5.33k
void GopherMockServer::RunLoop(CURLM* multi, CURL* easy, const curl::fuzzer::proto::Scenario& scenario) {
89
5.33k
  if (mode_ == curl::fuzzer::proto::GOPHER_PROBE_PARTIAL_WRITE_NO_TIMEOUT) {
90
179
    (void)curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, 0L);
91
179
    ScopedEnvironmentOverride partial_writes("CURL_DBG_SOCK_WPARTIAL", "50");
92
179
    MockServer::RunLoop(multi, easy, scenario);
93
179
    return;
94
179
  }
95
5.15k
  if (mode_ == curl::fuzzer::proto::GOPHER_PROBE_SEND_TIMEOUT) {
96
112
    (void)curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, kTimeoutProbeMilliseconds);
97
112
  }
98
5.15k
  MockServer::RunLoop(multi, easy, scenario);
99
5.15k
}
100
101
}  // namespace proto_fuzzer