/src/curl_fuzzer/proto_fuzzer/pop3_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 Implementation of the command-aware in-process POP3 peer. |
9 | | |
10 | | #include "proto_fuzzer/pop3_mock_server.h" |
11 | | |
12 | | #include <string> |
13 | | #include <string_view> |
14 | | |
15 | | #include "proto_fuzzer/allocation_fault_injection.h" |
16 | | |
17 | | namespace proto_fuzzer { |
18 | | |
19 | | /// Begin without a borrowed protobuf or open socketpair. RunLoop borrows a |
20 | | /// Scenario only for the synchronous DriveScenario call in which socket |
21 | | /// callbacks can run. |
22 | 4.14k | Pop3MockServer::Pop3MockServer() = default; |
23 | | |
24 | | /// Release the server-owned socketpair half after curl has returned its client |
25 | | /// descriptor. The out-of-line destructor keeps MockConnection's complete type |
26 | | /// local to this translation unit. |
27 | 4.14k | Pop3MockServer::~Pop3MockServer() = default; |
28 | | |
29 | | /// Advance one reply for every complete command. POP3's status lines are |
30 | | /// self-describing, so the scripted bytes are written unchanged; curl chooses |
31 | | /// multi-line handling from the command it sent, not from the reply. |
32 | | /// @param line One newline-terminated command from curl. |
33 | 6.12k | void Pop3MockServer::HandleLine(std::string_view line) { |
34 | 6.12k | if (scenario() == nullptr) { |
35 | 95 | return; |
36 | 95 | } |
37 | 6.03k | const std::string* response = NextControlReply(); |
38 | 6.03k | if (response == nullptr || !WriteControlBytes(*response)) { |
39 | 3.04k | return; |
40 | 3.04k | } |
41 | 2.98k | if (!scenario()->pop3_starttls_filter_allocation_failure()) { |
42 | 2.23k | return; |
43 | 2.23k | } |
44 | 755 | const bool starttls_accepted = TokenEquals(FirstToken(line), "STLS") && TokenEquals(FirstToken(*response), "+OK"); |
45 | 755 | if (starttls_accepted) { |
46 | 4 | allocation_fault_injection::ArmNextAllocationFailure(); |
47 | 4 | } |
48 | 755 | } |
49 | | |
50 | | /// Queue a +OK for the blocking QUIT that follows easy cleanup, then let the |
51 | | /// shared transport half-close so an unfinished transfer cannot wait on a |
52 | | /// reply that will never arrive. |
53 | 3.63k | void Pop3MockServer::QueueShutdownReply() { (void)WriteControlBytes(kShutdownReply); } |
54 | | |
55 | | /// Retain protocol outcomes in coverage and introspector builds. |
56 | 4.11k | void Pop3MockServer::ObserveState() { |
57 | 4.11k | #if defined(PROTO_FUZZER_OBSERVE_STATE) |
58 | 4.11k | PingpongMockServer::ObserveState(); |
59 | 4.11k | const volatile std::size_t pop3_observation[] = {control_transcript().size(), uploaded_data().size()}; |
60 | 4.11k | (void)pop3_observation; |
61 | 4.11k | #endif |
62 | 4.11k | } |
63 | | |
64 | | } // namespace proto_fuzzer |