curl-fuzzer C++ reference
Loading...
Searching...
No Matches
websocket_mock_server.h
Go to the documentation of this file.
1/*
2 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3 *
4 * SPDX-License-Identifier: curl
5 */
6
10
11#ifndef PROTO_FUZZER_WEBSOCKET_MOCK_SERVER_H_
12#define PROTO_FUZZER_WEBSOCKET_MOCK_SERVER_H_
13
14#include <curl/curl.h>
15
16#include <cstddef>
17#include <memory>
18#include <string>
19#include <vector>
20
21#include "curl_fuzzer.pb.h"
24
25namespace proto_fuzzer {
26
27bool ScenarioRequestsManualWsDrive(const curl::fuzzer::proto::Scenario& scenario);
28
35class WebSocketMockServer : public MockServerBase, public IncomingDataObserver {
36 public:
39
40 void ObserveState() override;
41
42 void Install(CURL* easy) override;
43
44 void SetFrames(std::vector<std::string> frames);
45
46 void SetManualDelivery(bool manual);
47 bool manual_delivery() const;
48
49 bool TryAdvanceHandshake(bool* made_progress = nullptr);
50 bool handshake_sent() const;
51
52 bool DeliverNextChunk();
53 bool has_more_chunks() const;
54
55 std::size_t remaining_chunks() const;
56 const std::string& PeekChunk(std::size_t index) const;
57 void ConsumeChunk();
58 bool PushRawBytes(const unsigned char* data, std::size_t size);
59
60 protected:
61 virtual std::unique_ptr<MockConnection> CreateConnection();
62
63 virtual std::size_t DrainHandshakeData();
64
65 virtual void FlushTransport();
66
67 void ObserveIncomingData(const unsigned char* data, std::size_t size) override;
68
69 curl_socket_t HandleOpenSocket(curlsocktype purpose = CURLSOCKTYPE_IPCXN,
70 struct curl_sockaddr* address = nullptr) override;
71 void RunLoop(CURLM* multi, CURL* easy, const curl::fuzzer::proto::Scenario& scenario) override;
72
73 public:
74 bool ws_probe_fired() const;
75 void MarkWsProbeFired();
76 CURL* easy_handle() const;
77 std::size_t manual_received_bytes() const;
78
79 private:
80 std::vector<std::string> frames_;
81 std::size_t next_chunk_;
82 bool manual_delivery_;
83 bool handshake_sent_;
84 bool ws_probe_fired_;
85 CURL* easy_handle_;
86 std::size_t manual_received_bytes_;
87 std::string ws_request_buffer_;
88};
89
90} // namespace proto_fuzzer
91
92#endif // PROTO_FUZZER_WEBSOCKET_MOCK_SERVER_H_
Receives bytes after any transport decoding.
bool TryAdvanceHandshake(bool *made_progress=nullptr)
Drive the WebSocket opening handshake: read whatever curl has written so far, and once we've seen the...
bool PushRawBytes(const unsigned char *data, std::size_t size)
Push raw bytes onto the server fd.
void ObserveIncomingData(const unsigned char *data, std::size_t size) override
Collect application bytes consumed by the active transport.
bool DeliverNextChunk()
Push the next queued frame when curl is ready.
WebSocketMockServer()
Construct an idle WebSocketMockServer with no queued frames.
void SetFrames(std::vector< std::string > frames)
Queue RFC 6455 wire-byte chunks to emit once the handshake has completed.
virtual void FlushTransport()
Flush application writes when the carrier queues them.
const std::string & PeekChunk(std::size_t index) const
Access a pending chunk without consuming it.
void ConsumeChunk()
Advance the pending-chunk cursor by one. No-op when no chunks remain.
void RunLoop(CURLM *multi, CURL *easy, const curl::fuzzer::proto::Scenario &scenario) override
Seed the mock from the scenario, then run the perform loop.
void SetManualDelivery(bool manual)
Toggle streaming (false, default) vs manual (true) chunk delivery.
void Install(CURL *easy) override
Install the shared socket trampolines via the base, then layer a WebSocket-aware WRITEFUNCTION / HEAD...
~WebSocketMockServer() override
Default destructor; the owned MockConnection (if any) cleans up its socketpair.
bool ws_probe_fired() const
Return the one-shot probe gate.
curl_socket_t HandleOpenSocket(curlsocktype purpose=CURLSOCKTYPE_IPCXN, struct curl_sockaddr *address=nullptr) override
Called by the OPENSOCKETFUNCTION trampoline in the base class.
virtual std::size_t DrainHandshakeData()
Consume the plaintext Upgrade request without applying DrainIncoming's backpressure limit.
void ObserveState() override
Retain protocol outcomes in coverage and introspector builds.
virtual std::unique_ptr< MockConnection > CreateConnection()
Construct the transport used for the WebSocket exchange.
void MarkWsProbeFired()
Flip the one-shot gate closed.
MockConnection and MockServer — the in-process peer that feeds canned response bytes to libcurl over ...
MockServerBase — common plumbing shared by every protocol-specific mock server.
bool ScenarioRequestsManualWsDrive(const curl::fuzzer::proto::Scenario &scenario)
Mirror the sequential CURLOPT_CONNECT_ONLY setopts applied by ApplyScenarioOptions.