|
| 1 | +/**************************************************************************/ |
| 2 | +/* test_stream_peer_tcp.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/net_socket.h" |
| 34 | +#include "core/io/stream_peer_tcp.h" |
| 35 | +#include "tests/test_macros.h" |
| 36 | + |
| 37 | +namespace TestStreamPeerTCP { |
| 38 | + |
| 39 | +class MockNetSocket : public NetSocket { |
| 40 | + GDSOFTCLASS(MockNetSocket, NetSocket); |
| 41 | + |
| 42 | +public: |
| 43 | + static void make_default(); |
| 44 | + static void cleanup(); |
| 45 | + |
| 46 | + virtual Error open(Family p_family, Type p_type, IP::Type &ip_type) override; |
| 47 | + virtual void close() override; |
| 48 | + virtual Error bind(Address p_addr) override; |
| 49 | + virtual Error listen(int p_max_pending) override; |
| 50 | + virtual Error connect_to_host(Address p_addr) override; |
| 51 | + virtual Error poll(PollType p_type, int timeout) const override; |
| 52 | + virtual Error recv(uint8_t *p_buffer, int p_len, int &r_read) override; |
| 53 | + virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port, bool p_peek) override; |
| 54 | + virtual Error send(const uint8_t *p_buffer, int p_len, int &r_sent) override; |
| 55 | + virtual Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) override; |
| 56 | + virtual Ref<NetSocket> accept(Address &r_addr) override; |
| 57 | + |
| 58 | + virtual bool is_open() const override; |
| 59 | + virtual int get_available_bytes() const override; |
| 60 | + virtual Error get_socket_address(Address *r_ip) const override; |
| 61 | + |
| 62 | + virtual Error set_broadcasting_enabled(bool p_enabled) override; |
| 63 | + virtual void set_blocking_enabled(bool p_enabled) override; |
| 64 | + virtual void set_ipv6_only_enabled(bool p_enabled) override; |
| 65 | + virtual void set_tcp_no_delay_enabled(bool p_enabled) override; |
| 66 | + virtual void set_reuse_address_enabled(bool p_enabled) override; |
| 67 | + virtual Error join_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) override; |
| 68 | + virtual Error leave_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) override; |
| 69 | + |
| 70 | + Address host_addr; |
| 71 | + Address dest_ip; |
| 72 | + bool blocking_enabled = true; |
| 73 | + |
| 74 | + // Helper methods for testing. |
| 75 | + void _set_available_bytes(int p_available_bytes); |
| 76 | + void _set_send_data(uint8_t *p_sent_data); |
| 77 | + void _set_recv_data(uint8_t *p_recv_data); |
| 78 | + |
| 79 | + MockNetSocket(); |
| 80 | + ~MockNetSocket() override; |
| 81 | + |
| 82 | +protected: |
| 83 | + static NetSocket *_create_func(); |
| 84 | + |
| 85 | +private: |
| 86 | + bool _is_open = false; |
| 87 | + int _available_bytes = 0; |
| 88 | + uint8_t *_sent_data = nullptr; |
| 89 | + uint8_t *_recv_data = nullptr; |
| 90 | +}; |
| 91 | + |
| 92 | +NetSocket *MockNetSocket::_create_func() { |
| 93 | + return memnew(MockNetSocket); |
| 94 | +} |
| 95 | + |
| 96 | +void MockNetSocket::_set_available_bytes(int p_available_bytes) { |
| 97 | + _available_bytes = p_available_bytes; |
| 98 | +} |
| 99 | + |
| 100 | +void MockNetSocket::_set_send_data(uint8_t *p_sent_data) { |
| 101 | + _sent_data = p_sent_data; |
| 102 | +} |
| 103 | + |
| 104 | +void MockNetSocket::_set_recv_data(uint8_t *p_recv_data) { |
| 105 | + _recv_data = p_recv_data; |
| 106 | +} |
| 107 | + |
| 108 | +void MockNetSocket::make_default() { |
| 109 | + ERR_FAIL_COND(_create != nullptr); |
| 110 | + |
| 111 | + _create = _create_func; |
| 112 | +} |
| 113 | + |
| 114 | +void MockNetSocket::cleanup() { |
| 115 | + ERR_FAIL_NULL(_create); |
| 116 | + |
| 117 | + _create = nullptr; |
| 118 | +} |
| 119 | + |
| 120 | +Error MockNetSocket::open(Family p_family, Type p_type, IP::Type &ip_type) { |
| 121 | + _is_open = true; |
| 122 | + return OK; |
| 123 | +} |
| 124 | + |
| 125 | +void MockNetSocket::close() { |
| 126 | + _is_open = false; |
| 127 | +} |
| 128 | + |
| 129 | +Error MockNetSocket::bind(Address p_addr) { |
| 130 | + host_addr = p_addr; |
| 131 | + return OK; |
| 132 | +} |
| 133 | + |
| 134 | +Error MockNetSocket::listen(int p_max_pending) { |
| 135 | + return OK; |
| 136 | +} |
| 137 | + |
| 138 | +Error MockNetSocket::connect_to_host(Address p_addr) { |
| 139 | + dest_ip = p_addr; |
| 140 | + return OK; |
| 141 | +} |
| 142 | + |
| 143 | +Error MockNetSocket::poll(PollType p_type, int timeout) const { |
| 144 | + return OK; |
| 145 | +} |
| 146 | + |
| 147 | +Error MockNetSocket::recv(uint8_t *p_buffer, int p_len, int &r_read) { |
| 148 | + // Receives one byte of _recv_data on each invocation. |
| 149 | + p_buffer[0] = _recv_data[0]; |
| 150 | + _recv_data += 1; |
| 151 | + r_read = 1; |
| 152 | + return OK; |
| 153 | +} |
| 154 | + |
| 155 | +Error MockNetSocket::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port, bool p_peek = false) { |
| 156 | + return OK; |
| 157 | +} |
| 158 | + |
| 159 | +Error MockNetSocket::send(const uint8_t *p_buffer, int p_len, int &r_sent) { |
| 160 | + // Sends one byte to _sent_data on each invocation. |
| 161 | + _sent_data[0] = p_buffer[0]; |
| 162 | + _sent_data += 1; |
| 163 | + r_sent = 1; |
| 164 | + return OK; |
| 165 | +} |
| 166 | + |
| 167 | +Error MockNetSocket::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) { |
| 168 | + return OK; |
| 169 | +} |
| 170 | + |
| 171 | +Ref<NetSocket> MockNetSocket::accept(Address &r_ip) { |
| 172 | + return this; |
| 173 | +} |
| 174 | + |
| 175 | +bool MockNetSocket::is_open() const { |
| 176 | + return _is_open; |
| 177 | +} |
| 178 | + |
| 179 | +int MockNetSocket::get_available_bytes() const { |
| 180 | + return _available_bytes; |
| 181 | +} |
| 182 | + |
| 183 | +Error MockNetSocket::get_socket_address(Address *r_ip) const { |
| 184 | + return OK; |
| 185 | +} |
| 186 | + |
| 187 | +Error MockNetSocket::set_broadcasting_enabled(bool p_enabled) { |
| 188 | + return OK; |
| 189 | +} |
| 190 | + |
| 191 | +void MockNetSocket::set_blocking_enabled(bool p_enabled) { |
| 192 | + blocking_enabled = p_enabled; |
| 193 | +} |
| 194 | + |
| 195 | +void MockNetSocket::set_ipv6_only_enabled(bool p_enabled) {} |
| 196 | + |
| 197 | +void MockNetSocket::set_tcp_no_delay_enabled(bool p_enabled) {} |
| 198 | + |
| 199 | +void MockNetSocket::set_reuse_address_enabled(bool p_enabled) {} |
| 200 | + |
| 201 | +Error MockNetSocket::join_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) { |
| 202 | + return OK; |
| 203 | +} |
| 204 | + |
| 205 | +Error MockNetSocket::leave_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) { |
| 206 | + return OK; |
| 207 | +} |
| 208 | + |
| 209 | +MockNetSocket::MockNetSocket() {} |
| 210 | + |
| 211 | +MockNetSocket::~MockNetSocket() {} |
| 212 | + |
| 213 | +TEST_CASE("[StreamPeerTCP] Basics") { |
| 214 | + Ref<MockNetSocket> ns; |
| 215 | + ns.instantiate(); |
| 216 | + Ref<StreamPeerTCP> spt; |
| 217 | + spt.instantiate(); |
| 218 | + NetSocket::Address peer_ip = NetSocket::Address(IPAddress("127.0.1.1"), 5678); |
| 219 | + spt->accept_socket(ns, peer_ip); |
| 220 | + REQUIRE(ns->blocking_enabled == false); |
| 221 | + |
| 222 | + SUBCASE("Invalid port numbers returns an Error") { |
| 223 | + ERR_PRINT_OFF; |
| 224 | + Error negative_ret = spt->bind(-901, IPAddress("127.0.2.1")); |
| 225 | + REQUIRE(negative_ret != OK); |
| 226 | + |
| 227 | + Error too_high_ret = spt->bind(70000, IPAddress("127.0.3.1")); |
| 228 | + REQUIRE(too_high_ret != OK); |
| 229 | + ERR_PRINT_ON; |
| 230 | + } |
| 231 | + |
| 232 | + SUBCASE("Invoking bind calls opens and binds NetSocket") { |
| 233 | + int bind_port = 7890; |
| 234 | + IPAddress bind_ip = IPAddress("127.0.4.1"); |
| 235 | + Error bind_ret = spt->bind(bind_port, bind_ip); |
| 236 | + REQUIRE(bind_ret == OK); |
| 237 | + REQUIRE(ns->is_open() == true); |
| 238 | + REQUIRE(ns->host_addr.ip() == bind_ip); |
| 239 | + REQUIRE(ns->host_addr.port() == bind_port); |
| 240 | + } |
| 241 | + |
| 242 | + SUBCASE("Invoking disconnect_from_host closes NetSocket") { |
| 243 | + spt->disconnect_from_host(); |
| 244 | + REQUIRE(ns->is_open() == false); |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +TEST_CASE("[StreamPeerTCP] Poll") { |
| 249 | + Ref<MockNetSocket> ns; |
| 250 | + ns.instantiate(); |
| 251 | + Ref<StreamPeerTCP> spt; |
| 252 | + spt.instantiate(); |
| 253 | + IPAddress peer_ip = IPAddress("127.2.2.2"); |
| 254 | + int peer_port = 45878; |
| 255 | + spt->accept_socket(ns, NetSocket::Address(peer_ip, peer_port)); |
| 256 | + IPAddress bind_ip = IPAddress("127.0.0.1"); |
| 257 | + int bind_port = 9043; |
| 258 | + Error bind_ret = spt->bind(bind_port, bind_ip); |
| 259 | + REQUIRE(bind_ret == OK); |
| 260 | + ns->_set_available_bytes(100); |
| 261 | + |
| 262 | + SUBCASE("Unconnected status causes connect_to_host") { |
| 263 | + Error poll_ret = spt->poll(); |
| 264 | + REQUIRE(poll_ret == OK); |
| 265 | + REQUIRE(spt->get_status() == StreamPeerTCP::STATUS_CONNECTED); |
| 266 | + REQUIRE(ns->is_open() == true); |
| 267 | + } |
| 268 | + |
| 269 | + SUBCASE("FIN causes disconnect_from_host") { |
| 270 | + // This is the condition for FIN. |
| 271 | + ns->_set_available_bytes(0); |
| 272 | + Error fin_ret = spt->poll(); |
| 273 | + REQUIRE(fin_ret == OK); |
| 274 | + REQUIRE(ns->is_open() == false); |
| 275 | + } |
| 276 | +} |
| 277 | + |
| 278 | +TEST_CASE("[StreamPeerTCP] Data") { |
| 279 | + Ref<MockNetSocket> ns; |
| 280 | + ns.instantiate(); |
| 281 | + Ref<StreamPeerTCP> spt; |
| 282 | + spt.instantiate(); |
| 283 | + IPAddress peer_ip = IPAddress("127.5.4.3"); |
| 284 | + int peer_port = 8908; |
| 285 | + spt->accept_socket(ns, NetSocket::Address(peer_ip, peer_port)); |
| 286 | + IPAddress bind_ip = IPAddress("127.0.0.1"); |
| 287 | + int bind_port = 2039; |
| 288 | + Error bind_ret = spt->bind(bind_port, bind_ip); |
| 289 | + REQUIRE(bind_ret == OK); |
| 290 | + |
| 291 | + SUBCASE("Invoking put_data on an ascii-encoded stream") { |
| 292 | + const String expected = "hello, world"; |
| 293 | + Vector<uint8_t> send_data; |
| 294 | + send_data.resize_initialized(expected.length()); |
| 295 | + ns->_set_send_data(send_data.ptrw()); |
| 296 | + REQUIRE_EQ(spt->put_data(expected.to_ascii_buffer().ptrw(), expected.length()), Error::OK); |
| 297 | + CHECK_EQ(send_data, expected.to_ascii_buffer()); |
| 298 | + } |
| 299 | + |
| 300 | + SUBCASE("Invoking get_data on an ascii-encoded stream") { |
| 301 | + const String expected = "I - too - say hello!"; |
| 302 | + Vector<uint8_t> recv_data = expected.to_ascii_buffer(); |
| 303 | + ns->_set_recv_data(recv_data.ptrw()); |
| 304 | + Vector<uint8_t> in_data; |
| 305 | + in_data.resize(expected.length()); |
| 306 | + REQUIRE_EQ(spt->get_data(in_data.ptrw(), expected.length()), Error::OK); |
| 307 | + CHECK_EQ(in_data, expected.to_ascii_buffer()); |
| 308 | + } |
| 309 | +} |
| 310 | + |
| 311 | +} // namespace TestStreamPeerTCP |
0 commit comments