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 {
22 public:
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 =
37 std::variant<std::monostate, engine::io::Sockaddr, std::string>;
38
39 RecordType type{RecordType::kInvalid};
40 AnswerData data;
41 int ttl{0};
42 };
43
44 using DnsAnswerVector = std::vector<DnsAnswer>;
45
46 struct NoAnswer : std::exception {};
47
48 // throwing an exception will cause SERVFAIL
49 using DnsHandler = std::function<DnsAnswerVector(const DnsQuery&)>;
50
51 explicit DnsServerMock(DnsHandler);
52
53 std::string GetServerAddress() const;
54
55 private:
56 void ProcessRequests();
57
58 std::shared_ptr<internal::net::UdpListener> listener_;
59 DnsHandler handler_;
60 engine::Task receiver_task_;
61};
62
63} // namespace utest
64
65USERVER_NAMESPACE_END