aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/network_ut.cpp
blob: f1798419db281671b8681fc6c86c79847dc84dfb (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
#include <library/cpp/testing/unittest/registar.h>

#include "network.h"

#include <library/cpp/messagebus/test/helper/fixed_port.h>

using namespace NBus;
using namespace NBus::NPrivate;
using namespace NBus::NTest;

namespace {
    int GetSockPort(SOCKET socket) {
        sockaddr_storage addr;
        Zero(addr);

        socklen_t len = sizeof(addr);

        int r = ::getsockname(socket, (sockaddr*)&addr, &len);
        UNIT_ASSERT(r >= 0);

        if (addr.ss_family == AF_INET) {
            sockaddr_in* addr_in = (sockaddr_in*)&addr;
            return InetToHost(addr_in->sin_port);
        } else if (addr.ss_family == AF_INET6) {
            sockaddr_in6* addr_in6 = (sockaddr_in6*)&addr;
            return InetToHost(addr_in6->sin6_port);
        } else {
            UNIT_FAIL("unknown AF");
            throw 1;
        }
    }
}

Y_UNIT_TEST_SUITE(Network) {
    Y_UNIT_TEST(BindOnPortConcrete) {
        if (!IsFixedPortTestAllowed()) {
            return;
        }

        TVector<TBindResult> r = BindOnPort(FixedPort, false).second;
        UNIT_ASSERT_VALUES_EQUAL(size_t(2), r.size());

        for (TVector<TBindResult>::iterator i = r.begin(); i != r.end(); ++i) {
            UNIT_ASSERT_VALUES_EQUAL(i->Addr.GetPort(), GetSockPort(i->Socket->operator SOCKET()));
        }
    }

    Y_UNIT_TEST(BindOnPortRandom) {
        TVector<TBindResult> r = BindOnPort(0, false).second;
        UNIT_ASSERT_VALUES_EQUAL(size_t(2), r.size());

        for (TVector<TBindResult>::iterator i = r.begin(); i != r.end(); ++i) {
            UNIT_ASSERT_VALUES_EQUAL(i->Addr.GetPort(), GetSockPort(i->Socket->operator SOCKET()));
            UNIT_ASSERT(i->Addr.GetPort() > 0);
        }

        UNIT_ASSERT_VALUES_EQUAL(r.at(0).Addr.GetPort(), r.at(1).Addr.GetPort());
    }

    Y_UNIT_TEST(BindOnBusyPort) {
        auto r = BindOnPort(0, false);

        UNIT_ASSERT_EXCEPTION_CONTAINS(BindOnPort(r.first, false), TSystemError, "failed to bind on port " + ToString(r.first));
    }
}