userver: userver/utest/dns_server_mock.hpp Source File
Loading...
Searching...
No Matches
dns_server_mock.hpp
1#pragma once
2
3#include <exception>
4#include <functional>
5#include <memory>
6#include <string>
7#include <variant>
8#include <vector>
9
10#include <userver/engine/io/sockaddr.hpp>
11#include <userver/engine/task/task.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace internal::net {
16struct UdpListener;
17} // namespace internal::net
18
19namespace utest {
20
21class DnsServerMock final {
22public:
23 enum class RecordType {
24 kInvalid = 0,
25 kA = 1,
26 kAAAA = 28,
27 kCname = 5,
28 };
29
30 struct DnsQuery {
31 RecordType type{RecordType::kInvalid};
32 std::string name;
33 };
34
35 struct DnsAnswer {
36 using AnswerData = std::variant<std::monostate, engine::io::Sockaddr, std::string>;
37
38 RecordType type{RecordType::kInvalid};
39 AnswerData data;
40 int ttl{0};
41 };
42
43 using DnsAnswerVector = std::vector<DnsAnswer>;
44
45 struct NoAnswer : std::exception {};
46
47 // throwing an exception will cause SERVFAIL
48 using DnsHandler = std::function<DnsAnswerVector(const DnsQuery&)>;
49
50 explicit DnsServerMock(DnsHandler);
51
52 std::string GetServerAddress() const;
53
54private:
55 void ProcessRequests();
56
57 std::shared_ptr<internal::net::UdpListener> listener_;
58 DnsHandler handler_;
59 engine::Task receiver_task_;
60};
61
62} // namespace utest
63
64USERVER_NAMESPACE_END