aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/dnsresolver/dnsresolver_ut.cpp
blob: 089bd5179f23d36c9bdf1e148a7a773e64196cf4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "dnsresolver.h"

#include <library/cpp/actors/testlib/test_runtime.h>
#include <library/cpp/testing/unittest/registar.h>
#include <util/string/builder.h>

#include <ares.h>

using namespace NActors;
using namespace NActors::NDnsResolver;

Y_UNIT_TEST_SUITE(DnsResolver) {

    struct TSilentUdpServer {
        TInetDgramSocket Socket;
        ui16 Port;

        TSilentUdpServer() {
            TSockAddrInet addr("127.0.0.1", 0);
            int err = Socket.Bind(&addr);
            Y_VERIFY(err == 0, "Cannot bind a udp socket");
            Port = addr.GetPort();
        }
    };

    Y_UNIT_TEST(ResolveLocalHost) {
        TTestActorRuntimeBase runtime;
        runtime.Initialize();
        auto sender = runtime.AllocateEdgeActor();
        auto resolver = runtime.Register(CreateSimpleDnsResolver());
        runtime.Send(new IEventHandleFat(resolver, sender, new TEvDns::TEvGetHostByName("localhost", AF_UNSPEC)),
                0, true);
        auto ev = runtime.GrabEdgeEventRethrow<TEvDns::TEvGetHostByNameResult>(sender);
        UNIT_ASSERT_VALUES_EQUAL_C(ev->Get()->Status, 0, ev->Get()->ErrorText);
        size_t addrs = ev->Get()->AddrsV4.size() + ev->Get()->AddrsV6.size();
        UNIT_ASSERT_C(addrs > 0, "Got " << addrs << " addresses");
    }

    Y_UNIT_TEST(ResolveYandexRu) {
        TTestActorRuntimeBase runtime;
        runtime.Initialize();
        auto sender = runtime.AllocateEdgeActor();
        auto resolver = runtime.Register(CreateSimpleDnsResolver());
        runtime.Send(new IEventHandleFat(resolver, sender, new TEvDns::TEvGetHostByName("yandex.ru", AF_UNSPEC)),
                0, true);
        auto ev = runtime.GrabEdgeEventRethrow<TEvDns::TEvGetHostByNameResult>(sender);
        UNIT_ASSERT_VALUES_EQUAL_C(ev->Get()->Status, 0, ev->Get()->ErrorText);
        size_t addrs = ev->Get()->AddrsV4.size() + ev->Get()->AddrsV6.size();
        UNIT_ASSERT_C(addrs > 0, "Got " << addrs << " addresses");
    }

    Y_UNIT_TEST(GetAddrYandexRu) {
        TTestActorRuntimeBase runtime;
        runtime.Initialize();
        auto sender = runtime.AllocateEdgeActor();
        auto resolver = runtime.Register(CreateSimpleDnsResolver());

        runtime.Send(new IEventHandleFat(resolver, sender, new TEvDns::TEvGetAddr("yandex.ru", AF_UNSPEC)),
                0, true);
        auto ev = runtime.GrabEdgeEventRethrow<TEvDns::TEvGetAddrResult>(sender);
        UNIT_ASSERT_VALUES_EQUAL_C(ev->Get()->Status, 0, ev->Get()->ErrorText);
        UNIT_ASSERT_C(ev->Get()->IsV4() || ev->Get()->IsV6(), "Expect v4 or v6 address");
    }

    Y_UNIT_TEST(ResolveTimeout) {
        TSilentUdpServer server;
        TTestActorRuntimeBase runtime;
        runtime.Initialize();
        auto sender = runtime.AllocateEdgeActor();
        TSimpleDnsResolverOptions options;
        options.Timeout = TDuration::MilliSeconds(250);
        options.Attempts = 2;
        options.Servers.emplace_back(TStringBuilder() << "127.0.0.1:" << server.Port);
        auto resolver = runtime.Register(CreateSimpleDnsResolver(options));
        runtime.Send(new IEventHandleFat(resolver, sender, new TEvDns::TEvGetHostByName("timeout.yandex.ru", AF_INET)),
                0, true);
        auto ev = runtime.GrabEdgeEventRethrow<TEvDns::TEvGetHostByNameResult>(sender);
        UNIT_ASSERT_VALUES_EQUAL_C(ev->Get()->Status, ARES_ETIMEOUT, ev->Get()->ErrorText);
    }

    Y_UNIT_TEST(ResolveGracefulStop) {
        TSilentUdpServer server;
        TTestActorRuntimeBase runtime;
        runtime.Initialize();
        auto sender = runtime.AllocateEdgeActor();
        TSimpleDnsResolverOptions options;
        options.Timeout = TDuration::Seconds(5);
        options.Attempts = 5;
        options.Servers.emplace_back(TStringBuilder() << "127.0.0.1:" << server.Port);
        auto resolver = runtime.Register(CreateSimpleDnsResolver(options));
        runtime.Send(new IEventHandleFat(resolver, sender, new TEvDns::TEvGetHostByName("timeout.yandex.ru", AF_INET)),
                0, true);
        runtime.Send(new IEventHandleFat(resolver, sender, new TEvents::TEvPoison), 0, true);
        auto ev = runtime.GrabEdgeEventRethrow<TEvDns::TEvGetHostByNameResult>(sender);
        UNIT_ASSERT_VALUES_EQUAL_C(ev->Get()->Status, ARES_ECANCELLED, ev->Get()->ErrorText);
    }

}