46 lines
1.7 KiB
C++
46 lines
1.7 KiB
C++
#include "blitz/misc/Test.h"
|
|
|
|
#include "blitz/network/TCPListener.h"
|
|
#include "blitz/network/TCPSocket.h"
|
|
#include "blitz/misc/Random.h"
|
|
|
|
// Test the connection and disconnection of a TCPSocket
|
|
void ConnectDisconnect() {
|
|
blitz::network::TCPListener localserver;
|
|
localserver.Listen(0, 1);
|
|
blitz::network::TCPSocket socket;
|
|
// Try to connect to a local server on port 8080
|
|
blitz_test_assert(socket.Connect("localhost", localserver.GetListeningPort()));
|
|
// Check if the socket status is Connected after connection
|
|
blitz_test_assert(socket.GetStatus() == blitz::network::TCPSocket::Status::Connected);
|
|
socket.Disconnect();
|
|
// Check if the socket status is Disconnected after disconnection
|
|
blitz_test_assert(socket.GetStatus() == blitz::network::TCPSocket::Status::Disconnected);
|
|
}
|
|
|
|
// Test the listening and accepting of a TCPListener
|
|
void Listener() {
|
|
// Choose a random port
|
|
std::uint16_t randomPort = blitz::utils::GetRandomInt(30000, 35000);
|
|
|
|
blitz::network::TCPListener listener;
|
|
// Start listening on random port with a maximum of 10 connections
|
|
blitz_test_assert(listener.Listen(randomPort, 10));
|
|
// Check if the listener is listening on the correct port
|
|
blitz_test_assert(listener.GetListeningPort() == randomPort);
|
|
// Check if the listener has the correct maximum number of connections
|
|
blitz_test_assert(listener.GetMaximumConnections() == 10);
|
|
|
|
blitz::network::TCPSocket socket;
|
|
// Try to connect to the listener
|
|
blitz_test_assert(socket.Connect("localhost", randomPort));
|
|
blitz::network::TCPSocket newSocket;
|
|
// Try to accept the connection from the socket
|
|
blitz_test_assert(listener.Accept(newSocket));
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
ConnectDisconnect();
|
|
Listener();
|
|
return BLITZ_TEST_SUCCESSFUL;
|
|
} |