1+ /*
2+ ** EPITECH PROJECT, 2022
3+ ** SimpleHandshake.cpp
4+ ** File description:
5+ ** SimpleHandshake.cpp
6+ */
7+
8+ #include < gtest/gtest.h>
9+ #include < thread>
10+ #include " polymorph/network/tcp/Server.hpp"
11+ #include " polymorph/network/tcp/Client.hpp"
12+ #include " ../utils.hpp"
13+ #include " polymorph/network/dto/DisconnectionDto.hpp"
14+
15+ TEST (tcpE2E, ServerPrematureDisconnect)
16+ {
17+ // checks
18+ std::uint16_t input_data = 42 ;
19+ std::uint16_t output_data = 0 ;
20+ std::atomic<bool > receivedDisconnect (false );
21+
22+ using namespace polymorph ::network;
23+ using namespace polymorph ::network::tcp;
24+
25+ std::unique_ptr<Client> client;
26+
27+ {
28+ // Server Setup
29+ auto server = Server::create (4242 );
30+ server->start ();
31+ server->registerReceiveHandler <std::uint16_t >(10 , [&output_data](const PacketHeader &, uint16_t payload) {
32+ output_data = payload;
33+ return true ;
34+ });
35+ server->registerReceiveHandler <DisconnectionDto>(DisconnectionDto::opId,
36+ [&receivedDisconnect](const PacketHeader &,
37+ const DisconnectionDto &) {
38+ receivedDisconnect = true ;
39+ return true ;
40+ });
41+
42+ // Client Setup
43+ client = Client::create (" 127.0.0.1" , 4242 );
44+
45+ // Client Infos
46+ SessionId id;
47+ bool connected = false ;
48+
49+ client->connect ([&id, &connected](bool authorized, SessionId sId ) {
50+ connected = authorized;
51+ id = sId ;
52+ });
53+
54+ PNL_WAIT_COND_LOOP (!connected, PNL_TIME_OUT , 5 )
55+ ASSERT_TRUE (connected);
56+ }
57+
58+ client->send (10 , input_data);
59+ PNL_WAIT (PNL_TIME_OUT )
60+ ASSERT_NE (input_data, output_data);
61+
62+ std::this_thread::sleep_for (std::chrono::milliseconds (500 ));
63+ }
64+
65+
66+ #ifdef PNL_CLIENT_TEST
67+ #if PNL_CLIENT_TEST == 1
68+ TEST (tcpE2E, SafetyClientSend)
69+ {
70+ // checks
71+ std::uint32_t input_data = 4294967295 ;
72+ std::uint32_t output_data = 0 ;
73+
74+ using namespace polymorph ::network;
75+ using namespace polymorph ::network::tcp;
76+
77+ // Client Setup
78+ SessionStore serverStore;
79+ Client client (" 127.0.0.1" , 4242 , serverStore);
80+
81+ // Client Infos
82+ SessionId id;
83+ bool connected = false ;
84+
85+ client.connect ([&id, &connected](bool authorized, SessionId sId ) {
86+ connected = authorized;
87+ id = sId ;
88+ });
89+
90+ PNL_WAIT_COND_LOOP (!connected, PNL_TIME_OUT , 5 )
91+ ASSERT_TRUE (connected);
92+ client.send (2 , input_data);
93+ PNL_WAIT_COND (input_data != output_data, PNL_TIME_OUT )
94+ ASSERT_EQ (input_data, output_data);
95+ }
96+ #endif
97+ #endif
0 commit comments