Debian Codegraph @

tests/core/io/test_udp_server.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_udp_server.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/io/packet_peer_udp.h"
34 #include "core/io/udp_server.h"
35 #include "tests/test_macros.h"
36
37 namespace TestUDPServer {
38
39 const int PORT = 12345;
40 const IPAddress LOCALHOST("127.0.0.1");
41 const uint32_t SLEEP_DURATION = 1000;
42 const uint64_t MAX_WAIT_USEC = 100000;
43
44 void wait_for_condition(std::function<bool()> f_test) {
45 const uint64_t time = OS::get_singleton()->get_ticks_usec();
46 while (!f_test() && (OS::get_singleton()->get_ticks_usec() - time) < MAX_WAIT_USEC) {
47 OS::get_singleton()->delay_usec(SLEEP_DURATION);
48 }
49 }
50
51 Ref<UDPServer> create_server(const IPAddress &p_address, int p_port) {
52 Ref<UDPServer> server;
53 server.instantiate();
54
55 Error err = server->listen(PORT, LOCALHOST);
56 REQUIRE_EQ(Error::OK, err);
57 REQUIRE(server->is_listening());
58 CHECK_FALSE(server->is_connection_available());
59 CHECK_EQ(server->get_max_pending_connections(), 16);
60
61 return server;
62 }
63
64 Ref<PacketPeerUDP> create_client(const IPAddress &p_address, int p_port) {
65 Ref<PacketPeerUDP> client;
66 client.instantiate();
67
68 Error err = client->connect_to_host(LOCALHOST, PORT);
69 REQUIRE_EQ(Error::OK, err);
70 CHECK(client->is_bound());
71 CHECK(client->is_socket_connected());
72
73 return client;
74 }
75
76 Ref<PacketPeerUDP> accept_connection(Ref<UDPServer> &p_server) {
77 wait_for_condition([&]() {
78 return p_server->poll() != Error::OK || p_server->is_connection_available();
79 });
80
81 CHECK_EQ(p_server->poll(), Error::OK);
82 REQUIRE(p_server->is_connection_available());
83 Ref<PacketPeerUDP> client_from_server = p_server->take_connection();
84 REQUIRE(client_from_server.is_valid());
85 CHECK(client_from_server->is_bound());
86 CHECK(client_from_server->is_socket_connected());
87
88 return client_from_server;
89 }
90
91 TEST_CASE("[UDPServer] Instantiation") {
92 Ref<UDPServer> server;
93 server.instantiate();
94
95 REQUIRE(server.is_valid());
96 CHECK_EQ(false, server->is_listening());
97 }
98
99 TEST_CASE("[UDPServer] Accept a connection and receive/send data") {
100 Ref<UDPServer> server = create_server(LOCALHOST, PORT);
101 Ref<PacketPeerUDP> client = create_client(LOCALHOST, PORT);
102
103 // Sending data from client to server.
104 const String hello_world = "Hello World!";
105 CHECK_EQ(client->put_var(hello_world), Error::OK);
106
107 Variant hello_world_received;
108 Ref<PacketPeerUDP> client_from_server = accept_connection(server);
109 CHECK_EQ(client_from_server->get_var(hello_world_received), Error::OK);
110 CHECK_EQ(String(hello_world_received), hello_world);
111
112 // Sending data from server to client.
113 const Variant pi = 3.1415;
114 CHECK_EQ(client_from_server->put_var(pi), Error::OK);
115
116 wait_for_condition([&]() {
117 return client->get_available_packet_count() > 0;
118 });
119
120 CHECK_GT(client->get_available_packet_count(), 0);
121
122 Variant pi_received;
123 CHECK_EQ(client->get_var(pi_received), Error::OK);
124 CHECK_EQ(pi_received, pi);
125
126 client->close();
127 server->stop();
128 CHECK_FALSE(server->is_listening());
129 }
130
131 TEST_CASE("[UDPServer] Handle multiple clients at the same time") {
132 Ref<UDPServer> server = create_server(LOCALHOST, PORT);
133
134 Vector<Ref<PacketPeerUDP>> clients;
135 for (int i = 0; i < 5; i++) {
136 Ref<PacketPeerUDP> c = create_client(LOCALHOST, PORT);
137
138 // Sending data from client to server.
139 const String hello_client = itos(i);
140 CHECK_EQ(c->put_var(hello_client), Error::OK);
141
142 clients.push_back(c);
143 }
144
145 Array packets;
146 for (int i = 0; i < clients.size(); i++) {
147 Ref<PacketPeerUDP> cfs = accept_connection(server);
148
149 Variant received_var;
150 CHECK_EQ(cfs->get_var(received_var), Error::OK);
151 CHECK_EQ(received_var.get_type(), Variant::STRING);
152 packets.push_back(received_var);
153
154 // Sending data from server to client.
155 const float sent_float = 3.1415 + received_var.operator String().to_float();
156 CHECK_EQ(cfs->put_var(sent_float), Error::OK);
157 }
158
159 CHECK_EQ(packets.size(), clients.size());
160
161 packets.sort();
162 for (int i = 0; i < clients.size(); i++) {
163 CHECK_EQ(packets[i].operator String(), itos(i));
164 }
165
166 wait_for_condition([&]() {
167 bool should_exit = true;
168 for (Ref<PacketPeerUDP> &c : clients) {
169 int count = c->get_available_packet_count();
170 if (count < 0) {
171 return true;
172 }
173 if (count == 0) {
174 should_exit = false;
175 }
176 }
177 return should_exit;
178 });
179
180 for (int i = 0; i < clients.size(); i++) {
181 CHECK_GT(clients[i]->get_available_packet_count(), 0);
182
183 Variant received_var;
184 const float expected = 3.1415 + i;
185 CHECK_EQ(clients[i]->get_var(received_var), Error::OK);
186 CHECK_EQ(received_var.get_type(), Variant::FLOAT);
187 CHECK_EQ(received_var.operator float(), expected);
188 }
189
190 for (Ref<PacketPeerUDP> &c : clients) {
191 c->close();
192 }
193 server->stop();
194 }
195
196 TEST_CASE("[UDPServer] Should not accept new connections after stop") {
197 Ref<UDPServer> server = create_server(LOCALHOST, PORT);
198 Ref<PacketPeerUDP> client = create_client(LOCALHOST, PORT);
199
200 // Sending data from client to server.
201 const String hello_world = "Hello World!";
202 CHECK_EQ(client->put_var(hello_world), Error::OK);
203
204 wait_for_condition([&]() {
205 return server->poll() != Error::OK || server->is_connection_available();
206 });
207
208 REQUIRE(server->is_connection_available());
209
210 server->stop();
211
212 CHECK_FALSE(server->is_listening());
213 CHECK_FALSE(server->is_connection_available());
214 }
215
216 } // namespace TestUDPServer