libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
test_server.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// A complete in-process SSH server for tests. See docs/design/08 §8.8.
4//
5// Scope note relative to the design doc: no Options::faults fault-injection
6// (truncated banners, mid-transfer disconnects, slow handshakes) and no
7// Stats/allow_interactive/allow_none/allow_port_forwarding - this covers the
8// common case (password/public-key auth, exec, optional SFTP root) that
9// makes the rest of the test suite hermetic; the rest can be added later
10// without changing this shape.
11#pragma once
12
13#include <sshpp/config.hpp>
14
15#include <sshpp/key.hpp>
16#include <sshpp/result.hpp>
17#include <sshpp/server/bind.hpp>
19#include <sshpp/types.hpp>
20
21#include <atomic>
22#include <cstdint>
23#include <filesystem>
24#include <functional>
25#include <istream>
26#include <optional>
27#include <ostream>
28#include <string>
29#include <string_view>
30#include <thread>
31#include <vector>
32
33namespace sshpp::server {
34
38class SSHPP_API TestServer {
39public:
40 struct Options {
41 std::string user = "testuser";
43 std::vector<Key> authorized_keys;
44 bool allow_password = true;
45 bool allow_public_key = true;
46 std::optional<std::filesystem::path> sftp_root; // enables the SFTP subsystem
47 std::function<int(std::string_view, std::istream&, std::ostream&, std::ostream&)> exec;
48 };
49
50 // No `= Options{}` default here: GCC rejects a default argument whose
51 // type is a nested class of the same enclosing class when that nested
52 // class has a default member initializer. Callers wanting defaults pass
53 // `Options{}` explicitly.
54 explicit TestServer(Options options);
56 TestServer(const TestServer&) = delete;
57 TestServer& operator=(const TestServer&) = delete;
58
59 std::uint16_t port() const noexcept { return port_; }
60 const Key& host_key() const noexcept { return host_key_; }
61 Fingerprint host_key_fingerprint(HashType type = HashType::sha256) const;
64 SessionOptions client_options() const;
65 std::filesystem::path known_hosts_file() const { return known_hosts_path_; }
66
67private:
68 void accept_loop();
69 void serve_connection(Session session);
70
71 Options options_;
72 Key host_key_;
73 Bind bind_;
74 std::uint16_t port_ = 0;
75 std::filesystem::path known_hosts_path_;
76 std::atomic<bool> stop_requested_{false};
77 std::thread accept_thread_;
78 std::vector<std::thread> connection_threads_;
79};
80
81} // namespace sshpp::server
82
83#if SSHPP_HEADER_ONLY
85#endif
Definition key.hpp:38
Definition key.hpp:59
Definition types.hpp:132
The listening server socket. See docs/design/08 §8.3.
Definition bind.hpp:37
One accepted server-side connection. See docs/design/08 §8.4.
Definition server_session.hpp:32
Definition test_server.hpp:38
TestServer(const TestServer &)=delete
std::filesystem::path known_hosts_file() const
Definition test_server.hpp:65
TestServer & operator=(const TestServer &)=delete
std::uint16_t port() const noexcept
Definition test_server.hpp:59
const Key & host_key() const noexcept
Definition test_server.hpp:60
Definition server_bind.ipp:15
HashType
Definition key.hpp:27
Definition session_options.hpp:25
Definition test_server.hpp:40
std::optional< std::filesystem::path > sftp_root
Definition test_server.hpp:46
std::vector< Key > authorized_keys
Definition test_server.hpp:43
std::function< int(std::string_view, std::istream &, std::ostream &, std::ostream &)> exec
Definition test_server.hpp:47