55#include < catch2/catch_test_macros.hpp>
66#include < set>
77#include < unordered_set>
8+ #include < thread>
89
910TEST_CASE (" Ensure addr works" , " [sock_addr]" ) {
1011 netkit::sock::addr addr (" google.com" , 443 , netkit::sock::addr_type::hostname);
@@ -128,4 +129,47 @@ TEST_CASE("Ensure general utility functions work", "[utility]") {
128129 REQUIRE (tokens[4 ] == " five" );
129130 std::string joined = netkit::utility::join (tokens, " ," );
130131 REQUIRE (joined == to_split);
132+ }
133+
134+ TEST_CASE (" Ensure basic sockets work" , " [socket]" ) {
135+ std::thread t ([]() {
136+ netkit::sock::addr addr (" 127.0.0.1" , 1337 , netkit::sock::addr_type::ipv4);
137+ netkit::sock::sync_sock sock (addr, netkit::sock::type::tcp);
138+
139+ sock.bind ();
140+ sock.listen ();
141+
142+ while (true ) {
143+ auto result = sock.recv (-1 );
144+ if (result.status != netkit::sock::recv_status::success) {
145+ return ;
146+ }
147+
148+ if (result.status == netkit::sock::recv_status::success) {
149+ std::string data = result.data ;
150+ REQUIRE (data == " Hello, World!" );
151+ sock.send (" Hello, World!" );
152+ }
153+ }
154+
155+ sock.unbind ();
156+ });
157+ t.detach ();
158+
159+ std::this_thread::sleep_for (std::chrono::milliseconds (1000 ));
160+
161+ netkit::sock::addr addr (" 127.0.0.1" , 1337 , netkit::sock::addr_type::ipv4);
162+ netkit::sock::sync_sock sock (addr, netkit::sock::type::tcp);
163+
164+ sock.connect ();
165+ sock.send (" Hello, World!" );
166+ auto result = sock.recv (-1 );
167+ if (result.status == netkit::sock::recv_status::success) {
168+ std::string data = result.data ;
169+ REQUIRE (data == " Hello, World!" );
170+ } else {
171+ REQUIRE (false ); // fail the test if we didn't receive a response
172+ }
173+
174+ t.join ();
131175}
0 commit comments