aboutsummaryrefslogtreecommitdiffstats
path: root/util/network/sock_ut.cpp
blob: fd8c78374787becef8acafc10371c645e4fff8a1 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "sock.h"

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

#include <util/system/fs.h>

Y_UNIT_TEST_SUITE(TSocketTest) {
    Y_UNIT_TEST(InetDgramTest) {
        char buf[256];
        TSockAddrInetDgram servAddr(IpFromString("127.0.0.1"), 0);
        TSockAddrInetDgram cliAddr(IpFromString("127.0.0.1"), 0);
        TSockAddrInetDgram servFromAddr;
        TSockAddrInetDgram cliFromAddr;
        TInetDgramSocket cliSock;
        TInetDgramSocket servSock;
        cliSock.CheckSock();
        servSock.CheckSock();

        TBaseSocket::Check(cliSock.Bind(&cliAddr));
        TBaseSocket::Check(servSock.Bind(&servAddr));

        // client
        const char reqStr[] = "Hello, world!!!";
        TBaseSocket::Check(cliSock.SendTo(reqStr, sizeof(reqStr), &servAddr));

        // server
        TBaseSocket::Check(servSock.RecvFrom(buf, 256, &servFromAddr));
        UNIT_ASSERT(strcmp(reqStr, buf) == 0);
        const char repStr[] = "The World's greatings to you";
        TBaseSocket::Check(servSock.SendTo(repStr, sizeof(repStr), &servFromAddr));

        // client
        TBaseSocket::Check(cliSock.RecvFrom(buf, 256, &cliFromAddr));
        UNIT_ASSERT(strcmp(repStr, buf) == 0);
    }

    void RunLocalDgramTest(const char* localServerSockName, const char* localClientSockName) {
        char buf[256];
        TSockAddrLocalDgram servAddr(localServerSockName);
        TSockAddrLocalDgram cliAddr(localClientSockName);
        TSockAddrLocalDgram servFromAddr;
        TSockAddrLocalDgram cliFromAddr;
        TLocalDgramSocket cliSock;
        TLocalDgramSocket servSock;
        cliSock.CheckSock();
        servSock.CheckSock();

        TBaseSocket::Check(cliSock.Bind(&cliAddr), "bind client");
        TBaseSocket::Check(servSock.Bind(&servAddr), "bind server");

        // client
        const char reqStr[] = "Hello, world!!!";
        TBaseSocket::Check(cliSock.SendTo(reqStr, sizeof(reqStr), &servAddr), "send from client");

        // server
        TBaseSocket::Check(servSock.RecvFrom(buf, 256, &servFromAddr), "receive from client");
        UNIT_ASSERT(strcmp(reqStr, buf) == 0);
        const char repStr[] = "The World's greatings to you";
        TBaseSocket::Check(servSock.SendTo(repStr, sizeof(repStr), &servFromAddr), "send to client");

        // client
        TBaseSocket::Check(cliSock.RecvFrom(buf, 256, &cliFromAddr), "receive from server");
        UNIT_ASSERT(strcmp(repStr, buf) == 0);
    }

    Y_UNIT_TEST(LocalDgramTest) {
        const char* localServerSockName = "./serv_sock";
        const char* localClientSockName = "./cli_sock";
        RunLocalDgramTest(localServerSockName, localClientSockName);
        NFs::Remove(localServerSockName);
        NFs::Remove(localClientSockName);
    }

    template <class A, class S>
    void RunInetStreamTest(const char* ip) {
        char buf[256];
        A servAddr(ip, 0);
        A newAddr;
        S cliSock;
        S servSock;
        S newSock;
        cliSock.CheckSock();
        servSock.CheckSock();
        newSock.CheckSock();

        // server
        int yes = 1;
        CheckedSetSockOpt(servSock, SOL_SOCKET, SO_REUSEADDR, yes, "servSock, SO_REUSEADDR");
        TBaseSocket::Check(servSock.Bind(&servAddr), "bind");
        TBaseSocket::Check(servSock.Listen(10), "listen");

        // client
        TBaseSocket::Check(cliSock.Connect(&servAddr), "connect");

        // server
        TBaseSocket::Check(servSock.Accept(&newSock, &newAddr), "accept");

        // client
        const char reqStr[] = "Hello, world!!!";
        TBaseSocket::Check(cliSock.Send(reqStr, sizeof(reqStr)), "send");

        // server - new
        TBaseSocket::Check(newSock.Recv(buf, 256), "recv");
        UNIT_ASSERT(strcmp(reqStr, buf) == 0);
        const char repStr[] = "The World's greatings to you";
        TBaseSocket::Check(newSock.Send(repStr, sizeof(repStr)), "send");

        // client
        TBaseSocket::Check(cliSock.Recv(buf, 256), "recv");
        UNIT_ASSERT(strcmp(repStr, buf) == 0);
    }

    Y_UNIT_TEST(InetStreamTest) {
        RunInetStreamTest<TSockAddrInetStream, TInetStreamSocket>("127.0.0.1");
    }

    Y_UNIT_TEST(Inet6StreamTest) {
        RunInetStreamTest<TSockAddrInet6Stream, TInet6StreamSocket>("::1");
    }

    void RunLocalStreamTest(const char* localServerSockName) {
        char buf[256];
        TSockAddrLocalStream servAddr(localServerSockName);
        TSockAddrLocalStream newAddr;
        TLocalStreamSocket cliSock;
        TLocalStreamSocket servSock;
        TLocalStreamSocket newSock;
        cliSock.CheckSock();
        servSock.CheckSock();
        newSock.CheckSock();

        // server
        TBaseSocket::Check(servSock.Bind(&servAddr), "bind");
        TBaseSocket::Check(servSock.Listen(10), "listen");

        NThreading::TLegacyFuture<void> f([&]() {
            // server
            TBaseSocket::Check(servSock.Accept(&newSock, &newAddr), "accept");
        });

        // client
        TBaseSocket::Check(cliSock.Connect(&servAddr), "connect");

        f.Get();

        // client
        const char reqStr[] = "Hello, world!!!";
        TBaseSocket::Check(cliSock.Send(reqStr, sizeof(reqStr)), "send");

        // server - new
        TBaseSocket::Check(newSock.Recv(buf, 256), "recv");
        UNIT_ASSERT(strcmp(reqStr, buf) == 0);
        const char repStr[] = "The World's greatings to you";
        TBaseSocket::Check(newSock.Send(repStr, sizeof(repStr)), "send");

        // client
        TBaseSocket::Check(cliSock.Recv(buf, 256), "recv");
        UNIT_ASSERT(strcmp(repStr, buf) == 0);
    }

    Y_UNIT_TEST(LocalStreamTest) {
        const char* localServerSockName = "./serv_sock2";
        RunLocalStreamTest(localServerSockName);
        NFs::Remove(localServerSockName);
    }

}