aboutsummaryrefslogtreecommitdiffstats
path: root/util/network/endpoint_ut.cpp
blob: 216f8cb071510d9fd9f3668483403bf1375a8935 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "endpoint.h"

#include <library/cpp/testing/unittest/registar.h> 

#include <util/generic/hash_set.h>
#include <util/generic/strbuf.h>

Y_UNIT_TEST_SUITE(TEndpointTest) {
    Y_UNIT_TEST(TestSimple) {
        TVector<TNetworkAddress> addrs;

        TEndpoint ep0;

        UNIT_ASSERT(ep0.IsIpV4());
        UNIT_ASSERT_VALUES_EQUAL(0, ep0.Port());
        UNIT_ASSERT_VALUES_EQUAL("0.0.0.0", ep0.IpToString());

        TEndpoint ep1;

        try {
            TNetworkAddress na1("25.26.27.28", 24242);

            addrs.push_back(na1);

            ep1 = TEndpoint(new NAddr::TAddrInfo(&*na1.Begin()));

            UNIT_ASSERT(ep1.IsIpV4());
            UNIT_ASSERT_VALUES_EQUAL("25.26.27.28", ep1.IpToString());
            UNIT_ASSERT_VALUES_EQUAL(24242, ep1.Port());
        } catch (const TNetworkResolutionError&) {
            TNetworkAddress n("2a02:6b8:0:1420:0::5f6c:f3c2", 11111);

            addrs.push_back(n);

            ep1 = TEndpoint(new NAddr::TAddrInfo(&*n.Begin()));
        }

        ep0.SetPort(12345);

        TEndpoint ep2(ep0);

        ep0.SetPort(0);

        UNIT_ASSERT_VALUES_EQUAL(12345, ep2.Port());

        TEndpoint ep2_;

        ep2_.SetPort(12345);

        UNIT_ASSERT(ep2 == ep2_);

        TNetworkAddress na3("2a02:6b8:0:1410::5f6c:f3c2", 54321);
        TEndpoint ep3(new NAddr::TAddrInfo(&*na3.Begin()));

        UNIT_ASSERT(ep3.IsIpV6());
        UNIT_ASSERT(ep3.IpToString().StartsWith(TStringBuf("2a02:6b8:0:1410:")));
        UNIT_ASSERT(ep3.IpToString().EndsWith(TStringBuf(":5f6c:f3c2")));
        UNIT_ASSERT_VALUES_EQUAL(54321, ep3.Port());

        TNetworkAddress na4("2a02:6b8:0:1410:0::5f6c:f3c2", 1);
        TEndpoint ep4(new NAddr::TAddrInfo(&*na4.Begin()));

        TEndpoint ep3_ = ep4;

        ep3_.SetPort(54321);

        THashSet<TEndpoint> he;

        he.insert(ep0);
        he.insert(ep1);
        he.insert(ep2);

        UNIT_ASSERT_VALUES_EQUAL(3u, he.size());

        he.insert(ep2_);

        UNIT_ASSERT_VALUES_EQUAL(3u, he.size());

        he.insert(ep3);
        he.insert(ep3_);

        UNIT_ASSERT_VALUES_EQUAL(4u, he.size());

        he.insert(ep4);

        UNIT_ASSERT_VALUES_EQUAL(5u, he.size());
    }

    Y_UNIT_TEST(TestEqual) {
        const TString ip1 = "2a02:6b8:0:1410::5f6c:f3c2";
        const TString ip2 = "2a02:6b8:0:1410::5f6c:f3c3";

        TNetworkAddress na1(ip1, 24242);
        TEndpoint ep1(new NAddr::TAddrInfo(&*na1.Begin()));

        TNetworkAddress na2(ip1, 24242);
        TEndpoint ep2(new NAddr::TAddrInfo(&*na2.Begin()));

        TNetworkAddress na3(ip2, 24242);
        TEndpoint ep3(new NAddr::TAddrInfo(&*na3.Begin()));

        TNetworkAddress na4(ip2, 24243);
        TEndpoint ep4(new NAddr::TAddrInfo(&*na4.Begin()));

        UNIT_ASSERT(ep1 == ep2);
        UNIT_ASSERT(!(ep1 == ep3));
        UNIT_ASSERT(!(ep1 == ep4));
    }

    Y_UNIT_TEST(TestIsUnixSocket) {
        TNetworkAddress na1(TUnixSocketPath("/tmp/unixsocket"));
        TEndpoint ep1(new NAddr::TAddrInfo(&*na1.Begin()));

        TNetworkAddress na2("2a02:6b8:0:1410::5f6c:f3c2", 24242);
        TEndpoint ep2(new NAddr::TAddrInfo(&*na2.Begin()));

        UNIT_ASSERT(ep1.IsUnix());
        UNIT_ASSERT(ep1.SockAddr()->sa_family == AF_UNIX);

        UNIT_ASSERT(!ep2.IsUnix());
        UNIT_ASSERT(ep2.SockAddr()->sa_family != AF_UNIX);
    }
}