Coverage Report

Created: 2026-10-31 06:46

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
82
  ScopedEnvironmentOverride(const char* name, const char* value) : name_(name) {
24
82
    const char* previous = std::getenv(name);
25
82
    if (previous != nullptr) {
26
0
      had_previous_ = true;
27
0
      previous_ = previous;
28
0
    }
29
82
    active_ = setenv(name, value, 1) == 0;
30
82
  }
31
32
82
  ~ScopedEnvironmentOverride() {
33
82
    if (!active_) {
34
0
      return;
35
0
    }
36
82
    if (had_previous_) {
37
0
      (void)setenv(name_.c_str(), previous_.c_str(), 1);
38
82
    } else {
39
82
      (void)unsetenv(name_.c_str());
40
82
    }
41
82
  }
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
4.58k
GopherMockServer::GopherMockServer(curl::fuzzer::proto::GopherProbeMode mode) : mode_(mode) {}
61
62
4.58k
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.12k
std::unique_ptr<MockConnection> GopherMockServer::CreateConnection() {
69
4.12k
  std::unique_ptr<MockConnection> connection = MockServer::CreateConnection();
70
4.12k
  if (mode_ != curl::fuzzer::proto::GOPHER_PROBE_SEND_TIMEOUT) {
71
4.09k
    return connection;
72
4.09k
  }
73
34
  if (connection == nullptr || !connection->ok()) {
74
0
    return nullptr;
75
0
  }
76
34
  connection->ApplyBackpressure(kTimeoutProbeBufferBytes, 0);
77
34
  if (!connection->FillClientSendBuffer()) {
78
0
    return nullptr;
79
0
  }
80
34
  return connection;
81
34
}
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
4.46k
void GopherMockServer::RunLoop(CURLM* multi, CURL* easy, const curl::fuzzer::proto::Scenario& scenario) {
89
4.46k
  if (mode_ == curl::fuzzer::proto::GOPHER_PROBE_PARTIAL_WRITE_NO_TIMEOUT) {
90
82
    (void)curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, 0L);
91
82
    ScopedEnvironmentOverride partial_writes("CURL_DBG_SOCK_WPARTIAL", "50");
92
82
    MockServer::RunLoop(multi, easy, scenario);
93
82
    return;
94
82
  }
95
4.38k
  if (mode_ == curl::fuzzer::proto::GOPHER_PROBE_SEND_TIMEOUT) {
96
50
    (void)curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, kTimeoutProbeMilliseconds);
97
50
  }
98
4.38k
  MockServer::RunLoop(multi, easy, scenario);
99
4.38k
}
100
101
}  // namespace proto_fuzzer